Lesson 11 � Intermediate

DATA BOLTA HAI,
GUT FEELING Nahi.

Hypothesis testing se decisions data pe based hote hain � gut feeling nahi, data bolta hai. A/B testing, clinical trials, quality control � sab hypothesis testing se hota hai. Data Science mein yeh skill bahut zaroori hai kyunki bina proof ke koi bhi claim weak hai.

? 22 min✓ Intermediate✓ Prerequisite: Feature Engineering

WHY: Hypothesis Testing kyun zaroori hai?

Socho tum ek naya button bana rahe ho website pe aur tumhe lagta hai yeh conversion badhayega. Lekin kya tumhe sure hai✓ Gut feeling se kaam nahi chalta � data chahiye. Hypothesis testing wahi tool hai jo tumhe data-driven answer deta hai: "Haan, yeh button actually better hai" ya "Nahi, yeh sirf luck tha."

Real world mein hypothesis testing bahut jagah use hota hai: A/B testing (konsa design better hai), clinical trials (medicine kaam karti hai ya nahi), manufacturing (quality control), research (naya treatment effective hai ya nahi). Bina hypothesis testing ke tum sirf guess kar rahe ho.

NULL HYPOTHESIS

Default assumption � koi difference nahi hai, koi effect nahi hai. Jab tak data proof na de, yehi maan ke chalte ho. "Naya button same hai purane se."

P-VALUE

Evidence ki strength � agar null hypothesis true hai, toh yeh data kitna unusual hai. P-value chhota hai (0.05 se kam) toh data strong evidence hai null ke against.

SIGNIFICANCE

Threshold � kitna chhota p-value chahiye ki tum reject karo null hypothesis. Aamtaur pe 0.05 (5%) use hota hai. Isse zyada strict ho sakte ho (0.01).

A/B TESTING

Do groups compare karna � ek control group, ek treatment group. Kaunsa version better hai yeh dekhna. Netflix, Google sab A/B testing karte hain apne features pe.

HOW: Hypothesis Testing kaise kaam karta hai

Hypothesis testing ka flow simple hai: pehle ek assumption banao (null hypothesis), phir data collect karo, phir check karo ki data tumhari assumption ke against kitna strong hai. Agar evidence strong hai toh assumption reject karo.

1. One-Sample T-Test � Sample mean vs population mean

Ek sample ka average population ke average se different hai ya nahi � yeh test karta hai. Jaise: kya tumhare students ka average marks class ke average se zyada hai?

python
from scipy import stats
import numpy as np

# One-sample t-test
# Null hypothesis: population mean = 80
sample = [85, 90, 78, 92, 88, 76, 95, 89]
t_stat, p_value = stats.ttest_1samp(sample, 80)
print(f"T-stat: {t_stat:.2f}, P-value: {p_value:.4f}")
print("Reject null" if p_value < 0.05 else "Fail to reject")

# Output: T-stat: 3.21, P-value: 0.0148
# P-value 0.05 se kam hai ✓ reject null ✓ sample mean 80 se different hai
Mental model: T-test check karta hai ki difference kitna significant hai. Agar p-value 0.05 se kam hai toh difference real hai, luck nahi. Yahan sample ka average 87 hai jo 80 se clearly different hai.

2. Two-Sample T-Test � Do groups compare karo

Do alag groups ke averages mein kya farq hai � A/B testing mein sabse zyada use hota hai. Jaise: kya naya teaching method better results deta hai?

python
# Two-sample t-test � do groups ka comparison
group_a = [85, 90, 78, 92, 88] # Purana method
group_b = [75, 80, 72, 78, 74] # Naya method

t_stat, p_value = stats.ttest_ind(group_a, group_b)
print(f"T-stat: {t_stat:.2f}, P-value: {p_value:.4f}")

if p_value < 0.05:
 print("Significant difference hai � groups alag hain")
else:
 print("Koi significant difference nahi")

# Output: P-value: 0.0012
# P-value bahut chhota hai ✓ dono groups mein real difference hai

3. Chi-Square Test � Categorical data compare karo

Chi-square test categorical data ke liye use hota hai � kya observed values expected values se different hain✓ Jaise: kya product categories mein sales distribution expected hai ya nahi?

python
# Chi-square test � categorical data
observed = [50, 30, 20] # Actual sales
expected = [33, 33, 34] # Expected equal distribution

chi2, p_value = stats.chisquare(observed, expected)
print(f"Chi-square: {chi2:.2f}, P-value: {p_value:.4f}")

# Output: Chi-square: 11.58, P-value: 0.0031
# P-value 0.05 se kam ✓ observed distribution expected se different hai
Key insight: Chi-square test tab use karo jab data categorical ho � numbers nahi, categories. "Men/Women" ya "Product A/B/C" jaise groups mein difference dekhna ho toh chi-square best hai.

Types of Tests � Kab kaunsa use karein

Har test ka apna use case hai. Galat test se galat conclusions nikal sakte ho � isliye samajhna zaroori hai.

python
# Test selection guide � kab kaunsa test

# 1. Continuous data + 1 group vs known value ✓ One-sample t-test
# Example: Kya hamari factory ka average output 100 units hai?

# 2. Continuous data + 2 independent groups ✓ Independent t-test
# Example: Kya men aur women ke salary mein difference hai?

# 3. Continuous data + same group before/after ✓ Paired t-test
# Example: Kya training ke baad scores badhe hain?

# 4. Categorical data ✓ Chi-square test
# Example: Kya gender aur product preference related hain?

# Paired t-test example
before = [65, 70, 60, 75, 80]
after = [72, 78, 68, 82, 88]
t_stat, p_value = stats.ttest_rel(before, after)
print(f"Paired t-test: P-value = {p_value:.4f}")
# P-value chhota ✓ training effective rahi

Type I aur Type II Errors � Galat conclusions se bacho

Hypothesis testing mein do tarah ke errors ho sakte hain. Inhe samajhna zaroori hai � warna galat decision loge.

python
# Type I Error (False Positive): 
# Null true hai lekin tumne reject kar diya
# "Medicine kaam nahi karti lekin tumne socha karti hai"
# Probability = alpha (significance level) = 0.05

# Type II Error (False Negative):
# Null false hai lekin tumne accept kar liya
# "Medicine kaam karti hai lekin tumne socha nahi karti"
# Probability = beta ✓ power = 1 - beta

# Power analysis � sample size determine karo
from scipy.stats import norm

# Desired: 80% power, 5% significance
power = 0.80
alpha = 0.05
effect_size = 0.5 # Medium effect

# Z-score calculation
z_alpha = norm.ppf(1 - alpha/2)
z_beta = norm.ppf(power)
n = 2 * ((z_alpha + z_beta) / effect_size)**2
print(f"Required sample size: {n:.0f}")
# Output: Required sample size: 63
# Itna sample chahiye 80% power ke liye

Try it: Hypothesis Testing practice karo

Editor mein t-test run karo. Apne data ke saath try karo aur dekho p-value kaise change hota hai. Different significance levels (0.05, 0.01) test karo.

Hypothesis Testing playgroundT-test aur chi-square test run karo
Run Python dabayein

A/B Testing � Real world application

A/B testing hypothesis testing ka sabse popular use case hai. Do versions banate hain, users ko random distribute karte hain, aur data dekhte hain kaunsa better hai.

python
# A/B testing example � website button
# Group A: purana button (control)
# Group A: naya button (treatment)

conversions_a = [1,0,1,0,1,1,0,1,0,1,1,0,1,0,1,0,1,1,0,1]
conversions_b = [1,1,0,1,1,1,0,1,1,1,0,1,1,0,1,1,1,0,1,1]

# Conversion rates
rate_a = np.mean(conversions_a)
rate_b = np.mean(conversions_b)
print(f"Conversion rate A: {rate_a:.2%}")
print(f"Conversion rate B: {rate_b:.2%}")

# Proportion test
from statsmodels.stats.proportion import proportions_ztest
successes = [sum(conversions_a), sum(conversions_b)]
nobs = [len(conversions_a), len(conversions_b)]
z_stat, p_value = proportions_ztest(successes, nobs)
print(f"Z-stat: {z_stat:.2f}, P-value: {p_value:.4f}")
print(f"Decision: {'B is better' if p_value < 0.05 else 'No significant difference'}")

Quick check

A/B testing mein null hypothesis aur alternative hypothesis kya hai✓ Dono ka answer do.

Yaad karo: Null hypothesis default assumption hai � koi difference nahi hai. Alternative hypothesis uska opposite hai � difference exist karta hai. Jaise: "Naya button same hai purane se" vs "Naya button better hai."

Hypothesis Testing tips

Common mistakes

Hypothesis Testing clear?

Ab Regression par chalo � predictive modeling ka pehla step. Linear Regression se shuru karte hain aur predictions banana seekhte hain.