% Senate estimate 30 June 2014
% Copyright Sam Wang, Princeton University, sswang@princeton.edu
% GNU license - free use but retain this header.
%
%
% Dem/Ind safe: 45
% GOP safe: 46
% 9 races remain: AK, AR, CO, GA, IA, KY, LA, MI, NC

if exist('bias')~=1
    bias=0; % this allows the future change to be biased in one direction or
% the other. bias>0 indicates toward Democrats
end
demsafe=45;
GOPsafe=46;
un=1.5;
numstates=100-demsafe-GOPsafe;
% polls newest to oldest from HuffPost. last three polls, going 
% back no more than 6 weeks
% If over 3 polls are available and the oldest polls are near one another in time, take them all
margins_ak=[5 -5]; % http://elections.huffingtonpost.com/pollster/2014-alaska-senate-sullivan-vs-begich
margins_ar=[-7 -8 -4]; % http://elections.huffingtonpost.com/pollster/2014-arkansas-senate-cotton-vs-pryor
margins_co=[1 4 -2]; % http://elections.huffingtonpost.com/pollster/2014-colorado-senate-gardner-vs-udall
margins_ga=[2 3 -5]; % http://elections.huffingtonpost.com/pollster/2014-georgia-senate-perdue-vs-nunn
margins_ia=[4 -6 -1 -5]; % http://elections.huffingtonpost.com/pollster/2014-iowa-senate-ernst-vs-braley
margins_ky=[2 3 -7]; % http://elections.huffingtonpost.com/pollster/2014-kentucky-senate-mcconnell-vs-grimes
margins_la=[0 -6]; % http://elections.huffingtonpost.com/pollster/2014-louisiana-senate-cassidy-vs-landrieu
margins_mi=[3 5 9]; % http://elections.huffingtonpost.com/pollster/2014-michigan-senate-land-vs-peters
margins_nc=[1 5 7]; % http://elections.huffingtonpost.com/pollster/2014-north-carolina-senate-tillis-vs-hagan

med(1)=median(margins_ak);
med(2)=median(margins_ar);
med(3)=median(margins_co);
med(4)=median(margins_ga);
med(5)=median(margins_ia);
med(6)=median(margins_ky);
med(7)=median(margins_la);
med(8)=median(margins_mi);
med(9)=median(margins_nc);

len(1)=length(margins_ak);
len(2)=length(margins_ar);
len(3)=length(margins_co);
len(4)=length(margins_ga);
len(5)=length(margins_ia);
len(6)=length(margins_ky);
len(7)=length(margins_la);
len(8)=length(margins_mi);
len(9)=length(margins_nc);

sem(1)=mad(margins_ak)/0.6745*1.2/sqrt(length(margins_ak));
sem(2)=mad(margins_ar)/0.6745*1.2/sqrt(length(margins_ar));
sem(3)=mad(margins_co)/0.6745*1.2/sqrt(length(margins_co));
sem(4)=mad(margins_ga)/0.6745*1.2/sqrt(length(margins_ga));
sem(5)=mad(margins_ia)/0.6745*1.2/sqrt(length(margins_ia));
sem(6)=mad(margins_ky)/0.6745*1.2/sqrt(length(margins_ky));
sem(7)=mad(margins_la)/0.6745*1.2/sqrt(length(margins_la));
sem(8)=mad(margins_mi)/0.6745*1.2/sqrt(length(margins_mi));
sem(9)=mad(margins_nc)/0.6745*1.2/sqrt(length(margins_nc));

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

for diff=bias-2:0.2:bias+2

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') % uses Presidential probability in an
% on-year
meanseats=sum(alldists.*[0:numstates])/100+demsafe
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_2014.csv', [mode_dem mode_gop prob_dem]);