Density Namespace Reference


Functions

double dbeta (const double x, const double a, const double b, const bool give_log)
double dbinom (const int k, const int n, double p, bool give_log)
double dcauchy (const double x, const double l, const double s, const bool give_log)
double dchisq (const double x, const double n, const bool give_log)
double ddirch (const std::vector< double > &p, const std::vector< double > &a, const bool give_log, const bool include_const)
double dexp (const double x, const double b, const bool give_log)
double df (const double x, const double m, const double n, const bool give_log)
double dgamma (const double x, const double shape, const double scale, const bool give_log)
double dgeom (const unsigned x, const double p, const bool give_log)
double dhyper (const unsigned x, const unsigned r, const unsigned b, const unsigned n, bool giveLog)
double dinvgamma (const double y, const double shape, const double scale, const bool give_log)
double dlnorm (const double x, const double mu, const double sigma, const bool give_log)
double dlogis (const double x, const double m, const double s, const bool give_log)
double dmulti (const std::vector< int > &n, const std::vector< double > &p, const bool give_log, const bool include_factorial)
double dnbinom (const unsigned x, const double n, const double p, const bool give_log)
double dnorm (const double x_in, const double mu, const double sigma, const bool give_log)
double dpois (const unsigned x, const double lambda, const bool give_log)
double dt (const double x, const double n, const bool give_log)
double dweibull (const double x, const double a, const double b, const bool give_log)
double gamln (const double x)
double logChoose (const double n, const double k)
double lbeta (const double a, const double b)
double BetaEntropy (const double a, const double b)

Variables

const double MinGammaPar
const double MaxGammaPar


Detailed Description

Used to isolate functions for density evaluation.

Provides a variety of numerical density routines. All of them are derived from R. Details are provided in the comments accompanying each one.


Function Documentation

double Density::BetaEntropy ( const double  a,
const double  b 
)

Entropy of a beta distribution with parameters a and b.

Formula:

\[(a-1)(\Psi(a) - \Psi(a+b)) + (b-1)(\Psi(b) - \Psi(a+b)) /// - \log(\beta(a, b))\]

Parameters:
a first parameter of the beta distribution
b second parameter of the beta distribution
$\Psi(x)$ is Euler's psi function (also known as the digamma function).

Definition at line 1999 of file Density.cpp.

References lbeta().

double Density::dbeta ( const double  x,
const double  a,
const double  b,
const bool  give_log = false 
)

Density of the beta distribution

Returns the probability density associated with a beta variate:

\[f(x) = \frac{\Gamma(a+b)}{\Gamma(a)\Gamma(b)}x^{a-1}(1-x)^{b-1} \]

Parameters:
x 
a 
b 
give_log Return log density?

\[ \mbox{E}(x) = \frac{a}{a+b} \]

\[ \mbox{Var}(x) = \frac{ab}{(a+b)^2(a+b+1)} \]

This implementation is derived from R. It assumes that the caller has ensured that a and b are positive.

Definition at line 841 of file Density.cpp.

Referenced by logQBeta().

double Density::dbinom ( const int  k,
const int  n,
double  p,
bool  give_log 
)

Density of the binomial distribution.

\[ f(k) = {n \choose k}p^k (1-p)^{n-k} \]

Returns probability of getting k successes in n binomial trials with a probability p of success on each trial, if give_log == false. If give_log == true, returns the natural logarithm of the probability.

Parameters:
k Number of successes
n Number of trials
p Probability of success on each trial
give_log Return log probability?

\[ \mbox{E}(x) = np \]

\[ \mbox{Var}(x) = np(1-p) \]

The implementation uses dbinom_raw from R

Definition at line 890 of file Density.cpp.

double Density::dcauchy ( const double  x,
const double  l,
const double  s,
const bool  give_log 
)

Density of the Cauchy distribution

\[ f(x) = \frac{1}{\pi\mbox{s} (1 + (\frac{x-\mbox{l}}{\mbox{s}})^2)} \]

Parameters:
x the x value at which the density is to be calculated
l the location parameter
s the scale parameter
give_log return natural log of density if true
The expectation and variance of the Cauchy distribution are infinite. The mode is equal to the location parameter.

This implementation is adapted from R v2.0. The sanity checks for s > 0 and ISNAN(x) have been removed.

Definition at line 932 of file Density.cpp.

double Density::dchisq ( const double  x,
const double  n,
const bool  give_log 
)

Density of the chi-squared distribution

\[ f(x) = \frac{1}{2^{n/2}\Gamma(n/2)}x^{n/2-1}e^{-x/2} \]

Parameters:
x the chi-squared variate whose density is desired
n degrees of freedom for the chi-squared density
give_log true if natural logarithm of density is desired

\[ \mbox{E}(x) = \mbox{n} \]

\[ \mbox{Var}(x) = 2\mbox{n} \]

From R v2.0. The implementation simply calls dgamma with shape = n/2 and scale = 2.

Definition at line 978 of file Density.cpp.

References dgamma().

double Density::ddirch ( const std::vector< double > &  p,
const std::vector< double > &  a,
const bool  give_log = false,
const bool  include_const = true 
)

Density of the Dirichlet distribution

A brute-force implementation of the Dirichlet density:

\[f({\bf p}) /// = \Gamma(\sum_k a_k)\prod_k \frac{p_k^{a_k-1}}{\Gamma(a_k)}\]

Parameters:
p Vector of probabilities
a Vector of Dirichlet parameters
give_log Return log density?
include_const Include normalizing constant

Definition at line 995 of file Density.cpp.

References gamln(), and Util::log_dbl_min.

Referenced by logQDirch().

double Density::dexp ( const double  x,
const double  b,
const bool  give_log 
)

Exponential density

\[ f(x) = \frac{1}{b}e^{-x/b} \]

Parameters:
x the exponential variate
b the parameter of the exponential density
give_log return log density if true
Returns:
0 or Util::log_dbl_min if x < 0

\[ \mbox{E}(x) = \mbox{b} \]

\[ \mbox{Var}(x) = \mbox{b} \]

Derived from R v2.0. Does not propagate NaNs. Does not check to ensure b > 0.

Definition at line 1053 of file Density.cpp.

double Density::df ( const double  x,
const double  m,
const double  n,
const bool  give_log 
)

Density of the F distribution

\[ f(x) = \frac{\Gamma((m+n)/2)}{\Gamma(m/2)\Gamma(n/2)}(m/n)^{m/2}x^{m/2-1}(1+(m/n)x)^{-(m+n)/2} \]

Parameters:
x the F variate
m ``numerator'' degrees of freedom
n ``denominator'' degrees of freedom
give_log return log density if true

\[ \mbox{E}(x) = \frac{m}{m-2}, m > 2 \]

\[ \mbox{Var}(x) = \frac{2m^2(n-2)}{n(m+2)}, n > 2 \]

Derived from R v2.0. Does not do isnan() check on arguments. Does not check m > 0 and n > 0. Callers must ensure that these conditions are met.

Definition at line 1109 of file Density.cpp.

double Density::dgamma ( const double  x,
const double  shape,
const double  scale,
const bool  give_log 
)

Density of the gamma distribution.

Returns the probability density associated with a gamma variate:

\[f(x) = \frac{1}{s^{a} \Gamma(a)} x^{a-1} e^{-x/s}\]

Parameters:
x A gamma variate (x)
shape Shape of the distribution (a)
scale Scale of the distribution (s)
give_log Return log density?

\[ \mbox{E}(x) = sa \]

\[ \mbox{Var}(x) = s^2a \]

The implementation is derived from R.

Definition at line 1146 of file Density.cpp.

References Util::dbl_max.

Referenced by dchisq().

double Density::dgeom ( const unsigned  x,
const double  p,
const bool  give_log 
)

Density of the geometric distribution

\[ f(x) = p(1-p)^x \]

Parameters:
x the (integer) geometric variate
p the parameter of the geometric distribution
give_log return log density if true

\[ \mbox{E}(x) = \frac{1-p}{p} \]

\[ \mbox{Var}(x) = \frac{1-p}{p^2} \]

Derived from R v2.0. Does not check isnan() on x and p. Does not check for 0 < p < 1. Changed x to unsigned int, so check on it is no longer required.

Definition at line 1215 of file Density.cpp.

double Density::dhyper ( const unsigned  x,
const unsigned  r,
const unsigned  b,
const unsigned  n,
bool  giveLog 
)

Density of the hypergeometric distribution.

\[ f(x) = \frac{{r \choose x}{b \choose n-x}}{{r+b \choose n}} \]

Returns the probability of choosing x white balls in a sample of size n from an urn with r white balls and b black balls (sampling without replacement.

Parameters:
x The number of white balls in the sample
r The number of white balls in the urn
b The number of black balls in the urn
n The sample size
giveLog Return log probability?

\[ \mbox{E}(x) = n(\frac{r}{r+b}) \]

\[ \mbox{Var}(x) = \frac{n(\frac{r}{r+b})(1-\frac{r}{r+b})((r+b)-n)}{r+b-1} \]

The code is modified from R v1.8.1 to take unsigned integer arguments rather than doubles.

Definition at line 1278 of file Density.cpp.

double Density::dinvgamma ( const double  y,
const double  shape,
const double  scale,
const bool  give_log 
)

Density of the inverse gamma distribution.

Returns the probability density associated with an inverse gamma variate:

\[f(y) = f(1/x) \]

\[f(y) = \frac{s^a}{\Gamma(a)} y^{-(a+1)} e^{-s/y}\]

Parameters:
y An inverse gamma variate (y)
shape Shape of the distribution (a)
scale Scale of the distribution (s)
give_log Return log density?

\[ \mbox{E}(x) = \frac{s}{a-1} \mbox{ for } a > 1 \]

\[ \mbox{Var}(x) = \frac{s^2}{(a-1)^2(a-2)} \mbox{ for } a > 2 \]

The implementation is based on the R implementation of dgamma(). It first calculates

\[ p(y) = \frac{(s/y)^a e^{-s/y}}{\Gamma(a)} \]

$f(y)$ is then given by

\[ f(y) = \frac{p(y)}{y} \]

Definition at line 1323 of file Density.cpp.

References gamln().

double Density::dlnorm ( const double  x,
const double  mu,
const double  sigma,
const bool  give_log 
)

Density of the lognormal distribution

\[ f(x) = \frac{1}{\sigma x\sqrt{2\pi}}e^{-\frac{(\log(x) - \mu)^2}{2\sigma^2}} \]

Parameters:
x the lognormal variate
mu logarithm of the mean of the corresponding normal ($\mu$)
sigma logarithm of the sd of the corresponding normal ($\sigma$)
give_log return log density if true

\[ \mbox{E}(x) = e^{\mu + \sigma^2/2} \]

\[ \mbox{Var}(x) = e^{2\mu + \sigma^2}(e^{\sigma^2}-1) \]

\[ \mbox{mode} = \frac{e^\mu}{e^{\sigma^2}} \]

\[ \mbox{median} = e^\mu \]

Derived from R v2.0. Does not do isnan() checks on arguments. Does not check for sigma > 0.

Definition at line 1380 of file Density.cpp.

double Density::dlogis ( const double  x,
const double  m,
const double  s,
const bool  give_log 
)

Density of the logistic distribution

\[ f(x) = \frac{1}{s}\frac{e^{\frac{x-m}{s}}}{(1 + e^{\frac{x-m}{s}})^2} \]

or equivalently (dividing numerator and denominator by $e^{2\frac{x-m}{s}}$)

\[ f(x) = \frac{1}{s}\frac{e^{\frac{-(x-m)}{s}}}{(1 + e^{\frac{-(x-m)}{s}})^2} \]

Parameters:
x the logistic variate
m the location parameter
s the scale parameter
give_log return log density if true

\[ \mbox{E}(x) = m \]

\[ \mbox{Var}(x) = \frac{\pi^2s^2}{3} \]

Derived from R v2.0. Does not do isnan() checks on parameters. Does not check for scale > 0.

Definition at line 1429 of file Density.cpp.

double Density::dmulti ( const std::vector< int > &  n,
const std::vector< double > &  p,
const bool  give_log = false,
const bool  include_factorial = false 
)

Density of the multinomial distribution

A brute-force implementation of the multinomial density

\[ f({\bf n}) = {\sum_i n_i \choose n_1 \dots n_I}\prod_i p_i^{n_i} \]

Parameters:
n Vector of observations
p Vector of probabilities
give_log Return log probability
include_factorial Leave out combinatorial coefficient?

Definition at line 1452 of file Density.cpp.

References Util::dbl_min, gamln(), and Util::log_dbl_min.

double Density::dnbinom ( const unsigned  x,
const double  n,
const double  p,
const bool  give_log 
)

Density of the negative binomial distribution

\[ f(x) = \frac{\Gamma(x+n)}{\Gamma(n)x!}p^n(1-p)^x \]

Parameters:
x the negative binomial variate
n the ``size'' parameter
p the ``probability'' parameter
give_log return log density if true

\[ \mbox{E}(x) = \frac{x(1-p)}{p} \]

\[ \mbox{Var}(x) = \frac{x(1-p)}{p^2} \]

Derived from R v2.0. Does not check isnan() on arguments. Does not check 0 < p < 1 or n > 0. Allows non-integer n (as in R). Integer checks for x not needed, since it is passed as unsigned.

Definition at line 1524 of file Density.cpp.

double Density::dnorm ( const double  x_in,
const double  mu,
const double  sigma,
const bool  give_log 
)

Density of the normal distribution.

Returns the probability density associated with a normal variate:

\[f(x) = \frac{1}{\sigma\sqrt{2\pi}}e^{-(x-\mu)^2/2\sigma^2} \]

Parameters:
x_in A normal variate (x)
mu Mean ($\mu$)
sigma Standard deviation ($\sigma$)
give_log Return log density?

\[ \mbox{E}(x) = \mu \]

\[ \mbox{Var}(x) = \sigma^2 \]

This implementation is derived from Mathlib via R.

Definition at line 1577 of file Density.cpp.

Referenced by logQNorm().

double Density::dpois ( const unsigned  x,
const double  lambda,
const bool  give_log 
)

Density of the Poisson distribution

\[ f(x) = \frac{\lambda^x e^{-\lambda}}{x!} \]

Parameters:
x the Poisson variate
lambda the Poisson parameter ($\lambda$)
give_log return log density if true

\[ \mbox{E}(x) = \lambda \]

\[ \mbox{Var}(x) = \lambda \]

Derived from R v2.0. No isnan() checks. Integer check on x discarded since it is unsigned.

Definition at line 1628 of file Density.cpp.

double Density::dt ( const double  x,
const double  n,
const bool  give_log 
)

Density of Student's t distribution

\[ f(x) = \frac{\Gamma(\frac{\nu+1}{2})}{\sqrt{\pi\nu}\Gamma(\frac{\nu}{2})(1 + \frac{x^2}{\nu})^{(\nu + 1)/2}} \]

Parameters:
x the t deviate
n the degrees of freedom ($\nu$)
give_log return log density if true

\[ \mbox{E}(x) = 0 \quad , \quad \nu > 1 \]

\[ \mbox{Var}(x) = \frac{\nu}{\nu - 2} \quad , \quad \nu > 2 \]

Derived from R v2.0. Does not check isnan() or isfinite() on arguments. Does not verify n >= 0.

Definition at line 1680 of file Density.cpp.

Referenced by lot::hypergeom().

double Density::dweibull ( const double  x,
const double  a,
const double  b,
const bool  give_log 
)

Density of the Weibull distribution

\[ f(x) = (\frac{a}{b})(\frac{x}{b})^{a-1}e^{-\frac{x}{b}^a} \]

Parameters:
x the Weibull variate
a the ``shape'' parameter
b the ``scale'' parameter
give_log return log density if true

\[ \mbox{E}(x) = b\Gamma(1 + \frac{1}{a}) \]

\[ \mbox{Var}(x) = b^2(\Gamma(1+\frac{2}{a}) - \Gamma(1+\frac{1}{a})^2) \]

Derived from R v2.0. Does not check isnan() on arguments. Does not check for finite x.

Definition at line 1730 of file Density.cpp.

double Density::gamln ( const double  x  ) 

Natural log of the gamma function

Parameters:
x 
from

NIST Guide to Available Math Software. Source for module GAMLN from package CMLIB. Retrieved from TIBER on Wed Apr 29 17:30:20 1998.

Definition at line 1785 of file Density.cpp.

Referenced by ddirch(), dinvgamma(), dmulti(), lbeta(), and logChoose().

double Density::lbeta ( const double  a,
const double  b 
)

Logarithm of $\beta(a,b)$.

Formula:

\[\log(\Gamma(a)) + \log(\Gamma(b)) - \log(\Gamma(a+b))\]

Parameters:
a 
b 

Definition at line 1981 of file Density.cpp.

References gamln().

Referenced by BetaEntropy().

double Density::logChoose ( const double  n,
const double  k 
)

Logarithm of n choose k.

Formula:

\[\log(\gamma(n+1)) - \log(\gamma(k+1)) - log(\gamma(n-k+1))\]

Parameters:
n Sample size
k Number of successes

Definition at line 1967 of file Density.cpp.

References gamln().


Variable Documentation

const double Density::MaxGammaPar

Maximum value of parameters to be used in gamln()

Definition at line 45 of file Density.cpp.

const double Density::MinGammaPar

Minimum value of parameters to be used in gamln()

Definition at line 44 of file Density.cpp.


Generated on Tue Mar 27 16:03:53 2007 for mcmc by  doxygen 1.5.1