# What’s the distribution of (a−d)2+4bc(a-d)^2+4bc, where a,b,c,da,b,c,d are uniform distributions?

I have four independent uniformly distributed variables $a,b,c,d$, each in
$[0,1]$. I want to calculate the distribution of $(a-d)^2+4bc$. I computed the distribution of $u_2=4bc$ to be (hence $u_2\in(0,4]$), and of $u_1=(a-d)^2$ to be Now, the distribution of a sum $u_1+u_2$ is ($u_1,\, u_2$ are also independent) because $y\in(0,4]$. Here, it has to be $x>y$ so the integral is equal to Now I insert it to Mathematica and get that

I made four independent sets $a,b,c,d$ consisting of $10^6$ numbers each and drew a histogram of $(a-d)^2+4bc$:

and drew a plot of $f_{u_1+u_2}(x)$:

Generally, the plot is similar to the histogram, but on the interval $(0,5)$ most of it is negative (the root is at 2.27034). And the integral of the positive part is $\approx 0.77$.

Where’s the mistake? Or where am I missing something?

EDIT: I scaled the histogram to show the PDF.

EDIT 2: I think I know where’s the problem in my reasoning – in the integration limits. Because $y\in (0,4]$ and $x-y\in(0,1]$, I cannot simply $\int_0^x$. The plot shows the region I have to integrate in:

This means I have $\int_0^x$ for $y\in(0,1]$ (that’s why part of my $f$ was correct), $\int_{x-1}^x$ in $y\in(1,4]$, and $\int_{x-1}^4$ in $y\in (4,5]$. Unfortunately, Mathematica fails to compute the latter two integrals (well, it does calculate the second, by there’s an imaginary unit in the output that spoils everything…).

EDIT 3: It appears that Mathematica CAN compute the last three integrals with the following code:

(1/4)*Integrate[((1-Sqrt[u1-u2])*Log[4/u2])/Sqrt[u1-u2],{u2,0,u1}, Assumptions ->0 <= u2 <= u1 && u1 > 0]

(1/4)*Integrate[((1-Sqrt[u1-u2])*Log[4/u2])/Sqrt[u1-u2],{u2,u1-1,u1}, Assumptions -> 1 <= u2 <= 3 && u1 > 0]

(1/4)*Integrate[((1-Sqrt[u1-u2])*Log[4/u2])/Sqrt[u1-u2],{u2,u1-1,4}, Assumptions -> 4 <= u2 <= 4 && u1 > 0]

which gives a correct answer 🙂

Often it helps to use cumulative distribution functions.

First,

Next,

Let $\delta$ range between the smallest ($0$) and largest ($5$) possible values of $(a-d)^2 + 4 b c$. Writing $x=(a-d)^2$ with CDF $F$ and $y=4 b c$ with PDF $g = G^\prime$, we need to compute

We can expect this to be nasty–the uniform distribution PDF is discontinuous and thus ought to produce breaks in the definition of $H$–so it is somewhat amazing that Mathematica obtains a closed form (which I will not reproduce here). Differentiating it with respect to $\delta$ gives the desired density. It is defined piecewise within three intervals. In $0 \lt \delta \lt 1$,

In $1 \lt \delta \lt 4$,

And in $4 \lt \delta \lt 5$,

This figure overlays a plot of $h$ on a histogram of $10^6$ iid realizations of $(a-d)^2 + 4bc$. The two are almost indistinguishable, suggesting the correctness of the formula for $h$.

The following is a nearly mindless, brute-force Mathematica solution. It automates practically everything about the calculation. For instance, it will even compute the range of the resulting variable:

ClearAll[ a, b, c, d, ff, gg, hh, g, h, x, y, z, zMin, zMax, assumptions];
assumptions = 0 <= a <= 1 && 0 <= b <= 1 && 0 <= c <= 1 && 0 <= d <= 1;
zMax = First@Maximize[{(a - d)^2 + 4 b c, assumptions}, {a, b, c, d}];
zMin = First@Minimize[{(a - d)^2 + 4 b c, assumptions}, {a, b, c, d}];


Here is all the integration and differentiation. (Be patient; computing $H$ takes a couple of minutes.)

ff[x_] := Evaluate@FullSimplify@Integrate[Boole[(a - d)^2 <= x], {a, 0, 1}, {d, 0, 1}];
gg[y_] := Evaluate@FullSimplify@Integrate[Boole[4 b c <= y], {b, 0, 1}, {c, 0, 1}];
g[y_]  := Evaluate@FullSimplify@D[gg[y], y];
hh[z_] := Evaluate@FullSimplify@Integrate[ff[-y + z] g[y], {y, 0, 4},
Assumptions -> zMin <= z <= zMax];
h[z_]  :=  Evaluate@FullSimplify@D[hh[z], z];


Finally, a simulation and comparison to the graph of $h$:

x = RandomReal[{0, 1}, {4, 10^6}];
x = (x[[1, All]] - x[[4, All]])^2 + 4 x[[2, All]] x[[3, All]];
Show[Histogram[x, {.1}, "PDF"],
Plot[h[z], {z, zMin, zMax}, Exclusions -> {1, 4}],
AxesLabel -> {"\[Delta]", "Density"}, BaseStyle -> Medium,
Ticks -> {{{0, "0"}, {1, "1"}, {4, "4"}, {5, "5"}}, Automatic}]