Tutorials › Response-Adaptive Randomization › A Worked Gamma-Poisson Allocation Example

Thompson Sampling for Trials · Tutorial 266 of 501

A Worked Gamma-Poisson Allocation Example

Update Gamma event-rate posteriors from counts and exposures, and calculate the probability each treatment has the lowest rate.

Advanced 10 min read

What You'll Learn

  • Update a Gamma prior using a Poisson event count and its exposure.
  • Interpret the shape-rate parameterization and posterior event-rate units.
  • Derive a posterior best-treatment probability for independent Gamma posteriors.
  • Calculate and check exact Thompson-sampling probabilities for three treatments.
  • Distinguish the probability of having the lowest event rate from a plug-in decision.
  • Diagnose calculation and modeling failures in a Gamma-Poisson allocation.

From Event Counts to Allocation Probabilities

When a trial outcome is a count of events observed over follow-up, the count alone does not describe the evidence about a treatment’s event rate. Four events during 10 person-years and four events during 100 person-years imply different rates. A Gamma-Poisson model incorporates both the observed event count and its exposure, then gives a posterior distribution for each treatment’s rate. Thompson sampling uses those distributions to calculate the probability that each treatment has the best rate.

Throughout this tutorial, events are adverse outcomes, so a smaller event rate is preferred. The calculations would reverse the selection direction if larger rates represented better outcomes. All examples are hypothetical.

Definition: For treatment \(j\), let \(Y_j\) be the number of events observed over exposure \(E_j\), measured in person-years, and let \(\lambda_j\) be its event rate in events per person-year. The Gamma-Poisson model assumes \(Y_j\mid\lambda_j\sim\operatorname{Poisson}(E_j\lambda_j)\), with independent prior distributions \(\lambda_j\sim\operatorname{Gamma}(a_j,b_j)\). Here Gamma uses the shape-rate parameterization: its density is proportional to \(\lambda_j^{a_j-1}e^{-b_j\lambda_j}\), and its mean is \(a_j/b_j\). The rate parameter \(b_j\) has units of person-years.

This model assumes that, within each treatment, events follow a Poisson process with a constant rate over the observed exposure; exposure is known; and the prior distributions across treatments are independent. It also assumes that the event counts are recorded and classified correctly. These conditions can be questionable if rates change materially over time, outcomes are clustered, or the count variance is much larger than the Poisson model permits. The basic update is useful only if the model is a reasonable description of the data.

The Gamma-Poisson Update

For a single treatment, the likelihood of an observed count \(y\), given exposure \(E\) and rate \(\lambda\), is proportional to \(\lambda^y e^{-E\lambda}\). Multiplying by the Gamma prior density gives a posterior kernel proportional to \(\lambda^{a+y-1}e^{-(b+E)\lambda}\). Therefore, the posterior is again Gamma:

$$ \lambda\mid Y=y,E\sim\operatorname{Gamma}(a+y,b+E). $$

The posterior mean is \((a+y)/(b+E)\) events per person-year. The prior shape \(a\) is updated by the event count, while the prior rate parameter \(b\) is updated by exposure. Exposure does not increase the shape: it contributes to the posterior rate parameter.

Worked Example: Updating One Treatment’s Event Rate

Suppose a hypothetical treatment has prior \(\lambda\sim\operatorname{Gamma}(2,3)\), where the prior rate parameter is 3 person-years. In the trial, 4 events occur over 2 person-years. The Poisson model is taken to have a constant event rate over this exposure, and the exposure is known.

The posterior parameters are \(a+y=2+4=6\) and \(b+E=3+2=5\) person-years, so

$$ \lambda\mid Y=4,E=2\sim\operatorname{Gamma}(6,5), \qquad E(\lambda\mid Y=4,E=2)=\frac{6}{5}=1.20 \text{ events per person-year}. $$

The posterior mean is not the allocation probability. It summarizes one posterior distribution; a Thompson-sampling probability compares the full posterior distributions across treatments.

Best-Treatment Probabilities

For \(K\) treatments with independent posterior rates, define \(\pi_j\) as the posterior probability that treatment \(j\) has the smallest event rate. With continuous Gamma distributions, exact ties have probability zero. If each allocation is made by drawing one rate from each treatment’s posterior and choosing the treatment with the smallest draw, then the probability of allocating to treatment \(j\) is \(\pi_j\).

$$ \pi_j=P\left(\lambda_j<\lambda_\ell\text{ for every }\ell\ne j\mid\text{trial data}\right). $$

When all posterior shapes are 2, the calculation for three treatments has a convenient closed form. Write their posterior rate parameters as \(r_1,r_2,r_3\), so the posterior density for treatment \(j\) is \(r_j^2x e^{-r_jx}\), and its survival probability at \(x\) is \(e^{-r_jx}(1+r_jx)\). Conditioning on the value drawn for treatment \(j\), multiply its density by the probabilities that each other rate exceeds that value, then integrate:

$$ \begin{aligned} \pi_j &=\int_0^\infty r_j^2x e^{-r_jx} \prod_{\ell\ne j}\left[e^{-r_\ell x}(1+r_\ell x)\right]\,dx\\ &=r_j^2\int_0^\infty x e^{-Tx} \left(1+\sum_{\ell\ne j}r_\ell x+ \prod_{\ell\ne j}r_\ell x^2\right)\,dx\\ &=r_j^2\left[ \frac{1}{T^2} +\frac{2\sum_{\ell\ne j}r_\ell}{T^3} +\frac{6\prod_{\ell\ne j}r_\ell}{T^4} \right], \qquad T=r_1+r_2+r_3. \end{aligned} $$

The last equality follows from \(\int_0^\infty x^m e^{-Tx}\,dx=m!/T^{m+1}\) for \(T>0\), applied for \(m=1,2,3\). This result is specific to three independent Gamma posteriors with shape 2; it is not a general formula for arbitrary shapes or numbers of treatments.

Worked Example: Exact Allocation Probabilities for Three Treatments

Consider three hypothetical treatments with independent \(\operatorname{Gamma}(1,1)\) priors. Suppose their event counts and exposures are \(Y_1=Y_2=Y_3=1\), with exposures \(E_1=3\), \(E_2=1\), and \(E_3=2\) person-years. Assume each count is Poisson with its treatment’s constant rate over its known exposure. The posterior distributions are

$$ \lambda_1\mid\text{data}\sim\operatorname{Gamma}(2,4),\quad \lambda_2\mid\text{data}\sim\operatorname{Gamma}(2,2),\quad \lambda_3\mid\text{data}\sim\operatorname{Gamma}(2,3). $$

The posterior rate parameters sum to \(T=4+2+3=9\) person-years. For treatment 1, the exact calculation is

$$ \begin{aligned} \pi_1 &=4^2\left[\frac{1}{9^2}+\frac{2(2+3)}{9^3} +\frac{6(2)(3)}{9^4}\right]\\ &=16\left[\frac{81+90+36}{6561}\right] =\frac{3312}{6561}\approx 0.5048. \end{aligned} $$

The same formula gives

$$ \begin{aligned} \pi_2 &=2^2\left[\frac{1}{9^2}+\frac{2(4+3)}{9^3} +\frac{6(4)(3)}{9^4}\right] =\frac{1116}{6561}\approx 0.1701,\\ \pi_3 &=3^2\left[\frac{1}{9^2}+\frac{2(4+2)}{9^3} +\frac{6(4)(2)}{9^4}\right] =\frac{2133}{6561}\approx 0.3251. \end{aligned} $$

The exact fractions sum to \(6561/6561=1\), as required. Treatment 1 has the largest posterior probability of having the lowest event rate, about 0.5048, but it is not certain to be best. Under Thompson sampling, treatment 2 would still be selected with probability about 0.1701 and treatment 3 with probability about 0.3251. This is not the same as choosing the treatment with the smallest posterior mean every time.

A Two-Treatment Calculation With Unequal Shapes

The shape-2 formula is convenient, but posterior shapes need not match. For two independent posteriors, the best-treatment probability can always be expressed by integrating one posterior density against the other posterior’s survival function. In a simple unequal-shape case, that calculation is still exact.

Worked Example: Comparing Unequal Posterior Shapes

Suppose hypothetical treatment A has prior \(\operatorname{Gamma}(1,1)\), zero events, and 1 person-year of exposure. Treatment B has the same prior, one event, and 2 person-years of exposure. Assume known exposures, independent priors, and constant Poisson rates. The posteriors are \(\lambda_A\sim\operatorname{Gamma}(1,2)\) and \(\lambda_B\sim\operatorname{Gamma}(2,3)\).

A Gamma(1,2) variable has density \(2e^{-2x}\), and a Gamma(2,3) variable has survival probability \(e^{-3x}(1+3x)\). Thus the posterior probability that A has the lower event rate is

$$ \begin{aligned} P(\lambda_A<\lambda_B\mid\text{data}) &=\int_0^\infty 2e^{-2x}e^{-3x}(1+3x)\,dx\\ &=2\left(\frac{1}{5}+\frac{3}{25}\right) =\frac{16}{25}=0.6400. \end{aligned} $$

The integration uses \(\int_0^\infty e^{-5x}\,dx=1/5\) and \(\int_0^\infty xe^{-5x}\,dx=1/25\). Treatment A is therefore selected with probability 0.6400 under posterior sampling, and B with probability 0.3600. The larger allocation probability for A reflects uncertainty in both rates, not a claim that A’s event rate is known to be lower.

Computation, Diagnostics, and Limits

For general Gamma shapes, or for many treatments, posterior best probabilities can be estimated by simulation. On each iteration, independently draw one rate from each treatment’s posterior and record which draw is smallest. The estimated probability for treatment \(j\) is the fraction of iterations on which it has the smallest draw. A sufficiently large simulation makes Monte Carlo error small, but it does not correct a mistaken model, prior, exposure, or event count.

Several checks are especially useful:

  • Check the parameterization. In the shape-rate convention, posterior parameters are \(a+y\) and \(b+E\). A shape-scale implementation uses a different second parameter and can silently produce incorrect draws.
  • Check the exposure units. If exposure is entered in days but the prior rate parameter is in person-years, convert the exposure before updating.
  • Check that probabilities sum to one. Exact probabilities should sum to one; simulated estimates should be close, with small deviations attributable to Monte Carlo error.
  • Check the selection direction. For adverse events, select the smallest sampled rate. Selecting the largest reverses the intended clinical objective.
  • Check the Poisson assumption. Strong time variation, clustering, or excess variability can make a constant-rate Poisson model inadequate. Posterior precision can then be overstated.

Posterior best probability differs from both a plug-in greedy rule and predictive response probability. A greedy rule selects according to point summaries, such as the smallest posterior mean, and does not preserve the uncertainty-based randomization of Thompson sampling. A predictive response probability concerns the chance of an outcome for a future participant; it need not equal the probability that a treatment has the best underlying rate. The posterior probability here answers a specific question: which treatment has the smallest event rate, given the model and observed data?

Common pitfall: Treating the treatment with the smallest posterior mean as if its Thompson-sampling allocation probability were 1. This ignores posterior uncertainty. Thompson sampling draws from every treatment’s posterior and allocates according to how often each treatment has the best draw; even a treatment with a higher posterior mean can have a nonzero probability of the lowest rate.

These calculations rely on independent treatment posteriors and a specified clinical direction in which lower rates are better. They do not resolve whether the prior is appropriate or whether a rate alone captures clinical value. When posterior probabilities are computed numerically, compare estimates across larger simulation runs and, where possible, verify a small case with an exact calculation such as the examples above. A stable numerical answer is reassuring about computation, not proof that the model assumptions hold.

Check Your Understanding

Answer each question, then compare your reasoning with the solutions.

  1. A treatment has prior \(\operatorname{Gamma}(2,4)\), observes 3 events over 2 person-years, and is modeled with a constant Poisson rate. What is its posterior distribution and posterior mean?
  2. In the three-treatment example, why is treatment 1’s allocation probability not 1, even though it has the largest probability of the lowest rate?
  3. For a posterior \(\operatorname{Gamma}(1,2)\), what are the density and survival probability needed to compare it with another treatment?
  4. Name two data or model checks that should be made before trusting Gamma-Poisson allocation probabilities.

Solutions. (1) The posterior is \(\operatorname{Gamma}(2+3,4+2)=\operatorname{Gamma}(5,6)\), with mean \(5/6\approx0.8333\) events per person-year. (2) Posterior rates are uncertain; treatments 2 and 3 also have positive probabilities of having the smallest rate, so Thompson sampling allocates to each in proportion to its best-rate probability. (3) Its density is \(2e^{-2x}\) for \(x>0\), and its survival probability is \(e^{-2x}\). (4) Examples include confirming exposure and prior rate units match, verifying the shape-rate parameterization, checking event coding, and assessing whether the constant-rate Poisson assumption is plausible.

Clinical Biostats