Solved – Generating random numbers from a t-distribution

excelrandom-generationsamplingt-distribution

How can I generate random numbers that follow a student-t distribution?
From several sources I understand that this can be done using a random sample of size $n$ drawn from a normally distributed population, as follows:

$$
t = \frac {(x – m)} {(s/\sqrt n)}
$$

Where $x$ is the sample mean, $m$ is the mean of the normal distribution (I assume you can just use the standard normal distribution, so $m=0$?), and $s$ is the sample standard deviation.

The degrees of freedom of the student-t distribution will then be $n-1$.

Do I understand correctly that in order to generate a random student-t value with $f$ degrees of freedom, I should first generate $f+1$ normally distributed values (i.e. standard normal), and then calculate the mean ($x$) and standard deviation ($s$) of these and apply the above formula?
And if I do this repeatedly many times, the resulting random values will approach a student-t distribution with $f$ degrees of freedom?

I tried this in Excel using a macro that uses the above formula and another macro that generates random Gaussians (which works, I tested it) but the resulting random values do not seem to be completely student-t distributed. For instance with 6 degrees of freedom, the variance of 10,000 random values is about $1.7$ while it should be $6/(6-2) = 1.5$.

Best Answer

I have an answer to the practical part of your question, though not quite the theoretical one.

There is a function called TINV that directly does this. Except that it conly returns positive random t variates. You can get around that limitation with the following formula:

=TINV(RAND(),6)*(RANDBETWEEN(0,1)*2-1)

...you can replace 6 with whatever value you want for the DF, and the rand() can be replaced with any number between 0 and 1. The rest of it simply guarantees equal probability of negative and positive values.