Skip to content

Commit 93ca898

Browse files
committed
Add cdf(::TracyWidom)
1 parent ea060a9 commit 93ca898

File tree

3 files changed

+31
-10
lines changed

3 files changed

+31
-10
lines changed

src/RandomMatrices.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ using GSL
66
using ODE
77

88
import Base: isinf, rand
9-
import Distributions: ContinuousMatrixDistribution, Chi, pdf
9+
import Distributions: ContinuousMatrixDistribution, Chi, cdf, pdf
1010

1111
export bidrand, #Generate random bidiagonal matrix
1212
tridrand, #Generate random tridiagonal matrix
1313
sprand, #Generate random sparse matrix
1414
eigvalrand, #Generate random set of eigenvalues
1515
eigvaljpdf, #Eigenvalue joint probability density
16-
pdf
16+
pdf,
17+
cdf
1718

1819
typealias Dim2 Tuple{Int, Int} #Dimensions of a rectangular matrix
1920

src/densities/TracyWidom.jl

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,38 @@ immutable TracyWidom <: ContinuousUnivariateDistribution end
99
Computes the Tracy-Widom distribution by directly solving the
1010
Painlevé II equation by numerical integration
1111
"""
12-
function pdf(d::TracyWidom, t::Real)
13-
t0 = -8.0
12+
function pdf{S<:Real}(d::TracyWidom, t::S, t0::S = convert(S, -8.0))
1413
tt0 && return 0.0
1514
t5 && return 0.0
1615

17-
deq(t, y) = [y[2], t*y[1]+2y[1]^3, y[4], y[1]^2]
16+
ts, y = _solve_painleve_ii(t0, t)
17+
18+
ΔF2=exp(-y[end-1][3]) - exp(-y[end][3]) # the cumulative distribution
19+
f2=ΔF2/(ts[end]-ts[end-1]) # the density at t
20+
end
21+
pdf(d::Type{TracyWidom}, t::Real) = pdf(d(), t)
1822

23+
"""
24+
Computes the Tracy-Widom distribution by directly solving the
25+
Painlevé II equation by numerical integration
26+
"""
27+
function cdf{S<:Real}(d::TracyWidom, t::S, t0::S = convert(S, -8.0))
28+
tt0 && return 0.0
29+
t5 && return 1.0
30+
31+
ts, y = _solve_painleve_ii(t0, t)
32+
33+
F2=exp(-y[end][3])
34+
end
35+
cdf(d::Type{TracyWidom}, t::Real) = cdf(d(), t)
36+
37+
function _solve_painleve_ii{S<:Real}(t0::S, t::S)
38+
deq(t, y) = [y[2], t*y[1]+2y[1]^3, y[4], y[1]^2]
1939
a0 = airy(t0)
2040
T = typeof(a0)
2141
y0=T[a0, airy(1, t0), 0, airy(t0)^2] # Initial conditions
2242
(ts, y)=ode23(deq, y0, [t0, t]) # Solve the ODE
23-
ΔF2=exp(-y[end-1][3]) - exp(-y[end][3]) # the cumulative distribution
24-
f2=ΔF2/(ts[end]-ts[end-1]) # the density at t
2543
end
26-
pdf(d::Type{TracyWidom}, t::Real) = pdf(d(), t)
2744

2845
"""
2946
Samples the largest eigenvalue of the n x n GUE matrix

test/densities/TracyWidom.jl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@ using Base.Test
33

44
#Test far outside support
55
#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
6+
@test pdf(TracyWidom, -10) == pdf(TracyWidom, 10) == 0
7+
@test cdf(TracyWidom, -10) == 0
8+
@test cdf(TracyWidom, 10) == 1
79

810
if isdefined(:ODE) && isa(ODE, Module)
911
t = rand()
10-
@test RandomMatrices.pdf(TracyWidom, t) > 0
12+
@test pdf(TracyWidom, t) > 0
13+
@test 0 < cdf(TracyWidom, t) < 1
1114
end
1215

1316
@test isfinite(rand(TracyWidom, 10))

0 commit comments

Comments
 (0)