use crate::Kernel;
pub fn kweight(x: &[f64], c: f64, h: f64, kernel: Kernel) -> Vec<f64> {
x.iter()
.map(|&xi| {
let u = (xi - c) / h;
let au = u.abs();
if au > 1.0 {
0.0
} else {
match kernel {
Kernel::Triangular => (1.0 - au) / h,
Kernel::Epanechnikov => 0.75 * (1.0 - u * u) / h,
Kernel::Uniform => 0.5 / h,
}
}
})
.collect()
}
pub fn eff_weight(x: &[f64], c: f64, h: f64, kernel: Kernel, wt: Option<&[f64]>) -> Vec<f64> {
let mut w = kweight(x, c, h, kernel);
if let Some(u) = wt {
for (wi, &ui) in w.iter_mut().zip(u.iter()) {
*wi *= ui;
}
}
w
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn triangular_matches_closed_form() {
let w = kweight(&[0.0, 0.5, 1.0, 2.0], 0.0, 1.0, Kernel::Triangular);
assert_eq!(w, vec![1.0, 0.5, 0.0, 0.0]);
}
#[test]
fn epanechnikov_matches_closed_form() {
let w = kweight(&[0.0, 0.5, 1.0, 2.0], 0.0, 1.0, Kernel::Epanechnikov);
assert_eq!(w, vec![0.75, 0.5625, 0.0, 0.0]);
}
#[test]
fn uniform_is_inclusive_at_boundary() {
let w = kweight(&[0.5, 1.0, 1.0001], 0.0, 1.0, Kernel::Uniform);
assert_eq!(w, vec![0.5, 0.5, 0.0]);
}
#[test]
fn bandwidth_scales_weights() {
let w = kweight(&[1.0], 0.0, 2.0, Kernel::Triangular);
assert_eq!(w, vec![0.25]);
}
}