random-variates (empty) → 0.1.0.0
raw patch · 14 files changed
+421/−0 lines, 14 filesdep +basedep +containersdep +lenssetup-changed
Dependencies added: base, containers, lens, random, reinterpret-cast
Files
- LICENSE +7/−0
- Setup.hs +2/−0
- random-variates.cabal +43/−0
- src/Helpers.hs +56/−0
- src/Stochastic/Bernoulli.hs +20/−0
- src/Stochastic/Binomial.hs +20/−0
- src/Stochastic/Distribution.hs +26/−0
- src/Stochastic/Distributions.hs +89/−0
- src/Stochastic/Exponential.hs +19/−0
- src/Stochastic/Geometric.hs +23/−0
- src/Stochastic/Normal.hs +25/−0
- src/Stochastic/Poisson.hs +24/−0
- src/Stochastic/Uniform.hs +22/−0
- src/Stochastic/ZipF.hs +45/−0
+ LICENSE view
@@ -0,0 +1,7 @@+Copyright (c) 2015 Keynan James Pratt++Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:++The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ random-variates.cabal view
@@ -0,0 +1,43 @@+-- Initial content-simulation.cabal generated by cabal init. For further +-- documentation, see http://haskell.org/cabal/users-guide/++name: random-variates+version: 0.1.0.0+synopsis: "Uniform RNG => Non-Uniform RNGs"+description: "Collection of transforms uniform random number generators (RNGs) into any of a dozen common RNGs. Each presenting several common interfaces" +license: MIT+license-file: LICENSE+author: Keynan James Pratt <keynan.pratt@gmail.com>+maintainer: Keynan James Pratt <keynan.pratt@gmail.com>+copyright: (c) 2015, Keynan James Pratt+category: Statistics+build-type: Simple+-- extra-source-files: +cabal-version: >=1.10+homepage: https://bitbucket.org/kpratt/random-variate+bug-reports: https://bitbucket.org/kpratt/random-variate/issues?status=new&status=open+source-repository head+ type: git+ location: git@bitbucket.org:kpratt/random-variate.git+ +library+ exposed-modules: Stochastic.Distribution+ Stochastic.Distributions+ Stochastic.Uniform++ other-modules: Stochastic.Bernoulli+ Stochastic.Binomial+ Stochastic.Exponential+ Stochastic.Geometric+ Stochastic.Normal+ Stochastic.Poisson+ Stochastic.ZipF+ Helpers+ build-depends: + base >=4.6 && <5.0+ , reinterpret-cast >= 0.1.0+ , containers >= 0.5.0.0+ , lens >=4.13+ , random >=1.1+ hs-source-dirs: src+ default-language: Haskell2010
+ src/Helpers.hs view
@@ -0,0 +1,56 @@+module Helpers(maybeHead,+ headOrElse,+ statefully,+ mapTuple,+ statefullyTakeWhile,+ histogram) where++import Data.Maybe+import qualified Data.Map as Map++maybeHead :: [a] -> Maybe a+maybeHead [] = Nothing+maybeHead (x:xs) = Just x++headOrElse :: a -> [a] -> a+headOrElse d ls = fromMaybe d (maybeHead ls)++statefully :: (g -> (a, g)) -> Int -> g -> ([a], g)+statefully f n g0 = case n of+ 0 -> ([], g0)+ x -> (r:rest, g2)+ where+ (r, g1) = f g0+ (rest, g2) = statefully f (n-1) g1++statefullyTakeWhile :: (g -> (a, g)) ->+ ([a] -> Bool) ->+ g ->+ ([a], g)+statefullyTakeWhile f p g0 = r ([], g0)+ where+ r (list, g1)+ | p list = (list, g1)+ | otherwise = mapTuple (\l -> l:list) (id) (f g1)+++mapTuple :: (a -> b) -> (c -> d) -> (a, c) -> (b, d) +mapTuple f g (x, y) = (f x, g y)++histogram :: Double -> [Double] -> [Int]+histogram precision ls = map lookup0 [0..limit]+ where+ limit = truncate $ (1.0 / precision)+ divs = map (\x -> x / precision) ls+ ints = map (truncate) divs+ m = foldl+ (\b a ->+ Map.insert a ((fromMaybe 0 (Map.lookup a b))+1) b)+ Map.empty+ ints+ lookup0 = (\x -> fromMaybe 0 $ Map.lookup x m)+ +toDbl = fromInteger . toInteger+++
+ src/Stochastic/Bernoulli.hs view
@@ -0,0 +1,20 @@+module Stochastic.Bernoulli(Bernoulli, mkBernoulli) where++import Stochastic.Distribution+import Stochastic.Uniform+import Helpers++data Bernoulli = Bernoulli Double Uniform++mkBernoulli :: Uniform -> Double -> Bernoulli+mkBernoulli base p = Bernoulli p base++instance DiscreteDistribution Bernoulli where+ randIntIn (a, b) (Bernoulli p g0) =+ mapTuple+ (\x -> a + ((floor $ x + (1-p)) * (b-a)) )+ (Bernoulli p)+ (randDouble g0)+ randInt g0 = randIntIn (0, 1) g0++toDbl = fromInteger . toInteger
+ src/Stochastic/Binomial.hs view
@@ -0,0 +1,20 @@+module Stochastic.Binomial(Binomial, mkBinomial) where++import Stochastic.Distribution+import Stochastic.Bernoulli+import Helpers++data Binomial = Binomial Int Bernoulli++mkBinomial :: Bernoulli -> Int -> Binomial+mkBinomial base n = Binomial n base++instance DiscreteDistribution Binomial where+ randIntIn (a, b) (Binomial n g0) =+ mapTuple+ (\xs -> (a - 1) + (sum xs))+ (Binomial n)+ (randInts (b - a + 1) g0)+ randInt (Binomial n g0) = randIntIn (0, n) (Binomial n g0)++toDbl = fromInteger . toInteger
+ src/Stochastic/Distribution.hs view
@@ -0,0 +1,26 @@+{-# LANGUAGE DefaultSignatures #-}++module Stochastic.Distribution(ContinuousDistribution(..), DiscreteDistribution(..)) where++import Helpers(histogram, statefully)++class DiscreteDistribution g where+ randInt :: g -> (Int, g)+ randInt g = randIntIn (0, maxBound::Int) g+ randInts :: Int -> g -> ([Int], g)+ randInts n g0 = statefully (randInt) n g0+ randIntIn :: (Int, Int) -> g -> (Int, g)+ default randIntIn :: (ContinuousDistribution g) =>+ (Int, Int) -> g -> (Int, g)+ randIntIn (a, b) g0 = ((ceiling (toDbl (b - a + 1) * d)) + (a-1), g1)+ where+ (d, g1) = randDouble g0+ ++class ContinuousDistribution g where+ randDouble :: g -> (Double, g)+ randDoubles :: Int -> g -> ([Double], g)+ randDoubles n g0 = statefully (randDouble) n g0+++toDbl = fromInteger . toInteger
+ src/Stochastic/Distributions.hs view
@@ -0,0 +1,89 @@+module Stochastic.Distributions(+ ContinuousDistribution(..)+ ,DiscreteDistribution(..)+ ,Distributions(..)+ ,mkDistributions+ ,stdDistributions+ ,liftD+ ,liftC+ ,liftDN+ ,liftCN+-- ,plot+ ) where++import Helpers(histogram, statefully, mapTuple)+import Stochastic.Distribution++import qualified Stochastic.Uniform as Uni+import qualified Stochastic.ZipF as Zipf+import qualified Stochastic.Geometric as Geo+import qualified Stochastic.Exponential as Exp+import qualified Stochastic.Poisson as Poi+import qualified Stochastic.Normal as Nor+import qualified Stochastic.Bernoulli as Ber+import qualified Stochastic.Binomial as Bin++data Distributions = Distributions {+ mkUniform :: Int -> Uni.Uniform+ ,mkExp :: Int -> Double -> Exp.Exponential+ ,mkNormal :: Int -> Double -> Double -> Nor.Normal++ ,mkZipF :: Int -> Int -> Double -> Zipf.ZipF+ ,mkGeometric :: Int -> Double -> Geo.Geometric+ ,mkPoisson :: Int -> Double -> Poi.Poisson+ ,mkBernoulli :: Int -> Double -> Ber.Bernoulli+ ,mkBinomial :: Int -> Double -> Int -> Bin.Binomial+}++stdDistributions = mkDistributions Uni.stdUniform++mkDistributions uniform = Distributions {+ mkUniform = uniform+ , mkZipF = apiMkZipF (uniform)+ , mkGeometric = apiMkGeometric (uniform)+ , mkExp = apiMkExp (uniform)+ , mkPoisson = apiMkPoisson (uniform)+ , mkNormal = apiMkNormal (uniform)+ , mkBernoulli = apiMkBernoulli (uniform)+ , mkBinomial = apiMkBinomial (uniform)+}++apiMkZipF mkUni seed n slope = Zipf.mkZipF (mkUni seed) n slope+apiMkGeometric mkUni seed p = Geo.mkGeometric (mkUni seed) p+apiMkExp mkUni seed y = Exp.mkExp (mkUni seed) y+apiMkPoisson mkUni seed y = Poi.mkPoisson $ apiMkExp (mkUni) seed y+apiMkNormal mkUni seed m d = Nor.mkNormal (mkUni seed) m d+apiMkBernoulli mkUni seed d = Ber.mkBernoulli (mkUni seed) d+apiMkBinomial mkUni seed d n = Bin.mkBinomial (apiMkBernoulli (mkUni) seed d) n++liftD :: (DiscreteDistribution g) => (Int, Int) -> (Int -> a) -> (g -> (a, g))+liftD range f g0 = ((f r), g1)+ where+ (r, g1) = (randIntIn range g0)++liftDN :: (DiscreteDistribution g) => [(Int, Int)] -> ([Int] -> a) -> (g -> (a, g))+liftDN ranges f g0 = mapTuple (f) (id) (h ranges g0)+ where+ h [] g1 = ([], g1)+ h (r:rs) g1 = let (s, g2) = randIntIn r g1 in mapTuple (\x -> s:x) (id) (h rs g2)++liftC :: (ContinuousDistribution g) => (Double -> a) -> (g -> (a, g))+liftC f g0 = ((f r), g1)+ where+ (r, g1) = (randDouble g0)++liftCN :: (ContinuousDistribution g) => Int -> ([Double] -> a) -> (g -> (a, g))+liftCN n f g0 = mapTuple (f) (id) (randDoubles n g0)++plot :: DiscreteDistribution g => g -> Int -> Int -> [Int]+plot g0 interval samples = []+{-+plot g n samples = map (truncate . (+0.5) . (*100))+ $ map+ (\x ->+ (toDbl x)/(toDbl samples))+ $ histogram (1.0 / (toDbl n))+ (fst (randInts g samples))+-}+toDbl = fromInteger . toInteger+
+ src/Stochastic/Exponential.hs view
@@ -0,0 +1,19 @@+module Stochastic.Exponential(Exponential, mkExp) where++import Stochastic.Distribution+import Stochastic.Uniform+import Data.Word+import Data.ReinterpretCast+import Helpers++data Exponential = Exponential Double Uniform++viaWord :: Double -> Int+viaWord w = fromInteger $ toInteger $ doubleToWord w++mkExp :: Uniform -> Double -> Exponential+mkExp base y = Exponential y base++instance ContinuousDistribution Exponential where+ randDouble (Exponential y u) =+ mapTuple (\x -> (-1.0/y) * (log $ x)) (Exponential y) (randDouble u)
+ src/Stochastic/Geometric.hs view
@@ -0,0 +1,23 @@+module Stochastic.Geometric (mkGeometric, Geometric) where++import Stochastic.Distribution+import Stochastic.Uniform+import Helpers++data Geometric = Geometric Double Uniform++mkGeometric :: Uniform -> Double -> Geometric+mkGeometric base p = Geometric p base++instance DiscreteDistribution Geometric where+ randIntIn (a, b) (Geometric p g0) = mapTuple+ (\u -> trunc $ invert u)+ (Geometric p)+ (randDouble g0)+ where+ invert u = ceiling $ (log u) / (log (1-p))+ trunc x+ | x > (b-a) = b+ | otherwise = a+x++toDbl = fromInteger . toInteger
+ src/Stochastic/Normal.hs view
@@ -0,0 +1,25 @@+module Stochastic.Normal(mkNormal, Normal) where++import Data.Maybe+import Stochastic.Distribution+import Stochastic.Uniform+import Helpers++data Normal = Normal Double Double (Maybe Double) Uniform++mkNormal :: Uniform -> Double -> Double -> Normal+mkNormal uni mean dev = Normal mean dev Nothing uni++toDbl :: Int -> Double+toDbl = fromInteger . toInteger++instance ContinuousDistribution Normal where+ randDouble (Normal mean dev m uni) = f m+ where+ f (Just x) = (x, (Normal mean dev Nothing uni))+ f Nothing = (y, (Normal mean dev (Just z) uni))+ ([u1, u2], uni') = randDoubles 2 uni+ from_u g = mean + dev * (sqrt (-2 * (log u1))) * ( g (2 * pi * u2) )+ y = from_u (sin)+ z = from_u (cos)+
+ src/Stochastic/Poisson.hs view
@@ -0,0 +1,24 @@+module Stochastic.Poisson(mkPoisson, Poisson) where++import Stochastic.Distribution+import Stochastic.Exponential+import Helpers++data Poisson = Poisson Exponential++mkPoisson :: Exponential -> Poisson+mkPoisson base = Poisson base++toDbl :: Int -> Double+toDbl = fromInteger . toInteger++instance DiscreteDistribution Poisson where+ randIntIn (a, b) (Poisson g0) = mapTuple+ (\x -> min (x+a-1) b)+ (Poisson)+ (f 0 0 g0)+ where+ f x s g1+ | s > 1 = (x-1, g1)+ | otherwise = f (x+1) (s+y) g2+ where (y, g2) = (randDouble g1)
+ src/Stochastic/Uniform.hs view
@@ -0,0 +1,22 @@+module Stochastic.Uniform (Uniform(..)+ , stdUniform) where++import Helpers+import System.Random+import Stochastic.Distribution+++instance ContinuousDistribution Uniform where+ randDouble uni = rDouble uni ++data Uniform = Uniform {+ rDouble :: (Double, Uniform)+}++stdGen2Uni gen = Uniform {+ rDouble = mapTuple (id) (stdGen2Uni) (randomR (0,1) gen)+ }++stdUniform s = stdGen2Uni (mkStdGen s)++
+ src/Stochastic/ZipF.hs view
@@ -0,0 +1,45 @@+module Stochastic.ZipF (mkZipF, ZipF) where++import Helpers+import Stochastic.Uniform+import Data.Maybe+import Stochastic.Distribution++data ZipF = ZipF Int Double Uniform++mkZipF :: Uniform -> Int -> Double -> ZipF+mkZipF base n slope = ZipF n slope base++-- index, number, cumlative+harmonics :: Double -> [(Double, Double, Double)]+harmonics s = (h 1 0)+ where+ h n acc = (n, v, v+acc) : h (n+1) (v+acc)+ where+ v = (1.0/(n**s))++toDbl = fromInteger . toInteger++f n s d = h2 $ h1 hs+ where+ hs = harmonics s+ mx = _3 $ head $ drop (n-1) $ take n $ hs+ h1 xs = headOrElse (toDbl n, 0, 0) $+ (dropWhile (\(i, v, c) -> c < (d * mx))) (take (n) xs)+ h2 (x, _, _) = truncate x+ +g = f 10 1++instance DiscreteDistribution ZipF where+ randIntIn (a, b) (ZipF n slope u0) = (f n slope d, ZipF n slope u1)+ where+ (d, u1) = randDouble u0++_1 :: (a, b, c) -> a+_1 (x, y, z) = x++_3 :: (a, b, c) -> c+_3 (x, y, z) = z+++