jax-ml/jax · error · ValueError

integrate_box_1d() only handles 1D pdfs

Error message

integrate_box_1d() only handles 1D pdfs

What it means

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.

Source

Thrown at jax/_src/scipy/stats/kde.py:162

    mean = jnp.atleast_1d(jnp.squeeze(mean))
    cov = jnp.atleast_2d(cov)

    if mean.shape != (self.d,):
      raise ValueError(f"mean does not have dimension {self.d}")
    if cov.shape != (self.d, self.d):
      raise ValueError(f"covariance does not have dimension {self.d}")

    chol = linalg.cho_factor(self.covariance + cov)
    norm = jnp.sqrt(2 * np.pi)**self.d * jnp.prod(jnp.diag(chol[0]))
    norm = 1.0 / norm
    return _gaussian_kernel_convolve(chol, norm, self.dataset, self.weights,
                                     mean)

  @api.jit
  def integrate_box_1d(self, low, high):
    """Integrate the distribution over the given limits."""
    if self.d != 1:
      raise ValueError("integrate_box_1d() only handles 1D pdfs")
    if np.ndim(low) != 0 or np.ndim(high) != 0:
      raise ValueError(
          "the limits of integration in integrate_box_1d must be scalars")
    sigma = jnp.squeeze(jnp.sqrt(self.covariance))
    low = jnp.squeeze((low - self.dataset) / sigma)
    high = jnp.squeeze((high - self.dataset) / sigma)
    return jnp.sum(self.weights * (special.ndtr(high) - special.ndtr(low)))

  def integrate_kde(self, other):
    """Integrate the product of two Gaussian KDE distributions."""
    if other.d != self.d:
      raise ValueError("KDEs are not the same dimensionality")

    chol = linalg.cho_factor(self.covariance + other.covariance)
    norm = jnp.sqrt(2 * np.pi)**self.d * jnp.prod(jnp.diag(chol[0]))
    norm = 1.0 / norm

    sm, lg = (self, other) if self.n < other.n else (other, self)

View on GitHub (pinned to 1e1c6a8fc0)

Solutions

  1. For d == 1, construct the KDE from a 1-D dataset: gaussian_kde(x) with x.ndim == 1 so atleast_2d yields (1, n)
  2. For multivariate integrals, use Monte Carlo sampling from kde.resample or evaluate the pdf on a grid and numerically integrate
  3. Note integrate_box is not implemented in JAX at all, so there is no built-in N-D box integral

Example fix

// before
kde2d = gaussian_kde(pts)  # pts.shape == (2, n)
p = kde2d.integrate_box_1d(0.0, 1.0)
// after
samples = kde2d.resample(100_000, seed=key)
p = ((samples[0] >= 0.0) & (samples[0] <= 1.0)).mean()
Defensive patterns

Strategy: validation

Validate before calling

assert kde.d == 1, 'integrate_box_1d requires a 1-D KDE'

Type guard

def kde_is_1d(kde) -> bool:
    return kde.d == 1

Prevention

When it happens

Trigger: Fitting gaussian_kde on a (2, n) dataset and then calling kde.integrate_box_1d(lo, hi).

Common situations: 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.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of jax-ml/jax@1e1c6a8fc0 (2026-08-27). Data as JSON: /api/errors/e8b4590bc66ae146. Report an issue: GitHub.