Usage Examples

Example 1: Basic Smoothing

import numpy as np
from epa_smoothing import epa_local_median, silverman_bandwidth

# Generate noisy data
np.random.seed(42)
x = np.linspace(0, 2*np.pi, 100)
y_true = np.sin(x)
y_noisy = y_true + np.random.normal(0, 0.3, len(x))

# Smooth with automatic bandwidth
h = silverman_bandwidth(x)
y_smooth = epa_local_median(x, y_noisy, bandwidth=h)

print(f"Bandwidth: {h:.3f}")
print(f"RMSE: {np.sqrt(np.mean((y_smooth - y_true)**2)):.3f}")

Example 2: Handling Outliers

import numpy as np
from epa_smoothing import epa_local_median, epa_local_mean

# Data with outliers
np.random.seed(42)
x = np.linspace(0, 10, 100)
y_true = np.sin(x)
y_noisy = y_true + np.random.normal(0, 0.2, len(x))

# Add 10% outliers
outlier_idx = np.random.choice(100, 10, replace=False)
y_noisy[outlier_idx] += np.random.choice([-1, 1], 10) * 5

# Compare smoothers
h = 0.8
y_median = epa_local_median(x, y_noisy, bandwidth=h)
y_mean = epa_local_mean(x, y_noisy, bandwidth=h)

rmse_median = np.sqrt(np.mean((y_median - y_true)**2))
rmse_mean = np.sqrt(np.mean((y_mean - y_true)**2))

print(f"RMSE Median: {rmse_median:.3f}")
print(f"RMSE Mean: {rmse_mean:.3f}")
print(f"Improvement: {(rmse_mean - rmse_median)/rmse_mean*100:.1f}%")

Example 3: Bandwidth Selection

import numpy as np
from epa_smoothing import epa_local_median

x = np.linspace(0, 2*np.pi, 150)
y_true = np.sin(x) + 0.5*np.cos(2*x)
y_noisy = y_true + np.random.normal(0, 0.25, len(x))

# Test different bandwidths
bandwidths = [0.2, 0.4, 0.6, 0.8, 1.0]
for h in bandwidths:
    y_smooth = epa_local_median(x, y_noisy, bandwidth=h)
    rmse = np.sqrt(np.mean((y_smooth - y_true)**2))
    print(f"h={h:.1f}: RMSE={rmse:.3f}")

Example 4: Custom Evaluation Points

import numpy as np
from epa_smoothing import epa_local_median

# Sparse input data
x_data = np.sort(np.random.uniform(0, 10, 50))
y_data = np.sin(x_data) + np.random.normal(0, 0.3, 50)

# Dense evaluation grid
x_eval = np.linspace(0, 10, 200)

# Smooth at evaluation points
y_smooth = epa_local_median(x_data, y_data, x_eval=x_eval, bandwidth=0.5)

Example 5: Real-World Template

import numpy as np
import pandas as pd
from epa_smoothing import epa_local_median, silverman_bandwidth

# Load your data
# df = pd.read_csv('your_data.csv')
# x = df['time'].values
# y = df['measurement'].values

# Simulated example
x = np.arange(0, 100)
y = np.cumsum(np.random.randn(100)) + np.sin(x * 0.1) * 5

# Automatic bandwidth
h = silverman_bandwidth(x)
print(f"Recommended bandwidth: {h:.2f}")

# Apply smoothing
y_smooth = epa_local_median(x, y, bandwidth=h)

# Results
residuals = y - y_smooth
print(f"Residual std: {np.std(residuals):.3f}")