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 is shrunk by a factor . A discount factor 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 , a set of actions , transition probabilities , a reward function , and a discount factor . A policy gives the probability of choosing action in state . Let be the distribution of the starting state. Under policy , write for the probability that the state at time is , and for the probability that the state at time is and the action is . Both depend on and .
The expected discounted return of the policy (the Expected Value of the discounted sum of rewards) is
Here is an expectation over trajectories generated by , and is the weight on the reward at step .
Definition
The discounted state-action occupancy measure of is
and its state marginal (the same distribution with the action summed out, so it only tracks which state) is .
Read the definition in words. For each step , take the probability that the agent is at at that step, multiply by , and add over all steps. The factor normalises the total. Since (a Geometric Series), the weights add to one:
So 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 :
The derivation takes two lines. Swap the sum over time with the expectation, and group terms by state and action:
The inner sum is 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:
The left side is the discounted time spent in . The right side says the same time comes from two sources: the start (weighted by ) and the transitions into from earlier visits (weighted by ). A distribution 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 .
Derive the flow equation once to see that the constraint is exact. Starting from the definition, sum over actions and split off the term:
Shift the index in the second sum, using , and factor out . The sum over then rebuilds , 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:
Every feasible 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
The agent starts in state with probability 1. It moves to on the first step and stays there forever. The discount is . The reward is and .
- Visit probabilities: at , and at every .
- Occupancy of : .
- Occupancy of : . The two values add to 1.
- Flow check for : the right side is , where the incoming term sums . So , which matches .
- Return from the identity: .
- Return checked directly: rewards of 1 occur at , so .
Interpretation: the two methods agree. The occupancy measure gives the return without tracking paths, and the direct sum confirms it.
The chain has two states, and , and one action. From , the agent stays in with probability 0.5 and moves to with probability 0.5. From , it always moves to . The agent starts in , and . The reward is and .
- Let . Then , and .
- Solve the recurrence: . Check: and , as the recurrence requires.
- Occupancy of : .
- Occupancy of : .
- Flow check for : the right side is , which matches.
- Return: .
Interpretation: the agent spends 80 percent of its discounted time in , even though the long-run share is two thirds. The early visits carry the most weight, and the start state is visited at , so it dominates the histogram.
Keep the chain from example 2, but raise the discount to , so the agent cares about the long run. Everything else is unchanged.
- The occupancy of is . Using the same formula for , the sum splits into two pieces: , and .
- Add the pieces: .
- Multiply by 0.1: , and .
- Flow check for : , which matches.
- Return: .
Interpretation: raising 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
| Situation | What goes wrong | Better choice |
|---|---|---|
| The discount is very close to 1 | Occupancy weights become spread over a long horizon, and the normalising factor becomes small, so numbers get large and unstable | Use an average-reward formulation, or compute with a moderate discount and check sensitivity |
| The state space is large | Storing for every pair is impossible, and the flow equation has one constraint per state | Use function approximation, and estimate occupancy from samples instead of solving for it |
| Sampled trajectories are used | Estimates of from a few episodes are noisy, and they are biased if the sampling policy differs from | Use [[importance-sampling |
| The objective is a return, but the constraint is on | With function approximation, the flow equation no longer holds exactly | Accept approximate flow constraints, or work with value functions instead, as in Value Function |
| The goal is imitation | Matching occupancies is stronger than matching actions in a few states | Use 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 , a distribution that weights each visit by . The return is the reward averaged under , scaled by . 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 form a geometric series that sums to for . The factor 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 and , and rewards , , with . What is its expected discounted return?Show answer
. Interpretation: the policy earns 1.6 units of discounted reward, from a reward of 1 each time it is in , 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 near 0, the weights drop to near zero after a few steps, so only the first visits matter, and the start state dominates. With 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 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.