{"record":{"id":"6c0d3881698de6a8","repo":"jax-ml/jax","slug":"kdes-are-not-the-same-dimensionality","errorCode":null,"errorMessage":"KDEs are not the same dimensionality","messagePattern":"KDEs are not the same dimensionality","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/scipy/stats/kde.py","lineNumber":174,"sourceCode":"                                     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)\n    return jnp.sum(self.weights * (special.ndtr(high) - special.ndtr(low)))\n\n  def integrate_kde(self, other):\n    \"\"\"Integrate the product of two Gaussian KDE distributions.\"\"\"\n    if other.d != self.d:\n      raise ValueError(\"KDEs are not the same dimensionality\")\n\n    chol = linalg.cho_factor(self.covariance + other.covariance)\n    norm = jnp.sqrt(2 * np.pi)**self.d * jnp.prod(jnp.diag(chol[0]))\n    norm = 1.0 / norm\n\n    sm, lg = (self, other) if self.n < other.n else (other, self)\n    result = api.vmap(partial(_gaussian_kernel_convolve, chol, norm, lg.dataset,\n                              lg.weights),\n                      in_axes=1)(sm.dataset)\n    return jnp.sum(result * sm.weights)\n\n  @api.jit(static_argnames=(\"shape\",))\n  def resample(self, key, shape=()):\n    r\"\"\"Randomly sample a dataset from the estimated pdf\n\n    Args:\n      key: a PRNG key used as the random key.\n      shape: optional, a tuple of nonnegative integers specifying the result","sourceCodeStart":156,"sourceCodeEnd":192,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/scipy/stats/kde.py#L156-L192","documentation":"gaussian_kde.integrate_kde(other) computes the convolution of two Gaussian KDEs, which requires both KDEs to have the same dimensionality (other.d == self.d). Mismatched dimensionalities — e.g. a 1-D KDE against a 2-D KDE — raise this error before the covariance sum.","triggerScenarios":"kde1.integrate_kde(kde2) where kde1 was built from shape (1, n) data and kde2 from (2, m) data.","commonSituations":"Comparing densities of features with different dimensionalities in a two-sample test; one dataset accidentally kept an extra leading axis so atleast_2d produced a different d.","solutions":["Verify d with kde.dataset.shape[0] for both objects and make the input layouts consistent","Rebuild KDEs from consistently shaped data (1-D vectors for 1-D KDEs)","If the dimensionalities genuinely differ, compare marginals by fitting KDEs on the same feature subset"],"exampleFix":"// before\np = kde_a.integrate_kde(kde_b)  # d=1 vs d=2\n// after\nkde_b1 = gaussian_kde(pts_b[0])  # marginal of first dim\np = kde_a.integrate_kde(kde_b1)","handlingStrategy":"validation","validationCode":"assert kde_a.d == kde_b.d, 'KDE dimensionality mismatch'","typeGuard":"def same_dimensionality(a, b) -> bool:\n    return a.d == b.d","tryCatchPattern":null,"preventionTips":["Compare kde.dataset.shape[0] before integrate_kde","Fit KDEs from consistent array layouts","Compare marginals when dimensionalities differ"],"tags":["jax","scipy","kde","dimensionality","shape-validation"],"backgroundTag":"dimension-mismatch","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}