Skip to content

Instantly share code, notes, and snippets.

@akelleh
Created January 21, 2019 14:57
Show Gist options
  • Save akelleh/451032abcc46d83c8eee78a3223422d5 to your computer and use it in GitHub Desktop.
Save akelleh/451032abcc46d83c8eee78a3223422d5 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import dowhy.datasets\n",
"\n",
"\n",
"data = dowhy.datasets.linear_dataset(beta=10,\n",
" num_common_causes=5,\n",
" num_instruments = 2,\n",
" num_samples=1000,\n",
" treatment_is_binary=True)\n",
"df = data[\"df\"]"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"from dowhy.causal_estimators.gformula_estimator import GFormulaEstimator\n",
"from dowhy.do_why import CausalModel"
]
},
{
"cell_type": "code",
"execution_count": 3,
"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:{'X2', 'X0', 'U', 'X3', 'X1', 'X4'}\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"['X2', 'X0', 'X3', 'X1', 'X4']\n",
"yes\n",
"{'observed': 'yes'}\n",
"yes\n",
"{'observed': 'yes'}\n",
"yes\n",
"{'observed': 'yes'}\n",
"yes\n",
"{'observed': 'yes'}\n",
"yes\n",
"{'observed': 'yes'}\n",
"Model to find the causal effect of treatment v on outcome y\n",
"{'observed': 'yes'}\n",
"{'observed': 'yes'}\n",
"{'label': 'Unobserved Confounders', 'observed': 'no'}\n",
"There are unobserved common causes. Causal effect cannot be identified.\n",
"WARN: Do you want to continue by ignoring these unobserved confounders? [y/n] \n",
"Please respond with 'y' or 'n'\n",
"WARN: Do you want to continue by ignoring these unobserved confounders? [y/n] yes\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"INFO:dowhy.causal_identifier:Instrumental variables for treatment and outcome:[]\n"
]
}
],
"source": [
"model= CausalModel(\n",
" data=df,\n",
" treatment=data[\"treatment_name\"],\n",
" outcome=data[\"outcome_name\"],\n",
" common_causes=data[\"common_causes_names\"])\n",
"\n",
"identified_estimand = model.identify_effect()\n"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"INFO:dowhy.causal_estimator:INFO: Using Linear Robins G-Formula Estimator\n",
"INFO:dowhy.causal_estimator:b: y~v+X2+X0+X3+X1+X4\n"
]
}
],
"source": [
"test_significance=False\n",
"estimator = GFormulaEstimator(df, identified_estimand, 'v', 'y',\n",
" test_significance, params=None)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAANMAAAASCAYAAADBs+vIAAAABHNCSVQICAgIfAhkiAAAAfJJREFUaIHtmr1KA0EUhT8FG0Wi2CoIIeojWG7ewMLX8B3SaSEi2FiphQ9gqRY2Ym+nUcTYKHb+FlqozSSGZTfZmcleLuF+sAyZmXPn3OLAkh0wDKMUVoEd4Bx4A36Bwz6aWWAPeAS+gBawDUwHnB9SS0Kj1Zf1Uv4ZIZkA4NJtfgeuCgirwLPbdwRsAGfu9zUwU+TQiFoSGq2+rBcZX76Z6FAHasAIkBQQnrg9a6n5LTe/W+TQiFoSGq2+QjRafYVopHz5ZiKTfsKqW78HRlNrk8AH8AlMFDgrpJaERqsv60XGV5qEHplIF/Wh7sZT4Ce19g5cAOPAckm1JDRafVkvMr68iAnTohtvctZv3bhQUi0JjVZfIRqtvkI0Ur68iAlTxY2vOevt+amSaklotPoK0Wj1FaKR8uVFTJgMw+giJkztJFdy1tvzLyXVktBo9RWi0eorRCPly4uYMDXdmPeOWXNj3jtqbC0JjVZfIRqtvkI0Ur4GRoL9Na7Vl/Wi8K/xXhQRhnwEqwJLwNgAag3Tx0HrRWcv3SR4hGkFOHDPsRPedc1tpvanr2es8389o0n29YyWW58fQC0JjVZf1ouML99MdGi4zXlPK0MzB+wDT8A38EDvi4MtssMUUktKo9WX9VL+GQ38M2EYhmEYhmEMD39EAOyLfgE4wAAAAABJRU5ErkJggg==\n",
"text/latex": [
"$$10.00000000000001$$"
],
"text/plain": [
"10.00000000000001"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"estimator.estimate_outcome(1.) - estimator.estimate_outcome(0.)"
]
},
{
"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.7"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment