> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Bayes Theorem and Statistical Inference

> Explains Bayes' theorem and how to update probabilities with examples like spam detection and DJ identification, highlighting applications in data science and decision making under uncertainty.

Welcome — it's Justyna from KodeKloud.

In this lesson we’ll see how Bayes’ theorem turns new evidence into better decisions. We’ll define the theorem, explain how it updates probabilities, and walk through intuitive examples — from recognizing DJs at an expo to how your inbox detects spam.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/1ndeLDstm3GBsOYi/images/Mathematics-for-Computing/Probability-and-Statistics/Bayes-Theorem-and-Statistical-Inference/woman-presentation-bayes-theorem-cartoon-wolf.jpg?fit=max&auto=format&n=1ndeLDstm3GBsOYi&q=85&s=49bd010616810064df6b668251292631" alt="The image features a woman speaking in front of a presentation slide with a cartoon wolf. The slide outlines three points about Bayes' Theorem, including its definition, real-world applications, and connection to machine learning." width="1920" height="1080" data-path="images/Mathematics-for-Computing/Probability-and-Statistics/Bayes-Theorem-and-Statistical-Inference/woman-presentation-bayes-theorem-cartoon-wolf.jpg" />
</Frame>

Probability in data science is not guessing — it’s updating. Every new observation refines our belief, like adding pieces to a puzzle.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/1ndeLDstm3GBsOYi/images/Mathematics-for-Computing/Probability-and-Statistics/Bayes-Theorem-and-Statistical-Inference/bayes-theorem-data-science-illustration.jpg?fit=max&auto=format&n=1ndeLDstm3GBsOYi&q=85&s=cbe643ba441a157bc1960fc466bd54f4" alt="The image discusses Bayes' Theorem, highlighting its application in data science, machine learning, and AI, with a comparison of guess accuracy illustrated through bar charts." width="1920" height="1080" data-path="images/Mathematics-for-Computing/Probability-and-Statistics/Bayes-Theorem-and-Statistical-Inference/bayes-theorem-data-science-illustration.jpg" />
</Frame>

Why Bayes matters: it gives a principled rule for combining prior knowledge with observed evidence. Common application areas include:

| Area                | How Bayes is used                                                        |
| ------------------- | ------------------------------------------------------------------------ |
| Weather forecasting | Update rain probability as sensors and satellite data arrive             |
| Security screening  | Combine initial risk scores with secondary scan evidence                 |
| Recommendations     | Update user preferences from clicks and browsing behavior                |
| Spam filtering      | Adjust spam probability based on message features (words, links, sender) |
| Healthcare          | Combine symptoms and tests to estimate disease risk                      |
| Autonomous vehicles | Merge uncertain sensor readings to estimate hazard probabilities         |

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/1ndeLDstm3GBsOYi/images/Mathematics-for-Computing/Probability-and-Statistics/Bayes-Theorem-and-Statistical-Inference/airport-security-screening-bar-graph.jpg?fit=max&auto=format&n=1ndeLDstm3GBsOYi&q=85&s=89d7d16a9c1f9cc3595e4ab9a830fb3f" alt="The image features a bar graph titled &#x22;Airport Security Screening,&#x22; showing &#x22;Low Risk&#x22; and &#x22;Higher Risk&#x22; categories with corresponding bar heights, alongside a woman standing on the right." width="1920" height="1080" data-path="images/Mathematics-for-Computing/Probability-and-Statistics/Bayes-Theorem-and-Statistical-Inference/airport-security-screening-bar-graph.jpg" />
</Frame>

A concrete illustration: spam filtering. Email classifiers extract features (words, links, sender, formatting), compute how likely those features are under “spam” versus “not spam,” and update the spam probability. If the updated (posterior) probability exceeds a threshold, the email is flagged.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/1ndeLDstm3GBsOYi/images/Mathematics-for-Computing/Probability-and-Statistics/Bayes-Theorem-and-Statistical-Inference/email-spam-detection-presentation-slide.jpg?fit=max&auto=format&n=1ndeLDstm3GBsOYi&q=85&s=0859c83f71f7dea88b1ad0cacef755c6" alt="The image shows a presentation slide with the title &#x22;How Does Your Email Detect a Spam?&#x22; and a woman standing next to it. The slide includes a mock email interface highlighting a spam email titled &#x22;Crypt Lottery.&#x22;" width="1920" height="1080" data-path="images/Mathematics-for-Computing/Probability-and-Statistics/Bayes-Theorem-and-Statistical-Inference/email-spam-detection-presentation-slide.jpg" />
</Frame>

This is often implemented as a simple decision pipeline: extract evidence → compute likelihoods → update posterior → compare with threshold → act.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/1ndeLDstm3GBsOYi/images/Mathematics-for-Computing/Probability-and-Statistics/Bayes-Theorem-and-Statistical-Inference/email-spam-flowchart-decision-process.jpg?fit=max&auto=format&n=1ndeLDstm3GBsOYi&q=85&s=4af576314e2699304ca7490a4939e0fe" alt="The image shows a flowchart explaining how systems decide if an email is spam, accompanied by a person speaking. The flowchart categorizes emails into &#x22;High Probability&#x22; or &#x22;Low Probability&#x22; of being spam, leading to actions like &#x22;Mark as Spam&#x22; or &#x22;Deliver to Inbox.&#x22;" width="1920" height="1080" data-path="images/Mathematics-for-Computing/Probability-and-Statistics/Bayes-Theorem-and-Statistical-Inference/email-spam-flowchart-decision-process.jpg" />
</Frame>

The core formula — simple, expressive, and practical:

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/1ndeLDstm3GBsOYi/images/Mathematics-for-Computing/Probability-and-Statistics/Bayes-Theorem-and-Statistical-Inference/bayes-theorem-formula-explanation-cartoon.jpg?fit=max&auto=format&n=1ndeLDstm3GBsOYi&q=85&s=67d023ceb56fc6af822ecc7bdcc0294c" alt="The image presents Bayes' Theorem with its formula displayed, featuring a person explaining the concept and a cartoon character to the side." width="1920" height="1080" data-path="images/Mathematics-for-Computing/Probability-and-Statistics/Bayes-Theorem-and-Statistical-Inference/bayes-theorem-formula-explanation-cartoon.jpg" />
</Frame>

P(A | B) = P(B | A) × P(A) / P(B)

* P(A) — prior: what you believed about A before seeing B.
* P(B | A) — likelihood: how probable B is when A is true.
* P(B) — marginal: how common B is overall.
* P(A | B) — posterior: updated belief in A after observing B.

<Callout icon="lightbulb" color="#1CB2FE">
  Bayes' theorem is a procedure: start with a prior, measure how likely the evidence is under each hypothesis, and update to get the posterior. This mindset is key for principled decision-making under uncertainty.
</Callout>

Example 1 — DJ vs Developer (intuitive grid)
You meet someone at a tech + music expo who looks "DJ-like" (shaved head, music gear). How likely are they actually a DJ?

Assumptions:

* 90% of attendees are developers, 10% are DJs.
* 70% of DJs behave in a way you’d call “DJ-like.”
* 10% of developers also behave “DJ-like.”

Visualize 100 people:

* DJs: 10 total → 70% of those act like DJs → 7 people.
* Developers: 90 total → 10% act like DJs → 9 people.
* Total acting like DJs: 7 + 9 = 16.

So the probability someone who looks like a DJ actually is a DJ = 7 / 16 ≈ 0.4375 (≈ 44%).

Here’s the same computation expressed as Bayes’ theorem in Python-style code:

```python theme={null}
# DJ example
p_dj = 0.10
p_style_given_dj = 0.70
p_dev = 0.90
p_style_given_dev = 0.10

p_style = p_style_given_dj * p_dj + p_style_given_dev * p_dev
p_dj_given_style = (p_style_given_dj * p_dj) / p_style

p_style, p_dj_given_style  # (0.16, 0.4375)
```

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/1ndeLDstm3GBsOYi/images/Mathematics-for-Computing/Probability-and-Statistics/Bayes-Theorem-and-Statistical-Inference/woman-gesturing-pie-chart-developers-djs.jpg?fit=max&auto=format&n=1ndeLDstm3GBsOYi&q=85&s=1d07afc775f78dd19d6afc1d1a7fbcc1" alt="The image shows a woman gesturing beside a pie chart, which indicates that 90% are developers and 10% are DJs." width="1920" height="1080" data-path="images/Mathematics-for-Computing/Probability-and-Statistics/Bayes-Theorem-and-Statistical-Inference/woman-gesturing-pie-chart-developers-djs.jpg" />
</Frame>

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/1ndeLDstm3GBsOYi/images/Mathematics-for-Computing/Probability-and-Statistics/Bayes-Theorem-and-Statistical-Inference/probabilities-roles-diagram-woman-explaining.jpg?fit=max&auto=format&n=1ndeLDstm3GBsOYi&q=85&s=d3886ce584d25589e0119f4dc19e6817" alt="The image shows a diagram with probabilities related to different roles and a woman standing beside it, explaining the content." width="1920" height="1080" data-path="images/Mathematics-for-Computing/Probability-and-Statistics/Bayes-Theorem-and-Statistical-Inference/probabilities-roles-diagram-woman-explaining.jpg" />
</Frame>

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/1ndeLDstm3GBsOYi/images/Mathematics-for-Computing/Probability-and-Statistics/Bayes-Theorem-and-Statistical-Inference/dj-developer-probabilities-bayes-theorem.jpg?fit=max&auto=format&n=1ndeLDstm3GBsOYi&q=85&s=f689fa421669890f3401bfb27d4bb8c1" alt="The image illustrates probabilities and conditional probabilities related to DJs and developers, possibly using Bayes' Theorem, with a character asking about finding the probability of someone acting like a DJ." width="1920" height="1080" data-path="images/Mathematics-for-Computing/Probability-and-Statistics/Bayes-Theorem-and-Statistical-Inference/dj-developer-probabilities-bayes-theorem.jpg" />
</Frame>

Both the 100-person grid and Bayes’ calculation give the same counterintuitive result: even strong-looking evidence can be outweighed by base rates (priors).

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/1ndeLDstm3GBsOYi/images/Mathematics-for-Computing/Probability-and-Statistics/Bayes-Theorem-and-Statistical-Inference/bayes-theorem-graphs-beliefs-evidence.jpg?fit=max&auto=format&n=1ndeLDstm3GBsOYi&q=85&s=8b00ba89f7986bf51b3e41a5bda1e55c" alt="The image illustrates Bayes' Theorem with graphs showing how beliefs update with new evidence, and features a person speaking next to the graphs." width="1920" height="1080" data-path="images/Mathematics-for-Computing/Probability-and-Statistics/Bayes-Theorem-and-Statistical-Inference/bayes-theorem-graphs-beliefs-evidence.jpg" />
</Frame>

Example 2 — Spam filtering with the word "lottery"
Suppose we scan 1,000 emails:

| Category | Count | Contains "lottery" |
| -------- | ----: | -----------------: |
| Spam     |   100 |                  8 |
| Not spam |   900 |                  2 |
| Total    |  1000 |                 10 |

From this:

* P(spam) = 100 / 1000 = 0.10
* P("lottery" | spam) = 8 / 100 = 0.08
* P("lottery") = (8 + 2) / 1000 = 0.01

Apply Bayes:

P(spam | "lottery") = P("lottery" | spam) × P(spam) / P("lottery")\
\= 0.08 × 0.10 / 0.01 = 0.8

So, given the observed frequencies, an email containing "lottery" has an 80% posterior probability of being spam.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/1ndeLDstm3GBsOYi/images/Mathematics-for-Computing/Probability-and-Statistics/Bayes-Theorem-and-Statistical-Inference/email-scanning-lottery-results-table.jpg?fit=max&auto=format&n=1ndeLDstm3GBsOYi&q=85&s=5eb4a48fa44658bb4cb55c74e3dd6dba" alt="The image shows the results of a model scanning 1,000 emails for the word &#x22;lottery,&#x22; with a table categorizing emails as spam or not spam. A person is presenting the information next to the table." width="1920" height="1080" data-path="images/Mathematics-for-Computing/Probability-and-Statistics/Bayes-Theorem-and-Statistical-Inference/email-scanning-lottery-results-table.jpg" />
</Frame>

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/1ndeLDstm3GBsOYi/images/Mathematics-for-Computing/Probability-and-Statistics/Bayes-Theorem-and-Statistical-Inference/bayes-theorem-spam-probability-presentation.jpg?fit=max&auto=format&n=1ndeLDstm3GBsOYi&q=85&s=ee174b5a71c0c9f36df21bc1e81c58ef" alt="The image shows a person speaking in front of a dark background with Bayes' Theorem and its application to a spam probability problem displayed onscreen." width="1920" height="1080" data-path="images/Mathematics-for-Computing/Probability-and-Statistics/Bayes-Theorem-and-Statistical-Inference/bayes-theorem-spam-probability-presentation.jpg" />
</Frame>

Decision rule
Spam filters then compare the posterior probability to a threshold (e.g., 70%). If the posterior exceeds the threshold, the message is marked as spam. This threshold encodes the operational trade-off between false positives and false negatives.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/1ndeLDstm3GBsOYi/images/Mathematics-for-Computing/Probability-and-Statistics/Bayes-Theorem-and-Statistical-Inference/email-spam-decision-rule-threshold.jpg?fit=max&auto=format&n=1ndeLDstm3GBsOYi&q=85&s=c93ac4eebd7a9b0a2a4eb0a39adc39d4" alt="The image illustrates a decision-making rule for marking emails as spam if the likelihood exceeds a 70% threshold, with a person standing beside the explanation." width="1920" height="1080" data-path="images/Mathematics-for-Computing/Probability-and-Statistics/Bayes-Theorem-and-Statistical-Inference/email-spam-decision-rule-threshold.jpg" />
</Frame>

Summary

* Bayes’ theorem converts priors and likelihoods into posteriors — a formal way to update beliefs.
* It’s fundamental in spam filtering, medical testing, recommender systems, sensor fusion, and many ML tasks.
* Always consider the prior (base rate): a single piece of evidence can be misleading without it.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/1ndeLDstm3GBsOYi/images/Mathematics-for-Computing/Probability-and-Statistics/Bayes-Theorem-and-Statistical-Inference/woman-kodekloud-shirt-chance-dj-chart.jpg?fit=max&auto=format&n=1ndeLDstm3GBsOYi&q=85&s=c14315a95d2cd1933a82c2df2d007e05" alt="The image shows a woman in a KodeKloud T-shirt standing next to a chart labeled &#x22;Chance of being a DJ,&#x22; with the text indicating that only 10% are DJs and 10% of developers act like DJs." width="1920" height="1080" data-path="images/Mathematics-for-Computing/Probability-and-Statistics/Bayes-Theorem-and-Statistical-Inference/woman-kodekloud-shirt-chance-dj-chart.jpg" />
</Frame>

Bayes’ theorem is more than a formula — it’s a disciplined way to combine past knowledge and fresh observations to make better decisions under uncertainty.

Links and references

* [Bayes' theorem — Wikipedia](https://en.wikipedia.org/wiki/Bayes%27_theorem)
* [Naive Bayes classifiers — scikit-learn](https://scikit-learn.org/stable/modules/naive_bayes.html)
* Bishop, C. M. “Pattern Recognition and Machine Learning” — chapter on probabilistic models and inference

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/mathematics-for-computing/module/7badce97-9acb-48b8-9fb6-bd5ce7e09045/lesson/0f8cd34b-333f-408a-b9a6-9c5219a44fea" />
</CardGroup>
