Skip to content

Dynamic Bayesian networks in the mid level - #94

Open
richard303d wants to merge 17 commits into
pyc-team:devfrom
richard303d:dynamic-bayesian-network
Open

richard303d wants to merge 17 commits into
pyc-team:devfrom
richard303d:dynamic-bayesian-network

Conversation

@richard303d

@richard303d richard303d commented Oct 27, 2026 •

Copy link
Copy Markdown

Description

This PR adds dynamic Bayesian networks (DBNs) to the mid level. A DBN is a model over time given by the CPDs of the first step and the CPDs of one transition, the same at every step (Murphy, 2002). The existing engines run on the network unrolled over the horizon.

  • Variable.previous(lag=1): a handle to the variable lag steps earlier, for the parents of a transition CPD.
  • DynamicBayesianNetwork(variables, initial, transition): validates the CPDs and builds from them the unrolled network of steps 0..H (unroll(H), one variable per step). For a model of order k, initial holds one list of CPDs for each of the first k steps. It is a BayesianNetwork: that of its first steps, unroll(k), with one variable per step (c_0, c_1), on which any static engine and plot_pgm run.
  • DynamicInference(pgm, engine=None): runs any static engine (DeterministicInference by default) on pgm.unroll(H). In a query, a name stands for every step at which its variable exists and (name, t) for step t only. The output is an InferenceOutput with the steps as the last leading axis, (n, T, k), the layout of the concepts over time that Concepts on the last axis in the data layer, with leading axes such as time steps #92 added to the data layer. It is a BaseInference that holds the DBN as pgm.
  • WearDataset: a small synthetic fleet of machines modelled on N-CMAPSS (Arias Chao et al., 2021), with concepts over time.
  • Two mid-level examples: 08_dynamic_concept_bottleneck_model.py, a CBM over time, and 09_dynamic_cbm_with_residual.py, the model of the example we discussed, with concepts moved forward by a fitted mechanism plus a learned residual.

The engines are unchanged, apart from two lines of unpack_plates, which now rebuild a BayesianNetwork for a subclass of it; Variable gains previous() and two private attributes.

Changes Made

  • Feature added
  • Documentation updated

Checklist

  • Tests added or updated
  • Documentation updated
  • Code follows style guidelines

Tests

  • test_dynamic_bayesian_network.py, 40 tests: previous(), unroll(), a model of order 2, the DBN as a BayesianNetwork and the errors.
  • test_dynamic_inference.py, 52 tests with seven engines, most of them against the recursion of the model written in plain torch with the same modules, gradients included, and DynamicInference as an engine.
  • test_wear.py, 4 tests.
  • pytest tests --ignore=tests/data on Python 3.10, 3.12 and 3.13: 2737 passed, 6 skipped, against 2645 on dev at a16e60d, whose tests all keep their outcome. tests/data with the [data] extras: 466 passed (462 on dev).
  • The documentation builds with the same warnings as on dev.

DynamicBayesianNetwork(variables, initial, transition) is a directed
graphical model over discrete time steps, given by the CPDs of the
first steps and of one transition (Murphy, 2002); Variable.previous()
is the arc from the step before, previous(lag) from an older one, for
a model of any order. It builds the BayesianNetwork of each kind of
step, where the interface variables of the steps before are observed
roots, and the unrolled network of any horizon.

DynamicInference runs any engine of a static model on a DBN, on the
network unrolled over the horizon, built once per horizon. It returns
an InferenceOutput with the steps as the last leading axis.

WearDataset is a synthetic fleet of machines with concepts over time.
The mid-level examples 08 and 09 build a concept bottleneck model over
time, and concepts moved forward by a fitted mechanism and a learned
residual; 08 ends by running the two networks of the DBN one step at a
time by hand.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@codecov

codecov Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

together with the dataset, this PR also need to implement the corresponding DataModule

def __init__(
self,
variables: List[Variable],
initial: Union[List[ParametricCPD], List[List[ParametricCPD]]],

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it really necessary to permit List[List[ParametricCPD]] ? a list of CPDs should suffice for a prior of a first-order DBN.
Except we want to include second-order, but I would keep it simple for now

class DynamicConceptDataset(ConceptDataset):
"""A ConceptDataset whose samples are windows of time series.

The inputs are ``N`` series of ``T`` steps, ``(T, N, F)``, and the

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

F channels

"""A ConceptDataset whose samples are windows of time series.

The inputs are ``N`` series of ``T`` steps, ``(T, N, F)``, and the
optional concepts are series over the same steps, ``(T, N, C)``. Each

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

explain the flattening operation also here

>>> from torch_concepts.data.base import DynamicConceptDataset
>>> x, c = torch.randn(100, 3, 4), torch.randn(100, 3, 2) # 3 series of 100 steps
>>> annotations = Annotations(labels=["a", "b"], cardinalities=[1, 1], types=["continuous"] * 2)
>>> dataset = DynamicConceptDataset(x, c, annotations, window=10, stride=5)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do the example with stride = 1 so that is easier to read

Comment thread torch_concepts/data/datasets/wear.py Outdated
class WearDataset(DynamicConceptDataset):
"""Synthetic dataset of machines whose two components wear out, with concepts over time.

Each of the ``n_gen`` machines runs for ``n_steps`` steps, and at every

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the docstring is criptic. Be clearer about the data shape it produces

Comment thread torch_concepts/data/datasets/wear.py Outdated
show the load, but no concept records it. The alarm fires when the mean
health of the two components drops below ``ALARM_THRESHOLD = -0.45``.

The dataset is modelled on N-CMAPSS. Like the flight conditions of an

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reference the N-CMAPSS citation

Comment thread torch_concepts/data/datasets/wear.py Outdated
and load. Reading the health from the sensors means undoing the
conditions, a nonlinear task.

By default a sample is the whole series of one machine. Its inputs are

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also here the docstring could be made much more readable. Probably even more compact

# Data
# ----
# Each machine runs for 30 steps, enough for almost all of them to reach the
# alarm. A sample is a window of ``W + H`` steps of one machine: ``D``

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would put D as an hyperparameter

# Each machine runs for 30 steps, enough for almost all of them to reach the
# alarm. A sample is a window of ``W + H`` steps of one machine: ``D``
# channels, the health of its two components and the alarm. A window starts
# every 2 steps, so the samples cover every stage of wear. The model reads the

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would describe the model later, here data only

x = dataset.input_data[:, :W]
c = dataset.concepts[:, W - 1:][["health_A", "health_B"]]
y = dataset.concepts[:, W - 1:][["alarm"]]
train, test = dataset.series < 1000, dataset.series >= 1000

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

specify "inductive split" here

n_gen=1500, n_steps=30, window=W + H, stride=2, root=str(DATA_ROOT / "wear")
)
x = dataset.input_data[:, :W]
c = dataset.concepts[:, W - 1:][["health_A", "health_B"]]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comment in line with the expected shapes

D = dataset.input_data.shape[-1]

# %%
# Variables

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

variables are part of the "model"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we add a demo of the intervention on this kind of models?

variables = dict.fromkeys(templates.values()) # each variable once, in the order passed
# A copy of each variable at each step where it has a CPD.
copies = {
(v.name, t): _copy(v, t)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

think on whether you want to keep this notation (v.name, t) for referring to the variable. It has its advantages w.r.t. using strings -> it is harder to get things wrong, but at the same time also disadvantages -> the api is different and the user would need to learn two different APIs, and needs to be documented separately, which is usually better to avoid

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

they will still be references as name_1, name_2 in the pgm, as that is forced to be a string. So maybe better keep it that way to avoid duplicating the same thing?

print(f"\nmean over the {len(x_open)} test windows with 0.2 < P(alarm_H = 1) < 0.8:")
for label, given in evidence.items():
p = sampler.query({("alarm", 4): ones}, evidence=given, horizon=H).probabilities
print(f" {label:32s} {p.mean():.3f}")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also from the output alone, it is not super clear what the example is about and what exactly is being claimed

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same comments as the other example :)

return {_at(n, t): v for t, step in enumerate(per_step) for n, v in step.items()}


class DynamicInference(BaseInference):

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure this is the best name? is it established in the literature? I would consider RolloutInference

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants