Functions & Formulae

Each applied formula has its own function page, with a signature, implementations, and tests.

Beta distribution density, moments and fitting for an uncertain probability

f(p | alpha, beta) = Gamma(alpha + beta) / (Gamma(alpha) * Gamma(beta)) * p^(alpha - 1) * (1 - p)^(beta - 1), 0 < p < 1

Maps two positive shape parameters, alpha and beta, to a probability density on the unit interval that describes uncertainty about a probability, such as a transition probability in a decision tree or Markov model. The parameters are fitted from event counts, with alpha counting events and beta non-events, or from a reported mean and standard error by the method of moments, and the fitted distribution is sampled in probabilistic sensitivity analysis. The formulae below give the mean and variance, the two fitting routes, the gamma alternative for a utility decrement and the Dirichlet extension for states with three or more exits. Gamma(.) in the density is the gamma function.

  • Mean, variance and standard error of a beta-distributed probability

    E_p = alpha / (alpha + beta); V_p = alpha * beta / ((alpha + beta)^2 * (alpha + beta + 1)); SE_p = sqrt(V_p)

    Gives the expected value and the variance of a probability that follows Beta(alpha, beta), and its standard error as the square root of the variance. The mean depends only on the ratio of the two parameters, while their sum acts like a sample size: the larger alpha + beta, the narrower the distribution. The function sqrt is the square root.

  • Beta parameters for a probability from binomial event counts and a conjugate prior

    alpha = alpha_0 + r; beta = beta_0 + n - r; E_p = alpha / (alpha + beta)

    Updates a beta prior for a probability with r events among n patients at risk. Because the beta is the conjugate prior for a binomial probability, the result is again a beta distribution. With the limiting prior alpha_0 = beta_0 = 0 the rule reduces to alpha equal to the number of events and beta equal to the number of non-events, n minus r, so the mean is the observed proportion r/n. A uniform prior, alpha_0 = beta_0 = 1, adds one to each parameter.

  • Beta shape parameters from a reported mean and standard error by the method of moments

    alpha = mu * (mu * (1 - mu) / s^2 - 1); beta = (1 - mu) * (mu * (1 - mu) / s^2 - 1)

    Sets the beta mean and variance equal to a reported mean probability and the square of its standard error and solves for the two parameters. The sum alpha + beta equals mu (1 minus mu) divided by s squared, minus one, and alpha and beta are that sum multiplied by mu and by 1 minus mu. A solution exists only when s squared is below mu (1 minus mu).

  • Gamma shape and scale for a utility decrement fitted by moments

    mu_D = 1 - mu_U; k = mu_D^2 / s^2; theta = s^2 / mu_D

    Describes a mean utility as a decrement from full health, one minus the utility, and fits a gamma distribution to the decrement by matching its mean and standard error. The shape is the squared mean decrement over the squared standard error and the scale is the squared standard error over the mean decrement. Each sampled utility, one minus a gamma draw, is at most one with no lower limit, which suits states where a mean utility below zero is plausible.

  • Dirichlet transition probabilities from three normalised gamma draws

    p_1 = G_1 / (G_1 + G_2 + G_3); p_2 = G_2 / (G_1 + G_2 + G_3); p_3 = G_3 / (G_1 + G_2 + G_3)

    Samples one row of a transition matrix for a state with three exits by drawing an independent gamma variable for each destination, with shape equal to that destination's count plus any prior and a scale of one, and dividing each draw by their total. The result is a draw from a Dirichlet distribution, the multivariate generalisation of the beta, and the three probabilities add to one in every iteration. Each component has a beta marginal: the second probability from Dirichlet(90, 24, 6) follows Beta(24, 96).