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.
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.
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."
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.
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).
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?
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 hai2. 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?
# 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 hai3. 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?
# 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 haiTypes 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.
# 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 rahiType 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.
# 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 liyeTry 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.
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.
# 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
- Sample size matter karta hai: Bahut chhote sample se reliable results nahi aate. Power analysis karke minimum sample size pata karo.
- P-value ko sahi samjho: P-value "null hypothesis probability" nahi hai. Yeh data ki strength hai null ke against.
- Practical significance dekho: Statistical significance zaroori hai lekin practical significance bhi dekho. 1% improvement statistically significant ho sakta hai lekin practically useless.
- Multiple testing correction: Bahut saare tests kar rahe ho toh Bonferroni correction lagao � warna false positives badh jayenge.
Common mistakes
- Outcome pehle decide mat karo: Hypothesis pehle banao, phir data collect karo. Data dekh ke hypothesis banana wrong hai.
- P-hacking se bacho: P-value 0.05 se thoda zyada hai toh data manipulate karke threshold mat lao � yeh research misconduct hai.
- Correlation ✓ Causation: Significant result ka matlab yeh nahi ki ek cheez doosri cause kar rahi hai. Confounders check karo.
- Assumptions check karo: T-test ke liye normal distribution chahiye. Data check karo pehle � warna wrong results aayenge.
Ab Regression par chalo � predictive modeling ka pehla step. Linear Regression se shuru karte hain aur predictions banana seekhte hain.