{"record":{"id":"13cc92e7beecc25a","repo":"jax-ml/jax","slug":"incompatible-numbers-of-samples-and-aweights","errorCode":null,"errorMessage":"incompatible numbers of samples and aweights","messagePattern":"incompatible numbers of samples and aweights","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"jax/_src/numpy/lax_numpy.py","lineNumber":9224,"sourceCode":"    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\n  avg, w_sum = reductions.average(X, axis=1, weights=w, returned=True)\n  w_sum = w_sum[0]\n\n  if w is None:\n    f = X.shape[1] - ddof\n  elif ddof == 0:\n    f = w_sum\n  elif aweights is None:\n    f = w_sum - ddof\n  else:","sourceCodeStart":9206,"sourceCodeEnd":9242,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/numpy/lax_numpy.py#L9206-L9242","documentation":"Observation weights (aweights) passed to jnp.cov must have length equal to the number of observations X.shape[1]. If np.shape(aweights)[0] != X.shape[1], RuntimeError('incompatible numbers of samples and aweights') is raised.","triggerScenarios":"jnp.cov(m, y=None, aweights=np.ones(4)) with m of shape (2, 3) (3 observations); weights sized to variables rather than samples.","commonSituations":"rowvar confusion (weights computed for rows when columns are observations); subsampling data without adjusting weights.","solutions":["Match weight length to observation count: len(aweights) == m.shape[1] (or m.shape[0] with rowvar=False)","Slice or recompute weights after filtering data","Add an assertion before the call"],"exampleFix":"// before\njnp.cov(m, aweights=w)  # len(w) != n_obs\n// after\nassert w.shape[0] == m.shape[-1]\njnp.cov(m, aweights=w)","handlingStrategy":"validation","validationCode":"n_obs = m.shape[-1]\nassert jnp.shape(aweights)[0] == n_obs, 'aweights length must equal number of samples'\njnp.cov(m, aweights=aweights)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Match aweights length to X.shape[1]","Adjust weights after subsampling","Account for rowvar orientation"],"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"}