Repository Public
1import Foundation2import SheetCore3import Statistics45func Statistics_normalQuantile(_ p: Double) -> Double { NormalDistribution.standard.quantile(p) }67/// Deterministic wording of test decisions. No judgement beyond "reject / fail to reject" at the chosen α.8public enum Interpret {9 /// "At α = 0.05, reject the null hypothesis (p = 0.0123 < α)."10 public static func decision(p: Double, alpha: Double, locale: Locale = .current) -> String {11 guard p.isFinite else { return L("The p-value is not available, so no decision is made.") }12 let a = Fmt.fixed(alpha, alpha < 0.01 ? 4 : 2, locale: locale)13 // "p = 0.0123" or, for tiny values, "p < 0.0001"14 let shown = p < 0.0001 ? "p " + Fmt.p(p, locale: locale) : "p = " + Fmt.p(p, locale: locale)15 if p < alpha { return L("At α = %@, reject the null hypothesis (%@ < α).", a, shown) }16 return L("At α = %@, fail to reject the null hypothesis (%@ ≥ α).", a, shown)17 }1819 /// Decision sentence followed by the stated null hypothesis.20 public static func decision(p: Double, alpha: Double, null: String, locale: Locale = .current) -> String {21 decision(p: p, alpha: alpha, locale: locale) + " " + L("Null hypothesis: %@.", null)22 }2324 public static func sidedness(_ a: Alternative) -> String {25 switch a {26 case .twoSided: L("two-sided")27 case .less: L("one-sided, lower tail")28 case .greater: L("one-sided, upper tail")29 }30 }31}3233/// Standard presentation of a `HypothesisTest`.34public enum TestBlocks {35 /// Headline figures, detail table, interpretation at `alpha`, assumptions and notes.36 public static func blocks(_ t: HypothesisTest, alpha: Double, null: String? = nil, locale: Locale = .current) -> [ResultBlock] {37 var metrics = [Metric(L("Statistic (%@)", t.statisticName), t.statistic)]38 if let df = t.df {39 if let df2 = t.df2 { metrics.append(Metric(L("Degrees of freedom"), text: Fmt.df(df, locale: locale) + ", " + Fmt.df(df2, locale: locale))) }40 else { metrics.append(Metric(L("Degrees of freedom"), text: Fmt.df(df, locale: locale))) }41 }42 metrics.append(Metric(L("p-value"), t.pValue, .pValue, detail: "(" + t.pValueDescription + ")"))43 if let e = t.effectSize { metrics.append(Metric(L("Effect size (%@)", e.name), e.value)) }44 var blocks: [ResultBlock] = [.metrics(metrics), .table(table(t))]45 blocks.append(.interpretation(null.map { Interpret.decision(p: t.pValue, alpha: alpha, null: $0, locale: locale) }46 ?? Interpret.decision(p: t.pValue, alpha: alpha, locale: locale)))47 if !t.assumptions.isEmpty { blocks.append(.assumptions(t.assumptions, title: L("Assumptions"))) }48 if !t.notes.isEmpty { blocks.append(.note(t.notes)) }49 return blocks50 }5152 /// Label / value table of the test (statistic, df, p-value with sidedness, estimate, interval, effect size, details).53 public static func table(_ t: HypothesisTest, title: String? = nil) -> ReportTable {54 var items: [(String, Value, CellFormat)] = [(L("Statistic (%@)", t.statisticName), .finite(t.statistic), .auto)]55 if let df = t.df { items.append((t.df2 == nil ? L("Degrees of freedom") : L("Degrees of freedom (numerator)"), .finite(df), .auto)) }56 if let df2 = t.df2 { items.append((L("Degrees of freedom (denominator)"), .finite(df2), .auto)) }57 items.append((L("p-value"), .finite(t.pValue), .pValue))58 items.append((L("p-value refers to"), .text(t.pValueDescription), .auto))59 if let e = t.estimate { items.append((e.name, .finite(e.value), .auto)) }60 if let ci = t.confidenceInterval {61 let level = Fmt.percent(ci.level, 0)62 items.append((L("%@ confidence interval, lower", level), .finite(ci.lower), .auto))63 items.append((L("%@ confidence interval, upper", level), .finite(ci.upper), .auto))64 }65 if let e = t.effectSize { items.append((L("Effect size (%@)", e.name), .finite(e.value), .auto)) }66 for d in t.details { items.append((d.name, .finite(d.value), .auto)) }67 return ReportTable.keyValue(title: title ?? t.name, header: (L("Quantity"), L("Value")), items)68 }69}70