<jupyter_start><jupyter_text>Testing Assumptions in model with DoWhy: A simple exampleThis is a quick introduction to how we can test if our assumed graph is correct and the assumptions match with the dataset.We do so by checking the conditional independences in the graph and see if they hold true for the data as well. Currently we are using partial correlation to do that. First, let us load all required packages.<jupyter_code>%load_ext autoreload
%autoreload 2
import numpy as np
import pandas as pd
import os, sys
sys.path.append(os.path.abspath("../../../"))
import dowhy
from dowhy import CausalModel
import dowhy.datasets<jupyter_output><empty_output><jupyter_text>Step 1: Load dataset<jupyter_code>data = dowhy.datasets.linear_dataset(beta=10,
        num_common_causes=5,
        num_instruments = 2,
        num_effect_modifiers=1,
        num_samples=5000, 
        treatment_is_binary=True,
        stddev_treatment_noise=10,
        num_discrete_common_causes=1)

df = data["df"] #Insert dataset here
df.head()<jupyter_output><empty_output><jupyter_text>Note that we are using a pandas dataframe to load the data. At present, DoWhy only supports pandas dataframe as input.  Step 2: Input causal graph We now input a causal graph. You can do that in the GML graph format (recommended), DOT format or the output from daggity -To create the causal graph for your dataset, you can use a tool like [DAGitty](http://dagitty.net/dags.html) that provides a GUI to construct the graph. You can export the graph string that it generates.<jupyter_code>graph_string = """dag {
        W0 [pos="-2.200,-1.520"]
        W1 [pos="-1.457,-1.533"]
        W2 [pos="-0.763,-1.547"]
        W3 [pos="1.041,-1.587"]
        W4 [pos="1.510,-1.560"]
        X0 [pos="1.222,-0.625"]
        Z0 [pos="0.390,-1.601"]
        Z1 [pos="-0.176,-1.540"]
        v0 [pos="-0.219,-0.881"]
        y [pos="-0.144,-0.296"]
        W0 -> v0
        W0 -> y
        W1 -> v0
        W1 -> y
        W2 -> v0
        W2 -> y
        W3 -> v0
        W3 -> y
        W4 -> v0
        W4 -> y
        X0 -> y
        Z0 -> v0
        Z1 -> v0
        v0 -> y
        }"""<jupyter_output><empty_output><jupyter_text>Step 3: Create Causal Model<jupyter_code>model=CausalModel(
        data = df,
        treatment=data["treatment_name"],
        outcome=data["outcome_name"],
        graph=graph_string
        )
model.view_model()
from IPython.display import Image, display
display(Image(filename="causal_model.png"))<jupyter_output><empty_output><jupyter_text>Step 4: Testing for Conditional IndependenceWe can check if the assumptions of the graph hold true for the data using model.refute_graph(k, method_name,independence_constraints) We are testing X ⫫ Y | Z where X and Y are singular sets and Z can have k number of variables. Currently we are using "partial_correlation" method by default and k value is 1 by default unless input.<jupyter_code>refuter_object = model.refute_graph(k=2, method_name = "partial_correlation")  #Change k parameter to test conditional independence given different number of variables 
print(refuter_object)<jupyter_output><empty_output><jupyter_text>Testing for a set of edgesWe can also test a set of conditional independences whether they are true or not The input has to be in the form - [( x1, y1, (z1, z2)),  ( x2, y2, (z3, z4)), ( x3, y3, (z5,)), ( x4, y4, ()) ]<jupyter_code>print(model.refute_graph(method_name = "partial_correlation",independence_constraints = [('W3', 'Z0', ()), ('W0', 'Z0', ()),('W3', 'Z0', ('W1','W2',))]))<jupyter_output><empty_output><jupyter_text>Testing with a wrong graph input<jupyter_code>graph_string = """dag {
        W0 [pos="-2.200,-1.520"]
        W1 [pos="-1.457,-1.533"]
        W2 [pos="-0.763,-1.547"]
        W3 [pos="1.041,-1.587"]
        W4 [pos="1.510,-1.560"]
        X0 [pos="1.222,-0.625"]
        Z0 [pos="0.390,-1.601"]
        Z1 [pos="-0.176,-1.540"]
        v0 [pos="-0.219,-0.881"]
        y [pos="-0.144,-0.296"]
        W0 -> v0
        W0 -> y
        W1 -> v0
        W1 -> y
        W2 -> v0
        W2 -> y
        W3 -> v0
        W3 -> y
        W4 -> v0
        X0 -> Z0
        Z0 -> Z1
        }"""
model = CausalModel(
            data=df,
            treatment=data["treatment_name"],
            outcome=data["outcome_name"],
            graph=graph_string,
            proceed_when_unidentifiable=True,
            test_significance=None
        )
model.view_model()
from IPython.display import Image, display
display(Image(filename="causal_model.png"))
refuter_object = model.refute_graph(k=1,method_name = "partial_correlation")<jupyter_output><empty_output><jupyter_text>We can see that since we input the wrong graph, many conditional independences were not met<jupyter_code>print(refuter_object)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12310566742785967, 'fraction_numerical': 0.05356030724517334, 'mean_word_length': 2.9752475247524752, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 58, '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': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '131888', 'license': 'MIT', 'n_tokens_mistral': 1724, 'n_tokens_neox': 1596, 'n_words': 461, 'path': 'docs/source/example_notebooks/graph_conditional_independence_refuter.ipynb', 'repo_name': 'guyk1971/dowhy', 'size': '4817'}