{"record":{"id":"ce96304518297017","repo":"jax-ml/jax","slug":"the-number-of-data-points-must-exceed-order-to-sca","errorCode":null,"errorMessage":"the number of data points must exceed order to scale the covariance matrix","messagePattern":"the number of data points must exceed order to scale the covariance matrix","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/numpy/polynomial.py","lineNumber":292,"sourceCode":"    # For multi-dimensional output, make scale (1, order) to divide\n    # across the c.T of shape (num_rhs, order)\n    c = (c.T / scale[np.newaxis, :]).T\n  else:\n    # Simple case for 1D output\n    c = c / scale\n\n  if full:\n    assert rcond is not None\n    return c, resids, rank, s, lax.asarray(rcond)\n  elif cov:\n    Vbase = linalg.inv(dot(lhs.T, lhs))\n    Vbase /= outer(scale, scale)\n\n    if cov == \"unscaled\":\n      fac = array(1.0)\n    else:\n      if len(x_arr) <= order:\n        raise ValueError(\"the number of data points must exceed order\"\n                         \" to scale the covariance matrix\")\n      fac = resids / (len(x_arr) - order)\n\n    if y_arr.ndim == 1:\n      fac = atleast_1d(fac)[np.newaxis]\n      # For 1D output, simple scalar multiplication\n      return c, Vbase * fac\n    else:\n      # For multiple rhs, broadcast fac to match shape\n      return c, Vbase[:, :, np.newaxis] * atleast_1d(fac)[np.newaxis, np.newaxis, :]\n  else:\n    return c\n\n@export\n@api.jit\ndef poly(seq_of_zeros: ArrayLike) -> Array:\n  r\"\"\"Returns the coefficients of a polynomial for the given sequence of roots.\n","sourceCodeStart":274,"sourceCodeEnd":310,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/numpy/polynomial.py#L274-L310","documentation":"Thrown by jnp.polyfit when a covariance estimate is requested (cov=True or 'unscaled' handling) but the number of sample points len(x) is not greater than the polynomial order. The covariance of the fit residuals requires a positive number of degrees of freedom (len(x) - order) to be well defined.","triggerScenarios":"Calling jnp.polyfit(x, y, deg, cov=True) where deg >= len(x), e.g. fitting a degree-3 polynomial to 3 data points. Note the check is len(x) <= order, where order = deg + 1 internally in numpy semantics.","commonSituations":"Overfitting small datasets: fitting high-degree polynomials to few samples; degenerate input where x has fewer elements than expected (e.g. wrong axis or accidental scalar input); porting numpy code that also raises here but users missed it.","solutions":["Increase the number of data points so len(x) > deg + 1","Lower the polynomial degree deg","Pass cov=False (or omit cov) if you don't need the covariance matrix","Validate len(x) and deg before calling polyfit"],"exampleFix":"// before\njnp.polyfit(x, y, deg=3, cov=True)  # x has 3 points\n// after\nassert len(x) > deg + 1, \"need more points than degree+1 for covariance\"\njnp.polyfit(x, y, deg=min(deg, len(x) - 2), cov=True)","handlingStrategy":"validation","validationCode":"if cov and len(x) <= deg + 1: raise ValueError(f'need len(x) > deg+1, got {len(x)} points, deg={deg}')","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Check len(x) > deg + 1 whenever cov=True","Cap degree from data size: deg = min(deg, len(x) - 2)","Log dataset size before fitting"],"tags":["jax","polyfit","covariance","insufficient-data"],"backgroundTag":"insufficient-data-for-fit","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}