Module type Smc_inference.S

The sequential Monte Carlo DSL.

Core language API shared across all inference engines.

include Intf.S
type 'a t

'a t is the type of computations of type 'a

val return : 'a -> 'a t

return x injects a value x as a computation

val bind : 'a t -> ('a -> 'b t) -> 'b t

Monadic bind

val map : 'a t -> ('a -> 'b) -> 'b t

Functorial map

val map2 : 'a t -> 'b t -> ('a -> 'b -> 'c) -> 'c t

Applicative structure

val map_array : 'a t array -> ('a array -> 'b) -> 'b t

N-ary applicative structure

val if_ : bool t -> (bool -> 'a t) -> 'a t

If-then-else, mostly useful for monads featuring incremental computation. Allows to efficiently bind on a boolean computation.

module Infix : Intf.Infix with type 'a t := 'a t
val sample : 'a Dist.t -> 'a t

sample dist builds a computation that samples from dist. Note that dist must be a pure computation.

val samplei : 'a Dist.t t -> 'a t

samplei dist is similar to sample except that dist can be an impure computation (ie computing the distribution can involve sampling from other distributions).

val map_score : 'a t -> ('a -> float) -> 'a t

map_score m f behaves similarly to m except that the associated computation will be reweighted according to the result of evaluating f on the value of m.

val map_log_score : 'a t -> ('a -> Log_space.t) -> 'a t

Same as map_score excepts that a log-space likelihood is expected.

val score : float -> unit t

score s reweights the computation by s.

  • raises Invalid_arg

    if s < 0

val log_score : Log_space.t -> unit t

log_score behaves as score except that a log-space weight is expected.

module List_ops : Intf.Foldable with type 'a t = 'a list and type 'a m := 'a t
module Array_ops : Intf.Foldable with type 'a t = 'a array and type 'a m := 'a t
module Seq_ops : Intf.Foldable with type 'a t = 'a Stdlib.Seq.t and type 'a m := 'a t
type particle_output

particle_output is the type of values emitted by particles at yield points.

type resampling_state

resampling_state is the type of values produced by resampling at yield points.

val fork : int -> unit t

fork n creates n-1 particles. fork 1 does not create any new particle.

  • raises [Invalid_arg]

    if n < 1.

val get_score : Log_space.t t

get_score returns the score of the current particle.

val set_score : Log_space.t -> unit t

set_score sets the score of the current particle. This drops the current score of the particle: use wisely.

yield o signals that the particle produced output o and is ready for resampling. The output o is associated to the score of that particle at yield time (i.e. just before resampling) in the output of the SMC sampler (see type population).

type 'a population = {
  1. terminated : ('a * float) array;
    (*

    The terminated particles. These carry their return value.

    *)
  2. active : (particle_output * float) array;
    (*

    The active particles. These carry the value given in argument to yield.

    *)
  3. total_mass : float;
    (*

    The total mass of the population (including terminated particles).

    *)
}

'a population is the type of a population.

exception Invalid_population
type resampling_strategy := (particle_output, float, resampling_state) Resampling.strategy
val run : resampling_strategy -> resampling_state -> npart:int -> 'a t -> RNG.t -> 'a population Stdlib.Seq.t

run resampling resampling_state ~npart model rng returns a lazy sequence of populations. The initial population has size npart. resampling corresponds to the resampling strategy. resampling_state is the initial resampling state.

The model is evaluated as follows:

  1. all particles in the population have initially the same score;
  2. each particle evolves until it terminates or until it calls yield o, at which point it is suspended;
  3. when all particles are either terminated or suspended, resampling is executed on all particles (including terminated ones), resulting in a freshened population;
  4. execution resumes in step 2.

The output of the algorithm is a sequence of population. Each population is composed of yielded particles, attached to some value of type particle_output, and of terminated particles.

Very important note: the output sequence is ephemeral.

  • raises Invalid_population

    if the total mass of the population becomes equal to Log_space.zero

val run_custom : resampling_strategy -> resampling_state -> npart:int -> (int -> 'a t) -> RNG.t -> 'a population Stdlib.Seq.t

run_custom resampling resampling_state ~npart model rng returns a lazy sequence of populations. See the documentation of run for more details on the meaning of each argument.

The only difference with run is that run_custom starts with a custom initial population, obtained by evaluating model on the integers from 0 to npart-1.