🎯 Mục tiêu bài học
Sau bài học này, bạn sẽ:
✅ Hiểu khái niệm và quy trình Hypothesis Testing
✅ Nắm vững P-value, Significance Level, Type I/II Errors
✅ Thực hiện Z-test, One-sample, Two-sample và Paired T-tests
✅ Áp dụng Chi-square test cho categorical data
Thời gian: 2.5 giờ | Độ khó: Intermediate | Yêu cầu: Bài 09
📖 Bảng Thuật Ngữ Quan Trọng
| Thuật ngữ | Tiếng Việt | Mô tả |
|---|---|---|
| H₀ | Giả thuyết không | Status quo |
| H₁ | Giả thuyết thay thế | Claim cần chứng minh |
| α | Mức ý nghĩa | P(Type I Error) |
| P-value | Giá trị P | P(kết quả cực đoan | H₀ true) |
| Type I Error | Lỗi loại I | False Positive |
| Type II Error | Lỗi loại II | False Negative |
| Power | Lực kiểm định | 1-β |
| One-sample t | T-test 1 mẫu | So sánh mean vs giá trị |
| Independent t | T-test độc lập | So sánh 2 nhóm khác nhau |
| Paired t | T-test ghép cặp | Cùng subjects, 2 điều kiện |
| Chi-square | Khi-bình phương | Test cho categorical data |
Checkpoint
P-value < α → Reject H₀. P-value ≠ P(H₀ đúng). Ghi nhớ!
🔬 Giới thiệu Hypothesis Testing
1.1 Các bước kiểm định
Quy trình Kiểm định Giả thuyết
📋 Null và Alternative Hypothesis
| Giả thuyết | Ký hiệu | Ý nghĩa |
|---|---|---|
| Null Hypothesis | H₀ | "Không có gì đặc biệt", status quo |
| Alternative Hypothesis | H₁ | Claim cần chứng minh |
One-tailed vs Two-tailed
| Loại | H₁ | Khi nào dùng |
|---|---|---|
| Two-tailed | μ ≠ μ₀ | Chỉ quan tâm "khác" |
| Right-tailed | μ > μ₀ | Quan tâm "lớn hơn" |
| Left-tailed | μ < μ₀ | Quan tâm "nhỏ hơn" |
📊 P-value & Significance Level
3.1 α (Significance Level)
α = 0.05 nghĩa là chấp nhận 5% rủi ro kết luận sai, 95% confident khi reject H₀.
| α | Two-tailed z | One-tailed z |
|---|---|---|
| 0.10 | ±1.645 | 1.28 |
| 0.05 | ±1.96 | 1.645 |
| 0.01 | ±2.576 | 2.33 |
3.2 P-value
| So sánh | Quyết định |
|---|---|
| P-value < α | Reject H₀ (significant) |
| P-value ≥ α | Fail to Reject H₀ |
❌ Xác suất H₀ đúng ❌ Xác suất kết quả do ngẫu nhiên ❌ Mức độ quan trọng thực tế
⚠️ Type I, Type II Errors & Power
| H₀ True | H₀ False | |
|---|---|---|
| Reject H₀ | Type I Error (α) | Correct ✓ |
| Fail to Reject | Correct ✓ | Type II Error (β) |
Ví dụ COVID:
- Type I (False Positive): Không bệnh nhưng dương tính
- Type II (False Negative): Có bệnh nhưng âm tính
📊 Z-Test for Mean
5.1 Điều kiện: σ đã biết, n ≥ 30
5.2 Ví dụ hoàn chỉnh
Bài toán: Pin claim 500 giờ. Mẫu 36 pin: x̄ = 490, σ = 30. α = 0.05.
P-value (0.0456) < α (0.05) → Reject H₀
1from scipy import stats2import numpy as np34x_bar, mu_0, sigma, n, alpha = 490, 500, 30, 36, 0.055z = (x_bar - mu_0) / (sigma / np.sqrt(n))6p_value = 2 * stats.norm.cdf(z)78print(f"Z-statistic: {z:.4f}")9print(f"P-value: {p_value:.4f}")10print(f"→ {'Reject' if p_value < alpha else 'Fail to Reject'} H₀")📊 T-test Overview
6.1 Khi nào dùng T-test?
| Điều kiện | Z-test | T-test |
|---|---|---|
| σ known | ✓ | |
| σ unknown | ✓ | |
| n < 30 | ✓ |
6.2 Các loại T-test
Các loại T-test
One-sample T-test
1from scipy import stats2import numpy as np34sample = [192, 198, 190, 195, 202, 188, 197, 194, 199, 195]5mu_0, alpha = 200, 0.0567t_stat, p_value = stats.ttest_1samp(sample, mu_0)8print(f"Mean: {np.mean(sample):.2f}, t: {t_stat:.4f}, P: {p_value:.4f}")9print(f"→ {'Reject' if p_value < alpha else 'Fail to Reject'} H₀")Independent Two-sample T-test
8.1 So sánh means của 2 nhóm độc lập
1from scipy import stats2import numpy as np34class_A = [85, 90, 78, 92, 88, 76, 95, 89, 82, 91]5class_B = [72, 85, 80, 78, 88, 70, 82, 75, 79, 84]6alpha = 0.0578print(f"A: mean={np.mean(class_A):.2f}, B: mean={np.mean(class_B):.2f}")910# Standard t-test11t_stat, p_value = stats.ttest_ind(class_A, class_B)12print(f"t: {t_stat:.4f}, P: {p_value:.4f}")1314# Welch's t-test (unequal variances)15t_welch, p_welch = stats.ttest_ind(class_A, class_B, equal_var=False)16print(f"Welch: t={t_welch:.4f}, P={p_welch:.4f}")1718# Check equal variances with Levene's test19stat, p_lev = stats.levene(class_A, class_B)20print(f"Levene: p={p_lev:.4f} → {'Equal' if p_lev > 0.05 else 'Unequal'} variances")Paired T-test
9.1 So sánh cùng một nhóm ở 2 thời điểm
1from scipy import stats2import numpy as np34before = [85, 90, 78, 92, 88, 76, 95, 89, 82, 91]5after = [82, 86, 75, 88, 84, 74, 90, 85, 79, 87]6alpha = 0.0578diff = np.array(before) - np.array(after)9print(f"Mean difference: {np.mean(diff):.2f}")1011t_stat, p_value = stats.ttest_rel(before, after)12print(f"t: {t_stat:.4f}, P: {p_value:.4f}")13print(f"→ {'Reject' if p_value < alpha else 'Fail to Reject'} H₀")Checkpoint
Before/After cùng nhóm → Paired T-test. 2 nhóm khác → Independent T-test.
Chọn đúng T-test
Chọn đúng T-test
Chi-square Goodness of Fit
1from scipy import stats23observed = [95, 105, 98, 110, 92, 100] # 600 lần tung xúc xắc4expected = [100] * 656chi2, p_value = stats.chisquare(observed, expected)7print(f"Chi-square: {chi2:.4f}, P-value: {p_value:.4f}")8print(f"→ {'Reject' if p_value < 0.05 else 'Fail to Reject'} H₀")Chi-square Test of Independence
12.1 Contingency Table
1from scipy import stats2import numpy as np3import pandas as pd45observed = np.array([[30, 45], # Male: A, B6 [40, 35]]) # Female: A, B78chi2, p_value, dof, expected = stats.chi2_contingency(observed)910print("Observed:")11print(pd.DataFrame(observed, index=['Male', 'Female'], columns=['A', 'B']))12print(f"\nChi-square: {chi2:.4f}, P: {p_value:.4f}, df: {dof}")13print(f"→ {'Reject' if p_value < 0.05 else 'Fail to Reject'} H₀")12.2 Cramér's V (Effect Size)
1def cramers_v(table):2 chi2, p, dof, exp = stats.chi2_contingency(table)3 n = table.sum()4 return np.sqrt(chi2 / (n * (min(table.shape) - 1)))56v = cramers_v(observed)7print(f"Cramér's V: {v:.4f}")📊 Practical vs Statistical Significance
- Statistical Significance: P-value < α
- Practical Significance: Effect có ý nghĩa thực tế
Cohen's d cho mean difference:
| d | Interpretation |
|---|---|
| 0.2 | Small effect |
| 0.5 | Medium effect |
| 0.8 | Large effect |
🧩 Bài tập thực hành
Bài tập 1: Z-test
Cân nặng claim 500g (σ = 15g). Mẫu 49: x̄ = 495g. Test ở α = 0.05.
Bài tập 2: Paired T-test
| Drug A | Drug B |
|---|---|
| 23, 25, 28, 22, 26 | 30, 32, 29, 31, 28 |
Bài tập 3: Chi-square
| Thành công | Thất bại | |
|---|---|---|
| Method A | 60 | 40 |
| Method B | 45 | 55 |
📝 Tổng Kết
| Test | Khi nào dùng | scipy function |
|---|---|---|
| Z-test | σ known, n ≥ 30 | manual |
| One-sample t | Mean vs value | ttest_1samp |
| Independent t | 2 group means | ttest_ind |
| Paired t | Before/After | ttest_rel |
| Chi-square GoF | Fit distribution | chisquare |
| Chi-square Ind | Independence | chi2_contingency |
Câu hỏi tự kiểm tra
- P-value có ý nghĩa gì và tại sao nó KHÔNG phải là xác suất H₀ đúng?
- Phân biệt Independent t-test và Paired t-test — khi nào dùng loại nào?
- Chi-square test dùng cho loại dữ liệu nào và kiểm định điều gì?
- Tại sao cần phân biệt statistical significance và practical significance?
- Reject H₀ khi P-value < α
- P-value ≠ P(H₀ đúng)
- One-sample t: mean vs known value
- Independent t: 2 different groups
- Paired t: same subjects, 2 conditions
- Chi-square: categorical variables
- Luôn kiểm tra assumptions và practical significance
🎉 Tuyệt vời! Bạn đã hoàn thành bài học Kiểm định giả thuyết (Hypothesis Testing)!
Tiếp theo: Chúng ta sẽ tìm hiểu về Tương quan và Hồi quy (Correlation & Regression) — phân tích mối quan hệ giữa các biến.
