    {-# LANGUAGE MultiParamTypeClasses #-}
    {-# LANGUAGE OverloadedStrings #-}
    {-# LANGUAGE DeriveDataTypeable, DeriveGeneric #-}
    -- |
    -- Module    : Statistics.Distribution.Normal
    -- Copyright : (c) 2009 Bryan O'Sullivan
    -- License   : BSD3
    --
    -- Maintainer  : bos@serpentine.com
    -- Stability   : experimental
    -- Portability : portable
    --
    -- The normal distribution.  This is a continuous probability
    -- distribution that describes data that cluster around a mean.

    module Statistics.Distribution.Normal
        (
          NormalDistribution
        -- * Constructors
        , normalDistr
        , normalDistrE
        , normalDistrErr
        , standard
        ) 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.MathFunctions.Constants (m_sqrt_2, m_sqrt_2_pi)
    import Numeric.SpecFunctions (erfc, invErfc)
    import qualified System.Random.MWC.Distributions as MWC
    import qualified Data.Vector.Generic as G

    import qualified Statistics.Distribution as D
    import qualified Statistics.Sample as S
    import Statistics.Internal

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

    instance Show NormalDistribution where
      showsPrec :: Int -> NormalDistribution -> ShowS
    showsPrec Int
    i (ND Double
    m Double
    s Double
    _ Double
    _) = [Char] -> Double -> Double -> Int -> ShowS
    forall a b. (Show a, Show b) => [Char] -> a -> b -> Int -> ShowS
    defaultShow2 [Char]
    "normalDistr" Double
    m Double
    s Int
    i
    instance Read NormalDistribution where
      readPrec :: ReadPrec NormalDistribution
    readPrec = [Char]
    -> (Double -> Double -> Maybe NormalDistribution)
    -> ReadPrec NormalDistribution
    forall a b r.
    (Read a, Read b) =>
    [Char] -> (a -> b -> Maybe r) -> ReadPrec r
    defaultReadPrecM2 [Char]
    "normalDistr" Double -> Double -> Maybe NormalDistribution
    normalDistrE

    instance ToJSON NormalDistribution
    instance FromJSON NormalDistribution where
      parseJSON :: Value -> Parser NormalDistribution
    parseJSON (Object Object
    v) = do
        Double
    m  <- Object
    v Object -> Key -> Parser Double
    forall a. FromJSON a => Object -> Key -> Parser a
    .: Key
    "mean"
        Double
    sd <- Object
    v Object -> Key -> Parser Double
    forall a. FromJSON a => Object -> Key -> Parser a
    .: Key
    "stdDev"
        ([Char] -> Parser NormalDistribution)
    -> (NormalDistribution -> Parser NormalDistribution)
    -> Either [Char] NormalDistribution
    -> Parser NormalDistribution
    forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
    either [Char] -> Parser NormalDistribution
    forall a. [Char] -> Parser a
    forall (m :: * -> *) a. MonadFail m => [Char] -> m a
    fail NormalDistribution -> Parser NormalDistribution
    forall a. a -> Parser a
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Either [Char] NormalDistribution -> Parser NormalDistribution)
    -> Either [Char] NormalDistribution -> Parser NormalDistribution
    forall a b. (a -> b) -> a -> b
    $ Double -> Double -> Either [Char] NormalDistribution
    normalDistrErr Double
    m Double
    sd
      parseJSON Value
    _ = Parser NormalDistribution
    forall a. Parser a
    forall (f :: * -> *) a. Alternative f => f a
    empty

    instance Binary NormalDistribution where
        put :: NormalDistribution -> Put
    put (ND Double
    m Double
    sd Double
    _ Double
    _) = Double -> Put
    forall t. Binary t => t -> Put
    put Double
    m 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
    sd
        get :: Get NormalDistribution
    get = do
          Double
    m  <- Get Double
    forall t. Binary t => Get t
    get
          Double
    sd <- Get Double
    forall t. Binary t => Get t
    get
          ([Char] -> Get NormalDistribution)
    -> (NormalDistribution -> Get NormalDistribution)
    -> Either [Char] NormalDistribution
    -> Get NormalDistribution
    forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
    either [Char] -> Get NormalDistribution
    forall a. [Char] -> Get a
    forall (m :: * -> *) a. MonadFail m => [Char] -> m a
    fail NormalDistribution -> Get NormalDistribution
    forall a. a -> Get a
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Either [Char] NormalDistribution -> Get NormalDistribution)
    -> Either [Char] NormalDistribution -> Get NormalDistribution
    forall a b. (a -> b) -> a -> b
    $ Double -> Double -> Either [Char] NormalDistribution
    normalDistrErr Double
    m Double
    sd

    instance D.Distribution NormalDistribution where
        cumulative :: NormalDistribution -> Double -> Double
    cumulative      = NormalDistribution -> Double -> Double
    cumulative
        complCumulative :: NormalDistribution -> Double -> Double
    complCumulative = NormalDistribution -> Double -> Double
    complCumulative

    instance D.ContDistr NormalDistribution where
        logDensity :: NormalDistribution -> Double -> Double
    logDensity    = NormalDistribution -> Double -> Double
    logDensity
        quantile :: NormalDistribution -> Double -> Double
    quantile      = NormalDistribution -> Double -> Double
    quantile
        complQuantile :: NormalDistribution -> Double -> Double
    complQuantile = NormalDistribution -> Double -> Double
    complQuantile

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

    instance D.Mean NormalDistribution where
        mean :: NormalDistribution -> Double
    mean = NormalDistribution -> Double
    mean

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

    instance D.Variance NormalDistribution where
        stdDev :: NormalDistribution -> Double
    stdDev = NormalDistribution -> Double
    stdDev

    instance D.Entropy NormalDistribution where
      entropy :: NormalDistribution -> Double
    entropy NormalDistribution
    d = Double
    0.5 Double -> Double -> Double
    forall a. Num a => a -> a -> a
    * Double -> Double
    forall a. Floating a => a -> a
    log (Double
    2 Double -> Double -> Double
    forall a. Num a => a -> a -> a
    * Double
    forall a. Floating a => a
    pi Double -> Double -> Double
    forall a. Num a => a -> a -> a
    * Double -> Double
    forall a. Floating a => a -> a
    exp Double
    1 Double -> Double -> Double
    forall a. Num a => a -> a -> a
    * NormalDistribution -> Double
    forall d. Variance d => d -> Double
    D.variance NormalDistribution
    d)

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

    instance D.ContGen NormalDistribution where
        genContVar :: forall g (m :: * -> *).
    StatefulGen g m =>
    NormalDistribution -> g -> m Double
    genContVar NormalDistribution
    d = Double -> Double -> g -> m Double
    forall g (m :: * -> *).
    StatefulGen g m =>
    Double -> Double -> g -> m Double
    MWC.normal (NormalDistribution -> Double
    mean NormalDistribution
    d) (NormalDistribution -> Double
    stdDev NormalDistribution
    d)

    -- | Standard normal distribution with mean equal to 0 and variance equal to 1
    standard :: NormalDistribution
    standard :: NormalDistribution
    standard = ND { mean :: Double
    mean       = Double
    0.0
                  , stdDev :: Double
    stdDev     = Double
    1.0
                  , ndPdfDenom :: Double
    ndPdfDenom = Double -> Double
    forall a. Floating a => a -> a
    log Double
    m_sqrt_2_pi
                  , ndCdfDenom :: Double
    ndCdfDenom = Double
    m_sqrt_2
                  }

    -- | Create normal distribution from parameters.
    --
    -- IMPORTANT: prior to 0.10 release second parameter was variance not
    -- standard deviation.
    normalDistr :: Double            -- ^ Mean of distribution
                -> Double            -- ^ Standard deviation of distribution
                -> NormalDistribution
    normalDistr :: Double -> Double -> NormalDistribution
    normalDistr Double
    m Double
    sd = ([Char] -> NormalDistribution)
    -> (NormalDistribution -> NormalDistribution)
    -> Either [Char] NormalDistribution
    -> NormalDistribution
    forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
    either [Char] -> NormalDistribution
    forall a. HasCallStack => [Char] -> a
    error NormalDistribution -> NormalDistribution
    forall a. a -> a
    id (Either [Char] NormalDistribution -> NormalDistribution)
    -> Either [Char] NormalDistribution -> NormalDistribution
    forall a b. (a -> b) -> a -> b
    $ Double -> Double -> Either [Char] NormalDistribution
    normalDistrErr Double
    m Double
    sd

    -- | Create normal distribution from parameters.
    --
    -- IMPORTANT: prior to 0.10 release second parameter was variance not
    -- standard deviation.
    normalDistrE :: Double            -- ^ Mean of distribution
                 -> Double            -- ^ Standard deviation of distribution
                 -> Maybe NormalDistribution
    normalDistrE :: Double -> Double -> Maybe NormalDistribution
    normalDistrE Double
    m Double
    sd = ([Char] -> Maybe NormalDistribution)
    -> (NormalDistribution -> Maybe NormalDistribution)
    -> Either [Char] NormalDistribution
    -> Maybe NormalDistribution
    forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
    either (Maybe NormalDistribution -> [Char] -> Maybe NormalDistribution
    forall a b. a -> b -> a
    const Maybe NormalDistribution
    forall a. Maybe a
    Nothing) NormalDistribution -> Maybe NormalDistribution
    forall a. a -> Maybe a
    Just (Either [Char] NormalDistribution -> Maybe NormalDistribution)
    -> Either [Char] NormalDistribution -> Maybe NormalDistribution
    forall a b. (a -> b) -> a -> b
    $ Double -> Double -> Either [Char] NormalDistribution
    normalDistrErr Double
    m Double
    sd

    -- | Create normal distribution from parameters.
    --
    normalDistrErr :: Double            -- ^ Mean of distribution
                   -> Double            -- ^ Standard deviation of distribution
                   -> Either String NormalDistribution
    normalDistrErr :: Double -> Double -> Either [Char] NormalDistribution
    normalDistrErr Double
    m Double
    sd
      | Double
    sd Double -> Double -> Bool
    forall a. Ord a => a -> a -> Bool
    > Double
    0    = NormalDistribution -> Either [Char] NormalDistribution
    forall a b. b -> Either a b
    Right (NormalDistribution -> Either [Char] NormalDistribution)
    -> NormalDistribution -> Either [Char] NormalDistribution
    forall a b. (a -> b) -> a -> b
    $ ND { mean :: Double
    mean       = Double
    m
                               , stdDev :: Double
    stdDev     = Double
    sd
                               , ndPdfDenom :: Double
    ndPdfDenom = Double -> Double
    forall a. Floating a => a -> a
    log (Double -> Double) -> Double -> Double
    forall a b. (a -> b) -> a -> b
    $ Double
    m_sqrt_2_pi Double -> Double -> Double
    forall a. Num a => a -> a -> a
    * Double
    sd
                               , ndCdfDenom :: Double
    ndCdfDenom = Double
    m_sqrt_2 Double -> Double -> Double
    forall a. Num a => a -> a -> a
    * Double
    sd
                               }
      | Bool
    otherwise = [Char] -> Either [Char] NormalDistribution
    forall a b. a -> Either a b
    Left ([Char] -> Either [Char] NormalDistribution)
    -> [Char] -> Either [Char] NormalDistribution
    forall a b. (a -> b) -> a -> b
    $ Double -> Double -> [Char]
    errMsg Double
    m Double
    sd

    errMsg :: Double -> Double -> String
    errMsg :: Double -> Double -> [Char]
    errMsg Double
    _ Double
    sd = [Char]
    "Statistics.Distribution.Normal.normalDistr: standard deviation must be positive. Got " [Char] -> ShowS
    forall a. [a] -> [a] -> [a]
    ++ Double -> [Char]
    forall a. Show a => a -> [Char]
    show Double
    sd

    -- | Variance is estimated using maximum likelihood method
    --   (biased estimation).
    --
    --   Returns @Nothing@ if sample contains less than one element or
    --   variance is zero (all elements are equal)
    instance D.FromSample NormalDistribution Double where
      fromSample :: forall (v :: * -> *).
    Vector v Double =>
    v Double -> Maybe NormalDistribution
    fromSample v Double
    xs
        | v Double -> Int
    forall (v :: * -> *) a. Vector v a => v a -> Int
    G.length v Double
    xs Int -> Int -> Bool
    forall a. Ord a => a -> a -> Bool
    <= Int
    1 = Maybe NormalDistribution
    forall a. Maybe a
    Nothing
        | Double
    v Double -> Double -> Bool
    forall a. Eq a => a -> a -> Bool
    == Double
    0           = Maybe NormalDistribution
    forall a. Maybe a
    Nothing
        | Bool
    otherwise        = NormalDistribution -> Maybe NormalDistribution
    forall a. a -> Maybe a
    Just (NormalDistribution -> Maybe NormalDistribution)
    -> NormalDistribution -> Maybe NormalDistribution
    forall a b. (a -> b) -> a -> b
    $! Double -> Double -> NormalDistribution
    normalDistr Double
    m (Double -> Double
    forall a. Floating a => a -> a
    sqrt Double
    v)
        where
          (Double
    m,Double
    v) = v Double -> (Double, Double)
    forall (v :: * -> *).
    Vector v Double =>
    v Double -> (Double, Double)
    S.meanVariance v Double
    xs

    logDensity :: NormalDistribution -> Double -> Double
    logDensity :: NormalDistribution -> Double -> Double
    logDensity NormalDistribution
    d Double
    x = (-Double
    xm Double -> Double -> Double
    forall a. Num a => a -> a -> a
    * Double
    xm Double -> Double -> Double
    forall a. Fractional a => a -> a -> a
    / (Double
    2 Double -> Double -> Double
    forall a. Num a => a -> a -> a
    * Double
    sd Double -> Double -> Double
    forall a. Num a => a -> a -> a
    * Double
    sd)) Double -> Double -> Double
    forall a. Num a => a -> a -> a
    - NormalDistribution -> Double
    ndPdfDenom NormalDistribution
    d
        where xm :: Double
    xm = Double
    x Double -> Double -> Double
    forall a. Num a => a -> a -> a
    - NormalDistribution -> Double
    mean NormalDistribution
    d
              sd :: Double
    sd = NormalDistribution -> Double
    stdDev NormalDistribution
    d

    cumulative :: NormalDistribution -> Double -> Double
    cumulative :: NormalDistribution -> Double -> Double
    cumulative NormalDistribution
    d Double
    x = Double -> Double
    erfc ((NormalDistribution -> Double
    mean NormalDistribution
    d Double -> Double -> Double
    forall a. Num a => a -> a -> a
    - Double
    x) Double -> Double -> Double
    forall a. Fractional a => a -> a -> a
    / NormalDistribution -> Double
    ndCdfDenom NormalDistribution
    d) Double -> Double -> Double
    forall a. Fractional a => a -> a -> a
    / Double
    2

    complCumulative :: NormalDistribution -> Double -> Double
    complCumulative :: NormalDistribution -> Double -> Double
    complCumulative NormalDistribution
    d Double
    x = Double -> Double
    erfc ((Double
    x Double -> Double -> Double
    forall a. Num a => a -> a -> a
    - NormalDistribution -> Double
    mean NormalDistribution
    d) Double -> Double -> Double
    forall a. Fractional a => a -> a -> a
    / NormalDistribution -> Double
    ndCdfDenom NormalDistribution
    d) Double -> Double -> Double
    forall a. Fractional a => a -> a -> a
    / Double
    2

    quantile :: NormalDistribution -> Double -> Double
    quantile :: NormalDistribution -> Double -> Double
    quantile NormalDistribution
    d Double
    p
      | Double
    p Double -> Double -> Bool
    forall a. Eq a => a -> a -> Bool
    == Double
    0         = -Double
    inf
      | Double
    p Double -> Double -> Bool
    forall a. Eq a => a -> a -> Bool
    == Double
    1         = Double
    inf
      | Double
    p Double -> Double -> Bool
    forall a. Eq a => a -> a -> Bool
    == Double
    0.5       = NormalDistribution -> Double
    mean NormalDistribution
    d
      | 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
    x Double -> Double -> Double
    forall a. Num a => a -> a -> a
    * NormalDistribution -> Double
    ndCdfDenom NormalDistribution
    d Double -> Double -> Double
    forall a. Num a => a -> a -> a
    + NormalDistribution -> Double
    mean NormalDistribution
    d
      | Bool
    otherwise      =
        [Char] -> Double
    forall a. HasCallStack => [Char] -> a
    error ([Char] -> Double) -> [Char] -> Double
    forall a b. (a -> b) -> a -> b
    $ [Char]
    "Statistics.Distribution.Normal.quantile: p must be in [0,1] range. Got: "[Char] -> ShowS
    forall a. [a] -> [a] -> [a]
    ++Double -> [Char]
    forall a. Show a => a -> [Char]
    show Double
    p
      where x :: Double
    x          = - Double -> Double
    invErfc (Double
    2 Double -> Double -> Double
    forall a. Num a => a -> a -> a
    * Double
    p)
            inf :: Double
    inf        = Double
    1Double -> Double -> Double
    forall a. Fractional a => a -> a -> a
    /Double
    0

    complQuantile :: NormalDistribution -> Double -> Double
    complQuantile :: NormalDistribution -> Double -> Double
    complQuantile NormalDistribution
    d Double
    p
      | Double
    p Double -> Double -> Bool
    forall a. Eq a => a -> a -> Bool
    == Double
    0         = Double
    inf
      | Double
    p Double -> Double -> Bool
    forall a. Eq a => a -> a -> Bool
    == Double
    1         = -Double
    inf
      | Double
    p Double -> Double -> Bool
    forall a. Eq a => a -> a -> Bool
    == Double
    0.5       = NormalDistribution -> Double
    mean NormalDistribution
    d
      | 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
    x Double -> Double -> Double
    forall a. Num a => a -> a -> a
    * NormalDistribution -> Double
    ndCdfDenom NormalDistribution
    d Double -> Double -> Double
    forall a. Num a => a -> a -> a
    + NormalDistribution -> Double
    mean NormalDistribution
    d
      | Bool
    otherwise      =
        [Char] -> Double
    forall a. HasCallStack => [Char] -> a
    error ([Char] -> Double) -> [Char] -> Double
    forall a b. (a -> b) -> a -> b
    $ [Char]
    "Statistics.Distribution.Normal.complQuantile: p must be in [0,1] range. Got: "[Char] -> ShowS
    forall a. [a] -> [a] -> [a]
    ++Double -> [Char]
    forall a. Show a => a -> [Char]
    show Double
    p
      where x :: Double
    x          = Double -> Double
    invErfc (Double
    2 Double -> Double -> Double
    forall a. Num a => a -> a -> a
    * Double
    p)
            inf :: Double
    inf        = Double
    1Double -> Double -> Double
    forall a. Fractional a => a -> a -> a
    /Double
    0