jax-ml/jax · error · ValueError

`bw_method` should be 'scott', 'silverman', a scalar, or a c

Error message

`bw_method` should be 'scott', 'silverman', a scalar, or a callable.

What it means

The bw_method argument of gaussian_kde must be the string 'scott' or 'silverman', a non-string scalar, or a callable taking the KDE object. Anything else — including string typos or None passed positionally where a factor is expected — raises this ValueError at construction time.

Source

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

    else:
      dataset, = promote_dtypes_inexact(dataset)
      weights = jnp.full(n, 1.0 / n, dtype=dataset.dtype)

    self._setattr("dataset", dataset)
    self._setattr("weights", weights)
    neff = self._setattr("neff", 1 / jnp.sum(weights**2))

    bw_method = "scott" if bw_method is None else bw_method
    if bw_method == "scott":
      factor = jnp.power(neff, -1. / (d + 4))
    elif bw_method == "silverman":
      factor = jnp.power(neff * (d + 2) / 4.0, -1. / (d + 4))
    elif jnp.isscalar(bw_method) and not isinstance(bw_method, str):
      factor = cast(Array, bw_method)
    elif callable(bw_method):
      factor = bw_method(self)
    else:
      raise ValueError(
          "`bw_method` should be 'scott', 'silverman', a scalar, or a callable."
      )

    data_covariance = jnp.atleast_2d(
        jnp.cov(dataset, rowvar=True, bias=False, aweights=weights))
    data_inv_cov = jnp.linalg.inv(data_covariance)
    covariance = data_covariance * factor**2
    inv_cov = data_inv_cov / factor**2
    self._setattr("covariance", covariance)
    self._setattr("inv_cov", inv_cov)

  def _setattr(self, name, value):
    # Frozen dataclasses don't support setting attributes so we have to
    # overload that operation here as they do in the dataclass implementation
    object.__setattr__(self, name, value)
    return value

  def tree_flatten(self):

View on GitHub (pinned to 1e1c6a8fc0)

Solutions

  1. Use exactly 'scott', 'silverman', a numeric scalar (e.g. 0.5), or a callable like lambda kde: kde.n ** -0.2
  2. Fix case and spelling of the string
  3. If bandwidth came from config as a string number, convert to float before passing

Example fix

// before
kde = gaussian_kde(data, bw_method='0.5')
// after
kde = gaussian_kde(data, bw_method=0.5)
Defensive patterns

Strategy: validation

Validate before calling

import jnp
valid = bw in ('scott', 'silverman') or (callable(bw)) or (not isinstance(bw, str) and jnp.isscalar(bw))
assert valid

Type guard

def bw_method_valid(bw) -> bool:
    return bw in ('scott', 'silverman') or callable(bw) or (not isinstance(bw, str) and not hasattr(bw, '__len__'))

Prevention

When it happens

Trigger: gaussian_kde(data, bw_method='Scott') (wrong case), bw_method='hansen', or passing a string that is neither 'scott' nor 'silverman'.

Common situations: Porting scipy code with a custom bandwidth name scipy accepts; case-sensitivity mistakes; passing a string-form number like '0.5' instead of the float 0.5.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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