Tutorials › Response-Adaptive Randomization › Response Coding Errors That Change Posterior Allocations

Thompson Sampling for Trials · Tutorial 276 of 501

Response Coding Errors That Change Posterior Allocations

Trace three common response-coding errors from the recorded data through the Beta posterior to the resulting probability of assigning each treatment.

Advanced 11 min read

What You'll Learn

  • Derive the Beta update from arm-specific success and failure counts.
  • Identify how reversing the success code changes posterior parameters and allocation probabilities.
  • Check that each arm’s denominator matches its recorded successes and failures.
  • Diagnose the allocation distortion caused by swapped treatment labels.
  • Apply practical data and posterior checks to detect coding errors before allocation.

Why Response Coding Matters

Thompson sampling uses recorded outcomes to update uncertainty about each treatment arm, then uses those posteriors to determine assignment probabilities. A response coded as a success rather than a failure changes the posterior; a response attached to the wrong arm changes which treatment that posterior describes. The allocation probabilities can therefore be calculated correctly from incorrect data and still be wrong.

This tutorial focuses on three errors in a Bernoulli-response setting: reversing the success definition, using an inconsistent denominator, and assigning outcomes to incorrect arm labels. The arithmetic is simple, but the consequences can be substantial when the resulting posterior is used repeatedly for sequential assignments.

Model and Correct Beta Update

Suppose participant \(i\) is assigned to arm \(k\), and let \(Y_{ik}=1\) denote a response defined in advance as a success and \(Y_{ik}=0\) a failure. Conditional on assignment to arm \(k\), assume the outcomes are independent Bernoulli observations with a stable success probability \(\theta_k\). Put an independent Beta prior on each arm’s probability, \(\theta_k\sim\operatorname{Beta}(a_k,b_k)\), where \(a_k,b_k>0\). This model treats a success as desirable only if that is how the clinical response has been defined; for an adverse event, the coding and clinical utility may instead make absence of the event the success of interest.

For arm \(k\), let \(s_k\) be its number of recorded successes, \(f_k\) its number of recorded failures, and \(n_k\) its number of participants with outcomes included in this Bernoulli update. Thus \(n_k=s_k+f_k\). The likelihood and posterior are:

$$ L(\theta_k)\propto \theta_k^{s_k}(1-\theta_k)^{f_k}, \qquad \theta_k\mid\text{data}\sim \operatorname{Beta}(a_k+s_k,\ b_k+f_k). $$

The first posterior parameter accumulates successes; the second accumulates failures. In a two-arm comparison with independent, continuous posteriors and no minimum allocation floor, the probability that arm \(A\) is best is \(P(\theta_A>\theta_B\mid\text{data})\), and the probability for arm \(B\) is its complement. With more arms, each arm’s posterior best probability is the probability its \(\theta_k\) exceeds every other arm’s value. These probabilities are the quantities that guide Thompson allocation; they are not the observed success proportions.

Definition: A response-coding error is an error in how an observed outcome is represented as a success or failure, how it is counted in an arm’s denominator, or which treatment arm it is assigned to. Such an error changes the sufficient counts \(s_k\) and \(f_k\), and therefore can change the arm-specific posterior and its allocation probability.

How the Three Errors Propagate

The propagation has a clear sequence: recorded outcome, arm-specific counts, posterior parameters, and finally posterior best probabilities. Checking only the final allocation probabilities does not reveal whether the input counts were valid. The checks must start with the outcome definition and data record.

1
Define the response.
Specify what observed event is coded \(1\) and what is coded \(0\), including the follow-up window and rules for missing or indeterminate outcomes.
2
Reconcile counts by arm.
For every arm, verify that \(s_k+f_k=n_k\), where \(n_k\) counts exactly the outcomes eligible for this update.
3
Check arm identity.
Confirm that each outcome is attached to the treatment actually assigned, using a stable arm code rather than a potentially ambiguous display label.
4
Recompute and compare.
Recreate posterior parameters from validated counts, then verify that the reported allocation probabilities agree with those posteriors and the allocation rule.

Worked Examples

Worked Example: Reversing the Success Code

Suppose two arms have independent \(\operatorname{Beta}(1,1)\) priors. One participant assigned to arm \(A\) has the prespecified favorable outcome; arm \(B\) has no recorded outcomes yet. The correct coding is one success and zero failures for \(A\), so its posterior is \(\operatorname{Beta}(2,1)\), while \(B\)'s posterior remains \(\operatorname{Beta}(1,1)\).

For two arms with no ties, the allocation probability for \(A\) is \(P(\theta_A>\theta_B\mid\text{data})\). Since \(\theta_B\) is uniform on \((0,1)\), conditional on \(\theta_A=x\), the probability that \(\theta_B<x\) is \(x\). Therefore:

$$ P(\theta_A>\theta_B\mid\text{data}) =E(\theta_A\mid\text{data}) =\frac{2}{2+1} =\frac{2}{3} \approx 0.667. $$

Now suppose the favorable outcome is mistakenly coded as a failure. The erroneous posterior for \(A\) is \(\operatorname{Beta}(1,2)\), not \(\operatorname{Beta}(2,1)\). Its mean is \(1/(1+2)=1/3\), so the erroneous allocation probability for \(A\) is \(0.333\). The error changes the probability from about \(0.667\) to \(0.333\), reversing which arm is more likely to be best under these posteriors.

The model check is straightforward: the example assumes a binary response, a stable arm-specific probability, and a favorable event defined consistently before the outcome is observed. If the recorded variable is actually an adverse event, calling its occurrence a “success” without changing the clinical interpretation would be a coding-definition error even if the data processing were internally consistent.

Worked Example: An Inconsistent Denominator

Suppose arm \(A\) has four participants with observed outcomes: three successes and one failure. Arm \(B\) has no outcomes and a \(\operatorname{Beta}(1,1)\) posterior. With a \(\operatorname{Beta}(1,1)\) prior for \(A\), the correct count identity is \(n_A=s_A+f_A=3+1=4\), giving:

$$ \theta_A\mid\text{data}\sim\operatorname{Beta}(1+3,1+1) =\operatorname{Beta}(4,2). $$

Because \(B\)'s posterior is uniform, the probability that \(A\) is best equals the mean of \(A\)'s posterior:

$$ P(\theta_A>\theta_B\mid\text{data}) =E(\theta_A\mid\text{data}) =\frac{4}{4+2} =\frac{2}{3} \approx 0.667. $$

Imagine a processing error that uses a denominator of five while retaining the three successes. If the program derives failures as denominator minus successes, it creates two failures and reports \(\operatorname{Beta}(4,3)\). The resulting probability is \(4/(4+3)=4/7\approx0.571\), not \(0.667\). The extra failure has no corresponding observed participant.

The relevant check is not merely that the observed success proportion seems plausible. Reconcile the denominator against participant-level records eligible for the update and verify \(n_A=s_A+f_A\). If an outcome is missing, it should not automatically be counted as a failure; it must be handled under the trial’s stated missing-outcome rule or a suitable model, rather than inserted to make counts add up.

Worked Example: Swapping Arm Labels

Consider two arms with independent \(\operatorname{Beta}(1,1)\) priors. One participant assigned to \(A\) has a success, and one participant assigned to \(B\) has a failure. The correct posteriors are \(\theta_A\mid\text{data}\sim\operatorname{Beta}(2,1)\) and \(\theta_B\mid\text{data}\sim\operatorname{Beta}(1,2)\).

The density of a \(\operatorname{Beta}(2,1)\) variable at \(x\) is \(2x\), and the density of a \(\operatorname{Beta}(1,2)\) variable at \(y\) is \(2(1-y)\). Thus the correct posterior best probability for \(A\) is:

$$ \begin{aligned} P(\theta_A>\theta_B\mid\text{data}) &=\int_0^1 2x\left[\int_0^x 2(1-y)\,dy\right]dx\\ &=\int_0^1 2x(2x-x^2)\,dx\\ &=\frac{4}{3}-\frac{1}{2} =\frac{5}{6} \approx 0.833. \end{aligned} $$

If the arm labels on the records are swapped, the posteriors assigned to the labels become \(\operatorname{Beta}(1,2)\) for \(A\) and \(\operatorname{Beta}(2,1)\) for \(B\). The probability that \(A\) is best is then \(1/6\approx0.167\). The label error has turned a high posterior probability for \(A\) into a low one, even though the two underlying records are unchanged.

The example assumes that each observed response is correctly recorded and only its arm identity is wrong. In practice, verify assignment against the randomization record using a stable treatment identifier; a display name alone may be mistyped, changed, or mapped inconsistently.

Derivation and Diagnostic Checks

The update formula follows directly from the model. A \(\operatorname{Beta}(a_k,b_k)\) prior has density proportional to \(\theta_k^{a_k-1}(1-\theta_k)^{b_k-1}\). Multiplying this prior kernel by the Bernoulli likelihood gives:

$$ \theta_k^{a_k-1}(1-\theta_k)^{b_k-1} \theta_k^{s_k}(1-\theta_k)^{f_k} = \theta_k^{a_k+s_k-1}(1-\theta_k)^{b_k+f_k-1}. $$

This is the kernel of a \(\operatorname{Beta}(a_k+s_k,b_k+f_k)\) density. Consequently, reversing success and failure changes the posterior from \(\operatorname{Beta}(a_k+s_k,b_k+f_k)\) to \(\operatorname{Beta}(a_k+f_k,b_k+s_k)\). Relabeling arms does not change the outcome likelihood itself, but it attaches the resulting posterior to a different treatment.

Useful checks include reviewing the response definition alongside the data dictionary, tabulating successes and failures by assigned arm, and comparing the arm-level totals with the participant records. After recomputing the posteriors, confirm that allocation probabilities are between zero and one and sum to one when the rule allocates among all arms without floors or other modifications. A sum of one is necessary but not sufficient: swapped labels can preserve the sum while assigning each probability to the wrong treatment.

A second independent implementation can help detect errors in the update or probability calculation, but it cannot correct a shared error in the definition or source data. It should therefore be paired with record-level checks. Unexpected changes in an allocation probability after a small data update can prompt an audit, but are not proof of miscoding; posterior uncertainty and the allocation rule can also produce substantial changes.

Comparison, Limitations, and Common Pitfall

The main alternatives are not competing posterior formulas; they are different ways to prevent or detect bad inputs. A data-validation rule can reject records with an invalid response code, a count reconciliation can flag inconsistent denominators, and a blinded record audit can check arm identities against assignment logs. Recomputing from validated participant-level data is stronger than relying on a manually entered count table. No one check covers all three errors.

These checks do not establish that the Bernoulli model is appropriate. The update assumes a binary outcome and a common probability within each arm for the observations being combined. If the response definition, follow-up period, or population changes, or if outcomes are not adequately represented by the assumed model, a correct count audit alone does not resolve the modeling issue. Nor does a posterior best probability by itself establish a clinical benefit; it is conditional on the model, prior, response definition, and observed data.

  • Wrong reasoning: “The database has a success column, so every value of 1 must be a favorable outcome.”
  • Correction: Check the explicit event represented by 1 against the prespecified response definition. A numeric code does not determine its clinical meaning.
  • Wrong reasoning: “The allocation probabilities sum to one, so the arm coding must be right.”
  • Correction: Normalization checks arithmetic among probabilities, not whether the outcomes or arm labels were recorded correctly.
Key takeaway: Before interpreting a Thompson allocation probability, validate the event coded as success, reconcile each arm’s successes and failures with its eligible denominator, and verify that every outcome is attached to the assigned arm. The Beta update is only as trustworthy as those inputs.

Check Your Understanding

Use the stated Beta-Bernoulli model for each question. Brief solutions follow.

  1. With a \(\operatorname{Beta}(2,3)\) prior, an arm has five successes and two failures. What is its posterior?
  2. With independent uniform priors, arm \(A\) has no observations and arm \(B\) has one correctly coded success. What is \(P(\theta_A>\theta_B\mid\text{data})\)?
  3. An arm has six eligible participants, four successes, and one failure recorded. What reconciliation issue must be resolved before updating?
  4. Two arms have independent uniform priors. One success is assigned to \(A\), and one failure to \(B\). If labels are swapped, what happens to \(P(\theta_A>\theta_B\mid\text{data})\)?
  5. Why does a sum of allocation probabilities equal to one not rule out a response-coding error?

Solutions. (1) Add successes to the first parameter and failures to the second: \(\operatorname{Beta}(2+5,3+2)=\operatorname{Beta}(7,5)\). (2) Arm \(A\)'s posterior is uniform; \(B\)'s is \(\operatorname{Beta}(2,1)\). By symmetry, \(P(\theta_A>\theta_B)=1-E(\theta_B)=1-2/3=1/3\). (3) The counts total five, not six. Identify the sixth participant’s outcome status and apply the prespecified missing-outcome handling rule; do not silently add a failure. (4) The correct probability is \(5/6\); swapping labels makes it \(1/6\). (5) Probabilities can be normalized correctly from incorrect counts or incorrect arm identities, so their sum checks normalization, not data validity.

Clinical Biostats