Search

Search papers and lessons

Score Function

Level 3 · 12 min read

The score function is the gradient of a density's log, which points uphill toward likely values and needs no normalising constant.

On this page

  • Colour = topic mix
  • Calculus
  • Probability
  • Discrete math
  • Linear algebra
  • Inference
  • Sign in to see your progress

TL;DR

The score function of a probability density is the gradient of its logarithm, ∇xlog⁡p(x)\nabla_x \log p(x). At every point it is an arrow that points toward higher density, and its length says how steeply the density is rising or falling there. It never needs the normalising constant of the density, which is why it is useful when that constant is impossible to compute.

Two facts make it central. Averaged over the distribution it is exactly zero, and its variance is the Fisher information, a measure of how much data says about a parameter.

Where you'll see it: in score matching and score-based diffusion models, in Langevin sampling, and in the REINFORCE gradient used to train policies.

Intuition

A map of which way is uphill

Picture a probability density as a landscape, with high ground where values are likely and low ground where they are rare. The score is the compass that sits at each point of that landscape and points uphill. Where the ground is flat at a peak the score is zero, because there is no direction to climb. Far down the slope on the left, the score points right, back toward the hill.

The log is there for a practical reason. Densities are products of many factors, and their values can span many orders of magnitude. Taking the logarithm turns the landscape into a height map where a multiplication becomes an addition, and the gradient of that height map is the score. Multiplying the score by the density gives back the ordinary gradient, so nothing is lost by working with the log.

Why not just use the density?

Often you know a density only up to a constant. Write p(x)=q(x)/Zp(x) = q(x) / Z where qq is easy to compute and ZZ is an integral that is hard or impossible to evaluate. Energy-based models and most Bayesian posteriors look like this. The log of pp splits into log⁡q(x)−log⁡Z\log q(x) - \log Z, and the gradient with respect to xx removes the second term, because ZZ does not depend on xx. So the score can be computed from qq alone. That single fact drives most of the modern uses of the score.

−4−3−2−101234−3−2−10123xvalue
  • score of N(0,1): -x
  • density N(0,1)
For a standard Gaussian the score is -x. It is positive to the left of 0, negative to the right, and zero at the peak, so it always points back toward the centre.

The math

Definition

Definition: Score function

For a positive, differentiable probability density p(x)p(x) on x∈Rdx \in \mathbb{R}^d, the score is the vector

s(x)=∇xlog⁡p(x)=∇xp(x)p(x).s(x) = \nabla_x \log p(x) = \frac{\nabla_x p(x)}{p(x)}.

Its ii-th component is ∂log⁡p(x)/∂xi\partial \log p(x) / \partial x_i.

Here ∇x\nabla_x is the Gradient with respect to the value xx, and log⁡\log is the natural logarithm (Logarithm). The second form follows from the Chain Rule: the derivative of log⁡p\log p is p′/pp'/p. This is why the score is also called the log-derivative of the density.

When the density depends on a parameter θ\theta, we write the score with respect to that parameter, ∇θlog⁡p(x;θ)\nabla_\theta \log p(x; \theta). Setting the sum of these scores over the data to zero is exactly the condition that defines the maximum likelihood estimate.

The Gaussian score

For X∼N(μ,σ2)X \sim \mathcal{N}(\mu, \sigma^2) in one dimension,

log⁡p(x)=−(x−μ)22σ2−log⁡(σ2π).\log p(x) = -\frac{(x-\mu)^2}{2\sigma^2} - \log\left(\sigma\sqrt{2\pi}\right).

The second term does not depend on xx, so it vanishes under the derivative:

s(x)=−x−μσ2.s(x) = -\frac{x - \mu}{\sigma^2}.

The score is a straight line through the mean. It pulls toward μ\mu, and it is steeper when σ\sigma is small, because a narrow bell falls off faster. That linear pull is the reason the Gaussian is the simplest case of Langevin Dynamics.

The score has mean zero

The average score under the distribution itself is zero:

Ep[∇xlog⁡p(X)]=∫p(x) ∇xp(x)p(x) dx=∫∇xp(x) dx=0.\mathbb{E}_{p}\left[\nabla_x \log p(X)\right] = \int p(x)\,\frac{\nabla_x p(x)}{p(x)}\,dx = \int \nabla_x p(x)\,dx = 0.

The first step is the definition of an Expected Value. The p(x)p(x) cancels. The last step holds because the density vanishes at infinity, so the total change of pp over all of space is zero. The identity is short, but it is the root of several results below.

Fisher information

Take a parametric model p(x;θ)p(x;\theta). Its Fisher information is the variance of the score with respect to θ\theta:

I(θ)=E[(∂∂θlog⁡p(X;θ))2].I(\theta) = \mathbb{E}\left[\left(\frac{\partial}{\partial \theta} \log p(X;\theta)\right)^2\right].

The same cancellation as above works for the parameter score, as long as the support of pp does not depend on θ\theta: E[∂θlog⁡p(X;θ)]=∫∂θp(x;θ) dx=∂θ∫p dx=0\mathbb{E}[\partial_\theta \log p(X;\theta)] = \int \partial_\theta p(x;\theta)\,dx = \partial_\theta \int p\,dx = 0. So this second moment is also the variance of the score. See Fisher Information for more. It measures how sharply the log-likelihood peaks when the parameter moves. A sharp peak means the data pins the parameter down.

For a Gaussian with known σ\sigma, the score for the mean is (x−μ)/σ2(x-\mu)/\sigma^2. Its second moment is Var(X)/σ4=1/σ2\mathrm{Var}(X)/\sigma^4 = 1/\sigma^2, so I(μ)=1/σ2I(\mu) = 1/\sigma^2. With nn independent samples the information adds up to n/σ2n/\sigma^2.

Score matching

Sometimes you want to learn a model whose score matches the data's score, without ever knowing the data's normalising constant. Hyvärinen's score matching does this by minimising

J(θ)=12 Ep[∥sθ(X)−∇xlog⁡p(X)∥2],J(\theta) = \frac{1}{2}\,\mathbb{E}_{p}\left[\left\lVert s_\theta(X) - \nabla_x \log p(X) \right\rVert^2\right],

where sθ(x)=∇xlog⁡qθ(x)s_\theta(x) = \nabla_x \log q_\theta(x) is the score of the model. The unknown term ∇xlog⁡p\nabla_x \log p looks like it blocks the way, but integration by parts removes it, provided pp vanishes suitably at infinity and sθs_\theta is differentiable. Up to a constant that does not depend on θ\theta,

J(θ)=Ep[12∥sθ(X)∥2+∇x⋅sθ(X)]+const,J(\theta) = \mathbb{E}_{p}\left[\frac{1}{2}\lVert s_\theta(X) \rVert^2 + \nabla_x \cdot s_\theta(X)\right] + \text{const},

where ∇x⋅sθ\nabla_x \cdot s_\theta is the divergence, the trace of the Jacobian of sθs_\theta. Only the model's score appears now, and the data's score has disappeared.

Langevin sampling

If you know the score of a density, you can draw samples from it with noisy gradient steps:

xk+1=xk+ε2 ∇xlog⁡p(xk)+ε zk,zk∼N(0,I).x_{k+1} = x_k + \frac{\varepsilon}{2}\,\nabla_x \log p(x_k) + \sqrt{\varepsilon}\,z_k, \qquad z_k \sim \mathcal{N}(0, I).

The drift term climbs the score, and the noise keeps the samples spread out. For a small step size ε\varepsilon, the chain's long-run distribution is approximately pp. Only the score is needed, so the normalising constant never has to be computed.

Worked examples

Example
Reading the score off a Gaussian

Let X∼N(2,9)X \sim \mathcal{N}(2, 9), so μ=2\mu = 2 and σ2=9\sigma^2 = 9. What is the score at x=5x = 5?

  1. Use the formula s(x)=−(x−μ)/σ2s(x) = -(x-\mu)/\sigma^2, which gives s(x)=−(x−2)/9s(x) = -(x-2)/9.
  2. Substitute x=5x = 5: s(5)=−(5−2)/9=−3/9s(5) = -(5-2)/9 = -3/9.
  3. So s(5)=−1/3≈−0.333s(5) = -1/3 \approx -0.333.
  4. The score is negative, so the density is falling at 5. Its height is below the peak at 2, and the arrow points left, back toward the mean.
Example
When the boundary breaks the mean-zero rule

Let XX be exponential with rate λ=2\lambda = 2, so p(x)=2e−2xp(x) = 2e^{-2x} for x≥0x \ge 0 and p(x)=0p(x) = 0 otherwise.

  1. For x>0x \gt 0, log⁡p(x)=log⁡2−2x\log p(x) = \log 2 - 2x, so the score is s(x)=−2s(x) = -2 for every x>0x \gt 0.
  2. The expected score is E[s(X)]=−2\mathbb{E}[s(X)] = -2, not zero.
  3. The reason: the density jumps from 0 to p(0)=2p(0) = 2 at the boundary, so ∫0∞p′(x) dx=−p(0)=−2\int_0^\infty p'(x)\,dx = -p(0) = -2 rather than zero.
  4. The mean-zero identity needs the density to vanish at the edges of its support. Check that before you rely on it.
Example
One Langevin step on a standard Gaussian

The target is N(0,1)\mathcal{N}(0, 1), so the score is −x-x. Start at x0=3x_0 = 3 with step size ε=0.1\varepsilon = 0.1, and suppose the noise draw is z0=0.5z_0 = 0.5.

  1. The drift is ε2⋅(−x0)=0.05×(−3)=−0.15\frac{\varepsilon}{2} \cdot (-x_0) = 0.05 \times (-3) = -0.15.
  2. The noise is ε z0=0.1×0.5≈0.316×0.5=0.158\sqrt{\varepsilon}\,z_0 = \sqrt{0.1} \times 0.5 \approx 0.316 \times 0.5 = 0.158.
  3. So x1=3−0.15+0.158≈3.008x_1 = 3 - 0.15 + 0.158 \approx 3.008.
  4. The drift pulled the sample toward 0 by 0.15, and the noise pushed it back up by 0.158. Over many steps the drift wins on average, and the samples settle into the bell around 0.

Tradeoffs and failure modes

The score is powerful, but each use has a characteristic way of failing.

SituationProblemBetter choice
You need actual probabilities or likelihood ratiosThe score carries no normalising constant, so it gives no probability valuesUse a normalised model, or estimate the constant separately
The density has several separated modesLangevin follows the local score, so a chain started in one mode rarely crosses a low-density valleyTempering, annealing, or several chains from different starts
Data lies on a thin, low-dimensional surfaceThe density is concentrated there, so the score is huge or undefined off the surfacePerturb the data with noise at several scales, then learn the score of each noisy version
You need a gradient through a random choice (REINFORCE)The score-function estimator is unbiased but has high varianceBaselines, or the Reparameterization Trick when the distribution allows it
The density is zero at a boundaryThe mean-zero identity fails, as in the exponential exampleHandle the boundary explicitly, or use a transformed variable

Two practical warnings. First, a learned score is only accurate where training data was dense. Sampling then visits low-density regions the model never saw, and the errors compound. Second, do not read the score as a probability. Its size tells you how steep the density is, not how much mass lies nearby.

History

Ronald Fisher built the theory of maximum likelihood and the information measure that now bears his name during the 1920s. The derivative of the log-likelihood sits at the centre of that theory, and it came to be called the score. Fisher's framework is the reason we can say how precisely a parameter can be estimated from data.

For decades the score mostly served estimation. Ronald Williams, in 1992, showed that the score-function gradient could train stochastic units in neural networks. This is the REINFORCE method, and it is still the basis of policy gradient reinforcement learning.

Aapo Hyvärinen introduced score matching in 2005. It was designed for models whose normalising constant cannot be computed, and the integration-by-parts trick above made the objective practical. Yang Song and Stefano Ermon revived it in 2019. They trained a network to estimate the score of noise-perturbed data at several noise levels, and then sampled with annealed Langevin dynamics, moving from large noise to small. That approach is a close relative of the diffusion models that became prominent soon after, where the network learns the score of progressively noisier images.

Check yourself

QuestionWrite the score function of a density p(x)p(x) in one line.Show answer

s(x)=∇xlog⁡p(x)s(x) = \nabla_x \log p(x), the gradient with respect to xx of the natural log of the density. Equivalently ∇xp(x)/p(x)\nabla_x p(x) / p(x).

QuestionA Gaussian has mean 0 and variance 4. What is its score at x=−2x = -2, and which way does it point?Show answer

The score is −(x−μ)/σ2=−(−2−0)/4=0.5-(x-\mu)/\sigma^2 = -(-2-0)/4 = 0.5. It is positive, so it points right, toward the mean at 0.

QuestionWhy is the score useful when the normalising constant of a density is unknown?Show answer

Write p(x)=q(x)/Zp(x) = q(x)/Z. Then log⁡p(x)=log⁡q(x)−log⁡Z\log p(x) = \log q(x) - \log Z. The constant ZZ does not depend on xx, so the gradient with respect to xx removes log⁡Z\log Z, and the score depends only on the computable qq.

QuestionA Langevin sampler for a density with two separated humps keeps returning to the left hump and never visits the right. Why, and what would you change?Show answer

Langevin steps follow the local score, which is near zero at each hump's peak and gives no push across the low-density valley between them. The chain mixes very slowly. Changing the sampler to use tempering or annealing (more noise early, to flatten the valley) or running several chains from different starting points would help.

Sources