Stepwise Noise Peeling (SNP) for Nadaraya–Watson Regression
Project description
SNP: Stepwise Noise Peeling for Nadaraya-Watson Regression
The SNP package implements the Stepwise Noise Peeling algorithm that bypasses bandwidth selection in Nadaraya-Watson regression by using iterative smoothing. SNP provides a scalable alternative to Direct Generalized Cross-Validation (DGCV) by converting continuous bandwidth optimization into discrete iteration selection, dramatically reducing computational cost while maintaining statistical equivalence.
Installation
You can install the development version of SNP from GitHub with:
# Install from GitHub
pip install https://github.com/bistoonh/SNP-Python/releases/download/v0.2.4/pysnp-0.2.4-py3-none-any.whl
# Or install from PyPI (when published)
pip install pysnp
Quick Start
import numpy as np
from snp import SNP, DGCV
import matplotlib.pyplot as plt
# Generate sample data
np.random.seed(123)
n = 2000
x = np.sort(np.random.uniform(0, 1, n))
y = np.sin(2*np.pi*x) + np.random.normal(0, 0.35, n)
# Apply SNP smoothing with default parameters
snp_result = SNP(x, y)
# Compare with traditional DGCV
dgcv_result = DGCV(x, y)
# Plot results
plt.figure(figsize=(10, 6))
plt.scatter(x, y, s=5, c="gray", alpha=0.7, label="Data")
plt.plot(x, snp_result['y_k_opt'], 'r-', linewidth=2, label="SNP")
plt.plot(x, dgcv_result['y_h_opt'], 'b--', linewidth=2, label="DGCV")
plt.title("SNP vs DGCV Comparison")
plt.legend()
plt.show()
# Performance comparison
y_true = np.sin(2*np.pi*x) # True function without noise
rmse_snp = np.sqrt(np.mean((snp_result['y_k_opt'] - y_true)**2))
rmse_dgcv = np.sqrt(np.mean((dgcv_result['y_h_opt'] - y_true)**2))
print(f"SNP time: {snp_result['time_elapsed']:.4f} seconds")
print(f"DGCV time: {dgcv_result['time_elapsed']:.4f} seconds")
print(f"SNP RMSE: {rmse_snp:.4f}")
print(f"DGCV RMSE: {rmse_dgcv:.4f}")
Parameter Tuning
SNP provides two key parameters for balancing speed and accuracy:
# Faster computation (fewer bandwidth candidates and slices)
snp_fast = SNP(x, y, num_h_points=20, num_slices=30)
# More thorough search (more bandwidth candidates and slices)
snp_thorough = SNP(x, y, num_h_points=60, num_slices=100)
# Performance comparison
print(f"Fast SNP time: {snp_fast['time_elapsed']:.4f} seconds")
print(f"Thorough SNP time: {snp_thorough['time_elapsed']:.4f} seconds")
Key Features
- Fast: Orders of magnitude faster than DGCV for large datasets
- Accurate: Statistically equivalent results to DGCV
- Adaptive: Automatically adjusts bandwidth through iterative process
- Configurable: Tunable parameters for speed vs accuracy trade-offs
- Robust: Handles edge cases and various data sizes
- Well-documented: Comprehensive help files and examples
Algorithm Overview
SNP operates in two phases:
-
Phase I: Constructs a conservative initial bandwidth using random slices of data and lightweight GCV within each slice
num_slices: Controls number of random data slices (default: 60)num_h_points: Controls bandwidth candidates per slice (default: 40)
-
Phase II: Fixes the smoothing operator and repeatedly applies it, selecting optimal iterations via discrete GCV
This reformulation preserves the adaptivity of GCV while converting costly continuous bandwidth search into lightweight discrete selection.
Main Functions
SNP(x, y, num_h_points=40, num_slices=60): Main Stepwise Noise Peeling algorithmDGCV(x, y, num_h_points=50): Direct Generalized Cross-Validation (reference method)construct_W(x, h): Construct Gaussian kernel weight matrixexample_stepwise(): Stepwise function demonstrationexample_wavy(): Complex wavy function demonstrationexample_california_housing(): Real dataset example
Performance
For datasets with n > 1000, SNP typically shows:
- Speed: Orders of magnitude faster than DGCV
- Accuracy: < 1% difference in RMSE compared to DGCV
- Memory: More efficient memory usage due to iterative approach
- Scalability: Parameter tuning allows adaptation to computational constraints
Examples
The package includes comprehensive examples that demonstrate SNP performance:
from snp import example_stepwise
# Run stepwise function example
example_stepwise()
Requirements
- Python >= 3.7
- numpy >= 1.19.0
- scipy >= 1.6.0
Optional dependencies for examples:
- matplotlib >= 3.3.0 (for plotting)
- pandas >= 1.2.0 (for California housing example)
Citation
If you use this package in your research, please cite:
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Issues
Found a bug? Have a feature request? Please open an issue on GitHub.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pysnp-0.2.4.tar.gz.
File metadata
- Download URL: pysnp-0.2.4.tar.gz
- Upload date:
- Size: 11.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ce59ff904222902a93ff659a731e790ad88cd7a481b61ce44eb83d0e947a1672
|
|
| MD5 |
dc178d6bb85d7fd6dd4c22435412b9fd
|
|
| BLAKE2b-256 |
af5fc21c4cd074b223d7c0e25d98655b5f02298617ede1daebcf2ef05281cdec
|
File details
Details for the file pysnp-0.2.4-py3-none-any.whl.
File metadata
- Download URL: pysnp-0.2.4-py3-none-any.whl
- Upload date:
- Size: 12.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
67fc380d8c5f35eb276c67fae66f17615b3ebbc73d352fcce646d4e50b40d357
|
|
| MD5 |
e3d7c7e85b6482539c02abcb5a064815
|
|
| BLAKE2b-256 |
5f5ce54b334b287c878547235af1478a0db0650c4cf16d5f35354d4141ed1d3c
|