.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_example/plot_kernel_herding_example.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_example_plot_kernel_herding_example.py: Kernel herding examples ======================= The aim of this page is to provide simple examples where kernel herding is applied to multivariate random inputs with or without a dependency structure. .. GENERATED FROM PYTHON SOURCE LINES 8-14 .. code-block:: default import numpy as np import openturns as ot import otkerneldesign as otkd import matplotlib.pyplot as plt from matplotlib import cm .. GENERATED FROM PYTHON SOURCE LINES 15-16 The following helper class will make plotting easier. .. GENERATED FROM PYTHON SOURCE LINES 16-45 .. code-block:: default class DrawFunctions: def __init__(self): dim = 2 self.grid_size = 100 lowerbound = [0.] * dim upperbound = [1.] * dim mesher = ot.IntervalMesher([self.grid_size-1] * dim) interval = ot.Interval(lowerbound, upperbound) mesh = mesher.build(interval) self.nodes = mesh.getVertices() self.X0, self.X1 = np.array(self.nodes).T.reshape(2, self.grid_size, self.grid_size) def draw_2D_contour(self, title, function=None, distribution=None, colorbar=cm.coolwarm): fig = plt.figure(figsize=(7, 6)) if distribution is not None: Zpdf = np.array(distribution.computePDF(self.nodes)).reshape(self.grid_size, self.grid_size) nb_isocurves = 9 contours = plt.contour(self.X0, self.X1, Zpdf, nb_isocurves, colors='black', alpha=0.6) plt.clabel(contours, inline=True, fontsize=8) if function is not None: Z = np.array(function(self.nodes)).reshape(self.grid_size, self.grid_size) plt.contourf(self.X0, self.X1, Z, 18, cmap=colorbar) plt.colorbar() plt.title(title) plt.xlabel("$x_0$") plt.ylabel("$x_1$") return fig .. GENERATED FROM PYTHON SOURCE LINES 46-48 Independent bivariate random mixture ------------------------------------ .. GENERATED FROM PYTHON SOURCE LINES 48-55 .. code-block:: default modes = [ot.Normal(0.3, 0.12), ot.Normal(0.7, 0.1)] weight = [0.4, 1.0] mixture = ot.Mixture(modes, weight) normal = ot.Normal(0.6, 0.15) distribution = ot.ComposedDistribution([mixture, normal]) .. GENERATED FROM PYTHON SOURCE LINES 56-57 Draw a contour plot of the PDF. .. GENERATED FROM PYTHON SOURCE LINES 57-63 .. code-block:: default d = DrawFunctions() fig = d.draw_2D_contour('Bivariate random mixture', None, distribution) plt.show() .. image-sg:: /auto_example/images/sphx_glr_plot_kernel_herding_example_001.png :alt: Bivariate random mixture :srcset: /auto_example/images/sphx_glr_plot_kernel_herding_example_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 64-65 First, sample from the distribution to get a Monte-Carlo design. .. GENERATED FROM PYTHON SOURCE LINES 65-70 .. code-block:: default dimension = distribution.getDimension() size = 20 mc_design = distribution.getSample(size) .. GENERATED FROM PYTHON SOURCE LINES 71-72 Define a kernel. .. GENERATED FROM PYTHON SOURCE LINES 72-76 .. code-block:: default ker_list = [ot.MaternModel([0.1], [1.0], 2.5)] * dimension kernel = ot.ProductCovarianceModel(ker_list) .. GENERATED FROM PYTHON SOURCE LINES 77-78 Build a kernel herding-based design representative of the distribution. .. GENERATED FROM PYTHON SOURCE LINES 78-86 .. code-block:: default kh = otkd.KernelHerding( kernel=kernel, candidate_set_size=2 ** 12, distribution=distribution ) kh_design = kh.select_design(size) .. GENERATED FROM PYTHON SOURCE LINES 87-91 Because the copula of the distribution is independent and we used a product kernel, the :class:`~otkerneldesign.KernelHerdingTensorized` class can do this in a computationally more efficient way. .. GENERATED FROM PYTHON SOURCE LINES 91-100 .. code-block:: default kht = otkd.KernelHerdingTensorized( kernel=kernel, candidate_set_size=2 ** 12, distribution=distribution ) kht_design = kht.select_design(size) .. GENERATED FROM PYTHON SOURCE LINES 101-102 Draw the designs. .. GENERATED FROM PYTHON SOURCE LINES 102-111 .. code-block:: default fig = d.draw_2D_contour('Sampling a bivariate random mixture', None, distribution) plt.scatter(mc_design[:, 0], mc_design[:, 1], label='Monte Carlo (n={})'.format(size), marker='o', alpha=0.5) plt.scatter(kh_design[:, 0], kh_design[:, 1], label='Kernel Herding (n={})'.format(size), marker='X', color='C1') plt.scatter(kht_design[:, 0], kht_design[:, 1], label='Kernel Herding Tensorized (n={})'.format(size), marker='^', color='C2') plt.legend() #plt.legend(bbox_to_anchor=(0.5, -0.1), loc='upper center') # outside bounds plt.show() .. image-sg:: /auto_example/images/sphx_glr_plot_kernel_herding_example_002.png :alt: Sampling a bivariate random mixture :srcset: /auto_example/images/sphx_glr_plot_kernel_herding_example_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 112-117 Dependent bivariate random mixture ---------------------------------- Using the same example, we can add a Copula as a dependency structure. Note that the :class:`~otkerneldesign.KernelHerdingTensorized` class cannot be used in this case. .. GENERATED FROM PYTHON SOURCE LINES 117-122 .. code-block:: default distribution.setCopula(ot.ClaytonCopula(2.)) fig = d.draw_2D_contour('Bivariate random mixture', None, distribution) plt.show() .. image-sg:: /auto_example/images/sphx_glr_plot_kernel_herding_example_003.png :alt: Bivariate random mixture :srcset: /auto_example/images/sphx_glr_plot_kernel_herding_example_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 123-124 We build both Monte Carlo and kernel herding designs. .. GENERATED FROM PYTHON SOURCE LINES 124-133 .. code-block:: default mc_design = distribution.getSample(size) kh = otkd.KernelHerding( kernel=kernel, candidate_set_size=2 ** 12, distribution=distribution ) kh_design = kh.select_design(size) .. GENERATED FROM PYTHON SOURCE LINES 134-135 Draw the designs. .. GENERATED FROM PYTHON SOURCE LINES 135-142 .. code-block:: default fig = d.draw_2D_contour('Sampling a bivariate random mixture', None, distribution) plt.scatter(mc_design[:, 0], mc_design[:, 1], label='Monte Carlo (n={})'.format(size), marker='o', alpha=0.5) plt.scatter(kh_design[:, 0], kh_design[:, 1], label='Kernel Herding (n={})'.format(size), marker='X', color='C1') plt.legend() #plt.legend(bbox_to_anchor=(0.5, -0.1), loc='upper center') # outside bounds plt.show() .. image-sg:: /auto_example/images/sphx_glr_plot_kernel_herding_example_004.png :alt: Sampling a bivariate random mixture :srcset: /auto_example/images/sphx_glr_plot_kernel_herding_example_004.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 4.325 seconds) .. _sphx_glr_download_auto_example_plot_kernel_herding_example.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_kernel_herding_example.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_kernel_herding_example.ipynb `