deanie

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

Inference.hs (968B)


      1 
      2 module Deanie.Inference (
      3 
      4   -- * functions
      5 
      6     mc
      7   , mcw
      8 
      9   -- * modules
     10 
     11   , module Export
     12   ) where
     13 
     14 import qualified Control.Foldl as L
     15 import Data.Monoid
     16 import Deanie.Inference.Importance as Export
     17 import Deanie.Inference.Metropolis as Export
     18 import Deanie.Inference.Rejection as Export
     19 
     20 data Average a = Average !a !a
     21 
     22 instance Fractional a => Monoid (Average a) where
     23   mempty = Average 0 0
     24   mappend (Average nL xL) (Average nR xR) = Average n x where
     25     n = nL + nR
     26     x = (xL * nL + xR * nR) / (nL + nR)
     27 
     28 average :: Fractional a => L.Fold a a
     29 average = L.Fold tally mempty summarize where
     30   tally x a = x <> Average 1 a
     31   summarize (Average _ x) = x
     32 {-# INLINE average #-}
     33 
     34 mc :: (Foldable f, Fractional a) => f a -> a
     35 mc = L.fold average
     36 {-# INLINE mc #-}
     37 
     38 mcw :: (Foldable f, Fractional a) => f (a, a) -> a
     39 mcw pts = L.fold (L.premap weight L.sum) pts where
     40   mass = L.fold (L.premap fst L.sum) pts
     41   weight (w, v) = w / mass * v
     42 {-# INLINE mcw #-}
     43