Created
March 19, 2019 08:07
-
-
Save NaimKabir/5eda54786b7596a8d7f60e081ad37552 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# linear programming problem\n", | |
"import pulp\n", | |
"min_problem = pulp.LpProblem(\"min ATE\", pulp.LpMinimize)\n", | |
"max_problem = pulp.LpProblem(\"max ATE\", pulp.LpMaximize)\n", | |
"\n", | |
"# our hidden variables are the the probability of being in one of the partitions\n", | |
"partition_names = ['/'.join([compliance, response]) for compliance, response in partition_types]\n", | |
"q = {partition: pulp.LpVariable(partition, lowBound=0) for partition in partition_names}" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# since our hidden vars are probabilities the sum of them should all be under 1\n", | |
"min_problem += sum([v for k,v in q.items()]) == 1" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 11, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# statements\n", | |
"p_treatment_untreated_bad = q['never_taker/never_better'] + q['defier/never_better'] \\\n", | |
" + q['never_taker/helped'] + q['defier/helped']\n", | |
"\n", | |
"p_treatment_untreated_good = q['never_taker/always_better'] + q['defier/always_better'] \\\n", | |
" + q['never_taker/hurt'] + q['defier/hurt']\n", | |
"\n", | |
"p_treatment_treated_bad = q['always_taker/never_better'] + q['complier/never_better'] \\\n", | |
" + q['always_taker/hurt'] + q['complier/hurt']\n", | |
"\n", | |
"p_treatment_treated_good = q['always_taker/always_better'] + q['complier/always_better'] \\\n", | |
" + q['always_taker/helped'] + q['complier/helped']\n", | |
"\n", | |
"p_control_untreated_bad = q['never_taker/never_better'] + q['complier/never_better'] \\\n", | |
" + q['never_taker/helped'] + q['complier/helped']\n", | |
"\n", | |
"p_control_untreated_good = q['never_taker/always_better'] + q['complier/never_better'] \\\n", | |
" + q['never_taker/hurt'] + q['complier/hurt']\n", | |
"\n", | |
"p_control_treated_bad = q['always_taker/never_better'] + q['defier/never_better'] \\\n", | |
" + q['always_taker/hurt'] + q['defier/hurt']\n", | |
"\n", | |
"p_control_treated_good = q['always_taker/always_better'] + q['defier/always_better'] \\\n", | |
" + q['always_taker/helped'] + q['defier/helped']" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 12, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# there's a natural mapping from probabilities we see to the hidden variables we have\n", | |
"# we'll spell these out one by one.\n", | |
"# there are probably smarter ways to express this as a vector operation,\n", | |
"# but this is easier to understand\n", | |
"min_problem += p_treatment_untreated_bad == p_states['treatment/untreated/bad']\n", | |
"\n", | |
"min_problem += p_treatment_untreated_good == p_states['treatment/untreated/good']\n", | |
"\n", | |
"min_problem += p_treatment_treated_bad == p_states['treatment/treated/bad']\n", | |
"\n", | |
"min_problem += p_control_untreated_bad == p_states['control/untreated/bad']\n", | |
"\n", | |
"min_problem += p_control_untreated_good == p_states['control/untreated/good']\n", | |
"\n", | |
"min_problem += p_control_treated_bad == p_states['control/treated/bad']\n", | |
"\n", | |
"#min_problem += p_treatment_treated_good == p_states['treatment/treated/good']\n", | |
"\n", | |
"#min_problem += p_control_treated_good == p_states['control/treated/good']" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 13, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"min_problem += q['complier/helped'] + q['defier/helped'] + q['always_taker/helped'] + q['never_taker/helped'] \\\n", | |
" - q['complier/hurt'] - q['defier/hurt'] - q['always_taker/hurt'] - q['never_taker/hurt']" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 14, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'Optimal'" | |
] | |
}, | |
"execution_count": 14, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"pulp.LpStatus[min_problem.solve()]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 15, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"{'always_taker/always_better': 0.174,\n", | |
" 'always_taker/helped': 0.0,\n", | |
" 'always_taker/hurt': 0.316,\n", | |
" 'always_taker/never_better': 0.0,\n", | |
" 'complier/always_better': 0.0,\n", | |
" 'complier/helped': 0.03,\n", | |
" 'complier/hurt': 0.0,\n", | |
" 'complier/never_better': 0.028,\n", | |
" 'defier/always_better': 0.02,\n", | |
" 'defier/helped': 0.0,\n", | |
" 'defier/hurt': 0.0,\n", | |
" 'defier/never_better': 0.0,\n", | |
" 'never_taker/always_better': 0.0,\n", | |
" 'never_taker/helped': 0.0,\n", | |
" 'never_taker/hurt': 0.154,\n", | |
" 'never_taker/never_better': 0.278}" | |
] | |
}, | |
"execution_count": 15, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"q_min = {partition:pulp.value(partition_p) for partition, partition_p in q.items()}\n", | |
"q_min" | |
] | |
} | |
], | |
"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.5" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment