    {-# LANGUAGE OverloadedStrings #-}
    {-# 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)

    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
    /= :: 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 i :: Int
    i (BD n :: Int
    n p :: Double
    p) = String -> Int -> Double -> Int -> ShowS
    forall a b. (Show a, Show b) => String -> a -> b -> Int -> ShowS
    defaultShow2 "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 "binomial" Int -> Double -> Maybe BinomialDistribution
    binomialE

    instance ToJSON BinomialDistribution
    instance FromJSON BinomialDistribution where
      parseJSON :: Value -> Parser BinomialDistribution
    parseJSON (Object v :: Object
    v) = do
        Int
    n <- Object
    v Object -> Text -> Parser Int
    forall a. FromJSON a => Object -> Text -> Parser a
    .: "bdTrials"
        Double
    p <- Object
    v Object -> Text -> Parser Double
    forall a. FromJSON a => Object -> Text -> Parser a
    .: "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 _ = Parser BinomialDistribution
    forall (f :: * -> *) a. Alternative f => f a
    empty

    instance Binary BinomialDistribution where
      put :: BinomialDistribution -> Put
    put (BD x :: Int
    x y :: 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 n :: Int
    n p :: Double
    p)
        | Int
    n Int -> Int -> Bool
    forall a. Eq a => a -> a -> Bool
    == 0 = 0
        | Int
    n Int -> Int -> Bool
    forall a. Ord a => a -> a -> Bool
    <= 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 n :: Int
    n p :: Double
    p) k :: Int
    k
      | Int
    k Int -> Int -> Bool
    forall a. Ord a => a -> a -> Bool
    < 0 Bool -> Bool -> Bool
    || Int
    k Int -> Int -> Bool
    forall a. Ord a => a -> a -> Bool
    > Int
    n = 0
      | Int
    n Int -> Int -> Bool
    forall a. Eq a => a -> a -> Bool
    == 0         = 1
        -- choose could overflow Double for n >= 1030 so we switch to
        -- log-domain to calculate probability
      | Int
    n Int -> Int -> Bool
    forall a. Ord a => a -> a -> Bool
    < 1000       = Int -> Int -> Double
    choose Int
    n Int
    k Double -> Double -> Double
    forall a. Num a => a -> a -> a
    * Double
    pDouble -> Int -> Double
    forall a b. (Num a, Integral b) => a -> b -> a
    ^Int
    k Double -> Double -> Double
    forall a. Num a => a -> a -> a
    * (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)
      | 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
        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 n :: Int
    n p :: Double
    p) k :: Int
    k
      | Int
    k Int -> Int -> Bool
    forall a. Ord a => a -> a -> Bool
    < 0 Bool -> Bool -> Bool
    || Int
    k Int -> Int -> Bool
    forall a. Ord a => a -> a -> Bool
    > Int
    n          = (-1)Double -> Double -> Double
    forall a. Fractional a => a -> a -> a
    /0
      | Int
    n Int -> Int -> Bool
    forall a. Eq a => a -> a -> Bool
    == 0                  = 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

    -- Summation from different sides required to reduce roundoff errors
    cumulative :: BinomialDistribution -> Double -> Double
    cumulative :: BinomialDistribution -> Double -> Double
    cumulative (BD n :: Int
    n p :: Double
    p) x :: Double
    x
      | Double -> Bool
    forall a. RealFloat a => a -> Bool
    isNaN Double
    x      = String -> Double
    forall a. HasCallStack => String -> a
    error "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
    > 0 then 1 else 0
      | Int
    k Int -> Int -> Bool
    forall a. Ord a => a -> a -> Bool
    <  0       = 0
      | Int
    k Int -> Int -> Bool
    forall a. Ord a => a -> a -> Bool
    >= Int
    n       = 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
    +1)) (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 n :: Int
    n p :: 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 n :: Int
    n p :: 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
    * (1 Double -> Double -> Double
    forall a. Num a => a -> a -> a
    - Double
    p)

    directEntropy :: BinomialDistribution -> Double
    directEntropy :: BinomialDistribution -> Double
    directEntropy d :: BinomialDistribution
    d@(BD n :: Int
    n _) =
      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 <- [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 n :: Int
    n p :: 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

    -- | 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 n :: Int
    n p :: Double
    p
      | Int
    n Int -> Int -> Bool
    forall a. Ord a => a -> a -> Bool
    < 0            = Maybe BinomialDistribution
    forall a. Maybe a
    Nothing
      | Double
    p Double -> Double -> Bool
    forall a. Ord a => a -> a -> Bool
    >= 0 Bool -> Bool -> Bool
    || Double
    p Double -> Double -> Bool
    forall a. Ord a => a -> a -> Bool
    <= 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 n :: Int
    n p :: Double
    p
      = "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]
    ++ " 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]
    ++ "but n>=0 and p in [0,1]"