mbdlyb.formalisms.bayesnet.base

Bayesian Network base classes for node and network representation.

This module provides the core classes for building and managing Bayesian networks using the pyAgrum library as the backend. It includes node classes with conditional probability table (CPT) support and automatic parent splitting for handling complex relationships.

Classes:

BNDependency: Represents a dependency relationship between Bayesian network nodes. BNElement: Abstract base class for elements in a Bayesian network. BayesNode: A probabilistic variable node in a Bayesian network. BayesAutoSplitNode: A specialized node with automatic parent splitting. BayesNet: The main Bayesian network container and manager.

Attributes

MAX_PARENTS

Classes

BNDependency

Represents a dependency relationship in a Bayesian network.

BNElement

Abstract base class for Bayesian network elements.

BayesAutoSplitNode

A Bayesian network node with automatic parent splitting capability.

BayesNet

A Bayesian network container and reasoning structure.

BayesNode

A probabilistic node in a Bayesian network.

Module Contents

class mbdlyb.formalisms.bayesnet.base.BNDependency(source: MBDNode, target: MBDNode, net: MBDNet | None = None)

Bases: mbdlyb.MBDDependency

Represents a dependency relationship in a Bayesian network.

This class encapsulates a dependency between two nodes in a Bayesian network, where one node (parent) influences the conditional probability of another node (child).

class mbdlyb.formalisms.bayesnet.base.BNElement(name: str, net: MBDNet | None = None)

Bases: mbdlyb.MBDElement

Abstract base class for Bayesian network elements.

Defines the interface for creating Bayesian network nodes and connections within a pyAgrum BayesNet structure.

abstractmethod create_bn_connections(bn: pyagrum.BayesNet)

Create connections (arcs) in the Bayesian network.

Establishes the edges from parent nodes to this node and fills the conditional probability tables.

Parameters:

bn – The pyAgrum BayesNet to add connections to.

Raises:

NotImplementedError – This method must be implemented by subclasses.

abstractmethod create_bn_node(bn: pyagrum.BayesNet, node_sets: dict[Type[mbdlyb.MBDNode], list[int]])

Create a node in the Bayesian network.

Parameters:
  • bn – The pyAgrum BayesNet to add the node to.

  • node_sets – A dictionary mapping node types to lists of node IDs.

Raises:

NotImplementedError – This method must be implemented by subclasses.

class mbdlyb.formalisms.bayesnet.base.BayesAutoSplitNode(name: str, net: MBDNet | None = None)

Bases: BayesNode

A Bayesian network node with automatic parent splitting capability.

When a node has too many parents (exceeding MAX_PARENTS), this class automatically splits the parents into groups and creates intermediate nodes to handle the large parent set while maintaining probability correctness.

Variables:
  • _split_preserve_classes – List of node classes whose parents should not be split.

  • _parent_split – Mapping of split node FQNs to their parent lists, or None if no split.

create_bn_connections(bn: pyagrum.BayesNet)

Create connections with automatic parent splitting.

If parents were split, creates connections to intermediate split nodes. Otherwise, delegates to parent class for standard connection creation.

Parameters:

bn – The pyAgrum BayesNet to add connections to.

create_bn_node(bn: pyagrum.BayesNet, node_sets: dict[Type[mbdlyb.MBDNode], list[int]])

Create this node in the Bayesian network with automatic parent splitting.

If the number of parents exceeds MAX_PARENTS, creates intermediate split nodes to handle the large parent set. Otherwise, delegates to parent class.

Parameters:
  • bn – The pyAgrum BayesNet to add this node to.

  • node_sets – A dictionary mapping node types to lists of node IDs.

class mbdlyb.formalisms.bayesnet.base.BayesNet(name: str, net: MBDNet | None = None)

Bases: BNElement, mbdlyb.MBDNet

A Bayesian network container and reasoning structure.

Manages a collection of Bayesian network nodes and their relationships, providing methods to convert the abstract representation into a pyAgrum BayesNet for inference and analysis.

Variables:

elements – Dictionary mapping element names to BNElement instances.

create_bn_connections(bn: pyagrum.BayesNet)

Add all connections to the Bayesian network.

Iterates through all BNElement instances and creates their connections (arcs and CPTs) in the pyAgrum BayesNet structure.

Parameters:

bn – The pyAgrum BayesNet to add connections to.

create_bn_node(bn: pyagrum.BayesNet, node_sets: dict[Type[mbdlyb.MBDNode], list[int]])

Add all nodes to the Bayesian network.

Iterates through all BNElement instances and creates them in the pyAgrum BayesNet structure.

Parameters:
  • bn – The pyAgrum BayesNet to add nodes to.

  • node_sets – A dictionary mapping node types to lists of node IDs.

get_bn_nodes() → list[BNElement]

Get all Bayesian network elements.

Returns:

A list of all BNElement instances in this network.

to_bn() → tuple[pyagrum.BayesNet, dict[Type[mbdlyb.MBDNode], list[int]]]

Convert this abstract network to a pyAgrum BayesNet.

Creates a complete Bayesian network with all nodes and connections, including conditional probability tables.

Returns:

  • The pyAgrum BayesNet instance.

  • A dictionary mapping node types to lists of their pyAgrum IDs.

Return type:

A tuple containing

class mbdlyb.formalisms.bayesnet.base.BayesNode[T](name: str, net: MBDNet | None = None)

Bases: BNElement, mbdlyb.formalisms.CPTEnabledNode[T]

A probabilistic node in a Bayesian network.

Represents a random variable with discrete states and conditional probability tables (CPTs) that define the node’s probability distribution given its parents.

Variables:

_PARENT_RELATION_TYPE – The type of dependency used in this network.

Type Parameters:

T: The type of states this node can take.

create_bn_connections(bn: pyagrum.BayesNet)

Create connections from parent nodes to this node.

Establishes arcs from all parent nodes to this node and fills the conditional probability table with values computed from the CPT.

Parameters:

bn – The pyAgrum BayesNet to add connections to.

create_bn_node(bn: pyagrum.BayesNet, node_sets: dict[Type[mbdlyb.MBDNode], list[int]])

Create this node in the Bayesian network.

Adds the node as a variable to the BayesNet and registers it in the node_sets dictionary for later processing.

Parameters:
  • bn – The pyAgrum BayesNet to add this node to.

  • node_sets – A dictionary mapping node types to lists of node IDs.

Raises:

AssertionError – If bn is None.

static create_variable(bn: pyagrum.BayesNet, name: str, label: str, states: list[T]) → int

Create a new variable in the Bayesian network.

Parameters:
  • bn – The pyAgrum BayesNet to add the variable to.

  • name – The unique name of the variable.

  • label – The human-readable label for the variable.

  • states – A list of possible state values for this variable.

Returns:

The node ID assigned by pyAgrum for the newly created variable.

mbdlyb.formalisms.bayesnet.base.MAX_PARENTS = 10