Assignment 6 Test Scripts

 

Part A

%A6A

% Generate a random protein

n = 100; t = randn(n,1); x = cos(t); y = sin(t); z = 10*t; A = [x y z]';

% Generate a random rotation and translation vector and move A accordinly
c1 = rand ; s1 = sqrt(1-c1^2); Q1 = [ c1 s1 0 ; -s1 c1 0; 0 0 1];
c2 = rand ; s2 = sqrt(1-c2^2); Q2 = [ c2 0 s2 ; 0 1 0; -s2 0 c2];
c3 = rand ; s3 = sqrt(1-c3^2); Q3 = [ 1 0 0 ; 0 c3 s3; 0 -s3 c3];
Q0 = Q1*Q2*Q3;
v0 = randn(3,1);
e = ones(n,1);
B = Q0'*A - v0*e';

clc
disp('   Noise       Tol         norm(v-v0)    norm(Q - Q0)  norm(A-Q*(B+v*e''),''fro'')')
disp('---------------------------------------------------------------------------')
for noise = [0 .000001 .0001  .01]
   for tol = [.01  .0001  .000001  .0000001  ]
       [v,Q] = Match(A,B+noise*randn(3,n),tol);
       ev = norm(v-v0);
       eQ = norm(Q-Q0);
       fit = norm(A-Q*(B+v*e'),'fro');
       disp(sprintf('%10.6f  %10.8f    %10.8f      %10.8f       %10.8f ',noise,tol,ev,eQ,fit))
   end
end

Part B

% A6B
 clc
 rand('seed',7876343)
 p = .85; tol = .0001; itsMax = 20;
 
 close all 
 % Example 1
 disp('::::::::::::::::   Example 1   :::::::::::::::')
 disp(' ')
 r = [ 1 2 2 1 2 2 3 2 1 2 ];
 ShowPageRank(r,p,tol,itsMax)
 
 % Example 2 
 disp(' ')
 disp('::::::::::::::::   Example 2 :::::::::::::::::')
 disp(' ')
 r = floor(1 + 5*rand(1,500)); r(100) = 100; r(200) = 50;
 ShowPageRank(r,p,tol,itsMax)
 
 
   function ShowPageRank(r,p,tol,itsMax)
% r is a column n-vector of in-degrees
% p is a probability, usually .85
% Uses power method to determine page rank.
% tol is the power method tolerance
% itsmax is the maximumnumber of power iterations

n = length(r);
GSparse = GenWeb(r);
[pageRank,its] = PR(GSparse,p,tol,itsMax);
[z,idx] = sort(pageRank);
idx = idx(n:-1:1);
if n<=10
   G = full(GSparse) %This will display G in conventional form
end
figure
bar(pageRank)
ylabel('PageRank')
xlabel('Page Index')
title(sprintf('n = %1d',n))

outDegree  = GSparse'*ones(n,1);
inDegree   = GSparse*ones(n,1);
disp(' ')
disp('Page    Rank      In-Degree  Out-Degree')
disp('-------------------------------------------------------')
for i=1:min(n,10)
    k = idx(i);
    disp(sprintf('%3d    %7.4f     %3d        %3d',k,pageRank(k),inDegree(k),outDegree(k)))
end
disp(' ')
disp(sprintf('Tol = %6.3e    Power iterations required = %1d',tol,its))
 
 
   function Gsparse = GenWeb(r)
% r is a column n-vector of positive integers with 0 < r(i) < n.
% Gsparse is the sparse representation of a randomly generated the 
% connectivity matrix G that has the property that there are
% exactly r(i) 1's randomly spread out across its i-th row. If
% G(i,j) has the interpretation of being 1 iff there is a link
% from page j to page i, then r(i)is the in-degree of page i.

  n = length(r);
  G = zeros(n,n);
  for i = 1:n
     % idx =  a random permutation of the integers 1 thru n-1...
     [z,idx] = sort(rand(n-1,1));
     % Generate a random 0-1 vector v of length n-1 with sum(v) = r(i).
     v = zeros(1,n-1);
     v(idx(1:r(i))) = ones(1,r(i));
     G(i,:) = [v(1:i-1) 0 v(i:n-1)];
  end
  Gsparse = sparse(G);