Runoob Tutorial ArchiveEnglish

Maximum Likelihood Estimation and Maximum A Posteriori Estimation

Maximum Likelihood Estimation and Maximum A Posteriori Estimation

MLE and MAP are two core methods of statistical inference. The essence of training a neural network is approximately MLE.


Concept Analysis

Assume you have a set of observed data, and you believe this data comes from a known type of probability distribution (such as a normal distribution), but you do not know the parameters of this distribution (mean $\mu$ and standard deviation $\sigma$).

MLE and MAP answer the same question: Given the data, what is the most reasonable parameter value?

The difference between the two is that MLE only looks at the data itself, while MAP also considers your prior knowledge about the parameters.

Likelihood vs. Probability: Looking at the same thing from two directions

Probability P(data|parameters): With parameters fixed, it asks "What is the probability of observing this data under these parameters?"

Likelihood L(parameters|data): With data fixed, it asks "Which parameter value is most likely to have produced this data?"

The value of the likelihood function itself is not normalized (it does not require the sum to be 1); its meaning is to compare the relative plausibility of different parameter values.

MLE: Which parameters are most likely to have generated this data?

[ \hat{\theta}{\text{MLE}} = \arg\max\theta P(D | \theta) ]

Only looking at the data, select the parameters most likely to have produced the observed data.

Why take the log?

MAP: introducing prior knowledge

[ \hat{\theta}{\text{MAP}} = \arg\max\theta P(\theta | D) = \arg\max_\theta P(D | \theta) \cdot P(\theta) ]

MAP = MLE + prior P(θ). When the prior is a uniform distribution, MAP = MLE.

MLE only looks at data, while MAP also looks at the prior. When the amount of data is large, the influence of the prior is diluted, and MAP approaches MLE.

When the amount of data is small, the prior acts as a "regularizer"—L2 regularization ≡ MAP of a Gaussian prior.


Real-life example

**Estimate average daily sales

Online store daily sales for 10 consecutive days: [23, 25, 22, 24, 26, 23, 25, 24, 22, 24].

Assuming sales follow a normal distribution, the MLE estimate of the mean = the average of these numbers ≈ 23.8.

Intuitively, you would do this—MLE is just mathematicalizing intuition.


Python hands-on practice

Example

import

numpy

as

np

np.

random

.

seed

(

42

)

data

=

np.

random

.

normal

(

5.0

,

2.0

,

100

)

 

True $\mu$=5, $\sigma$=2

MLE: $\mu$ = sample mean

mu_mle

=

np.

mean

(

data

)

print

(

f

"MLE μ = {mu_mle:.3f} (True=5.0)"

)

MAP: Prior μ ~ N(0, 1), shrinks towards the prior direction

prior_mu

,

prior_var

=

0.0

,

1.0

n

=

len

(

data

)

mu_map

=

(

prior_mu/prior_var + n*mu_mle/np.

std

(

data

)

**

2

)

/

(

1

/prior_var + n/np.

std

(

data

)

**

2

)

print

(

f

"MAP μ = {mu_map:.3f} (a compromise between MLE and prior 0)"

)

Run output:

Original prompt (Chinese, left untranslated)



MLE μ = 4.968 (真实=5.0)

MAP μ = 4.776 (在MLE和先验0之间折中)


Application scenarios in AI

Loss function design = MLE

Minimizing cross-entropy loss ≈ Maximum Likelihood Estimation under a multinomial distribution assumption. Minimizing MSE ≈ MLE under a normal error distribution assumption. The loss function is not chosen arbitrarily—each loss function corresponds to an implicit probability distribution assumption.

L2 regularization = MAP of a Gaussian prior

Adding ( \lambda|w|^2 ) to the loss is equivalent to assuming the prior of weights w is ( N(0, 1/\lambda) ). MAP balances between data likelihood and prior—when data is scarce, the prior dominates (weights tend towards 0), and when data is abundant, the likelihood dominates.

L1 regularization = MAP of the Laplace prior

Adding ( \lambda|w|_1 ) to the loss is equivalent to assuming the prior of weights w is the Laplace(0, 1/\lambda) distribution. The Laplace distribution is sharp at 0—MAP tends to produce sparse solutions (many weights are exactly 0), achieving automatic feature selection.

Bayesian Neural Networks

The weights of a standard neural network are fixed point estimates (MLE/MAP). Bayesian neural networks assign a probability distribution (posterior) to each weight, and the output is also a probability distribution—thus having natural uncertainty estimation. During prediction, weights can be sampled multiple times, and the variance of the output can be used to judge the model's confidence in its prediction.