
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "auto_examples/gaussian_process/plot_gpr_prior_posterior.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        Click :ref:`here <sphx_glr_download_auto_examples_gaussian_process_plot_gpr_prior_posterior.py>`
        to download the full example code

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_auto_examples_gaussian_process_plot_gpr_prior_posterior.py:


==========================================================================
Illustration of prior and posterior Gaussian process for different kernels
==========================================================================

This example illustrates the prior and posterior of a GPR with different
kernels. Mean, standard deviation, and 10 samples are shown for both prior
and posterior.

.. GENERATED FROM PYTHON SOURCE LINES 10-79



.. rst-class:: sphx-glr-horizontal


    *

      .. image-sg:: /auto_examples/gaussian_process/images/sphx_glr_plot_gpr_prior_posterior_001.png
         :alt: Prior (kernel:  1**2 * RBF(length_scale=1)), Posterior (kernel: 0.594**2 * RBF(length_scale=0.279))  Log-Likelihood: -0.067
         :srcset: /auto_examples/gaussian_process/images/sphx_glr_plot_gpr_prior_posterior_001.png
         :class: sphx-glr-multi-img

    *

      .. image-sg:: /auto_examples/gaussian_process/images/sphx_glr_plot_gpr_prior_posterior_002.png
         :alt: Prior (kernel:  1**2 * RationalQuadratic(alpha=0.1, length_scale=1)), Posterior (kernel: 0.594**2 * RationalQuadratic(alpha=1e+05, length_scale=0.279))  Log-Likelihood: -0.067
         :srcset: /auto_examples/gaussian_process/images/sphx_glr_plot_gpr_prior_posterior_002.png
         :class: sphx-glr-multi-img

    *

      .. image-sg:: /auto_examples/gaussian_process/images/sphx_glr_plot_gpr_prior_posterior_003.png
         :alt: Prior (kernel:  1**2 * ExpSineSquared(length_scale=1, periodicity=3)), Posterior (kernel: 0.799**2 * ExpSineSquared(length_scale=0.791, periodicity=2.87))  Log-Likelihood: 3.394
         :srcset: /auto_examples/gaussian_process/images/sphx_glr_plot_gpr_prior_posterior_003.png
         :class: sphx-glr-multi-img

    *

      .. image-sg:: /auto_examples/gaussian_process/images/sphx_glr_plot_gpr_prior_posterior_004.png
         :alt: Prior (kernel:  0.316**2 * DotProduct(sigma_0=1) ** 2), Posterior (kernel: 0.674**2 * DotProduct(sigma_0=2.13) ** 2)  Log-Likelihood: -7957695978.947
         :srcset: /auto_examples/gaussian_process/images/sphx_glr_plot_gpr_prior_posterior_004.png
         :class: sphx-glr-multi-img

    *

      .. image-sg:: /auto_examples/gaussian_process/images/sphx_glr_plot_gpr_prior_posterior_005.png
         :alt: Prior (kernel:  1**2 * Matern(length_scale=1, nu=1.5)), Posterior (kernel: 0.609**2 * Matern(length_scale=0.484, nu=1.5))  Log-Likelihood: -1.185
         :srcset: /auto_examples/gaussian_process/images/sphx_glr_plot_gpr_prior_posterior_005.png
         :class: sphx-glr-multi-img


.. rst-class:: sphx-glr-script-out

 Out:

 .. code-block:: none


    /build/scikit-learn-ZSX7SD/scikit-learn-0.23.2/.pybuild/cpython3_3.10/build/sklearn/gaussian_process/_gpr.py:504: ConvergenceWarning: lbfgs failed to converge (status=2):
    ABNORMAL_TERMINATION_IN_LNSRCH.

    Increase the number of iterations (max_iter) or scale the data as shown in:
        https://scikit-learn.org/stable/modules/preprocessing.html
      _check_optimize_result("lbfgs", opt_res)
    /build/scikit-learn-ZSX7SD/scikit-learn-0.23.2/.pybuild/cpython3_3.10/build/sklearn/gaussian_process/_gpr.py:504: ConvergenceWarning: lbfgs failed to converge (status=2):
    ABNORMAL_TERMINATION_IN_LNSRCH.

    Increase the number of iterations (max_iter) or scale the data as shown in:
        https://scikit-learn.org/stable/modules/preprocessing.html
      _check_optimize_result("lbfgs", opt_res)
    /build/scikit-learn-ZSX7SD/scikit-learn-0.23.2/.pybuild/cpython3_3.10/build/sklearn/gaussian_process/_gpr.py:370: UserWarning: Predicted variances smaller than 0. Setting those variances to 0.
      warnings.warn("Predicted variances smaller than 0. "






|

.. code-block:: default

    print(__doc__)

    # Authors: Jan Hendrik Metzen <jhm@informatik.uni-bremen.de>
    #
    # License: BSD 3 clause

    import numpy as np

    from matplotlib import pyplot as plt

    from sklearn.gaussian_process import GaussianProcessRegressor
    from sklearn.gaussian_process.kernels import (RBF, Matern, RationalQuadratic,
                                                  ExpSineSquared, DotProduct,
                                                  ConstantKernel)


    kernels = [1.0 * RBF(length_scale=1.0, length_scale_bounds=(1e-1, 10.0)),
               1.0 * RationalQuadratic(length_scale=1.0, alpha=0.1),
               1.0 * ExpSineSquared(length_scale=1.0, periodicity=3.0,
                                    length_scale_bounds=(0.1, 10.0),
                                    periodicity_bounds=(1.0, 10.0)),
               ConstantKernel(0.1, (0.01, 10.0))
                   * (DotProduct(sigma_0=1.0, sigma_0_bounds=(0.1, 10.0)) ** 2),
               1.0 * Matern(length_scale=1.0, length_scale_bounds=(1e-1, 10.0),
                            nu=1.5)]

    for kernel in kernels:
        # Specify Gaussian Process
        gp = GaussianProcessRegressor(kernel=kernel)

        # Plot prior
        plt.figure(figsize=(8, 8))
        plt.subplot(2, 1, 1)
        X_ = np.linspace(0, 5, 100)
        y_mean, y_std = gp.predict(X_[:, np.newaxis], return_std=True)
        plt.plot(X_, y_mean, 'k', lw=3, zorder=9)
        plt.fill_between(X_, y_mean - y_std, y_mean + y_std,
                         alpha=0.2, color='k')
        y_samples = gp.sample_y(X_[:, np.newaxis], 10)
        plt.plot(X_, y_samples, lw=1)
        plt.xlim(0, 5)
        plt.ylim(-3, 3)
        plt.title("Prior (kernel:  %s)" % kernel, fontsize=12)

        # Generate data and fit GP
        rng = np.random.RandomState(4)
        X = rng.uniform(0, 5, 10)[:, np.newaxis]
        y = np.sin((X[:, 0] - 2.5) ** 2)
        gp.fit(X, y)

        # Plot posterior
        plt.subplot(2, 1, 2)
        X_ = np.linspace(0, 5, 100)
        y_mean, y_std = gp.predict(X_[:, np.newaxis], return_std=True)
        plt.plot(X_, y_mean, 'k', lw=3, zorder=9)
        plt.fill_between(X_, y_mean - y_std, y_mean + y_std,
                         alpha=0.2, color='k')

        y_samples = gp.sample_y(X_[:, np.newaxis], 10)
        plt.plot(X_, y_samples, lw=1)
        plt.scatter(X[:, 0], y, c='r', s=50, zorder=10, edgecolors=(0, 0, 0))
        plt.xlim(0, 5)
        plt.ylim(-3, 3)
        plt.title("Posterior (kernel: %s)\n Log-Likelihood: %.3f"
                  % (gp.kernel_, gp.log_marginal_likelihood(gp.kernel_.theta)),
                  fontsize=12)
        plt.tight_layout()

    plt.show()


.. rst-class:: sphx-glr-timing

   **Total running time of the script:** ( 0 minutes  0.778 seconds)


.. _sphx_glr_download_auto_examples_gaussian_process_plot_gpr_prior_posterior.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_gpr_prior_posterior.py <plot_gpr_prior_posterior.py>`



  .. container:: sphx-glr-download sphx-glr-download-jupyter

     :download:`Download Jupyter notebook: plot_gpr_prior_posterior.ipynb <plot_gpr_prior_posterior.ipynb>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
