{"record":{"id":"8737541798e2c0a0","repo":"jax-ml/jax","slug":"covariance-does-not-have-dimension-self-d","errorCode":null,"errorMessage":"covariance does not have dimension {self.d}","messagePattern":"covariance does not have dimension (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/scipy/stats/kde.py","lineNumber":150,"sourceCode":"    \"\"\"Evaluate the Gaussian KDE on the given points.\"\"\"\n    check_arraylike(\"evaluate\", points)\n    points = self._reshape_points(points)\n    result = _gaussian_kernel_eval(False, self.dataset.T, self.weights[:, None],\n                                   points.T, self.inv_cov)\n    return result[:, 0]\n\n  def __call__(self, points):\n    return self.evaluate(points)\n\n  def integrate_gaussian(self, mean, cov):\n    \"\"\"Integrate the distribution weighted by a Gaussian.\"\"\"\n    mean = jnp.atleast_1d(jnp.squeeze(mean))\n    cov = jnp.atleast_2d(cov)\n\n    if mean.shape != (self.d,):\n      raise ValueError(f\"mean does not have dimension {self.d}\")\n    if cov.shape != (self.d, self.d):\n      raise ValueError(f\"covariance does not have dimension {self.d}\")\n\n    chol = linalg.cho_factor(self.covariance + cov)\n    norm = jnp.sqrt(2 * np.pi)**self.d * jnp.prod(jnp.diag(chol[0]))\n    norm = 1.0 / norm\n    return _gaussian_kernel_convolve(chol, norm, self.dataset, self.weights,\n                                     mean)\n\n  @api.jit\n  def integrate_box_1d(self, low, high):\n    \"\"\"Integrate the distribution over the given limits.\"\"\"\n    if self.d != 1:\n      raise ValueError(\"integrate_box_1d() only handles 1D pdfs\")\n    if np.ndim(low) != 0 or np.ndim(high) != 0:\n      raise ValueError(\n          \"the limits of integration in integrate_box_1d must be scalars\")\n    sigma = jnp.squeeze(jnp.sqrt(self.covariance))\n    low = jnp.squeeze((low - self.dataset) / sigma)\n    high = jnp.squeeze((high - self.dataset) / sigma)","sourceCodeStart":132,"sourceCodeEnd":168,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/scipy/stats/kde.py#L132-L168","documentation":"In gaussian_kde.integrate_gaussian, cov is atleast_2d'd and must have shape exactly (self.d, self.d) to be added to the KDE covariance. Non-square, wrong-size, or wrongly-oriented matrices raise this error.","triggerScenarios":"Passing cov of shape (d, d+1), (1,) (becomes (1,1) while d > 1), or a full covariance of a different model with mismatched dimensionality.","commonSituations":"Mixing KDEs of different dimensionality in a mixture model; passing a precision/Cholesky factor instead of the covariance; passing a vector of variances instead of a matrix (use jnp.diag(variances)).","solutions":["Ensure cov.shape == (kde.d, kde.d)","If you have per-dimension variances, wrap with jnp.diag(variances)","If cov is a Cholesky factor, reconstruct the covariance via L @ L.T"],"exampleFix":"// before\nkde.integrate_gaussian(mu, variances)  # shape (d,)\n// after\nkde.integrate_gaussian(mu, jnp.diag(variances))","handlingStrategy":"validation","validationCode":"cov = jnp.atleast_2d(jnp.asarray(cov))\nassert cov.shape == (kde.d, kde.d), f'cov must be {(kde.d, kde.d)}'","typeGuard":"def cov_shape_ok(cov, kde) -> bool:\n    return jnp.asarray(cov).shape == (kde.d, kde.d)","tryCatchPattern":null,"preventionTips":["Wrap variance vectors with jnp.diag","Reconstruct covariance from factors via L @ L.T","Unit-test helper shapes against kde.d"],"tags":["jax","scipy","kde","covariance","shape-validation"],"backgroundTag":"invalid-matrix-shape","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}