{"record":{"id":"4841f8dbe7530dc3","repo":"jax-ml/jax","slug":"cannot-handle-multidimensional-fweights","errorCode":null,"errorMessage":"cannot handle multidimensional fweights","messagePattern":"cannot handle multidimensional fweights","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"jax/_src/numpy/lax_numpy.py","lineNumber":9212,"sourceCode":"    return array([]).reshape(0, 0)\n\n  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)","sourceCodeStart":9194,"sourceCodeEnd":9230,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/numpy/lax_numpy.py#L9194-L9230","documentation":"jnp.cov supports optional frequency weights (fweights) which must be a 1D array with one weight per observation. If np.ndim(fweights) > 1, RuntimeError('cannot handle multidimensional fweights') is raised, mirroring NumPy.","triggerScenarios":"jnp.cov(m, fweights=w) where w has 2 or more dimensions, e.g. a (n, 1) column vector instead of (n,) weights.","commonSituations":"Passing weights that kept a trailing axis after slicing (w[:, None]) or batched weight matrices; forgetting to squeeze a column vector.","solutions":["Flatten weights: fweights=w.ravel() or w.squeeze()","Ensure weights shape is (n_observations,) matching X.shape[1]","Squeeze stray axes introduced by keepdims=True upstream"],"exampleFix":"// before\njnp.cov(m, fweights=w[:, None])  # ValueError (RuntimeError)\n// after\njnp.cov(m, fweights=w.ravel())","handlingStrategy":"validation","validationCode":"if fweights is not None:\n    fweights = jnp.asarray(fweights).ravel()\njnp.cov(m, fweights=fweights)","typeGuard":"def valid_fweights(w, n_obs) -> bool:\n    w = jnp.asarray(w)\n    return w.ndim == 1 and w.shape[0] == n_obs","tryCatchPattern":null,"preventionTips":["Squeeze weights to 1D","Avoid keepdims=True when producing weights","Match weight length to observation count"],"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"}