.. only:: html
.. note::
:class: sphx-glr-download-link-note
Click :ref:`here ` to download the full example code
.. rst-class:: sphx-glr-example-title
.. _sphx_glr_auto_probabilistic_modeling_stochastic_processes_plot_random_walk_process.py:
Create a random walk process
============================
This example details first how to create and manipulate a random walk.
A random walk :math:`X: \Omega \times \mathcal{D} \rightarrow \mathbb{R}^d` is a process
where :math:`\mathcal{D}=\mathbb{R}` discretized on the time grid :math:`(t_i)_{i \geq 0}` such
that:
.. math::
\begin{aligned}
X_{t_0} & = & \underline{x}_{t_0} \\
\forall n>0,\: X_{t_n} & = & X_{t_{n-1}} + \varepsilon_{t_n}
\end{aligned}
where :math:`\underline{x}_0 \in \mathbb{R}^d` and :math:`\varepsilon` is a white noise of
dimension :math:`d`.
The library proposes to model it through the object *RandomWalk* defined
thanks to the origin, the distribution of the white noise and the time
grid.
.. code-block:: default
from __future__ import print_function
import openturns as ot
import openturns.viewer as viewer
from matplotlib import pylab as plt
import math as m
ot.Log.Show(ot.Log.NONE)
Define the origin
.. code-block:: default
origin = [0.0]
Define an 1-d mesh
.. code-block:: default
tgrid = ot.RegularGrid(0.0, 1.0, 500)
1-d random walk and discrete distribution
.. code-block:: default
dist = ot.UserDefined([[-1], [10]],[0.9, 0.1] )
process = ot.RandomWalk(origin, dist, tgrid)
sample = process.getSample(5)
graph = sample.drawMarginal(0)
graph.setTitle('1D Random Walk with discrete steps')
view = viewer.View(graph)
.. image:: /auto_probabilistic_modeling/stochastic_processes/images/sphx_glr_plot_random_walk_process_001.png
:alt: 1D Random Walk with discrete steps
:class: sphx-glr-single-img
1-d random walk and continuous distribution
.. code-block:: default
dist = ot.Normal(0.0, 1.0)
process = ot.RandomWalk(origin, dist, tgrid)
sample = process.getSample(5)
graph = sample.drawMarginal(0)
graph.setTitle('1D Random Walk with continuous steps')
view = viewer.View(graph)
.. image:: /auto_probabilistic_modeling/stochastic_processes/images/sphx_glr_plot_random_walk_process_002.png
:alt: 1D Random Walk with continuous steps
:class: sphx-glr-single-img
Define the origin
.. code-block:: default
origin = [0.0]*2
color palette
.. code-block:: default
pal = ['red', 'cyan', 'blue', 'yellow', 'green']
2-d random walk and discrete distribution
.. code-block:: default
dist = ot.UserDefined([[-1., -2.], [1., 3.]], [0.5, 0.5])
process = ot.RandomWalk(origin, dist, tgrid)
sample = process.getSample(5)
graph = ot.Graph('2D Random Walk with discrete steps', 'X1', 'X2', True)
for i in range(5) :
graph.add(ot.Curve(sample[i], pal[i % len(pal)], 'solid'))
view = viewer.View(graph)
.. image:: /auto_probabilistic_modeling/stochastic_processes/images/sphx_glr_plot_random_walk_process_003.png
:alt: 2D Random Walk with discrete steps
:class: sphx-glr-single-img
2-d random walk and continuous distribution
.. code-block:: default
dist = ot.Normal(2)
process = ot.RandomWalk(origin, dist, tgrid)
sample = process.getSample(5)
graph = ot.Graph('2D Random Walk with continuous steps', 'X1', 'X2', True)
for i in range(5) :
graph.add(ot.Curve(sample[i], pal[i % len(pal)], 'solid'))
view = viewer.View(graph)
plt.show()
.. image:: /auto_probabilistic_modeling/stochastic_processes/images/sphx_glr_plot_random_walk_process_004.png
:alt: 2D Random Walk with continuous steps
:class: sphx-glr-single-img
.. rst-class:: sphx-glr-timing
**Total running time of the script:** ( 0 minutes 0.339 seconds)
.. _sphx_glr_download_auto_probabilistic_modeling_stochastic_processes_plot_random_walk_process.py:
.. only :: html
.. container:: sphx-glr-footer
:class: sphx-glr-footer-example
.. container:: sphx-glr-download sphx-glr-download-python
:download:`Download Python source code: plot_random_walk_process.py `
.. container:: sphx-glr-download sphx-glr-download-jupyter
:download:`Download Jupyter notebook: plot_random_walk_process.ipynb `
.. only:: html
.. rst-class:: sphx-glr-signature
`Gallery generated by Sphinx-Gallery `_