ETIA.CausalLearning.CausalModel package

Submodules

ETIA.CausalLearning.CausalModel.BayessianNetwork module

class BayesianNetwork(edges)[source]

Bases: object

add_node(node)[source]

Add a node to the Bayesian Network.

Parameters:

node (str) – The name of the node to be added.

remove_node(node)[source]

Remove a node from the Bayesian Network.

Parameters:

node (str) – The name of the node to be removed.

add_edge(edge)[source]

Add an edge between two nodes in the Bayesian Network.

Parameters:

edge (tuple) – A tuple representing the directed edge between two nodes.

remove_edge(edge)[source]

Remove an edge between two nodes in the Bayesian Network.

Parameters:

edge (tuple) – A tuple representing the directed edge between two nodes.

get_parents(node)[source]

Get the parents of a given node in the Bayesian Network.

Parameters:

node (str) – The name of the node whose parents are to be retrieved.

Returns:

List of parent nodes.

Return type:

list

get_children(node)[source]

Get the children of a given node in the Bayesian Network.

Parameters:

node (str) – The name of the node whose children are to be retrieved.

Returns:

List of child nodes.

Return type:

list

get_nodes()[source]

Get all the nodes in the Bayesian Network.

Returns:

List of all nodes.

Return type:

list

get_edges()[source]

Get all the edges in the Bayesian Network.

Returns:

List of all edges.

Return type:

list

get_cpds()[source]

Get all the Conditional Probability Distributions (CPDs) in the Bayesian Network.

Returns:

List of CPDs.

Return type:

list

get_inference()[source]

Get the VariableElimination object for inference.

Returns:

Object for performing inference.

Return type:

VariableElimination

set_evidence(evidence)[source]

Set the evidence for inference in the Bayesian Network.

Parameters:

evidence (dict) – Dictionary where keys are node names and values are observed states.

query(nodes, show_progress=False)[source]

Perform inference and return the marginal probabilities for the given nodes.

Parameters:
  • nodes (str or list) – Single node or list of nodes for which marginal probabilities are to be computed.

  • show_progress (bool, optional) – If True, display a progress bar during inference. Default is False.

Returns:

Dictionary containing marginal probabilities of the queried nodes.

Return type:

dict

map_query(show_progress=False)[source]

Perform inference and return the most probable states of the nodes.

Parameters:

show_progress (bool, optional) – If True, display a progress bar during inference. Default is False.

Returns:

Dictionary containing the most probable states of the nodes.

Return type:

dict

maximum_likelihood_estimation(data)[source]

Estimate the parameters of the Bayesian Network using Maximum Likelihood Estimation.

Parameters:

data (pandas DataFrame) – The dataset for parameter estimation.

bayesian_parameter_estimation(data)[source]

Estimate the parameters of the Bayesian Network using Bayesian Parameter Estimation.

Parameters:

data (pandas DataFrame) – The dataset for parameter estimation.

ETIA.CausalLearning.CausalModel.CPDAG module

class CPDAGWrapper(incoming_directed_edges=None, incoming_undirected_edges=None)[source]

Bases: GraphWrapperBase

add_node(node)[source]

Add a node to the CPDAG.

Parameters:

node (hashable object) – The node to be added.

remove_node(node)[source]

Remove a node from the CPDAG.

Parameters:

node (hashable object) – The node to be removed.

add_directed_edge(source, target)[source]

Add a directed edge to the CPDAG.

Parameters:
  • source (hashable object) – The source node of the directed edge.

  • target (hashable object) – The target node of the directed edge.

add_undirected_edge(source, target)[source]

Add an undirected edge to the CPDAG.

Parameters:
  • source (hashable object) – The source node of the undirected edge.

  • target (hashable object) – The target node of the undirected edge.

remove_edge(source, target)[source]

Remove an edge from the CPDAG.

Parameters:
  • source (hashable object) – The source node of the edge to be removed.

  • target (hashable object) – The target node of the edge to be removed.

get_nodes()[source]

Return the nodes of the CPDAG.

Returns:

List of nodes in the CPDAG.

Return type:

list

get_edges()[source]

Return the edges of the CPDAG.

Returns:

Dictionary containing directed and undirected edges of the CPDAG.

Return type:

dict

possible_children(node)[source]

Return an iterator over possible children of a node.

Parameters:

node (hashable object) – The node whose possible children are to be retrieved.

Returns:

Iterator over possible children of the given node.

Return type:

iterator

possible_parents(node)[source]

Return an iterator over possible parents of a node.

Parameters:

node (hashable object) – The node whose possible parents are to be retrieved.

Returns:

Iterator over possible parents of the given node.

Return type:

iterator

orient_uncertain_edge(u, v)[source]

Orient an undirected edge into a directed edge.

Parameters:
  • u (hashable object) – One endpoint of the undirected edge.

  • v (hashable object) – The other endpoint of the undirected edge.

ETIA.CausalLearning.CausalModel.DAG module

class DAGWrapper[source]

Bases: GraphWrapperBase

add_node(node)[source]

Add a node to the DAG.

Parameters:

node (hashable object) – The node to be added.

remove_node(node)[source]

Remove a node from the DAG.

Parameters:

node (hashable object) – The node to be removed.

add_directed_edge(source, target)[source]

Add a directed edge to the DAG. Checks for cycles and raises an error if an edge creates a cycle.

Parameters:
  • source (hashable object) – The source node of the directed edge.

  • target (hashable object) – The target node of the directed edge.

remove_edge(source, target)[source]

Remove an edge from the DAG.

Parameters:
  • source (hashable object) – The source node of the edge to be removed.

  • target (hashable object) – The target node of the edge to be removed.

get_nodes()[source]

Return the nodes of the DAG.

Returns:

List of nodes in the DAG.

Return type:

list

get_edges()[source]

Return the edges of the DAG.

Returns:

List of edges in the DAG.

Return type:

list

ETIA.CausalLearning.CausalModel.GraphWrapperBase module

class GraphWrapperBase[source]

Bases: object

add_node(node)[source]

Add a node to the graph.

Parameters:

node (hashable object) – The node to be added.

remove_node(node)[source]

Remove a node from the graph.

Parameters:

node (hashable object) – The node to be removed.

add_edge(source, target)[source]

Add an edge to the graph.

Parameters:
  • source (hashable object) – The source node of the edge.

  • target (hashable object) – The target node of the edge.

remove_edge(source, target)[source]

Remove an edge from the graph.

Parameters:
  • source (hashable object) – The source node of the edge to be removed.

  • target (hashable object) – The target node of the edge to be removed.

get_nodes()[source]

Return the nodes of the graph.

Returns:

List of nodes in the graph.

Return type:

list

get_edges()[source]

Return the edges of the graph.

Returns:

List of edges in the graph.

Return type:

list

ETIA.CausalLearning.CausalModel.MAG module

class MAGWrapper(incoming_directed_edges=None, incoming_bidirected_edges=None)[source]

Bases: GraphWrapperBase

add_node(node)[source]

Add a node to the MAG.

Parameters:

node (hashable object) – The node to be added.

remove_node(node)[source]

Remove a node from the MAG.

Parameters:

node (hashable object) – The node to be removed.

add_directed_edge(source, target)[source]

Add a directed edge to the MAG.

Parameters:
  • source (hashable object) – The source node of the directed edge.

  • target (hashable object) – The target node of the directed edge.

add_bidirected_edge(source, target)[source]

Add a bidirected edge to the MAG.

Parameters:
  • source (hashable object) – The source node of the bidirected edge.

  • target (hashable object) – The target node of the bidirected edge.

remove_edge(source, target)[source]

Remove an edge from the MAG.

Parameters:
  • source (hashable object) – The source node of the edge to be removed.

  • target (hashable object) – The target node of the edge to be removed.

get_nodes()[source]

Return the nodes of the MAG.

Returns:

List of nodes in the MAG.

Return type:

list

get_edges()[source]

Return the edges of the MAG.

Returns:

Dictionary containing directed and bidirected edges of the MAG.

Return type:

dict

ETIA.CausalLearning.CausalModel.PAG module

class PAGWrapper(incoming_directed_edges=None, incoming_undirected_edges=None, incoming_bidirected_edges=None, incoming_circle_edges=None)[source]

Bases: GraphWrapperBase

add_node(node)[source]

Add a node to the PAG.

Parameters:

node (hashable object) – The node to be added.

remove_node(node)[source]

Remove a node from the PAG.

Parameters:

node (hashable object) – The node to be removed.

add_directed_edge(source, target)[source]

Add a directed edge to the PAG.

Parameters:
  • source (hashable object) – The source node of the directed edge.

  • target (hashable object) – The target node of the directed edge.

add_bidirected_edge(source, target)[source]

Add a bidirected edge to the PAG.

Parameters:
  • source (hashable object) – The source node of the bidirected edge.

  • target (hashable object) – The target node of the bidirected edge.

add_undirected_edge(source, target)[source]

Add an undirected edge to the PAG.

Parameters:
  • source (hashable object) – The source node of the undirected edge.

  • target (hashable object) – The target node of the undirected edge.

add_circle_edge(source, target)[source]

Add a circle edge (circle edge on one side) to the PAG.

Parameters:
  • source (hashable object) – One side of the circle edge.

  • target (hashable object) – The other side of the circle edge.

remove_edge(source, target)[source]

Remove an edge from the PAG.

Parameters:
  • source (hashable object) – The source node of the edge to be removed.

  • target (hashable object) – The target node of the edge to be removed.

get_nodes()[source]

Return the nodes of the PAG.

Returns:

List of nodes in the PAG.

Return type:

list

get_edges()[source]

Return the edges of the PAG.

Returns:

Dictionary containing directed, bidirected, undirected, and circle edges of the PAG.

Return type:

dict

possible_children(node)[source]

Return an iterator over possible children of a node.

Parameters:

node (hashable object) – The node whose possible children are to be retrieved.

Returns:

Iterator over possible children of the given node.

Return type:

iterator

possible_parents(node)[source]

Return an iterator over possible parents of a node.

Parameters:

node (hashable object) – The node whose possible parents are to be retrieved.

Returns:

Iterator over possible parents of the given node.

Return type:

iterator

children(node)[source]

Return the definite children of a node.

Parameters:

node (hashable object) – The node whose definite children are to be retrieved.

Returns:

List of definite children of the given node.

Return type:

list

parents(node)[source]

Return the definite parents of a node.

Parameters:

node (hashable object) – The node whose definite parents are to be retrieved.

Returns:

List of definite parents of the given node.

Return type:

list

ETIA.CausalLearning.CausalModel.utils module

matrix_to_pywhy_graph(matrix, graph_type='DAG')[source]

Convert a matrix representation to a pywhy-graphs graph.

Parameters:
  • matrix (numpy.ndarray or pandas.DataFrame) – The matrix representation of the graph.

  • graph_type (str, optional) – The type of graph to be created. Default is ‘DAG’. Supported types are ‘DAG’, ‘CPDAG’, ‘PAG’, and ‘MAG’.

Returns:

The pywhy-graphs graph object.

Return type:

GraphWrapperBase

pywhy_graph_to_matrix(graph)[source]

Convert a pywhy-graphs graph to a matrix representation.

Parameters:

graph (GraphWrapperBase) – The pywhy-graphs graph object.

Returns:

The matrix representation of the graph.

Return type:

numpy.ndarray