Tutorials › Bayesian Clinical Trials › Numerical Evaluation Of Conjugate Posterior Summaries

Posterior Computation · Tutorial 116 of 696

Numerical Evaluation Of Conjugate Posterior Summaries

You will learn to evaluate conjugate posterior tail probabilities and quantiles accurately, and to recognize numerical results that are unreliable.

Intermediate 12 min read

What You'll Learn

  • Express beta and gamma posterior tails using regularized incomplete functions.
  • Distinguish lower-tail and upper-tail probabilities without unstable subtraction.
  • Use log-gamma functions to evaluate beta and gamma normalizing constants.
  • Find posterior quantiles by numerical inversion and verify the result.
  • Diagnose overflow, underflow, and tail-orientation errors in clinical-trial calculations.
  • Compare deterministic numerical routines with direct integration and simulation.

Why Numerical Evaluation Matters

Conjugate updating can produce a simple posterior distribution, but a useful trial summary may still require numerical work. A posterior probability such as \(P(p>p_0\mid \text{data})\), or a quantile defining a credible interval, is usually not obtained by substituting into the posterior mean and variance. It requires evaluating a distribution function or inverting one. When posterior parameters are large, calculating normalizing constants directly can overflow or underflow even though the desired probability is an ordinary number between zero and one.

This tutorial focuses on numerical evaluation once the conjugate posterior has been identified. The model and data must still justify that posterior: for example, a beta posterior presumes a binomial sampling model and beta prior, while a gamma posterior for a Poisson rate uses a gamma prior and a Poisson likelihood with a consistent exposure definition. Numerical accuracy cannot repair an inappropriate likelihood, prior, or parameterization.

Distribution Functions and Stable Formulas

Definition: For \(a,b>0\), the regularized incomplete beta function is \(I_x(a,b)=\frac{1}{B(a,b)}\int_0^x t^{a-1}(1-t)^{b-1}\,dt\), for \(0\le x\le1\). It is the cumulative distribution function of a \(\operatorname{Beta}(a,b)\) random variable. Its upper-tail counterpart is \(1-I_x(a,b)=I_{1-x}(b,a)\). For \(a>0\), the regularized lower incomplete gamma function is \(P(a,x)=\frac{1}{\Gamma(a)}\int_0^x t^{a-1}e^{-t}\,dt\), for \(x\ge0\), and the upper-tail function is \(Q(a,x)=1-P(a,x)\).

A beta posterior for a response probability \(p\) has parameters \(a,b\), with \(p\) dimensionless and restricted to \([0,1]\). Its lower-tail probability at a threshold \(p_0\) is \(I_{p_0}(a,b)\); its upper tail is \(I_{1-p_0}(b,a)\). For a gamma posterior on a rate \(\lambda\), use the shape-rate parameterization: \(\operatorname{Gamma}(a,r)\) has density proportional to \(\lambda^{a-1}e^{-r\lambda}\), where \(r\) is the rate parameter of the gamma distribution. The probability \(P(\lambda>\lambda_0)\) is \(Q(a,r\lambda_0)\). If \(\lambda\) is an event rate per patient-year, for instance, \(\lambda_0\) must use the same units.

The beta and gamma normalizing constants involve gamma functions. Rather than calculating factorials or gamma functions on their original scale, calculate their logarithms:

$$ \log B(a,b)=\log\Gamma(a)+\log\Gamma(b)-\log\Gamma(a+b), \qquad \log\Gamma(a)=\operatorname{lgamma}(a). $$

A log-gamma routine returns the logarithm of \(\Gamma(a)\) directly, avoiding an intermediate value that may be too large to represent. When evaluating a beta density at \(x\), for example, its log density is \((a-1)\log x+(b-1)\log(1-x)-\log B(a,b)\). This is safer than separately calculating \(B(a,b)\), its reciprocal, and the powers in the density.

Tail Orientation: A Short Derivation

Tail orientation deserves explicit checking because a correct numerical value attached to the wrong tail reverses the clinical interpretation. For positive integers \(a,b\), let \(Z\sim\operatorname{Binomial}(a+b-1,x)\). The beta-binomial identity is

$$ I_x(a,b)=P(Z\ge a),\qquad 1-I_x(a,b)=P(Z\le a-1). $$

One justification is to differentiate the finite binomial sum \(S(x)=\sum_{j=a}^{a+b-1}{a+b-1\choose j}x^j(1-x)^{a+b-1-j}\). Differentiating and cancelling adjacent terms gives \(S'(x)=x^{a-1}(1-x)^{b-1}/B(a,b)\). Also, \(S(0)=0=I_0(a,b)\). The two functions therefore have the same derivative and the same value at zero, so they are equal. The complementary identity follows by taking one minus each side. In particular, the binomial lower tail through \(a-1\) is the beta *upper* tail, not the beta lower tail.

Worked Example: Posterior Probability That a Response Rate Exceeds One Half

Suppose a hypothetical trial observes 10 responses among 16 participants in one arm. Under a \(\operatorname{Beta}(2,2)\) prior and a binomial likelihood, the posterior is \(\operatorname{Beta}(12,8)\). Find the posterior probability that the response probability \(p\) exceeds 0.5.

The required quantity is the upper tail: \(P(p>0.5\mid\text{data})=1-I_{0.5}(12,8)=I_{0.5}(8,12)\). The integer-parameter identity gives \(I_{0.5}(12,8)=P(Z\ge12)\) for \(Z\sim\operatorname{Binomial}(19,0.5)\). By symmetry, \(P(Z\ge12)=P(Z\le7)\). The latter sum is

$$ P(Z\le7)=\frac{\sum_{j=0}^{7}{19\choose j}}{2^{19}} =\frac{1+19+171+969+3876+11628+27132+50388}{524288} =\frac{94184}{524288}\approx0.1796. $$

This is the beta lower tail at 0.5. Therefore the requested posterior upper-tail probability is its complement: \(1-0.1796=0.8204\), more precisely about 0.8203583. In context, given this hypothetical model and data, the posterior probability that the response rate exceeds 50% is about 82.04%. The value 0.1796 would instead be the posterior probability that \(p\le0.5\). This distinction is essential: confusing \(I_x(a,b)\) with \(1-I_x(a,b)\) changes the conclusion.

Posterior Quantiles by Numerical Inversion

A \(q\)-quantile \(x_q\) of a continuous posterior distribution is a value whose cumulative probability is \(q\). For a beta posterior it solves \(I_{x_q}(a,b)=q\); for a gamma posterior it solves \(P(a,r x_q)=q\) under the shape-rate parameterization. Because each cumulative distribution function is continuous and strictly increasing over its support when the parameters are positive, the solution is unique for \(0<q<1\).

A standard quantile routine evaluates this inverse directly. Conceptually, it brackets the root and repeatedly narrows the interval until the cumulative probability at the returned value is sufficiently close to \(q\). Bisection is reliable because the function is monotone; safeguarded Newton methods can be faster but must retain a valid bracket. A returned quantile should be checked by evaluating the CDF at it, and, where practical, at nearby values on either side. Rounding the quantile before checking can make the displayed CDF appear less accurate than the internal result.

Worked Example: A Beta Posterior Ninetieth Percentile

Consider a hypothetical posterior \(\operatorname{Beta}(2,2)\). Its CDF is \(I_x(2,2)=3x^2-2x^3\). Find the 90th percentile.

Solve \(3x^2-2x^3=0.90\) on \([0,1]\). A numerical inversion routine gives \(x_{0.90}\approx0.8042\). Substitution checks the result:

$$ 3(0.8042)^2-2(0.8042)^3 =3(0.64673764)-2(0.52010641) \approx0.9000001. $$

Thus the posterior 90th percentile is approximately 0.8042. In an application where \(p\) is a response probability, this says that 90% of the posterior distribution lies at or below a response probability of about 0.8042. Under the stated Bayesian model and data, the posterior probability that \(p\le0.8042\) is approximately 0.90; this is a posterior probability, not a frequentist long-run coverage statement.

Gamma Upper Tails in Event-Rate Analyses

For a gamma posterior, the CDF uses the dimensionless argument \(r\lambda_0\), not \(\lambda_0\) alone. This product is important both mathematically and for units: if \(r\) is in patient-years and \(\lambda_0\) is events per patient-year, \(r\lambda_0\) is dimensionless. Many numerical libraries provide separate lower- and upper-regularized gamma functions and their inverse quantiles. Requesting the upper tail directly is preferable to calculating \(1-P(a,x)\) when \(P(a,x)\) is extremely close to one.

Worked Example: Posterior Probability of a High Event Rate

Suppose, hypothetically, that a Poisson event count is modeled with a \(\operatorname{Gamma}(2,1)\) prior on the rate \(\lambda\), using shape-rate notation. Two events are observed over one patient-year of exposure. The likelihood contributes \(\lambda^2e^{-\lambda}\), so the posterior is \(\operatorname{Gamma}(4,2)\). Calculate \(P(\lambda>3\mid\text{data})\), where rates are measured in events per patient-year.

Here \(a=4\), \(r=2\), and \(r\lambda_0=2(3)=6\), so the desired probability is \(Q(4,6)\). For integer shape 4, the gamma upper tail has the finite expression \(Q(4,x)=e^{-x}\sum_{k=0}^{3}x^k/k!\). Therefore,

$$ P(\lambda>3\mid\text{data}) =Q(4,6) =e^{-6}\left(1+6+\frac{6^2}{2!}+\frac{6^3}{3!}\right) =61e^{-6} \approx61(0.00247875) \approx0.1512. $$

Under this hypothetical model and data, the posterior probability that the event rate exceeds 3 events per patient-year is about 15.12%. The complement is the probability that the rate is at or below that threshold, about 84.88%. A routine that returns the upper tail directly can evaluate the first quantity without subtracting two nearly equal numbers.

Numerical Practice, Diagnostics, and Alternatives

A numerical routine is not a substitute for checking its inputs or output. Before interpreting a tail probability or quantile, verify that the posterior shape parameters are positive, that thresholds lie within the appropriate support, and that gamma rate and exposure units agree. Check whether the requested event is \(<\), \(\le\), \(>\), or \(\ge\); for continuous beta and gamma distributions, equality at a single threshold has probability zero, but the direction of the tail still matters.

  • Check bounds. A probability should be between 0 and 1; a beta quantile should lie in \([0,1]\), and a gamma quantile should be nonnegative.
  • Check complements. Lower and upper tails at the same threshold should sum to 1 within numerical tolerance. If one tail is tiny, request it directly rather than relying on subtraction.
  • Check inversion. For a reported \(q\)-quantile \(x_q\), evaluate the appropriate CDF at \(x_q\) and confirm it is close to \(q\). Monotonicity also implies that values just below and above the quantile should have CDFs below and above \(q\).
  • Check scales. Logarithms, exponentials, and rate-times-threshold products must use valid arguments and consistent units. A gamma calculation with the wrong rate parameterization can be numerically precise but substantively wrong.

The main alternative to a library’s incomplete-beta or incomplete-gamma routines is numerical integration of the density. Adaptive quadrature can be useful for checking a small number of moderate cases, but it must handle endpoints and sharply concentrated densities accurately; integrating a density on its original scale can also underflow. A further alternative is Monte Carlo: draw posterior values and estimate a tail by the fraction exceeding the threshold, or estimate a quantile from the simulated draws. That method is flexible, but it introduces Monte Carlo error and is generally unnecessary for a one-dimensional conjugate posterior with reliable special-function routines. The introductory material on Monte Carlo error explains why simulation estimates fluctuate; here, deterministic CDF and quantile routines are typically more direct.

A common mistake is to calculate a probability called “the upper tail” by reading a software function’s name without checking its definition, or to use a lower-tail CDF where a complement is needed. Another is to evaluate \(\Gamma(a)\) or factorials directly and then encounter overflow. The correction is to write the event first, translate it into the correct lower- or upper-tail function, use log-gamma calculations for normalization, and verify the result using a complementary tail or an independent calculation when feasible. Numerical evaluation has limits: an accurate answer only describes the assumed posterior and does not establish that the trial model, prior, likelihood, or endpoint definition is appropriate.

Key takeaway: Evaluate beta and gamma posterior summaries with regularized special functions, use log-gamma methods for large normalizing constants, and verify tail orientation and quantile inversion before interpreting results.

Check Your Understanding

Answer each question, then compare with the solutions below.

  1. For \(p\sim\operatorname{Beta}(8,12)\), express \(P(p>0.5)\) using a regularized incomplete beta function. For integer parameters, identify the equivalent binomial event.
  2. A \(\operatorname{Gamma}(5,3)\) posterior uses shape-rate notation. Write \(P(\lambda>2)\) as a regularized gamma function and state its dimensionless argument.
  3. What equation defines the 0.95 quantile of a \(\operatorname{Beta}(a,b)\) posterior, and what simple numerical check should follow a computed quantile?
  4. Why is calculating \(1-F(x)\) potentially unreliable when \(F(x)\) is extremely close to one?

Solutions. (1) \(P(p>0.5)=I_{0.5}(12,8)\). By the integer-parameter identity, this is \(P(Z\le7)\) for \(Z\sim\operatorname{Binomial}(19,0.5)\). (2) \(P(\lambda>2)=Q(5,3\cdot2)=Q(5,6)\); the dimensionless argument is 6. (3) Solve \(I_{x_{0.95}}(a,b)=0.95\), then evaluate the CDF at the returned value and confirm it is close to 0.95, with nearby values bracketing the target. (4) Floating-point rounding may erase the small difference between a number near one and one, producing a zero or inaccurate tail. Use a direct upper-tail routine instead.

Clinical Biostats