# Variable rate jumps diffeq?

**URL:** https://discourse.julialang.org/t/variable-rate-jumps-diffeq/42356
**Category:** General Usage
**Created:** [July 1, 2020, 7:17am UTC](https://discourse.julialang.org/t/variable-rate-jumps-diffeq/42356 "2020-07-01T07:17:51Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![mcreel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcreel/32/30088_2.png) [@mcreel](https://discourse.julialang.org/u/mcreel)
#### Post date: [July 1, 2020, 7:17am UTC](https://discourse.julialang.org/t/variable-rate-jumps-diffeq/42356/1 "2020-07-01T07:17:51Z")

</div>

I’m trying to get a variable rate jump to work. I am using

```julia

## jump in price
#rate1(u,p,t) = λ0 # constant jump rate
rate1(u,p,t) = λ0.*exp(u[2]) # volatility dependent jump rate
# jump is normal with st. dev. equal to λ1 times current st. dev.
affect1!(integrator) = (integrator.u[1] = integrator.u[1].+randn(size(integrator.u[1])).*λ1.*exp(integrator.u[2]./2.0))

# this works:
jump1 = ConstantRateJump(rate1,affect1!)
# this does not
#jump1 = VariableRateJump(rate1,affect1!)
jump_prob = JumpProblem(prob,Direct(), jump1)

```

The rate, rate1, depends on the second integrator, so I understand that it is a variable rate jump. However, I get a dimension error if I try to specify it as variable rate (jump1, commented out). The solver gives a solution if I specify it as constant rate.

Am I making an error in the syntax for a variable rate jump?  
Thanks!

---

<div class="post-metadata">

### Author: ![mcreel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcreel/32/30088_2.png) [@mcreel](https://discourse.julialang.org/u/mcreel)
#### Post date: [July 1, 2020, 9:07am UTC](https://discourse.julialang.org/t/variable-rate-jumps-diffeq/42356/2 "2020-07-01T09:07:59Z")

</div>

Here’s the environment, and the whole file, if it helps

```julia
(JD) pkg> st
Status `~/Desktop/JD/Project.toml`
  [a077e3f3] DiffEqProblemLibrary v4.8.0
  [0c46a032] DifferentialEquations v6.14.0 `https://github.com/SciML/DifferentialEquations.jl.git#master`
  [91a5bcdd] Plots v0.29.9
  [e6cf234a] RandomNumbers v1.4.0

(JD) pkg> 

```

```julia
using DifferentialEquations, Plots

function MyProblem(μ0,μ1,κ,α,σ,ρ,u0,tspan)
    f = function (du,u,p,t)
        du[1] = μ0 + μ1*(u[2]-α)/σ # drift in prices
        du[2] = κ*(α-u[2]) # mean reversion in shocks
    end
    g = function (du,u,p,t)
        du[1] = exp(u[2]/2.0)
        du[2] = σ
    end
    Γ = [1 ρ;ρ 1] # Covariance Matrix
    noise = CorrelatedWienerProcess!(Γ,tspan[1],zeros(2),zeros(2))
    sde_f = SDEFunction{true}(f,g)
    SDEProblem(sde_f,g,u0,tspan,noise=noise)
end

μ0 = 0.0
μ1 = 0.0
κ = 0.05
α = 0.2
σ = 0.2
ρ = -0.7
λ0 = 2.0
λ1 = 3.0
u0 = [0;α]
dt = 0.01
prob = MyProblem(μ0, μ1, κ, α, σ, ρ, u0, (0.0,1000.0))

## jump in price
#rate1(u,p,t) = λ0 # constant jump rate
rate1(u,p,t) = λ0.*exp(u[2]) # volatility dependent jump rate
# jump is normal with st. dev. equal to λ1 times current st. dev.
affect1!(integrator) = (integrator.u[1] = integrator.u[1].+randn(size(integrator.u[1])).*λ1.*exp(integrator.u[2]./2.0))

# this works:
jump1 = ConstantRateJump(rate1,affect1!)
# this does not
#jump1 = VariableRateJump(rate1,affect1!)
jump_prob = JumpProblem(prob,Direct(), jump1)
sol = solve(jump_prob,SRIW1(), dt=dt, adaptive=false)

```

---

<div class="post-metadata">

### Author: ![mcreel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcreel/32/30088_2.png) [@mcreel](https://discourse.julialang.org/u/mcreel)
#### Post date: [July 1, 2020, 9:09am UTC](https://discourse.julialang.org/t/variable-rate-jumps-diffeq/42356/3 "2020-07-01T09:09:57Z")

</div>

An incidental question, is there a way to visualize exactly when jumps occur?

---

<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: [July 2, 2020, 2:56pm UTC](https://discourse.julialang.org/t/variable-rate-jumps-diffeq/42356/4 "2020-07-02T14:56:13Z")

</div>

Xref: [Specify jumps in a Heston-like model? - #6 by mcreel](https://discourse.julialang.org/t/specify-jumps-in-a-heston-like-model/42422/6) for the solution

---

<div class="post-metadata">

### Author: ![tzxiong](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tzxiong/32/17502_2.png) [@tzxiong](https://discourse.julialang.org/u/tzxiong)
#### Post date: [September 26, 2020, 7:41pm UTC](https://discourse.julialang.org/t/variable-rate-jumps-diffeq/42356/5 "2020-09-26T19:41:57Z")

</div>

Hi @mcreel

I ran into the same problem today. The example from the tutorial

[https://diffeq.sciml.ai/stable/tutorials/jump\_diffusion/](https://diffeq.sciml.ai/stable/tutorials/jump_diffusion/)

works fine for the constant rate jump, but not for the variable rate jump

The error message I’m getting from the variable rate jump is

```julia
MethodError: no method matching CartesianIndices(::Array{Float64,1}, ::Int64, ::Int64)

```

---

<div class="post-metadata">

### Author: ![tzxiong](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tzxiong/32/17502_2.png) [@tzxiong](https://discourse.julialang.org/u/tzxiong)
#### Post date: [September 26, 2020, 7:42pm UTC](https://discourse.julialang.org/t/variable-rate-jumps-diffeq/42356/6 "2020-09-26T19:42:35Z")

</div>

BTW I updated to the latest version of DifferentialEquations.jl and the problem still exists

---

<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: [September 26, 2020, 10:42pm UTC](https://discourse.julialang.org/t/variable-rate-jumps-diffeq/42356/7 "2020-09-26T22:42:17Z")

</div>

I just ran the tutorial code:

```julia
using OrdinaryDiffEq, DiffEqJump
function f(du,u,p,t)
  du[1] = u[1]
end

prob = ODEProblem(f,[0.2],(0.0,10.0))
rate(u,p,t) = u[1]
affect!(integrator) = (integrator.u[1] = integrator.u[1]/2)
jump = VariableRateJump(rate,affect!)
jump2 = deepcopy(jump)
jump_prob = JumpProblem(prob,Direct(),jump,jump2)
sol = solve(jump_prob,Tsit5())

```

It ran fine. What are you running?

---

<div class="post-metadata">

### Author: ![mcreel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcreel/32/30088_2.png) [@mcreel](https://discourse.julialang.org/u/mcreel)
#### Post date: [September 27, 2020, 3:06pm UTC](https://discourse.julialang.org/t/variable-rate-jumps-diffeq/42356/8 "2020-09-27T15:06:01Z")

</div>

Hi @tzxiong. Please see ChrisRackaukas’ answer in the other thread. The variable rate problem was more complicated than what I was interested in getting into, so I ended up working with constant rate jumps, and that all worked out well.
