random-fu 0.2.4.0 → 0.2.5.0
raw patch · 5 files changed
+55/−7 lines, 5 filesdep +math-functionsdep −gamma
Dependencies added: math-functions
Dependencies removed: gamma
Files
- random-fu.cabal +3/−2
- src/Data/Random/Distribution/ChiSquare.hs +2/−2
- src/Data/Random/Distribution/Gamma.hs +3/−3
- src/Data/Random/Distribution/T.hs +46/−0
- src/Data/Random/Distribution/Uniform.hs +1/−0
random-fu.cabal view
@@ -1,5 +1,5 @@ name: random-fu-version: 0.2.4.0+version: 0.2.5.0 stability: provisional cabal-version: >= 1.6@@ -76,6 +76,7 @@ Data.Random.Distribution.Pareto Data.Random.Distribution.Poisson Data.Random.Distribution.Rayleigh+ Data.Random.Distribution.T Data.Random.Distribution.Triangular Data.Random.Distribution.Uniform Data.Random.Distribution.Weibull@@ -99,7 +100,7 @@ else build-depends: mtl == 1.* - build-depends: gamma,+ build-depends: math-functions, monad-loops >= 0.3.0.1, random-shuffle, random-source == 0.3.*,
src/Data/Random/Distribution/ChiSquare.hs view
@@ -8,7 +8,7 @@ import Data.Random.Distribution import Data.Random.Distribution.Gamma -import Math.Gamma (p)+import Numeric.SpecFunctions chiSquare :: Distribution ChiSquare t => Integer -> RVar t chiSquare = rvar . ChiSquare@@ -25,4 +25,4 @@ | otherwise = fail "chi-square distribution: degrees of freedom must be positive" instance (Real t, Distribution ChiSquare t) => CDF ChiSquare t where- cdf (ChiSquare n) x = p (0.5 * fromInteger n) (0.5 * realToFrac x)+ cdf (ChiSquare n) x = incompleteGamma (0.5 * fromInteger n) (0.5 * realToFrac x)
src/Data/Random/Distribution/Gamma.hs view
@@ -21,7 +21,7 @@ import Data.Ratio -import Math.Gamma (p)+import Numeric.SpecFunctions -- |derived from Marsaglia & Tang, "A Simple Method for generating gamma -- variables", ACM Transactions on Mathematical Software, Vol 26, No 3 (2000), p363-372.@@ -80,11 +80,11 @@ rvarT (Gamma a b) = mtGamma a b instance (Real a, Distribution Gamma a) => CDF Gamma a where- cdf (Gamma a b) x = p (realToFrac a) (realToFrac x / realToFrac b)+ cdf (Gamma a b) x = incompleteGamma (realToFrac a) (realToFrac x / realToFrac b) instance (Integral a, Floating b, Ord b, Distribution Normal b, Distribution StdUniform b) => Distribution (Erlang a) b where rvarT (Erlang a) = mtGamma (fromIntegral a) 1 instance (Integral a, Real b, Distribution (Erlang a) b) => CDF (Erlang a) b where- cdf (Erlang a) x = p (fromIntegral a) (realToFrac x)+ cdf (Erlang a) x = incompleteGamma (fromIntegral a) (realToFrac x)
+ src/Data/Random/Distribution/T.hs view
@@ -0,0 +1,46 @@+{-# LANGUAGE MultiParamTypeClasses #-}+{-# LANGUAGE FlexibleInstances #-}+{-# LANGUAGE FlexibleContexts #-}+{-# LANGUAGE UndecidableInstances #-}++{-# OPTIONS_GHC -Wall #-}+{-# OPTIONS_GHC -fno-warn-name-shadowing #-}+{-# OPTIONS_GHC -fno-warn-type-defaults #-}+{-# OPTIONS_GHC -fno-warn-unused-do-bind #-}+{-# OPTIONS_GHC -fno-warn-missing-methods #-}+{-# OPTIONS_GHC -fno-warn-orphans #-}++module Data.Random.Distribution.T where++import Data.RVar+import Data.Random.Distribution+import Data.Random.Distribution.ChiSquare+import Data.Random.Distribution.Normal++import Numeric.SpecFunctions++t :: Distribution T a => Integer -> RVar a+t = rvar . T++tT :: Distribution T a => Integer -> RVarT m a+tT = rvarT . T++newtype T a = T Integer+ deriving (Eq, Ord, Show)++instance (Floating a, Distribution Normal a, Distribution ChiSquare a) => Distribution T a where+ rvarT (T n)+ | n > 0 = do+ x <- stdNormalT+ y <- chiSquareT n+ return (x * sqrt (fromInteger n / y))+ | otherwise = fail "Student's t-distribution: degrees of freedom must be positive"++instance (Real a, Distribution T a) => CDF T a where+ cdf (T n) t = incompleteBeta v2 v2 x+ where+ v = fromIntegral n+ v2 = 0.5 * v+ tD = realToFrac t+ u = sqrt (tD*tD + v)+ x = (tD + u) / (u + u)
src/Data/Random/Distribution/Uniform.hs view
@@ -30,6 +30,7 @@ , floatStdUniform , doubleStdUniform + , boundedStdUniformCDF , realStdUniformCDF , realUniformCDF ) where