# How to obtain the variance of a chi-squared distribution with Mathematica

I'm new to Mathematica and would like to show that for $$x_1, ..., x_k$$ independent, standard normal random variables the variance of the sum of their squares, i.e., $$var(\sum_{i=1}^kx_k^2)$$, is equal to $$2k$$.

Therefore, I implemented the following:

x= NormalDistribution[mx,stdx];

Variance[TransformedDistribution[Sum[a*a,{i,i_max}], {Distributed[a, x]}]];


The result is not 2i_max. However, the output is:

2(2mx^2stdx^2i_max^2 + stdx^4i_max^2)


What am I doing wrong?

• i_max is not a valid variable name. Use imax, instead. Nov 20 '21 at 22:36
• The result is the same only with imax replaced by i_max. Nov 21 '21 at 1:40
• Your code only has a single random variable ($a$) so the result is correct for that. You are essentially getting the variance of $imax * a$. What you probably intend (because you state the answer is $2k$) is that you have $k$ independent and identically distributed random variables. Therefore you should use a[i] instead of a.
– JimB
Nov 21 '21 at 1:47
• Then, the result is 0. Or do I also need to change something inside “Distributed“ ? Nov 21 '21 at 1:58
• var[k_] = FindSequenceFunction[seq, k] where seq is the sequence provided by @JimB Alternatively, you can do a proof by induction. Nov 21 '21 at 3:00

## 2 Answers

Besides the ways given in the comments a more direct symbolic approach is to use moment generating functions (or characteristic functions). One obtains the mgf for the square of a unit normal and the mgf of the sum of n independent and identically distributed random variables is the individual mgf raised to the n-th power. Then find the variance from that resulting mgf.

(* Determine moment generating function for a single random variable *)
mgf = MomentGeneratingFunction[TransformedDistribution[x^2, x \[Distributed] NormalDistribution[0, 1]], t];

(* First raw moment of the sum of n iid random variables *)
m1 = (D[mgf^n, t]) /. t -> 0;

(* Second raw moment *)
m2 = (D[mgf^n, {t, 2}]) /. t -> 0;

(* Find variance *)
variance = m2 - m1^2 // Expand
(* 2 n *)

• Thanks a lot! That works. I'd like to calculate now the variance of $\sum_{i=1}^k x_i(x_i+y_i)$ with $k$ independent and identically distributed random variables $x_i, y_i$. Therefore, I changed your first line to: mgf = MomentGeneratingFunction[TransformedDistribution[x*(x+y), { x \[Distributed] NormalDistribution[0, s1],y \[Distributed] NormalDistribution[0, s2]}], t];. Is that correct? The solution is, unfortunately, not such a short expression and contains different terms with "MomentGeneratingFunction". Nov 21 '21 at 8:11
• @user82859 - your comment is not a clarification of your original question but rather a whole new question. This should be posted as a new and separate question. You should link back here for context. You should also upvote this answer and, since it apparently answers your original question, accept the answer. Nov 21 '21 at 15:04
• Thanks, Bob, you’re right! I upvoted your answer. Thanks again, this really helped. I try to adjust to the stackexchange standards as fast as possible. :-) Nov 21 '21 at 15:11
• This is @JimB answer and you have not upvoted it yet since the count only shows 1 which is mine. Nov 21 '21 at 16:16
• It says “ Thanks for the feedback! You need at least 15 reputation to cast a vote, but your feedback has been recorded.” Nov 21 '21 at 21:01

Using proof by induction

Clear["Global*"]

\$Version

(* "12.3.1 for Mac OS X x86 (64-bit) (June 19, 2021)" *)


Each individual term of the sum of squares of i.i.d. standard normal variates (i.e., NormalDistribution[0, 1]) are distributed ChiSquareDistribution[1]

TransformedDistribution[x^2,
x \[Distributed] NormalDistribution[]]

(* ChiSquareDistribution[1] *)


The problem is then just the sum of k i.i.d. variates each with distribution ChiSquareDistribution[1]

The distribution of the sum of two of these variates is

TransformedDistribution[z1 + z2,
{z1 \[Distributed] ChiSquareDistribution[1],
z2 \[Distributed] ChiSquareDistribution[1]}]

(* ChiSquareDistribution[2] *)


Consequently, assuming that the sum of k - 1 of these variates is distributed ChiSquareDistribution[k - 1] then the sum of k variates would be

TransformedDistribution[z1 + z2,
{z1 \[Distributed] ChiSquareDistribution[k - 1],
z2 \[Distributed] ChiSquareDistribution[1]}]

(* ChiSquareDistribution[k] *)


This is consistent with the assumption; so if it is true for k - 1 it is also true for k. Since the assumption is already shown to be true for k = 2 then by induction it is true for all k >= 2

The Variance is then

Variance[ChiSquareDistribution[k]]

(* 2 k *)
`
• +1 This answer is more general (a very good thing) and doesn't require "special knowledge" about moment generating functions.
– JimB
Nov 21 '21 at 17:42