|
| 1 | +// Copyright 2022 The Go Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package testmath |
| 6 | + |
| 7 | +import ( |
| 8 | + "errors" |
| 9 | + "math" |
| 10 | +) |
| 11 | + |
| 12 | +// A TTestSample is a sample that can be used for a one or two sample |
| 13 | +// t-test. |
| 14 | +type TTestSample interface { |
| 15 | + Weight() float64 |
| 16 | + Mean() float64 |
| 17 | + Variance() float64 |
| 18 | +} |
| 19 | + |
| 20 | +var ( |
| 21 | + ErrSampleSize = errors.New("sample is too small") |
| 22 | + ErrZeroVariance = errors.New("sample has zero variance") |
| 23 | + ErrMismatchedSamples = errors.New("samples have different lengths") |
| 24 | +) |
| 25 | + |
| 26 | +// TwoSampleWelchTTest performs a two-sample (unpaired) Welch's t-test |
| 27 | +// on samples x1 and x2. This t-test does not assume the distributions |
| 28 | +// have equal variance. |
| 29 | +func TwoSampleWelchTTest(x1, x2 TTestSample, alt LocationHypothesis) (*TTestResult, error) { |
| 30 | + n1, n2 := x1.Weight(), x2.Weight() |
| 31 | + if n1 <= 1 || n2 <= 1 { |
| 32 | + // TODO: Can we still do this with n == 1? |
| 33 | + return nil, ErrSampleSize |
| 34 | + } |
| 35 | + v1, v2 := x1.Variance(), x2.Variance() |
| 36 | + if v1 == 0 && v2 == 0 { |
| 37 | + return nil, ErrZeroVariance |
| 38 | + } |
| 39 | + |
| 40 | + dof := math.Pow(v1/n1+v2/n2, 2) / |
| 41 | + (math.Pow(v1/n1, 2)/(n1-1) + math.Pow(v2/n2, 2)/(n2-1)) |
| 42 | + s := math.Sqrt(v1/n1 + v2/n2) |
| 43 | + t := (x1.Mean() - x2.Mean()) / s |
| 44 | + return newTTestResult(int(n1), int(n2), t, dof, alt), nil |
| 45 | +} |
| 46 | + |
| 47 | +// A TTestResult is the result of a t-test. |
| 48 | +type TTestResult struct { |
| 49 | + // N1 and N2 are the sizes of the input samples. For a |
| 50 | + // one-sample t-test, N2 is 0. |
| 51 | + N1, N2 int |
| 52 | + |
| 53 | + // T is the value of the t-statistic for this t-test. |
| 54 | + T float64 |
| 55 | + |
| 56 | + // DoF is the degrees of freedom for this t-test. |
| 57 | + DoF float64 |
| 58 | + |
| 59 | + // AltHypothesis specifies the alternative hypothesis tested |
| 60 | + // by this test against the null hypothesis that there is no |
| 61 | + // difference in the means of the samples. |
| 62 | + AltHypothesis LocationHypothesis |
| 63 | + |
| 64 | + // P is p-value for this t-test for the given null hypothesis. |
| 65 | + P float64 |
| 66 | +} |
| 67 | + |
| 68 | +func newTTestResult(n1, n2 int, t, dof float64, alt LocationHypothesis) *TTestResult { |
| 69 | + dist := TDist{dof} |
| 70 | + var p float64 |
| 71 | + switch alt { |
| 72 | + case LocationDiffers: |
| 73 | + p = 2 * (1 - dist.CDF(math.Abs(t))) |
| 74 | + case LocationLess: |
| 75 | + p = dist.CDF(t) |
| 76 | + case LocationGreater: |
| 77 | + p = 1 - dist.CDF(t) |
| 78 | + } |
| 79 | + return &TTestResult{N1: n1, N2: n2, T: t, DoF: dof, AltHypothesis: alt, P: p} |
| 80 | +} |
| 81 | + |
| 82 | +// A LocationHypothesis specifies the alternative hypothesis of a |
| 83 | +// location test such as a t-test or a Mann-Whitney U-test. The |
| 84 | +// default (zero) value is to test against the alternative hypothesis |
| 85 | +// that they differ. |
| 86 | +type LocationHypothesis int |
| 87 | + |
| 88 | +const ( |
| 89 | + // LocationLess specifies the alternative hypothesis that the |
| 90 | + // location of the first sample is less than the second. This |
| 91 | + // is a one-tailed test. |
| 92 | + LocationLess LocationHypothesis = -1 |
| 93 | + |
| 94 | + // LocationDiffers specifies the alternative hypothesis that |
| 95 | + // the locations of the two samples are not equal. This is a |
| 96 | + // two-tailed test. |
| 97 | + LocationDiffers LocationHypothesis = 0 |
| 98 | + |
| 99 | + // LocationGreater specifies the alternative hypothesis that |
| 100 | + // the location of the first sample is greater than the |
| 101 | + // second. This is a one-tailed test. |
| 102 | + LocationGreater LocationHypothesis = 1 |
| 103 | +) |
| 104 | + |
| 105 | +// A TDist is a Student's t-distribution with V degrees of freedom. |
| 106 | +type TDist struct { |
| 107 | + V float64 |
| 108 | +} |
| 109 | + |
| 110 | +// PDF returns the value at x of the probability distribution function for the |
| 111 | +// distribution. |
| 112 | +func (t TDist) PDF(x float64) float64 { |
| 113 | + return math.Exp(lgamma((t.V+1)/2)-lgamma(t.V/2)) / |
| 114 | + math.Sqrt(t.V*math.Pi) * math.Pow(1+(x*x)/t.V, -(t.V+1)/2) |
| 115 | +} |
| 116 | + |
| 117 | +// CDF returns the value at x of the cumulative distribution function for the |
| 118 | +// distribution. |
| 119 | +func (t TDist) CDF(x float64) float64 { |
| 120 | + if x == 0 { |
| 121 | + return 0.5 |
| 122 | + } else if x > 0 { |
| 123 | + return 1 - 0.5*betaInc(t.V/(t.V+x*x), t.V/2, 0.5) |
| 124 | + } else if x < 0 { |
| 125 | + return 1 - t.CDF(-x) |
| 126 | + } else { |
| 127 | + return math.NaN() |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +func (t TDist) Bounds() (float64, float64) { |
| 132 | + return -4, 4 |
| 133 | +} |
| 134 | + |
| 135 | +func lgamma(x float64) float64 { |
| 136 | + y, _ := math.Lgamma(x) |
| 137 | + return y |
| 138 | +} |
| 139 | + |
| 140 | +// betaInc returns the value of the regularized incomplete beta |
| 141 | +// function Iₓ(a, b) = 1 / B(a, b) * ∫₀ˣ tᵃ⁻¹ (1-t)ᵇ⁻¹ dt. |
| 142 | +// |
| 143 | +// This is not to be confused with the "incomplete beta function", |
| 144 | +// which can be computed as BetaInc(x, a, b)*Beta(a, b). |
| 145 | +// |
| 146 | +// If x < 0 or x > 1, returns NaN. |
| 147 | +func betaInc(x, a, b float64) float64 { |
| 148 | + // Based on Numerical Recipes in C, section 6.4. This uses the |
| 149 | + // continued fraction definition of I: |
| 150 | + // |
| 151 | + // (xᵃ*(1-x)ᵇ)/(a*B(a,b)) * (1/(1+(d₁/(1+(d₂/(1+...)))))) |
| 152 | + // |
| 153 | + // where B(a,b) is the beta function and |
| 154 | + // |
| 155 | + // d_{2m+1} = -(a+m)(a+b+m)x/((a+2m)(a+2m+1)) |
| 156 | + // d_{2m} = m(b-m)x/((a+2m-1)(a+2m)) |
| 157 | + if x < 0 || x > 1 { |
| 158 | + return math.NaN() |
| 159 | + } |
| 160 | + bt := 0.0 |
| 161 | + if 0 < x && x < 1 { |
| 162 | + // Compute the coefficient before the continued |
| 163 | + // fraction. |
| 164 | + bt = math.Exp(lgamma(a+b) - lgamma(a) - lgamma(b) + |
| 165 | + a*math.Log(x) + b*math.Log(1-x)) |
| 166 | + } |
| 167 | + if x < (a+1)/(a+b+2) { |
| 168 | + // Compute continued fraction directly. |
| 169 | + return bt * betacf(x, a, b) / a |
| 170 | + } else { |
| 171 | + // Compute continued fraction after symmetry transform. |
| 172 | + return 1 - bt*betacf(1-x, b, a)/b |
| 173 | + } |
| 174 | +} |
| 175 | + |
| 176 | +// betacf is the continued fraction component of the regularized |
| 177 | +// incomplete beta function Iₓ(a, b). |
| 178 | +func betacf(x, a, b float64) float64 { |
| 179 | + const maxIterations = 200 |
| 180 | + const epsilon = 3e-14 |
| 181 | + |
| 182 | + raiseZero := func(z float64) float64 { |
| 183 | + if math.Abs(z) < math.SmallestNonzeroFloat64 { |
| 184 | + return math.SmallestNonzeroFloat64 |
| 185 | + } |
| 186 | + return z |
| 187 | + } |
| 188 | + |
| 189 | + c := 1.0 |
| 190 | + d := 1 / raiseZero(1-(a+b)*x/(a+1)) |
| 191 | + h := d |
| 192 | + for m := 1; m <= maxIterations; m++ { |
| 193 | + mf := float64(m) |
| 194 | + |
| 195 | + // Even step of the recurrence. |
| 196 | + numer := mf * (b - mf) * x / ((a + 2*mf - 1) * (a + 2*mf)) |
| 197 | + d = 1 / raiseZero(1+numer*d) |
| 198 | + c = raiseZero(1 + numer/c) |
| 199 | + h *= d * c |
| 200 | + |
| 201 | + // Odd step of the recurrence. |
| 202 | + numer = -(a + mf) * (a + b + mf) * x / ((a + 2*mf) * (a + 2*mf + 1)) |
| 203 | + d = 1 / raiseZero(1+numer*d) |
| 204 | + c = raiseZero(1 + numer/c) |
| 205 | + hfac := d * c |
| 206 | + h *= hfac |
| 207 | + |
| 208 | + if math.Abs(hfac-1) < epsilon { |
| 209 | + return h |
| 210 | + } |
| 211 | + } |
| 212 | + panic("betainc: a or b too big; failed to converge") |
| 213 | +} |
0 commit comments