Search

Search papers and lessons

Occupancy Measure

Level 4 · 13 min read

An occupancy measure records how much time a policy spends in each state and action, weighted by how soon it gets there, so a policy's return becomes a single weighted sum.

On this page

  • Colour = topic mix
  • Discrete math
  • Probability
  • Calculus
  • Linear algebra
  • Reinforcement learning
  • Stochastic processes
  • Sign in to see your progress

TL;DR

An agent in Reinforcement Learning that follows a policy visits some states often and others rarely. The occupancy measure is the distribution of those visits: for each state and action, how much weight the policy puts there, with visits near the start counting for more than visits far in the future. Once you have it, the expected discounted return of the policy is a single weighted sum of rewards, and the constraints on which visit patterns are possible become a set of linear equations.

Where you'll see it: the policy-gradient derivations in Policy Gradient, Linear Programming formulations of planning problems, and imitation learning methods such as GAIL and Inverse Reinforcement Learning that match the visit patterns of an expert.

Intuition

Start from a question you already know

Imagine a robot vacuum in a flat with three rooms. Over one week, you could record every room it enters, minute by minute. A tally of those visits tells you where the robot spends its time. That tally is a distribution: it adds to one, and a room the robot visits often has a big share.

Now suppose the robot is judged on rewards, such as dust collected. A dust pile in the kitchen is worth as much as one in the hall, but it matters when the robot reaches it: collecting dust in five minutes is better than collecting it in five days, because the flat may get dirtier and the robot's battery may run out. A plain tally treats all visits equally. An occupancy measure does not: a visit soon after the start counts for more than a visit far in the future.

The picture

Think of the occupancy measure as a histogram of visits in which each visit at time tt is shrunk by a factor γt\gamma^t. A discount factor γ\gamma close to 0 piles almost all the weight onto the first few steps. A factor close to 1 spreads the weight across a long horizon, so the histogram approaches the ordinary long-run visit frequency.

Why does this object exist? A policy is judged by an expected sum of rewards over a whole trajectory of states and actions. That sum is hard to handle directly, because it depends on the path. The occupancy measure collapses all the paths into one object that answers the same question. Every policy then becomes a point in a space of distributions, and the value of a policy is a weighted average of rewards under that distribution.

The math

Setup

Use the notation of the Markov Decision Process: a set of states S\mathcal{S}, a set of actions A\mathcal{A}, transition probabilities P(s′∣s,a)P(s' \mid s, a), a reward function r(s,a)r(s, a), and a discount factor γ∈[0,1)\gamma \in [0, 1). A policy π(a∣s)\pi(a \mid s) gives the probability of choosing action aa in state ss. Let μ\mu be the distribution of the starting state. Under policy π\pi, write Pr⁡(st=s)\Pr(s_t = s) for the probability that the state at time tt is ss, and Pr⁡(st=s,at=a)\Pr(s_t = s, a_t = a) for the probability that the state at time tt is ss and the action is aa. Both depend on π\pi and μ\mu.

The expected discounted return of the policy (the Expected Value of the discounted sum of rewards) is

J(π)=E ⁣[∑t=0∞γtr(st,at)].J(\pi) = \mathbb{E}\!\left[\sum_{t=0}^{\infty} \gamma^t r(s_t, a_t)\right].

Here E\mathbb{E} is an expectation over trajectories generated by π\pi, and γt\gamma^t is the weight on the reward at step tt.

Definition

Definition: Discounted occupancy measure

The discounted state-action occupancy measure of π\pi is

dπ(s,a)=(1−γ)∑t=0∞γtPr⁡(st=s,at=a),d^\pi(s, a) = (1 - \gamma) \sum_{t=0}^{\infty} \gamma^t \Pr(s_t = s, a_t = a),

and its state marginal (the same distribution with the action summed out, so it only tracks which state) is dπ(s)=∑adπ(s,a)d^\pi(s) = \sum_a d^\pi(s, a).

Read the definition in words. For each step tt, take the probability that the agent is at (s,a)(s, a) at that step, multiply by γt\gamma^t, and add over all steps. The factor (1−γ)(1 - \gamma) normalises the total. Since ∑tγt=11−γ\sum_{t} \gamma^t = \frac{1}{1-\gamma} (a Geometric Series), the weights add to one:

∑s,adπ(s,a)=(1−γ)∑tγt⋅1=1.\sum_{s, a} d^\pi(s, a) = (1 - \gamma) \sum_t \gamma^t \cdot 1 = 1.

So dπd^\pi is a genuine probability distribution over state-action pairs, even though it is built from an infinite sum.

The return as a weighted reward

The central identity is that the return is the reward averaged under dπd^\pi:

J(π)=11−γ∑s,adπ(s,a) r(s,a).J(\pi) = \frac{1}{1 - \gamma} \sum_{s, a} d^\pi(s, a)\, r(s, a).

The derivation takes two lines. Swap the sum over time with the expectation, and group terms by state and action:

J(π)=∑tγt∑s,aPr⁡(st=s,at=a) r(s,a)=∑s,ar(s,a)∑tγtPr⁡(st=s,at=a).J(\pi) = \sum_{t} \gamma^t \sum_{s,a} \Pr(s_t = s, a_t = a)\, r(s, a) = \sum_{s,a} r(s, a) \sum_t \gamma^t \Pr(s_t = s, a_t = a).

The inner sum is dπ(s,a)/(1−γ)d^\pi(s, a) / (1 - \gamma) by definition, which gives the identity.

The flow equation

The occupancy measure cannot be any distribution. It must respect how the agent moves. Each visit to a state either starts the process or is the result of a transition from the previous step. This gives a linear constraint, sometimes called the Bellman flow equation:

∑adπ(s,a)=(1−γ) μ(s)+γ∑s′,a′P(s∣s′,a′) dπ(s′,a′).\sum_a d^\pi(s, a) = (1 - \gamma)\, \mu(s) + \gamma \sum_{s', a'} P(s \mid s', a')\, d^\pi(s', a').

The left side is the discounted time spent in ss. The right side says the same time comes from two sources: the start (weighted by 1−γ1 - \gamma) and the transitions into ss from earlier visits (weighted by γ\gamma). A distribution dd that satisfies this equation for every state, and is non-negative and sums to one, is the occupancy measure of some policy. The policy is recovered by π(a∣s)=d(s,a)/∑a′d(s,a′)\pi(a \mid s) = d(s, a) / \sum_{a'} d(s, a').

Derive the flow equation once to see that the constraint is exact. Starting from the definition, sum over actions and split off the t=0t = 0 term:

dπ(s)=(1−γ)μ(s)+(1−γ)∑t≥1γtPr⁡(st=s).d^\pi(s) = (1 - \gamma)\mu(s) + (1 - \gamma)\sum_{t \geq 1} \gamma^t \Pr(s_t = s).

Shift the index in the second sum, using Pr⁡(st+1=s)=∑s′,a′P(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'), and factor out γ\gamma. The sum over tt then rebuilds dπd^\pi, giving the equation above.

Linear programming view

Combining the two identities, the best policy maximises a linear function over a polytope (a region cut out by linear equalities and inequalities), which is the form of a linear program:

max⁡d≥011−γ∑s,ad(s,a) r(s,a)subject to the flow equation and∑s,ad(s,a)=1.\max_{d \geq 0} \frac{1}{1 - \gamma} \sum_{s,a} d(s,a)\, r(s,a) \quad \text{subject to the flow equation and} \sum_{s,a} d(s,a) = 1.

Every feasible dd corresponds to a policy, so solving this linear program solves the decision problem. The variables are the occupancies, not the policy. This is why occupancy measures appear both in planning and in methods that match an expert's visit pattern.

Worked examples

Example
1. A two-state chain with one action

The agent starts in state s0s_0 with probability 1. It moves to s1s_1 on the first step and stays there forever. The discount is γ=0.5\gamma = 0.5. The reward is r(s0)=0r(s_0) = 0 and r(s1)=1r(s_1) = 1.

  1. Visit probabilities: Pr⁡(s0)=1\Pr(s_0) = 1 at t=0t = 0, and Pr⁡(s1)=1\Pr(s_1) = 1 at every t≥1t \geq 1.
  2. Occupancy of s0s_0: d(s0)=(1−0.5)⋅1=0.5d(s_0) = (1 - 0.5) \cdot 1 = 0.5.
  3. Occupancy of s1s_1: d(s1)=0.5∑t≥10.5t=0.5⋅1=0.5d(s_1) = 0.5 \sum_{t \geq 1} 0.5^t = 0.5 \cdot 1 = 0.5. The two values add to 1.
  4. Flow check for s1s_1: the right side is (1−γ)⋅0+γ⋅(incoming)(1 - \gamma) \cdot 0 + \gamma \cdot (\text{incoming}), where the incoming term sums P(s1∣s0)d(s0)+P(s1∣s1)d(s1)=0.5+0.5=1P(s_1 \mid s_0) d(s_0) + P(s_1 \mid s_1) d(s_1) = 0.5 + 0.5 = 1. So 0.5⋅1=0.50.5 \cdot 1 = 0.5, which matches d(s1)d(s_1).
  5. Return from the identity: J=10.5⋅0.5⋅1=1J = \frac{1}{0.5} \cdot 0.5 \cdot 1 = 1.
  6. Return checked directly: rewards of 1 occur at t≥1t \geq 1, so ∑t≥10.5t=1\sum_{t \geq 1} 0.5^t = 1.

Interpretation: the two methods agree. The occupancy measure gives the return without tracking paths, and the direct sum confirms it.

Example
2. A chain that returns to the start

The chain has two states, AA and BB, and one action. From AA, the agent stays in AA with probability 0.5 and moves to BB with probability 0.5. From BB, it always moves to AA. The agent starts in AA, and γ=0.5\gamma = 0.5. The reward is r(A)=1r(A) = 1 and r(B)=0r(B) = 0.

  1. Let xt=Pr⁡(st=A)x_t = \Pr(s_t = A). Then x0=1x_0 = 1, and xt+1=0.5 xt+1⋅(1−xt)=1−0.5 xtx_{t+1} = 0.5 \, x_t + 1 \cdot (1 - x_t) = 1 - 0.5 \, x_t.
  2. Solve the recurrence: xt=23+13(−0.5)tx_t = \frac{2}{3} + \frac{1}{3}(-0.5)^t. Check: x0=1x_0 = 1 and x1=0.5x_1 = 0.5, as the recurrence requires.
  3. Occupancy of AA: d(A)=(1−0.5)∑t0.5txt=0.5[23⋅2+13⋅11.25]=0.5⋅1.6=0.8d(A) = (1 - 0.5) \sum_t 0.5^t x_t = 0.5 \left[\frac{2}{3} \cdot 2 + \frac{1}{3} \cdot \frac{1}{1.25}\right] = 0.5 \cdot 1.6 = 0.8.
  4. Occupancy of BB: d(B)=1−0.8=0.2d(B) = 1 - 0.8 = 0.2.
  5. Flow check for AA: the right side is 0.5⋅1+0.5[0.5⋅0.8+1⋅0.2]=0.5+0.3=0.80.5 \cdot 1 + 0.5 \left[0.5 \cdot 0.8 + 1 \cdot 0.2\right] = 0.5 + 0.3 = 0.8, which matches.
  6. Return: J=10.5⋅0.8⋅1=1.6J = \frac{1}{0.5} \cdot 0.8 \cdot 1 = 1.6.

Interpretation: the agent spends 80 percent of its discounted time in AA, even though the long-run share is two thirds. The early visits carry the most weight, and the start state is visited at t=0t = 0, so it dominates the histogram.

Example
3. The same chain with a patient agent

Keep the chain from example 2, but raise the discount to γ=0.9\gamma = 0.9, so the agent cares about the long run. Everything else is unchanged.

  1. The occupancy of AA is d(A)=0.1∑t0.9txtd(A) = 0.1 \sum_t 0.9^t x_t. Using the same formula for xtx_t, the sum splits into two pieces: 23∑t0.9t=23⋅10=203\frac{2}{3} \sum_t 0.9^t = \frac{2}{3} \cdot 10 = \frac{20}{3}, and 13∑t(0.9⋅(−0.5))t=13⋅11.45=2087\frac{1}{3} \sum_t (0.9 \cdot (-0.5))^t = \frac{1}{3} \cdot \frac{1}{1.45} = \frac{20}{87}.
  2. Add the pieces: 203+2087=60087=20029\frac{20}{3} + \frac{20}{87} = \frac{600}{87} = \frac{200}{29}.
  3. Multiply by 0.1: d(A)=2029≈0.690d(A) = \frac{20}{29} \approx 0.690, and d(B)=929≈0.310d(B) = \frac{9}{29} \approx 0.310.
  4. Flow check for AA: 0.1⋅1+0.9[0.5⋅2029+929]=0.1+0.9⋅1929=20290.1 \cdot 1 + 0.9 \left[0.5 \cdot \frac{20}{29} + \frac{9}{29}\right] = 0.1 + 0.9 \cdot \frac{19}{29} = \frac{20}{29}, which matches.
  5. Return: J=10.1⋅2029⋅1=20029≈6.90J = \frac{1}{0.1} \cdot \frac{20}{29} \cdot 1 = \frac{200}{29} \approx 6.90.

Interpretation: raising γ\gamma from 0.5 to 0.9 moved weight from the start state (0.8 down to 0.69) toward the long-run share of two thirds. The return grew because later rewards now count for more. The same policy has a different occupancy measure under each discount, so comparing policies at a fixed discount is essential.

Tradeoffs and failure modes

SituationWhat goes wrongBetter choice
The discount is very close to 1Occupancy weights become spread over a long horizon, and the normalising factor (1−γ)(1 - \gamma) becomes small, so numbers get large and unstableUse an average-reward formulation, or compute with a moderate discount and check sensitivity
The state space is largeStoring d(s,a)d(s, a) for every pair is impossible, and the flow equation has one constraint per stateUse function approximation, and estimate occupancy from samples instead of solving for it
Sampled trajectories are usedEstimates of dd from a few episodes are noisy, and they are biased if the sampling policy differs from π\piUse [[importance-sampling
The objective is a return, but the constraint is on ddWith function approximation, the flow equation no longer holds exactlyAccept approximate flow constraints, or work with value functions instead, as in Value Function
The goal is imitationMatching occupancies is stronger than matching actions in a few statesUse occupancy matching when the expert's long-run behaviour matters; plain Behavior Cloning copies actions state by state and drifts into states the expert never visited

Occupancy measures are not always the right tool. For a single policy, computing the return directly from value functions is usually simpler. The occupancy view pays off when you want to reason about many policies at once, such as in linear programming or when comparing a learner's visit pattern with an expert's.

History

The linear-programming view of Markov decision processes is older than most of the modern methods. Alan Manne showed in 1960 that sequential decision problems with a discount could be written as linear programs. The idea of looking at the distribution of visits, rather than the policy, grew from that work and from the analysis of Markov chains. Martin Puterman's 1994 book Markov Decision Processes gives a standard treatment of the discounted and average-reward cases.

The use of occupancy measures in modern reinforcement learning came from imitation learning. Ho and Ermon (2016) framed imitation as matching the occupancy measure of an expert, which led to generative adversarial imitation learning. The same objects appear in the derivation of policy gradients, where the gradient of the return is an expectation over the occupancy of the current policy.

Recap

A policy's behaviour can be summarised by its discounted occupancy measure dπ(s,a)d^\pi(s, a), a distribution that weights each visit by γt\gamma^t. The return is the reward averaged under dπd^\pi, scaled by 11−γ\frac{1}{1 - \gamma}. The occupancy measures that are possible satisfy the flow equation, which is a set of linear constraints tying visits to transitions. Together these turn policy choice into a linear program. The Value Function and the Bellman Equation give an equivalent view from the other side, scoring states rather than visit counts. The derivation of Policy Gradient methods uses the occupancy of the current policy.

Check yourself

QuestionWhy is the discounted occupancy measure a probability distribution even though it is built from an infinite sum over time?Show answer

The weights γt\gamma^t form a geometric series that sums to 11−γ\frac{1}{1 - \gamma} for 0≤γ<10 \leq \gamma \lt 1. The factor (1−γ)(1 - \gamma) in the definition cancels that sum, so the total mass is exactly 1. Each step's visits are counted once, weighted by how far in the future they happen.

QuestionA policy has occupancy d(A)=0.8d(A) = 0.8 and d(B)=0.2d(B) = 0.2, and rewards r(A)=1r(A) = 1, r(B)=0r(B) = 0, with γ=0.5\gamma = 0.5. What is its expected discounted return?Show answer

J=11−γ∑s,ad(s,a) r(s,a)=10.5⋅(0.8⋅1+0.2⋅0)=2⋅0.8=1.6J = \frac{1}{1 - \gamma} \sum_{s,a} d(s,a)\, r(s,a) = \frac{1}{0.5} \cdot (0.8 \cdot 1 + 0.2 \cdot 0) = 2 \cdot 0.8 = 1.6. Interpretation: the policy earns 1.6 units of discounted reward, from a reward of 1 each time it is in AA, weighted by how early those visits happen.

QuestionWhy does a discount close to 1 make the occupancy measure look more like the long-run visit frequency, while a discount close to 0 favours the start?Show answer

With γ\gamma near 0, the weights γt\gamma^t drop to near zero after a few steps, so only the first visits matter, and the start state dominates. With γ\gamma near 1, the weights decay slowly, so visits far into the future count nearly as much as early ones, and the histogram approaches the long-run average of the chain.

QuestionWhy can the flow equation be seen as a constraint that every occupancy measure must satisfy?Show answer

Each visit to a state either comes from the starting distribution or from a transition out of an earlier state. The equation writes that balance: the discounted time spent in a state equals the start weight (1−γ)μ(s)(1 - \gamma) \mu(s) plus the discounted probability of arriving there from earlier. A distribution that breaks this balance cannot have come from following any policy, so it is not an occupancy measure.

Sources