Inference in CTBN

Exact Inference in CTBN

Amalgamation is used to compute exact inference. It consists in merging all the CIMs of a CTBN into one CIM. Then we use the properties of markov process : \(P(X_t) = P(X_0)*exp(Q_Xt)\) for enough time to notice convergence.

class pyagrum.ctbn.SimpleInference(ctbn)

Exact inference using amalgamation to compute the Intensity Matrix corresponding to the CTBN (not efficient for models with great number of variables)

Parameters:

ctbn (CTBN) – The CTBN used for simple inference.

Scientific notes

SimpleInference computes exactly the distribution at time \(t\) of a pyagrum.ctbn.CTBN from its joint intensity matrix \(Q_{\mathbf{X}}\) (see pyagrum.ctbn.CTBN) [1]:

\[P(\mathbf{X}(t)) = P(\mathbf{X}(0)) \exp(t\, Q_{\mathbf{X}}),\]

with \(P(\mathbf{X}(0))\) uniform over the joint states. The posterior of a variable is a marginal of \(P(\mathbf{X}(t))\). With the default \(t = 5000\), it is the stationary distribution of an ergodic process whenever the process mixes in a time much shorter than \(t\) (rates not too small).

Implementation limit. The joint matrix has \(\prod_X |\mathrm{Val}(X)|\) rows: only small models can be handled. No evidence can be given.

Methods

makeInference()

Compute exact inference at time t.

posterior()

setEvidence()

Sampling Inference in CTBN

Sampling method used is Forward Sampling.

Forward sampling works this way:
  • At time \(t\) :

  • Draw a “time until next change” \(dt\) value for each variable’s exponential distribution of their waiting time.

  • Select the variable with smallest transition time : \(next time = t + dt\).

  • Draw the variable next state uniformly.

  • loop until \(t = timeHorizon\).

When doing forward sampling, we first iterate this process over a given amount of iterations (called burnIn). We consider the last values as the initial configuration for sampling. The reason is that the initial state of a CTBN is unknown without real data.

class pyagrum.ctbn.ForwardSamplingInference(ctbn)

Inference using forward sampling (slow convergence).

Notes

Sometimes samples are called trajectories. One sample is one trajectory. When making inference, the last sample is stored in the class as a trajectory. idtraj indicates the number of samplings done.

Parameters:

ctbn (CTBN) – The CTBN used for sampling.

idtraj

Id of the sample.

Type:

int

trajectory

Contains the trajectory from the last sampling.

Type:

list[tuple[float, str, str]]

Examples

How to read a trajectory? A tuple (t, var, s) means that at time t the variable var transition from state s to another one.

When storing many trajectories from different samples, a trajectory is identified by an id.

Scientific notes

ForwardSamplingInference estimates the long-run distribution of the variables of a pyagrum.ctbn.CTBN from simulated trajectories.

Sampling a trajectory. Each variable starts in a state drawn uniformly. At each step, every variable \(X\) draws a waiting time from the exponential law of parameter \(q_{x \mid u}\) (see pyagrum.ctbn.CIM); the variable with the shortest time moves to \(x'\) with probability \(q_{xx' \mid u} / q_{x \mid u}\), and all the times are drawn again. This is the first-reaction method of [2] applied to the variables: the minimum of independent exponential times is the waiting time of the joint process, reached by each variable in proportion to its rate, and the exponential law has no memory, so the trajectory is an exact sample of the CTBN.

The estimator. The first burnIn transitions (100 by default) are discarded (fewer if the process reaches an absorbing state, where all the exit rates are 0). Then, over a duration \(T\) (timeHorizon, 5000 by default), the posterior of a value \(x\) is the fraction of time spent in it,

\[\hat P(X = x) = \frac{1}{T} \int_0^T \mathbb{1}[X(s) = x] \, ds ,\]

which converges to the stationary distribution when the process is ergodic, not to the distribution at a given time. averageInference and makeParallelInference add the times of several independent trajectories before normalizing.

Implementation limit. There is no stopping criterion: the precision is set by the total simulated time only, and the burn-in counts transitions, not time. No evidence can be given.

Methods

averageInference()

Start nbTrajectories sampling and approximate the inference over all samples.

makeInference()

Start a new sample and normalize resulting posteriors to get an aproximation of the inference.

makeParallelInference()

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

makeSample()

Fills posteriors using forward sampling.

posterior()

writeTrajectoryCSV()

Makes n samples using Forward Sampling and saves trajectories into a csv file.