Skip to content
intermediate

Prioritized Experience Replay: Deriving Sampling Probabilities and Importance Weights

You add prioritized replay. Training speeds up. Loss drops faster than it ever did with uniform sampling. Then, a few hundred thousand steps later, the…

Published 2026-10-02Updated 2026-10-0411 min read
Experience scuba diving among vibrant corals in the Maldives underwater paradise.
Experience scuba diving among vibrant corals in the Maldives underwater paradise. Photo by Md Rafatul Islam on Pexels.

You add prioritized replay. Training speeds up. Loss drops faster than it ever did with uniform sampling. Then, a few hundred thousand steps later, the value estimates settle somewhere slightly wrong — not catastrophically wrong, just wrong enough that the policy is worse than the uniform-replay baseline you were trying to beat.

Nothing crashed. Nothing diverged. The sampler quietly changed the distribution you were averaging over, and nobody paid the invoice.

That invoice has two line items: the sampling probability and the importance weight. This article derives both. By the end, you should be able to compute them by hand for a small buffer and explain why the correction exists at all.

What Prioritized Replay Actually Changes

Uniform replay samples each stored transition with probability 1/N1/N, where NN is the current number of transitions in the buffer. The Q-learning update is then an average over that distribution. That average is the thing you are implicitly trusting.

Prioritized replay replaces that distribution with one proportional to a priority score. In the common variant, the priority is the absolute temporal-difference (TD) error:

pi=∣δi∣+ϵp_i = |\delta_i| + \epsilon

A large TD error means the current value estimate is wrong at that transition. The intuition — "sample the surprising stuff more" — is correct as far as it goes. But it is not the whole story, and treating it as the whole story is exactly how you end up with the drift described above.

The assumption we will hold fixed for the derivation: the buffer is fixed, the policy is fixed, and we are asking what the expected update converges to under a non-uniform sampler. That is the cleanest setting to see the bias. Real training violates all three, but the bias does not disappear when they move — it just gets harder to isolate.

Two knobs control everything. Alpha (α\alpha) controls how much prioritization you apply. Beta (β\beta) controls how much correction you apply. We will derive both.

Notation and the Two Distributions

Before any formula, name the objects.

SymbolMeaning
NNCurrent number of transitions in the buffer (not capacity)
iiIndex of a stored transition
pip_iPriority of transition ii
α\alphaPriority exponent, α≥0\alpha \geq 0
P(i)P(i)Probability of sampling transition ii
β\betaImportance-sampling exponent, 0≤β≤10 \leq \beta \leq 1
wiw_iImportance weight for transition ii
ϵ\epsilonSmall constant added to priorities so nothing is unsampleable

The central tension is this: there is a distribution you sample from, and a distribution you want to average over. Prioritized replay makes those two different. The importance weight is what reconciles them.

The sampling rule is:

P(i)=piα∑k=1NpkαP(i) = \frac{p_i^{\alpha}}{\sum_{k=1}^{N} p_k^{\alpha}}

Read it aloud: raise each priority to a power, then normalize so the probabilities sum to one. That is the whole formula.

Two boundary cases matter. At α=0\alpha = 0, every pi0=1p_i^0 = 1, so P(i)=1/NP(i) = 1/N — uniform sampling. At α=1\alpha = 1, sampling is fully proportional to priority. Everything between is a dial.

The ϵ\epsilon term is not cosmetic. Without it, a transition whose TD error is exactly zero gets pi=0p_i = 0 and can never be sampled again. It becomes a permanent resident of the buffer that contributes nothing. Small ϵ\epsilon keeps every transition reachable.

The target distribution is the uniform distribution over the buffer. That is what the unweighted update implicitly assumes, and it is what we are trying to recover.

Deriving the Sampling Probability

Start from intent, not from the formula. We want a monotone, controllable mapping from priority to sampling chance. Monotone means higher priority never gets sampled less. Controllable means one knob that slides between "ignore priorities" and "trust them completely."

A power law piαp_i^{\alpha} satisfies both. A linear map pip_i would give you proportional sampling but no dial — you would have to redesign the formula to get uniform behavior back. The power law degrades gracefully: set α=0\alpha = 0 and you are exactly uniform, no special case required.

Normalization is forced by probability. The probabilities must sum to one, so:

∑i=1NP(i)=1  ⟹  P(i)=piα∑k=1Npkα\sum_{i=1}^{N} P(i) = 1 \implies P(i) = \frac{p_i^{\alpha}}{\sum_{k=1}^{N} p_k^{\alpha}}

Now work a small buffer by hand. Five transitions with priorities $1, 4, 9, 16, 25.Use. Use \alpha = 0.6$.

iipip_ipi0.6p_i^{0.6}P(i)P(i)
111.0000.056
242.2970.129
393.7370.210
4165.2780.296
5256.8990.387
19.2111.000

The highest-priority transition is sampled roughly seven times as often as the lowest. Now move the dial.

At α=0\alpha = 0, every P(i)=0.200P(i) = 0.200. Uniform. At α=1\alpha = 1, the probabilities become $0.018, 0.073, 0.164, 0.291, 0.455$ — the top transition is sampled twenty-five times as often as the bottom. The dial is not subtle.

One implementation note: naive sampling from P(i)P(i) costs O(N)O(N) per draw because you must scan the cumulative distribution. Production implementations use a sum-tree to get O(log⁡N)O(\log N) sampling and updates, often combined with stratified sampling to reduce variance. That is an engineering detail, not a mathematical one, but it is why you rarely see the raw formula in code.

Knowledge check

Check your understanding

Answer this question before you continue.

A buffer has two transitions with priorities 1 and 4. Using α = 0.5, what are their sampling probabilities, in that order?
Output Prediction

Focus: Calculate normalized sampling probabilities from priorities and an exponent.

Why Non-Uniform Sampling Biases the Update

Here is the mechanism, stated plainly. The update is an expectation over sampled transitions. Changing the sampler changes the expectation — not just the variance.

High-priority transitions get counted many times per epoch. Their gradient contribution is inflated relative to their true share of the buffer. If you train long enough with a fixed policy and a fixed state distribution, the value estimate converges toward the solution of a reweighted objective, not the original one.

This is the part that catches people. Prioritization is often described as a variance-reduction trick, in the same family as importance sampling in Monte Carlo estimation. It is not. It is a deliberate change to the distribution you average over, and that change has a real cost.

The failure mode is specific: uncorrected prioritization looks faster early, because the samples with the largest learning signal dominate the gradient. Then it settles at the wrong place, because the objective it is actually optimizing is not the one you wrote down. The drift is not mysterious. It is the sampler's distribution showing up in the fixed point.

Knowledge check

Check your understanding

Answer this question before you continue.

With a fixed buffer and policy, why can uncorrected prioritized replay converge to a different value estimate than uniform replay?
Misconception Check

Focus: Explain why uncorrected prioritized sampling can change the learned fixed point.

Deriving the Importance Weight

Three linked groups of five bars: sampling probabilities rise from transition 1 to 5, beta-one importance weights fall in the opposite direction, and their products are equal-height bars.
At β = 1, inverse-probability weights cancel the sampler’s preference in expectation, recovering the uniform-buffer average.

Importance sampling in one line: to estimate an expectation under distribution qq using samples drawn from distribution pp, weight each sample by q(i)/p(i)q(i)/p(i).

Here, qq is uniform — q(i)=1/Nq(i) = 1/N — and pp is P(i)P(i). So the raw weight is:

q(i)P(i)=1/NP(i)=1N⋅P(i)\frac{q(i)}{P(i)} = \frac{1/N}{P(i)} = \frac{1}{N \cdot P(i)}

Introduce β\beta as a partial correction:

wi=(1N⋅P(i))βw_i = \left( \frac{1}{N \cdot P(i)} \right)^{\beta}

At β=1\beta = 1, the correction is full and the estimator is unbiased with respect to the uniform distribution. At β=0\beta = 0, every weight is 1 and you are back to the uncorrected case. Everything between trades a little bias for a little stability.

Then normalize within the mini-batch:

wi←wimax⁡jwjw_i \leftarrow \frac{w_i}{\max_j w_j}

Dividing by the maximum weight keeps the largest weight at 1 and prevents gradient magnitudes from exploding when a rare transition receives a very large raw weight. This is a stability measure, not a mathematical necessity — the expectation is unchanged, but the variance of the gradient is much better behaved.

The annealing schedule is the practical part. β\beta typically starts below 1 and rises toward 1 over training. Early updates benefit from aggressive prioritization because the value estimates are far from correct and the largest TD errors carry the most useful signal. Late updates need unbiased targets because the estimates are close and the remaining bias is what determines the final fixed point.

Common mistake: Treating β\beta as a fixed hyperparameter. If β\beta stays at 0.4 for the entire run, you never fully pay back the sampling debt, and the drift described at the top of this article is exactly what you get.

Knowledge check

Check your understanding

Answer this question before you continue.

For N = 5, a transition has sampling probability P(i) = 0.1, and β = 1. What is its raw importance weight before any max-normalization?
Single Choice

Focus: Calculate the full importance weight from a sampling probability and buffer size.

Worked Example: Weights for a Five-Transition Buffer

Reuse the five priorities from earlier. The P(i)P(i) values at α=0.6\alpha = 0.6 are $0.056, 0.129, 0.210, 0.296, 0.387.Computerawweights. Compute raw weights (N \cdot P(i))^{-\beta}atat\beta = 0.4andand\beta = 1.0$, then apply max-normalization.

iiP(i)P(i)wiw_i at β=0.4\beta=0.4normalizedwiw_i at β=1.0\beta=1.0normalized
10.0561.7161.0003.5711.000
20.1291.2140.7081.5500.434
30.2101.0000.5830.9520.267
40.2960.8690.5060.6760.189
50.3870.7880.4590.5170.145

Read the result. The highest-priority transition gets weight 1.0 in both cases — it is the reference point after normalization. The lowest-priority transition is down-weighted to 0.459 at β=0.4\beta = 0.4 and to 0.145 at β=1.0\beta = 1.0. The correction is doing exactly what the bias argument demanded: pulling the inflated contribution of high-priority samples back toward their true share.

At β=0\beta = 0, every weight would be 1. That is the uncorrected case, and it is the one that drifts.

Sanity check: if all five priorities were equal, every P(i)P(i) would be 1/N=0.21/N = 0.2, every raw weight would be (5⋅0.2)−β=1(5 \cdot 0.2)^{-\beta} = 1, and every normalized weight would be 1 regardless of β\beta. When the sampling distribution is already uniform, there is nothing to correct.

Knowledge check

Check your understanding

Answer this question before you continue.

In the five-transition example, what is the max-normalized weight for transition 2 when β = 1.0?
Output Prediction

Focus: Read a normalized importance weight from the five-transition worked example.

Where the Derivation Breaks Down

The formulas rest on assumptions that real training violates. Knowing which ones matters more than knowing the formulas.

Stale priorities. A transition's TD error is recorded when it is sampled. A transition that becomes relevant later — because the policy changed, or because the agent reached a new region of state space — can stay buried under an old low priority. Periodic priority refresh and a small ϵ\epsilon mitigate this, but they do not eliminate it.

Sampling with replacement. Most reference implementations sample with replacement, which means the same high-priority transition can land in a mini-batch several times. Its influence is then inflated beyond what the importance weight corrects, because the weight was derived for a single draw. Sampling without replacement is an option, but it changes the distribution in ways the standard derivation does not cover.

The weight corrects the sampler, not the target. The importance weight fixes the distribution mismatch introduced by prioritization. It does nothing about the fact that the TD target is a bootstrapped estimate. That instability is a separate problem with separate tools.

Recurrent agents. If your value function carries hidden state, random access to a transition requires reconstructing the correct hidden state by replaying the episode up to that point. Some frameworks simply do not support prioritized replay for recurrent agents, and the ones that do pay a real cost per sample.

Hyperparameter sensitivity. Performance depends noticeably on α\alpha, β\beta, and ϵ\epsilon. These are tuning parameters with consequences, not defaults to ignore.

Choosing Alpha and Beta in Practice

The derivation gives you the dials. Here is how I would turn them.

Start with α≈0.6\alpha \approx 0.6 and β\beta annealed from roughly 0.40.4 to 1.01.0. These are common starting points, not laws.

If training is unstable or value estimates drift, raise β\beta toward 1 before touching α\alpha. The drift is almost always unpaid sampling debt, and β\beta is the payment.

If learning is slow and the buffer is large with many near-duplicate transitions, α\alpha is the dial to raise. More prioritization means more focus on the transitions that still have something to teach.

When not to use it: small buffers, short runs, or settings where uniform replay already converges. The extra machinery buys nothing there and adds two hyperparameters plus a sum-tree to maintain.

Debugging rule: Log the distribution of importance weights. If most weights are near zero and a few are near one, the correction is doing heavy lifting — which usually means the priorities are too aggressive for the current β\beta. If every weight is near one, the priorities are nearly uniform and prioritization is not doing much at all.

The opening symptom — faster early, wrong later — was never mysterious. Prioritization changes the distribution you average over. The importance weight is the receipt. If you never pay it, the fixed point moves.

The next concrete move: instrument your own replay buffer. Log the priorities and the importance weights for a few thousand sampled batches. Plot both distributions. If the weight distribution does not look like the inverse of the priority distribution you intended, the correction is not doing what you think it is — and you now have the derivation to explain why.

Knowledge check

Final check

Finish the article by checking the ideas you just learned.

A run learns quickly at first, but later value estimates drift under prioritized replay. Following the article's practical guidance, which adjustment should be tried first?
Question 1 of 2Scenario Interpretation

Focus: Choose how to adjust prioritization correction when value estimates drift under prioritized replay.

What does the importance weight correct, according to the article's stated limitation?
Question 2 of 2Comparison Reasoning

Focus: Distinguish sampling-distribution correction from unrelated sources of target instability.

References

  1. PrioritizedReplayBuffer — torchrl 0.6 documentationdocs.pytorch.org
  2. Off-Policy Actor-Critic with Shared Experience Replayproceedings.mlr.press
  3. rlPrioritizedReplayMemory - Replay memory experience buffer with prioritized sampling - MATLABwww.mathworks.com
  4. Understanding Prioritized Experience Replaydanieltakeshi.github.io
Practical resource

Build a stronger RL mental model

Use structured resources to connect equations, algorithms, and observable agent behavior.

Browse resources
Related sites

Continue across related AI foundations

Use LearnPyFast for Python foundations and LearnLLMFast for practical language-model and agent application concepts.

Python tutorialstutorial

LearnPyFast

Beginner-friendly Python tutorials, examples, and learning paths for practical programming foundations.

PythonProgrammingBeginners
Visit LearnPyFast
LLM tutorialstutorial

LearnLLMFast

Practical LLM tutorials for builders who want to understand prompting, workflows, agents, and AI applications.

LLMAIBuilders
Visit LearnLLMFast

Keep learning

Related reinforcement learning tutorials

Continue with nearby RL concepts, algorithms, and experiments that build on the same decision process.