Skip to content

Commit 1b40715

Browse files
committed
example script
1 parent d1123e6 commit 1b40715

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

alpha_search.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import numpy as np
2+
from numpy.linalg import norm
3+
from skglm import Lasso
4+
from skglm.experimental import SqrtLasso
5+
6+
np.random.seed(0)
7+
X = np.random.randn(10, 20)
8+
y = np.random.randn(10)
9+
y += 1
10+
11+
n = len(y)
12+
13+
alpha_max = norm(X.T @ y, ord=np.inf) / n
14+
15+
alpha = alpha_max / 10
16+
17+
lass = Lasso(alpha=alpha, fit_intercept=True, tol=1e-8).fit(X, y)
18+
w_lass = lass.coef_
19+
assert norm(w_lass) > 0
20+
21+
scal = n / norm(y - lass.predict(X))
22+
23+
sqrt = SqrtLasso(alpha=alpha * scal, fit_intercept=True, tol=1e-8).fit(X, y)
24+
25+
print(norm(w_lass - sqrt.coef_))
26+
27+
28+
# diffs = []
29+
# alphas = np.linspace(16.07, 16.08, num=50)
30+
# for scal in alphas:
31+
# sqrt = SqrtLasso(alpha=alpha * scal).fit(X, y)
32+
# diffs.append(norm(w_lass - sqrt.coef_))
33+
34+
# best = np.argmin(diffs)
35+
# print(alphas[best])
36+
# print(diffs[best])

example.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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

Comments
 (0)