Tutorials › Marginal Structural Models › Deriving Two-Stage Q-Learning

Dynamic Treatment Regimes · Tutorial 410 of 501

Deriving Two-Stage Q-Learning

Learn how the optimized second-stage value determines the first-stage action, and how the resulting two-stage recursion follows from conditional outcome models.

Advanced 12 min read

What You'll Learn

  • Define the second-stage and first-stage Q-functions for a two-stage treatment regime.
  • Derive the first-stage value by averaging over intermediate covariates after optimizing the second action.
  • State the assumptions needed to identify the recursion from observed data.
  • Work through numerical examples with covariate-dependent second-stage decisions.
  • Recognize errors from reversing optimization and averaging or using unsupported actions.
  • Compare Q-learning with evaluating a prespecified regime and identify its main limitations.

From a Second-Stage Decision to a First-Stage Decision

In a two-stage treatment problem, the first action can affect both the final outcome and the information available when the second action is chosen. The optimal first action therefore cannot generally be found by comparing immediate treatment effects alone. It must account for what will happen after the intermediate covariates are observed and the best second action is selected.

Two-stage Q-learning makes this calculation explicit. Starting with the conditional mean of the terminal outcome at the second stage, it optimizes the second action for each observed history. It then averages that optimized value over the intermediate covariates that would follow each possible first action. The first action with the largest resulting value is optimal.

Notation and Conditions

Let \(X\) denote covariates measured before the first treatment \(A_1\), and let \(L\) denote covariates measured after \(A_1\) but before the second treatment \(A_2\). The history available before \(A_2\) is \(H_2=(X,A_1,L)\), and \(Y\) is the terminal outcome to be maximized. For clarity, assume each treatment has a finite action set and that the conditional means below are finite. Finite action sets ensure that the maxima are attained; ties can be resolved by a fixed rule.

Definition: The second-stage Q-function is the conditional mean outcome for a specified second action and observed history, \(Q_2(h_2,a_2)=E[Y\mid H_2=h_2,A_2=a_2]\). The optimized second-stage value at history \(h_2\) is \(V_2(h_2)=\max_{a_2}Q_2(h_2,a_2)\). The first-stage Q-function is \(Q_1(x,a_1)=E[V_2(H_2)\mid X=x,A_1=a_1]\), where this expectation averages over the intermediate covariates following the specified first action.

These definitions describe conditional outcome models in the observed data. To interpret their optimization causally, suppose consistency holds (the observed outcome equals the potential outcome under the treatments received), there is no interference between individuals, and sequential exchangeability holds: conditional on \(X\), \(A_1\) is independent of the potential intermediate covariates and outcomes; conditional on \(H_2\), \(A_2\) is independent of the potential outcomes under the possible second actions. Also assume positivity: each action the target regime may choose has positive probability at the histories where it is chosen. These conditions, together with integrability, allow the observed conditional means and covariate distributions to represent their intervention counterparts. If positivity fails, the recursion can still be written mathematically, but the relevant causal quantities are not identified from the observed data at unsupported histories.

Deriving the Two-Stage Recursion

Write \(L(a_1)\) for the intermediate covariates that would follow first action \(a_1\), and \(Y(a_1,a_2)\) for the terminal outcome under the two actions. Under the assumptions above, the second-stage conditional outcome model at a history \((x,a_1,l)\) represents the conditional mean outcome under second action \(a_2\). Denote that causal conditional mean by \(m_2(x,a_1,l,a_2)=E[Y(a_1,a_2)\mid X=x,L(a_1)=l]\). Then the optimized value after observing \(L(a_1)\) is the maximum of these conditional means.

$$ Q_1(x,a_1) = E\!\left[ \max_{a_2}m_2(x,a_1,L(a_1),a_2) \mid X=x \right] = E\!\left[ \max_{a_2}Q_2(H_2,a_2) \mid X=x,A_1=a_1 \right]. $$

The second equality uses sequential exchangeability and consistency to identify the conditional outcome model and the distribution of \(L(a_1)\) from the observed data among people with \(X=x,A_1=a_1\). The first-stage decision is consequently \(d_1^*(x)\in\arg\max_{a_1}Q_1(x,a_1)\).

Theorem: Under the stated causal assumptions, finite treatment action sets, and finite conditional means, a regime that chooses \(d_2^*(h_2)\in\arg\max_{a_2}Q_2(h_2,a_2)\) at the second stage and then chooses \(d_1^*(x)\in\arg\max_{a_1}Q_1(x,a_1)\) maximizes the mean terminal outcome among regimes with first action depending on \(X\) and second action depending on \(H_2\).

Proof. Consider any regime \((d_1,d_2)\). Conditional on \(X=x\) and the intermediate history produced after \(d_1(x)\), its conditional mean terminal outcome is \(Q_2(H_2,d_2(H_2))\). For every such history, \(Q_2(H_2,d_2(H_2))\leq\max_{a_2}Q_2(H_2,a_2)\). Taking the conditional expectation over the intermediate covariates gives a value no greater than \(Q_1(x,d_1(x))\). By definition of \(d_1^*\), this is no greater than \(Q_1(x,d_1^*(x))\). A fixed tie rule selects a maximizing action at each history; because the action set is finite, this selection is measurable. Thus \(d_2^*\) attains the first bound, and \(d_1^*\) attains the second. Averaging over \(X\) proves that the stated regime has at least as large a mean outcome as any other regime of the specified form. \(\square\)

Worked Examples

Worked Example: Choosing the First Action When There Is No Intermediate Covariate

Suppose there are no varying intermediate covariates and the second-stage conditional mean is \(Q_2(a_1,a_2)=10+3a_1+(4-6a_1)a_2\), with both actions in \(\{0,1\}\). Find the optimal actions by backward calculation.

For \(a_1=0\), the two second-stage means are \(Q_2(0,0)=10\) and \(Q_2(0,1)=14\). The optimized second-stage value is therefore \(V_2(0)=14\), attained by \(a_2=1\).

For \(a_1=1\), \(Q_2(1,0)=13\), while \(Q_2(1,1)=10+3+(4-6)=11\). Thus \(V_2(1)=13\), attained by \(a_2=0\). With no intermediate covariate to average over, \(Q_1(0)=14\) and \(Q_1(1)=13\). The optimal first action is \(a_1=0\), followed by \(a_2=1\). The best second action changes with the first action, so treating the second-stage choice as fixed would not reproduce this calculation.

Worked Example: Averaging Over an Intermediate Covariate

Let \(L\) be binary, and suppose the second-stage means are \(Q_2(L=0,0)=8\), \(Q_2(L=0,1)=10\), \(Q_2(L=1,0)=14\), and \(Q_2(L=1,1)=11\). The distribution of \(L\) depends on the first action: \(P(L=1\mid A_1=0)=0.25\) and \(P(L=1\mid A_1=1)=0.75\). Calculate both first-stage Q-values.

At \(L=0\), the better second action is \(1\), giving value \(10\). At \(L=1\), the better second action is \(0\), giving value \(14\). Therefore \(Q_1(0)=0.75(10)+0.25(14)=7.5+3.5=11\), whereas \(Q_1(1)=0.25(10)+0.75(14)=2.5+10.5=13\). The optimal first action is \(1\). Its larger value comes from changing the distribution of \(L\), even though the optimal second action is selected only after \(L\) is observed.

For comparison, if the second action were forced to be \(0\) at both values of \(L\), the first-action values would be \(0.75(8)+0.25(14)=9.5\) and \(0.25(8)+0.75(14)=12.5\). If it were forced to be \(1\), they would be \(0.75(10)+0.25(11)=10.25\) and \(0.25(10)+0.75(11)=10.75\). The history-dependent choice gives values \(11\) and \(13\), respectively, and is at least as good in each case.

Worked Example: Maximizing Before Averaging

Suppose \(L\) is equally likely to be 0 or 1 after a particular first action. At \(L=0\), the second-action means are \(8\) for \(a_2=0\) and \(10\) for \(a_2=1\); at \(L=1\), they are \(14\) and \(11\), respectively. Compare a history-dependent second-stage decision with choosing one fixed second action before \(L\) is observed.

The history-dependent decision chooses \(a_2=1\) when \(L=0\) and \(a_2=0\) when \(L=1\). Its expected value is \(0.5(10)+0.5(14)=12\). If the action must be fixed in advance, \(a_2=0\) has expected value \(0.5(8)+0.5(14)=11\), while \(a_2=1\) has expected value \(0.5(10)+0.5(11)=10.5\). The fixed-action maximum is \(11\), below \(12\). The calculation shows why the recursion takes the maximum at each observed history and then averages those maxima.

How to Use the Derivation and Diagnose Problems

The recursion describes the population target of Q-learning: determine the optimized second-stage conditional mean, then calculate its conditional expectation under each first action. In an analysis, conditional means and covariate distributions have to be estimated. The derivation itself does not guarantee that fitted models are accurate, or that the data support the actions being compared.

A common mistake is to reverse the order of averaging and optimization. In general, \(E[\max_{a_2}Q_2(H_2,a_2)\mid X,A_1]\) is not equal to \(\max_{a_2}E[Q_2(H_2,a_2)\mid X,A_1]\). The left side allows the second action to respond to the observed history; the right side chooses a single action before averaging over that history. The third worked example shows a strict difference.

Other checks follow directly from the derivation:

  • Check the timing of each variable. A variable used in \(H_2\) must be measured before \(A_2\). Omitting a relevant intermediate covariate can change the second-stage conditional means or the distribution being averaged.
  • Check action support. If a candidate action is rarely or never observed at a history, its conditional mean may be unstable or unidentified. Positivity is a condition for causal interpretation, not a detail that the maximization can repair.
  • Check optimization and averaging separately. At each history, verify which second action maximizes the fitted conditional mean; then average the resulting values using the distribution of intermediate covariates under each first action.
  • Check close comparisons. If fitted action-specific means are nearly tied, small prediction errors can change the selected action. A plausible-looking action rule is not by itself evidence that its value is precisely estimated.

Q-learning is a backward outcome-regression approach: it builds the decision rule from conditional outcome models. Evaluating a prespecified regime by the g-formula or inverse-probability weighting instead targets that regime’s value without necessarily searching over actions. Those approaches address different tasks and rely on their own modeling or identification conditions. The advantage of Q-learning is a direct route to an estimated optimal rule; a limitation is that errors in conditional outcome models can affect both the estimated values and the maximizing actions. The recursion identifies the target under its assumptions, but does not remove finite-sample uncertainty or model dependence.

Key takeaway: For each possible first action, optimize the second action separately at every second-stage history, then average that optimized value over the covariates induced by the first action. Finally, choose the first action with the largest averaged value.

Check Your Understanding

Answer each question, then compare with the solutions below.

  1. In the two-stage setting, what does \(Q_2(h_2,a_2)\) represent, and what information is included in \(h_2\)?
  2. If the optimized second-stage values are \(10\) when \(L=0\) and \(14\) when \(L=1\), with \(P(L=1\mid A_1=0)=0.4\), calculate \(Q_1(0)\).
  3. Why is maximizing the conditional mean separately at each second-stage history generally different from choosing one second action before averaging over histories?
  4. Name one assumption needed to give the observed-data recursion a causal interpretation, and describe what can fail if it is violated.

Solutions. 1. \(Q_2(h_2,a_2)\) is the conditional mean terminal outcome for second action \(a_2\) at the observed pre-second-treatment history \(h_2=(X,A_1,L)\). 2. \(Q_1(0)=0.6(10)+0.4(14)=6+5.6=11.6\). 3. Separate maximization permits the action to depend on the observed history; a fixed action cannot adapt to it, so the two operations need not agree. 4. For example, positivity requires the actions chosen by the regime to have positive probability at the relevant histories. Without it, the observed data may not identify the corresponding action-specific conditional outcome.

Clinical Biostats