.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/sliced-wasserstein/plot_variance.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_sliced-wasserstein_plot_variance.py: =============================================== Sliced Wasserstein Distance on 2D distributions =============================================== This example illustrates the computation of the sliced Wasserstein Distance as proposed in [31]. [31] Bonneel, Nicolas, et al. "Sliced and radon wasserstein barycenters of measures." Journal of Mathematical Imaging and Vision 51.1 (2015): 22-45 .. GENERATED FROM PYTHON SOURCE LINES 14-26 .. code-block:: Python # Author: Adrien Corenflos # # License: MIT License # sphinx_gallery_thumbnail_number = 2 import matplotlib.pylab as pl import numpy as np import ot .. GENERATED FROM PYTHON SOURCE LINES 27-29 Generate data ------------- .. GENERATED FROM PYTHON SOURCE LINES 31-45 .. code-block:: Python n = 200 # nb samples mu_s = np.array([0, 0]) cov_s = np.array([[1, 0], [0, 1]]) mu_t = np.array([4, 4]) cov_t = np.array([[1, -.8], [-.8, 1]]) xs = ot.datasets.make_2D_samples_gauss(n, mu_s, cov_s) xt = ot.datasets.make_2D_samples_gauss(n, mu_t, cov_t) a, b = np.ones((n,)) / n, np.ones((n,)) / n # uniform distribution on samples .. GENERATED FROM PYTHON SOURCE LINES 46-48 Plot data --------- .. GENERATED FROM PYTHON SOURCE LINES 50-57 .. code-block:: Python pl.figure(1) pl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples') pl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples') pl.legend(loc=0) pl.title('Source and target distributions') .. image-sg:: /auto_examples/sliced-wasserstein/images/sphx_glr_plot_variance_001.png :alt: Source and target distributions :srcset: /auto_examples/sliced-wasserstein/images/sphx_glr_plot_variance_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Text(0.5, 1.0, 'Source and target distributions') .. GENERATED FROM PYTHON SOURCE LINES 58-60 Sliced Wasserstein distance for different seeds and number of projections ------------------------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 60-65 .. code-block:: Python n_seed = 20 n_projections_arr = np.logspace(0, 3, 10, dtype=int) res = np.empty((n_seed, 10)) .. GENERATED FROM PYTHON SOURCE LINES 66-73 .. code-block:: Python for seed in range(n_seed): for i, n_projections in enumerate(n_projections_arr): res[seed, i] = ot.sliced_wasserstein_distance(xs, xt, a, b, n_projections, seed=seed) res_mean = np.mean(res, axis=0) res_std = np.std(res, axis=0) .. GENERATED FROM PYTHON SOURCE LINES 74-76 Plot Sliced Wasserstein Distance -------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 76-89 .. code-block:: Python pl.figure(2) pl.plot(n_projections_arr, res_mean, label="SWD") pl.fill_between(n_projections_arr, res_mean - 2 * res_std, res_mean + 2 * res_std, alpha=0.5) pl.legend() pl.xscale('log') pl.xlabel("Number of projections") pl.ylabel("Distance") pl.title('Sliced Wasserstein Distance with 95% confidence interval') pl.show() .. image-sg:: /auto_examples/sliced-wasserstein/images/sphx_glr_plot_variance_002.png :alt: Sliced Wasserstein Distance with 95% confidence interval :srcset: /auto_examples/sliced-wasserstein/images/sphx_glr_plot_variance_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 2.491 seconds) .. _sphx_glr_download_auto_examples_sliced-wasserstein_plot_variance.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_variance.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_variance.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_