Files
MCM-ICM_Toolkit/src/mcm_toolkit/stats/__init__.py
T
guzhujushi 95f5266563 feat(stats): correlation_matrix 按方案 A 拆成三个函数,收口返回契约(L0 9/12)
用户 2026-10-03 拍板方案 A(见决策 41),由 Codex 落地。

拆成三个函数:
- correlation_matrix  -> 数值系数矩阵(对角线 1.0)
- correlation_pvalues -> 数值 p 值矩阵(对角线 NaN)
- correlation_table   -> 星标字符串表(对角线 1.000 不带星)
三者共用 _check_corr_args 守卫与成对删缺失口径;correlation_table 在最外层调另外两个。
理由: 字符串表做不了后续计算(挑高相关对、喂聚类/PCA、画热力图),反过来随时能格式化;
      一旦返回字符串,数值信息不可逆地丢失。

顺带修掉两处:
- 对角线 p 值由 0.0 改成 NaN —— 写 0 会被下游读成"极显著",而"自己和自己"没有 p 值
- p 值与 r 现在用同一批观测: 先成对 dropna() 再算,并同样尊重 min_periods。
  此前 corr 是成对删缺失、pearsonr 不删,两者样本量不同,r 与 p 对不上
- p 值只算上三角再镜像(成对变量的 p 天然对称),省一半重复计算

tests: test_correlation_matrix_diagonal_is_one 摘掉 xfail 转正;新增 8 条
  (数值性+对称性、非数值列跳过、p 值对照解析式 t = r*sqrt((n-2)/(1-r^2))、
   对角线 NaN、成对删缺失口径、min_periods、对角线无星标、表格与数值输出一致)。
  测试 105 passed / 38 skipped / 1 xfail -> 114 passed / 38 skipped / 0 xfail。

验证: pytest 114 passed / 38 skipped;ruff All checks passed;pyright 0 errors。

docs: STATUS.md(L0 9/12 + 决策 41 + 下一步);CHANGELOG;
      personal-pitfalls 追加第 14 条(把"能看的表"和"能算的表"塞进同一个返回值);
      roadmap 记录 correlation_matrix 的两个伴随函数
2026-10-03 14:53:05 +08:00

37 lines
888 B
Python

"""stats —— 描述统计、统计推断、bootstrap。
S 级模块。`bootstrap_ci` 建议自己实现一遍(S 档,配完整函数卡片):
它是赛题里"没有解析解时怎么给区间"的通用答案。
"""
from .bootstrap import bootstrap_ci, bootstrap_statistic
from .descriptive import coefficient_of_variation, describe, zscore
from .inference import (
anova,
chi_square_test,
confidence_interval,
correlation_matrix,
correlation_pvalues,
correlation_table,
fit_distribution,
goodness_of_fit,
t_test,
)
__all__ = [
"describe",
"zscore",
"coefficient_of_variation",
"correlation_matrix",
"correlation_pvalues",
"correlation_table",
"confidence_interval",
"t_test",
"anova",
"chi_square_test",
"fit_distribution",
"goodness_of_fit",
"bootstrap_statistic",
"bootstrap_ci",
]