Imagine you work at an electric car manufacturing company. The horn is an important tool. If it is too loud, people may get annoyed. If the horn sounds too small, people may think that someone is blowing his/her nose instead of thinking that is a car horn sound. So, you want to ensure that the variance of the sound levels of car horns produced should not exceed decibels.
Suppose we want to test if the variance of the weight of horns produced by a machine is equal to a specified value. We will use a one-sample variance test to determine if there is a significant difference.
Step 1: State the Hypotheses
The null hypothesis and the alternative hypothesis
can be stated as follows:
where is the true variance of the weight and
is the specified variance.
Step 2: Collect Data
Suppose we randomly sample 20 nose horns produced by the machine and measure their weights (in grams). We find the following weights:
x = [12.3, 11.7, 12.5, 12.1, 12.4, 11.8, 12.2, 12.3, 11.9, 12.0, 12.2, 12.3, 12.1, 11.8, 12.4, 12.2, 12.3, 11.9, 12.1, 12.2]
Step 3: Calculate the Test Statistic
First, we calculate the sample variance :
where is the sample mean.
Next, we calculate the test statistic :
where is the sample size and
is the specified variance.
Step 4: Determine the Critical Value
Using the chi-squared distribution, we find the critical value for a two-tailed test at the significance level with
degrees of freedom.
Step 5: Make a Decision
Compare the calculated statistic to the critical value from the chi-squared distribution table. If the
statistic is greater than the critical value or less than the lower critical value, we reject the null hypothesis.
Conclusion
Based on the comparison of the statistic and the critical value, we determine whether there is enough evidence to conclude that the variance of the weight of nose horns is different from the specified value.
implementation
Python
import numpy as np
from scipy.stats import chi2
# Data
weights = np.array([12.3, 11.7, 12.5, 12.1, 12.4, 11.8, 12.2, 12.3, 11.9, 12.0,
12.2, 12.3, 12.1, 11.8, 12.4, 12.2, 12.3, 11.9, 12.1, 12.2])
n = len(weights)
sigma0_squared = 0.1 # specified variance
# Calculate sample variance
sample_variance = np.var(weights, ddof=1)
# Calculate test statistic
chi_squared_statistic = (n - 1) * sample_variance / sigma0_squared
# Degrees of freedom
df = n - 1
# Calculate p-value
p_value = 2 * (1 - chi2.cdf(np.abs(chi_squared_statistic), df))
# Critical value for two-tailed test at alpha = 0.05
alpha = 0.05
critical_value_lower = chi2.ppf(alpha / 2, df)
critical_value_upper = chi2.ppf(1 - alpha / 2, df)
# Print results
print(f"Sample Variance: {sample_variance}")
print(f"Chi-Squared Statistic: {chi_squared_statistic}")
print(f"Degrees of Freedom: {df}")
print(f"P-Value: {p_value}")
print(f"Critical Value (lower): {critical_value_lower}")
print(f"Critical Value (upper): {critical_value_upper}")
# Conclusion
if chi_squared_statistic < critical_value_lower or chi_squared_statistic > critical_value_upper:
print("Reject the null hypothesis.")
else:
print("Fail to reject the null hypothesis.")
R
# Data
weights <- c(12.3, 11.7, 12.5, 12.1, 12.4, 11.8, 12.2, 12.3, 11.9, 12.0,
12.2, 12.3, 12.1, 11.8, 12.4, 12.2, 12.3, 11.9, 12.1, 12.2)
n <- length(weights)
sigma0_squared <- 0.1 # specified variance
# Calculate sample variance
sample_variance <- var(weights)
# Calculate test statistic
chi_squared_statistic <- (n - 1) * sample_variance / sigma0_squared
# Degrees of freedom
df <- n - 1
# Calculate p-value
p_value <- 2 * (1 - pchisq(abs(chi_squared_statistic), df))
# Critical value for two-tailed test at alpha = 0.05
alpha <- 0.05
critical_value_lower <- qchisq(alpha / 2, df)
critical_value_upper <- qchisq(1 - alpha / 2, df)
# Print results
cat("Sample Variance:", sample_variance, "\n")
cat("Chi-Squared Statistic:", chi_squared_statistic, "\n")
cat("Degrees of Freedom:", df, "\n")
cat("P-Value:", p_value, "\n")
cat("Critical Value (lower):", critical_value_lower, "\n")
cat("Critical Value (upper):", critical_value_upper, "\n")
# Conclusion
if (chi_squared_statistic < critical_value_lower || chi_squared_statistic > critical_value_upper) {
cat("Reject the null hypothesis.\n")
} else {
cat("Fail to reject the null hypothesis.\n")
}