Simple addition to random module - Student's t

Thomas Philips tkpmep at gmail.com
Wed Sep 2 19:03:07 CEST 2009


On Sep 2, 12:28 pm, Mark Dickinson <dicki... at gmail.com> wrote:
> On Sep 2, 2:51 pm, Thomas Philips <tkp... at gmail.com> wrote:
>
> > 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'
>
> By the way, why do you exclude the possibility df=1 here?
>
> --
> Mark

I exclude df=1 hereBecause the variance is then infinite (in fact, the
distribution is then Cauchy). That said, your point is well taken;
allowing df=1 makes the Cauchy distribution available to users of
random, in much the same way as the Gamma makes the Chi-squared
available. Here's the revised code:

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

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

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


I'll follow your suggestion, add in documentation and submit it to
bugs.python.com. Thanks for your guidance.

Thomas Philips




More information about the Python-list mailing list