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

Testing the Goodness of Fit with Discrete Data | 离散数据的拟合优度检验

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

📚 Testing the Goodness of Fit with Discrete Data | 离散数据的拟合优度检验

In A-Level Computer Science, we often work with discrete data—categorical outcomes, counts, or classifications. Whether you are evaluating a random number generator or checking if a machine learning model produces expected class distributions, the chi-squared test for goodness of fit is a vital statistical tool. This article explores how to test the goodness of fit with discrete data, linking theoretical concepts to real computing applications on the Edexcel specification.

在 A-Level 计算机科学中,我们经常处理离散数据——类别型结果、计数或分类。无论是评估随机数生成器,还是检查机器学习模型是否产生期望的类别分布,卡方拟合优度检验都是重要的统计工具。本文探讨如何使用离散数据检验拟合优度,并将理论概念与 Edexcel 大纲中的真实计算应用联系起来。


1. Understanding Goodness of Fit | 理解拟合优度

Goodness of fit measures how well an observed set of discrete outcomes matches a theoretical probability distribution. For instance, if you toss a fair die 60 times, you expect each face to appear about 10 times. A goodness-of-fit test tells you whether the deviation from this expectation is due to chance or indicates the die is biased.

拟合优度衡量一组观测到的离散结果与理论概率分布的吻合程度。例如,投掷一枚公平骰子 60 次,你期望每个面大约出现 10 次。拟合优度检验可以告诉你,偏离期望值是由偶然因素造成的,还是表明骰子有偏倚。

In computing, goodness of fit helps validate simulations, verify randomness in cryptographic contexts, and assess the performance of classifiers. It is a foundational technique for testing hypotheses about discrete data.

在计算领域,拟合优度有助于验证模拟、在密码学场景中验证随机性,并评估分类器的性能。它是检验离散数据假设的基础技术。


2. Why Discrete Data Matters in Computing | 离散数据在计算科学中的重要性

Discrete data in computing takes forms such as binary success/failure flags, enum values, hash outputs, or pixel colour quantisations. Unlike continuous measurements, discrete values fall into distinct categories, making chi-squared based methods particularly appropriate.

计算中的离散数据形式包括二进制的成功/失败标志、枚举值、哈希输出或像素颜色量化。与连续测量不同,离散值落在明确的类别中,这使得基于卡方的方法特别适用。

Understanding distribution shapes of discrete variables is crucial in algorithm analysis: random number generators must produce uniform distributions; load balancers should distribute requests evenly across servers; and feature selection in AI often involves testing whether categories are independent.

理解离散变量分布形状在算法分析中至关重要:随机数生成器必须产生均匀分布;负载均衡器应使请求均匀分布在各服务器;人工智能中的特征选择常需要检验类别是否独立。


3. The Chi-Squared Test: An Overview | 卡方检验概述

The chi-squared (χ²) goodness-of-fit test compares observed frequencies with expected frequencies under a null hypothesis. The test statistic measures the cumulative discrepancy across categories.

卡方(χ²)拟合优度检验将观测频率与零假设下的期望频率进行比较。检验统计量衡量各类别间累积的差异。

The formula for the statistic is:

χ² = Σ ((Oi - Ei)² / Ei)

where Oi is the observed count in category i, and Ei is the expected count. The sum is taken over all k categories.

其中 Oi 是第 i 个类别的观测频数,Ei 是期望频数。总和涵盖所有 k 个类别。

If the data perfectly match the expected distribution, χ² is close to 0. The larger the value, the stronger the evidence against the null hypothesis.

如果数据与期望分布完全吻合,χ² 接近 0。数值越大,对零假设不利的证据就越强。


4. Expected Frequencies and Hypotheses | 期望频率与假设

The null hypothesis (H₀) states that the data follow a specified discrete distribution. The alternative hypothesis (H₁) states that the data do not follow that distribution.

零假设(H₀)声明数据服从特定的离散分布。备择假设(H₁)声明数据不服从该分布。

Expected frequencies must be computed based on the hypothesised model. For a uniform distribution across k categories with N observations, each Ei robabilities (e.g. 0.5, 0.3, 0.2 for three categories), multiply N by those probabilities.

期望频率必须根据假设模型计算。对于 N 次观测的 k 个类别的均匀分布,每个 Ei = N / k。如果零假设假定特定概率(例如三类分别为 0.5、0.3、0.2),则将 N 乘以这些概率。

A common pitfall is having expected frequencies less than 5, which reduces the validity of the χ² approximation. In computing experiments, pooling categories can resolve this.

常见陷阱是期望频率低于 5,这会降低 χ² 近似的有效性。在计算实验中,合并类别可以解决这一问题。


5. Calculating the Chi-Squared Statistic | 计算卡方统计量

To calculate χ²:

计算 χ² 的步骤:

In programming, this is easily implemented with a loop iterating over categories. Care must be taken to avoid division by zero—if an expected frequency is zero, the test is invalid for that category.

在编程中,这可以通过循环遍历类别轻松实现。必须小心避免除以零——如果某个类别的期望频率为零,则该类别不适合进行检验。


6. Degrees of Freedom and Critical Values | 自由度与临界值

from the data to define the distribution. For a simple uniform test, p = 0, so df = k - 1.

拟合优度检验的自由度(df)为 k - 1 - p,其中 k 是类别数,p 是根据数据估计以确定分布的参数个数。对于简单的均匀性检验,p = 0,因此 df = k - 1。

A higher df makes the χ² distribution more symmetric. Critical values are obtained from the χ² distribution table for a chosen significance level α (commonly 0.05). If χ² > χ²critical, reject H₀.

较高的自由度使 χ² 分布更加对称。临界值可从选定显著性水平 α(通常为 0.05)的 χ² 分布表获得。如果 χ² > χ²临界值,则拒绝 H₀。

In coding exercises, you might use a pre-computed lookup table or library functions to obtain critical values or p-values.

在编程练习中,你可以使用预计算查找表或库函数来获取临界值或 p 值。


广告 / Sponsored

7. Interpreting the Results | 结果解读

If the computed χ² is less than the critical value, we do not have enough evidence to reject the null hypothesis—the discrete data are consistent with the claimed distribution. If χ² exceeds the critical value, we conclude the data deviate significantly from the expected distribution.

如果计算的 χ² 小于临界值,则我们没有足够证据拒绝零假设——离散数据与声称的分布一致。如果 χ² 超过临界值,我们断定数据显著偏离期望分布。

It is crucial to remember that statistical significance does not imply practical significance. A minor deviation might be flagged as significant with a huge sample size, so always contextualise findings within your computing problem.

必须记住,统计显著性并不意味着实际显著性。在样本量巨大时,微小的偏差可能被标记为显著,因此始终要结合具体计算问题来分析结果。


8. Application: Testing Random Number Generators | 应用:测试随机数生成器

Pseudo-random number generators (PRNGs) are fundamental in simulations, games, and cryptography. A goodness-of-fit test can check if a PRNG produces a uniform discrete distribution. For example, test whether digits 0-9 appear equally often over 1000 calls.

伪随机数生成器(PRNG)在模拟、游戏和密码学中至关重要。拟合优度检验可以检查 PRNG 是否产生均匀的离散分布。例如,检验 0-9 这 10 个数字在 1000 次调用中是否出现频率相等。

Table: Observed versus expected digit counts.

DigitOiEi
098100
1105100
292100
3101100

7 might be well below the critical 16.9 at α=0.05, so the PRNG passes the uniformity test.

当类别数为 10 时,自由度为 9。若 χ² 值约为 1.7,远低于 α=0.05 时的临界值 16.9,因此 PRNG 通过了均匀性检验。


9. Application: Evaluating Classification Models | 应用:评估分类模型

When a machine learning classifier predicts discrete labels, you can test whether the distribution of predicted classes matches an expected baseline. For example, in a balanced three-class problem, you expect roughly equal numbers of predictions per class from an unbiased model.

当机器学习分类器预测离散标签时,你可以检验预测类别的分布是否与期望基线相符。例如,在一个均衡的三分类问题中,你期望无偏倚模型各类别的预测数量大致相等。

Observe the confusion matrix row totals; apply the χ² test to these observed frequencies against the expected equal distribution. A significant result might indicate model bias or a problem with training data representation.

观察混淆矩阵的行总和;对这些观测频率与期望的均等分布进行 χ² 检验。显著的结果可能表明模型存在偏差或训练数据代表性有问题。


10. Application: Fairness in Algorithms | 应用:算法公平性检验

Many ethical computing discussions involve fairness checks. If an algorithm makes decisions that assign outcomes (e.g., loan approval, shortlisting) across different demographic groups, a goodness-of-fit test can examine whether the approval rates deviate from a policy-determined expected distribution.

许多伦理计算讨论涉及公平性检查。如果某个算法在做出决策时将结果(如贷款批准、入围)分配给不同人口群体,拟合优度检验可以考察批准率是否偏离政策规定的期望分布。

While the χ² test only assesses statistical deviation, it is a useful first step in auditing algorithmic fairness, complemented by other metrics.

虽然 χ² 检验仅评估统计偏差,但它是对算法公平性进行审计的有用第一步,并可结合其他指标使用。


11. Practical Worked Example | 实例演练

logs 400 errors categorised as 4xx (client) and 5xx (server). Historical data suggests 70% client errors and 30% er errors. Test if the current error distribution matches the historical pattern (α=0.05).

假设一台网络服务器记录 400 个错误,分为 4xx(客户端)和 5xx(服务器)两类。历史数据显示 70% 为客户端错误,30% 为服务器错误。我们记录到 250 个客户端错误和 150 个服务器错误。检验当前错误分布是否符合历史模式(α=0.05)。

Expected: client E₁ = 0.7×400 = 280, server E₂ = 0.3×400 = 120.

期望:客户端 E₁ = 0.7×400 = 280,服务器 E₂ = 0.3×400 = 120。

Compute χ²: (250-280)²/280 + (150-120)²/120 = 900/280 + 900/120 ≈ 3.214 + 7.5 = 10.714.

With df = 2 - 1 = 1, critical value at 0.05 is 3.84. Since 10.714 > 3.84, reject H₀—the error pattern has changed significantly.

自由度 df = 2 - 1 = 1,在 0.05 下的临界值为 3.84。因为 10.714 > 3.84,拒绝 H₀——错误模式已发生显著变化。


12. Common Pitfalls and Extensions | 常见陷阱与延伸

Do not apply the χ² test when expected frequencies are too low; use exact alternatives like the binomial test or Monte Carlo simulation. Also, the test assumes independent observations—when data points are dependent (e.g., consecutive random numbers), additional tests like runs tests are needed.

当期望频率过低时,不要使用 χ² 检验;应使用二项检验或蒙特卡洛模拟等精确替代方法。此外,该检验假设观测值独立——当数据点存在依赖关系时(如连续的随机数),需要运行检验(runs test)等额外检验。

For A-Level computing, the χ² test is a manageable yet powerful technique to integrate statistical thinking into algorithmic assessment and system evaluation. Extensions include testing independence in contingency tables and multinomial testing.

对于 A-Level 计算机科学,χ² 检验是一种易掌握且有强大功能的技术,可将统计思维融入算法评估和系统评价。延伸应用包括列联表独立性检验和多项式检验。

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

广告 / Sponsored
9618考试剑桥A level计算机科学教材Cambridge International AS and A Leve…
计算机 备考教辅 · 正版现货 · 与本文同科同级别
查看详情 / View on Taobao参考价 ¥491.0

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

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