\chapter{Distribution Functions}

Several statistical distribution functions and their inverses
are described in this chapter. The functions form a self
contained module that may be added to YAMP if you need them.
They are collected from several sources. Most of them are based
on Numerical Recipes in C. I have brought them up to date by
making them acceptable to C++. I also converted them to using
doubles. Some are translations from Abramowitz and Stegun
\cite{AS:mh}. Some are taken from the Journal of Applied Statistics.

The distribution module contains the 4 most frequently used
continuous distributions in applied statistics. The standard
normal distribution is a normalized version of Euler's error
function. It represents the limiting distribution of sample
means from many random variables. The F-distributions are
derived from the beta distributions, and are used in the
analysis of variance. The t-distributions are also used in
hypothesis testing. The chi-square familly is derived from the
gamma distribution, and are also used in hypothesis testing. 

In general, the probability functions are distribution
functions. They return the probability that a random variable is
less than a value, ie
\begin{equation}
  P[ X \le x ] = \int_{-\infty}^x \!\! f(t) dt
\end{equation}
\noindent where $f(t)$ is a probability density function. The
inverse functions return the value of $x$ for a given
probability. The inverse functions are calculated almost
exclusively by bracketting and bisection.

The test function is a simple menu program called
$DISTTEST.CPP$. These are not the best programs on the market
but they are reasonably accurate for my applications. They tend
to fall apart in the far tails or far extremes of the parameter
spaces. The regions where they fail though is where the
probabilities are extremely close to zero or one. The
non-central distributions may also fail with very large or very
small non-centrality parameters. You might like to see Louis
Baker's new book for another presentation of similar
functions\cite{Bk:mf}. It has most of the functions from
Abramowitz and Stegun too. (A minor miracle in my
humble opinion). You also should not hesitate to turn to the
IMSL or SAS routines for these probabilities since they
represent industry standards.

\section{Header and Description}

The header is given below \index{Distribution Functions}
\begin{verbatim}

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

 double gammln (double xx);
 double gammp(double x, double a, double b);
 double ncbeta(double x, double a, double b, double lam);
 double betai (double a, double b, double x);
 double ncf (double f, double n1, double n2, double lam);
 double probf (double f, double n1, double n2);
 double probnormi (double u);
 double probnorm (double x);
 double ncfinv (double p, double v1, double v2, double lam);
 double probfi (double p, double n1, double n2);
 double nct (double t, double df, double nc);
 double probt (double t, double n1);
 double probti (double p, double df);
 double ncti (double p, double df, double nc);
 double ncgamma (double x, double a, double b, double lam);
 double probchi (double x, double df);
 double ncchi (double x, double df, double lam);
 double ncgammai (double p, double a, double b, double lam);
 double ncchii (double p, double a, double lam);
 double probchii (double p, double df);
\end{verbatim}

All of the probability density functions are in Abramowitz and
Stegun, so I won't bother with typing them in. They are pretty,
but most of the people reading this don't have \LaTeX, so it
will look bad in this document. Besides, leafing through AS is a
good adventure. It always reminds me of the times when people
had to think about the potential value of their equations before
computing them because computation was extremely expensive.
These days people just crunch numbers without really
understanding the mathematical foundation of the powerful
programs we use. Something elegant is being forgotten.

In the following table, I will indicate that the routine
comes from Numerical Recipes by NR and the page number. You will
need their book if you want to understand the code. If
the authors of this reference object to my use of their code, I
will remove it from YAMP. AS stands for an Abramowitz and Stegun
formula.

Throughout the code, \verb+x+ is for quantiles, \verb+p+ or
\verb+u+ are for probabilities, \verb+a+ or \verb+b+ are 
for $\alpha$ or $\beta$, \verb+nc+ or \verb+lam+ are for
non--centrality parameters, and \verb+df+ is for degrees of
freedom. The degrees of freedom are continuous throughout.  
The following table contains the range of the parameters in the
functions and a short description.

\begin{tabular}{|l|l|l|} \hline\hline
{\em Function} & {\em Description } & {\em Ranges } \\ \hline
gammln(xx) & log Gamma function(NR 168) & $xx > 0$ \\
\hline 
gammp(x, a, b) & Incomplete Gamma(NR 172) & $x \ge
0,a,b > 0$ \\ \hline
ncbeta(u,a,b,lam) & Non-central Beta & $u \in [0,1]$, $a,b > 0
$,  and $lam \geq 0,$ 
\\ \hline
ncf(x, n1,n2, lam) & Non-central F  & 
ncbeta( $u = ax/(ax+b)$, $a=n1/2$, $b=n2/2$) \\ \hline
probf(x,n1,n2) & Central F & ncf with lam=0 \\ \hline
probnormi( u ) & Inverse Std. Normal (AS 26.2.23) & $ 0 < u
< 1 $ \\ \hline
probnorm( x ) & Standard Normal (NR 175) & $ x \in
\Re$ \\ \hline
ncfinv( p, v1, v2, lam) & Inverse Non-Central F & $ p \in (0,1)
$ \\ \hline
nct( t, df, nc) & Non-central t & $t \in \Re$, $ df > 0 $, $nc
\geq 0$ \\ \hline
probt( t, df ) & Central t & same as nct but with nc = 0 \\
\hline 
probt( p, df ) & Inverse Central t & $p \in (0,1) $ \\ \hline
probti( p, df, nc) & Inverse Non--central t & same as probt but
$nc > 0$ \\ \hline
ncgamma(g, a, b, lam) & Non-central Gamma & $g,lam \ge 0$ and
$a,b > 0$ \\ \hline
probchi(x,n1) & Central Chi-Square &
ncgamma($g=x/b,a=n1/2,b=0.5,lam=0$) \\ \hline
ncchi(x,n1,lam) & Non-Central Chi-Square & probchi with lam $>$ 0
\\ \hline
ncgammai( p,a,b,lam ) & Inverse Non-central gamma & $p \in
(0,1) $ \\ \hline
ncchii( p, a, lam ) & Inverse Non-central chi-square & $p \in
(0,1)$ \\ \hline
probchii(p, df) & Inverse central Chi-square & $p \in
(0,1)$ \\ 
\hline\hline
\end{tabular}
