commit 5419fee6f81e897df97fb71fc5b085e66e4f4538
parent 0f4f58b2aa1d0e9731555645d299f0a5d93d2377
Author: Marco Zocca <marco.zocca@recordunion.com>
Date: Thu, 25 Jan 2018 17:46:30 +0100
add Laplace distribution
Diffstat:
1 file changed, 9 insertions(+), 0 deletions(-)
diff --git a/src/System/Random/MWC/Probability.hs b/src/System/Random/MWC/Probability.hs
@@ -58,6 +58,7 @@ module System.Random.MWC.Probability (
, normal
, logNormal
, exponential
+ , laplace
, gamma
, inverseGamma
, chiSquare
@@ -188,6 +189,14 @@ exponential :: PrimMonad m => Double -> Prob m Double
exponential r = Prob $ MWC.Dist.exponential r
{-# INLINABLE exponential #-}
+-- | The Laplace distribution with provided location and scale parameters.
+laplace :: (Floating a, Variate a, PrimMonad m) => a -> a -> Prob m a
+laplace mu sigma = do
+ u <- uniformR (-0.5, 0.5)
+ let b = sigma / sqrt 2
+ return $ mu - b * signum u * log (1 - 2 * abs u)
+
+
-- | The gamma distribution with shape parameter a and scale parameter b.
--
-- This is the parameterization used more traditionally in frequentist