In [1]:
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)
graph = ic_algorithm.search(X, variable_types)
In [12]:
X.head()
Out[12]:
In [2]:
graph.edges(data=True)
Out[2]:
In [3]:
from causality.estimation.adjustments import AdjustForDirectCauses
from networkx import DiGraph
g = DiGraph()
g.add_nodes_from(['x1','x2','x3','x4', 'x5'])
g.add_edges_from([('x1','x2'),('x1','x3'),('x2','x4'),('x3','x4')])
adjustment = AdjustForDirectCauses()
In [5]:
print (adjustment.admissable_set(g, ['x2'], ['x3']))
In [6]:
from causality.estimation.nonparametric import CausalEffect
In [7]:
admissable_set = adjustment.admissable_set(g,['x2'], ['x3'])
In [8]:
effect = CausalEffect(X, ['x2'], ['x3'], variable_types=variable_types, admissable_set=list(admissable_set))
In [9]:
x = pd.DataFrame({'x2' : [0.], 'x3' : [0.]})
In [10]:
effect.pdf(x)
Out[10]:
In [ ]: