My current solution for the OU process where parameters \theta(s_t), \mu(s_t), \sigma(s_t), are subject to discrete switches governed by discrete 2-element Continuous-time Markov chain, s_t \in \{1,2\}, which is parameterized by intensity matrix Q such that p(s_{t+dt} | s_t) \sim \exp(d t Q) is below.
I do not believe the solution is ideal, as I treat the discrete process, s_t, as the second element of an SDEProblem
, rather than a mixture between a SDEProblem
and a DiscreteProblem
. Of particular concern is the call to Int(u[2])
in the rate(du, u, p, t)
function, which I can’t imagine is good for performance (and may cause type-instability?).
using DifferentialEquations
using JumpProcesses
using Plots
Q = [-0.1 0.1; 0.3 -0.3]
θ = [0.4, 0.2]
μ = [0.9, -0.9]
σ = [0.1, 0.5]
p = (θ, μ, σ, Q)
function rate(u, p, t)
P = exp(p[4])
if u[2]==1
return P[1,1]
else
return P[2,2]
end
end
function affect!(integrator)
if integrator.u[2] == 1
integrator.u[2] += 1
else
integrator.u[2] -= 1
end
end
jump = ConstantRateJump(rate, affect!)
function f(du, u, p ,t) #dt
θ = p[1]
μ = p[2]
du[1] = θ[Int(u[2])] * (μ[Int(u[2])] - u[1])
du[2] = 0.0
nothing
end
function g(du, u, p ,t) #dW
σ = p[3]
du[1] = σ[Int(u[2])]
du[2] = 0.0
nothing
end
prob = SDEProblem(f, g, [0.0, 1.0], (0.0, 1000.0), p)
jump_prob = JumpProblem(prob, Direct(), jump)
sol = solve(jump_prob, SRIW1())
plot(sol[1:200], layout = (2,1))