polls.pop=[.473 .781 .884 2.913 .634 .287 1.953 1.110 1.190 1.631 .302 .279 .286 2.350 .714 2.281 1.062 1.437 1.109 .336 1.237 1.284 ]*10; for j=1:numpolls %%%%%%%%%%%%%%%%%%%%%%%%%%% The calculation %%%%%%%%%%%%%%%%%%%%%%%% %%%% Can assign bias and then cut-and-paste this to get result %%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Calculate z-score and convert to probability, assuming normal distribution. polls.z=(polls.averagemargin+bias)./polls.SEM; polls.probkerrywin=(erf(polls.z/sqrt(2))+1)/2; polls.probbushwin=1-polls.probkerrywin; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ztemp=(polls.averagemargin(j)-1/polls.pop(j)+bias)./polls.SEM(j); polls.probkerrywin(j)=(erf(ztemp/sqrt(2))+1)/2; EVpoly=[polls.probkerrywin(1) zeros(1, polls.EV(1)-1) 1-polls.probkerrywin(1)]; for i=2:numpolls nextEV=[polls.probkerrywin(i) zeros(1, polls.EV(i)-1) 1-polls.probkerrywin(i)]; EVpoly=conv(EVpoly,nextEV); end histogram=fliplr(EVpoly); % Cumulative histogram of all possibilities cumulative_prob=cumsum(histogram); electoralvotes=kerrysafe:kerrysafe+uncertainvotes; % Calculate statistics from cumulative histogram confidence_low=electoralvotes(max(find(cumulative_prob<0.025))); % 95-pct lower limit KerrymedianEV=electoralvotes(min(find(cumulative_prob>0.5))); % 50-pct outcome confidence_high=electoralvotes(min(find(cumulative_prob>0.975))); % 95-pct upper limit probability_Kerry_win=1-cumulative_prob(min(find(electoralvotes>=269))); BushmedianEV=538-KerrymedianEV; problow(j)=probability_Kerry_win; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ztemp=(polls.averagemargin(j)+1/polls.pop(j)+bias)./polls.SEM(j); polls.probkerrywin(j)=(erf(ztemp/sqrt(2))+1)/2; EVpoly=[polls.probkerrywin(1) zeros(1, polls.EV(1)-1) 1-polls.probkerrywin(1)]; for i=2:numpolls nextEV=[polls.probkerrywin(i) zeros(1, polls.EV(i)-1) 1-polls.probkerrywin(i)]; EVpoly=conv(EVpoly,nextEV); end histogram=fliplr(EVpoly); % Cumulative histogram of all possibilities cumulative_prob=cumsum(histogram); electoralvotes=kerrysafe:kerrysafe+uncertainvotes; % Calculate statistics from cumulative histogram confidence_low=electoralvotes(max(find(cumulative_prob<0.025))); % 95-pct lower limit KerrymedianEV=electoralvotes(min(find(cumulative_prob>0.5))); % 50-pct outcome confidence_high=electoralvotes(min(find(cumulative_prob>0.975))); % 95-pct upper limit probability_Kerry_win=1-cumulative_prob(min(find(electoralvotes>=269))); BushmedianEV=538-KerrymedianEV; probhigh(j)=probability_Kerry_win; end problow probhigh [sortedlows i_low]=sort(problow); [sortedhighs i_high]=sort(-probhigh); sortedhighs=-sortedhighs; Bush_wins_if=[i_low' 100*(1-sortedlows')] Kerry_wins_if=[i_high' 100*sortedhighs']