From an urn rule to an allocation algorithm
A generalized urn rule becomes an allocation algorithm only when its operations are specified in order. At each trial step, the current urn determines the probability of each treatment; a random draw assigns a treatment; the participant's response is observed and classified; and the corresponding replacement rule changes the urn before the next allocation. The timing matters: a response cannot affect the treatment assignment that produced it, but it can affect later assignments.
This tutorial describes the procedure using counts rather than a literal collection of balls. A count-vector implementation is equivalent to drawing uniformly from a physical urn when each ball represents one treatment and the algorithm uses the current counts to compute the draw probabilities.
A complete specification also needs an initial composition, a list of response categories, and a rule mapping each treatment-response pair to a net-change vector. The categories should be mutually exclusive and exhaustive for the outcomes the algorithm can encounter. If a response cannot be classified, the algorithm needs a prespecified handling rule rather than an improvised update.
The net-change representation accommodates both additions and removals. Given treatment \(i\) and response \(j\), the next state is \(U_{t+1}=U_t+r_{ij}\). The result must have nonnegative integer counts and a positive total before another allocation can be made. If the rule describes a sequence of physical removals and additions, that sequence must itself be feasible; an acceptable final count vector does not make an impossible intermediate operation valid.
The allocation algorithm, in order
The following procedure makes explicit which state is used for each decision. Here, \(J_t\) denotes the response category observed after assigning the \(t\)th participant. If outcomes are not yet available when the next participant is enrolled, the algorithm must specify how it handles that delay; the steps below assume the response is available before the next update and draw.
Set the starting counts \(U_1\). Check that each count is a nonnegative integer and \(S_1>0\). Specify the response categories and all replacement vectors before running the allocation procedure.
For each treatment \(i\), calculate \(u_{i,t}/S_t\). These probabilities are conditional on the current urn composition and sum to one.
Use a random draw with those probabilities and record the assigned treatment \(A_t\). Do not update the urn using a response that has not yet been observed.
Record the response and map it to exactly one prespecified category \(J_t\). The allocation rule does not itself specify how responses are generated.
Look up \(r_{A_t,J_t}\), calculate \(U_{t+1}=U_t+r_{A_t,J_t}\), and check count, feasibility, and total-size conditions before proceeding.
Save the pre-draw composition, probabilities, assignment, response, and post-update composition. Use \(U_{t+1}\), not \(U_t\), to calculate the next participant's probabilities.
A useful implementation keeps the randomization step separate from the update step. For example, the draw can be made by generating a uniform random number and dividing the interval from zero to one into consecutive segments with lengths \(u_{1,t}/S_t,\ldots,u_{K,t}/S_t\). The treatment corresponding to the segment containing the random number is selected. The particular software routine may differ, but the probabilities must match the current count vector.
Worked examples
Worked Example: Updating After a Binary Response
Suppose a hypothetical two-treatment urn starts at \(U_1=(3,2)\), where the coordinates refer to treatments A and B. The response categories are success and failure. For this example, the rule adds the following balls after the drawn ball is returned:
- A success: \(r_{A,\mathrm{success}}=(2,0)\); A failure: \(r_{A,\mathrm{failure}}=(0,1)\).
- B success: \(r_{B,\mathrm{success}}=(0,2)\); B failure: \(r_{B,\mathrm{failure}}=(1,0)\).
Initially, \(S_1=3+2=5\), so \(P(A_1=A\mid U_1)=3/5=0.6000\) and \(P(A_1=B\mid U_1)=2/5=0.4000\). Suppose the random draw assigns A and the participant has a success. The applicable vector is \(r_{A,\mathrm{success}}=(2,0)\), so
The next allocation probabilities are \(P(A_2=A\mid U_2)=5/7=0.7143\) and \(P(A_2=B\mid U_2)=2/7=0.2857\). These are the probabilities for the next draw, not a retrospective probability for the participant already assigned. The example specifies an allocation mechanism; it does not assert that receiving A causes success.
Worked Example: Applying a Removal Rule
Consider a hypothetical three-treatment urn with composition \(U_t=(4,3,2)\), for treatments A, B, and C. Suppose the rule for assigning B and observing an intermediate response is \(r_{B,\mathrm{intermediate}}=(1,0,-1)\): add one A ball and remove one C ball. The total is \(S_t=4+3+2=9\), so the probability of assigning B on this draw is \(3/9=0.3333\).
Suppose B is selected and the observed response is intermediate. Applying the specified vector gives
All updated counts are nonnegative integers and \(S_{t+1}=5+3+1=9>0\), so another draw is possible. The next probabilities are \(5/9=0.5556\) for A, \(3/9=0.3333\) for B, and \(1/9=0.1111\) for C. The removal was feasible because there were two C balls before it. A full implementation of this rule must also check feasibility whenever the same response-treatment pair occurs later; it cannot assume that one successful update guarantees every future removal is possible.
Worked Example: Tracing Several Assignments
For a hypothetical two-treatment urn, start at \(U_1=(2,2)\). Use this binary-response rule: after a success, add one ball of the assigned treatment; after a failure, add one ball of the other treatment. Suppose the observed sequence generated during one run is: A with success, B with failure, then A with failure. The response sequence is specified as hypothetical input to the algorithm, not as data from a real trial.
At the first draw, the probabilities are \(2/4=0.5000\) for A and \(2/4=0.5000\) for B. Suppose the draw selects A. After the success, add one A ball: \(U_2=(2,2)+(1,0)=(3,2)\).
At the second draw, the probabilities are \(3/5=0.6000\) for A and \(2/5=0.4000\) for B. Suppose the draw selects B. A failure calls for adding one ball of the other treatment, A, so \(U_3=(3,2)+(1,0)=(4,2)\).
At the third draw, the probabilities are \(4/6=0.6667\) for A and \(2/6=0.3333\) for B. Suppose A is selected and has a failure. The rule adds one B ball, producing \(U_4=(4,2)+(0,1)=(4,3)\). The changing probabilities follow from applying each response-specific update before the next draw.
Why the state update has this form
The update equation follows directly from defining \(r_{ij}\) as the net change from the pre-draw state. Conditional on \(U_t\), treatment \(i\) is drawn with probability \(u_{i,t}/S_t\). If the conditional probability of response category \(j\), after assigning treatment \(i\), is \(q_{j\mid i,t}\), then the probability of that treatment-response pair is the product of the two conditional probabilities:
For that pair, the next urn state is \(U_t+r_{ij}\). If two different pairs produce the same next state, the probability of that state is the sum of the probabilities of all such pairs. This calculation justifies the transition implied by an algorithm; it does not require the response probabilities to be equal across treatments. If the response distribution depends on information beyond \(U_t\), such as earlier outcomes, the state \(U_t\) alone need not determine the next-state probabilities. The algorithm still operates, but a transition description must include the relevant history or other conditioning information.
Checks, alternatives, and limitations
Before running an algorithm, test its specification with small, deliberate checks. Confirm that the initial state has positive total size; that every possible treatment-response pair has a defined update; and that each draw probability is between zero and one and the probabilities sum to one. After an update, check that the new counts are integers, no count is negative, and the total is positive. If removal rules are present, test them at states where the count to be removed is small, not only at the initial state.
A frequent implementation error is updating before drawing. The incorrect reasoning is that the current participant's response should influence the treatment they receive. But that response is not observed until after assignment and treatment; using it for the same allocation reverses the intended order and can introduce information that was not available at randomization. The correction is to draw from \(U_t\), observe the response, and use the response only to produce \(U_{t+1}\).
Two common implementation choices are to maintain a literal list of balls or to store only the count vector. A literal list can make the physical draw easy to visualize, while the count vector is compact and computes the same probabilities when draws are uniform over balls. Count vectors are particularly convenient for additions and removals, but they make explicit feasibility checks essential. A fixed allocation schedule is another kind of procedure: it does not use response-linked urn updates. It can serve as a conceptual contrast, but it is not a substitute for implementing the specified generalized urn rule.
The algorithm is only as well-defined as its response coding and replacement rule. Missing or delayed responses, an unlisted response category, a rule that removes a nonexistent ball, or a state with total zero prevents the next draw unless a handling policy has been specified. In practice, save a log of every pre-draw state, probability vector, assignment, response category, update vector, and resulting state. Such a log makes it possible to locate whether an unexpected allocation arose from the draw, the response classification, or the update.
Finally, writing the algorithm does not supply a response model, establish a treatment effect, or guarantee that the urn's allocation behavior is desirable for a particular study. Those are separate questions. Here the objective is narrower: to state and execute the randomization and replacement steps without ambiguity.
Check Your Understanding
Answer each question, then compare with the solutions below.
- An urn has counts \((6,4)\). What are the two treatment probabilities on the next draw?
- From state \((3,1)\), the selected treatment is B and the response rule gives net change \((0,1)\). What is the next state and its total?
- Why must a treatment be drawn before the response-specific replacement vector is applied?
- A rule removes one A ball, but the current A count is zero. Is a nonnegative final count sufficient to justify this update?
Solutions. (1) The total is \(10\), so the probabilities are \(6/10=0.6000\) and \(4/10=0.4000\). (2) The update is \((3,1)+(0,1)=(3,2)\), with total \(5\). (3) The response is observed after assignment; it can change later allocations, not the assignment that generated it. (4) No. Removing an A ball is infeasible when none is available, even if another operation might make the final vector nonnegative. The update must respect the rule's actual operation order and feasibility conditions.