/
ornstein_uhlenbeck.jl
135 lines (121 loc) · 4.15 KB
/
ornstein_uhlenbeck.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
struct OrnsteinUhlenbeck{T1, T2, T3}
Θ::T1
μ::T2
σ::T3
end
# http://www.math.ku.dk/~susanne/StatDiff/Overheads1b.pdf
function (X::OrnsteinUhlenbeck)(dW, W, dt, u, p, t, rng) #dist
if dW isa AbstractArray
rand_val = wiener_randn(rng, dW)
else
rand_val = wiener_randn(rng, typeof(dW))
end
drift = X.μ .+ (W.curW .- X.μ) .* exp.(-X.Θ * dt)
diffusion = X.σ .* sqrt.(-expm1.(-2X.Θ * dt) ./ (2X.Θ))
drift .+ rand_val .* diffusion .- W.curW
end
#=
http://www.tandfonline.com/doi/pdf/10.1080/14697688.2014.941913?needAccess=true
http://www.tandfonline.com/doi/full/10.1080/14697688.2014.941913?src=recsys
=#
#=
(qX + r) = Θ(μ-x) = Θμ - Θx
q = -Θ
r = Θμ
https://arxiv.org/pdf/1011.0067.pdf page 18
note that in the paper there is a typo in the formula for σ^2
=#
function ou_bridge(dW, ou, W, W0, Wh, q, h, u, p, t, rng)
if dW isa AbstractArray
rand_vec = wiener_randn(rng, dW)
var = @. ou.σ^2 * sinh(ou.Θ * (h * (1.0 - q))) * (sinh(ou.Θ * (q * h))) /
(ou.Θ * sinh(ou.Θ * h))
@. (W0 - ou.μ) * (sinh(ou.Θ * (h * (1.0 - q))) / sinh(ou.Θ * h) - 1.0) +
(Wh + W0 - ou.μ) * sinh(ou.Θ * q * h) / sinh(ou.Θ * h) + sqrt(var) * rand_vec
else
var = ou.σ^2 * sinh(ou.Θ * (h * (1.0 - q))) * (sinh(ou.Θ * (q * h))) /
(ou.Θ * sinh(ou.Θ * h))
(W0 - ou.μ) * (sinh(ou.Θ * (h * (1.0 - q))) / sinh(ou.Θ * h) - 1.0) +
(Wh + W0 - ou.μ) * sinh(ou.Θ * q * h) / sinh(ou.Θ * h) +
sqrt(var) * wiener_randn(rng, typeof(dW))
end
end
function ou_bridge!(rand_vec, ou, W, W0, Wh, q, h, u, p, t, rng)
wiener_randn!(rng, rand_vec)
@.. rand_vec = (W0 - ou.μ) * (sinh(ou.Θ * (h * (1.0 - q))) / sinh(ou.Θ * h) - 1.0) +
(Wh + W0 - ou.μ) * sinh(ou.Θ * q * h) / sinh(ou.Θ * h) +
sqrt(ou.σ^2 * sinh(ou.Θ * (h * (1.0 - q))) * (sinh(ou.Θ * (q * h))) /
(ou.Θ * sinh(ou.Θ * h))) * rand_vec
end
@doc doc"""
a `Ornstein-Uhlenbeck` process, which is a Wiener process defined
by the stochastic differential equation
```math
dX_t = \theta (\mu - X_t) dt + \sigma dW_t
```
The `OrnsteinUhlenbeckProcess` is distribution exact (meaning, not a numerical
solution of the stochastic differential equation, but instead follows the exact
distribution properties). The constructor is:
```julia
OrnsteinUhlenbeckProcess(Θ,μ,σ,t0,W0,Z0=nothing;kwargs...)
OrnsteinUhlenbeckProcess!(Θ,μ,σ,t0,W0,Z0=nothing;kwargs...)
```
"""
function OrnsteinUhlenbeckProcess(Θ, μ, σ, t0, W0, Z0 = nothing; kwargs...)
ou = OrnsteinUhlenbeck(Θ, μ, σ)
NoiseProcess{false}(t0, W0, Z0, ou,
(rand_vec, W, W0, Wh, q, h, u, p, t, rng) -> ou_bridge(rand_vec,
ou,
W,
W0,
Wh,
q,
h,
u,
p,
t,
rng); kwargs...)
end
struct OrnsteinUhlenbeck!{T1, T2, T3}
Θ::T1
μ::T2
σ::T3
end
function (X::OrnsteinUhlenbeck!)(rand_vec, W, dt, u, p, t, rng) #dist!
wiener_randn!(rng, rand_vec)
@.. rand_vec = X.μ + (W.curW - X.μ) * exp(-X.Θ * dt) +
rand_vec * X.σ * sqrt((-expm1.(-2 * X.Θ .* dt)) / (2 * X.Θ)) - W.curW
end
@doc doc"""
A `Ornstein-Uhlenbeck` process, which is a Wiener process defined
by the stochastic differential equation
```math
dX_t = \theta (\mu - X_t) dt + \sigma dW_t
```
The `OrnsteinUhlenbeckProcess` is distribution exact (meaning, not a numerical
solution of the stochastic differential equation, but instead follows the exact
distribution properties). The constructor is:
```julia
OrnsteinUhlenbeckProcess(Θ,μ,σ,t0,W0,Z0=nothing;kwargs...)
OrnsteinUhlenbeckProcess!(Θ,μ,σ,t0,W0,Z0=nothing;kwargs...)
```
"""
function OrnsteinUhlenbeckProcess!(Θ, μ, σ, t0, W0, Z0 = nothing; kwargs...)
ou = OrnsteinUhlenbeck!(Θ, μ, σ)
NoiseProcess{true}(t0,
W0,
Z0,
ou,
(rand_vec, W, W0, Wh, q, h, u, p, t, rng) -> ou_bridge!(rand_vec,
ou,
W,
W0,
Wh,
q,
h,
u,
p,
t,
rng);
kwargs...)
end