{-# 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
/= :: NormalDistribution -> NormalDistribution -> Bool
$c/= :: NormalDistribution -> NormalDistribution -> Bool
== :: NormalDistribution -> NormalDistribution -> Bool
$c== :: NormalDistribution -> NormalDistribution -> Bool
Eq, Typeable, Typeable NormalDistribution
DataType
Constr
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 -> DataType
NormalDistribution -> Constr
(forall b. Data b => b -> b)
-> NormalDistribution -> NormalDistribution
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> NormalDistribution
-> c NormalDistribution
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c 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)
$cND :: Constr
$tNormalDistribution :: DataType
gmapMo :: (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
gmapMp :: (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
gmapM :: (forall d. Data d => d -> m d)
-> NormalDistribution -> m NormalDistribution
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> NormalDistribution -> m NormalDistribution
gmapQi :: Int -> (forall d. Data d => d -> u) -> NormalDistribution -> u
$cgmapQi :: forall u.
Int -> (forall d. Data d => d -> u) -> NormalDistribution -> u
gmapQ :: (forall d. Data d => d -> u) -> NormalDistribution -> [u]
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> NormalDistribution -> [u]
gmapQr :: (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
gmapQl :: (r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> NormalDistribution -> r
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> NormalDistribution -> r
gmapT :: (forall b. Data b => b -> b)
-> NormalDistribution -> NormalDistribution
$cgmapT :: (forall b. Data b => b -> b)
-> NormalDistribution -> NormalDistribution
dataCast2 :: (forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c NormalDistribution)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c NormalDistribution)
dataCast1 :: (forall d. Data d => c (t d)) -> Maybe (c NormalDistribution)
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c NormalDistribution)
dataTypeOf :: NormalDistribution -> DataType
$cdataTypeOf :: NormalDistribution -> DataType
toConstr :: NormalDistribution -> Constr
$ctoConstr :: NormalDistribution -> Constr
gunfold :: (forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c NormalDistribution
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c NormalDistribution
gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> NormalDistribution
-> c NormalDistribution
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> NormalDistribution
-> c NormalDistribution
$cp1Data :: Typeable 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
$cto :: forall x. Rep NormalDistribution x -> NormalDistribution
$cfrom :: forall x. NormalDistribution -> Rep NormalDistribution x
Generic)

instance Show NormalDistribution where
  showsPrec :: Int -> NormalDistribution -> ShowS
showsPrec Int
i (ND Double
m Double
s Double
_ Double
_) = String -> Double -> Double -> Int -> ShowS
forall a b. (Show a, Show b) => String -> a -> b -> Int -> ShowS
defaultShow2 String
"normalDistr" Double
m Double
s Int
i
instance Read NormalDistribution where
  readPrec :: ReadPrec NormalDistribution
readPrec = String
-> (Double -> Double -> Maybe NormalDistribution)
-> ReadPrec NormalDistribution
forall a b r.
(Read a, Read b) =>
String -> (a -> b -> Maybe r) -> ReadPrec r
defaultReadPrecM2 String
"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"
    (String -> Parser NormalDistribution)
-> (NormalDistribution -> Parser NormalDistribution)
-> Either String NormalDistribution
-> Parser NormalDistribution
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either String -> Parser NormalDistribution
forall (m :: * -> *) a. MonadFail m => String -> m a
fail NormalDistribution -> Parser NormalDistribution
forall (m :: * -> *) a. Monad m => a -> m a
return (Either String NormalDistribution -> Parser NormalDistribution)
-> Either String NormalDistribution -> Parser NormalDistribution
forall a b. (a -> b) -> a -> b
$ Double -> Double -> Either String NormalDistribution
normalDistrErr Double
m Double
sd
  parseJSON Value
_ = Parser NormalDistribution
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 (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
      (String -> Get NormalDistribution)
-> (NormalDistribution -> Get NormalDistribution)
-> Either String NormalDistribution
-> Get NormalDistribution
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either String -> Get NormalDistribution
forall (m :: * -> *) a. MonadFail m => String -> m a
fail NormalDistribution -> Get NormalDistribution
forall (m :: * -> *) a. Monad m => a -> m a
return (Either String NormalDistribution -> Get NormalDistribution)
-> Either String NormalDistribution -> Get NormalDistribution
forall a b. (a -> b) -> a -> b
$ Double -> Double -> Either String 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 :: 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 :: Double -> Double -> Double -> Double -> NormalDistribution
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 = (String -> NormalDistribution)
-> (NormalDistribution -> NormalDistribution)
-> Either String NormalDistribution
-> NormalDistribution
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either String -> NormalDistribution
forall a. HasCallStack => String -> a
error NormalDistribution -> NormalDistribution
forall a. a -> a
id (Either String NormalDistribution -> NormalDistribution)
-> Either String NormalDistribution -> NormalDistribution
forall a b. (a -> b) -> a -> b
$ Double -> Double -> Either String 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 = (String -> Maybe NormalDistribution)
-> (NormalDistribution -> Maybe NormalDistribution)
-> Either String NormalDistribution
-> Maybe NormalDistribution
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Maybe NormalDistribution -> String -> 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 String NormalDistribution -> Maybe NormalDistribution)
-> Either String NormalDistribution -> Maybe NormalDistribution
forall a b. (a -> b) -> a -> b
$ Double -> Double -> Either String 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 String NormalDistribution
normalDistrErr Double
m Double
sd
  | Double
sd Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
> Double
0    = NormalDistribution -> Either String NormalDistribution
forall a b. b -> Either a b
Right (NormalDistribution -> Either String NormalDistribution)
-> NormalDistribution -> Either String NormalDistribution
forall a b. (a -> b) -> a -> b
$ ND :: Double -> Double -> Double -> Double -> NormalDistribution
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 = String -> Either String NormalDistribution
forall a b. a -> Either a b
Left (String -> Either String NormalDistribution)
-> String -> Either String NormalDistribution
forall a b. (a -> b) -> a -> b
$ Double -> Double -> String
errMsg Double
m Double
sd

errMsg :: Double -> Double -> String
errMsg :: Double -> Double -> String
errMsg Double
_ Double
sd = String
"Statistics.Distribution.Normal.normalDistr: standard deviation must be positive. Got " String -> ShowS
forall a. [a] -> [a] -> [a]
++ Double -> String
forall a. Show a => a -> String
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 :: 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      =
    String -> Double
forall a. HasCallStack => String -> a
error (String -> Double) -> String -> Double
forall a b. (a -> b) -> a -> b
$ String
"Statistics.Distribution.Normal.quantile: p must be in [0,1] range. Got: "String -> ShowS
forall a. [a] -> [a] -> [a]
++Double -> String
forall a. Show a => a -> String
show Double
p
  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      =
    String -> Double
forall a. HasCallStack => String -> a
error (String -> Double) -> String -> Double
forall a b. (a -> b) -> a -> b
$ String
"Statistics.Distribution.Normal.complQuantile: p must be in [0,1] range. Got: "String -> ShowS
forall a. [a] -> [a] -> [a]
++Double -> String
forall a. Show a => a -> String
show Double
p
  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