# Resources on Modelling SDE with Parameter Switches

**URL:** <https://discourse.julialang.org/t/resources-on-modelling-sde-with-parameter-switches/90137>\
**Category:** Modelling & Simulations\
**Tags:** diffeq, sciml\
**Created:** [November 11, 2022, 9:14pm UTC](https://discourse.julialang.org/t/resources-on-modelling-sde-with-parameter-switches/90137 "2022-11-11T21:14:04Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dleather](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dleather/32/43762_2.png) [@dleather](https://discourse.julialang.org/u/dleather)\
**Post date:** [November 11, 2022, 9:14pm UTC](https://discourse.julialang.org/t/resources-on-modelling-sde-with-parameter-switches/90137/1 "2022-11-11T21:14:04Z")

</div>

I’m exploring the SciMl ecosystem for the possibility of modelling something like an OU process where the drift, autoregressive, and volatility parameters are subject to discrete switches governed by a continuous-time Markov chain.

For example, let s\_t \in \{1,2,\ldots,N\} be governed by the intensity matrix U, such that the transition matrix of s\_t between period t and t + \tau is \exp(\tau U).

The process I’d like to model is then dX\_t = \theta(s\_t) (\mu(s\_t) - X\_t) dt + \sigma(s\_t) dW\_t .

As someone who traditionally works in discrete-time, I’m having a hard time mapping this problem into ModellingToolkit.jl.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [November 12, 2022, 10:21am UTC](https://discourse.julialang.org/t/resources-on-modelling-sde-with-parameter-switches/90137/2 "2022-11-12T10:21:01Z")

</div>

You’re looking at a jump diffusion equation. See the jump diffusion tutorial: [Jump Diffusion Equations · DifferentialEquations.jl](https://docs.sciml.ai/DiffEqDocs/stable/tutorials/jump_diffusion/)

---

<div class="post-metadata">

**Author:** ![dleather](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dleather/32/43762_2.png) [@dleather](https://discourse.julialang.org/u/dleather)\
**Post date:** [November 15, 2022, 7:53pm UTC](https://discourse.julialang.org/t/resources-on-modelling-sde-with-parameter-switches/90137/3 "2022-11-15T19:53:47Z")

</div>

Give the rate function, `rate(u, p ,t) = ...`, does not specify `dt` as a possible argument, is it correct to assume giving the proper rate for `dt=1` will automatically induce the scaling invoked by \exp(\tau Q), where \tau in the previously post is equivalent to dt?

For example, with `p = (Q)` representing a Continuous-Time Markov chain which jumps between two states s\_t = \{1,2\} would it be correct to code up the jump as:

```julia
function rate(u, p, t)
    P = exp(Q)
    if u[1]==1
        return P[1,1] 
    else
        return P[2,2]
    end
end

function affect!(integrator)
    if integrator.u[1] == 1
        integrator.u[1] += 1
    else
        integrator.u[1] -= 1
    end
end

jump = ConstantRateJump(rate, affect!)

Q = [-0.1 0.1; 0.3 -0.3]
p = Q
prob = DiscreteProblem([1], (0.0, 100.0), (Q))

jump_prob = JumpProblem(prob, Direct(), jump)
sol = solve(jump_prob, SSAStepper())

plot(sol)

```

---

<div class="post-metadata">

**Author:** ![dleather](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dleather/32/43762_2.png) [@dleather](https://discourse.julialang.org/u/dleather)\
**Post date:** [November 15, 2022, 8:55pm UTC](https://discourse.julialang.org/t/resources-on-modelling-sde-with-parameter-switches/90137/4 "2022-11-15T20:55:07Z")

</div>

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?).

```julia
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))

```

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [November 16, 2022, 3:26pm UTC](https://discourse.julialang.org/t/resources-on-modelling-sde-with-parameter-switches/90137/5 "2022-11-16T15:26:17Z")

</div>

> [@dleather](#):
>
> Give the rate function, `rate(u, p ,t) = ...`, does not specify `dt` as a possible argument, is it correct to assume giving the proper rate for `dt=1` will automatically induce the scaling invoked by \exp(\tau Q) exp(τQ)\exp(\tau Q) , where \tau τ\tau in the previously post is equivalent to dt dtdt ?

Yes. So you don’t need `dt`.

> [@dleather](#):
>
> I do not believe the solution is ideal, as I treat the discrete process, s\_t sts\_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?).

That’s type-stable.

---

<div class="post-metadata">

**Author:** ![dleather](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dleather/32/43762_2.png) [@dleather](https://discourse.julialang.org/u/dleather)\
**Post date:** [November 16, 2022, 6:09pm UTC](https://discourse.julialang.org/t/resources-on-modelling-sde-with-parameter-switches/90137/6 "2022-11-16T18:09:26Z")

</div>

Thank you!
