192. One-Sample T-Test for Mean
Population standard deviation, , is unknown.
Example
An airline claims their mean departure time is 5 minutes late, but you believe their flights are more delayed than that. You take a simple random sample of 20 flights. Your sample has a mean delay of 12 mins with a sample SD of 27 mins. Assume population flight times are normally distributed. At test the airline’s claims.
Where:
Fail to reject . At there is not sufficient evidence that the mean departure delay is more than 5 minutes.
Code
import numpy as np
from scipy.stats import t
mu = 5
s = 27
xbar = 12
n = 20
df = n - 1
alpha = 0.05
cv = t.ppf(1 - alpha, df = df)
t_statistic = (xbar - mu) / (s / np.sqrt(n))
p = t.cdf(z)
if cv > t_statistic:
print("Reject null hypothesis")
else:
print("Fail to reject null hypothesis")192.1. One-sample
Tests if the mean of a single sample differs from a known or hypothesized population mean.
- : sample mean
- : hypothesized population mean
- : sample standard deviation
- : sample size
Example
Step 1: State Hypotheses
Step 2: Summarize Data
- Sample values:
- Sample size:
Step 3: Calculate Sample Mean ()
Step 4: Calculate Sample Standard Deviation:
- Find the deviations from the mean and square them
- Sum of squared deviations
- Calculate variance of sample
- Calculate Sample Standard Deviation
Step 5: Calculate the Test Statistic
Step 6: Determine the Degrees of Freedom
Step 7: Find Critical t-value
- For a two-tailed test at a significance level () of 0.05 and 2 degrees of freedom ()
Step 8: Compare the t-Value to the Critical t-Value
- If the absolute value of the test statistic is greater than the critical t-value, reject the null hypothesis.
- If the absolute value of the test statistic is less than the critical t-value, fail to reject the null hypothesis.
Step 8: Find the p-Value
t_test_one_sample.py
from scipy import stats
rvs = stats.uniform.rvs(size=50)
stats.ttest_1samp(rvs, popmean=0.5)