← 返回资讯屏轩教育 · 行业资讯

6. Apply Goodness-of-Fit Tests to Geometric Distributions | 6. 应用拟合优度检验于几何分布

2026-08-02 17:21 · 本文部分内容由AI辅助创作

📚 6. Apply Goodness-of-Fit Tests to Geometric Distributions | 6. 应用拟合优度检验于几何分布

In computational statistics and simulation modelling, we often need to verify whether observed data follow a specific theoretical distribution. The geometric distribution models the number of trials required to achieve the first success in a sequence of independent Bernoulli trials. A goodness-of-fit test, typically the chi-squared test, allows us to assess how well the observed frequencies align with the geometric model. This article covers the entire process of applying such a test, from formulating hypotheses to interpreting results, with a practical example and a Python implementation.

在计算统计学与模拟建模中,我们常需验证观测数据是否服从某种理论分布。几何分布描述在独立的伯努利试验序列中获得首次成功所需的试验次数。拟合优度检验(通常采用卡方检验)能让我们评估观测频数与几何模型之间的吻合程度。本文涵盖从建立假设到阐释结果的全过程,并提供实际案例与 Python 实现。

1. Geometric Distribution Refresher | 几何分布回顾

probability p (0 < p ≤ 1) and failures occur with probability 1 − p, then the probability mass function is P(X = x) = p (1 − p)x−1 for x = 1, 2, 3, … . The mean or expected value is E(X) = 1/p, and the variance is Var(X) = (1 − p) / p².

几何分布针对随机变量 X,表示首次成功出现的试验次数。若每次试验成功概率为 p(0 < p ≤ 1),失败概率为 1 − p,则概率质量函数为 P(X = x) = p (1 − p)x−1,x = 1, 2, 3, … 。期望值 E(X) = 1/p,方差 Var(X) = (1 − p) / p²。

In many computing scenarios, such as modelling the number of attempts before a packet is successfully transmitted over a noisy channel, the geometric distribution provides a natural fit. Testing this fit ensures that simulation assumptions hold.

在许多计算场景中,例如对数据包在噪声信道上首次成功传输所需尝试次数进行建模时,几何分布是一种自然的分布。检验该拟合程度能确保模拟假设成立。


2. What Is a Goodness-of-Fit Test? | 什么是拟合优度检验?

A goodness-of-fit test determines whether a sample of data comes from a specific probability distribution. The chi-squared (χ²) goodness-of-fit test is the most common approach. It compares observed frequencies Oᵢ with expected frequencies Eᵢ under the hypothesised distribution and computes a test statistic that measures the discrepancy.

拟合优度检验用于判断一个数据样本是否来自特定的概率分布。卡方(χ²)拟合优度检验是最常用的方法。它比较观测频数 Oᵢ 与在假设分布下得到的期望频数 Eᵢ,并计算衡量差异程度的检验统计量。

The formula for the test statistic is:

χ² = Σ (Oᵢ − Eᵢ)² / Eᵢ

检验统计量的公式为:

χ² = Σ (Oᵢ − Eᵢ)² / Eᵢ

If the observed and expected frequencies are close, the statistic will be small, suggesting a good fit. A large value indicates a poor fit, possibly leading to rejection of the hypothesised distribution.

如果观测频数与期望频数接近,统计量就较小,表明拟合良好。数值较大则说明拟合较差,可能导致拒绝所假设的分布。


3. The Chi-Squared Test and the Geometric Distribution | 卡方检验与几何分布

t define the outcome categories. Usually these are the possible trial numbers x = 1, 2, 3, … etric distribution has infinite support, we group higher values into a tail category (e.g. "≥ k"). Each category must have an expected frequency of at least 5 to satisfy the test's requirements.

对几何分布应用 χ² 检验时,我们首先定义结果类别。通常类别为可能的试验次数 x = 1, 2, 3, … 直至某个上限,但由于几何分布有无穷支撑集,我们会将较高的取值归入一个尾部类别(如 “≥ k”)。每个类别的期望频数须至少为 5 才能满足检验要求。

the null hypothesis, we estimate it from the sample, often using the method of moments (p̂ = 1 / x̄) or maximum likelihood estimation.

类别 x 的期望频数通过 n × P(X = x) 计算,使用假定的概率 p。若原假设未指定 p,我们通常从样本中估计它,常见方法为矩估计(p̂ = 1 / x̄)或最大似然估计。


4. Formulating Hypotheses | 建立假设

The null hypothesis H₀ states that the data follow a geometric distribution with a specific (or unspecified) parameter p. The alternative hypothesis H₁ is that the data do not follow that distribution.

原假设 H₀ 声称数据服从具有特定(或未指定)参数 p 的几何分布。备择假设 H₁ 则表示数据不服从该分布。

For a completely specified distribution, e.g. "H₀: the number of trials until heads appears follows Geom(0.5)", no parameter estimation is needed. If p is unspecified, we would state "H₀: the data follow a geometric distribution" and estimate p from the data.

对于完全指定的分布,例如 “H₀:直到出现正面所需的试验次数服从几何分布 Geom(0.5)”,则无需进行参数估计。若 p 未指定,我们会表述为 “H₀:数据服从几何分布”,并从数据中估计 p。


5. Calculating Expected Frequencies | 计算期望频数

ations. For a given p, the expected number of observations equal to x is Eₓ = n · p(1 − p)x−1 expected count for x ≥ k is at least 5, and then combine all categories from k onward into "≥ k".

假设有 n 个观测值。对于给定的 p,等于 x 的期望观测次数为 Eₓ = n · p(1 − p)x−1。我们必须合并或截断以确保没有任何期望值低于 5。通常我们找出最小的 k 使得 x ≥ k 的期望计数至少为 5,然后将 k 及之后的所有类别合并为 “≥ k”。

If p has been estimated from the data, the expected frequencies must be computed using the estimated p̂, and one degree of freedom is lost for the estimation. Always check that after pooling, the number of categories is sufficient to give a valid test.

如果 p 是从数据中估计得到的,必须使用估计值 p̂ 计算期望频数,并且会因估计而损失一个自由度。合并后务必检查类别数目是否足以进行有效检验。


6. Degrees of Freedom | 自由度

meters estimated from the data. For a fully specified geometric distribution, m = 0 and ν = k − 1. If we estimate p, then m = 1, giving ν = k − 2.

卡方检验统计量的自由度为 ν = k − 1 − m,其中 k 为合并后的类别数,m 为从数据中估计的参数个数。对于完全指定的几何分布,m = 0,ν = k − 1。若我们估计了 p,则 m = 1,因此 ν = k − 2。

This adjustment is crucial; failing to subtract for estimated parameters inflates the test statistic and increases the risk of a type I error. In A-level problems, examiners will often specify whether the parameter is known or estimated.

这一调整至关重要;若不扣除估计参数的自由度,会夸大检验统计量,增加第一类错误的风险。在 A-Level 考题中,通常考官会指明参数是已知的还是需要估计的。


广告 / Sponsored

7. Worked Example: Flipping a Fair Coin Until Heads | 案例:抛掷公平硬币直到出现正面

ies are: 1 head: 48, 2 flips: 27, 3: 13, 4: 7, 5: 3, 6: 1, and 7 or more: 1. Test at the 5% significance level whether the data come from a geometric distribution with p = 0.5.

一台计算机模拟公平硬币抛掷 100 次,记录首次得到正面所需的抛掷次数。观测频数为:1 次:48,2 次:27,3 次:13,4 次:7,5 次:3,6 次:1,7 次及以上:1。试在 5% 显著性水平下检验数据是否来自 p = 0.5 的几何分布。

The hypotheses are H₀: X ~ w Geom(0.5). We first compute expected frequencies for each x under p = 0.5, with n = 100.

假设为 H₀:X ~ Geom(0.5),H₁:X 不服从 Geom(0.5)。我们首先在 p = 0.5、n = 100 下计算每个 x 的期望频数。


8. Performing the Chi-Squared Calculation | 执行卡方计算

→ E₅ = 3.125; P(X=6) = 0.015625 → E₆ = 1.5625; P(X≥7) = (0.5)⁶ = 0.015625 → E₇₊ = 1.5625.

概率与原始期望值:P(X=1) = 0.5 → E₁ = 50;P(X=2) = 0.25 → E₂ = 25;P(X=3) = 0.125 → E₃ = 12.5;P(X=4) = 0.0625 → E₄ = 6.25;P(X=5) = 0.03125 → E₅ = 3.125;P(X=6) = 0.015625 → E₆ = 1.5625;P(X≥7) = (0.5)⁶ = 0.015625 → E₇₊ = 1.5625。

expected frequencies for 5, 6 and 7+ are all below 5, we pool them into a single category "≥ 5" cted E₅₊ = 3.125 + 1.5625 + 1.5625 = 6.25. The final categories are x=1,2,3,4,≥5 with k=5.

由于 5、6 和 7 及以上的期望频数均低于 5,我们将它们合并为单一类别 “≥ 5”。合并后观测值 O₅₊ = 3 + 1 + 1 = 5,合并期望值 E₅₊ = 3.125 + 1.5625 + 1.5625 = 6.25。最终类别为 x=1, 2, 3, 4, ≥5,k = 5。

Category xObserved OExpected E(O − E)² / E
148500.08
227250.16
31312.50.02
476.250.09
≥556.250.25

Summing the final column gives χ² = 0.08 + 0.16 + 0.02 + 0.09 + 0.25 = 0.60.

将最后一列相加,得到 χ² = 0.08 + 0.16 + 0.02 + 0.09 + 0.25 = 0.60。


9. Critical Value and Conclusion | 临界值与结论

0.5 is specified and no parameter is estimated, degrees of freedom ν = k − 1 = 4. At the 5% significance level, the critical value from χ² tables with 4 degrees of freedom is 9.488.

由于 p=0.5 已指定且未进行参数估计,自由度 ν = k − 1 = 4。在 5% 显著性水平下,自由度为 4 的 χ² 表临界值为 9.488。

geometric distribution with p=0.5. The simulation seems consistent with a fair coin model.

由于 0.60 < 9.488,我们不拒绝 H₀。没有足够证据表明数据不服从 p=0.5 的几何分布。该模拟结果与公平硬币模型相符。


10. Conditions and Assumptions | 条件与假设

The chi-squared test relies on several conditions: (i) the data are a random sample; (ii) the categories are mutually exclusive; (iii) expected frequencies are at least 5 (or no more than 20% below 5); (iv) the test statistic approximately follows the χ² distribution under H₀. When estimating p, the method used (e.g. MLE) must be appropriate, and degrees of freedom must be correctly adjusted.

卡方检验依赖若干条件:(i) 数据来自随机样本;(ii) 类别互斥;(iii) 期望频数不低于 5(或低于 5 的不超过 20%);(iv) 在 H₀ 下检验统计量近似服从 χ² 分布。当估计 p 时,所用方法(如最大似然估计)必须恰当,且自由度必须正确调整。

In computational experiments, ensuring independence and correct random number generation is vital. Violations can lead to spurious test results.

在计算实验中,确保独立性与正确的随机数生成至关重要。违反这些条件可能导致检验结果失真。


11. Computational Implementation in Python | Python 计算实现

Here is a Python example that automates the chi-squared goodness-of-fit test for the geometric distribution using scipy.stats. We use the observed frequencies from the worked example and compare them with the expected probabilities from geom.pmf.

以下是一个 Python 示例,利用 scipy.stats 自动完成几何分布的卡方拟合优度检验。我们使用前述案例中的观测频数,并与 geom.pmf 计算出的期望概率进行对比。

import numpy as np
from scipy.stats import chisquare, geom

# Observed frequencies (including tail category after pooling)
observed = np.array([48, 27, 13, 7, 5])
# Corresponding x values (categories: 1,2,3,4,5+)
# We treat the last category as '5 or more'
p = 0.5
n = sum(observed)

# Calculate probabilities for x=1,2,3,4
probs = [geom.pmf(x, p) for x in range(1,5)]
# Probability for x>=5
prob_tail = 1 - sum(probs)
probs.append(prob_tail)

expected = np.array(probs) * n
chi2_stat, pvalue = chisquare(fobs=observed, fexp=expected)
print(f"Chi-squared statistic: {chi2_stat:.3f}")
print(f"P-value: {pvalue:.4f}")

这段代码计算了卡方统计量和 P 值。chisquare 函数自动检验类别期望值是否满足要求。输出的 P 值可与显著性水平 α 比较,从而得出结论。


12. Summary and Exam Tips | 总结与应试技巧

Applying a goodness-of-fit test to a geometric distribution involves defining the distribution, pooling sparse categories, calculating expected frequencies, computing the χ² statistic, determining the correct degrees of freedom, and comparing the statistic with a critical value or using the p‑value. Always state hypotheses clearly and show all working. If p is unknown, remember to estimate it and reduce degrees of freedom by one. When coding, utilise library functions but understand the underlying statistical logic.

对几何分布应用拟合优度检验的步骤包括:定义分布、合并稀疏类别、计算期望频数、计算 χ² 统计量、确定正确的自由度,并将统计量与临界值或 P 值进行对比。务必清晰陈述假设并展示完整过程。若 p 未知,记得估计它并将自由度减一。在编程时,可以利用库函数,但必须理解背后的统计逻辑。

Published by TutorHao | Computer Science Revision Series | aleveler.com

广告 / Sponsored
现货 Cambridge International AS & ALevel IT Coursebook剑桥国际IGC…
计算机 备考教辅 · 正版现货 · 与本文内容相关
查看详情 / View on Taobao参考价 ¥467.0

更多咨询请联系16621398022(同微信)

备考资讯每日更新,关注不错过
考纲变化 · 科目分析 · 学习方法
扫码关注公众号 tutorhao
在微信里长按二维码识别关注
或截图后用「扫一扫 → 相册」选择本图
公众号:tutorhao