jax-ml/jax · error · ValueError
precision matrix must match data dims
Error message
precision matrix must match data dims
What it means
In _gaussian_kernel_eval the precision matrix must be a full (d, d) matrix matching the data dimensionality, because it is Cholesky-factorized for whitening. Passing a diagonal vector of precisions, a scalar, or a wrongly sized matrix raises this error.
Source
Thrown at jax/_src/scipy/stats/kde.py:264
def _gaussian_kernel_convolve(chol, norm, target, weights, mean):
diff = target - mean[:, None]
alpha = linalg.cho_solve(chol, diff)
arg = 0.5 * jnp.sum(diff * alpha, axis=0)
return norm * jnp.sum(jnp.exp(-arg) * weights)
@api.jit(static_argnums=0)
def _gaussian_kernel_eval(in_log, points, values, xi, precision):
points, values, xi, precision = promote_dtypes_inexact(
points, values, xi, precision)
d = points.shape[1]
if xi.shape[1] != d:
raise ValueError("points and xi must have same trailing dim")
if precision.shape != (d, d):
raise ValueError("precision matrix must match data dims")
whitening = linalg.cholesky(precision, lower=True)
points = jnp.dot(points, whitening)
xi = jnp.dot(xi, whitening)
log_norm = jnp.sum(jnp.log(
jnp.diag(whitening))) - 0.5 * d * jnp.log(2 * np.pi)
def kernel(x_test, x_train, y_train):
arg = log_norm - 0.5 * jnp.sum(jnp.square(x_train - x_test))
if in_log:
return jnp.log(y_train) + arg
else:
return y_train * jnp.exp(arg)
reduce = special.logsumexp if in_log else jnp.sum
reduced_kernel = lambda x: reduce(api.vmap(kernel, in_axes=(None, 0, 0))
(x, points, values),
axis=0)View on GitHub (pinned to 1e1c6a8fc0)
Solutions
- Wrap per-dimension precisions into a diagonal matrix: jnp.diag(precision_vec)
- Ensure precision.shape == (d, d) where d == points.shape[1]
- If precision is a Cholesky factor L, pass L @ L.T
Example fix
// before vals = _gaussian_kernel_eval(False, pts, vals, xi, prec_vec) # shape (d,) // after vals = _gaussian_kernel_eval(False, pts, vals, xi, jnp.diag(prec_vec))
Defensive patterns
Strategy: validation
Validate before calling
d = points.shape[1]
if precision.ndim == 1:
precision = jnp.diag(precision)
assert precision.shape == (d, d) Type guard
def precision_matches(precision, points) -> bool:
d = jnp.asarray(points).shape[1]
return jnp.asarray(precision).shape == (d, d) Prevention
- Never pass precision vectors; always full (d, d) matrices
- Build precision from L @ L.T when starting from a Cholesky factor
- Validate matrix shapes in kernel unit tests
When it happens
Trigger: Direct calls to _gaussian_kernel_eval with precision of shape (d,), (1,), or (k, k) with k != d; upstream, this usually surfaces as a wrongly-built inv_cov passed through evaluate.
Common situations: Constructing a per-feature precision vector instead of a matrix; using a Cholesky factor or correlation matrix of the wrong size when wiring custom precision into the KDE.
Related errors
- `weights` input should be one-dimensional.
- mean does not have dimension {self.d}
- covariance does not have dimension {self.d}
- KDEs are not the same dimensionality
- points have dimension {}, dataset has dimension {}
AI-assisted analysis of jax-ml/jax@1e1c6a8fc0 (2026-08-27).
Data as JSON: /api/errors/7c87aa96bde59a99.
Report an issue: GitHub.