.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_example/plot_energy_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_energy_example.py: Energy computation example ========================== This page provides an example of energy computation for a 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 Bivariate uniform distribution .. GENERATED FROM PYTHON SOURCE LINES 16-21 .. code-block:: default unifrom = ot.Uniform(0., 1.) dimension = 5 distribution = ot.ComposedDistribution([unifrom] * dimension) .. GENERATED FROM PYTHON SOURCE LINES 22-24 Standard and tensorized kernel herding -------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 24-45 .. code-block:: default size = 300 # Kernel definition theta = 0.3 ker_list = [ot.MaternModel([theta], [1.0], 2.5)] * dimension kernel = ot.ProductCovarianceModel(ker_list) # Kernel herding design kh = otkd.KernelHerding( kernel=kernel, candidate_set_size=2 ** 14, distribution=distribution ) kh_design = kh.select_design(size) # Tensorized kernel herding design kht = otkd.KernelHerdingTensorized( kernel=kernel, candidate_set_size=2 ** 14, distribution=distribution ) kht_design = kht.select_design(size) .. GENERATED FROM PYTHON SOURCE LINES 46-55 Energy convergence ------------------ The main difference between the two classes is the way to compute the target potential function. With independent inputs and a covariance kernel built as the product of one-dimensional kernels, the TensorizedKernelHerding allows to write the multivariate potential as a product of univariate potentials, easing its computation in high dimension. .. GENERATED FROM PYTHON SOURCE LINES 55-64 .. code-block:: default kh_target_energy = kh._target_energy kht_target_energy = kht._target_energy target_energy_aerror = np.abs(kh_target_energy - kht_target_energy) target_energy_rerror = np.abs(kh_target_energy - kht_target_energy) / kh_target_energy print("Target energy absolute error: {:.4}".format(target_energy_aerror)) print("Target energy relative error: {:.3%}".format(target_energy_rerror)) .. rst-class:: sphx-glr-script-out .. code-block:: none Target energy absolute error: 7.503e-05 Target energy relative error: 0.169% .. GENERATED FROM PYTHON SOURCE LINES 65-67 Draw the energy convergence of KernelHerding and TensorizedKernelHerding designs.  Notice how they both converge towards their respective target energies. .. GENERATED FROM PYTHON SOURCE LINES 67-83 .. code-block:: default fig1, plot_data1 = kh.draw_energy_convergence(kh.get_indices(kh_design)) fig2, plot_data2 = kht.draw_energy_convergence(kht.get_indices(kht_design)) fig3, ax3 = plt.subplots(1, sharey=True, sharex=True) # Plot data from fig1 and fig2 ax3.plot(plot_data1.get_data()[0], np.log(plot_data1.get_data()[1]), label=kh._method_label + 'standard') ax3.plot(plot_data2.get_data()[0], np.log(plot_data2.get_data()[1]), label=kht._method_label) ax3.axhline(np.log(kh_target_energy), color='C0', linestyle='dashed', label='target energy standard') ax3.axhline(np.log(kht_target_energy), color='C1', linestyle='dashed', label='target energy tensorized') ax3.set_title('Energy convergence') ax3.set_xlabel('design size ($n$)') ax3.set_ylabel('Energy (log-scale)') ax3.legend(loc='best') plt.show() .. image-sg:: /auto_example/images/sphx_glr_plot_energy_example_001.png :alt: Energy convergence :srcset: /auto_example/images/sphx_glr_plot_energy_example_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none /usr/share/miniconda3/envs/test/lib/python3.10/site-packages/otkerneldesign-0.1.4-py3.10.egg/otkerneldesign/KernelHerding.py:312: RuntimeWarning: Mean of empty slice. /usr/share/miniconda3/envs/test/lib/python3.10/site-packages/numpy/core/_methods.py:190: RuntimeWarning: invalid value encountered in double_scalars ret = ret.dtype.type(ret / rcount) .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 35.473 seconds) .. _sphx_glr_download_auto_example_plot_energy_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_energy_example.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_energy_example.ipynb `