{"record":{"id":"7376920f509c8b5c","repo":"jax-ml/jax","slug":"incompatible-numbers-of-samples-and-fweights","errorCode":null,"errorMessage":"incompatible numbers of samples and fweights","messagePattern":"incompatible numbers of samples and fweights","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"jax/_src/numpy/lax_numpy.py","lineNumber":9214,"sourceCode":"  if y is not None:\n    y_arr = atleast_2d(y)\n    if not rowvar and y_arr.shape[0] != 1:\n      y_arr = y_arr.T\n    X = concatenate((X, y_arr), axis=0)\n  if X.shape[1] == 0:\n    cov_shape = () if X.shape[0] == 1 else (X.shape[0], X.shape[0])\n    return array_creation.full(cov_shape, np.nan, dtype=X.dtype)\n\n  if ddof is None:\n    ddof = 1 if bias == 0 else 0\n\n  w: Array | None = None\n  if fweights is not None:\n    fweights = util.ensure_arraylike(\"cov\", fweights)\n    if np.ndim(fweights) > 1:\n      raise RuntimeError(\"cannot handle multidimensional fweights\")\n    if np.shape(fweights)[0] != X.shape[1]:\n      raise RuntimeError(\"incompatible numbers of samples and fweights\")\n    if not issubdtype(fweights.dtype, np.integer):\n      raise TypeError(\"fweights must be integer.\")\n    # Ensure positive fweights; note that numpy raises an error on negative fweights.\n    w = abs(fweights)\n  if aweights is not None:\n    aweights = util.ensure_arraylike(\"cov\", aweights)\n    if np.ndim(aweights) > 1:\n      raise RuntimeError(\"cannot handle multidimensional aweights\")\n    if np.shape(aweights)[0] != X.shape[1]:\n      raise RuntimeError(\"incompatible numbers of samples and aweights\")\n    # Ensure positive aweights: note that numpy raises an error for negative aweights.\n    aweights = abs(aweights)\n    w = asarray(aweights if w is None else w * aweights)\n\n  if dtype is not None:\n    X = X.astype(dtype)\n    w = w.astype(dtype) if w is not None else w\n","sourceCodeStart":9196,"sourceCodeEnd":9232,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/numpy/lax_numpy.py#L9196-L9232","documentation":"When frequency weights are given to jnp.cov, their length must equal the number of observations (X.shape[1]). If np.shape(fweights)[0] != X.shape[1], RuntimeError('incompatible numbers of samples and fweights') is raised.","triggerScenarios":"jnp.cov(m, fweights=np.arange(5)) when m has 3 observations (m.shape[1] == 3), or weights sized to the number of variables instead of samples.","commonSituations":"Confusing variables x observations orientation (rowvar semantics); computing weights over a filtered subset of the data while passing the full m.","solutions":["Resize weights to match the observation count: fweights = fweights[:m.shape[1]]","Transpose m or set rowvar=False if the orientation is flipped","Verify fweights.shape[0] == m.shape[-1] before calling"],"exampleFix":"// before\njnp.cov(m, fweights=w)  # len(w) != m.shape[1]\n// after\nassert w.shape[0] == m.shape[-1]\njnp.cov(m, fweights=w)","handlingStrategy":"validation","validationCode":"n_obs = m.shape[-1]\nassert jnp.shape(fweights)[0] == n_obs, f'fweights len {jnp.shape(fweights)[0]} != {n_obs} samples'\njnp.cov(m, fweights=fweights)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Size weights to observations, not variables","Mind rowvar orientation","Recompute weights whenever data is filtered"],"tags":["jax","covariance","weights","length-mismatch"],"backgroundTag":"argument-shape-mismatch","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}