.. 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_userdefined_covariance_model.py:
Create a custom covariance model
================================
This example illustrates how the user can define his own covariance model.
.. 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)
Create the time grid
.. code-block:: default
N = 32
a = 4.0
mesh = ot.IntervalMesher([N]).build(ot.Interval(-a, a))
Create the covariance function at (s,t)
.. code-block:: default
def C(s, t):
return m.exp( -4.0 * abs(s - t) / (1 + (s * s + t * t)))
Create the large covariance matrix
.. code-block:: default
covariance = ot.CovarianceMatrix(mesh.getVerticesNumber())
for k in range(mesh.getVerticesNumber()):
t = mesh.getVertices()[k]
for l in range(k + 1):
s = mesh.getVertices()[l]
covariance[k, l] = C(s[0], t[0])
Create the covariance model
.. code-block:: default
covmodel = ot.UserDefinedCovarianceModel(mesh, covariance)
Draw the covariance model
.. code-block:: default
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:: /auto_probabilistic_modeling/stochastic_processes/images/sphx_glr_plot_userdefined_covariance_model_001.png
:alt: $cov$ as a function of ($s$,$t$)
:class: sphx-glr-single-img
.. rst-class:: sphx-glr-timing
**Total running time of the script:** ( 0 minutes 1.617 seconds)
.. _sphx_glr_download_auto_probabilistic_modeling_stochastic_processes_plot_userdefined_covariance_model.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_userdefined_covariance_model.py `
.. container:: sphx-glr-download sphx-glr-download-jupyter
:download:`Download Jupyter notebook: plot_userdefined_covariance_model.ipynb `
.. only:: html
.. rst-class:: sphx-glr-signature
`Gallery generated by Sphinx-Gallery `_