# Working with $X^2$, where $X$ has the uniform distribution in $[-b,b]$

Consider $b>0$ and let $X \sim U[-b,b]$ (the continuous uniform distribution in $[-b,b]$ ). I want to do two computations in Mathematica.

First is to compute the density function of $X^2$, which I calculated (by hand) as being $f_{X^2}(x) = \frac{1}{2b\sqrt{x}}\cdot\textbf{I}_{(0,b^2]}(x)$. Then I used the following code to make Mathematica compute this density.

X = UniformDistribution[{-b, b}] ;
Z = TransformedDistribution[X^2,X \[Distributed] UniformDistribution[{-b, b}]];
PDF[Z, x]

This output is weird. First because it's different from my own calculation (which I think are correct), second because $\lim_{x\to b^2}\frac{1}{2b\sqrt{x}} = \frac{1}{2b^2} \neq \frac{1}{4b^2}$, where the last expression is the value of the density suggested by the Mathematica's output. This is a problem, for $f_{X^2}$ has to be a continuous function. Probably the problem is me, not Mathematica, but where is my reasoning wrong?

After this, I want to compute the joint density $f_{(X,X^2)}$ of the random vector $(X,X^2)$ and plot it. In this case I'm a bit lost how to proceed.

Thank you for you help.

• The first line is in effect on a zero measure support – Dr. belisarius Jan 20 '16 at 18:40
• @Dr.belisarius You are right, it won't affect the calculations. Still, it makes the density function not continuous. So, I think a continuous random variable not necessarily has a continuous density function, but a density function continuous almost everywhere. Is that it? – Integral Jan 20 '16 at 18:47
• The joint density will be hard to plot because its support $\{(x,x^2):-b\le x\le b\}$ is a one-dimensional subset of the plane. – Rahul Jan 20 '16 at 19:26
• Your weird output is due to re-using X rather than x (or another dummy variable) in your definition for Z, when you already set X equal to something in the previous line. If you replace X with something else in that line, it gives the expected result. – Michael Seifert Jan 20 '16 at 21:10
• @MichaelSeifert You are totally right, I don't even need that first line. Thank you! – Integral Jan 20 '16 at 22:46

Explicitly telling Mathematica that b is real and positive seems to help.

Assuming[b > 0,
X = UniformDistribution[{-b, b}];
Z = TransformedDistribution[X^2, X \[Distributed] UniformDistribution[{-b, b}]];
PDF[Z, x]
]

Assuming[b > 0,
XZ = ProductDistribution[X, Z];
pdf = PDF[XZ, {x, z}]
]

Block[{b = 1},
Plot3D[pdf, {x, -1, 1}, {z, -1, 1}]]  (* or {z, 0, 1} *)

AFAICT, the ProductDistribution is the joint distribution as described in the documentation, assuming the component distributions are independent (maybe that's not what the OP meant); it is not the Product Distribution described on Wikipedia.

Since the joint distribution function is singular (proportional to $\delta(y - x^2)$ or something like that), you'll have to be creative about plotting it. Here's an attempt that's kludgey as hell, but it does give something:

b = 1;
ListPointPlot3D[Table[{x, x^2, PDF[Z, x] PDF[X, x]}, {x, -2 b, 2 b, b/100}],
RegionFunction -> Function[{x, y, z}, z > 0],
PlotRange -> {{-2 b, 2 b}, Automatic, Automatic}, Filling -> Bottom]
Clear[b]

(Method cribbed from the answers here.) I didn't want to use ParametricPlot3D because it didn't really convey that this was a function of $x$ and $y$ that happened to be zero except along a line; you can judge for yourself whether my code does that any better.

• Isn't there a way to explicitly compute the joint density of $(X, X^2)$ ? The plot would follow easily from that. – Integral Jan 21 '16 at 0:03

If you start with $b = 1$ (for instance), you get (Mathematica version 10.3):

$\begin{cases} \text{Indeterminate} & x=0\lor x=1 \\ \frac{1}{2 \sqrt{x}} & 0<x<1 \end{cases}$

Which seems perfect.

Plot3D[ 1/(4 Sqrt[x]), {x, -1, 1}, {y, -1, 1}]

• Thank you for your response, but this question has two parts. In fact, the second is more important (the first is just a technical issue). Also, what is (v. 10.3) ? Thanks. – Integral Jan 20 '16 at 18:54
• Are you sure this plot is the joint density function of $(X,X²)$ ? Just for reference: probabilitycourse.com/chapter5/5_2_1_joint_pdf.php – Integral Jan 20 '16 at 19:26