Note
This is a weekend project I did to get my hands dirty for the first time with Reinforcement Learning.
Lately, I’ve been reading some papers about off-policy RL methods. They don’t work that well in practice.
In standard Q-learning, people usually predict only the expected Q-value.
So I thought: is it possible to predict the entire probability distribution of Q?
The answer is yes. In this repository I’ll show you how to perform probabilistic Q-learning from first principles.
Warning
This is the nerd area, proceed with caution.
Off-policy reinforcement learning revolves around the Bellman equation:
Where:
-
$Q^*(s,a)$ is the optimal$Q$ -value for taking action$a$ in state$s$ . It represents the maximum total discounted reward the agent can expect from this point forward. -
$r(s,a)$ is the immediate reward received after taking action$a$ from state$s$ . -
$\gamma \in (0,1]$ is the discount factor. -
$\max_{a'}Q^*(s',a')$ is the maximum$Q$ -value for the next state$s'$ across all possible actions$a'$ . This links the current$Q$ -value to the optimal future value, assuming the agent acts optimally.
Training is typically bootstrapped. A common approach is to minimize the
where
The main issue is that the model has no notion of confidence in its predictions. The loss above implicitly assumes
Some states are much harder to evaluate than others. We therefore need a formulation that accounts for uncertainty.
Moreover, the
Suppose we have a ground-truth distribution
This can also be expressed as:
where
Note
By
The family of distributions to which
-
Closed under maximization (for the
$\max_{a'}$ term). -
Closed under linear transformations (for the reward shift and scaling by
$\gamma$ ).
These properties hold for the Generalized Extreme Value (GEV) distribution.
In this work, we use the Gumbel distribution:
Here:
-
$\mu = \mu(s,a)$ depends on both state and action. -
$\beta = \beta(s)$ depends only on the state.
Tip
This is crucial because we now have a principled way of estimating the confidence of the prediction!
Knowing that
We want to learn a distribution
$\mu_q = \mu_\theta(s,a)$ $\beta_q = \beta_\phi(s)$
with learnable parameters
As for the target, since we don’t know
which is also Gumbel:
with:
With this, we can express analytic formulas for:
Schematic representation of how it works. You compare the extreme value distributions of the next moves
$p$ with the distribution$q$
For
where
Let
The KL divergence has a closed form [source]:
where
Warning
The formulas might look scary, but they are actually simple and numerically stable.
To improve numerical stability, we reparameterize with
Then:
and:
So the total loss becomes:
If you are at the final step (no future states), the loss simplifies to:
- The KL divergence measures how much information the model gains when predicting future outcomes.
- The Entropy measures how uncertain the target distribution is.
- The total loss captures both the uncertainty and the error of the model when deciding on an action.
A key advantage of this approach lies in the KL divergence term.
If the future states are highly uncertain, then the target distribution
This has a very important effect: the model focuses its learning effort on states where there is actual signal to be extracted.
In contrast, the standard
Summary:
-
$\textrm{KL}$ encourages the model to learn where it matters. -
$L_2$ loss blindly penalizes everything, even noise.
Finally, for stability, the gradient should also be computed with respect to
I tested the model on the CartPole environment.
The model learns to balance the pole correctly. Below is the training curve for this algorithm compared to the
The training is reasonably stable, though it could be improved. For now, I’m satisfied—
I just wanted to make it exist first, I’ll make it good later
To reproduce the experiments simply install the python libraries
pip install uv
uv venv
uv pip install -e .
source .venv/bin/activateand then choose which experiment to run
python q-prob.py # probabilistic Q-learning with KL loss
python base-q-learning.py # Q-learning with L2 loss

