{"record":{"id":"c9a9eca67d35a4c8","repo":"vitessio/vitess","slug":"samples-have-different-lengths","errorCode":null,"errorMessage":"samples have different lengths","messagePattern":"samples have different lengths","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"go/mathstats/ttest.go","lineNumber":81,"sourceCode":"\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\n\tdof := n1 + n2 - 2","sourceCodeStart":63,"sourceCodeEnd":99,"githubUrl":"https://github.com/vitessio/vitess/blob/01a25a7d176f94613b8d59d799f438380a8760e4/go/mathstats/ttest.go#L63-L99","documentation":"ErrMismatchedSamples is a sentinel error returned only by PairedTTest when x1 and x2 have different lengths. A paired t-test compares observations one-to-one (x1[i]-x2[i]), so unequal lengths make pairing impossible.","triggerScenarios":"PairedTTest(x1, x2, mu0, alt) where len(x1) != len(x2).","commonSituations":"Before/after datasets where some rows are missing on one side after filtering or joining; appending measurements collected at different times; a data pipeline dropping nulls from only one of the two slices.","solutions":["Verify len(x1) == len(x2) before calling PairedTTest and fix the data-prep so both sides are aligned","Pair rows by key and drop unpaired entries before the test","Use TwoSampleWelchTTest (unpaired) if the samples are genuinely independent rather than matched pairs"],"exampleFix":"// before\nres, err := mathstats.PairedTTest(before, after, 0, mathstats.LocationDiffers)\n// after\nif len(before) != len(after) {\n    return nil, fmt.Errorf(\"paired samples differ: %d vs %d\", len(before), len(after))\n}\nres, err := mathstats.PairedTTest(before, after, 0, mathstats.LocationDiffers)","handlingStrategy":"validation","validationCode":"if len(x1) != len(x2) { return fmt.Errorf(\"paired samples must match: %d vs %d\", len(x1), len(x2)) }","typeGuard":null,"tryCatchPattern":"res, err := mathstats.PairedTTest(x1, x2, mu0, alt)\nif errors.Is(err, mathstats.ErrMismatchedSamples) { /* realign data */ }","preventionTips":["Pair rows by key before building the slices","Drop unpaired entries on both sides simultaneously","Verify slice lengths in unit tests for your data prep"],"tags":["statistics","ttest","paired-samples"],"backgroundTag":"sample-length-mismatch","analyzedSha":"01a25a7d176f94613b8d59d799f438380a8760e4","analyzedAt":"2026-09-01T17:28:30.605Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}