DPO Derivation › Deriving the Optimal Policy
Language AI HandbookAll chapters DPO Derivation › Deriving the Optimal PolicyOne section · Sections in this chapter3 / 12 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
The key insight behind DPO is that this optimization problem has a closed-form solution. Unlike many optimization problems in machine learning that require iterative gradient descent, this particular formulation admits an analytical answer. For a fixed prompt , we're optimizing over the distribution for all possible responses . This is a constrained optimization problem over probability distributions: we must ensure our solution is a valid probability distribution that sums to one and assigns non-negative probability to every possible response.
The special structure of the RLHF objective allows for this closed-form solution. The expectation under combined with the KL divergence creates what's known as a variational problem over distributions. Such problems often have elegant solutions when the constraints are simple probability simplex constraints.
Let's work through the derivation step by step. For a single prompt , the objective is:
where:
- : sum over all possible responses in the vocabulary
- : the entire probability distribution over responses for prompt
- : probability of response given prompt
- : the reward function
- : the KL penalty coefficient
- : the natural logarithm
- : the reference policy probability
We need to maximize this subject to the constraint that is a valid probability distribution: and for all .
To solve this constrained optimization problem, we use the method of Lagrange multipliers, a classical technique from calculus. The idea is to incorporate the constraint directly into the objective by introducing a new variable (the Lagrange multiplier) that penalizes violations of the constraint. At the optimal solution, the gradient of the objective with respect to the decision variables must be proportional to the gradient of the constraint.
The Lagrangian is:
where:
- : the Lagrangian function
- : sum over all responses
- : probability of response
- : the reward function
- : the KL penalty coefficient
- : the natural logarithm
- : the reference policy probability
- : the Lagrange multiplier for the constraint that probabilities sum to 1
Notice that we've expanded the log ratio term from the original objective, separating it into and . This separation makes taking derivatives more straightforward. The term enforces our normalization constraint: if the probabilities don't sum to one, this term will be nonzero, and the Lagrange multiplier will adjust to push us toward a valid distribution.
Taking the derivative with respect to for a specific :
where:
- : the partial derivative of the Lagrangian with respect to the probability of response
- : the reward function
- : the KL penalty coefficient
- : the natural logarithm
- : the policy probability
- : the reference policy probability
- : the Lagrange multiplier
The term comes from differentiating , which gives . This is a standard result from calculus: when we differentiate with respect to , we apply the product rule, obtaining . Setting this derivative equal to zero gives us the first-order optimality conditions.
Solving for :
where:
- : the log-probability of response
- : the reward function
- : the KL penalty coefficient
- : the reference policy probability
- : constant terms related to the normalization constraint
This expression for the log-probability has an illuminating structure. The log-probability under our optimal policy equals the log-probability under the reference, adjusted by the scaled reward , plus some constants that don't depend on . Higher reward responses get higher log-probabilities, with controlling how much the reward matters relative to staying close to the reference.
Exponentiating both sides:
where:
- : the policy probability
- : the reference policy probability
- : the exponential function
- : the reward-scaling term
- : the reward function
- : the KL penalty coefficient
- : the normalization term constant across all
The term is just a normalizing constant that ensures the distribution sums to 1. This term doesn't depend on ; it only depends on through the constraint that all probabilities must sum to one. We can absorb it into a partition function :
where:
- : the optimal policy distribution
- : the partition function (normalizing constant) for prompt
- : the reference policy distribution
- : the reward function
- : the temperature parameter
This is the optimal policy for the RLHF objective. It has a clear interpretation: the optimal policy takes the reference distribution and reweights each response by the exponentiated reward, then normalizes. The exponential function ensures all probabilities are positive, naturally satisfying the constraint. Responses with higher reward get exponentially more probability mass, with controlling how aggressively we reweight.
Consider what happens at extreme values of beta to see how this reweighting works. When is very large, the reward term becomes small, and for all responses. In this limit, the optimal policy stays very close to the reference distribution, barely adjusting for reward at all. Conversely, when is very small, the exponential amplifies small reward differences into massive probability shifts, putting all probability mass on the highest-reward response. The choice of therefore controls the exploration-exploitation trade-off: larger values favor staying close to the reference (exploration), while smaller values favor chasing high reward (exploitation).




The optimal policy has the form of a Boltzmann (or Gibbs) distribution from statistical mechanics, where plays the role of negative energy and plays the role of temperature. Lower temperature (smaller ) concentrates probability on the highest-reward responses.
The Partition Function Problem
Before moving on, we should acknowledge a serious computational obstacle lurking in the optimal policy formula. The partition function is defined as:
where the sum ranges over all possible responses . For a language model, "all possible responses" means all sequences of tokens up to some maximum length. The number of such sequences is astronomical: a modest vocabulary of 32,000 tokens with a maximum length of 200 tokens gives possible responses, a number larger than the estimated number of atoms in the observable universe. Computing directly is completely intractable.
This intractability is precisely why naive approaches to optimizing the RLHF objective are so expensive. Algorithms like PPO work around it by using sample-based estimates and importance weighting, but those approaches have their own difficulties, including high variance and sensitivity to hyperparameters. The DPO derivation sidesteps this problem entirely, and the key step is coming up next in the reparameterization.