{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE DeriveDataTypeable, DeriveGeneric #-}
-----------------------------------------------------------------------------
-- |
-- Module      :  Statistics.Distribution.Beta
-- Copyright   :  (C) 2012 Edward Kmett,
-- License     :  BSD-style (see the file LICENSE)
--
-- Maintainer  :  Edward Kmett <ekmett@gmail.com>
-- Stability   :  provisional
-- Portability :  DeriveDataTypeable
--
----------------------------------------------------------------------------
module Statistics.Distribution.Beta
  ( BetaDistribution
    -- * Constructor
  , betaDistr
  , betaDistrE
  , improperBetaDistr
  , improperBetaDistrE
    -- * Accessors
  , bdAlpha
  , bdBeta
  ) where

import Control.Applicative
import Data.Aeson            (FromJSON(..), ToJSON, Value(..), (.:))
import Data.Binary           (Binary(..))
import Data.Data             (Data, Typeable)
import GHC.Generics          (Generic)
import Numeric.SpecFunctions (
  incompleteBeta, invIncompleteBeta, logBeta, digamma, log1p)
import Numeric.MathFunctions.Constants (m_NaN,m_neg_inf)
import qualified Statistics.Distribution as D
import Statistics.Internal


-- | The beta distribution
data BetaDistribution = BD
 { BetaDistribution -> Double
bdAlpha :: {-# UNPACK #-} !Double
   -- ^ Alpha shape parameter
 , BetaDistribution -> Double
bdBeta  :: {-# UNPACK #-} !Double
   -- ^ Beta shape parameter
 } deriving (BetaDistribution -> BetaDistribution -> Bool
(BetaDistribution -> BetaDistribution -> Bool)
-> (BetaDistribution -> BetaDistribution -> Bool)
-> Eq BetaDistribution
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: BetaDistribution -> BetaDistribution -> Bool
$c/= :: BetaDistribution -> BetaDistribution -> Bool
== :: BetaDistribution -> BetaDistribution -> Bool
$c== :: BetaDistribution -> BetaDistribution -> Bool
Eq, Typeable, Typeable BetaDistribution
DataType
Constr
Typeable BetaDistribution
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> BetaDistribution -> c BetaDistribution)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c BetaDistribution)
-> (BetaDistribution -> Constr)
-> (BetaDistribution -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c BetaDistribution))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e))
    -> Maybe (c BetaDistribution))
-> ((forall b. Data b => b -> b)
    -> BetaDistribution -> BetaDistribution)
-> (forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> BetaDistribution -> r)
-> (forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> BetaDistribution -> r)
-> (forall u.
    (forall d. Data d => d -> u) -> BetaDistribution -> [u])
-> (forall u.
    Int -> (forall d. Data d => d -> u) -> BetaDistribution -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d)
    -> BetaDistribution -> m BetaDistribution)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d)
    -> BetaDistribution -> m BetaDistribution)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d)
    -> BetaDistribution -> m BetaDistribution)
-> Data BetaDistribution
BetaDistribution -> DataType
BetaDistribution -> Constr
(forall b. Data b => b -> b)
-> BetaDistribution -> BetaDistribution
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> BetaDistribution -> c BetaDistribution
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c BetaDistribution
forall a.
Typeable a
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u.
Int -> (forall d. Data d => d -> u) -> BetaDistribution -> u
forall u. (forall d. Data d => d -> u) -> BetaDistribution -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> BetaDistribution -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> BetaDistribution -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> BetaDistribution -> m BetaDistribution
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> BetaDistribution -> m BetaDistribution
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c BetaDistribution
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> BetaDistribution -> c BetaDistribution
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c BetaDistribution)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c BetaDistribution)
$cBD :: Constr
$tBetaDistribution :: DataType
gmapMo :: (forall d. Data d => d -> m d)
-> BetaDistribution -> m BetaDistribution
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> BetaDistribution -> m BetaDistribution
gmapMp :: (forall d. Data d => d -> m d)
-> BetaDistribution -> m BetaDistribution
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> BetaDistribution -> m BetaDistribution
gmapM :: (forall d. Data d => d -> m d)
-> BetaDistribution -> m BetaDistribution
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> BetaDistribution -> m BetaDistribution
gmapQi :: Int -> (forall d. Data d => d -> u) -> BetaDistribution -> u
$cgmapQi :: forall u.
Int -> (forall d. Data d => d -> u) -> BetaDistribution -> u
gmapQ :: (forall d. Data d => d -> u) -> BetaDistribution -> [u]
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> BetaDistribution -> [u]
gmapQr :: (r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> BetaDistribution -> r
$cgmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> BetaDistribution -> r
gmapQl :: (r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> BetaDistribution -> r
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> BetaDistribution -> r
gmapT :: (forall b. Data b => b -> b)
-> BetaDistribution -> BetaDistribution
$cgmapT :: (forall b. Data b => b -> b)
-> BetaDistribution -> BetaDistribution
dataCast2 :: (forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c BetaDistribution)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c BetaDistribution)
dataCast1 :: (forall d. Data d => c (t d)) -> Maybe (c BetaDistribution)
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c BetaDistribution)
dataTypeOf :: BetaDistribution -> DataType
$cdataTypeOf :: BetaDistribution -> DataType
toConstr :: BetaDistribution -> Constr
$ctoConstr :: BetaDistribution -> Constr
gunfold :: (forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c BetaDistribution
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c BetaDistribution
gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> BetaDistribution -> c BetaDistribution
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> BetaDistribution -> c BetaDistribution
$cp1Data :: Typeable BetaDistribution
Data, (forall x. BetaDistribution -> Rep BetaDistribution x)
-> (forall x. Rep BetaDistribution x -> BetaDistribution)
-> Generic BetaDistribution
forall x. Rep BetaDistribution x -> BetaDistribution
forall x. BetaDistribution -> Rep BetaDistribution x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep BetaDistribution x -> BetaDistribution
$cfrom :: forall x. BetaDistribution -> Rep BetaDistribution x
Generic)

instance Show BetaDistribution where
  showsPrec :: Int -> BetaDistribution -> ShowS
showsPrec Int
n (BD Double
a Double
b) = String -> Double -> Double -> Int -> ShowS
forall a b. (Show a, Show b) => String -> a -> b -> Int -> ShowS
defaultShow2 String
"improperBetaDistr" Double
a Double
b Int
n
instance Read BetaDistribution where
  readPrec :: ReadPrec BetaDistribution
readPrec = String
-> (Double -> Double -> Maybe BetaDistribution)
-> ReadPrec BetaDistribution
forall a b r.
(Read a, Read b) =>
String -> (a -> b -> Maybe r) -> ReadPrec r
defaultReadPrecM2 String
"improperBetaDistr" Double -> Double -> Maybe BetaDistribution
improperBetaDistrE

instance ToJSON BetaDistribution
instance FromJSON BetaDistribution where
  parseJSON :: Value -> Parser BetaDistribution
parseJSON (Object Object
v) = do
    Double
a <- Object
v Object -> Key -> Parser Double
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"bdAlpha"
    Double
b <- Object
v Object -> Key -> Parser Double
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"bdBeta"
    Parser BetaDistribution
-> (BetaDistribution -> Parser BetaDistribution)
-> Maybe BetaDistribution
-> Parser BetaDistribution
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (String -> Parser BetaDistribution
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Parser BetaDistribution)
-> String -> Parser BetaDistribution
forall a b. (a -> b) -> a -> b
$ Double -> Double -> String
errMsgI Double
a Double
b) BetaDistribution -> Parser BetaDistribution
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe BetaDistribution -> Parser BetaDistribution)
-> Maybe BetaDistribution -> Parser BetaDistribution
forall a b. (a -> b) -> a -> b
$ Double -> Double -> Maybe BetaDistribution
improperBetaDistrE Double
a Double
b
  parseJSON Value
_ = Parser BetaDistribution
forall (f :: * -> *) a. Alternative f => f a
empty

instance Binary BetaDistribution where
  put :: BetaDistribution -> Put
put (BD Double
a Double
b) = Double -> Put
forall t. Binary t => t -> Put
put Double
a Put -> Put -> Put
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Double -> Put
forall t. Binary t => t -> Put
put Double
b
  get :: Get BetaDistribution
get = do
    Double
a <- Get Double
forall t. Binary t => Get t
get
    Double
b <- Get Double
forall t. Binary t => Get t
get
    Get BetaDistribution
-> (BetaDistribution -> Get BetaDistribution)
-> Maybe BetaDistribution
-> Get BetaDistribution
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (String -> Get BetaDistribution
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Get BetaDistribution) -> String -> Get BetaDistribution
forall a b. (a -> b) -> a -> b
$ Double -> Double -> String
errMsgI Double
a Double
b) BetaDistribution -> Get BetaDistribution
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe BetaDistribution -> Get BetaDistribution)
-> Maybe BetaDistribution -> Get BetaDistribution
forall a b. (a -> b) -> a -> b
$ Double -> Double -> Maybe BetaDistribution
improperBetaDistrE Double
a Double
b


-- | Create beta distribution. Both shape parameters must be positive.
betaDistr :: Double             -- ^ Shape parameter alpha
          -> Double             -- ^ Shape parameter beta
          -> BetaDistribution
betaDistr :: Double -> Double -> BetaDistribution
betaDistr Double
a Double
b = BetaDistribution
-> (BetaDistribution -> BetaDistribution)
-> Maybe BetaDistribution
-> BetaDistribution
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (String -> BetaDistribution
forall a. HasCallStack => String -> a
error (String -> BetaDistribution) -> String -> BetaDistribution
forall a b. (a -> b) -> a -> b
$ Double -> Double -> String
errMsg Double
a Double
b) BetaDistribution -> BetaDistribution
forall a. a -> a
id (Maybe BetaDistribution -> BetaDistribution)
-> Maybe BetaDistribution -> BetaDistribution
forall a b. (a -> b) -> a -> b
$ Double -> Double -> Maybe BetaDistribution
betaDistrE Double
a Double
b

-- | Create beta distribution. Both shape parameters must be positive.
betaDistrE :: Double             -- ^ Shape parameter alpha
          -> Double             -- ^ Shape parameter beta
          -> Maybe BetaDistribution
betaDistrE :: Double -> Double -> Maybe BetaDistribution
betaDistrE Double
a Double
b
  | Double
a Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
> Double
0 Bool -> Bool -> Bool
&& Double
b Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
> Double
0 = BetaDistribution -> Maybe BetaDistribution
forall a. a -> Maybe a
Just (Double -> Double -> BetaDistribution
BD Double
a Double
b)
  | Bool
otherwise      = Maybe BetaDistribution
forall a. Maybe a
Nothing

errMsg :: Double -> Double -> String
errMsg :: Double -> Double -> String
errMsg Double
a Double
b = String
"Statistics.Distribution.Beta.betaDistr: "
          String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
"shape parameters must be positive. Got a = "
          String -> ShowS
forall a. [a] -> [a] -> [a]
++ Double -> String
forall a. Show a => a -> String
show Double
a
          String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
" b = "
          String -> ShowS
forall a. [a] -> [a] -> [a]
++ Double -> String
forall a. Show a => a -> String
show Double
b


-- | Create beta distribution. Both shape parameters must be
-- non-negative. So it allows to construct improper beta distribution
-- which could be used as improper prior.
improperBetaDistr :: Double             -- ^ Shape parameter alpha
                  -> Double             -- ^ Shape parameter beta
                  -> BetaDistribution
improperBetaDistr :: Double -> Double -> BetaDistribution
improperBetaDistr Double
a Double
b
  = BetaDistribution
-> (BetaDistribution -> BetaDistribution)
-> Maybe BetaDistribution
-> BetaDistribution
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (String -> BetaDistribution
forall a. HasCallStack => String -> a
error (String -> BetaDistribution) -> String -> BetaDistribution
forall a b. (a -> b) -> a -> b
$ Double -> Double -> String
errMsgI Double
a Double
b) BetaDistribution -> BetaDistribution
forall a. a -> a
id (Maybe BetaDistribution -> BetaDistribution)
-> Maybe BetaDistribution -> BetaDistribution
forall a b. (a -> b) -> a -> b
$ Double -> Double -> Maybe BetaDistribution
improperBetaDistrE Double
a Double
b

-- | Create beta distribution. Both shape parameters must be
-- non-negative. So it allows to construct improper beta distribution
-- which could be used as improper prior.
improperBetaDistrE :: Double             -- ^ Shape parameter alpha
                   -> Double             -- ^ Shape parameter beta
                   -> Maybe BetaDistribution
improperBetaDistrE :: Double -> Double -> Maybe BetaDistribution
improperBetaDistrE Double
a Double
b
  | Double
a Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
>= Double
0 Bool -> Bool -> Bool
&& Double
b Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
>= Double
0 = BetaDistribution -> Maybe BetaDistribution
forall a. a -> Maybe a
Just (Double -> Double -> BetaDistribution
BD Double
a Double
b)
  | Bool
otherwise        = Maybe BetaDistribution
forall a. Maybe a
Nothing

errMsgI :: Double -> Double -> String
errMsgI :: Double -> Double -> String
errMsgI Double
a Double
b
  =  String
"Statistics.Distribution.Beta.betaDistr: "
  String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
"shape parameters must be non-negative. Got a = " String -> ShowS
forall a. [a] -> [a] -> [a]
++ Double -> String
forall a. Show a => a -> String
show Double
a
  String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
" b = " String -> ShowS
forall a. [a] -> [a] -> [a]
++ Double -> String
forall a. Show a => a -> String
show Double
b



instance D.Distribution BetaDistribution where
  cumulative :: BetaDistribution -> Double -> Double
cumulative (BD Double
a Double
b) Double
x
    | Double
x Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
<= Double
0    = Double
0
    | Double
x Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
>= Double
1    = Double
1
    | Bool
otherwise = Double -> Double -> Double -> Double
incompleteBeta Double
a Double
b Double
x
  complCumulative :: BetaDistribution -> Double -> Double
complCumulative (BD Double
a Double
b) Double
x
    | Double
x Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
<= Double
0    = Double
1
    | Double
x Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
>= Double
1    = Double
0
    -- For small x we use direct computation to avoid precision loss
    -- when computing (1-x)
    | Double
x Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
<  Double
0.5  = Double
1 Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double -> Double -> Double -> Double
incompleteBeta Double
a Double
b Double
x
    -- Otherwise we use property of incomplete beta:
    --  > I(x,a,b) = 1 - I(1-x,b,a)
    | Bool
otherwise = Double -> Double -> Double -> Double
incompleteBeta Double
b Double
a (Double
1Double -> Double -> Double
forall a. Num a => a -> a -> a
-Double
x)

instance D.Mean BetaDistribution where
  mean :: BetaDistribution -> Double
mean (BD Double
a Double
b) = Double
a Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ (Double
a Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
b)

instance D.MaybeMean BetaDistribution where
  maybeMean :: BetaDistribution -> Maybe Double
maybeMean = Double -> Maybe Double
forall a. a -> Maybe a
Just (Double -> Maybe Double)
-> (BetaDistribution -> Double) -> BetaDistribution -> Maybe Double
forall b c a. (b -> c) -> (a -> b) -> a -> c
. BetaDistribution -> Double
forall d. Mean d => d -> Double
D.mean

instance D.Variance BetaDistribution where
  variance :: BetaDistribution -> Double
variance (BD Double
a Double
b) = Double
aDouble -> Double -> Double
forall a. Num a => a -> a -> a
*Double
b Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ (Double
apbDouble -> Double -> Double
forall a. Num a => a -> a -> a
*Double
apbDouble -> Double -> Double
forall a. Num a => a -> a -> a
*(Double
apbDouble -> Double -> Double
forall a. Num a => a -> a -> a
+Double
1))
    where apb :: Double
apb = Double
a Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
b

instance D.MaybeVariance BetaDistribution where
  maybeVariance :: BetaDistribution -> Maybe Double
maybeVariance = Double -> Maybe Double
forall a. a -> Maybe a
Just (Double -> Maybe Double)
-> (BetaDistribution -> Double) -> BetaDistribution -> Maybe Double
forall b c a. (b -> c) -> (a -> b) -> a -> c
. BetaDistribution -> Double
forall d. Variance d => d -> Double
D.variance

instance D.Entropy BetaDistribution where
  entropy :: BetaDistribution -> Double
entropy (BD Double
a Double
b) =
    Double -> Double -> Double
logBeta Double
a Double
b
    Double -> Double -> Double
forall a. Num a => a -> a -> a
- (Double
aDouble -> Double -> Double
forall a. Num a => a -> a -> a
-Double
1) Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double -> Double
digamma Double
a
    Double -> Double -> Double
forall a. Num a => a -> a -> a
- (Double
bDouble -> Double -> Double
forall a. Num a => a -> a -> a
-Double
1) Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double -> Double
digamma Double
b
    Double -> Double -> Double
forall a. Num a => a -> a -> a
+ (Double
aDouble -> Double -> Double
forall a. Num a => a -> a -> a
+Double
bDouble -> Double -> Double
forall a. Num a => a -> a -> a
-Double
2) Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double -> Double
digamma (Double
aDouble -> Double -> Double
forall a. Num a => a -> a -> a
+Double
b)

instance D.MaybeEntropy BetaDistribution where
  maybeEntropy :: BetaDistribution -> Maybe Double
maybeEntropy = Double -> Maybe Double
forall a. a -> Maybe a
Just (Double -> Maybe Double)
-> (BetaDistribution -> Double) -> BetaDistribution -> Maybe Double
forall b c a. (b -> c) -> (a -> b) -> a -> c
. BetaDistribution -> Double
forall d. Entropy d => d -> Double
D.entropy

instance D.ContDistr BetaDistribution where
  density :: BetaDistribution -> Double -> Double
density (BD Double
a Double
b) Double
x
    | Double
a Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
<= Double
0 Bool -> Bool -> Bool
|| Double
b Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
<= Double
0 = Double
m_NaN
    | Double
x Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
<= Double
0 = Double
0
    | Double
x Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
>= Double
1 = Double
0
    | Bool
otherwise = Double -> Double
forall a. Floating a => a -> a
exp (Double -> Double) -> Double -> Double
forall a b. (a -> b) -> a -> b
$ (Double
aDouble -> Double -> Double
forall a. Num a => a -> a -> a
-Double
1)Double -> Double -> Double
forall a. Num a => a -> a -> a
*Double -> Double
forall a. Floating a => a -> a
log Double
x Double -> Double -> Double
forall a. Num a => a -> a -> a
+ (Double
bDouble -> Double -> Double
forall a. Num a => a -> a -> a
-Double
1) Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double -> Double
forall a. Floating a => a -> a
log1p (-Double
x) Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double -> Double -> Double
logBeta Double
a Double
b
  logDensity :: BetaDistribution -> Double -> Double
logDensity (BD Double
a Double
b) Double
x
    | Double
a Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
<= Double
0 Bool -> Bool -> Bool
|| Double
b Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
<= Double
0 = Double
m_NaN
    | Double
x Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
<= Double
0 = Double
m_neg_inf
    | Double
x Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
>= Double
1 = Double
m_neg_inf
    | Bool
otherwise = (Double
aDouble -> Double -> Double
forall a. Num a => a -> a -> a
-Double
1)Double -> Double -> Double
forall a. Num a => a -> a -> a
*Double -> Double
forall a. Floating a => a -> a
log Double
x Double -> Double -> Double
forall a. Num a => a -> a -> a
+ (Double
bDouble -> Double -> Double
forall a. Num a => a -> a -> a
-Double
1)Double -> Double -> Double
forall a. Num a => a -> a -> a
*Double -> Double
forall a. Floating a => a -> a
log1p (-Double
x) Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double -> Double -> Double
logBeta Double
a Double
b

  quantile :: BetaDistribution -> Double -> Double
quantile (BD Double
a Double
b) Double
p
    | Double
p Double -> Double -> Bool
forall a. Eq a => a -> a -> Bool
== Double
0         = Double
0
    | Double
p Double -> Double -> Bool
forall a. Eq a => a -> a -> Bool
== Double
1         = Double
1
    | Double
p Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
> Double
0 Bool -> Bool -> Bool
&& Double
p Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
< Double
1 = Double -> Double -> Double -> Double
invIncompleteBeta Double
a Double
b Double
p
    | Bool
otherwise      =
        String -> Double
forall a. HasCallStack => String -> a
error (String -> Double) -> String -> Double
forall a b. (a -> b) -> a -> b
$ String
"Statistics.Distribution.Gamma.quantile: p must be in [0,1] range. Got: "String -> ShowS
forall a. [a] -> [a] -> [a]
++Double -> String
forall a. Show a => a -> String
show Double
p

instance D.ContGen BetaDistribution where
  genContVar :: BetaDistribution -> g -> m Double
genContVar = BetaDistribution -> g -> m Double
forall d g (m :: * -> *).
(ContDistr d, StatefulGen g m) =>
d -> g -> m Double
D.genContinuous