# Integral of Absolute Difference of Piecewise Functions

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?

• 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. Aug 19, 2022 at 22:52
• Hi, if you remove the boundaries -Infinity and +Infinity, Mathematica is able to compute the indefinite integral/anti-derivative. Aug 19, 2022 at 23:06
• @thorimur Oh fascinating thanks for explaining that to me. Aug 19, 2022 at 23:28
• @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... Aug 19, 2022 at 23:29
• 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 Aug 19, 2022 at 23:39

## 1 Answer

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 }$$

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