114 lines
3.3 KiB
Python
114 lines
3.3 KiB
Python
"""统计推断:区间估计、假设检验、分布拟合。
|
||||
|
|
|
|||
|
|
原建议里的 `mean_test`/`proportion_test`/`distribution_pdf`/`distribution_cdf`/
|
|||
|
|
`distribution_ppf`/`sample_distribution` 都是 scipy 的一行转发,已删。
|
|||
|
|
需要 pdf/cdf/ppf 时,用 `fit_distribution(...).dist.pdf(...)` 即可。
|
|||
|
|
"""
|
|||
|
|
|
|||
|
|
from __future__ import annotations
|
|||
|
|
|
|||
|
|
from typing import Any
|
|||
|
|
|
|||
|
|
__all__ = [
|
|||
|
|
"anova",
|
|||
|
|
"chi_square_test",
|
|||
|
|
"confidence_interval",
|
|||
|
|
"correlation_matrix",
|
|||
|
|
"fit_distribution",
|
|||
|
|
"goodness_of_fit",
|
|||
|
|
"t_test",
|
|||
|
|
]
|
|||
|
|
|
|||
|
|
|
|||
|
|
def correlation_matrix(data: Any, method: str = "pearson", min_periods: int = 1) -> Any:
|
|||
|
|
"""相关矩阵 + 显著性星标。
|
|||
|
|
|
|||
|
|
Returns
|
|||
|
|
-------
|
|||
|
|
DataFrame,值 = 相关系数;`min_periods` 控制缺失值容差。
|
|||
|
|
|
|||
|
|
Examples
|
|||
|
|
--------
|
|||
|
|
>>> correlation_matrix(df).round(3) # doctest: +SKIP
|
|||
|
|
"""
|
|||
|
|
# TODO(L0)
|
|||
|
|
raise NotImplementedError
|
|||
|
|
|
|||
|
|
|
|||
|
|
def confidence_interval(
|
|||
|
|
x: Any,
|
|||
|
|
confidence: float = 0.95,
|
|||
|
|
method: str = "normal",
|
|||
|
|
) -> tuple[float, float]:
|
|||
|
|
"""均值或比例的置信区间。
|
|||
|
|
|
|||
|
|
Parameters
|
|||
|
|
----------
|
|||
|
|
method:
|
|||
|
|
`"normal"`:`mean ± z·s/√n`(n 大时用);
|
|||
|
|
`"t"`:小样本用 t 分布(更保守,n < 30 建议用这个);
|
|||
|
|
`"proportion"`:比例的标准误 `√(p(1-p)/n)`。
|
|||
|
|
|
|||
|
|
Examples
|
|||
|
|
--------
|
|||
|
|
>>> lo, hi = confidence_interval([1, 2, 3, 4, 5], confidence=0.95)
|
|||
|
|
"""
|
|||
|
|
# TODO(L0)
|
|||
|
|
raise NotImplementedError
|
|||
|
|
|
|||
|
|
|
|||
|
|
def t_test(a: Any, b: Any | None = None, value: float = 0.0, equal_var: bool = False, paired: bool = False, alpha: float = 0.05) -> Any:
|
|||
|
|
"""t 检验。`b=None` 时是单样本检验。
|
|||
|
|
|
|||
|
|
Returns
|
|||
|
|
-------
|
|||
|
|
dict:`{"statistic", "pvalue", "df", "significant", "conclusion"}`。
|
|||
|
|
`conclusion` 是一句能直接抄进论文的中文结论。
|
|||
|
|
"""
|
|||
|
|
# TODO(L1)
|
|||
|
|
raise NotImplementedError
|
|||
|
|
|
|||
|
|
|
|||
|
|
def anova(*groups: Any, alpha: float = 0.05) -> Any:
|
|||
|
|
"""单因素方差分析:多组均值是否相同。
|
|||
|
|
|
|||
|
|
Returns
|
|||
|
|
-------
|
|||
|
|
dict:`{"F", "pvalue", "conclusion"}`。
|
|||
|
|
"""
|
|||
|
|
# TODO(L1)
|
|||
|
|
raise NotImplementedError
|
|||
|
|
|
|||
|
|
|
|||
|
|
def chi_square_test(observed: Any, expected: Any | None = None, alpha: float = 0.05) -> Any:
|
|||
|
|
"""卡方检验:观察频数与期望频数是否一致(分类变量的独立性 / 拟合优度)。"""
|
|||
|
|
# TODO(L1)
|
|||
|
|
raise NotImplementedError
|
|||
|
|
|
|||
|
|
|
|||
|
|
def fit_distribution(x: Any, candidates: tuple[str, ...] = ("norm", "lognorm", "gamma", "expon", "uniform")) -> Any:
|
|||
|
|
"""对候选分布逐个拟合,按 AIC(或 KS 统计量)排序。
|
|||
|
|
|
|||
|
|
Returns
|
|||
|
|
-------
|
|||
|
|
`Result`:`data` 是排好序的候选表;`params["best"]` 是最优分布名,
|
|||
|
|
`extras["dist"]` 是可直接调 `.pdf / .cdf / .ppf / `.rvs` 的 scipy 分布对象。
|
|||
|
|
|
|||
|
|
Notes
|
|||
|
|
-----
|
|||
|
|
这是"反正态题"的标准做法:先看图 → 候选分布拟合 → 比较 AIC → 做 KS 检验。
|
|||
|
|
|
|||
|
|
Examples
|
|||
|
|
--------
|
|||
|
|
>>> r = fit_distribution(data) # doctest: +SKIP
|
|||
|
|
>>> r.params["best"] # doctest: +SKIP
|
|||
|
|
'lognorm'
|
|||
|
|
"""
|
|||
|
|
# TODO(L1)
|
|||
|
|
raise NotImplementedError
|
|||
|
|
|
|||
|
|
|
|||
|
|
def goodness_of_fit(x: Any, dist: str = "norm", method: str = "ks") -> Any:
|
|||
|
|
"""拟合优度检验:`"ks"`(连续)/ `"chi2"`(分箱)。"""
|
|||
|
|
# TODO(L1)
|
|||
|
|
raise NotImplementedError
|