.. 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 Click :ref:`here ` 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:: default import openturns as ot import openturns.viewer as viewer from numpy import square ot.Log.Show(ot.Log.NONE) .. 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:: default 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:: default 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 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:: default 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:: default 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-71 .. code-block:: default 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("topleft") _ = 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 72-73 Draw the trajectory on the complex plane. .. GENERATED FROM PYTHON SOURCE LINES 75-85 .. code-block:: default 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 86-94 Change the correlation between the outputs ------------------------------------------ By setting covariance matrix of the outputs, we have implicitely 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 96-100 .. code-block:: default # Recall C print(C) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none [[ 1 0.9 ] [ 0.9 1.5 ]] .. GENERATED FROM PYTHON SOURCE LINES 101-105 .. code-block:: default # Print squared amplitudes print(square(kronecker.getAmplitude())) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none [1. 1.5] .. GENERATED FROM PYTHON SOURCE LINES 106-107 The diagonal of the correlation matrix is by definition filled with ones. .. GENERATED FROM PYTHON SOURCE LINES 109-112 .. code-block:: default output_correlation = kronecker.getOutputCorrelation() print(output_correlation) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none [[ 1 0.734847 ] [ 0.734847 1 ]] .. GENERATED FROM PYTHON SOURCE LINES 113-116 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 118-121 .. code-block:: default output_correlation[0, 1] = 0.9 print(output_correlation) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none [[ 1 0.9 ] [ 0.9 1 ]] .. GENERATED FROM PYTHON SOURCE LINES 122-123 Changing the output correlation matrix does not change the amplitudes. .. GENERATED FROM PYTHON SOURCE LINES 125-128 .. code-block:: default kronecker.setOutputCorrelation(output_correlation) print(square(kronecker.getAmplitude())) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none [1. 1.5] .. GENERATED FROM PYTHON SOURCE LINES 129-133 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 135-147 .. code-block:: default 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("topleft") _ = 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 148-157 .. code-block:: default 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 .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.272 seconds) .. _sphx_glr_download_auto_probabilistic_modeling_stochastic_processes_plot_kronecker_covmodel.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_kronecker_covmodel.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_kronecker_covmodel.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_