Inference in k-TBNs
Inference in a k-TBN computes P(target[t] | observations, interventions) for every declared target at every time slice, under any mix of soft/hard observations and Pearl-style do-interventions – without ever unrolling the model. aGrUM/pyAgrum implements one exact algorithm, a generalization of Murphy’s interface algorithm to order k.
- class pyagrum.ktbn.KTBNInference(ktbn)
KTBNInference answers exact queries on a
pyagrum.ktbn.KTBNunder any mix of observations (soft or hard evidence) and hard interventions (Pearl’s do-operator), returning P(target[t] | obs, do(…)) for every declared target at every time slice, without ever unrolling the model.An observation is conditioning: it revises the whole network, ancestors included. An intervention do(V[t]=v) is surgery: V[t] is cut from its causes and forced to v, so the effect only reaches its descendants. Both may be combined, and a node may carry both.
Warning
The k-TBN is referenced, not copied: it must outlive the inference engine.
- KTBNInference(ktbn) -> KTBNInference
- Parameters:
ktbn (
pyagrum.ktbn.KTBN) – the k-TBN to reason about (referenced, not copied)
- Raises:
pyagrum.InvalidArgument – if ktbn is None
- Parameters:
ktbn (
KTBN)
Example
import pyagrum as gum
import pyagrum.ktbn as ktbn
model = ktbn.KTBN.fromBN(gum.fastBN("X0->X1->Y1;Y0"))
model.cpt("X", 0).fillWith([0.5, 0.5])
model.cpt("X", 1).fillWith([0.9, 0.1, 0.2, 0.8]) # P(X[t] | X[t-1])
model.cpt("Y", 0).fillWith([0.5, 0.5])
model.cpt("Y", 1).fillWith([0.8, 0.2, 0.3, 0.7]) # P(Y[t] | X[t])
ie = ktbn.KTBNInference(model)
ie.addObservation("Y", 3, 1) # observe Y[3] = 1
ie.addIntervention("X", 1, 1) # do(X[1] = 1)
ie.addTarget("X") # the whole series P(X[t] | ...)
ie.makeInference(5) # slices 0..4
[[round(p, 4) for p in post.tolist()] for post in ie.posteriors("X")] # X[0] is not revised: do(X[1]) cuts it off
# [[0.5, 0.5], [0.0, 1.0], [0.0943, 0.9057], [0.1283, 0.8717], [0.2898, 0.7102]]
round(ie.log2ObservationProbability(), 4) # log2 P(Y[3] = 1 | do(X[1] = 1))
# -0.9159
Scientific notes
KTBNInference is the exact interface algorithm of [1] (Section 3.4), generalized from 2-TBNs to
the k-TBNs of pyagrum.ktbn.KTBN, to atemporal variables and to interventions. The model is never unrolled: the
time per slice does not depend on the horizon, nor does the memory when nothing is observed (with observations, one
interface message per slice is kept for the backward sweep).
The forward interface. In a 2-TBN, the forward interface \(I_t\) is the set of the variables of slice \(t\) which have a child in slice \(t+1\); it d-separates the past from the future [1]. For a k-TBN, \(I_t\) is the set of the instances \(V_s\), \(s \leq t\), which still have a child after \(t\) (from slice \(k-2\) on, the last \(\ell_V\) instances of \(V\), with \(\ell_V\) the largest lag of an arc leaving \(V\) in the kernel), plus every atemporal variable which, shared by all the slices, never leaves the interface.
Windows. The window of slice \(t\) is \(H_t = I_{t-1} \cup \mathbf{V}_t\). It is compiled once into a
junction tree (moralization of the families, \(I_{t-1}\) and \(I_t\) each forced into a clique, triangulation).
By time homogeneity, all the windows from slice \(k\) on have the same shape: one junction tree serves the whole
repeating part, and only the first \(k\) slices get their own. Inside a window, Shafer-Shenoy message passing is
used (see pyagrum.ShaferShenoyInference). The forward message is
where \(\lambda\) is the likelihood of an observation (hard or soft), the window of slice 0 also holds the CPTs of
the atemporal variables, and \(c_t\) normalizes \(m_t\), so that
\(\log_2 P(\mathbf{e} \mid do(\cdot)) = \sum_t \log_2 c_t\) (log2ObservationProbability). When observations exist,
a backward sweep sends the symmetric messages \(r_t(I_{t-1})\), and each posterior is read in the window where
\(m_{t-1}\) and \(r_{t+1}\) meet (smoothing). Without observation, the backward messages are uniform, so only
the forward sweep is run.
Interventions. \(do(V_t = v)\) replaces the CPT of \(V_t\) by a point mass on \(v\), cutting it from its causes [2]: the effect reaches only its descendants, while an observation also revises its ancestors. Both can be combined.
Targets. The computation is restricted to the requisite variables: the targets (all the variables by default), the observed variables and their ancestors.
Implementation limit. The cost per slice is exponential in the width of the window, which grows with the size of the interface: every atemporal variable (and every lag of the kernel) widens it. With nearly deterministic CPTs, a backward message may underflow, which raises an error.
Methods
Record a hard intervention do(base[slice] = value). |
|
Record an observation on base[slice]: a hard observation base[slice]=value (conditioning: revises the whole network, ancestors included), or a soft (likelihood) observation via a list of per-modality likelihoods (a one-hot vector is equivalent to a hard observation). |
|
Record several hard observations at once (batch form of |
|
Declare base as a target: a base variable whose marginals are wanted. |
|
Remove all recorded interventions. |
|
Remove all recorded observations. |
|
Remove all targets (restores default-all-targets mode). |
|
Remove a recorded intervention (silent no-op if absent). |
|
Remove a recorded observation (silent no-op if absent). |
|
Remove a target; when the last one is removed, default-all-targets mode is restored. |
|
With no argument, tells whether any observation at all is recorded (when False, |
|
Introspection only: the size of the forward interface of the repeating window, i.e. how many node occurrences have to cross each slice boundary. |
|
Return log2 P(obs | do(…)) for the last run: the log2-likelihood of the recorded observations under the (possibly mutilated) model. |
|
The natural logarithm of the probability of the observations for the last run. |
|
Run the interface algorithm over nbTimeSlices slices (0..nbTimeSlices-1) and cache, for every targeted base, its marginal at every slice. |
|
Return P(obs | do(…)), i.e. 2 raised to |
|
Return P(base[slice] | obs, do(…)). |
|
The whole marginal time-series of a targeted base variable. |
|
Introspection only: the junction tree of the repeating window, compiled from the k-slice template and re-entered at every step from slice k on. |
Attributes
int([x]) -> integer int(x, base=10) -> integer |