If you are interested in T-test, you can do similar:
- z-statistics (z-score) is used when the data follows a normal distribution, population standard deviation sigma is known and the sample size is above 30. Z-Score tells you how many standard deviations from the mean your result is. The z-score is calculated using the formula:
z_score = (xbar - mu) / sigma
- t-statistics (t-score), also known as Student's T-Distribution, is used when the data follows a normal distribution, population standard deviation (sigma) is NOT known, but the sample standard deviation (s) is known or can be calculated, and the sample size is below 30. T-Score tells you how many standard deviations from the mean your result is. The t-score is calculated using the formula:
t_score = (xbar - mu) / (s/sqrt(n))
Summary: If the sample sizes are larger than 30, the z-distribution and the t-distributions are pretty much the same and either one can be used. If the population standard deviation is available and the sample size is greater than 30, t-distribution can be used with the population standard deviation instead of the sample standard deviation.
test statistics |
lookup table |
lookup values |
critical value |
normal distribution |
population standard deviation (sigma) |
sample size |
z-statistics |
z-table |
z-score |
z-critical is z-score at a specific confidence level |
yes |
known |
> 30 |
t-statistics |
t-table |
t-score |
t-critical is t-score at a specific confidence level |
yes |
not known |
< 30 |
Python Percent Point Function is used to calculate the critical values at a specific confidence level:
- z-critical
= stats.norm.ppf(1 - alpha) (use alpha = alpha/2 for two-sided)
- t-critical
= stats.t.ppf(alpha/numOfTails, ddof)
Codes
import numpy as np
from scipy import stats
# alpha to critical
alpha = 0.05
n_sided = 2 # 2-sided test
z_crit = stats.norm.ppf(1-alpha/n_sided)
print(z_crit) # 1.959963984540054
# critical to alpha
alpha = stats.norm.sf(z_crit) * n_sided
print(alpha) # 0.05