Causality數(shù)據(jù)集因果分析工具
Causality 是一款數(shù)據(jù)集因果分析工具。
安裝
如果有 pip,只需運行:
pip install causality
因果推論
因果關(guān)系模塊將包含用于推斷因果DAG的各種算法。
要在數(shù)據(jù)集上運行圖形搜索,可以使用類似的算法(以IC *為例):
import numpy
import pandas as pd
from causality.inference.search import IC
from causality.inference.independence_tests import RobustRegressionTest
# generate some toy data:
SIZE = 2000
x1 = numpy.random.normal(size=SIZE)
x2 = x1 + numpy.random.normal(size=SIZE)
x3 = x1 + numpy.random.normal(size=SIZE)
x4 = x2 + x3 + numpy.random.normal(size=SIZE)
x5 = x4 + numpy.random.normal(size=SIZE)
# load the data into a dataframe:
X = pd.DataFrame({'x1' : x1, 'x2' : x2, 'x3' : x3, 'x4' : x4, 'x5' : x5})
# define the variable types: 'c' is 'continuous'. The variables defined here
# are the ones the search is performed over -- NOT all the variables defined
# in the data frame.
variable_types = {'x1' : 'c', 'x2' : 'c', 'x3' : 'c', 'x4' : 'c', 'x5' : 'c'}
# run the search
ic_algorithm = IC(RobustRegressionTest, X, variable_types)
graph = ic_algorithm.search()
現(xiàn)在,我們推斷圖存儲在圖中。 在此圖中,每個變量是一個節(jié)點(從DataFrame列命名),每個edge表示不能通過對搜索指定的變量進行調(diào)整來消除的節(jié)點之間的統(tǒng)計相關(guān)性。 如果edge可以用可用數(shù)據(jù)定向,則arrowhead在'arrows'中指示。 如果edge也滿足用于真實因果的局部標(biāo)準(zhǔn),則marked=True。 如果我們從搜索的結(jié)果打印edges,我們可以看到哪些edges是定向的,并且滿足真正因果的局部標(biāo)準(zhǔn):
>>> graph.edges(data=True)
[('x2', 'x1', {'arrows': [], 'marked': False}),
('x2', 'x4', {'arrows': ['x4'], 'marked': False}),
('x3', 'x1', {'arrows': [], 'marked': False}),
('x3', 'x4', {'arrows': ['x4'], 'marked': False}),
('x4', 'x5', {'arrows': ['x5'], 'marked': True})]
我們可以看到從'x2'到'x4','x3'到'x4'和'x4'到'x5'的edges都朝向每對的第二個。 此外,我們看到從'x4'到'x5'的edge滿足真正因果關(guān)系的局部標(biāo)準(zhǔn)。 這與Pearl(2000)中figure 2.3(d)中給出的結(jié)構(gòu)相符。
評論
圖片
表情
