# Mean and variance of a function of a (continuous) random variable

I am interested in calculating the mean and the variance of a (univariate continuous) random variable whose density function can be actually expressed as a transformation of the density function of another random variable; more precisely, it happens to be a transformation of the doubly noncentral $F$ distribution (NoncentralFRatioDistribution in Mathematica).

As an example, if I try this:

Mean[NoncentralFRatioDistribution[7, 3, 0, 0.5]]


I get:

2.54662


But if I try this:

dist1 := ProbabilityDistribution[Sqrt[PDF[NoncentralFRatioDistribution[7, 3, 0, 0.5], x]],
{x, 0, \[Infinity]}, Method -> "Normalize"]


and then I ask for the mean of this transformed variable:

Mean[dist1]


I get:

Mean[ProbabilityDistribution[
0.188114 Sqrt[
PDF[NoncentralFRatioDistribution[7, 3, 0,
0.5], \[FormalX]]], {\[FormalX], 0, \[Infinity]}]]


Is it that the expected behaviour of Mathematica 11.1? Am I doing something wrong? What else can I try?

• As shown in the answers below, one applies the transformation to the random variable and not the density function. – JimB Sep 7 '17 at 17:06

First, we define your distribution using TransformedDistribution:

dist1 = TransformedDistribution[Sqrt[x], x \[Distributed] NoncentralFRatioDistribution[7., 3., 0., 0.5]]


Now, we can get the mean using N:

N@Mean@dist1
(* 1.23037 *)

• Wow! I still have to read it twice, but thank you! – Vicent Sep 7 '17 at 9:36
• There is also the shortcut NExpectation[Sqrt[x], x \[Distributed] NoncentralFRatioDistribution[7., 3., 0., 0.5]]. – Sjoerd Smit Sep 7 '17 at 10:19
• @Mathe172 Why is N@Mean working better than the sentence Mean (at least in the examples I've been trying)? – Vicent Sep 7 '17 at 14:57
• @Vicent I wondered about that myself... I assume MMA somehow wants to give you an "exact" result, which it cannot do. N forces it to evaluate it numerically, which gives it more freedom in what to do. But this is just a guess - I could be completely wrong. – Lukas Lang Sep 7 '17 at 17:17
f = NoncentralFRatioDistribution[7, 3, 0, 0.5];
td = TransformedDistribution[Sqrt[x], x \[Distributed] f];


You can simulate:

rv = RandomVariate[td, 100000] // Mean
rvf = Mean@Sqrt[RandomVariate[f, 100000]]


or explicitly calculate:

NIntegrate[x PDF[td, x], {x, 0, Infinity}]


or use N@Mean@td

Outputs: 1.236, 1.22386, 1.23037, 1.23037