Princeton Election Consortium

A first draft of electoral history. Since 2004

Meta-Margins for control: House D+2.8% Senate R+5.6% Find key elections near you!

MATLAB – gerrymandering code – gerry.m

Here’s the core code for the gerrymandering analysis. Currently a mess, still cleaning it up.

% gerry.m – Gerrymandering simulation.
% Copyright Sam Wang under GNU License, 2013.
% OK to copy, distribute, and modify, but retain this header
%
% Princeton Election Consortium
% http://election.princeton.edu

% If calling gerry.m from delegations.m, comment out plotting commands

load votedata2012 % Dvotes and Rvotes – can be vote counts or percentages
% Or…information is available from David Wasserman @Redistrict
% https://docs.google.com/spreadsheet/ccc?key=0AjYj9mXElO_QdHZCbzJocGtxYkR6
% OTdZbzZwRUFvS3c#gid=0

% Problem states
OH=[299:314]; % really, really bad
NC=[285:297]; % really bad
PA=[325:342]; % really bad USE AS EXAMPLE
IN=[151:159]; % bad
VA=[403:413]; % bad
MI=[199:212]; % bad
WI=[427:434]; % somewhat bad

% Seem to be OK by criteria based on this code
MO=[225:232]; % fine
IL=[133:150]; % bad
NY=[258:284]; % fine
FL=[88:114]; % fine
CA=[22:74]; % fine
MD=[182:189]; % fine
MA=[190:198]; % fine
NJ=[243:254]; % fine
TN=[353:361]; % fine
TX=[362:397]; % Democrats are overrepresented by 2-3 seats. Courts bite back!
AZ=[9:17]; % Democrats are overrepresented by about 1 seat
Hisp=[9:17 22:74 362:397]; % Hispanic states. not out of the ordinary

% alldistricts=[1:435]; if you wanted to use voting results from all
% districts
alldistricts=setdiff([1:435],[9:17 88:114 133:150 151:159 199:212 285:297 299:314 325:342 362:397 403:411]); % omit AZ FL IL IN MI NC OH PA TX VA

% sdist=[1:435];
sdist=[PA OH MI NC VA FL IN]; % all the bads at once. HOLY COW
% equivalent of 4 million voters disenfranchised.
% sdist=PA;

% assume 75-25 vote split for unopposed races
for i=1:length(Rvotes)
if Dvotes(i)==0
Dvotes(i)=Rvotes(i)/3;
end
if Rvotes(sdist)==0
Rvotes(i)=Dvotes(i)/3;
end
end

s_dvote=sum(Dvotes(sdist));
s_rvote=sum(Rvotes(sdist));
s_p=s_dvote/(s_dvote+s_rvote)*100;
Rp=Rvotes./(Rvotes+Dvotes)*100;
s_dseats=sum(Rp(sdist)<50); s_rseats=sum(Rp(sdist)>50);
[sdist(1) s_p s_dseats s_rseats]

numdist=length(sdist);

num=0; cumul_s=0; rtail=0; dtail=0;
for i=1:1000
districts=alldistricts([floor(rand([1 numdist])*length(alldistricts))+1]);
dvote=sum(Dvotes(districts));
rvote=sum(Rvotes(districts));
p=dvote/(dvote+rvote)*100;
dseats=sum(Rp(districts)<50); % if this is being called from delegations.m then suppress plot plot(p,dseats,'.k') hold on if abs(p-s_p)<0.5 num=num+1; cumul_s=cumul_s+dseats; if numdist-dseats<=s_rseats rtail=rtail+1; end if dseats<=s_dseats dtail=dtail+1; end end end outs=[num dtail/num rtail/num cumul_s/num cumul_s/num-s_dseats] if num==0 outs=[0 .5 .5 s_dseats s_dseats] sdist(1) end % if this is being called from delegations.m then suppress plot(s_p,s_dseats,'or') grid on plot([30 70],[length(sdist)/2 length(sdist)/2],'-g') axis([30 70 0 length(sdist)]) xlabel('Democratic fraction of two-party House vote (%)') ylabel('Democratic House seats') ist=[1 8 9 18 22 75 82 87 88 115 129 131 133 151 160 164 168 174 180 182 190 199 213 221 225 233 234 237 241 243 255 258 285 298 299 315 320 325 343 345 352 353 362 398 402 403 414 424 427 435 436]; for i=1:50 i1=ist(i);i2=ist(i+1)-1; del(i)=i2-i1+1; st=[i1:i2]; dv(i)=sum(Dvotes(st))/(sum(Rvotes(st))+sum(Dvotes(st)))*100; % ds(i)=sum(Rp(st)<50)/del(i)*100; ds(i)=sum(Rp(st)<50); end % identify states where seats and votes go in opposite directions % match=(((dv-50).*(ds-50))<0); % foo=find(match==1); % ist(foo) % AZ MI NC PA WI are the states where >votes and