# How to use causality - 10 common examples

## To help you get started, we’ve selected a few causality examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

akelleh / causality / tests / unit / nonparametric.py View on Github
``````def test_densities(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)
density =  lambda x: effect.density.pdf( data_predict=[x])
integral = nquad( density, [effect.support[d_var] for d_var in admissable_set])[0]
print integral
assert(abs(integral - 1.) &lt; TOL)

x_vals = [np.mean(effect.support[var]) for var in causes]
z_vals = [np.mean(effect.support[var]) for var in admissable_set]
density = lambda x: effect.conditional_density.pdf(endog_predict=[x], exog_predict=x_vals + z_vals)
integral = nquad(density, [effect.support[d_var] for d_var in effects])[0]
print x_vals, z_vals,integral
assert(abs(integral - 1.) &lt; TOL)``````
akelleh / causality / tests / unit / nonparametric.py View on Github
``````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 ) &lt; 0.02 )``````
akelleh / causality / tests / unit / nonparametric.py View on Github
``````def test_expectation_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,
density=False,
expectation=True)

x = pd.DataFrame({ 'c' : [400]})
p1 = effect.expected_value(x)
print "E(d | do(c = 400) ): ", p1

x = pd.DataFrame({ 'c' : [600]})
p2 = effect.expected_value(x)
print "E(d | do(c = 600) ): ", p2
assert( abs( p2 - p1 ) / 200 &lt; 0.5 )``````
akelleh / causality / tests / unit / nonparametric.py View on Github
``````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 &lt; 0.05 )``````
akelleh / causality / tests / unit / nonparametric.py View on Github
``````def test_get_support(self):
data_ranges = {}
for variable in self.X.columns:
data_ranges[variable] = ( self.X[variable].min(), self.X[variable].max())
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)
for variable, (supp_min, supp_max) in effect.support.items():
(xmin, xmax) = data_ranges[variable]
assert(supp_min &lt;= xmin)
assert(supp_max &gt;= xmax)``````
akelleh / causality / tests / unit / nonparametric.py View on Github
``````def test_pdf_discrete(self):
causes = ['c']
effects = ['d']
admissable_set = ['a']
variable_types={'a': 'u','b': 'u','c': 'u','d' : 'u'}
effect = CausalEffect(self.discrete,causes,effects,admissable_set,variable_types)
p = effect.pdf(pd.DataFrame({ 'd' : [1], 'c' : [0]}))
print p
# p(d=1|do(c=0) = 0.45, p(d=1|c=0) = 0.40
assert( abs( 0.45 - p ) &lt; 0.02 )``````
akelleh / causality / tests / unit / nonparametric.py View on Github
``````def test_expectation_discrete(self):
causes = ['c']
effects = ['d']
admissable_set = ['a']
variable_types={'a': 'u','b': 'u','c': 'u','d' : 'u'}
effect = CausalEffect(self.discrete,
causes,
effects,
admissable_set,
variable_types,
density=False,
expectation=True)

x = pd.DataFrame({ 'c' : [0]})
p = effect.expected_value(x)
print "p(d=1 | do(c = 0) ): ", p
assert( abs( 0.40 - p ) &lt; 0.05 )

x = pd.DataFrame({ 'c' : [1]})
p = effect.expected_value(x)
print "p(d=1 | do(c = 1) ): ", p
assert( abs( 0.40 - p ) &lt; 0.05 )``````
akelleh / causality / tests / unit / nonparametric.py View on Github
``````def test_integration_function(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)``````
akelleh / causality / tests / unit / parametric.py View on Github
``````def test_match(self):
matcher = PropensityScoreMatching()
X = pd.DataFrame({'assignment': [1, 0, 0, 0, 0, 0],
'propensity score': [3, 1, 2, 3, 5, 4]})

test, control = matcher.match(X, n_neighbors=3)
assert set(control['propensity score'].values) == set([2, 3, 4])``````
akelleh / causality / tests / unit / parametric.py View on Github
``````def test_at_estimators(self):
N = 1000  # how many data points

z1 = 0.5 * np.random.normal(size=N)  # a few confounding variables
z2 = 0.5 * np.random.normal(size=N)
z3 = 0.5 * np.random.normal(size=N)

arg = (z1 + z2 + z3 + np.random.normal(size=N))
p = np.exp(arg) / (1. + np.exp(arg))  # propensity to receive treatment, P(d|z), taking on a logistic form
d = np.random.binomial(1, p)

y = (np.random.normal(size=N) + (z1 + z2 + z3 + 1.) * d)  # effect of d is confounded by z. True ATE is 1.

X = pd.DataFrame({'d': d, 'z1': z1, 'z2': z2, 'z3': z3, 'y': y, 'p': p})

matcher = PropensityScoreMatching()
ATE = matcher.estimate_ATE(X, 'd', 'y', {'z1': 'c', 'z2': 'c', 'z3': 'c'})
assert 0.9 &lt;= ATE &lt;= 1.1``````

## causality

Tools for causal inference

MIT
Latest version published 3 years ago

42 / 100