📚 Applying Goodness-of-Fit Tests to Geometric Distributions in Computer Science | 计算机科学中对几何分布应用拟合优度检验
In computer science simulations, we often model waiting times as geometric: the number of attempts until a packet is acknowledged, the number of draws until a rare event occurs, or the number of retries before a successful login. Before trusting such a model, we can test it using a chi-square goodness-of-fit test. This article explains how to apply the test to a geometric distribution, using computing-related examples throughout.
在计算机科学模拟中,我们常将等待时间建模为几何分布:数据包被确认前的尝试次数、罕见事件发生前的抽样次数、或成功登录前的重试次数。在对模型产生信任之前,我们可以使用卡方拟合优度检验进行检验。本文讲解如何对几何分布应用该检验,并使用计算相关的例子。
1. Why Geometric Distributions Matter in Computer Science | 为什么几何分布在计算机科学中重要
The geometric distribution describes the number of independent trials needed to obtain the first success. In computer science, this appears in network retransmission, login attempts, cache miss patterns, and Monte Carlo simulations that run until an event fires. A goodness-of-fit test allows us to check whether observed data really follow the assumed geometric law before we rely on further predictions.
几何分布描述了获得第一次成功所需的独立试验次数。在计算机科学中,它出现在网络重传、登录尝试、缓存未命中模式以及一直运行到某个事件发生的蒙特卡洛模拟中。拟合优度检验使我们可以检查观测数据是否真的服从假设的几何规律,然后再依赖进一步的预测。
For example, if a simulation claims that a packet needs on average 3 attempts before the first acknowledgement, we can record many simulated runs and test whether the counts 1, 2, 3, … match a geometric distribution with p = 1/3.
例如,如果一个模拟声称一个数据包平均需要 3 次尝试才能获得第一次确认,我们可以记录许多次模拟运行,并检验计数 1、2、3、… 是否与 p = 1/3 的几何分布相匹配。
2. Geometric Distribution: Key Facts | 几何分布:关键事实
In the Edexcel A-Level Statistics convention, a geometric random variable X counts the number of trials up to and including the first success. Its probability mass function is:
在 Edexcel A-Level 统计学的约定中,几何随机变量 X 计算直到第一次成功为止的试验次数。其概率质量函数为:
P(X = x) = p(1 − p)ˣ⁻¹, x = 1, 2, 3, …
The mean and variance are:
均值和方差为:
E(X) = 1 / p
Var(X) = (1 − p) / p²
The cumulative probability is P(X ≤ k) = 1 − (1 − p)ᵏ, and the tail probability is P(X > k) = (1 − p)ᵏ. If p is not given, we can estimate it from the sample mean using p̂ = 1 / x̄.
累积概率为 P(X ≤ k) = 1 − (1 − p)ᵏ,尾概率为 P(X > k) = (1 − p)ᵏ。如果 p 未给出,我们可以使用样本均值通过 p̂ = 1 / x̄ 来估计它。
Some programming libraries use the alternative convention where X counts failures before the first success, so X = 0, 1, 2, …. If that version has mean (1 − p)/p, then Y = X + 1 follows the Edexcel convention. Always check which definition is used in your data.
一些编程库使用另一种约定,即 X 计算第一次成功前的失败次数,因此 X = 0, 1, 2, …。如果该版本的均值为 (1 − p)/p,则 Y = X + 1 遵循 Edexcel 的约定。务必检查你的数据使用的是哪种定义。
3. Goodness-of-Fit Test: The Core Idea | 拟合优度检验:核心思想
A chi-square goodness-of-fit test compares observed frequencies O with expected frequencies E calculated under the null hypothesis. The test statistic is:
卡方拟合优度检验将观测频率 O 与在原假设下计算出的期望频率 E 进行比较。检验统计量为:
χ² = Σ (O − E)² / E
If the geometric model fits well, O and E should be close, so χ² will be small. If χ² is large, the observed data are far from what the geometric model predicts, and we may reject the model. Under suitable conditions, the statistic approximately follows a χ² distribution.
如果几何模型拟合良好,O 和 E 应当接近,因此 χ² 会很小。如果 χ² 很大,则观测数据与几何模型预测的结果相差很远,我们可以拒绝该模型。在适当条件下,该统计量近似服从 χ² 分布。
For the approximation to be reliable, expected frequencies must be sufficiently large. A common rule is that every expected frequency should be at least 5.
为了使近似可靠,期望频率必须足够大。常见规则是每个期望频率至少为 5。
4. Stating the Hypotheses | 陈述假设
When testing a geometric distribution, the null hypothesis states that the data come from a geometric distribution. If p is specified, we write:
检验几何分布时,原假设指出数据来自几何分布。如果 p 已指定,我们写:
H₀: X follows Geom(p = p₀)
H₁: X does not follow Geom(p = p₀)
If p is not specified, the null hypothesis is simply that X follows some geometric distribution, and p is estimated from the data. The alternative hypothesis is that the data are not geometric.
如果 p 未指定,原假设只是 X 服从某个几何分布,p 由数据估计。备择假设是数据不服从几何分布。
For example, a computer simulation may claim that a successful login has a probability p = 0.4 per attempt. Then H₀ is that the number of attempts follows Geom(0.4).
例如,一个计算机模拟可能声称每次尝试成功登录的概率为 p = 0.4。那么 H₀ 就是尝试次数服从 Geom(0.4)。
5. Estimating p When It Is Not Given | 当 p 未给出时如何估计 p
If p is not specified, we estimate it from the sample. For the Edexcel geometric distribution with support X = 1, 2, 3, …, the maximum likelihood estimator of p is:
如果 p 未指定,我们从样本中估计它。对于支持集为 X = 1, 2, 3, … 的 Edexcel 几何分布,p 的最大似然估计量为:
p̂ = 1 / x̄
This comes from maximizing the likelihood L(p) = ∏ p(1 − p)ˣⁱ⁻¹. When data are grouped, use the midpoint of each class as an approximate x-value for the grouped tail class, although raw data are preferable.
这来自最大化似然函数 L(p) = ∏ p(1 − p)ˣⁱ⁻¹。当数据分组时,使用每个类的中值作为分组尾部类的近似 x 值,不过原始数据更可取。
If the alternative convention with support X = 0, 1, 2, … is used, the estimator becomes p̂ = 1 / (x̄ + 1). Converting the random variable to Y = X + 1 avoids confusion.
如果使用支持集为 X = 0, 1, 2, … 的另一种约定,估计量变为 p̂ = 1 / (x̄ + 1)。将随机变量转换为 Y = X + 1 可以避免混淆。
6. Calculating Expected Frequencies and Merging Classes | 计算期望频率与合并类别
Once p is known or estimated, the expected frequency for each value or class is:
一旦 p 已知或被估计,每个值或类别的期望频率为:
Eᵢ = n × P(X = i)
For a geometric tail such as X ≥ 6, use P(X ≥ 6) = (1 − p)⁵. If any expected frequency is less than 5, merge adjacent classes until all expected frequencies are at least 5. The observed frequencies must be merged in the same way.
Published by TutorHao | A-Level Computer Science Revision Series | aleveler.com更多咨询请联系16621398022(同微信)
屏轩国际教育cambridge primary/secondary checkpoint, cat4, ukiset,ukcat,igcse,alevel,PAT,STEP,MAT, ibdp,ap,ssat,sat,sat2课程辅导,国外大学本科硕士研究生博士课程论文辅导Cancel reply