causalexplain.estimators.lingam package#
Submodules#
Python implementation of the LiNGAM algorithms. The LiNGAM Project: https://sites.google.com/site/sshimizu06/lingam
Python implementation of the LiNGAM algorithms. The LiNGAM Project: https://sites.google.com/site/sshimizu06/lingam
- class BootstrapMixin[source]#
Bases:
object
Mixin class for all LiNGAM algorithms that implement the method of bootstrapping.
Methods
bootstrap
(X, n_sampling)Evaluate the statistical reliability of DAG based on the bootstrapping.
- bootstrap(X, n_sampling)[source]#
Evaluate the statistical reliability of DAG based on the bootstrapping.
- Parameters:
X (array-like, shape (n_samples, n_features)) – Training data, where
n_samples
is the number of samples andn_features
is the number of features.n_sampling (int) – Number of bootstrapping samples.
- Returns:
result – Returns the result of bootstrapping.
- Return type:
- class BootstrapResult(adjacency_matrices, total_effects)[source]#
Bases:
object
The result of bootstrapping.
- Attributes:
adjacency_matrices_
The adjacency matrix list by bootstrapping.
total_effects_
The total effect list by bootstrapping.
Methods
get_causal_direction_counts
([n_directions, ...])Get causal direction count as a result of bootstrapping.
get_directed_acyclic_graph_counts
([n_dags, ...])Get DAGs count as a result of bootstrapping.
get_paths
(from_index, to_index[, ...])Get all paths from the start variable to the end variable and their bootstrap probabilities.
get_probabilities
([min_causal_effect])Get bootstrap probability.
get_total_causal_effects
([min_causal_effect])Get total effects list.
- __init__(adjacency_matrices, total_effects)[source]#
Construct a BootstrapResult.
- Parameters:
adjacency_matrices (array-like, shape (n_sampling)) – The adjacency matrix list by bootstrapping.
total_effects (array-like, shape (n_sampling)) – The total effects list by bootstrapping.
- property adjacency_matrices_#
The adjacency matrix list by bootstrapping.
- Returns:
adjacency_matrices_ – The adjacency matrix list, where
n_sampling
is the number of bootstrap sampling.- Return type:
array-like, shape (n_sampling)
- property total_effects_#
The total effect list by bootstrapping.
- Returns:
total_effects_ – The total effect list, where
n_sampling
is the number of bootstrap sampling.- Return type:
array-like, shape (n_sampling)
- get_causal_direction_counts(n_directions=None, min_causal_effect=None, split_by_causal_effect_sign=False)[source]#
Get causal direction count as a result of bootstrapping.
- Parameters:
n_directions (int, optional (default=None)) – If int, then The top
n_directions
items are included in the resultmin_causal_effect (float, optional (default=None)) – Threshold for detecting causal direction. If float, then causal directions with absolute values of causal effects less than
min_causal_effect
are excluded.split_by_causal_effect_sign (boolean, optional (default=False)) – If True, then causal directions are split depending on the sign of the causal effect.
- Returns:
causal_direction_counts – List of causal directions sorted by count in descending order. The dictionary has the following format:
{'from': [n_directions], 'to': [n_directions], 'count': [n_directions]}
where
n_directions
is the number of causal directions.- Return type:
- get_directed_acyclic_graph_counts(n_dags=None, min_causal_effect=None, split_by_causal_effect_sign=False)[source]#
Get DAGs count as a result of bootstrapping.
- Parameters:
n_dags (int, optional (default=None)) – If int, then The top
n_dags
items are included in the resultmin_causal_effect (float, optional (default=None)) – Threshold for detecting causal direction. If float, then causal directions with absolute values of causal effects less than
min_causal_effect
are excluded.split_by_causal_effect_sign (boolean, optional (default=False)) – If True, then causal directions are split depending on the sign of the causal effect.
- Returns:
directed_acyclic_graph_counts – List of directed acyclic graphs sorted by count in descending order. The dictionary has the following format:
{'dag': [n_dags], 'count': [n_dags]}.
where
n_dags
is the number of directed acyclic graphs.- Return type:
- get_probabilities(min_causal_effect=None)[source]#
Get bootstrap probability.
- Parameters:
min_causal_effect (float, optional (default=None)) – Threshold for detecting causal direction. If float, then causal directions with absolute values of causal effects less than
min_causal_effect
are excluded.- Returns:
probabilities – List of bootstrap probability matrix.
- Return type:
array-like
- get_total_causal_effects(min_causal_effect=None)[source]#
Get total effects list.
- Parameters:
min_causal_effect (float, optional (default=None)) – Threshold for detecting causal direction. If float, then causal directions with absolute values of causal effects less than
min_causal_effect
are excluded.- Returns:
total_causal_effects – List of bootstrap total causal effect sorted by probability in descending order. The dictionary has the following format:
{'from': [n_directions], 'to': [n_directions], 'effect': [n_directions], 'probability': [n_directions]}
where
n_directions
is the number of causal directions.- Return type:
- get_paths(from_index, to_index, min_causal_effect=None)[source]#
Get all paths from the start variable to the end variable and their bootstrap probabilities.
- Parameters:
from_index (int) – Index of the variable at the start of the path.
to_index (int) – Index of the variable at the end of the path.
min_causal_effect (float, optional (default=None)) – Threshold for detecting causal direction. Causal directions with absolute values of causal effects less than
min_causal_effect
are excluded.
- Returns:
paths – List of path and bootstrap probability. The dictionary has the following format:
{'path': [n_paths], 'effect': [n_paths], 'probability': [n_paths]}
where
n_paths
is the number of paths.- Return type:
Python implementation of the LiNGAM algorithms. The LiNGAM Project: https://sites.google.com/site/sshimizu06/lingam
- class DirectLiNGAM(name, random_state=None, prior_knowledge=None, apply_prior_knowledge_softly=False, measure='pwling', th=0.0, inverse=False, absolute_values=False, verbose=False)[source]#
Bases:
_BaseLiNGAM
Implementation of DirectLiNGAM Algorithm [1] [2]
References
[1]- Shimizu, T. Inazumi, Y. Sogawa, A. Hyvärinen, Y. Kawahara, T. Washio,
Hoyer and K. Bollen.
- DirectLiNGAM: A direct method for learning a linear non-Gaussian structural
equation model.
Journal of Machine Learning Research, 12(Apr): 1225–1248, 2011.
[2]- Hyvärinen and S. M. Smith. Pairwise likelihood ratios for estimation
of non-Gaussian structural eauation models.
Journal of Machine Learning Research 14:111-152, 2013.
- Attributes:
adjacency_matrix_
Estimated adjacency matrix.
causal_order_
Estimated causal ordering.
- dag
- feature_names
- metrics
Methods
bootstrap
(X, n_sampling)Evaluate the statistical reliability of DAG based on the bootstrapping.
estimate_total_effect
(X, from_index, to_index)Estimate total effect using causal model.
fit
(X)Fit the model to X.
fit_predict
(train, test[, ref_graph])Fit the model to X and return the estimated DAG.
get_error_independence_p_values
(X)Calculate the p-value matrix of independence between error variables.
- is_fitted_ = False#
- metrics = None#
- dag = None#
- feature_names = None#
- __init__(name, random_state=None, prior_knowledge=None, apply_prior_knowledge_softly=False, measure='pwling', th=0.0, inverse=False, absolute_values=False, verbose=False)[source]#
Construct a DirectLiNGAM model.
- Parameters:
random_state (int, optional (default=None)) –
random_state
is the seed used by the random number generator.prior_knowledge (array-like, shape (n_features, n_features),) –
optional (default=None) Prior knowledge used for causal discovery, where
n_features
is the number of features.The elements of prior knowledge matrix are defined as follows [1]_:
0
: \(x_i\) does not have a directed path to \(x_j\)1
: \(x_i\) has a directed path to \(x_j\)-1
No prior knowledge is available to know if either of the twocases above (0 or 1) is true.
apply_prior_knowledge_softly (boolean, optional (default=False)) – If True, apply prior knowledge softly.
measure ({'pwling', 'kernel'}, optional (default='pwling')) – Measure to evaluate independence: ‘pwling’ [2]_ or ‘kernel’ [1]_.
- fit(X)[source]#
Fit the model to X.
- Parameters:
X (array-like, shape (n_samples, n_features)) – Training data, where
n_samples
is the number of samples andn_features
is the number of features.- Returns:
self – Returns the instance itself.
- Return type:
- fit_predict(train, test, ref_graph=None)[source]#
Fit the model to X and return the estimated DAG.
- Parameters:
X (array-like, shape (n_samples, n_features)) – Training data, where
n_samples
is the number of samples andn_features
is the number of features.- Returns:
self – Returns the instance itself.
- Return type:
- main(dataset_name, threshold=0.0, inverse=True, absolute_values=True, input_path='/Users/renero/phd/data/sachs/', output_path='/Users/renero/phd/output/RC4/sachs/compared/', save=False)[source]#
Python implementation of the LiNGAM algorithms. The LiNGAM Project: https://sites.google.com/site/sshimizu06/lingam
- find_all_paths(dag, from_index, to_index, min_causal_effect=0.0)[source]#
Find all paths from point to point in DAG.
- Parameters:
dag (array-like, shape (n_features, n_features)) – The adjacency matrix to fine all paths, where n_features is the number of features.
from_index (int) – Index of the variable at the start of the path.
to_index (int) – Index of the variable at the end of the path.
min_causal_effect (float, optional (default=0.0)) – Threshold for detecting causal direction. Causal directions with absolute values of causal effects less than
min_causal_effect
are excluded.
- Returns:
paths (array-like, shape (n_paths)) – List of found path, where n_paths is the number of paths.
effects (array-like, shape (n_paths)) – List of causal effect, where n_paths is the number of paths.
- predict_adaptive_lasso(X, predictors, target, gamma=1.0)[source]#
Predict with Adaptive Lasso.
- Parameters:
X (array-like, shape (n_samples, n_features)) – Training data, where n_samples is the number of samples and n_features is the number of features.
predictors (array-like, shape (n_predictors)) – Indices of predictor variable.
target (int) – Index of target variable.
- Returns:
coef – Coefficients of predictor variable.
- Return type:
array-like, shape (n_features)