|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | +=============================== |
| 4 | +1D Screened optimal transport |
| 5 | +=============================== |
| 6 | +
|
| 7 | +This example illustrates the computation of Screenkhorn: |
| 8 | +Screening Sinkhorn Algorithm for Optimal transport. |
| 9 | +""" |
| 10 | + |
| 11 | +# Author: Mokhtar Z. Alaya <mokhtarzahdi.alaya@gmail.com> |
| 12 | +# |
| 13 | +# License: MIT License |
| 14 | + |
| 15 | +import numpy as np |
| 16 | +import matplotlib.pylab as pl |
| 17 | +import ot.plot |
| 18 | +from ot.datasets import make_1D_gauss as gauss |
| 19 | +from ot.bregman import screenkhorn |
| 20 | + |
| 21 | +############################################################################## |
| 22 | +# Generate data |
| 23 | +# ------------- |
| 24 | + |
| 25 | +#%% parameters |
| 26 | + |
| 27 | +n = 100 # nb bins |
| 28 | + |
| 29 | +# bin positions |
| 30 | +x = np.arange(n, dtype=np.float64) |
| 31 | + |
| 32 | +# Gaussian distributions |
| 33 | +a = gauss(n, m=20, s=5) # m= mean, s= std |
| 34 | +b = gauss(n, m=60, s=10) |
| 35 | + |
| 36 | +# loss matrix |
| 37 | +M = ot.dist(x.reshape((n, 1)), x.reshape((n, 1))) |
| 38 | +M /= M.max() |
| 39 | + |
| 40 | +############################################################################## |
| 41 | +# Plot distributions and loss matrix |
| 42 | +# ---------------------------------- |
| 43 | + |
| 44 | +#%% plot the distributions |
| 45 | + |
| 46 | +pl.figure(1, figsize=(6.4, 3)) |
| 47 | +pl.plot(x, a, 'b', label='Source distribution') |
| 48 | +pl.plot(x, b, 'r', label='Target distribution') |
| 49 | +pl.legend() |
| 50 | + |
| 51 | +# plot distributions and loss matrix |
| 52 | + |
| 53 | +pl.figure(2, figsize=(5, 5)) |
| 54 | +ot.plot.plot1D_mat(a, b, M, 'Cost matrix M') |
| 55 | + |
| 56 | +############################################################################## |
| 57 | +# Solve Screenkhorn |
| 58 | +# ----------------------- |
| 59 | + |
| 60 | +# Screenkhorn |
| 61 | +lambd = 2e-03 # entropy parameter |
| 62 | +ns_budget = 30 # budget number of points to be keeped in the source distribution |
| 63 | +nt_budget = 30 # budget number of points to be keeped in the target distribution |
| 64 | + |
| 65 | +G_screen = screenkhorn(a, b, M, lambd, ns_budget, nt_budget, uniform=False, restricted=True, verbose=True) |
| 66 | +pl.figure(4, figsize=(5, 5)) |
| 67 | +ot.plot.plot1D_mat(a, b, G_screen, 'OT matrix Screenkhorn') |
| 68 | +pl.show() |
0 commit comments