Logo Xingxin on Bug

What is the Cross Entropy Method in Policy Search?

November 19, 2026
5 min read

These are my learning notes for Section 10.4 (Cross Entropy Method) of Algorithms for Decision Making.

📝Definition

The cross entropy method is a policy search algorithm that optimizes a policy’s parameter vector θ\theta by maintaining a search probability distribution p(θ∣ψ)p(\theta \mid \psi) over the parameter space.

Remark

The ψ\psi represents the parameters of the search distribution. A common choice is the Gaussian distribution, where ψ=(μ,σ2)\psi = (\mu, \sigma^2), or (μ,Σ)(\mu, \Sigma) in higher dimensions, consists of the mean and variance. Other common choices include the categorical distribution and the Gaussian mixture model.

The goal is to find the optimal distribution parameters ψ∗\psi^* that maximize the expected policy utility U(θ)U(\theta) when sampling policy parameter θ\theta from p(⋅∣ψ)p(\cdot \mid \psi):

ψ∗=arg⁡max⁡ψEθ∼p(⋅∣ψ)[U(θ)]=arg⁡max⁡ψ∫U(θ)p(θ∣ψ) dθ.\psi^* = \arg\max_{\psi} \mathop{\mathbb{E}}\limits_{\theta \sim p(\cdot \mid \psi)} [U(\theta)] = \arg\max_{\psi} \int U(\theta) p(\theta \mid \psi) \, \mathrm{d}\theta.
Remark

Notice that this equation uses U(θ)U(\theta) rather than Uπ(s)U^\pi(s). Here, U(θ)U(\theta) is the overall expected utility of running the parameterized policy πθ\pi_\theta, whereas Uπ(s)U^\pi(s) is the value of a single state ss under policy π\pi.

🗃Example

Don’t let the integral intimidate you. The core idea is just shifting a search distribution toward better parameters.

Let’s walk through a simple example. Suppose a robot arm needs to choose a 1D action (single motor torque θ\theta) to reach a target:

  1. We start with an initial search distribution N(μ=0,σ2=10)\mathcal{N}(\mu=0, \sigma^2=10).
  2. We draw m=4m=4 candidate torques, evaluate each one’s utility U(θ)U(\theta), and plan to keep the top melite=2m_{\text{elite}}=2 candidates:
Candidate iiSampled Torque θ(i)\theta^{(i)}Reward U(θ(i))U(\theta^{(i)})
1−2.0-2.011
21.01.055
34.04.099
46.06.088
Remark

Normally, a rollout spans many time steps where the controller continuously output torque. For simplicity, we assume a one-step rollout here, so the policy is just a single torque value θ\theta and its utility U(θ)U(\theta) is simply the immediate reward.

Try answering these two questions before checking the solutions below:

  • 💬Question 1: Which two torque values θ(i)\theta^{(i)} make up our elite set?
  • 💬Question 2: Using those two elite torques, what are the updated parameters μnew\mu_{\text{new}} and σnew2\sigma^2_{\text{new}}?

🗣Answer 1

The two highest utilities are 99 and 88, so our elite set is:

  • θ(3)=4.0\theta^{(3)}=4.0
  • θ(4)=6.0\theta^{(4)}=6.0

🗣Answer 2

We update the search distribution using MLE on the melitem_{\text{elite}} elite samples:

  1. update the mean: μnew=1melite∑i=1meliteθelite(i)\displaystyle \mu_{\text{new}} = \frac{1}{m_{\text{elite}}} \sum_{i=1}^{m_{\text{elite}}} \theta_{\text{elite}}^{(i)}
  2. update the variance: σnew2=1melite∑i=1melite(θelite(i)−μnew)2\displaystyle \sigma^2_{\text{new}} = \frac{1}{m_{\text{elite}}} \sum_{i=1}^{m_{\text{elite}}} \left(\theta_{\text{elite}}^{(i)} - \mu_{\text{new}}\right)^2

Plugging in our two elite samples (4.04.0 and 6.06.0):

μnew=4.0+6.02=5.0σnew2=12((4.0−5.0)2+(6.0−5.0)2)=1+12=1.0\begin{align} \mu_{\text{new}} &= \frac{4.0 + 6.0}{2} = 5.0 \\ \sigma^2_{\text{new}} &= \frac{1}{2}\Big((4.0 - 5.0)^2 + (6.0 - 5.0)^2\Big) = \frac{1 + 1}{2} = 1.0 \end{align}

In just one iteration, the search distribution shifts from a wide guess N(0,10)\mathcal{N}(0, 10) to a much tighter distribution N(5.0,1.0)\mathcal{N}(5.0, 1.0) centered right around the high-reward torques.

N(0,10)\mathcal{N}(0, 10)N(5.0,1.0)\mathcal{N}(5.0, 1.0)
mu_0_sigma_10.webpmu_5_sigma_1.webp

🐍Algorithm

Here is the original Julia implementation from Algorithms for Decision Making:

struct CrossEntropyPolicySearch
    p       # initial distribution
    m       # number of samples
    m_elite # number of elite samples
    k_max   # number of iterations
end
 
function optimize_dist(M::CrossEntropyPolicySearch, π, U)
    p, m, m_elite, k_max = M.p, M.m, M.m_elite, M.k_max
    for k in 1:k_max
        θs = rand(p, m)
        us = [U(π, θs[:,i]) for i in 1:m]
        θ_elite = θs[:,sortperm(us)[(m-m_elite+1):m]]
        p = Distributions.fit(typeof(p), θ_elite)
    end
    return p
end
 
function optimize(M, π, U)
    return Distributions.mode(optimize_dist(M, π, U))
end

After reading the book’s version, I wrote a simplified Gaussian-only version in Julia to make each step easier to follow:

using Distributions, Statistics
 
struct CrossEntropyMethod
    μ       # initial mean vector
    Σ       # initial covariance matrix
    m       # number of samples
    m_elite # number of elite samples
    k_max   # number of iterations
end
 
function optimize(M::CrossEntropyMethod, U)
    μ, Σ, m, m_elite, k_max = M.μ, M.Σ, M.m, M.m_elite, M.k_max
    for k in 1:k_max
        D = MvNormal(μ, Σ)
        θs = [rand(D) for _ in 1:m]
        us = [U(θ) for θ in θs]
        order = sortperm(us, rev=true)
        θ_elite = θs[order[1:m_elite]]
 
        # Refit the Gaussian via MLE (corrected=false divides by m_elite)
        elite_matrix = reduce(hcat, θ_elite)
        μ = mean(θ_elite)
        Σ = cov(elite_matrix, dims=2, corrected=false)
    end
    return μ
end
Remark

Returning μ here matches Distributions.mode(...) in the book’s code because the mode of a Gaussian distribution is its mean. Also, in practice, if m_elite is very small (or the samples become nearly identical), Σ can lose positive definiteness and cause MvNormal(μ, Σ) to fail—adding a tiny value like 1e-6 * I to Σ prevents this.

How the loop works, step by step:

  1. Build a multivariate Gaussian distribution D = MvNormal(μ, Σ) from the current mean and covariance.
  2. Draw m candidate parameter vectors from D.
  3. Evaluate each candidate’s utility U(θ).
  4. Sort the candidates by utility (highest first) and keep the top m_elite samples.
  5. Update the mean μ and covariance Σ using MLE on those elite samples.
  6. Repeat for k_max iterations, then return the final mean μ.

See also...