.. 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_discrete_markov_chain_process.py:
Create a discrete Markov chain process
======================================
This example details first how to create and manipulate a discrete Markov chain.
A discrete Markov chain :math:`X: \Omega \times \mathcal{D} \rightarrow E`, where :math:`E = [\![ 0,...,p-1]\!]` is a process
where :math:`\mathcal{D}=\mathbb{R}` discretized on the time grid :math:`(t_k)_{k \geq 0}` such
that:
.. math::
\begin{aligned}
\forall k > 0,\: \mathbb{P} ( X_{t_k} \> | \> X_{t_0},...X_{t_{k-1}} ) = \mathbb{P} ( X_{t_k} \> | \> X_{t_{k-1}} )
\end{aligned}
The transition matrix of the process :math:`\mathcal{M} = (m_{i,j})` can be defined such that:
.. math::
\begin{aligned}
\forall t_k \in \mathcal{D}, \forall i,j < p , \> m_{i+1,j+1} = \mathbb{P} (X_{t_{k+1}} = j \> | \> X_{t_{k}} = i)
\end{aligned}
The library proposes to model it through the object *DiscreteMarkovChain* defined thanks to the origin :math:`X_{t_0}` (which can be either deterministic or uncertain), the transition matrix :math:`\mathcal{M}` 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
ot.Log.Show(ot.Log.NONE)
Define the origin
.. code-block:: default
origin = ot.Dirac(0.0)
Define the transition matrix
.. code-block:: default
transition = ot.SquareMatrix([[0.1, 0.3, 0.6], [0.7, 0.1, 0.2], [0.5, 0.3, 0.2]])
Define an 1-d mesh
.. code-block:: default
tgrid = ot.RegularGrid(0.0, 1.0, 50)
Markov chain definition and realization
.. code-block:: default
process = ot.DiscreteMarkovChain(origin, transition, tgrid)
real = process.getRealization()
graph = real.drawMarginal(0)
graph.setTitle('Discrete Markov chain')
view = viewer.View(graph)
.. image:: /auto_probabilistic_modeling/stochastic_processes/images/sphx_glr_plot_discrete_markov_chain_process_001.png
:alt: Discrete Markov chain
:class: sphx-glr-single-img
Get several realizations
.. code-block:: default
process.setTimeGrid(ot.RegularGrid(0.0,1.0,20))
reals = process.getSample(3)
graph = reals.drawMarginal(0)
graph.setTitle('Discrete Markov chain, 3 realizations')
view = viewer.View(graph)
.. image:: /auto_probabilistic_modeling/stochastic_processes/images/sphx_glr_plot_discrete_markov_chain_process_002.png
:alt: Discrete Markov chain, 3 realizations
:class: sphx-glr-single-img
Markov chain future 10 steps
.. code-block:: default
future = process.getFuture(10)
graph = future.drawMarginal(0)
graph.setTitle('Markov chain future 10 steps')
view = viewer.View(graph)
.. image:: /auto_probabilistic_modeling/stochastic_processes/images/sphx_glr_plot_discrete_markov_chain_process_003.png
:alt: Markov chain future 10 steps
:class: sphx-glr-single-img
Markov chain 3 different futures
.. code-block:: default
futures = process.getFuture(10,3)
graph = futures.drawMarginal(0)
graph.setTitle('Three Markov chain futures, 10 steps')
view = viewer.View(graph)
plt.show()
.. image:: /auto_probabilistic_modeling/stochastic_processes/images/sphx_glr_plot_discrete_markov_chain_process_004.png
:alt: Three Markov chain futures, 10 steps
:class: sphx-glr-single-img
.. rst-class:: sphx-glr-timing
**Total running time of the script:** ( 0 minutes 0.312 seconds)
.. _sphx_glr_download_auto_probabilistic_modeling_stochastic_processes_plot_discrete_markov_chain_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_discrete_markov_chain_process.py `
.. container:: sphx-glr-download sphx-glr-download-jupyter
:download:`Download Jupyter notebook: plot_discrete_markov_chain_process.ipynb `
.. only:: html
.. rst-class:: sphx-glr-signature
`Gallery generated by Sphinx-Gallery `_