# Use Sobol sequence in DifferentialEquations.jl / StochasticDiffEq.jl

**URL:** https://discourse.julialang.org/t/use-sobol-sequence-in-differentialequations-jl-stochasticdiffeq-jl/109220
**Category:** Modelling & Simulations
**Tags:** sde, difference-equations
**Created:** [January 24, 2024, 9:32pm UTC](https://discourse.julialang.org/t/use-sobol-sequence-in-differentialequations-jl-stochasticdiffeq-jl/109220 "2024-01-24T21:32:15Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![stochasticguy](https://avatars.discourse-cdn.com/v4/letter/s/278dde/32.png) [@stochasticguy](https://discourse.julialang.org/u/stochasticguy)
#### Post date: [January 24, 2024, 9:32pm UTC](https://discourse.julialang.org/t/use-sobol-sequence-in-differentialequations-jl-stochasticdiffeq-jl/109220/1 "2024-01-24T21:32:15Z")

</div>

Hi everyone,

I’m trying to use the Sobol sequence to simulate Stochastic Differential Equations with StochasticDiffEq.jl / DifferentialEquations.jl. I’m trying to use the Sobol sequence to generate the random numbers because of the improved convergence rates that this sequence introduces in the simulation of this type of equations.

To that end I defined the following:

```julia
using DifferentialEquations 
using StatsFuns
using Sobol
using Plots

abstract type AbstractSobolSequence <: RandomNumbers.AbstractRNG{UInt32} end 

struct SobolSequence{T} <: AbstractSobolSequence 
    s::SobolSeq
end

function Random.randn(rng::SobolSequence, ::Union{Type{Float16}, Type{Float32}, Type{Float64}})    
    return norminvcdf(rng)
end

S0 = 111.0
r = 0.0
σ = 0.1
p = (μ = r, σ = σ)

tspan = (0.0, 1.0)

sbl_rng = SobolSequence(1, Float64)

W_sbl = WienerProcess(0.0, 1.0, 1.0; rng=sbl_rng, reseed=false)

sde_sbl = SDEProblem(f, g, S0, tspan, p; noise=W_sbl)

s1 = solve(EnsembleProblem(sde_sbl), trajectories=1000)
plot(s1)

```

which produces the following:

![paths](https://global.discourse-cdn.com/julialang/original/3X/2/f/2f94d23ee090bee2b2cd622989899cd9fa529425.png)

which is clearly not correct.

I think that this issue could be solved by generating a SobolSequence of dimension n, where n is the number of time steps, but as DifferentialEquations.jl uses adaptive algorithms the number of time steps is not defined beforehand and I can’t do that.

To summarize, my questions :

- Is it possible to use a Sobol sequence with DifferentialEquations.jl?

- Is it possible to use a Sobol sequence with adaptive discretization algorithms?

- If it’s possible which could be the strategy to generate the random numbers?

Thanks in advance!

---

<div class="post-metadata">

### Author: ![SteffenPL](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steffenpl/32/206270_2.png) [@SteffenPL](https://discourse.julialang.org/u/SteffenPL)
#### Post date: [January 25, 2024, 10:49am UTC](https://discourse.julialang.org/t/use-sobol-sequence-in-differentialequations-jl-stochasticdiffeq-jl/109220/2 "2024-01-25T10:49:47Z")

</div>

Maybe my math is a bit rusty, but the Sobol sequence is primarily used in Quasi Monte-Carlo-type applications to generate advantageous pseudorandom numbers in a hypercube. Usually to estimate expectations/integrals such as \int\_{[0,1]^d} f(x) \, \mathrm{d} x\approx \frac{1}{N} \sum\_{j=1}^N f(x\_j). For such types of applications Sobol numbers can give good convergence. In particular since the points have low discrepancy (cover the whole space quite uniformly everywhere).

But for time-stepping these numbers are not useful as they are correlated (exactly because of the low discrepancy property). That’s why you see the periodic patterns.

---

<div class="post-metadata">

### Author: ![stochasticguy](https://avatars.discourse-cdn.com/v4/letter/s/278dde/32.png) [@stochasticguy](https://discourse.julialang.org/u/stochasticguy)
#### Post date: [January 25, 2024, 4:25pm UTC](https://discourse.julialang.org/t/use-sobol-sequence-in-differentialequations-jl-stochasticdiffeq-jl/109220/3 "2024-01-25T16:25:12Z")

</div>

Thanks for your answer, but I’m not trying to time step the sequence. I’m trying to:

1- Generate numbers using the Sobol sequence  
2- Use Sobol numbers to generate random normal numbers  
3- Make DifferentialEquations.jl to simulate a SDE with these numbers instead of the default random numbers generator.

---

<div class="post-metadata">

### Author: ![SteffenPL](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steffenpl/32/206270_2.png) [@SteffenPL](https://discourse.julialang.org/u/SteffenPL)
#### Post date: [January 25, 2024, 4:35pm UTC](https://discourse.julialang.org/t/use-sobol-sequence-in-differentialequations-jl-stochasticdiffeq-jl/109220/4 "2024-01-25T16:35:53Z")

</div>

The point is that those random numbers are not independent, identically distributed. Hence the generated noise does not satisfy the axioms of a Wiener process.

---

<div class="post-metadata">

### Author: ![tom-plaa](https://avatars.discourse-cdn.com/v4/letter/t/d26b3c/32.png) [@tom-plaa](https://discourse.julialang.org/u/tom-plaa)
#### Post date: [January 25, 2024, 5:47pm UTC](https://discourse.julialang.org/t/use-sobol-sequence-in-differentialequations-jl-stochasticdiffeq-jl/109220/5 "2024-01-25T17:47:49Z")

</div>

My tip is to use [GitHub - SciML/DiffEqNoiseProcess.jl: A library of noise processes for stochastic systems like stochastic differential equations (SDEs) and other systems that are present in scientific machine learning (SciML)](https://github.com/SciML/DiffEqNoiseProcess.jl) to define your own noise process. That way it’s easy to define the rng function without type piracy or creating a new method on `randn`.  
If the numerical results are the same, the problem might lie elsewhere. Or there might not be a problem at all because of what @SteffenPL mentioned. Simulating an sde trajectory is time stepping. If you just want an integral over the distribution of terminal values, that’s a different thing and it is true that you can calculate it by Monte Carlo simulation of multiple sde trajectories and taking the samples of the desired time point; but there are other ways as well.

---

<div class="post-metadata">

### Author: ![stochasticguy](https://avatars.discourse-cdn.com/v4/letter/s/278dde/32.png) [@stochasticguy](https://discourse.julialang.org/u/stochasticguy)
#### Post date: [January 26, 2024, 6:13pm UTC](https://discourse.julialang.org/t/use-sobol-sequence-in-differentialequations-jl-stochasticdiffeq-jl/109220/6 "2024-01-26T18:13:30Z")

</div>

Thanks for answer.

I’m actually doing that when I define the WiennerProcess and pass the SobolSequence as rng. In addition, I defined the randn function which is called in DiffEqNoiseProcess.jl when sampling the random numbers to generate the WienerProcess.

---

<div class="post-metadata">

### Author: ![tom-plaa](https://avatars.discourse-cdn.com/v4/letter/t/d26b3c/32.png) [@tom-plaa](https://discourse.julialang.org/u/tom-plaa)
#### Post date: [January 29, 2024, 11:30am UTC](https://discourse.julialang.org/t/use-sobol-sequence-in-differentialequations-jl-stochasticdiffeq-jl/109220/7 "2024-01-29T11:30:35Z")

</div>

My suggestion was for you to define a different process with its own random sampling, not to replace the Wiener’s random sampling function. Check out [Abstract Noise Processes · DiffEqNoiseProcess.jl](https://docs.sciml.ai/DiffEqNoiseProcess/stable/abstract_noise_processes/#The-Standard-AbstractNoiseProcess)

It should be equivalent to what you did but it could be a sanity check.

---

<div class="post-metadata">

### Author: ![Alec\_Loudenback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alec_loudenback/32/278_2.png) [@Alec\_Loudenback](https://discourse.julialang.org/u/Alec_Loudenback)
#### Post date: [May 14, 2024, 2:23am UTC](https://discourse.julialang.org/t/use-sobol-sequence-in-differentialequations-jl-stochasticdiffeq-jl/109220/8 "2024-05-14T02:23:36Z")

</div>

I’m also interested in this - did you ever get something working?

---

<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: [May 14, 2024, 1:31pm UTC](https://discourse.julialang.org/t/use-sobol-sequence-in-differentialequations-jl-stochasticdiffeq-jl/109220/9 "2024-05-14T13:31:21Z")

</div>

I’m not sure what the question is there. What was asked was mathematically non-sensical as it wouldn’t be a Weiner process?
