Probability for Data Science
Statistics and probability help data scientists measure uncertainty, analyse patterns, and evaluate predictions. These concepts support fraud detection, customer behaviour analysis, machine learning, and business experimentation.
This module covers four lessons: probability fundamentals, probability distributions, statistical inference, and A/B testing.
Lesson 1 Probability Fundamentals and Conditional Probability
Probability measures the likelihood of an event occurring. In data science, it helps estimate outcomes such as whether a customer will purchase a product or a transaction will be fraudulent.
Probability ranges from 0 to 1, where 0 represents an impossible event and 1 represents a certain event under the specified model.
\[ 0\leq P(A)\leq1 \]
For example, a churn probability of 0.75 means a model estimates a 75% likelihood of cancellation within the defined prediction period. It does not guarantee cancellation.
What Are the Basic Concepts of Probability?
| Concept | Meaning | Example |
|---|---|---|
| Random Experiment | Process with uncertain outcomes | Customer makes a purchase |
| Sample Space | All possible outcomes | Purchase, no purchase |
| Event | A specified outcome | Purchase completed |
| Probability | Likelihood of an event | 0.65 |
| Independent Events | One does not change the probability of another | Independent coin tosses |
| Conditional Probability | Probability given another event | Purchase given discount |
| Complement | Event does not occur | No purchase |
How Is Probability Calculated?
For equally likely outcomes:
\[ P(A)=\frac{\text{Favourable Outcomes}}{\text{Total Outcomes}} \]
A fair die has three even-numbered outcomes out of six.
\[ P(Even)=\frac{3}{6}=0.5 \]
Empirical Probability
Empirical probability uses observed frequencies.
Suppose 2,500 of 10,000 website sessions result in purchases.
\[ P(Purchase)=\frac{2500}{10000}=0.25 \]
total_sessions = 10000purchases = 2500probability = purchases / total_sessionsprint(probability)Output: 0.25
The observed conversion probability is 25%. Its usefulness for future predictions depends on how representative the data is.
What Are the Main Probability Rules?
Complement Rule
The probability that an event does not occur is:
\[ P(A^c)=1-P(A) \]
If purchase probability is 0.25:
\[ P(No\ Purchase)=1-0.25=0.75 \]
Addition Rule
The probability that A or B occurs is:
\[ P(A\cup B)=P(A)+P(B)-P(A\cap B) \]
Suppose 40% of customers purchase electronics, 30% purchase clothing, and 10% purchase both.
\[ P(A\cup B)=0.40+0.30-0.10=0.60 \]
Therefore, 60% purchase at least one of the two categories.
Multiplication Rule
\[ P(A\cap B)=P(A)P(B|A) \]
For independent events:
\[ P(A\cap B)=P(A)P(B) \]
Independence must be established rather than assumed.
What Is Conditional Probability?
Conditional probability measures the likelihood of an event given another event.
\[ P(A|B)=\frac{P(A\cap B)}{P(B)} \]
The formula requires \(P(B)>0\).
Suppose 300 customers receive a discount and 180 complete purchases.
\[ P(Purchase|Discount)=\frac{180}{300}=0.60 \]
The purchase probability among discount recipients is 60%.
Python Example
import pandas as pddf = pd.DataFrame({ "discount": [True, True, False, True, False], "purchased": [True, True, False, False, True]})discount_group = df[df["discount"]]probability = discount_group["purchased"].mean()print(probability)Output: 0.6666666666666666
Two of the three discount recipients purchased. This is an observed association, not proof that the discount caused their purchases.
What Is Bayes' Theorem?
Bayes' theorem updates the probability of a hypothesis using new evidence.
\[ P(A|B)=\frac{P(B|A)P(A)}{P(B)} \]
It combines:
- Prior probability: Initial likelihood of an event.
- Likelihood: Probability of the evidence under the hypothesis.
- Posterior probability: Updated probability after observing evidence.
Fraud Detection Example
Suppose:
- 1% of transactions are fraudulent.
- A detection system flags 90% of fraudulent transactions.
- It incorrectly flags 5% of legitimate transactions.
The overall flag probability is:
\[ P(Flag)=0.90(0.01)+0.05(0.99)=0.0585 \]
The probability of fraud given a flag is:
\[ P(Fraud|Flag)=\frac{0.90(0.01)}{0.0585} \]
\[ P(Fraud|Flag)\approx0.1538 \]
Only approximately 15.38% of flagged transactions are fraudulent under these assumptions.
fraud_rate = 0.01sensitivity = 0.90false_positive_rate = 0.05flag_probability = ( sensitivity * fraud_rate + false_positive_rate * (1 - fraud_rate))posterior = sensitivity * fraud_rate / flag_probabilityprint(f"{posterior:.2%}")Output: 15.38%
This demonstrates why base rates matter when interpreting predictions about rare events.
Where Is Probability Used in Data Science?
| Application | Purpose |
|---|---|
| Fraud Detection | Estimate transaction risk |
| Customer Churn | Predict cancellation probability |
| Spam Filtering | Classify suspicious messages |
| Healthcare | Estimate clinical risks |
| Recommendation Systems | Model user preferences |
| Forecasting | Quantify uncertainty in future outcomes |
Common mistakes: Confusing conditional probabilities, assuming independence, ignoring base rates, interpreting association as causation, and treating predicted probabilities as certain outcomes.
Data Science Tutorial for Beginners
Vaishali Ardhana








