.. 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_kronecker_covmodel.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_kronecker_covmodel.py: Sample trajectories from a Gaussian Process with correlated outputs =================================================================== A :class:`~openturns.KroneckerCovarianceModel` takes a covariance function with 1-d output and makes its output multidimensional. In this example, we use one to define a Gaussian Process with 2 correlated scalar outputs. For that purpose, a covariance matrix for the outputs is needed in addition to a scalar correlation function :math:`\rho`. .. GENERATED FROM PYTHON SOURCE LINES 16-22 .. code-block:: Python import openturns as ot import openturns.viewer as viewer from numpy import square ot.Log.Show(ot.Log.NONE) .. GENERATED FROM PYTHON SOURCE LINES 23-27 Create a Kronecker covariance function -------------------------------------- First, define the scalar correlation function :math:`\rho`. .. GENERATED FROM PYTHON SOURCE LINES 29-32 .. code-block:: Python theta = [2.0] rho = ot.MaternModel(theta, 1.5) .. GENERATED FROM PYTHON SOURCE LINES 33-34 Second, define the covariance matrix of the outputs. .. GENERATED FROM PYTHON SOURCE LINES 36-42 .. code-block:: Python C = ot.CovarianceMatrix(2) C[0, 0] = 1.0 C[1, 1] = 1.5 C[1, 0] = 0.9 print(C) .. rst-class:: sphx-glr-script-out .. code-block:: none [[ 1 0.9 ] [ 0.9 1.5 ]] .. GENERATED FROM PYTHON SOURCE LINES 43-44 Use these ingredients to build the :class:`~openturns.KroneckerCovarianceModel`. .. GENERATED FROM PYTHON SOURCE LINES 46-48 .. code-block:: Python kronecker = ot.KroneckerCovarianceModel(rho, C) .. GENERATED FROM PYTHON SOURCE LINES 49-53 Build a Gaussian Process with Kronecker covariance function ----------------------------------------------------------- Define a :class:`~openturns.GaussianProcess` with null trend using this covariance function. .. GENERATED FROM PYTHON SOURCE LINES 55-57 .. code-block:: Python gp = ot.GaussianProcess(kronecker, ot.RegularGrid(0.0, 0.1, 100)) .. GENERATED FROM PYTHON SOURCE LINES 58-59 Sample and draw a realization of the Gaussian process. .. GENERATED FROM PYTHON SOURCE LINES 61-72 .. code-block:: Python ot.RandomGenerator.SetSeed(5) realization = gp.getRealization() graph = realization.drawMarginal(0) graph.add(realization.drawMarginal(1)) graph.setYTitle("") graph.setTitle("") graph.setColors(ot.Drawable.BuildDefaultPalette(2)) graph.setLegends(["Y1", "Y2"]) graph.setLegendPosition("upper left") _ = viewer.View(graph) .. image-sg:: /auto_probabilistic_modeling/stochastic_processes/images/sphx_glr_plot_kronecker_covmodel_001.png :alt: plot kronecker covmodel :srcset: /auto_probabilistic_modeling/stochastic_processes/images/sphx_glr_plot_kronecker_covmodel_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 73-74 Draw the trajectory on the complex plane. .. GENERATED FROM PYTHON SOURCE LINES 76-86 .. code-block:: Python graph = realization.draw() graph.setXTitle("Real part") graph.setYTitle("Imaginary part") graph.setTitle("Trajectory on the complex plane") diagonal = ot.Curve([-1.5, 1.5], [-1.5, 1.5]) diagonal.setLineStyle("dotdash") diagonal.setColor("grey") graph.add(diagonal) _ = viewer.View(graph, square_axes=True) .. image-sg:: /auto_probabilistic_modeling/stochastic_processes/images/sphx_glr_plot_kronecker_covmodel_002.png :alt: Trajectory on the complex plane :srcset: /auto_probabilistic_modeling/stochastic_processes/images/sphx_glr_plot_kronecker_covmodel_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 87-95 Change the correlation between the outputs ------------------------------------------ By setting covariance matrix of the outputs, we have implicitly set the amplitudes and the correlation matrix of the Kronecker covariance function. The amplitudes are the square roots of the diagonal elements of the covariance matrix. .. GENERATED FROM PYTHON SOURCE LINES 97-101 .. code-block:: Python # Recall C print(C) .. rst-class:: sphx-glr-script-out .. code-block:: none [[ 1 0.9 ] [ 0.9 1.5 ]] .. GENERATED FROM PYTHON SOURCE LINES 102-106 .. code-block:: Python # Print squared amplitudes print(square(kronecker.getAmplitude())) .. rst-class:: sphx-glr-script-out .. code-block:: none [1. 1.5] .. GENERATED FROM PYTHON SOURCE LINES 107-108 The diagonal of the correlation matrix is by definition filled with ones. .. GENERATED FROM PYTHON SOURCE LINES 110-113 .. code-block:: Python output_correlation = kronecker.getOutputCorrelation() print(output_correlation) .. rst-class:: sphx-glr-script-out .. code-block:: none [[ 1 0.734847 ] [ 0.734847 1 ]] .. GENERATED FROM PYTHON SOURCE LINES 114-117 Since the correlation matrix is symmetric and its diagonal necessarily contains ones, we only need to change its upper right (or bottom left) element. .. GENERATED FROM PYTHON SOURCE LINES 119-122 .. code-block:: Python output_correlation[0, 1] = 0.9 print(output_correlation) .. rst-class:: sphx-glr-script-out .. code-block:: none [[ 1 0.9 ] [ 0.9 1 ]] .. GENERATED FROM PYTHON SOURCE LINES 123-124 Changing the output correlation matrix does not change the amplitudes. .. GENERATED FROM PYTHON SOURCE LINES 126-129 .. code-block:: Python kronecker.setOutputCorrelation(output_correlation) print(square(kronecker.getAmplitude())) .. rst-class:: sphx-glr-script-out .. code-block:: none [1. 1.5] .. GENERATED FROM PYTHON SOURCE LINES 130-134 Let us resample a trajectory. To show the effect ot the output correlation change, we use the same random seed in order to get a comparable trajectory. .. GENERATED FROM PYTHON SOURCE LINES 136-148 .. code-block:: Python gp = ot.GaussianProcess(kronecker, ot.RegularGrid(0.0, 0.1, 100)) ot.RandomGenerator.SetSeed(5) realization = gp.getRealization() graph = realization.drawMarginal(0) graph.add(realization.drawMarginal(1)) graph.setYTitle("") graph.setTitle("") graph.setColors(ot.Drawable.BuildDefaultPalette(2)) graph.setLegends(["Y1", "Y2"]) graph.setLegendPosition("upper left") _ = viewer.View(graph) .. image-sg:: /auto_probabilistic_modeling/stochastic_processes/images/sphx_glr_plot_kronecker_covmodel_003.png :alt: plot kronecker covmodel :srcset: /auto_probabilistic_modeling/stochastic_processes/images/sphx_glr_plot_kronecker_covmodel_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 149-158 .. code-block:: Python graph = realization.draw() graph.setXTitle("Real part") graph.setYTitle("Imaginary part") graph.setTitle("Trajectory on the complex plane") diagonal = ot.Curve([-1.5, 1.5], [-1.5, 1.5]) diagonal.setLineStyle("dotdash") diagonal.setColor("grey") graph.add(diagonal) _ = viewer.View(graph, square_axes=True) .. image-sg:: /auto_probabilistic_modeling/stochastic_processes/images/sphx_glr_plot_kronecker_covmodel_004.png :alt: Trajectory on the complex plane :srcset: /auto_probabilistic_modeling/stochastic_processes/images/sphx_glr_plot_kronecker_covmodel_004.png :class: sphx-glr-single-img .. _sphx_glr_download_auto_probabilistic_modeling_stochastic_processes_plot_kronecker_covmodel.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_kronecker_covmodel.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_kronecker_covmodel.py `