Tuesday, August 30, 2016

General Poll for US Presidential Election 2016

We continue our adventure in the Bayesian USA 2016 election forecast through near-ignorance priors.

I will today show how to compute the lower and upepr probabilities for Clinton of winning the general election 2016. First, we load the lower and upper probabilities for Clinton of winning in every single State (see http://idpstat.blogspot.ch/2016/08/bayesian-winning-lower-and-upper.html) as well as the Electoral Vore for each state

In [12]:
import pandas as pd
lowerupper     = pd.read_csv('LowerUpper.csv')
electoralvotes = pd.read_csv('electoralvotes.csv')
In [40]:
lowerupper
Out[40]:
Unnamed: 0 LowerProbability UpperProbability
0 0 0.0026 0.0076
1 1 0.0155 0.0314
2 2 0.0367 0.0762
3 3 0.0690 0.1759
4 4 1.0000 1.0000
5 5 0.9034 0.9500
6 6 0.9937 0.9980
7 7 0.9950 0.9988
8 8 0.9850 0.9949
9 9 0.9459 0.9689
10 10 0.1747 0.2734
11 11 1.0000 1.0000
12 12 0.0013 0.0170
13 13 1.0000 1.0000
14 14 0.0003 0.0019
15 15 0.4979 0.6543
16 16 0.0098 0.0339
17 17 0.1462 0.2677
18 18 0.0081 0.0232
19 19 0.9931 0.9966
20 20 1.0000 1.0000
21 21 1.0000 1.0000
22 22 0.9549 0.9755
23 23 0.9774 0.9924
24 24 0.0006 0.0012
25 25 0.3789 0.5690
26 26 0.1162 0.3647
27 27 0.1013 0.2727
28 28 0.5729 0.7442
29 29 0.8637 0.9293
30 30 0.9999 1.0000
31 31 0.9890 0.9962
32 32 1.0000 1.0000
33 33 0.5132 0.6745
34 34 0.0003 0.0009
35 35 0.9250 0.9622
36 36 0.0000 0.0000
37 37 0.8899 0.9510
38 38 0.6900 0.8112
39 39 1.0000 1.0000
40 40 0.1803 0.3053
41 41 0.0448 0.0985
42 42 0.0079 0.0252
43 43 0.0000 0.0008
44 44 0.0001 0.0003
45 45 1.0000 1.0000
46 46 0.9994 0.9998
47 47 0.9996 1.0000
48 48 0.0712 0.2090
49 49 0.8702 0.9318
50 50 0.0000 0.0000
In [41]:
electoralvotes
Out[41]:
Index State Vote
0 1 Alabama 9
1 2 Alaska 3
2 3 Arizona 11
3 4 Arkansas 6
4 5 California 55
5 6 Colorado 9
6 7 Connecticut 7
7 8 Delaware 3
8 9 D.C. 3
9 10 Florida 29
10 11 Georgia 16
11 12 Hawaii 4
12 13 Idaho 4
13 14 Illinois 20
14 15 Indiana 11
15 16 Iowa 6
16 17 Kansas 6
17 18 Kentucky 8
18 19 Louisiana 8
19 20 Maine 4
20 21 Maryland 10
21 22 Massachusetts 11
22 23 Michigan 16
23 24 Minnesota 10
24 25 Mississippi 6
25 26 Missouri 10
26 27 Montana 3
27 28 Nebraska 5
28 29 Nevada 6
29 30 Hampshire 4
30 31 Jersey 14
31 32 Mexico 5
32 33 York 29
33 34 Carolina 15
34 35 Dakota 3
35 36 Ohio 18
36 37 Oklahoma 7
37 38 Oregon 7
38 39 Pennsylvania 20
39 40 Island 4
40 41 Carolina 9
41 42 Dakota 3
42 43 Tennessee 11
43 44 Texas 38
44 45 Utah 6
45 46 Vermont 3
46 47 Virginia 13
47 48 Washington 12
48 49 Virginia 5
49 50 Wisconsin 10
50 51 Wyoming 3

We compute two histograms: one relative to the lower probability and the other relative to the upper probability. To obtain the histogram of the lower: for each State, we generate a random number r in [0,1] and we assign the electoral vote of the State to Clinton if $r \leq LowerProbability$ in the State or to Trump otherwise. (same for the upper). We also compute the lower and upper probability that the total electoral votes for Clinton exceeds the break-even line (that is equal to 269)

In [34]:
import numpy as np
#break-even line
evenline=269
#monte Carlo samples
Np=10000

lowvotes=0
upvotes=0
LowElec=np.zeros(Np)
UpElec=np.zeros(Np)
for i in range(0,Np):
    lowElec=0
    upElec=0
    for s in range(0,51):
        if np.random.rand(1)<lowerupper['LowerProbability'][s]:
            lowElec=lowElec+electoralvotes['Vote'][s]
        if np.random.rand(1)<lowerupper['UpperProbability'][s]:
            upElec=upElec+electoralvotes['Vote'][s]
    LowElec[i]=lowElec
    UpElec[i]=upElec
    if lowElec>evenline:
        lowvotes=lowvotes+1
    if upElec>evenline:
        upvotes=upvotes+1
    
upvotes=upvotes/Np
lowvotes=lowvotes/Np
print('['+str(lowvotes) +',' +str(upvotes)+']')
[0.9981,0.9999]
In [44]:
import warnings
warnings.filterwarnings('ignore')
%matplotlib inline

import seaborn as sns
import matplotlib.pyplot as plt

sns.distplot(LowElec, axlabel="Electoral Votes (even-line in green)", 
                 kde=True, hist=True,color='darkred',label=str(lowvotes)) 
go=sns.distplot(UpElec, 
                 kde=True, hist=True,color='darkblue',label=str(upvotes)) 
go.set_title('Lower (red) and Upper (blue) distirbution for Clinton')
go.legend()
plt.axvline(x=270.,color='g')
Out[44]:
<matplotlib.lines.Line2D at 0x7f1513db14a8>
In [ ]:
 

No comments:

Post a Comment