Simple Algorithm
Reinforcement Learning · Fixed Point · 28 min

Occupancy Measures & the Dual View

Where a policy spends its time, the flow constraints it obeys, and the linear program they give.

Change the question from "what should I do?" to "where will I spend my time?" A policy's discounted occupancy measure records how much it visits each state–action pair. The return is a linear function of it, the occupancy measures that policies can produce are exactly the solutions of a set of flow constraints, and planning becomes a linear program. That dual view is the starting point of constrained RL, imitation by distribution matching, off-policy estimation without exploding importance weights, and convex objectives such as maximum-entropy exploration.

What you’ll learn

  • Define the normalized discounted occupancy measure dμπd^\pi_\mu and prove J(π)=⟨dμπ,r⟩/(1−γ)J(\pi) = \langle d^\pi_\mu, r\rangle/(1-\gamma).
  • Prove the flow constraints and the correspondence between stationary policies and feasible occupancy measures.
  • Derive the primal and dual linear programs of a discounted MDP and interpret complementary slackness and the vertices of the dual.
  • Explain how occupancy measures underlie constrained MDPs, imitation by occupancy matching, DICE estimators and convex RL.

Where a policy spends its time

DerivationThe return is linear in d
  1. J(π)=∑t≥0γt Eμπ[r(St,At)]=∑s,a(∑t≥0γtPr⁡μπ(St=s,At=a)) r(s,a)J(\pi) = \sum_{t\ge0}\gamma^t\,\E^\pi_\mu\big[r(S_t, A_t)\big] = \sum_{s,a}\Big(\sum_{t\ge0}\gamma^t\Pr^\pi_\mu(S_t = s, A_t = a)\Big)\,r(s,a)

    Expand the expected discounted return and regroup the terms by the state–action pair visited:

  2. J(π)=11−γ∑s,adμπ(s,a) r(s,a)=⟨dμπ, r⟩1−γJ(\pi) = \frac{1}{1-\gamma}\sum_{s,a} d^\pi_\mu(s,a)\,r(s,a) = \frac{\langle d^\pi_\mu,\, r\rangle}{1-\gamma}

    The bracket is dμπ(s,a)/(1−γ)d^\pi_\mu(s,a)/(1-\gamma). The policy enters the return only through its occupancy measure, and it enters linearly:

Policy, starting in s1s_1Occupancy measureJ=10 ⟨d,r⟩J = 10\,\langle d, r\rangle
(stay, stay)d(s1,stay)=1d(s_1, \text{stay}) = 110⋅1=1010 \cdot 1 = 10
(go, stay)d(s1,go)=0.1d(s_1, \text{go}) = 0.1, d(s2,stay)=0.9d(s_2, \text{stay}) = 0.910⋅(0.9⋅2)=1810 \cdot (0.9 \cdot 2) = 18
(stay, go)d(s1,stay)=1d(s_1, \text{stay}) = 11010
(go, go)d(s1,go)=11.9≈0.526d(s_1, \text{go}) = \tfrac{1}{1.9} \approx 0.526, d(s2,go)=0.91.9≈0.474d(s_2, \text{go}) = \tfrac{0.9}{1.9} \approx 0.47400
The two-state MDP of the valuespace widget (in s1s_1, stay pays 1 and go pays 0 and moves to s2s_2; in s2s_2, stay pays 2 and go pays 0 and moves back) with γ=0.9\gamma = 0.9. (stay, stay) and (stay, go) never leave s1s_1, so they have the same occupancy measure and the same return: no reward can tell them apart from this start.
Predict before you look

In the same MDP, start in s1s_1 and follow the stationary policy that goes with probability ½ in s1s_1 and stays in s2s_2. What is the discounted state occupancy d(s2)d(s_2)?

Show the answer

0.82

The agent is still in s1s_1 at time tt with probability (12)t(\tfrac12)^t, so d(s1)=(1−γ)∑tγt2−t=0.1/(1−0.45)≈0.182d(s_1) = (1-\gamma)\sum_t \gamma^t 2^{-t} = 0.1/(1 - 0.45) \approx 0.182 and d(s2)≈0.818d(s_2) \approx 0.818. Then J=10 (0.091⋅1+0.818⋅2)≈17.27J = 10\,(0.091 \cdot 1 + 0.818 \cdot 2) \approx 17.27, which matches V(s1)V(s_1) from the Bellman equation V1=12(1+0.9V1)+12(0.9⋅20)V_1 = \tfrac12(1 + 0.9V_1) + \tfrac12(0.9 \cdot 20).

Flow constraints and the policy–occupancy correspondence

DerivationEvery occupancy measure obeys the flow constraints
  1. Pr⁡(St+1=s′)=∑s,aP(s′∣s,a) Pr⁡(St=s,At=a)\Pr(S_{t+1} = s') = \sum_{s,a} P(s' \mid s, a)\,\Pr(S_t = s, A_t = a)

    Whatever the policy, even a history-dependent one, the next state depends on the past only through the current pair:

  2. d(s′)−(1−γ) μ(s′)=γ∑s,aP(s′∣s,a) d(s,a)d(s') - (1-\gamma)\,\mu(s') = \gamma\sum_{s,a}P(s' \mid s,a)\,d(s,a)

    Multiply by (1−γ)γt+1(1-\gamma)\gamma^{t+1} and sum over t≥0t \ge 0. The left side becomes d(s′)d(s') minus its t=0t = 0 term, (1−γ)μ(s′)(1-\gamma)\mu(s'):

  3. ∑ad(s′,a)=(1−γ) μ(s′)+γ∑s,aP(s′∣s,a) d(s,a)for every s′∈S\sum_a d(s',a) = (1-\gamma)\,\mu(s') + \gamma\sum_{s,a}P(s' \mid s,a)\,d(s,a)\qquad\text{for every } s' \in \mathcal S

    These are the flow constraints, one per state. Read dd as a discounted flow of probability: what is spent in s′s' (left) is what started there plus what arrives from every state–action pair, one discount factor later (right). Summing them over s′s' gives ∑s,ad(s,a)=1\sum_{s,a} d(s,a) = 1 automatically.

DerivationProof of (ii)
  1. ν⊤=(1−γ) μ⊤+γ ν⊤Pπd\nu^\top = (1-\gamma)\,\mu^\top + \gamma\,\nu^\top P^{\pi_d}

    Let ν(s)=∑ad(s,a)\nu(s) = \sum_a d(s,a). Then d(s,a)=ν(s) πd(a∣s)d(s,a) = \nu(s)\,\pi_d(a \mid s) for every pair, including states with ν(s)=0\nu(s) = 0, where both sides vanish. Substituting into the flow constraints turns them into a linear equation for ν\nu alone:

  2. ν⊤=(1−γ) μ⊤(I−γPπd)−1=dμπd\nu^\top = (1-\gamma)\,\mu^\top\big(I - \gamma P^{\pi_d}\big)^{-1} = d^{\pi_d}_\mu

    γPπd\gamma P^{\pi_d} has spectral radius at most γ<1\gamma < 1, so I−γPπdI - \gamma P^{\pi_d} is invertible and the solution is unique. It is the discounted state distribution of πd\pi_d itself, by the same derivation applied to πd\pi_d:

  3. Therefore d(s,a)=dμπd(s) πd(a∣s)=dμπd(s,a)d(s,a) = d^{\pi_d}_\mu(s)\,\pi_d(a \mid s) = d^{\pi_d}_\mu(s,a). If another stationary policy π′\pi' had the same occupancy measure, then π′(a∣s)=d(s,a)/ν(s)=πd(a∣s)\pi'(a \mid s) = d(s,a)/\nu(s) = \pi_d(a \mid s) wherever ν(s)>0\nu(s) > 0. ■\blacksquare

Check your understanding

A history-dependent policy π\pi and a stationary policy πˉ\bar\pi have the same discounted occupancy measure from μ\mu. Which statement must be true?

  1. They choose the same action in every state at every time
  2. They have the same expected discounted return from μ\mu for every reward function, though their return distributions may differ
  3. They have the same value from every other start distribution too
  4. They have the same expected return only for the reward used to construct πˉ\bar\pi
Show the answer

B. They have the same expected discounted return from μ\mu for every reward function, though their return distributions may differ

J(π)=⟨dμπ,r⟩/(1−γ)J(\pi) = \langle d^\pi_\mu, r\rangle/(1-\gamma) for every rr, so equal occupancy measures give equal expected returns for all rewards at once. The measure is a time-averaged, one-step summary, so it says nothing about correlations across time, and it is tied to μ\mu: from another start the two policies can differ.

Planning as a linear program, and its dual

DerivationPrimal over values, dual over occupancies
  1. min⁡V ∑sμ(s) V(s)s.t.V(s) ≥ r(s,a)+γ∑s′P(s′∣s,a) V(s′)for all (s,a)\min_{V}\ \sum_s \mu(s)\,V(s)\qquad\text{s.t.}\quad V(s) \ \ge\ r(s,a) + \gamma\sum_{s'}P(s' \mid s,a)\,V(s')\quad\text{for all } (s,a)

    Primal. V⋆V^\star is the smallest function that satisfies every Bellman inequality, so with weights μ(s)>0\mu(s) > 0 it is the unique solution of a linear program with one variable per state and one constraint per state–action pair:

  2. max⁡x≥0 ∑s,ax(s,a) r(s,a)s.t.∑ax(s′,a)−γ∑s,aP(s′∣s,a) x(s,a)=μ(s′)for all s′\max_{x \ge 0}\ \sum_{s,a} x(s,a)\,r(s,a)\qquad\text{s.t.}\quad \sum_a x(s',a) - \gamma\sum_{s,a}P(s' \mid s,a)\,x(s,a) = \mu(s')\quad\text{for all } s'

    Dual. Attach a multiplier x(s,a)≥0x(s,a) \ge 0 to each inequality. The dual has one variable per state–action pair and one equality per state, and its constraints are the flow constraints for the unnormalized occupancy x=d/(1−γ)x = d/(1-\gamma):

  3. x⋆(s,a)>0  ⟹  V⋆(s)=r(s,a)+γ∑s′P(s′∣s,a) V⋆(s′)x^\star(s,a) > 0 \;\Longrightarrow\; V^\star(s) = r(s,a) + \gamma\sum_{s'}P(s' \mid s,a)\,V^\star(s')

    Strong duality and complementary slackness. Both optima equal ∑sμ(s)V⋆(s)=J(π⋆)\sum_s\mu(s)V^\star(s) = J(\pi^\star). At optimal solutions, x(s,a)>0x(s,a) > 0 only where the primal inequality is tight, that is, where aa attains the Bellman maximum: an optimal policy puts weight only on greedy actions. The optimal V⋆V^\star are the shadow prices of the flow constraints, the value of injecting one more unit of starting mass at each state.

The dual explains the shape of the problem. Its feasible set is a polytope with SS equality constraints, so a vertex has at most SS positive entries; with μ>0\mu > 0 every state needs at least one, so vertices are exactly the occupancy measures of deterministic stationary policies, and a linear objective is maximized at one of them. This is a second proof that deterministic optimal policies exist. It also contrasts with value space: the set of value functions of stationary policies is in general a non-convex polytope (Dadashi et al., 2019); for the two-state MDP of the valuespace widget it is a bowtie, two triangles touching at one point. The set of occupancy measures from a fixed start is always convex. Linear-programming formulations of MDPs go back to Manne (1960), and LP methods remain the basis of the known polynomial-time algorithms for MDPs whose discount is part of the input (see Linear Programming for MDPs).

Uses of the dual view

Once a policy is a point dd in a convex polytope and the return is linear in dd, several problems that are awkward in policy space become natural.

  • Constrained MDPs. Expected costs are linear in dd too, so a budget ⟨d,ci⟩≤Ci\langle d, c_i\rangle \le C_i is one more linear constraint and the problem stays a linear program (Altman, 1999). Strong LP duality justifies Lagrangian methods: for a fixed multiplier λ\lambda, the inner problem is an ordinary MDP with reward r−λcr - \lambda c. Basic solutions now have up to S+kS + k positive entries, which is why optimal stationary policies may randomize, in at most kk states.
  • Imitation as occupancy matching. If dμπ=dμπEd^\pi_\mu = d^{\pi_E}_\mu for an expert πE\pi_E, then by the correspondence π\pi agrees with πE\pi_E on every state the expert visits. Apprenticeship learning by linear programming (Syed, Bowling & Schapire, 2008) optimizes directly over occupancy measures, and GAIL (Ho & Ermon, 2016) imitates by matching them, training a discriminator to tell dπd^\pi from dπEd^{\pi_E} and using it as a reward.
  • Convex objectives. Some goals are not linear in dd at all, such as making the state visitation as uniform as possible, max⁡πH(dπ)\max_\pi H\big(d^\pi\big) with HH the entropy. Such convex RL problems maximize a concave function over a convex set, and Zahavy, O'Donoghue, Desjardins and Singh (2021) showed that many of them can be solved as a game between a policy player that solves ordinary MDPs and a player that picks the reward.
DerivationMaximum-entropy exploration by Frank–Wolfe (Hazan et al., 2019)
  1. rk(s)=−log⁡dk(s)⟹πk+1∈arg max⁡π ⟨dπ, rk⟩r_k(s) = -\log d_k(s)\qquad\Longrightarrow\qquad \pi_{k+1} \in \argmax_\pi\ \big\langle d^\pi,\, r_k\big\rangle

    Given the current occupancy dkd_k, linearize the concave objective. The gradient of H(d)=−∑sd(s)log⁡d(s)H(d) = -\sum_s d(s)\log d(s) is −log⁡dk(s)−1-\log d_k(s) - 1, so the linearized problem is an ordinary MDP whose reward is large in rarely visited states:

  2. dk+1=(1−ηk) dk+ηk dπk+1d_{k+1} = (1-\eta_k)\,d_k + \eta_k\,d^{\pi_{k+1}}

    Move part of the way toward the new policy's occupancy. A mixture of policies, choosing πk+1\pi_{k+1} with probability ηk\eta_k at the start of each episode, realizes the mixture of occupancies:

  3. Conditional-gradient analysis then shows convergence to the maximum-entropy occupancy (for a smoothed entropy, whose gradient stays bounded), with an approximate planning oracle and estimated densities, in polynomially many samples and oracle calls. The objective is not concave in the parameters of a single policy; it is the move to occupancy space, where mixtures of policies realize mixtures of occupancies, that makes it tractable.

DerivationOff-policy evaluation by ratio estimation (DualDICE)
  1. E(s,a)∼dπ[f(s,a)−γ f(s′,a′)]=(1−γ) Es0∼μ, a0∼π[f(s0,a0)]\E_{(s,a)\sim d^\pi}\big[f(s,a) - \gamma\,f(s',a')\big] = (1-\gamma)\,\E_{s_0\sim\mu,\ a_0\sim\pi}\big[f(s_0,a_0)\big]

    For a stationary target policy π\pi, the state–action flow constraints, multiplied by any test function ff and summed, give an identity that involves only single transitions (s′∼P(⋅∣s,a)s' \sim P(\cdot \mid s,a) and a′∼π(⋅∣s′)a' \sim \pi(\cdot \mid s')):

  2. min⁡ν 12 E(s,a)∼dD[eν(s,a)2]−(1−γ) Es0∼μ, a0∼π[ν(s0,a0)]\min_\nu\ \tfrac12\,\E_{(s,a)\sim d^D}\big[e_\nu(s,a)^2\big] - (1-\gamma)\,\E_{s_0\sim\mu,\ a_0\sim\pi}\big[\nu(s_0,a_0)\big]

    Let the data come from a distribution dDd^D and write w=dπ/dDw = d^\pi/d^D, so that Edπ[g]=EdD[w g]\E_{d^\pi}[g] = \E_{d^D}[w\,g]. Nachum, Chow, Dai and Li (2019) minimize the objective below over ν\nu; by the identity it equals 12EdD[(eν−w)2]\tfrac12\E_{d^D}\big[(e_\nu - w)^2\big] up to a constant, where eν(s,a)=ν(s,a)−γ E[ν(s′,a′)]e_\nu(s,a) = \nu(s,a) - \gamma\,\E[\nu(s',a')]:

  3. So the minimizer satisfies eν⋆=dπ/dDe_{\nu^\star} = d^\pi/d^D: the correction ratio is learned from logged transitions, without knowing the behavior policy, and J(π)=EdD[w r]/(1−γ)J(\pi) = \E_{d^D}[w\,r]/(1-\gamma). (The square of an expectation inside eν2e_\nu^2 cannot be estimated from one sampled next state; DualDICE removes this double-sampling problem with a Fenchel-dual reformulation.) No product of per-step importance ratios appears, which is how such DICE estimators sidestep the curse of horizon.

Key takeaways

  • The discounted occupancy measure dμπd^\pi_\mu is a probability distribution over state–action pairs, and J(π)=⟨dμπ,r⟩/(1−γ)J(\pi) = \langle d^\pi_\mu, r\rangle/(1-\gamma) is linear in it.
  • Every policy's occupancy satisfies the flow constraints, and every nonnegative solution is the occupancy of the stationary policy πd(a∣s)∝d(s,a)\pi_d(a \mid s) \propto d(s,a); for any objective that depends on the occupancy, stationary policies suffice.
  • Planning is a linear program: the primal over values, the dual over occupancies. Complementary slackness says optimal policies use only greedy actions, and the dual's vertices are deterministic policies.
  • Constraints, imitation by occupancy matching, ratio estimation for off-policy evaluation, and convex objectives such as state entropy all become natural in the dual view.

Check yourself

In the interactive lesson these are scored with your confidence, and what you miss comes back for review.

Question 1

What is ∑s,adμπ(s,a)\sum_{s,a} d^\pi_\mu(s,a), and why?

  1. 1/(1−γ)1/(1-\gamma), the effective horizon
  2. The number of states
  3. 1, because the weights (1−γ)γt(1-\gamma)\gamma^t sum to 1 and each Pr⁡(St=s,At=a)\Pr(S_t = s, A_t = a) sums to 1 over pairs
  4. It depends on the policy
Show the answer

C. 1, because the weights (1−γ)γt(1-\gamma)\gamma^t sum to 1 and each Pr⁡(St=s,At=a)\Pr(S_t = s, A_t = a) sums to 1 over pairs

∑s,adμπ(s,a)=(1−γ)∑tγt⋅1=1\sum_{s,a}d^\pi_\mu(s,a) = (1-\gamma)\sum_t\gamma^t\cdot 1 = 1. Without the factor 1−γ1-\gamma the total would be 1/(1−γ)1/(1-\gamma); the unnormalized version is what appears as the dual variable of the LP.

Question 2

A feasible occupancy measure has d(s,a1)=0.06d(s, a_1) = 0.06, d(s,a2)=0.02d(s, a_2) = 0.02 and d(s,a3)=0d(s, a_3) = 0 in state ss. Which stationary policy produces it?

  1. π(a1∣s)=0.06\pi(a_1 \mid s) = 0.06, with the rest of the probability spread over the other actions
  2. π(⋅∣s)=(0.75,0.25,0)\pi(\cdot \mid s) = (0.75, 0.25, 0)
  3. π(⋅∣s)=(1,0,0)\pi(\cdot \mid s) = (1, 0, 0), the most visited action
  4. It cannot be determined from dd
Show the answer

B. π(⋅∣s)=(0.75,0.25,0)\pi(\cdot \mid s) = (0.75, 0.25, 0)

πd(a∣s)=d(s,a)/∑a′d(s,a′)=0.06/0.08=0.75\pi_d(a \mid s) = d(s,a)/\sum_{a'}d(s,a') = 0.06/0.08 = 0.75 for a1a_1 and 0.02/0.08=0.250.02/0.08 = 0.25 for a2a_2. The state is visited (ν(s)=0.08>0\nu(s) = 0.08 > 0), so this policy is unique there.

Question 3

With μ(s)>0\mu(s) > 0 for every state, why does the dual linear program always have an optimal solution that corresponds to a deterministic policy?

  1. A linear objective attains its optimum at a vertex; a vertex has at most SS positive entries, and the flow constraints force every state to have at least one, so each state has exactly one action
  2. LP solvers return integer solutions
  3. The flow constraints force d(s,a)∈{0,1}d(s,a) \in \{0, 1\}
  4. Randomizing always lowers the objective
Show the answer

A. A linear objective attains its optimum at a vertex; a vertex has at most SS positive entries, and the flow constraints force every state to have at least one, so each state has exactly one action

The feasible set is a polytope in RS×A\R^{S \times A} cut out by SS equality constraints, so a basic feasible solution has at most SS nonzero coordinates. Each flow constraint gives ∑ax(s,a)≥μ(s)>0\sum_a x(s,a) \ge \mu(s) > 0, so each state has exactly one positive action: a deterministic policy. One extra constraint, as in a constrained MDP, allows S+1S + 1 positive entries, and randomization in one state.

Question 4

Why can DICE-style estimators avoid the "curse of horizon" of trajectory-wise importance sampling in off-policy evaluation?

  1. They discard the logged data and simulate new trajectories
  2. They require the behavior policy's probabilities at every step
  3. They use only the first transition of each trajectory
  4. They estimate the occupancy ratio dπ(s,a)/dD(s,a)d^\pi(s,a)/d^D(s,a) directly, one number per state–action pair, instead of multiplying one importance ratio per time step
Show the answer

D. They estimate the occupancy ratio dπ(s,a)/dD(s,a)d^\pi(s,a)/d^D(s,a) directly, one number per state–action pair, instead of multiplying one importance ratio per time step

Trajectory-wise importance weights are products of HH per-step ratios, whose variance can grow exponentially with the horizon. The flow constraints characterize dπd^\pi through single transitions, so the ratio w=dπ/dDw = d^\pi/d^D can be learned from individual (s,a,s′)(s, a, s') samples, and J(π)=EdD[w r]/(1−γ)J(\pi) = \E_{d^D}[w\,r]/(1-\gamma).

Further reading

  • M. L. Puterman, Markov Decision Processes: Discrete Stochastic Dynamic Programming, §6.9 (Wiley, 1994).
  • E. Altman, Constrained Markov Decision Processes (Chapman & Hall/CRC, 1999).
  • J. Ho & S. Ermon, "Generative adversarial imitation learning," NeurIPS (2016).
  • O. Nachum, Y. Chow, B. Dai & L. Li, "DualDICE: Behavior-agnostic estimation of discounted stationary distribution corrections," NeurIPS (2019).
  • E. Hazan, S. M. Kakade, K. Singh & A. Van Soest, "Provably efficient maximum entropy exploration," ICML, PMLR 97, 2681–2691 (2019).