def test_pdf_no_adjustment(self): causes = ['c'] effects = ['d'] admissable_set = [] variable_types={'a': 'u','b': 'u','c': 'u','d' : 'u'} effect = CausalEffect(self.discrete,causes,effects,admissable_set,variable_types) # p(d=1|do(c=0) = 0.45, p(d=1|b=0) = 0.40 p = effect.pdf(pd.DataFrame({ 'd' : [1], 'c' : [0]})) print p assert( abs( 0.40 - p ) < 0.02 )
def test_pdf_no_adjustment(self): causes = ['c'] effects = ['d'] admissable_set = [] variable_types = {'a': 'u', 'b': 'u', 'c': 'u', 'd': 'u'} effect = CausalEffect(self.discrete, causes, effects, admissable_set, variable_types) # p(d=1|do(c=0) = 0.45, p(d=1|b=0) = 0.40 p = effect.pdf(pd.DataFrame({'d': [1], 'c': [0]})) print p assert (abs(0.40 - p) < 0.02)
def test_pdf_continuous(self): causes = ["c"] effects = ["d"] admissable_set = ["a"] variable_types = {"a": "c", "b": "c", "c": "c", "d": "c"} effect = CausalEffect(self.X, causes, effects, admissable_set, variable_types) c = np.mean(effect.support["c"]) d = np.mean(effect.support["d"]) e1 = effect.pdf(pd.DataFrame({"d": [d], "c": [0.9 * c]})) e2 = effect.pdf(pd.DataFrame({"d": [d], "c": [1.1 * c]})) print(e2, e1, e2 - e1, (e2 - e1) / e2) assert abs(e2 - e1) / e2 < 0.05
def test_pdf_no_adjustment(self): causes = ["c"] effects = ["d"] admissable_set = [] variable_types = {"a": "u", "b": "u", "c": "u", "d": "u"} effect = CausalEffect( self.discrete, causes, effects, admissable_set, variable_types ) # p(d=1|do(c=0) = 0.45, p(d=1|b=0) = 0.40 p = effect.pdf(pd.DataFrame({"d": [1], "c": [0]})) print(p) assert abs(0.40 - p) < 0.02
def test_pdf_continuous(self): causes = ['c'] effects = ['d'] admissable_set = ['a'] variable_types={'a': 'c','b': 'c','c': 'c','d' : 'c'} effect = CausalEffect(self.X,causes,effects,admissable_set,variable_types) c = np.mean(effect.support['c']) d = np.mean(effect.support['d']) e1 = effect.pdf(pd.DataFrame({ 'd' : [d], 'c' : [ 0.9 * c]})) e2 = effect.pdf(pd.DataFrame({ 'd' : [d], 'c' : [ 1.1 * c]})) print e2, e1, e2 - e1, (e2 - e1) / e2 assert( abs(e2 - e1) / e2 < 0.05 )
def test_pdf_continuous(self): causes = ['c'] effects = ['d'] admissable_set = ['a'] variable_types = {'a': 'c', 'b': 'c', 'c': 'c', 'd': 'c'} effect = CausalEffect(self.X, causes, effects, admissable_set, variable_types) c = np.mean(effect.support['c']) d = np.mean(effect.support['d']) e1 = effect.pdf(pd.DataFrame({'d': [d], 'c': [0.9 * c]})) e2 = effect.pdf(pd.DataFrame({'d': [d], 'c': [1.1 * c]})) print e2, e1, e2 - e1, (e2 - e1) / e2 assert (abs(e2 - e1) / e2 < 0.05)
g.add_edges_from([('x1', 'x2'), ('x1', 'x3'), ('x2', 'x4'), ('x3', 'x4')]) adjustment = AdjustForDirectCauses() admissable_set = adjustment.admissable_set(g, ['x2'], ['x3']) print(admissable_set) # 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'} effect = CausalEffect(X, ['x2'], ['x3'], variable_types=variable_types, admissable_set=list(admissable_set)) x = pd.DataFrame({'x2': [0.], 'x3': [0.]}) print(effect.pdf(x))
# in the data frame. variable_types = {'x1': 'c', 'x2': 'c', 'x3': 'c', 'x4': 'c', 'x5': 'c'} 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() print(adjustment.admissable_set(g, ['x2'], ['x3'])) # set(['x1']) from causality.estimation.nonparametric import CausalEffect admissable_set = adjustment.admissable_set(g, ['x2'], ['x3']) effect = CausalEffect(X, ['x2'], ['x3'], variable_types=variable_types, admissable_set=list(admissable_set)) x = pd.DataFrame({'x2': [0.], 'x3': [0.]}) res = effect.pdf(x) print(res) # 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