Blame doc/randist.rst

Packit 67cb25
.. index::
Packit 67cb25
   single: random number distributions
Packit 67cb25
   single: cumulative distribution functions (CDFs)
Packit 67cb25
   single: CDFs, cumulative distribution functions
Packit 67cb25
   single: inverse cumulative distribution functions
Packit 67cb25
   single: quantile functions
Packit 67cb25
Packit 67cb25
.. _chap_random-number-distributions:
Packit 67cb25
Packit 67cb25
***************************
Packit 67cb25
Random Number Distributions
Packit 67cb25
***************************
Packit 67cb25
Packit 67cb25
.. include:: include.rst
Packit 67cb25
Packit 67cb25
This chapter describes functions for generating random variates and
Packit 67cb25
computing their probability distributions.  Samples from the
Packit 67cb25
distributions described in this chapter can be obtained using any of the
Packit 67cb25
random number generators in the library as an underlying source of
Packit 67cb25
randomness.  
Packit 67cb25
Packit 67cb25
In the simplest cases a non-uniform distribution can be obtained
Packit 67cb25
analytically from the uniform distribution of a random number generator
Packit 67cb25
by applying an appropriate transformation.  This method uses one call to
Packit 67cb25
the random number generator.  More complicated distributions are created
Packit 67cb25
by the *acceptance-rejection* method, which compares the desired
Packit 67cb25
distribution against a distribution which is similar and known
Packit 67cb25
analytically.  This usually requires several samples from the generator.
Packit 67cb25
Packit 67cb25
The library also provides cumulative distribution functions and inverse
Packit 67cb25
cumulative distribution functions, sometimes referred to as quantile
Packit 67cb25
functions.  The cumulative distribution functions and their inverses are
Packit 67cb25
computed separately for the upper and lower tails of the distribution,
Packit 67cb25
allowing full accuracy to be retained for small results.
Packit 67cb25
Packit 67cb25
The functions for random variates and probability density functions
Packit 67cb25
described in this section are declared in :file:`gsl_randist.h`.  The
Packit 67cb25
corresponding cumulative distribution functions are declared in
Packit 67cb25
:file:`gsl_cdf.h`.
Packit 67cb25
Packit 67cb25
Note that the discrete random variate functions always
Packit 67cb25
return a value of type :code:`unsigned int`, and on most platforms this
Packit 67cb25
has a maximum value of
Packit 67cb25
Packit 67cb25
.. only:: not texinfo
Packit 67cb25
   
Packit 67cb25
   .. math:: 2^{32}-1 \approx 4.29 \times 10^9
Packit 67cb25
Packit 67cb25
.. only:: texinfo
Packit 67cb25
Packit 67cb25
   ::
Packit 67cb25
Packit 67cb25
      2^32-1 ~=~ 4.29e9
Packit 67cb25
Packit 67cb25
They should only be called with
Packit 67cb25
a safe range of parameters (where there is a negligible probability of
Packit 67cb25
a variate exceeding this limit) to prevent incorrect results due to
Packit 67cb25
overflow.
Packit 67cb25
Packit 67cb25
Introduction
Packit 67cb25
============
Packit 67cb25
Packit 67cb25
Continuous random number distributions are defined by a probability
Packit 67cb25
density function, :math:`p(x)`, such that the probability of :math:`x`
Packit 67cb25
occurring in the infinitesimal range :math:`x` to :math:`x + dx` is
Packit 67cb25
:math:`p(x) dx`.
Packit 67cb25
Packit 67cb25
The cumulative distribution function for the lower tail :math:`P(x)` is
Packit 67cb25
defined by the integral,
Packit 67cb25
Packit 67cb25
.. math:: P(x) = \int_{-\infty}^{x} dx' p(x')
Packit 67cb25
Packit 67cb25
and gives the probability of a variate taking a value less than :math:`x`.
Packit 67cb25
Packit 67cb25
The cumulative distribution function for the upper tail :math:`Q(x)` is
Packit 67cb25
defined by the integral,
Packit 67cb25
Packit 67cb25
.. math:: Q(x) = \int_{x}^{+\infty} dx' p(x')
Packit 67cb25
Packit 67cb25
and gives the probability of a variate taking a value greater than :math:`x`.
Packit 67cb25
Packit 67cb25
The upper and lower cumulative distribution functions are related by
Packit 67cb25
:math:`P(x) + Q(x) = 1` and satisfy :math:`0 \le P(x) \le 1`,
Packit 67cb25
:math:`0 \le Q(x) \le 1`.
Packit 67cb25
Packit 67cb25
The inverse cumulative distributions, :math:`x = P^{-1}(P)`
Packit 67cb25
and :math:`x = Q^{-1}(Q)`
Packit 67cb25
give the values of :math:`x`
Packit 67cb25
which correspond to a specific value of :math:`P` or :math:`Q`.  
Packit 67cb25
They can be used to find confidence limits from probability values.
Packit 67cb25
Packit 67cb25
For discrete distributions the probability of sampling the integer
Packit 67cb25
value :math:`k` is given by :math:`p(k)`, where :math:`\sum_k p(k) = 1`.
Packit 67cb25
The cumulative distribution for the lower tail :math:`P(k)` of a
Packit 67cb25
discrete distribution is defined as,
Packit 67cb25
Packit 67cb25
.. math:: P(k) = \sum_{i \le k} p(i) 
Packit 67cb25
Packit 67cb25
where the sum is over the allowed range of the distribution less than
Packit 67cb25
or equal to :math:`k`.  
Packit 67cb25
Packit 67cb25
The cumulative distribution for the upper tail of a discrete
Packit 67cb25
distribution :math:`Q(k)` is defined as
Packit 67cb25
Packit 67cb25
.. math:: Q(k) = \sum_{i > k} p(i) 
Packit 67cb25
Packit 67cb25
giving the sum of probabilities for all values greater than :math:`k`.
Packit 67cb25
These two definitions satisfy the identity :math:`P(k)+Q(k)=1`.
Packit 67cb25
Packit 67cb25
If the range of the distribution is 1 to :math:`n` inclusive then
Packit 67cb25
:math:`P(n) = 1`, :math:`Q(n) = 0` while :math:`P(1) = p(1)`,
Packit 67cb25
:math:`Q(1) = 1 - p(1)`.
Packit 67cb25
Packit 67cb25
|newpage|
Packit 67cb25
Packit 67cb25
The Gaussian Distribution
Packit 67cb25
=========================
Packit 67cb25
Packit 67cb25
.. index:: Gaussian distribution
Packit 67cb25
Packit 67cb25
.. function:: double gsl_ran_gaussian (const gsl_rng * r, double sigma)
Packit 67cb25
Packit 67cb25
   This function returns a Gaussian random variate, with mean zero and
Packit 67cb25
   standard deviation :data:`sigma`.  The probability distribution for
Packit 67cb25
   Gaussian random variates is,
Packit 67cb25
Packit 67cb25
   .. math:: p(x) dx = {1 \over \sqrt{2 \pi \sigma^2}} \exp (-x^2 / 2\sigma^2) dx
Packit 67cb25
Packit 67cb25
   for :math:`x` in the range :math:`-\infty` to :math:`+\infty`.  Use the
Packit 67cb25
   transformation :math:`z = \mu + x` on the numbers returned by
Packit 67cb25
   :func:`gsl_ran_gaussian` to obtain a Gaussian distribution with mean
Packit 67cb25
   :math:`\mu`.  This function uses the Box-Muller algorithm which requires two
Packit 67cb25
   calls to the random number generator :data:`r`.
Packit 67cb25
Packit 67cb25
.. function:: double gsl_ran_gaussian_pdf (double x, double sigma)
Packit 67cb25
Packit 67cb25
   This function computes the probability density :math:`p(x)` at :data:`x`
Packit 67cb25
   for a Gaussian distribution with standard deviation :data:`sigma`, using
Packit 67cb25
   the formula given above.
Packit 67cb25
Packit 67cb25
   .. image:: /images/rand-gaussian.png
Packit 67cb25
Packit 67cb25
.. index:: Ziggurat method
Packit 67cb25
Packit 67cb25
.. function:: double gsl_ran_gaussian_ziggurat (const gsl_rng * r, double sigma)
Packit 67cb25
              double gsl_ran_gaussian_ratio_method (const gsl_rng * r, double sigma)
Packit 67cb25
Packit 67cb25
   This function computes a Gaussian random variate using the alternative
Packit 67cb25
   Marsaglia-Tsang ziggurat and Kinderman-Monahan-Leva ratio methods.  The
Packit 67cb25
   Ziggurat algorithm is the fastest available algorithm in most cases.
Packit 67cb25
Packit 67cb25
.. function:: double gsl_ran_ugaussian (const gsl_rng * r)
Packit 67cb25
              double gsl_ran_ugaussian_pdf (double x)
Packit 67cb25
              double gsl_ran_ugaussian_ratio_method (const gsl_rng * r)
Packit 67cb25
Packit 67cb25
   These functions compute results for the unit Gaussian distribution.  They
Packit 67cb25
   are equivalent to the functions above with a standard deviation of one,
Packit 67cb25
   :data:`sigma` = 1.
Packit 67cb25
Packit 67cb25
.. function:: double gsl_cdf_gaussian_P (double x, double sigma)
Packit 67cb25
              double gsl_cdf_gaussian_Q (double x, double sigma)
Packit 67cb25
              double gsl_cdf_gaussian_Pinv (double P, double sigma)
Packit 67cb25
              double gsl_cdf_gaussian_Qinv (double Q, double sigma)
Packit 67cb25
Packit 67cb25
   These functions compute the cumulative distribution functions
Packit 67cb25
   :math:`P(x)`, :math:`Q(x)` and their inverses for the Gaussian
Packit 67cb25
   distribution with standard deviation :data:`sigma`.
Packit 67cb25
Packit 67cb25
.. function:: double gsl_cdf_ugaussian_P (double x)
Packit 67cb25
              double gsl_cdf_ugaussian_Q (double x)
Packit 67cb25
              double gsl_cdf_ugaussian_Pinv (double P)
Packit 67cb25
              double gsl_cdf_ugaussian_Qinv (double Q)
Packit 67cb25
Packit 67cb25
   These functions compute the cumulative distribution functions
Packit 67cb25
   :math:`P(x)`, :math:`Q(x)` and their inverses for the unit Gaussian
Packit 67cb25
   distribution.
Packit 67cb25
Packit 67cb25
|newpage|
Packit 67cb25
Packit 67cb25
The Gaussian Tail Distribution
Packit 67cb25
==============================
Packit 67cb25
Packit 67cb25
.. index:: Gaussian Tail distribution
Packit 67cb25
Packit 67cb25
.. function:: double gsl_ran_gaussian_tail (const gsl_rng * r, double a, double sigma)
Packit 67cb25
Packit 67cb25
   This function provides random variates from the upper tail of a Gaussian
Packit 67cb25
   distribution with standard deviation :data:`sigma`.  The values returned
Packit 67cb25
   are larger than the lower limit :data:`a`, which must be positive.  The
Packit 67cb25
   method is based on Marsaglia's famous rectangle-wedge-tail algorithm (Ann. 
Packit 67cb25
   Math. Stat. 32, 894--899 (1961)), with this aspect explained in Knuth, v2,
Packit 67cb25
   3rd ed, p139,586 (exercise 11).
Packit 67cb25
Packit 67cb25
   The probability distribution for Gaussian tail random variates is,
Packit 67cb25
Packit 67cb25
   .. math:: p(x) dx = {1 \over N(a;\sigma) \sqrt{2 \pi \sigma^2}} \exp (- x^2 / 2\sigma^2) dx
Packit 67cb25
Packit 67cb25
   for :math:`x > a` where :math:`N(a;\sigma)` is the normalization constant,
Packit 67cb25
Packit 67cb25
   .. only:: not texinfo
Packit 67cb25
Packit 67cb25
      .. math:: N(a;\sigma) = {1 \over 2} \hbox{erfc}\left({a \over \sqrt{2 \sigma^2}}\right).
Packit 67cb25
Packit 67cb25
   .. only:: texinfo
Packit 67cb25
Packit 67cb25
      ::
Packit 67cb25
Packit 67cb25
         N(a;\sigma) = (1/2) erfc(a / sqrt(2 sigma^2)).
Packit 67cb25
Packit 67cb25
.. function:: double gsl_ran_gaussian_tail_pdf (double x, double a, double sigma)
Packit 67cb25
Packit 67cb25
   This function computes the probability density :math:`p(x)` at :data:`x`
Packit 67cb25
   for a Gaussian tail distribution with standard deviation :data:`sigma` and
Packit 67cb25
   lower limit :data:`a`, using the formula given above.
Packit 67cb25
Packit 67cb25
   .. image:: /images/rand-gaussian-tail.png
Packit 67cb25
Packit 67cb25
.. function:: double gsl_ran_ugaussian_tail (const gsl_rng * r, double a)
Packit 67cb25
              double gsl_ran_ugaussian_tail_pdf (double :data:`x`, double :data:`a`)
Packit 67cb25
Packit 67cb25
   These functions compute results for the tail of a unit Gaussian
Packit 67cb25
   distribution.  They are equivalent to the functions above with a standard
Packit 67cb25
   deviation of one, :data:`sigma` = 1.
Packit 67cb25
Packit 67cb25
|newpage|
Packit 67cb25
Packit 67cb25
The Bivariate Gaussian Distribution
Packit 67cb25
===================================
Packit 67cb25
Packit 67cb25
.. index::
Packit 67cb25
   single: Bivariate Gaussian distribution
Packit 67cb25
   single: two dimensional Gaussian distribution
Packit 67cb25
   single: Gaussian distribution, bivariate
Packit 67cb25
Packit 67cb25
.. function:: void gsl_ran_bivariate_gaussian (const gsl_rng * r, double sigma_x, double sigma_y, double rho, double * x, double * y)
Packit 67cb25
Packit 67cb25
   This function generates a pair of correlated Gaussian variates, with
Packit 67cb25
   mean zero, correlation coefficient :data:`rho` and standard deviations
Packit 67cb25
   :data:`sigma_x` and :data:`sigma_y` in the :math:`x` and :math:`y` directions.
Packit 67cb25
   The probability distribution for bivariate Gaussian random variates is,
Packit 67cb25
Packit 67cb25
   .. only:: not texinfo
Packit 67cb25
Packit 67cb25
      .. math:: p(x,y) dx dy = {1 \over 2 \pi \sigma_x \sigma_y \sqrt{1-\rho^2}} \exp \left(-{(x^2/\sigma_x^2 + y^2/\sigma_y^2 - 2 \rho x y/(\sigma_x\sigma_y)) \over 2(1-\rho^2)}\right) dx dy
Packit 67cb25
Packit 67cb25
   .. only:: texinfo
Packit 67cb25
Packit 67cb25
      ::
Packit 67cb25
Packit 67cb25
         p(x,y) dx dy = {1 \over 2 \pi \sigma_x \sigma_y \sqrt{1-\rho^2}} \exp (-(x^2/\sigma_x^2 + y^2/\sigma_y^2 - 2 \rho x y/(\sigma_x\sigma_y))/2(1-\rho^2)) dx dy
Packit 67cb25
Packit 67cb25
   for :math:`x,y` in the range :math:`-\infty` to :math:`+\infty`.  The
Packit 67cb25
   correlation coefficient :data:`rho` should lie between :math:`1` and
Packit 67cb25
   :math:`-1`.
Packit 67cb25
Packit 67cb25
.. function:: double gsl_ran_bivariate_gaussian_pdf (double x, double y, double sigma_x, double sigma_y, double rho)
Packit 67cb25
Packit 67cb25
   This function computes the probability density :math:`p(x,y)` at
Packit 67cb25
   (:data:`x`, :data:`y`) for a bivariate Gaussian distribution with standard
Packit 67cb25
   deviations :data:`sigma_x`, :data:`sigma_y` and correlation coefficient
Packit 67cb25
   :data:`rho`, using the formula given above.
Packit 67cb25
Packit 67cb25
   .. image:: /images/rand-bivariate-gaussian.png
Packit 67cb25
Packit 67cb25
|newpage|
Packit 67cb25
Packit 67cb25
The Multivariate Gaussian Distribution
Packit 67cb25
======================================
Packit 67cb25
Packit 67cb25
.. index::
Packit 67cb25
   single: Bivariate Gaussian distribution
Packit 67cb25
   single: two dimensional Gaussian distribution
Packit 67cb25
   single: Gaussian distribution, bivariate
Packit 67cb25
Packit 67cb25
.. function:: int gsl_ran_multivariate_gaussian (const gsl_rng * r, const gsl_vector * mu, const gsl_matrix * L, gsl_vector * result)
Packit 67cb25
Packit 67cb25
   This function generates a random vector satisfying the :math:`k`-dimensional multivariate Gaussian
Packit 67cb25
   distribution with mean :math:`\mu` and variance-covariance matrix
Packit 67cb25
   :math:`\Sigma`. On input, the :math:`k`-vector :math:`\mu` is given in :data:`mu`, and
Packit 67cb25
   the Cholesky factor of the :math:`k`-by-:math:`k` matrix :math:`\Sigma = L L^T` is
Packit 67cb25
   given in the lower triangle of :data:`L`, as output from :func:`gsl_linalg_cholesky_decomp`.
Packit 67cb25
   The random vector is stored in :data:`result` on output. The probability distribution
Packit 67cb25
   for multivariate Gaussian random variates is
Packit 67cb25
Packit 67cb25
   .. only:: not texinfo
Packit 67cb25
Packit 67cb25
      .. math:: p(x_1,\dots,x_k) dx_1 \dots dx_k = {1 \over \sqrt{(2 \pi)^k |\Sigma|}} \exp \left(-{1 \over 2} (x - \mu)^T \Sigma^{-1} (x - \mu)\right) dx_1 \dots dx_k
Packit 67cb25
Packit 67cb25
   .. only:: texinfo
Packit 67cb25
Packit 67cb25
      ::
Packit 67cb25
Packit 67cb25
         p(x_1,...,x_k) dx_1 ... dx_k = 1 / ( \sqrt{(2 \pi)^k |\Sigma| ) \exp (-1/2 (x - \mu)^T \Sigma^{-1} (x - \mu)) dx_1 ... dx_k
Packit 67cb25
Packit 67cb25
.. function:: int gsl_ran_multivariate_gaussian_pdf (const gsl_vector * x, const gsl_vector * mu, const gsl_matrix * L, double * result, gsl_vector * work)
Packit 67cb25
              int gsl_ran_multivariate_gaussian_log_pdf (const gsl_vector * x, const gsl_vector * mu, const gsl_matrix * L, double * result, gsl_vector * work)
Packit 67cb25
Packit 67cb25
   These functions compute :math:`p(x)` or :math:`\log{p(x)}` at the point :data:`x`, using mean vector
Packit 67cb25
   :data:`mu` and variance-covariance matrix specified by its Cholesky factor :data:`L` using the formula
Packit 67cb25
   above. Additional workspace of length :math:`k` is required in :data:`work`.
Packit 67cb25
Packit 67cb25
.. function:: int gsl_ran_multivariate_gaussian_mean (const gsl_matrix * X, gsl_vector * mu_hat)
Packit 67cb25
Packit 67cb25
   Given a set of :math:`n` samples :math:`X_j` from a :math:`k`-dimensional multivariate Gaussian distribution,
Packit 67cb25
   this function computes the maximum likelihood estimate of the mean of the distribution, given by
Packit 67cb25
Packit 67cb25
   .. math:: \Hat{\mu} = {1 \over n} \sum_{j=1}^n X_j
Packit 67cb25
Packit 67cb25
   The samples :math:`X_1,X_2,\dots,X_n` are given in the :math:`n`-by-:math:`k` matrix :data:`X`, and the maximum
Packit 67cb25
   likelihood estimate of the mean is stored in :data:`mu_hat` on output.
Packit 67cb25
Packit 67cb25
.. function:: int gsl_ran_multivariate_gaussian_vcov (const gsl_matrix * X, gsl_matrix * sigma_hat)
Packit 67cb25
Packit 67cb25
   Given a set of :math:`n` samples :math:`X_j` from a :math:`k`-dimensional multivariate Gaussian distribution,
Packit 67cb25
   this function computes the maximum likelihood estimate of the variance-covariance matrix of the distribution,
Packit 67cb25
   given by
Packit 67cb25
Packit 67cb25
   .. only:: not texinfo
Packit 67cb25
Packit 67cb25
      .. math:: \Hat{\Sigma} = {1 \over n} \sum_{j=1}^n \left( X_j - \Hat{\mu} \right) \left( X_j - \Hat{\mu} \right)^T
Packit 67cb25
Packit 67cb25
   .. only:: texinfo
Packit 67cb25
Packit 67cb25
      ::
Packit 67cb25
Packit 67cb25
         \Hat{\Sigma} = (1 / n) \sum_{j=1}^n ( X_j - \Hat{\mu} ) ( X_j - \Hat{\mu} )^T
Packit 67cb25
Packit 67cb25
   The samples :math:`X_1,X_2,\dots,X_n` are given in the :math:`n`-by-:math:`k` matrix :data:`X` and the maximum
Packit 67cb25
   likelihood estimate of the variance-covariance matrix is stored in :data:`sigma_hat` on output.
Packit 67cb25
Packit 67cb25
|newpage|
Packit 67cb25
Packit 67cb25
The Exponential Distribution
Packit 67cb25
============================
Packit 67cb25
Packit 67cb25
.. index:: Exponential distribution
Packit 67cb25
Packit 67cb25
.. function:: double gsl_ran_exponential (const gsl_rng * r, double mu)
Packit 67cb25
Packit 67cb25
   This function returns a random variate from the exponential distribution
Packit 67cb25
   with mean :data:`mu`. The distribution is,
Packit 67cb25
Packit 67cb25
   .. math:: p(x) dx = {1 \over \mu} \exp(-x/\mu) dx
Packit 67cb25
Packit 67cb25
   for :math:`x \ge 0`.
Packit 67cb25
Packit 67cb25
.. function:: double gsl_ran_exponential_pdf (double x, double mu)
Packit 67cb25
Packit 67cb25
   This function computes the probability density :math:`p(x)` at :data:`x`
Packit 67cb25
   for an exponential distribution with mean :data:`mu`, using the formula
Packit 67cb25
   given above.
Packit 67cb25
Packit 67cb25
   .. image:: /images/rand-exponential.png
Packit 67cb25
Packit 67cb25
.. function:: double gsl_cdf_exponential_P (double x, double mu)
Packit 67cb25
              double gsl_cdf_exponential_Q (double x, double mu)
Packit 67cb25
              double gsl_cdf_exponential_Pinv (double P, double mu)
Packit 67cb25
              double gsl_cdf_exponential_Qinv (double Q, double mu)
Packit 67cb25
Packit 67cb25
   These functions compute the cumulative distribution functions
Packit 67cb25
   :math:`P(x)`, :math:`Q(x)` and their inverses for the exponential
Packit 67cb25
   distribution with mean :data:`mu`.
Packit 67cb25
Packit 67cb25
|newpage|
Packit 67cb25
Packit 67cb25
The Laplace Distribution
Packit 67cb25
========================
Packit 67cb25
Packit 67cb25
.. index::
Packit 67cb25
   single: two-sided exponential distribution
Packit 67cb25
   single: Laplace distribution
Packit 67cb25
Packit 67cb25
.. function:: double gsl_ran_laplace (const gsl_rng * r, double a)
Packit 67cb25
Packit 67cb25
   This function returns a random variate from the Laplace distribution
Packit 67cb25
   with width :data:`a`.  The distribution is,
Packit 67cb25
Packit 67cb25
   .. math:: p(x) dx = {1 \over 2 a}  \exp(-|x/a|) dx
Packit 67cb25
Packit 67cb25
   for :math:`-\infty < x < \infty`.
Packit 67cb25
Packit 67cb25
.. function:: double gsl_ran_laplace_pdf (double x, double a)
Packit 67cb25
Packit 67cb25
   This function computes the probability density :math:`p(x)` at :data:`x`
Packit 67cb25
   for a Laplace distribution with width :data:`a`, using the formula
Packit 67cb25
   given above.
Packit 67cb25
Packit 67cb25
   .. image:: /images/rand-laplace.png
Packit 67cb25
Packit 67cb25
.. function:: double gsl_cdf_laplace_P (double x, double a)
Packit 67cb25
              double gsl_cdf_laplace_Q (double x, double a)
Packit 67cb25
              double gsl_cdf_laplace_Pinv (double P, double a)
Packit 67cb25
              double gsl_cdf_laplace_Qinv (double Q, double a)
Packit 67cb25
Packit 67cb25
   These functions compute the cumulative distribution functions
Packit 67cb25
   :math:`P(x)`, :math:`Q(x)` and their inverses for the Laplace
Packit 67cb25
   distribution with width :data:`a`.
Packit 67cb25
Packit 67cb25
|newpage|
Packit 67cb25
Packit 67cb25
The Exponential Power Distribution
Packit 67cb25
==================================
Packit 67cb25
Packit 67cb25
.. index:: Exponential power distribution
Packit 67cb25
Packit 67cb25
.. function:: double gsl_ran_exppow (const gsl_rng * r, double a, double b)
Packit 67cb25
Packit 67cb25
   This function returns a random variate from the exponential power distribution
Packit 67cb25
   with scale parameter :data:`a` and exponent :data:`b`.  The distribution is,
Packit 67cb25
Packit 67cb25
   .. math:: p(x) dx = {1 \over 2 a \Gamma(1+1/b)} \exp(-|x/a|^b) dx
Packit 67cb25
Packit 67cb25
   for :math:`x \ge 0`.
Packit 67cb25
   For :math:`b = 1` this reduces to the Laplace
Packit 67cb25
   distribution.  For :math:`b = 2` it has the same form as a Gaussian
Packit 67cb25
   distribution, but with :math:`a = \sqrt{2} \sigma`.
Packit 67cb25
Packit 67cb25
.. function:: double gsl_ran_exppow_pdf (double x, double a, double b)
Packit 67cb25
Packit 67cb25
   This function computes the probability density :math:`p(x)` at :data:`x`
Packit 67cb25
   for an exponential power distribution with scale parameter :data:`a`
Packit 67cb25
   and exponent :data:`b`, using the formula given above.
Packit 67cb25
Packit 67cb25
   .. image:: /images/rand-exppow.png
Packit 67cb25
Packit 67cb25
.. function:: double gsl_cdf_exppow_P (double x, double a, double b)
Packit 67cb25
              double gsl_cdf_exppow_Q (double x, double a, double b)
Packit 67cb25
Packit 67cb25
   These functions compute the cumulative distribution functions
Packit 67cb25
   :math:`P(x)`, :math:`Q(x)` for the exponential power distribution with
Packit 67cb25
   parameters :data:`a` and :data:`b`.
Packit 67cb25
Packit 67cb25
|newpage|
Packit 67cb25
Packit 67cb25
The Cauchy Distribution
Packit 67cb25
=======================
Packit 67cb25
Packit 67cb25
.. index:: Cauchy distribution
Packit 67cb25
Packit 67cb25
.. function:: double gsl_ran_cauchy (const gsl_rng * r, double a)
Packit 67cb25
Packit 67cb25
   This function returns a random variate from the Cauchy distribution with
Packit 67cb25
   scale parameter :data:`a`.  The probability distribution for Cauchy
Packit 67cb25
   random variates is,
Packit 67cb25
Packit 67cb25
   .. math:: p(x) dx = {1 \over a\pi (1 + (x/a)^2) } dx
Packit 67cb25
Packit 67cb25
   for :math:`x` in the range :math:`-\infty` to :math:`+\infty`.  The Cauchy
Packit 67cb25
   distribution is also known as the Lorentz distribution.
Packit 67cb25
Packit 67cb25
.. function:: double gsl_ran_cauchy_pdf (double x, double a)
Packit 67cb25
Packit 67cb25
   This function computes the probability density :math:`p(x)` at :data:`x`
Packit 67cb25
   for a Cauchy distribution with scale parameter :data:`a`, using the formula
Packit 67cb25
   given above.
Packit 67cb25
Packit 67cb25
   .. image:: /images/rand-cauchy.png
Packit 67cb25
Packit 67cb25
.. function:: double gsl_cdf_cauchy_P (double x, double a)
Packit 67cb25
              double gsl_cdf_cauchy_Q (double x, double a)
Packit 67cb25
              double gsl_cdf_cauchy_Pinv (double P, double a)
Packit 67cb25
              double gsl_cdf_cauchy_Qinv (double Q, double a)
Packit 67cb25
Packit 67cb25
   These functions compute the cumulative distribution functions
Packit 67cb25
   :math:`P(x)`, :math:`Q(x)` and their inverses for the Cauchy
Packit 67cb25
   distribution with scale parameter :data:`a`.
Packit 67cb25
Packit 67cb25
|newpage|
Packit 67cb25
Packit 67cb25
The Rayleigh Distribution
Packit 67cb25
=========================
Packit 67cb25
Packit 67cb25
.. index:: Rayleigh distribution
Packit 67cb25
Packit 67cb25
.. function:: double gsl_ran_rayleigh (const gsl_rng * r, double sigma)
Packit 67cb25
Packit 67cb25
   This function returns a random variate from the Rayleigh distribution with
Packit 67cb25
   scale parameter :data:`sigma`.  The distribution is,
Packit 67cb25
Packit 67cb25
   .. math:: p(x) dx = {x \over \sigma^2} \exp(- x^2/(2 \sigma^2)) dx
Packit 67cb25
Packit 67cb25
   for :math:`x > 0`.
Packit 67cb25
Packit 67cb25
.. function:: double gsl_ran_rayleigh_pdf (double x, double sigma)
Packit 67cb25
Packit 67cb25
   This function computes the probability density :math:`p(x)` at :data:`x`
Packit 67cb25
   for a Rayleigh distribution with scale parameter :data:`sigma`, using the
Packit 67cb25
   formula given above.
Packit 67cb25
Packit 67cb25
   .. image:: /images/rand-rayleigh.png
Packit 67cb25
Packit 67cb25
.. function:: double gsl_cdf_rayleigh_P (double x, double sigma)
Packit 67cb25
              double gsl_cdf_rayleigh_Q (double x, double sigma)
Packit 67cb25
              double gsl_cdf_rayleigh_Pinv (double P, double sigma)
Packit 67cb25
              double gsl_cdf_rayleigh_Qinv (double Q, double sigma)
Packit 67cb25
Packit 67cb25
   These functions compute the cumulative distribution functions
Packit 67cb25
   :math:`P(x)`, :math:`Q(x)` and their inverses for the Rayleigh
Packit 67cb25
   distribution with scale parameter :data:`sigma`.
Packit 67cb25
Packit 67cb25
|newpage|
Packit 67cb25
Packit 67cb25
The Rayleigh Tail Distribution
Packit 67cb25
==============================
Packit 67cb25
Packit 67cb25
.. index:: Rayleigh Tail distribution
Packit 67cb25
Packit 67cb25
.. function:: double gsl_ran_rayleigh_tail (const gsl_rng * r, double a, double sigma)
Packit 67cb25
Packit 67cb25
   This function returns a random variate from the tail of the Rayleigh
Packit 67cb25
   distribution with scale parameter :data:`sigma` and a lower limit of
Packit 67cb25
   :data:`a`.  The distribution is,
Packit 67cb25
Packit 67cb25
   .. math:: p(x) dx = {x \over \sigma^2} \exp ((a^2 - x^2) /(2 \sigma^2)) dx
Packit 67cb25
Packit 67cb25
   for :math:`x > a`.
Packit 67cb25
Packit 67cb25
.. function:: double gsl_ran_rayleigh_tail_pdf (double x, double a, double sigma)
Packit 67cb25
Packit 67cb25
   This function computes the probability density :math:`p(x)` at :data:`x`
Packit 67cb25
   for a Rayleigh tail distribution with scale parameter :data:`sigma` and
Packit 67cb25
   lower limit :data:`a`, using the formula given above.
Packit 67cb25
Packit 67cb25
   .. image:: /images/rand-rayleigh-tail.png
Packit 67cb25
Packit 67cb25
|newpage|
Packit 67cb25
Packit 67cb25
The Landau Distribution
Packit 67cb25
=======================
Packit 67cb25
Packit 67cb25
.. index:: Landau distribution
Packit 67cb25
Packit 67cb25
.. function:: double gsl_ran_landau (const gsl_rng * r)
Packit 67cb25
Packit 67cb25
   This function returns a random variate from the Landau distribution.  The
Packit 67cb25
   probability distribution for Landau random variates is defined
Packit 67cb25
   analytically by the complex integral,
Packit 67cb25
Packit 67cb25
   .. only:: not texinfo
Packit 67cb25
Packit 67cb25
      .. math:: p(x) = {1 \over {2 \pi i}} \int_{c-i\infty}^{c+i\infty} ds\, \exp(s \log(s) + x s) 
Packit 67cb25
Packit 67cb25
   .. only:: texinfo
Packit 67cb25
Packit 67cb25
      ::
Packit 67cb25
Packit 67cb25
         p(x) = (1/(2 \pi i)) \int_{c-i\infty}^{c+i\infty} ds exp(s log(s) + x s) 
Packit 67cb25
Packit 67cb25
   For numerical purposes it is more convenient to use the following
Packit 67cb25
   equivalent form of the integral,
Packit 67cb25
Packit 67cb25
   .. math:: p(x) = (1/\pi) \int_0^\infty dt \exp(-t \log(t) - x t) \sin(\pi t).
Packit 67cb25
Packit 67cb25
.. function:: double gsl_ran_landau_pdf (double x)
Packit 67cb25
Packit 67cb25
   This function computes the probability density :math:`p(x)` at :data:`x`
Packit 67cb25
   for the Landau distribution using an approximation to the formula given
Packit 67cb25
   above.
Packit 67cb25
Packit 67cb25
   .. image:: /images/rand-landau.png
Packit 67cb25
Packit 67cb25
|newpage|
Packit 67cb25
Packit 67cb25
The Levy alpha-Stable Distributions
Packit 67cb25
===================================
Packit 67cb25
Packit 67cb25
.. index:: Levy distribution
Packit 67cb25
Packit 67cb25
.. function:: double gsl_ran_levy (const gsl_rng * r, double c, double alpha)
Packit 67cb25
Packit 67cb25
   This function returns a random variate from the Levy symmetric stable
Packit 67cb25
   distribution with scale :data:`c` and exponent :data:`alpha`.  The symmetric
Packit 67cb25
   stable probability distribution is defined by a Fourier transform,
Packit 67cb25
Packit 67cb25
   .. only:: not texinfo
Packit 67cb25
Packit 67cb25
      .. math:: p(x) = {1 \over 2 \pi} \int_{-\infty}^{+\infty} dt \exp(-it x - |c t|^\alpha)
Packit 67cb25
Packit 67cb25
   .. only:: texinfo
Packit 67cb25
Packit 67cb25
      ::
Packit 67cb25
Packit 67cb25
         p(x) = 1 / (2 \pi) \int_{-\infty}^{+\infty} dt \exp(-it x - |c t|^alpha)
Packit 67cb25
Packit 67cb25
   There is no explicit solution for the form of :math:`p(x)` and the
Packit 67cb25
   library does not define a corresponding :code:`pdf` function.  For
Packit 67cb25
   :math:`\alpha = 1` the distribution reduces to the Cauchy distribution.  For
Packit 67cb25
   :math:`\alpha = 2` it is a Gaussian distribution with :math:`\sigma = \sqrt{2} c`.
Packit 67cb25
   For :math:`\alpha < 1` the tails of the distribution become extremely wide.
Packit 67cb25
Packit 67cb25
   The algorithm only works for :math:`0 < \alpha \le 2`.
Packit 67cb25
Packit 67cb25
   .. image:: /images/rand-levy.png
Packit 67cb25
Packit 67cb25
|newpage|
Packit 67cb25
Packit 67cb25
The Levy skew alpha-Stable Distribution
Packit 67cb25
=======================================
Packit 67cb25
Packit 67cb25
.. index::
Packit 67cb25
   single: Levy distribution, skew
Packit 67cb25
   single: Skew Levy distribution
Packit 67cb25
Packit 67cb25
.. function:: double gsl_ran_levy_skew (const gsl_rng * r, double c, double alpha, double beta)
Packit 67cb25
Packit 67cb25
   This function returns a random variate from the Levy skew stable
Packit 67cb25
   distribution with scale :data:`c`, exponent :data:`alpha` and skewness
Packit 67cb25
   parameter :data:`beta`.  The skewness parameter must lie in the range
Packit 67cb25
   :math:`[-1,1]`.  The Levy skew stable probability distribution is defined
Packit 67cb25
   by a Fourier transform,
Packit 67cb25
Packit 67cb25
   .. only:: not texinfo
Packit 67cb25
Packit 67cb25
      .. math:: p(x) = {1 \over 2 \pi} \int_{-\infty}^{+\infty} dt \exp(-it x - |c t|^\alpha (1-i \beta \sgn(t) \tan(\pi\alpha/2)))
Packit 67cb25
Packit 67cb25
   .. only:: texinfo
Packit 67cb25
Packit 67cb25
      ::
Packit 67cb25
Packit 67cb25
         p(x) = 1 / (2 \pi) \int_{-\infty}^{+\infty} dt \exp(-it x - |c t|^alpha (1-i beta sign(t) tan(pi alpha/2)))
Packit 67cb25
Packit 67cb25
   When :math:`\alpha = 1` the term :math:`\tan(\pi \alpha/2)` is replaced by
Packit 67cb25
   :math:`-(2/\pi)\log|t|`.  There is no explicit solution for the form of
Packit 67cb25
   :math:`p(x)` and the library does not define a corresponding :code:`pdf`
Packit 67cb25
   function.  For :math:`\alpha = 2` the distribution reduces to a Gaussian
Packit 67cb25
   distribution with :math:`\sigma = \sqrt{2} c`
Packit 67cb25
   and the skewness parameter has no effect.  
Packit 67cb25
   For :math:`\alpha < 1` the tails of the distribution become extremely
Packit 67cb25
   wide.  The symmetric distribution corresponds to :math:`\beta = 0`.
Packit 67cb25
Packit 67cb25
   The algorithm only works for :math:`0 < \alpha \le 2`.
Packit 67cb25
Packit 67cb25
The Levy alpha-stable distributions have the property that if :math:`N`
Packit 67cb25
alpha-stable variates are drawn from the distribution :math:`p(c, \alpha, \beta)`
Packit 67cb25
then the sum :math:`Y = X_1 + X_2 + \dots + X_N` will also be
Packit 67cb25
distributed as an alpha-stable variate,
Packit 67cb25
:math:`p(N^{1/\alpha} c, \alpha, \beta)`.
Packit 67cb25
Packit 67cb25
.. PDF not available because there is no analytic expression for it
Packit 67cb25
..
Packit 67cb25
.. @deftypefun double gsl_ran_levy_pdf (double x, double mu)
Packit 67cb25
.. This function computes the probability density :math:`p(x)` at :data:`x`
Packit 67cb25
.. for a symmetric Levy distribution with scale parameter :data:`mu` and
Packit 67cb25
.. exponent :data:`a`, using the formula given above.
Packit 67cb25
.. @end deftypefun
Packit 67cb25
Packit 67cb25
.. image:: /images/rand-levyskew.png
Packit 67cb25
Packit 67cb25
|newpage|
Packit 67cb25
Packit 67cb25
The Gamma Distribution
Packit 67cb25
======================
Packit 67cb25
Packit 67cb25
.. index::
Packit 67cb25
   single: Gamma distribution
Packit 67cb25
   single: Erlang distribution
Packit 67cb25
Packit 67cb25
.. function:: double gsl_ran_gamma (const gsl_rng * r, double a, double b)
Packit 67cb25
Packit 67cb25
   This function returns a random variate from the gamma
Packit 67cb25
   distribution.  The distribution function is,
Packit 67cb25
Packit 67cb25
   .. math:: p(x) dx = {1 \over \Gamma(a) b^a} x^{a-1} e^{-x/b} dx
Packit 67cb25
Packit 67cb25
   for :math:`x > 0`.
Packit 67cb25
Packit 67cb25
   The gamma distribution with an integer parameter :data:`a` is known as the Erlang distribution.
Packit 67cb25
Packit 67cb25
   The variates are computed using the Marsaglia-Tsang fast gamma method.
Packit 67cb25
   This function for this method was previously called
Packit 67cb25
   :func:`gsl_ran_gamma_mt` and can still be accessed using this name.
Packit 67cb25
Packit 67cb25
.. If @xmath{X} and @xmath{Y} are independent gamma-distributed random
Packit 67cb25
.. variables of order @xmath{a} and @xmath{b}, then @xmath{X+Y} has a gamma
Packit 67cb25
.. distribution of order @xmath{a+b}.
Packit 67cb25
Packit 67cb25
.. function:: double gsl_ran_gamma_knuth (const gsl_rng * r, double a, double b)
Packit 67cb25
Packit 67cb25
   This function returns a gamma variate using the algorithms from Knuth (vol 2).
Packit 67cb25
Packit 67cb25
.. function:: double gsl_ran_gamma_pdf (double x, double a, double b)
Packit 67cb25
Packit 67cb25
   This function computes the probability density :math:`p(x)` at :data:`x`
Packit 67cb25
   for a gamma distribution with parameters :data:`a` and :data:`b`, using the
Packit 67cb25
   formula given above.
Packit 67cb25
Packit 67cb25
   .. image:: /images/rand-gamma.png
Packit 67cb25
Packit 67cb25
.. function:: double gsl_cdf_gamma_P (double x, double a, double b)
Packit 67cb25
              double gsl_cdf_gamma_Q (double x, double a, double b)
Packit 67cb25
              double gsl_cdf_gamma_Pinv (double P, double a, double b)
Packit 67cb25
              double gsl_cdf_gamma_Qinv (double Q, double a, double b)
Packit 67cb25
Packit 67cb25
   These functions compute the cumulative distribution functions
Packit 67cb25
   :math:`P(x)`, :math:`Q(x)` and their inverses for the gamma
Packit 67cb25
   distribution with parameters :data:`a` and :data:`b`.
Packit 67cb25
Packit 67cb25
|newpage|
Packit 67cb25
Packit 67cb25
The Flat (Uniform) Distribution
Packit 67cb25
===============================
Packit 67cb25
Packit 67cb25
.. index::
Packit 67cb25
   single: flat distribution
Packit 67cb25
   single: uniform distribution
Packit 67cb25
Packit 67cb25
.. function:: double gsl_ran_flat (const gsl_rng * r, double a, double b)
Packit 67cb25
Packit 67cb25
   This function returns a random variate from the flat (uniform)
Packit 67cb25
   distribution from :data:`a` to :data:`b`. The distribution is,
Packit 67cb25
Packit 67cb25
   .. math:: p(x) dx = {1 \over (b-a)} dx
Packit 67cb25
Packit 67cb25
   if :math:`a \le x < b` and 0 otherwise.
Packit 67cb25
Packit 67cb25
.. function:: double gsl_ran_flat_pdf (double x, double a, double b)
Packit 67cb25
Packit 67cb25
   This function computes the probability density :math:`p(x)` at :data:`x`
Packit 67cb25
   for a uniform distribution from :data:`a` to :data:`b`, using the formula
Packit 67cb25
   given above.
Packit 67cb25
Packit 67cb25
   .. image:: /images/rand-flat.png
Packit 67cb25
Packit 67cb25
.. function:: double gsl_cdf_flat_P (double x, double a, double b)
Packit 67cb25
              double gsl_cdf_flat_Q (double x, double a, double b)
Packit 67cb25
              double gsl_cdf_flat_Pinv (double P, double a, double b)
Packit 67cb25
              double gsl_cdf_flat_Qinv (double Q, double a, double b)
Packit 67cb25
Packit 67cb25
   These functions compute the cumulative distribution functions
Packit 67cb25
   :math:`P(x)`, :math:`Q(x)` and their inverses for a uniform distribution
Packit 67cb25
   from :data:`a` to :data:`b`.
Packit 67cb25
Packit 67cb25
|newpage|
Packit 67cb25
Packit 67cb25
The Lognormal Distribution
Packit 67cb25
==========================
Packit 67cb25
Packit 67cb25
.. index:: Lognormal distribution
Packit 67cb25
Packit 67cb25
.. function:: double gsl_ran_lognormal (const gsl_rng * r, double zeta, double sigma)
Packit 67cb25
Packit 67cb25
   This function returns a random variate from the lognormal
Packit 67cb25
   distribution.  The distribution function is,
Packit 67cb25
Packit 67cb25
   .. math:: p(x) dx = {1 \over x \sqrt{2 \pi \sigma^2}} \exp(-(\ln(x) - \zeta)^2/2 \sigma^2) dx
Packit 67cb25
Packit 67cb25
   for :math:`x > 0`.
Packit 67cb25
Packit 67cb25
.. function:: double gsl_ran_lognormal_pdf (double x, double zeta, double sigma)
Packit 67cb25
Packit 67cb25
   This function computes the probability density :math:`p(x)` at :data:`x`
Packit 67cb25
   for a lognormal distribution with parameters :data:`zeta` and :data:`sigma`,
Packit 67cb25
   using the formula given above.
Packit 67cb25
Packit 67cb25
   .. image:: /images/rand-lognormal.png
Packit 67cb25
Packit 67cb25
.. function:: double gsl_cdf_lognormal_P (double x, double zeta, double sigma)
Packit 67cb25
              double gsl_cdf_lognormal_Q (double x, double zeta, double sigma)
Packit 67cb25
              double gsl_cdf_lognormal_Pinv (double P, double zeta, double sigma)
Packit 67cb25
              double gsl_cdf_lognormal_Qinv (double Q, double zeta, double sigma)
Packit 67cb25
Packit 67cb25
   These functions compute the cumulative distribution functions
Packit 67cb25
   :math:`P(x)`, :math:`Q(x)` and their inverses for the lognormal
Packit 67cb25
   distribution with parameters :data:`zeta` and :data:`sigma`.
Packit 67cb25
Packit 67cb25
|newpage|
Packit 67cb25
Packit 67cb25
The Chi-squared Distribution
Packit 67cb25
============================
Packit 67cb25
Packit 67cb25
The chi-squared distribution arises in statistics.  If :math:`Y_i` are
Packit 67cb25
:math:`n` independent Gaussian random variates with unit variance then the
Packit 67cb25
sum-of-squares,
Packit 67cb25
Packit 67cb25
.. math:: X_i = \sum_i Y_i^2
Packit 67cb25
Packit 67cb25
has a chi-squared distribution with :math:`n` degrees of freedom.
Packit 67cb25
Packit 67cb25
.. index:: Chi-squared distribution
Packit 67cb25
Packit 67cb25
.. function:: double gsl_ran_chisq (const gsl_rng * r, double nu)
Packit 67cb25
Packit 67cb25
   This function returns a random variate from the chi-squared distribution
Packit 67cb25
   with :data:`nu` degrees of freedom. The distribution function is,
Packit 67cb25
Packit 67cb25
   .. math:: p(x) dx = {1 \over 2 \Gamma(\nu/2) } (x/2)^{\nu/2 - 1} \exp(-x/2) dx
Packit 67cb25
Packit 67cb25
   for :math:`x \ge 0`.
Packit 67cb25
Packit 67cb25
.. function:: double gsl_ran_chisq_pdf (double x, double nu)
Packit 67cb25
Packit 67cb25
   This function computes the probability density :math:`p(x)` at :data:`x`
Packit 67cb25
   for a chi-squared distribution with :data:`nu` degrees of freedom, using
Packit 67cb25
   the formula given above.
Packit 67cb25
Packit 67cb25
   .. image:: /images/rand-chisq.png
Packit 67cb25
Packit 67cb25
.. function:: double gsl_cdf_chisq_P (double x, double nu)
Packit 67cb25
              double gsl_cdf_chisq_Q (double x, double nu)
Packit 67cb25
              double gsl_cdf_chisq_Pinv (double P, double nu)
Packit 67cb25
              double gsl_cdf_chisq_Qinv (double Q, double nu)
Packit 67cb25
Packit 67cb25
   These functions compute the cumulative distribution functions
Packit 67cb25
   :math:`P(x)`, :math:`Q(x)` and their inverses for the chi-squared
Packit 67cb25
   distribution with :data:`nu` degrees of freedom.
Packit 67cb25
Packit 67cb25
|newpage|
Packit 67cb25
Packit 67cb25
The F-distribution
Packit 67cb25
==================
Packit 67cb25
Packit 67cb25
The F-distribution arises in statistics.  If :math:`Y_1` and :math:`Y_2`
Packit 67cb25
are chi-squared deviates with :math:`\nu_1` and :math:`\nu_2` degrees of
Packit 67cb25
freedom then the ratio,
Packit 67cb25
Packit 67cb25
.. math:: X = { (Y_1 / \nu_1) \over (Y_2 / \nu_2) }
Packit 67cb25
Packit 67cb25
has an F-distribution :math:`F(x;\nu_1,\nu_2)`.
Packit 67cb25
Packit 67cb25
.. index:: F-distribution
Packit 67cb25
Packit 67cb25
.. function:: double gsl_ran_fdist (const gsl_rng * r, double nu1, double nu2)
Packit 67cb25
Packit 67cb25
   This function returns a random variate from the F-distribution with degrees of freedom :data:`nu1` and :data:`nu2`.
Packit 67cb25
   The distribution function is,
Packit 67cb25
Packit 67cb25
   .. only:: not texinfo
Packit 67cb25
Packit 67cb25
      .. math::
Packit 67cb25
Packit 67cb25
         p(x) dx = 
Packit 67cb25
            { \Gamma((\nu_1 + \nu_2)/2)
Packit 67cb25
                 \over \Gamma(\nu_1/2) \Gamma(\nu_2/2) } 
Packit 67cb25
            \nu_1^{\nu_1/2} \nu_2^{\nu_2/2} 
Packit 67cb25
            x^{\nu_1/2 - 1} (\nu_2 + \nu_1 x)^{-\nu_1/2 -\nu_2/2}
Packit 67cb25
Packit 67cb25
   .. only:: texinfo
Packit 67cb25
Packit 67cb25
      ::
Packit 67cb25
Packit 67cb25
         p(x) dx = 
Packit 67cb25
            { \Gamma((\nu_1 + \nu_2)/2)
Packit 67cb25
                 \over \Gamma(\nu_1/2) \Gamma(\nu_2/2) } 
Packit 67cb25
            \nu_1^{\nu_1/2} \nu_2^{\nu_2/2} 
Packit 67cb25
            x^{\nu_1/2 - 1} (\nu_2 + \nu_1 x)^{-\nu_1/2 -\nu_2/2}
Packit 67cb25
Packit 67cb25
   for :math:`x \ge 0`.
Packit 67cb25
Packit 67cb25
.. function:: double gsl_ran_fdist_pdf (double x, double nu1, double nu2)
Packit 67cb25
Packit 67cb25
   This function computes the probability density :math:`p(x)` at :data:`x`
Packit 67cb25
   for an F-distribution with :data:`nu1` and :data:`nu2` degrees of freedom,
Packit 67cb25
   using the formula given above.
Packit 67cb25
Packit 67cb25
   .. image:: /images/rand-fdist.png
Packit 67cb25
Packit 67cb25
.. function:: double gsl_cdf_fdist_P (double x, double nu1, double nu2)
Packit 67cb25
              double gsl_cdf_fdist_Q (double x, double nu1, double nu2)
Packit 67cb25
              double gsl_cdf_fdist_Pinv (double P, double nu1, double nu2)
Packit 67cb25
              double gsl_cdf_fdist_Qinv (double Q, double nu1, double nu2)
Packit 67cb25
Packit 67cb25
   These functions compute the cumulative distribution functions
Packit 67cb25
   :math:`P(x)`, :math:`Q(x)` and their inverses for the F-distribution
Packit 67cb25
   with :data:`nu1` and :data:`nu2` degrees of freedom.
Packit 67cb25
Packit 67cb25
|newpage|
Packit 67cb25
Packit 67cb25
The t-distribution
Packit 67cb25
==================
Packit 67cb25
Packit 67cb25
The t-distribution arises in statistics.  If :math:`Y_1` has a normal
Packit 67cb25
distribution and :math:`Y_2` has a chi-squared distribution with
Packit 67cb25
:math:`\nu` degrees of freedom then the ratio,
Packit 67cb25
Packit 67cb25
.. math:: X = { Y_1 \over \sqrt{Y_2 / \nu} }
Packit 67cb25
Packit 67cb25
has a t-distribution :math:`t(x;\nu)` with :math:`\nu` degrees of freedom.
Packit 67cb25
Packit 67cb25
.. index::
Packit 67cb25
   single: t-distribution
Packit 67cb25
   single: Student t-distribution
Packit 67cb25
Packit 67cb25
.. function:: double gsl_ran_tdist (const gsl_rng * r, double nu)
Packit 67cb25
Packit 67cb25
   This function returns a random variate from the t-distribution.  The
Packit 67cb25
   distribution function is,
Packit 67cb25
Packit 67cb25
   .. math::
Packit 67cb25
Packit 67cb25
      p(x) dx = {\Gamma((\nu + 1)/2) \over \sqrt{\pi \nu} \Gamma(\nu/2)}
Packit 67cb25
         (1 + x^2/\nu)^{-(\nu + 1)/2} dx
Packit 67cb25
Packit 67cb25
   for :math:`-\infty < x < +\infty`.
Packit 67cb25
Packit 67cb25
.. function:: double gsl_ran_tdist_pdf (double x, double nu)
Packit 67cb25
Packit 67cb25
   This function computes the probability density :math:`p(x)` at :data:`x`
Packit 67cb25
   for a t-distribution with :data:`nu` degrees of freedom, using the formula
Packit 67cb25
   given above.
Packit 67cb25
Packit 67cb25
   .. image:: /images/rand-tdist.png
Packit 67cb25
Packit 67cb25
.. function:: double gsl_cdf_tdist_P (double x, double nu)
Packit 67cb25
              double gsl_cdf_tdist_Q (double x, double nu)
Packit 67cb25
              double gsl_cdf_tdist_Pinv (double P, double nu)
Packit 67cb25
              double gsl_cdf_tdist_Qinv (double Q, double nu)
Packit 67cb25
Packit 67cb25
   These functions compute the cumulative distribution functions
Packit 67cb25
   :math:`P(x)`, :math:`Q(x)` and their inverses for the t-distribution
Packit 67cb25
   with :data:`nu` degrees of freedom.
Packit 67cb25
Packit 67cb25
|newpage|
Packit 67cb25
Packit 67cb25
The Beta Distribution
Packit 67cb25
=====================
Packit 67cb25
Packit 67cb25
.. index:: Beta distribution
Packit 67cb25
Packit 67cb25
.. function:: double gsl_ran_beta (const gsl_rng * r, double a, double b)
Packit 67cb25
Packit 67cb25
   This function returns a random variate from the beta
Packit 67cb25
   distribution.  The distribution function is,
Packit 67cb25
Packit 67cb25
   .. math:: p(x) dx = {\Gamma(a+b) \over \Gamma(a) \Gamma(b)} x^{a-1} (1-x)^{b-1} dx
Packit 67cb25
Packit 67cb25
   for :math:`0 \le x \le 1`.
Packit 67cb25
Packit 67cb25
.. function:: double gsl_ran_beta_pdf (double x, double a, double b)
Packit 67cb25
Packit 67cb25
   This function computes the probability density :math:`p(x)` at :data:`x`
Packit 67cb25
   for a beta distribution with parameters :data:`a` and :data:`b`, using the
Packit 67cb25
   formula given above.
Packit 67cb25
Packit 67cb25
   .. image:: /images/rand-beta.png
Packit 67cb25
Packit 67cb25
.. function:: double gsl_cdf_beta_P (double x, double a, double b)
Packit 67cb25
              double gsl_cdf_beta_Q (double x, double a, double b)
Packit 67cb25
              double gsl_cdf_beta_Pinv (double P, double a, double b)
Packit 67cb25
              double gsl_cdf_beta_Qinv (double Q, double a, double b)
Packit 67cb25
Packit 67cb25
   These functions compute the cumulative distribution functions
Packit 67cb25
   :math:`P(x)`, :math:`Q(x)` and their inverses for the beta
Packit 67cb25
   distribution with parameters :data:`a` and :data:`b`.
Packit 67cb25
Packit 67cb25
|newpage|
Packit 67cb25
Packit 67cb25
The Logistic Distribution
Packit 67cb25
=========================
Packit 67cb25
Packit 67cb25
.. index:: Logistic distribution
Packit 67cb25
Packit 67cb25
.. function:: double gsl_ran_logistic (const gsl_rng * r, double a)
Packit 67cb25
Packit 67cb25
   This function returns a random variate from the logistic
Packit 67cb25
   distribution.  The distribution function is,
Packit 67cb25
Packit 67cb25
   .. math:: p(x) dx = { \exp(-x/a) \over a (1 + \exp(-x/a))^2 } dx
Packit 67cb25
Packit 67cb25
   for :math:`-\infty < x < +\infty`.
Packit 67cb25
Packit 67cb25
.. function:: double gsl_ran_logistic_pdf (double x, double a)
Packit 67cb25
Packit 67cb25
   This function computes the probability density :math:`p(x)` at :data:`x`
Packit 67cb25
   for a logistic distribution with scale parameter :data:`a`, using the
Packit 67cb25
   formula given above.
Packit 67cb25
Packit 67cb25
   .. image:: /images/rand-logistic.png
Packit 67cb25
Packit 67cb25
.. function:: double gsl_cdf_logistic_P (double x, double a)
Packit 67cb25
              double gsl_cdf_logistic_Q (double x, double a)
Packit 67cb25
              double gsl_cdf_logistic_Pinv (double P, double a)
Packit 67cb25
              double gsl_cdf_logistic_Qinv (double Q, double a)
Packit 67cb25
Packit 67cb25
   These functions compute the cumulative distribution functions
Packit 67cb25
   :math:`P(x)`, :math:`Q(x)` and their inverses for the logistic
Packit 67cb25
   distribution with scale parameter :data:`a`.
Packit 67cb25
Packit 67cb25
|newpage|
Packit 67cb25
Packit 67cb25
The Pareto Distribution
Packit 67cb25
=======================
Packit 67cb25
Packit 67cb25
.. index:: Pareto distribution
Packit 67cb25
Packit 67cb25
.. function:: double gsl_ran_pareto (const gsl_rng * r, double a, double b)
Packit 67cb25
Packit 67cb25
   This function returns a random variate from the Pareto distribution of
Packit 67cb25
   order :data:`a`.  The distribution function is,
Packit 67cb25
Packit 67cb25
   .. math:: p(x) dx = (a/b) / (x/b)^{a+1} dx
Packit 67cb25
Packit 67cb25
   for :math:`x \ge b`.
Packit 67cb25
Packit 67cb25
.. function:: double gsl_ran_pareto_pdf (double x, double a, double b)
Packit 67cb25
Packit 67cb25
   This function computes the probability density :math:`p(x)` at :data:`x`
Packit 67cb25
   for a Pareto distribution with exponent :data:`a` and scale :data:`b`, using
Packit 67cb25
   the formula given above.
Packit 67cb25
Packit 67cb25
   .. image:: /images/rand-pareto.png
Packit 67cb25
Packit 67cb25
.. function:: double gsl_cdf_pareto_P (double x, double a, double b)
Packit 67cb25
              double gsl_cdf_pareto_Q (double x, double a, double b)
Packit 67cb25
              double gsl_cdf_pareto_Pinv (double P, double a, double b)
Packit 67cb25
              double gsl_cdf_pareto_Qinv (double Q, double a, double b)
Packit 67cb25
Packit 67cb25
   These functions compute the cumulative distribution functions
Packit 67cb25
   :math:`P(x)`, :math:`Q(x)` and their inverses for the Pareto
Packit 67cb25
   distribution with exponent :data:`a` and scale :data:`b`.
Packit 67cb25
Packit 67cb25
|newpage|
Packit 67cb25
Packit 67cb25
Spherical Vector Distributions
Packit 67cb25
==============================
Packit 67cb25
Packit 67cb25
The spherical distributions generate random vectors, located on a
Packit 67cb25
spherical surface.  They can be used as random directions, for example in
Packit 67cb25
the steps of a random walk.
Packit 67cb25
Packit 67cb25
.. index::
Packit 67cb25
   single: 2D random direction vector
Packit 67cb25
   single: direction vector, random 2D
Packit 67cb25
   single: spherical random variates, 2D
Packit 67cb25
Packit 67cb25
.. function:: void gsl_ran_dir_2d (const gsl_rng * r, double * x, double * y)
Packit 67cb25
              void gsl_ran_dir_2d_trig_method (const gsl_rng * r, double * x, double * y)
Packit 67cb25
Packit 67cb25
   This function returns a random direction vector :math:`v` =
Packit 67cb25
   (:data:`x`, :data:`y`) in two dimensions.  The vector is normalized such that
Packit 67cb25
   :math:`|v|^2 = x^2 + y^2 = 1`.  The obvious way to do this is to take a
Packit 67cb25
   uniform random number between 0 and :math:`2\pi` and let :data:`x` and
Packit 67cb25
   :data:`y` be the sine and cosine respectively.  Two trig functions would
Packit 67cb25
   have been expensive in the old days, but with modern hardware
Packit 67cb25
   implementations, this is sometimes the fastest way to go.  This is the
Packit 67cb25
   case for the Pentium (but not the case for the Sun Sparcstation).
Packit 67cb25
   One can avoid the trig evaluations by choosing :data:`x` and
Packit 67cb25
   :data:`y` in the interior of a unit circle (choose them at random from the
Packit 67cb25
   interior of the enclosing square, and then reject those that are outside
Packit 67cb25
   the unit circle), and then dividing by :math:`\sqrt{x^2 + y^2}`.
Packit 67cb25
   A much cleverer approach, attributed to von Neumann (See Knuth, v2, 3rd
Packit 67cb25
   ed, p140, exercise 23), requires neither trig nor a square root.  In
Packit 67cb25
   this approach, :data:`u` and :data:`v` are chosen at random from the
Packit 67cb25
   interior of a unit circle, and then :math:`x=(u^2-v^2)/(u^2+v^2)` and
Packit 67cb25
   :math:`y=2uv/(u^2+v^2)`.
Packit 67cb25
Packit 67cb25
.. index::
Packit 67cb25
   single: 3D random direction vector
Packit 67cb25
   single: direction vector, random 3D
Packit 67cb25
   single: spherical random variates, 3D
Packit 67cb25
Packit 67cb25
.. function:: void gsl_ran_dir_3d (const gsl_rng * r, double * x, double * y, double * z)
Packit 67cb25
Packit 67cb25
   This function returns a random direction vector :math:`v` =
Packit 67cb25
   (:data:`x`, :data:`y`, :data:`z`) in three dimensions.  The vector is normalized
Packit 67cb25
   such that :math:`|v|^2 = x^2 + y^2 + z^2 = 1`.  The method employed is
Packit 67cb25
   due to Robert E. Knop (CACM 13, 326 (1970)), and explained in Knuth, v2,
Packit 67cb25
   3rd ed, p136.  It uses the surprising fact that the distribution
Packit 67cb25
   projected along any axis is actually uniform (this is only true for 3
Packit 67cb25
   dimensions).
Packit 67cb25
Packit 67cb25
.. index::
Packit 67cb25
   single: N-dimensional random direction vector
Packit 67cb25
   single: direction vector, random N-dimensional
Packit 67cb25
   single: spherical random variates, N-dimensional
Packit 67cb25
Packit 67cb25
.. function:: void gsl_ran_dir_nd (const gsl_rng * r, size_t n, double * x)
Packit 67cb25
Packit 67cb25
   This function returns a random direction vector
Packit 67cb25
   :math:`v = (x_1,x_2,\ldots,x_n)`
Packit 67cb25
   in :data:`n` dimensions.  The vector is normalized such that 
Packit 67cb25
   :math:`|v|^2 = x_1^2 + x_2^2 + \cdots + x_n^2 = 1`.
Packit 67cb25
   The method
Packit 67cb25
   uses the fact that a multivariate Gaussian distribution is spherically
Packit 67cb25
   symmetric.  Each component is generated to have a Gaussian distribution,
Packit 67cb25
   and then the components are normalized.  The method is described by
Packit 67cb25
   Knuth, v2, 3rd ed, p135--136, and attributed to G. W. Brown, Modern
Packit 67cb25
   Mathematics for the Engineer (1956).
Packit 67cb25
Packit 67cb25
|newpage|
Packit 67cb25
Packit 67cb25
The Weibull Distribution
Packit 67cb25
========================
Packit 67cb25
Packit 67cb25
.. index:: Weibull distribution
Packit 67cb25
Packit 67cb25
.. function:: double gsl_ran_weibull (const gsl_rng * r, double a, double b)
Packit 67cb25
Packit 67cb25
   This function returns a random variate from the Weibull distribution.  The
Packit 67cb25
   distribution function is,
Packit 67cb25
Packit 67cb25
   .. math:: p(x) dx = {b \over a^b} x^{b-1}  \exp(-(x/a)^b) dx
Packit 67cb25
Packit 67cb25
   for :math:`x \ge 0`.
Packit 67cb25
Packit 67cb25
.. function:: double gsl_ran_weibull_pdf (double x, double a, double b)
Packit 67cb25
Packit 67cb25
   This function computes the probability density :math:`p(x)` at :data:`x`
Packit 67cb25
   for a Weibull distribution with scale :data:`a` and exponent :data:`b`,
Packit 67cb25
   using the formula given above.
Packit 67cb25
Packit 67cb25
   .. image:: /images/rand-weibull.png
Packit 67cb25
Packit 67cb25
.. function:: double gsl_cdf_weibull_P (double x, double a, double b)
Packit 67cb25
              double gsl_cdf_weibull_Q (double x, double a, double b)
Packit 67cb25
              double gsl_cdf_weibull_Pinv (double P, double a, double b)
Packit 67cb25
              double gsl_cdf_weibull_Qinv (double Q, double a, double b)
Packit 67cb25
Packit 67cb25
   These functions compute the cumulative distribution functions
Packit 67cb25
   :math:`P(x)`, :math:`Q(x)` and their inverses for the Weibull
Packit 67cb25
   distribution with scale :data:`a` and exponent :data:`b`.
Packit 67cb25
Packit 67cb25
|newpage|
Packit 67cb25
Packit 67cb25
The Type-1 Gumbel Distribution
Packit 67cb25
==============================
Packit 67cb25
Packit 67cb25
.. index::
Packit 67cb25
   single: Gumbel distribution (Type 1)
Packit 67cb25
   single: Type 1 Gumbel distribution, random variates
Packit 67cb25
Packit 67cb25
.. function:: double gsl_ran_gumbel1 (const gsl_rng * r, double a, double b)
Packit 67cb25
Packit 67cb25
   This function returns  a random variate from the Type-1 Gumbel
Packit 67cb25
   distribution.  The Type-1 Gumbel distribution function is,
Packit 67cb25
Packit 67cb25
   .. math:: p(x) dx = a b \exp(-(b \exp(-ax) + ax)) dx
Packit 67cb25
Packit 67cb25
   for :math:`-\infty < x < \infty`. 
Packit 67cb25
Packit 67cb25
.. function:: double gsl_ran_gumbel1_pdf (double x, double a, double b)
Packit 67cb25
Packit 67cb25
   This function computes the probability density :math:`p(x)` at :data:`x`
Packit 67cb25
   for a Type-1 Gumbel distribution with parameters :data:`a` and :data:`b`,
Packit 67cb25
   using the formula given above.
Packit 67cb25
Packit 67cb25
   .. image:: /images/rand-gumbel1.png
Packit 67cb25
Packit 67cb25
.. function:: double gsl_cdf_gumbel1_P (double x, double a, double b)
Packit 67cb25
              double gsl_cdf_gumbel1_Q (double x, double a, double b)
Packit 67cb25
              double gsl_cdf_gumbel1_Pinv (double P, double a, double b)
Packit 67cb25
              double gsl_cdf_gumbel1_Qinv (double Q, double a, double b)
Packit 67cb25
Packit 67cb25
   These functions compute the cumulative distribution functions
Packit 67cb25
   :math:`P(x)`, :math:`Q(x)` and their inverses for the Type-1 Gumbel
Packit 67cb25
   distribution with parameters :data:`a` and :data:`b`.
Packit 67cb25
Packit 67cb25
|newpage|
Packit 67cb25
Packit 67cb25
The Type-2 Gumbel Distribution
Packit 67cb25
==============================
Packit 67cb25
Packit 67cb25
.. index::
Packit 67cb25
   single: Gumbel distribution (Type 2)
Packit 67cb25
   single: Type 2 Gumbel distribution
Packit 67cb25
Packit 67cb25
.. function:: double gsl_ran_gumbel2 (const gsl_rng * r, double a, double b)
Packit 67cb25
Packit 67cb25
   This function returns a random variate from the Type-2 Gumbel
Packit 67cb25
   distribution.  The Type-2 Gumbel distribution function is,
Packit 67cb25
Packit 67cb25
   .. math:: p(x) dx = a b x^{-a-1} \exp(-b x^{-a}) dx
Packit 67cb25
Packit 67cb25
   for :math:`0 < x < \infty`.
Packit 67cb25
Packit 67cb25
.. function:: double gsl_ran_gumbel2_pdf (double x, double a, double b)
Packit 67cb25
Packit 67cb25
   This function computes the probability density :math:`p(x)` at :data:`x`
Packit 67cb25
   for a Type-2 Gumbel distribution with parameters :data:`a` and :data:`b`,
Packit 67cb25
   using the formula given above.
Packit 67cb25
Packit 67cb25
   .. image:: /images/rand-gumbel2.png
Packit 67cb25
Packit 67cb25
.. function:: double gsl_cdf_gumbel2_P (double x, double a, double b)
Packit 67cb25
              double gsl_cdf_gumbel2_Q (double x, double a, double b)
Packit 67cb25
              double gsl_cdf_gumbel2_Pinv (double P, double a, double b)
Packit 67cb25
              double gsl_cdf_gumbel2_Qinv (double Q, double a, double b)
Packit 67cb25
Packit 67cb25
   These functions compute the cumulative distribution functions
Packit 67cb25
   :math:`P(x)`, :math:`Q(x)` and their inverses for the Type-2 Gumbel
Packit 67cb25
   distribution with parameters :data:`a` and :data:`b`.
Packit 67cb25
Packit 67cb25
|newpage|
Packit 67cb25
Packit 67cb25
The Dirichlet Distribution
Packit 67cb25
==========================
Packit 67cb25
Packit 67cb25
.. index:: Dirichlet distribution 
Packit 67cb25
Packit 67cb25
.. function:: void gsl_ran_dirichlet (const gsl_rng * r, size_t K, const double alpha[], double theta[])
Packit 67cb25
Packit 67cb25
   This function returns an array of :data:`K` random variates from a Dirichlet
Packit 67cb25
   distribution of order :data:`K`-1. The distribution function is
Packit 67cb25
Packit 67cb25
   .. only:: not texinfo
Packit 67cb25
Packit 67cb25
      .. math::
Packit 67cb25
Packit 67cb25
         p(\theta_1,\ldots,\theta_K) \, d\theta_1 \cdots d\theta_K = 
Packit 67cb25
                 {1 \over Z} \prod_{i=1}^{K} \theta_i^{\alpha_i - 1} 
Packit 67cb25
                   \; \delta(1 -\sum_{i=1}^K \theta_i) d\theta_1 \cdots d\theta_K
Packit 67cb25
Packit 67cb25
   .. only:: texinfo
Packit 67cb25
Packit 67cb25
      ::
Packit 67cb25
Packit 67cb25
         p(\theta_1, ..., \theta_K) d\theta_1 ... d\theta_K = 
Packit 67cb25
           (1/Z) \prod_{i=1}^K \theta_i^{\alpha_i - 1} \delta(1 -\sum_{i=1}^K \theta_i) d\theta_1 ... d\theta_K
Packit 67cb25
Packit 67cb25
   for :math:`\theta_i \ge 0`
Packit 67cb25
   and :math:`\alpha_i > 0`.
Packit 67cb25
   The delta function ensures that :math:`\sum \theta_i = 1`.
Packit 67cb25
   The normalization factor :math:`Z` is
Packit 67cb25
Packit 67cb25
   .. math:: Z = {\prod_{i=1}^K \Gamma(\alpha_i) \over \Gamma( \sum_{i=1}^K \alpha_i)}
Packit 67cb25
Packit 67cb25
   The random variates are generated by sampling :data:`K` values 
Packit 67cb25
   from gamma distributions with parameters 
Packit 67cb25
   :math:`a=\alpha_i$, $b=1`,
Packit 67cb25
   and renormalizing. 
Packit 67cb25
   See A.M. Law, W.D. Kelton, *Simulation Modeling and Analysis* (1991).
Packit 67cb25
Packit 67cb25
.. function:: double gsl_ran_dirichlet_pdf (size_t K, const double alpha[], const double theta[]) 
Packit 67cb25
Packit 67cb25
   This function computes the probability density 
Packit 67cb25
   :math:`p(\theta_1, \ldots , \theta_K)`
Packit 67cb25
   at :code:`theta[K]` for a Dirichlet distribution with parameters 
Packit 67cb25
   :code:`alpha[K]`, using the formula given above.
Packit 67cb25
Packit 67cb25
.. function:: double gsl_ran_dirichlet_lnpdf (size_t K, const double alpha[], const double theta[]) 
Packit 67cb25
Packit 67cb25
   This function computes the logarithm of the probability density 
Packit 67cb25
   :math:`p(\theta_1, \ldots , \theta_K)`
Packit 67cb25
   for a Dirichlet distribution with parameters 
Packit 67cb25
   :code:`alpha[K]`.
Packit 67cb25
Packit 67cb25
|newpage|
Packit 67cb25
Packit 67cb25
General Discrete Distributions
Packit 67cb25
==============================
Packit 67cb25
Packit 67cb25
Given :math:`K` discrete events with different probabilities :math:`P[k]`,
Packit 67cb25
produce a random value :math:`k` consistent with its probability.
Packit 67cb25
Packit 67cb25
The obvious way to do this is to preprocess the probability list by
Packit 67cb25
generating a cumulative probability array with :math:`K + 1` elements:
Packit 67cb25
Packit 67cb25
.. only:: not texinfo
Packit 67cb25
Packit 67cb25
   .. math::
Packit 67cb25
Packit 67cb25
      C[0] & = 0 \\
Packit 67cb25
      C[k+1] &= C[k] + P[k]
Packit 67cb25
Packit 67cb25
.. only:: texinfo
Packit 67cb25
Packit 67cb25
   ::
Packit 67cb25
Packit 67cb25
     C[0] = 0 
Packit 67cb25
     C[k+1] = C[k] + P[k]
Packit 67cb25
Packit 67cb25
Note that this construction produces :math:`C[K] = 1`.  Now choose a
Packit 67cb25
uniform deviate :math:`u` between 0 and 1, and find the value of :math:`k`
Packit 67cb25
such that :math:`C[k] \le u < C[k+1]`.
Packit 67cb25
Although this in principle requires of order :math:`\log K` steps per
Packit 67cb25
random number generation, they are fast steps, and if you use something
Packit 67cb25
like :math:`\lfloor uK \rfloor` as a starting point, you can often do
Packit 67cb25
pretty well.
Packit 67cb25
Packit 67cb25
But faster methods have been devised.  Again, the idea is to preprocess
Packit 67cb25
the probability list, and save the result in some form of lookup table;
Packit 67cb25
then the individual calls for a random discrete event can go rapidly.
Packit 67cb25
An approach invented by G. Marsaglia (Generating discrete random variables
Packit 67cb25
in a computer, Comm ACM 6, 37--38 (1963)) is very clever, and readers
Packit 67cb25
interested in examples of good algorithm design are directed to this
Packit 67cb25
short and well-written paper.  Unfortunately, for large :math:`K`,
Packit 67cb25
Marsaglia's lookup table can be quite large.  
Packit 67cb25
Packit 67cb25
A much better approach is due to Alastair J. Walker (An efficient method
Packit 67cb25
for generating discrete random variables with general distributions, ACM
Packit 67cb25
Trans on Mathematical Software 3, 253--256 (1977); see also Knuth, v2,
Packit 67cb25
3rd ed, p120--121,139).  This requires two lookup tables, one floating
Packit 67cb25
point and one integer, but both only of size :math:`K`.  After
Packit 67cb25
preprocessing, the random numbers are generated in O(1) time, even for
Packit 67cb25
large :math:`K`.  The preprocessing suggested by Walker requires
Packit 67cb25
:math:`O(K^2)` effort, but that is not actually necessary, and the
Packit 67cb25
implementation provided here only takes :math:`O(K)` effort.  In general,
Packit 67cb25
more preprocessing leads to faster generation of the individual random
Packit 67cb25
numbers, but a diminishing return is reached pretty early.  Knuth points
Packit 67cb25
out that the optimal preprocessing is combinatorially difficult for
Packit 67cb25
large :math:`K`.
Packit 67cb25
Packit 67cb25
This method can be used to speed up some of the discrete random number
Packit 67cb25
generators below, such as the binomial distribution.  To use it for
Packit 67cb25
something like the Poisson Distribution, a modification would have to
Packit 67cb25
be made, since it only takes a finite set of :math:`K` outcomes.
Packit 67cb25
Packit 67cb25
.. index::
Packit 67cb25
   single: Discrete random numbers
Packit 67cb25
   single: Discrete random numbers, preprocessing
Packit 67cb25
Packit 67cb25
.. type:: gsl_ran_discrete_t
Packit 67cb25
Packit 67cb25
   This structure contains the lookup table for the discrete random number generator.
Packit 67cb25
Packit 67cb25
.. function:: gsl_ran_discrete_t * gsl_ran_discrete_preproc (size_t K, const double * P)
Packit 67cb25
Packit 67cb25
   This function returns a pointer to a structure that contains the lookup
Packit 67cb25
   table for the discrete random number generator.  The array :data:`P` contains
Packit 67cb25
   the probabilities of the discrete events; these array elements must all be 
Packit 67cb25
   positive, but they needn't add up to one (so you can think of them more
Packit 67cb25
   generally as "weights")---the preprocessor will normalize appropriately.
Packit 67cb25
   This return value is used
Packit 67cb25
   as an argument for the :func:`gsl_ran_discrete` function below.
Packit 67cb25
Packit 67cb25
.. index:: Discrete random numbers
Packit 67cb25
Packit 67cb25
.. function:: size_t gsl_ran_discrete (const gsl_rng * r, const gsl_ran_discrete_t * g)
Packit 67cb25
Packit 67cb25
   After the preprocessor, above, has been called, you use this function to
Packit 67cb25
   get the discrete random numbers.
Packit 67cb25
Packit 67cb25
.. index:: Discrete random numbers
Packit 67cb25
Packit 67cb25
.. function:: double gsl_ran_discrete_pdf (size_t k, const gsl_ran_discrete_t * g)
Packit 67cb25
Packit 67cb25
   Returns the probability :math:`P[k]` of observing the variable :data:`k`.
Packit 67cb25
   Since :math:`P[k]` is not stored as part of the lookup table, it must be
Packit 67cb25
   recomputed; this computation takes :math:`O(K)`, so if :data:`K` is large
Packit 67cb25
   and you care about the original array :math:`P[k]` used to create the
Packit 67cb25
   lookup table, then you should just keep this original array :math:`P[k]`
Packit 67cb25
   around.
Packit 67cb25
Packit 67cb25
.. index:: Discrete random numbers
Packit 67cb25
Packit 67cb25
.. function:: void gsl_ran_discrete_free (gsl_ran_discrete_t * g)
Packit 67cb25
Packit 67cb25
   De-allocates the lookup table pointed to by :data:`g`.
Packit 67cb25
Packit 67cb25
|newpage|
Packit 67cb25
Packit 67cb25
The Poisson Distribution
Packit 67cb25
========================
Packit 67cb25
Packit 67cb25
.. index:: Poisson random numbers
Packit 67cb25
Packit 67cb25
.. function:: unsigned int gsl_ran_poisson (const gsl_rng * r, double mu)
Packit 67cb25
Packit 67cb25
   This function returns a random integer from the Poisson distribution
Packit 67cb25
   with mean :data:`mu`.  The probability distribution for Poisson variates is,
Packit 67cb25
Packit 67cb25
   .. math:: p(k) = {\mu^k \over k!} \exp(-\mu)
Packit 67cb25
Packit 67cb25
   for :math:`k \ge 0`.
Packit 67cb25
Packit 67cb25
.. function:: double gsl_ran_poisson_pdf (unsigned int k, double mu)
Packit 67cb25
Packit 67cb25
   This function computes the probability :math:`p(k)` of obtaining  :data:`k`
Packit 67cb25
   from a Poisson distribution with mean :data:`mu`, using the formula
Packit 67cb25
   given above.
Packit 67cb25
Packit 67cb25
   .. image:: /images/rand-poisson.png
Packit 67cb25
Packit 67cb25
.. function:: double gsl_cdf_poisson_P (unsigned int k, double mu)
Packit 67cb25
              double gsl_cdf_poisson_Q (unsigned int k, double mu)
Packit 67cb25
Packit 67cb25
   These functions compute the cumulative distribution functions
Packit 67cb25
   :math:`P(k)`, :math:`Q(k)` for the Poisson distribution with parameter
Packit 67cb25
   :data:`mu`.
Packit 67cb25
Packit 67cb25
|newpage|
Packit 67cb25
Packit 67cb25
The Bernoulli Distribution
Packit 67cb25
==========================
Packit 67cb25
Packit 67cb25
.. index::
Packit 67cb25
   single: Bernoulli trial, random variates
Packit 67cb25
Packit 67cb25
.. function:: unsigned int gsl_ran_bernoulli (const gsl_rng * r, double p)
Packit 67cb25
Packit 67cb25
   This function returns either 0 or 1, the result of a Bernoulli trial
Packit 67cb25
   with probability :data:`p`.  The probability distribution for a Bernoulli
Packit 67cb25
   trial is,
Packit 67cb25
Packit 67cb25
   .. only:: not texinfo
Packit 67cb25
Packit 67cb25
      .. math::
Packit 67cb25
Packit 67cb25
         p(0) & = 1 - p \\
Packit 67cb25
         p(1) & = p
Packit 67cb25
Packit 67cb25
   .. only:: texinfo
Packit 67cb25
Packit 67cb25
      ::
Packit 67cb25
Packit 67cb25
         p(0) = 1 - p
Packit 67cb25
         p(1) = p
Packit 67cb25
Packit 67cb25
.. function:: double gsl_ran_bernoulli_pdf (unsigned int k, double p)
Packit 67cb25
Packit 67cb25
   This function computes the probability :math:`p(k)` of obtaining
Packit 67cb25
   :data:`k` from a Bernoulli distribution with probability parameter
Packit 67cb25
   :data:`p`, using the formula given above.
Packit 67cb25
Packit 67cb25
   .. image:: /images/rand-bernoulli.png
Packit 67cb25
Packit 67cb25
|newpage|
Packit 67cb25
Packit 67cb25
The Binomial Distribution
Packit 67cb25
=========================
Packit 67cb25
Packit 67cb25
.. index:: Binomial random variates
Packit 67cb25
Packit 67cb25
.. function:: unsigned int gsl_ran_binomial (const gsl_rng * r, double p, unsigned int n)
Packit 67cb25
Packit 67cb25
   This function returns a random integer from the binomial distribution,
Packit 67cb25
   the number of successes in :data:`n` independent trials with probability
Packit 67cb25
   :data:`p`.  The probability distribution for binomial variates is,
Packit 67cb25
Packit 67cb25
   .. math:: p(k) = {n! \over k! (n-k)!} p^k (1-p)^{n-k}
Packit 67cb25
Packit 67cb25
   for :math:`0 \le k \le n`.
Packit 67cb25
Packit 67cb25
.. function:: double gsl_ran_binomial_pdf (unsigned int k, double p, unsigned int n)
Packit 67cb25
Packit 67cb25
   This function computes the probability :math:`p(k)` of obtaining :data:`k`
Packit 67cb25
   from a binomial distribution with parameters :data:`p` and :data:`n`, using
Packit 67cb25
   the formula given above.
Packit 67cb25
Packit 67cb25
   .. image:: /images/rand-binomial.png
Packit 67cb25
Packit 67cb25
.. function:: double gsl_cdf_binomial_P (unsigned int k, double p, unsigned int n)
Packit 67cb25
              double gsl_cdf_binomial_Q (unsigned int k, double p, unsigned int n)
Packit 67cb25
Packit 67cb25
   These functions compute the cumulative distribution functions
Packit 67cb25
   :math:`P(k)`, :math:`Q(k)`  for the binomial
Packit 67cb25
   distribution with parameters :data:`p` and :data:`n`.
Packit 67cb25
Packit 67cb25
|newpage|
Packit 67cb25
Packit 67cb25
The Multinomial Distribution
Packit 67cb25
============================
Packit 67cb25
Packit 67cb25
.. index:: Multinomial distribution 
Packit 67cb25
Packit 67cb25
.. function:: void gsl_ran_multinomial (const gsl_rng * r, size_t K, unsigned int N, const double p[], unsigned int n[])
Packit 67cb25
Packit 67cb25
   This function computes a random sample :data:`n` from the multinomial
Packit 67cb25
   distribution formed by :data:`N` trials from an underlying distribution
Packit 67cb25
   :data:`p[K]`. The distribution function for :data:`n` is,
Packit 67cb25
Packit 67cb25
   .. only:: not texinfo
Packit 67cb25
Packit 67cb25
      .. math::
Packit 67cb25
Packit 67cb25
         P(n_1, n_2,\cdots, n_K) = {{ N!}\over{n_1 ! n_2 ! \cdots n_K !}} \,
Packit 67cb25
           p_1^{n_1} p_2^{n_2} \cdots p_K^{n_K}
Packit 67cb25
Packit 67cb25
   .. only:: texinfo
Packit 67cb25
Packit 67cb25
      ::
Packit 67cb25
Packit 67cb25
         P(n_1, n_2, ..., n_K) = 
Packit 67cb25
           (N!/(n_1! n_2! ... n_K!)) p_1^n_1 p_2^n_2 ... p_K^n_K
Packit 67cb25
Packit 67cb25
   where :math:`(n_1, n_2, \ldots, n_K)`
Packit 67cb25
   are nonnegative integers with 
Packit 67cb25
   :math:`\sum_{k=1}^{K} n_k = N`,
Packit 67cb25
   and
Packit 67cb25
   :math:`(p_1, p_2, \ldots, p_K)`
Packit 67cb25
   is a probability distribution with :math:`\sum p_i = 1`.  
Packit 67cb25
   If the array :code:`p[K]` is not normalized then its entries will be
Packit 67cb25
   treated as weights and normalized appropriately.  The arrays :data:`n`
Packit 67cb25
   and :data:`p` must both be of length :data:`K`.
Packit 67cb25
Packit 67cb25
   Random variates are generated using the conditional binomial method (see
Packit 67cb25
   C.S. Davis, *The computer generation of multinomial random
Packit 67cb25
   variates*, Comp. Stat. Data Anal. 16 (1993) 205--217 for details).
Packit 67cb25
Packit 67cb25
.. function:: double gsl_ran_multinomial_pdf (size_t K, const double p[], const unsigned int n[]) 
Packit 67cb25
Packit 67cb25
   This function computes the probability 
Packit 67cb25
   :math:`P(n_1, n_2, \ldots, n_K)`
Packit 67cb25
   of sampling :code:`n[K]` from a multinomial distribution 
Packit 67cb25
   with parameters :code:`p[K]`, using the formula given above.
Packit 67cb25
Packit 67cb25
.. function:: double gsl_ran_multinomial_lnpdf (size_t K, const double p[], const unsigned int n[]) 
Packit 67cb25
Packit 67cb25
   This function returns the logarithm of the probability for the
Packit 67cb25
   multinomial distribution :math:`P(n_1, n_2, \ldots, n_K)`
Packit 67cb25
   with parameters :code:`p[K]`.
Packit 67cb25
Packit 67cb25
|newpage|
Packit 67cb25
Packit 67cb25
The Negative Binomial Distribution
Packit 67cb25
==================================
Packit 67cb25
Packit 67cb25
.. index::
Packit 67cb25
   single: Negative Binomial distribution, random variates
Packit 67cb25
Packit 67cb25
.. function:: unsigned int gsl_ran_negative_binomial (const gsl_rng * r, double p, double n)
Packit 67cb25
Packit 67cb25
   This function returns a random integer from the negative binomial
Packit 67cb25
   distribution, the number of failures occurring before :data:`n` successes
Packit 67cb25
   in independent trials with probability :data:`p` of success.  The
Packit 67cb25
   probability distribution for negative binomial variates is,
Packit 67cb25
Packit 67cb25
   .. math:: p(k) = {\Gamma(n + k) \over \Gamma(k+1) \Gamma(n) } p^n (1-p)^k
Packit 67cb25
Packit 67cb25
   Note that :math:`n` is not required to be an integer.
Packit 67cb25
Packit 67cb25
.. function:: double gsl_ran_negative_binomial_pdf (unsigned int k, double p, double  n)
Packit 67cb25
Packit 67cb25
   This function computes the probability :math:`p(k)` of obtaining :data:`k`
Packit 67cb25
   from a negative binomial distribution with parameters :data:`p` and
Packit 67cb25
   :data:`n`, using the formula given above.
Packit 67cb25
Packit 67cb25
   .. image:: /images/rand-nbinomial.png
Packit 67cb25
Packit 67cb25
.. function:: double gsl_cdf_negative_binomial_P (unsigned int k, double p, double n)
Packit 67cb25
              double gsl_cdf_negative_binomial_Q (unsigned int k, double p, double n)
Packit 67cb25
Packit 67cb25
   These functions compute the cumulative distribution functions
Packit 67cb25
   :math:`P(k)`, :math:`Q(k)` for the negative binomial distribution with
Packit 67cb25
   parameters :data:`p` and :data:`n`.
Packit 67cb25
Packit 67cb25
|newpage|
Packit 67cb25
Packit 67cb25
The Pascal Distribution
Packit 67cb25
=======================
Packit 67cb25
Packit 67cb25
.. function:: unsigned int gsl_ran_pascal (const gsl_rng * r, double p, unsigned int n)
Packit 67cb25
Packit 67cb25
   This function returns a random integer from the Pascal distribution.  The
Packit 67cb25
   Pascal distribution is simply a negative binomial distribution with an
Packit 67cb25
   integer value of :math:`n`.
Packit 67cb25
Packit 67cb25
   .. math:: p(k) = {(n + k - 1)! \over k! (n - 1)! } p^n (1-p)^k
Packit 67cb25
Packit 67cb25
   for :math:`k \ge 0`.
Packit 67cb25
Packit 67cb25
.. function:: double gsl_ran_pascal_pdf (unsigned int k, double p, unsigned int n)
Packit 67cb25
Packit 67cb25
   This function computes the probability :math:`p(k)` of obtaining :data:`k`
Packit 67cb25
   from a Pascal distribution with parameters :data:`p` and
Packit 67cb25
   :data:`n`, using the formula given above.
Packit 67cb25
Packit 67cb25
   .. image:: /images/rand-pascal.png
Packit 67cb25
Packit 67cb25
.. function:: double gsl_cdf_pascal_P (unsigned int k, double p, unsigned int n)
Packit 67cb25
              double gsl_cdf_pascal_Q (unsigned int k, double p, unsigned int n)
Packit 67cb25
Packit 67cb25
   These functions compute the cumulative distribution functions
Packit 67cb25
   :math:`P(k)`, :math:`Q(k)` for the Pascal distribution with
Packit 67cb25
   parameters :data:`p` and :data:`n`.
Packit 67cb25
Packit 67cb25
|newpage|
Packit 67cb25
Packit 67cb25
The Geometric Distribution
Packit 67cb25
==========================
Packit 67cb25
Packit 67cb25
.. index:: Geometric random variates
Packit 67cb25
Packit 67cb25
.. function:: unsigned int gsl_ran_geometric (const gsl_rng * r, double p)
Packit 67cb25
Packit 67cb25
   This function returns a random integer from the geometric distribution,
Packit 67cb25
   the number of independent trials with probability :data:`p` until the
Packit 67cb25
   first success.  The probability distribution for geometric variates
Packit 67cb25
   is,
Packit 67cb25
Packit 67cb25
   .. math:: p(k) = p (1-p)^{k-1}
Packit 67cb25
Packit 67cb25
   for :math:`k \ge 1`.
Packit 67cb25
   Note that the distribution begins with :math:`k = 1` with this
Packit 67cb25
   definition.  There is another convention in which the exponent :math:`k - 1` 
Packit 67cb25
   is replaced by :math:`k`.
Packit 67cb25
Packit 67cb25
.. function:: double gsl_ran_geometric_pdf (unsigned int k, double p)
Packit 67cb25
Packit 67cb25
   This function computes the probability :math:`p(k)` of obtaining :data:`k`
Packit 67cb25
   from a geometric distribution with probability parameter :data:`p`, using
Packit 67cb25
   the formula given above.
Packit 67cb25
Packit 67cb25
   .. image:: /images/rand-geometric.png
Packit 67cb25
Packit 67cb25
.. function:: double gsl_cdf_geometric_P (unsigned int k, double p)
Packit 67cb25
              double gsl_cdf_geometric_Q (unsigned int k, double p)
Packit 67cb25
Packit 67cb25
   These functions compute the cumulative distribution functions
Packit 67cb25
   :math:`P(k)`, :math:`Q(k)` for the geometric distribution with parameter
Packit 67cb25
   :data:`p`.
Packit 67cb25
Packit 67cb25
|newpage|
Packit 67cb25
Packit 67cb25
The Hypergeometric Distribution
Packit 67cb25
===============================
Packit 67cb25
Packit 67cb25
.. index::
Packit 67cb25
   single: hypergeometric random variates
Packit 67cb25
   single: Geometric random variates
Packit 67cb25
Packit 67cb25
.. function:: unsigned int gsl_ran_hypergeometric (const gsl_rng * r, unsigned int n1, unsigned int n2, unsigned int t)
Packit 67cb25
Packit 67cb25
   This function returns a random integer from the hypergeometric
Packit 67cb25
   distribution.  The probability distribution for hypergeometric
Packit 67cb25
   random variates is,
Packit 67cb25
Packit 67cb25
   .. math:: p(k) = C(n_1, k) C(n_2, t - k) / C(n_1 + n_2, t)
Packit 67cb25
Packit 67cb25
   where :math:`C(a,b) = a!/(b!(a-b)!)` and 
Packit 67cb25
   :math:`t \leq n_1 + n_2`.
Packit 67cb25
   The domain of :math:`k` is 
Packit 67cb25
   :math:`\max(0, t - n_2), \ldots, \min(t, n_1)`
Packit 67cb25
Packit 67cb25
   If a population contains :math:`n_1` elements of "type 1" and
Packit 67cb25
   :math:`n_2` elements of "type 2" then the hypergeometric
Packit 67cb25
   distribution gives the probability of obtaining :math:`k` elements of
Packit 67cb25
   "type 1" in :math:`t` samples from the population without
Packit 67cb25
   replacement.
Packit 67cb25
Packit 67cb25
.. function:: double gsl_ran_hypergeometric_pdf (unsigned int k, unsigned int n1, unsigned int n2, unsigned int t)
Packit 67cb25
Packit 67cb25
   This function computes the probability :math:`p(k)` of obtaining :data:`k`
Packit 67cb25
   from a hypergeometric distribution with parameters :data:`n1`, :data:`n2`,
Packit 67cb25
   :data:`t`, using the formula given above.
Packit 67cb25
Packit 67cb25
   .. image:: /images/rand-hypergeometric.png
Packit 67cb25
Packit 67cb25
.. function:: double gsl_cdf_hypergeometric_P (unsigned int k, unsigned int n1, unsigned int n2, unsigned int t)
Packit 67cb25
              double gsl_cdf_hypergeometric_Q (unsigned int k, unsigned int n1, unsigned int n2, unsigned int t)
Packit 67cb25
Packit 67cb25
   These functions compute the cumulative distribution functions
Packit 67cb25
   :math:`P(k)`, :math:`Q(k)` for the hypergeometric distribution with
Packit 67cb25
   parameters :data:`n1`, :data:`n2` and :data:`t`.
Packit 67cb25
Packit 67cb25
|newpage|
Packit 67cb25
Packit 67cb25
.. index:: Logarithmic random variates
Packit 67cb25
Packit 67cb25
The Logarithmic Distribution
Packit 67cb25
============================
Packit 67cb25
Packit 67cb25
.. function:: unsigned int gsl_ran_logarithmic (const gsl_rng * r, double p)
Packit 67cb25
Packit 67cb25
   This function returns a random integer from the logarithmic
Packit 67cb25
   distribution.  The probability distribution for logarithmic random variates
Packit 67cb25
   is,
Packit 67cb25
Packit 67cb25
   .. only:: not texinfo
Packit 67cb25
Packit 67cb25
      .. math:: p(k) = {-1 \over \log(1-p)} {\left( p^k \over k \right)}
Packit 67cb25
Packit 67cb25
   .. only:: texinfo
Packit 67cb25
      
Packit 67cb25
      ::
Packit 67cb25
Packit 67cb25
         p(k) = {-1 \over \log(1-p)} {(p^k \over k)}
Packit 67cb25
Packit 67cb25
   for :math:`k \ge 1`.
Packit 67cb25
Packit 67cb25
.. function:: double gsl_ran_logarithmic_pdf (unsigned int k, double p)
Packit 67cb25
Packit 67cb25
   This function computes the probability :math:`p(k)` of obtaining :data:`k`
Packit 67cb25
   from a logarithmic distribution with probability parameter :data:`p`,
Packit 67cb25
   using the formula given above.
Packit 67cb25
Packit 67cb25
   .. image:: /images/rand-logarithmic.png
Packit 67cb25
Packit 67cb25
|newpage|
Packit 67cb25
Packit 67cb25
.. index:: Wishart random variates
Packit 67cb25
Packit 67cb25
The Wishart Distribution
Packit 67cb25
========================
Packit 67cb25
Packit 67cb25
.. function:: int gsl_ran_wishart (const gsl_rng * r, const double n, const gsl_matrix * L, gsl_matrix * result, gsl_matrix * work)
Packit 67cb25
Packit 67cb25
   This function computes a random symmetric :math:`p`-by-:math:`p` matrix from the Wishart distribution.
Packit 67cb25
   The probability distribution for Wishart random variates is,
Packit 67cb25
Packit 67cb25
   .. only:: not texinfo
Packit 67cb25
Packit 67cb25
      .. math:: p(X) = \frac{|X|^{(n-p-1)/2} e^{-\textrm{tr}\left( V^{-1} X\right)/2}}{2^{\frac{np}{2}} \left| V \right|^{n/2} \Gamma_p(\frac{n}{2})}
Packit 67cb25
Packit 67cb25
   .. only:: texinfo
Packit 67cb25
Packit 67cb25
      .. math:: p(X) = \frac{|X|^{(n-p-1)/2} e^{-tr( V^{-1} X)/2}}{2^{(np)/2} |V|^{n/2} \Gamma_p(n/2)}
Packit 67cb25
Packit 67cb25
   Here, :math:`n > p - 1` is the number of degrees of freedom, :math:`V` is a symmetric positive definite
Packit 67cb25
   :math:`p`-by-:math:`p` scale matrix, whose Cholesky factor is specified by :data:`L`, and :data:`work` is
Packit 67cb25
   :math:`p`-by-:math:`p` workspace. The :math:`p`-by-:math:`p` Wishart distributed matrix :math:`X` is stored
Packit 67cb25
   in :data:`result` on output.
Packit 67cb25
Packit 67cb25
.. function:: int gsl_ran_wishart_pdf (const gsl_matrix * X, const gsl_matrix * L_X, const double n, const gsl_matrix * L, double * result, gsl_matrix * work)
Packit 67cb25
              int gsl_ran_wishart_log_pdf (const gsl_matrix * X, const gsl_matrix * L_X, const double n, const gsl_matrix * L, double * result, gsl_matrix * work)
Packit 67cb25
Packit 67cb25
   These functions compute :math:`p(X)` or :math:`\log{p(X)}` for the :math:`p`-by-:math:`p` matrix
Packit 67cb25
   :data:`X`, whose Cholesky factor is specified in :data:`L_X`. The degrees of freedom is
Packit 67cb25
   given by :data:`n`, the Cholesky factor of the scale matrix :math:`V` is specified in :data:`L`,
Packit 67cb25
   and :data:`work` is :math:`p`-by-:math:`p` workspace. The probably density value is returned
Packit 67cb25
   in :data:`result`.
Packit 67cb25
Packit 67cb25
|newpage|
Packit 67cb25
Packit 67cb25
Shuffling and Sampling
Packit 67cb25
======================
Packit 67cb25
Packit 67cb25
The following functions allow the shuffling and sampling of a set of
Packit 67cb25
objects.  The algorithms rely on a random number generator as a source of
Packit 67cb25
randomness and a poor quality generator can lead to correlations in the
Packit 67cb25
output.  In particular it is important to avoid generators with a short
Packit 67cb25
period.  For more information see Knuth, v2, 3rd ed, Section 3.4.2,
Packit 67cb25
"Random Sampling and Shuffling".
Packit 67cb25
Packit 67cb25
.. function:: void gsl_ran_shuffle (const gsl_rng * r, void * base, size_t n, size_t size)
Packit 67cb25
Packit 67cb25
   This function randomly shuffles the order of :data:`n` objects, each of
Packit 67cb25
   size :data:`size`, stored in the array :code:`base[0..n-1]`.  The
Packit 67cb25
   output of the random number generator :data:`r` is used to produce the
Packit 67cb25
   permutation.  The algorithm generates all possible :math:`n!`
Packit 67cb25
   permutations with equal probability, assuming a perfect source of random
Packit 67cb25
   numbers.
Packit 67cb25
Packit 67cb25
   The following code shows how to shuffle the numbers from 0 to 51::
Packit 67cb25
Packit 67cb25
      int a[52];
Packit 67cb25
Packit 67cb25
      for (i = 0; i < 52; i++)
Packit 67cb25
        {
Packit 67cb25
          a[i] = i;
Packit 67cb25
        }
Packit 67cb25
Packit 67cb25
      gsl_ran_shuffle (r, a, 52, sizeof (int));
Packit 67cb25
Packit 67cb25
.. function:: int gsl_ran_choose (const gsl_rng * r, void * dest, size_t k, void * src, size_t n, size_t size)
Packit 67cb25
Packit 67cb25
   This function fills the array :code:`dest[k]` with :data:`k` objects taken
Packit 67cb25
   randomly from the :data:`n` elements of the array
Packit 67cb25
   :code:`src[0..n-1]`.  The objects are each of size :data:`size`.  The
Packit 67cb25
   output of the random number generator :data:`r` is used to make the
Packit 67cb25
   selection.  The algorithm ensures all possible samples are equally
Packit 67cb25
   likely, assuming a perfect source of randomness.
Packit 67cb25
Packit 67cb25
   The objects are sampled **without** replacement, thus each object can
Packit 67cb25
   only appear once in :data:`dest`.  It is required that :data:`k` be less
Packit 67cb25
   than or equal to :data:`n`.  The objects in :data:`dest` will be in the
Packit 67cb25
   same relative order as those in :data:`src`.  You will need to call
Packit 67cb25
   :code:`gsl_ran_shuffle(r, dest, n, size)` if you want to randomize the
Packit 67cb25
   order.
Packit 67cb25
Packit 67cb25
   The following code shows how to select a random sample of three unique
Packit 67cb25
   numbers from the set 0 to 99::
Packit 67cb25
Packit 67cb25
      double a[3], b[100];
Packit 67cb25
Packit 67cb25
      for (i = 0; i < 100; i++)
Packit 67cb25
        {
Packit 67cb25
          b[i] = (double) i;
Packit 67cb25
        }
Packit 67cb25
Packit 67cb25
      gsl_ran_choose (r, a, 3, b, 100, sizeof (double));
Packit 67cb25
Packit 67cb25
.. function:: void gsl_ran_sample (const gsl_rng * r, void * dest, size_t k, void * src, size_t n, size_t size)
Packit 67cb25
Packit 67cb25
   This function is like :func:`gsl_ran_choose` but samples :data:`k` items
Packit 67cb25
   from the original array of :data:`n` items :data:`src` with replacement, so
Packit 67cb25
   the same object can appear more than once in the output sequence
Packit 67cb25
   :data:`dest`.  There is no requirement that :data:`k` be less than :data:`n`
Packit 67cb25
   in this case.
Packit 67cb25
Packit 67cb25
Examples
Packit 67cb25
========
Packit 67cb25
Packit 67cb25
The following program demonstrates the use of a random number generator
Packit 67cb25
to produce variates from a distribution.  It prints 10 samples from the
Packit 67cb25
Poisson distribution with a mean of 3.
Packit 67cb25
Packit 67cb25
.. include:: examples/randpoisson.c
Packit 67cb25
   :code:
Packit 67cb25
Packit 67cb25
If the library and header files are installed under :file:`/usr/local`
Packit 67cb25
(the default location) then the program can be compiled with these
Packit 67cb25
options::
Packit 67cb25
Packit 67cb25
  $ gcc -Wall demo.c -lgsl -lgslcblas -lm
Packit 67cb25
Packit 67cb25
Here is the output of the program,
Packit 67cb25
Packit 67cb25
.. include:: examples/randpoisson.txt
Packit 67cb25
   :code:
Packit 67cb25
Packit 67cb25
The variates depend on the seed used by the generator.  The seed for the
Packit 67cb25
default generator type :type:`gsl_rng_default` can be changed with the
Packit 67cb25
:macro:`GSL_RNG_SEED` environment variable to produce a different stream
Packit 67cb25
of variates::
Packit 67cb25
Packit 67cb25
  $ GSL_RNG_SEED=123 ./a.out
Packit 67cb25
Packit 67cb25
giving output
Packit 67cb25
Packit 67cb25
.. include:: examples/randpoisson2.txt
Packit 67cb25
   :code:
Packit 67cb25
Packit 67cb25
The following program generates a random walk in two dimensions.
Packit 67cb25
Packit 67cb25
.. include:: examples/randwalk.c
Packit 67cb25
   :code:
Packit 67cb25
Packit 67cb25
:numref:`fig_rand-walk` shows the output from the program.
Packit 67cb25
Packit 67cb25
.. _fig_rand-walk:
Packit 67cb25
Packit 67cb25
.. figure:: /images/random-walk.png
Packit 67cb25
   :scale: 60%
Packit 67cb25
Packit 67cb25
   Four 10-step random walks from the origin.
Packit 67cb25
Packit 67cb25
The following program computes the upper and lower cumulative
Packit 67cb25
distribution functions for the standard normal distribution at
Packit 67cb25
:math:`x = 2`.
Packit 67cb25
Packit 67cb25
.. include:: examples/cdf.c
Packit 67cb25
   :code:
Packit 67cb25
Packit 67cb25
Here is the output of the program,
Packit 67cb25
Packit 67cb25
.. include:: examples/cdf.txt
Packit 67cb25
   :code:
Packit 67cb25
Packit 67cb25
References and Further Reading
Packit 67cb25
==============================
Packit 67cb25
Packit 67cb25
For an encyclopaedic coverage of the subject readers are advised to
Packit 67cb25
consult the book "Non-Uniform Random Variate Generation" by Luc
Packit 67cb25
Devroye.  It covers every imaginable distribution and provides hundreds
Packit 67cb25
of algorithms.
Packit 67cb25
Packit 67cb25
* Luc Devroye, "Non-Uniform Random Variate Generation",
Packit 67cb25
  Springer-Verlag, ISBN 0-387-96305-7.  Available online at
Packit 67cb25
  http://cg.scs.carleton.ca/~luc/rnbookindex.html.
Packit 67cb25
Packit 67cb25
The subject of random variate generation is also reviewed by Knuth, who
Packit 67cb25
describes algorithms for all the major distributions.
Packit 67cb25
Packit 67cb25
* Donald E. Knuth, "The Art of Computer Programming: Seminumerical
Packit 67cb25
  Algorithms" (Vol 2, 3rd Ed, 1997), Addison-Wesley, ISBN 0201896842.
Packit 67cb25
Packit 67cb25
The Particle Data Group provides a short review of techniques for
Packit 67cb25
generating distributions of random numbers in the "Monte Carlo"
Packit 67cb25
section of its Annual Review of Particle Physics.
Packit 67cb25
Packit 67cb25
* Review of Particle Properties,
Packit 67cb25
  R.M. Barnett et al., Physical Review D54, 1 (1996)
Packit 67cb25
  http://pdg.lbl.gov/.
Packit 67cb25
Packit 67cb25
The Review of Particle Physics is available online in postscript and pdf
Packit 67cb25
format.
Packit 67cb25
Packit 67cb25
An overview of methods used to compute cumulative distribution functions
Packit 67cb25
can be found in *Statistical Computing* by W.J. Kennedy and
Packit 67cb25
J.E. Gentle. Another general reference is *Elements of Statistical
Packit 67cb25
Computing* by R.A. Thisted.
Packit 67cb25
Packit 67cb25
* William E. Kennedy and James E. Gentle, Statistical Computing (1980),
Packit 67cb25
  Marcel Dekker, ISBN 0-8247-6898-1.
Packit 67cb25
Packit 67cb25
* Ronald A. Thisted, Elements of Statistical Computing (1988), 
Packit 67cb25
  Chapman & Hall, ISBN 0-412-01371-1.
Packit 67cb25
Packit 67cb25
The cumulative distribution functions for the Gaussian distribution
Packit 67cb25
are based on the following papers,
Packit 67cb25
Packit 67cb25
* Rational Chebyshev Approximations Using Linear Equations,
Packit 67cb25
  W.J. Cody, W. Fraser, J.F. Hart. Numerische Mathematik 12, 242--251 (1968).
Packit 67cb25
Packit 67cb25
* Rational Chebyshev Approximations for the Error Function,
Packit 67cb25
  W.J. Cody. Mathematics of Computation 23, n107, 631--637 (July 1969).