Repository Public
1import Foundation23extension Inference {4 private static func ci(_ est: Double, _ se: Double, _ dist: some ContinuousDistribution, _ level: Double, _ method: String) -> ConfidenceInterval {5 let half = dist.isf((1 - level) / 2) * se6 return ConfidenceInterval(estimate: est, lower: est - half, upper: est + half, level: level, method: method)7 }89 /// One-sample t-test of H0: μ = `mu`. Effect size: Cohen's d = (x̄ − μ)/s. The confidence interval10 /// is the two-sided t interval for the mean whatever the alternative.11 public static func tTest(_ x: [Double], mu: Double = 0, alternative: Alternative = .twoSided, level: Double = 0.95) throws -> HypothesisTest {12 let v = try clean(x, min: 2, "sample")13 try checkLevel(level)14 let n = Double(v.count), m = Descriptive.mean(v), s = Descriptive.standardDeviation(v)15 guard s > 0 else { throw StatError.invalidInput("the sample is constant: the t statistic is undefined") }16 let se = s / n.squareRoot(), df = n - 117 let t = (m - mu) / se18 let dist = StudentTDistribution(df: df)19 return HypothesisTest(name: "One-sample t-test", statisticName: "t", statistic: t, df: df,20 pValue: dist.pValue(t, alternative: alternative), alternative: alternative,21 estimate: NamedValue("Mean", m), confidenceInterval: ci(m, se, dist, level, "Student t, df = \(v.count - 1)"),22 effectSize: NamedValue("Cohen's d", (m - mu) / s),23 details: [NamedValue("n", n), NamedValue("Hypothesised mean", mu), NamedValue("Standard deviation", s), NamedValue("Standard error", se)],24 assumptions: ["Observations are independent", "The population is normal, or n is large enough for the central limit theorem"])25 }2627 /// Two-sample t-test of H0: μx − μy = `difference`.28 /// `equalVariances: true` is Student's pooled test (df = n₁+n₂−2); false (default) is Welch's test with29 /// Welch–Satterthwaite df. Effect size: Cohen's d = (x̄ − ȳ)/s_pooled in both cases.30 public static func tTest(_ x: [Double], _ y: [Double], equalVariances: Bool = false, difference: Double = 0,31 alternative: Alternative = .twoSided, level: Double = 0.95) throws -> HypothesisTest {32 let s = try twoSampleStats(x, y, equalVariances: equalVariances)33 try checkLevel(level)34 let t = (s.diff - difference) / s.se35 let dist = StudentTDistribution(df: s.df)36 var assumptions = ["The two samples are independent", "Each population is normal, or both samples are large"]37 if equalVariances { assumptions.append("The two populations have the same variance") }38 return HypothesisTest(name: equalVariances ? "Two-sample t-test (pooled variance)" : "Welch two-sample t-test", statisticName: "t",39 statistic: t, df: s.df, pValue: dist.pValue(t, alternative: alternative), alternative: alternative,40 estimate: NamedValue("Mean difference", s.diff),41 confidenceInterval: ci(s.diff, s.se, dist, level, equalVariances ? "Pooled-variance t" : "Welch t"),42 effectSize: NamedValue("Cohen's d (pooled SD)", s.diff / s.pooledSD),43 details: [NamedValue("n1", Double(s.nx)), NamedValue("n2", Double(s.ny)), NamedValue("Mean 1", s.mx), NamedValue("Mean 2", s.my),44 NamedValue("Variance 1", s.vx), NamedValue("Variance 2", s.vy), NamedValue("Standard error", s.se),45 NamedValue("Hypothesised difference", difference)],46 assumptions: assumptions)47 }4849 /// Paired t-test of H0: mean(x − y) = `difference` (a one-sample test on the differences).50 /// Effect size: Cohen's d_z = mean difference / SD of the differences.51 public static func pairedTTest(_ x: [Double], _ y: [Double], difference: Double = 0, alternative: Alternative = .twoSided,52 level: Double = 0.95) throws -> HypothesisTest {53 guard x.count == y.count else { throw StatError.invalidInput("paired samples must have the same length") }54 var r = try tTest(zip(x, y).map { $0 - $1 }, mu: difference, alternative: alternative, level: level)55 r.name = "Paired t-test"56 r.estimate = NamedValue("Mean difference", r.estimate?.value ?? .nan)57 r.effectSize = NamedValue("Cohen's d_z", r.effectSize?.value ?? .nan)58 r.details[0].name = "Pairs"59 r.details[1].name = "Hypothesised difference"60 r.details[2].name = "SD of differences"61 r.assumptions = ["Pairs are independent of each other", "The differences are normal, or the number of pairs is large"]62 return r63 }6465 /// One-sample z-test of H0: μ = `mu`. With `sigma` the population standard deviation is taken as known;66 /// with nil the sample standard deviation is used (large-sample test, as statsmodels `ztest`).67 public static func zTest(_ x: [Double], mu: Double = 0, sigma: Double? = nil, alternative: Alternative = .twoSided,68 level: Double = 0.95) throws -> HypothesisTest {69 let v = try clean(x, min: sigma == nil ? 2 : 1, "sample")70 try checkLevel(level)71 let n = Double(v.count), m = Descriptive.mean(v)72 let sd = sigma ?? Descriptive.standardDeviation(v)73 guard sd > 0 else { throw StatError.invalidInput("the standard deviation must be positive") }74 let se = sd / n.squareRoot(), z = (m - mu) / se75 let dist = NormalDistribution.standard76 return HypothesisTest(name: "One-sample z-test", statisticName: "z", statistic: z, pValue: dist.pValue(z, alternative: alternative),77 alternative: alternative, estimate: NamedValue("Mean", m), confidenceInterval: ci(m, se, dist, level, "Normal"),78 effectSize: NamedValue("Cohen's d", (m - mu) / sd),79 details: [NamedValue("n", n), NamedValue("Hypothesised mean", mu), NamedValue("Standard deviation used", sd), NamedValue("Standard error", se)],80 assumptions: ["Observations are independent",81 sigma == nil ? "n is large (the sample standard deviation replaces σ)" : "The population standard deviation is known",82 "The sample mean is approximately normal"])83 }8485 /// Two-sample z-test of H0: μx − μy = `difference`. Known σ's when given; otherwise the unpooled sample86 /// variances (large-sample test; statsmodels `CompareMeans.ztest_ind(usevar: "unequal")`).87 public static func zTest(_ x: [Double], _ y: [Double], sigmaX: Double? = nil, sigmaY: Double? = nil, difference: Double = 0,88 alternative: Alternative = .twoSided, level: Double = 0.95) throws -> HypothesisTest {89 let a = try clean(x, min: sigmaX == nil ? 2 : 1, "first sample"), b = try clean(y, min: sigmaY == nil ? 2 : 1, "second sample")90 try checkLevel(level)91 let v1 = sigmaX.map { $0 * $0 } ?? Descriptive.variance(a), v2 = sigmaY.map { $0 * $0 } ?? Descriptive.variance(b)92 let se = (v1 / Double(a.count) + v2 / Double(b.count)).squareRoot()93 guard se > 0 else { throw StatError.invalidInput("the standard error is zero") }94 let diff = Descriptive.mean(a) - Descriptive.mean(b)95 let z = (diff - difference) / se96 let dist = NormalDistribution.standard97 return HypothesisTest(name: "Two-sample z-test", statisticName: "z", statistic: z, pValue: dist.pValue(z, alternative: alternative),98 alternative: alternative, estimate: NamedValue("Mean difference", diff),99 confidenceInterval: ci(diff, se, dist, level, "Normal"),100 details: [NamedValue("n1", Double(a.count)), NamedValue("n2", Double(b.count)), NamedValue("Standard error", se),101 NamedValue("Hypothesised difference", difference)],102 assumptions: ["The two samples are independent",103 sigmaX != nil && sigmaY != nil ? "Both population standard deviations are known" : "Samples are large (sample variances replace σ²)"])104 }105106 /// One-sample z-test for a proportion, H0: p = `p0`. The standard error uses the null value,107 /// √(p0(1−p0)/n) (score test). The confidence interval reported is the Wilson interval.108 public static func proportionZTest(successes k: Int, trials n: Int, p0: Double, alternative: Alternative = .twoSided,109 level: Double = 0.95) throws -> HypothesisTest {110 guard n > 0, k >= 0, k <= n, p0 > 0, p0 < 1 else { throw StatError.invalidInput("need 0 ≤ successes ≤ trials, trials > 0 and 0 < p0 < 1") }111 let p = Double(k) / Double(n)112 let se = (p0 * (1 - p0) / Double(n)).squareRoot()113 let z = (p - p0) / se114 return HypothesisTest(name: "One-sample proportion z-test", statisticName: "z", statistic: z,115 pValue: NormalDistribution.standard.pValue(z, alternative: alternative), alternative: alternative,116 estimate: NamedValue("Sample proportion", p),117 confidenceInterval: try proportionInterval(successes: k, trials: n, level: level),118 effectSize: NamedValue("Cohen's h", 2 * asin(p.squareRoot()) - 2 * asin(p0.squareRoot())),119 details: [NamedValue("n", Double(n)), NamedValue("Hypothesised proportion", p0), NamedValue("Standard error (under H0)", se)],120 assumptions: ["Trials are independent with constant success probability", "n·p0 and n·(1−p0) are both at least about 10"])121 }122123 /// Two-sample z-test for proportions, H0: p₁ = p₂, with the pooled proportion in the standard error.124 /// The confidence interval for p₁ − p₂ uses the unpooled (Wald) standard error.125 public static func proportionZTest(successes1 k1: Int, trials1 n1: Int, successes2 k2: Int, trials2 n2: Int,126 alternative: Alternative = .twoSided, level: Double = 0.95) throws -> HypothesisTest {127 guard n1 > 0, n2 > 0, k1 >= 0, k2 >= 0, k1 <= n1, k2 <= n2 else { throw StatError.invalidInput("need 0 ≤ successes ≤ trials and trials > 0") }128 try checkLevel(level)129 let p1 = Double(k1) / Double(n1), p2 = Double(k2) / Double(n2)130 let pooled = Double(k1 + k2) / Double(n1 + n2)131 let se = (pooled * (1 - pooled) * (1 / Double(n1) + 1 / Double(n2))).squareRoot()132 guard se > 0 else { throw StatError.invalidInput("pooled proportion is 0 or 1: the test is undefined") }133 let z = (p1 - p2) / se134 let seWald = (p1 * (1 - p1) / Double(n1) + p2 * (1 - p2) / Double(n2)).squareRoot()135 return HypothesisTest(name: "Two-sample proportion z-test", statisticName: "z", statistic: z,136 pValue: NormalDistribution.standard.pValue(z, alternative: alternative), alternative: alternative,137 estimate: NamedValue("Difference of proportions", p1 - p2),138 confidenceInterval: ci(p1 - p2, seWald, NormalDistribution.standard, level, "Wald (unpooled)"),139 effectSize: NamedValue("Cohen's h", 2 * asin(p1.squareRoot()) - 2 * asin(p2.squareRoot())),140 details: [NamedValue("Proportion 1", p1), NamedValue("Proportion 2", p2), NamedValue("Pooled proportion", pooled), NamedValue("Standard error (pooled)", se)],141 assumptions: ["The two samples are independent", "Expected successes and failures are at least about 10 in each sample"])142 }143}144