Runoob Tutorial ArchiveEnglish

Bayes' Theorem — Updating your judgment with new evidence

Bayes' Theorem -- Updating your judgment with new evidence

Bayes' Theorem is the cornerstone of probabilistic reasoning. Prior belief + New evidence → Updated belief.


Concept analysis

Prior P(H)

Belief before seeing evidence

×

Likelihood P(E|H)

Credibility of evidence

→

Posterior P(H|E)

Updated belief

[ P(H|E) = \frac{P(E|H) \cdot P(H)}{P(E)} ]

P(E) is the normalization factor: ( P(E) = P(E|H)P(H) + P(E|\text{not }H)P(\text{not }H) ).

Core idea:Continuously update our judgment with new dataThis is the basic pattern for all Bayesian methods.


Real-life examples

**The false positive trap in disease detection

Disease prevalence is 0.1%, and detection accuracy is 99%. If you test positive—the probability of actually having the disease is only about 9%.

Why? Because the disease is extremely rare, and the absolute number of false positives (1% of healthy people misdiagnosed) far exceeds the true positives.

Bayes' theorem reveals this counter-intuitive conclusion: when the prior probability is extremely low, a positive result is likely a false alarm even if the test is very accurate.


Python hands-on practice

Instance

Disease Detection Bayesian Analysis

P_H

=

0.001

         

Prior: Prevalence 0.1%

P_E_given_H

=

0.99

   

Sensitivity 99%

P_E_given_notH

=

0.01

False positive rate 1%

P_E

=

P_E_given_H * P_H + P_E_given_notH *

(

1

)

P_H_given_E

=

P_E_given_H * P_H / P_E

print

(

"=== RUNOOB Disease Detection ==="

)

print

(

f

"Prior prevalence: {P_H:.1%}"

)

print

(

f

"Actual probability of being sick after testing positive: {P_H_given_E:.1%}"

)

print

(

f

"Conclusion: Under extremely low base probability, false positives far exceed true positives"

)

Output:

Original prompt (Chinese, left untranslated)



=== RUNOOB 疾病检测 ===

先验患病率: 0.1%

检测阳性后,实际患病概率: 9.0%

结论:极低基础概率下,假阳性远超真阳性


Application scenarios in AI

Naive Bayes classifier

Assuming conditional independence between features, calculate the posterior probability P(class|feature) using Bayes' theorem and select the class with the highest posterior. It performs surprisingly well in text classification (spam filtering, sentiment analysis)—although the "naive" independence assumption often doesn't hold in practice, the classification accuracy remains high.

Bayesian optimization for hyperparameter tuning

When selecting hyperparameters like learning rate and number of layers, each training run is time-consuming. Bayesian optimization uses a Gaussian process as a prior, updates the posterior distribution based on evaluated hyperparameter combinations (evidence), and intelligently selects the next set of most promising hyperparameters. It is much more efficient than grid search and random search.

Variational Autoencoder (VAE)

The encoder of a VAE outputs the mean $\mu$ and variance $\sigma^2$ of the latent distribution, and then constrains this distribution to be close to the standard normal prior $N(0,1)$ using KL divergence. The "prior $\rightarrow$ posterior" framework here is Bayes' theorem—the prior is $N(0,1)$, the likelihood comes from the reconstruction error of the decoder, and the posterior is the distribution output by the encoder.