In [1]:
using Plots,LaTeXStrings
default(markersize=3,linewidth=1.5)
using LightGraphs,GraphPlot
using Images,TestImages
using DataFrames,JLD
using LinearAlgebra
include("FNC.jl");

Example 7.1.4

Here is the adjacency matrix of a "small-world" network on 200 nodes. Each node is connected to 4 neighbors, and then some edges are randomly changed to distant connections.


In [2]:
g = watts_strogatz(200,4,0.06);
gplot(g)


Out[2]:

The adjacency matrix for this graph reveals the connections as mostly local (i.e., the nonzeros are near the diagonal).


In [3]:
A = adjacency_matrix(g,Float64);
spy(A,m=2,color=:blues,title="Adjacency matrix",leg=:none)


Out[3]:
50 100 150 200 50 100 150 200 Adjacency matrix

Example 7.1.5

We will use the Images package for working with images. We also load here the TestImages package for a large library of well-known standard images.


In [4]:
img = testimage("peppers")


Out[4]:

The details vary by image type, but for the most part an image is an array of color values.


In [5]:
@show size(img),eltype(img)


(size(img), eltype(img)) = ((512, 512), RGB{Normed{UInt8,8}})
Out[5]:
((512, 512), RGB{Normed{UInt8,8}})

The elements here have four values, for red, green, blue, and alpha (opacity). We can convert each of those "planes" into an ordinary matrix.


In [6]:
R = red.(img)
R[1:5,1:5]


Out[6]:
5×5 Array{N0f8,2} with eltype Normed{UInt8,8}:
 0.396  0.549  0.592  0.627  0.631
 0.482  0.749  0.745  0.729  0.714
 0.494  0.725  0.725  0.722  0.702
 0.482  0.733  0.729  0.718  0.729
 0.498  0.733  0.714  0.757  0.722

The values above go from zero (no red) to one (full red). It may also be convenient to convert the image to grayscale, which has just one "layer" from zero (black) to one (white).


In [7]:
G = Gray.(img)


Out[7]:

In [8]:
A = @. gray(Gray(img))
A[1:5,1:5]


Out[8]:
5×5 Array{N0f8,2} with eltype Normed{UInt8,8}:
 0.118  0.227  0.243  0.251  0.255
 0.145  0.467  0.447  0.451  0.451
 0.149  0.475  0.451  0.427  0.439
 0.145  0.424  0.475  0.427  0.447
 0.149  0.451  0.486  0.463  0.431

Finally, we can save an image locally for reloading later.


In [9]:
save("peppers.png",Gray.(img))

In [10]:
load("peppers.png")


Out[10]:

Example 7.2.1

The eigvals command will return just the eigenvalues, as a vector.


In [11]:
A = pi*ones(2,2)
lambda = eigvals(A)


Out[11]:
2-element Array{Float64,1}:
 0.0              
 6.283185307179586

If we also want the eigenvectors (returned as the matrix $V$), we use eigen.


In [12]:
lambda,V = eigen(A)


Out[12]:
Eigen{Float64,Float64,Array{Float64,2},Array{Float64,1}}
eigenvalues:
2-element Array{Float64,1}:
 0.0              
 6.283185307179586
eigenvectors:
2×2 Array{Float64,2}:
 -0.707107  0.707107
  0.707107  0.707107

We can check the fact that this is an EVD.


In [13]:
D = diagm(0=>lambda)
opnorm( A - V*D/V )      # "/V" is like "*inv(V)""


Out[13]:
8.881784197001252e-16

Even if the matrix is not diagonalizable, eigen will run successfully, but the matrix ${V}$ will not be invertible.


In [14]:
lambda,V = eigen([1 1;0 1])
@show rank(V);


rank(V) = 1

Example 7.2.2

We will confirm the Bauer-Fike theorem on a triangular matrix. These tend to be far from normal.


In [15]:
n = 15;
lambda = 1:n
A = triu( ones(n)*lambda' );
A[1:5,1:5]


Out[15]:
5×5 Array{Float64,2}:
 1.0  2.0  3.0  4.0  5.0
 0.0  2.0  3.0  4.0  5.0
 0.0  0.0  3.0  4.0  5.0
 0.0  0.0  0.0  4.0  5.0
 0.0  0.0  0.0  0.0  5.0

The Bauer-Fike theorem provides an upper bound on the condition number of these eigenvalues.


In [16]:
lambda,V = eigen(A)
@show cond(V);


cond(V) = 7.197767264538044e7

The theorem suggests that eigenvalue changes may be up to 7 orders of magnitude larger than a perturbation to the matrix. A few random experiments show that effects of nearly that size are not hard to observe.


In [17]:
for k = 1:3
    E = randn(n,n);  E = 1e-7*E/opnorm(E);
    mu = eigvals(A+E)
    @show max_change = norm( sort(mu)-lambda, Inf )
end


max_change = norm(sort(mu) - lambda, Inf) = 0.01771360024608981
max_change = norm(sort(mu) - lambda, Inf) = 0.03205041336169323
max_change = norm(sort(mu) - lambda, Inf) = 0.10970713327344228

Example 7.2.3

Let's start with a known set of eigenvalues and an orthogonal eigenvector basis.


In [18]:
D = diagm(0=>[-6,-1,2,4,5])
V,R = qr(randn(5,5))
A = V*D*V';    # note that V' = inv(V)

Now we will take the QR factorization and just reverse the factors.


In [19]:
Q,R = qr(A)
A = R*Q;

It turns out that this is a similarity transformation, so the eigenvalues are unchanged.


In [20]:
sort( eigvals(A) )


Out[20]:
5-element Array{Float64,1}:
 -6.000000000000003 
 -1.0000000000000013
  2.000000000000003 
  4.000000000000004 
  5.000000000000002 

What's remarkable is that if we repeat the transformation many times, the process converges to $D$.


In [21]:
for k = 1:40
    Q,R = qr(A)
    A = R*Q
end
A


Out[21]:
5×5 Array{Float64,2}:
 -6.0          -0.00022496    2.6118e-7    3.52573e-16   1.56401e-15
 -0.00022496    5.0          -0.000151367  4.08687e-16   4.48604e-19
  2.6118e-7    -0.000151367   4.0          8.68251e-13   2.30643e-15
  2.84522e-19  -5.08551e-18   8.69585e-13  2.0           2.73548e-12
  1.42067e-31   3.07636e-28  -5.8895e-25   2.73615e-12  -1.0        

Example 7.3.2

We verify some of the fundamental SVD properties using the built-in Julia command svd.


In [22]:
A = [i^j for i=1:5, j=0:3]


Out[22]:
5×4 Array{Int64,2}:
 1  1   1    1
 1  2   4    8
 1  3   9   27
 1  4  16   64
 1  5  25  125

In [23]:
U,sigma,V = svd(A);

Note that while the "full" SVD has a square $U$, the "thin" form is the default. Here the columns are orthonormal even though $U$ is not square.


In [24]:
@show size(U),opnorm(U'*U - I);


(size(U), opnorm(U' * U - I)) = ((5, 4), 1.3502036581898522e-15)

In [25]:
@show size(V),opnorm(V'*V - I);


(size(V), opnorm(V' * V - I)) = ((4, 4), 7.993056481202561e-16)

In [26]:
sigma


Out[26]:
4-element Array{Float64,1}:
 146.69715365883005   
   5.7385697809536955 
   0.9998486640841036 
   0.11928082685242103

In [27]:
@show opnorm(A),sigma[1];


(opnorm(A), sigma[1]) = (146.69715365883005, 146.69715365883005)

In [28]:
@show cond(A), sigma[1]/sigma[end];


(cond(A), sigma[1] / sigma[end]) = (1229.8468876337488, 1229.8468876337483)

Example 7.4.1

The following matrix is not hermitian.


In [29]:
A = [0 2; -2 0]


Out[29]:
2×2 Array{Int64,2}:
  0  2
 -2  0

It has an eigenvalue decomposition with a unitary matrix of eigenvectors, though, so it is normal.


In [30]:
lambda,V = eigen(A)
opnorm( V'*V - I )


Out[30]:
2.220446049250313e-16

The eigenvalues are pure imaginary.


In [31]:
lambda


Out[31]:
2-element Array{Complex{Float64},1}:
 0.0 + 2.0000000000000004im
 0.0 - 2.0000000000000004im

The singular values are the complex magnitudes of the eigenvalues.


In [32]:
svdvals(A)


Out[32]:
2-element Array{Float64,1}:
 2.0
 2.0

Example 7.4.2

We construct a real symmetric matrix with known eigenvalues by using the QR factorization to produce a random orthogonal set of eigenvectors.


In [33]:
n = 30;
lambda = 1:n 

D = diagm(0=>lambda)
V,R = qr(randn(n,n))   # get a random orthogonal V
A = V*D*V';

The condition number of these eigenvalues is one. Thus the effect on them is bounded by the norm of the perturbation to $A$.


In [34]:
for k = 1:3
    E = randn(n,n); E = 1e-4*E/opnorm(E);
    mu = sort(eigvals(A+E))
    @show max_change = norm(mu-lambda,Inf)
end


max_change = norm(mu - lambda, Inf) = 1.7141487264282773e-5
max_change = norm(mu - lambda, Inf) = 2.0353933102512656e-5
max_change = norm(mu - lambda, Inf) = 2.3232168391729147e-5

Example 7.4.3

We construct a symmetric matrix with a known EVD.


In [35]:
n = 20;
lambda = 1:n 

D = diagm(0=>lambda)
V,R = qr(randn(n,n))   # get a random orthogonal V
A = V*D*V';

The Rayleigh quotient of an eigenvector is its eigenvalue.


In [36]:
R = x -> (x'*A*x)/(x'*x);
R(V[:,7])


Out[36]:
6.999999999999997

The Rayleigh quotient's value is much closer to an eigenvalue than its input is to an eigenvector. In this experiment, each additional digit of accuracy in the eigenvector estimate gives two more digits to the eigenvalue estimate.


In [37]:
delta = @. 1 ./10^(1:4)
quotient = zeros(size(delta))
for (k,delta) = enumerate(delta)
    e = randn(n);  e = delta*e/norm(e);
    x = V[:,7] + e
    quotient[k] = R(x)
end
DataFrame(perturbation=delta,RQminus7=quotient.-7)


Out[37]:

4 rows × 2 columns

perturbationRQminus7
Float64Float64
10.10.0137426
20.010.00026109
30.0012.6434e-6
40.00012.236e-8

Example 7.5.1

We make an image from some text, then reload it as a matrix.


In [38]:
plot([],[],leg=:none,annotations=(0.5,0.5,text("Hello world",44,:center,:middle)),
    grid=:none,frame=:none)


Out[38]:
Hello world

In [39]:
savefig("hello.png")
img = load("hello.png")
A = @. Float64(Gray(img));
@show m,n = size(A);


(m, n) = size(A) = (400, 600)

Next we show that the singular values decrease exponentially, until they reach zero (more precisely, are about $\sigma_1 \varepsilon_\text{mach}$). For all numerical purposes, this determines the rank of the matrix.


In [40]:
U,sigma,V = svd(A)
scatter(sigma,
    title="Singular values",xaxis=(L"i"), yaxis=(:log10,L"\sigma_i"),leg=:none )


Out[40]:
0 100 200 300 400 10 - 12.5 10 - 10.0 10 - 7.5 10 - 5.0 10 - 2.5 10 0.0 10 2.5 Singular values

In [41]:
r = findlast(@.sigma/sigma[1] > 10*eps())


Out[41]:
41

The rapid decrease suggests that we can get fairly good low-rank approximations.


In [42]:
Ak = [ U[:,1:k]*diagm(0=>sigma[1:k])*V[:,1:k]' for k=2*(1:4) ]
reshape( [ @.Gray(Ak[i]) for i=1:4 ],2,2)


Out[42]:

Consider how little data is needed to reconstruct these images. For rank 8, for instance, we have 8 left and right singular vectors plus 8 singular values, for a compression ratio of better than 25:1.


In [43]:
compression = 8*(m+n+1) / (m*n)


Out[43]:
0.03336666666666667

Example 7.5.2

This matrix describes the votes on bills in the 111th session of the United States Senate. (The data set was obtained from voteview.com.) Each row is one senator and each column is a vote item.


In [44]:
vars = load("voting.jld")
A = vars["A"]
m,n = size(A)


Out[44]:
(112, 696)

If we visualize the votes (white is "yea," black is "nay," and gray is anything else), we can see great similarity between many rows, reflecting party unity.


In [45]:
heatmap(A,color=:viridis,
    title="Votes in 111th U.S. Senate",xlabel="bill",ylabel="senator")


Out[45]:
100 200 300 400 500 600 20 40 60 80 100 Votes in 111th U.S. Senate bill senator - 1.00 - 0.75 - 0.50 - 0.25 0 0.25 0.50 0.75 1.00

We use singular value "energy" to quantify the decay rate of the values.


In [46]:
U,sigma,V = svd(A)
tau = cumsum(sigma.^2) / sum(sigma.^2)
scatter(tau[1:16],label="",
    xaxis=("k"), yaxis=(L"\tau_k"), title="Fraction of singular value energy")


Out[46]:
3 6 9 12 15 0.60 0.65 0.70 0.75 0.80 0.85 Fraction of singular value energy k

The first and second singular triples contain about 58% and 17% respectively of the energy of the matrix. All others have far less effect, suggesting that the information is primarily two-dimensional. The first left and right singular vectors also contain interesting structure.


In [47]:
scatter( U[:,1],label="",layout=(1,2),
    xlabel="senator" ,title="left singular vector")
scatter!( V[:,1],label="",subplot=2,
    xlabel="bill",title="right singular vector")


Out[47]:
0 25 50 75 100 -0.10 -0.05 0.00 0.05 0.10 left singular vector senator 0 200 400 600 -0.04 -0.02 0.00 0.02 0.04 right singular vector bill

Both vectors have values greatly clustered near $\pm C$ for a constant $C$. These can be roughly interpreted as how partisan a particular senator or bill was, and for which political party. Projecting the senators' vectors into the first two $\V$-coordinates gives a particularly nice way to reduce them to two dimensions. Political scientists label these dimensions "partisanship" and "bipartisanship." Here we color them by actual party affiliation (also given in the data file): red for Republican, blue for Democrat, and yellow for independent.


In [48]:
x1 = A*V[:,1];   x2 = A*V[:,2];

Rep = vec(vars["Rep"]); Dem = vec(vars["Dem"]);  Ind = vec(vars["Ind"]);
scatter(x1[Dem],x2[Dem],color=:blue,label="D",
    xaxis=("partisanship"),yaxis=("bipartisanship"),title="111th US Senate in 2D" )
scatter!(x1[Rep],x2[Rep],color=:red,label="R")
scatter!(x1[Ind],x2[Ind],color=:yellow,label="I")


Out[48]:
-20 -10 0 10 20 -15 -10 -5 0 111th US Senate in 2D partisanship bipartisanship D R I