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

What You Will Learn

  • probabilty formula
  • conditional probabaility
  • Bayes Theorom

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%

Probability Scale

Diagram
Probability Meaning
0 Impossible
0.2 Unlikely
0.5 50–50 chance
0.8 Very likely
1 Guaranteed

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.

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.

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.