Princeton Election Consortium

A first draft of electoral history. Since 2004

MATLAB code – Senate 2012 estimate

% Senate estimate 19 September 2012
% Copyright Sam Wang, Princeton University, sswang@princeton.edu
% GNU license – free use but retain this header.
%
%
% Dem/Ind safe: 47
% GOP safe: 45 (counting NV and AZ for now)
% 8 races remain: CT, IN, MA, ND, VA, WI, MT, MO

bias=0; % this allows the future change to be biased in one direction or the other. bias>0 indicates toward Democrats
demsafe=47;
GOPsafe=45;
un=0;
numstates=100-demsafe-GOPsafe;
% polls newest to oldest from HuffPost. last three polls, going back to second half August at the latest
margins_ct=[-3 4 -3]; % http://elections.huffingtonpost.com/pollster/2012-connecticut-senate-mcmahon-vs-murphy
margins_in=[3 2 -2]; % http://elections.huffingtonpost.com/pollster/2012-indiana-senate-mourdock-vs-donnelly
margins_ma=[2 4 6]; % http://elections.huffingtonpost.com/pollster/2012-massachusetts-senate-brown-vs-warren
margins_nd=[6 -9]; % http://elections.huffingtonpost.com/pollster/2012-north-dakota-senate-berg-vs-heitkamp
margins_va=[1 2 0]; % http://elections.huffingtonpost.com/pollster/2012-virginia-senate-allen-vs-kaine
margins_wi=[3 5 -6]; % http://elections.huffingtonpost.com/pollster/2012-wisconsin-senate-thompson-vs-baldwin
margins_mt=[2 -4]; % http://elections.huffingtonpost.com/pollster/2012-montana-senate-rehberg-vs-tester
margins_mo=[6 -5 1 2 9]; % http://elections.huffingtonpost.com/pollster/2012-missouri-senate-mccaskill-vs-akin

med(1)=median(margins_ct);
med(2)=median(margins_in);
med(3)=median(margins_ma);
med(4)=median(margins_nd);
med(5)=median(margins_va);
med(6)=median(margins_wi);
med(7)=median(margins_mt);
med(8)=median(margins_mo);

len(1)=length(margins_ct);
len(2)=length(margins_in);
len(3)=length(margins_ma);
len(4)=length(margins_nd);
len(5)=length(margins_va);
len(6)=length(margins_wi);
len(7)=length(margins_mt);
len(8)=length(margins_mo);

sem(1)=mad(margins_ct)/0.6745*1.2/sqrt(length(margins_ct));
sem(2)=mad(margins_in)/0.6745*1.2/sqrt(length(margins_in));
sem(3)=mad(margins_ma)/0.6745*1.2/sqrt(length(margins_ma));
sem(4)=mad(margins_nd)/0.6745*1.2/sqrt(length(margins_nd));
sem(5)=mad(margins_va)/0.6745*1.2/sqrt(length(margins_va));
sem(6)=mad(margins_wi)/0.6745*1.2/sqrt(length(margins_wi));
sem(7)=mad(margins_mt)/0.6745*1.2/sqrt(length(margins_mt));
sem(8)=mad(margins_mo)/0.6745*1.2/sqrt(length(margins_mo));

alldists=zeros(1,numstates+1);num=0;allprobs=zeros(1,numstates);

for diff=-3+bias:0.2:3+bias;

for i=1:numstates
z(i)=(med(i)+diff)/max(sqrt(sem(i)*sem(i)+un*un),sqrt(1/500/len(i)));
end

senate_probs=tcdf(z,len);

[diff sum(senate_probs)]

senate_dist=[1-senate_probs(1) senate_probs(1)];
for i=2:numstates
senate_dist=conv(senate_dist,[1-senate_probs(i) senate_probs(i)]);
end

allprobs=allprobs+senate_probs;
alldists=alldists+senate_dist;
num=num+1;
end
alldists=alldists/num;
allprobs=allprobs/num
alldists=alldists*100;
bar(demsafe:demsafe+numstates,alldists,’r')
% bar(demsafe:49,alldists(1:50-demsafe),’r')
hold on
bar(50:demsafe+numstates,alldists(51-demsafe:numstates+1),’b')
bar(50,alldists(4)*0.12,’r')
grid on
xlabel(‘Democratic/Independent Senate seats’)
ylabel(‘Probability (%)’)

dlmwrite(‘Sen_histogram.csv’, alldists’);

mode_dem=demsafe-1+find(alldists==max(alldists));
mode_gop=100-mode_dem;
prob_dem=sum(alldists(52-demsafe:numstates+1))+alldists(51-demsafe)*0.88

dlmwrite(‘Sen_estimates_2012.csv’, [mode_dem mode_gop prob_dem]);