Two samples of 50 petal lengths (cm), each flower carrying weight 1/50, are to be moved onto each other at squared-distance cost. In one dimension the exact optimal plan is the sorted matching (the quantile coupling): the diagonal of the plan matrix. Entropic regularisation with strength \(\gamma\) smears that diagonal into a smooth plan with the same marginals, and the Sinkhorn iterations (6.217) compute it by alternately rescaling the rows and the columns. Move \(\gamma\) to see the plan sharpen towards the exact solution (and the iterations needed grow), or cut the iterations short to see the marginal constraints violated.
Data: petal length (cm) of the 50 flowers of each iris species, sorted; cost \(C_{ij}=(x_i-y_j)^2\). The plan \(P=\mathrm{diag}(e^{f/\gamma})\,K\,\mathrm{diag}(e^{g/\gamma})\) with \(K=e^{-C/\gamma}\) is computed with log-sum-exp updates. "Marginal error" is \(\sum_i|a_i-\sum_jP_{ij}|\) after the column update. The exact cost is the mean squared difference of the sorted samples; the Gaussian value is \((m_1-m_2)^2+(s_1-s_2)^2\) for Gaussians with the same means and standard deviations.
code/ch06_optimization.py (sinkhorn, ot_lp, bures_w2sq).