0 | module Control.Monad.Bayes.Inference.RMSMC
 1 |
 2 | import Control.Monad.Bayes.Interface
 3 | import Control.Monad.Bayes.Population
 4 | import Control.Monad.Bayes.Sequential
 5 | import Control.Monad.Bayes.Weighted
 6 | import Control.Monad.Bayes.Traced.Static
 7 | import Control.Monad.Bayes.Traced.Basic
 8 | import Control.Monad.Bayes.Traced.Dynamic
 9 |
10 | ||| Resample-move Sequential Monte Carlo.
11 | export
12 | rmsmc :
13 |   MonadSample m =>
14 |   -- | number of timesteps
15 |   (n_timesteps : Nat) ->
16 |   -- | number of particles
17 |   (n_particles : Nat) ->
18 |   -- | number of Metropolis-Hastings transitions after each resampling
19 |   (n_mhsteps : Nat) ->
20 |   -- | model
21 |   Sequential (Static.Traced (Population m)) a ->
22 |   Population m a
23 | rmsmc n_timesteps n_particles n_mhsteps =
24 |   Static.marginal
25 |     . sis (composeCopies n_mhsteps mhStep . hoistT resampleSystematic) n_timesteps
26 |     . Sequential.hoistFirst (hoistT (spawn n_particles >>))
27 |
28 | ||| Resample-move Sequential Monte Carlo with a more efficient
29 | ||| tracing representation.
30 | export
31 | rmsmcBasic :
32 |   MonadSample m =>
33 |   -- | number of timesteps
34 |   (n_timesteps : Nat) ->
35 |   -- | number of particles
36 |   (n_particles : Nat) ->
37 |   -- | number of Metropolis-Hastings transitions after each resampling
38 |   (n_mhsteps : Nat) ->
39 |   -- | model
40 |   Sequential (Basic.Traced (Population m)) a ->
41 |   Population m a
42 | rmsmcBasic n_timesteps n_particles n_mhsteps =
43 |   Basic.marginal
44 |     . sis (composeCopies n_mhsteps Basic.mhStep . Basic.hoistT resampleSystematic) n_timesteps
45 |     . Sequential.hoistFirst (Basic.hoistT (spawn n_particles >>))
46 |
47 | ||| A variant of resample-move Sequential Monte Carlo
48 | ||| where only random variables since last resampling are considered
49 | ||| for rejuvenation.
50 | export
51 | rmsmcLocal :
52 |   MonadSample m =>
53 |   -- | number of timesteps
54 |   (n_timesteps : Nat) ->
55 |   -- | number of particles
56 |   (n_particles : Nat) ->
57 |   -- | number of Metropolis-Hastings transitions after each resampling
58 |   (n_mhsteps : Nat) ->
59 |   -- | model
60 |   Sequential (Dynamic.Traced (Population m)) a ->
61 |   Population m a
62 | rmsmcLocal n_timesteps n_particles n_mhsteps =
63 |   Dynamic.marginal
64 |     . sis (Dynamic.freeze . composeCopies n_mhsteps Dynamic.mhStep . Dynamic.hoistT resampleSystematic) n_timesteps
65 |     . Sequential.hoistFirst (Dynamic.hoistT (spawn n_particles >>))
66 |