causal_hub.models#
Submodule models.
- class causal_hub.models.CatBN(graph, cpds)#
Bases:
objectA categorical Bayesian network (BN).
- classmethod __new__(*args, **kwargs)#
- cpds()#
Returns the a map labels-distributions.
- 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:
- 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:
- classmethod from_bif_file(path)#
Read class from a BIF file.
- classmethod from_bif_string(bif)#
Read class from a BIF string.
- classmethod from_json_file(path)#
Read instance from a JSON file.
- classmethod from_json_string(json)#
Read instance from a JSON string.
- labels()#
Returns the labels of the variables.
- name()#
Returns the name of the model, if any.
- Returns:
The name of the model, if it exists.
- Return type:
str | None
- sample(n, seed=31, parallel=True)#
Generate samples from the model.
- 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:
- class causal_hub.models.GaussBN(graph, cpds)#
Bases:
objectA Gaussian Bayesian network.
- classmethod __new__(*args, **kwargs)#
- cpds()#
Returns the a map labels-distributions.
- 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:
- 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:
- classmethod from_json_file(path)#
Read instance from a JSON file.
- classmethod from_json_string(json)#
Read instance from a JSON string.
- labels()#
Returns the labels of the variables.
- name()#
Returns the name of the model, if any.
- Returns:
The name of the model, if it exists.
- Return type:
str | None
- sample(n, seed=31, parallel=True)#
Generate samples from the model.
- Parameters:
- Returns:
A new dataset containing the samples.
- Return type:
- class causal_hub.models.CatCTBN(graph, cims)#
Bases:
objectA continuous-time Bayesian network (CTBN).
- classmethod __new__(*args, **kwargs)#
- cims()#
Returns the a map labels-distributions.
- 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:
- classmethod from_json_file(path)#
Read instance from a JSON file.
- classmethod from_json_string(json)#
Read instance from a JSON string.
- initial_distribution()#
Returns the initial distribution.
- Returns:
A reference to the initial distribution.
- Return type:
- labels()#
Returns the labels of the variables.
- name()#
Returns the name of the model, if any.
- Returns:
The name of the model, if it exists.
- Return type:
str | None
- 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:
- class causal_hub.models.CatCPD#
Bases:
objectA struct representing a categorical conditional probability distribution.
- conditioning_labels()#
Returns the labels of the conditioned variables.
- conditioning_shape()#
Returns the shape of the conditioning variables.
- conditioning_states()#
Returns the states of the conditioning variables.
- classmethod from_json_file(path)#
Read instance from a JSON file.
- classmethod from_json_string(json)#
Read instance from a JSON string.
- labels()#
Returns the label of the conditioned variable.
- parameters()#
Returns the parameters.
- Returns:
A reference to the parameters.
- Return type:
- 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.
- shape()#
Returns the shape of the conditioned variable.
- states()#
Returns the states of the conditioned variable.
- class causal_hub.models.GaussCPD#
Bases:
objectA struct representing a Gaussian conditional probability distribution.
- conditioning_labels()#
Returns the labels of the conditioned variables.
- classmethod from_json_file(path)#
Read instance from a JSON file.
- classmethod from_json_string(json)#
Read instance from a JSON string.
- labels()#
Returns the label of the conditioned variable.
- parameters()#
Returns the parameters.
- 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.
- class causal_hub.models.CatCIM#
Bases:
objectA struct representing a categorical conditional intensity matrix (CIM).
- conditioning_labels()#
Returns the labels of the conditioned variables.
- conditioning_shape()#
Returns the shape of the conditioning variables.
- conditioning_states()#
Returns the states of the conditioning variables.
- classmethod from_json_file(path)#
Read instance from a JSON file.
- classmethod from_json_string(json)#
Read instance from a JSON string.
- labels()#
Returns the label of the conditioned variable.
- parameters()#
Returns the parameters.
- Returns:
A reference to the parameters.
- Return type:
- 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.
- shape()#
Returns the shape of the conditioned variable.
- states()#
Returns the states of the conditioned variable.
- class causal_hub.models.DiGraph#
Bases:
objectA struct representing a directed graph using an adjacency matrix.
- add_edge(x, y)#
Adds an edge between vertices x and y.
- ancestors(x)#
Returns the ancestors of a vertex x.
- children(x)#
Returns the children of a vertex x.
- classmethod complete(vertices)#
Creates a complete directed graph with the given vertices.
- del_edge(x, y)#
Deletes the edge between vertices x and y.
- descendants(x)#
Returns the descendants of a vertex x.
- edges()#
Returns the edges of the graph.
- classmethod empty(vertices)#
Creates an empty directed graph with the given vertices.
- 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:
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.
- 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:
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.
- classmethod from_json_file(path)#
Read instance from a JSON file.
- classmethod from_json_string(json)#
Read instance from a JSON string.
- 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:
- has_edge(x, y)#
Checks if there is an edge between vertices x and y.
- has_vertex(x)#
Checks if a vertex exists in the graph.
- is_backdoor_set(x, y, z)#
Checks if the vertex set Z is a backdoor set for X and Y.
- Parameters:
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:
- 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:
- 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:
- is_separator_set(x, y, z)#
Checks if the vertex set Z is a separator set for X and Y.
- Parameters:
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:
- parents(x)#
Returns the parents of a vertex x.
- 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:
- to_networkx()#
Converts to a NetworkX DiGraph.
- Returns:
A NetworkX DiGraph representation of the graph.
- Return type:
- vertices()#
Returns the vertices of the graph.
# Returns
A list of vertices.