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 by maintaining a search probability distribution over the parameter space.
RemarkThe represents the parameters of the search distribution. A common choice is the Gaussian distribution, where , or 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 that maximize the expected policy utility when sampling policy parameter from :
RemarkNotice that this equation uses rather than . Here, is the overall expected utility of running the parameterized policy , whereas is the value of a single state under policy .
🗃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 ) to reach a target:
- We start with an initial search distribution .
- We draw candidate torques, evaluate each one’s utility , and plan to keep the top candidates:
| Candidate | Sampled Torque | Reward |
|---|---|---|
| 1 | ||
| 2 | ||
| 3 | ||
| 4 |
RemarkNormally, 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 and its utility is simply the immediate reward.
Try answering these two questions before checking the solutions below:
- 💬Question 1: Which two torque values make up our elite set?
- 💬Question 2: Using those two elite torques, what are the updated parameters and ?
🗣Answer 1
The two highest utilities are and , so our elite set is:
🗣Answer 2
We update the search distribution using MLE on the elite samples:
- update the mean:
- update the variance:
Plugging in our two elite samples ( and ):
In just one iteration, the search distribution shifts from a wide guess to a much tighter distribution centered right around the high-reward torques.
![]() | ![]() |
🐍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))
endAfter 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 μ
endRemarkReturning
μhere matchesDistributions.mode(...)in the book’s code because the mode of a Gaussian distribution is its mean. Also, in practice, ifm_eliteis very small (or the samples become nearly identical),Σcan lose positive definiteness and causeMvNormal(μ, Σ)to fail—adding a tiny value like1e-6 * ItoΣprevents this.
How the loop works, step by step:
- Build a multivariate Gaussian distribution
D = MvNormal(μ, Σ)from the current mean and covariance. - Draw
mcandidate parameter vectors fromD. - Evaluate each candidate’s utility
U(θ). - Sort the candidates by utility (highest first) and keep the top
m_elitesamples. - Update the mean
μand covarianceΣusing MLE on those elite samples. - Repeat for
k_maxiterations, then return the final meanμ.

