.. 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_process_manipulation.py" .. LINE NUMBERS ARE GIVEN BELOW. .. 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_process_manipulation.py: Draw fields =========== .. GENERATED FROM PYTHON SOURCE LINES 6-17 The objective here is to manipulate a multivariate stochastic process :math:`X: \Omega \times \mathcal{D} \rightarrow \mathbb{R}^d`, where :math:`\mathcal{D} \in \mathbb{R}^n` is discretized on the mesh :math:`\mathcal{M}` and exhibit some of the services exposed by the *Process* objects: - ask for the dimension, with the method getOutputDimension - ask for the mesh, with the method getMesh - ask for the mesh as regular 1-d mesh, with the getTimeGrid method - ask for a realization, with the method the getRealization method - ask for a continuous realization, with the getContinuousRealization method - ask for a sample of realizations, with the getSample method - ask for the normality of the process with the isNormal method - ask for the stationarity of the process with the isStationary method .. GENERATED FROM PYTHON SOURCE LINES 19-26 .. 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) .. GENERATED FROM PYTHON SOURCE LINES 27-28 We create a mesh -a time grid- which is a RegularGrid : .. GENERATED FROM PYTHON SOURCE LINES 28-34 .. code-block:: default tMin = 0.0 timeStep = 0.1 n = 100 time_grid = ot.RegularGrid(tMin, timeStep, n) time_grid.setName('time') .. GENERATED FROM PYTHON SOURCE LINES 35-37 We create a Normal process :math:`X_t = (X_t^0, X_t^1, X_t^2)` in dimension 3 with an exponential covariance model. We define the amplitude and the scale of the `ExponentialModel` .. GENERATED FROM PYTHON SOURCE LINES 37-40 .. code-block:: default scale = [4.0] amplitude = [1.0, 2.0, 3.0] .. GENERATED FROM PYTHON SOURCE LINES 41-42 We define a spatial correlation : .. GENERATED FROM PYTHON SOURCE LINES 42-47 .. code-block:: default spatialCorrelation = ot.CorrelationMatrix(3) spatialCorrelation[0, 1] = 0.8 spatialCorrelation[0, 2] = 0.6 spatialCorrelation[1, 2] = 0.1 .. GENERATED FROM PYTHON SOURCE LINES 48-49 The covariance model is now created with : .. GENERATED FROM PYTHON SOURCE LINES 49-51 .. code-block:: default myCovarianceModel = ot.ExponentialModel(scale, amplitude, spatialCorrelation) .. GENERATED FROM PYTHON SOURCE LINES 52-53 Eventually, the process is built with : .. GENERATED FROM PYTHON SOURCE LINES 53-55 .. code-block:: default process = ot.GaussianProcess(myCovarianceModel, time_grid) .. GENERATED FROM PYTHON SOURCE LINES 56-57 The dimension d of the process may be retrieved by .. GENERATED FROM PYTHON SOURCE LINES 57-60 .. code-block:: default dim = process.getOutputDimension() print("Dimension : %d" % dim) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none Dimension : 3 .. GENERATED FROM PYTHON SOURCE LINES 61-62 The underlying mesh of the process is obtained with the `getMesh` method : .. GENERATED FROM PYTHON SOURCE LINES 62-64 .. code-block:: default mesh = process.getMesh() .. GENERATED FROM PYTHON SOURCE LINES 65-66 We have access to peculiar data of the mesh such as the corners : .. GENERATED FROM PYTHON SOURCE LINES 66-69 .. code-block:: default minMesh = mesh.getVertices().getMin()[0] maxMesh = mesh.getVertices().getMax()[0] .. GENERATED FROM PYTHON SOURCE LINES 70-71 We draw it : .. GENERATED FROM PYTHON SOURCE LINES 71-78 .. code-block:: default graph = mesh.draw() graph.setTitle("Time grid (mesh)") graph.setXTitle("t") graph.setYTitle("") view = viewer.View(graph) .. image-sg:: /auto_probabilistic_modeling/stochastic_processes/images/sphx_glr_plot_process_manipulation_001.png :alt: Time grid (mesh) :srcset: /auto_probabilistic_modeling/stochastic_processes/images/sphx_glr_plot_process_manipulation_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 79-80 We can get the time grid of the process when the mesh can be interpreted as a regular time grid : .. GENERATED FROM PYTHON SOURCE LINES 80-83 .. code-block:: default print(process.getTimeGrid()) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none RegularGrid(start=0, step=0.1, n=100) .. GENERATED FROM PYTHON SOURCE LINES 84-85 A typical feature of a process is the generation of a realization of itself : .. GENERATED FROM PYTHON SOURCE LINES 85-87 .. code-block:: default realization = process.getRealization() .. GENERATED FROM PYTHON SOURCE LINES 88-89 Here it is a sample of size :math:`100 \times 4` (100 time steps, 3 spatial cooordinates and the time variable). We are able to draw its marginals, for instance the first (index 0) one :math:`X_t^0`, against the time with no interpolation : .. GENERATED FROM PYTHON SOURCE LINES 89-96 .. code-block:: default interpolate = False graph = realization.drawMarginal(0, interpolate) graph.setTitle("First marginal of a realization of the process") graph.setXTitle("t") graph.setYTitle(r"$X_t^0$") view = viewer.View(graph) .. image-sg:: /auto_probabilistic_modeling/stochastic_processes/images/sphx_glr_plot_process_manipulation_002.png :alt: First marginal of a realization of the process :srcset: /auto_probabilistic_modeling/stochastic_processes/images/sphx_glr_plot_process_manipulation_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 97-98 The same graph, but with interpolated values (default behaviour) : .. GENERATED FROM PYTHON SOURCE LINES 98-105 .. code-block:: default graph = realization.drawMarginal(0) graph.setTitle("First marginal of a realization of the process") graph.setXTitle("t") graph.setYTitle(r"$X_t^0$") view = viewer.View(graph) .. image-sg:: /auto_probabilistic_modeling/stochastic_processes/images/sphx_glr_plot_process_manipulation_003.png :alt: First marginal of a realization of the process :srcset: /auto_probabilistic_modeling/stochastic_processes/images/sphx_glr_plot_process_manipulation_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 106-107 We can build a function representing the process using P1-Lagrange interpolation (when not defined from a functional model). .. GENERATED FROM PYTHON SOURCE LINES 107-109 .. code-block:: default continuousRealization = process.getContinuousRealization() .. GENERATED FROM PYTHON SOURCE LINES 110-111 Once again we draw its first marginal : .. GENERATED FROM PYTHON SOURCE LINES 111-118 .. code-block:: default marginal0 = continuousRealization.getMarginal(0) graph = marginal0.draw(minMesh, maxMesh) graph.setTitle( "First marginal of a P1-Lagrange continuous realization of the process") graph.setXTitle("t") view = viewer.View(graph) .. image-sg:: /auto_probabilistic_modeling/stochastic_processes/images/sphx_glr_plot_process_manipulation_004.png :alt: First marginal of a P1-Lagrange continuous realization of the process :srcset: /auto_probabilistic_modeling/stochastic_processes/images/sphx_glr_plot_process_manipulation_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 119-121 Please note that the `marginal0` object is a function. Consequently we can evaluate it at any point of the domain, say :math:`t_0=3.5678` .. GENERATED FROM PYTHON SOURCE LINES 121-125 .. code-block:: default t0 = 3.5678 print(t0, marginal0([t0])) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none 3.5678 [-0.593188] .. GENERATED FROM PYTHON SOURCE LINES 126-127 Several realizations of the process may be determined at once : .. GENERATED FROM PYTHON SOURCE LINES 127-130 .. code-block:: default number = 10 fieldSample = process.getSample(number) .. GENERATED FROM PYTHON SOURCE LINES 131-132 Let us draw them the first marginal) .. GENERATED FROM PYTHON SOURCE LINES 132-138 .. code-block:: default graph = fieldSample.drawMarginal(0, False) graph.setTitle("First marginal of 10 realizations of the process") graph.setXTitle("t") graph.setYTitle(r"$X_t^0$") view = viewer.View(graph) .. image-sg:: /auto_probabilistic_modeling/stochastic_processes/images/sphx_glr_plot_process_manipulation_005.png :alt: First marginal of 10 realizations of the process :srcset: /auto_probabilistic_modeling/stochastic_processes/images/sphx_glr_plot_process_manipulation_005.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 139-140 Same graph, but with interpolated values : .. GENERATED FROM PYTHON SOURCE LINES 140-147 .. code-block:: default graph = fieldSample.drawMarginal(0) graph.setTitle("First marginal of 10 realizations of the process") graph.setXTitle("t") graph.setYTitle(r"$X_t^0$") view = viewer.View(graph) .. image-sg:: /auto_probabilistic_modeling/stochastic_processes/images/sphx_glr_plot_process_manipulation_006.png :alt: First marginal of 10 realizations of the process :srcset: /auto_probabilistic_modeling/stochastic_processes/images/sphx_glr_plot_process_manipulation_006.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 148-155 Miscellanies ------------ We can extract any marginal of the process with the `getMarginal` method. Beware the numerotation begins at 0 ! It may be not implemented yet for some processes. The extracted marginal is a 1D gaussian process : .. GENERATED FROM PYTHON SOURCE LINES 155-157 .. code-block:: default print(process.getMarginal([1])) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none GaussianProcess(trend=[x0]->[0.0], covariance=ExponentialModel(scale=[4], amplitude=[2], no spatial correlation)) .. GENERATED FROM PYTHON SOURCE LINES 158-159 If we extract simultaneously two indices we build a 2D gaussian process : .. GENERATED FROM PYTHON SOURCE LINES 159-161 .. code-block:: default print(process.getMarginal([0, 2])) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none GaussianProcess(trend=[x0]->[0.0,0.0], covariance=ExponentialModel(scale=[4], amplitude=[1,3], spatial correlation= [[ 1 0.6 ] [ 0.6 1 ]])) .. GENERATED FROM PYTHON SOURCE LINES 162-163 We can check whether the process is normal or not : .. GENERATED FROM PYTHON SOURCE LINES 163-165 .. code-block:: default print("Is normal ? ", process.isNormal()) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none Is normal ? True .. GENERATED FROM PYTHON SOURCE LINES 166-167 and the stationarity as well : .. GENERATED FROM PYTHON SOURCE LINES 167-170 .. code-block:: default print("Is stationary ? ", process.isStationary()) plt.show() .. rst-class:: sphx-glr-script-out Out: .. code-block:: none Is stationary ? True .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.697 seconds) .. _sphx_glr_download_auto_probabilistic_modeling_stochastic_processes_plot_process_manipulation.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_process_manipulation.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_process_manipulation.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_