# Simulation HJM model via stochastic Diffeq

**URL:** <https://discourse.julialang.org/t/simulation-hjm-model-via-stochastic-diffeq/61089>\
**Category:** New to Julia\
**Tags:** diffeq, forward, finance\
**Created:** [May 13, 2021, 3:05pm UTC](https://discourse.julialang.org/t/simulation-hjm-model-via-stochastic-diffeq/61089 "2021-05-13T15:05:51Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![kirgush](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kirgush/32/25026_2.png) [@kirgush](https://discourse.julialang.org/u/kirgush)\
**Post date:** [May 13, 2021, 3:05pm UTC](https://discourse.julialang.org/t/simulation-hjm-model-via-stochastic-diffeq/61089/1 "2021-05-13T15:05:51Z")

</div>

I want to do forward curve simulation from simplest HJM model using awesome DiffEq package:  
 ![hjm_simplest](https://global.discourse-cdn.com/julialang/original/3X/c/9/c94cb0c8239984d633094ed264cd11ef49cf82c1.png)

```julia
using Parameters
using Statistics
using Printf
using Plots
using DifferentialEquations

α=0.4
σ = 1.4
M = 10
u₀= 10
f(u,p,t) = 0
g(u,p,t) = u * σ * exp(- α * (M-t))
dt = 1
tspan = (0.0,10.0)
prob = SDEProblem(f,g,u₀,(0.0,10.0))	
sol = solve(prob,EM(),dt=dt,save_noise=true)
ensembleprob = EnsembleProblem(prob)
sol = solve(ensembleprob,EnsembleThreads(),trajectories=100)
plotly()
plot(sol,linealpha=0.4)

```

Using my code I can get simulation F(t,) for fixed maturity T = 10:

My code threat T as fixed parameter, but I want to get simulations of the whole forward (for all T) curve at any moment t:

Can I treat T correctly: to get the whole forward curve for each t and not fixed maturity T?

Thanks!

---

<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:** [May 13, 2021, 4:08pm UTC](https://discourse.julialang.org/t/simulation-hjm-model-via-stochastic-diffeq/61089/2 "2021-05-13T16:08:25Z")

</div>

> [@kirgush](#):
>
> My code threat T as fixed parameter, but I want to get simulations of the whole forward (for all T) curve at any moment t:
> 
> Can I treat T correctly: to get the whole forward curve for each t and not fixed maturity T?

Are you saying `T` is a continuous variable itself? As in, you have a PDE?

---

<div class="post-metadata">

**Author:** ![kirgush](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kirgush/32/25026_2.png) [@kirgush](https://discourse.julialang.org/u/kirgush)\
**Post date:** [May 14, 2021, 5:53am UTC](https://discourse.julialang.org/t/simulation-hjm-model-via-stochastic-diffeq/61089/3 "2021-05-14T05:53:40Z")

</div>

Ideally I want `T` to be a continuous variable, to get continious forward curve at any moment.  
But discrete `T` also have a meaning of monthly tradable futures, so it is also possible way.

In case of discrete T I need to use smth like this example about Systems of SDE?:  
[https://diffeq.sciml.ai/stable/tutorials/sde\_example/#Example-3:-Systems-of-SDEs-with-Scalar-Noise](https://diffeq.sciml.ai/stable/tutorials/sde_example/#Example-3:-Systems-of-SDEs-with-Scalar-Noise)

Here is viz of my final aim:

 ![Screenshot 2021-05-14 at 08.49.04](https://global.discourse-cdn.com/julialang/original/3X/0/1/013182223eb34949dde91da269174fb8f096a05d.jpeg)

Source of pics: [https://github.com/omartinsky/HJM/blob/main/hjm.ipynb](https://github.com/omartinsky/HJM/blob/main/hjm.ipynb)

Thanks!

---

<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:** [May 14, 2021, 1:33pm UTC](https://discourse.julialang.org/t/simulation-hjm-model-via-stochastic-diffeq/61089/4 "2021-05-14T13:33:45Z")

</div>

For discrete `T` you can use an ensemble of simulations:

[https://diffeq.sciml.ai/stable/features/ensemble/](https://diffeq.sciml.ai/stable/features/ensemble/)

which seems to be what they did in that notebook. If you want a truly continuous `T` you could represent it as a stochastic partial differential equation, though solving that could get a little tricky (and it would have a different noise than normal so it would be a little odd).

---

<div class="post-metadata">

**Author:** ![kirgush](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kirgush/32/25026_2.png) [@kirgush](https://discourse.julialang.org/u/kirgush)\
**Post date:** [May 17, 2021, 1:24pm UTC](https://discourse.julialang.org/t/simulation-hjm-model-via-stochastic-diffeq/61089/5 "2021-05-17T13:24:27Z")

</div>

Thank you for your answers. I’ll try and write here my results.  
I think it would be useful for users, who want to build quantitative finance models with Julia and DiffEq.

---

<div class="post-metadata">

**Author:** ![mschauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mschauer/32/13946_2.png) [@mschauer](https://discourse.julialang.org/u/mschauer)\
**Post date:** [May 17, 2021, 1:39pm UTC](https://discourse.julialang.org/t/simulation-hjm-model-via-stochastic-diffeq/61089/6 "2021-05-17T13:39:40Z")

</div>

> [@kirgush](#):
>
> ![hjm_simplest](https://global.discourse-cdn.com/julialang/original/3X/c/9/c94cb0c8239984d633094ed264cd11ef49cf82c1.png)

Note that this is a heterogenous community and people will have a hard time helping you from this because we don’t know what does HJM stands for and neither what `z` is and we have to guess that `T` is a parameter which is independent of `z`. I am not sure you want an ensemble problem here, I imagine it can well be that all `F(., M)` are driven by the same noise?

---

<div class="post-metadata">

**Author:** ![kirgush](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kirgush/32/25026_2.png) [@kirgush](https://discourse.julialang.org/u/kirgush)\
**Post date:** [May 17, 2021, 3:38pm UTC](https://discourse.julialang.org/t/simulation-hjm-model-via-stochastic-diffeq/61089/7 "2021-05-17T15:38:46Z")

</div>

HJM model is a general framework for modelling the evolution of interest rates curves:

> **[Heath–Jarrow–Morton framework](https://en.wikipedia.org/wiki/Heath%E2%80%93Jarrow%E2%80%93Morton_framework)**
>
> The Heath–Jarrow–Morton (HJM) framework is a general framework to model the evolution of interest rate curves – instantaneous forward rate curves in particular (as opposed to simple forward rates). When the volatility and drift of the instantaneous forward rate are assumed to be deterministic, this is known as the Gaussian Heath–Jarrow–Morton (HJM) model of forward rates.: 394  For direct modeling of simple forward rates the Brace–Gatarek–Musiela model represents an example.
> The HJM framework or...

This framework was adopted to build commodity forward curves. And my formula is the simplest possible version of this model. Pertrubation `dF(t,T)` is specified as deterministic shape function \sigma multiplied by Gaussian factor `dz`.

T is a parameter which is indepent of `z`. The meaning of T is time to the delivery of commodity.

> I imagine it can well be that all `F(., M)` are driven by the same noise?

yes, this is exactly what I am looking for.

---

<div class="post-metadata">

**Author:** ![mschauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mschauer/32/13946_2.png) [@mschauer](https://discourse.julialang.org/u/mschauer)\
**Post date:** [May 17, 2021, 4:35pm UTC](https://discourse.julialang.org/t/simulation-hjm-model-via-stochastic-diffeq/61089/8 "2021-05-17T16:35:11Z")

</div>

> [@kirgush](#):
>
> [https://diffeq.sciml.ai/stable/tutorials/sde\_example/#Example-3:-Systems-of-SDEs-with-Scalar-Noise](https://diffeq.sciml.ai/stable/tutorials/sde_example/#Example-3:-Systems-of-SDEs-with-Scalar-Noise)

This is then the right thing to follow writing `g` in parallel for all “dimensions” given by the discretisation of `T`.

---

<div class="post-metadata">

**Author:** ![sob](https://avatars.discourse-cdn.com/v4/letter/s/65b543/32.png) [@sob](https://discourse.julialang.org/u/sob)\
**Post date:** [January 5, 2023, 10:55pm UTC](https://discourse.julialang.org/t/simulation-hjm-model-via-stochastic-diffeq/61089/9 "2023-01-05T22:55:08Z")

</div>

@kirgush,

I came across this thread and was wondering if as part of your experiment above, you also found (or created) some code to calibrate an HJM specification to market observables (short rate futures, swaps, and vanilla options on both).
