.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_example/plot_quadrature_weights_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_quadrature_weights_example.py: Bayesian quadrature weighting example ===================================== The aim of this page is to provide a simple example to compute optimal-weights for quadrature on any given sample. .. 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-54 .. 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 def draw_candidates(self, candidate_sample, title='Design of experiments'): fig = d.draw_2D_contour(title, None, distribution) plt.title(title) plt.xlabel("$x_0$") plt.ylabel("$x_1$") plt.scatter(candidate_sample[:, 0], candidate_sample[:, 1], alpha=0.1, label='candidate points ($N={}$)'.format(len(candidate_sample)), color='C7') plt.legend(loc='best') return fig .. GENERATED FROM PYTHON SOURCE LINES 55-58 Dependent bivariate random mixture ---------------------------------- Let us first define an independent bivariate random mixture. .. GENERATED FROM PYTHON SOURCE LINES 58-65 .. 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 66-67 Draw a contour plot of the PDF. .. GENERATED FROM PYTHON SOURCE LINES 67-73 .. 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_quadrature_weights_example_001.png :alt: Bivariate random mixture :srcset: /auto_example/images/sphx_glr_plot_quadrature_weights_example_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 74-77 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 77-82 .. 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_quadrature_weights_example_002.png :alt: Bivariate random mixture :srcset: /auto_example/images/sphx_glr_plot_quadrature_weights_example_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 83-84 Define a kernel. .. GENERATED FROM PYTHON SOURCE LINES 84-89 .. code-block:: default dimension = distribution.getDimension() ker_list = [ot.MaternModel([0.1], [1.0], 2.5)] * dimension kernel = ot.ProductCovarianceModel(ker_list) .. GENERATED FROM PYTHON SOURCE LINES 90-92 We build both Monte Carlo and kernel herding designs representative of the distribution. .. GENERATED FROM PYTHON SOURCE LINES 92-102 .. code-block:: default size = 10 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 103-105 Draw the designs and the empirical representation of the target distribution (a.k.a., candidate set). .. GENERATED FROM PYTHON SOURCE LINES 105-113 .. code-block:: default fig = d.draw_candidates(kh._candidate_set, 'Sampling a bivariate random mixture') plt.scatter(mc_design[:, 0], mc_design[:, 1], label='Monte Carlo (n={})'.format(size), marker='o', color='C5') plt.scatter(kh_design[:, 0], kh_design[:, 1], label='Kernel Herding (n={})'.format(size), marker='o', color='C0') plt.legend(loc='best') #plt.legend(bbox_to_anchor=(0.5, -0.1), loc='upper center') # outside bounds plt.show() .. image-sg:: /auto_example/images/sphx_glr_plot_quadrature_weights_example_003.png :alt: Sampling a bivariate random mixture :srcset: /auto_example/images/sphx_glr_plot_quadrature_weights_example_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 114-118 Bayesian quadrature weights --------------------------- For any given sample and target distribution, let us compute a set of optimal weights for quadrature. .. GENERATED FROM PYTHON SOURCE LINES 118-126 .. code-block:: default bqm = otkd.BayesianQuadratureWeighting( kernel=kernel, distribution=distribution, ) kh_weights = bqm.compute_bayesian_quadrature_weights(kh_design) mc_weights = bqm.compute_bayesian_quadrature_weights(mc_design) .. GENERATED FROM PYTHON SOURCE LINES 127-129 Draw samples with corresponding weights proportionate to the markers sizes. .. GENERATED FROM PYTHON SOURCE LINES 129-138 .. code-block:: default fig = d.draw_candidates(kh._candidate_set, 'Sampling a bivariate random mixture') x_label = '{} sequential design ($n={}$)'.format('kernel herding', len(kh_design)) plt.scatter(mc_design[:, 0], mc_design[:, 1], color='C5', label='Monte Carlo design ($n={}$)'.format(len(mc_design)), s=mc_weights * 1500, marker='o') plt.scatter(kh_design[:, 0], kh_design[:, 1], color='C0', label=x_label, s=kh_weights * 1500, marker='o') for i in range(len(kh_design)): plt.text(kh_design[i][0], kh_design[i][1], "{}".format(i + 1), weight="bold", fontsize=12) plt.legend(loc='best') plt.show() .. image-sg:: /auto_example/images/sphx_glr_plot_quadrature_weights_example_004.png :alt: Sampling a bivariate random mixture :srcset: /auto_example/images/sphx_glr_plot_quadrature_weights_example_004.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 2.111 seconds) .. _sphx_glr_download_auto_example_plot_quadrature_weights_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_quadrature_weights_example.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_quadrature_weights_example.ipynb `