measurable

A simple shallowly-embedded DSL for dealing with measures.
git clone git://git.jtobin.io/measurable.git
Log | Files | Refs | README | LICENSE

Measures.hs (1740B)


      1 
      2 module Measurable.Measures where
      3 
      4 import Measurable.Core
      5 import Numeric.SpecFunctions (choose)
      6 import Statistics.Distribution
      7 import qualified Statistics.Distribution.Beta as Statistics
      8 import qualified Statistics.Distribution.Binomial as Statistics
      9 import qualified Statistics.Distribution.ChiSquared as Statistics
     10 import qualified Statistics.Distribution.Gamma as Statistics
     11 import qualified Statistics.Distribution.Exponential as Statistics
     12 import qualified Statistics.Distribution.Normal as Statistics
     13 
     14 standard :: Measure Double
     15 standard = fromDensityFunction pdf where
     16   pdf = density Statistics.standard
     17 
     18 normal :: Double -> Double -> Measure Double
     19 normal m s = fromDensityFunction pdf where
     20   pdf = density $ Statistics.normalDistr m s
     21 
     22 logNormal :: Double -> Double -> Measure Double
     23 logNormal m s = fmap exp (normal m s)
     24 
     25 exponential :: Double -> Measure Double
     26 exponential r = fromDensityFunction pdf where
     27   pdf = density $ Statistics.exponential r
     28 
     29 gamma :: Double -> Double -> Measure Double
     30 gamma a b = fromDensityFunction pdf where
     31   pdf = density $ Statistics.gammaDistr a b
     32 
     33 inverseGamma :: Double -> Double -> Measure Double
     34 inverseGamma a b = fmap recip (gamma a b)
     35 
     36 chiSquare :: Int -> Measure Double
     37 chiSquare k = fromDensityFunction pdf where
     38   pdf = density $ Statistics.chiSquared k
     39 
     40 beta :: Double -> Double -> Measure Double
     41 beta a b = fromDensityFunction pdf where
     42   pdf = density $ Statistics.betaDistr a b
     43 
     44 binomial :: Int -> Double -> Measure Int
     45 binomial n p = fromMassFunction pmf [0..n] where
     46   pmf = binomialMass n p
     47 
     48 bernoulli :: Double -> Measure Int
     49 bernoulli = binomial 1
     50 
     51 binomialMass :: Int -> Double -> Int -> Double
     52 binomialMass n p k = bc * p ^ k * (1 - p) ^ (n - k) where
     53   bc = n `choose` k
     54