{"record":{"id":"52a4867b5a799c1d","repo":"jax-ml/jax","slug":"gaussian-kde-does-not-support-complex-data","errorCode":null,"errorMessage":"gaussian_kde does not support complex data","messagePattern":"gaussian_kde does not support complex data","errorType":"exception","errorClass":"NotImplementedError","httpStatus":null,"severity":"error","filePath":"jax/_src/scipy/stats/kde.py","lineNumber":58,"sourceCode":"\n  Parameters:\n    dataset: arraylike, real-valued. Data from which to estimate the distribution.\n      If 1D, shape is (n_data,). If 2D, shape is (n_dimensions, n_data).\n    bw_method: string, scalar, or callable. Either \"scott\", \"silverman\", a scalar\n      value, or a callable function which takes ``self`` as a parameter.\n    weights: arraylike, optional. Weights of the same shape as the dataset.\n  \"\"\"\n  neff: Any\n  dataset: Any\n  weights: Any\n  covariance: Any\n  inv_cov: Any\n\n  def __init__(self, dataset, bw_method: BwMethod = None, weights=None):\n    check_arraylike(\"gaussian_kde\", dataset)\n    dataset = jnp.atleast_2d(dataset)\n    if dtypes.issubdtype(lax.dtype(dataset), np.complexfloating):\n      raise NotImplementedError(\"gaussian_kde does not support complex data\")\n    if not dataset.size > 1:\n      raise ValueError(\"`dataset` input should have multiple elements.\")\n\n    d, n = dataset.shape\n    if weights is not None:\n      check_arraylike(\"gaussian_kde\", weights)\n      dataset, weights = promote_dtypes_inexact(dataset, weights)\n      weights = jnp.atleast_1d(weights)\n      weights /= jnp.sum(weights)\n      if weights.ndim != 1:\n        raise ValueError(\"`weights` input should be one-dimensional.\")\n      if len(weights) != n:\n        raise ValueError(\"`weights` input should be of length n\")\n    else:\n      dataset, = promote_dtypes_inexact(dataset)\n      weights = jnp.full(n, 1.0 / n, dtype=dataset.dtype)\n\n    self._setattr(\"dataset\", dataset)","sourceCodeStart":40,"sourceCodeEnd":76,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/scipy/stats/kde.py#L40-L76","documentation":"jax.scipy.stats.gaussian_kde explicitly rejects complex-valued datasets because the covariance/Cholesky machinery is implemented for real floating types only. This mirrors an equivalent restriction historically present in scipy.","triggerScenarios":"Constructing gaussian_kde with a complex64/complex128 array, e.g. jax.scipy.stats.gaussian_kde(jnp.array([1+2j, 3-1j, ...])).","commonSituations":"Applying KDE to FFT output or signal-processing data left in complex dtype after a transform; dtype promotion accidentally producing complex after mixing complex and real arrays.","solutions":["Take real (or imaginary/absolute) part of the data before fitting: data = data.real","Convert to real explicitly: dataset = dataset.astype(jnp.float64)","If complex KDE is genuinely needed, fit separate KDEs on real and imaginary parts"],"exampleFix":"// before\nkde = gaussian_kde(fft_result)  # complex\n// after\nkde = gaussian_kde(fft_result.real)","handlingStrategy":"type-guard","validationCode":"data = jnp.real(jnp.asarray(data)) if jnp.iscomplexobj(data) else data","typeGuard":"def is_real_dataset(data) -> bool:\n    return not jnp.iscomplexobj(jnp.asarray(data))","tryCatchPattern":null,"preventionTips":["Strip complex dtype right after FFT-based pipelines","Standardize on float32/float64 inputs for statistical routines","Check jnp.iscomplexobj in data loaders"],"tags":["jax","scipy","kde","complex-dtype"],"backgroundTag":"unsupported-dtype","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}