.. 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-21 .. code-block:: Python import openturns as ot import openturns.viewer as viewer from numpy import square .. GENERATED FROM PYTHON SOURCE LINES 22-26 Create a Kronecker covariance function -------------------------------------- First, define the scalar correlation function :math:`\rho`. .. GENERATED FROM PYTHON SOURCE LINES 28-31 .. code-block:: Python theta = [2.0] rho = ot.MaternModel(theta, 1.5) .. GENERATED FROM PYTHON SOURCE LINES 32-33 Second, define the covariance matrix of the outputs. .. GENERATED FROM PYTHON SOURCE LINES 35-41 .. 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 42-43 Use these ingredients to build the :class:`~openturns.KroneckerCovarianceModel`. .. GENERATED FROM PYTHON SOURCE LINES 45-47 .. code-block:: Python kronecker = ot.KroneckerCovarianceModel(rho, C) .. GENERATED FROM PYTHON SOURCE LINES 48-52 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 54-56 .. code-block:: Python gp = ot.GaussianProcess(kronecker, ot.RegularGrid(0.0, 0.1, 100)) .. GENERATED FROM PYTHON SOURCE LINES 57-58 Sample and draw a realization of the Gaussian process. .. GENERATED FROM PYTHON SOURCE LINES 60-70 .. code-block:: Python ot.RandomGenerator.SetSeed(5) realization = gp.getRealization() graph = realization.drawMarginal(0) graph.add(realization.drawMarginal(1)) graph.setYTitle("") graph.setTitle("") 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.svg :alt: plot kronecker covmodel :srcset: /auto_probabilistic_modeling/stochastic_processes/images/sphx_glr_plot_kronecker_covmodel_001.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 71-72 Draw the trajectory on the complex plane. .. GENERATED FROM PYTHON SOURCE LINES 74-84 .. 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.svg :alt: Trajectory on the complex plane :srcset: /auto_probabilistic_modeling/stochastic_processes/images/sphx_glr_plot_kronecker_covmodel_002.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 85-93 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 95-99 .. 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 100-104 .. 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 105-106 The diagonal of the correlation matrix is by definition filled with ones. .. GENERATED FROM PYTHON SOURCE LINES 108-111 .. 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 112-115 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 117-120 .. 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 121-122 Changing the output correlation matrix does not change the amplitudes. .. GENERATED FROM PYTHON SOURCE LINES 124-127 .. 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 128-132 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 134-145 .. 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.setLegends(["Y1", "Y2"]) graph.setLegendPosition("upper left") _ = viewer.View(graph) .. image-sg:: /auto_probabilistic_modeling/stochastic_processes/images/sphx_glr_plot_kronecker_covmodel_003.svg :alt: plot kronecker covmodel :srcset: /auto_probabilistic_modeling/stochastic_processes/images/sphx_glr_plot_kronecker_covmodel_003.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 146-155 .. 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.svg :alt: Trajectory on the complex plane :srcset: /auto_probabilistic_modeling/stochastic_processes/images/sphx_glr_plot_kronecker_covmodel_004.svg :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 ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_kronecker_covmodel.zip `