Lesson 04
Probability
Master probability formulas, conditional probability, and Bayes theorem used throughout machine learning and AI systems.
probabilty formula
Probability is one of the 3 core pillars of Machine Learning (Linear Algebra + Calculus + Probability).
Probability is the measure of how likely an event is to happen.
It always ranges from 0 to 1.
- 0 → Impossible
- 1 → Certain
- 0.5 → 50% chance
Intuition: A bag of balls
Imagine a bag with 5 balls.
Suppose:
- Red = 1
- Blue = 1
- Green = 1
- Yellow = 1
- Purple = 1
Question:
What is the probability of picking the red ball?
There is 1 favorable outcome out of 5 total outcomes.
Answer = 20%
Probability Formula
The fundamental formula is:
Where:
| Symbol | Meaning |
|---|---|
| P(E) | Probability of event E |
| Favorable | Outcomes you want |
| Total | All possible outcomes |
This is the most important probability formula.
Example 1: Dice
A standard dice has 6 faces.
Question:
Probability of getting 4?
≈ 0.167 or 16.7%
Example 2: Coin Toss
Possible outcomes:
- Heads
- Tails
Probability of Heads:
50%
Probability Scale
| Probability | Meaning |
|---|---|
| 0 | Impossible |
| 0.2 | Unlikely |
| 0.5 | 50–50 chance |
| 0.8 | Very likely |
| 1 | Guaranteed |
ML Use Case 1: Spam Detection
An email arrives.
AI predicts:
| Class | Probability |
|---|---|
| Spam | 0.92 |
| Not spam | 0.08 |
Interpretation:
- 92% chance it's spam
- 8% chance it's genuine
The model outputs probabilities, not certainty.
ML Use Case 2: House Price Classification
Your real estate AI predicts:
| Class | Probability |
|---|---|
| Budget | 0.10 |
| Premium | 0.75 |
| Luxury | 0.15 |
The highest probability is Premium (75%).
The prediction is based on probability.
ML Use Case 3: Face Recognition
A camera detects a face.
| Person | Probability |
|---|---|
| Alice | 0.95 |
| Bob | 0.03 |
| Charlie | 0.02 |
AI chooses Alice because she has the highest probability.
ML Use Case 4: ChatGPT
When ChatGPT predicts the next word, it assigns probabilities.
Example prompt:
"Machine learning is..."
| Word | Probability |
|---|---|
| powerful | 0.41 |
| useful | 0.30 |
| amazing | 0.18 |
| difficult | 0.11 |
ChatGPT samples from this probability distribution to generate text.
Python Example
# Probability of drawing a red ball
favorable = 1
total = 5
probability = favorable / total
print(probability)Output:
0.2Key Terms
| Term | Meaning |
|---|---|
| Experiment | Tossing a coin |
| Outcome | Heads |
| Event | Getting Heads |
| Sample Space | All possible outcomes |
| Probability | Likelihood of the event |
Sample Space Example
Dice:
Coin:
Summary
| Concept | Formula |
|---|---|
| Probability | Favorable / Total |
| Coin | 1/2 |
| Dice | 1/6 |
| Range | 0 ≤ P ≤ 1 |
Memory Trick
Probability = Number you want ÷ Total possible outcomes
Interview Question
Q: Why is probability important in Machine Learning?
Answer: ML models rarely output absolute certainty. Instead, they estimate the likelihood of different outcomes—such as spam vs. not spam, disease vs. healthy, or the next word in a sentence—using probabilities between 0 and 1.
conditional probabaility
Conditional probability is one of the most important probability concepts in AI and Machine Learning. It is used in spam filters, medical diagnosis, recommendation systems, and Bayesian models.
Conditional Probability = The probability of Event A happening, given that Event B has already happened.
The keyword is "given".
Real-life intuition: Property buyers
Suppose your real estate platform has 100 users.
- 40 are Premium Members
- 25 are Premium Members who booked a site visit
The question is not:
What's the probability a random user booked a visit?
Instead, it's:
Given the user is Premium, what's the probability they booked a visit?
That is conditional probability.
The formula
Example with sample values:
Where:
| Symbol | Meaning |
|---|---|
| P(A | B) |
| P(A ∩ B) | Both A and B happen |
| P(B) | Event B has happened |
A ∩ B is the part of B where A also happens.
Read it as:
Probability of A, given B
Example 1: Premium users
Given:
| Group | Users |
|---|---|
| Total users | 100 |
| Premium users | 40 |
| Premium + Site Visit | 25 |
We want:
Step 1:
Favorable = 25
Step 2:
Given Premium = 40
Answer = 62.5%
Meaning:
If a user is Premium, there is a 62.5% chance they book a site visit.
Why do we divide by Premium only?
This is the biggest confusion.
Imagine 100 students.
Only 20 wear glasses.
Question:
Given the student wears glasses, what's the chance they are left-handed?
You ignore the other 80 students.
Your world becomes only the people with glasses.
Conditional probability changes the sample space.
Original sample space: 100 students
New sample space: 20 students
That's exactly what "given" means.
Example 2: Dice
Roll one dice.
Question:
Given the number is even, what's the probability it's 4?
Sample space becomes:
Only 3 outcomes remain.
Favorable:
Answer = 33.3%
Notice we did not divide by 6.
We divided by 3.
Example 3: Rain
Suppose:
| Event | Probability |
|---|---|
| Rain | 0.30 |
| Carry Umbrella | 0.20 |
| Rain & Umbrella | 0.18 |
Question:
Probability of carrying an umbrella given it rains?
Answer = 60%
ML Use Case 1: Spam Detection
AI predicts spam using conditional probability.
Email contains the word:
"FREE"
Historical data:
| Condition | Count |
|---|---|
| Emails with FREE | 500 |
| Spam + FREE | 450 |
Question:
What's the probability an email is spam given it contains FREE?
90%
This is the foundation of the Naive Bayes classifier.
ML Use Case 2: Medical Diagnosis
A patient tested positive.
Data:
| Condition | Count |
|---|---|
| Positive tests | 100 |
| Actually sick + Positive | 80 |
Question:
Probability patient is sick given positive test?
80%
Notice this is different from overall disease probability.
ML Use Case 3: Recommendation System
Your property app.
A user searched:
- 3 BHK
- Whitefield
- Under ₹1 Cr
Historical data:
| Event | Users |
|---|---|
| Same search | 200 |
| Same search + Contacted Builder | 120 |
Conditional probability:
60%
The platform can predict purchase intent.
ML Use Case 4: ChatGPT
When generating text, GPT doesn't ask:
What's the probability of the word "learning"?
It asks:
Given the previous words, what's the probability of the next word?
Example:
Prompt:
Machine ______
| Next Word | Probability |
|---|---|
| Learning | 0.70 |
| Vision | 0.12 |
| Translation | 0.08 |
| Robot | 0.10 |
This is conditional probability:
The previous words become the condition.
Python Example
# Premium users example
premium = 40
visit_and_premium = 25
p = visit_and_premium / premium
print(p)Output:
0.625Normal vs Conditional Probability
| Normal Probability | Conditional Probability |
|---|---|
| All outcomes | Given a condition |
| Divide by total | Divide by the given event |
| P(A) | P(A |
| Random user | Premium user only |
Summary
| Concept | Formula |
|---|---|
| Probability | Favorable / Total |
| Conditional Probability | P(A ∩ B) / P(B) |
| Symbol | P(A |
| Meaning | Probability of A given B |
Memory Trick
Probability asks: "How likely is A?"
Conditional Probability asks: "How likely is A, given B has already happened?"
Interview Question
Q: Why is conditional probability important in Machine Learning?
Answer: Most ML predictions depend on existing information. Spam filters predict spam given the email content, recommendation systems predict user actions given browsing history, and language models predict the next word given previous words. Conditional probability models these "given" relationships.
Bayes Theorom
Bayes' Theorem is one of the most important mathematical foundations of AI. It is used in spam detection, medical diagnosis, search ranking, recommendation systems, and Bayesian Machine Learning.
Bayes' Theorem updates an old probability using new evidence.
Think of it as:
Old belief + New evidence = Updated belief
Real-life intuition: Medical test
Imagine a hospital.
A patient gets a positive test result.
The question is not:
Is the test accurate?
The real question is:
Given the test is positive, what is the probability the patient actually has the disease?
That is exactly what Bayes' Theorem answers.
Bayes' Theorem formula
Read it as:
Probability of A given B = (Probability of B given A × Probability of A) ÷ Probability of B
| Symbol | Meaning |
|---|---|
| P(A | B) |
| P(B | A) |
| P(A) | Prior probability |
| P(B) | Evidence |
Understanding each term (spoon-fed)
Imagine 10,000 people.
Suppose:
- 200 people have the disease
- Test detects 170 correctly
- Total positive tests = 500
Prior Probability
Before seeing the test:
2%
This is your initial belief.
Likelihood
Among sick people:
170 test positive.
85%
This measures how good the test is.
Evidence
Overall positive tests:
500
5%
Solve with Bayes' Theorem
We want:
Apply Bayes:
Step-by-step:
Answer = 34%
Important insight
Even with an 85% accurate test, a positive result does not mean an 85% chance of disease.
It is only 34% because the disease is rare.
This is why Bayes' Theorem is so powerful.
Visual intuition
Think of probabilities flowing through evidence.
Bayes updates the belief after seeing evidence.
ML Use Case 1: Spam Detection
An email contains:
WIN FREE MONEY
Historical data:
| Event | Value |
|---|---|
| Spam emails | 20% |
| FREE appears in spam | 90% |
| FREE appears overall | 30% |
Bayes computes:
Meaning:
Given the word FREE appears, what's the probability the email is spam?
This is the core idea behind the Naive Bayes classifier.
ML Use Case 2: Medical AI
An AI predicts cancer.
Input:
- MRI
- Blood report
- Age
Output:
The AI continuously updates probability as new evidence arrives.
ML Use Case 3: Recommendation System
Your property app.
A user:
- Searches Whitefield
- Budget ₹80L
- 3 BHK
AI asks:
Given these behaviors, what's the probability they'll book a visit?
This helps prioritize leads.
ML Use Case 4: ChatGPT
Suppose the prompt is:
Machine
Possible next words:
| Word | Prior |
|---|---|
| Learning | 0.30 |
| Vision | 0.20 |
| Robot | 0.10 |
Now the next context is:
Machine Deep
Bayes-style updating increases the probability of Learning while decreasing others.
Modern transformers don't explicitly implement Bayes' formula, but they perform the same high-level task: updating probabilities using context.
Bayes vs Conditional Probability
| Conditional Probability | Bayes' Theorem |
|---|---|
| Finds P(A | B) |
| Direct relationship | Updates belief with evidence |
| Uses existing data | Combines prior + likelihood |
| Example: Visit given Premium | Spam given FREE |
Think of it like this:
- Conditional Probability asks a question.
- Bayes' Theorem gives a systematic way to answer it.
Python Example
# Bayes Theorem
prior = 0.02
likelihood = 0.85
evidence = 0.05
posterior = (likelihood * prior) / evidence
print(posterior)Output:
0.34The updated probability is 34%.
Summary Table
| Term | Meaning |
|---|---|
| Prior | Initial belief |
| Likelihood | Probability of evidence if A is true |
| Evidence | Overall probability of evidence |
| Posterior | Updated belief after evidence |
Memory Trick
Prior → What I believed before.
Evidence → New information arrives.
Posterior → What I believe now.
Interview Question
Q: Why is Bayes' Theorem important in Machine Learning?
Answer: Bayes' Theorem allows ML systems to update predictions as new evidence becomes available. It is fundamental to Naive Bayes classifiers, probabilistic reasoning, medical diagnosis, spam filtering, recommendation systems, and uncertainty-aware AI models.