.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_stochastic_processes/plot_gaussian_processes_comparison.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_stochastic_processes_plot_gaussian_processes_comparison.py: Compare covariance models ========================= .. GENERATED FROM PYTHON SOURCE LINES 7-12 The main goal of this example is to briefly review the most important covariance models and compare them in terms of regularity of the trajectories. We first show how to define a covariance model, a temporal grid and a Gaussian process. We first consider the squared exponential covariance model and show how the trajectories are sensitive to its parameters. We show how to define a trend. In the final section, we compare the trajectories from exponential and Matérn covariance models. .. GENERATED FROM PYTHON SOURCE LINES 14-18 References ---------- * Carl Edward Rasmussen and Christopher K. I. Williams (2006) Gaussian Processes for Machine Learning. Chapter 4: "Covariance Functions", www.GaussianProcess.org/gpml .. GENERATED FROM PYTHON SOURCE LINES 20-27 The anisotropic squared exponential model ----------------------------------------- The :class:`~openturns.SquaredExponential` class allows one to define covariance models: * :math:`\sigma\in\mathbb{R}` is the amplitude parameter, * :math:`\boldsymbol{\theta}\in\mathbb{R}^d` is the scale. .. GENERATED FROM PYTHON SOURCE LINES 29-33 .. code-block:: Python import openturns.viewer as otv import openturns as ot import matplotlib.pyplot as plt .. GENERATED FROM PYTHON SOURCE LINES 34-35 Amplitude values .. GENERATED FROM PYTHON SOURCE LINES 35-41 .. code-block:: Python amplitude = [3.5] # Scale values scale = [1.5] # Covariance model myModel = ot.SquaredExponential(scale, amplitude) .. GENERATED FROM PYTHON SOURCE LINES 42-57 Gaussian processes ------------------ In order to create a :class:`~openturns.GaussianProcess`, we must have: * a covariance model, * a grid. Optionnally, we can define a trend (we will see that later in the example). By default, the trend is zero. We consider the domain :math:`\mathcal{D}=[0,10]`. We discretize this domain with 100 cells (which corresponds to 101 nodes), with steps equal to 0.1 starting from 0: .. math:: (x_0=x_{min}=0,\:x_1=0.1,\:\ldots,\:x_n=x_{max}=10). .. GENERATED FROM PYTHON SOURCE LINES 59-67 .. code-block:: Python xmin = 0.0 step = 0.1 n = 100 myTimeGrid = ot.RegularGrid(xmin, step, n + 1) graph = myTimeGrid.draw() graph.setTitle("Regular grid") view = otv.View(graph) .. image-sg:: /auto_stochastic_processes/images/sphx_glr_plot_gaussian_processes_comparison_001.svg :alt: Regular grid :srcset: /auto_stochastic_processes/images/sphx_glr_plot_gaussian_processes_comparison_001.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 68-69 Then we create the Gaussian process (by default the trend is zero). .. GENERATED FROM PYTHON SOURCE LINES 71-73 .. code-block:: Python process = ot.GaussianProcess(myModel, myTimeGrid) .. GENERATED FROM PYTHON SOURCE LINES 74-75 Then we generate 10 trajectores with the `getSample` method. This trajectories are in a :class:`~openturns.ProcessSample`. .. GENERATED FROM PYTHON SOURCE LINES 77-81 .. code-block:: Python nbTrajectories = 10 sample = process.getSample(nbTrajectories) type(sample) .. GENERATED FROM PYTHON SOURCE LINES 82-83 We can draw the trajectories with `drawMarginal`. .. GENERATED FROM PYTHON SOURCE LINES 85-90 .. code-block:: Python graph = sample.drawMarginal(0) graph.setTitle("amplitude=%.3f, scale=%.3f" % (amplitude[0], scale[0])) view = otv.View(graph) .. image-sg:: /auto_stochastic_processes/images/sphx_glr_plot_gaussian_processes_comparison_002.svg :alt: amplitude=3.500, scale=1.500 :srcset: /auto_stochastic_processes/images/sphx_glr_plot_gaussian_processes_comparison_002.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 91-92 In order to make the next examples easier, we define a function which plots a given number of trajectories from a Gaussian process based on a covariance model. .. GENERATED FROM PYTHON SOURCE LINES 95-104 .. code-block:: Python def plotCovarianceModel(myCovarianceModel, myTimeGrid, nbTrajectories): """Plots the given number of trajectories with given covariance model.""" process = ot.GaussianProcess(myCovarianceModel, myTimeGrid) sample = process.getSample(nbTrajectories) graph = sample.drawMarginal(0) graph.setTitle("") return graph .. GENERATED FROM PYTHON SOURCE LINES 105-106 The amplitude parameter sets the variance of the process. A greater amplitude increases the chances of getting larger absolute values of the process. .. GENERATED FROM PYTHON SOURCE LINES 108-115 .. code-block:: Python amplitude = [7.0] scale = [1.5] myModel = ot.SquaredExponential(scale, amplitude) graph = plotCovarianceModel(myModel, myTimeGrid, 10) graph.setTitle("amplitude=%.3f, scale=%.3f" % (amplitude[0], scale[0])) view = otv.View(graph) .. image-sg:: /auto_stochastic_processes/images/sphx_glr_plot_gaussian_processes_comparison_003.svg :alt: amplitude=7.000, scale=1.500 :srcset: /auto_stochastic_processes/images/sphx_glr_plot_gaussian_processes_comparison_003.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 116-117 Modifying the scale parameter is here equivalent to stretch or contract the "time" :math:`x`. .. GENERATED FROM PYTHON SOURCE LINES 119-126 .. code-block:: Python amplitude = [3.5] scale = [0.5] myModel = ot.SquaredExponential(scale, amplitude) graph = plotCovarianceModel(myModel, myTimeGrid, 10) graph.setTitle("amplitude=%.3f, scale=%.3f" % (amplitude[0], scale[0])) view = otv.View(graph) .. image-sg:: /auto_stochastic_processes/images/sphx_glr_plot_gaussian_processes_comparison_004.svg :alt: amplitude=3.500, scale=0.500 :srcset: /auto_stochastic_processes/images/sphx_glr_plot_gaussian_processes_comparison_004.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 127-131 Define the trend ---------------- The trend is a deterministic function. With the :class:`~openturns.GaussianProcess` class, the associated process is the sum of a trend and a Gaussian process with zero mean. .. GENERATED FROM PYTHON SOURCE LINES 133-136 .. code-block:: Python f = ot.SymbolicFunction(["x"], ["2*x"]) fTrend = ot.TrendTransform(f, myTimeGrid) .. GENERATED FROM PYTHON SOURCE LINES 137-142 .. code-block:: Python amplitude = [3.5] scale = [1.5] myModel = ot.SquaredExponential(scale, amplitude) process = ot.GaussianProcess(fTrend, myModel, myTimeGrid) .. GENERATED FROM PYTHON SOURCE LINES 143-144 sphinx_gallery_thumbnail_number = 5 .. GENERATED FROM PYTHON SOURCE LINES 144-150 .. code-block:: Python nbTrajectories = 10 sample = process.getSample(nbTrajectories) graph = sample.drawMarginal(0) graph.setTitle("amplitude=%.3f, scale=%.3f" % (amplitude[0], scale[0])) view = otv.View(graph) .. image-sg:: /auto_stochastic_processes/images/sphx_glr_plot_gaussian_processes_comparison_005.svg :alt: amplitude=3.500, scale=1.500 :srcset: /auto_stochastic_processes/images/sphx_glr_plot_gaussian_processes_comparison_005.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 151-165 Other covariance models ----------------------- There are other covariance models. The models which are used more often are the following: * :class:`~openturns.SquaredExponential`. The generated processes can be derivated in mean square at all orders. * :class:`~openturns.MaternModel`. When :math:`\nu\rightarrow+\infty`, it converges to the squared exponential model. This model can be derivated :math:`k` times only if :math:`k<\nu`. In other words, when :math:`\nu` increases, then the trajectories are more and more regular. The particular case :math:`\nu=1/2` is the exponential model. The most commonly used values are :math:`\nu=3/2` and :math:`\nu=5/2`, which produce trajectories that are, in terms of regularity, in between the squared exponential and the exponential models. * :class:`~openturns.ExponentialModel`. The associated process is continuous, but not differentiable. .. GENERATED FROM PYTHON SOURCE LINES 167-169 The Matérn and exponential models --------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 171-178 .. code-block:: Python amplitude = [1.0] scale = [1.0] nu1, nu2, nu3 = 2.5, 1.5, 0.5 myModel1 = ot.MaternModel(scale, amplitude, nu1) myModel2 = ot.MaternModel(scale, amplitude, nu2) myModel3 = ot.MaternModel(scale, amplitude, nu3) .. GENERATED FROM PYTHON SOURCE LINES 179-184 .. code-block:: Python nbTrajectories = 10 graph1 = plotCovarianceModel(myModel1, myTimeGrid, nbTrajectories) graph2 = plotCovarianceModel(myModel2, myTimeGrid, nbTrajectories) graph3 = plotCovarianceModel(myModel3, myTimeGrid, nbTrajectories) .. GENERATED FROM PYTHON SOURCE LINES 185-196 .. code-block:: Python fig = plt.figure(figsize=(20, 6)) ax1 = fig.add_subplot(1, 3, 1) _ = otv.View(graph1, figure=fig, axes=[ax1]) _ = ax1.set_title("Matern 5/2") ax2 = fig.add_subplot(1, 3, 2) _ = otv.View(graph2, figure=fig, axes=[ax2]) _ = ax2.set_title("Matern 3/2") ax3 = fig.add_subplot(1, 3, 3) _ = otv.View(graph3, figure=fig, axes=[ax3]) _ = ax3.set_title("Matern 1/2") .. image-sg:: /auto_stochastic_processes/images/sphx_glr_plot_gaussian_processes_comparison_006.svg :alt: , Matern 5/2, Matern 3/2, Matern 1/2 :srcset: /auto_stochastic_processes/images/sphx_glr_plot_gaussian_processes_comparison_006.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 197-198 We see than, when :math:`\nu` increases, then the trajectories are smoother and smoother. .. GENERATED FROM PYTHON SOURCE LINES 200-202 .. code-block:: Python myExpModel = ot.ExponentialModel(scale, amplitude) .. GENERATED FROM PYTHON SOURCE LINES 203-207 .. code-block:: Python graph = plotCovarianceModel(myExpModel, myTimeGrid, nbTrajectories) graph.setTitle("Exponential") view = otv.View(graph) .. image-sg:: /auto_stochastic_processes/images/sphx_glr_plot_gaussian_processes_comparison_007.svg :alt: Exponential :srcset: /auto_stochastic_processes/images/sphx_glr_plot_gaussian_processes_comparison_007.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 208-209 We see that the exponential model produces very irregular trajectories. .. GENERATED FROM PYTHON SOURCE LINES 209-210 .. code-block:: Python otv.View.ShowAll() .. _sphx_glr_download_auto_stochastic_processes_plot_gaussian_processes_comparison.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_gaussian_processes_comparison.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_gaussian_processes_comparison.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_gaussian_processes_comparison.zip `