Skip to content

Inference in CTNB

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(Xt)=P(X0)exp(QXt)P(X_t) = P(X_0)*exp(Q_Xt) for enough time to notice convergence.

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.

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

  • Parameters: t (float) – Time to make inference calculation at.
  • Parameters: v (NameOrId) – Name or id of the variable.
  • Returns: The computed distribution of variable v using exact inference.
  • Return type: pyagrum.Tensor

Sampling method used is Forward Sampling.

Forward sampling works this way: : - At time tt :

  • Draw a “time until next change” dtdt value for each variable’s exponential distribution of their waiting time.
  • Select the variable with smallest transition time : nexttime=t+dtnext time = t + dt.
  • Draw the variable next state uniformly.
  • loop until t=timeHorizont = 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)

Section titled “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.

Id of the sample.

  • Type: int

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)

Section titled “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)

Section titled “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)

Section titled “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)

Section titled “makeSample(posteriors, timeHorizon=5000, burnIn=100)”

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).
  • Returns: Number of runs.
  • Return type: int
  • Parameters: name (str) – Name of the variable.
  • Returns: The aproximate inference of a given variable.
  • Return type: pyagrum.Tensor

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

Section titled “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.