Artificial intelligence frequently operates with incomplete information. An email classifier cannot know with certainty whether every message is spam; a speech recognizer must choose among plausible transcriptions; and a forecasting model must account for uncertain future outcomes. Probability provides a mathematical language for representing such uncertainty.
Bayes’ theorem is a rule for updating probabilities when new evidence becomes available. Together, probability and Bayesian reasoning help explain how AI systems combine prior information, observed data, and assumptions to make predictions. These ideas also provide foundations for Naive Bayes classification, model evaluation, and probabilistic machine learning.
Start with a simple question: what changes when you learn something new?
Imagine that only a few messages arriving in your inbox are spam. A filter flags a message because it contains a suspicious phrase. Should you immediately assume it is spam? Not necessarily. A suspicious phrase might also appear in a legitimate message. The answer depends on two pieces of information: how common spam is before seeing the phrase, and how often the phrase appears in spam versus legitimate email.
That is the central idea behind conditional probability and Bayesian updating. Probability describes uncertainty before an observation; Bayes’ theorem tells us how to revise it after observing evidence.
For a concrete illustration, suppose 10 out of every 100 emails are spam. A detector flags 8 of those 10 spam emails, but it also flags 9 of the 90 legitimate emails. Among the 17 flagged messages, only 8 are actually spam. So a flag raises the probability of spam from 10% to about 47%, not to 80%. The detector’s 80% detection rate and the probability that a flagged email is spam are different quantities.
As you read, focus on the question being conditioned on. Does a number refer to the probability of a flag given spam, or the probability of spam given a flag? Bayes’ theorem relates these two quantities, but does not make them equal.
What is probability?
Probability is a mathematical measure of the likelihood of an event within a specified model of uncertainty. It takes a value between and , inclusive:
Here, denotes the probability of event .
- means the event has probability zero in the model.
- means the event occurs with probability one in the model.
- means the model assigns the event a probability of .
Important: A probability of zero does not always mean an event is logically impossible. In continuous models, an exact outcome can have probability zero even though it is possible. Similarly, a model’s assigned probability is not a guarantee about a particular prediction.
Experiments, outcomes, and events
The basic terms are:
| Term | Meaning | Example |
|---|---|---|
| Random experiment | A process whose outcome is uncertain before it is observed | Rolling a die |
| Outcome | One possible result of the experiment | The die shows |
| Sample space | The set of all possible outcomes, denoted | |
| Event | A subset of the sample space | Getting an even number, |
For a fair six-sided die, all six outcomes are equally likely. If is the event that the result is even, then:
This counting method works when the possible outcomes are finite and equally likely. In general, outcomes can have different probabilities, so probability cannot always be calculated simply by counting favorable outcomes.
Three probability rules
Probability obeys a few fundamental rules:
- Non-negativity: for every event .
- Normalization: , because the sample space represents all possible outcomes.
- Additivity: If events and cannot occur together, .
The complement of , written , is the event that does not occur:
For any two events, whether or not they overlap:
The symbols and mean union (at least one event occurs) and intersection (both events occur), respectively. We subtract the intersection because it would otherwise be counted twice.
Conditional probability: when information changes the question
A conditional probability measures the probability of one event given that another event has occurred.
For events and , provided :
The vertical bar means given. Thus, is read as the probability of A given B.
Example: selecting a student
Suppose a class contains students:
| Studies Python | Does not study Python | Total | |
|---|---|---|---|
| Studies AI | 12 | 8 | 20 |
| Does not study AI | 6 | 14 | 20 |
| Total | 18 | 22 | 40 |
Let mean the selected student studies AI and mean the selected student studies Python. Assume one student is selected uniformly at random.
The unconditional probability that the student studies AI is:
But given that the student studies Python, the relevant group contains only the Python students. Of these, also study AI:
Notice that , while . Learning that a student studies Python changes the probability assigned to studying AI.
The reverse conditional probability is different:
In general, is not equal to . Confusing these two probabilities is a common mistake when interpreting AI predictions.
Joint probability and the multiplication rule
The joint probability is the probability that both events occur.
Rearranging the definition of conditional probability gives:
Equivalently, when :
This is often called the multiplication rule. It allows a joint probability to be calculated from a conditional probability and a marginal probability.
In the student example:
Using the multiplication rule gives the same result:
Independence and conditional independence
Two events and are independent if knowing whether one occurs does not change the probability of the other. Formally:
When the relevant conditioning event has positive probability, this is equivalent to .
For example, suppose a fair coin is flipped twice. Let be the first flip is heads and be the second flip is heads. The outcomes of the two flips are independent:
Independent events are not the same as mutually exclusive events. Mutually exclusive events cannot happen together. If two mutually exclusive events both have positive probability, they cannot be independent.
Conditional independence
Events and are conditionally independent given when learning provides no additional information about once is known. For , the defining relationship is:
This assumption appears in Naive Bayes classifiers: individual features are treated as conditionally independent once the class label is known. It is a simplifying modeling assumption, not a claim that words or other features are independent in everyday life.
Bayes’ theorem
Bayes’ theorem relates the conditional probability to the reverse conditional probability .
Starting with the multiplication rule:
Substitute this expression into the definition of :
The result is Bayes’ theorem:
This form is valid when .
The four parts of a Bayesian update
In a classification setting, let represent a hypothesis and represent observed evidence:
| Quantity | Name | Interpretation |
|---|---|---|
| Prior probability | Probability assigned to the hypothesis before the new evidence | |
| Likelihood | How probable the observed evidence is if the hypothesis is true | |
| Evidence or marginal likelihood | Overall probability of observing the evidence | |
| Posterior probability | Updated probability of the hypothesis after observing the evidence |
The distinction between likelihood and posterior probability matters. A signal can be common when a hypothesis is true without making that hypothesis highly probable whenever the signal is observed.
When there are two exhaustive hypotheses, and , the law of total probability gives:
This supplies the denominator required to compute the posterior probability.
Worked example: an email spam warning
Imagine an email filtering system with these hypothetical characteristics:
- Only of incoming emails are spam.
- It flags of spam emails as suspicious.
- It incorrectly flags of non-spam emails.
A new email has been flagged. What is the probability that it is actually spam?
Define:
- : the email is spam.
- : the email is flagged.
The given probabilities are:
Apply Bayes’ theorem and the law of total probability:
Substitute the numbers:
The probability that a flagged email is spam is approximately under these assumptions, not .
Why? The filter detects most spam, but legitimate messages are so much more common that the false alarms outnumber the correctly flagged spam messages.
Visualizing the base rate
The same calculation is easier to see using a hypothetical batch of emails:
| Actual class | Flagged | Not flagged | Total |
|---|---|---|---|
| Spam | 95 | 5 | 100 |
| Not spam | 990 | 8,910 | 9,900 |
| Total | 1,085 | 8,915 | 10,000 |
Among the flagged messages, are truly spam:
This illustrates the base-rate fallacy: overlooking how common or rare a condition is in the population when interpreting evidence about an individual case.
Calculating the posterior in Python
prior_spam = 0.01
sensitivity = 0.95 # P(flagged | spam)
false_positive_rate = 0.10 # P(flagged | not spam)
p_flagged = (
sensitivity * prior_spam
+ false_positive_rate * (1 - prior_spam)
)
posterior_spam = sensitivity * prior_spam / p_flagged
print(f"P(spam | flagged) = {posterior_spam:.2%}")
Output:
P(spam | flagged) = 8.76%
This example uses given probabilities. In real applications, priors and error rates must be estimated from suitable data, and the resulting predictions must be evaluated on representative examples.
Probability distributions and random variables
A random variable assigns a numerical value to each outcome of a random experiment. A probability distribution describes how probability is assigned across that variable’s possible values.
Two broad kinds of random variables are important in AI:
| Kind | Possible values | Mathematical description | Example |
|---|---|---|---|
| Discrete | Countable values | Probability mass function (PMF) | Number of clicks on an ad |
| Continuous | Values over intervals | Probability density function (PDF) | A measured temperature |
Discrete distributions
A probability mass function directly assigns probability to individual possible values. Its probabilities must be nonnegative and sum to :
A Bernoulli distribution models a variable with success probability :
For example, could mean an email is spam and could mean it is not. A Bernoulli model specifies the probability of each class for a single binary outcome.
Continuous distributions
A probability density function describes a continuous random variable. Probabilities are obtained by integrating the density over an interval:
The total area under a valid density equals . The height is not itself the probability of exactly ; for a continuous variable, the probability at any single exact value is zero.
The normal distribution is a common continuous model with a symmetric, bell-shaped density. It is characterized by a mean and variance :
Normal distributions arise in statistical modeling and in some AI algorithms, but not every real dataset or model error follows a normal distribution. Distributional assumptions should be checked rather than applied automatically.
Expected value and variance
The expected value describes the probability-weighted mean of a random variable. For a discrete variable:
The variance measures how spread out values are around their expected value:
For a Bernoulli variable with success probability :
These quantities connect probability to Descriptive and Inferential Statistics, which explains how distributions can be estimated and evaluated using sampled data.
How Bayes’ theorem appears in AI
Bayesian reasoning can be used whenever predictions combine uncertain evidence with information about which outcomes are plausible before that evidence is seen.
Naive Bayes classification
A Naive Bayes classifier applies Bayes’ theorem to predict a class from features . Under the assumption that the features are conditionally independent given the class:
The symbol means proportional to. To obtain normalized probabilities across classes, the class scores must be divided by their sum (assuming the relevant evidence has positive probability).
For an email classifier, features might represent whether certain words occur. The model combines a prior for each class with feature likelihoods to select a class. This assumption makes the calculation efficient but can be inaccurate when features are strongly dependent, and the resulting class probabilities are not automatically well-calibrated.
A separate PlainCS article, Naive Bayes and Probabilistic Classification, develops the algorithm and its variants in more detail.
Other applications
| AI application | Role of probability or Bayesian reasoning |
|---|---|
| Spam and document classification | Comparing how likely classes are given observed features |
| Speech recognition | Ranking possible transcriptions from uncertain audio evidence |
| Sensor fusion | Combining noisy sensor observations with existing estimates |
| Fault detection | Updating the probability of a malfunction after receiving warning signals |
| Bayesian machine learning | Expressing uncertainty about parameters and predictions |
| Decision-making under uncertainty | Evaluating expected consequences of alternative actions |
Not every AI model is a Bayesian model. Many systems use probabilistic outputs without explicitly representing parameter uncertainty or performing Bayesian updates.
Important distinctions and common mistakes
Probability is not certainty
A prediction of does not mean an event must occur. It expresses a degree of uncertainty under the model. Whether such predictions are calibrated must be checked empirically: for example, among cases assigned probabilities near , the event should occur roughly of the time if the model is well-calibrated and the test data represent the intended setting.
Conditional probabilities cannot be reversed
is the rate at which spam is flagged. is the probability that a flagged message is spam. Bayes’ theorem connects them, but they are not interchangeable.
Independence is an assumption to justify
Multiplying probabilities of features without accounting for dependence can produce misleading results. The simplified Naive Bayes formula requires conditional independence given the class, not merely convenience.
A density is not a point probability
For continuous variables, probabilities refer to ranges. A density may be greater than at some points while its total integrated area remains .
Estimated probabilities may change
Real-world class frequencies and feature patterns change over time. A prior learned from one population may not describe another. Data drift and incorrect modeling assumptions can weaken predictions, so models should be validated and monitored after deployment.
Practice problems
- A fair die is rolled. What is the probability of obtaining a number greater than ? What is the probability of not obtaining a number greater than ?
- In the student table above, calculate and explain why it differs from .
- Suppose a classifier flags of rare positive cases, falsely flags of negative cases, and the positive class has a prevalence of . Calculate .
- Why does the formula for Naive Bayes use conditional independence rather than independence of all features in the overall population?
Show worked answers
- Outcomes greater than are and , so . Its complement is .
- , while . The conditions select different reference groups.
- , or about .
- Features can be associated overall because they share a class label. The simplifying assumption is that, after conditioning on the class, one feature supplies no additional information about another.
Related concepts
- Vectors and Matrices explains the numerical representations used to store data and probabilities.
- Descriptive and Inferential Statistics covers sampling, estimation, and statistical inference.
- Naive Bayes and Probabilistic Classification applies Bayesian ideas to supervised classification.
- Entropy, Cross-Entropy, and KL Divergence develops mathematical measures involving probability distributions.
- Evaluating Machine Learning Models explains how probabilistic predictions and classification decisions are assessed.
- AI Knowledge Map connects these mathematical concepts to the broader AI curriculum.
References and further reading
- MIT OpenCourseWare: Introduction to Probability and Statistics (18.05) — introductory probability, conditional probability, Bayesian inference, and statistics.
- NIST/SEMATECH Engineering Statistics Handbook: What Is a Probability Distribution? — discrete and continuous distributions, probability functions, and densities.
- Scikit-learn User Guide: Naive Bayes — the probabilistic classification rule and conditional-independence assumption.
Related articles in
- AI Agents and Tool-Using Workflows — Learn agent goals, tool invocation, planning, state, permissions, and how to evaluate multi-step AI workflows.
- AI Development Tools: Git, Jupyter, and APIs — Learn how Git, Jupyter notebooks, Python environments, and APIs fit together in reproducible, secure artificial intelligence workflows.
- How AI Is Changing Theoretical Computer Science — AI is beginning to help researchers explore algorithms, mathematical structures, and formal proofs. Here is what it can do, what still needs human judgment, and why verification matters.