# DifferentialEquations: Gaussian error term in one of the equations makes it extremely slow (and error prone)

**URL:** https://discourse.julialang.org/t/differentialequations-gaussian-error-term-in-one-of-the-equations-makes-it-extremely-slow-and-error-prone/105647
**Category:** Modelling & Simulations
**Tags:** question
**Created:** [November 1, 2023, 10:03am UTC](https://discourse.julialang.org/t/differentialequations-gaussian-error-term-in-one-of-the-equations-makes-it-extremely-slow-and-error-prone/105647 "2023-11-01T10:03:25Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![MvanSteenbergen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mvansteenbergen/32/202866_2.png) [@MvanSteenbergen](https://discourse.julialang.org/u/MvanSteenbergen)
#### Post date: [November 1, 2023, 10:03am UTC](https://discourse.julialang.org/t/differentialequations-gaussian-error-term-in-one-of-the-equations-makes-it-extremely-slow-and-error-prone/105647/1 "2023-11-01T10:03:25Z")

</div>

Hey!

For my thesis, I’m trying to implement [this](https://www.frontiersin.org/articles/10.3389/fpsyg.2023.1099257/full) model in Julia, using the `DifferentialEquations.jl` package. I’ve made a rule based on a set of coupled differential equations to simulate some data. I have no previous knowledge of the subject, so bear in mind that I might have to awkwardly stumble over words to explain my problem:

```julia
function threePlusOneDimensions!(du, u, p, t)

    gauss = 0.2
    Smax, τx, τy, τz, τf, α, β, P, S, Rₛ, L, λᵦ, λₛ = p

    du[1] = (Smax /(1 + exp((Rₛ - u[2]) / λₛ)) - u[1]) / τx
    du[2] = (P / (1 + exp((Rₛ - u[2]) / λₛ)) + u[4] * L - u[1] * u[2] - u[3]) / τy
    du[3] = (S * (α * u[1] + β * u[2]) - u[3]) * gauss / τz
    du[4] = (u[2] - λᵦ * u[4])/ τf
    return nothing
end

```

This is the rule I’m specifying. The `gauss` variable should “integrate the equation as external noise ζ(_t_), set between –1 and 1 with Gaussian distribution.” Without the drawn value, it is very fast. As soon as I add the external noise term, it slows down tremendously. How do I implement this in an idiomatic manner? I copied the full text from the paper explaining the third equation below.

> ### Equation 3: Modeling of perceived environment
> 
> The third equation refers to the environment (or external world) perceived by a patient, modeled by the variable _z_ (Equation 3), which evolves with a time constant τ_z_. It depends on the overal sensitivity level _S_, and the joint effects of symptoms _x_ and the potentiation _y_ respectively pondered by factor α and β. The factors α and β may be positive or negative depending on the type of psychiatric disease considered. _The perceived environment integrates the equation as external noise ζ(t), set between –1 and 1 with Gaussian distribution._ The release occurs with an exponential decay (−_z_).

This part of the paper is also relevant:

> Thirdly, the rate of the noise ζ(_t_) is chosen at 0.01, meaning that the perceived environment variable _z_ changes every 0.01 days (noise will be generated every 14.4 min). It is a compromise between the duration of variability of the symptoms of psychiatric disorders and their environment (i.e., considering a psychological state change every 14.4 min). In other words, the model provides a smoothness of 14.4 min, i.e., informs about potential changes in its variables approximately every quarter of an hour.

How do I do this idiomatically, so that the package properly recognizes that there is a random term? I’ve tried doing it with rand() or through drawing from a distribution, but I cannot do so according to the specifications in the paper without it slowing down tremendously. I wanted to solve it myself, and also tried many other things, asked my supervisors for help as well, but I haven’t been able to proceed for some time now.

---

<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, 2023, 11:56am UTC](https://discourse.julialang.org/t/differentialequations-gaussian-error-term-in-one-of-the-equations-makes-it-extremely-slow-and-error-prone/105647/2 "2023-11-01T11:56:37Z")

</div>

> [@MvanSteenbergen](#):
>
> Thirdly, the rate of the noise ζ(_t_) is chosen at 0.01, meaning that the perceived environment variable _z_ changes every 0.01 days (noise will be generated every 14.4 min). It is a compromise between the duration of variability of the symptoms of psychiatric disorders and their environment (i.e., considering a psychological state change every 14.4 min). In other words, the model provides a smoothness of 14.4 min, i.e., informs about potential changes in its variables approximately every quarter of an hour.

You should implement this as a discrete callback which changes a parameter according this this schedule. I recommend using a PeriodicCallback.

---

<div class="post-metadata">

### Author: ![MvanSteenbergen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mvansteenbergen/32/202866_2.png) [@MvanSteenbergen](https://discourse.julialang.org/u/MvanSteenbergen)
#### Post date: [November 1, 2023, 12:02pm UTC](https://discourse.julialang.org/t/differentialequations-gaussian-error-term-in-one-of-the-equations-makes-it-extremely-slow-and-error-prone/105647/3 "2023-11-01T12:02:45Z")

</div>

Thank you! This is exactly the kind of clear hint I was looking for!
