Skip to content

Commit 0e46ada

Browse files
committed
Added code from Chapter 1 of Alan's book
1 parent d9cb4e6 commit 0e46ada

File tree

7 files changed

+147
-0
lines changed

7 files changed

+147
-0
lines changed

demos/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.png #Ignore output plots
2+

demos/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
This folder contains code snippets from the upcoming book "Random Eigenvalues" by Alan Edelman.
2+

demos/book/1/bellcurve.jl

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#bellcurve.jl
2+
#Algorithm 1.1 of Random Eigenvalues by Alan Edelman
3+
4+
#Experiment: Generate random samples from the normal distribution
5+
#Plot: Histogram of random samples
6+
#Theory: The normal distribution curve
7+
8+
## Experiment
9+
t=1000000
10+
dx=.2
11+
v=randn(t)
12+
count=hist(v,[-4-dx/2:dx:4+dx/2])
13+
count/=sum(count)*dx
14+
15+
## Theory
16+
x=[-4:dx:4]
17+
y=exp(-x.^2/2)/sqrt(2*pi)
18+
19+
## Plot
20+
#XXX Currently Histogram() must start plotting from 0.
21+
#For now, add offset to theoretical curve
22+
#Filed: https://github.com/nolta/Winston.jl/issues/28
23+
#cjh 2013-03-16
24+
using Winston
25+
p = FramedPlot()
26+
add(p, Histogram(count, dx))
27+
add(p, Curve(x+4+dx/2, y, "color", "blue", "linewidth", 2))
28+
file(p, "bellcurve.png")

demos/book/1/gradient.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#Calculate numerical derivative using:
2+
#forward first-order finite difference for first element
3+
#backward first-order finite difference for last element
4+
#central first-order finite difference for all intermediate elements
5+
function gradient(y::Vector{Float64}, x::Vector{Float64})
6+
dy = zeros(length(y))
7+
n = length(dy)
8+
dy[1] = (y[2]-y[1])/(x[2]-x[1])
9+
dy[n] = (y[n]-y[n-1])/(x[n]-x[n-1])
10+
for i=2:n-1
11+
dy[i] = (y[i+1]-y[i-1])/(x[i+1]-x[i-1])
12+
end
13+
return dy
14+
end
15+

demos/book/1/largeeig.jl

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#largeeig.png
2+
#Algorithm 1.4 of Random Eigenvalues by Alan Edelman
3+
4+
#Experiment: Largest eigenvalue of random Hermitian matrices
5+
#Plot: Histogram of the normalized largest eigenvalues
6+
#Theory: Tracy-Widom as n->infinity
7+
8+
include("tracywidom.jl")
9+
10+
## Parameters
11+
n=100 # matrix size
12+
t=5000 # trials
13+
v=[] # eigenvalue samples
14+
dx=.2 # binsize
15+
## Experiment
16+
for i=1:t
17+
a=randn(n,n)+im*randn(n,n) # random nxn complex matrix
18+
s=(a+a')/2 # Hermitian matrix
19+
v=[v; max(eig(s)[1])] # Largest Eigenvalue
20+
end
21+
v=n^(1/6)*(v-2*sqrt(n)) # normalized eigenvalues
22+
23+
count=hist(v,-5:dx:2)
24+
count/=t*dx
25+
26+
## Theory
27+
(t, f) = tracywidom(5., -8.)
28+
29+
## Plot
30+
#XXX Currently Histogram() must start plotting from 0.
31+
#For now, add offset to theoretical curve
32+
#Filed: https://github.com/nolta/Winston.jl/issues/28
33+
#cjh 2013-03-16
34+
using Winston
35+
p = FramedPlot()
36+
add(p, Histogram(count, dx))
37+
add(p, Curve(t+5, f, "linewidth", 2, "color", "blue"))
38+
file(p, "largeeig.png")
39+

demos/book/1/semicircle.jl

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#semicircle.m
2+
#Algorithm 1.2 of Random Eigenvalues by Alan Edelman
3+
4+
#Experiment: Sample random symmetric Gaussian matrices
5+
#Plot: Histogram of the eigenvalues
6+
#Theory: Semicircle as n->infinity
7+
## Parameters
8+
n=1000 # matrix size
9+
t=10 # trials
10+
v=[] # eigenvalue samples
11+
dx=.2 # binsize
12+
## Experiment
13+
for i = 1:t
14+
a=randn(n,n) # n by n matrix of random Gaussians
15+
s=(a+a')/sqrt(2*n)# symmetrize and normalize matrix
16+
v=[v;eig(s)[1]] # eigenvalues
17+
end
18+
count=hist(v,-2.4:dx:2.4)
19+
count/=n*t*dx
20+
## Theory
21+
x=[-2:dx:2]
22+
y=sqrt(4-x.^2)/(2*pi)
23+
24+
## Plot
25+
using Winston
26+
p = FramedPlot()
27+
add(p, Histogram(count, dx))
28+
add(p, Curve(x+2.4+dx/2, y, "color", "blue", "linewidth", 2))
29+
file(p, "semicircle.png")

demos/book/1/tracywidom.jl

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#tracywidom.m
2+
#Algorithm 1.3 of Random Eigenvalues by Alan Edelman
3+
4+
#Theory: Compute and plot the Tracy-Widom distribution
5+
6+
##Parameters
7+
t0=5. # right endpoint
8+
tn=-8. # left endpoint
9+
10+
##Theory: The differential equation solver
11+
#Compute Tracy-Widom distribution
12+
using ODE
13+
include("gradient.jl")
14+
function tracywidom(t0::Float64, tn::Float64)
15+
function deq(t::Float64, y::Vector{Float64})
16+
yout = [y[2]; t*y[1]+2y[1]^3; y[4]; y[1]^2]
17+
end
18+
19+
y0=[airy(t0); airy(1, t0); 0; airy(t0)^2] # Initial conditions
20+
(t, y)=ode23(deq, [t0, tn], y0) # Solve the ODE
21+
F2=exp(-y[:,3]) # the distribution
22+
f2=gradient(F2, t) # the density
23+
return (t, f2)
24+
end
25+
(t, f2) = tracywidom(t0, tn)
26+
27+
## Plot
28+
using Winston
29+
p = FramedPlot()
30+
add(p, Curve(t, f2, "linewidth", 2))
31+
file(p, "tracywidom.png")
32+

0 commit comments

Comments
 (0)