6
$\begingroup$

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?

$\endgroup$
9
  • $\begingroup$ 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. $\endgroup$
    – JimB
    Commented Mar 17, 2020 at 16:24
  • 1
    $\begingroup$ Your $Y\sim U[-X,X]$.is not correctly defined: think of negative values of $X\sim U[-1,1]$ . $\endgroup$
    – user64494
    Commented Mar 17, 2020 at 17:28
  • 1
    $\begingroup$ @FZS With your edit for the reason to solve that problem, I agree. $\endgroup$
    – JimB
    Commented Mar 17, 2020 at 17:45
  • 1
    $\begingroup$ @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". $\endgroup$
    – user64494
    Commented Mar 17, 2020 at 21:05
  • 1
    $\begingroup$ @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. $\endgroup$
    – JimB
    Commented Mar 17, 2020 at 21:08

3 Answers 3

5
$\begingroup$

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]
$\endgroup$
5
  • 1
    $\begingroup$ 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$. $\endgroup$
    – JimB
    Commented Mar 17, 2020 at 19:59
  • $\begingroup$ +1 Your edit fixed things. $\endgroup$
    – JimB
    Commented Mar 17, 2020 at 22:02
  • $\begingroup$ @JimB You're right, just needed some basic probability knowledge refreshment :) $\endgroup$
    – swish
    Commented Mar 17, 2020 at 22:04
  • $\begingroup$ Method -> "Normalize" doesn't work in v10.1; is it strictly necessary here? What purpose does it serve? $\endgroup$
    – Mr.Wizard
    Commented Mar 18, 2020 at 16:15
  • 1
    $\begingroup$ @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. $\endgroup$
    – swish
    Commented Mar 18, 2020 at 16:28
8
$\begingroup$

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[2]/4, t == 0}, {Log[-2/t]/4, Inequality[-2, Less, t, Less, 0]}, 
  {Log[2/t]/4, Inequality[0, Less, t, Less, 2]}}, 0] *)
$\endgroup$
6
  • 2
    $\begingroup$ +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[2]/4? And why not have that value being $\infty$? $\endgroup$
    – JimB
    Commented Mar 17, 2020 at 21:00
  • 2
    $\begingroup$ @JimB intriguing question. No idea why Mathematica did that - of course the value at a set of measure 0 has no practical effect. $\endgroup$
    – mikado
    Commented Mar 17, 2020 at 21:10
  • $\begingroup$ 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]". $\endgroup$
    – user64494
    Commented Mar 17, 2020 at 21:35
  • 2
    $\begingroup$ @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. $\endgroup$
    – mikado
    Commented Mar 17, 2020 at 23:34
  • $\begingroup$ @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. $\endgroup$
    – JimB
    Commented Mar 18, 2020 at 15:00
8
$\begingroup$

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

Distribution of X and Y

Now find the distribution of $Z=X+Y$:

dz = TransformedDistribution[x + y, {x, y} \[Distributed] d];
pdf = PDF[dz, z]

Density of sum of X and Y

Plot[pdf, {z, -2.5, 2.5}]

Plot of density function

The expression of the pdf can be simplified to

pdf = Piecewise[{{Log[2/Abs[z]]/4, -2 < z < 2}}, 0]

Short version of pdf

$\endgroup$
3
  • 3
    $\begingroup$ This is done rather by hand with the help of Mathematica. $\endgroup$
    – user64494
    Commented Mar 17, 2020 at 21:37
  • $\begingroup$ @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. $\endgroup$
    – JimB
    Commented Mar 17, 2020 at 21:42
  • $\begingroup$ 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. $\endgroup$
    – user64494
    Commented Mar 18, 2020 at 6:05

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.