Princeton Election Consortium

A first draft of electoral history. Since 2004

Script for unrolling a rolling average

% by Sam Wang – Copyright 2008

% Gallup – uncomment the polls you want to use
rollavg=[45 46 45 45 45 45 46 45 45 46 45 48 49 49 48 49 50 49 49 48 47];
% rollavg=[45 43 44 43 44 44 44 45 45 44 44 42 41 41 42 43 42 43 42 44 45];

% Rasmussen (doesn’t work very well)
% rollavg=[44 43 44 45 45 46 46 46 45 45 46 46 46 45 45 46 45 45 44 43 44 45 46 46 46 46 46 46 47 47 46 46 46 47 46 46 46 46 46 45 45 45 46 45 46 46 46 46 45 45 46 47 47 45 45 46 46 45 45 45 46 46 48 ];
% rollavg=[49 49 48 48 47 47 46 47 47 48 46 47 46 47 46 46 47 48 49 49 49 48 47 48 48 47 47 47 46 46 47 47 47 46 48 48 48 48 47 47 46 47 47 47 47 48 47 48 48 48 46 46 47 49 49 49 49 51 50 50 48 49 48 ];

days=size(rollavg,2);
stepsize=0.2;
work=[rollavg(1) rollavg(1) rollavg];
steps=[work(1)-15*stepsize:stepsize:work(1)+15];
numsteps=size(steps,2);
for i=1:numsteps
work(1)=steps(i);
work(2)=(3*rollavg(1)-work(1))/2;
work(3)=work(2);
for j=1:days-1
work(j+3)=3*rollavg(j+1)-work(j+2)-work(j+1);
end
stdevs(i)=std(work);
end
[y,imin]=min(stdevs);
work(1)=steps(imin); % this value of first one-day poll minimizes the variance

for i=1:numsteps
work(2)=steps(i);
for j=1:days
work(j+2)=3*rollavg(j)-work(j+1)-work(j);
end
stdevs(i)=std(work);
end
[y,imin]=min(stdevs);
work(2)=steps(imin); % value of first one-day poll that minimizes variance

for j=1:days
work(j+2)=3*rollavg(j)-work(j+1)-work(j);
end
unroll=work