mbdlyb.cpt ========== .. py:module:: mbdlyb.cpt .. autoapi-nested-parse:: 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 ------- .. autoapisummary:: mbdlyb.cpt.CPT mbdlyb.cpt.CPTCell mbdlyb.cpt.CPTLine Module Contents --------------- .. py:class:: CPT(node: mbdlyb.gdb.GMBDNode, lines: Optional[list[Union[CPTLine, list[float]]]] = 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). :ivar node: The GMBDNode this CPT is defined for. :ivar lines: List of CPTLine objects, one per parent state combination. :ivar parents: List of parent nodes (derived from node.parents()). .. py:method:: create_lines(_lines: Optional[list[Union[CPTLine, list[float]]]] = 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. :param _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. .. py:method:: from_df(node: mbdlyb.gdb.GMBDNode, df: pandas.DataFrame) -> tuple[CPT, Optional[str]] :staticmethod: 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. :param node: The node this CPT is for. :param df: DataFrame in the format produced by to_df(). :returns: Tuple of (CPT object, error message). Error message is None if successful. .. py:method:: from_dict(node: mbdlyb.gdb.GMBDNode, d: dict) -> tuple[CPT, Optional[str]] :staticmethod: 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. :param node: The node this CPT is for. :param d: Dictionary in the format produced by to_dict(). :returns: Tuple of (CPT object, error message). Error message is None if successful. .. py:method:: normalize() -> CPT Normalize all lines in this CPT. Ensures that each line's probabilities sum to 1.0. :returns: Self for method chaining. .. py:method:: set_prob(state: str, prob: float) Set probability for a given state across all lines. :param state: The state name to set probability for. :param prob: The probability value to assign. :raises ValueError: If the state is not in the node's states. .. py:method:: 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. .. py:method:: to_dict() -> dict Convert CPT to dictionary format. :returns: 'state_count', 'parents' (with FQNs and state counts), and 'lines' (lists of probabilities). :rtype: Dictionary with keys .. py:method:: to_np() -> numpy.ndarray Convert CPT to NumPy array format. :returns: 2D NumPy array with shape (num_conditions, num_states). .. py:attribute:: lines :type: list[CPTLine] .. py:attribute:: node :type: mbdlyb.gdb.GMBDNode .. py:property:: parents :type: list[mbdlyb.gdb.GMBDNode] Get the parent nodes for this CPT. :returns: List of parent nodes. .. py:property:: values :type: list[str] Get the possible states of the node this CPT is for. :returns: List of state names. .. py:class:: 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. :ivar line: The CPTLine that contains this cell. :ivar probability: The probability value (should be in [0, 1]). .. py:property:: cpt :type: CPT Get the CPT that contains this cell. :returns: The CPT object through the parent line. .. py:attribute:: line :type: CPTLine .. py:attribute:: probability :type: float .. py:class:: CPTLine(cpt: CPT, conditions: list[str], probabilities: Optional[list[float]] = 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. :ivar cpt: The parent CPT that contains this line. :ivar conditions: List of parent state values that define this row's conditions. :ivar cells: List of CPTCell objects, one per node state. .. py:method:: 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. .. py:method:: to_np() -> numpy.ndarray Convert this line's probabilities to a NumPy array. :returns: 1D NumPy array of probability values. .. py:attribute:: cells :type: list[CPTCell] .. py:attribute:: conditions :type: list[str] .. py:attribute:: cpt :type: CPT .. py:property:: values :type: list[str] Get the possible node states for cells in this line. :returns: List of state names from the parent CPT's node.