.. 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_userdefined_covariance_model.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_userdefined_covariance_model.py: Create a custom covariance model ================================ .. GENERATED FROM PYTHON SOURCE LINES 7-12 This example illustrates how the user can define his own covariance model in both cases: - Case 1: stationary covariance model, - Case 2: any covariance model. .. GENERATED FROM PYTHON SOURCE LINES 14-19 .. code-block:: Python import openturns as ot import openturns.viewer as viewer from matplotlib import pyplot as plt import math as m .. GENERATED FROM PYTHON SOURCE LINES 20-40 Case 1: stationary covariance model ----------------------------------- In this example, we show how to build our own stationary covariance modelusing the class :class:`~openturns.UserDefinedStationaryCovarianceModel`. We define the covariance model from: - a mesh :math:`\mathcal{M}` of dimension :math:`n` defined by the vertices :math:`(\vect{\tau}_0,\dots, \vect{\tau}_{N-1})` and the associated simplices, - a collection of covariance matrices stored in the object :class:`~openturns.CovarianceMatrixCollection` denoted by :math:`\vect{C}_0, \dots, \vect{C}_{N-1}`, where :math:`\vect{C}_k \in \mathcal{M}_{d \times d}(\mathbb{R})` for :math:`0 \leq k \leq N-1`. Then we build a stationary covariance function which is a piecewise constant function on :math:`\mathcal{D}` defined by: .. math:: \forall \vect{\tau} \in \mathcal{D}, \, C^{stat}(\vect{\tau}) = \vect{C}_k where :math:`k` is such that :math:`\vect{\tau}_k` is the vertex of :math:`\mathcal{M}` the nearest to :math:`\vect{t}.` .. GENERATED FROM PYTHON SOURCE LINES 42-43 We first create the time grid and the covariance function .. GENERATED FROM PYTHON SOURCE LINES 43-60 .. code-block:: Python t0 = 0.0 dt = 0.5 N = int((20.0 - t0) / dt) mesh = ot.RegularGrid(t0, dt, N) def gamma(tau): return 1.0 / (1.0 + tau * tau) # We create the collection of :class:`~openturns.SquareMatrix`. coll = ot.SquareMatrixCollection() for k in range(N): t = mesh.getValue(k) matrix = ot.SquareMatrix([[gamma(t)]]) coll.add(matrix) .. GENERATED FROM PYTHON SOURCE LINES 61-62 We create the final stationary covariance model. .. GENERATED FROM PYTHON SOURCE LINES 62-64 .. code-block:: Python covmodel = ot.UserDefinedStationaryCovarianceModel(mesh, coll) .. GENERATED FROM PYTHON SOURCE LINES 65-66 We can draw the covariance function. .. GENERATED FROM PYTHON SOURCE LINES 66-80 .. code-block:: Python x = ot.Sample(N, 2) for k in range(N): t = mesh.getValue(k) x[k, 0] = t value = covmodel(t) x[k, 1] = value[0, 0] curve = ot.Curve(x, "User Model") myGraph = ot.Graph("User covariance model", "Time", "Covariance function", True) myGraph.add(curve) myGraph.setLegendPosition("upper right") view = viewer.View(myGraph) .. image-sg:: /auto_probabilistic_modeling/stochastic_processes/images/sphx_glr_plot_userdefined_covariance_model_001.svg :alt: User covariance model :srcset: /auto_probabilistic_modeling/stochastic_processes/images/sphx_glr_plot_userdefined_covariance_model_001.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 81-86 Case 2: any covariance model ---------------------------- In this example, we build a covariance model from a time grid and a covariance function depending on :math:`s,t)`, using the class :class:`~openturns.UserDefinedCovarianceModel`. .. GENERATED FROM PYTHON SOURCE LINES 86-95 .. code-block:: Python N = 32 a = 4.0 mesh = ot.IntervalMesher([N]).build(ot.Interval(-a, a)) def C(s, t): return m.exp(-4.0 * abs(s - t) / (1 + (s * s + t * t))) .. GENERATED FROM PYTHON SOURCE LINES 96-97 The, we create the covariance matrix on the mesh. .. GENERATED FROM PYTHON SOURCE LINES 97-104 .. code-block:: Python covariance = ot.CovarianceMatrix(mesh.getVerticesNumber()) for k in range(mesh.getVerticesNumber()): t = mesh.getVertices()[k] for ll in range(k + 1): s = mesh.getVertices()[ll] covariance[k, ll] = C(s[0], t[0]) .. GENERATED FROM PYTHON SOURCE LINES 105-106 The, we create the final non stationary covariance model. .. GENERATED FROM PYTHON SOURCE LINES 106-108 .. code-block:: Python covmodel = ot.UserDefinedCovarianceModel(mesh, covariance) .. GENERATED FROM PYTHON SOURCE LINES 109-110 We can draw the covariance model as a function: first, we define the function to draw. .. GENERATED FROM PYTHON SOURCE LINES 110-119 .. code-block:: Python def f(x): return [covmodel([x[0]], [x[1]])[0, 0]] func = ot.PythonFunction(2, 1, f) func.setDescription(["$s$", "$t$", "$cov$"]) .. GENERATED FROM PYTHON SOURCE LINES 120-121 Then we can draw the function with default options. .. GENERATED FROM PYTHON SOURCE LINES 121-126 .. code-block:: Python cov_graph = func.draw([-a] * 2, [a] * 2, [512] * 2) cov_graph.setLegendPosition("") view = viewer.View(cov_graph) .. image-sg:: /auto_probabilistic_modeling/stochastic_processes/images/sphx_glr_plot_userdefined_covariance_model_002.svg :alt: $cov$ as a function of ($s$,$t$) :srcset: /auto_probabilistic_modeling/stochastic_processes/images/sphx_glr_plot_userdefined_covariance_model_002.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 127-129 We can draw the function in a filled contour graph. sphinx_gallery_thumbnail_number = 3 .. GENERATED FROM PYTHON SOURCE LINES 129-134 .. code-block:: Python cov_graph = func.draw( 0, 1, 0, [0] * 2, [-a] * 2, [a] * 2, [512] * 2, ot.GraphImplementation.NONE, True ) view = viewer.View(cov_graph) .. image-sg:: /auto_probabilistic_modeling/stochastic_processes/images/sphx_glr_plot_userdefined_covariance_model_003.svg :alt: $cov$ as a function of ($s$,$t$) :srcset: /auto_probabilistic_modeling/stochastic_processes/images/sphx_glr_plot_userdefined_covariance_model_003.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 135-136 We can also draw the covariance model as a matrix, using the raw matshow. .. GENERATED FROM PYTHON SOURCE LINES 136-138 .. code-block:: Python _ = plt.matshow(covariance) .. image-sg:: /auto_probabilistic_modeling/stochastic_processes/images/sphx_glr_plot_userdefined_covariance_model_004.svg :alt: plot userdefined covariance model :srcset: /auto_probabilistic_modeling/stochastic_processes/images/sphx_glr_plot_userdefined_covariance_model_004.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 139-144 The, we draw the covariance model as a matrix with the correct axes. To obtain the correct orientation of the y axis we use the origin argument. To obtain the correct graduations we use the extent argument. We also change the colormap used. .. GENERATED FROM PYTHON SOURCE LINES 144-153 .. code-block:: Python pas = 2 * a / (N - 1) plt.matshow( covariance, cmap="gray", origin="lower", extent=(-a - pas / 2, a + pas / 2, -a - pas / 2, a + pas / 2), ) plt.show() .. image-sg:: /auto_probabilistic_modeling/stochastic_processes/images/sphx_glr_plot_userdefined_covariance_model_005.svg :alt: plot userdefined covariance model :srcset: /auto_probabilistic_modeling/stochastic_processes/images/sphx_glr_plot_userdefined_covariance_model_005.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 154-155 Display all figures .. GENERATED FROM PYTHON SOURCE LINES 155-156 .. code-block:: Python viewer.View.ShowAll() .. _sphx_glr_download_auto_probabilistic_modeling_stochastic_processes_plot_userdefined_covariance_model.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_userdefined_covariance_model.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_userdefined_covariance_model.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_userdefined_covariance_model.zip `