    {-# LANGUAGE MultiParamTypeClasses #-}
    {-# LANGUAGE OverloadedStrings #-}
    {-# LANGUAGE DeriveDataTypeable, DeriveGeneric #-}
    -- |
    -- Module    : Statistics.Distribution.Laplace
    -- Copyright : (c) 2015 Mihai Maruseac
    -- License   : BSD3
    --
    -- Maintainer  : mihai.maruseac@maruseac.com
    -- Stability   : experimental
    -- Portability : portable
    --
    -- The Laplace distribution.  This is the continuous probability
    -- defined as the difference of two iid exponential random variables
    -- or a Brownian motion evaluated as exponentially distributed times.
    -- It is used in differential privacy (Laplace Method), speech
    -- recognition and least absolute deviations method (Laplace's first
    -- law of errors, giving a robust regression method)
    --
    module Statistics.Distribution.Laplace
        (
          LaplaceDistribution
        -- * Constructors
        , laplace
        , laplaceE
        -- * Accessors
        , 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
        -- ^ Location.
        , LaplaceDistribution -> Double
    ldScale    :: {-# UNPACK #-} !Double
        -- ^ Scale.
        } deriving (LaplaceDistribution -> LaplaceDistribution -> Bool
    (LaplaceDistribution -> LaplaceDistribution -> Bool)
    -> (LaplaceDistribution -> LaplaceDistribution -> Bool)
    -> Eq LaplaceDistribution
    forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
    $c== :: LaplaceDistribution -> LaplaceDistribution -> Bool
    == :: LaplaceDistribution -> LaplaceDistribution -> Bool
    $c/= :: LaplaceDistribution -> LaplaceDistribution -> Bool
    /= :: LaplaceDistribution -> LaplaceDistribution -> Bool
    Eq, Typeable, Typeable LaplaceDistribution
    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 -> Constr
    LaplaceDistribution -> DataType
    (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)
    $cgfoldl :: forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g)
    -> LaplaceDistribution
    -> c LaplaceDistribution
    gfoldl :: forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g)
    -> LaplaceDistribution
    -> c LaplaceDistribution
    $cgunfold :: forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c LaplaceDistribution
    gunfold :: forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c LaplaceDistribution
    $ctoConstr :: LaplaceDistribution -> Constr
    toConstr :: LaplaceDistribution -> Constr
    $cdataTypeOf :: LaplaceDistribution -> DataType
    dataTypeOf :: LaplaceDistribution -> DataType
    $cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c LaplaceDistribution)
    dataCast1 :: forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c LaplaceDistribution)
    $cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e))
    -> Maybe (c LaplaceDistribution)
    dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e))
    -> Maybe (c LaplaceDistribution)
    $cgmapT :: (forall b. Data b => b -> b)
    -> LaplaceDistribution -> LaplaceDistribution
    gmapT :: (forall b. Data b => b -> b)
    -> LaplaceDistribution -> LaplaceDistribution
    $cgmapQl :: 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
    $cgmapQr :: forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> LaplaceDistribution -> r
    gmapQr :: forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> LaplaceDistribution -> r
    $cgmapQ :: forall u.
    (forall d. Data d => d -> u) -> LaplaceDistribution -> [u]
    gmapQ :: forall u.
    (forall d. Data d => d -> u) -> LaplaceDistribution -> [u]
    $cgmapQi :: forall u.
    Int -> (forall d. Data d => d -> u) -> LaplaceDistribution -> u
    gmapQi :: forall u.
    Int -> (forall d. Data d => d -> u) -> LaplaceDistribution -> u
    $cgmapM :: forall (m :: * -> *).
    Monad 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
    $cgmapMp :: 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
    $cgmapMo :: forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d)
    -> LaplaceDistribution -> m LaplaceDistribution
    gmapMo :: forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d)
    -> LaplaceDistribution -> m 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
    $cfrom :: forall x. LaplaceDistribution -> Rep LaplaceDistribution x
    from :: forall x. LaplaceDistribution -> Rep LaplaceDistribution x
    $cto :: forall x. Rep LaplaceDistribution x -> LaplaceDistribution
    to :: forall x. Rep LaplaceDistribution x -> LaplaceDistribution
    Generic)

    instance Show LaplaceDistribution where
      showsPrec :: Int -> LaplaceDistribution -> ShowS
    showsPrec Int
    i (LD Double
    l Double
    s) = [Char] -> Double -> Double -> Int -> ShowS
    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 = [Char]
    -> (Double -> Double -> Maybe LaplaceDistribution)
    -> ReadPrec LaplaceDistribution
    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
        l <- Object
    v Object -> Key -> Parser Double
    forall a. FromJSON a => Object -> Key -> Parser a
    .: Key
    "ldLocation"
        s <- v .: "ldScale"
        maybe (fail $ errMsg l s) return $ laplaceE l s
      parseJSON Value
    _ = Parser LaplaceDistribution
    forall a. Parser a
    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 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
    s
      get :: Get LaplaceDistribution
    get = do
        l <- Get Double
    forall t. Binary t => Get t
    get
        s <- get
        maybe (fail $ errMsg l s) return $ laplaceE l 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 :: forall g (m :: * -> *).
    StatefulGen g m =>
    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          =
        [Char] -> Double
    forall a. HasCallStack => [Char] -> a
    error ([Char] -> Double) -> [Char] -> Double
    forall a b. (a -> b) -> a -> b
    $ [Char]
    "Statistics.Distribution.Laplace.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
        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          =
        [Char] -> Double
    forall a. HasCallStack => [Char] -> a
    error ([Char] -> Double) -> [Char] -> Double
    forall a b. (a -> b) -> a -> b
    $ [Char]
    "Statistics.Distribution.Laplace.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
        inf :: Double
    inf = Double
    1 Double -> Double -> Double
    forall a. Fractional a => a -> a -> a
    / Double
    0

    -- | Create an Laplace distribution.
    laplace :: Double         -- ^ Location
            -> Double        -- ^ Scale
            -> 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 ([Char] -> LaplaceDistribution
    forall a. HasCallStack => [Char] -> a
    error ([Char] -> LaplaceDistribution) -> [Char] -> LaplaceDistribution
    forall a b. (a -> b) -> a -> b
    $ Double -> Double -> [Char]
    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

    -- | Create an Laplace distribution.
    laplaceE :: Double         -- ^ Location
             -> Double        -- ^ Scale
             -> 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 -> [Char]
    errMsg Double
    _ Double
    s = [Char]
    "Statistics.Distribution.Laplace.laplace: scale parameter must be positive. Got " [Char] -> ShowS
    forall a. [a] -> [a] -> [a]
    ++ Double -> [Char]
    forall a. Show a => a -> [Char]
    show Double
    s

    -- | Create Laplace distribution from sample.  The location is estimated
    --   as the median of the sample, and the scale as the mean absolute
    --   deviation of the median.
    instance D.FromSample LaplaceDistribution Double where
      fromSample :: forall (v :: * -> *).
    Vector v Double =>
    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