.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_hv_approx.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_plot_hv_approx.py: Comparing methods for approximating the hypervolume =================================================== This example shows how to approximate the hypervolume metric of the ``CPFs.txt`` dataset using both HypE, :func:`moocore.whv_hype()`, and DZ2019, :func:`moocore.hv_approx()` for several values of the number of samples between :math:`10^1` and :math:`10^5`. We repeat each calculation 10 times to account for stochasticity. .. GENERATED FROM PYTHON SOURCE LINES 8-38 .. code-block:: Python import numpy as np import moocore ref = 2.1 x = moocore.get_dataset("CPFs.txt")[:, :-1] x = moocore.filter_dominated(x) x = moocore.normalise(x, to_range=[1, 2]) true_hv = moocore.hypervolume(x, ref=ref) rng1 = np.random.default_rng(42) rng2 = np.random.default_rng(42) hype = {} dz = {} for i in range(1, 6): hype[i] = [] dz[i] = [] for r in range(15): res = moocore.whv_hype(x, ref=ref, ideal=0, nsamples=10**i, seed=rng1) hype[i].append(res) res = moocore.hv_approx(x, ref=ref, nsamples=10**i, seed=rng2) dz[i].append(res) print( f"True HV : {true_hv:.5f}", f"Mean HYPE : {np.mean(hype[5]):.5f} [{np.min(hype[5]):.5f}, {np.max(hype[5]):.5f}]", f"Mean DZ2019: {np.mean(dz[5]):.5f} [{np.min(dz[5]):.5f}, {np.max(dz[5]):.5f}]", sep="\n", ) .. rst-class:: sphx-glr-script-out .. code-block:: none True HV : 1.05704 Mean HYPE : 1.05601 [1.04504, 1.06391] Mean DZ2019: 1.05698 [1.05652, 1.05776] .. GENERATED FROM PYTHON SOURCE LINES 39-40 Next, we plot the results. .. GENERATED FROM PYTHON SOURCE LINES 40-61 .. code-block:: Python import pandas as pd hype = pd.DataFrame(hype) dz = pd.DataFrame(dz) hype["Method"] = "HypE" dz["Method"] = "DZ2019" df = ( pd.concat([hype, dz]) .reset_index(names="rep") .melt(id_vars=["rep", "Method"], var_name="samples") ) df["samples"] = 10 ** df["samples"] df["value"] = np.abs(df["value"] - true_hv) / true_hv import matplotlib.pyplot as plt import seaborn as sns ax = sns.lineplot(x="samples", y="value", hue="Method", data=df, marker="o") ax.set(xscale="log", yscale="log", ylabel="Relative error") plt.show() .. image-sg:: /auto_examples/images/sphx_glr_plot_hv_approx_001.png :alt: plot hv approx :srcset: /auto_examples/images/sphx_glr_plot_hv_approx_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 18.867 seconds) .. _sphx_glr_download_auto_examples_plot_hv_approx.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_hv_approx.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_hv_approx.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_hv_approx.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_