diff options
author | Gard Spreemann <gspr@nonempty.org> | 2020-01-20 14:07:53 +0100 |
---|---|---|
committer | Gard Spreemann <gspr@nonempty.org> | 2020-01-20 14:07:53 +0100 |
commit | bdfb24ff37ea777d6e266b145047cd4e281ebac3 (patch) | |
tree | 00cbac5f3dc25a4ee76164828abd72c1cbab37cc /docs/source/auto_examples/plot_free_support_barycenter.rst | |
parent | abc441b00f0fe2fa4ef0efc4e1aa67b27cca9a13 (diff) | |
parent | 5e70a77fbb2feec513f21c9ef65dcc535329ace6 (diff) |
Merge tag '0.6.0' into debian/sid
Diffstat (limited to 'docs/source/auto_examples/plot_free_support_barycenter.rst')
-rw-r--r-- | docs/source/auto_examples/plot_free_support_barycenter.rst | 140 |
1 files changed, 140 insertions, 0 deletions
diff --git a/docs/source/auto_examples/plot_free_support_barycenter.rst b/docs/source/auto_examples/plot_free_support_barycenter.rst new file mode 100644 index 0000000..d1b3b80 --- /dev/null +++ b/docs/source/auto_examples/plot_free_support_barycenter.rst @@ -0,0 +1,140 @@ + + +.. _sphx_glr_auto_examples_plot_free_support_barycenter.py: + + +==================================================== +2D free support Wasserstein barycenters of distributions +==================================================== + +Illustration of 2D Wasserstein barycenters if discributions that are weighted +sum of diracs. + + + + +.. code-block:: python + + + # Author: Vivien Seguy <vivien.seguy@iip.ist.i.kyoto-u.ac.jp> + # + # License: MIT License + + import numpy as np + import matplotlib.pylab as pl + import ot + + + + + + + + +Generate data + ------------- +%% parameters and data generation + + + +.. code-block:: python + + N = 3 + d = 2 + measures_locations = [] + measures_weights = [] + + for i in range(N): + + n_i = np.random.randint(low=1, high=20) # nb samples + + mu_i = np.random.normal(0., 4., (d,)) # Gaussian mean + + A_i = np.random.rand(d, d) + cov_i = np.dot(A_i, A_i.transpose()) # Gaussian covariance matrix + + x_i = ot.datasets.make_2D_samples_gauss(n_i, mu_i, cov_i) # Dirac locations + b_i = np.random.uniform(0., 1., (n_i,)) + b_i = b_i / np.sum(b_i) # Dirac weights + + measures_locations.append(x_i) + measures_weights.append(b_i) + + + + + + + + +Compute free support barycenter +------------- + + + +.. code-block:: python + + + k = 10 # number of Diracs of the barycenter + X_init = np.random.normal(0., 1., (k, d)) # initial Dirac locations + b = np.ones((k,)) / k # weights of the barycenter (it will not be optimized, only the locations are optimized) + + X = ot.lp.free_support_barycenter(measures_locations, measures_weights, X_init, b) + + + + + + + + +Plot data +--------- + + + +.. code-block:: python + + + pl.figure(1) + for (x_i, b_i) in zip(measures_locations, measures_weights): + color = np.random.randint(low=1, high=10 * N) + pl.scatter(x_i[:, 0], x_i[:, 1], s=b * 1000, label='input measure') + pl.scatter(X[:, 0], X[:, 1], s=b * 1000, c='black', marker='^', label='2-Wasserstein barycenter') + pl.title('Data measures and their barycenter') + pl.legend(loc=0) + pl.show() + + + +.. image:: /auto_examples/images/sphx_glr_plot_free_support_barycenter_001.png + :align: center + + + + +**Total running time of the script:** ( 0 minutes 0.129 seconds) + + + +.. only :: html + + .. container:: sphx-glr-footer + + + .. container:: sphx-glr-download + + :download:`Download Python source code: plot_free_support_barycenter.py <plot_free_support_barycenter.py>` + + + + .. container:: sphx-glr-download + + :download:`Download Jupyter notebook: plot_free_support_barycenter.ipynb <plot_free_support_barycenter.ipynb>` + + +.. only:: html + + .. rst-class:: sphx-glr-signature + + `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.readthedocs.io>`_ |