ForwardSamplingInference.makeParallelInference

ForwardSamplingInference.makeParallelInference(nbTrajectories=5, timeHorizon=5000, burnIn=100)

Start nbTrajectories samplings in parallel processes and approximate the inference over all the samples.

The result is the same as the one of averageInference with the same state of the random generator (see pyagrum.initRandom). Starting the processes has a cost: this method pays off for long samplings only. The trajectories are not kept (trajectory is left unchanged).

Parameters:
  • nbTrajectories (int) – Number of samplings in parallel.

  • timeHorizon (float) – Duration of each sampling.

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

Returns:

The number of runs of each sampling.

Return type:

list[int]

Raises:

pyagrum.InvalidArgument – If nbTrajectories is not positive.

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]]
gum.initRandom(42)
ie = ct.ForwardSamplingInference(ctbn)
nbEvents = ie.makeParallelInference(nbTrajectories=2, timeHorizon=1000, burnIn=10)
len(nbEvents)  # the number of runs of each sampling
# 2
np.allclose(ie.posterior("A").toarray(), [1 / 3, 2 / 3], atol=0.05)  # exact P(A): [1/3, 2/3]
# True

Class

ForwardSamplingInference