% SVD analysis of 2012 House of Representatives
% Jesse Chan, 3/27/2014

fid = fopen('names.txt');
names = textscan(fid, '%25s','Delimiter','\t');
fclose(fid);
names = names{1};
load parties.txt
load votes.txt
votes = votes';
% for i = 1:size(votes,2)
%     votes(:,i) = votes(:,i)-mean(votes(:,i));
% end

dems = find(parties==100);
reps = find(parties==200);

[U S V] = svd(votes,'econ');
Uk = U(:,1:2);
Sk = S(1:2,1:2);
Vk = V(:,1:2);

% % all these choices are equivalent. 
% c = Uk'*votes; 
% c = Sk*Vk';
c = Vk';
% c = (Uk*Sk)'*votes; % can also use (Uk*Sk)'*votes

%% Plot all reps
plot(c(1,reps),c(2,reps),'r.') 
hold on;plot(c(1,dems),c(2,dems),'c.')
ylabel('c_2: tendency to vote with majority','fontsize',15)
xlabel('c_1: tendency to vote with party','fontsize',15)
set(gca,'fontsize',14)

%% Obama/Boehner

off = mean(c(:))*.075;
p = [156 180];
plot(c(1,p),c(2,p),'ks')
text(c(1,p) + off,c(2,p) + off,names(p))

%% Bachmann, Cantor, Pelosi, Hinojosa

p = [386 387 170 7];
plot(c(1,p),c(2,p),'ks')
text(c(1,p) + off,c(2,p) + off,names(p))

%% Paul, Kucinich

p = [93 382];
plot(c(1,p),c(2,p),'ks')
text(c(1,p) + off,c(2,p) + off,names(p))

%% Houston representatives

p = [190, 12, 144, 77, 131, 372, 47, 77]; 
plot(c(1,p),c(2,p),'ks')

%% Austin representatives
p = [131, 353, 359, 266, 303]; 
plot(c(1,p),c(2,p),'ks')
