CausalDiscoveryToolbox:因果建模、因果图代码实现
⽂章⽬录
最近在分析观测数据的因果关系时,发现⼀个很好⽤的⼯具包——CausalDiscoveryToolbox(以下简称Cdt),功能齐全,轻松上⼿因果发现。
下⾯简单整理下该⼯具包的原理+⽤法。
CausalDiscoveryToolbox简介
⽤于在从数据的联合概率分布样本中学习因果图和相关的因果机制。
实现了端到端的因果发现⽅法,⽀持从观测数据中恢复直接依赖关系(因果图的⾻架)和变量之间的因果关系。
实现了许多⽤于图结构恢复的算法(包括来⾃bnlearn,pcalg包的算法)。
基于Numpy,Scikit-learn,Pytorch和R开发。
⽀持GPU硬件加速。
因果建模的过程
Cdt⼯具包对⼀般的因果建模流程进⾏了概括:
Cdt⼯具包可以直接从观测数据中进⾏因果发现(获得因果有向图),也可以先恢复图结构(获得⽆向依赖图)之后,再进⾏因果发现(获得因果有向图)。
图恢复算法 Graph recovery algorithms
在Cdt⼯具包中实现了两种从原始数据中恢复⽆向依赖图的⽅法:
⼆元依赖 (Bivariate dependencies)
⽤于确定因果图中的(⽆向)边。
依赖于统计检验,例如⽪尔逊相关性(Pearson’s correlation)或互信息分数(mutual information scores)。在第⼀阶段中可以使⽤⼆元依赖关系来建⽴因果图⾻架。
多元⽅法 (Multivariate methods)
⽬的是恢复全因果图。即为图的所有变量选择⽗、⼦和配偶节点(⼦的⽗母)。
输出⼀个networkx.Graph图对象。
因果发现算法 Causal Discovery algorithms
Cdt包的主要焦点是从观测数据中发现因果关系,从成对设置到全图建模。
成对建模(The pairwise setting )
把每⼀对变量作为因果对问题(CEP,cause-effect pair problem)进⾏处理,从⽽确定这些变量之间的因果关系。 ⽐如识别X→Y 还是Y→X,或者X与Y没有关系。
成对建模的两个经典⽅法:加性噪声模型(ANM)和IGCI (Information-Geometric Causal Inference),在有篇因果对的综述⾥有详细介绍,后⾯有空梳理下。成对建模的前提假设是:这两个变量已经受到其他协变量的制约,并且剩余的潜在协变量⼏乎没有影响,可以被认为是“噪声”。全图建模(The graph setting )基于贝叶斯或基于分数的⽅法,输出有向⽆环图或部分有向⽆环图。①依赖于条件独⽴性测试,称为基于约束的⽅法,如PC或FCI。②依赖于基于分数的⽅法,通过图搜索启发式⽅法(如GES或CAM)到使似然分数最⼤化的图。
③利⽤著名的对抗⽣成⽹络,例如CGNN或SAM。
安装Cdt ⼯具包
直接使⽤pip安装:
使⽤⽰例
⼯具包模块
该软件包分为5个模块:pip install cdt
1import cdt from cdt import SETTINGS SETTINGS .verbose =True #SETTINGS.NJOBS=16#SETTINGS.GPU=1import networkx as nx import matplotlib .pyplot as plt plt .axis ('off')# Load data data = pd .read_csv ("lucas0_train.csv")print (data .head ())# Finding the structure of the graph glasso = cdt .independence .graph .Glasso ()skeleton = glasso .predict (data )# Pairwise setting model = cdt .causality .pairwise .ANM ()output_graph = model .predict (data , skeleton )# Visualize causality graph options = { "node_color": "#A0CBE2", "width": 1, "node_size":400, "edge_cmap": plt .cm .Blues , "with_labels": True , }nx .draw_networkx (output_graph ,**options )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
因果关系:cdt.causality在成对设置或图形设置中实施因果发现算法。
独⽴性:cdt.independence包括恢复数据依赖关系图的⽅法。
tool工具箱数据:cdt.data为⽤户提供⽣成数据和加载基准数据的⼯具。
实⽤程序:cdt.utils为⽤户提供⽤于模型构建,图形实⽤程序和设置的⼯具。
指标:ics包括图表的评分指标,以输⼊为准 networkx.DiGraph
⽤于计算的所有⽅法,接⼝与Scikit-learn类似,在这⾥.predict() 发动对给定的数据到⼯具箱中的算法,.fit()使得训练学习算法⼤部分的算法是类,它们的参数可以在⾃定义.init()中设置。
程序包及其算法的结构:
使⽤感想接⼝简单,⽂档清晰,易于上⼿。不过⽬前还不⽀持使⽤⼲预措施。
Cdt⼯具包是在观察环境进⾏因果发现的软件包,所以相当于还是在因果科学的第⼀层级“关联”。不过从当前的实际应⽤⾓度来说,“⼲预”和“反事实”的实施难度较⼤,“关联”层级的因果发现和推理已经能起到⼀定的作⽤。 cdt package | |- independence | |- graph (Infering the skeleton from data) | | |- Lasso variants (Randomized Lasso[1], Glasso[2], HSICLasso[3]) | | |- FSGNN (CGNN[12] variant for feature selection) | | |- Skeleton recovery using feature selection algorithms (RFECV[5], LinearSVR[6], RRelief[7], ARD[8,9], DecisionTree) | | | |- stats (pairwise methods for dependency) | |- Correlation (Pearson, Spearman, KendallTau) | |- Kernel based (NormalizedHSIC[10]) | |- Mutual information based (MIRegression, Adjusted Mutual Information[11], Normalized mutual inform
ation[11]) | |- data | |- CausalPairGenerator (Generate causal pairs) | |- AcyclicGraphGenerator (Generate FCM-based graphs) | |- load_dataset (load standard benchmark datasets) | |- causality | |- graph (methods for graph inference) | | |- CGNN[12] | | |- PC[13] | | |- GES[13] | | |- GIES[13] | | |- LiNGAM[13] | | |- CAM[13] | | |- GS[23] | | |- IAMB[24] | | |- MMPC[25] | | |- SAM[26] | | |- CCDr[27] | | | |- pairwise (methods for pairwise inference) | |- ANM[14] (Additive Noise Model) | |- IGCI[15] (Information Geometric Causal Inference) | |- RCC[16] (Randomized Causation Coefficient) | |- NCC[17] (Neural Causation Coefficient) | |- GNN[12] (Generative Neural Network -- Part of CGNN ) | |- Bivariate fit (Baseline method of regression) | |- Jarfo[20] | |- CDS[20] | |- RECI[28] | |- metrics (Implements the metrics for graph scoring) | |- Precision Recall | |- SHD | |- SID [29] | |- utils |- Settings -> SETTINGS class (hardware settings) |- loss -> MMD loss [21, 22] & various other loss functions |- io -> for importing data formats |- graph -> graph utilities
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
1. Marco Scutari. Package ‘bnlearn’, 2018.
2. Markus Kalisch, Alain Hauser, et al. Package ‘pcalg’. 2018.
3. Nguyen Xuan Vinh, Julien Epps, and James Bailey. Information theoretic measures for clusterings comparison: Variants,
properties, normalization and correction for chance. Journal of Machine Learning Research, 11(Oct):2837–2854, 2010.
4.
5. Patrik O Hoyer, Dominik Janzing, Joris M Mooij, Jonas Peters, and Bernhard Sch¨olkopf. Nonlinear causal discovery with
additive noise models. In Neural Information Processing Systems (NIPS), pages 689–696, 2009.
6. Janzing, D., Mooij, J., Zhang, K., Lemeire, et al… (2012). Information-geometric approach to inferring causal directions.
Artificial Intelligence, 182, 1-31.
7. Peter Spirtes, Clark N Glymour, and Richard Scheines. Causation, prediction, and search.MIT press, 2000
8. Eric V Strobl, Kun Zhang, and Shyam Visweswaran. Approximate kernel-based conditional independence tests for fast
non-parametric causal discovery. 2017
9. David Maxwell Chickering. Optimal structure identification with greedy search. Journal
of machine learning research, 3(Nov):507–554, 2002.
0. Peter B¨uhlmann, Jonas Peters, Jan Ernest, et al. Cam: Causal additive models, highdimensional order search and
penalized regression. The Annals of Statistics, 2014.
1. Olivier Goudet, Diviyan Kalainathan, et al. Learning functional causal models with generative neural networks. 2017.
2. Diviyan Kalainathan, Olivier Goudet, et al. Sam: Structural agnostic model, causal discovery and penalized adversarial
learning. 2018.
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论