TL;DR
The score function of a probability density is the gradient of its logarithm, . 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 where is easy to compute and is an integral that is hard or impossible to evaluate. Energy-based models and most Bayesian posteriors look like this. The log of splits into , and the gradient with respect to removes the second term, because does not depend on . So the score can be computed from alone. That single fact drives most of the modern uses of the score.
- score of N(0,1): -x
- density N(0,1)
The math
Definition
For a positive, differentiable probability density on , the score is the vector
Its -th component is .
Here is the Gradient with respect to the value , and is the natural logarithm (Logarithm). The second form follows from the Chain Rule: the derivative of is . This is why the score is also called the log-derivative of the density.
When the density depends on a parameter , we write the score with respect to that parameter, . 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 in one dimension,
The second term does not depend on , so it vanishes under the derivative:
The score is a straight line through the mean. It pulls toward , and it is steeper when 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:
The first step is the definition of an Expected Value. The cancels. The last step holds because the density vanishes at infinity, so the total change of 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 . Its Fisher information is the variance of the score with respect to :
The same cancellation as above works for the parameter score, as long as the support of does not depend on : . 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 , the score for the mean is . Its second moment is , so . With independent samples the information adds up to .
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
where is the score of the model. The unknown term looks like it blocks the way, but integration by parts removes it, provided vanishes suitably at infinity and is differentiable. Up to a constant that does not depend on ,
where is the divergence, the trace of the Jacobian of . 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:
The drift term climbs the score, and the noise keeps the samples spread out. For a small step size , the chain's long-run distribution is approximately . Only the score is needed, so the normalising constant never has to be computed.
Worked examples
Let , so and . What is the score at ?
- Use the formula , which gives .
- Substitute : .
- So .
- 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.
Let be exponential with rate , so for and otherwise.
- For , , so the score is for every .
- The expected score is , not zero.
- The reason: the density jumps from 0 to at the boundary, so rather than zero.
- The mean-zero identity needs the density to vanish at the edges of its support. Check that before you rely on it.
The target is , so the score is . Start at with step size , and suppose the noise draw is .
- The drift is .
- The noise is .
- So .
- 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.
| Situation | Problem | Better choice |
|---|---|---|
| You need actual probabilities or likelihood ratios | The score carries no normalising constant, so it gives no probability values | Use a normalised model, or estimate the constant separately |
| The density has several separated modes | Langevin follows the local score, so a chain started in one mode rarely crosses a low-density valley | Tempering, annealing, or several chains from different starts |
| Data lies on a thin, low-dimensional surface | The density is concentrated there, so the score is huge or undefined off the surface | Perturb 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 variance | Baselines, or the Reparameterization Trick when the distribution allows it |
| The density is zero at a boundary | The mean-zero identity fails, as in the exponential example | Handle 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 in one line.Show answer
, the gradient with respect to of the natural log of the density. Equivalently .
QuestionA Gaussian has mean 0 and variance 4. What is its score at , and which way does it point?Show answer
The score is . 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 . Then . The constant does not depend on , so the gradient with respect to removes , and the score depends only on the computable .
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.