|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | +==================================================== |
| 4 | +Weak Optimal Transport VS exact Optimal Transport |
| 5 | +==================================================== |
| 6 | +
|
| 7 | +Illustration of 2D optimal transport between distributions that are weighted |
| 8 | +sum of diracs. The OT matrix is plotted with the samples. |
| 9 | +
|
| 10 | +""" |
| 11 | + |
| 12 | +# Author: Remi Flamary <remi.flamary@polytechnique.edu> |
| 13 | +# |
| 14 | +# License: MIT License |
| 15 | + |
| 16 | +# sphinx_gallery_thumbnail_number = 4 |
| 17 | + |
| 18 | +import numpy as np |
| 19 | +import matplotlib.pylab as pl |
| 20 | +import ot |
| 21 | +import ot.plot |
| 22 | + |
| 23 | +############################################################################## |
| 24 | +# Generate data an plot it |
| 25 | +# ------------------------ |
| 26 | + |
| 27 | +#%% parameters and data generation |
| 28 | + |
| 29 | +n = 50 # nb samples |
| 30 | + |
| 31 | +mu_s = np.array([0, 0]) |
| 32 | +cov_s = np.array([[1, 0], [0, 1]]) |
| 33 | + |
| 34 | +mu_t = np.array([4, 4]) |
| 35 | +cov_t = np.array([[1, -.8], [-.8, 1]]) |
| 36 | + |
| 37 | +xs = ot.datasets.make_2D_samples_gauss(n, mu_s, cov_s) |
| 38 | +xt = ot.datasets.make_2D_samples_gauss(n, mu_t, cov_t) |
| 39 | + |
| 40 | +a, b = ot.unif(n), ot.unif(n) # uniform distribution on samples |
| 41 | + |
| 42 | +# loss matrix |
| 43 | +M = ot.dist(xs, xt) |
| 44 | +M /= M.max() |
| 45 | + |
| 46 | +#%% plot samples |
| 47 | + |
| 48 | +pl.figure(1) |
| 49 | +pl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples') |
| 50 | +pl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples') |
| 51 | +pl.legend(loc=0) |
| 52 | +pl.title('Source and target distributions') |
| 53 | + |
| 54 | +pl.figure(2) |
| 55 | +pl.imshow(M, interpolation='nearest') |
| 56 | +pl.title('Cost matrix M') |
| 57 | + |
| 58 | + |
| 59 | +############################################################################## |
| 60 | +# Compute Weak OT and exact OT solutions |
| 61 | +# -------------------------------------- |
| 62 | + |
| 63 | +#%% EMD |
| 64 | + |
| 65 | +G0 = ot.emd(a, b, M) |
| 66 | + |
| 67 | +#%% Weak OT |
| 68 | + |
| 69 | +Gweak = ot.weak_optimal_transport(xs, xt, a, b) |
| 70 | + |
| 71 | + |
| 72 | +############################################################################## |
| 73 | +# Plot weak OT and exact OT solutions |
| 74 | +# -------------------------------------- |
| 75 | + |
| 76 | +pl.figure(3, (8, 5)) |
| 77 | + |
| 78 | +pl.subplot(1, 2, 1) |
| 79 | +pl.imshow(G0, interpolation='nearest') |
| 80 | +pl.title('OT matrix') |
| 81 | + |
| 82 | +pl.subplot(1, 2, 2) |
| 83 | +pl.imshow(Gweak, interpolation='nearest') |
| 84 | +pl.title('Weak OT matrix') |
| 85 | + |
| 86 | +pl.figure(4, (8, 5)) |
| 87 | + |
| 88 | +pl.subplot(1, 2, 1) |
| 89 | +ot.plot.plot2D_samples_mat(xs, xt, G0, c=[.5, .5, 1]) |
| 90 | +pl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples') |
| 91 | +pl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples') |
| 92 | +pl.title('OT matrix with samples') |
| 93 | + |
| 94 | +pl.subplot(1, 2, 2) |
| 95 | +ot.plot.plot2D_samples_mat(xs, xt, Gweak, c=[.5, .5, 1]) |
| 96 | +pl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples') |
| 97 | +pl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples') |
| 98 | +pl.title('Weak OT matrix with samples') |
0 commit comments