From dd6cf5fcd5784879a99731eb71d6b23a6b3827f5 Mon Sep 17 00:00:00 2001 From: Ben Gamari Date: Thu, 31 May 2012 21:32:30 -0400 Subject: [PATCH] Add stretched exponential distribution --- random-fu/random-fu.cabal | 1 + .../Distribution/StretchedExponential.hs | 32 +++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 random-fu/src/Data/Random/Distribution/StretchedExponential.hs diff --git a/random-fu/random-fu.cabal b/random-fu/random-fu.cabal index 1767173..6879cda 100644 --- a/random-fu/random-fu.cabal +++ b/random-fu/random-fu.cabal @@ -78,6 +78,7 @@ Library Data.Random.Distribution.ChiSquare Data.Random.Distribution.Dirichlet Data.Random.Distribution.Exponential + Data.Random.Distribution.StretchedExponential Data.Random.Distribution.Gamma Data.Random.Distribution.Multinomial Data.Random.Distribution.Normal diff --git a/random-fu/src/Data/Random/Distribution/StretchedExponential.hs b/random-fu/src/Data/Random/Distribution/StretchedExponential.hs new file mode 100644 index 0000000..9e70d54 --- /dev/null +++ b/random-fu/src/Data/Random/Distribution/StretchedExponential.hs @@ -0,0 +1,32 @@ +{-# LANGUAGE + MultiParamTypeClasses, + FlexibleInstances, FlexibleContexts, + UndecidableInstances + #-} + +module Data.Random.Distribution.StretchedExponential where + +import Data.Random.RVar +import Data.Random.Distribution +import Data.Random.Distribution.Uniform + +newtype StretchedExponential a = StretchedExp (a,a) + +floatingStretchedExponential :: (Floating a, Distribution StdUniform a) => a -> a -> RVarT m a +floatingStretchedExponential beta lambdaRecip = do + x <- stdUniformT + return (negate (log (1-x))**(1/beta) * lambdaRecip) + +floatingStretchedExponentialCDF :: Real a => a -> a -> a -> Double +floatingStretchedExponentialCDF beta lambdaRecip x = 1 - exp (negate (realToFrac x / realToFrac lambdaRecip)**(realToFrac beta)) + +stretchedExponential :: Distribution StretchedExponential a => a -> a -> RVar a +stretchedExponential beta lambdaRecip = rvar $ StretchedExp (beta, lambdaRecip) + +stretchedExponentialT :: Distribution StretchedExponential a => a -> a -> RVarT m a +stretchedExponentialT beta lambdaRecip = rvarT $ StretchedExp (beta, lambdaRecip) + +instance (Floating a, Distribution StdUniform a) => Distribution StretchedExponential a where + rvarT (StretchedExp (beta,lambdaRecip)) = floatingStretchedExponential beta lambdaRecip +instance (Real a, Distribution StretchedExponential a) => CDF StretchedExponential a where + cdf (StretchedExp (beta,lambdaRecip)) = floatingStretchedExponentialCDF beta lambdaRecip \ No newline at end of file