样本容量是指从总体中抽取的样本数量。单个总体样本容量的确定是指在给定的置信水平和误差范围内,确定从总体中抽取的样本数量。样本容量的确定有多种方法,常用的方法有:
正态分布法:如果总体服从正态分布,则可以使用正态分布法来确定样本容量。正态分布法的公式为:
n
=
Z
2
σ
2
e
2
n = \frac{Z^2 \sigma^2}{e^2}
n=e2Z2σ2?
其中,n是样本容量,Z是置信水平对应的z值,σ是总体标准差,e是允许误差。
t分布法:如果总体不服从正态分布,则可以使用t分布法来确定样本容量。t分布法的公式为:
n
=
t
2
σ
2
e
2
n = \frac{t^2 \sigma^2}{e^2}
n=e2t2σ2?
其中,n是样本容量,t是置信水平对应的t值,σ是总体标准差,e是允许误差。
卡方分布法:如果总体服从卡方分布,则可以使用卡方分布法来确定样本容量。卡方分布法的公式为:
n
=
χ
2
σ
2
e
2
n = \frac{\chi^2 \sigma^2}{e^2}
n=e2χ2σ2?
其中,n是样本容量,χ^2是置信水平对应的卡方值,σ是总体标准差,e是允许误差。
单个总体样本容量的确定在实际工程中有广泛的应用,例如:
单个总体样本容量的确定有多种方法,每种方法都有其优缺点。
Python代码:
import numpy as np
import scipy.stats as stats
# 正态分布法
def sample_size_normal(confidence_level, margin_of_error, population_std_dev):
"""Calculates the sample size for a normal distribution.
Args:
confidence_level: The desired confidence level, as a decimal between 0 and 1.
margin_of_error: The maximum allowed error, as a decimal between 0 and 1.
population_std_dev: The standard deviation of the population.
Returns:
The sample size, as an integer.
"""
z = stats.norm.ppf(confidence_level)
n = (z ** 2 * population_std_dev ** 2) / (margin_of_error ** 2)
return int(np.ceil(n))
# t分布法
def sample_size_t(confidence_level, margin_of_error, population_std_dev, degrees_of_freedom):
"""Calculates the sample size for a t-distribution.
Args:
confidence_level: The desired confidence level, as a decimal between 0 and 1.
margin_of_error: The maximum allowed error, as a decimal between 0 and 1.
population_std_dev: The standard deviation of the population.
degrees_of_freedom: The degrees of freedom for the t-distribution.
Returns:
The sample size, as an integer.
"""
t = stats.t.ppf(confidence_level, degrees_of_freedom)
n = (t ** 2 * population_std_dev ** 2) / (margin_of_error ** 2)
return int(np.ceil(n))
# 卡方分布法
def sample_size_chi_square(confidence_level, margin_of_error, population_proportion):
"""Calculates the sample size for a chi-square distribution.
Args:
confidence_level: The desired confidence level, as a decimal between 0 and 1.
margin_of_error: The maximum allowed error, as a decimal between 0 and 1.
population_proportion: The proportion of the population that has the characteristic of interest.
Returns:
The sample size, as an integer.
"""
chi_square = stats.chi2.ppf(confidence_level, 1)
n = (chi_square * population_proportion * (1 - population_proportion)) / (margin_of_error ** 2)
return int(np.ceil(n))
# 使用正态分布法计算样本容量
confidence_level = 0.95
margin_of_error = 0.05
population_std_dev = 10
sample_size = sample_size_normal(confidence_level, margin_of_error, population_std_dev)
print("Sample size (normal distribution):", sample_size)
# 使用t分布法计算样本容量
degrees_of_freedom = 10
sample_size = sample_size_t(confidence_level, margin_of_error, population_std_dev, degrees_of_freedom)
print("Sample size (t-distribution):", sample_size)
# 使用卡方分布法计算样本容量
population_proportion = 0.5
sample_size = sample_size_chi_square(confidence_level, margin_of_error, population_proportion)
print("Sample size (chi-square distribution):", sample_size)
R代码:
# 正态分布法
sample_size_normal <- function(confidence_level, margin_of_error, population_std_dev) {
z <- qnorm(confidence_level)
n <- (z^2 * population_std_dev^2) / margin_of_error^2
return(ceiling(n))
}
# t分布法
sample_size_t <- function(confidence_level, margin_of_error, population_std_dev, degrees_of_freedom) {
t <- qt(confidence_level, degrees_of_freedom)
n <- (t^2 * population_std_dev^2) / margin_of_error^2
return(ceiling(n))
}
# 卡方分布法
sample_size_chi_square <- function(confidence_level, margin_of_error, population_proportion) {
chi_square <- qchisq(confidence_level, 1)
n <- (chi_square * population_proportion * (1 - population_proportion)) / margin_of_error^2
return(ceiling(n))
}
# 使用正态分布法计算样本容量
confidence_level <- 0.95
margin_of_error <- 0.05
population_std_dev <- 10
sample_size <- sample_size_normal(confidence_level, margin_of_error, population_std_dev)
print(paste("Sample size (normal distribution):", sample_size))
# 使用t分布法计算样本容量
degrees_of_freedom <- 10
sample_size <- sample_size_t(confidence_level, margin_of_error, population_std_dev, degrees_of_freedom)
print(paste("Sample size (t-distribution):", sample_size))
# 使用卡方分布法计算样本容量
population_proportion <- 0.5
sample_size <- sample_size_chi_square(confidence_level, margin_of_error, population_proportion)
print(paste("Sample size (chi-square distribution):", sample_size))