Skip to content

Commit 224e060

Browse files
committed
rmt book chapter 2 (2.3 to 2.7)
1 parent e829d96 commit 224e060

File tree

6 files changed

+172
-8
lines changed

6 files changed

+172
-8
lines changed

demos/book/1/bellcurve.jl

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,19 @@
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)[2]/(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, step(grid))
21+
h = Histogram(count/(t*dx), step(grid))
2322
h.x0 = first(grid)
2423
add(p, h)
2524
add(p, Curve(x, y, "color", "blue", "linewidth", 2))

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/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: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
include("patiencesort.jl")
15+
function tracywidomlis(t, n, dx)
16+
v = zeros(t) # samples
17+
for i=1:t
18+
v[i] = patiencesort(randperm(n))
19+
end
20+
w = (v-2sqrt(n))/n^(1/6)
21+
return hist(w, -5:dx:2)
22+
end
23+
grid, count = tracywidomlis(t, n, dx)
24+
25+
## Plot
26+
h = Histogram(count/(t*dx), step(grid))
27+
h.x0 = first(grid)
28+
include("../1/tracywidom.jl")
29+
add(p, h)
30+
if isinteractive()
31+
Winston.display(p)
32+
else
33+
file(p, "tracywidomlis.png")
34+
end

demos/book/2/unitarylis.jl

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#unitarylis.jl
2+
#Algorithm 2.5 of Random Eigenvalues by Alan Edelman
3+
4+
#Experiment: Generate random orthogonal/unitary matrices
5+
#Theory: Counts longest increasing subsequence statistics
6+
7+
## Parameters
8+
t = 200000 # Number of trials
9+
n = 4 # permutation size
10+
k = 2 # length of longest increasing subsequence
11+
12+
include("patiencesort.jl")
13+
14+
function unitarylis(t, n, k)
15+
v = zeros(t) # samples
16+
## Experiment
17+
for i = 1:t
18+
X = QR(complex(randn(k,k), randn(k,k)))[:Q]
19+
X = X*diagm(sign(complex(randn(k),randn(k))))
20+
v[i] = abs(trace(X)) ^ (2n)
21+
end
22+
z = mean(v)
23+
c = 0
24+
for i=1:factorial(n)
25+
c = c + int(patiencesort(nthperm([1:n],i))<=k)
26+
end
27+
return (z, c)
28+
end
29+
30+
unitarylis(t, n, k)

0 commit comments

Comments
 (0)