.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_probabilistic_modeling/stochastic_processes/plot_discrete_markov_chain_process.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` 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 ====================================== .. GENERATED FROM PYTHON SOURCE LINES 7-28 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 :class:`~openturns.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. .. GENERATED FROM PYTHON SOURCE LINES 30-34 .. code-block:: Python import openturns as ot import openturns.viewer as viewer from matplotlib import pyplot as plt .. GENERATED FROM PYTHON SOURCE LINES 35-36 Define a generic function to plot matrices .. GENERATED FROM PYTHON SOURCE LINES 36-63 .. code-block:: Python def plotMatrix(matrix, texts=False, origin=None, colorbar=False, extent=None, **kwargs): """Generic procedure for displaying a matrix with or without text overlay and color bar""" res = plt.matshow(matrix, origin=origin, extent=extent, **kwargs) if texts: if extent is None: extent = (-0.5, matrix.getNbColumns() - 0.5, -0.5, matrix.getNbRows() - 0.5) x_step = (extent[1] - extent[0]) / matrix.getNbColumns() y_step = (extent[3] - extent[2]) / matrix.getNbRows() for i in range(matrix.getNbColumns()): for j in range(matrix.getNbRows()): c = round( matrix[j if origin == "lower" else (matrix.getNbRows() - j - 1), i], 2, ) plt.text( i * x_step + extent[0] + x_step / 2, j * y_step + extent[2] + y_step / 2, str(c), va="center", ha="center", ) if colorbar: plt.colorbar(res) .. GENERATED FROM PYTHON SOURCE LINES 64-65 Define the origin .. GENERATED FROM PYTHON SOURCE LINES 65-67 .. code-block:: Python origin = ot.Dirac(0.0) .. GENERATED FROM PYTHON SOURCE LINES 68-69 Define the transition matrix .. GENERATED FROM PYTHON SOURCE LINES 69-78 .. code-block:: Python transition = ot.SquareMatrix( [ [0.1, 0.3, 0.5, 0.1], [0.6, 0.1, 0.2, 0.1], [0.4, 0.3, 0.1, 0.2], [0.2, 0.0, 0.1, 0.7], ] ) .. GENERATED FROM PYTHON SOURCE LINES 79-80 Visualize the transition matrix .. GENERATED FROM PYTHON SOURCE LINES 80-82 .. code-block:: Python _ = plt.matshow(transition) .. image-sg:: /auto_probabilistic_modeling/stochastic_processes/images/sphx_glr_plot_discrete_markov_chain_process_001.svg :alt: plot discrete markov chain process :srcset: /auto_probabilistic_modeling/stochastic_processes/images/sphx_glr_plot_discrete_markov_chain_process_001.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 83-84 Invert axes and add texts .. GENERATED FROM PYTHON SOURCE LINES 84-95 .. code-block:: Python plotMatrix( transition, cmap="gray", texts=True, origin="lower", colorbar=True, alpha=0.5, vmin=0, vmax=1, ) .. image-sg:: /auto_probabilistic_modeling/stochastic_processes/images/sphx_glr_plot_discrete_markov_chain_process_002.svg :alt: plot discrete markov chain process :srcset: /auto_probabilistic_modeling/stochastic_processes/images/sphx_glr_plot_discrete_markov_chain_process_002.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 96-97 Define an 1-d mesh .. GENERATED FROM PYTHON SOURCE LINES 97-99 .. code-block:: Python tgrid = ot.RegularGrid(0.0, 1.0, 50) .. GENERATED FROM PYTHON SOURCE LINES 100-101 Markov chain definition and realization .. GENERATED FROM PYTHON SOURCE LINES 101-107 .. code-block:: Python process = ot.DiscreteMarkovChain(origin, transition, tgrid) real = process.getRealization() graph = real.drawMarginal(0) graph.setTitle("Discrete Markov chain") view = viewer.View(graph) .. image-sg:: /auto_probabilistic_modeling/stochastic_processes/images/sphx_glr_plot_discrete_markov_chain_process_003.svg :alt: Discrete Markov chain :srcset: /auto_probabilistic_modeling/stochastic_processes/images/sphx_glr_plot_discrete_markov_chain_process_003.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 108-109 Get several realizations .. GENERATED FROM PYTHON SOURCE LINES 109-115 .. code-block:: Python 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-sg:: /auto_probabilistic_modeling/stochastic_processes/images/sphx_glr_plot_discrete_markov_chain_process_004.svg :alt: Discrete Markov chain, 3 realizations :srcset: /auto_probabilistic_modeling/stochastic_processes/images/sphx_glr_plot_discrete_markov_chain_process_004.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 116-117 Markov chain future 10 steps .. GENERATED FROM PYTHON SOURCE LINES 117-122 .. code-block:: Python future = process.getFuture(10) graph = future.drawMarginal(0) graph.setTitle("Markov chain future 10 steps") view = viewer.View(graph) .. image-sg:: /auto_probabilistic_modeling/stochastic_processes/images/sphx_glr_plot_discrete_markov_chain_process_005.svg :alt: Markov chain future 10 steps :srcset: /auto_probabilistic_modeling/stochastic_processes/images/sphx_glr_plot_discrete_markov_chain_process_005.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 123-124 Markov chain 3 different futures .. GENERATED FROM PYTHON SOURCE LINES 124-129 .. code-block:: Python futures = process.getFuture(10, 3) graph = futures.drawMarginal(0) graph.setTitle("Three Markov chain futures, 10 steps") view = viewer.View(graph) plt.show() .. image-sg:: /auto_probabilistic_modeling/stochastic_processes/images/sphx_glr_plot_discrete_markov_chain_process_006.svg :alt: Three Markov chain futures, 10 steps :srcset: /auto_probabilistic_modeling/stochastic_processes/images/sphx_glr_plot_discrete_markov_chain_process_006.svg :class: sphx-glr-single-img .. _sphx_glr_download_auto_probabilistic_modeling_stochastic_processes_plot_discrete_markov_chain_process.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_discrete_markov_chain_process.ipynb ` .. 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-zip :download:`Download zipped: plot_discrete_markov_chain_process.zip `