.. 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 6-9 This example illustrates how the user can define his own covariance model. .. GENERATED FROM PYTHON SOURCE LINES 11-18 .. code-block:: Python 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 19-20 Create the time grid .. GENERATED FROM PYTHON SOURCE LINES 20-25 .. code-block:: Python N = 32 a = 4.0 mesh = ot.IntervalMesher([N]).build(ot.Interval(-a, a)) .. GENERATED FROM PYTHON SOURCE LINES 26-27 Create the covariance function at (s,t) .. GENERATED FROM PYTHON SOURCE LINES 27-31 .. code-block:: Python def C(s, t): return m.exp(-4.0 * abs(s - t) / (1 + (s * s + t * t))) .. GENERATED FROM PYTHON SOURCE LINES 32-33 Create the large covariance matrix .. GENERATED FROM PYTHON SOURCE LINES 33-40 .. 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 41-42 Create the covariance model .. GENERATED FROM PYTHON SOURCE LINES 42-45 .. code-block:: Python covmodel = ot.UserDefinedCovarianceModel(mesh, covariance) .. GENERATED FROM PYTHON SOURCE LINES 46-47 Draw the covariance model .. GENERATED FROM PYTHON SOURCE LINES 47-57 .. 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$"]) cov_graph = func.draw([-a] * 2, [a] * 2, [512] * 2) cov_graph.setLegendPosition("") view = viewer.View(cov_graph) plt.show() .. image-sg:: /auto_probabilistic_modeling/stochastic_processes/images/sphx_glr_plot_userdefined_covariance_model_001.png :alt: $cov$ as a function of ($s$,$t$) :srcset: /auto_probabilistic_modeling/stochastic_processes/images/sphx_glr_plot_userdefined_covariance_model_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 2.089 seconds) .. _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 `