{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import dowhy.datasets\n", "from dowhy.do_samplers.kernel_density_sampler import KernelDensitySampler\n", "from dowhy.do_why import CausalModel\n", "from dowhy.api.causal_data_frame import CausalDataFrame\n", "\n", "import numpy as np\n", "import pandas as pd\n", "\n", "from statsmodels.api import OLS" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "\n", "data = dowhy.datasets.linear_dataset(beta=5,\n", " num_common_causes=1,\n", " num_instruments = 0,\n", " num_samples=1000,\n", " treatment_is_binary=True)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "scrolled": true }, "outputs": [], "source": [ "data['dot_graph'] = 'digraph { v ->y;X0-> v;X0-> y;}'\n", "df = data['df']\n", "df['y'] = df['y'] + np.random.normal(size=len(df)) # without noise, the variance in Y|X, Z is zero, and mcmc fails.\n" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "WARNING:dowhy.do_why:Causal Graph not provided. DoWhy will construct a graph based on data inputs.\n", "INFO:dowhy.causal_identifier:Common causes of treatment and outcome:{'U', 'X0'}\n", "INFO:dowhy.causal_identifier:Instrumental variables for treatment and outcome:[]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "['X0']\n", "yes\n", "{'observed': 'yes'}\n", "Model to find the causal effect of treatment v on outcome y\n", "{'label': 'Unobserved Confounders', 'observed': 'no'}\n", "All common causes are observed. Causal effect can be identified.\n", "McmcSampler\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "INFO:dowhy.do_sampler:Using McmcSampler for do sampling.\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "treatments ['v']\n", "backdoor ['X0']\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "INFO:pymc3:Auto-assigning NUTS sampler...\n", "INFO:pymc3:Initializing NUTS using jitter+adapt_diag...\n", "INFO:pymc3:Multiprocess sampling (4 chains in 4 jobs)\n", "INFO:pymc3:NUTS: [y_sd, beta_y, v_sd, beta_v]\n", "Sampling 4 chains: 100%|██████████| 8000/8000 [00:05<00:00, 1550.20draws/s]\n" ] }, { "data": { "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" }, { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXYAAAEQCAYAAACk818iAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvOIA7rQAADXBJREFUeJzt3X2MpeVdxvHrctnNKt2UdDjSlmEcIoihJgUykpqaGsAKSNOGpm0gwbctmSa2piQaxZCg/sdfBjS+jW2lxMpWUGKldLVqSdNYlp1l14ZdWltXKrMpMKxt+pJsednLP/asXYZZduc8d+Y55zffTzJhz8vezy9k+fLsPc+Zx0kEAKjjh/oeAADQFmEHgGIIOwAUQ9gBoBjCDgDFEHYAKIawA0AxhB0AiiHsAFDMGX0c9Oyzz87s7GwfhwaAibVnz57nkgxO9b5ewj47O6vFxcU+Dg0AE8v210/nfWzFAEAxncNu+yLb+074+rbtW1oMBwBYu85bMUm+IukSSbK9SdIhSQ90XRcAMJrWe+xXSfqvJKe1D3SiF154QUtLSzpy5EjjkdrZunWrpqentXnz5r5HAYCTah32GyTdu9oLtuclzUvSzMzMK15fWlrStm3bNDs7K9uNx+ouiQ4fPqylpSWdf/75fY8DACfV7JuntrdIeqek+1Z7PclCkrkkc4PBK6/WOXLkiKampsYy6pJkW1NTU2P9NwoAkNpeFXOtpMeSPDPqAuMa9ePGfT4AkNqG/UadZBsGALB+muyx2z5T0tslfaDFepI0e+unWy0lSXryjuuargf0qfV/HxtdtT40CXuS70maarEWAKAbPnk6dPvtt+vOO+/8/8e33Xab7rrrrh4nAoDREPah7du365577pEkHT16VDt27NBNN93U81QAsHa9/BCwcTQ7O6upqSn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h9J+nFJ9+jlJx6/LOm/k3yor9k2GsIOoBnb1+rlJx6HJH0qyUP9TbXxEPYibL8jyYN9zwGgf1zuWMdP9z0AcDLDex5jnfDN0wlj+ye1+l91f6+/qYBTct8DbCScsU8Q278jaYeO/Ufy6PDLku61fWufswGn8HzfA2wk7LFPENv/KelNSV5Y8fwWSfu5jh3jyvb/JJnpe46Ngq2YyXJU0hv1yhuBv2H4GtAb21862UuS+NEC64iwT5ZbJP2r7a/qB9cJz0i6QBLXCKNv50i6WtI3VzxvSf++/uNsXIR9giTZafsnJF2ul3/zdPfxnx0D9OhBSa9Jsm/lC7YfXv9xNi722AGgGK6KAYBiCDsAFEPYAaAYwg4AxRB2QJLtO2x/8ITHv2/7t/qcCRgVYQeO+aSk953w+H3D54CJw3XsgKQke23/qO03ShpI+maSp071+4BxRNiBH7hP0nskvV6crWOC8QElYMj2myT9paSzJf1ckm/0PBIwEvbYgaEk+yVtk3SIqGOSccYOAMVwxg4AxRB2ACiGsANAMYQdAIoh7ABQDGEHgGIIOwAUQ9gBoJj/A8Sb/MKZv07KAAAAAElFTkSuQmCC\n", "text/plain": [ "
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "cdf = CausalDataFrame(df)\n", "cdf.causal.do(x={'v': 1}, \n", " variable_types={'v': 'b', 'y': 'c', 'X0': 'c'}, \n", " outcome='y',\n", " method='mcmc', \n", " common_causes=['X0'],\n", " keep_original_treatment=True,\n", " proceed_when_unidentifiable=True).groupby('v').mean().plot(y='y', kind='bar')" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "scrolled": true }, "outputs": [], "source": [ "cdf = CausalDataFrame(df)\n" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "scrolled": false }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "INFO:dowhy.causal_identifier:Common causes of treatment and outcome:{'U', 'X0'}\n", "INFO:dowhy.causal_identifier:Instrumental variables for treatment and outcome:[]\n", "INFO:dowhy.do_sampler:Using McmcSampler for do sampling.\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Error: Pygraphviz cannot be loaded. No module named 'pygraphviz'\n", "Trying pydot ...\n", "['X0']\n", "yes\n", "{'observed': 'yes'}\n", "Model to find the causal effect of treatment v on outcome y\n", "{'label': 'Unobserved Confounders', 'observed': 'no'}\n", "All common causes are observed. Causal effect can be identified.\n", "McmcSampler\n", "treatments ['v']\n", "backdoor ['X0']\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "INFO:pymc3:Auto-assigning NUTS sampler...\n", "INFO:pymc3:Initializing NUTS using jitter+adapt_diag...\n", "INFO:pymc3:Multiprocess sampling (4 chains in 4 jobs)\n", "INFO:pymc3:NUTS: [y_sd, beta_y, v_sd, beta_v]\n", "Sampling 4 chains: 100%|██████████| 8000/8000 [00:05<00:00, 1479.09draws/s]\n", "INFO:dowhy.causal_identifier:Common causes of treatment and outcome:{'U', 'X0'}\n", "INFO:dowhy.causal_identifier:Instrumental variables for treatment and outcome:[]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "{'label': 'Unobserved Confounders', 'observed': 'no'}\n", "All common causes are observed. Causal effect can be identified.\n", "McmcSampler\n" ] } ], "source": [ "cdf_1 = cdf.causal.do(x={'v': 1}, \n", " variable_types={'v': 'b', 'y': 'c', 'X0': 'c'}, \n", " outcome='y',\n", " method='mcmc', \n", " dot_graph=data['dot_graph'],\n", " proceed_when_unidentifiable=True)\n", "\n", "cdf_0 = cdf.causal.do(x={'v': 0}, \n", " variable_types={'v': 'b', 'y': 'c', 'X0': 'c'}, \n", " outcome='y',\n", " method='mcmc', \n", " dot_graph=data['dot_graph'],\n", " proceed_when_unidentifiable=True,\n", " use_previous_sampler=True)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
X0vy
0-0.6795710-0.540989
1-0.2435370-0.816734
2-0.1951010-0.420884
30.92328800.543097
40.38977302.000232
50.3453400-0.235523
60.5399890-0.191927
71.29438301.841164
8-0.5576560-1.179258
9-0.5813190-1.585532
100.08957800.371095
110.82696103.333531
12-0.6712210-2.539698
130.98679101.801363
141.59410902.040565
15-0.2455270-0.289327
160.17818700.141852
172.02492402.980681
18-1.7602410-3.197609
191.91177004.160535
200.41153300.891029
21-1.6018900-3.900897
22-0.7796020-3.024336
232.11711803.186386
240.51538801.042302
250.1957950-1.043282
260.11283900.106886
270.5087120-0.138736
281.44932902.797328
290.94828502.310356
............
9700.73301202.388490
971-0.9705420-2.013554
9721.45177003.385053
9730.0862230-0.224399
9740.0621560-0.295354
9751.17805301.891484
9760.03804500.491061
977-0.4204540-1.158550
9780.49816800.916308
9790.17932001.385915
980-1.0819550-4.169600
9810.79272600.799942
9820.81715901.660682
9830.27712400.915327
9841.13944102.971099
9852.63630504.528907
9860.84908100.664671
9872.23170104.656510
9881.06503002.355032
989-0.13443800.298530
990-2.5407740-5.619055
991-0.3681380-0.208480
9921.05025601.603709
9930.66963100.961663
994-0.5087340-0.771802
995-0.1032550-0.251265
996-0.9067000-2.711775
9970.15640300.602863
998-0.2765390-0.986462
9990.60826000.644999
\n", "

1000 rows × 3 columns

\n", "
" ], "text/plain": [ " X0 v y\n", "0 -0.679571 0 -0.540989\n", "1 -0.243537 0 -0.816734\n", "2 -0.195101 0 -0.420884\n", "3 0.923288 0 0.543097\n", "4 0.389773 0 2.000232\n", "5 0.345340 0 -0.235523\n", "6 0.539989 0 -0.191927\n", "7 1.294383 0 1.841164\n", "8 -0.557656 0 -1.179258\n", "9 -0.581319 0 -1.585532\n", "10 0.089578 0 0.371095\n", "11 0.826961 0 3.333531\n", "12 -0.671221 0 -2.539698\n", "13 0.986791 0 1.801363\n", "14 1.594109 0 2.040565\n", "15 -0.245527 0 -0.289327\n", "16 0.178187 0 0.141852\n", "17 2.024924 0 2.980681\n", "18 -1.760241 0 -3.197609\n", "19 1.911770 0 4.160535\n", "20 0.411533 0 0.891029\n", "21 -1.601890 0 -3.900897\n", "22 -0.779602 0 -3.024336\n", "23 2.117118 0 3.186386\n", "24 0.515388 0 1.042302\n", "25 0.195795 0 -1.043282\n", "26 0.112839 0 0.106886\n", "27 0.508712 0 -0.138736\n", "28 1.449329 0 2.797328\n", "29 0.948285 0 2.310356\n", ".. ... .. ...\n", "970 0.733012 0 2.388490\n", "971 -0.970542 0 -2.013554\n", "972 1.451770 0 3.385053\n", "973 0.086223 0 -0.224399\n", "974 0.062156 0 -0.295354\n", "975 1.178053 0 1.891484\n", "976 0.038045 0 0.491061\n", "977 -0.420454 0 -1.158550\n", "978 0.498168 0 0.916308\n", "979 0.179320 0 1.385915\n", "980 -1.081955 0 -4.169600\n", "981 0.792726 0 0.799942\n", "982 0.817159 0 1.660682\n", "983 0.277124 0 0.915327\n", "984 1.139441 0 2.971099\n", "985 2.636305 0 4.528907\n", "986 0.849081 0 0.664671\n", "987 2.231701 0 4.656510\n", "988 1.065030 0 2.355032\n", "989 -0.134438 0 0.298530\n", "990 -2.540774 0 -5.619055\n", "991 -0.368138 0 -0.208480\n", "992 1.050256 0 1.603709\n", "993 0.669631 0 0.961663\n", "994 -0.508734 0 -0.771802\n", "995 -0.103255 0 -0.251265\n", "996 -0.906700 0 -2.711775\n", "997 0.156403 0 0.602863\n", "998 -0.276539 0 -0.986462\n", "999 0.608260 0 0.644999\n", "\n", "[1000 rows x 3 columns]" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cdf_0" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
X0vy
0-0.67957113.545042
1-0.24353714.665001
2-0.19510114.499220
30.92328816.673522
40.38977315.976617
50.34534015.553557
60.53998916.375131
71.29438317.499560
8-0.55765613.655596
9-0.58131914.275826
100.08957815.298058
110.82696116.531107
12-0.67122113.541304
130.98679117.004788
141.59410917.981339
15-0.24552714.159227
160.17818715.359598
172.02492419.260993
18-1.76024111.503151
191.91177018.763278
200.41153316.014290
21-1.60189011.895632
22-0.77960213.509766
232.11711819.529310
240.51538815.812637
250.19579515.080119
260.11283915.088389
270.50871215.831991
281.44932918.066816
290.94828516.386133
............
9700.73301216.507144
971-0.97054213.359742
9721.45177017.869123
9730.08622314.587526
9740.06215615.341651
9751.17805317.150807
9760.03804514.709112
977-0.42045413.717059
9780.49816815.969144
9790.17932015.106763
980-1.08195513.072383
9810.79272616.142553
9820.81715916.614257
9830.27712415.478525
9841.13944117.124206
9852.63630519.761611
9860.84908116.316136
9872.23170119.592429
9881.06503016.767247
989-0.13443814.681228
990-2.54077410.112361
991-0.36813814.395120
9921.05025617.209847
9930.66963116.251246
994-0.50873414.251908
995-0.10325515.060815
996-0.90670013.055053
9970.15640315.534543
998-0.27653914.520962
9990.60826016.146062
\n", "

1000 rows × 3 columns

\n", "
" ], "text/plain": [ " X0 v y\n", "0 -0.679571 1 3.545042\n", "1 -0.243537 1 4.665001\n", "2 -0.195101 1 4.499220\n", "3 0.923288 1 6.673522\n", "4 0.389773 1 5.976617\n", "5 0.345340 1 5.553557\n", "6 0.539989 1 6.375131\n", "7 1.294383 1 7.499560\n", "8 -0.557656 1 3.655596\n", "9 -0.581319 1 4.275826\n", "10 0.089578 1 5.298058\n", "11 0.826961 1 6.531107\n", "12 -0.671221 1 3.541304\n", "13 0.986791 1 7.004788\n", "14 1.594109 1 7.981339\n", "15 -0.245527 1 4.159227\n", "16 0.178187 1 5.359598\n", "17 2.024924 1 9.260993\n", "18 -1.760241 1 1.503151\n", "19 1.911770 1 8.763278\n", "20 0.411533 1 6.014290\n", "21 -1.601890 1 1.895632\n", "22 -0.779602 1 3.509766\n", "23 2.117118 1 9.529310\n", "24 0.515388 1 5.812637\n", "25 0.195795 1 5.080119\n", "26 0.112839 1 5.088389\n", "27 0.508712 1 5.831991\n", "28 1.449329 1 8.066816\n", "29 0.948285 1 6.386133\n", ".. ... .. ...\n", "970 0.733012 1 6.507144\n", "971 -0.970542 1 3.359742\n", "972 1.451770 1 7.869123\n", "973 0.086223 1 4.587526\n", "974 0.062156 1 5.341651\n", "975 1.178053 1 7.150807\n", "976 0.038045 1 4.709112\n", "977 -0.420454 1 3.717059\n", "978 0.498168 1 5.969144\n", "979 0.179320 1 5.106763\n", "980 -1.081955 1 3.072383\n", "981 0.792726 1 6.142553\n", "982 0.817159 1 6.614257\n", "983 0.277124 1 5.478525\n", "984 1.139441 1 7.124206\n", "985 2.636305 1 9.761611\n", "986 0.849081 1 6.316136\n", "987 2.231701 1 9.592429\n", "988 1.065030 1 6.767247\n", "989 -0.134438 1 4.681228\n", "990 -2.540774 1 0.112361\n", "991 -0.368138 1 4.395120\n", "992 1.050256 1 7.209847\n", "993 0.669631 1 6.251246\n", "994 -0.508734 1 4.251908\n", "995 -0.103255 1 5.060815\n", "996 -0.906700 1 3.055053\n", "997 0.156403 1 5.534543\n", "998 -0.276539 1 4.520962\n", "999 0.608260 1 6.146062\n", "\n", "[1000 rows x 3 columns]" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cdf_1" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "WARNING:dowhy.do_why:Causal Graph not provided. DoWhy will construct a graph based on data inputs.\n", "INFO:dowhy.causal_identifier:Common causes of treatment and outcome:{'U', 'X0'}\n", "INFO:dowhy.causal_identifier:Instrumental variables for treatment and outcome:[]\n", "INFO:dowhy.do_sampler:Using McmcSampler for do sampling.\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "['X0']\n", "yes\n", "{'observed': 'yes'}\n", "Model to find the causal effect of treatment v on outcome y\n", "{'label': 'Unobserved Confounders', 'observed': 'no'}\n", "All common causes are observed. Causal effect can be identified.\n", "McmcSampler\n", "treatments ['v']\n", "backdoor ['X0']\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "INFO:pymc3:Auto-assigning NUTS sampler...\n", "INFO:pymc3:Initializing NUTS using jitter+adapt_diag...\n", "INFO:pymc3:Multiprocess sampling (4 chains in 4 jobs)\n", "INFO:pymc3:NUTS: [y_sd, beta_y, v_sd, beta_v]\n", "Sampling 4 chains: 100%|██████████| 8000/8000 [00:08<00:00, 919.62draws/s] \n", "INFO:dowhy.causal_identifier:Common causes of treatment and outcome:{'U', 'X0'}\n", "INFO:dowhy.causal_identifier:Instrumental variables for treatment and outcome:[]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "{'label': 'Unobserved Confounders', 'observed': 'no'}\n", "All common causes are observed. Causal effect can be identified.\n", "McmcSampler\n" ] } ], "source": [ "cdf_1 = cdf.causal.do(x={'v': 1}, \n", " variable_types={'v': 'b', 'y': 'c', 'X0': 'c'}, \n", " outcome='y',\n", " method='mcmc', \n", " common_causes=['X0'],\n", " proceed_when_unidentifiable=True,\n", " use_previous_sampler=False)\n", "cdf_0 = cdf.causal.do(x={'v': 0}, \n", " variable_types={'v': 'b', 'y': 'c', 'X0': 'c'}, \n", " outcome='y',\n", " method='mcmc', \n", " common_causes=['X0'],\n", " proceed_when_unidentifiable=True,\n", " use_previous_sampler=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "(cdf_1['y'] - cdf_0['y']).mean()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "1.96*(cdf_1['y'] - cdf_0['y']).std() / np.sqrt(len(cdf))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": false }, "outputs": [], "source": [ "model = OLS(df['y'], df[['X0', 'v']])\n", "result = model.fit()\n", "result.summary()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "cdf_1" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "cdf_0" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cdf_do = cdf.causal.do(x={'v': 0}, \n", " variable_types={'v': 'b', 'y': 'c', 'X0': 'c'}, \n", " outcome='y',\n", " method='mcmc', \n", " common_causes=['X0'],\n", " proceed_when_unidentifiable=True,\n", " keep_original_treatment=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cdf_do" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.6" } }, "nbformat": 4, "nbformat_minor": 2 }