{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PatternGuards #-}
{-# LANGUAGE DeriveDataTypeable, DeriveGeneric #-}
module Statistics.Distribution.Binomial
(
BinomialDistribution
, binomial
, binomialE
, bdTrials
, bdProbability
) 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 (choose,logChoose,incompleteBeta,log1p)
import Numeric.MathFunctions.Constants (m_epsilon,m_tiny)
import qualified Statistics.Distribution as D
import qualified Statistics.Distribution.Poisson.Internal as I
import Statistics.Internal
data BinomialDistribution = BD {
BinomialDistribution -> Int
bdTrials :: {-# UNPACK #-} !Int
, BinomialDistribution -> Double
bdProbability :: {-# UNPACK #-} !Double
} deriving (BinomialDistribution -> BinomialDistribution -> Bool
(BinomialDistribution -> BinomialDistribution -> Bool)
-> (BinomialDistribution -> BinomialDistribution -> Bool)
-> Eq BinomialDistribution
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: BinomialDistribution -> BinomialDistribution -> Bool
$c/= :: BinomialDistribution -> BinomialDistribution -> Bool
== :: BinomialDistribution -> BinomialDistribution -> Bool
$c== :: BinomialDistribution -> BinomialDistribution -> Bool
Eq, Typeable, Typeable BinomialDistribution
DataType
Constr
Typeable BinomialDistribution
-> (forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> BinomialDistribution
-> c BinomialDistribution)
-> (forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c BinomialDistribution)
-> (BinomialDistribution -> Constr)
-> (BinomialDistribution -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c BinomialDistribution))
-> (forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c BinomialDistribution))
-> ((forall b. Data b => b -> b)
-> BinomialDistribution -> BinomialDistribution)
-> (forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> BinomialDistribution -> r)
-> (forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> BinomialDistribution -> r)
-> (forall u.
(forall d. Data d => d -> u) -> BinomialDistribution -> [u])
-> (forall u.
Int -> (forall d. Data d => d -> u) -> BinomialDistribution -> u)
-> (forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> BinomialDistribution -> m BinomialDistribution)
-> (forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> BinomialDistribution -> m BinomialDistribution)
-> (forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> BinomialDistribution -> m BinomialDistribution)
-> Data BinomialDistribution
BinomialDistribution -> DataType
BinomialDistribution -> Constr
(forall b. Data b => b -> b)
-> BinomialDistribution -> BinomialDistribution
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> BinomialDistribution
-> c BinomialDistribution
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c BinomialDistribution
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) -> BinomialDistribution -> u
forall u.
(forall d. Data d => d -> u) -> BinomialDistribution -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> BinomialDistribution -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> BinomialDistribution -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> BinomialDistribution -> m BinomialDistribution
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> BinomialDistribution -> m BinomialDistribution
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c BinomialDistribution
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> BinomialDistribution
-> c BinomialDistribution
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c BinomialDistribution)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c BinomialDistribution)
$cBD :: Constr
$tBinomialDistribution :: DataType
gmapMo :: (forall d. Data d => d -> m d)
-> BinomialDistribution -> m BinomialDistribution
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> BinomialDistribution -> m BinomialDistribution
gmapMp :: (forall d. Data d => d -> m d)
-> BinomialDistribution -> m BinomialDistribution
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> BinomialDistribution -> m BinomialDistribution
gmapM :: (forall d. Data d => d -> m d)
-> BinomialDistribution -> m BinomialDistribution
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> BinomialDistribution -> m BinomialDistribution
gmapQi :: Int -> (forall d. Data d => d -> u) -> BinomialDistribution -> u
$cgmapQi :: forall u.
Int -> (forall d. Data d => d -> u) -> BinomialDistribution -> u
gmapQ :: (forall d. Data d => d -> u) -> BinomialDistribution -> [u]
$cgmapQ :: forall u.
(forall d. Data d => d -> u) -> BinomialDistribution -> [u]
gmapQr :: (r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> BinomialDistribution -> r
$cgmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> BinomialDistribution -> r
gmapQl :: (r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> BinomialDistribution -> r
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> BinomialDistribution -> r
gmapT :: (forall b. Data b => b -> b)
-> BinomialDistribution -> BinomialDistribution
$cgmapT :: (forall b. Data b => b -> b)
-> BinomialDistribution -> BinomialDistribution
dataCast2 :: (forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c BinomialDistribution)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c BinomialDistribution)
dataCast1 :: (forall d. Data d => c (t d)) -> Maybe (c BinomialDistribution)
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c BinomialDistribution)
dataTypeOf :: BinomialDistribution -> DataType
$cdataTypeOf :: BinomialDistribution -> DataType
toConstr :: BinomialDistribution -> Constr
$ctoConstr :: BinomialDistribution -> Constr
gunfold :: (forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c BinomialDistribution
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c BinomialDistribution
gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> BinomialDistribution
-> c BinomialDistribution
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> BinomialDistribution
-> c BinomialDistribution
$cp1Data :: Typeable BinomialDistribution
Data, (forall x. BinomialDistribution -> Rep BinomialDistribution x)
-> (forall x. Rep BinomialDistribution x -> BinomialDistribution)
-> Generic BinomialDistribution
forall x. Rep BinomialDistribution x -> BinomialDistribution
forall x. BinomialDistribution -> Rep BinomialDistribution x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep BinomialDistribution x -> BinomialDistribution
$cfrom :: forall x. BinomialDistribution -> Rep BinomialDistribution x
Generic)
instance Show BinomialDistribution where
showsPrec :: Int -> BinomialDistribution -> ShowS
showsPrec Int
i (BD Int
n Double
p) = String -> Int -> Double -> Int -> ShowS
forall a b. (Show a, Show b) => String -> a -> b -> Int -> ShowS
defaultShow2 String
"binomial" Int
n Double
p Int
i
instance Read BinomialDistribution where
readPrec :: ReadPrec BinomialDistribution
readPrec = String
-> (Int -> Double -> Maybe BinomialDistribution)
-> ReadPrec BinomialDistribution
forall a b r.
(Read a, Read b) =>
String -> (a -> b -> Maybe r) -> ReadPrec r
defaultReadPrecM2 String
"binomial" Int -> Double -> Maybe BinomialDistribution
binomialE
instance ToJSON BinomialDistribution
instance FromJSON BinomialDistribution where
parseJSON :: Value -> Parser BinomialDistribution
parseJSON (Object Object
v) = do
Int
n <- Object
v Object -> Key -> Parser Int
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"bdTrials"
Double
p <- Object
v Object -> Key -> Parser Double
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"bdProbability"
Parser BinomialDistribution
-> (BinomialDistribution -> Parser BinomialDistribution)
-> Maybe BinomialDistribution
-> Parser BinomialDistribution
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (String -> Parser BinomialDistribution
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Parser BinomialDistribution)
-> String -> Parser BinomialDistribution
forall a b. (a -> b) -> a -> b
$ Int -> Double -> String
errMsg Int
n Double
p) BinomialDistribution -> Parser BinomialDistribution
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe BinomialDistribution -> Parser BinomialDistribution)
-> Maybe BinomialDistribution -> Parser BinomialDistribution
forall a b. (a -> b) -> a -> b
$ Int -> Double -> Maybe BinomialDistribution
binomialE Int
n Double
p
parseJSON Value
_ = Parser BinomialDistribution
forall (f :: * -> *) a. Alternative f => f a
empty
instance Binary BinomialDistribution where
put :: BinomialDistribution -> Put
put (BD Int
x Double
y) = Int -> Put
forall t. Binary t => t -> Put
put Int
x 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
y
get :: Get BinomialDistribution
get = do
Int
n <- Get Int
forall t. Binary t => Get t
get
Double
p <- Get Double
forall t. Binary t => Get t
get
Get BinomialDistribution
-> (BinomialDistribution -> Get BinomialDistribution)
-> Maybe BinomialDistribution
-> Get BinomialDistribution
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (String -> Get BinomialDistribution
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Get BinomialDistribution)
-> String -> Get BinomialDistribution
forall a b. (a -> b) -> a -> b
$ Int -> Double -> String
errMsg Int
n Double
p) BinomialDistribution -> Get BinomialDistribution
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe BinomialDistribution -> Get BinomialDistribution)
-> Maybe BinomialDistribution -> Get BinomialDistribution
forall a b. (a -> b) -> a -> b
$ Int -> Double -> Maybe BinomialDistribution
binomialE Int
n Double
p
instance D.Distribution BinomialDistribution where
cumulative :: BinomialDistribution -> Double -> Double
cumulative = BinomialDistribution -> Double -> Double
cumulative
instance D.DiscreteDistr BinomialDistribution where
probability :: BinomialDistribution -> Int -> Double
probability = BinomialDistribution -> Int -> Double
probability
logProbability :: BinomialDistribution -> Int -> Double
logProbability = BinomialDistribution -> Int -> Double
logProbability
instance D.Mean BinomialDistribution where
mean :: BinomialDistribution -> Double
mean = BinomialDistribution -> Double
mean
instance D.Variance BinomialDistribution where
variance :: BinomialDistribution -> Double
variance = BinomialDistribution -> Double
variance
instance D.MaybeMean BinomialDistribution where
maybeMean :: BinomialDistribution -> Maybe Double
maybeMean = Double -> Maybe Double
forall a. a -> Maybe a
Just (Double -> Maybe Double)
-> (BinomialDistribution -> Double)
-> BinomialDistribution
-> Maybe Double
forall b c a. (b -> c) -> (a -> b) -> a -> c
. BinomialDistribution -> Double
forall d. Mean d => d -> Double
D.mean
instance D.MaybeVariance BinomialDistribution where
maybeStdDev :: BinomialDistribution -> Maybe Double
maybeStdDev = Double -> Maybe Double
forall a. a -> Maybe a
Just (Double -> Maybe Double)
-> (BinomialDistribution -> Double)
-> BinomialDistribution
-> Maybe Double
forall b c a. (b -> c) -> (a -> b) -> a -> c
. BinomialDistribution -> Double
forall d. Variance d => d -> Double
D.stdDev
maybeVariance :: BinomialDistribution -> Maybe Double
maybeVariance = Double -> Maybe Double
forall a. a -> Maybe a
Just (Double -> Maybe Double)
-> (BinomialDistribution -> Double)
-> BinomialDistribution
-> Maybe Double
forall b c a. (b -> c) -> (a -> b) -> a -> c
. BinomialDistribution -> Double
forall d. Variance d => d -> Double
D.variance
instance D.Entropy BinomialDistribution where
entropy :: BinomialDistribution -> Double
entropy (BD Int
n Double
p)
| Int
n Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 = Double
0
| Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
100 = BinomialDistribution -> Double
directEntropy (Int -> Double -> BinomialDistribution
BD Int
n Double
p)
| Bool
otherwise = Double -> Double
I.poissonEntropy (Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
n Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
p)
instance D.MaybeEntropy BinomialDistribution where
maybeEntropy :: BinomialDistribution -> Maybe Double
maybeEntropy = Double -> Maybe Double
forall a. a -> Maybe a
Just (Double -> Maybe Double)
-> (BinomialDistribution -> Double)
-> BinomialDistribution
-> Maybe Double
forall b c a. (b -> c) -> (a -> b) -> a -> c
. BinomialDistribution -> Double
forall d. Entropy d => d -> Double
D.entropy
probability :: BinomialDistribution -> Int -> Double
probability :: BinomialDistribution -> Int -> Double
probability (BD Int
n Double
p) Int
k
| Int
k Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0 Bool -> Bool -> Bool
|| Int
k Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
n = Double
0
| Int
n Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 = Double
1
| Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
1000
, Double
pK Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
>= Double
m_tiny
, Double
pNK Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
>= Double
m_tiny = Int -> Int -> Double
choose Int
n Int
k Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
pK Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
pNK
| Bool
otherwise = Double -> Double
forall a. Floating a => a -> a
exp (Double -> Double) -> Double -> Double
forall a b. (a -> b) -> a -> b
$ Int -> Int -> Double
logChoose Int
n Int
k Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double -> Double
forall a. Floating a => a -> a
log Double
p Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
k' Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double -> Double
forall a. Floating a => a -> a
log1p (-Double
p) Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
nk'
where
pK :: Double
pK = Double
pDouble -> Int -> Double
forall a b. (Num a, Integral b) => a -> b -> a
^Int
k
pNK :: Double
pNK = (Double
1Double -> Double -> Double
forall a. Num a => a -> a -> a
-Double
p)Double -> Int -> Double
forall a b. (Num a, Integral b) => a -> b -> a
^(Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
k)
k' :: Double
k' = Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
k
nk' :: Double
nk' = Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Double) -> Int -> Double
forall a b. (a -> b) -> a -> b
$ Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
k
logProbability :: BinomialDistribution -> Int -> Double
logProbability :: BinomialDistribution -> Int -> Double
logProbability (BD Int
n Double
p) Int
k
| Int
k Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0 Bool -> Bool -> Bool
|| Int
k Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
n = (-Double
1)Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/Double
0
| Int
n Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 = Double
0
| Bool
otherwise = Int -> Int -> Double
logChoose Int
n Int
k Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double -> Double
forall a. Floating a => a -> a
log Double
p Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
k' Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double -> Double
forall a. Floating a => a -> a
log1p (-Double
p) Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
nk'
where
k' :: Double
k' = Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
k
nk' :: Double
nk' = Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Double) -> Int -> Double
forall a b. (a -> b) -> a -> b
$ Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
k
cumulative :: BinomialDistribution -> Double -> Double
cumulative :: BinomialDistribution -> Double -> Double
cumulative (BD Int
n Double
p) Double
x
| Double -> Bool
forall a. RealFloat a => a -> Bool
isNaN Double
x = String -> Double
forall a. HasCallStack => String -> a
error String
"Statistics.Distribution.Binomial.cumulative: NaN input"
| Double -> Bool
forall a. RealFloat a => a -> Bool
isInfinite Double
x = if Double
x Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
> Double
0 then Double
1 else Double
0
| Int
k Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0 = Double
0
| Int
k Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
n = Double
1
| Bool
otherwise = Double -> Double -> Double -> Double
incompleteBeta (Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
k)) (Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int
kInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1)) (Double
1 Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
p)
where
k :: Int
k = Double -> Int
forall a b. (RealFrac a, Integral b) => a -> b
floor Double
x
mean :: BinomialDistribution -> Double
mean :: BinomialDistribution -> Double
mean (BD Int
n Double
p) = Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
n Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
p
variance :: BinomialDistribution -> Double
variance :: BinomialDistribution -> Double
variance (BD Int
n Double
p) = Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
n Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
p Double -> Double -> Double
forall a. Num a => a -> a -> a
* (Double
1 Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
p)
directEntropy :: BinomialDistribution -> Double
directEntropy :: BinomialDistribution -> Double
directEntropy d :: BinomialDistribution
d@(BD Int
n Double
_) =
Double -> Double
forall a. Num a => a -> a
negate (Double -> Double) -> ([Double] -> Double) -> [Double] -> Double
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Double] -> Double
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum ([Double] -> Double) -> [Double] -> Double
forall a b. (a -> b) -> a -> b
$
(Double -> Bool) -> [Double] -> [Double]
forall a. (a -> Bool) -> [a] -> [a]
takeWhile (Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
< Double -> Double
forall a. Num a => a -> a
negate Double
m_epsilon) ([Double] -> [Double]) -> [Double] -> [Double]
forall a b. (a -> b) -> a -> b
$
(Double -> Bool) -> [Double] -> [Double]
forall a. (a -> Bool) -> [a] -> [a]
dropWhile (Bool -> Bool
not (Bool -> Bool) -> (Double -> Bool) -> Double -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
< Double -> Double
forall a. Num a => a -> a
negate Double
m_epsilon)) ([Double] -> [Double]) -> [Double] -> [Double]
forall a b. (a -> b) -> a -> b
$
[ let x :: Double
x = BinomialDistribution -> Int -> Double
probability BinomialDistribution
d Int
k in Double
x Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double -> Double
forall a. Floating a => a -> a
log Double
x | Int
k <- [Int
0..Int
n]]
binomial :: Int
-> Double
-> BinomialDistribution
binomial :: Int -> Double -> BinomialDistribution
binomial Int
n Double
p = BinomialDistribution
-> (BinomialDistribution -> BinomialDistribution)
-> Maybe BinomialDistribution
-> BinomialDistribution
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (String -> BinomialDistribution
forall a. HasCallStack => String -> a
error (String -> BinomialDistribution) -> String -> BinomialDistribution
forall a b. (a -> b) -> a -> b
$ Int -> Double -> String
errMsg Int
n Double
p) BinomialDistribution -> BinomialDistribution
forall a. a -> a
id (Maybe BinomialDistribution -> BinomialDistribution)
-> Maybe BinomialDistribution -> BinomialDistribution
forall a b. (a -> b) -> a -> b
$ Int -> Double -> Maybe BinomialDistribution
binomialE Int
n Double
p
binomialE :: Int
-> Double
-> Maybe BinomialDistribution
binomialE :: Int -> Double -> Maybe BinomialDistribution
binomialE Int
n Double
p
| Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0 = Maybe BinomialDistribution
forall a. Maybe a
Nothing
| 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 = BinomialDistribution -> Maybe BinomialDistribution
forall a. a -> Maybe a
Just (Int -> Double -> BinomialDistribution
BD Int
n Double
p)
| Bool
otherwise = Maybe BinomialDistribution
forall a. Maybe a
Nothing
errMsg :: Int -> Double -> String
errMsg :: Int -> Double -> String
errMsg Int
n Double
p
= String
"Statistics.Distribution.Binomial.binomial: n=" String -> ShowS
forall a. [a] -> [a] -> [a]
++ Int -> String
forall a. Show a => a -> String
show Int
n
String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
" p=" String -> ShowS
forall a. [a] -> [a] -> [a]
++ Double -> String
forall a. Show a => a -> String
show Double
p String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
"but n>=0 and p in [0,1]"