<jupyter_start><jupyter_text>2D free support Wasserstein barycenters of distributionsIllustration of 2D Wasserstein barycenters if distributions are weightedsum of diracs.<jupyter_code># Author: <NAME> <<EMAIL>>
#
# License: MIT License

import numpy as np
import matplotlib.pylab as pl
import ot
from src.main import *<jupyter_output><empty_output><jupyter_text>Generate data<jupyter_code>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)
# len(measures_locations[0])
# b.shape
# X_init.shape

# type(measures_weights)<jupyter_output><empty_output><jupyter_text>Compute free support barycenter<jupyter_code>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)
X_bary = (measures_locations, measures_weights, X_init, b)<jupyter_output><empty_output><jupyter_text>Plot data<jupyter_code>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()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13509316770186336, 'fraction_numerical': 0.018115942028985508, 'mean_word_length': 5.235483870967742, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '59042', 'license': 'MIT', 'n_tokens_mistral': 779, 'n_tokens_neox': 725, 'n_words': 169, 'path': 'plot_free_support_barycenter.ipynb', 'repo_name': 'mail-ecnu/OTDyadicFair', 'size': '1967'}