The k-TBN model and its generator

pyagrum.ktbn.KTBN is the main class for representing and manipulating k-order dynamic Bayesian networks in pyAgrum. It stores a compact template of k time slices, distinguishing temporal processes from atemporal (static) variables, and can be unrolled into a plain pyagrum.BayesNet for any number of time slices.

class pyagrum.ktbn.KTBN(*args)

KTBN represents a k-order dynamic Bayesian network (k-TBN): a compact template of k consecutive time slices that fully characterizes a time-homogeneous dynamic process. It generalizes the classical 2-TBN by letting a variable at time t depend on up to the k most recent time slices instead of a single step backward.

A KTBN distinguishes two kinds of variables:
  • temporal variables (processes), which evolve through time and have one instance per slice (added with addTemporal());

  • atemporal variables, constant through time (e.g. a static context), which have a single instance (added with addAtemporal()).

Internally, nodes are addressed either by an engine name using bracket notation ("X[2]" for the 3rd slice of process X, or a bare name such as "C" for an atemporal variable), or, in most of the public API, by an explicit (base, slice) pair – slice being ATEMPORAL (-1) for an atemporal variable. Both spellings are accepted almost everywhere.

The template can be unrolled into a plain pyagrum.BayesNet for any number of time slices with unroll().

KTBN(k=2) -> KTBN
Parameters:
  • k (int) – the order of the k-TBN (number of time slices in the template). Must be >= 1.

Raises:

pyagrum.InvalidArgument – If k is 0.

Scientific notes

A k-TBN is a time-homogeneous dynamic Bayesian network [1] given by a template of \(k\) consecutive time slices \(0, \dots, k-1\). It generalizes the 2-TBN of [2] and [3] (a prior network for the first slice and a transition network between two slices): a variable at time \(t\) may depend on the variables of the slices \(t-k+1, \dots, t\), so that the process is a Markov chain of order \(k-1\) (\(k = 2\) is the usual first-order case).

The template. The slices \(0, \dots, k-2\) hold the initial distribution, the slice \(k-1\) holds the transition kernel, which is reused at every later time step. With \(\mathbf{V}_t\) the temporal variables at time \(t\) and \(\mathbf{C}\) the atemporal ones, the joint distribution over a horizon \(T \geq k\) is

\[P(\mathbf{c}, \mathbf{v}_{0:T-1}) = \prod_{C \in \mathbf{C}} P(c \mid \mathrm{pa}_C) \prod_{t=0}^{k-2} \prod_{V} P_t(v_t \mid \mathrm{pa}_{V_t}) \prod_{t=k-1}^{T-1} \prod_{V} P_{k-1}(v_t \mid \mathrm{pa}_{V_t}),\]

where the parents of \(V_t\), for \(t \geq k-1\), are those of \(V_{k-1}\) in the template, shifted by \(t-k+1\) slices (the lags are kept). An arc never goes back in time; arcs inside a slice are allowed.

Atemporal variables (pyAgrum’s extension) are constant through time: they have a single instance, only atemporal parents, and may be parents of temporal variables at any slice. Since they are shared by all the slices, they couple the whole horizon (see pyagrum.ktbn.KTBNInference).

Methods

add()

Add a variable to the k-TBN.

addArc()

Add an arc between two (process, slice) endpoints.

addAtemporal()

Shortcut for add(var, False): adds an atemporal (static) variable.

addTemporal()

Shortcut for add(var, True): adds a temporal process.

arcs()

atemporalVarNames()

baseName()

bnToDot()

changeVariableName()

Rename a variable (temporal process or atemporal variable).

children()

Returns the children of a node, as (base, slice) pairs.

clear()

Remove all variables and arcs, keeping the order k.

cpt()

Returns the conditional probability table of a (process, slice) couple.

empty()

erase()

Remove a variable (and all its incident arcs).

eraseArc()

Remove an arc between two (process, slice) endpoints.

exists()

existsArc()

fillCPT()

Fill one conditional distribution P(node | parent configuration), addressing the node and its parents by their (base, slice) identity rather than by CPT positional order.

fromBN()

Build a k-TBN from an existing pyagrum.BayesNet, reading its node names under one of two mutually exclusive conventions – picked automatically, never mixed within one network:

generateCPT()

Randomly generate the CPT of a single node.

generateCPTs()

Randomly generate every CPT of the template.

k()

load()

Load a k-TBN from a file produced by save(), selected by the file extension.

nbAtemporalVars()

nbTemporalVars()

nodes()

parents()

Returns the parents of a node, as (base, slice) pairs.

save()

Save the k-TBN in a jgum (text/JSON) or bgum (binary/msgpack) file, selected by the file extension (.jgum for text, anything else for binary).

size()

sizeArcs()

summaryGraph()

Returns the Graphviz dot string of the summary graph: the projection of the transition kernel alone (the pattern that actually repeats through time when unrolling), not the whole template.

temporalVarNames()

timeSlice()

toBN()

toDot()

toString()

toUnrolledDot()

Returns a Graphviz dot string of the k-TBN unrolled over T time slices, without actually materializing the unrolled pyagrum.BayesNet.

unroll()

Unroll the k-TBN into a standard Bayesian network with exactly nbTimeSlices time slices.

variable()

Returns the variable of a (process, slice) couple.

Attributes

ATEMPORAL

int([x]) -> integer int(x, base=10) -> integer

pyagrum.ktbn.KTBNGenerator draws a random k-TBN template, mainly to build ground-truth models for learning experiments.

class pyagrum.ktbn.KTBNGenerator(*args)

KTBNGenerator draws a random pyagrum.ktbn.KTBN template: variables, a legal arc set and (optionally) random CPTs. It is the k-TBN counterpart of pyAgrum’s Bayesian network generators, mainly used to build ground-truth models for learning experiments: sample trajectories from a generated model, learn it back, and compare.

Acyclicity comes for free: a cross-slice arc can never close a cycle since the slice index strictly increases along it, so the generator only needs to avoid cycles among same-slice (temporal, lag 0) and atemporal-to-atemporal arcs, which it does by drawing one random ranking per family and only admitting arcs from lower to higher rank.

KTBNGenerator(k, nbTemporal, nbAtemporal=0, maxArcs=0, maxModality=2) -> KTBNGenerator
Parameters:
  • k (int) – order of the generated k-TBN (number of template slices); must be >= 1

  • nbTemporal (int) – number of temporal processes

  • nbAtemporal (int) – number of atemporal variables

  • maxArcs (int) – hard cap on the number of arcs; 0 (default) derives it from the density instead (see setDensity())

  • maxModality (int) – largest domain size; domains are drawn uniformly in [2, maxModality]

Raises:

pyagrum.InvalidArgument – if k is 0 or maxModality is below 2

Example

import pyagrum as gum
import pyagrum.ktbn as ktbn

gum.initRandom(42)  # reproducible
gen = ktbn.KTBNGenerator(3, 4, 1)  # k=3, 4 temporal processes, 1 atemporal variable
gen.setDensity(0.15).setMaxParents(4)
model = gen.generate()
model.k()
# 3
sorted(model.temporalVarNames())
# ['X0', 'X1', 'X2', 'X3']
sorted(model.atemporalVarNames())
# ['A0']

Methods

generate()

generateKTBN()

Fill out with a freshly drawn model (its previous content is discarded).

k()

nbLegalArcs()

setDensity()

Set the fraction of the legal arc set to draw, in [0,1].

setDomainRange()

Set the range domain sizes are drawn uniformly from.

setGenerateCPTs()

Whether to fill the CPTs with random values (default True).

setGuaranteeOrder()

Force one arc of lag k-1 into the kernel slice, so the generated model’s effective Markov order really equals k (default True).

setMaxParents()

Cap the number of parents of any node, which bounds CPT size.

setNamePrefixes()

Set the name prefixes used for generated variables: temporal processes are named prefix0, prefix1, .