Saturday, January 27, 2024

Investigating Discrepancies in Optimal Transport Costs: Using Python Optimal Transport (POT)

Investigating Discrepancies in Optimal Transport Costs: Using Python Optimal Transport (POT)

Investigating Discrepancies in Optimal Transport Costs: Using Python Optimal Transport (POT)

Optimal transport theory involves stratergies moving mass from one place to another so to minimize optimal transport cost.

Let’s introduce the mathematical model of optimal transport. Consider a set of nn points (x1,...,xn)(x_1, ..., x_n) on the plane, with a matrix distance DD with DijD_{ij} indicating the distances between the points xix_i and xjx_j. A vector a=(ai)i=1,...,nRna = (a_i)_{i=1,...,n} \in \mathbb{R}^n is said to be a mass distribution on (xi)(x_i) if each component is non-negative and the total mass is equal to 11.

The core question involves determining the minimum transportation cost to rearrange mass from one distribution aa to another bb. Denoted as OT(a,b)OT(a, b), this cost function considers the distances DijD_{ij} as the measure of cost for transferring one unit mass from xix_i to xjx_j. The total optimal cost is expressed as:

OT(a,b)=min{i,j=1nPijDij:j=1nPij=ai,i=1nPij=bji,j}(1)OT(a, b) = \min\left\{ \sum_{i, j=1}^n P_{ij} D_{ij} : \sum_{j=1}^n P_{ij} = a_i, \sum_{i=1}^n P_{ij} = b_j \forall i, j\right\} \tag{1}

Here, PijP_{ij} signifies the mass transferred from aia_i at xix_i to bjb_j at xjx_j. Preservation of mass is ensured by the conditions j=1,...,nPij=ai\sum_{j=1,..., n} P_{ij} = a_i and i=1,...,nPij=bj\sum_{i=1,..., n} P_{ij} = b_j for all i,j=1,...,ni, j=1,..., n.

An alternative cost function emerges when considering a straightforward mass rearrangement approach. Specifically, we focus on moving mass only from “heavy” locations xix_i such that ai>bia_i > b_i to “light” locations xjx_j with aj<bja_j < b_j. The cost in this scenario is expressed as:

OT([ab]+,[ab])(2)OT([a-b]_+, [a-b]_-) \tag{2}

Here, [z]+=max(z,0)[z]_+=\max(z, 0) and [z]=max(z,0)[z]_-=\max(-z, 0) represent the positive and negative components of zz, respectively.

Our investigation pivots on questioning the equality between the optimal transport costs represented by (1) and (2). Utilizing numerical examples, we demonstrate that these two cost functions are not equal. The divergence stems from the fact that, in (2), the mass transferred at xix_i (no matter sending or receiving mass) consistently equals aibi|a_i - b_i|, while in (1), this quantity can be exceeded.

import ot 
import numpy as np 
import matplotlib.pyplot as plt  
np.random.seed(111)

# generate 5 points and the matrix distance 
n = 5 
mu_s = np.array([0, 0])
cov_s = np.array([[1, 0], [0, 1]])
xs = ot.datasets.make_2D_samples_gauss(n, mu_s, cov_s)
M = ot.dist(xs, xs)

# OT(a, b)
a = np.random.rand(n)
a /= a.sum()
b = np.random.rand(n)
b /= b.sum()
opt_plan1 = ot.emd(a, b, M)
cost1 = (opt_plan1*M).sum()
print(f"Optimal cost 1 = {cost1}")

# OT([a-b]+, [a-b]-)
diff = a-b
c = diff.clip(min=0)
d = -diff.clip(max=0.)
opt_plan2 = ot.emd(c, d, M)
cost2 = (opt_plan2*M).sum()
print(f"Optimal cost 2 = {cost2}")

f, axs = plt.subplots(1, 2, figsize=(5*2, 5))
ax=axs[0]
ax.imshow(opt_plan1)
ax.axis("off")
ax.set_title(f"$OT(a, b)$={cost1:.4f}")
ax=axs[1]
ax.imshow(opt_plan2)
ax.axis("off")
ax.set_title(f"$OT([a-b]_+, [a-b]_-)$={cost2:.4f}")
plt.show()

Popular Posts