# Sum of dependent random variables

Suppose that $$X$$ and $$Y$$ are jointly distributed in such a way that $$X\sim U[-1,1]$$ and $$Y\sim U[-|X|,|X|]$$.

I am interested in obtaining the PDF of $$X+Y$$ using Mathematica.

Without much hope, I tried the following

c := TransformedDistribution[
u + v, {u \[Distributed] UniformDistribution[{-1, 1}],
v \[Distributed] UniformDistribution[{-u, u}]}]


which did not work (among other things, the resulting PDF[c] has two variables).

Beyond this relatively simple example that can be solved with pen and paper, how can one use Mathematica to obtain the PDF of the sum of two random variables when the (conditional) distribution of one depends on the realization of the other?

• You've given the distributions of $X$ and $Y$ given $X$. What you need is the joint distribution of $X$ and $Y$ to proceed. That question is better asked at stats.stackexchange.com.
– JimB
Mar 17, 2020 at 16:24
• Your $Y\sim U[-X,X]$.is not correctly defined: think of negative values of $X\sim U[-1,1]$ . Mar 17, 2020 at 17:28
• @FZS With your edit for the reason to solve that problem, I agree.
– JimB
Mar 17, 2020 at 17:45
• @JimB: Your statement "However, in Mathematica UniformDistribution[{1,-1}] works just fine " does not correspond to reality in view of PDF[UniformDistribution[{1, -1}], t] which outputs "UniformDistribution::lss: Parameter 1 at position {1,1} in UniformDistribution[{1,-1}] is expected to be less than -1". Mar 17, 2020 at 21:05
• @user64494. Yep, I overstated things. I only checked out RandomVariate[UniformDistribution[{1, -1}], 10] which does work fine. Reality just isn't what it used to be.
– JimB
Mar 17, 2020 at 21:08

You can construct any distribution from pdfs of other distributions with ProbabilityDistribution, and Method -> "Normalize" will normalize it:

dist = ProbabilityDistribution[
PDF[UniformDistribution[{-1, 1}], u] PDF[UniformDistribution[{-Abs[u], Abs[u]}], v]
, {u, -1, 1}, {v, -1, 1}, Method -> "Normalize"];

pdf = PDF@TransformedDistribution[u + v, {u, v} \[Distributed] dist]

• pdf is a joint density but it is not the joint density of $X$ and $Y$ nor is it the univariate density of the sum of $X$ and $Y$.
– JimB
Mar 17, 2020 at 19:59
• +1 Your edit fixed things.
– JimB
Mar 17, 2020 at 22:02
• @JimB You're right, just needed some basic probability knowledge refreshment :) Mar 17, 2020 at 22:04
• Method -> "Normalize" doesn't work in v10.1; is it strictly necessary here? What purpose does it serve? Mar 18, 2020 at 16:15
• @Mr.Wizard It ensures that the resulting distribution is valid, so that it integrates to 1 over its domain. It's straightforward to do integration separately and normalize it too. In this particular case it doesn't matter though, but better safe than sorry. Mar 18, 2020 at 16:28

This becomes quite straight forward if we note that y can be written as the product of x and an independent uniformly distributed random variable u

d = Block[{y = x u},
TransformedDistribution[x + y, {x \[Distributed] UniformDistribution[{-1, 1}],
u \[Distributed] UniformDistribution[{-1, 1}]}]];

PDF[d, t] // InputForm
(* Piecewise[{{Log/4, t == 0}, {Log[-2/t]/4, Inequality[-2, Less, t, Less, 0]},
{Log[2/t]/4, Inequality[0, Less, t, Less, 2]}}, 0] *)

• +1. This is short and sweet. While the density at zero could be made to be anything non-negative in this case, why does it end up at Log/4? And why not have that value being $\infty$?
– JimB
Mar 17, 2020 at 21:00
• @JimB intriguing question. No idea why Mathematica did that - of course the value at a set of measure 0 has no practical effect. Mar 17, 2020 at 21:10
• Your x and y are dependent., but the documentation says "TransformedDistribution[expr,{Subscript[x, 1][Distributed]Subscript[dist, 1],Subscript[x, 2][Distributed]Subscript[dist, 2] ,[Ellipsis]}] represents a transformed distribution where Subscript[x, 1], Subscript[x, 2], [Ellipsis] are independent and follow the distributions Subscript[dist, 1],"Subscript[dist, 2], [Ellipsis]". Mar 17, 2020 at 21:35
• @user64494 you are correct that x and y are dependent, but note that I define the problem in terms of the random variables x and u which are independent. The answer I obtain is essentially the same as that given by @JimB but a little shorter. Mar 17, 2020 at 23:34
• @user64494 But all three answers are the same. They just use 3 different approaches. If a large random sample supports one answer, it supports them all. My approach was just more brute force (following a paper and pencil) approach and the others used unknown-to-me flexibility of Mathematica statements and a neat observation about obtaining the joint distribution of $X$ and $Y$ from two independent uniform distributions.
– JimB
Mar 18, 2020 at 15:00

This particular problem can be completely performed with Mathematica without resorting to paper and pencil.

The joint density of $$X$$ and $$Y$$ is given by the product of the marginal density of $$X$$ (which is $$1\over2$$) and the conditional density of $$Y\mid X$$ (which is $$1\over{2\mid X \mid}$$):

f[x_, y_] := Piecewise[{{(1/2)*(1/(2 Abs[x]), -1 <= x <= 1 && Abs[y] <= Abs[x] && x != 0},
{∞, x == 0}}, 0]


Apply ProbabilityDistribution on the joint density:

d = ProbabilityDistribution[f[x, y], {x, -1, 1}, {y, -Abs[x], Abs[x]}] Now find the distribution of $$Z=X+Y$$:

dz = TransformedDistribution[x + y, {x, y} \[Distributed] d];
pdf = PDF[dz, z] Plot[pdf, {z, -2.5, 2.5}] The expression of the pdf can be simplified to

pdf = Piecewise[{{Log[2/Abs[z]]/4, -2 < z < 2}}, 0] • This is done rather by hand with the help of Mathematica. Mar 17, 2020 at 21:37
• @user64494. Yes, I didn't see an a approach that would do that kind of simplification. My feeble attempts using FullSimplify and PiecewiseExpand didn't provide any simplication.
– JimB
Mar 17, 2020 at 21:42
• The result of s = RandomVariate[UniformDistribution[{-1, 1}], 10^5];t = Flatten[ Map[# + RandomVariate[UniformDistribution[{-RealAbs[#], RealAbs[#]}], 1] &, s]];Histogram[t, 40] confirms your answer answer. Mar 18, 2020 at 6:05