Matrix SDE problem and dimension mismatch

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:

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.

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

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:

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

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.

yes

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

1 Like

Thanks for your help and insights. :slight_smile: