{"record":{"id":"7cf3b176588cb98d","repo":"vitessio/vitess","slug":"sample-has-zero-variance","errorCode":null,"errorMessage":"sample has zero variance","messagePattern":"sample has zero variance","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"go/mathstats/ttest.go","lineNumber":80,"sourceCode":"\tcase LocationLess:\n\t\tp = dist.CDF(t)\n\tcase LocationGreater:\n\t\tp = 1 - dist.CDF(t)\n\t}\n\treturn &TTestResult{N1: n1, N2: n2, T: t, DoF: dof, AltHypothesis: alt, P: p}\n}\n\n// A TTestSample is a sample that can be used for a one or two sample\n// t-test.\ntype TTestSample interface {\n\tWeight() float64\n\tMean() float64\n\tVariance() float64\n}\n\nvar (\n\tErrSampleSize        = errors.New(\"sample is too small\")\n\tErrZeroVariance      = errors.New(\"sample has zero variance\")\n\tErrMismatchedSamples = errors.New(\"samples have different lengths\")\n)\n\n// TwoSampleTTest performs a two-sample (unpaired) Student's t-test on\n// samples x1 and x2. This is a test of the null hypothesis that x1\n// and x2 are drawn from populations with equal means. It assumes x1\n// and x2 are independent samples, that the distributions have equal\n// variance, and that the populations are normally distributed.\nfunc TwoSampleTTest(x1, x2 TTestSample, alt LocationHypothesis) (*TTestResult, error) {\n\tn1, n2 := x1.Weight(), x2.Weight()\n\tif n1 == 0 || n2 == 0 {\n\t\treturn nil, ErrSampleSize\n\t}\n\tv1, v2 := x1.Variance(), x2.Variance()\n\tif v1 == 0 && v2 == 0 {\n\t\treturn nil, ErrZeroVariance\n\t}\n","sourceCodeStart":62,"sourceCodeEnd":98,"githubUrl":"https://github.com/vitessio/vitess/blob/01a25a7d176f94613b8d59d799f438380a8760e4/go/mathstats/ttest.go#L62-L98","documentation":"ErrZeroVariance is a sentinel error meaning all input samples have zero variance — every observation is identical — so the t-statistic denominator would be zero and the test is undefined. It is returned by TwoSampleTTest, TwoSampleWelchTTest, OneSampleTTest (when sample variance is 0) and PairedTTest (when the standard deviation of the differences is 0).","triggerScenarios":"TwoSampleTTest or TwoSampleWelchTTest with v1==0 && v2==0; OneSampleTTest with Variance()==0; PairedTTest when StdDev of x1[i]-x2[i] is 0 (identical pairs, e.g. x1 == x2).","commonSituations":"Comparing a dataset against itself; all rows returning the same metric value (e.g. a constant column, feature flag always off); paired test on before/after data where nothing changed.","solutions":["Check Variance() (or StdDev of differences) before the call and short-circuit when it is 0","Return a special 'no difference detected' result instead of running the test when variance is zero","errors.Is(err, mathstats.ErrZeroVariance) to detect the constant-data case"],"exampleFix":"// before\nres, err := mathstats.PairedTTest(before, after, 0, mathstats.LocationDiffers)\n// after\nres, err := mathstats.PairedTTest(before, after, 0, mathstats.LocationDiffers)\nif errors.Is(err, mathstats.ErrZeroVariance) {\n    return \"no change between before and after\", nil\n}","handlingStrategy":"validation","validationCode":"if x.Variance() == 0 { return errors.New(\"sample is constant; t-test undefined\") }","typeGuard":"func hasVariance(s mathstats.TTestSample) bool { return s.Variance() > 0 }","tryCatchPattern":"res, err := mathstats.PairedTTest(a, b, 0, alt)\nif errors.Is(err, mathstats.ErrZeroVariance) { return \"no change\", nil }","preventionTips":["Check Variance() (or StdDev of diffs) before the test","Treat constant data as 'no effect detected' rather than an error","Guard against comparing a dataset with itself"],"tags":["statistics","ttest","zero-variance"],"backgroundTag":"zero-variance-sample","analyzedSha":"01a25a7d176f94613b8d59d799f438380a8760e4","analyzedAt":"2026-09-01T17:28:30.605Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}