I'm trying to determine an analytic formula for the Wasserstein-1 distance between two Laplace distributions.

Since the distriutions are in 1D, I can use the relationship between the W1 distance and the $L_1$ difference of the cumulative density functions:

$$ \mathbb{W}(\mathrm{L}(\mu_1,b_1),\mathrm{L}(\mu_2,b_2)) = \int_{x\in\mathbb{R}} |c_1(x)-c_2(x)| $$

where $c_i(x) = \frac{1}{2}e^{\frac{x-\mu_i}{b_i}}$ when $x \leq \mu_i$ and $1-\frac{1}{2}e^{-\frac{x-\mu_i}{b_i}}$ otherwise (sorry I can't write this as a piecewise function; the SE editor thinks it's code and won't let me publish the question).

We can tell already that this is a simple integral in the sense that it breaks down into sums of exponential integrals. I was hoping Mathematica could spare me from filling in the details, so I wrote the following code:

f1[x_] := Piecewise[{{0.5*Exp[(x - \[Mu]1)/b1], x < \[Mu]1}, {1 - 0.5*Exp[-(x - \[Mu]1)/b1], x > \[Mu]1}}]
f2[x_] := Piecewise[{{0.5*Exp[(x - \[Mu]2)/b2], x < \[Mu]2}, {1 - 0.5*Exp[-(x - \[Mu]2)/b2], x > \[Mu]2}}]
Integrate[Abs[f1[x] - f2[x]], {x, -Infinity, Infinity}, Assumptions -> {b1 > 0, b2 > 0,Element[\[Mu]1, Reals], Element[\[Mu]2, Reals]}]

Unfortunately, this code doesn't work (it just returns the integral statement).

Is there a better way to formulate this integral?

  • 2
    $\begingroup$ Welcome to MMA SE! Note that in a context like this you should use 1/2, not 0.5, as 1/2 is an exact number and 0.5 is a machine number (subject to machine arithmetic and rounding errors). Unfortunately, this change doesn't help mathematica integrate the expression. $\endgroup$
    – thorimur
    Commented Aug 19, 2022 at 22:52
  • 1
    $\begingroup$ Hi, if you remove the boundaries -Infinity and +Infinity, Mathematica is able to compute the indefinite integral/anti-derivative. $\endgroup$ Commented Aug 19, 2022 at 23:06
  • $\begingroup$ @thorimur Oh fascinating thanks for explaining that to me. $\endgroup$ Commented Aug 19, 2022 at 23:28
  • $\begingroup$ @userrandrand Oh thanks, indeed it does! Though the expression is a pretty gnarly forest of piecewise functions that Simplify doesn't seem to help with... $\endgroup$ Commented Aug 19, 2022 at 23:29
  • 1
    $\begingroup$ Actually it seems like the antiderivative might be kind of wrong as it might not enforce continuity between the pieces. For example, the output from Integrate[ Piecewise[{{(1/2)*Exp[(x - 3)], x < 3}, {1 - (1/2)*Exp[-(x - 3)], x > 3}}], x] looks like a reasonable answer as it is continuous at x=3 but Integrate[ Piecewise[{{(1/2)*Exp[(x - \[Mu]1)], x < \[Mu]1}, {1 - (1/2)*Exp[-(x - \[Mu]1)], x > \[Mu]1}}], x] is not continuous at x= [Mu]1 $\endgroup$ Commented Aug 19, 2022 at 23:39

1 Answer 1


Four parameters are too much for Integrate. It works well, when specifying the parameters:

\[Mu]1 = -1; b1 = 1; b2 = 1/Pi; \[Mu]2 = 2; 
Integrate[RealAbs[Piecewise[{{1/2*Exp[(x - \[Mu]1)/b1], 
x <= \[Mu]1}, {1 - 1/2*Exp[-(x - \[Mu]1)/b1], x > \[Mu]1}}] - 
Piecewise[{{1/2*Exp[(x - \[Mu]2)/b2], x <= \[Mu]2}, {1 - 1/2*Exp[-(x - \[Mu]2)/b2], 
x > \[Mu]2}}]], {x, -Infinity, Infinity}]

$$ \frac{e^{-\frac{3 \pi }{\pi -1}} \left(-1+\pi +3 e^{\frac{3 \pi }{\pi -1}} \pi \right)}{\pi }$$

  • $\begingroup$ Thanks for your reply! So you think computing a general solution to this problem is simply beyond Mathematica's current capabilities? $\endgroup$ Commented Aug 20, 2022 at 16:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.