{"record":{"id":"e8b4590bc66ae146","repo":"jax-ml/jax","slug":"integrate-box-1d-only-handles-1d-pdfs","errorCode":null,"errorMessage":"integrate_box_1d() only handles 1D pdfs","messagePattern":"integrate_box_1d\\(\\) only handles 1D pdfs","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/scipy/stats/kde.py","lineNumber":162,"sourceCode":"    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)\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)","sourceCodeStart":144,"sourceCodeEnd":180,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/scipy/stats/kde.py#L144-L180","documentation":"gaussian_kde.integrate_box_1d computes a closed-form 1-D Gaussian-mixture CDF difference and therefore only exists for 1-D KDEs (self.d == 1). Calling it on a KDE fitted to 2-D or higher data raises this error.","triggerScenarios":"Fitting gaussian_kde on a (2, n) dataset and then calling kde.integrate_box_1d(lo, hi).","commonSituations":"Generalizing working 1-D code to multivariate data without switching integration strategy; forgetting that jnp.atleast_2d makes a (n,) input into (1, n), so a 1-D KDE must be built from a flat vector.","solutions":["For d == 1, construct the KDE from a 1-D dataset: gaussian_kde(x) with x.ndim == 1 so atleast_2d yields (1, n)","For multivariate integrals, use Monte Carlo sampling from kde.resample or evaluate the pdf on a grid and numerically integrate","Note integrate_box is not implemented in JAX at all, so there is no built-in N-D box integral"],"exampleFix":"// before\nkde2d = gaussian_kde(pts)  # pts.shape == (2, n)\np = kde2d.integrate_box_1d(0.0, 1.0)\n// after\nsamples = kde2d.resample(100_000, seed=key)\np = ((samples[0] >= 0.0) & (samples[0] <= 1.0)).mean()","handlingStrategy":"validation","validationCode":"assert kde.d == 1, 'integrate_box_1d requires a 1-D KDE'","typeGuard":"def kde_is_1d(kde) -> bool:\n    return kde.d == 1","tryCatchPattern":null,"preventionTips":["Build 1-D KDEs from flat 1-D arrays so atleast_2d yields (1, n)","Use Monte Carlo from resample for N-D mass","Check kde.dataset.shape[0] before dimension-specific calls"],"tags":["jax","scipy","kde","dimensionality"],"backgroundTag":"unsupported-operation","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}