Skip to content

Commit

Permalink
clean up notebook
Browse files Browse the repository at this point in the history
  • Loading branch information
wtadler committed Aug 17, 2018
1 parent 16f998c commit 7a33826
Showing 1 changed file with 56 additions and 45 deletions.
101 changes: 56 additions & 45 deletions run_gerrymandering_metrics.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"source": [
"# Compute gerrymandering metrics\n",
"\n",
"This notebook demonstrates how to load in election results, creating 3 different DataFrames that one might be interested in:\n",
"This notebook demonstrates how to load in election results, creating 3 different DataFrames that you might be interested in:\n",
"1. Election results\n",
"2. Metrics\n",
"3. Percentiles for metrics\n",
Expand All @@ -27,10 +27,10 @@
"text": [
" 0%| | 0/47 [00:00<?, ?it/s]/Users/wtadler/miniconda3/envs/py3k/lib/python3.6/site-packages/numpy/lib/function_base.py:4033: RuntimeWarning: Invalid value encountered in median\n",
" r = func(a, **kwargs)\n",
"100%|██████████| 47/47 [00:01<00:00, 29.48it/s]\n",
"100%|██████████| 47/47 [00:01<00:00, 31.06it/s]\n",
"/Users/wtadler/miniconda3/envs/py3k/lib/python3.6/site-packages/numpy/core/fromnumeric.py:2957: RuntimeWarning: Mean of empty slice.\n",
" out=out, **kwargs)\n",
"100%|██████████| 35/35 [00:02<00:00, 15.22it/s]\n"
"100%|██████████| 35/35 [00:02<00:00, 15.95it/s]\n"
]
}
],
Expand All @@ -41,38 +41,49 @@
"\n",
"from collections import defaultdict\n",
"\n",
"impute_val = 1 # impute uncontested races at voteshare=1; in other words, don't impute\n",
"competitiveness_threshold = .55 # only examine races where D voteshare is between .45 and .55\n",
"min_districts = 7 # only examine races in states with at least 7 districts\n",
"min_year = 1972 # only examine races after 1972\n",
"# impute uncontested races at a voteshare of 0 or 1; in other words, don't impute them\n",
"impute_val = 1\n",
"\n",
"# only consider races after 1972\n",
"min_year = 1972\n",
"\n",
"# when identifying the worst gerrymanders:\n",
"# only examine races where D voteshare is between .45 and .55\n",
"competitiveness_threshold = .55 \n",
"\n",
"# only examine races in states with at least 7 districts\n",
"min_districts = 7\n",
"\n",
"\n",
"\n",
"chambers = defaultdict(lambda: defaultdict(list))\n",
"chambers['State Legislative']['filepath'] = 'election_data/state_legislative/state_legislative_election_results_post1971.csv'\n",
"chambers['Congressional']['filepath'] = 'election_data/congressional_election_results_post1948.csv'\n",
"\n",
"metric_dict = {'t_test_diff': m.t_test_diff,\n",
" 'mean_median_diff': m.mean_median,\n",
" 'declination': m.declination,\n",
" 'declination_buffered': m.bdec,\n",
" 'efficiency_gap': m.EG,\n",
" 'loss_gap': m.EG_loss_only,\n",
" 'difference_gap': m.EG_difference,\n",
" 'surplus_gap': m.EG_surplus_only,\n",
" 'vote_centric_gap': m.EG_vote_centric,\n",
" 'vote_centric_gap_two': m.EG_vote_centric_two,\n",
" 'partisan_bias': m.partisan_bias,\n",
"metric_dict = {'t_test_diff': m.t_test_diff,\n",
" 'mean_median_diff': m.mean_median,\n",
" 'declination': m.declination,\n",
" 'declination_buffered': m.bdec,\n",
" 'efficiency_gap': m.EG,\n",
" 'loss_gap': m.EG_loss_only,\n",
" 'difference_gap': m.EG_difference,\n",
" 'surplus_gap': m.EG_surplus_only,\n",
" 'vote_centric_gap': m.EG_vote_centric,\n",
" 'vote_centric_gap_two': m.EG_vote_centric_two,\n",
" 'partisan_bias': m.partisan_bias,\n",
" 'equal_vote_weight_bias': m.equal_vote_weight}\n",
"\n",
"for chamber in chambers:\n",
" chambers[chamber]['elections_df'] = u.parse_results(chambers[chamber]['filepath'])\n",
" chambers[chamber]['tests_df'] = u.tests_df(u.run_all_tests(chambers[chamber]['elections_df'],\n",
" impute_val=impute_val,\n",
" metrics=metric_dict))\n",
" chambers[chamber]['tests_df'] = u.tests_df(u.run_all_tests(\n",
" chambers[chamber]['elections_df'],\n",
" impute_val=impute_val,\n",
" metrics=metric_dict))\n",
" chambers[chamber]['percentile_df'] = u.generate_percentiles(chambers[chamber]['tests_df'],\n",
" metric_dict.keys(),\n",
" competitiveness_threshold=competitiveness_threshold,\n",
" min_districts=min_districts,\n",
" min_year=min_year)"
" metric_dict.keys(),\n",
" competitiveness_threshold=competitiveness_threshold,\n",
" min_districts=min_districts,\n",
" min_year=min_year)"
]
},
{
Expand Down Expand Up @@ -685,26 +696,26 @@
{
"data": {
"text/plain": [
"voteshare 0.514767\n",
"dseats 4\n",
"seats 4\n",
"voteshare 0.492911\n",
"dseats 3\n",
"seats 3\n",
"ndists 11\n",
"state VA\n",
"year 2016\n",
"weighted_voteshare 0.514767\n",
"t_test_diff 89.6552\n",
"mean_median_diff 89.6552\n",
"declination 88.67\n",
"declination_buffered 89.6552\n",
"efficiency_gap 88.1773\n",
"loss_gap 88.1773\n",
"difference_gap 89.6552\n",
"surplus_gap 32.5123\n",
"vote_centric_gap 88.1773\n",
"vote_centric_gap_two 88.67\n",
"partisan_bias 85.468\n",
"equal_vote_weight_bias 94.0887\n",
"Name: (2016, VA), dtype: object"
"year 2012\n",
"weighted_voteshare 0.492911\n",
"t_test_diff 87.1921\n",
"mean_median_diff 72.9064\n",
"declination 97.0443\n",
"declination_buffered 92.1182\n",
"efficiency_gap 94.5813\n",
"loss_gap 95.5665\n",
"difference_gap 94.0887\n",
"surplus_gap 14.2857\n",
"vote_centric_gap 95.5665\n",
"vote_centric_gap_two 96.0591\n",
"partisan_bias 97.7833\n",
"equal_vote_weight_bias 37.6847\n",
"Name: (2012, VA), dtype: object"
]
},
"execution_count": 5,
Expand All @@ -713,7 +724,7 @@
}
],
"source": [
"chambers['Congressional']['percentile_df'].loc[2016, 'VA']"
"chambers['Congressional']['percentile_df'].loc[2012, 'VA']"
]
},
{
Expand Down Expand Up @@ -759,7 +770,7 @@
"\n",
"Only showing elections for states that have at least {n_districts} districts,\n",
"and for which the statewide total voteshare was competitive,\n",
"i.e., between {comp_lo:.2g} and {comp_hi:.2g}.\n",
"i.e., between {comp_lo:.2} and {comp_hi:.2}.\n",
"'''.format(start=cycle_start_year,\n",
" min_tests=min_n_tests,\n",
" total_tests=len(metric_dict),\n",
Expand Down

0 comments on commit 7a33826

Please sign in to comment.