|
| 1 | +import time |
| 2 | +import numpy as np |
| 3 | +from sklearn.datasets import make_regression |
| 4 | +from sklearn.preprocessing import StandardScaler |
| 5 | +from sklearn.linear_model import QuantileRegressor |
| 6 | +from skglm.experimental.smooth_quantile_regressor import SmoothQuantileRegressor |
| 7 | +from sklearn.model_selection import train_test_split |
| 8 | + |
| 9 | +from numpy.linalg import norm |
| 10 | + |
| 11 | + |
| 12 | +def pinball_loss(y_true, y_pred, tau=0.5): |
| 13 | + """Compute Pinball (quantile) loss.""" |
| 14 | + residuals = y_true - y_pred |
| 15 | + return np.mean(np.where(residuals >= 0, |
| 16 | + tau * residuals, |
| 17 | + (1 - tau) * -residuals)) |
| 18 | + |
| 19 | + |
| 20 | +# Test different problem sizes |
| 21 | +n_samples, n_features = 100, 100 |
| 22 | +X, y = make_regression(n_samples=n_samples, n_features=n_features, |
| 23 | + noise=0.1, random_state=0) |
| 24 | +alpha = 0.01 |
| 25 | + |
| 26 | +# Test different noise distributions |
| 27 | + |
| 28 | +# Quantiles to test |
| 29 | +tau = 0.3 |
| 30 | + |
| 31 | +# Store results |
| 32 | +results = [] |
| 33 | + |
| 34 | +X_train, X_test, y_train, y_test = train_test_split( |
| 35 | + X, y, test_size=0.2, random_state=42 |
| 36 | +) |
| 37 | + |
| 38 | +# scikit-learn QuantileRegressor |
| 39 | +qr = QuantileRegressor(quantile=tau, alpha=alpha, solver="highs") |
| 40 | +t0 = time.time() |
| 41 | +qr.fit(X_train, y_train) |
| 42 | +qr_time = time.time() - t0 |
| 43 | + |
| 44 | + |
| 45 | +ours = SmoothQuantileRegressor(quantile=tau, alpha=alpha) |
| 46 | +t0 = time.time() |
| 47 | +ours.fit(X_train, y_train) |
| 48 | +ours_time = time.time() - t0 |
| 49 | + |
| 50 | + |
| 51 | +print(ours.coef_ - qr.coef_) |
| 52 | +print(norm(ours.coef_ - qr.coef_) / norm(qr.coef_)) |
0 commit comments