Maximum Likelihood Estimation › Formula
Machine Learning from ScratchAll chapters Maximum Likelihood Estimation › FormulaSection view · Sections in this chapter4 / 10 ↓
Reading tools Definitions, links & notes
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
We can build the likelihood in three steps. First, hold the observed data fixed; they are no longer random quantities to be predicted. Second, choose a probability model that assigns a probability or density to each observation for any candidate parameter value. Third, compare candidate parameter values by asking which one gives the entire observed sample the highest combined plausibility. The formula below is simply the bookkeeping for those three steps.
Suppose we observe independent and identically distributed data . Let be the probability mass function for discrete data or the probability density function for continuous data, where is a parameter or a vector of parameters. The likelihood is
where:
- : the likelihood of the parameter given the full observed sample
- : the number of observations in the sample
- : the -th observed data value, held fixed as varies
- : the probability mass (discrete case) or probability density (continuous case) assigned to by the model with parameter
- : the product over all observations from to
The product symbol means that we multiply the terms for through . That multiplication is not an arbitrary convention. Independence is exactly what makes the joint probability or joint density of the sample factor into one term per observation. If the observations were dependent, the product of marginal terms would not be the joint distribution, and the likelihood would have to be built from the joint model instead. The semicolon in separates the observed data from the parameter ; it reminds us that the data are fixed and is the input we are varying. For discrete data, each is a probability, so the product is the probability of the observed sequence under independence. For continuous data, each term is a density, so the product is a likelihood density rather than the probability of observing an exact value.
The log-likelihood is
The symbols in this expression are:
- : the log-likelihood, the natural logarithm of the likelihood
- : the natural logarithm
- : the sum over all observations from to
- the remaining symbols are defined as above.
The logarithm is monotone increasing. If , then . Therefore the parameter that maximizes also maximizes . Logs turn products into sums, which are easier to differentiate and much less likely to underflow when is large. It is common to see negative log-likelihood minimized instead, because optimization software usually minimizes rather than maximizes. These three facts, factorization under independence, monotone transformation, and numerical stability, explain nearly every manipulation that follows.
Likelihood and log-likelihood
The likelihood is a function of the parameter, not a probability distribution over the parameter. This point deserves emphasis. If we compute and , we can say that the observed data are five times more likely under than under in the likelihood ratio sense. We cannot say that there is a 1% probability that the parameter equals . To obtain a probability distribution over , we would need a prior distribution and a normalization step, which leads to Bayesian inference. Here we remain in the frequentist likelihood framework.
The independence assumption is what allows us to multiply the individual terms. If observations are correlated, the product of marginal probabilities is not the joint probability, and the likelihood must be built from the joint distribution instead. Identically distributed means that every observation has the same distribution. Independent observations may instead have different densities, all indexed by a shared parameter; their likelihood is still the product of those individual densities. Dependence requires a joint model.
For discrete data, the likelihood is a probability of the observed data. For continuous data, it is a density. A density can be greater than one when the distribution is concentrated over a narrow range, so a likelihood value above one is not an error. For estimation, what matters is the relative size of the likelihood at different parameter values, not whether the number looks like a probability.
To turn this function into an estimate, we need a way to locate its peak. The score is the first derivative of the log-likelihood:
where:
- : the score, the first derivative of the log-likelihood with respect to the parameter
- : the parameter (or parameter vector) being estimated
- : the partial derivative of the log-likelihood with respect to
At a differentiable maximum in the interior of the parameter space, the score is zero:
This equation is called the score equation or the likelihood equation. In simple models we can solve it algebraically. In more complex models we solve it numerically. The score tells us the local slope of the log-likelihood. A positive score means increasing increases the log-likelihood, and a negative score means decreasing increases it. A zero score means we are at a flat point, which could be a maximum, a minimum, or a saddle point. A negative second derivative gives a strict local maximum in one dimension; a positive one gives a strict local minimum. A zero second derivative is inconclusive. For a vector parameter, negative definiteness of the Hessian is a sufficient local maximum test.
Bernoulli and Gaussian worked estimators
The general formula becomes concrete only after we choose a probability model. You now derive two estimators that appear constantly in practice. The first is the Bernoulli probability, which gives us the sample proportion. The second is the Gaussian mean and variance, which gives us the sample mean and the maximum likelihood variance. These derivations show how the general likelihood formula becomes a concrete estimate.
For Bernoulli data, each observation is either or . Assume , and let be the probability of a success. For , the probability mass is
where:
- : the -th observation, taking the value for a success or for a failure
- : the probability of a success, the unknown parameter
- : the probability of a failure
When , this becomes . When , it becomes . If we observe successes and failures, then the product over all observations is
where:
- : the number of observed successes
- : the number of observed failures
- : the success probability
The log-likelihood is
where:
- : the log-likelihood for the Bernoulli success probability
- : the number of observed successes
- : the number of observed failures
- : the success probability
Differentiating with respect to gives
where:
- : the derivative of the Bernoulli log-likelihood with respect to
- : the term contributed by the successes
- : the derivative term contributed by the failures
Setting this equal to zero, we multiply both sides by :
where:
- : the number of observed successes
- : the candidate failure probability
- : the number of observed failures
- : the success probability
Expanding gives , and the terms cancel, leaving . Therefore
The second derivative is
where:
- : the second derivative of the Bernoulli log-likelihood
- : the magnitude of the negative curvature contribution from the successes
- : the magnitude of the negative curvature contribution from the failures
This derivative is negative for . With , the stationary point is the unique global maximum. On the closed parameter space , or puts the maximum at or , using the limiting convention . The plug-in Wald standard error is zero there, but that does not establish zero uncertainty. The interior information formula below is not valid at those endpoints.
If we only record the count rather than the exact sequence of successes and failures, the binomial likelihood is . The binomial coefficient does not depend on , so it does not change the maximizing value. That is why we can work with the simpler Bernoulli form.
For Gaussian data, suppose . The density is
where:
- : the mean of the normal distribution
- : the variance of the normal distribution
- : the standard deviation, the positive square root of the variance
- : the exponential function
The log-likelihood for all observations is
where:
- : the Gaussian log-likelihood for all observations
- : the number of observations
- : the constant log term from the normalizing factor
- : the log of the variance
- : the sum of squared deviations divided by twice the variance
To estimate , we differentiate with respect to . The first two terms do not depend on , and the derivative of the squared term brings down a factor of :
where:
- : the score with respect to the Gaussian mean
- : the variance of the normal distribution
- : the deviation of the -th observation from the candidate mean
Setting this to zero gives
where:
- : the score set to zero
- : the sum of all observations
- : the sum if every observation were equal to
- : the maximum likelihood estimate of the mean
- : the sample mean
The maximum likelihood estimate of the mean is the sample mean. This result does not depend on the value of .
To estimate the variance, let and write the sum of squared deviations as
After substituting , the profile log-likelihood for becomes
where:
- : the Gaussian log-likelihood with the mean fixed at its fitted value and candidate variance
- : the number of observations
- : the candidate variance
- : the sum of squared deviations from the sample mean
Differentiating with respect to gives
Setting this to zero, we multiply by :
where:
- : the number of observations
- : the candidate variance
- : the sum of squared deviations from the sample mean
- : the maximum likelihood variance estimate
If , the profile likelihood rises for and falls for , so this is the global maximum over . If all observations are identical, and the Gaussian likelihood is unbounded as tends to zero: no positive-variance MLE is attained. For nondegenerate i.i.d. Gaussian samples, the variance estimator is biased downward because
where:
- : expectation over repeated samples
- : the sum of squared deviations from the sample mean
- : the number of observations
- : the true variance
The bias shrinks as grows, and the estimator is consistent, but it is not unbiased. This is a concrete reminder that maximum likelihood and unbiasedness are different properties.
The Gaussian likelihood also explains why squared error appears so often in regression. If we model with , the log-likelihood is
Here is a column vector of responses, is a matrix with one row per observation and one column per predictor, is a column vector of coefficients, and is the identity matrix. The expression is exactly the sum of squared errors written in matrix notation. For a fixed , maximizing the likelihood over is the same as minimizing the sum of squared errors. We will see the full regression derivation in the later chapters on ordinary least squares, but the likelihood connection is already visible here.
Four normal densities
The mean controls the center of the distribution, and the standard deviation controls its width. When you evaluate a likelihood, you are not plotting a distribution over or . You are comparing candidate distributions over the observed data .
PythonIn [3]Expand
import matplotlib.pyplot as plt
from book_plot_style import PALETTE, polish_axes, theme_color, use_book_style
use_book_style()
fig, ax = plt.subplots(figsize=(6.0, 4.0))
plot_colors = [
PALETTE["primary"],
PALETTE["amber"],
PALETTE["teal"],
PALETTE["muted"],
]
for (mu, sigma, label), density, color in zip(
normal_variants, normal_densities, plot_colors
):
ax.plot(x_grid, density, color=color, label=label)
ax.set_xlabel("Observation y")
ax.set_ylabel("Density f(y)")
ax.set_title("Candidate normal distributions")
ax.legend()
polish_axes(ax)
plt.show()VisualizationOut [3]Expand

Score, curvature, and numerical estimation
The score equation is the first-order condition for a maximum. To confirm a maximum and to measure uncertainty, we use the second derivative. Intuitively, the first derivative tells us where the likelihood is flat, while the second derivative tells us how quickly it curves away from that flat point. A sharp peak is informative. A broad, nearly flat peak is not. The observed information is
where:
- : the observed information, evaluated at the estimate
- : the maximum likelihood estimate of the parameter
- : the second derivative of the log-likelihood with respect to
- the vertical bar with subscript : evaluation of the second derivative at the estimate
For a vector parameter, the second derivative becomes a Hessian matrix, and the observed information is the negative Hessian:
The Fisher information is the expected value of the observed information under the model:
where:
- : the Fisher information in the full sample for the parameter
- : the expectation taken over repeated samples generated with the true parameter equal to
- : the Hessian of the log-likelihood (the matrix of second partial derivatives)
For a correctly specified regular i.i.d. model, , where is per-observation information. Under the usual consistency and derivative-convergence conditions, and converge to the same nonsingular limit. This statement concerns information per observation; their unnormalized difference need not tend to zero.
At a regular interior estimate with positive information, a large-sample standard error for a scalar parameter is
where:
- : the approximate standard error of the estimate
- : the reciprocal of the observed information (the inverse information matrix in the vector case)
- : the second derivative of the log-likelihood evaluated at the estimate
For a regular interior vector estimate with nonsingular positive-definite observed information, its inverse approximates the covariance matrix:
The diagonal entries give approximate variances, and their square roots give approximate standard errors. This is where the shape of the likelihood becomes uncertainty: a sharply curved peak has large information and small standard error, while a flat peak has small information and large standard error.
The quadratic approximation below shows why curvature is used as a local summary of information. Near the peak, the log-likelihood and its second-order approximation almost coincide; farther away, the approximation drifts from the exact curve.
PythonIn [4]Expand
bernoulli_p_hat_quad = 12 / 20
bernoulli_info_quad = 20 / (bernoulli_p_hat_quad * (1 - bernoulli_p_hat_quad))
bernoulli_p_grid_quad = np.linspace(0.35, 0.85, 500)
bernoulli_loglik_quad = 12 * np.log(bernoulli_p_grid_quad) + 8 * np.log(
1 - bernoulli_p_grid_quad
)
bernoulli_loglik_quad_relative = bernoulli_loglik_quad - (
12 * np.log(bernoulli_p_hat_quad) + 8 * np.log1p(-bernoulli_p_hat_quad)
)
bernoulli_quadratic_approx = (
-0.5
* bernoulli_info_quad
* (bernoulli_p_grid_quad - bernoulli_p_hat_quad) ** 2
)VisualizationOut [5]Expand

For the Bernoulli model with , the observed information at is
where:
- : the observed information for the Bernoulli probability
- : the number of trials
- : the estimated success probability
- : the estimated failure probability
so the approximate standard error is
where:
- : the approximate standard error of the Bernoulli estimate
- : the estimated success probability
- : the estimated failure probability
- : the number of trials
This is the familiar standard error of a sample proportion. For the Gaussian mean with known variance, the observed information is , so the standard error is . For the Gaussian variance, the observed information at is . The approximate standard error is therefore , where is the variance estimate.
When no closed-form solution exists, you can maximize the log-likelihood numerically. A simple one-dimensional approach is a grid search: evaluate the log-likelihood on a fine grid of parameter values and choose the grid point with the largest value. Grid search is easy to reason about but becomes impractical in more than a few dimensions.
A more scalable approach is Newton-Raphson. It comes from a second-order Taylor expansion of the objective around the current iterate, and it approximately solves the score equation at each step. To maximize , you update
where:
- : the parameter value at iteration
- : the updated parameter value
- : the gradient (score) of the log-likelihood at
- : the Hessian matrix of second derivatives at
- : the matrix inverse
The gradient points in the direction of increase, and the inverse Hessian rescales that direction. The update is equivalent to solving , which makes the score approximately zero after the step. When the Hessian is negative definite and the score is nonzero, the Newton direction is an ascent direction, but the full step can overshoot and decrease the likelihood. A line search can shorten the step. If the starting value is poor or the Hessian is not negative definite, Newton-Raphson can diverge. Practical implementations often minimize the negative log-likelihood with quasi-Newton methods, add bounds, or reparameterize positive quantities using logarithms. For example, instead of optimizing directly, you can optimize and exponentiate the result. This keeps the standard deviation positive.
Identifiability, sampling uncertainty, and limitations
A model is identifiable if different parameter values produce different distributions for the data. Formally, if
implies , then the parameter is identifiable. Here is the model probability of an event . If distinct parameters give the same distribution, no amount of data from that model can distinguish those equivalent values. A continuous equivalence can create a flat ridge, while a discrete label symmetry can create separate equivalent peaks. A mixture model with unlabeled components is a classic example: swapping the labels of two components gives the same likelihood. In linear regression, perfectly collinear predictor columns make singular, so different coefficient vectors can produce the same fitted values. We discussed the practical consequences of that in Multicollinearity.
Identifiability is a property of the model and the data design, not a property of the optimizer. A numerical optimizer may return a point on a flat ridge, but the data do not distinguish that point from the other equivalent points. Before trusting an estimate, ask whether the data contain enough variation to distinguish the parameters. If not, we need to simplify the model, collect more informative data, or impose a constraint.
Sampling uncertainty is different from identifiability. Even in an identifiable model, the maximum likelihood estimate is a random variable because it depends on a random sample. For a correctly specified identifiable model with an interior true parameter, sufficient smoothness, nonsingular Fisher information, a consistent maximizing solution, and the derivative-convergence conditions used in the asymptotic argument, we obtain:
where:
- : the square root of sample size, which scales the centered estimation error to a nondegenerate limit under the stated regularity conditions
- : the maximum likelihood estimate
- : the true parameter value
- : convergence in distribution as
- : the limiting normal distribution with mean zero and covariance
- : the per-observation Fisher information evaluated at the true parameter
This is the basis for the curvature-based standard errors above. The approximation can be poor in small samples, near boundaries, or when the model is misspecified. Alternatives include profile likelihood, a bootstrap appropriate to the sampling design, and Bayesian inference with an explicit prior. Each requires its own assumptions and coverage or sensitivity checks; none automatically repairs a boundary or a misspecified model.
Maximum likelihood also has failure modes that are worth recognizing. The likelihood can be unbounded, as in a Gaussian mixture when a component variance collapses to zero around a single point. In logistic regression, complete separation can push coefficients toward infinity because the likelihood keeps increasing as the separating boundary becomes sharper. Non-concave likelihoods can have multiple local maxima, so different starting values may produce different answers. And if the model is wrong, the estimate may be precise but precisely wrong.
Mathematical Properties
Maximum likelihood has several properties that make it useful beyond the basic examples. The first is invariance under reparameterization. If is the maximum likelihood estimate of , and is a one-to-one function, then is the maximum likelihood estimate of . For example, if , then is the finite maximum likelihood estimate of the odds. This property is convenient, but it does not mean that standard errors transform by simply plugging in the transformed estimate. Uncertainty usually requires the delta method or a profile-likelihood calculation.
The second property is additivity for independent observations. The log-likelihood is a sum of per-observation log-density or log-mass contributions, so each new independent observation adds one term. This makes the likelihood scalable and explains why the curvature typically grows with sample size. In regular models, information adds across independent observations. For a correctly specified regular i.i.d. model with fixed, finite, positive definite per-observation information, total information is proportional to , and the usual large-sample standard errors shrink at rate .
Under regularity conditions, maximum likelihood estimators are consistent and asymptotically efficient. Consistency means the estimate converges to the true parameter as the sample size grows. Asymptotic efficiency means attaining the inverse-Fisher-information bound within the regular estimator class. It does not compare every possible consistent estimator at every parameter value. These are large-sample guarantees. They do not imply that the estimate is unbiased in a small sample, that the model is correct, or that the likelihood is well behaved in every finite dataset.
Finally, maximum likelihood is not the same as least squares in general. For independent Gaussian errors and any fixed common positive variance, maximizing over the mean parameters is equivalent to minimizing squared error. Bernoulli, Poisson, and other non-Gaussian likelihoods generally give different objectives, although their maximizing estimates can sometimes coincide with least-squares estimates. This is why the likelihood framework is more general than squared error, and why the choice of likelihood encodes assumptions about the data-generating process.
Comments
Add to the conversation
A question, a correction, or an example from your own work.
No comments yet. Be the first to share your thoughts!