Skip to content

Commit 8d6e890

Browse files
committed
More code cleanup
- Fixed normalization bug in rand(GaussianHermite, .) - renamed UniformHaar to Haar and added optional GSL wrappers to Haar code. - moved LargestEigenvalue.jl code to densities/TracyWidom.jl - added new abstract StochasticProcess type
1 parent 623d5a5 commit 8d6e890

File tree

8 files changed

+79
-35
lines changed

8 files changed

+79
-35
lines changed

src/GaussianEnsembles.jl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
# Alan Edelman, Per-Olof Persson and Brian D Sutton, "The fourfold way"
1414
# http://www-math.mit.edu/~edelman/homepage/papers/ffw.pdf
1515

16-
export Wigner, GaussianHermite, GaussianLaguerre, GaussianJacobi
16+
export GaussianHermite, GaussianLaguerre, GaussianJacobi,
17+
Wigner, Wishart, MANOVA #Synonyms for Hermite, Laguerre and Jacobi
1718

1819
#A convenience function to define a chi scalar random variable
1920
chi(df::Real) = sqrt(rand(Chisq(df)))
@@ -25,7 +26,7 @@ chi(df::Real) = sqrt(rand(Chisq(df)))
2526
type GaussianHermite <: ContinuousMatrixDistribution
2627
beta::Real
2728
end
28-
typealias Wigner GaussianHermite
29+
typealias Wigner GaussianHermite
2930

3031
# Generates a NxN symmetric Wigner matrix
3132
function rand(d::GaussianHermite, n::Integer)
@@ -41,7 +42,7 @@ function rand(d::GaussianHermite, n::Integer)
4142
else
4243
error(@sprintf("beta = %d is not implemented", d.beta))
4344
end
44-
normalization = sqrt(2*d.beta)
45+
normalization = sqrt(2*d.beta*n)
4546
return (A + A') / normalization
4647
end
4748

@@ -63,7 +64,6 @@ tridrand(d::GaussianHermite, dims::Dim2) = dims[1]==dims[2] ? tridrand(d, dims[1
6364
#Return n eigenvalues distributed according to the Hermite ensemble
6465
eigvalrand(d::GaussianHermite, n::Integer) = eigvals(tridrand(d, b))
6566

66-
6767
#Calculate Vandermonde determinant term
6868
function VandermondeDeterminant{Eigenvalue<:Number}(lambda::Vector{Eigenvalue}, beta::Real)
6969
n = length(lambda)
@@ -83,10 +83,7 @@ function eigvaljpdf{Eigenvalue<:Number}(d::GaussianHermite, lambda::Vector{Eigen
8383
for j=1:n
8484
c *= gamma(1 + beta/2)/gamma(1 + beta*j/2)
8585
end
86-
87-
#Calculate argument of exponential
88-
Energy = sum(lambda.^2/2)
89-
86+
Energy = sum(lambda.^2/2) #Calculate argument of exponential
9087
VandermondeDeterminant(lambda, beta) * exp(-Energy)
9188
end
9289

@@ -100,6 +97,8 @@ type GaussianLaguerre <: ContinuousMatrixDistribution
10097
beta::Real
10198
a::Real
10299
end
100+
typealias Wishart GaussianLaguerre
101+
103102

104103
#Generates a NxN Hermitian Wishart matrix
105104
#TODO Check - the eigenvalue distribution looks funky
@@ -172,6 +171,7 @@ type GaussianJacobi <: ContinuousMatrixDistribution
172171
a::Real
173172
b::Real
174173
end
174+
typealias MANOVA GaussianJacobi
175175

176176
function rand(d::GaussianJacobi, m::Integer)
177177
w1 = Wishart(m, int(2.0*d.a/d.beta), d.beta)

src/Haar.jl

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
using GSL
21
using Catalan
32

4-
export permutations_in_Sn, compose, cycle_structure, data, part #Functions working with partitions and permutations
5-
UniformHaar, expectation, WeingartenUnitary
3+
export permutations_in_Sn, compose, cycle_structure, data, part, #Functions working with partitions and permutations
4+
partition, Haar, expectation, WeingartenUnitary
65

6+
7+
typealias partition Vector{Int}
78
#Functions working with partitions and permutations
89
# TODO Migrate these functions to Catalan
910

@@ -31,6 +32,9 @@ function permutations_in_Sn(n::Integer)
3132
end
3233
end
3334

35+
36+
if _HAVE_GSL
37+
3438
function compose(P::Ptr{gsl_permutation}, Q::Ptr{gsl_permutation})
3539
#Compose the permutations
3640
n=convert(Int64, permutation_size(P))
@@ -64,8 +68,10 @@ end
6468
data(P::Ptr{gsl_permutation}) = [convert(Int64, x)+1 for x in
6569
pointer_to_array(permutation_data(P), (convert(Int64, permutation_size(P)) ,))]
6670

71+
end #_HAVE_GSL
72+
6773

68-
type UniformHaar <: AbstractMatrix
74+
type Haar <: ContinuousMatrixDistribution
6975
beta::Real
7076
end
7177

@@ -105,20 +111,20 @@ function expectation(X::Expr)
105111
for i=1:n
106112
thingy=X.args[i+1]
107113
if isa(thingy, Symbol)
108-
if isa(eval(thingy), UniformHaar)
114+
if isa(eval(thingy), Haar)
109115
if MyQ==None MyQ=thingy end
110116
if MyQ == thingy
111117
Qidx=[Qidx; i]
112118
else
113-
warning("only one instance of UniformHaar supported, skipping the other guy ", thingy) end
119+
warning("only one instance of Haar supported, skipping the other guy ", thingy) end
114120
else
115121
Others = [Others; (thingy, i, i+1)]
116122
end
117123
println(i, ' ', thingy, "::", typeof(eval(thingy)))
118124
elseif isa(thingy, Expr)
119125
println(i, ' ', thingy, "::Expr")
120126
if thingy.head==symbol('\'') && length(thingy.args)>=1 #Maybe this is a Q'
121-
if isa(thingy.args[1], Symbol) && isa(eval(thingy.args[1]), UniformHaar)
127+
if isa(thingy.args[1], Symbol) && isa(eval(thingy.args[1]), Haar)
122128
println("Here is a Qtranspose")
123129
Qpidx=[Qpidx; i]
124130
end
@@ -264,11 +270,16 @@ function expectation(X::Expr)
264270
eval(X)
265271
end
266272

273+
if _HAVE_GSL
274+
267275
#Computes the Weingarten function for permutations
268276
function WeingartenUnitary(P::Ptr{gsl_permutation})
269277
C = cycle_structure(P)
270278
WeingartenUnitary(C)
271279
end
280+
281+
end #_HAVE_GSL
282+
272283
#Computes the Weingarten function for partitions
273284
function WeingartenUnitary(P::partition)
274285
n = sum(P)

src/HaarMeasure.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import Base.rand
2121
# Edelman and Rao, 2005
2222
# Mezzadri, 2006, math-ph/0609050
2323
#TODO implement O(n^2) method
24-
function rand(W::UniformHaar, doCorrection::Integer)
24+
function rand(W::Haar, doCorrection::Integer)
2525
n, beta = W.N, W.beta
2626
M=rand(Ginibre(n, beta))
2727
q,r=qr(M)
@@ -54,7 +54,7 @@ end
5454
#By default, always do piecewise correction
5555
#For most applications where you use the HaarMatrix as a similarity transform
5656
#it doesn't matter, but better safe than sorry... let the user choose else
57-
rand(W::UniformHaar) = rand(W, 1)
57+
rand(W::Haar) = rand(W, 1)
5858

5959
#A utility method to check if the piecewise correction is needed
6060
#This checks the R part of the QR factorization; if correctly done,

src/LargestEigenvalue.jl

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/RandomMatrices.jl

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,27 @@ typealias Dim2 (Int, Int) #Dimensions of a rectangular matrix
2626
#Classical Gaussian matrix ensembles
2727
include("GaussianEnsembles.jl")
2828

29+
2930
# Classical univariate distributions
3031
include("densities/Semicircle.jl")
3132
include("densities/TracyWidom.jl")
3233

33-
#Fast histogrammer for matrix eigenvalues
34+
#Generating matrices of Haar measure
35+
include("Haar.jl")
36+
include("HaarMeasure.jl")
37+
include("HaarSymbolic.jl")
38+
39+
#Fast histogrammer for matrix eigenvalues - hist_eig
3440
include("FastHistogram.jl")
3541

3642
#Formal power series
3743
include("FormalPowerSeries.jl")
44+
45+
#Statistical tests based on random matrix theory
46+
include("StatisticalTests.jl")
47+
48+
#Stochastic processes
49+
include("StochasticProcess.jl")
50+
3851
end
3952

src/StochasticProcess.jl

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,31 @@
1+
#Matrices related to stochastic processes
2+
3+
abstract StochasticProcess
4+
export evolve
5+
6+
immutable BrownianProcess <: StochasticProcess
7+
end
8+
19
# Generates Brownian motion
2-
BrownianProcess(dx::Real, xend::Real) = cumsum(WhiteNoise(dx, xend))
10+
evolve(p::BrownianProcess, dx::Real, xend::Real) = cumsum(WhiteNoiseProcess(dx, xend))
311

412
# Generates a white noise process
513
function WhiteNoiseProcess(dx::Real, xend::Real)
6-
x=[0:dx:xend]
7-
dW=randn(length(x),1)*sqrt(dx)
14+
x=[0:dx:xend]
15+
randn(length(x),1)*sqrt(dx)
16+
end
17+
18+
################
19+
# Airy process #
20+
################
21+
22+
immutable AiryProcess <: StochasticProcess
23+
beta::Real
824
end
925

1026
# Calculates the largest eigenvalue of a stochastic Airy process
1127
# with Brownian noise
12-
function StochasticAiryProcess(dx::Real, xend::Real, beta::Real)
28+
function evolve(p::AiryProcess, dx::Real, xend::Real)
1329
x=[0:dx:xend]
1430
N=length(x)
1531

@@ -18,14 +34,14 @@ function StochasticAiryProcess(dx::Real, xend::Real, beta::Real)
1834
b=+(1/dx^2)*ones(N-1)
1935
#Plus noise
2036
dW=WhiteNoiseProcess(dx, xend)
21-
a+=(2/sqrt(beta))*dW/dx
37+
a+=(2/sqrt(p.beta))*dW/dx
2238

2339
maxeig(SymTridiagonal(a,b))
2440
end
2541

2642
#Sample program
2743
#t=10000 #number of trials
28-
#v=[StochasticAiryProcess(0.001, 10, 2) for i=1:t]
44+
#v=[AiryProcess(0.001, 10, 2) for i=1:t]
2945
#binsize=.2
3046
#grid=[-5:binsize:2]
3147
#x=hist(v, grid)

src/densities/Semicircle.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ importall Distributions
44
using Catalan
55
export Semicircle
66

7-
type Semicircle <: ContinuousUnivariateDistribution
7+
immutable Semicircle <: ContinuousUnivariateDistribution
88
mean::Float64
99
radius::Float64
1010
Semicircle(mu, r) = r > 0 ? new(float64(mu), float64(r)) :

src/densities/TracyWidom.jl

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
#Computes the Tracy-Widom distribution by directly solving the
22
#Painlevé II equation
3+
importall Distributions
34
export TracyWidom
45
using ODE
56

67
include("gradient.jl")
78

8-
function TracyWidom(t::Real)
9+
immutable TracyWidom <: ContinuousUnivariateDistribution
10+
end
11+
12+
function pdf(d::TracyWidom, t::Real)
913
t0 = -8.0
1014
t>t0 ? (return 0.0) : nothing
1115
t>5 ? (return 0.0) : nothing
@@ -18,3 +22,12 @@ function TracyWidom(t::Real)
1822
f2=(F2[end]-F2[end-1])/(ts[end]-ts[end-1])# the density at t
1923
end
2024

25+
26+
#Samples the largest eigenvalue of the n x n GUE matrix
27+
function rand(d::TracyWidom, n::Integer)
28+
k=int(n-10*n^(1/3)-1)
29+
A=[chi(i) for i=(n-1):-1:k]
30+
B=randn(n-k+1)
31+
v=eigmax(SymTridiagonal(B, A))
32+
end
33+

0 commit comments

Comments
 (0)