Skip to content

The CTBN model

A CTBN is : : - A bayesian network with pyagrum.DiscreteVariable random variables.

  • A pyagrum.DiGraph to represents dependency between random variables. The model allows cycles.
  • A pyagrum.ctbn.CIM containing exponential distribution parameters for each variable describing its transition duration.

A CIM is : : - A Conditional Intensity Matrix. The CIM of a variable XX contains at least two variables XiX_i and XjX_j, from/to states.

  • Diagonal coefficients qi,jq_{i,j}.
  • Di,jD_{i,j} ~ exp(qi,j)exp(q_{i,j}) and DiD_i ~ exp(jqi,j)exp(\sum_jq_{i,j}), waiting time before going from state i to state j (resp. before leaving state i).

Properties : : - PX(x1x2)=qi,jqiP_X(x_1\rightarrow x_2) = \frac{q_{i,j}}{q_i}

  • E(Di,j)=1qi,j\mathbb{E}(D_{i,j}) = \frac{1}{q_{i,j}}
  • ijqi,j=0\forall i \sum_{j}q_{i,j} = 0

This class is used to represent a CIM (random variables and parameters of transition time). A CIM is mainly a pyagrum.Tensor that contains the parameters of an exponential distribution. This class contains also contains the amalgamation operator, used for merging CIMs into one.

Notes

A static string DELIMITER is used to differentiate between the current state of the variable and its next state. <V_name>i : starting node <V_name>j : ending node

  • Parameters: pot (pyagrum.Tensor | None) – Defines the new CIM using existing tensor.

Add a new variable to the CIM.

Amalgamation of 2 CIMs, i.e combine 2 CIMs into one. When manipulating CIMs * can be used instead.

  • Parameters:
    • cimY (CIM) – CIM to amalgamate self with.
    • self (CIM)
  • Returns: Amalgamation of the CIMs.
  • Return type: CIM

Creates a new CIM extracted from the current CIM using the instantiation ctxt.

  • Parameters: ctxt (Optional [**Dict [**str , str ] ]) – Instantiation of variables.
  • Returns: The extracted CIM.
  • Return type: CIM

Finds a variable in the CIM using its name.

  • Parameters: name (str) – Name of the variable to find.
  • Returns: The variable if it is in the CIM.
  • Return type: pyagrum.DiscreteVariable | None

Fills the CIM with matrix mat.

Notes

Only works for square-shaped matrixes, if the CIM is not conditional.

  • Parameters: mat (numpy.array) – Matrix to convert into a CIM.
  • Returns: True if there is no conditioning variable (parent) in the CIM.
  • Return type: bool

Uses the syntax convention to check if a variable’s name is the name of a parent in the CIM.

  • Parameters: v (pyagrum.DiscreteVariable) – Variable to check.
  • Returns: True if v’s name corresponds to the name of a parent in the CIM.
  • Return type: bool
  • Returns: The number of variables in the CIM (including v_i and v_j variables).
  • Return type: int

Removes a variable from the CIM. Only for parent variables.

Converts a CIM to a numpy.array matrix.

Notes

Only works for CIMs with no conditioning variable, or CIMs obtained through amalgamation.

  • Parameters: ctxt (Optional [**Dict [**str , str ] ] | None) – Instantiation of variables to use if given.
  • Returns: The CIM as a numpy.array.
  • Return type: numpy.array
  • Parameters: name (str) – Name of the variable to format.
  • Returns: The name of the variable using {name}{DELIMITER}i format.
  • Return type: str
  • Parameters: name (str) – Name of the variable to format.
  • Returns: The name of the variable using {name}{DELIMITER}j format.
  • Return type: str
  • Returns: A list containing the name of each variable in the CIM.
  • Return type: List[str]
  • Parameters: v (pyagrum.DiscreteVariable) – Variable to get name from.
  • Returns: The name of the variable (without DELIMITER formatting).
  • Return type: str
  • Parameters: arg (int) – Index of the variable in the CIM (i.e its node id).
  • Returns: The variable at index arg.
  • Return type: pyagrum.DiscreteVariable

This class is used to represent a CTBN. A CTBN is : a set of random variables, a CIM for each one of them and a pyagrum.DiGraph to represent dependency relations.

Graph representing dependency relations between variables. Also used to link a variable with an id.

Dict containing a CIM for each nodeId(the integer given to a variable).

  • Type: Dict[NodeId, CIM]

Dict containing the variable associated to a node id.

Dict containing the nodeId associated to a variable’s name.

  • Type: Dict[str, NodeId]
  • Parameters: val (NameOrId) – The variable’s name or id.
  • Returns: The variable’s CIM.
  • Return type: CIM
  • Raises: pyagrum.NotFound – If the variable isn’t in the CTBN.

Add a new variable to the Ctbn.

  • Parameters: var (pyagrum.DiscreteVariable) – The variable to add to the CTBN.
  • Returns: The id given to the variable.
  • Return type: NodeId
  • Raises:
    • NameError – If a variable with the same name already exists.
    • ValueError – If the variable is None.

Adds an arc val1 -> val2.

  • Parameters:
    • val1 (NameOrId) – The name or id of the first variable.
    • val2 (NameOrId) – The name or id of the second variable.
  • Returns: The created arc (val1, val2).
  • Return type: Tuple[NodeId, NodeId]
  • Raises: pyagrum.NotFound – If one the variables is not in the CTBN.
  • Returns: The set of arcs as a set of couple of NodeIds in the CTBN.
  • Return type: Set[Tuple[NodeId, NodeId]]
  • Parameters: val (NameOrId) – The variable’s name or id.
  • Returns: A set containing the ids of the variable’s children.
  • Return type: Set[NodeId]
  • Raises: pyagrum.NotFound – If the variable isn’t in the CTBN.
  • Parameters: val (NameOrId) – The variable’s name or id.
  • Returns: A list containing the names of a variable’s children.
  • Return type: List[str]
  • Raises: pyagrum.NotFound – If the variable isn’t in the CTBN.

Tests the topologic equality with another CTBN.

  • Parameters: ctbn (CTBN) – CTBN to test equality with.
  • Returns: True if they are equal, False if not.
  • Return type: bool

Erases an arc from the graph.

  • Parameters:
    • val1 (NameOrId) – The name or id of the first variable.
    • val2 (NameOrId) – The name or id of the second variable.
  • Raises:
  • Returns: An instantiation of the variables in the CTBN including the corresponding starting and ending (i.e from/to variables) variables.
  • Return type: pyagrum.Instatiation
  • Parameters: val (NameOrId) – The name or id of the variable.
  • Returns: A tuple containing the labels of the variable.
  • Return type: tuple
  • Raises: pyagrum.NotFound – If the variable is not found in the CTBN.
  • Parameters: node (NodeId) – The id of the variable.
  • Returns: The variable’s name linked to the NodeId.
  • Return type: str
  • Raises: pyagrum.NotFound – If the variable is not found in the CTBN.
  • Returns: The list of variables name in the CTBN.
  • Return type: List[str]
  • Parameters: name (str) – The name of the variable.
  • Returns: The id of the variable.
  • Return type: NodeId
  • Raises: pyagrum.NotFound – If the variable is not found in the CTBN.
  • Returns: The list of variables id in the CTBN.
  • Return type: List[NodeId]
  • Parameters: val (NameOrId) – The variable’s name or id.
  • Returns: A list containing the names of the variable’s parents.
  • Return type: List[str]
  • Raises: pyagrum.NotFound – If the variable isn’t in the CTBN.
  • Parameters: val (NameOrId) – The variable’s name or id.
  • Returns: A set containing the id of the variable’s parents in the CTBN.
  • Return type: Set[NodeId]
  • Raises: pyagrum.NotFound – If the variable isn’t found in the CTBN.

Create a display of the graph representating the CTBN.

  • Returns: A display of the graph.
  • Return type: str
  • Parameters: val (NameOrId) – The name or id of the variable.
  • Returns: The corresponding variable.
  • Return type: pyagrum.DiscreteVariable
  • Raises: pyagrum.NotFound – If the variable is not found in the CTBN.

pyagrum.ctbn.randomCTBN(valueRange, n=1, parMax=1, modal=2)

Section titled “pyagrum.ctbn.randomCTBN(valueRange, n=1, parMax=1, modal=2)”

Generates a random CTBN using Prufer’s sequence. Note : as diagonal coefficents are the negative sum of the coefficients one the line in a CIM, their value does not necessarily belong to valueRange.

  • Parameters:
    • valueRange (Tuple [**float , float ]) – Range to choose values from when filling the CIMs.
    • n (int) – Number of variables.
    • parMax (int) – Maximum number of parents a variable can have.
    • modal (int) – Number of states a variable has (domain size).
  • Returns: A randomly generated ctbn.
  • Return type: CTBN