deanie

An embedded probabilistic programming language.
git clone git://git.jtobin.io/deanie.git
Log | Files | Refs | README | LICENSE

Density.hs (1237B)


      1 
      2 module Deanie.Density (
      3     logDensityBernoulli
      4   , logDensityBeta
      5   , logDensityGamma
      6   , logDensityGaussian
      7   ) where
      8 
      9 import Numeric.SpecFunctions (logGamma)
     10 
     11 logDensityBernoulli :: Double -> Bool -> Double
     12 logDensityBernoulli p x
     13     | p < 0 || p > 1 = log 0
     14     | x              = log p
     15     | otherwise      = log (1 - p)
     16 {-# INLINE logDensityBernoulli #-}
     17 
     18 logDensityBeta :: Double -> Double -> Double -> Double
     19 logDensityBeta a b x
     20   | x <= 0 || x >= 1 = log 0
     21   | a < 0 || b < 0   = log 0
     22   | otherwise        = (a - 1) * log x + (b - 1) * log (1 - x)
     23 {-# INLINE logDensityBeta #-}
     24 
     25 -- | The log-density for the gamma distribution with rate parameter b.
     26 --
     27 --     f(x) = b ^ a * x ^ (a - 1) * x ^ (-b * x) / Gamma(a)
     28 logDensityGamma :: Double -> Double -> Double -> Double
     29 logDensityGamma a b x
     30   | a < 0 || b < 0 = log 0
     31   | otherwise      = a * log b + (a - 1) * log x - b * x - logGamma a
     32 {-# INLINE logDensityGamma #-}
     33 
     34 -- | The density for the normal distribution with specified mean and standard
     35 --   deviation.
     36 logDensityGaussian :: Double -> Double -> Double -> Double
     37 logDensityGaussian m sd x
     38   | sd <= 0   = log 0
     39   | otherwise = negate (log sd) - (x - m) * (x - m) / (2 * sd * sd)
     40 {-# INLINE logDensityGaussian #-}
     41