.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/gaussian_processes/plot_periodic_kernel.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_examples_gaussian_processes_plot_periodic_kernel.py: Gaussian Process with Period Kernels ==================================== A look at periodic kernels. .. GENERATED FROM PYTHON SOURCE LINES 8-17 .. code-block:: default import numpy as np import tensorflow_probability as tfp import matplotlib.pyplot as plt import seaborn as sns from etudes.gaussian_process import gp_sample_custom, dataframe_from_gp_samples .. GENERATED FROM PYTHON SOURCE LINES 19-38 .. code-block:: default # shortcuts tfd = tfp.distributions kernels = tfp.math.psd_kernels # constants num_features = 1 # dimensionality num_index_points = 256 # nbr of index points num_samples = 8 x_min, x_max = -np.pi, np.pi period = np.float64(2. * np.pi) X_grid = np.linspace(x_min, x_max, num_index_points).reshape(-1, num_features) seed = 23 # set random seed for reproducibility random_state = np.random.RandomState(seed) kernel_cls = kernels.ExpSinSquared kernel = kernel_cls(period=period) .. GENERATED FROM PYTHON SOURCE LINES 39-43 Kernel profile -------------- The exponentiated quadratic kernel is *stationary*. That is, :math:`k(x, x') = k(x, 0)` for all :math:`x, x'`. .. GENERATED FROM PYTHON SOURCE LINES 43-53 .. code-block:: default fig, ax = plt.subplots() ax.plot(X_grid, kernel.apply(X_grid, np.zeros((1, num_features)))) ax.set_xlabel(r'$x$') ax.set_ylabel(r'$k(x, 0)$') plt.show() .. image:: /auto_examples/gaussian_processes/images/sphx_glr_plot_periodic_kernel_001.png :alt: plot periodic kernel :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 54-56 Kernel matrix ------------- .. GENERATED FROM PYTHON SOURCE LINES 56-67 .. code-block:: default fig, ax = plt.subplots() ax.pcolormesh(*np.broadcast_arrays(X_grid, X_grid.T), kernel.matrix(X_grid, X_grid), cmap="cividis") ax.invert_yaxis() ax.set_xlabel(r'$x$') ax.set_ylabel(r'$x$') plt.show() .. image:: /auto_examples/gaussian_processes/images/sphx_glr_plot_periodic_kernel_002.png :alt: plot periodic kernel :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out Out: .. code-block:: none /usr/src/app/examples/gaussian_processes/plot_periodic_kernel.py:59: MatplotlibDeprecationWarning: shading='flat' when X and Y have the same dimensions as C is deprecated since 3.3. Either specify the corners of the quadrilaterals with X and Y, or pass shading='auto', 'nearest' or 'gouraud', or set rcParams['pcolor.shading']. This will become an error two minor releases later. kernel.matrix(X_grid, X_grid), cmap="cividis") .. GENERATED FROM PYTHON SOURCE LINES 68-70 Prior samples ------------- .. GENERATED FROM PYTHON SOURCE LINES 70-73 .. code-block:: default gp = tfd.GaussianProcess(kernel=kernel, index_points=X_grid) samples = gp.sample(num_samples, seed=seed) .. GENERATED FROM PYTHON SOURCE LINES 74-84 .. code-block:: default fig, ax = plt.subplots() ax.plot(X_grid, samples.numpy().T) ax.set_xlabel(r'$x$') ax.set_ylabel(r'$f(x)$') ax.set_title(r'Draws of $f(x)$ from GP prior') plt.show() .. image:: /auto_examples/gaussian_processes/images/sphx_glr_plot_periodic_kernel_003.png :alt: Draws of $f(x)$ from GP prior :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 85-95 .. code-block:: default fig, ax = plt.subplots(subplot_kw=dict(projection='polar')) ax.plot(X_grid, samples.numpy().T) ax.set_xlabel(r'$x$') ax.set_ylabel(r'$f(x)$') ax.set_title(r'Draws of $f(x)$ from GP prior') plt.show() .. image:: /auto_examples/gaussian_processes/images/sphx_glr_plot_periodic_kernel_004.png :alt: Draws of $f(x)$ from GP prior :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 96-99 .. code-block:: default amplitude, length_scale_inv = np.ogrid[1.5:3.6:2j, 10.0:0.5:3j] length_scale = 1.0 / length_scale_inv .. GENERATED FROM PYTHON SOURCE LINES 100-104 .. code-block:: default kernel = kernel_cls(amplitude=amplitude, length_scale=length_scale, period=period) gp = tfd.GaussianProcess(kernel=kernel, index_points=X_grid) gp_samples = gp_sample_custom(gp, num_samples, seed=seed) .. GENERATED FROM PYTHON SOURCE LINES 105-109 .. code-block:: default data = dataframe_from_gp_samples(gp_samples.numpy(), X_grid, amplitude, length_scale, num_samples) data.rename(lambda s: s.replace('_', ' '), axis="columns", inplace=True) .. GENERATED FROM PYTHON SOURCE LINES 110-118 .. code-block:: default g = sns.relplot(x="index point", y="function value", hue="sample", row="amplitude", col="length scale", height=5.0, aspect=1.0, kind="line", data=data, alpha=0.7, linewidth=3.0, facet_kws=dict(subplot_kws=dict(projection='polar'), despine=False)) g.set_titles(row_template=r"amplitude $\sigma={{{row_name:.2f}}}$", col_template=r"lengthscale $\ell={{{col_name:.3f}}}$") g.set_axis_labels(r"$x$", r"$f^{(i)}(x)$") .. image:: /auto_examples/gaussian_processes/images/sphx_glr_plot_periodic_kernel_005.png :alt: amplitude $\sigma={1.50}$ | lengthscale $\ell={0.100}$, amplitude $\sigma={1.50}$ | lengthscale $\ell={0.190}$, amplitude $\sigma={1.50}$ | lengthscale $\ell={2.000}$, amplitude $\sigma={3.60}$ | lengthscale $\ell={0.100}$, amplitude $\sigma={3.60}$ | lengthscale $\ell={0.190}$, amplitude $\sigma={3.60}$ | lengthscale $\ell={2.000}$ :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out Out: .. code-block:: none .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 9.140 seconds) .. _sphx_glr_download_auto_examples_gaussian_processes_plot_periodic_kernel.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_periodic_kernel.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_periodic_kernel.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_