How this calculation works
Compute an entropy-regularized transport plan between two discrete probability distributions using a nonnegative cost matrix. The solver scales in the log domain to match both marginals, then reports transport cost, entropy contribution, regularized objective and a duality diagnostic. The regularization parameter controls a tradeoff between concentrating on cheap routes and spreading mass; this is not the unregularized linear transport optimum.
Minimize ΣC_ij P_ij + εΣP_ij(log P_ij−1), with P1=a and Pᵀ1=b.A worked example
With equal two-point marginals, zero diagonal costs, unit off-diagonal costs and epsilon one, each diagonal mass is 1/[2(1+e^−1)] and each off-diagonal mass is e^−1/[2(1+e^−1)]. Raw transport cost is 1/(1+e), which differs from the regularized objective.
Good to know
≤15×15 costs, strictly positive normalized marginals, epsilon 0.0001–1000. Log-stabilized Sinkhorn with marginal tolerance and numerical duality diagnostic. No exact unregularized Wasserstein metric, structured transport constraints or claim that every epsilon converges within the cap.
Rows and columns describe two marginals
Source probabilities correspond to cost-matrix rows and destination probabilities to columns. Both vectors must be positive and sum to one. A plan entry is the probability mass moved from one source row to one destination column.
Its row sums and column sums must reproduce the supplied marginals. The entries are not conditional probabilities until divided by the appropriate source or destination mass. Matrix orientation therefore matters when interpreting a particular route.
Uses an entered cost matrix and entropy penalty, returning the actual transport plan; it is not the exact unregularized Wasserstein metric. The Elliptic integral calculator with modulus and parameter conventions offers a complementary calculation under its own stated assumptions.
The entropy convention is explicit
The objective adds cost times transported mass to epsilon times the sum of P times log P minus P. Some references omit the final minus-P term; for unit total mass that changes the objective by a constant but not the optimal plan.
Other references use negative Shannon entropy without stating a sign. Compare definitions before comparing objective numbers. The page reports the raw cost separately so the regularization contribution cannot be mistaken for a physical shipping expense.
Worked example and mathematical rule
With equal two-point marginals, zero diagonal costs, unit off-diagonal costs and epsilon one, each diagonal mass is 1/[2(1+e^−1)] and each off-diagonal mass is e^−1/[2(1+e^−1)]. Raw transport cost is 1/(1+e), which differs from the regularized objective.
Minimize ΣC_ij P_ij + εΣP_ij(log P_ij−1), with P1=a and Pᵀ1=b.
How Sinkhorn scaling enforces mass
The exponential cost kernel is alternately scaled to satisfy source and destination totals. Log-domain updates avoid direct underflow when cost divided by epsilon is large.
Iteration stops only when marginal residuals meet the supported tolerance. Tiny epsilon can still create difficult conditioning and slow convergence. A failure is not a license to return the last matrix as an optimal transport plan; unmatched marginals would change the problem being solved.
Regularization changes the answer
Larger epsilon tends to spread mass more broadly, while smaller epsilon emphasizes lower-cost pairings. In the zero-cost case the independent coupling of the marginals is the maximum-entropy plan.
The positive-epsilon optimum is generally dense even if a cheapest unregularized plan is sparse. A small entry is still transported mass rather than a binary assignment decision. This formulation does not enforce one-to-one matching, integer shipment quantities or route capacities.
Primal and dual diagnostics
The primal objective evaluates the returned plan. Dual potentials provide a comparison value under the displayed entropy convention, and their gap is a useful numerical diagnostic.
Floating-point residuals can affect the gap, so a tiny signed discrepancy should be interpreted alongside marginal error rather than as an exact symbolic certificate. Adding a constant to every cost shifts the raw objective predictably while leaving the optimal plan unchanged when marginals remain fixed.
Using cost units consistently
Epsilon has the same units as an individual cost because it multiplies a dimensionless entropy expression. Rescaling every cost should be accompanied by the same epsilon scaling if the intended plan is to remain unchanged.
The solver normalizes no arbitrary demand totals automatically; inputs describe probabilities. For actual quantities, state how total mass converts a probability plan to units shipped. Keep the cost matrix, marginals, epsilon and convergence diagnostics together when reporting a numerical comparison.
A few questions, answered
Is the displayed cost the regularized objective?
They are separate outputs: raw transport cost and cost plus the stated entropy term.
Does epsilon have units?
Yes. It shares the cost scale.
Is this a one-to-one assignment?
No. Mass can split among several destinations.
Why use logarithmic updates?
They reduce underflow and overflow in exponential scaling, though difficult conditioning can remain.