Skip to main content
MahiDev — Learn. Build. Become AI-ready.

Lesson 04

Probability

Master probability formulas, conditional probability, and Bayes theorem used throughout machine learning and AI systems.

Beginner30 min

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.

Diagram

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.

Equation P(Red) = (1)/(5) = 0.2

Answer = 20%

Probability Formula

The fundamental formula is:

Equation P(E) = (Favorable Outcomes)/(Total Outcomes)

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.

Diagram

Question:

Probability of getting 4?

Equation P(4) = (1)/(6)

≈ 0.167 or 16.7%

Example 2: Coin Toss

Diagram

Possible outcomes:

  • Heads
  • Tails

Probability of Heads:

Equation P(H) = (1)/(2) = 0.5

50%

Probability Scale

Diagram
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

python
# Probability of drawing a red ball

favorable = 1
total = 5

probability = favorable / total

print(probability)

Output:

text
0.2

Key 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:

Equation S = {1, 2, 3, 4, 5, 6}

Coin:

Equation S = {H, T}

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

Equation conditional probability P(A|B)

Example with sample values:

Equation P(A|B) ≈ 0.46

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

Diagram

Given:

Group Users
Total users 100
Premium users 40
Premium + Site Visit 25

We want:

Equation P(Visit given Premium)

Step 1:

Favorable = 25

Step 2:

Given Premium = 40

Equation P(Visit|Premium) = 25/40 = 0.625

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.

Diagram

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:

Equation {2, 4, 6}

Only 3 outcomes remain.

Favorable:

Equation {4}
Equation P(4|Even) = 1/3

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?

Equation (0.18)/(0.30) = 0.60

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?

Equation (450)/(500) = 0.9

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?

Equation (80)/(100) = 0.8

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:

Equation (120)/(200) = 0.6

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:

Equation P(Learning given Machine)

The previous words become the condition.

Python Example

python
# Premium users example

premium = 40
visit_and_premium = 25

p = visit_and_premium / premium

print(p)

Output:

text
0.625

Normal 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

Equation Bayes theorem P(A|B)

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.

Diagram

Suppose:

  • 200 people have the disease
  • Test detects 170 correctly
  • Total positive tests = 500

Prior Probability

Before seeing the test:

Equation P(Disease) = (200)/(10000) = 0.02

2%

This is your initial belief.

Likelihood

Among sick people:

170 test positive.

Equation P(Positive|Disease) = 0.85

85%

This measures how good the test is.

Evidence

Overall positive tests:

500

Equation P(Positive) = (500)/(10000) = 0.05

5%

Solve with Bayes' Theorem

We want:

Equation P(Disease given Positive)

Apply Bayes:

Equation (0.85 × 0.02)/(0.05)

Step-by-step:

Equation 0.017 ÷ 0.05 = 0.34

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.

Diagram

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:

Equation P(Spam given FREE)

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:

Equation P(Cancer given evidence)

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?

Equation P(Book given search signals)

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

python
# Bayes Theorem

prior = 0.02
likelihood = 0.85
evidence = 0.05

posterior = (likelihood * prior) / evidence

print(posterior)

Output:

text
0.34

The 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.