{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PatternGuards     #-}
{-# LANGUAGE DeriveDataTypeable, DeriveGeneric #-}
-- |
-- Module    : Statistics.Distribution.Binomial
-- Copyright : (c) 2009 Bryan O'Sullivan
-- License   : BSD3
--
-- Maintainer  : bos@serpentine.com
-- Stability   : experimental
-- Portability : portable
--
-- The binomial distribution.  This is the discrete probability
-- distribution of the number of successes in a sequence of /n/
-- independent yes\/no experiments, each of which yields success with
-- probability /p/.

module Statistics.Distribution.Binomial
    (
      BinomialDistribution
    -- * Constructors
    , binomial
    , binomialE
    -- * Accessors
    , 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


-- | The binomial distribution.
data BinomialDistribution = BD {
      BinomialDistribution -> Int
bdTrials      :: {-# UNPACK #-} !Int
    -- ^ Number of trials.
    , BinomialDistribution -> Double
bdProbability :: {-# UNPACK #-} !Double
    -- ^ Probability.
    } deriving (BinomialDistribution -> BinomialDistribution -> Bool
(BinomialDistribution -> BinomialDistribution -> Bool)
-> (BinomialDistribution -> BinomialDistribution -> Bool)
-> Eq BinomialDistribution
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: BinomialDistribution -> BinomialDistribution -> Bool
== :: BinomialDistribution -> BinomialDistribution -> Bool
$c/= :: BinomialDistribution -> BinomialDistribution -> Bool
/= :: BinomialDistribution -> BinomialDistribution -> Bool
Eq, Typeable, Typeable BinomialDistribution
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 -> Constr
BinomialDistribution -> DataType
(forall b. Data b => b -> b)
-> BinomialDistribution -> 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)
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> BinomialDistribution
-> c BinomialDistribution
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> BinomialDistribution
-> c BinomialDistribution
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c BinomialDistribution
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c BinomialDistribution
$ctoConstr :: BinomialDistribution -> Constr
toConstr :: BinomialDistribution -> Constr
$cdataTypeOf :: BinomialDistribution -> DataType
dataTypeOf :: BinomialDistribution -> DataType
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c BinomialDistribution)
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c BinomialDistribution)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c BinomialDistribution)
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c BinomialDistribution)
$cgmapT :: (forall b. Data b => b -> b)
-> BinomialDistribution -> BinomialDistribution
gmapT :: (forall b. Data b => b -> b)
-> BinomialDistribution -> BinomialDistribution
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> BinomialDistribution -> r
gmapQl :: forall r r'.
(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
gmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> BinomialDistribution -> r
$cgmapQ :: forall u.
(forall d. Data d => d -> u) -> BinomialDistribution -> [u]
gmapQ :: forall u.
(forall d. Data d => d -> u) -> BinomialDistribution -> [u]
$cgmapQi :: forall u.
Int -> (forall d. Data d => d -> u) -> BinomialDistribution -> u
gmapQi :: forall u.
Int -> (forall d. Data d => d -> u) -> BinomialDistribution -> u
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> BinomialDistribution -> m BinomialDistribution
gmapM :: forall (m :: * -> *).
Monad m =>
(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
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(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
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> BinomialDistribution -> m 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
$cfrom :: forall x. BinomialDistribution -> Rep BinomialDistribution x
from :: forall x. BinomialDistribution -> Rep BinomialDistribution x
$cto :: forall x. Rep BinomialDistribution x -> BinomialDistribution
to :: forall x. Rep BinomialDistribution x -> BinomialDistribution
Generic)

instance Show BinomialDistribution where
  showsPrec :: Int -> BinomialDistribution -> ShowS
showsPrec Int
i (BD Int
n Double
p) = [Char] -> Int -> Double -> Int -> ShowS
forall a b. (Show a, Show b) => [Char] -> a -> b -> Int -> ShowS
defaultShow2 [Char]
"binomial" Int
n Double
p Int
i
instance Read BinomialDistribution where
  readPrec :: ReadPrec BinomialDistribution
readPrec = [Char]
-> (Int -> Double -> Maybe BinomialDistribution)
-> ReadPrec BinomialDistribution
forall a b r.
(Read a, Read b) =>
[Char] -> (a -> b -> Maybe r) -> ReadPrec r
defaultReadPrecM2 [Char]
"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 ([Char] -> Parser BinomialDistribution
forall a. [Char] -> Parser a
forall (m :: * -> *) a. MonadFail m => [Char] -> m a
fail ([Char] -> Parser BinomialDistribution)
-> [Char] -> Parser BinomialDistribution
forall a b. (a -> b) -> a -> b
$ Int -> Double -> [Char]
errMsg Int
n Double
p) BinomialDistribution -> Parser BinomialDistribution
forall a. a -> Parser a
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 a. Parser a
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 a b. PutM a -> PutM b -> PutM b
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 ([Char] -> Get BinomialDistribution
forall a. [Char] -> Get a
forall (m :: * -> *) a. MonadFail m => [Char] -> m a
fail ([Char] -> Get BinomialDistribution)
-> [Char] -> Get BinomialDistribution
forall a b. (a -> b) -> a -> b
$ Int -> Double -> [Char]
errMsg Int
n Double
p) BinomialDistribution -> Get BinomialDistribution
forall a. a -> Get a
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
    complCumulative :: BinomialDistribution -> Double -> Double
complCumulative = BinomialDistribution -> Double -> Double
complCumulative

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

-- This could be slow for big n
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
    -- choose could overflow Double for n >= 1030 so we switch to
    -- log-domain to calculate probability
    --
    -- We also want to avoid underflow when computing p^k &
    -- (1-p)^(n-k).
  | 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      = [Char] -> Double
forall a. HasCallStack => [Char] -> a
error [Char]
"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 b. Integral b => Double -> b
forall a b. (RealFrac a, Integral b) => a -> b
floor Double
x

complCumulative :: BinomialDistribution -> Double -> Double
complCumulative :: BinomialDistribution -> Double -> Double
complCumulative (BD Int
n Double
p) Double
x
  | Double -> Bool
forall a. RealFloat a => a -> Bool
isNaN Double
x      = [Char] -> Double
forall a. HasCallStack => [Char] -> a
error [Char]
"Statistics.Distribution.Binomial.complCumulative: 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
0 else Double
1
  | Int
k Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<  Int
0       = Double
1
  | Int
k Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
n       = Double
0
  | Bool
otherwise    = Double -> Double -> Double -> Double
incompleteBeta (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)) (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)) Double
p
  where
    k :: Int
k = Double -> Int
forall b. Integral b => Double -> b
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 a. Num a => [a] -> a
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]]

-- | Construct binomial distribution. Number of trials must be
--   non-negative and probability must be in [0,1] range
binomial :: Int                 -- ^ Number of trials.
         -> Double              -- ^ Probability.
         -> 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 ([Char] -> BinomialDistribution
forall a. HasCallStack => [Char] -> a
error ([Char] -> BinomialDistribution) -> [Char] -> BinomialDistribution
forall a b. (a -> b) -> a -> b
$ Int -> Double -> [Char]
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

-- | Construct binomial distribution. Number of trials must be
--   non-negative and probability must be in [0,1] range
binomialE :: Int                 -- ^ Number of trials.
          -> Double              -- ^ Probability.
          -> 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 -> [Char]
errMsg Int
n Double
p
  = [Char]
"Statistics.Distribution.Binomial.binomial: n=" [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ Int -> [Char]
forall a. Show a => a -> [Char]
show Int
n
  [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ [Char]
" p=" [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ Double -> [Char]
forall a. Show a => a -> [Char]
show Double
p [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ [Char]
"but n>=0 and p in [0,1]"