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 𝛼=0.05 test the airline’s claims.

𝐻0:𝜇5𝐻𝑎:𝜇>5𝑡=𝑥̄𝜇𝑠/𝑛

Where:

  • 𝑥̄=12

  • 𝜇=5

  • 𝑠=27

  • 𝑛=20

  • df=𝑛1=19

  • 𝑠/𝑛=27/20=6.0374

𝑡=12527/20=1.1594𝑃(𝑡1.1594)=0.1303𝑡-value<critical-value1.1594<1.7291𝑝-value>𝛼-value0.1303>0.05

Fail to reject 𝐻0. At 𝛼=0.05 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.

𝑡=𝑥̄𝜇0𝑠𝑛
Example
[78,82,89]

Step 1: State Hypotheses

𝐻0:𝜇=85cm𝐻1:𝜇85cm

Step 2: Summarize Data

  • Sample values:
𝑥1=78,𝑥2=82,𝑥3=89
  • Sample size:
𝑛=3

Step 3: Calculate Sample Mean (𝑥̄)

𝑥̄=𝑥1+𝑥2+𝑥3𝑛=78+82+893=83

Step 4: Calculate Sample Standard Deviation:

𝑠=𝑖=1𝑛(𝑥𝑖𝑥̄)2𝑛1
  • Find the deviations from the mean and square them
(𝑥1𝑥̄)2=(7883)2=(5)2=25(𝑥2𝑥̄)2=(8283)2=(1)2=1(𝑥3𝑥̄)2=(8983)2=(6)2=36
  • Sum of squared deviations
SSD=25+1+36
  • Calculate variance of sample
𝑠2=SSD𝑛1=6231=622=31
  • Calculate Sample Standard Deviation
𝑠=𝑠2=32=5.57

Step 5: Calculate the Test Statistic

𝑡=𝑥̄𝜇0𝑠𝑛=83855.573=33.22=0.62

Step 6: Determine the Degrees of Freedom

𝑑𝑓=𝑛1=151=14

Step 7: Find Critical t-value

  • For a two-tailed test at a significance level (𝛼) of 0.05 and 2 degrees of freedom (𝑑𝑓)
4.303

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

𝑡=4.303and𝑑𝑓=3p-value=0.58
t_test_one_sample.py
from scipy import stats
rvs = stats.uniform.rvs(size=50)
stats.ttest_1samp(rvs, popmean=0.5)