# Simulation of a non stationary stochastic process

**URL:** <https://openturns.discourse.group/t/simulation-of-a-non-stationary-stochastic-process/114>\
**Category:** Methodology\
**Created:** [April 17, 2021, 6:08am UTC](https://openturns.discourse.group/t/simulation-of-a-non-stationary-stochastic-process/114 "2021-04-17T06:08:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![FMangeant](https://yyz2.discourse-cdn.com/free1/user_avatar/openturns.discourse.group/fmangeant/32/133_2.png) [@FMangeant](https://openturns.discourse.group/u/FMangeant)\
**Post date:** [April 17, 2021, 6:08am UTC](https://openturns.discourse.group/t/simulation-of-a-non-stationary-stochastic-process/114/1 "2021-04-17T06:08:33Z")

</div>

Hello Open TURNS team,

Long time without you ;-)!  
I try to simulate a 1D non stationary stochastic process whose variance decreases along time. What is the most suitable way to achieve it?  
Thanks a lot in advance.  
Fabien

---

<div class="post-metadata">

**Author:** ![josephmure](https://yyz2.discourse-cdn.com/free1/user_avatar/openturns.discourse.group/josephmure/32/114_2.png) [@josephmure](https://openturns.discourse.group/u/josephmure)\
**Post date:** [April 17, 2021, 11:37am UTC](https://openturns.discourse.group/t/simulation-of-a-non-stationary-stochastic-process/114/2 "2021-04-17T11:37:00Z")

</div>

Hello @FMangeant, what kind of stochastic process do you want to simulate? In the following, I assume it is a non-stationary Gaussian process.

I think the class you are looking for is [UserDefinedCovarianceModel](https://openturns.github.io/openturns/1.16/user_manual/_generated/openturns.UserDefinedCovarianceModel.html). The doc page contains an example showing how to define your own covariance function.

After this, you can define your [GaussianProcess](https://openturns.github.io/openturns/1.16/user_manual/_generated/openturns.GaussianProcess.html) with:

`myProcess = ot.GaussianProcess(myCovarianceModel, myMesh)`

(It is best to reuse the mesh used to create `myCovarianceModel`.)

You can then obtain one realisation with `myProcess.getRealization()` or several with `myProcess.getSample(10)`.

---

<div class="post-metadata">

**Author:** ![FMangeant](https://yyz2.discourse-cdn.com/free1/user_avatar/openturns.discourse.group/fmangeant/32/133_2.png) [@FMangeant](https://openturns.discourse.group/u/FMangeant)\
**Post date:** [April 18, 2021, 12:41pm UTC](https://openturns.discourse.group/t/simulation-of-a-non-stationary-stochastic-process/114/3 "2021-04-18T12:41:04Z")

</div>

OK, I will try to build it with this your advice. My goal is to build a 1D stochastic process with a mean trend being linear and a random part following a gaussian noise whose variance is also decreasing along the x-axis until a minimum value. Hope this is clear enough…

---

<div class="post-metadata">

**Author:** ![josephmure](https://yyz2.discourse-cdn.com/free1/user_avatar/openturns.discourse.group/josephmure/32/114_2.png) [@josephmure](https://openturns.discourse.group/u/josephmure)\
**Post date:** [April 19, 2021, 7:41am UTC](https://openturns.discourse.group/t/simulation-of-a-non-stationary-stochastic-process/114/4 "2021-04-19T07:41:19Z")

</div>

I have tried implementing something along these lines. Perhaps you can modify this script to suit your needs?

```auto
import numpy as np
import openturns as ot
from openturns.viewer import View

# Define the mesh the Gaussian Process will be discretized on
N = 100
a = 0.1
mesh = ot.RegularGrid(0.0, a, N)

# Create a nonstationary white noise covariance model
def noise(t):
    return 0.2 + 40.0 * np.exp(-0.5 * t)

covariance_matrix = ot.CovarianceMatrix(np.diag(noise(mesh.getValues())))
covariance_model = ot.UserDefinedCovarianceModel(mesh, covariance_matrix)

# Define a linear trend
trend_function = ot.SymbolicFunction('t', '-0.5 * t')
trend = ot.TrendTransform(trend_function, mesh)

# Define the Gaussian process
gp = ot.GaussianProcess(trend, covariance_model, mesh)

# View a few realizations of the process
ot.RandomGenerator.SetSeed(123) # for reproducibility
View(gp.getSample(5).drawMarginal(0))

```

Here is the output:

![image](https://global.discourse-cdn.com/free1/uploads/openturns/original/1X/98fa7b38c183658edcff447d2723b35596c742a0.png)
