Skip to content

Commit 623d5a5

Browse files
committed
Merge pull request #1 from vtjnash/jn
Code from Alan's RMT book
2 parents 1145dd9 + ca6d488 commit 623d5a5

32 files changed

+988
-38
lines changed

demos/book/1/bellcurve.jl

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,25 @@
66
#Theory: The normal distribution curve
77

88
## Experiment
9-
t=1000000
10-
dx=.2
11-
v=randn(t)
12-
grid=[-4:dx:4]
13-
count=hist(v,grid)/(t*dx)
9+
t = 1000000
10+
dx = .2
11+
v = randn(t)
12+
x = -4:dx:4
13+
grid,count = hist(v,x)
1414

1515
## Theory
16-
x=grid
17-
y=exp(-x.^2/2)/sqrt(2*pi)
16+
y = exp(-x.^2/2)/sqrt(2*pi)
1817

1918
## Plot
2019
using Winston
2120
p = FramedPlot()
22-
h = Histogram(count, dx)
23-
h.x0 = grid[1]
21+
h = Histogram(count/(t*dx), step(grid))
22+
h.x0 = first(grid)
2423
add(p, h)
2524
add(p, Curve(x, y, "color", "blue", "linewidth", 2))
26-
file(p, "bellcurve.png")
25+
if isinteractive()
26+
Winston.display(p)
27+
else
28+
file(p, "bellcurve.png")
29+
end
30+

demos/book/1/largeeig.jl

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,36 @@ include("tracywidom.jl")
1010
## Parameters
1111
n=100 # matrix size
1212
t=5000 # trials
13-
v=[] # eigenvalue samples
1413
dx=.2 # binsize
15-
grid=[-5:dx:2]
16-
## Experiment
17-
for i=1:t
18-
a=randn(n,n)+im*randn(n,n) # random nxn complex matrix
19-
s=(a+a')/2 # Hermitian matrix
20-
v=[v; max(eig(s)[1])] # Largest Eigenvalue
21-
end
22-
v=n^(1/6)*(v-2*sqrt(n)) # normalized eigenvalues
14+
function largeeig_experiment(n,t,dx)
15+
## Experiment
16+
v=Float64[] # eigenvalue samples
17+
for i=1:t
18+
a=randn(n,n)+im*randn(n,n) # random nxn complex matrix
19+
s=(a+a')/2 # Hermitian matrix
20+
push!(v,max(eig(s)[1])) # Largest Eigenvalue
21+
end
22+
v=n^(1/6)*(v-2*sqrt(n)) # normalized eigenvalues
2323

24-
count=hist(v,grid)/(t*dx)
24+
grid=-5:dx:2
25+
count=hist(v,grid)[2]/(t*dx)
26+
(grid, count)
27+
end
28+
(grid, count) = largeeig_experiment(n,t,dx)
2529

2630
## Theory
2731
(t, f) = tracywidom(5., -8.)
2832

2933
## Plot
3034
using Winston
3135
p = FramedPlot()
32-
h = Histogram(count, dx)
33-
h.x0 = grid[1]
36+
h = Histogram(count, step(grid))
37+
h.x0 = first(grid)
3438
add(p, h)
35-
add(p, Curve(t+5, f, "linewidth", 2, "color", "blue"))
36-
file(p, "largeeig.png")
39+
add(p, Curve(t, f, "linewidth", 2, "color", "blue"))
40+
if isinteractive()
41+
Winston.display(p)
42+
else
43+
file(p, "largeeig.png")
44+
end
3745

demos/book/1/semicircle.jl

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,39 @@
44
#Experiment: Sample random symmetric Gaussian matrices
55
#Plot: Histogram of the eigenvalues
66
#Theory: Semicircle as n->infinity
7+
#See also: RandomMatrices.SemiCircle Distribution
8+
79
## Parameters
810
n=1000 # matrix size
911
t=10 # trials
10-
v=[] # eigenvalue samples
1112
dx=.2 # binsize
12-
grid=[-2.4:dx:2.4]
13-
## Experiment
14-
for i = 1:t
15-
a=randn(n,n) # n by n matrix of random Gaussians
16-
s=(a+a')/sqrt(2*n)# symmetrize and normalize matrix
17-
v=[v;eig(s)[1]] # eigenvalues
13+
function semicircle_experiment(n,t,dx)
14+
## Experiment
15+
v=Float64[] # eigenvalue samples
16+
for i = 1:t
17+
a=randn(n,n) # n by n matrix of random Gaussians
18+
s=(a+a')/sqrt(2*n)# symmetrize and normalize matrix
19+
append!(v,eig(s)[1])# eigenvalues
20+
end
21+
grid=-2.4:dx:2.4
22+
count=hist(v,grid)[2]/(n*t*dx)
23+
(grid, count)
1824
end
19-
count=hist(grid)/(n*t*dx)
25+
grid,count = semicircle_experiment(n,t,dx)
26+
2027
## Theory
2128
x=[-2:dx:2]
2229
y=sqrt(4-x.^2)/(2*pi)
2330

2431
## Plot
2532
using Winston
2633
p = FramedPlot()
27-
h = Histogram(count, dx)
28-
h.x0 = grid[1]
34+
h = Histogram(count, step(grid))
35+
h.x0 = first(grid)
2936
add(p, h)
30-
add(p, Curve(x+2.4+dx/2, y, "color", "blue", "linewidth", 2))
31-
file(p, "semicircle.png")
37+
add(p, Curve(x, y, "color", "blue", "linewidth", 2))
38+
if isinteractive()
39+
Winston.display(p)
40+
else
41+
file(p, "semicircle.png")
42+
end

demos/book/1/tracywidom.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#Algorithm 1.3 of Random Eigenvalues by Alan Edelman
33

44
#Theory: Compute and plot the Tracy-Widom distribution
5+
#See also: RandomMatrices.TracyWidom(t) for tn=-8
56

67
##Parameters
78
t0=5. # right endpoint
@@ -28,5 +29,9 @@ end
2829
using Winston
2930
p = FramedPlot()
3031
add(p, Curve(t, f2, "linewidth", 2))
31-
file(p, "tracywidom.png")
32+
if isinteractive()
33+
Winston.display(p)
34+
else
35+
file(p, "tracywidom.png")
36+
end
3237

demos/book/2/bellcurve2.jl

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#bellcurve2.jl
2+
#Algorithm 2.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+
9+
## Experiment
10+
t = 10000 # Trials
11+
dx = 0.25 # binsize
12+
v = randn(t,2) # samples
13+
x = -4:dx:4 # range
14+
15+
## Plot
16+
using Winston
17+
module Plot3D
18+
using Winston
19+
# Warning: the 3D interface is very new at the time
20+
# of writing this and is likely to not be available
21+
# and/or have changed
22+
if Winston.output_surface == :gtk
23+
include(Pkg.dir("Winston","src","canvas3d_gtk.jl"))
24+
else
25+
include(Pkg.dir("Winston","src","canvas3d.jl"))
26+
end
27+
end
28+
edg1, edg2, count = hist2(v, x-dx/2)
29+
#cntrs = ((first(x)+dx/2), (last(x)-dx/2), length(x)-1)
30+
#x,y = meshgrid(cntrs, cntrs)
31+
cntrs = (first(x)+dx/2):dx:(last(x)-dx/2)
32+
x = Float64[x for x = cntrs, y = cntrs]
33+
y = Float64[y for x = cntrs, y = cntrs]
34+
Plot3D.plot3d(Plot3D.surf(x,count./(t*dx^2) *50,y))
35+
Plot3D.plot3d(Plot3D.surf(x,exp(-(x.^2+y.^2)/2)/(2*pi) *50,y))

demos/book/2/haar.jl

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#haar.jl
2+
#Algorithm 2.4 of Random Eigenvalues by Alan Edelman
3+
4+
#Experiment: Generate random orthogonal/unitary matrices
5+
#Plot: Histogram eigenvalues
6+
#Theory: Eigenvalues are on unit circle
7+
8+
## Parameters
9+
t = 5000 # trials
10+
dx = 0.05 # binsize
11+
n = 10 # matrix size
12+
13+
function haar_experiment(t,dx,n)
14+
v = Complex{Float64}[] # eigenvalue samples
15+
for i = 1:t
16+
# Sample random orthogonal matrix
17+
# X = QR(randn(n,n))[:Q]
18+
# If you have non-uniformly sampled eigenvalues, you may need this fix:
19+
#X = X*diagm(sign(randn(n,1)))
20+
#
21+
# Sample random unitary matrix
22+
X = QR(randn(n,n)+im*randn(n,n))[:Q]
23+
# If you have non-uniformly sampled eigenvalues, you may need this fix:
24+
#X = X*diagm(sign(randn(n)+im*randn(n)))
25+
append!(v, eigvals(full(X)))
26+
end
27+
x = ((-1+dx/2):dx:(1+dx/2))*pi
28+
x, v
29+
end
30+
x, v = haar_experiment(t,dx,n)
31+
grid, count = hist(angle(v), x)
32+
33+
## Theory
34+
h2 = t*n*dx/2*x.^0
35+
36+
## Plot
37+
using Winston
38+
p = FramedPlot()
39+
h = Histogram(count, step(grid))
40+
h.x0 = first(grid)
41+
add(p, h)
42+
add(p, Curve(x, h2, "color", "blue", "linewidth", 2))
43+
if isinteractive()
44+
Winston.display(p)
45+
else
46+
file(p, "haar.png")
47+
end

demos/book/2/hist2d.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#hist2d.jl
2+
#Algorithm 2.2 of Random Eigenvalues by Alan Edelman
3+
4+
#see Base.hist2 for a simple implementation

demos/book/2/patiencesort.jl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#patiencesort.jl
2+
#Algorithm 2.5 of Random Eigenvalues by Alan Edelman
3+
4+
function patiencesort(p)
5+
piles = similar(p, 0)
6+
for pi in p
7+
idx = 1
8+
for pp in piles
9+
if pi > pp
10+
idx += 1
11+
end
12+
end
13+
if idx > length(piles)
14+
d = length(piles)
15+
resize!(piles, idx)
16+
piles[d+1:end] = 0
17+
end
18+
piles[idx] = pi
19+
end
20+
return length(piles)
21+
end

demos/book/2/spherecomponent.jl

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#spherecomponent.jl
2+
#Algorithm 2.3 of Random Eigenvalues by Alan Edelman
3+
4+
#Experiment: Random components of a vector on a sphere
5+
#Plot: Histogram of a single component
6+
#Theory: Beta distribution
7+
8+
## Parameters
9+
t = 100000 # trials
10+
n = 12 # dimensions of sphere
11+
dx = 0.1 # bin size
12+
13+
## Experiment
14+
v = randn(n, t)
15+
v = (v[1,:] ./ sqrt(sum(v.^2,1)))'
16+
grid, count = hist(v, -1:dx:1)
17+
18+
## Theory
19+
x = grid
20+
y = (gamma(n-1)/(2^(n-2)*gamma((n-1)/2)^2))*(1-x.^2).^((n-3)/2)
21+
22+
## Plot
23+
using Winston
24+
p = FramedPlot()
25+
h = Histogram(count/(t*dx), step(grid))
26+
h.x0 = first(grid)
27+
add(p, h)
28+
add(p, Curve(x, y, "color", "blue", "linewidth", 2))
29+
if isinteractive()
30+
Winston.display(p)
31+
else
32+
file(p, "spherecomponent.png")
33+
end

demos/book/2/tracywidomlis.jl

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#tracywidomlis.jl
2+
#Algorithm 2.7 of Random Eigenvalues by Alan Edelman
3+
4+
#Experiment: Sample random permutations
5+
#Plot: Histogram of lengths of longest increasing subsequences
6+
#Theory: The Tracy-Widom law
7+
8+
## Parameters
9+
t = 10000 # number of trials
10+
n = 6^6 # length of permutations
11+
dx = 1/6 # bin size
12+
13+
## Experiment
14+
require("patiencesort.jl")
15+
function tracywidomlis(t, n, dx)
16+
## single processor: 1x speed
17+
#v = [patiencesort(randperm(n)) for i = 1:t]
18+
19+
## simple parallelism: Nx speed * 130%
20+
v = pmap((i)->patiencesort(randperm(n)), 1:t)
21+
22+
## maximum parallelism: Nx speed
23+
#grouped = floor(t/(nprocs()-2))
24+
#println(grouped)
25+
#v = vcat(pmap((i)->[patiencesort(randperm(n)) for j = 1:grouped], 1:t/grouped)...)
26+
27+
w = (v-2sqrt(n))/n^(1/6)
28+
return hist(w, -5:dx:2)
29+
end
30+
@time grid, count = tracywidomlis(t, n, dx)
31+
32+
## Plot
33+
using Winston
34+
p = FramedPlot()
35+
h = Histogram(count/(t*dx), step(grid))
36+
h.x0 = first(grid)
37+
include("../1/tracywidom.jl")
38+
add(p, h)
39+
if isinteractive()
40+
Winston.display(p)
41+
else
42+
file(p, "tracywidomlis.png")
43+
end

0 commit comments

Comments
 (0)