24  Average Reward MDPs

Core idea. Discounted reinforcement learning values near-term reward more than distant reward. In many continuing systems, however, there is no natural terminal time and no natural discount factor. The mathematically natural objective is the long-run average reward:

\[ \rho^{\pi}(s) = \lim_{T\to\infty} \frac{1}{T} \mathbb E_{\pi,s} \left[ \sum_{t=0}^{T-1} R_{t+1} \right], \]

when the limit exists. Average-reward theory replaces discounted value functions by two objects: the gain \(\rho^{\pi}\), which measures long-run reward per time step, and the bias or differential value \(h^{\pi}\), which measures transient advantage relative to that long-run rate.

24.1 Learning goals

After reading this chapter, students should be able to:

  1. distinguish discounted, finite-horizon, and average-reward objectives;
  2. define the average reward, gain, and bias functions for a fixed policy;
  3. compute average reward from the stationary distribution of a policy-induced Markov chain;
  4. derive the Poisson equation for the bias function;
  5. explain why the bias function is unique only up to an additive constant;
  6. solve the average-reward policy-evaluation equation by imposing a normalization condition;
  7. state the average-reward Bellman optimality equation;
  8. implement relative value iteration and average-reward policy iteration in finite examples;
  9. connect average-reward values to the limiting behavior of discounted values as \(\gamma\uparrow 1\);
  10. explain unichain and multichain issues;
  11. implement a simple sample-based average-reward learning method;
  12. use AI tools to audit recurrence assumptions, normalizations, and algorithmic update equations.

24.2 21.1 Why average reward?

The discounted objective is

\[ V_{\gamma}^{\pi}(s) = \mathbb E_{\pi,s} \left[ \sum_{t=0}^{\infty}\gamma^t R_{t+1} \right], \qquad 0<\gamma<1. \]

Discounting is mathematically convenient because it makes the Bellman operator a contraction. But in a continuing system, discounting may introduce an artificial preference for short-term reward. Examples include queueing systems, inventory control, machine maintenance, online recommendation, server allocation, and repeated portfolio rebalancing. In such settings, the system does not naturally terminate; it runs indefinitely.

The average-reward objective asks for reward per unit time:

\[ \rho^{\pi}(s) = \lim_{T\to\infty} \frac{1}{T} \mathbb E_{\pi,s} \left[ \sum_{t=0}^{T-1}R_{t+1} \right]. \]

The key difference is this:

Objective Quantity optimized Mathematical behavior
Finite horizon \(\mathbb E[\sum_{t=0}^{T-1}R_{t+1}]\) time-dependent dynamic programming
Discounted \(\mathbb E[\sum_{t=0}^{\infty}\gamma^tR_{t+1}]\) contraction with factor \(\gamma\)
Average reward \(\lim_{T\to\infty}T^{-1}\mathbb E[\sum_{t=0}^{T-1}R_{t+1}]\) ergodic and Poisson-equation methods

In discounted RL, the main unknown is a value function. In average-reward RL, the main unknowns are a scalar long-run reward rate \(\rho\) and a relative value function \(h\).

The interactive figure compares the discounted value scale with the average-reward scale. As \(\gamma\) approaches one, discounted values often grow like \(\rho/(1-\gamma)\). The average reward \(\rho\) remains finite.

24.3 21.2 Fixed-policy average reward

Consider a finite MDP

\[ \mathcal M=(\mathcal S,\mathcal A,P,r), \]

where \(\mathcal S=\{1,\ldots,n\}\) and \(\mathcal A(s)\) is finite. A stationary randomized policy \(\pi(a\mid s)\) induces a Markov chain with transition matrix

\[ P_{\pi}(s,s') = \sum_{a\in\mathcal A(s)}\pi(a\mid s)P(s'\mid s,a), \]

and expected one-step reward

\[ r_{\pi}(s) = \sum_{a\in\mathcal A(s)}\pi(a\mid s)r(s,a). \]

If the induced chain has stationary distribution \(d_{\pi}\), then under standard ergodicity assumptions the average reward is

\[ \rho^{\pi} = \sum_{s\in\mathcal S}d_{\pi}(s)r_{\pi}(s) = d_{\pi}^T r_{\pi}. \]

This formula is one of the cleanest mathematical facts in average-reward RL. It says that the long-run reward is the stationary average of the one-step reward.

Average reward under an ergodic policy. Suppose \(P_{\pi}\) is irreducible and aperiodic on a finite state space, with stationary distribution \(d_{\pi}\). Then

\[ \rho^{\pi}(s) = \sum_{x} d_{\pi}(x)r_{\pi}(x) \]

for every initial state \(s\).

Reason. The Markov chain spends asymptotic fraction \(d_{\pi}(x)\) of time in state \(x\). Therefore the long-run average reward is the stationary expectation of \(r_{\pi}\).

This result explains why average-reward RL requires more Markov-chain structure than discounted RL. Discounted values are well defined for every bounded reward function and every finite transition matrix when \(\gamma<1\). Average reward depends on limiting state frequencies.

24.3.1 Python example: stationary distribution and average reward

import numpy as np

P_pi = np.array([
    [0.70, 0.25, 0.05],
    [0.20, 0.60, 0.20],
    [0.10, 0.30, 0.60]
])
r_pi = np.array([1.0, 2.0, 4.0])

# Solve d^T P = d^T with sum(d)=1.
n = P_pi.shape[0]
A = np.vstack([P_pi.T - np.eye(n), np.ones(n)])
b = np.append(np.zeros(n), 1.0)
d_pi, *_ = np.linalg.lstsq(A, b, rcond=None)

rho_pi = d_pi @ r_pi
print("stationary distribution:", np.round(d_pi, 4))
print("average reward rho:", round(rho_pi, 4))
print("stationarity check:", np.round(d_pi @ P_pi - d_pi, 12))
stationary distribution: [0.3509 0.4035 0.2456]
average reward rho: 2.1404
stationarity check: [ 0.  0. -0.]

24.4 21.3 The bias function

The average reward \(\rho^{\pi}\) tells us the long-run reward per step. It does not tell us which states are temporarily better or worse. For this we need the bias function, also called the differential value function.

One useful definition is

\[ h^{\pi}(s) = \mathbb E_{\pi,s} \left[ \sum_{t=0}^{\infty} \left(R_{t+1}-\rho^{\pi}\right) \right], \]

when this centered infinite sum is well defined. The summand subtracts the long-run average reward rate. The bias therefore measures transient advantage relative to steady-state behavior.

Applying first-step analysis gives

\[ h^{\pi}(s) = r_{\pi}(s)-\rho^{\pi} + \sum_{s'}P_{\pi}(s,s')h^{\pi}(s'). \]

Equivalently,

\[ \rho^{\pi}+h^{\pi}(s) = r_{\pi}(s)+\sum_{s'}P_{\pi}(s,s')h^{\pi}(s'). \]

In vector form,

\[ \rho^{\pi}\mathbf 1+h^{\pi} = r_{\pi}+P_{\pi}h^{\pi}. \]

This is a discrete Poisson equation for the Markov chain.

The bias function is not unique. If \(h^{\pi}\) solves the Poisson equation, then \(h^{\pi}+c\mathbf 1\) also solves it for any constant \(c\). Only differences \(h^{\pi}(s)-h^{\pi}(x)\) are meaningful.

To obtain a unique solution, impose a normalization such as

\[ h^{\pi}(s_0)=0 \]

for a reference state \(s_0\), or

\[ d_{\pi}^T h^{\pi}=0. \]

The plot shows reward, centered reward, and bias. Positive bias means that starting from that state gives a favorable transient position compared with the average long-run reward rate.

24.4.1 Python example: solving the Poisson equation

The following example imposes the normalization \(h(s_0)=0\) by replacing one equation with the constraint.

import numpy as np

P = np.array([
    [0.70, 0.25, 0.05],
    [0.20, 0.60, 0.20],
    [0.10, 0.30, 0.60]
])
r = np.array([1.0, 2.0, 4.0])

n = len(r)
A = np.vstack([P.T - np.eye(n), np.ones(n)])
b = np.append(np.zeros(n), 1.0)
d, *_ = np.linalg.lstsq(A, b, rcond=None)
rho = d @ r

# Solve (I - P) h = r - rho 1 with h[0] = 0.
M = np.eye(n) - P
c = r - rho * np.ones(n)
M[0, :] = 0.0
M[0, 0] = 1.0
c[0] = 0.0
h = np.linalg.solve(M, c)

print("rho:", round(rho, 4))
print("bias h with h[0]=0:", np.round(h, 4))
print("Poisson residual:", np.round(rho + h - (r + P @ h), 12))
rho: 2.1404
bias h with h[0]=0: [0.     3.1579 7.0175]
Poisson residual: [0. 0. 0.]

24.5 21.4 Matrix form and the fundamental matrix

For an ergodic finite chain, define the rank-one matrix

\[ \Pi=\mathbf 1 d_{\pi}^T. \]

The matrix \(\Pi\) maps any vector to a constant vector equal to its stationary mean. The centered reward vector is

\[ g=r_{\pi}-\rho^{\pi}\mathbf 1. \]

Since \(d_{\pi}^Tg=0\), the Poisson equation can be solved on the subspace of mean-zero functions. A common formula uses the fundamental matrix

\[ Z_{\pi} = (I-P_{\pi}+\Pi)^{-1}. \]

Then the mean-zero bias is

\[ h^{\pi}=Z_{\pi}(r_{\pi}-\rho^{\pi}\mathbf 1), \]

and it satisfies \(d_{\pi}^T h^{\pi}=0\).

This formula is useful theoretically, but for large state spaces one usually avoids forming the inverse. Instead, iterative methods and stochastic approximation methods are used.

24.5.1 Python example: mean-zero bias using the fundamental matrix

import numpy as np

P = np.array([
    [0.70, 0.25, 0.05],
    [0.20, 0.60, 0.20],
    [0.10, 0.30, 0.60]
])
r = np.array([1.0, 2.0, 4.0])
n = len(r)

A = np.vstack([P.T - np.eye(n), np.ones(n)])
b = np.append(np.zeros(n), 1.0)
d, *_ = np.linalg.lstsq(A, b, rcond=None)
rho = d @ r
Pi = np.ones((n, 1)) @ d.reshape(1, -1)
Z = np.linalg.inv(np.eye(n) - P + Pi)
h_mean_zero = Z @ (r - rho * np.ones(n))

print("rho:", round(rho, 4))
print("mean-zero bias:", np.round(h_mean_zero, 4))
print("stationary mean of h:", round(d @ h_mean_zero, 12))
rho: 2.1404
mean-zero bias: [-2.9978  0.16    4.0197]
stationary mean of h: -0.0

24.6 21.5 Connection with discounted value functions

Average reward can be understood as a limiting version of discounted reward. For a fixed policy, the discounted value satisfies

\[ V_{\gamma}^{\pi} = r_{\pi}+\gamma P_{\pi}V_{\gamma}^{\pi}. \]

As \(\gamma\uparrow 1\), \(V_{\gamma}^{\pi}\) usually diverges. The leading divergence is the average reward term:

\[ V_{\gamma}^{\pi}(s) \approx \frac{\rho^{\pi}}{1-\gamma}+h^{\pi}(s)+C_{\gamma}. \]

More precisely, under standard ergodicity assumptions,

\[ \lim_{\gamma\uparrow 1}(1-\gamma)V_{\gamma}^{\pi}(s)=\rho^{\pi}. \]

Bias differences can also be recovered:

\[ \lim_{\gamma\uparrow 1} \left(V_{\gamma}^{\pi}(s)-V_{\gamma}^{\pi}(s_0)\right) = h^{\pi}(s)-h^{\pi}(s_0). \]

This relationship is useful pedagogically because it shows that average-reward RL is not unrelated to discounted RL. It is the limiting case where the discount factor approaches one, but the limit must be normalized correctly.

24.6.1 Python example: discounted values approaching gain and bias

import numpy as np

P = np.array([
    [0.70, 0.25, 0.05],
    [0.20, 0.60, 0.20],
    [0.10, 0.30, 0.60]
])
r = np.array([1.0, 2.0, 4.0])

gammas = [0.5, 0.8, 0.9, 0.97, 0.99, 0.995]
for gamma in gammas:
    V = np.linalg.solve(np.eye(3) - gamma * P, r)
    gain_est = (1 - gamma) * V
    relative = V - V[0]
    print(
        f"gamma={gamma:0.3f}",
        "(1-gamma)V=", np.round(gain_est, 4),
        "relative V=", np.round(relative, 4)
    )
gamma=0.500 (1-gamma)V= [1.304  2.1006 3.4004] relative V= [0.     1.5933 4.1929]
gamma=0.800 (1-gamma)V= [1.6827 2.1446 2.7871] relative V= [0.     2.3092 5.5221]
gamma=0.900 (1-gamma)V= [1.8805 2.1483 2.4985] relative V= [0.     2.6777 6.1792]
gamma=0.970 (1-gamma)V= [2.0544 2.1443 2.2566] relative V= [0.     2.9993 6.7427]
gamma=0.990 (1-gamma)V= [2.1108 2.1419 2.1801] relative V= [0.     3.1035 6.9234]
gamma=0.995 (1-gamma)V= [2.1255 2.1411 2.1603] relative V= [0.     3.1305 6.9702]

24.7 21.6 Optimality equations for average reward

For control, we want a policy with maximal long-run average reward. Under appropriate finite unichain assumptions, there exists a scalar \(\rho^*\) and a bias function \(h^*\) satisfying the average-reward Bellman optimality equation

\[ \rho^*+h^*(s) = \max_{a\in\mathcal A(s)} \left[ r(s,a)+\sum_{s'}P(s'\mid s,a)h^*(s') \right]. \]

A greedy policy with respect to \(h^*\) is any policy satisfying

\[ \pi^*(s) \in \operatorname*{argmax}_{a\in\mathcal A(s)} \left[ r(s,a)+\sum_{s'}P(s'\mid s,a)h^*(s') \right]. \]

The scalar \(\rho^*\) is the optimal gain. The function \(h^*\) describes transient preference among states.

Notice the contrast with the discounted optimality equation:

\[ V^*(s) = \max_a \left[ r(s,a)+\gamma\sum_{s'}P(s'\mid s,a)V^*(s') \right]. \]

In average reward, there is no factor \(\gamma\). Instead, the scalar \(\rho^*\) appears on the left side.

Average-reward optimality is invariant under adding a constant to \(h^*\). This is why algorithms must normalize the bias after each iteration.

24.8 21.7 Relative value iteration

A natural dynamic-programming algorithm is relative value iteration. Define the optimality backup

\[ (Th)(s) = \max_a \left[ r(s,a)+\sum_{s'}P(s'\mid s,a)h(s') \right]. \]

Since \(h\) is only defined up to an additive constant, choose a reference state \(s_0\) and update

\[ h_{k+1}(s) = (Th_k)(s)-(Th_k)(s_0). \]

The subtracted term normalizes the new bias so that \(h_{k+1}(s_0)=0\). The corresponding gain estimate can be tracked by

\[ \rho_{k+1}=(Th_k)(s_0). \]

This algorithm resembles value iteration, but it removes the arbitrary additive drift at every step.

Relative value iteration

  1. Choose a reference state \(s_0\) and initialize \(h_0\).
  2. Compute \(u_{k+1}=Th_k\).
  3. Normalize \(h_{k+1}=u_{k+1}-u_{k+1}(s_0)\mathbf 1\).
  4. Track \(\rho_{k+1}=u_{k+1}(s_0)\).
  5. Stop when the span seminorm of \(h_{k+1}-h_k\) is small.

The relevant norm is often the span seminorm

\[ \operatorname{span}(v)=\max_s v(s)-\min_s v(s). \]

The span seminorm ignores additive constants and is therefore natural for bias functions.

24.8.1 Python example: relative value iteration

import numpy as np

# Two actions for each of three states.
P = np.array([
    [[0.70, 0.25, 0.05], [0.40, 0.50, 0.10]],
    [[0.20, 0.60, 0.20], [0.10, 0.60, 0.30]],
    [[0.10, 0.30, 0.60], [0.05, 0.25, 0.70]]
])
r = np.array([
    [1.0, 0.8],
    [2.0, 2.4],
    [3.0, 3.2]
])

n_states, n_actions = r.shape
ref = 0
h = np.zeros(n_states)

for k in range(80):
    q = np.zeros((n_states, n_actions))
    for s in range(n_states):
        for a in range(n_actions):
            q[s, a] = r[s, a] + P[s, a] @ h
    u = q.max(axis=1)
    h_new = u - u[ref]
    rho_est = u[ref]
    if np.max(h_new - h) - np.min(h_new - h) < 1e-10:
        break
    h = h_new

policy = q.argmax(axis=1)
print("iterations:", k + 1)
print("estimated optimal gain:", round(rho_est, 4))
print("bias h with h[0]=0:", np.round(h, 4))
print("greedy policy:", policy)
iterations: 32
estimated optimal gain: 2.5951
bias h with h[0]=0: [0.     2.7317 4.2927]
greedy policy: [1 1 1]

24.9 21.8 Average-reward policy iteration

Average-reward policy iteration alternates between two steps.

Policy evaluation. For a fixed policy \(\pi\), solve

\[ \rho^{\pi}+h^{\pi}(s) = r_{\pi}(s)+\sum_{s'}P_{\pi}(s,s')h^{\pi}(s') \]

with a normalization such as \(h^{\pi}(s_0)=0\).

Policy improvement. Define

\[ q^{\pi}(s,a) = r(s,a)+\sum_{s'}P(s'\mid s,a)h^{\pi}(s'). \]

Then choose

\[ \pi_{\text{new}}(s) \in \operatorname*{argmax}_{a} q^{\pi}(s,a). \]

If the greedy action improves the right side, it improves the average-reward objective under the appropriate recurrence assumptions.

24.9.1 Python example: average-reward policy iteration

import numpy as np

P = np.array([
    [[0.70, 0.25, 0.05], [0.40, 0.50, 0.10]],
    [[0.20, 0.60, 0.20], [0.10, 0.60, 0.30]],
    [[0.10, 0.30, 0.60], [0.05, 0.25, 0.70]]
])
r = np.array([
    [1.0, 0.8],
    [2.0, 2.4],
    [3.0, 3.2]
])

n_states, n_actions = r.shape
ref = 0
policy = np.zeros(n_states, dtype=int)

def evaluate_policy(policy):
    P_pi = np.array([P[s, policy[s]] for s in range(n_states)])
    r_pi = np.array([r[s, policy[s]] for s in range(n_states)])

    # Unknowns are rho and h. Use equations rho + h - P h = r,
    # plus h[ref] = 0.
    A = np.zeros((n_states + 1, n_states + 1))
    b = np.zeros(n_states + 1)
    for s in range(n_states):
        A[s, 0] = 1.0
        A[s, 1 + s] = 1.0
        A[s, 1:] -= P_pi[s]
        b[s] = r_pi[s]
    A[n_states, 1 + ref] = 1.0
    sol, *_ = np.linalg.lstsq(A, b, rcond=None)
    return sol[0], sol[1:]

for it in range(20):
    rho, h = evaluate_policy(policy)
    q = np.zeros((n_states, n_actions))
    for s in range(n_states):
        for a in range(n_actions):
            q[s, a] = r[s, a] + P[s, a] @ h
    new_policy = q.argmax(axis=1)
    print(f"iter {it}: rho={rho:.4f}, policy={policy}, h={np.round(h, 3)}")
    if np.array_equal(new_policy, policy):
        break
    policy = new_policy

print("final policy:", policy)
iter 0: rho=1.8947, policy=[0 0 0], h=[-0.     2.632  4.737]
iter 1: rho=2.5951, policy=[1 1 1], h=[-0.     2.732  4.293]
final policy: [1 1 1]

24.10 21.9 Unichain and multichain issues

Average-reward MDPs are more delicate than discounted MDPs because the long-run average may depend on which recurrent class is reached.

A policy-induced Markov chain is unichain if it has one recurrent class, possibly with transient states. An MDP is often called unichain if every stationary deterministic policy induces a unichain Markov chain.

In a unichain setting, the average reward of a fixed policy is independent of the initial state after transients disappear. In a multichain setting, a policy may have several recurrent classes with different average rewards. Then

\[ \rho^{\pi}(s) \]

may depend on \(s\).

The figure contrasts an ergodic chain, where all initial distributions lead to the same average reward, with a multichain example, where different initial states may enter different recurrent classes.

For a first graduate course, it is reasonable to focus on finite unichain average-reward MDPs. This avoids many technical cases while preserving the main mathematical ideas: gain, bias, Poisson equations, and relative dynamic programming.

24.11 21.10 Learning from samples: differential TD and R-learning

When the transition probabilities are unknown, the average reward and bias must be learned from data. For policy evaluation, a differential TD update has the form

\[ \delta_t = R_{t+1}-\rho_t+h_t(S_{t+1})-h_t(S_t), \]

\[ h_{t+1}(S_t) = h_t(S_t)+\alpha_t\delta_t, \]

and the average reward estimate can be updated by

\[ \rho_{t+1} = \rho_t+\beta_t(R_{t+1}-\rho_t). \]

For control, a classical average-reward Q-learning method is R-learning. It uses the TD error

\[ \delta_t = R_{t+1}-\rho_t+ \max_{a'}Q_t(S_{t+1},a')-Q_t(S_t,A_t), \]

and updates

\[ Q_{t+1}(S_t,A_t) = Q_t(S_t,A_t)+\alpha_t\delta_t. \]

The estimate of \(\rho\) is typically updated when the selected action is greedy:

\[ \rho_{t+1} = \rho_t+ \beta_t \left[ R_{t+1}+ \max_{a'}Q_t(S_{t+1},a')- \max_a Q_t(S_t,a)-\rho_t \right]. \]

These algorithms are stochastic approximation methods for the average-reward optimality equations. Their convergence theory requires careful assumptions about exploration, step sizes, recurrence, and boundedness.

24.11.1 Python example: a tiny R-learning experiment

import numpy as np

rng = np.random.default_rng(5110)

P = np.array([
    [[0.70, 0.25, 0.05], [0.40, 0.50, 0.10]],
    [[0.20, 0.60, 0.20], [0.10, 0.60, 0.30]],
    [[0.10, 0.30, 0.60], [0.05, 0.25, 0.70]]
])
r = np.array([
    [1.0, 0.8],
    [2.0, 2.4],
    [3.0, 3.2]
])

n_states, n_actions = r.shape
Q = np.zeros((n_states, n_actions))
rho = 0.0
s = 0
eps = 0.15
alpha = 0.08
beta = 0.01
rho_trace = []

for t in range(8000):
    if rng.random() < eps:
        a = rng.integers(n_actions)
    else:
        a = int(np.argmax(Q[s]))

    next_s = rng.choice(n_states, p=P[s, a])
    reward = r[s, a]

    max_next = np.max(Q[next_s])
    old_max = np.max(Q[s])
    delta = reward - rho + max_next - Q[s, a]
    greedy_before = a == int(np.argmax(Q[s]))
    Q[s, a] += alpha * delta

    if greedy_before:
        rho += beta * (reward + max_next - old_max - rho)

    s = next_s
    if t % 50 == 0:
        rho_trace.append(rho)

print("learned rho estimate:", round(rho, 4))
print("learned greedy policy:", np.argmax(Q, axis=1))
print("learned Q:")
print(np.round(Q, 3))
learned rho estimate: 2.6561
learned greedy policy: [1 1 1]
learned Q:
[[5.283 6.306]
 [7.424 8.859]
 [9.543 9.989]]

24.12 21.11 Statistical viewpoint

For MS Statistics students, the average-reward objective highlights several statistical issues.

First, the samples along a trajectory are dependent. The empirical average

\[ \widehat{\rho}_T = \frac{1}{T}\sum_{t=0}^{T-1}R_{t+1} \]

is not an average of iid random variables. Its uncertainty depends on the mixing rate of the Markov chain.

Second, a central limit theorem may hold under ergodicity:

\[ \sqrt{T}(\widehat{\rho}_T-\rho^{\pi}) \Rightarrow N(0,\sigma_{\mathrm{asymp}}^2), \]

but the asymptotic variance includes autocorrelation terms. Positive autocorrelation reduces the effective sample size.

Third, regenerative cycles provide a clean estimation idea. If the chain repeatedly returns to a reference state \(s_0\), define cycle lengths and rewards by

\[ \tau_k=T_k-T_{k-1}, \qquad Y_k=\sum_{t=T_{k-1}}^{T_k-1}R_{t+1}. \]

Then the average reward can be estimated by a ratio estimator:

\[ \widehat{\rho}_{\mathrm{regen}} = \frac{\sum_{k=1}^m Y_k}{\sum_{k=1}^m \tau_k}. \]

The regenerative viewpoint is important because it connects average reward to renewal reward theory, a topic familiar from stochastic processes.

24.12.1 Python example: empirical average reward and autocorrelation

import numpy as np

rng = np.random.default_rng(6241)
P = np.array([
    [0.70, 0.25, 0.05],
    [0.20, 0.60, 0.20],
    [0.10, 0.30, 0.60]
])
r = np.array([1.0, 2.0, 4.0])

T = 20000
s = 0
rewards = np.zeros(T)
for t in range(T):
    rewards[t] = r[s]
    s = rng.choice(3, p=P[s])

running_average = np.cumsum(rewards) / np.arange(1, T + 1)
print("final running average:", round(running_average[-1], 4))
print("first 5 running averages:", np.round(running_average[:5], 4))
print("last 5 running averages:", np.round(running_average[-5:], 4))
final running average: 2.1579
first 5 running averages: [1.     1.     1.3333 1.5    1.6   ]
last 5 running averages: [2.1575 2.1576 2.1577 2.1578 2.1579]

24.13 21.12 Average reward versus discounting in modeling

When should one use average reward rather than discounting?

Use average reward when:

  1. the task is continuing and has no natural terminal time;
  2. performance is naturally measured per unit time;
  3. delaying reward should not automatically make it less important;
  4. the system is expected to operate in steady state;
  5. recurrence and stability assumptions are reasonable.

Use discounting when:

  1. near-term reward should be preferred by design;
  2. the modeler wants a contraction mapping with simple error bounds;
  3. long-range predictions are highly uncertain;
  4. the task is episodic or effectively finite-horizon;
  5. algorithmic stability is more important than exact steady-state interpretation.

Average reward is not simply discounted reward with \(\gamma=1\). It requires a different normalization, a different value object, and stronger recurrence assumptions.

24.14 21.13 AI-assisted learning components

24.14.1 AI prompt: check recurrence assumptions

Ask an AI assistant:

I have a finite MDP and want to use an average-reward objective. What recurrence assumptions should I check before using the equation \(\rho+h=Th\)? Explain unichain and multichain behavior with a small example.

Then verify that the response distinguishes discounted well-posedness from average-reward well-posedness.

24.14.2 AI prompt: audit a Poisson-equation solution

Ask:

Here are \(P\), \(r\), \(\rho\), and \(h\). Check whether \(\rho\mathbf 1+h=r+Ph\) and whether the normalization is stated clearly. Do not assume \(h\) is unique.

This is a useful way to catch the common mistake of treating \(h\) as an absolute value function.

24.14.3 AI prompt: compare objectives

Ask:

Give one application where discounted reward is more appropriate and one application where average reward is more appropriate. For each, state the mathematical reason.

A good answer should mention time preference, stationarity, continuing operation, and recurrence.

24.15 21.14 Summary

Average-reward MDPs replace discounted cumulative value by long-run reward per time step. For a fixed policy, the gain is often computed as a stationary expectation:

\[ \rho^{\pi}=d_{\pi}^T r_{\pi}. \]

The transient part is described by a bias function satisfying the Poisson equation

\[ \rho^{\pi}\mathbf 1+h^{\pi}=r_{\pi}+P_{\pi}h^{\pi}. \]

For control, the average-reward optimality equation is

\[ \rho^*+h^*(s) = \max_a \left[ r(s,a)+\sum_{s'}P(s'\mid s,a)h^*(s') \right]. \]

The bias is defined only up to an additive constant, so relative value iteration and policy evaluation require normalization. Compared with discounted RL, average-reward RL is mathematically more delicate because recurrence and ergodicity assumptions matter.

24.16 Exercises

24.16.1 Conceptual exercises

  1. Explain why discounting can distort a continuing control problem.
  2. What is the difference between gain and bias?
  3. Why is the bias function not unique?
  4. What does the span seminorm measure, and why is it appropriate for average-reward algorithms?
  5. Explain why multichain behavior is a problem for average-reward theory.

24.16.2 Mathematical exercises

  1. Let \(P_{\pi}\) be an irreducible finite Markov chain with stationary distribution \(d_{\pi}\). Prove that \(\rho^{\pi}=d_{\pi}^Tr_{\pi}\).
  2. Starting from the definition of the centered return, derive the Poisson equation for \(h^{\pi}\).
  3. Show that if \(h\) solves \(\rho\mathbf 1+h=r+Ph\), then \(h+c\mathbf 1\) also solves it.
  4. Prove that \(d^T(r-\rho\mathbf 1)=0\) when \(\rho=d^Tr\).
  5. Verify that \(Z=(I-P+\mathbf 1d^T)^{-1}\) gives a mean-zero solution of the Poisson equation under ergodicity.
  6. Derive the average-reward Bellman optimality equation from one-step decomposition.
  7. Show that relative value iteration is invariant under adding a constant to \(h_k\).
  8. For a two-state Markov chain, compute \(\rho\) and \(h\) explicitly.

24.16.3 Computational exercises

  1. Modify the policy-evaluation Python code to use normalization \(d^T h=0\) instead of \(h(0)=0\).
  2. Implement relative value iteration for a four-state MDP.
  3. Compare the greedy policies obtained from discounted value iteration for \(\gamma=0.9,0.99,0.999\) with the average-reward greedy policy.
  4. Simulate a Markov chain and estimate average reward by a running average. Plot convergence.
  5. Implement R-learning for the three-state MDP and study sensitivity to \(\alpha\), \(\beta\), and \(\epsilon\).
  6. Construct a multichain example where \(\rho^{\pi}(s)\) depends on \(s\).
  7. Estimate the effective sample size of a reward trajectory using empirical autocorrelation.

24.16.4 AI-assisted exercises

  1. Ask an AI tool to explain the difference between discounted value and bias. Identify any places where it incorrectly treats the bias as an absolute value.
  2. Give an AI tool a transition matrix and reward vector. Ask it to compute \(\rho\) and \(h\). Verify the result in Python.
  3. Ask an AI tool to produce a multichain counterexample. Check whether the example truly has multiple recurrent classes.
  4. Ask an AI tool to compare relative value iteration and discounted value iteration. Add corrections using the span seminorm and normalization.
  5. Ask an AI tool to audit your R-learning code for off-by-one errors in the TD target.

24.17 Notes for instructors

This chapter is a natural bridge between stochastic processes and reinforcement learning. For MA Applied Math students, emphasize Poisson equations, invariant measures, and relative dynamic programming. For MS Statistics students, emphasize dependent data, stationary averages, autocorrelation, and estimation uncertainty. A good lecture sequence is:

  1. start with stationary distributions and long-run reward;
  2. derive the Poisson equation;
  3. explain nonuniqueness of the bias;
  4. implement relative value iteration;
  5. discuss statistical estimation from one long trajectory;
  6. contrast discounted and average-reward modeling choices.