Debian Codegraph @

haskell/MCMC/Moves/MH.hs

raw
1 module MCMC.Moves.MH where
2
3 import Probability.Dist -- for several things in propose
4
5 import MCMC.Types
6 import MCMC.Moves.Context
7
8 foreign import bpcall "MCMC:" acceptMH :: ContextIndex -> ContextIndex -> LogDouble -> IO Bool
9
10 metropolisHastings :: Proposal -> TransitionKernel
11 metropolisHastings (Proposal proposal) = TransitionKernel $ \c1 -> do
12 c2 <- copyContext c1
13 ratio <- proposal c2
14 accept <- acceptMH c1 c2 ratio
15 if accept then switchToContext c1 c2 else return ()
16 releaseContext c2
17 return () -- Should we allow `return accept`?
18
19 propose :: (Dist d, IOSampleable d, HasPdf d, Result d ~ a) => Modifiable a -> (a -> d) -> Proposal
20 propose x dist = Proposal $ \c -> do
21 x1 <- getAtomicModifiableValueInContext x c -- This is guaranteed to be an Int, Double, LogDouble, Char, or Object.
22 -- x has to be an atomic modifiable. We can't use it to propose trees...
23 x2 <- sampleIO (dist x1)
24 setAtomicModifiableValueInContext x x2 c
25 let rho12 = pdf (dist x1) x2
26 rho21 = pdf (dist x2) x1
27 hastingsRatio = rho21/rho12
28 return hastingsRatio
29