{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE DeriveDataTypeable, DeriveGeneric #-}
module Statistics.Distribution.Laplace
(
LaplaceDistribution
, laplace
, laplaceE
, ldLocation
, ldScale
) where
import Control.Applicative
import Data.Aeson (FromJSON(..), ToJSON, Value(..), (.:))
import Data.Binary (Binary(..))
import Data.Data (Data, Typeable)
import GHC.Generics (Generic)
import qualified Data.Vector.Generic as G
import qualified Statistics.Distribution as D
import qualified Statistics.Quantile as Q
import qualified Statistics.Sample as S
import Statistics.Internal
data LaplaceDistribution = LD {
LaplaceDistribution -> Double
ldLocation :: {-# UNPACK #-} !Double
, LaplaceDistribution -> Double
ldScale :: {-# UNPACK #-} !Double
} deriving (LaplaceDistribution -> LaplaceDistribution -> Bool
(LaplaceDistribution -> LaplaceDistribution -> Bool)
-> (LaplaceDistribution -> LaplaceDistribution -> Bool)
-> Eq LaplaceDistribution
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: LaplaceDistribution -> LaplaceDistribution -> Bool
$c/= :: LaplaceDistribution -> LaplaceDistribution -> Bool
== :: LaplaceDistribution -> LaplaceDistribution -> Bool
$c== :: LaplaceDistribution -> LaplaceDistribution -> Bool
Eq, Typeable, Typeable LaplaceDistribution
DataType
Constr
Typeable LaplaceDistribution
-> (forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> LaplaceDistribution
-> c LaplaceDistribution)
-> (forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c LaplaceDistribution)
-> (LaplaceDistribution -> Constr)
-> (LaplaceDistribution -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c LaplaceDistribution))
-> (forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c LaplaceDistribution))
-> ((forall b. Data b => b -> b)
-> LaplaceDistribution -> LaplaceDistribution)
-> (forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> LaplaceDistribution -> r)
-> (forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> LaplaceDistribution -> r)
-> (forall u.
(forall d. Data d => d -> u) -> LaplaceDistribution -> [u])
-> (forall u.
Int -> (forall d. Data d => d -> u) -> LaplaceDistribution -> u)
-> (forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> LaplaceDistribution -> m LaplaceDistribution)
-> (forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> LaplaceDistribution -> m LaplaceDistribution)
-> (forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> LaplaceDistribution -> m LaplaceDistribution)
-> Data LaplaceDistribution
LaplaceDistribution -> DataType
LaplaceDistribution -> Constr
(forall b. Data b => b -> b)
-> LaplaceDistribution -> LaplaceDistribution
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> LaplaceDistribution
-> c LaplaceDistribution
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c LaplaceDistribution
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) -> LaplaceDistribution -> u
forall u.
(forall d. Data d => d -> u) -> LaplaceDistribution -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> LaplaceDistribution -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> LaplaceDistribution -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> LaplaceDistribution -> m LaplaceDistribution
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> LaplaceDistribution -> m LaplaceDistribution
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c LaplaceDistribution
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> LaplaceDistribution
-> c LaplaceDistribution
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c LaplaceDistribution)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c LaplaceDistribution)
$cLD :: Constr
$tLaplaceDistribution :: DataType
gmapMo :: (forall d. Data d => d -> m d)
-> LaplaceDistribution -> m LaplaceDistribution
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> LaplaceDistribution -> m LaplaceDistribution
gmapMp :: (forall d. Data d => d -> m d)
-> LaplaceDistribution -> m LaplaceDistribution
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> LaplaceDistribution -> m LaplaceDistribution
gmapM :: (forall d. Data d => d -> m d)
-> LaplaceDistribution -> m LaplaceDistribution
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> LaplaceDistribution -> m LaplaceDistribution
gmapQi :: Int -> (forall d. Data d => d -> u) -> LaplaceDistribution -> u
$cgmapQi :: forall u.
Int -> (forall d. Data d => d -> u) -> LaplaceDistribution -> u
gmapQ :: (forall d. Data d => d -> u) -> LaplaceDistribution -> [u]
$cgmapQ :: forall u.
(forall d. Data d => d -> u) -> LaplaceDistribution -> [u]
gmapQr :: (r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> LaplaceDistribution -> r
$cgmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> LaplaceDistribution -> r
gmapQl :: (r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> LaplaceDistribution -> r
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> LaplaceDistribution -> r
gmapT :: (forall b. Data b => b -> b)
-> LaplaceDistribution -> LaplaceDistribution
$cgmapT :: (forall b. Data b => b -> b)
-> LaplaceDistribution -> LaplaceDistribution
dataCast2 :: (forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c LaplaceDistribution)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c LaplaceDistribution)
dataCast1 :: (forall d. Data d => c (t d)) -> Maybe (c LaplaceDistribution)
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c LaplaceDistribution)
dataTypeOf :: LaplaceDistribution -> DataType
$cdataTypeOf :: LaplaceDistribution -> DataType
toConstr :: LaplaceDistribution -> Constr
$ctoConstr :: LaplaceDistribution -> Constr
gunfold :: (forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c LaplaceDistribution
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c LaplaceDistribution
gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> LaplaceDistribution
-> c LaplaceDistribution
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> LaplaceDistribution
-> c LaplaceDistribution
$cp1Data :: Typeable LaplaceDistribution
Data, (forall x. LaplaceDistribution -> Rep LaplaceDistribution x)
-> (forall x. Rep LaplaceDistribution x -> LaplaceDistribution)
-> Generic LaplaceDistribution
forall x. Rep LaplaceDistribution x -> LaplaceDistribution
forall x. LaplaceDistribution -> Rep LaplaceDistribution x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep LaplaceDistribution x -> LaplaceDistribution
$cfrom :: forall x. LaplaceDistribution -> Rep LaplaceDistribution x
Generic)
instance Show LaplaceDistribution where
showsPrec :: Int -> LaplaceDistribution -> ShowS
showsPrec Int
i (LD Double
l Double
s) = String -> Double -> Double -> Int -> ShowS
forall a b. (Show a, Show b) => String -> a -> b -> Int -> ShowS
defaultShow2 String
"laplace" Double
l Double
s Int
i
instance Read LaplaceDistribution where
readPrec :: ReadPrec LaplaceDistribution
readPrec = String
-> (Double -> Double -> Maybe LaplaceDistribution)
-> ReadPrec LaplaceDistribution
forall a b r.
(Read a, Read b) =>
String -> (a -> b -> Maybe r) -> ReadPrec r
defaultReadPrecM2 String
"laplace" Double -> Double -> Maybe LaplaceDistribution
laplaceE
instance ToJSON LaplaceDistribution
instance FromJSON LaplaceDistribution where
parseJSON :: Value -> Parser LaplaceDistribution
parseJSON (Object Object
v) = do
Double
l <- Object
v Object -> Key -> Parser Double
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"ldLocation"
Double
s <- Object
v Object -> Key -> Parser Double
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"ldScale"
Parser LaplaceDistribution
-> (LaplaceDistribution -> Parser LaplaceDistribution)
-> Maybe LaplaceDistribution
-> Parser LaplaceDistribution
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (String -> Parser LaplaceDistribution
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Parser LaplaceDistribution)
-> String -> Parser LaplaceDistribution
forall a b. (a -> b) -> a -> b
$ Double -> Double -> String
errMsg Double
l Double
s) LaplaceDistribution -> Parser LaplaceDistribution
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe LaplaceDistribution -> Parser LaplaceDistribution)
-> Maybe LaplaceDistribution -> Parser LaplaceDistribution
forall a b. (a -> b) -> a -> b
$ Double -> Double -> Maybe LaplaceDistribution
laplaceE Double
l Double
s
parseJSON Value
_ = Parser LaplaceDistribution
forall (f :: * -> *) a. Alternative f => f a
empty
instance Binary LaplaceDistribution where
put :: LaplaceDistribution -> Put
put (LD Double
l Double
s) = Double -> Put
forall t. Binary t => t -> Put
put Double
l 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
s
get :: Get LaplaceDistribution
get = do
Double
l <- Get Double
forall t. Binary t => Get t
get
Double
s <- Get Double
forall t. Binary t => Get t
get
Get LaplaceDistribution
-> (LaplaceDistribution -> Get LaplaceDistribution)
-> Maybe LaplaceDistribution
-> Get LaplaceDistribution
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (String -> Get LaplaceDistribution
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Get LaplaceDistribution)
-> String -> Get LaplaceDistribution
forall a b. (a -> b) -> a -> b
$ Double -> Double -> String
errMsg Double
l Double
s) LaplaceDistribution -> Get LaplaceDistribution
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe LaplaceDistribution -> Get LaplaceDistribution)
-> Maybe LaplaceDistribution -> Get LaplaceDistribution
forall a b. (a -> b) -> a -> b
$ Double -> Double -> Maybe LaplaceDistribution
laplaceE Double
l Double
s
instance D.Distribution LaplaceDistribution where
cumulative :: LaplaceDistribution -> Double -> Double
cumulative = LaplaceDistribution -> Double -> Double
cumulative
complCumulative :: LaplaceDistribution -> Double -> Double
complCumulative = LaplaceDistribution -> Double -> Double
complCumulative
instance D.ContDistr LaplaceDistribution where
density :: LaplaceDistribution -> Double -> Double
density (LD Double
l Double
s) Double
x = Double -> Double
forall a. Floating a => a -> a
exp (- Double -> Double
forall a. Num a => a -> a
abs (Double
x Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
l) Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
s) Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ (Double
2 Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
s)
logDensity :: LaplaceDistribution -> Double -> Double
logDensity (LD Double
l Double
s) Double
x = - Double -> Double
forall a. Num a => a -> a
abs (Double
x Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
l) Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
s 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 -> Double
forall a. Floating a => a -> a
log Double
s
quantile :: LaplaceDistribution -> Double -> Double
quantile = LaplaceDistribution -> Double -> Double
quantile
complQuantile :: LaplaceDistribution -> Double -> Double
complQuantile = LaplaceDistribution -> Double -> Double
complQuantile
instance D.Mean LaplaceDistribution where
mean :: LaplaceDistribution -> Double
mean (LD Double
l Double
_) = Double
l
instance D.Variance LaplaceDistribution where
variance :: LaplaceDistribution -> Double
variance (LD Double
_ Double
s) = Double
2 Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
s Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
s
instance D.MaybeMean LaplaceDistribution where
maybeMean :: LaplaceDistribution -> Maybe Double
maybeMean = Double -> Maybe Double
forall a. a -> Maybe a
Just (Double -> Maybe Double)
-> (LaplaceDistribution -> Double)
-> LaplaceDistribution
-> Maybe Double
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LaplaceDistribution -> Double
forall d. Mean d => d -> Double
D.mean
instance D.MaybeVariance LaplaceDistribution where
maybeStdDev :: LaplaceDistribution -> Maybe Double
maybeStdDev = Double -> Maybe Double
forall a. a -> Maybe a
Just (Double -> Maybe Double)
-> (LaplaceDistribution -> Double)
-> LaplaceDistribution
-> Maybe Double
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LaplaceDistribution -> Double
forall d. Variance d => d -> Double
D.stdDev
maybeVariance :: LaplaceDistribution -> Maybe Double
maybeVariance = Double -> Maybe Double
forall a. a -> Maybe a
Just (Double -> Maybe Double)
-> (LaplaceDistribution -> Double)
-> LaplaceDistribution
-> Maybe Double
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LaplaceDistribution -> Double
forall d. Variance d => d -> Double
D.variance
instance D.Entropy LaplaceDistribution where
entropy :: LaplaceDistribution -> Double
entropy (LD Double
_ Double
s) = Double
1 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
s)
instance D.MaybeEntropy LaplaceDistribution where
maybeEntropy :: LaplaceDistribution -> Maybe Double
maybeEntropy = Double -> Maybe Double
forall a. a -> Maybe a
Just (Double -> Maybe Double)
-> (LaplaceDistribution -> Double)
-> LaplaceDistribution
-> Maybe Double
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LaplaceDistribution -> Double
forall d. Entropy d => d -> Double
D.entropy
instance D.ContGen LaplaceDistribution where
genContVar :: LaplaceDistribution -> g -> m Double
genContVar = LaplaceDistribution -> g -> m Double
forall d g (m :: * -> *).
(ContDistr d, StatefulGen g m) =>
d -> g -> m Double
D.genContinuous
cumulative :: LaplaceDistribution -> Double -> Double
cumulative :: LaplaceDistribution -> Double -> Double
cumulative (LD Double
l Double
s) Double
x
| Double
x Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
<= Double
l = Double
0.5 Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double -> Double
forall a. Floating a => a -> a
exp ( (Double
x Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
l) Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
s)
| Bool
otherwise = Double
1 Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
0.5 Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double -> Double
forall a. Floating a => a -> a
exp ( - (Double
x Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
l) Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
s )
complCumulative :: LaplaceDistribution -> Double -> Double
complCumulative :: LaplaceDistribution -> Double -> Double
complCumulative (LD Double
l Double
s) Double
x
| Double
x Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
<= Double
l = Double
1 Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
0.5 Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double -> Double
forall a. Floating a => a -> a
exp ( (Double
x Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
l) Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
s)
| Bool
otherwise = Double
0.5 Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double -> Double
forall a. Floating a => a -> a
exp ( - (Double
x Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
l) Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
s )
quantile :: LaplaceDistribution -> Double -> Double
quantile :: LaplaceDistribution -> Double -> Double
quantile (LD Double
l Double
s) 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 = Double
l
| 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
0.5 = Double
l Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
s 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
p)
| Double
p Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
> Double
0.5 Bool -> Bool -> Bool
&& Double
p Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
< Double
1 = Double
l Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
s 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
2 Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
p)
| Bool
otherwise =
String -> Double
forall a. HasCallStack => String -> a
error (String -> Double) -> String -> Double
forall a b. (a -> b) -> a -> b
$ String
"Statistics.Distribution.Laplace.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
inf :: Double
inf = Double
1 Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
0
complQuantile :: LaplaceDistribution -> Double -> Double
complQuantile :: LaplaceDistribution -> Double -> Double
complQuantile (LD Double
l Double
s) 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 = Double
l
| 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
0.5 = Double
l Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
s 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
p)
| Double
p Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
> Double
0.5 Bool -> Bool -> Bool
&& Double
p Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
< Double
1 = Double
l Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
s 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
2 Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
p)
| Bool
otherwise =
String -> Double
forall a. HasCallStack => String -> a
error (String -> Double) -> String -> Double
forall a b. (a -> b) -> a -> b
$ String
"Statistics.Distribution.Laplace.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
inf :: Double
inf = Double
1 Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
0
laplace :: Double
-> Double
-> LaplaceDistribution
laplace :: Double -> Double -> LaplaceDistribution
laplace Double
l Double
s = LaplaceDistribution
-> (LaplaceDistribution -> LaplaceDistribution)
-> Maybe LaplaceDistribution
-> LaplaceDistribution
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (String -> LaplaceDistribution
forall a. HasCallStack => String -> a
error (String -> LaplaceDistribution) -> String -> LaplaceDistribution
forall a b. (a -> b) -> a -> b
$ Double -> Double -> String
errMsg Double
l Double
s) LaplaceDistribution -> LaplaceDistribution
forall a. a -> a
id (Maybe LaplaceDistribution -> LaplaceDistribution)
-> Maybe LaplaceDistribution -> LaplaceDistribution
forall a b. (a -> b) -> a -> b
$ Double -> Double -> Maybe LaplaceDistribution
laplaceE Double
l Double
s
laplaceE :: Double
-> Double
-> Maybe LaplaceDistribution
laplaceE :: Double -> Double -> Maybe LaplaceDistribution
laplaceE Double
l Double
s
| Double
s Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
>= Double
0 = LaplaceDistribution -> Maybe LaplaceDistribution
forall a. a -> Maybe a
Just (Double -> Double -> LaplaceDistribution
LD Double
l Double
s)
| Bool
otherwise = Maybe LaplaceDistribution
forall a. Maybe a
Nothing
errMsg :: Double -> Double -> String
errMsg :: Double -> Double -> String
errMsg Double
_ Double
s = String
"Statistics.Distribution.Laplace.laplace: scale parameter must be positive. Got " String -> ShowS
forall a. [a] -> [a] -> [a]
++ Double -> String
forall a. Show a => a -> String
show Double
s
instance D.FromSample LaplaceDistribution Double where
fromSample :: v Double -> Maybe LaplaceDistribution
fromSample v Double
xs
| v Double -> Bool
forall (v :: * -> *) a. Vector v a => v a -> Bool
G.null v Double
xs = Maybe LaplaceDistribution
forall a. Maybe a
Nothing
| Bool
otherwise = LaplaceDistribution -> Maybe LaplaceDistribution
forall a. a -> Maybe a
Just (LaplaceDistribution -> Maybe LaplaceDistribution)
-> LaplaceDistribution -> Maybe LaplaceDistribution
forall a b. (a -> b) -> a -> b
$! Double -> Double -> LaplaceDistribution
LD Double
s Double
l
where
s :: Double
s = ContParam -> v Double -> Double
forall (v :: * -> *).
Vector v Double =>
ContParam -> v Double -> Double
Q.median ContParam
Q.medianUnbiased v Double
xs
l :: Double
l = v Double -> Double
forall (v :: * -> *). Vector v Double => v Double -> Double
S.mean (v Double -> Double) -> v Double -> Double
forall a b. (a -> b) -> a -> b
$ (Double -> Double) -> v Double -> v Double
forall (v :: * -> *) a b.
(Vector v a, Vector v b) =>
(a -> b) -> v a -> v b
G.map (\Double
x -> Double -> Double
forall a. Num a => a -> a
abs (Double -> Double) -> Double -> Double
forall a b. (a -> b) -> a -> b
$ Double
x Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
s) v Double
xs