deanie

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

Rejection.hs (605B)


      1 
      2 module Deanie.Inference.Rejection (
      3 
      4     rejection
      5   , grejection
      6   ) where
      7 
      8 import Control.Monad
      9 import qualified Data.Foldable as F
     10 
     11 grejection
     12   :: (Foldable f, Monad m)
     13   => ([a] -> [b] -> Bool) -> f b -> m c -> (c -> m a) -> m c
     14 grejection predicate observed proposal model = loop where
     15   len  = length observed
     16   loop = do
     17     parameters <- proposal
     18     generated  <- replicateM len (model parameters)
     19     if   predicate generated (F.toList observed)
     20     then return parameters
     21     else loop
     22 
     23 rejection :: (Foldable f, Monad m, Eq a) => f a -> m b -> (b -> m a) -> m b
     24 rejection = grejection (==)
     25