| 1 |
|
| 2 |
""" |
| 3 |
=============================== |
| 4 |
1D Unbalanced optimal transport |
| 5 |
=============================== |
| 6 |
|
| 7 |
This example illustrates the computation of Unbalanced Optimal transport |
| 8 |
using a Kullback-Leibler relaxation. |
| 9 |
""" |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
import numpy as np |
| 18 |
import matplotlib.pylab as pl |
| 19 |
import ot |
| 20 |
import ot.plot |
| 21 |
from ot.datasets import make_1D_gauss as gauss |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
n = 100 |
| 31 |
|
| 32 |
|
| 33 |
x = np.arange(n, dtype=np.float64) |
| 34 |
|
| 35 |
|
| 36 |
a = gauss(n, m=20, s=5) |
| 37 |
b = gauss(n, m=60, s=10) |
| 38 |
|
| 39 |
|
| 40 |
b *= 5.0 |
| 41 |
|
| 42 |
|
| 43 |
M = ot.dist(x.reshape((n, 1)), x.reshape((n, 1))) |
| 44 |
M /= M.max() |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
pl.figure(1, figsize=(6.4, 3)) |
| 54 |
pl.plot(x, a, "b", label="Source distribution") |
| 55 |
pl.plot(x, b, "r", label="Target distribution") |
| 56 |
pl.legend() |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
pl.figure(2, figsize=(5, 5)) |
| 61 |
ot.plot.plot1D_mat(a, b, M, "Cost matrix M") |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
epsilon = 0.1 |
| 71 |
alpha = 1.0 |
| 72 |
Gs = ot.unbalanced.sinkhorn_unbalanced(a, b, M, epsilon, alpha, verbose=True) |
| 73 |
|
| 74 |
pl.figure(3, figsize=(5, 5)) |
| 75 |
ot.plot.plot1D_mat(a, b, Gs, "UOT matrix Sinkhorn") |
| 76 |
|
| 77 |
pl.show() |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
pl.figure(4, figsize=(6.4, 3)) |
| 85 |
pl.plot(x, a, "b", label="Source distribution") |
| 86 |
pl.plot(x, b, "r", label="Target distribution") |
| 87 |
pl.fill(x, Gs.sum(1), "b", alpha=0.5, label="Transported source") |
| 88 |
pl.fill(x, Gs.sum(0), "r", alpha=0.5, label="Transported target") |
| 89 |
pl.legend(loc="upper right") |
| 90 |
pl.title("Distributions and transported mass for UOT") |