ForwardSamplingInference.makeSample

ForwardSamplingInference.makeSample(posteriors, timeHorizon=5000, burnIn=100, rng=None)

Fills posteriors using forward sampling.

Parameters:
  • posteriors (dict[str, pyagrum.Tensor]) – A dict containing a posterior for each variable of the CTBN.

  • timeHorizon (float) – Duration of the sampling.

  • burnIn (int) – Number of runs before starting the sampling (to ensure ergodicity).

  • rng (numpy.random.Generator | None) – The generator used for the draws. By default, a generator seeded by the generator of pyAgrum (the sampling is then reproducible after pyagrum.initRandom).

Returns:

Number of runs.

Return type:

int

Example

import numpy as np

import pyagrum as gum
import pyagrum.ctbn as ct

ctbn = ct.CTBN()
ctbn.add(gum.LabelizedVariable("A", "A", ["a0", "a1"]))
ctbn.add(gum.LabelizedVariable("B", "B", ["b0", "b1"]))
ctbn.addArc("A", "B")
ctbn.CIM("A").fromMatrix(np.array([[-2.0, 2.0], [1.0, -1.0]]))
ctbn.CIM("B")[{"A": "a0"}] = [[-1, 1], [2, -2]]
ctbn.CIM("B")[{"A": "a1"}] = [[-3, 3], [4, -4]]
ie = ct.ForwardSamplingInference(ctbn)
posteriors = {name: gum.Tensor().add(ctbn.variable(name)) for name in ctbn.names()}
nbEvents = ie.makeSample(posteriors, timeHorizon=50, burnIn=10)
posteriors["A"].names  # filled with the time spent in each state of A
# ('A',)

Class

ForwardSamplingInference