文章目录
一、前言
自己在使用微软Dowhy和causal-learn后写下此文,希望各位避坑!!!
Causal-learn,由CMU张坤老师主导,多个团队(CMU因果研究团队、广工DMIR实验室、宫明明老师团队和Shohei Shimizu老师团队)联合开发出品的因果发现算法平台。
Causal-learn用Python实现了CMU开发的基于Java的Tetrad因果发现平台(WAIC2020 SAIL 之 L奖),并进一步加入新的算法和功能。其中包含了因果发现的经典算法与API,并且提供了模块化的代码,以方便研究者实现自己的算法。Causal-learn所有模块均基于Python实现,从而避免了传统因果发现库对R/Java的依赖,为Python开发者提供便利。
(总结来说,causal-learn比微软的DoWhy友好太多了!!!)
二、内容
2.1 支持算法
-
Constraint-based causal discovery methods
-
PC
-
FCI
-
CD-NOD
-
-
Score-based causal discovery methods
-
GES with the BIC score or generalized score
-
Exact Search
-
-
Causal discovery methods based on constrained functional causal models
-
LiNGAM-based Methods
-
Post-nonlinear causal models
-
Additive noise models
-
-
Hidden causal representation learning
- Generalized Independence Noise (GIN) condition-based method
-
Granger causality
- Linear granger causality
2.2 独立性测试
-
Fisher-z test
-
Missing-value Fisher-z test
-
Chi-Square test
-
Kernel-based conditional independence (KCI) test and independence test
-
G-Square test
2.3 评分函数
-
BIC score
-
BDeu score
-
Generalized score with cross validation
-
Generalized score with marginal likelihood
三、安装causal-learn
3.1可以通过pip来安装
pip install causal-learn
3.2Requirements
-
python 3
-
numpy
-
networkx
-
pandas
-
scipy
-
scikit-learn
-
statsmodels
-
pydot
(For visualization)
-
matplotlib
-
graphviz
-
pygraphviz (might not support the most recent Mac)
四、用法举例
4.1以pc算法为例
cg = pc(data, alpha, indep_test, stable, uc_rule, uc_priority, mvpc, correction_name, background_knowledge, verbose, show_progress)
4.2参数解释
* data:必须要求numpy.ndarray,格式为(n_samples, n_features),其中n_samples 是样本数,n_features 是特征数
* alpha:独立性测试的显著性水平,(float) in (0, 1),Default: 0.05.
* indep_test:独立性测试的方法 Default: ‘fisherz’
* stable:是否执行稳定的图骨架发现,Default: True.
* uc_rule:how unshielded colliders are oriented. Default: 0.
* uc_priority:rule of resolving conflicts between unshielded colliders. Default: 2.
* mvpc:数据是否包含缺失值,若包含,请设置为True. Default: False.
* correction_name:Missing value correction if using missing-value PC
* background_knowledge:背景知识(指定因果边及方向)
* verbose:如果要打印verbose日志输出,请设置为True,Default: False.
* show_progress:如果要在控制台显示算法的进展,则为True。默认情况下. Default: True.
4.3Returns
a CausalGraph object
- cg.G.graph[j,i]=1 and cg.G.graph[i,j]=-1 indicate i –> j;
- cg.G.graph[i,j] = cg.G.graph[j,i] = -1 indicate i — j;
- cg.G.graph[i,j] = cg.G.graph[j,i] = 1 indicates i <-> j.
4.3完整样例
import numpy as np
from causallearn.search.ConstraintBased.PC import pc
from causallearn.utils.cit import fisherz,chisq,gsq,mv_fisherz,kci# 导入不同的独立性测试方法
data_path="TestData/data_discrete_1.txt"
example_data = np.loadtxt(data_path,skiprows=1)# skiprows,跳过读取数据的第一行(标题行)
example_result = pc(example_data,0.05,kci,True,0,-1)
example_result.to_nx_graph()
example_result.draw_nx_graph(skel=False)
4.4运行结果
Green: undirected; Blue: directed; Red: bi-directed
(注意,这个结果图中,没有红色箭头)
五、其它补充
5.1 相关链接
5.2参考
Causal learn使用方法
因果发现工具|causal-learn介绍
author:yewii