.. 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 :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_process_manipulation.py: Draw fields =========== .. GENERATED FROM PYTHON SOURCE LINES 7-20 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 :class:`~openturns.Process` objects: - ask for the dimension, with the method :meth:`~openturns.Process.getOutputDimension` ; - ask for the mesh, with the method :meth:`~openturns.Process.getMesh` ; - ask for the mesh as regular 1-d mesh, with the :meth:`~openturns.Process.getTimeGrid` method ; - ask for a realization, with the method the :meth:`~openturns.Process.getRealization` method ; - ask for a continuous realization, with the :meth:`~openturns.Process.getContinuousRealization` method ; - ask for a sample of realizations, with the :meth:`~openturns.Process.getSample` method ; - ask for the normality of the process with the :meth:`~openturns.Process.isNormal` method ; - ask for the stationarity of the process with the :meth:`~openturns.Process.isStationary` method. .. GENERATED FROM PYTHON SOURCE LINES 22-28 .. code-block:: Python import openturns as ot import openturns.viewer as viewer from matplotlib import pylab as plt ot.Log.Show(ot.Log.NONE) .. GENERATED FROM PYTHON SOURCE LINES 29-30 We create a mesh -a time grid- which is a :class:`~openturns.RegularGrid` : .. GENERATED FROM PYTHON SOURCE LINES 30-36 .. code-block:: Python tMin = 0.0 timeStep = 0.1 n = 100 time_grid = ot.RegularGrid(tMin, timeStep, n) time_grid.setName("time") .. GENERATED FROM PYTHON SOURCE LINES 37-39 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 :class:`~openturns.ExponentialModel` .. GENERATED FROM PYTHON SOURCE LINES 39-42 .. code-block:: Python scale = [4.0] amplitude = [1.0, 2.0, 3.0] .. GENERATED FROM PYTHON SOURCE LINES 43-44 We define a spatial correlation : .. GENERATED FROM PYTHON SOURCE LINES 44-49 .. code-block:: Python spatialCorrelation = ot.CorrelationMatrix(3) spatialCorrelation[0, 1] = 0.8 spatialCorrelation[0, 2] = 0.6 spatialCorrelation[1, 2] = 0.1 .. GENERATED FROM PYTHON SOURCE LINES 50-51 The covariance model is now created with : .. GENERATED FROM PYTHON SOURCE LINES 51-53 .. code-block:: Python myCovarianceModel = ot.ExponentialModel(scale, amplitude, spatialCorrelation) .. GENERATED FROM PYTHON SOURCE LINES 54-55 Eventually, the process is built with : .. GENERATED FROM PYTHON SOURCE LINES 55-57 .. code-block:: Python process = ot.GaussianProcess(myCovarianceModel, time_grid) .. GENERATED FROM PYTHON SOURCE LINES 58-59 The dimension `d` of the process may be retrieved by .. GENERATED FROM PYTHON SOURCE LINES 59-62 .. code-block:: Python dim = process.getOutputDimension() print("Dimension : %d" % dim) .. rst-class:: sphx-glr-script-out .. code-block:: none Dimension : 3 .. GENERATED FROM PYTHON SOURCE LINES 63-64 The underlying mesh of the process is obtained with the `getMesh` method : .. GENERATED FROM PYTHON SOURCE LINES 64-66 .. code-block:: Python mesh = process.getMesh() .. GENERATED FROM PYTHON SOURCE LINES 67-68 We have access to peculiar data of the mesh such as the corners : .. GENERATED FROM PYTHON SOURCE LINES 68-71 .. code-block:: Python minMesh = mesh.getVertices().getMin()[0] maxMesh = mesh.getVertices().getMax()[0] .. GENERATED FROM PYTHON SOURCE LINES 72-73 We draw it : .. GENERATED FROM PYTHON SOURCE LINES 73-80 .. code-block:: Python 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 81-82 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 82-85 .. code-block:: Python print(process.getTimeGrid()) .. rst-class:: sphx-glr-script-out .. code-block:: none RegularGrid(start=0, step=0.1, n=100) .. GENERATED FROM PYTHON SOURCE LINES 86-87 A typical feature of a process is the generation of a realization of itself : .. GENERATED FROM PYTHON SOURCE LINES 87-89 .. code-block:: Python realization = process.getRealization() .. GENERATED FROM PYTHON SOURCE LINES 90-92 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 92-99 .. code-block:: Python 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 100-101 The same graph, but with interpolated values (default behaviour) : .. GENERATED FROM PYTHON SOURCE LINES 101-108 .. code-block:: Python 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 109-110 We can build a function representing the process using P1-Lagrange interpolation (when not defined from a functional model). .. GENERATED FROM PYTHON SOURCE LINES 110-112 .. code-block:: Python continuousRealization = process.getContinuousRealization() .. GENERATED FROM PYTHON SOURCE LINES 113-114 Once again we draw its first marginal : .. GENERATED FROM PYTHON SOURCE LINES 114-120 .. code-block:: Python 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 121-123 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 123-127 .. code-block:: Python t0 = 3.5678 print(t0, marginal0([t0])) .. rst-class:: sphx-glr-script-out .. code-block:: none 3.5678 [-0.593188] .. GENERATED FROM PYTHON SOURCE LINES 128-129 Several realizations of the process may be determined at once : .. GENERATED FROM PYTHON SOURCE LINES 129-132 .. code-block:: Python number = 10 fieldSample = process.getSample(number) .. GENERATED FROM PYTHON SOURCE LINES 133-134 Let us draw them the first marginal .. GENERATED FROM PYTHON SOURCE LINES 134-140 .. code-block:: Python 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 141-142 Same graph, but with interpolated values : .. GENERATED FROM PYTHON SOURCE LINES 142-149 .. code-block:: Python 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 150-157 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 1-d Gaussian process : .. GENERATED FROM PYTHON SOURCE LINES 157-159 .. code-block:: Python print(process.getMarginal([1])) .. rst-class:: sphx-glr-script-out .. code-block:: none GaussianProcess(trend=[x0]->[0.0], covariance=ExponentialModel(scale=[4], amplitude=[2], no spatial correlation)) .. GENERATED FROM PYTHON SOURCE LINES 160-161 If we extract simultaneously two indices we build a 2-d Gaussian process : .. GENERATED FROM PYTHON SOURCE LINES 161-163 .. code-block:: Python print(process.getMarginal([0, 2])) .. rst-class:: sphx-glr-script-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 164-165 We can check whether the process is Gaussian or not : .. GENERATED FROM PYTHON SOURCE LINES 165-167 .. code-block:: Python print("Is normal ? ", process.isNormal()) .. rst-class:: sphx-glr-script-out .. code-block:: none Is normal ? True .. GENERATED FROM PYTHON SOURCE LINES 168-169 and the stationarity as well : .. GENERATED FROM PYTHON SOURCE LINES 169-172 .. code-block:: Python print("Is stationary ? ", process.isStationary()) plt.show() .. rst-class:: sphx-glr-script-out .. code-block:: none Is stationary ? True .. _sphx_glr_download_auto_probabilistic_modeling_stochastic_processes_plot_process_manipulation.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_process_manipulation.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_process_manipulation.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_process_manipulation.zip `