Distributions

copyright:
  1. 2015 by Mat Leonard.
license:

MIT, see LICENSE for more details.

Distribution log likelihoods for building Bayesian models.

These should all automatically sum the log likelihoods if x is a numpy array.

distributions.bernoulli(k, p)

Bernoulli distribution log-likelihood.

Parameters:
  • kint, np.array. Number of successes.
  • pint, float, np.array. Success probability.

Special case of binomial distribution, with n set to 1.

distributions.beta(x, alpha=1, beta=1)

Beta distribution log-likelihood.

Parameters:
  • xfloat, np.array. \(0 < x < 1\)
  • alpha – (optional) int, float. Shape parameter, \(\alpha > 0\)
  • beta – (optional) int, float. Shape parameter, \(\beta > 0\)
\[\log{P(x; \alpha, \beta)} \propto (\alpha - 1)\log{x} + (\beta - 1) \log{(1 - x)}\]
distributions.binomial(k, n, p)

Binomial distribution log-likelihood.

Parameters:
  • kint, np.array. Number of successes. \(k <= n\)
  • nint, np.array. Number of trials. \(n > 0\)
  • pint, float, np.array. Success probability. \(0<= p <= 1\)
\[\log{P(k; n, p)} \propto k \log{p} + (n-k)\log{(1-p)}\]
distributions.cauchy(x, alpha=0, beta=1)

Cauchy distribution log-likelihood.

Parameters:
  • xint, float, np.array. \(-\infty < x < \infty\)
  • alphaint, float, nparray. Location parameter, \(-\infty < \alpha < \infty\)
  • betaint, float. Scale parameter, \(\beta > 0\)
\[\log{P(x; \alpha, \beta)} \propto -\log{\beta} - \log{\left[1 + \left(\frac{x - \alpha}{\beta}\right)^2\right]} \]
distributions.discrete_uniform(x, lower=0, upper=1)

Discrete Uniform distribution log-likelihood.

Parameters:
  • xint, np.array[int].
  • lower – (optional) int, float. Lower bound, default is 0.
  • upper – (optional) int, float. Upper bound, default is 1.
\[\log{P(x; a, b)} = -n\log(b-a)\]
distributions.exponential(x, rate=1)

Log likelihood of the exponential distribution.

Parameters:
  • xint, float, np.array.
  • rate – (optional) int, float, np.array. Rate parameter, \(\lambda > 0\). Defaults to 1.
\[\log{P(x; \lambda)} \propto \log{\lambda} - \lambda x\]
distributions.fails_constraints(*conditions)

Utility function for catching out of bound parameters. Returns True if any of the conditions aren’t met. Typically you’ll use this at the beginning of defining the log P(X) functions. Example

def logp(x, y):
    # Bound x and y to be greater than 0
    if outofbounds(x > 0, y > 0):
        return -np.inf
distributions.half_cauchy(x, alpha=0, beta=1)

Half-Cauchy distribution log-likelihood (positive half).

Parameters:
  • xint, float, np.array. \(-\infty < x < \infty\)
  • alphaint, float, nparray. Location parameter, \(-\infty < \alpha < \infty\)
  • betaint, float. Scale parameter, \(\beta > 0\)
\[\log{P(x; \alpha, \beta)} \propto -\log{\beta} - \log{\left[1 + \left(\frac{x - \alpha}{\beta}\right)^2\right]} \]
distributions.laplace(x, mu, tau)

Laplace distribution log-likelihood

Parameters:
  • xint, float, np.array. \(-\infty < \mu < \infty\)
  • muint, float, np.array. Location parameter. \(-\infty < \mu < \infty\)
  • tauint, float. Scale parameter, \(\tau > 0\)
\[\log{P(x; \mu, \tau)} \propto \log{\tau/2} - \tau \left|x - \mu \right|\]
distributions.normal(x, mu=0, sig=1)

Normal distribution log-likelihood.

Parameters:
  • xint, float, np.array.
  • mu – (optional) int, float, np.array. Location parameter of the normal distribution. Defaults to 0.
  • sig – (optional) int, float. Standard deviation of the normal distribution, \(\sigma > 0\). Defaults to 1.
\[\log{P(x; \mu, \sigma)} \propto -\log{\sigma} - \frac{(x - \mu)^2}{2 \sigma^2}\]
distributions.poisson(x, rate=1)

Poisson distribution log-likelihood.

Parameters:
  • xint, float, np.array. Event count.
  • rate – (optional) int, float, np.array. Rate parameter, \(\lambda > 0\). Defaults to 1.
\[\log{P(x; \lambda)} \propto x \log{\lambda} - \lambda\]
distributions.student_t(x, nu=1)

Student’s t log-likelihood

Parameters:
  • xint, float, np.array.
  • nu – (optional) int. Degress of freedom.
\[\log{P(x; \nu)} \propto \log{\Gamma \left(\frac{\nu+1}{2} \right)} - \log{\Gamma \left( \frac{\nu}{2} \right) } - \frac{1}{2}\log{\nu} - \frac{\nu+1}{2}\log{\left(1 + \frac{x^2}{\nu} \right)}\]
distributions.uniform(x, lower=0, upper=1)

Uniform distribution log-likelihood. Bounds are inclusive.

Parameters:
  • xint, float, np.array.
  • lower – (optional) int, float. Lower bound, default is 0.
  • upper – (optional) int, float. Upper bound, default is 1.
\[\log{P(x; a, b)} = -n\log(b-a)\]
distributions.weibull(x, l, k)

Weibull distribution log-likelihood.

Parameters:
  • xint, float, np.array. \(x > 0\)
  • lfloat. Scale parameter. \(\lambda > 0\)
  • kfloat. Shape parameter. \(k > 0\)