{-# LANGUAGE BangPatterns #-}
module Mcmc.Prior
(
positive,
negative,
uniform,
normal,
exponential,
gamma,
product',
)
where
import Control.Monad
import Data.Maybe (fromMaybe)
import Numeric.Log
import qualified Statistics.Distribution as S
import qualified Statistics.Distribution.Exponential as S
import qualified Statistics.Distribution.Gamma as S
import qualified Statistics.Distribution.Normal as S
positive :: Double -> Log Double
positive x
| x <= 0 = 0
| otherwise = 1
negative :: Double -> Log Double
negative x
| x >= 0 = 0
| otherwise = 1
uniform ::
Double ->
Double ->
Double ->
Log Double
uniform a b x
| x <= a = 0
| x >= b = 0
| otherwise = Exp 0
normal ::
Double ->
Double ->
Double ->
Log Double
normal m s x = Exp $ S.logDensity d x
where
d = S.normalDistr m s
exponential ::
Double ->
Double ->
Log Double
exponential l x = Exp $ S.logDensity d x
where
d = S.exponential l
gamma ::
Double ->
Double ->
Double ->
Log Double
gamma k t x = Exp $ S.logDensity d x
where
d = S.gammaDistr k t
product' :: [Log Double] -> Log Double
product' = fromMaybe 0 . prodM
prodM :: [Log Double] -> Maybe (Log Double)
prodM = foldM (\ !acc x -> (acc * x) <$ guard (acc /= 0)) 1