Skip to content

Commit c309386

Browse files
make a bunch of problem constructors
1 parent 6769c3a commit c309386

File tree

2 files changed

+18
-163
lines changed

2 files changed

+18
-163
lines changed

src/DiffEqFinancial.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
__precompile__()
22

33
module DiffEqFinancial
4-
using DiffEqBase, DiffEqNoiseProcess, Markdown
4+
using DiffEqBase, DiffEqNoiseProcess, Markdown, LinearAlgebra
55

66
import RandomNumbers: Xorshifts
77

src/problems.jl

+17-162
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,7 @@ dW_1 dW_2 = ρ dt
77
```
88
99
"""
10-
mutable struct HestonProblem{uType,tType,tupType,isinplace,NP,F,F2,C,ND,MM} <: DiffEqBase.AbstractSDEProblem{uType,tType,isinplace,ND}
11-
μ::tType
12-
κ::tType
13-
Θ::tType
14-
σ::tType
15-
ρ::tType
16-
u0::uType
17-
tspan::tupType
18-
p::Nothing
19-
f::F
20-
g::F2
21-
noise::NP
22-
callback::C
23-
noise_rate_prototype::ND
24-
mass_matrix::MM
25-
seed::UInt64
26-
end
27-
28-
function HestonProblem(μ,κ,Θ,σ,ρ,u0,tspan;callback = CallbackSet(),seed=UInt64(0))
10+
function HestonProblem(μ,κ,Θ,σ,ρ,u0,tspan;seed=UInt64(0),kwargs...)
2911
f = function (du,u,p,t)
3012
du[1] = μ*u[1]
3113
du[2] = κ*-u[2])
@@ -35,19 +17,16 @@ function HestonProblem(μ,κ,Θ,σ,ρ,u0,tspan;callback = CallbackSet(),seed=UIn
3517
du[2] = Θ*√u[2]
3618
end
3719
Γ = [1 ρ;ρ 1] # Covariance Matrix
38-
mass_matrix=I
3920
noise_rate_prototype = nothing
21+
4022
if seed == 0
4123
seed = rand(UInt64)
4224
end
43-
noise = CorrelatedWienerProcess!(Γ,tspan[1],zeros(2),zeros(2),rng = Xorshifts.Xoroshiro128Plus(seed))
44-
isinplace = true
45-
HestonProblem{typeof(u0),eltype(tspan),typeof(tspan),isinplace,
46-
typeof(noise),
47-
typeof(f),typeof(g),
48-
typeof(callback),typeof(noise_rate_prototype),
49-
typeof(mass_matrix)}(μ,κ,Θ,σ,ρ,u0,tspan,nothing,
50-
f,g,noise,callback,noise_rate_prototype,mass_matrix,seed)
25+
noise = CorrelatedWienerProcess!(Γ,tspan[1],zeros(2),zeros(2),
26+
rng = Xorshifts.Xoroshiro128Plus(seed))
27+
28+
sde_f = SDEFunction{true}(f,g)
29+
SDEProblem(sde_f,g,u0,tspan,noise=noise,seed=seed,kwargs...)
5130
end
5231

5332
@doc doc"""
@@ -56,41 +35,14 @@ end
5635
5736
Solves for ``log S(t)``.
5837
"""
59-
mutable struct GeneralizedBlackScholesProblem{uType,tType,tupType,isinplace,NP,F,F2,thetaType,qType,rType,C,ND,MM} <: DiffEqBase.AbstractSDEProblem{uType,tType,isinplace,ND}
60-
r::rType
61-
q::qType
62-
Θ::thetaType
63-
σ::tType
64-
u0::uType
65-
tspan::tupType
66-
p::Nothing
67-
f::F
68-
g::F2
69-
noise::NP
70-
callback::C
71-
noise_rate_prototype::ND
72-
mass_matrix::MM
73-
seed::UInt64
74-
end
75-
76-
function GeneralizedBlackScholesProblem(r,q,Θ,σ,u0,tspan;callback = CallbackSet(),seed=UInt64(0))
38+
function GeneralizedBlackScholesProblem(r,q,Θ,σ,u0,tspan;kwargs...)
7739
f = function (u,p,t)
7840
r(t) - q(t) - Θ(t,exp(u))^2 / 2
7941
end
8042
g = function (u,p,t)
8143
σ
8244
end
83-
noise_rate_prototype = nothing
84-
noise = nothing
85-
isinplace = false
86-
mass_matrix=I
87-
GeneralizedBlackScholesProblem{typeof(u0),eltype(tspan),typeof(tspan),isinplace,
88-
typeof(noise),
89-
typeof(f),typeof(g),
90-
typeof(Θ),typeof(q),typeof(r),
91-
typeof(callback),typeof(noise_rate_prototype),typeof(mass_matrix)}(
92-
r,q,Θ,σ,u0,tspan,nothing,f,g,noise,callback,
93-
noise_rate_prototype,mass_matrix,seed)
45+
SDEProblem{false}(f,g,u0,tspan;kwargs...)
9446
end
9547

9648
@doc doc"""
@@ -124,79 +76,29 @@ BlackScholesProblem(r,Θ,σ,u0,tspan;callback = CallbackSet(),
12476
``dx = a(b(t)-x)dt + σ dW_t``
12577
12678
"""
127-
mutable struct ExtendedOrnsteinUhlenbeckProblem{uType,tType,tupType,isinplace,NP,F,F2,C,ND,MM} <: DiffEqBase.AbstractSDEProblem{uType,tType,isinplace,ND}
128-
a::tType
129-
b::tType
130-
σ::tType
131-
u0::uType
132-
tspan::tupType
133-
p::Nothing
134-
f::F
135-
g::F2
136-
noise::NP
137-
callback::C
138-
noise_rate_prototype::ND
139-
mass_matrix::MM
140-
seed::UInt64
141-
end
142-
143-
function ExtendedOrnsteinUhlenbeckProblem(a,b,σ,u0,tspan;callback = CallbackSet(),seed=UInt64(0))
79+
function ExtendedOrnsteinUhlenbeckProblem(a,b,σ,u0,tspan;kwargs...)
14480
f = function (u,p,t)
14581
a*(b(t)-u)
14682
end
14783
g = function (u,p,t)
14884
σ
14985
end
150-
noise_rate_prototype = nothing
151-
noise = nothing
152-
isinplace = false
153-
mass_matrix=I
154-
ExtendedOrnsteinUhlenbeckProblem{typeof(u0),eltype(tspan),typeof(tspan),isinplace,
155-
typeof(noise),
156-
typeof(f),typeof(g),
157-
typeof(callback),typeof(noise_rate_prototype),
158-
typeof(mass_matrix)}(a,b,σ,u0,tspan,nothing,f,g,noise,
159-
callback,noise_rate_prototype,mass_matrix,seed)
86+
SDEProblem{false}(f,g,u0,tspan;kwargs...)
16087
end
16188

16289
@doc doc"""
16390
16491
``dx = a(r-x)dt + σ dW_t``
16592
16693
"""
167-
mutable struct OrnsteinUhlenbeckProblem{uType,tType,tupType,isinplace,NP,F,F2,C,ND,MM} <: DiffEqBase.AbstractSDEProblem{uType,tType,isinplace,ND}
168-
a::tType
169-
r::tType
170-
σ::tType
171-
u0::uType
172-
tspan::tupType
173-
p::Nothing
174-
f::F
175-
g::F2
176-
noise::NP
177-
callback::C
178-
noise_rate_prototype::ND
179-
mass_matrix::MM
180-
seed::UInt64
181-
end
182-
183-
function OrnsteinUhlenbeckProblem(a,r,σ,u0,tspan;callback = CallbackSet(),seed=UInt64(0))
94+
function OrnsteinUhlenbeckProblem(a,r,σ,u0,tspan;kwargs...)
18495
f = function (u,p,t)
18596
a*(r-u)
18697
end
18798
g = function (u,p,t)
18899
σ
189100
end
190-
noise = nothing
191-
isinplace = false
192-
noise_rate_prototype = nothing
193-
mass_matrix=I
194-
OrnsteinUhlenbeckProblem{typeof(u0),eltype(tspan),typeof(tspan),isinplace,
195-
typeof(noise),
196-
typeof(f),typeof(g),
197-
typeof(callback),typeof(noise_rate_prototype),
198-
typeof(mass_matrix)}(a,r,σ,u0,tspan,nothing,f,g,noise,callback,
199-
noise_rate_prototype,mass_matrix,seed)
101+
SDEProblem{false}(f,g,u0,tspan;kwargs...)
200102
end
201103

202104

@@ -205,74 +107,27 @@ end
205107
``dx = μ dt + σ dW_t``
206108
207109
"""
208-
mutable struct GeometricBrownianMotionProblem{uType,tType,tupType,isinplace,NP,F,F2,C,ND,MM} <: DiffEqBase.AbstractSDEProblem{uType,tType,isinplace,ND}
209-
μ::tType
210-
σ::tType
211-
u0::uType
212-
tspan::tupType
213-
p::Nothing
214-
f::F
215-
g::F2
216-
noise::NP
217-
callback::C
218-
noise_rate_prototype::ND
219-
mass_matrix::MM
220-
seed::UInt64
221-
end
222-
223-
function OrnsteinUhlenbeckProblem(μ,σ,u0,tspan;callback = CallbackSet(),seed=UInt64(0))
110+
function GeometricBrownianMotionProblem(μ,σ,u0,tspan;kwargs...)
224111
f = function (u,p,t)
225112
μ
226113
end
227114
g = function (u,p,t)
228115
σ
229116
end
230-
noise = nothing
231-
isinplace = false
232-
mass_matrix=I
233-
noise_rate_prototype = nothing
234-
OrnsteinUhlenbeckProblem{typeof(u0),eltype(tspan),typeof(tspan),isinplace,
235-
typeof(noise),
236-
typeof(f),typeof(g),
237-
typeof(callback),typeof(noise_rate_prototype),
238-
typeof(mass_matrix)}(a,r,σ,u0,tspan,nothing,f,g,noise,callback,
239-
noise_rate_prototype,mass_matrix,seed)
117+
SDEProblem{false}(f,g,u0,tspan;kwargs...)
240118
end
241119

242120
@doc doc"""
243121
244122
``dx = σ(t)e^{at} dW_t``
245123
246124
"""
247-
mutable struct MfStateProblem{uType,tType,tupType,isinplace,NP,F,F2,C,ND,MM} <: DiffEqBase.AbstractSDEProblem{uType,tType,isinplace,ND}
248-
a::tType
249-
σ::tType
250-
u0::uType
251-
tspan::tupType
252-
p::Nothing
253-
f::F
254-
g::F2
255-
noise::NP
256-
callback::C
257-
noise_rate_prototype::ND
258-
mass_matrix::MM
259-
seed::UInt64
260-
end
261-
262-
function MfStateProblem(a,σ,u0,tspan;callback = CallbackSet(),noise_rate_prototype = nothing,seed=UInt64(0))
125+
function MfStateProblem(a,σ,u0,tspan;kwargs...)
263126
f = function (u,p,t)
264127
0
265128
end
266129
g = function (u,p,t)
267130
σ(t)*exp(a*t)
268131
end
269-
noise = nothing
270-
isinplace = false
271-
mass_matrix=I
272-
MfStateProblem{typeof(u0),eltype(tspan),typeof(tspan),isinplace,
273-
typeof(noise),
274-
typeof(f),typeof(g),
275-
typeof(callback),typeof(noise_rate_prototype),typeof(mass_matrix)}(
276-
a,σ,u0,tspan,nothing,f,g,noise,callback,
277-
noise_rate_prototype,mass_matrix,seed)
132+
SDEProblem{false}(f,g,u0,tspan;kwargs...)
278133
end

0 commit comments

Comments
 (0)