Blame randist/tdist.c

Packit 67cb25
/* randist/tdist.c
Packit 67cb25
 * 
Packit 67cb25
 * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 James Theiler, Brian Gough
Packit 67cb25
 * 
Packit 67cb25
 * This program is free software; you can redistribute it and/or modify
Packit 67cb25
 * it under the terms of the GNU General Public License as published by
Packit 67cb25
 * the Free Software Foundation; either version 3 of the License, or (at
Packit 67cb25
 * your option) any later version.
Packit 67cb25
 * 
Packit 67cb25
 * This program is distributed in the hope that it will be useful, but
Packit 67cb25
 * WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 67cb25
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 67cb25
 * General Public License for more details.
Packit 67cb25
 * 
Packit 67cb25
 * You should have received a copy of the GNU General Public License
Packit 67cb25
 * along with this program; if not, write to the Free Software
Packit 67cb25
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Packit 67cb25
 */
Packit 67cb25
Packit 67cb25
#include <config.h>
Packit 67cb25
#include <math.h>
Packit 67cb25
#include <gsl/gsl_math.h>
Packit 67cb25
#include <gsl/gsl_sf_gamma.h>
Packit 67cb25
#include <gsl/gsl_rng.h>
Packit 67cb25
#include <gsl/gsl_randist.h>
Packit 67cb25
Packit 67cb25
/* The t-distribution has the form
Packit 67cb25
Packit 67cb25
   p(x) dx = (Gamma((nu + 1)/2)/(sqrt(pi nu) Gamma(nu/2))
Packit 67cb25
   * (1 + (x^2)/nu)^-((nu + 1)/2) dx
Packit 67cb25
Packit 67cb25
   The method used here is the one described in Knuth */
Packit 67cb25
Packit 67cb25
double
Packit 67cb25
gsl_ran_tdist (const gsl_rng * r, const double nu)
Packit 67cb25
{
Packit 67cb25
  if (nu <= 2)
Packit 67cb25
    {
Packit 67cb25
      double Y1 = gsl_ran_ugaussian (r);
Packit 67cb25
      double Y2 = gsl_ran_chisq (r, nu);
Packit 67cb25
Packit 67cb25
      double t = Y1 / sqrt (Y2 / nu);
Packit 67cb25
Packit 67cb25
      return t;
Packit 67cb25
    }
Packit 67cb25
  else
Packit 67cb25
    {
Packit 67cb25
      double Y1, Y2, Z, t;
Packit 67cb25
      do
Packit 67cb25
        {
Packit 67cb25
          Y1 = gsl_ran_ugaussian (r);
Packit 67cb25
          Y2 = gsl_ran_exponential (r, 1 / (nu/2 - 1));
Packit 67cb25
Packit 67cb25
          Z = Y1 * Y1 / (nu - 2);
Packit 67cb25
        }
Packit 67cb25
      while (1 - Z < 0 || exp (-Y2 - Z) > (1 - Z));
Packit 67cb25
Packit 67cb25
      /* Note that there is a typo in Knuth's formula, the line below
Packit 67cb25
         is taken from the original paper of Marsaglia, Mathematics of
Packit 67cb25
         Computation, 34 (1980), p 234-256 */
Packit 67cb25
Packit 67cb25
      t = Y1 / sqrt ((1 - 2 / nu) * (1 - Z));
Packit 67cb25
      return t;
Packit 67cb25
    }
Packit 67cb25
}
Packit 67cb25
Packit 67cb25
double
Packit 67cb25
gsl_ran_tdist_pdf (const double x, const double nu)
Packit 67cb25
{
Packit 67cb25
  double p;
Packit 67cb25
Packit 67cb25
  double lg1 = gsl_sf_lngamma (nu / 2);
Packit 67cb25
  double lg2 = gsl_sf_lngamma ((nu + 1) / 2);
Packit 67cb25
Packit 67cb25
  p = ((exp (lg2 - lg1) / sqrt (M_PI * nu)) 
Packit 67cb25
       * pow ((1 + x * x / nu), -(nu + 1) / 2));
Packit 67cb25
  return p;
Packit 67cb25
}
Packit 67cb25
Packit 67cb25