MinAI - Về trang chủ
Lý thuyết
10/122.5 giờ
Đang tải...

Hypothesis Testing, T-tests & Chi-square

Kiểm định giả thuyết, P-value, T-tests và Chi-square Test

0

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

TB5 min

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

1

📖 Bảng Thuật Ngữ Quan Trọng

TB5 min
Thuật ngữTiếng ViệtMô tả
H₀Giả thuyết khôngStatus quo
H₁Giả thuyết thay thếClaim cần chứng minh
αMức ý nghĩaP(Type I Error)
P-valueGiá trị PP(kết quả cực đoan | H₀ true)
Type I ErrorLỗi loại IFalse Positive
Type II ErrorLỗi loại IIFalse Negative
PowerLực kiểm định1-β
One-sample tT-test 1 mẫuSo sánh mean vs giá trị
Independent tT-test độc lậpSo sánh 2 nhóm khác nhau
Paired tT-test ghép cặpCùng subjects, 2 điều kiện
Chi-squareKhi-bình phươngTest cho categorical data

Checkpoint

P-value < α → Reject H₀. P-value ≠ P(H₀ đúng). Ghi nhớ!

2

🔬 Giới thiệu Hypothesis Testing

TB5 min

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

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

3

📋 Null và Alternative Hypothesis

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

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"
4

📊 P-value & Significance Level

TB5 min

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 zOne-tailed z
0.10±1.6451.28
0.05±1.961.645
0.01±2.5762.33

3.2 P-value

So sánhQuyết định
P-value < αReject H₀ (significant)
P-value ≥ αFail to Reject 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ế

5

⚠️ Type I, Type II Errors & Power

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

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

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
6

📊 Z-Test for Mean

TB5 min

5.1 Điều kiện: σ đã biết, n ≥ 30

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

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.

z=49050030/36=2,P=2×P(Z<2)=0.0456z = \frac{490 - 500}{30/\sqrt{36}} = -2, \quad P = 2 \times P(Z < -2) = 0.0456

P-value (0.0456) < α (0.05) → Reject H₀

Python
1from scipy import stats
2import numpy as np
3
4x_bar, mu_0, sigma, n, alpha = 490, 500, 30, 36, 0.05
5z = (x_bar - mu_0) / (sigma / np.sqrt(n))
6p_value = 2 * stats.norm.cdf(z)
7
8print(f"Z-statistic: {z:.4f}")
9print(f"P-value: {p_value:.4f}")
10print(f"→ {'Reject' if p_value < alpha else 'Fail to Reject'} H₀")
7

📊 T-test Overview

TB5 min

6.1 Khi nào dùng T-test?

Điều kiệnZ-testT-test
σ known
σ unknown
n < 30

6.2 Các loại T-test

Các loại T-test

📊T-test
8

One-sample T-test

TB5 min

t=xˉμ0s/n,df=n1t = \frac{\bar{x} - \mu_0}{s / \sqrt{n}}, \quad df = n - 1

Python
1from scipy import stats
2import numpy as np
3
4sample = [192, 198, 190, 195, 202, 188, 197, 194, 199, 195]
5mu_0, alpha = 200, 0.05
6
7t_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₀")
9

Independent Two-sample T-test

TB5 min

8.1 So sánh means của 2 nhóm độc lập

t=xˉ1xˉ2sp1n1+1n2t = \frac{\bar{x}_1 - \bar{x}_2}{s_p \sqrt{\frac{1}{n_1} + \frac{1}{n_2}}}

Python
1from scipy import stats
2import numpy as np
3
4class_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.05
7
8print(f"A: mean={np.mean(class_A):.2f}, B: mean={np.mean(class_B):.2f}")
9
10# Standard t-test
11t_stat, p_value = stats.ttest_ind(class_A, class_B)
12print(f"t: {t_stat:.4f}, P: {p_value:.4f}")
13
14# 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}")
17
18# Check equal variances with Levene's test
19stat, p_lev = stats.levene(class_A, class_B)
20print(f"Levene: p={p_lev:.4f} → {'Equal' if p_lev > 0.05 else 'Unequal'} variances")
10

Paired T-test

TB5 min

9.1 So sánh cùng một nhóm ở 2 thời điểm

t=dˉsd/nt = \frac{\bar{d}}{s_d / \sqrt{n}}

Python
1from scipy import stats
2import numpy as np
3
4before = [85, 90, 78, 92, 88, 76, 95, 89, 82, 91]
5after = [82, 86, 75, 88, 84, 74, 90, 85, 79, 87]
6alpha = 0.05
7
8diff = np.array(before) - np.array(after)
9print(f"Mean difference: {np.mean(diff):.2f}")
10
11t_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.

11

Chọn đúng T-test

TB5 min

Chọn đúng T-test

So sánh means?
🔢Mấy nhóm?
1️⃣1 nhóm vs giá trị → One-sample
Cùng subjects?
Yes → Paired t-test
No → Independent t-test
12

Chi-square Goodness of Fit

TB5 min

χ2=(OiEi)2Ei\chi^2 = \sum \frac{(O_i - E_i)^2}{E_i}

Python
1from scipy import stats
2
3observed = [95, 105, 98, 110, 92, 100] # 600 lần tung xúc xắc
4expected = [100] * 6
5
6chi2, 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₀")
13

Chi-square Test of Independence

TB5 min

12.1 Contingency Table

Eij=Rowi×ColjTotalE_{ij} = \frac{Row_i \times Col_j}{Total}

Python
1from scipy import stats
2import numpy as np
3import pandas as pd
4
5observed = np.array([[30, 45], # Male: A, B
6 [40, 35]]) # Female: A, B
7
8chi2, p_value, dof, expected = stats.chi2_contingency(observed)
9
10print("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)

Python
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)))
5
6v = cramers_v(observed)
7print(f"Cramér's V: {v:.4f}")
14

📊 Practical vs Statistical Significance

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

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
15

🧩 Bài tập thực hành

TB5 min

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 ADrug B
23, 25, 28, 22, 2630, 32, 29, 31, 28

Bài tập 3: Chi-square

Thành côngThất bại
Method A6040
Method B4555
16

📝 Tổng Kết

TB5 min
TestKhi nào dùngscipy function
Z-testσ known, n ≥ 30manual
One-sample tMean vs valuettest_1samp
Independent t2 group meansttest_ind
Paired tBefore/Afterttest_rel
Chi-square GoFFit distributionchisquare
Chi-square IndIndependencechi2_contingency

Câu hỏi tự kiểm tra

  1. P-value có ý nghĩa gì và tại sao nó KHÔNG phải là xác suất H₀ đúng?
  2. Phân biệt Independent t-test và Paired t-test — khi nào dùng loại nào?
  3. Chi-square test dùng cho loại dữ liệu nào và kiểm định điều gì?
  4. Tại sao cần phân biệt statistical significance và practical significance?
Key Takeaways
  1. Reject H₀ khi P-value < α
  2. P-value ≠ P(H₀ đúng)
  3. One-sample t: mean vs known value
  4. Independent t: 2 different groups
  5. Paired t: same subjects, 2 conditions
  6. Chi-square: categorical variables
  7. Luôn kiểm tra assumptionspractical 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.