{"record":{"id":"8df6a760734f7f87","repo":"jax-ml/jax","slug":"cannot-handle-multidimensional-aweights","errorCode":null,"errorMessage":"cannot handle multidimensional aweights","messagePattern":"cannot handle multidimensional aweights","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"jax/_src/numpy/lax_numpy.py","lineNumber":9222,"sourceCode":"\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\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:","sourceCodeStart":9204,"sourceCodeEnd":9240,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/numpy/lax_numpy.py#L9204-L9240","documentation":"Observation weights (aweights) in jnp.cov must be a 1D array with one weight per observation. If np.ndim(aweights) > 1, RuntimeError('cannot handle multidimensional aweights') is raised.","triggerScenarios":"jnp.cov(m, aweights=W) where W is 2D, e.g. a full weight matrix or a (n, 1) array from slicing with keepdims.","commonSituations":"Passing a covariance weighting matrix instead of per-observation weights; weights retaining extra axes after reductions.","solutions":["Flatten: aweights=W.ravel()","Check aweights.ndim == 1 and length equals observation count before calling","Recompute weights as a 1D per-observation vector"],"exampleFix":"// before\njnp.cov(m, aweights=w_2d)\n// after\njnp.cov(m, aweights=w_2d.ravel())","handlingStrategy":"validation","validationCode":"if aweights is not None:\n    aweights = jnp.asarray(aweights).ravel()\njnp.cov(m, aweights=aweights)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep aweights 1D per observation","Flatten weight matrices before use","Squeeze after reductions"],"tags":["jax","covariance","weights","shape-validation"],"backgroundTag":"shape-validation-failed","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}