Skip to content

Conversation

@thisisamirv
Copy link

Add kernel functions with statistical properties and LOESS support

This PR prepares the foundation for KDE and LOESS implementations.

Summary

Major enhancement to the kernel module adding:

  1. Kernel density estimation (KDE) constants to consts.rs
  2. Complete rewrite of kernel.rs with AMISE efficiency metrics, dual evaluation modes, and LOESS integration
  3. Compile-time validation of statistical properties

Changes to consts.rs

  • Kernel variance (μ₂): ∫ u² K(u) du for 9 kernels
  • Kernel roughness (R(K)): ∫ K(u)² du for 9 kernels
  • Module docs explaining kernel properties and bandwidth conversion
  • References to Silverman (1986) Table 3.1 and Wand & Jones (1995)
  • Added missing SQRT_PI constant

Changes to kernel.rs

1. Statistical Correctness

  • Dual evaluation modes:
    • evaluate(): Normalized for KDE (integrates to 1)
    • evaluate_weight(): Unnormalized for LOESS (local regression weights)
  • AMISE properties: Efficiency, variance, roughness, bandwidth factors from Silverman (1986)
  • Compile-time validation: Ensures efficiency calculations match theoretical values

2. New Kernels & Renaming

  • Renamed QuarticBisquare (it's a more standard name)
  • Added Cosine kernel (high efficiency ≈ 0.9995)
  • Added Logistic kernel (heavy-tailed unbounded)
  • Added Sigmoid kernel (hyperbolic secant)
  • Fixed Sigmoid formula: was exp(πx), now correctly exp(x)

3. Enhanced API

  • KernelType enum: Runtime kernel selection for LOESS and other applications
  • CustomKernel: User-defined kernels with metadata
  • Batch operations: evaluate_batch(), compute_distance_weights()
  • Utilities: robust_reweights(), normalize_weights()
  • Recommendations: recommended_for_kde(), recommended_for_loess(), most_efficient()

4. Boundary Behavior Fix

  • Changed boundary handling from |x| <= 1 to |x| >= 1 for consistency
  • Support now returns open interval (-1, 1) as mathematically correct
  • All bounded kernels return 0 at exactly x = ±1

5. Documentation & Testing

  • Comprehensive table: Formula, support, efficiency, R(K), μ₂(K) for all kernels
  • Compile-time tests: Verify efficiency matches Silverman (1986) exact values
  • Many runtime tests: Integration, symmetry, monotonicity, normalization, edge cases

Breaking Changes

  1. Quartic → Bisquare: Renamed for statistical consistency
  2. Boundary behavior: |x| = 1 now returns 0 (was non-zero)
  3. Sigmoid formula: Fixed to match hyperbolic secant definition
  4. Tricube normalization: Now (70/81)(1-|x|³)³ (was unnormalized)

@codecov
Copy link

codecov bot commented Nov 10, 2025

Codecov Report

❌ Patch coverage is 84.49132% with 125 lines in your changes missing coverage. Please review.
✅ Project coverage is 93.88%. Comparing base (5da3470) to head (0588ded).
⚠️ Report is 6 commits behind head on master.

Files with missing lines Patch % Lines
src/function/kernel.rs 84.36% 124 Missing ⚠️
src/distribution/geometric.rs 92.30% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #359      +/-   ##
==========================================
- Coverage   94.99%   93.88%   -1.12%     
==========================================
  Files          61       59       -2     
  Lines       13615    13577      -38     
==========================================
- Hits        12934    12747     -187     
- Misses        681      830     +149     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@thisisamirv thisisamirv marked this pull request as draft November 10, 2025 17:41
@thisisamirv
Copy link
Author

Ok, this PR is becoming too long and complicated due to the many issues that I am addressing. I will create a more clean new PR instead.

@thisisamirv thisisamirv deleted the feat/kde_loess branch November 10, 2025 18:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant