# Problem for a system of stochastic differential equations (SDE) with multiple Wiener processes

**URL:** <https://discourse.julialang.org/t/problem-for-a-system-of-stochastic-differential-equations-sde-with-multiple-wiener-processes/93828>\
**Category:** General Usage\
**Created:** [January 31, 2023, 5:13pm UTC](https://discourse.julialang.org/t/problem-for-a-system-of-stochastic-differential-equations-sde-with-multiple-wiener-processes/93828 "2023-01-31T17:13:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![canturk](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/canturk/32/45545_2.png) [@canturk](https://discourse.julialang.org/u/canturk)\
**Post date:** [January 31, 2023, 5:13pm UTC](https://discourse.julialang.org/t/problem-for-a-system-of-stochastic-differential-equations-sde-with-multiple-wiener-processes/93828/1 "2023-01-31T17:13:08Z")

</div>

I attempted to solve a system of SDE as in the form below. But I am struggeling with a persisting error message as shown below.

A replicated SDE is as follows:  
 ![Screen Shot 2023-01-31 at 12.04.27 PM](https://global.discourse-cdn.com/julialang/original/3X/1/a/1a5191640485d5e939e70d002f0a46fef156a2b6.png)

Here A, B, C, and D are sparse matrices with size NxN; u is a vector of length N; and dw[i] is ith Wiener process forall i in {B,C,D}.

For implementation purpose, SDE will be restructured as in the form  
 ![Screen Shot 2023-01-31 at 12.07.52 PM](https://global.discourse-cdn.com/julialang/original/3X/7/d/7d94a6cc4213b9ae2e995886ea7b15566d21dfa1.png)

The following is an example code (based on a [post](https://discourse.julialang.org/t/stochastic-differential-equations-basic-question/44200) that generates a WARNING `"There is either an error in your model specification or the true solution is unstable"`.

```julia
using DifferentialEquations, SparseArrays
# Parameters
N = 10; 
tspan = (0.0, 10.0);
ntraj = 2;
# Matrices and initial value
A = sprand(N, N, 0.5); B = sprand(N, N, 0.5); C = sprand(N, N, 0.5); D = sprand(N, N, 0.5)
u0= rand(N);
# Deterministic and stochastic functions
function f!(Au, u, p, t) 
    Au .= A*u
    nothing
end
function g!(Gu, u, p, t)
    Gu[:,1] = B * u
    Gu[:,2] = C * u
    Gu[:,3] = D * u
    nothing
end;
# Construct SDE problem with three Wiener processes
prob = SDEProblem(f!, g!, u0, tspan, noise_rate_prototype=sparse(zeros(N,3)));
# Solve the equation of motion for an arbitrary number of trajectories
# sol = solve(EnsembleProblem(prob), LambaEM(), save_everystep=false, EnsembleThreads(),trajectories=ntraj)
sol = solve(EnsembleProblem(prob), LambaEulerHeun(), save_everystep=false, EnsembleThreads(),trajectories=ntraj)

```

Here is the warning message:

 ![Screen Shot 2023-01-31 at 12.14.43 PM](https://global.discourse-cdn.com/julialang/original/3X/8/f/8f66cca9279e05d42b4ee627e99a0f69461fa235.png)

I was wondering why this error message pops up: Is it because of an error in the model or anyting else?

Any help or suggestion is appreciated.

---

<div class="post-metadata">

**Author:** ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)\
**Post date:** [January 31, 2023, 5:51pm UTC](https://discourse.julialang.org/t/problem-for-a-system-of-stochastic-differential-equations-sde-with-multiple-wiener-processes/93828/2 "2023-01-31T17:51:29Z")

</div>

I think it may actually be unstable. I tried generating several matrices with the method you described and always get eigenvalues \> 1.0, so I expect the solution to grow. Try multiplying all your matrices by `1.0/N`.

```julia
julia> using LinearAlgebra, SparseArrays, Arpack

julia> N = 10
10

julia> A = sprand(N,N, 0.5);

julia> λ, ϕ = eigs(A);

julia> abs.(λ)
6-element Vector{Float64}:
 2.2336327664915077
 1.1642719859775836
 1.1642719859775836
 0.5622555935660684
 0.5622555935660684
 0.45944566554197847

```

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [January 31, 2023, 6:05pm UTC](https://discourse.julialang.org/t/problem-for-a-system-of-stochastic-differential-equations-sde-with-multiple-wiener-processes/93828/3 "2023-01-31T18:05:44Z")

</div>

The eigenvalues should be _negative_ in continuous time.

---

<div class="post-metadata">

**Author:** ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)\
**Post date:** [January 31, 2023, 6:35pm UTC](https://discourse.julialang.org/t/problem-for-a-system-of-stochastic-differential-equations-sde-with-multiple-wiener-processes/93828/4 "2023-01-31T18:35:24Z")

</div>

Oops, you are correct! I should have been looking at

```julia
ulia> real.(λ)
6-element Vector{Float64}:
  2.3685798031036582
  1.2284625269151874
 -0.3457237484386846
 -0.3457237484386846
 -0.7113750263386742
 -0.7113750263386742

```

Which gives the same answer.

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [January 31, 2023, 7:15pm UTC](https://discourse.julialang.org/t/problem-for-a-system-of-stochastic-differential-equations-sde-with-multiple-wiener-processes/93828/5 "2023-01-31T19:15:16Z")

</div>

…ah, I should have been more precise: negative real part, assuming linear deterministisk part and no state dependence on stochastic part…

---

<div class="post-metadata">

**Author:** ![canturk](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/canturk/32/45545_2.png) [@canturk](https://discourse.julialang.org/u/canturk)\
**Post date:** [February 4, 2023, 10:51pm UTC](https://discourse.julialang.org/t/problem-for-a-system-of-stochastic-differential-equations-sde-with-multiple-wiener-processes/93828/6 "2023-02-04T22:51:47Z")

</div>

Thanks for the suggestion but multiplying every matrix with 1/N didn’t help for a convergent solution. However I think if we would be able to normalize the solution vector u on very step during the evolution might fix the issues. So I was wondering whether it is possible to force the SDE solver to normalize in every step.
