code: def test_kernel_density(arrays, kernel, metric, bandwidth):
      X, X_test, sample_weight = arrays
      X_np, X_test_np, sample_weight_np = as_type("numpy", *arrays)
  
      if kernel == "cosine":
          # cosine is numerically unstable at high dimensions
          # for both cuml and sklearn
          assume(X.shape[1] <= 20)
      kde = KernelDensity(
          kernel=kernel, metric=metric, bandwidth=bandwidth
      ).fit(X, sample_weight=sample_weight)
      cuml_prob = kde.score_samples(X)
      cuml_prob_test = kde.score_samples(X_test)
  
      if X_np.dtype == np.float64:
          ref_prob = compute_kernel_naive(
              X_np, X_np, kernel, metric, bandwidth, sample_weight_np
          )
          ref_prob_test = compute_kernel_naive(
              X_test_np,
              X_np,
              kernel,
              metric,
              bandwidth,
              sample_weight_np,
          )
          tol = 1e-3
          assert np.allclose(
              np.exp(as_type("numpy", cuml_prob)),
              ref_prob,
              rtol=tol,
              atol=tol,
              equal_nan=True,
          )
          assert np.allclose(
              np.exp(as_type("numpy", cuml_prob_test)),
              ref_prob_test,
              rtol=tol,
              atol=tol,
              equal_nan=True,
          )
  
      if kernel in ["gaussian", "tophat"] and metric == "euclidean":
          sample = kde.sample(100, random_state=32).get()
          nearest = skl_pairwise_distances(sample, X_np, metric=metric)
          nearest = nearest.min(axis=1)
          if kernel == "gaussian":
              from scipy.stats import chi
  
              # The euclidean distance of each sample from its cluster
              # follows a chi distribution (not squared) with DoF=dimension
              # and scale = bandwidth
              # Fail the test if the largest observed distance
              # is vanishingly unlikely
              assert (
                  chi.sf(nearest.max(), X.shape[1], scale=bandwidth)
                  > 1e-8
              )
          elif kernel == "tophat":
              assert np.all(nearest <= bandwidth)
      else:
          with pytest.raises(
              NotImplementedError,
              match=r"Only \['gaussian', 'tophat'\] kernels,"
              " and the euclidean metric are supported.",
          ):
              kde.sample(100)
  
file_path: cuml/python/cuml/tests/test_kernel_density.py
imports: from cuml.testing.utils import as_type
  from sklearn.model_selection import GridSearchCV
  import pytest
  from hypothesis.extra.numpy import arrays
  from hypothesis import given, settings, assume, strategies
  from cuml.neighbors import KernelDensity, VALID_KERNELS, logsumexp_kernel
  from cuml.common.exceptions import NotFittedError
  from sklearn.metrics import pairwise_distances
  from sklearn.neighbors._ball_tree import kernel_norm
  from cuml.internals.safe_imports import cpu_only_import
label_desc: Use image node_id 3 for calling a global function with example usage: test_kernel_density(arrays, kernel, metric, bandwidth) without return types