Skip to content

Commit 0df6a8b

Browse files
committed
Redo stochastic processes to make use of iterator protocol
1 parent 4405165 commit 0df6a8b

File tree

1 file changed

+39
-36
lines changed

1 file changed

+39
-36
lines changed

src/StochasticProcess.jl

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,54 @@
11
#Matrices related to stochastic processes
22

3-
abstract StochasticProcess
4-
export evolve
3+
import Base: start, next, done
4+
export AiryProcess, BrownianProcess, WhiteNoiseProcess
55

6-
immutable BrownianProcess <: StochasticProcess
6+
abstract StochasticProcess{T<:Real}
7+
8+
immutable WhiteNoiseProcess{T<:Real} <: StochasticProcess{T}
9+
dt::T
710
end
811

9-
# Generates Brownian motion
10-
evolve(p::BrownianProcess, dx::Real, xend::Real) = cumsum(WhiteNoiseProcess(dx, xend))
12+
immutable BrownianProcess{T<:Real} <: StochasticProcess{T}
13+
dt::T
14+
end
1115

12-
# Generates a white noise process
13-
function WhiteNoiseProcess(dx::Real, xend::Real)
14-
x=[0:dx:xend]
15-
randn(length(x),1)*sqrt(dx)
16+
type AiryProcess{S<:Real, T<:Real} <: StochasticProcess{T}
17+
dt::T
18+
beta::S
1619
end
1720

18-
################
19-
# Airy process #
20-
################
21+
done{T}(p::StochasticProcess{T}) = false #Processes can always go on forever
22+
23+
#######################
24+
# White noise process #
25+
#######################
2126

22-
immutable AiryProcess <: StochasticProcess
23-
beta::Real
27+
start{T}(p::WhiteNoiseProcess{T}) = nothing
28+
next{T}(p::WhiteNoiseProcess{T}, x::Void=nothing) = (randn()*sqrt(p.dt), nothing)
29+
30+
####################
31+
# Brownian process #
32+
####################
33+
34+
start{T}(p::BrownianProcess{T}) = zero(T)
35+
function next{T}(p::BrownianProcess{T}, x::T)
36+
newx = x + randn()*sqrt(p.dt)
37+
newx, newx
2438
end
2539

26-
# Calculates the largest eigenvalue of a stochastic Airy process
27-
# with Brownian noise
28-
function evolve(p::AiryProcess, dx::Real, xend::Real)
29-
x=[0:dx:xend]
30-
N=length(x)
40+
################
41+
# Airy process #
42+
################
43+
start{T}(p::AiryProcess{T}) = SymTridiagonal(T[-(2/p.dt^2)], T[])
44+
function next{T}(p::AiryProcess{T}, S::SymTridiagonal{T})
45+
t = (size(S, 1)-1)*p.dt
3146

32-
#Discretized Airy operator
33-
a=-(2/dx^2)*ones(N) - x
34-
b=+(1/dx^2)*ones(N-1)
35-
#Plus noise
36-
dW=WhiteNoiseProcess(dx, xend)
37-
a+=(2/sqrt(p.beta))*dW/dx
47+
#Discretized Airy operator plus diagonal noise
48+
x = inv(p.dt^2)
49+
push!(S.dv, -2x - t + 2/sqrt(p.beta/p.dt)*randn())
50+
push!(S.ev, x)
3851

39-
maxeig(SymTridiagonal(a,b))
52+
(eigmax(S)*p.dt^2, S)
4053
end
4154

42-
#Sample program
43-
#t=10000 #number of trials
44-
#v=[AiryProcess(0.001, 10, 2) for i=1:t]
45-
#binsize=.2
46-
#grid=[-5:binsize:2]
47-
#x=hist(v, grid)
48-
#for i=1:length(grid)
49-
# @printf("%10.5f %8d %s\n",grid[i],x[i],repeat("*",int(x[i]/(t*binsize)*200)))
50-
#end
51-

0 commit comments

Comments
 (0)