0 | module Control.Monad.Bayes.Inference.SMC
 1 |
 2 | import Control.Monad.Bayes.Interface
 3 | import public Control.Monad.Bayes.Population
 4 | import public Control.Monad.Bayes.Sequential
 5 | import Control.Monad.Bayes.Weighted
 6 |
 7 | ||| Sequential importance resampling.
 8 | -- An SMC template that takes a custom resampler.
 9 | export
10 | sir :
11 |   (Monad m) =>
12 |   -- | resampler
13 |   (forall x. Population m x -> Population m x) ->
14 |   -- | number of timesteps
15 |   (n_timesteps : Nat) ->
16 |   -- | population size
17 |   (n_particles : Nat) ->
18 |   -- | model
19 |   Sequential (Population m) a ->
20 |   Population m a
21 | sir resampler n_timesteps n_particles = sis resampler n_timesteps . Sequential.hoistFirst (spawn n_particles >>)
22 |
23 | ||| Sequential Monte Carlo with multinomial resampling at each timestep.
24 | -- Weights are not normalized.
25 | export
26 | smcMultinomial :
27 |   MonadSample m =>
28 |   -- | number of timesteps
29 |   (n_timesteps : Nat) ->
30 |   -- | population size
31 |   (n_particles : Nat) ->
32 |   -- | model
33 |   Sequential (Population m) a ->
34 |   Population m a
35 | smcMultinomial = sir resampleMultinomial
36 |
37 | ||| Sequential Monte Carlo with systematic resampling at each timestep.
38 | -- Weights are not normalized.
39 | export
40 | smcSystematic :
41 |   MonadSample m =>
42 |   -- | number of timesteps
43 |   (n_timesteps : Nat) ->
44 |   -- | population size
45 |   (n_particles : Nat) ->
46 |   -- | model
47 |   Sequential (Population m) a ->
48 |   Population m a
49 | smcSystematic = sir resampleSystematic
50 |
51 | ||| Default synonym for smcSystematic
52 | export
53 | smc : MonadSample m => (n_timesteps : Nat) -> (n_particles : Nat) -> Sequential (Population m) a -> Population m a
54 | smc = smcSystematic
55 |
56 | ||| Sequential Monte Carlo with multinomial resampling at each timestep.
57 | -- Weights are normalized at each timestep and the total weight is pushed
58 | -- as a score into the transformed monad.
59 | export
60 | smcMultinomialPush :
61 |   MonadInfer m =>
62 |   -- | number of timesteps
63 |   (n_timesteps : Nat) ->
64 |   -- | population size
65 |   (n_particles : Nat) ->
66 |   -- | model
67 |   Sequential (Population m) a ->
68 |   Population m a
69 | smcMultinomialPush = sir (pushEvidence . resampleMultinomial)
70 |
71 | ||| Sequential Monte Carlo with systematic resampling at each timestep.
72 | -- Weights are normalized at each timestep and the total weight is pushed
73 | -- as a score into the transformed monad.
74 | export
75 | smcSystematicPush :
76 |   MonadInfer m =>
77 |   -- | number of timesteps
78 |   (n_timesteps : Nat) ->
79 |   -- | population size
80 |   (n_particles : Nat) ->
81 |   -- | model
82 |   Sequential (Population m) a ->
83 |   Population m a
84 | smcSystematicPush = sir (pushEvidence . resampleSystematic)
85 |