vitessio/vitess · error

samples have different lengths

Error message

samples have different lengths

What it means

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.

Source

Thrown at go/mathstats/ttest.go:81

		p = dist.CDF(t)
	case LocationGreater:
		p = 1 - dist.CDF(t)
	}
	return &TTestResult{N1: n1, N2: n2, T: t, DoF: dof, AltHypothesis: alt, P: p}
}

// A TTestSample is a sample that can be used for a one or two sample
// t-test.
type TTestSample interface {
	Weight() float64
	Mean() float64
	Variance() float64
}

var (
	ErrSampleSize        = errors.New("sample is too small")
	ErrZeroVariance      = errors.New("sample has zero variance")
	ErrMismatchedSamples = errors.New("samples have different lengths")
)

// TwoSampleTTest performs a two-sample (unpaired) Student's t-test on
// samples x1 and x2. This is a test of the null hypothesis that x1
// and x2 are drawn from populations with equal means. It assumes x1
// and x2 are independent samples, that the distributions have equal
// variance, and that the populations are normally distributed.
func TwoSampleTTest(x1, x2 TTestSample, alt LocationHypothesis) (*TTestResult, error) {
	n1, n2 := x1.Weight(), x2.Weight()
	if n1 == 0 || n2 == 0 {
		return nil, ErrSampleSize
	}
	v1, v2 := x1.Variance(), x2.Variance()
	if v1 == 0 && v2 == 0 {
		return nil, ErrZeroVariance
	}

	dof := n1 + n2 - 2

View on GitHub (pinned to 01a25a7d17)

Solutions

  1. Verify len(x1) == len(x2) before calling PairedTTest and fix the data-prep so both sides are aligned
  2. Pair rows by key and drop unpaired entries before the test
  3. Use TwoSampleWelchTTest (unpaired) if the samples are genuinely independent rather than matched pairs

Example fix

// before
res, err := mathstats.PairedTTest(before, after, 0, mathstats.LocationDiffers)
// after
if len(before) != len(after) {
    return nil, fmt.Errorf("paired samples differ: %d vs %d", len(before), len(after))
}
res, err := mathstats.PairedTTest(before, after, 0, mathstats.LocationDiffers)
Defensive patterns

Strategy: validation

Validate before calling

if len(x1) != len(x2) { return fmt.Errorf("paired samples must match: %d vs %d", len(x1), len(x2)) }

Try / catch

res, err := mathstats.PairedTTest(x1, x2, mu0, alt)
if errors.Is(err, mathstats.ErrMismatchedSamples) { /* realign data */ }

Prevention

When it happens

Trigger: PairedTTest(x1, x2, mu0, alt) where len(x1) != len(x2).

Common situations: 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.

Related errors


AI-assisted analysis of vitessio/vitess@01a25a7d17 (2026-09-01). Data as JSON: /api/errors/c9a9eca67d35a4c8. Report an issue: GitHub.