jax-ml/jax · error · ValueError

ddof and correction can't be provided simultaneously.

Error message

ddof and correction can't be provided simultaneously.

What it means

jnp.var accepts both the legacy numpy parameter ddof and the JAX-specific correction; they parameterize the same degrees-of-freedom adjustment, so both cannot be meaningfully given at once. correction defaults to ddof when correction is None; supplying a non-zero ddof together with correction raises.

Source

Thrown at jax/_src/numpy/reductions.py:1121

     [ 3.33]
     [10.92]]

    To include specific elements of the array to compute variance, you can use
    ``where``.

    >>> where = jnp.array([[1, 0, 1, 0],
    ...                    [0, 1, 1, 0],
    ...                    [1, 1, 1, 0]], dtype=bool)
    >>> with jnp.printoptions(precision=2, suppress=True):
    ...   print(jnp.var(x, axis=1, keepdims=True, where=where))
    [[2.25]
     [4.  ]
     [6.22]]
  """
  if correction is None:
    correction = ddof
  elif not isinstance(ddof, int) or ddof != 0:
    raise ValueError("ddof and correction can't be provided simultaneously.")
  a = ensure_arraylike("var", a)
  return _var(a, axis=_ensure_optional_axes(axis), dtype=dtype, out=out, correction=correction, keepdims=keepdims,
              where=where, a_mean=mean)

@api.jit(static_argnames=('axis', 'dtype', 'keepdims'))
def _var(a: Array, *, axis: Axis = None, dtype: DTypeLike | None = None,
         out: None = None, correction: int | float = 0, keepdims: bool = False,
         where: ArrayLike | None = None, a_mean: ArrayLike | None = None) -> Array:
  where = check_where("var", where)
  if dtype is not None:
    dtype = dtypes.check_and_canonicalize_user_dtype(dtype, "var")
  if out is not None:
    raise NotImplementedError("The 'out' argument to jnp.var is not supported.")

  computation_dtype, dtype = _var_promote_types(a.dtype, dtype)
  a = lax.asarray(a).astype(computation_dtype)
  if a_mean is None:
    a_mean = mean(a, axis, dtype=computation_dtype, keepdims=True, where=where)

View on GitHub (pinned to 1e1c6a8fc0)

Solutions

  1. Remove ddof and keep correction (preferred modern JAX API)
  2. Or remove correction and keep ddof for numpy parity
  3. In wrappers, only forward whichever parameter the caller actually set (use sentinel defaults)

Example fix

// before
jnp.var(x, ddof=1, correction=1)
// after
jnp.var(x, correction=1)
Defensive patterns

Strategy: validation

Validate before calling

def safe_var(x, ddof=0, correction=None):
    if correction is not None:
        return jnp.var(x, correction=correction)
    return jnp.var(x, ddof=ddof)

Prevention

When it happens

Trigger: jnp.var(x, ddof=1, correction=1) raises; ddof=0 with correction is allowed (ddof=0 is the no-op default); any non-zero or non-int ddof alongside correction raises.

Common situations: Code written for newer JAX (correction) refactored or wrapped around older numpy-style code that passes ddof; wrapper functions forwarding both parameters with defaults like ddof=1.

Related errors


AI-assisted analysis of jax-ml/jax@1e1c6a8fc0 (2026-08-27). Data as JSON: /api/errors/12c2fb502cea66b9. Report an issue: GitHub.