# Matrix SDE problem and dimension mismatch

**URL:** <https://discourse.julialang.org/t/matrix-sde-problem-and-dimension-mismatch/101194>\
**Category:** Modelling & Simulations\
**Created:** [July 5, 2023, 2:41am UTC](https://discourse.julialang.org/t/matrix-sde-problem-and-dimension-mismatch/101194 "2023-07-05T02:41:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![simoneb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simoneb/32/43744_2.png) [@simoneb](https://discourse.julialang.org/u/simoneb)\
**Post date:** [July 5, 2023, 2:41am UTC](https://discourse.julialang.org/t/matrix-sde-problem-and-dimension-mismatch/101194/1 "2023-07-05T02:41:20Z")

</div>

Hi, I’m trying to fit an SDE model to elements of a 2x2 symmetric positive-definite matrix. I need to give separate BMs to all the elements, with the constraint that the off-diagonal elements are identical. ie have the same BM. Further, I need to be able to provide separate scaling parameters for each BM. Here is my code:

```julia
using PosDefManifold, LinearAlgebra, DifferentialEquations, Plots;

function drift(du, u, p, t)
    du .= p.alpha * (p.mu .- u) ## Mean reversion
end  

function diffusion(du, u, p, t)
    du[1, 1] = p.sigma
    du[1, 2] = p.sigma/sqrt(2)
    du[2, 1] = du[1, 2]
    du[2, 2] = p.sigma
end
prob = SDEProblem(drift, diffusion, u0, tspan, p=pp, noise_rate_prototype = zeros(2, 2))
sol = solve(prob, EM(), p = pp, dt=dt) ## pp is a tuple of parameters. ie sigma, mu and alpha

```

I get this error:  
ERROR: MethodError: Cannot `convert` an object of type Matrix{Float64} to an object of type Float64. I’ve also been playing with the noise\_rate\_prototype argument and I get a matrix dimension mismatch error. Any help would be appreciated to get the syntax right!

Thanks,  
Simone.

---

<div class="post-metadata">

**Author:** ![simoneb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simoneb/32/43744_2.png) [@simoneb](https://discourse.julialang.org/u/simoneb)\
**Post date:** [July 5, 2023, 7:27am UTC](https://discourse.julialang.org/t/matrix-sde-problem-and-dimension-mismatch/101194/2 "2023-07-05T07:27:04Z")

</div>

I think I figured it out. Since I still want diagonal noise, and I have these drift and diffusion functions:

```julia
function drift(du, u, p, t)
    du .= p.alpha .* (p.mu - u) ## Mean reversion
end ## drift function

function diffusion(du, u, p, t)
    du .= p.sigma
end

```

then I can just pass:

```julia
sigma = [0.1 0.1/sqrt(2); 0.1/sqrt(2) 0.1]

```

as part of the parameter list, just giving me a different multiplier for the BM for each element in the matrix? Seems to work.  
 ![Figure_1](https://global.discourse-cdn.com/julialang/original/3X/f/c/fcdfbf0ff4643e9e2cbe9f2f04a3be7ae8ce7aed.png)

Above are the plots for a 2 x 2 symmetric positive definite matrix. Note that convergence to the stationary distributions is quick and there are 3 traces, although there are 4 in the legend. Two of the traces (the off-diagonal ones) are identical. Actually I think I modelled them separately but just chose to keep one. How to adjust the code so that I don’t model both off-diagonals? Just model one i, j then copy to j, i? This will become relevant in the n x n case.

---

<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 5, 2023, 12:33pm UTC](https://discourse.julialang.org/t/matrix-sde-problem-and-dimension-mismatch/101194/3 "2023-07-05T12:33:04Z")

</div>

> [@simoneb](#):
>
> as part of the parameter list, just giving me a different multiplier for the BM for each element in the matrix? Seems to work.

yes

---

<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 5, 2023, 12:34pm UTC](https://discourse.julialang.org/t/matrix-sde-problem-and-dimension-mismatch/101194/4 "2023-07-05T12:34:07Z")

</div>

> [@simoneb](#):
>
> Two of the traces (the off-diagonal ones) are identical. Actually I think I modelled them separately but just chose to keep one. How to adjust the code so that I don’t model both off-diagonals? Just model one i, j then copy to j, i? This will become relevant in the n x n case.

You can make your `u0` a lower triangular matrix, or keep it a vector and `reshape` as necessary.

---

<div class="post-metadata">

**Author:** ![simoneb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simoneb/32/43744_2.png) [@simoneb](https://discourse.julialang.org/u/simoneb)\
**Post date:** [July 6, 2023, 2:03am UTC](https://discourse.julialang.org/t/matrix-sde-problem-and-dimension-mismatch/101194/5 "2023-07-06T02:03:57Z")

</div>

Thanks for your help and insights. 🙂
