Lý thuyết
1.5 giờ
Bài 12/15

Kiểm định giả thuyết cơ bản

Hypothesis Testing, P-value và Statistical Significance

🔬 Kiểm định giả thuyết cơ bản

Mục tiêu bài học

Sau bài học này, bạn sẽ:

  • Hiểu khái niệm Hypothesis Testing
  • Nắm vững quy trình kiểm định
  • Hiểu P-value và Significance Level
  • Phân biệt Type I và Type II Errors

1. Giới thiệu Hypothesis Testing

1.1 Tại sao cần kiểm định?

Tình huống: Công ty claim sản phẩm mới giúp tăng 20% doanh thu.

Câu hỏi: Làm sao biết claim này có đúng không?

Hypothesis Testing giúp đưa ra quyết định dựa trên dữ liệu!

1.2 Các bước kiểm định

Quy trình Kiểm định Giả thuyết


2. Null và Alternative Hypothesis

2.1 Định nghĩa

Giả thuyếtKý hiệuÝ nghĩa
Null HypothesisH₀"Không có gì đặc biệt", status quo
Alternative HypothesisH₁ hoặc HₐClaim cần chứng minh

2.2 Cách đặt giả thuyết

Ví dụ: Thuốc mới có hiệu quả hơn không?

  • H₀: μ = μ₀ (không khác biệt)
  • H₁: μ ≠ μ₀ hoặc μ > μ₀ hoặc μ < μ₀

2.3 One-tailed vs Two-tailed

LoạiH₁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"
Python
1import numpy as np
2import matplotlib.pyplot as plt
3from scipy import stats
4
5x = np.linspace(-4, 4, 1000)
6y = stats.norm.pdf(x)
7
8fig, axes = plt.subplots(1, 3, figsize=(15, 4))
9
10# Two-tailed
11axes[0].plot(x, y)
12axes[0].fill_between(x, y, where=(x < -1.96) | (x > 1.96), alpha=0.3, color='red')
13axes[0].set_title('Two-tailed (H₁: μ ≠ μ₀)')
14axes[0].axvline(-1.96, color='red', linestyle='--')
15axes[0].axvline(1.96, color='red', linestyle='--')
16
17# Right-tailed
18axes[1].plot(x, y)
19axes[1].fill_between(x, y, where=(x > 1.645), alpha=0.3, color='red')
20axes[1].set_title('Right-tailed (H₁: μ > μ₀)')
21axes[1].axvline(1.645, color='red', linestyle='--')
22
23# Left-tailed
24axes[2].plot(x, y)
25axes[2].fill_between(x, y, where=(x < -1.645), alpha=0.3, color='red')
26axes[2].set_title('Left-tailed (H₁: μ < μ₀)')
27axes[2].axvline(-1.645, color='red', linestyle='--')
28
29plt.tight_layout()
30plt.show()

3. Significance Level (α)

3.1 Định nghĩa

α (alpha) = Xác suất reject H₀ khi H₀ đúng (Type I Error)

Giá trị thường dùng: α = 0.05 (5%)

3.2 Ý nghĩa

α = 0.05 nghĩa là:

  • Chấp nhận 5% rủi ro kết luận sai
  • 95% confident khi reject H₀

3.3 Critical Value

αTwo-tailed zOne-tailed z
0.10±1.6451.28
0.05±1.961.645
0.01±2.5762.33

4. P-value

4.1 Định nghĩa

P-value = Xác suất quan sát được kết quả ít nhất cực đoan như kết quả hiện tại, giả sử H₀ đúng.

4.2 Quy tắc quyết định

So sánhQuyết định
P-value < αReject H₀ (Kết quả significant)
P-value ≥ αFail to Reject H₀

4.3 Cách hiểu P-value

P-valueInterpretation
< 0.01Very strong evidence against H₀
0.01 - 0.05Strong evidence against H₀
0.05 - 0.10Weak evidence against H₀
> 0.10Little or no evidence against H₀
P-value KHÔNG phải

❌ 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ế (practical significance)


5. Type I và Type II Errors

Type I và Type II Errors

5.1 Ma trận lỗi

H₀ TrueH₀ False
Reject H₀Type I Error (α)Correct ✓
Fail to RejectCorrect ✓Type II Error (β)

5.2 Ví dụ thực tế

Xét nghiệm COVID:

  • Type I (False Positive): Không bệnh nhưng kết quả dương tính
  • Type II (False Negative): Có bệnh nhưng kết quả âm tính

5.3 Power of Test

Power=1β=P(Reject H0H0 false)Power = 1 - \beta = P(\text{Reject } H_0 | H_0 \text{ false})

Power cao → Khả năng phát hiện hiệu ứng thực cao

5.4 Trade-off

Trade-off giữa α, β và Power

⬇️α giảm
⬆️n tăng
⬆️Effect size tăng
β tăng
Power tăng ✨
Power tăng ✨

6. Z-Test for Mean

6.1 Điều kiện

  • σ đã biết
  • n ≥ 30 hoặc population normal

6.2 Test Statistic

z=xˉμ0σ/nz = \frac{\bar{x} - \mu_0}{\sigma / \sqrt{n}}

6.3 Ví dụ hoàn chỉnh

Bài toán: Công ty claim pin có tuổi thọ trung bình 500 giờ. Kiểm tra mẫu 36 pin: x̄ = 490, σ = 30. Test ở α = 0.05.

Bước 1: Đặt giả thuyết

  • H₀: μ = 500
  • H₁: μ ≠ 500 (two-tailed)

Bước 2: Tính z-statistic z=49050030/36=105=2z = \frac{490 - 500}{30/\sqrt{36}} = \frac{-10}{5} = -2

Bước 3: Tính P-value P=2×P(Z<2)=2×0.0228=0.0456P = 2 \times P(Z < -2) = 2 \times 0.0228 = 0.0456

Bước 4: Kết luận P-value (0.0456) < α (0.05) → Reject H₀

Python
1from scipy import stats
2import numpy as np
3
4# Dữ liệu
5x_bar = 490
6mu_0 = 500
7sigma = 30
8n = 36
9alpha = 0.05
10
11# Z-statistic
12z = (x_bar - mu_0) / (sigma / np.sqrt(n))
13print(f"Z-statistic: {z:.4f}")
14
15# P-value (two-tailed)
16p_value = 2 * stats.norm.cdf(z) # z < 0, nên dùng cdf
17print(f"P-value: {p_value:.4f}")
18
19# Quyết định
20if p_value < alpha:
21 print(f"P-value ({p_value:.4f}) < α ({alpha}) → Reject H₀")
22else:
23 print(f"P-value ({p_value:.4f}) ≥ α ({alpha}) → Fail to Reject H₀")

7. Z-Test for Proportion

7.1 Test Statistic

z=p^p0p0(1p0)nz = \frac{\hat{p} - p_0}{\sqrt{\frac{p_0(1-p_0)}{n}}}

7.2 Ví dụ

Bài toán: Tỷ lệ hài lòng trước đây là 70%. Khảo sát mới 200 người, 150 hài lòng (75%). Tỷ lệ có tăng không? (α = 0.05)

Python
1from scipy import stats
2import numpy as np
3
4# Dữ liệu
5p_hat = 150 / 200 # 0.75
6p_0 = 0.70
7n = 200
8alpha = 0.05
9
10# Z-statistic
11se = np.sqrt(p_0 * (1 - p_0) / n)
12z = (p_hat - p_0) / se
13print(f"Z-statistic: {z:.4f}")
14
15# P-value (right-tailed: H₁: p > 0.70)
16p_value = 1 - stats.norm.cdf(z)
17print(f"P-value (one-tailed): {p_value:.4f}")
18
19# Quyết định
20if p_value < alpha:
21 print("→ Reject H₀: Tỷ lệ hài lòng đã tăng")
22else:
23 print("→ Fail to Reject H₀: Không đủ bằng chứng")

8. Using scipy.stats

Python
1from scipy import stats
2import numpy as np
3
4# 1. One-sample Z-test (manual)
5def z_test_one_sample(x_bar, mu_0, sigma, n, alternative='two-sided'):
6 z = (x_bar - mu_0) / (sigma / np.sqrt(n))
7
8 if alternative == 'two-sided':
9 p_value = 2 * (1 - stats.norm.cdf(abs(z)))
10 elif alternative == 'greater':
11 p_value = 1 - stats.norm.cdf(z)
12 else: # less
13 p_value = stats.norm.cdf(z)
14
15 return z, p_value
16
17# 2. Proportion Z-test
18from statsmodels.stats.proportion import proportions_ztest
19
20count = 150 # successes
21nobs = 200 # total
22p_null = 0.70
23
24z_stat, p_value = proportions_ztest(count, nobs, value=p_null, alternative='larger')
25print(f"Z-stat: {z_stat:.4f}, P-value: {p_value:.4f}")

9. Practical vs Statistical Significance

9.1 Vấn đề

  • Statistical Significance: P-value < α
  • Practical Significance: Effect có ý nghĩa thực tế

9.2 Ví dụ

n = 10,000: Thuốc giảm cân trung bình 0.1kg

  • P-value = 0.001 (statistically significant)
  • Nhưng 0.1kg có ý nghĩa thực tế không? → Có thể KHÔNG!

9.3 Effect Size

Cohen's d cho mean difference:

d=xˉμ0σd = \frac{\bar{x} - \mu_0}{\sigma}

dInterpretation
0.2Small effect
0.5Medium effect
0.8Large effect
Python
1def cohens_d(x_bar, mu_0, sigma):
2 return (x_bar - mu_0) / sigma
3
4# Ví dụ
5d = cohens_d(490, 500, 30)
6print(f"Cohen's d: {d:.2f}") # -0.33 → Small-Medium effect

10. Bài tập thực hành

Bài tập 1: Z-test for Mean

Cân nặng trung bình claim là 500g (σ = 15g). Mẫu 49 sản phẩm: x̄ = 495g.

  1. Đặt H₀ và H₁ (two-tailed)
  2. Tính z-statistic
  3. Tính P-value
  4. Kết luận ở α = 0.05

Bài tập 2: Z-test for Proportion

Tỷ lệ lỗi target: 3%. Kiểm tra 400 sản phẩm, 20 lỗi.

  1. Tỷ lệ lỗi có vượt target không? (α = 0.05)

Bài tập 3: Type Errors

Nếu α = 0.01 thay vì 0.05:

  1. Type I Error tăng hay giảm?
  2. Type II Error tăng hay giảm?
  3. Cần làm gì để giảm cả hai?

Tóm tắt

Khái niệmĐịnh nghĩa
H₀Giả thuyết không (status quo)
H₁Giả thuyết thay thế (claim)
αP(Type I Error) = P(Reject H₀ | H₀ true)
βP(Type II Error) = P(Fail to Reject | H₀ false)
P-valueP(kết quả cực đoan | H₀ true)
Power1 - β
Key Takeaways
  1. Reject H₀ khi P-value < α
  2. P-value ≠ P(H₀ đúng)
  3. Type I (α): False Positive
  4. Type II (β): False Negative
  5. Practical significance quan trọng không kém statistical