Tutorials › Response-Adaptive Randomization › Coding Success and Failure Correctly

Urn Models · Tutorial 43 of 501

Coding Success and Failure Correctly

Use explicit two-arm urn states to verify play-the-winner updates and diagnose coding errors before they distort later allocations.

Intermediate 10 min read

What You'll Learn

  • Define success and failure relative to a prespecified binary outcome.
  • Update a two-arm urn correctly for each treatment-outcome combination.
  • Check how reversing outcome coding changes the urn state.
  • Detect treatment-label mismatches between assignment records and update code.
  • Audit a short allocation path with explicit states and transition checks.
  • Distinguish a coding error from a deliberate change to the randomization rule.

Why the Success–Failure Distinction Matters

In a play-the-winner urn, an observed outcome changes the urn that will determine later treatment assignments. A success reinforces the treatment that produced it; a failure reinforces the other treatment. Reversing either outcome coding or treatment labels therefore does more than misdescribe the past: it changes the probabilities used to assign future participants.

This tutorial focuses on a two-arm urn and on checking each update against the assigned treatment and the observed response. The update rule must be fixed before coding it. In particular, “success” must mean the prespecified favorable outcome, not whichever outcome happens to be represented by a convenient value in a data file.

Definition: Let the two treatments be \(A\) and \(B\). Immediately before participant \(t\) is assigned, write the urn state as \((a_t,b_t)\), where \(a_t\) and \(b_t\) are the numbers of \(A\)- and \(B\)-labelled balls. One ball is drawn at random, its treatment is assigned, and the drawn ball is returned. Let \(r\) be the positive integer reinforcement size. Under the two-arm play-the-winner rule, a success adds \(r\) balls of the assigned treatment, and a failure adds \(r\) balls of the other treatment.

The state records the urn composition, not the number of participants who have received each treatment and not the number of successes observed. For example, a state of \((5,3)\) means that five balls represent \(A\) and three represent \(B\). Given this state, the next assignment is \(A\) with probability \(5/8\) and \(B\) with probability \(3/8\). Those probabilities follow from drawing one ball uniformly from the urn.

The update rule assumes that the assigned treatment is recorded correctly, the binary response can be classified as success or failure under a prespecified definition, and the reinforcement size and update convention are applied consistently. It also assumes the drawn ball is returned before reinforcement. If a response is missing or cannot be classified, it is not automatically a failure: the protocol must specify how such cases are handled.

The Four Updates to Keep Separate

The safest audit is to ask two questions for every participant: Which treatment was assigned? and Was the observed outcome success or failure? Only then should the urn be changed. The resulting cases are:

Assigned treatmentOutcomeReinforcementState after update
ASuccessAdd \(r\) A balls\((a_t+r,b_t)\)
AFailureAdd \(r\) B balls\((a_t,b_t+r)\)
BSuccessAdd \(r\) B balls\((a_t,b_t+r)\)
BFailureAdd \(r\) A balls\((a_t+r,b_t)\)

A useful verbal check is that a success adds balls matching the assigned treatment, whereas a failure adds balls matching the other treatment. This is not the same as “success always adds \(A\)” or “failure always adds \(B\).” The treatment assigned determines which color corresponds to “same” and which to “other.”

Key rule: First identify the assigned arm. Then interpret the outcome relative to the prespecified success definition. A success reinforces the assigned arm; a failure reinforces its opposite. Do not determine the arm from the outcome code or determine success from the arm label.

Worked Example: Checking Both Outcomes After Assignment to A

Suppose the current urn state is \((5,3)\), the reinforcement size is \(r=2\), and the next ball drawn is labelled \(A\). Calculate the state after a success and after a failure.

The drawn \(A\) ball is returned, so before reinforcement the composition remains \((5,3)\). If the participant’s outcome is a success, add two \(A\) balls:

$$ (5,3)+(2,0)=(7,3). $$

If the outcome is a failure, add two balls of the other treatment, \(B\):

$$ (5,3)+(0,2)=(5,5). $$

The two possible updated states differ even though the assigned treatment is the same. A program that produces \((5,5)\) for the success case has applied the failure update; one that produces \((7,3)\) for the failure case has applied the success update.

A Compact Derivation for Auditing Updates

The four cases can be written as one rule, which is helpful when checking code that processes many records. Let \(Y_t=1\) denote success and \(Y_t=0\) denote failure, and let \(Z_t\) denote the treatment assigned to participant \(t\). Define \(\mathbf{1}\{\cdot\}\) to equal 1 when its condition is true and 0 otherwise. The state after the response is:

$$ a_{t+1}=a_t+r\mathbf{1}\{(Z_t=A,\;Y_t=1)\text{ or }(Z_t=B,\;Y_t=0)\}, $$
$$ b_{t+1}=b_t+r\mathbf{1}\{(Z_t=B,\;Y_t=1)\text{ or }(Z_t=A,\;Y_t=0)\}. $$

To justify the first equation, \(A\) balls are added in exactly two cases: a success on \(A\), because the assigned treatment is reinforced, or a failure on \(B\), because the other treatment is reinforced. The second equation follows by exchanging \(A\) and \(B\). In every case exactly one indicator is 1, so the total number of balls increases by \(r\):

$$ a_{t+1}+b_{t+1}=a_t+b_t+r. $$

This total-size check can reveal an implementation problem, such as adding reinforcement twice or failing to add any balls. It cannot by itself detect a reversed outcome code: a reversed update can still add exactly \(r\) balls. The arm-specific state must also be checked against the assignment and outcome.

Worked Example: Correcting a Reversed Outcome Code for B

Suppose the current state is \((4,6)\), \(r=2\), and a participant is assigned to \(B\). The response is a success, but a coding routine mistakenly treats success as failure. Find the correct and erroneous states.

For the correct update, success reinforces the assigned treatment, \(B\). Thus:

$$ (4,6)+(0,2)=(4,8). $$

The erroneous routine applies the failure rule for a participant assigned to \(B\), adding \(A\) balls instead:

$$ (4,6)+(2,0)=(6,6). $$

Both states contain 12 balls, so checking only the total would not catch the error. The decisive audit is that the record says \(Z_t=B\) and \(Y_t=1\), which maps to adding \(r\) to \(b_t\), not to \(a_t\). If the next draw is made from the erroneous state, its probability of assigning \(B\) is \(6/12=0.5\), rather than \(8/12=0.6667\) under the correct state.

Worked Path: Keeping Assignment and Outcome Labels Aligned

A common source of error is not the update formula itself but a mismatch among the treatment assigned, the response record, and the arm names used by the program. The next example follows a short hypothetical path, with \(r=1\), and makes every state explicit.

Worked Example: Auditing Three Consecutive Updates

Start with state \((2,1)\). Suppose the recorded sequence is: participant 1 is assigned \(A\) and fails; participant 2 is assigned \(B\) and succeeds; participant 3 is assigned \(A\) and succeeds. Find the state after each response.

Participant 1 is assigned \(A\) and fails, so add one \(B\) ball:

$$ (2,1)+(0,1)=(2,2). $$

Participant 2 is assigned \(B\) and succeeds, so add one \(B\) ball:

$$ (2,2)+(0,1)=(2,3). $$

Participant 3 is assigned \(A\) and succeeds, so add one \(A\) ball:

$$ (2,3)+(1,0)=(3,3). $$

The state sequence is therefore \((2,1)\), \((2,2)\), \((2,3)\), and \((3,3)\). For a further consistency check, the conditional probability of the recorded second assignment, \(B\), given the state \((2,2)\), is \(2/4=0.5\). The probability of the third assignment, \(A\), given state \((2,3)\), is \(2/5=0.4\). These probabilities do not prove that the records or updates are correct, but they confirm the state used for each draw produces the stated assignment probabilities.

If a data file stores “success” as 0 and “failure” as 1, that is not automatically wrong. It is wrong only if the update code assumes the opposite mapping. A separate danger is swapping the arm labels—for example, treating a database code of 1 as \(A\) in one step and as \(B\) in another. In the path above, if participant 2’s assignment were accidentally read as \(A\), the success update would add an \(A\) ball, producing \((3,2)\) rather than \((2,3)\). The total would still increase by one, so the assignment label must be audited directly.

Diagnostics, Alternatives, and Limits

A practical audit should preserve enough information to reconstruct each update: the state before the draw, the drawn ball or assigned treatment, the response as observed, the success/failure mapping, the reinforcement size, and the state after reinforcement. Compare the recorded post-update state with the applicable row of the four-case table. Investigate discrepancies rather than silently forcing the state to match.

  • Reversed response coding: verify what each stored response value means, then compare that mapping with the code’s success and failure branches.
  • Swapped arm labels: compare treatment names and codes across the assignment record, outcome record, and urn-state variables.
  • Wrong reinforcement size: check that exactly \(r\) balls are added, not \(r+1\), \(2r\), or a value from a different setting.
  • Missing or repeated update: verify that each eligible participant contributes one update and that an outcome is not processed twice.
  • Unexpected total: check whether the drawn ball was returned and whether reinforcement was added once. A changing total may be intentional under a different rule, but it is not the rule defined here.
Approach or implementationWhat it doesHow to distinguish it from the intended rule
Correct play-the-winner updateSuccess reinforces the assigned arm; failure reinforces the other arm.Check all four assignment-outcome cases against the state transition.
Reversed outcome codingApplies the success update to failures and the failure update to successes.Trace the stored response value to its documented meaning and then to the branch taken.
Swapped treatment labelsApplies the correct kind of update to the wrong arm.Reconcile treatment codes with the actual assigned treatment for each participant.
Fixed allocationUses prespecified assignment probabilities rather than changing them through response-based reinforcement.Its probabilities are not updated by these urn transitions.

The table distinguishes a coding mistake from a different allocation design. A fixed allocation rule is not an incorrectly coded urn; it simply does not use response-based reinforcement. Likewise, reversing which outcome is called success may be coherent only if the scientific endpoint and the entire update rule are deliberately redefined in advance. Silently changing the coding convention midway through a trial does not implement a consistent play-the-winner rule.

The urn update also does not establish that an outcome is clinically meaningful, that responses are measured without error, or that the adaptive design has desirable operating characteristics. Those are separate design and analysis questions. This tutorial’s checks verify only that the specified two-arm update is translated correctly into states. In particular, checking arithmetic cannot validate the prespecified definition of success or repair a treatment assignment that was recorded incorrectly.

Common mistake: “A success means add balls of the winning color, so I can add balls to whichever arm is coded as 1.” This reasoning confuses an outcome code with a treatment label. The correction is to read the assigned arm first, determine whether that participant succeeded, and then add reinforcement to the assigned arm for success or to the other arm for failure.

Check Your Understanding

For each question, use the rule that the drawn ball is returned and that success reinforces the assigned treatment.

  1. From state \((3,4)\), with \(r=2\), a participant assigned to \(A\) fails. What is the new state?
  2. From state \((6,2)\), with \(r=1\), a participant assigned to \(B\) succeeds. What is the new state, and what is the next-draw probability of \(B\)?
  3. A program processes an \(A\)-assigned success from state \((5,5)\), \(r=3\), and returns \((5,8)\). Identify the error and give the correct state.
  4. Why does verifying that the total urn size increases by \(r\) fail to detect every reversed-coding error?

Solutions. (1) Failure on \(A\) reinforces \(B\), so \((3,4)+(0,2)=(3,6)\). (2) Success on \(B\) reinforces \(B\), giving \((6,2)+(0,1)=(6,3)\); the next-draw probability of \(B\) is \(3/(6+3)=1/3\). (3) The returned state applies the failure update to an \(A\)-assigned participant. Success adds three \(A\) balls, so the correct state is \((5,5)+(3,0)=(8,5)\). (4) A reversed update still adds exactly \(r\) balls, so the total can be correct even while the reinforcement is placed in the wrong arm. Check the arm-specific state against both the recorded assignment and outcome.

Clinical Biostats