SPB Git

Repository Public MIT

gen_inference.py 12.3 KB 171 lines python

1#!/usr/bin/env python32# ORIX Calc3# File: Packages/ORIXStatistics/Reference/gen_inference.py4# Description: Generates confidence-interval and hypothesis-test references with scipy.stats (and statsmodels where scipy has none).5#6# Author: Simon-Pierre Boucher7# Contact: contact@spboucher.ai8import math9import numpy as np10import scipy.stats as st11import statsmodels.stats.proportion as smp12import statsmodels.stats.weightstats as smw13from reflib import write1415rng = np.random.default_rng(4242)16ALTS = {"twoSided": "two-sided", "less": "less", "greater": "greater"}17cases = []18F = float192021def lst(a):22    return [F(v) for v in a]232425# ---- one-sample t, paired t, one-sample z --------------------------------------------------------------------------26samples = {"n8": rng.normal(50, 8, 8), "n25": rng.normal(3.1, 0.4, 25), "n120": rng.exponential(2.0, 120)}27for name, x in samples.items():28    mu0 = F(np.round(x.mean() - 0.3 * x.std(ddof=1), 3))29    for alt, salt in ALTS.items():30        for alpha in (0.05, 0.01):31            r = st.ttest_1samp(x, mu0, alternative=salt)32            ci = st.ttest_1samp(x, mu0).confidence_interval(1 - alpha)33            df = len(x) - 134            crit = {"twoSided": st.t.isf(alpha / 2, df), "less": st.t.ppf(alpha, df), "greater": st.t.isf(alpha, df)}[alt]35            cases.append({"kind": "oneSampleT", "data": lst(x), "mu0": mu0, "alternative": alt, "alpha": alpha, "statistic": F(r.statistic),36                          "df": F(df), "p": F(r.pvalue), "critical": F(crit), "ciLower": F(ci.low), "ciUpper": F(ci.high),37                          "cohenD": F((x.mean() - mu0) / x.std(ddof=1))})38            sigma = 1.739            z = (x.mean() - mu0) / (sigma / math.sqrt(len(x)))40            p = {"twoSided": 2 * st.norm.sf(abs(z)), "less": st.norm.cdf(z), "greater": st.norm.sf(z)}[alt]41            zc = st.norm.isf(alpha / 2)42            zstat, zp = smw.ztest(x, value=mu0, alternative={"twoSided": "two-sided", "less": "smaller", "greater": "larger"}[alt], ddof=1)43            cases.append({"kind": "oneSampleZ", "data": lst(x), "sigma": sigma, "mu0": mu0, "alternative": alt, "alpha": alpha, "statistic": F(z), "p": F(p),44                          "critical": F({"twoSided": zc, "less": st.norm.ppf(alpha), "greater": st.norm.isf(alpha)}[alt]),45                          "ciLower": F(x.mean() - zc * sigma / math.sqrt(len(x))), "ciUpper": F(x.mean() + zc * sigma / math.sqrt(len(x)))})4647# ---- two-sample t (pooled & Welch), from data and from stats ---------------------------------------------------------48pairs = [(rng.normal(10, 2, 12), rng.normal(11.5, 2, 15)), (rng.normal(100, 5, 40), rng.normal(98, 15, 9)), (rng.normal(0, 1, 5), rng.normal(0.2, 1, 6))]49for a, b in pairs:50    for pooled in (True, False):51        for alt, salt in ALTS.items():52            alpha = 0.0553            r = st.ttest_ind(a, b, equal_var=pooled, alternative=salt)54            r2 = st.ttest_ind(a, b, equal_var=pooled)55            ci = r2.confidence_interval(1 - alpha)56            crit = {"twoSided": st.t.isf(alpha / 2, r2.df), "less": st.t.ppf(alpha, r2.df), "greater": st.t.isf(alpha, r2.df)}[alt]57            sp = math.sqrt(((len(a) - 1) * a.var(ddof=1) + (len(b) - 1) * b.var(ddof=1)) / (len(a) + len(b) - 2))58            fs = st.ttest_ind_from_stats(a.mean(), a.std(ddof=1), len(a), b.mean(), b.std(ddof=1), len(b), equal_var=pooled, alternative=salt)59            assert abs(fs.pvalue - r.pvalue) < 1e-1260            cases.append({"kind": "twoSampleT", "a": lst(a), "b": lst(b), "pooled": pooled, "alternative": alt, "alpha": alpha,61                          "statistic": F(r.statistic), "df": F(r2.df), "p": F(r.pvalue), "critical": F(crit), "ciLower": F(ci.low), "ciUpper": F(ci.high),62                          "cohenD": F((a.mean() - b.mean()) / sp),63                          "summary": [len(a), F(a.mean()), F(a.std(ddof=1)), len(b), F(b.mean()), F(b.std(ddof=1))]})64    # two-sample z with known sigmas65    s1, s2 = 2.0, 3.566    se = math.sqrt(s1 ** 2 / len(a) + s2 ** 2 / len(b)); z = (a.mean() - b.mean()) / se67    cases.append({"kind": "twoSampleZ", "m1": F(a.mean()), "s1": s1, "n1": len(a), "m2": F(b.mean()), "s2": s2, "n2": len(b),68                  "statistic": F(z), "p": F(2 * st.norm.sf(abs(z))), "pLess": F(st.norm.cdf(z)), "pGreater": F(st.norm.sf(z)),69                  "ciLower": F(a.mean() - b.mean() - st.norm.isf(0.025) * se), "ciUpper": F(a.mean() - b.mean() + st.norm.isf(0.025) * se)})7071# ---- paired t ---------------------------------------------------------------------------------------------------------72before = rng.normal(140, 12, 14); after = before - rng.normal(4, 6, 14)73for alt, salt in ALTS.items():74    r = st.ttest_rel(before, after, alternative=salt)75    ci = st.ttest_rel(before, after).confidence_interval(0.95)76    d = before - after77    cases.append({"kind": "pairedT", "a": lst(before), "b": lst(after), "alternative": alt, "statistic": F(r.statistic), "df": F(len(d) - 1),78                  "p": F(r.pvalue), "ciLower": F(ci.low), "ciUpper": F(ci.high), "cohenDz": F(d.mean() / d.std(ddof=1))})7980# ---- proportions ------------------------------------------------------------------------------------------------------81for x, n, p0 in [(7, 20, 0.5), (0, 15, 0.1), (15, 15, 0.9), (412, 1000, 0.4), (3, 250, 0.02), (58, 100, 0.5)]:82    for level in (0.9, 0.95, 0.99):83        z = st.norm.isf((1 - level) / 2); ph = x / n84        bt = st.binomtest(x, n, p0)85        wil = bt.proportion_ci(level, method="wilson"); ex = bt.proportion_ci(level, method="exact")86        ac = smp.proportion_confint(x, n, alpha=1 - level, method="agresti_coull")87        wald = (ph - z * math.sqrt(ph * (1 - ph) / n), ph + z * math.sqrt(ph * (1 - ph) / n))88        sw = smp.proportion_confint(x, n, alpha=1 - level, method="normal")89        assert max(abs(sw[0] - max(wald[0], 0)), abs(sw[1] - min(wald[1], 1))) < 1e-12     # statsmodels clips the Wald limits to [0, 1]90        cases.append({"kind": "proportionCI", "x": x, "n": n, "level": level, "wilson": lst([wil.low, wil.high]), "exact": lst([ex.low, ex.high]),91                      "agrestiCoull": lst(ac), "wald": lst(wald)})92    for alt, salt in ALTS.items():93        ph = x / n; se0 = math.sqrt(p0 * (1 - p0) / n); z = (ph - p0) / se094        p = {"twoSided": 2 * st.norm.sf(abs(z)), "less": st.norm.cdf(z), "greater": st.norm.sf(z)}[alt]95        sz, sp = smp.proportions_ztest(x, n, value=p0, alternative={"twoSided": "two-sided", "less": "smaller", "greater": "larger"}[alt], prop_var=p0)96        assert abs(sz - z) < 1e-10 and abs(sp - p) < 1e-1097        c = 0.5 / n; num = 0.0 if abs(ph - p0) <= c else (ph - p0 - math.copysign(c, ph - p0)); zc = num / se098        pc = {"twoSided": 2 * st.norm.sf(abs(zc)), "less": st.norm.cdf(zc), "greater": st.norm.sf(zc)}[alt]99        cases.append({"kind": "oneProportionZ", "x": x, "n": n, "p0": p0, "alternative": alt, "statistic": F(z), "p": F(p),100                      "statisticCC": F(zc), "pCC": F(pc), "exactP": F(st.binomtest(x, n, p0, alternative=salt).pvalue),101                      "cohenH": F(2 * math.asin(math.sqrt(ph)) - 2 * math.asin(math.sqrt(p0)))})102103for x1, n1, x2, n2 in [(45, 100, 30, 90), (8, 40, 15, 42), (510, 1000, 470, 1100)]:104    p1, p2 = x1 / n1, x2 / n2; pp = (x1 + x2) / (n1 + n2)105    se = math.sqrt(pp * (1 - pp) * (1 / n1 + 1 / n2)); z = (p1 - p2) / se106    sz, sp = smp.proportions_ztest([x1, x2], [n1, n2])107    assert abs(sz - z) < 1e-10108    seu = math.sqrt(p1 * (1 - p1) / n1 + p2 * (1 - p2) / n2); zc = st.norm.isf(0.025)109    cases.append({"kind": "twoProportionZ", "x1": x1, "n1": n1, "x2": x2, "n2": n2, "statistic": F(z), "p": F(2 * st.norm.sf(abs(z))),110                  "pLess": F(st.norm.cdf(z)), "pGreater": F(st.norm.sf(z)), "ciLower": F(p1 - p2 - zc * seu), "ciUpper": F(p1 - p2 + zc * seu)})111112# ---- chi-square -------------------------------------------------------------------------------------------------------113for obs, props, ddof in [([18, 22, 30, 15, 25, 10], None, 0), ([43, 52, 54, 40, 61], [0.2, 0.2, 0.25, 0.15, 0.2], 0),114                         ([12, 3, 2, 30], [0.3, 0.05, 0.05, 0.6], 0), ([20, 35, 30, 15], [0.15, 0.35, 0.35, 0.15], 1)]:115    n = sum(obs)116    exp = [n / len(obs)] * len(obs) if props is None else [n * p for p in props]117    r = st.chisquare(obs, exp, ddof=ddof)118    df = len(obs) - 1 - ddof119    cases.append({"kind": "gof", "observed": lst(obs), "proportions": props, "ddof": ddof, "statistic": F(r.statistic), "df": F(df), "p": F(r.pvalue),120                  "expected": lst(exp), "critical": F(st.chi2.isf(0.05, df)), "cohenW": F(math.sqrt(r.statistic / n))})121for table in [[[12, 5], [7, 14]], [[30, 12, 8], [14, 25, 11]], [[5, 0, 3], [2, 9, 1], [4, 4, 12], [10, 2, 2]], [[120, 90], [80, 110]], [[3, 1], [1, 3]]]:122    t = np.array(table, dtype=float)123    for yates in (False, True):124        r = st.chi2_contingency(t, correction=yates)125        applied = yates and t.shape == (2, 2)126        v = math.sqrt(r.statistic / (t.sum() * (min(t.shape) - 1)))127        if not yates:128            assert abs(v - st.contingency.association(t.astype(int), method="cramer")) < 1e-12129        cases.append({"kind": "independence", "table": [lst(row) for row in t], "yates": yates, "yatesApplied": applied, "statistic": F(r.statistic),130                      "df": F(r.dof), "p": F(r.pvalue), "expected": [lst(row) for row in r.expected_freq], "cramersV": F(v),131                      "critical": F(st.chi2.isf(0.05, r.dof))})132133# ---- ANOVA ------------------------------------------------------------------------------------------------------------134for groups in [[rng.normal(20, 4, 8), rng.normal(23, 4, 10), rng.normal(19, 4, 7)],135               [rng.normal(5, 1, 30), rng.normal(5.2, 1.5, 25), rng.normal(5.1, 0.8, 40), rng.normal(6.0, 1.2, 12)],136               [np.array([1.0, 2.0, 3.0]), np.array([2.0, 4.0]), np.array([10.0, 11.0, 12.5, 9.0])]]:137    r = st.f_oneway(*groups)138    allv = np.concatenate(groups); gm = allv.mean()139    ssb = sum(len(g) * (g.mean() - gm) ** 2 for g in groups); ssw = sum(((g - g.mean()) ** 2).sum() for g in groups)140    k, n = len(groups), len(allv)141    cases.append({"kind": "anova", "groups": [lst(g) for g in groups], "statistic": F(r.statistic), "p": F(r.pvalue), "df1": F(k - 1), "df2": F(n - k),142                  "ssb": F(ssb), "ssw": F(ssw), "msb": F(ssb / (k - 1)), "msw": F(ssw / (n - k)), "etaSquared": F(ssb / (ssb + ssw)),143                  "critical": F(st.f.isf(0.05, k - 1, n - k)),144                  "summaries": [[len(g), F(g.mean()), F(g.std(ddof=1))] for g in groups]})145146# ---- F test for two variances, variance CI ---------------------------------------------------------------------------147for a, b in pairs:148    f = a.var(ddof=1) / b.var(ddof=1); d1, d2 = len(a) - 1, len(b) - 1149    lo, hi = st.f.cdf(f, d1, d2), st.f.sf(f, d1, d2)150    cases.append({"kind": "fTest", "a": lst(a), "b": lst(b), "statistic": F(f), "df1": F(d1), "df2": F(d2), "p": F(min(1, 2 * min(lo, hi))),151                  "pLess": F(lo), "pGreater": F(hi), "criticalUpper": F(st.f.isf(0.025, d1, d2)), "criticalLower": F(st.f.ppf(0.025, d1, d2)),152                  "ciLower": F(f / st.f.isf(0.025, d1, d2)), "ciUpper": F(f / st.f.ppf(0.025, d1, d2))})153for name, x in samples.items():154    for level in (0.9, 0.95, 0.99):155        n = len(x); s2 = x.var(ddof=1); al = 1 - level156        cases.append({"kind": "varianceCI", "data": lst(x), "level": level, "lower": F((n - 1) * s2 / st.chi2.isf(al / 2, n - 1)),157                      "upper": F((n - 1) * s2 / st.chi2.ppf(al / 2, n - 1)),158                      "tLower": F(x.mean() - st.t.isf(al / 2, n - 1) * math.sqrt(s2 / n)), "tUpper": F(x.mean() + st.t.isf(al / 2, n - 1) * math.sqrt(s2 / n))})159160# ---- correlation ------------------------------------------------------------------------------------------------------161for n, slope in [(12, 0.8), (40, 0.2), (6, -1.5), (200, 0.05)]:162    x = rng.normal(0, 1, n); y = slope * x + rng.normal(0, 1, n)163    r = st.pearsonr(x, y)164    ci = r.confidence_interval(0.95)165    cases.append({"kind": "correlation", "x": lst(x), "y": lst(y), "r": F(r.statistic), "df": F(n - 2),166                  "statistic": F(r.statistic * math.sqrt((n - 2) / (1 - r.statistic ** 2))), "p": F(r.pvalue),167                  "pLess": F(st.pearsonr(x, y, alternative="less").pvalue), "pGreater": F(st.pearsonr(x, y, alternative="greater").pvalue),168                  "ciLower": F(ci.low), "ciUpper": F(ci.high)})169170write("inference", {"source": "scipy.stats %s, statsmodels %s" % (__import__("scipy").__version__, __import__("statsmodels").__version__), "cases": cases})171
Files main
Menu

Theme