Skip to content

Commit 5feef50

Browse files
committed
Add test of Tracy-Widom distribution
Make GSL a required dependency, and get rid of conditional testing code Drop obsolete dependency on Docile
1 parent da3f062 commit 5feef50

File tree

7 files changed

+57
-51
lines changed

7 files changed

+57
-51
lines changed

REQUIRE

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
julia 0.4
22
Combinatorics 0.2
3-
Docile
43
Distributions
54
ODE
5+
GSL
6+

src/Haar.jl

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ function permutations_in_Sn(n::Integer)
3131
end
3232

3333

34-
if _HAVE_GSL
35-
3634
function compose(P::Ptr{gsl_permutation}, Q::Ptr{gsl_permutation})
3735
#Compose the permutations
3836
n=convert(Int64, permutation_size(P))
@@ -66,8 +64,6 @@ end
6664
data(P::Ptr{gsl_permutation}) = [convert(Int64, x)+1 for x in
6765
pointer_to_array(permutation_data(P), (convert(Int64, permutation_size(P)) ,))]
6866

69-
end #_HAVE_GSL
70-
7167

7268
type Haar <: ContinuousMatrixDistribution
7369
beta::Real
@@ -268,16 +264,12 @@ function expectation(X::Expr)
268264
eval(X)
269265
end
270266

271-
if _HAVE_GSL
272-
273267
#Computes the Weingarten function for permutations
274268
function WeingartenUnitary(P::Ptr{gsl_permutation})
275269
C = cycle_structure(P)
276270
WeingartenUnitary(C)
277271
end
278272

279-
end #_HAVE_GSL
280-
281273
#Computes the Weingarten function for partitions
282274
function WeingartenUnitary(P::partition)
283275
n = sum(P)

src/RandomMatrices.jl

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,19 @@
11
module RandomMatrices
2-
import Distributions
3-
using Combinatorics,Compat
42

5-
import Distributions: ContinuousMatrixDistribution, Chi
6-
7-
8-
#If the GNU Scientific Library is present, turn on additional functionality.
9-
_HAVE_GSL = try
10-
using GSL
11-
true
12-
catch
13-
false
14-
end
3+
using Combinatorics
4+
using Compat
5+
using GSL
6+
using ODE
157

168
import Base: isinf, rand
9+
import Distributions: ContinuousMatrixDistribution, Chi, pdf
1710

1811
export bidrand, #Generate random bidiagonal matrix
1912
tridrand, #Generate random tridiagonal matrix
2013
sprand, #Generate random sparse matrix
2114
eigvalrand, #Generate random set of eigenvalues
22-
eigvaljpdf #Eigenvalue joint probability density
15+
eigvaljpdf, #Eigenvalue joint probability density
16+
pdf
2317

2418
typealias Dim2 Tuple{Int, Int} #Dimensions of a rectangular matrix
2519

src/densities/Semicircle.jl

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,19 +66,13 @@ std(X::Semicircle)=X.radius/2
6666
var(X::Semicircle)=std(X)^2
6767

6868
# moment of distribution
69-
if _HAVE_GSL
70-
function moment(X::Semicircle, order::Integer)
69+
function moment(X::Semicircle, order::Integer)
7170
a, r = X.mean, X.radius
7271
if X.mean != 0
73-
a^n*hypergeom([(1-n)/2, -n/2], 2, (r/a)^2)
72+
a^n*hypergeom([(1-n)/2, -n/2], 2, (r/a)^2)
7473
else
75-
order%2 ? (0.5*r)^(2n) * catalan(div(order,2)) : 0
74+
order%2 ? (0.5*r)^(2n) * catalan(div(order,2)) : 0
7675
end
77-
end
78-
else
79-
function moment(X::Semicircle, order::Integer)
80-
error("The moment of a semicircle distribution requires the GSL module to be loaded.")
81-
end
8276
end
8377

8478
# cumulant of distribution

src/densities/TracyWidom.jl

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,40 @@
1-
#Computes the Tracy-Widom distribution by directly solving the
2-
#Painlevé II equation
3-
import Distributions
41
export TracyWidom
5-
using ODE
62

73
include("gradient.jl")
84

9-
immutable TracyWidom <: ContinuousUnivariateDistribution
10-
end
5+
immutable TracyWidom <: ContinuousUnivariateDistribution end
6+
117

8+
"""
9+
Computes the Tracy-Widom distribution by directly solving the
10+
Painlevé II equation by numerical integration
11+
"""
1212
function pdf(d::TracyWidom, t::Real)
13-
t0 = -8.0
14-
t>t0 ? (return 0.0) : nothing
15-
t>5 ? (return 0.0) : nothing
13+
t0 = -8.0
14+
tt0 && return 0.0
15+
t5 && return 0.0
1616

17-
deq(t, y) = [y[2]; t*y[1]+2y[1]^3; y[4]; y[1]^2]
17+
deq(t, y) = [y[2]; t*y[1]+2y[1]^3; y[4]; y[1]^2]
1818

19-
y0=[airy(t0); airy(1, t0); 0; airy(t0)^2] # Initial conditions
20-
(ts, y)=ode23(deq, [t0, t], y0) # Solve the ODE
21-
F2=exp(-y[:,3]) # the cumulative distribution
22-
f2=(F2[end]-F2[end-1])/(ts[end]-ts[end-1])# the density at t
19+
y0=[airy(t0); airy(1, t0); 0; airy(t0)^2] # Initial conditions
20+
(ts, y)=ode23(deq, [t0, t], y0) # Solve the ODE
21+
F2=exp(-y[:,3]) # the cumulative distribution
22+
f2=(F2[end]-F2[end-1])/(ts[end]-ts[end-1])# the density at t
2323
end
24+
pdf(d::Type{TracyWidom}, t::Real) = pdf(d(), t)
2425

25-
26-
#Samples the largest eigenvalue of the n x n GUE matrix
26+
"""
27+
Samples the largest eigenvalue of the n x n GUE matrix
28+
"""
2729
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))
30+
n > 1 || error("Cannot construct $n × $n matrix")
31+
if n < 100
32+
k = n
33+
else #Exploit the fact that the eigenvector is concentrated in the top left corner
34+
k = round(Int, n-10*n^(1/3)-1)
35+
end
36+
a=randn(n-k+1)
37+
b=[χ(i) for i=(n-1):-1:k]
38+
v=eigmax(SymTridiagonal(a, b))
3239
end
40+
rand(d::Type{TracyWidom}, t::Integer) = rand(d(), t)

test/densities/TracyWidom.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using RandomMatrices
2+
using Base.Test
3+
4+
#Test far outside support
5+
#Tracy-Widom has support on all x>0, but the integration won't pick it up
6+
@test RandomMatrices.pdf(TracyWidom, -10) == RandomMatrices.pdf(TracyWidom, 10) == 0
7+
8+
if isdefined(:ODE) && isa(ODE, Module)
9+
t = rand()
10+
@test RandomMatrices.pdf(TracyWidom, t) > 0
11+
end
12+
13+
@test rand(TracyWidom, 10) > 0
14+
@test rand(TracyWidom, 100) > 0

test/runtests.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@ include("GaussianEnsembles.jl")
66
include("FormalPowerSeries.jl")
77
include("Haar.jl")
88
include("StochasticProcess.jl")
9+
10+
include("densities/TracyWidom.jl")
11+

0 commit comments

Comments
 (0)