Important: Please read the installation page for details about how to install the toolboxes. $\newcommand{\dotp}[2]{\langle #1, #2 \rangle}$ $\newcommand{\enscond}[2]{\lbrace #1, #2 \rbrace}$ $\newcommand{\pd}[2]{ \frac{ \partial #1}{\partial #2} }$ $\newcommand{\umin}[1]{\underset{#1}{\min}\;}$ $\newcommand{\umax}[1]{\underset{#1}{\max}\;}$ $\newcommand{\umin}[1]{\underset{#1}{\min}\;}$ $\newcommand{\uargmin}[1]{\underset{#1}{argmin}\;}$ $\newcommand{\norm}[1]{\|#1\|}$ $\newcommand{\abs}[1]{\left|#1\right|}$ $\newcommand{\choice}[1]{ \left\{ \begin{array}{l} #1 \end{array} \right. }$ $\newcommand{\pa}[1]{\left(#1\right)}$ $\newcommand{\diag}[1]{{diag}\left( #1 \right)}$ $\newcommand{\qandq}{\quad\text{and}\quad}$ $\newcommand{\qwhereq}{\quad\text{where}\quad}$ $\newcommand{\qifq}{ \quad \text{if} \quad }$ $\newcommand{\qarrq}{ \quad \Longrightarrow \quad }$ $\newcommand{\ZZ}{\mathbb{Z}}$ $\newcommand{\CC}{\mathbb{C}}$ $\newcommand{\RR}{\mathbb{R}}$ $\newcommand{\EE}{\mathbb{E}}$ $\newcommand{\Zz}{\mathcal{Z}}$ $\newcommand{\Ww}{\mathcal{W}}$ $\newcommand{\Vv}{\mathcal{V}}$ $\newcommand{\Nn}{\mathcal{N}}$ $\newcommand{\NN}{\mathcal{N}}$ $\newcommand{\Hh}{\mathcal{H}}$ $\newcommand{\Bb}{\mathcal{B}}$ $\newcommand{\Ee}{\mathcal{E}}$ $\newcommand{\Cc}{\mathcal{C}}$ $\newcommand{\Gg}{\mathcal{G}}$ $\newcommand{\Ss}{\mathcal{S}}$ $\newcommand{\Pp}{\mathcal{P}}$ $\newcommand{\Ff}{\mathcal{F}}$ $\newcommand{\Xx}{\mathcal{X}}$ $\newcommand{\Mm}{\mathcal{M}}$ $\newcommand{\Ii}{\mathcal{I}}$ $\newcommand{\Dd}{\mathcal{D}}$ $\newcommand{\Ll}{\mathcal{L}}$ $\newcommand{\Tt}{\mathcal{T}}$ $\newcommand{\si}{\sigma}$ $\newcommand{\al}{\alpha}$ $\newcommand{\la}{\lambda}$ $\newcommand{\ga}{\gamma}$ $\newcommand{\Ga}{\Gamma}$ $\newcommand{\La}{\Lambda}$ $\newcommand{\si}{\sigma}$ $\newcommand{\Si}{\Sigma}$ $\newcommand{\be}{\beta}$ $\newcommand{\de}{\delta}$ $\newcommand{\De}{\Delta}$ $\newcommand{\phi}{\varphi}$ $\newcommand{\th}{\theta}$ $\newcommand{\om}{\omega}$ $\newcommand{\Om}{\Omega}$ $\newcommand{\eqdef}{\equiv}$
This numerical tours exposes the general methodology of regularizing the optimal transport (OT) linear program using entropy. This allows to derive fast computation algorithm based on iterative projections according to a Kulback-Leiber divergence. $$ \DeclareMathOperator{\KL}{KL} \newcommand{\KLdiv}[2]{\KL\pa{#1 | #2}} \newcommand{\KLproj}{P^{\tiny\KL}} \def\ones{\mathbb{I}} $$
using PyPlot
using NtToolBox
We consider two input histograms $p,q \in \Si_N$, where we denote the simplex in $\RR^N$ $$ \Si_{N} \eqdef \enscond{ p \in (\RR^+)^N }{ \sum_i p_i = 1 }. $$ We consider the following discrete regularized transport $$ W_\ga(p,q) \eqdef \umin{\pi \in \Pi(p,q)} \dotp{C}{\pi} - \ga E(\pi). $$ where the polytope of coupling is defined as $$ \Pi(p,q) \eqdef \enscond{\pi \in (\RR^+)^{N \times N}}{ \pi \ones = p, \pi^T \ones = q }, $$ where $ \ones \eqdef (1,\ldots,1)^T \in \RR^N $, and for $\pi \in (\RR^+)^{N \times N}$, we define its entropy as $$ E(\pi) \eqdef - \sum_{i,j} \pi_{i,j} ( \log(\pi_{i,j}) - 1). $$
When $\ga=0$ one recovers the classical (discrete) optimal transport. We refer to the monograph Villani for more details about OT. The idea of regularizing transport to allows for faster computation is introduced in Cuturi.
Here the matrix $C \in (\RR^+)^{N \times N} $ defines the ground cost, i.e. $C_{i,j}$ is the cost of moving mass from a bin indexed by $i$ to a bin indexed by $j$.
The regularized transportation problem can be re-written as a projection $$ W_\ga(p,q) = \ga \umin{\pi \in \Pi(p,q)} \KLdiv{\pi}{\xi} \qwhereq \xi_{i,j} = e^{ -\frac{C_{i,j}}{\ga} } $$ of $\xi$ according to the Kullback-Leibler divergence. The Kullback-Leibler divergence between $\pi, \xi \in (\RR^+)^P$ is $$ \KLdiv{\pi}{\xi} = \sum_{i,j} \pi_{i,j} \pa{ \log\pa{ \frac{\pi_{i,j}}{\xi_{i,j}} } - 1}. $$
This interpretation of regularized transport as a KL projection and its numerical applications are detailed in BenamouEtAl.
Given a convex set $\Cc \subset \RR^N$, the projection according to the Kullback-Leiber divergence is defined as $$ \KLproj_\Cc(\xi) = \uargmin{ \pi \in \Cc } \KLdiv{\pi}{\xi}. $$
Given affine constraint sets $ (\Cc_1,\ldots,\Cc_K) $, we aim at computing $$ \KLproj_\Cc(\xi) \qwhereq \Cc = \Cc_1 \cap \ldots \cap \Cc_K. $$
This can be achieved, starting by $\pi_0=\xi$, by iterating $$ \forall \ell \geq 0, \quad \pi_{\ell+1} = \KLproj_{\Cc_\ell}(\pi_\ell), $$ where the index of the constraints should be understood modulo $K$, i.e. we set $ \Cc_{\ell+K}=\Cc_\ell $.
One can indeed show that $\pi_\ell \rightarrow \KLproj_\Cc(\bar \pi)$. We refer to BauschkeLewis for more details about this algorithm and its extension to compute the projection on the intersection of convex sets (Dikstra algorithm).
We can re-cast the regularized optimal transport problem within this framework by introducing $$ \Cc_1 \eqdef \enscond{\pi \in (\RR^+)^{N \times N} }{\pi \ones = p} \qandq \Cc_2 \eqdef \enscond{\pi \in (\RR^+)^{N \times N} }{\pi^T \ones = q}. $$
The KL projection on $\Cc_1$ sets are easily computed by divisive normalization of rows. Indeed, denoting $ \pi = \KLproj_{\Cc_1}(\bar \pi) $, one has $$ \forall (i,j), \quad \pi_{i,j} = \frac{ p_i \bar\pi_{i,j} }{ \sum_{s} \bar\pi_{i,s} } $$ and similarely for $\KLproj_{\Cc_1}(\bar \pi) $ by replacing rows by colums.
A fundamental remark is that, if $\bar\pi = \diag(a) \xi \diag(b)$ (a so-called diagonal scaling of the kernel $\xi$), then one has $$ \KLproj_{\Cc_1}(\bar \pi) = \diag(\tilde a) \xi \diag(b) \qandq \KLproj_{\Cc_2}(\bar \pi) = \diag(a) \xi \diag(\tilde b)$$ where the new scaling reads $$ \tilde a = \frac{p}{\xi(b)} \qandq \tilde b = \frac{q}{\xi^T(a)} $$ where $\frac{\cdot}{\cdot}$ is entry-wise division.
This means that the iterates of Bregman iterative projections, starting with $ a_0 \eqdef \ones $ always have the form $ \pi_\ell = \diag(a_\ell) \xi \diag(b_\ell) $ and these diagonal scaling weights are updated as follow $$ a_{\ell+1} \eqdef \frac{p}{\xi(b_\ell)} \qandq b_{\ell+1} \eqdef \frac{q}{\xi^T(a_{\ell+1})}. $$ This algorithm is in fact the well known Sinkhorn algorithm Sinkhorn.
We first test the method for two input measures that are uniform measures (i.e. constant histograms) supported on two point clouds (that do not necessarily have the same size).
We thus first load two points clouds $x=(x_i)_{i=1}^{N_1}, y=(y_i)_{i=1}^{N_2}, $ where $x_i, y_i \in \RR^2$.
Number of points in each cloud.
N = [300,200];
Dimension of the clouds.
d = 2;
Point cloud $x$, of $N_1$ points inside a square.
x = rand(2,N[1])-.5;
Point cloud $y$, of $N_2$ points inside an anulus.
theta = 2*pi*rand(1,N[2])
r = .8 + .2*rand(1,N[2])
y = [cos(theta).*r; sin(theta).*r];
Shortcut for displaying point clouds.
plotp = (x,col) -> scatter(x[1,:], x[2,:], s=200, edgecolors="k", c=col, linewidths=2);
Display of the two clouds.
figure(figsize=(10,10))
plotp(x, "b")
plotp(y, "r")
axis("off")
xlim(minimum(y[1,:])-.1,maximum(y[1,:])+.1)
ylim(minimum(y[2,:])-.1,maximum(y[2,:])+.1);
Cost matrix $C_{i,j} = \norm{x_i-y_j}^2$.
x2 = sum(x.^2,1)
y2 = sum(y.^2,1)
C = repeat(y2,outer=(N[1],1)) + repeat(x2',outer=(1,N[2])) - 2*x'*y;
Target histograms, here uniform histograms.
p = ones(N[1])/N[1]
q = ones(N[2])/N[2];
Regularization strength $\ga$.
gamma = .01;
Gibbs Kernel.
xi = exp(-C/gamma);
Initialization of $b_0=\ones_{N_2}$ ($a_0$ does not need to be initialized).
b = ones(N[2]);
One sinkhorn iterations.
a = p./(xi*b)
b = q./(xi'*a);
Exercise 1
Implement Sinkhorn algorithm. Display the evolution of the constraints satisfaction errors $ \norm{ \pi \ones - p }, \norm{ \pi^T \ones - q } $ (you need to think about how to compute these residuals from $(a,b)$ alone). isplay the violation of constraint error in log-plot.
include("NtSolutions/optimaltransp_5_entropic/exo1.jl");
# Insert your code here.
Compute the final matrix.
Pi = (diagm(a)*xi)*diagm(b);
Display it.
figure(figsize=(5,5))
imageplot(Pi)
Exercise 2
Display the regularized transport solution for various values of $\gamma$. For a too small value of $\gamma$, what do you observe ?
include("NtSolutions/optimaltransp_5_entropic/exo2.jl");
# Insert your code here.
Compute the obtained optimal $\pi$.
Pi = (diagm(a)*xi)*diagm(b);
Keep only the highest entries of the coupling matrix, and use them to draw a map between the two clouds. First we draw "strong" connexions, i.e. linkds $(i,j)$ corresponding to large values of $\pi_{i,j}$. We then draw weaker connexions.
figure(figsize=(10,10))
plotp(x, "b")
plotp(y, "r")
A = Pi .* (Pi .> minimum(1./Array(N))*.7)
i,j = findn(A)
plot([x[1,i],y[1,j]],[x[2,i],y[2,j]],"k",lw = 2)
A = Pi .* (Pi .> minimum(1./Array(N))*.3)
i,j <