# Distribution Densities Library

The Distribution Densities library is a library that ships with Analytica. The library contains functions that analytically (i.e., without sampling) compute the probability density at a point, cumulative probability at a point and inverse cumulative probability for many distributions. It contains a complete set of density functions for all continuous distributions, probability functions for all discrete distributions, as well as cumulative and inverse cumulative probability functions for almost all continuous built-in distributions.

All functions here take a first parameter, indicating the point where the function is being computed, followed by exactly the same parameters that the corresponding distribution function takes.

Download Distribution Densities.ana and place it in the Libraries directory under the Analytica install path, replacing the version that is there.

## Density Functions

For each continuous distribution, the library contains a function prefixed by Dens_, then the name of the distribution, which computes the probability density at a point. The parameters after the first are the same as the corresponding distribution function. So, for example, to find the density of a `Normal(5, 7)` distribution at `x = 3`, we would use:

`Dens_Normal(3, 5, 7) → 0.05471`

Probability densities are, of course, used for many purposes in statistics. One area where they are needed, for example, is for importance sampling.

## Probability Functions

For each discrete distribution, the library contains a corresponding function prefixed with the name Prob_ that computes the probability at the value k. The first parameter is the point k, for which the desired probability is desired, followed by the same parameters used by the corresponding distribution function. So, for example, to find the probability that a sample drawn from a `Binomial(10, 0.7)` distribution is equal to 4, we would use:

`Prob_Binomial(4, 10, 0.7) → 0.03676`

## Cumulative Probability Functions

These exist for univariate distributions, both continuous and discrete. They return the probability area to the left of and at the indicated value. These consist of functions prefixed with Cum, followed by the distribution name. A few of the more difficult cumulative distribution functions are already built into Analytica, and are not repeated in this library, for example CumNormal, BetaI, and GammaI. The BetaI and GammaI functions are the only ones that deviate from the CumDist naming convention, which occurs because these particular functions more often are refered to as the incomplete beta function and incomplete gamma function.

To find the cumulative probability to the left of 5.5 in a `Logistic(10)` distribution, use:

`CumLogistic(5.5, 10) → 0.01099`

The collection also contains cumulative distribution functions for the F-Distribution, CumFDist and CumFDistInv, functions often used in classical hypothesis testing, even though these aren't built-in distributions in Analytica.

For any distribution function, the cumulative probability at x can always be computed using:

`Probability(Distribution(a, b, c) <= x)`

where Distribution(a, b, c) is the arbitrary distribution function. However, doing it in this fashion computes the result using Monte Carlo or Latin Hypercube sampling, which in general is an approximation. The cumulative probability functions that are included in this library compute the cumulative probability analytically, rather than by sampling. Analytic results are usually more accurate and often more efficiently computed.

## Inverse Cumulative Probability Functions

Inverse cumulative distribution functions return the value where the probability mass to the left of the value is equal to the given value. These are included for all built-in parametric continuous distributions.

The inverse cumulative functions have the naming convention that they are prefixed with Cum and suffixed by Inv. So, for example, the inverse cumulative distribution function for Weibull is CumWeibullInv. The only exceptions are BetaIInv and GammaIInv.

As the name implies, the inverse cumulative distribution functions are inverses of the cumulative distribution functions. Thus:

`CumDistInv(CumDist(x, a, b, c), a, b, c) → x`

and

`CumDist(CumDistInv(p, a, b, c), a, b, c) → p`

where Dist is a distribution with parameters a, b, and c, and where x is within the valid range of the distribution and 0 ≤ p ≤ 1.

For any arbitrary distribution in Analytica, it is always possible to compute the inverse cumulative density at p using the GetFract function, as follows:

`GetFract(Distribution(a, b, c), p)`

While general, this computation approximates the inverse cumulative distribution function by using Monte Carlo sampling. For very complex distribution functions, that is often a reasonable method for obtaining the inverse. However, the general sampling approach using GetFract differs from the functions described here, in that the functions in this library compute the inverse CDF analytically, obtaining the value without necessarily resorting to simulation or approximation. For many distributions (the ones included here), this tends to produce a more accurate result and with less computation time.

## Listing of functions in this library

Most distribution functions themselves come built in to Analytica and do not require the Distribution Densities Library. To use any of the Density, Cumulative Density, or Inverse Cumulative functions you will need to add the library to your model.

### Continuous Distributions

Distribution Function Density Function Cumulative Density Function Inverse Cumulative Function
Beta(a, b) Dens_Beta(x, a, b) BetaI(x, a, b) BetaIInv(p, a, b)
ChiSquared(dof) Dens_ChiSquared(x, dof) CumChiSquared(x, dof) CumChiSquaredInv(p, dof)
CumDist(P, R, I) Dens_CumDist(x, P, R, I)
Exponential(rate) Dens_Exponential(x, rate) CumExponential(x, rate) CumExponentialInv(p, rate)
F-distribution
(no built-in dist)
Dens_FDist(x, d1, d2) CumFDist(x, d1, d2) CumFDistInv(p, d1, d2)
Gamma(a, b) Dens_Gamma(x, a, b) GammaI(x, a, b) GammaIInv(p, a, b)
Gaussian(m, cv, I, J) Dens_Gaussian(x, m, cv, I, J) n/a n/a
InvertedWishart(psi, n, I, J) LDens_InvertedWishart(x, psi, n, I, J) n/a n/a
Logistic(mean, scale) Dens_Logistic(x, mean, scale) CumLogistic(x, mean, scale) CumLogisticInv(x, mean, scale)
LogNormal(median, gsdev, mean, stddev) Dens_LogNormal(x, median, gsdev, mean, stddev) CumLogNormal(x, median, gsdev, mean, stddev) CumLogNormalInv(p, median, gsdev, mean, stddev)
Normal(m, sd) Dens_Normal(x, m, sd) CumNormal(x, m, sd) CumNormalInv(x, m, sd)
ProbDist(p, r, I) Dens_ProbDist(x, p, r, I)
StudentT(dof) Dens_StudentT(x, dof) CumStudentT(x, dof) CumStudentT(p, dof)
Triangular(min, mode, max) Dens_Triangular(x, min, mode, max) CumTriangular(x, min, mode, max) CumTriangularInv(p, min, mode, max)
Uniform(lb, ub) Dens_Uniform(x, lb, ub) CumUniform(x, lb, ub) CumUniformInv(p, lb, ub)
Weibull(shape, scale) Dens_Weibull(x, shape, scale) CumWeibull(x, shape, scale) CumWeibullInv(shape, scale)
Wishart(cv, n, I, J) LDens_Wishart(x, cv, n, I, J) n/a n/a

### Discrete Distributions

Distribution Function Probability Function Cumulative Probability Function Inverse Cumulative Probability Function
Bernoulli(p) Prob_Bernoulli(k, p) n/a n/a
Binomial(n, p) Prob_Binomial(k, n, p) CumBinomial(k, n, p) CumBinomialInv(u, n, p)
ChanceDist(p, a, I) Prob_ChanceDist(x, p, a, I)
Geometric(p) Prob_Geometric(k, p) CumGeometric(k, p) CumGeometricInv(u, p)
HyperGeometric(trials, posEvents, size) Prob_HyperGeometric(k, trials, posEvents, size)
NegativeBinomial(r, p) Prob_NegativeBinomia(k, r, p) CumNegativeBinomial(k, r, p) CumNegativeBinomInv(u, r, p)
Poisson(mean) Prob_Poisson(k, mean) CumPoisson(k, mean)