vitessio/vitess · error

betainc: a or b too big; failed to converge

Error message

betainc: a or b too big; failed to converge

What it means

The incomplete beta function implementation (betacf continued-fraction loop) panics when the continued fraction fails to converge within the iteration limit. This happens for very large a or b parameters where the numeric algorithm cannot reach the epsilon tolerance.

Source

Thrown at go/mathstats/beta.go:86

		// Even step of the recurrence.
		numer := mf * (b - mf) * x / ((a + 2*mf - 1) * (a + 2*mf))
		d = 1 / raiseZero(1+numer*d)
		c = raiseZero(1 + numer/c)
		h *= d * c

		// Odd step of the recurrence.
		numer = -(a + mf) * (a + b + mf) * x / ((a + 2*mf) * (a + 2*mf + 1))
		d = 1 / raiseZero(1+numer*d)
		c = raiseZero(1 + numer/c)
		hfac := d * c
		h *= hfac

		if math.Abs(hfac-1) < epsilon {
			return h
		}
	}
	panic("betainc: a or b too big; failed to converge")
}

View on GitHub (pinned to 01a25a7d17)

Solutions

  1. Reduce the magnitude of a and b parameters, or rescale the problem before computing the incomplete beta.
  2. Use a numerically stable alternative (e.g. regularized incomplete beta from a full stats library) for large parameters.
  3. Cap or validate inputs in a wrapper so huge parameters are rejected with a proper error instead of a panic.

Example fix

// before
result := mathstats.BetaInc(a, b, x) // a=1e6 panics
// after
if a > 1e4 || b > 1e4 {
    return 0, fmt.Errorf("betainc: parameters too large: a=%v b=%v", a, b)
}
result := mathstats.BetaInc(a, b, x)
Defensive patterns

Strategy: try-catch

Validate before calling

if a <= 0 || b <= 0 || a > 1e4 || b > 1e4 {
    return fmt.Errorf("betainc: unsupported parameters a=%v b=%v", a, b)
}

Try / catch

func safeBetaInc(a, b, x float64) (res float64, err error) {
    defer func() {
        if r := recover(); r != nil {
            err = fmt.Errorf("betainc failed: %v", r)
        }
    }()
    res = mathstats.BetaInc(a, b, x)
    return res, nil
}

Prevention

When it happens

Trigger: Calling the beta CDF / mathBetaInc path with extremely large shape parameters a or b, so the continued fraction in betacf never satisfies |hfac-1| < epsilon before exhausting iterations.

Common situations: Statistical computations with huge distribution parameters (e.g. computing tail probabilities of a Beta distribution with a or b in the thousands or millions); scaling issues after transforming data into p-value computations.

Related errors


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