{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Problem setup:\n", "\n", "1. 13,066,047 voters arrive to the polls.\n", "2. $p_{yes}\\%$ of them intend to vote \"Yes\", $(1-p_{yes})\\%$ of them intend to vote \"No.\"\n", "3. Each voter casts an invalid (unmarked or void) ballot with probability $p_{invalid}\\%$.\n", "4. Of the valid ballots, the poll workers misclassify the vote with probability $p_{misclassification}\\%$.\n", "5. Majority vote wins." ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "code", "execution_count": 37, "metadata": { "collapsed": true }, "outputs": [], "source": [ "YES_BALLOTS = 6377482\n", "NO_BALLOTS = 6431376\n", "UNMARKED_BALLOTS = 86243\n", "NULL_BALLOTS = 170946" ] }, { "cell_type": "code", "execution_count": 38, "metadata": { "collapsed": true }, "outputs": [], "source": [ "TOTAL_VOTES = YES_BALLOTS + NO_BALLOTS + UNMARKED_BALLOTS + NULL_BALLOTS\n", "P_INVALID = .02\n", "P_MISCLASSIFICATION = .01\n", "N_TRIALS = 100000" ] }, { "cell_type": "code", "execution_count": 39, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def simulate_vote(probability_yes):\n", " yes_votes = int(TOTAL_VOTES * probability_yes)\n", " no_votes = TOTAL_VOTES - yes_votes\n", " \n", " yes_votes_samples = N_TRIALS * [yes_votes]\n", " no_votes_samples = N_TRIALS * [no_votes]\n", " \n", " invalid_ballots_yes = np.random.binomial(n=yes_votes_samples, p=P_INVALID)\n", " invalid_ballots_no = np.random.binomial(n=no_votes_samples, p=P_INVALID)\n", " \n", " valid_yes_votes = yes_votes - invalid_ballots_yes\n", " valid_no_votes = no_votes - invalid_ballots_no\n", " \n", " yes_votes_from_yes_voters = np.random.binomial(n=valid_yes_votes, p=1-P_MISCLASSIFICATION)\n", " no_votes_from_yes_voters = valid_yes_votes - yes_votes_from_yes_voters\n", " \n", " no_votes_from_no_voters = np.random.binomial(n=valid_no_votes, p=1-P_MISCLASSIFICATION)\n", " yes_votes_from_no_voters = valid_no_votes - no_votes_from_no_voters\n", " \n", " tallied_yes_votes = yes_votes_from_yes_voters + yes_votes_from_no_voters\n", " tallied_no_votes = no_votes_from_no_voters + no_votes_from_yes_voters\n", " \n", " return tallied_yes_votes / (tallied_yes_votes + tallied_no_votes)" ] }, { "cell_type": "code", "execution_count": 40, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "p_yes: 60.000000% | no_win_percentage: 0.000%\n", "p_yes: 51.000000% | no_win_percentage: 0.000%\n", "p_yes: 50.100000% | no_win_percentage: 0.000%\n", "p_yes: 50.010000% | no_win_percentage: 0.191%\n", "p_yes: 50.001000% | no_win_percentage: 38.688%\n", "p_yes: 50.000100% | no_win_percentage: 48.791%\n", "p_yes: 50.000010% | no_win_percentage: 50.063%\n" ] } ], "source": [ "for epsilon in [1e-1, 1e-2, 1e-3, 1e-4, 1e-5, 1e-6, 1e-7]:\n", " probability_yes = .5 + epsilon\n", " percentage_of_tallied_votes_that_were_yes = simulate_vote(probability_yes)\n", " proportion_of_trials_won_by_no = (percentage_of_tallied_votes_that_were_yes < .5).mean()\n", " \n", " results = \"p_yes: {:1.6f}% | no_win_percentage: {:1.3f}%\"\n", " print(results.format(100*probability_yes, 100*proportion_of_trials_won_by_no))" ] } ], "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.5.1" } }, "nbformat": 4, "nbformat_minor": 0 }