Blame randist/pascal.c

Packit 67cb25
/* randist/pascal.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_rng.h>
Packit 67cb25
#include <gsl/gsl_randist.h>
Packit 67cb25
Packit 67cb25
/* The Pascal distribution is a negative binomial with valued integer n
Packit 67cb25
Packit 67cb25
   prob(k) =  (n - 1 + k)!/(n!(k - 1)!) *  p^n (1-p)^k for k = 0, 1, ..., n
Packit 67cb25
Packit 67cb25
   */
Packit 67cb25
Packit 67cb25
unsigned int
Packit 67cb25
gsl_ran_pascal (const gsl_rng * r, double p, unsigned int n)
Packit 67cb25
{
Packit 67cb25
  /* This is a separate interface for the pascal distribution so that
Packit 67cb25
     it can be optimized differently from the negative binomial in
Packit 67cb25
     future.
Packit 67cb25
     
Packit 67cb25
     e.g. if n < 10 it might be faster to generate the Pascal
Packit 67cb25
     distributions as the sum of geometric variates directly.  */
Packit 67cb25
  
Packit 67cb25
  unsigned int k = gsl_ran_negative_binomial (r, p, (double) n);
Packit 67cb25
  return k;
Packit 67cb25
}
Packit 67cb25
Packit 67cb25
double
Packit 67cb25
gsl_ran_pascal_pdf (const unsigned int k, const double p, unsigned int n)
Packit 67cb25
{
Packit 67cb25
  double P = gsl_ran_negative_binomial_pdf (k, p, (double) n);
Packit 67cb25
  return P;
Packit 67cb25
}