packages feed

random-fu 0.2.2.0 → 0.2.3.0

raw patch · 3 files changed

+63/−5 lines, 3 files

Files

random-fu.cabal view
@@ -1,5 +1,5 @@ name:                   random-fu-version:                0.2.2.0+version:                0.2.3.0 stability:              provisional  cabal-version:          >= 1.6@@ -29,6 +29,9 @@                         a fair bit slower than straight C implementations of                          the same algorithms.                         .+                        Changes in 0.2.3.0: Added stretched exponential distribution,+                        contributed by Ben Gamari.+                        .                         Changes in 0.2.2.0: Bug fixes in Data.Random.Distribution.Categorical.                         .                         Changes in 0.2.1.1: Changed some one-field data types@@ -46,10 +49,6 @@                         \"Buildable: False\" under GHC 7.2.1 (see                          flexible-defaults 0.0.0.2 for more detailed                         explanation).-                        .-                        Changes in 0.2: The old random-fu package has been -                        split into three parts: random-source, rvar, and this-                        new random-fu.  The end-user interface is mostly the same.                          tested-with:            GHC == 6.10.4, GHC == 6.12.1, GHC == 6.12.3,                         GHC == 7.0.1, GHC == 7.0.2, GHC == 7.0.4,@@ -78,9 +77,11 @@                         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+                        Data.Random.Distribution.Pareto                         Data.Random.Distribution.Poisson                         Data.Random.Distribution.Rayleigh                         Data.Random.Distribution.Triangular
+ src/Data/Random/Distribution/Pareto.hs view
@@ -0,0 +1,25 @@+{-# LANGUAGE FlexibleContexts #-}+{-# LANGUAGE FlexibleInstances #-}+{-# LANGUAGE MultiParamTypeClasses #-}+{-# LANGUAGE UndecidableInstances #-}+module Data.Random.Distribution.Pareto where++import Data.Random++pareto :: Distribution Pareto a => a -> a -> RVar a+pareto xM a = rvar (Pareto xM a)++paretoT :: Distribution Pareto a => a -> a -> RVarT m a+paretoT xM a = rvarT (Pareto xM a)++data Pareto a = Pareto !a !a++instance (Floating a, Distribution StdUniform a) => Distribution Pareto a where+    rvarT (Pareto xM a) = do+        u <- stdUniformT+        return (xM / (1 - u) ** recip a)++instance (Real a, Distribution Pareto a) => CDF Pareto a where+    cdf (Pareto xM a) x+         | x >= xM      = 1 - (realToFrac xM / realToFrac x) ** realToFrac a+         | otherwise    = 0
+ src/Data/Random/Distribution/StretchedExponential.hs view
@@ -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