Tutorials › Marginal Structural Models › Q-Learning With Three Treatment Stages

Dynamic Treatment Regimes · Tutorial 418 of 501

Q-Learning With Three Treatment Stages

Learn to construct and regress the three nested Q-learning pseudo-outcomes, then use the fitted Q-functions to choose actions at each treatment stage.

Advanced 12 min read

What You'll Learn

  • Define the three stage-specific Q-functions and their treatment histories.
  • Build a terminal-stage prediction and carry it backward through two pseudo-outcomes.
  • Fit each stage’s regression target using the appropriate observed history and treatment.
  • Distinguish an adaptive future treatment decision from one fixed action.
  • Diagnose weak support, unstable maximization, and errors in carrying rewards backward.
  • Compare backward Q-learning with weighting and direct policy-learning approaches.

From Terminal Predictions to Earlier Decisions

With three treatment decisions, the outcome of an early action depends not only on what happens immediately afterward, but also on the treatments that can be chosen later. Backward Q-learning handles this dependence by starting at the final treatment stage. It predicts the terminal contribution under each final action, chooses the best predicted continuation, and uses that prediction as part of the target for the preceding stage.

The essential operation is repeated twice: a fitted later-stage Q-function supplies a pseudo-outcome for an earlier-stage regression. This tutorial works through that sequence, including the intermediate rewards, the conditioning histories, and the maximization at each stage. The finite-horizon Bellman recursion and the general meaning of Q-functions are assumed from the earlier material on those subjects.

Definition: For stages \(t=1,2,3\), let \(H_t\) denote the information available immediately before treatment \(A_t\), and let \(\mathcal{A}_t(H_t)\) be the set of feasible actions at that history. Let \(R_t\) be the reward accrued after treatment \(A_t\), and let \(Y\) be the terminal reward after stage 3. The stage-3 Q-function is $$ Q_3(h_3,a_3)=E[R_3+Y\mid H_3=h_3,A_3=a_3]. $$ The earlier-stage Q-functions are defined recursively by $$ Q_2(h_2,a_2)=E\!\left[R_2+\max_{a_3\in\mathcal{A}_3(H_3)}Q_3(H_3,a_3)\mid H_2=h_2,A_2=a_2\right], $$ $$ Q_1(h_1,a_1)=E\!\left[R_1+\max_{a_2\in\mathcal{A}_2(H_2)}Q_2(H_2,a_2)\mid H_1=h_1,A_1=a_1\right]. $$ At each stage, the Q-function is the expected reward from that stage onward when the current action is fixed and later actions are chosen to maximize the fitted continuation value.

The histories must be timed correctly: \(H_t\) contains information observed before \(A_t\), while \(H_{t+1}\) may include new covariates observed after \(A_t\). In a longitudinal study, these histories ordinarily include prior treatments and covariates. A regression at stage 2 therefore uses stage-2 histories and treatments, not just the baseline variables.

For causal interpretation, assume consistency, sequential exchangeability of each treatment given its recorded history, and positivity for the actions being evaluated. The conditional means must be estimable from the observed data, and the action sets must permit a well-defined maximum (for example, finite action sets). These conditions do not make a misspecified regression correct; they describe the setting in which the observed conditional distributions can identify the relevant causal quantities.

The Three Backward Regression Targets

In a sample, the procedure first fits a model for \(Q_3\), using \(R_3+Y\) as the response and \(H_3,A_3\) as predictors. For each individual, the fitted terminal prediction is then maximized over feasible stage-3 actions. Adding the observed stage-2 reward gives the stage-2 pseudo-outcome. Regressing that target on \(H_2,A_2\) estimates \(Q_2\).

$$ \widehat Q_3 \ \longrightarrow\ \widetilde Y_2=R_2+\max_{a_3\in\mathcal{A}_3(H_3)}\widehat Q_3(H_3,a_3) \ \longrightarrow\ \widehat Q_2. $$

The same operation is repeated once more. Maximize the fitted stage-2 Q-function at each observed \(H_2\), add \(R_1\), and regress the resulting target on \(H_1,A_1\). The final fitted model is \(\widehat Q_1\).

$$ \widehat Q_2 \ \longrightarrow\ \widetilde Y_1=R_1+\max_{a_2\in\mathcal{A}_2(H_2)}\widehat Q_2(H_2,a_2) \ \longrightarrow\ \widehat Q_1. $$

The pseudo-outcome is not a newly observed clinical outcome. It is a constructed regression target: an observed reward plus a model-based prediction of future reward under the best available later action. The maximization happens separately for each person’s observed history, because later treatment can adapt to information that becomes available after the earlier decision.

Worked Example: Calculating a Stage-2 Pseudo-Outcome

Suppose the fitted terminal-stage model is \( \widehat Q_3(x,a_3)=5+2a_3+(1+x)a_3 \), where \(x\) is a component of \(H_3\) and \(a_3\in\{0,1\}\). For a person with \(x=1\), the observed stage-2 reward is \(R_2=1\), and both final actions are feasible. Calculate the stage-2 pseudo-outcome.

For action 0, \( \widehat Q_3(1,0)=5+0+0=5 \). For action 1, \( \widehat Q_3(1,1)=5+2+(1+1)=9 \). Thus the maximizing action is 1 and the predicted continuation value is 9. Adding the observed reward gives \( \widetilde Y_2=1+9=10 \).

This value of 10 is the response for this person’s stage-2 regression row. It is not the stage-2 Q-value by itself: the stage-2 Q-function is estimated by modeling such targets across people as a function of \(H_2,A_2\).

Worked Example: Carrying Rewards Through All Three Stages

Consider a small illustrative data set with binary actions. The fitted stage-3 model is \( \widehat Q_3(x,a_3)=5+2a_3+(1+x)a_3 \), with \(x\in\{0,1\}\). The following four rows are the observed stage-2 regression data; \(s\) denotes a stage-2 history group.

Group \(s\)\(A_2\)\(R_2\)\(x\)\(\widetilde Y_2\)
A0109
A1019
B02111
B1109

To verify the targets, at \(x=0\) the terminal predictions are 5 for action 0 and 8 for action 1, so the maximum is 8. At \(x=1\) they are 5 and 9, so the maximum is 9. The four targets are therefore \(1+8=9\), \(0+9=9\), \(2+9=11\), and \(1+8=9\), respectively.

Fit a saturated stage-2 regression, so each group-by-action mean is its fitted value. This gives \( \widehat Q_2(A,0)=9 \), \( \widehat Q_2(A,1)=9 \), \( \widehat Q_2(B,0)=11 \), and \( \widehat Q_2(B,1)=9 \). The maximized continuation values are 9 in group A and 11 in group B. Choose action 0 to break the tie in group A; group B’s fitted maximizing action is also 0.

Now suppose the stage-1 observations are as follows. In baseline group \(u\), one row has \(A_1=0,R_1=2,H_2=A\), and another has \(A_1=1,R_1=0,H_2=B\). Their stage-1 targets are \(2+9=11\) and \(0+11=11\). In baseline group \(v\), the two rows have \((A_1,R_1,H_2)=(0,1,B)\) and \((1,1,A)\); their targets are \(1+11=12\) and \(1+9=10\). A saturated stage-1 regression therefore gives \( \widehat Q_1(u,0)=11 \), \( \widehat Q_1(u,1)=11 \), \( \widehat Q_1(v,0)=12 \), and \( \widehat Q_1(v,1)=10 \). Group \(v\) favors action 0, while group \(u\) has a tie.

Why the Pseudo-Outcome Targets the Earlier Q-Function

The regression construction follows directly from the recursive conditional-mean definitions. This is a statement about the target of each regression, not a claim that any particular fitted model is correctly specified.

Result: If \(Q_3\) is the stage-3 conditional mean defined above, then the stage-2 target \(Z_2=R_2+\max_{a_3}Q_3(H_3,a_3)\) satisfies $$ E[Z_2\mid H_2,A_2]=Q_2(H_2,A_2). $$ Likewise, if \(Q_2\) is the stage-2 conditional mean, then \(Z_1=R_1+\max_{a_2}Q_2(H_2,a_2)\) satisfies \(E[Z_1\mid H_1,A_1]=Q_1(H_1,A_1)\).

Derivation. By the definition of \(Z_2\), conditioning on \(H_2,A_2\) gives $$ E[Z_2\mid H_2,A_2] =E\!\left[R_2+\max_{a_3\in\mathcal{A}_3(H_3)}Q_3(H_3,a_3)\mid H_2,A_2\right]. $$ The expression on the right is exactly the defining conditional mean for \(Q_2(H_2,A_2)\). The same substitution at stage 1 gives $$ E[Z_1\mid H_1,A_1] =E\!\left[R_1+\max_{a_2\in\mathcal{A}_2(H_2)}Q_2(H_2,a_2)\mid H_1,A_1\right] =Q_1(H_1,A_1). $$ These equalities require the displayed conditional expectations to exist; finite expected rewards are a sufficient condition. In estimation, fitted later-stage functions replace the population Q-functions, so the sample pseudo-outcomes are estimated targets rather than known conditional means. \(\square\)

A second useful fact explains why regression is the natural fitting step when squared-error loss is used.

Result: If \(Z\) is square-integrable, then among square-integrable functions \(g(H,A)\), the conditional mean \(m(H,A)=E[Z\mid H,A]\) minimizes \(E[(Z-g(H,A))^2]\), up to equality almost surely.

Proof. Write \(Z-g=(Z-m)+(m-g)\). Squaring and taking expectations yields $$ E[(Z-g)^2]=E[(Z-m)^2]+E[(m-g)^2]+2E[(Z-m)(m-g)]. $$ The cross-term is zero: conditioning on \(H,A\), the factor \(m-g\) is fixed and \(E[Z-m\mid H,A]=0\). Thus $$ E[(Z-g)^2]=E[(Z-m)^2]+E[(m-g)^2]\geq E[(Z-m)^2]. $$ Equality holds exactly when \(g=m\) almost surely. In practice, the regression model restricts the available functions \(g\), so the fitted model is the best approximation within that model class under the chosen loss, not automatically the true conditional mean. \(\square\)

Worked Example: Why Maximization Must Follow the New Covariate

Suppose a stage-2 decision is followed by an equally likely good or bad stage-3 history. At the good history, the fitted final Q-values for actions 0 and 1 are 8 and 10. At the bad history, they are 6 and 0. Let \(R_2=0\).

With a stage-3 decision made after observing the history, the continuation values are 10 in the good history and 6 in the bad history. Their average is \( (10+6)/2=8 \), so the stage-2 pseudo-outcome has conditional mean 8.

A single action fixed before learning whether the history is good or bad has a different value. Action 0 has average \( (8+6)/2=7 \); action 1 has average \( (10+0)/2=5 \). The best fixed action therefore has value 7, not 8. The correct stage-2 target uses the history-specific maximum before averaging. Reversing those operations would remove the value of adapting treatment to the later information.

Diagnostics, Alternatives, and Limitations

Several checks help identify a faulty backward fit. Confirm that every target contains the reward from the current stage exactly once, and that the continuation term comes from the next stage’s fitted Q-function. Check that maximization uses the feasible action set for the person’s history. Inspect the number of observations supporting each relevant history-action combination; sparse support makes conditional means and their maxima unstable. Finally, examine fitted values and pseudo-outcomes for implausible magnitudes, unexpected ranges, and sensitivity to a small number of observations.

  • Reward timing error: omitting \(R_2\) or adding it twice changes every affected stage-2 target and carries that error into stage 1.
  • Wrong maximization order: maximizing an average over future histories instead of maximizing separately at each observed history can understate the value of adaptive treatment.
  • Unsupported actions: a model may return predictions for actions rarely or never observed in a history subgroup. Those predictions rely on extrapolation, not direct information in the data.
  • Unstable maxima: when two fitted actions have similar values, small prediction changes can switch the selected action. A high maximized prediction alone is not evidence that the decision is reliable.

Backward Q-learning is a regression-based alternative to inverse-probability weighting approaches, including marginal structural models: it models outcome means and propagates estimated continuation values rather than constructing treatment weights. It also differs from direct policy-learning approaches, which target a decision rule or its value more directly instead of fitting a sequence of conditional outcome regressions. These approaches involve different modeling and stability trade-offs; none removes the need for adequate treatment support and credible assumptions.

The procedure’s main limitation is that errors in later-stage predictions enter earlier-stage targets. Misspecification, sparse data, or noisy maximization at stage 3 can therefore affect both \(\widehat Q_2\) and \(\widehat Q_1\). The construction defines the sequence of targets, but does not by itself guarantee good estimation, reliable decisions, or valid uncertainty quantification.

Key takeaway: Start with the terminal response \(R_3+Y\). At stage 2, add \(R_2\) to the history-specific maximum of the fitted stage-3 Q-function. At stage 1, add \(R_1\) to the history-specific maximum of the fitted stage-2 Q-function. Each resulting pseudo-outcome is then regressed on the history and treatment from its own stage.

Check Your Understanding

Answer each question, then compare with the solutions below.

  1. If the fitted stage-3 values at a person’s history are 4 for action 0 and 7 for action 1, and \(R_2=3\), what is the stage-2 pseudo-outcome?
  2. At a later history, two action values are 12 and 5. A different history, equally likely, has values 2 and 9. What is the expected history-specific maximum? What is the best value from choosing one fixed action before the history is known?
  3. State the response and predictors for the stage-3 regression in this setup.
  4. Why is a fitted Q-value for a rarely observed treatment-history combination a warning sign?

Solutions. (1) The maximum continuation value is 7, so the target is \(3+7=10\). (2) The history-specific maxima are 12 and 9, giving \( (12+9)/2=10.5 \). The fixed-action averages are \( (12+2)/2=7 \) for action 0 and \( (5+9)/2=7 \) for action 1, so the best fixed value is 7. (3) Use \(R_3+Y\) as the response and \(H_3,A_3\) as predictors. (4) The prediction may depend mainly on model extrapolation because the data provide little information about that conditional mean; its maximization can amplify the resulting error.

Clinical Biostats