# How does one have multiple random variables in an SDEProblem?

**URL:** <https://discourse.julialang.org/t/how-does-one-have-multiple-random-variables-in-an-sdeproblem/122066>\
**Category:** General Usage\
**Tags:** diffeq, sde\
**Created:** [October 31, 2024, 11:19am UTC](https://discourse.julialang.org/t/how-does-one-have-multiple-random-variables-in-an-sdeproblem/122066 "2024-10-31T11:19:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![AwesomeQuest](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/awesomequest/32/38910_2.png) [@AwesomeQuest](https://discourse.julialang.org/u/AwesomeQuest)\
**Post date:** [October 31, 2024, 11:19am UTC](https://discourse.julialang.org/t/how-does-one-have-multiple-random-variables-in-an-sdeproblem/122066/1 "2024-10-31T11:19:11Z")

</div>

I’m trying to simulate a stochastic state space model which looks something like:

```julia
x_dot(x) = A*x + B*u(x + n) + d

```

`A` and `B` are matrices, `x`, `n`, and `d` are vectors and `u` is an affine function.  
Each of the elements of `n` and `d` are independent random variables.  
How can I implement this as an SDEProblem for DifferentialEquations.jl?

I’ve figured out how to do this for `d` but only for the special case where `d=v*W` where `v` is a vector and `W` is a scalar random variable.

```julia
r = 1
kp = 0.5
kr = 0.5
A = [0 1
	 0 0]
B = [0,1]
u(x) = -kp*x[1] - kr*r
f(x,p,t) = A*x + B*u(x)
v = 0.3*[kp,1]
g(u,p,t) = v

prob = SDEProblem(f,g,[0,0], (0,10))
plot(solve(prob))

```

The documentation for SDE problem mentions that `g` can be a vector such that the equation becomes:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/4/6/46df936c9ea9fa61de5857f2753f08422fdbd050.png)  
But I’ve tried giving `g` as both a vector of vectors and a matrix and it always gives an error. What does `g` need to look like to have multiple independent random variables?

---

<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:** [November 1, 2024, 10:49am UTC](https://discourse.julialang.org/t/how-does-one-have-multiple-random-variables-in-an-sdeproblem/122066/2 "2024-11-01T10:49:03Z")

</div>

> [@AwesomeQuest](#):
>
> But I’ve tried giving `g` as both a vector of vectors and a matrix and it always gives an error. What does `g` need to look like to have multiple independent random variables?

See this tutorial:

> **[Stochastic Differential Equations · DifferentialEquations.jl](https://docs.sciml.ai/DiffEqDocs/stable/tutorials/sde_example/#Example-4:-Systems-of-SDEs-with-Non-Diagonal-Noise)**
>
> Documentation for DifferentialEquations.jl.

---

<div class="post-metadata">

**Author:** ![AwesomeQuest](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/awesomequest/32/38910_2.png) [@AwesomeQuest](https://discourse.julialang.org/u/AwesomeQuest)\
**Post date:** [November 1, 2024, 8:15pm UTC](https://discourse.julialang.org/t/how-does-one-have-multiple-random-variables-in-an-sdeproblem/122066/3 "2024-11-01T20:15:21Z")

</div>

Thank you!  
So what I was missing was the `kwarg` `noise_rate_prototype` which is an argument which is of the same type as the output of `g`.

```julia
r = 1
kp = 0.5
kr = 0.5
A = [0 1
	 0 0]
B = [0,1]
u(x) = -kp*x[1] - kr*r
f(x,p,t) = A*x + B*u(x)
g(u,p,t) = 0.1*[[kp,1] [kp,0.5]]

prob = SDEProblem(f,g,[0,0], (0,10), noise_rate_prototype = zeros(2, 2))
plot(solve(prob))

```
