Skip to content

Commit

Permalink
Add laplaceFromSample method.
Browse files Browse the repository at this point in the history
Allows estimating the parameters of a Laplace distribution based on
a sample (location is the sample median and scale is the average
mean deviation from the location). No checks are made to test that
the sample comes from a Laplace distribution.

With this, the Statistics.Distribution.Laplace module closely mimics the
Statistics.Distribution.Exponential one.
  • Loading branch information
mihaimaruseac authored and Shimuuar committed May 22, 2015
1 parent a31d2a5 commit f3a1bbb
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions Statistics/Distribution/Laplace.hs
Expand Up @@ -21,6 +21,7 @@ module Statistics.Distribution.Laplace
LaplaceDistribution
-- * Constructors
, laplace
, laplaceFromSample
-- * Accessors
, ldLocation
, ldScale
Expand All @@ -30,7 +31,11 @@ import Data.Aeson (FromJSON, ToJSON)
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.Types (Sample)
import Control.Applicative ((<$>), (<*>))


Expand Down Expand Up @@ -109,3 +114,11 @@ laplace l s
| s <= 0 =
error $ "Statistics.Distribution.Laplace.laplace: scale parameter must be positive. Got " ++ show s
| otherwise = LD l s

-- | Create Laplace distribution from sample. No tests are made to
-- check whether it truly is Laplace.
laplaceFromSample :: Sample -> LaplaceDistribution
laplaceFromSample xs = LD s l
where
s = Q.continuousBy Q.medianUnbiased 1 2 xs
l = S.mean $ G.map (\x -> abs $ x - s) xs

0 comments on commit f3a1bbb

Please sign in to comment.