causal_hub.models#

Submodule models.

class causal_hub.models.CatBN(graph, cpds)#

Bases: object

A categorical Bayesian network (BN).

classmethod __new__(*args, **kwargs)#
cpds()#

Returns the a map labels-distributions.

Returns:

A reference to the CPDs.

Return type:

dict[str, CatCPD]

description()#

Returns the description of the model, if any.

Returns:

The description of the model, if it exists.

Return type:

str | None

do_estimate(x, y, z, method='be', seed=31, parallel=True)#

Estimate a conditional causal effect (CACE).

Parameters:
  • x (str | Iterable[str]) – An intervention variable or an iterable of intervention variables.

  • y (str | Iterable[str]) – An outcome variable or an iterable of outcome variables.

  • z (str | Iterable[str]) – A conditioning variable or an iterable of conditioning variables.

  • method (str) – The method to use for estimation (default is be).

  • seed (int) – The seed of the random number generator (default is 31).

  • parallel (bool) – The flag to enable parallel estimation (default is true).

Returns:

A new conditional causal effect (CACE) distribution, if identifiable.

Return type:

CatCPD | None

estimate(x, z, method='be', seed=31, parallel=True)#

Estimate a conditional probability distribution (CPD).

Parameters:
  • x (str | Iterable[str]) – A variable or an iterable of variables.

  • z (str | Iterable[str]) – A conditioning variable or an iterable of conditioning variables.

  • method (str) – The method to use for estimation (default is be).

  • seed (int) – The seed of the random number generator (default is 31).

  • parallel (bool) – The flag to enable parallel estimation (default is true).

Returns:

A new conditional probability distribution.

Return type:

CatCPD

classmethod fit(dataset, graph, method='be', parallel=True, **kwargs)#

Fit the model to a dataset and a given graph.

Parameters:
  • dataset (CatTable | CatIncTable) – The dataset to fit the model to.

  • graph (DiGraph) – The graph to fit the model to.

  • method (str) – The method to use for fitting (default is be).

  • parallel (bool) – The flag to enable parallel fitting (default is true).

  • **kwargs (dict | None) – Optional keyword arguments:

  • alpha (-)

Returns:

A new fitted model.

Return type:

CatBN

classmethod from_bif_file(path)#

Read class from a BIF file.

Parameters:

path (str) – The path to the BIF file to read from.

Returns:

A new Bayesian network instance.

Return type:

CatBN

classmethod from_bif_string(bif)#

Read class from a BIF string.

Parameters:

bif (str) – The BIF string to read from.

Returns:

A new Bayesian network instance.

Return type:

CatBN

classmethod from_json_file(path)#

Read instance from a JSON file.

Parameters:

path (str) – The path to the JSON file to read from.

Returns:

A new instance.

Return type:

CatBN

classmethod from_json_string(json)#

Read instance from a JSON string.

Parameters:

json (str) – The JSON string to read from.

Returns:

A new instance.

Return type:

CatBN

graph()#

Returns the underlying graph.

Returns:

A reference to the graph.

Return type:

DiGraph

labels()#

Returns the labels of the variables.

Returns:

A reference to the labels.

Return type:

list[str]

name()#

Returns the name of the model, if any.

Returns:

The name of the model, if it exists.

Return type:

str | None

parameters_size()#

Returns the parameters size.

Returns:

The parameters size.

Return type:

int

sample(n, seed=31, parallel=True)#

Generate samples from the model.

Parameters:
  • n (int) – The number of samples to generate.

  • seed (int) – The seed of the random number generator (default is 31).

  • parallel (bool) – The flag to enable parallel sampling (default is true).

Returns:

A new dataset containing the samples.

Return type:

CatTable

to_bif_file(path)#

Write class to a BIF file.

Parameters:

path (str) – The path to the BIF file to write to.

to_bif_string()#

Write class to a BIF string.

Returns:

A BIF string representation of the model.

Return type:

str

to_json_file(path)#

Write instance to a JSON file.

Parameters:

path (str) – The path to the JSON file to write to.

to_json_string()#

Write instance to a JSON string.

Returns:

A JSON string representation of the instance.

Return type:

str

class causal_hub.models.GaussBN(graph, cpds)#

Bases: object

A Gaussian Bayesian network.

classmethod __new__(*args, **kwargs)#
cpds()#

Returns the a map labels-distributions.

Returns:

A reference to the CPDs.

Return type:

dict[str, GaussCPD]

description()#

Returns the description of the model, if any.

Returns:

The description of the model, if it exists.

Return type:

str | None

do_estimate(x, y, z, method='be', seed=31, parallel=True)#

Estimate a conditional causal effect (CACE).

Parameters:
  • x (str | Iterable[str]) – An intervention variable or an iterable of intervention variables.

  • y (str | Iterable[str]) – An outcome variable or an iterable of outcome variables.

  • z (str | Iterable[str]) – A conditioning variable or an iterable of conditioning variables.

  • method (str) – The method to use for estimation (default is be).

  • seed (int) – The seed of the random number generator (default is 31).

  • parallel (bool) – The flag to enable parallel estimation (default is true).

Returns:

A new conditional causal effect (CACE) distribution, if identifiable.

Return type:

GaussCPD | None

estimate(x, z, method='be', seed=31, parallel=True)#

Estimate a conditional probability distribution (CPD).

Parameters:
  • x (str | Iterable[str]) – A variable or an iterable of variables.

  • z (str | Iterable[str]) – A conditioning variable or an iterable of conditioning variables.

  • method (str) – The method to use for estimation (default is be).

  • seed (int) – The seed of the random number generator (default is 31).

  • parallel (bool) – The flag to enable parallel estimation (default is true).

Returns:

A new conditional probability distribution.

Return type:

GaussCPD

classmethod fit(dataset, graph, method='be', parallel=True, **kwargs)#

Fit the model to a dataset and a given graph.

Parameters:
  • dataset (GaussTable) – The dataset to fit the model to.

  • graph (DiGraph) – The graph to fit the model to.

  • method (str) – The method to use for fitting (default is be).

  • parallel (bool) – The flag to enable parallel fitting (default is true).

  • **kwargs (dict | None) – Optional keyword arguments:

  • alpha (-)

Returns:

A new fitted model.

Return type:

GaussBN

classmethod from_json_file(path)#

Read instance from a JSON file.

Parameters:

path (str) – The path to the JSON file to read from.

Returns:

A new instance.

Return type:

GaussBN

classmethod from_json_string(json)#

Read instance from a JSON string.

Parameters:

json (str) – The JSON string to read from.

Returns:

A new instance.

Return type:

GaussBN

graph()#

Returns the underlying graph.

Returns:

A reference to the graph.

Return type:

DiGraph

labels()#

Returns the labels of the variables.

Returns:

A reference to the labels.

Return type:

list[str]

name()#

Returns the name of the model, if any.

Returns:

The name of the model, if it exists.

Return type:

str | None

parameters_size()#

Returns the parameters size.

Returns:

The parameters size.

Return type:

int

sample(n, seed=31, parallel=True)#

Generate samples from the model.

Parameters:
  • n (int) – The number of samples to generate.

  • seed (int) – The seed of the random number generator (default is 31).

  • parallel (bool) – The flag to enable parallel sampling (default is true).

Returns:

A new dataset containing the samples.

Return type:

GaussTable

to_json_file(path)#

Write instance to a JSON file.

Parameters:

path (str) – The path to the JSON file to write to.

to_json_string()#

Write instance to a JSON string.

Returns:

A JSON string representation of the instance.

Return type:

str

class causal_hub.models.CatCTBN(graph, cims)#

Bases: object

A continuous-time Bayesian network (CTBN).

classmethod __new__(*args, **kwargs)#
cims()#

Returns the a map labels-distributions.

Returns:

A reference to the CIMs.

Return type:

dict[str, CatCIM]

description()#

Returns the description of the model, if any.

Returns:

The description of the model, if it exists.

Return type:

str | None

classmethod fit(dataset, graph, method='mle', parallel=True, **kwargs)#

Fit the model to a dataset and a given graph.

Parameters:
  • dataset (CatTrjs) – The dataset to fit the model to.

  • graph (DiGraph) – The graph to fit the model to.

  • method (str) – The method to use for fitting (default is mle).

  • parallel (bool) – The flag to enable parallel fitting (default is true).

  • **kwargs (dict | None) – Optional keyword arguments:

  • alpha (-)

Returns:

A new fitted model.

Return type:

CatCTBN

classmethod from_json_file(path)#

Read instance from a JSON file.

Parameters:

path (str) – The path to the JSON file to read from.

Returns:

A new instance.

Return type:

CatCTBN

classmethod from_json_string(json)#

Read instance from a JSON string.

Parameters:

json (str) – The JSON string to read from.

Returns:

A new instance.

Return type:

CatCTBN

graph()#

Returns the underlying graph.

Returns:

A reference to the graph.

Return type:

DiGraph

initial_distribution()#

Returns the initial distribution.

Returns:

A reference to the initial distribution.

Return type:

CatBN

labels()#

Returns the labels of the variables.

Returns:

A reference to the labels.

Return type:

list[str]

name()#

Returns the name of the model, if any.

Returns:

The name of the model, if it exists.

Return type:

str | None

parameters_size()#

Returns the parameters size.

Returns:

The parameters size.

Return type:

int

sample(n, max_len=None, max_time=None, seed=31, parallel=True)#

Sample from the model.

Parameters:
  • n (int) – The number of trajectories to sample.

  • max_len (int | None) – The maximum length of each trajectory (default is None). Must be set if max_time is None.

  • max_time (float | None) – The maximum time of each trajectory (default is None). Must be set if max_len is None.

  • seed (int) – The seed of the random number generator (default is 31).

  • parallel (bool) – The flag to enable parallel sampling (default is true).

Returns:

A new dataset containing the sampled trajectories.

Return type:

CatTrjs

to_json_file(path)#

Write instance to a JSON file.

Parameters:

path (str) – The path to the JSON file to write to.

to_json_string()#

Write instance to a JSON string.

Returns:

A JSON string representation of the instance.

Return type:

str

class causal_hub.models.CatCPD#

Bases: object

A struct representing a categorical conditional probability distribution.

conditioning_labels()#

Returns the labels of the conditioned variables.

Returns:

A reference to the conditioning labels.

Return type:

list[str]

conditioning_shape()#

Returns the shape of the conditioning variables.

Returns:

The shape of the conditioning variables.

Return type:

list[int]

conditioning_states()#

Returns the states of the conditioning variables.

Returns:

The states of the conditioning variables.

Return type:

dict[str, tuple[str, …]]

classmethod from_json_file(path)#

Read instance from a JSON file.

Parameters:

path (str) – The path to the JSON file to read from.

Returns:

A new instance.

Return type:

CatCPD

classmethod from_json_string(json)#

Read instance from a JSON string.

Parameters:

json (str) – The JSON string to read from.

Returns:

A new instance.

Return type:

CatCPD

labels()#

Returns the label of the conditioned variable.

Returns:

A reference to the label.

Return type:

list[str]

parameters()#

Returns the parameters.

Returns:

A reference to the parameters.

Return type:

numpy.ndarray

parameters_size()#

Returns the parameters size.

Returns:

The parameters size.

Return type:

int

sample_log_likelihood()#

Returns the sample log-likelihood given the distribution, if any.

Returns:

The sample log-likelihood given the distribution, if any.

Return type:

float | None

sample_statistics()#

Returns the sample statistics used to fit the distribution, if any.

Returns:

A dictionary containing the sample statistics used to fit the distribution, if any.

Return type:

dict[str, …] | None

shape()#

Returns the shape of the conditioned variable.

Returns:

The shape of the conditioned variable.

Return type:

list[int]

states()#

Returns the states of the conditioned variable.

Returns:

The states of the conditioned variable.

Return type:

dict[str, tuple[str, …]]

to_json_file(path)#

Write instance to a JSON file.

Parameters:

path (str) – The path to the JSON file to write to.

to_json_string()#

Write instance to a JSON string.

Returns:

A JSON string representation of the instance.

Return type:

str

class causal_hub.models.GaussCPD#

Bases: object

A struct representing a Gaussian conditional probability distribution.

conditioning_labels()#

Returns the labels of the conditioned variables.

Returns:

A reference to the conditioning labels.

Return type:

list[str]

classmethod from_json_file(path)#

Read instance from a JSON file.

Parameters:

path (str) – The path to the JSON file to read from.

Returns:

A new instance.

Return type:

GaussCPD

classmethod from_json_string(json)#

Read instance from a JSON string.

Parameters:

json (str) – The JSON string to read from.

Returns:

A new instance.

Return type:

GaussCPD

labels()#

Returns the label of the conditioned variable.

Returns:

A reference to the label.

Return type:

list[str]

parameters()#

Returns the parameters.

Returns:

A reference to the parameters.

Return type:

dict[str, …]

parameters_size()#

Returns the parameters size.

Returns:

The parameters size.

Return type:

int

sample_log_likelihood()#

Returns the sample log-likelihood given the distribution, if any.

Returns:

The sample log-likelihood given the distribution, if any.

Return type:

float | None

sample_statistics()#

Returns the sample statistics used to fit the distribution, if any.

Returns:

A dictionary containing the sample statistics used to fit the distribution, if any.

Return type:

dict[str, …] | None

to_json_file(path)#

Write instance to a JSON file.

Parameters:

path (str) – The path to the JSON file to write to.

to_json_string()#

Write instance to a JSON string.

Returns:

A JSON string representation of the instance.

Return type:

str

class causal_hub.models.CatCIM#

Bases: object

A struct representing a categorical conditional intensity matrix (CIM).

conditioning_labels()#

Returns the labels of the conditioned variables.

Returns:

A reference to the conditioning labels.

Return type:

list[str]

conditioning_shape()#

Returns the shape of the conditioning variables.

Returns:

The shape of the conditioning variables.

Return type:

list[int]

conditioning_states()#

Returns the states of the conditioning variables.

Returns:

The states of the conditioning variables.

Return type:

dict[str, tuple[str]]

classmethod from_json_file(path)#

Read instance from a JSON file.

Parameters:

path (str) – The path to the JSON file to read from.

Returns:

A new instance.

Return type:

CatCIM

classmethod from_json_string(json)#

Read instance from a JSON string.

Parameters:

json (str) – The JSON string to read from.

Returns:

A new instance.

Return type:

CatCIM

labels()#

Returns the label of the conditioned variable.

Returns:

A reference to the label.

Return type:

list[str]

parameters()#

Returns the parameters.

Returns:

A reference to the parameters.

Return type:

numpy.ndarray

parameters_size()#

Returns the parameters size.

Returns:

The parameters size.

Return type:

int

sample_log_likelihood()#

Returns the sample log-likelihood given the distribution, if any.

Returns:

The sample log-likelihood given the distribution, if any.

Return type:

float | None

sample_statistics()#

Returns the sample statistics used to fit the distribution, if any.

Returns:

A dictionary containing the sample statistics used to fit the distribution, if any.

Return type:

dict[str, …] | None

shape()#

Returns the shape of the conditioned variable.

Returns:

The shape of the conditioned variable.

Return type:

list[int]

states()#

Returns the states of the conditioned variable.

Returns:

A reference to the states.

Return type:

dict[str, tuple[str]]

to_json_file(path)#

Write instance to a JSON file.

Parameters:

path (str) – The path to the JSON file to write to.

to_json_string()#

Write instance to a JSON string.

Returns:

A JSON string representation of the instance.

Return type:

str

class causal_hub.models.DiGraph#

Bases: object

A struct representing a directed graph using an adjacency matrix.

add_edge(x, y)#

Adds an edge between vertices x and y.

Parameters:
  • x (str) – The first vertex.

  • y (str) – The second vertex.

Returns:

true if the edge was added, false if it already existed.

Return type:

bool

ancestors(x)#

Returns the ancestors of a vertex x.

Parameters:

x (str | Iterable[str]) – A vertex or an iterable of vertices.

Returns:

A list of ancestor vertices.

Return type:

list[str]

children(x)#

Returns the children of a vertex x.

Parameters:

x (str | Iterable[str]) – A vertex or an iterable of vertices.

Returns:

A list of child vertices.

Return type:

list[str]

classmethod complete(vertices)#

Creates a complete directed graph with the given vertices.

Parameters:

vertices (Iterable[str]) – The the vertices of the graph. Vertices will be sorted in alphabetical order. No self-loops are created.

Returns:

A new graph instance.

Return type:

DiGraph

del_edge(x, y)#

Deletes the edge between vertices x and y.

Parameters:
  • x (str) – The first vertex.

  • y (str) – The second vertex.

Returns:

true if the edge was deleted, false if it did not exist.

Return type:

bool

descendants(x)#

Returns the descendants of a vertex x.

Parameters:

x (str | Iterable[str]) – A vertex or an iterable of vertices.

Returns:

A list of descendant vertices.

Return type:

list[str]

edges()#

Returns the edges of the graph.

Returns:

A list of edges.

Return type:

list[tuple[str, str]]

classmethod empty(vertices)#

Creates an empty directed graph with the given vertices.

Parameters:

vertices (Iterable[str]) – The vertices of the graph. Vertices will be sorted in alphabetical order.

Returns:

A new graph instance.

Return type:

DiGraph

find_minimal_backdoor_set(x, y, w=None, v=None)#

Finds a minimal backdoor set for the vertex sets X and Y, if any.

Parameters:
  • x (Iterable[str]) – An iterable of vertices representing set X.

  • y (Iterable[str]) – An iterable of vertices representing set Y.

  • w (Iterable[str] | None) – An optional iterable of vertices representing set W.

  • v (Iterable[str] | None) – An optional iterable of vertices representing set V.

Notes

Raises an exception if:

  • Any of the vertex in X, Y, W or V are out of bounds.

  • X and Y are not disjoint sets.

  • X or Y are empty sets.

  • Not W <= V.

Returns:

A minimal backdoor set, or None if no backdoor set exists.

Return type:

list[str] | None

find_minimal_separator_set(x, y, w=None, v=None)#

Finds a minimal separator set for the vertex sets X and Y, if any.

Parameters:
  • x (Iterable[str]) – An iterable of vertices representing set X.

  • y (Iterable[str]) – An iterable of vertices representing set Y.

  • w (Iterable[str] | None) – An optional iterable of vertices representing set W.

  • v (Iterable[str] | None) – An optional iterable of vertices representing set V.

Notes

Raises an exception if:

  • Any of the vertex in X, Y, W or V are out of bounds.

  • X and Y are not disjoint sets.

  • X or Y are empty sets.

  • Not W <= V.

Returns:

A minimal separator set, or None if no separator set exists.

Return type:

list[str] | None

classmethod from_json_file(path)#

Read instance from a JSON file.

Parameters:

path (str) – The path to the JSON file to read from.

Returns:

A new instance.

Return type:

DiGraph

classmethod from_json_string(json)#

Read instance from a JSON string.

Parameters:

json (str) – The JSON string to read from.

Returns:

A new instance.

Return type:

DiGraph

classmethod from_networkx(g)#

Converts from a NetworkX DiGraph.

Parameters:

g (networkx.DiGraph) – A NetworkX DiGraph to convert from.

Returns:

A new instance.

Return type:

DiGraph

has_edge(x, y)#

Checks if there is an edge between vertices x and y.

Parameters:
  • x (str) – The first vertex.

  • y (str) – The second vertex.

Returns:

true if there is an edge between x and y, false otherwise.

Return type:

bool

has_vertex(x)#

Checks if a vertex exists in the graph.

Parameters:

x (str) – The vertex.

Returns:

true if the vertex exists, false otherwise.

Return type:

bool

is_backdoor_set(x, y, z)#

Checks if the vertex set Z is a backdoor set for X and Y.

Parameters:
  • x (Iterable[str]) – An iterable of vertices representing set X.

  • y (Iterable[str]) – An iterable of vertices representing set Y.

  • z (Iterable[str]) – An iterable of vertices representing set Z.

Notes

Raises an exception if:

  • Any of the vertex in X, Y, or Z are out of bounds.

  • X, Y or Z are not disjoint sets.

  • X and Y are empty sets.

Returns:

true if Z is a backdoor set for X and Y, false otherwise.

Return type:

bool

is_minimal_backdoor_set(x, y, z, w=None, v=None)#

Checks if the vertex set Z is a minimal backdoor set for X and Y.

Parameters:
  • x (Iterable[str]) – An iterable of vertices representing set X.

  • y (Iterable[str]) – An iterable of vertices representing set Y.

  • z (Iterable[str]) – An iterable of vertices representing set Z.

  • w (Iterable[str] | None) – An optional iterable of vertices representing set W.

  • v (Iterable[str] | None) – An optional iterable of vertices representing set V.

Notes

Raises an exception if:

  • Any of the vertex in X, Y, Z, W or V are out of bounds.

  • X, Y or Z are not disjoint sets.

  • X and Y are empty sets.

  • Not W <= Z <= V.

Returns:

true if Z is a minimal backdoor set for X and Y, false otherwise.

Return type:

bool

is_minimal_separator_set(x, y, z, w=None, v=None)#

Checks if the vertex set Z is a minimal separator set for X and Y.

Parameters:
  • x (Iterable[str]) – An iterable of vertices representing set X.

  • y (Iterable[str]) – An iterable of vertices representing set Y.

  • z (Iterable[str]) – An iterable of vertices representing set Z.

  • w (Iterable[str] | None) – An optional iterable of vertices representing set W.

  • v (Iterable[str] | None) – An optional iterable of vertices representing set V.

Notes

Raises an exception if:

  • Any of the vertex in X, Y, Z, W or V are out of bounds.

  • X, Y or Z are not disjoint sets.

  • X and Y are empty sets.

  • Not W <= Z <= V.

Returns:

true if Z is a minimal separator set for X and Y, false otherwise.

Return type:

bool

is_separator_set(x, y, z)#

Checks if the vertex set Z is a separator set for X and Y.

Parameters:
  • x (Iterable[str]) – An iterable of vertices representing set X.

  • y (Iterable[str]) – An iterable of vertices representing set Y.

  • z (Iterable[str]) – An iterable of vertices representing set Z.

Notes

Raises an exception if:

  • Any of the vertex in X, Y, or Z are out of bounds.

  • X, Y or Z are not disjoint sets.

  • X and Y are empty sets.

Returns:

true if X and Y are separated by Z, false otherwise.

Return type:

bool

parents(x)#

Returns the parents of a vertex x.

Parameters:

x (str | Iterable[str]) – A vertex or an iterable of vertices.

Returns:

A list of parent vertices.

Return type:

list[str]

to_json_file(path)#

Write instance to a JSON file.

Parameters:

path (str) – The path to the JSON file to write to.

to_json_string()#

Write instance to a JSON string.

Returns:

A JSON string representation of the instance.

Return type:

str

to_networkx()#

Converts to a NetworkX DiGraph.

Returns:

A NetworkX DiGraph representation of the graph.

Return type:

networkx.DiGraph

vertices()#

Returns the vertices of the graph.

# Returns

A list of vertices.