mbdlyb.cpt
Conditional Probability Table (CPT) management and manipulation.
This module provides classes for representing and managing conditional probability tables used in probabilistic graphical models (PGMs), such as Bayesian networks. It handles table creation, normalization, serialization, and deserialization.
The main classes are: - CPTCell: Individual probability cell in a CPT - CPTLine: A row in a CPT representing one condition combination - CPT: Complete conditional probability table for a node
Copyright (c) 2026 TNO-ESI All rights reserved.
Classes
Conditional Probability Table for a Bayesian network node. |
|
A single probability value in a conditional probability table. |
|
A single row in a conditional probability table. |
Module Contents
- class mbdlyb.cpt.CPT(node: mbdlyb.gdb.GMBDNode, lines: list[CPTLine | list[float]] | None = None)
Conditional Probability Table for a Bayesian network node.
Manages the complete probability distribution P(node|parents), organized as rows corresponding to each parent state combination. Supports creation, normalization, and conversion between multiple formats (dict, DataFrame, NumPy).
- Variables:
node – The GMBDNode this CPT is defined for.
lines – List of CPTLine objects, one per parent state combination.
parents – List of parent nodes (derived from node.parents()).
- create_lines(_lines: list[CPTLine | list[float]] | None = None) list[CPTLine]
Create CPT lines for all parent state combinations.
Generates one line for each possible combination of parent states. If no parent combinations exist, creates a single unconditional line.
- Parameters:
_lines – Optional list of probability arrays or CPTLine objects. If None, creates uninitialized lines with zero probabilities.
- Returns:
List of CPTLine objects ready for probability assignment.
- static from_df(node: mbdlyb.gdb.GMBDNode, df: pandas.DataFrame) tuple[CPT, str | None]
Create a CPT from pandas DataFrame format with validation.
Validates that the DataFrame matches the node’s current structure. If validation fails, returns a new empty CPT with an error message.
- Parameters:
node – The node this CPT is for.
df – DataFrame in the format produced by to_df().
- Returns:
Tuple of (CPT object, error message). Error message is None if successful.
- static from_dict(node: mbdlyb.gdb.GMBDNode, d: dict) tuple[CPT, str | None]
Create a CPT from dictionary format with validation.
Validates that the dictionary matches the node’s current structure. If validation fails, returns a new empty CPT with an error message.
- Parameters:
node – The node this CPT is for.
d – Dictionary in the format produced by to_dict().
- Returns:
Tuple of (CPT object, error message). Error message is None if successful.
- normalize() CPT
Normalize all lines in this CPT.
Ensures that each line’s probabilities sum to 1.0.
- Returns:
Self for method chaining.
- set_prob(state: str, prob: float)
Set probability for a given state across all lines.
- Parameters:
state – The state name to set probability for.
prob – The probability value to assign.
- Raises:
ValueError – If the state is not in the node’s states.
- to_df() pandas.DataFrame
Convert CPT to pandas DataFrame format.
Creates a DataFrame with parent states as index and node states as columns.
- Returns:
DataFrame representation of the CPT.
- to_dict() dict
Convert CPT to dictionary format.
- Returns:
‘state_count’, ‘parents’ (with FQNs and state counts), and ‘lines’ (lists of probabilities).
- Return type:
Dictionary with keys
- to_np() numpy.ndarray
Convert CPT to NumPy array format.
- Returns:
2D NumPy array with shape (num_conditions, num_states).
- node: mbdlyb.gdb.GMBDNode
- property parents: list[mbdlyb.gdb.GMBDNode]
Get the parent nodes for this CPT.
- Returns:
List of parent nodes.
- property values: list[str]
Get the possible states of the node this CPT is for.
- Returns:
List of state names.
- class mbdlyb.cpt.CPTCell(line: CPTLine, probability: float)
A single probability value in a conditional probability table.
Represents one probability cell within a CPT line, maintaining a reference to its parent line for accessing the complete CPT structure.
- Variables:
line – The CPTLine that contains this cell.
probability – The probability value (should be in [0, 1]).
- property cpt: CPT
Get the CPT that contains this cell.
- Returns:
The CPT object through the parent line.
- probability: float
- class mbdlyb.cpt.CPTLine(cpt: CPT, conditions: list[str], probabilities: list[float] | None = None)
A single row in a conditional probability table.
Represents all probability values for a specific combination of parent node states. Contains multiple CPTCell objects, one for each possible state of the node this table is for.
- Variables:
cpt – The parent CPT that contains this line.
conditions – List of parent state values that define this row’s conditions.
cells – List of CPTCell objects, one per node state.
- normalize() CPTLine
Normalize probabilities in this line to sum to 1.
If all probabilities are zero, distributes probability equally. Otherwise, normalizes by the sum of all probabilities.
- Returns:
Self for method chaining.
- to_np() numpy.ndarray
Convert this line’s probabilities to a NumPy array.
- Returns:
1D NumPy array of probability values.
- conditions: list[str]
- property values: list[str]
Get the possible node states for cells in this line.
- Returns:
List of state names from the parent CPT’s node.