<jupyter_start><jupyter_text>Demo for the DoWhy causal APIWe show a simple example of adding a causal extension to any dataframe.<jupyter_code>import os, sys
sys.path.append(os.path.abspath("../../"))
import dowhy.datasets
import dowhy.api

import numpy as np
import pandas as pd

from statsmodels.api import OLS
data = dowhy.datasets.linear_dataset(beta=5,
        num_common_causes=1,
        num_instruments = 0,
        num_samples=1000,
        treatment_is_binary=True)
df = data['df']
df['y'] = df['y'] + np.random.normal(size=len(df)) # Adding noise to data. Without noise, the variance in Y|X, Z is zero, and mcmc fails.
data['dot_graph'] = 'digraph { v ->y;X0-> v;X0-> y;}'
# data['df'] is just a regular pandas.DataFrame
df.causal.do(x='v',
                     variable_types={'v': 'b', 'y': 'c', 'X0': 'c'},
                     outcome='y',
                     common_causes=['X0']).groupby('v').mean().plot(y='y', kind='bar')
df.causal.do(x={'v': 1}, 
              variable_types={'v': 'b', 'y': 'c', 'X0': 'c'}, 
              outcome='y',
              method='weighting', 
              common_causes=['X0'],
              proceed_when_unidentifiable=True).groupby('v').mean().plot(y='y', kind='bar')
cdf_1 = df.causal.do(x={'v': 1}, 
              variable_types={'v': 'b', 'y': 'c', 'X0': 'c'}, 
              outcome='y', 
              dot_graph=data['dot_graph'],
              proceed_when_unidentifiable=True)

cdf_0 = df.causal.do(x={'v': 0}, 
              variable_types={'v': 'b', 'y': 'c', 'X0': 'c'}, 
              outcome='y', 
              dot_graph=data['dot_graph'],
              proceed_when_unidentifiable=True)
cdf_0
cdf_1<jupyter_output><empty_output><jupyter_text>Comparing the estimate to Linear RegressionFirst, estimating the effect using the causal data frame, and the 95% confidence interval.<jupyter_code>(cdf_1['y'] - cdf_0['y']).mean()
1.96*(cdf_1['y'] - cdf_0['y']).std() / np.sqrt(len(df))<jupyter_output><empty_output><jupyter_text>Comparing to the estimate from OLS.<jupyter_code>model = OLS(df['y'], df[['X0', 'v']])
result = model.fit()
result.summary()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1811391223155929, 'fraction_numerical': 0.014939309056956116, 'mean_word_length': 3.1773879142300196, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '8045', 'license': 'MIT', 'n_tokens_mistral': 786, 'n_tokens_neox': 764, 'n_words': 159, 'path': 'docs/source/dowhy_causal_api.ipynb', 'repo_name': 'ktmud/dowhy', 'size': '2142'}