Simple addition to random module - Student's t



While the random module allows one to generate randome numbers with a
variety of distributions, some useful distributions are omitted - the
Student's t being among them. This distribution is easily derived from
the normal distribution and the chi-squared distribution (which in
turn is a special case of the gamma distribution). I edited and tested
a routine to generate random variables with a Student's t distribution
that I found on http://www.johndcook.com/python_student_t_rng.html,
which has one bug - there is an extra factor of two in y. The
corrected and tested code follows - how does one go about getting this
incorporated into random so that the entire community can beneffit
from it?

Sincerely

Thomas Philips

def student_t(df): # df is the number of degrees of freedom
if df < 2 or int(df) != df:
raise ValueError, 'student_tvariate: df must be a integer > 1'

x = random.gauss(0, 1)
y = random.gammavariate(df/2.0, 2)

return x / (math.sqrt(y/df))


References:

1. Student's t distribution, including relationship to normal and chi-
squared distributions: http://en.wikipedia.org/wiki/Student's_t-distribution
2. Chi-squared distribution, including relationship to Gamma
distribution: http://en.wikipedia.org/wiki/Chi-square_distribution
3. John Cook's original version (with the extra factor of 2):
http://www.johndcook.com/python_student_t_rng.html
.



Relevant Pages

  • Re: On coverage probability of Confidence interval
    ... David Jones wrote: ... gamma distribution with a known shape parameter, ... remember the relation of the gamma to the chi-squared distribution, ...
    (sci.stat.math)
  • Re: Simple addition to random module - Students t
    ... This distribution is easily derived from ... corrected and tested code follows - how does one go about getting this ... To get this into core Python, you'd usually submit a feature request ... There's a lot of good information about how the Python ...
    (comp.lang.python)
  • Special Functions Revisited By PI
    ... forums developing relationships between PI and "Special Functions", ... the gamma distribution in probability-statistics, ... probably the main example of how PI goes beyond Shannon entropy ...
    (sci.stat.math)
  • Re: Gamma Distribution Question
    ... it follows a Gamma distribution better than a normal ... What relation does it have to Poisson processes? ... the nth-order interarrival time in a Poisson process has gamma distribution. ... But it looks like you are considering the *number* of harvests per month, ...
    (sci.stat.math)
  • Re: normal distribution with uncertain mean and standard deviation
    ... distribution for the variance parameter, ... However, while there's a connection to Bayesian calculations, ... variance parameter had a gamma distribution I think the result would ...
    (sci.stat.math)