KTBNInference.addObservation
- KTBNInference.addObservation(*args)
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). Recorded only; applied at
makeInference(). Re-observing the same node overwrites.The batch form (
addObservations) takes a dict {node: value} of hard observations only, node keyed by an engine name or a (base, slice) tuple, all-or-nothing validated before anything is recorded.- Parameters:
base (
str) – base name of the target (used together with slice)slice (
int) – slice of the target (pyagrum.ktbn.KTBN.ATEMPORALfor a static node)node_name (
str) – alternatively, the target’s engine namevalue (
int | str) – for a hard observation, the observed modality (index or label)likelihood (
list[float]) – for a soft observation, one non-negative, not-all-zero weight per modalityobservations (
dict) – for the batch form, one node -> value entry per hard observation
- Raises:
pyagrum.NotFound – if the node is unknown, or value is not a legal modality
pyagrum.InvalidArgument – if the slice is invalid, or likelihood has the wrong length, a negative entry, or is all zeros
- Return type:
None
Example
import pyagrum as gum
import pyagrum.ktbn as ktbn
model = ktbn.KTBN.fromBN(gum.fastBN("X0->X1"))
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])
ie = ktbn.KTBNInference(model)
ie.addObservation("X", 1, "1") # X[1] = 1, by label (or by index: 1)
ie.makeInference(3)
print(ie.posterior("X", 0)) # the past is revised
#
# X[0] │
# 0 │1 │
# ─────────│─────────│
# 0.1111 │ 0.8889 │
ie.addObservation("X[1]", [0.2, 0.8]) # a soft evidence
print(ie.posterior("X", 0))
#
# X[0] │
# 0 │1 │
# ─────────│─────────│
# 0.2766 │ 0.7234 │
Class