Skip to main content

kde

Function kde 

Source
pub fn kde(
    data: &[f64],
    method: BandwidthMethod,
    n_points: usize,
) -> Option<KdeResult>
Expand description

Gaussian kernel density estimation.

§Algorithm

f̂(x) = (1/nh) Σᵢ K((x - xᵢ)/h)

where K(u) = (1/√(2π)) exp(-u²/2) is the Gaussian kernel and h is the bandwidth (smoothing parameter).

The evaluation grid extends 3h beyond the data range to capture tail contributions from boundary points (3σ covers 99.7% of a Gaussian).

Reference: Silverman (1986), “Density Estimation for Statistics and Data Analysis”

§Parameters

  • data: Sample observations (must have ≥ 2 finite values)
  • method: Bandwidth selection method
  • n_points: Number of evaluation grid points (typical: 256–1024)

§Returns

None if fewer than 2 data points, fewer than 2 grid points, non-finite values, or zero variance (for automatic bandwidth methods).

§Examples

use u_analytics::distribution::{kde, BandwidthMethod};

let data = [1.0, 1.1, 1.2, 2.0, 2.1, 2.2, 5.0];
let result = kde(&data, BandwidthMethod::Silverman, 512).unwrap();
assert_eq!(result.x.len(), 512);
assert_eq!(result.density.len(), 512);
assert!(result.bandwidth > 0.0);
// Density should integrate approximately to 1
let dx = result.x[1] - result.x[0];
let integral: f64 = result.density.iter().sum::<f64>() * dx;
assert!((integral - 1.0).abs() < 0.05);