1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
.. only:: html
.. note::
:class: sphx-glr-download-link-note
Click :ref:`here <sphx_glr_download_auto_examples_plot_free_support_barycenter.py>` to download the full example code
.. rst-class:: sphx-glr-example-title
.. _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:: default
# 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:: default
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:: default
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:: default
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_i * 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
:class: sphx-glr-single-img
.. rst-class:: sphx-glr-script-out
Out:
.. code-block:: none
/home/rflamary/PYTHON/POT/examples/plot_free_support_barycenter.py:69: UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.
pl.show()
.. rst-class:: sphx-glr-timing
**Total running time of the script:** ( 0 minutes 0.080 seconds)
.. _sphx_glr_download_auto_examples_plot_free_support_barycenter.py:
.. only :: html
.. container:: sphx-glr-footer
:class: sphx-glr-footer-example
.. container:: sphx-glr-download sphx-glr-download-python
:download:`Download Python source code: plot_free_support_barycenter.py <plot_free_support_barycenter.py>`
.. container:: sphx-glr-download sphx-glr-download-jupyter
: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.github.io>`_
|