Inference

Exact Inference

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.

makeInference(t=5000)

Compute exact inference at time t. Distribution for initial state is uniform.

Parameters:

t (float) – Time to make inference calculation at.

posterior(v)
Parameters:

v (NameOrId) – Name or id of the variable.

Returns:

The computed distribution of variable v using exact inference.

Return type:

pyAgrum.Potential

setEvidence(evs=None)

Sampling Inference

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.

averageInference(nbTrajectories=5, timeHorizon=5000, burnIn=100)

Start nbTrajectories sampling and approximate the inference over all samples.

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

  • timeHorizon (float) – Duration of the sampling.

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

makeInference(timeHorizon=5000, burnIn=100)

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

Parameters:
  • timeHorizon (float) – Duration of the sampling.

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

Returns:

Number of runs.

Return type:

int

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

Start a given number of sample and approximate the inference over all the samples.

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

  • timeHorizon (float) – Duration of the sampling.

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

makeSample(posteriors, timeHorizon=5000, burnIn=100)

Fills posteriors using forward sampling.

Parameters:
  • posteriors (Dict[str, pyAgrum.Potential]) – 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).

Returns:

Number of runs.

Return type:

int

posterior(name)
Parameters:

name (str) – Name of the variable.

Returns:

The aproximate inference of a given variable.

Return type:

pyAgrum.Potential

writeTrajectoryCSV(filename, n=1, timeHorizon=10, burnIn=100)

Makes n samples using Forward Sampling and saves trajectories into a csv file. Storing format : {IdSample, time, Var, state}

Parameters:
  • filename (str) – Name of the file to save trajectories in.

  • n (int) – Number of sampling to run.

  • timeHorizon (float) – Duration of the sampling.

  • burnIn (int) – Number of preliminary iterations before starting.