Q-learning chooses treatments by comparing expected outcomes, starting with the final decision and working backward. We derive this recursion from the causal value formula, prove its population optimality, and show how Q-function estimation errors affect the learned regime's value. We follow Schulte et al. (2014), Sections 2–3 and 5.1, specializing the proofs to binary treatments; the numerical example is constructed for this note.
Recap: The regression result used in Q-learning
For a square-integrable outcome U and covariates Z, conditional expectation gives the squared-error minimizer m(Z)=E[U∣Z]. For any fixed square-integrable predictor g,
E[(U−g(Z))2]−E[(U−m(Z))2]=E[(g(Z)−m(Z))2]≥0.
Scroll to see the full equation →
The expectations use the joint distribution of (Z,U). This identity explains the regression targets in Section 5; causal assumptions are additionally needed to interpret them as treatment comparisons.
1. From one treatment decision to a treatment rule
Let X denote the covariates available before treatment, A∈{0,1} the treatment, and Y∈R an outcome for which larger values are preferable. A deterministic treatment rule is a measurable function d:X→{0,1}.
Write (X,A,Y)∼P for the observed-data distribution and PX for its baseline marginal. For observed quantities, unmarked E and Pr refer to P. Conditional means average over the corresponding conditional outcome distribution; a rule d is held fixed. The notation X∼PX does not impose a parametric model for the covariates.
For now, suppose that the conditional mean outcome under treatment a is identified by
Q(x,a)=EY∼PY∣X=x,A=a[Y].
Scroll to see the full equation →
Under the identification assumptions stated in Section 2, the rule's value is
V(d)=EX∼PX[Q{X,d(X)}].
Scroll to see the full equation →
Define the baseline mean and treatment contrast:
m(x)=Q(x,0),Δ(x)=Q(x,1)−Q(x,0).
Scroll to see the full equation →
For binary a, the following is an identity, not a linear-model assumption:
Q(x,a)Q{x,d(x)}=m(x)+aΔ(x),=m(x)+d(x)Δ(x).
Scroll to see the full equation →
The maximizing rule is
d∗(x)=1{Δ(x)>0}.
Scroll to see the full equation →
Ties are assigned to treatment 0. For any competing rule d,
Here L is linearity of expectation and cancellation of m(X); S uses the sign of Δ. The loss from a wrong recommendation is ∣Δ(X)∣, not an unweighted classification error.
2. Two-stage data and the causal target
Let L be observed between the two treatments. The record and decision histories are
O=(X,A1,L,A2,Y),A1,A2∈{0,1}.
Scroll to see the full equation →
Only information observed before a decision may enter its rule:
H1=X,H2=(X,A1,L).
Scroll to see the full equation →
In this two-stage setup, P now denotes the joint observed distribution of O, and O1,…,On∼iidP: the records are independent draws from the same population. Subscripts identify marginal and conditional distributions, such as PX and PL∣X,A1.
Both treatments are feasible at each relevant history. Outcomes are fully observed and have finite second moments under the interventions considered. Conditional equalities hold almost surely on the relevant support, so they may exclude histories with probability zero.
A dynamic treatment regime, or DTR, consists of two measurable rules,
d=(d1,d2),dt:Ht→{0,1},t=1,2.
Scroll to see the full equation →
We optimize over all measurable history-dependent rules, without cross-stage parameter restrictions.
Potential outcomes and regime value
Write L(a1) for the intermediate covariate that would be observed under first treatment a1, and Y(a1,a2) for the final outcome under the treatment sequence (a1,a2). Under a regime d, define
Our target is the expected final outcome in the baseline population,
V(d)=E[Yd],d∗∈dargmaxV(d).
Scroll to see the full equation →
Because the first treatment affects the intermediate covariate, the law of H2 changes with d1.
Expectations and probabilities involving potential variables use their underlying population distribution, whose observed-data marginal is P. In particular, E[Yd] averages the potential outcome over individuals in the baseline population. Equivalently, write Pd for the distribution of (X,A1d,Ld,A2d,Yd) under the regime and V(d)=EPd[Yd].
The regime determines treatment assignments in this average. It therefore differs from the unweighted observed mean EP[Y]. Below, the identification argument expresses the regime value using observed conditional distributions and X∼PX.
Assumptions connecting the records to interventions
C: Consistency. Treatments are well defined, L=L(A1), and Y=Y(A1,A2).
No interference. A patient's potential outcomes do not depend on other patients' treatments.
E1–E2: Sequential exchangeability. For the potential-outcome collection
W={L(a1),Y(a1,a2):a1,a2∈{0,1}}
Scroll to see the full equation →
as
A1⊥⊥W∣X,A2⊥⊥W∣H2.
Scroll to see the full equation →
Positivity. Define the logging propensities
et(a∣h)=Pr(At=a∣Ht=h).
Scroll to see the full equation →
require et(a∣h)>0 for both treatments at almost every history reachable under the candidate regimes. Exchangeability is not testable from an observational regression; positivity cannot be replaced by extrapolation.
Integrals become sums for discrete L. Positivity makes the conditional treatment comparisons identifiable; positive point masses are not required for continuous covariates.
4. Backward induction: optimize the last decision first
The maximum is inside the expectation because d2 may depend on the realized L.
Theorem 1: Optimality of backward induction
Under the assumptions in Section 2, the rules just defined satisfy
V(d∗)=EX[a1maxQ1∗(X,a1)]≥V(d)for every admissible d.
Scroll to see the full equation →
Proof. Write Q2d(h)=Q2∗{h,d2(h)}. We use identification (I), pointwise maximization (M), monotonicity of conditional expectation (CE), and substitution of the definition of Q1∗ (D).
The recorded outcome averages over the logging policy's second treatment, whereas Q-learning uses the best second treatment. Their first-stage treatment contrasts can therefore differ, even with perfect regression estimation.
5. From population Q-functions to fitted regressions
The backward recursion specifies conditional means, but those means are unknown. We estimate them by regression, writing qt for a working regression model and Qt for its fitted version. The squared-error identity recalled above identifies each regression target.
Lemma: The squared-error regression target
Let (Z,R)∼PZ,R with R square-integrable, and set m(Z)=E[R∣Z]. Within this lemma, a conditional expectation given Z=z integrates R∼PR∣Z=z; an unconditional expectation integrates (Z,R)∼PZ,R. The candidate g is fixed, not fitted on the same random pair. For any square-integrable g,
The expansion uses linearity and measurability of m(Z),g(Z) given Z; the cross term vanishes because E[R−m(Z)∣Z]=0. Taking expectations yields
E[(R−g(Z))2]−E[(R−m(Z))2]=E[(g(Z)−m(Z))2]≥0.
Scroll to see the full equation →
Apply the lemma to (R,Z)=(Y,(H2,A2)) and then (R,Z)=(M2(H2),(X,A1)). The unrestricted regression targets are exactly Q2∗ and Q1∗. Restricted regression families introduce approximation error.
Fit the second-stage regression
For each stage, choose fixed feature maps ut(h)∈Rrt and vt(h)∈Rst. A useful linear working model is
If Z2 has full column rank, the solution is (Z2⊤Z2)−1Z2⊤Y. In computation, a least-squares solver avoids explicitly forming the inverse. Set Q2(h,a)=q2(h,a;θ2) and recommend
d2(h)=1{v2(h)⊤ψ2>0}.
Scroll to see the full equation →
Construct the first-stage pseudo-outcome
For each recorded second-stage history, predict both treatment outcomes and retain the larger prediction:
The pseudo-outcome is a fitted prediction of the best conditional outcome at the second stage. It is not a measurement of what would have happened under an unobserved treatment. When computing it, keep the recorded A1i,Li in H2i.
Regress the pseudo-outcome on first-stage information
With Z1 defined analogously and Y1 the vector of pseudo-outcomes, the normal equations are
Z1⊤Z1θ1=Z1⊤Y1.
Scroll to see the full equation →
Finally, set Q1(x,a)=q1(x,a;θ1) and
d1(x)=1{v1(x)⊤ψ1>0},d=(d1,d2).
Scroll to see the full equation →
We fit the rules backward so that the first-stage regression accounts for optimal continuation, then apply the fitted rules in treatment order. Although these least-squares criteria do not use propensity weights, their causal interpretation still requires exchangeability and positivity. See Schulte et al., Section 5.1.
Algorithm 1Two-stage fitted Q-learning
Input: Trajectories {(H1i,A1i,H2i,A2i,Yi)}i=1n; linear features z1,z2 defined above.
Output: A regime d=(d1,d2) with treatments in {0,1}.
Fit θ2 by least squares of Yi on z2(H2i,A2i).
Set Q2(h,a)←z2(h,a)⊤θ2.
fori=1,…,ndo
Set Y1i←maxa∈{0,1}Q2(H2i,a).
end for
Fit θ1 by least squares of Y1i on z1(Xi,A1i).
Set Q1(x,a)←z1(x,a)⊤θ1.
returndt(h)=minargmaxa∈{0,1}Qt(h,a) for t=1,2.
Theorem 2: From Q-function error to value regret
For an integrable function g, define expectation under first-stage rule d1 as follows.
Draw X∼PX, assign A1=d1(X), and draw L∼PL∣X,A1=d1(X). The induced H2 distribution is the one used by Ed1, rather than the logged H2 marginal. Thus
Ed1[g(H2)]=EX[E{g(H2)∣X,A1=d1(X)}].
Scroll to see the full equation →
Let dt(h)∈argmaxaQt(h,a), with ties assigned to 0. If, on all relevant histories and treatment options,
∥Qt−Qt∗∥∞≤ϵt,t=1,2,
Scroll to see the full equation →
where ∥⋅∥∞ denotes the essential supremum over the reachable-history support and the finite treatment set, then
0≤V(d∗)−V(d)≤2ϵ1+2ϵ2.
Scroll to see the full equation →
First, we decompose the regret by adding and subtracting V(d1,d2∗), then applying the value formula:
The outer terms use the assumed uniform error bound; the middle term is nonpositive because a maximizes Qt.
Finally, substitute these bounds into the regret decomposition and use monotonicity of expectation:
V(d∗)−V(d)≤EX[2ϵ1]+Ed1[2ϵ2]=2ϵ1+2ϵ2.□
Scroll to see the full equation →
Conditional on the training data T=(O1,…,On)∼P⊗n, the fitted rules and Q-functions are fixed. The value expectations average over a fresh individual. The bound holds whenever the stated error bounds hold; it does not show that least squares achieves a particular error rate.
Convergence in probability refers to repeated training samples T∼P⊗n. If both uniform errors converge to zero in that sense, the difference between the optimal and learned values also converges to zero.
How second-stage error enters the first-stage target
where ε is an independent standard normal error. Treatments and L are generated in their time order using independent random draws conditional on the stated histories. This construction satisfies sequential exchangeability.
The raw regression prefers a1=1, although Q1∗(0,0)>Q1∗(0,1). At x=1, the logged means are 3.24 and 4.66. Its resulting first-stage rule always selects 1, and its value after optimal second-stage continuation is (2.5+4.8)/2=3.65<4.05.
7. A reproducible Python implementation
The working regressions below contain the true Q-functions of this example. Python 3 and NumPy are required.
Python code and numerical output
import numpy as np
rng = np.random.default_rng(17)
def mean_y(x, a1, l, a2):
return 2 + x + a1 * (1.4 * x - 0.3) + 0.5 * l + a2 * (1.5 - 2 * l)
def simulate(n):
x = rng.binomial(1, 0.5, n)
p1 = 0.2 + 0.6 * x
a1 = rng.binomial(1, p1)
l = rng.binomial(1, 0.2 + 0.1 * x + 0.5 * a1)
p2 = 0.2 + 0.6 * ((1 - a1) * l + a1 * (1 - l))
a2 = rng.binomial(1, p2)
y = mean_y(x, a1, l, a2) + rng.normal(size=n)
return x, a1, l, a2, y, p1, p2
def z2(x, a1, l, a2):
return np.column_stack([np.ones_like(x), x, a1, l, x * a1, a2, a2 * l])
def z1(x, a1):
return np.column_stack([np.ones_like(x), x, a1, x * a1])
x, a1, l, a2, y, _, _ = simulate(5000)
zero, one = np.zeros_like(x), np.ones_like(x)
# Fit the terminal outcome regression and optimize its continuation.
theta2 = np.linalg.lstsq(z2(x, a1, l, a2), y, rcond=None)[0]
pseudo = np.maximum(z2(x, a1, l, zero) @ theta2,
z2(x, a1, l, one) @ theta2)
theta1 = np.linalg.lstsq(z1(x, a1), pseudo, rcond=None)[0]
def d1(x):
q0 = z1(x, np.zeros_like(x)) @ theta1
q1 = z1(x, np.ones_like(x)) @ theta1
return (q1 > q0).astype(int)
def d2(x, a1, l):
q0 = z2(x, a1, l, np.zeros_like(x)) @ theta2
q1 = z2(x, a1, l, np.ones_like(x)) @ theta2
return (q1 > q0).astype(int)
# In this artificial population we can compute the learned regime's value exactly.
exact_value = 0.0
for xv in (0, 1):
av = int(d1(np.array([xv]))[0])
p = 0.2 + 0.1 * xv + 0.5 * av
for lv in (0, 1):
bv = int(d2(np.array([xv]), np.array([av]), np.array([lv]))[0])
exact_value += 0.5 * (p if lv else 1 - p) * mean_y(xv, av, lv, bv)
# Evaluate the frozen regime on independent logged records with known propensities.
x, a1, l, a2, y, p1, p2 = simulate(100000)
prob1 = np.where(a1 == 1, p1, 1 - p1)
prob2 = np.where(a2 == 1, p2, 1 - p2)
matches = (a1 == d1(x)) & (a2 == d2(x, a1, l))
ipw_value = np.mean(matches * y / (prob1 * prob2))
print("First-stage choices for X = 0, 1:", d1(np.array([0, 1])))
print("Exact value of learned regime:", round(float(exact_value), 4))
print("Held-out IPW estimate:", round(float(ipw_value), 4))
print("Optimal population value:", 4.05)
With the stated seed, the run gives:
First-stage choices for X = 0, 1: [0 1]
Exact value of learned regime: 4.05
Held-out IPW estimate: 4.0246
Optimal population value: 4.05
The exact value uses the known artificial population. The held-out estimate is one noisy realization, not a finite-sample guarantee.
Why the held-out weighting estimates regime value
Condition on the training data so that the learned rules are fixed. For an independent evaluation record, write
Averaging over X recovers the identified value formula for d. Thus the ordinary, unnormalized inverse-probability-weighted average used in the code is conditionally unbiased for V(d) when the logging propensities are known and the stated identification and integrability conditions hold. Estimated propensity scores require additional analysis; the exact cancellation is not a finite-sample unbiasedness result for arbitrary fitted weights.
8. Scope of the results
Theorem 1 concerns population optimality; Theorem 2 is conditional on uniform Q-function error bounds. Linear Q-functions need not remain linear after maximization and averaging. Ordinary regression Q-learning is not doubly robust, and generated pseudo-outcomes do not justify naive first-stage standard errors. Treatment ties can create nonregular inference; see Schulte et al., Section 8. We assume full histories, complete outcomes, and a terminal-outcome objective, not a general deep or long-horizon offline-RL setting.
9. Extending the recursion to more stages
The same population construction applies to T stages with a terminal outcome. Let Ht contain all information available before At, and let At(ht) be the finite set of feasible treatments. Under corresponding consistency, exchangeability, positivity, and moment conditions, define
At every stage, choose a treatment maximizing its Q-function, using a fixed convention to resolve ties. The fitted version works backward by replacing each future maximum with its fitted counterpart and regressing it on the current history and treatment.
For cumulative stage rewards, use the corresponding reward recursion; do not add a reward twice if it is already included in the target outcome.
References and further reading
Schulte, P. J., Tsiatis, A. A., Laber, E. B., and Davidian, M. (2014). Q- and A-Learning Methods for Estimating Optimal Dynamic Treatment Regimes. Statistical Science, 29(4), 640–661. Journal article, open manuscript. Sections 2–3 provide the causal framework and recursion; Section 5.1 describes regression-based Q-learning.