{-# 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
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
LaplaceDistribution -> DataType
LaplaceDistribution -> Constr
(forall b. Data b => b -> b)
-> LaplaceDistribution -> 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)
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(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 (m :: * -> *).
MonadPlus m =>
(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 (m :: * -> *).
Monad m =>
(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 :: forall u.
Int -> (forall d. Data d => d -> u) -> LaplaceDistribution -> u
$cgmapQi :: forall u.
Int -> (forall d. Data d => d -> u) -> LaplaceDistribution -> u
gmapQ :: forall u.
(forall d. Data d => d -> u) -> LaplaceDistribution -> [u]
$cgmapQ :: forall u.
(forall d. Data d => d -> u) -> LaplaceDistribution -> [u]
gmapQr :: forall r r'.
(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 :: forall r r'.
(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 (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(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 (t :: * -> *) (c :: * -> *).
Typeable t =>
(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 (c :: * -> *).
(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 (c :: * -> *).
(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
Data, 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) = forall a b. (Show a, Show b) => [Char] -> a -> b -> Int -> ShowS
defaultShow2 [Char]
"laplace" Double
l Double
s Int
i
instance Read LaplaceDistribution where
readPrec :: ReadPrec LaplaceDistribution
readPrec = forall a b r.
(Read a, Read b) =>
[Char] -> (a -> b -> Maybe r) -> ReadPrec r
defaultReadPrecM2 [Char]
"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 forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"ldLocation"
Double
s <- Object
v forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"ldScale"
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall (m :: * -> *) a. MonadFail m => [Char] -> m a
fail forall a b. (a -> b) -> a -> b
$ Double -> Double -> [Char]
errMsg Double
l Double
s) forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Double -> Double -> Maybe LaplaceDistribution
laplaceE Double
l Double
s
parseJSON Value
_ = forall (f :: * -> *) a. Alternative f => f a
empty
instance Binary LaplaceDistribution where
put :: LaplaceDistribution -> Put
put (LD Double
l Double
s) = forall t. Binary t => t -> Put
put Double
l forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall t. Binary t => t -> Put
put Double
s
get :: Get LaplaceDistribution
get = do
Double
l <- forall t. Binary t => Get t
get
Double
s <- forall t. Binary t => Get t
get
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall (m :: * -> *) a. MonadFail m => [Char] -> m a
fail forall a b. (a -> b) -> a -> b
$ Double -> Double -> [Char]
errMsg Double
l Double
s) forall (m :: * -> *) a. Monad m => a -> m a
return 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 = forall a. Floating a => a -> a
exp (- forall a. Num a => a -> a
abs (Double
x forall a. Num a => a -> a -> a
- Double
l) forall a. Fractional a => a -> a -> a
/ Double
s) forall a. Fractional a => a -> a -> a
/ (Double
2 forall a. Num a => a -> a -> a
* Double
s)
logDensity :: LaplaceDistribution -> Double -> Double
logDensity (LD Double
l Double
s) Double
x = - forall a. Num a => a -> a
abs (Double
x forall a. Num a => a -> a -> a
- Double
l) forall a. Fractional a => a -> a -> a
/ Double
s forall a. Num a => a -> a -> a
- forall a. Floating a => a -> a
log Double
2 forall a. Num a => a -> a -> a
- 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 forall a. Num a => a -> a -> a
* Double
s forall a. Num a => a -> a -> a
* Double
s
instance D.MaybeMean LaplaceDistribution where
maybeMean :: LaplaceDistribution -> Maybe Double
maybeMean = forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall d. Mean d => d -> Double
D.mean
instance D.MaybeVariance LaplaceDistribution where
maybeStdDev :: LaplaceDistribution -> Maybe Double
maybeStdDev = forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall d. Variance d => d -> Double
D.stdDev
maybeVariance :: LaplaceDistribution -> Maybe Double
maybeVariance = forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall d. Variance d => d -> Double
D.variance
instance D.Entropy LaplaceDistribution where
entropy :: LaplaceDistribution -> Double
entropy (LD Double
_ Double
s) = Double
1 forall a. Num a => a -> a -> a
+ forall a. Floating a => a -> a
log (Double
2 forall a. Num a => a -> a -> a
* Double
s)
instance D.MaybeEntropy LaplaceDistribution where
maybeEntropy :: LaplaceDistribution -> Maybe Double
maybeEntropy = forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall d. Entropy d => d -> Double
D.entropy
instance D.ContGen LaplaceDistribution where
genContVar :: forall g (m :: * -> *).
StatefulGen g m =>
LaplaceDistribution -> g -> m Double
genContVar = 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 forall a. Ord a => a -> a -> Bool
<= Double
l = Double
0.5 forall a. Num a => a -> a -> a
* forall a. Floating a => a -> a
exp ( (Double
x forall a. Num a => a -> a -> a
- Double
l) forall a. Fractional a => a -> a -> a
/ Double
s)
| Bool
otherwise = Double
1 forall a. Num a => a -> a -> a
- Double
0.5 forall a. Num a => a -> a -> a
* forall a. Floating a => a -> a
exp ( - (Double
x forall a. Num a => a -> a -> a
- Double
l) 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 forall a. Ord a => a -> a -> Bool
<= Double
l = Double
1 forall a. Num a => a -> a -> a
- Double
0.5 forall a. Num a => a -> a -> a
* forall a. Floating a => a -> a
exp ( (Double
x forall a. Num a => a -> a -> a
- Double
l) forall a. Fractional a => a -> a -> a
/ Double
s)
| Bool
otherwise = Double
0.5 forall a. Num a => a -> a -> a
* forall a. Floating a => a -> a
exp ( - (Double
x forall a. Num a => a -> a -> a
- Double
l) 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 forall a. Eq a => a -> a -> Bool
== Double
0 = -Double
inf
| Double
p forall a. Eq a => a -> a -> Bool
== Double
1 = Double
inf
| Double
p forall a. Eq a => a -> a -> Bool
== Double
0.5 = Double
l
| Double
p forall a. Ord a => a -> a -> Bool
> Double
0 Bool -> Bool -> Bool
&& Double
p forall a. Ord a => a -> a -> Bool
< Double
0.5 = Double
l forall a. Num a => a -> a -> a
+ Double
s forall a. Num a => a -> a -> a
* forall a. Floating a => a -> a
log (Double
2 forall a. Num a => a -> a -> a
* Double
p)
| Double
p forall a. Ord a => a -> a -> Bool
> Double
0.5 Bool -> Bool -> Bool
&& Double
p forall a. Ord a => a -> a -> Bool
< Double
1 = Double
l forall a. Num a => a -> a -> a
- Double
s forall a. Num a => a -> a -> a
* forall a. Floating a => a -> a
log (Double
2 forall a. Num a => a -> a -> a
- Double
2 forall a. Num a => a -> a -> a
* Double
p)
| Bool
otherwise =
forall a. HasCallStack => [Char] -> a
error forall a b. (a -> b) -> a -> b
$ [Char]
"Statistics.Distribution.Laplace.quantile: p must be in [0,1] range. Got: "forall a. [a] -> [a] -> [a]
++forall a. Show a => a -> [Char]
show Double
p
where
inf :: Double
inf = Double
1 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 forall a. Eq a => a -> a -> Bool
== Double
0 = Double
inf
| Double
p forall a. Eq a => a -> a -> Bool
== Double
1 = -Double
inf
| Double
p forall a. Eq a => a -> a -> Bool
== Double
0.5 = Double
l
| Double
p forall a. Ord a => a -> a -> Bool
> Double
0 Bool -> Bool -> Bool
&& Double
p forall a. Ord a => a -> a -> Bool
< Double
0.5 = Double
l forall a. Num a => a -> a -> a
- Double
s forall a. Num a => a -> a -> a
* forall a. Floating a => a -> a
log (Double
2 forall a. Num a => a -> a -> a
* Double
p)
| Double
p forall a. Ord a => a -> a -> Bool
> Double
0.5 Bool -> Bool -> Bool
&& Double
p forall a. Ord a => a -> a -> Bool
< Double
1 = Double
l forall a. Num a => a -> a -> a
+ Double
s forall a. Num a => a -> a -> a
* forall a. Floating a => a -> a
log (Double
2 forall a. Num a => a -> a -> a
- Double
2 forall a. Num a => a -> a -> a
* Double
p)
| Bool
otherwise =
forall a. HasCallStack => [Char] -> a
error forall a b. (a -> b) -> a -> b
$ [Char]
"Statistics.Distribution.Laplace.quantile: p must be in [0,1] range. Got: "forall a. [a] -> [a] -> [a]
++forall a. Show a => a -> [Char]
show Double
p
where
inf :: Double
inf = Double
1 forall a. Fractional a => a -> a -> a
/ Double
0
laplace :: Double
-> Double
-> LaplaceDistribution
laplace :: Double -> Double -> LaplaceDistribution
laplace Double
l Double
s = forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall a. HasCallStack => [Char] -> a
error forall a b. (a -> b) -> a -> b
$ Double -> Double -> [Char]
errMsg Double
l Double
s) forall a. a -> a
id 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 forall a. Ord a => a -> a -> Bool
>= Double
0 = forall a. a -> Maybe a
Just (Double -> Double -> LaplaceDistribution
LD Double
l Double
s)
| Bool
otherwise = forall a. Maybe a
Nothing
errMsg :: Double -> Double -> String
errMsg :: Double -> Double -> [Char]
errMsg Double
_ Double
s = [Char]
"Statistics.Distribution.Laplace.laplace: scale parameter must be positive. Got " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> [Char]
show Double
s
instance D.FromSample LaplaceDistribution Double where
fromSample :: forall (v :: * -> *).
Vector v Double =>
v Double -> Maybe LaplaceDistribution
fromSample v Double
xs
| forall (v :: * -> *) a. Vector v a => v a -> Bool
G.null v Double
xs = forall a. Maybe a
Nothing
| Bool
otherwise = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$! Double -> Double -> LaplaceDistribution
LD Double
s Double
l
where
s :: Double
s = forall (v :: * -> *).
Vector v Double =>
ContParam -> v Double -> Double
Q.median ContParam
Q.medianUnbiased v Double
xs
l :: Double
l = forall (v :: * -> *). Vector v Double => v Double -> Double
S.mean forall a b. (a -> b) -> a -> b
$ forall (v :: * -> *) a b.
(Vector v a, Vector v b) =>
(a -> b) -> v a -> v b
G.map (\Double
x -> forall a. Num a => a -> a
abs forall a b. (a -> b) -> a -> b
$ Double
x forall a. Num a => a -> a -> a
- Double
s) v Double
xs