.. 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 6-19 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 21-27 .. 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 28-29 We create a mesh -a time grid- which is a RegularGrid : .. GENERATED FROM PYTHON SOURCE LINES 29-35 .. 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 36-38 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 38-41 .. code-block:: Python scale = [4.0] amplitude = [1.0, 2.0, 3.0] .. GENERATED FROM PYTHON SOURCE LINES 42-43 We define a spatial correlation : .. GENERATED FROM PYTHON SOURCE LINES 43-48 .. 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 49-50 The covariance model is now created with : .. GENERATED FROM PYTHON SOURCE LINES 50-52 .. code-block:: Python myCovarianceModel = ot.ExponentialModel(scale, amplitude, spatialCorrelation) .. GENERATED FROM PYTHON SOURCE LINES 53-54 Eventually, the process is built with : .. GENERATED FROM PYTHON SOURCE LINES 54-56 .. code-block:: Python process = ot.GaussianProcess(myCovarianceModel, time_grid) .. GENERATED FROM PYTHON SOURCE LINES 57-58 The dimension d of the process may be retrieved by .. GENERATED FROM PYTHON SOURCE LINES 58-61 .. 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 62-63 The underlying mesh of the process is obtained with the `getMesh` method : .. GENERATED FROM PYTHON SOURCE LINES 63-65 .. code-block:: Python mesh = process.getMesh() .. GENERATED FROM PYTHON SOURCE LINES 66-67 We have access to peculiar data of the mesh such as the corners : .. GENERATED FROM PYTHON SOURCE LINES 67-70 .. code-block:: Python minMesh = mesh.getVertices().getMin()[0] maxMesh = mesh.getVertices().getMax()[0] .. GENERATED FROM PYTHON SOURCE LINES 71-72 We draw it : .. GENERATED FROM PYTHON SOURCE LINES 72-79 .. 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 80-81 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 81-84 .. 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 85-86 A typical feature of a process is the generation of a realization of itself : .. GENERATED FROM PYTHON SOURCE LINES 86-88 .. code-block:: Python realization = process.getRealization() .. GENERATED FROM PYTHON SOURCE LINES 89-91 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 91-98 .. 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 99-100 The same graph, but with interpolated values (default behaviour) : .. GENERATED FROM PYTHON SOURCE LINES 100-107 .. 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 108-109 We can build a function representing the process using P1-Lagrange interpolation (when not defined from a functional model). .. GENERATED FROM PYTHON SOURCE LINES 109-111 .. code-block:: Python continuousRealization = process.getContinuousRealization() .. GENERATED FROM PYTHON SOURCE LINES 112-113 Once again we draw its first marginal : .. GENERATED FROM PYTHON SOURCE LINES 113-119 .. 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 120-122 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 122-126 .. 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 127-128 Several realizations of the process may be determined at once : .. GENERATED FROM PYTHON SOURCE LINES 128-131 .. code-block:: Python number = 10 fieldSample = process.getSample(number) .. GENERATED FROM PYTHON SOURCE LINES 132-133 Let us draw them the first marginal) .. GENERATED FROM PYTHON SOURCE LINES 133-139 .. 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 140-141 Same graph, but with interpolated values : .. GENERATED FROM PYTHON SOURCE LINES 141-148 .. 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 149-156 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 156-158 .. 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 159-160 If we extract simultaneously two indices we build a 2D gaussian process : .. GENERATED FROM PYTHON SOURCE LINES 160-162 .. 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 163-164 We can check whether the process is normal or not : .. GENERATED FROM PYTHON SOURCE LINES 164-166 .. 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 167-168 and the stationarity as well : .. GENERATED FROM PYTHON SOURCE LINES 168-171 .. 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 `