Skip to content

Commit 4375b4c

Browse files
committed
script debug quantile
1 parent 39ced0d commit 4375b4c

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

debug_quantile.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
"""
2+
QuantileHuber vs Sklearn
3+
"""
4+
import numpy as np
5+
import time
6+
from sklearn.linear_model import QuantileRegressor
7+
from skglm.experimental.quantile_huber import QuantileHuber, SmoothQuantileRegressor
8+
import matplotlib.pyplot as plt
9+
from sklearn.datasets import make_regression
10+
11+
12+
def pinball_loss(residuals, quantile):
13+
"""True pinball loss."""
14+
return np.mean(residuals * (quantile - (residuals < 0)))
15+
16+
17+
X, y = make_regression(n_samples=1000, n_features=10, noise=0.1, random_state=0)
18+
tau = 0.8
19+
20+
start = time.time()
21+
sk = QuantileRegressor(quantile=tau, alpha=0.1, fit_intercept=True)
22+
sk.fit(X, y)
23+
sk_pred = sk.predict(X)
24+
sk_time = time.time() - start
25+
sk_cov = np.mean(y <= sk_pred)
26+
sk_pinball = pinball_loss(y - sk_pred, tau)
27+
28+
start = time.time()
29+
qh = SmoothQuantileRegressor(
30+
quantile=tau,
31+
alpha=0.1,
32+
delta_init=0.5,
33+
delta_final=0.01,
34+
n_deltas=5,
35+
solver="AndersonCD",
36+
verbose=True,
37+
fit_intercept=True
38+
)
39+
qh.fit(X, y)
40+
qh_time = time.time() - start
41+
qh_pred = qh.predict(X)
42+
qh_cov = np.mean(y <= qh_pred)
43+
qh_pinball = pinball_loss(y - qh_pred, tau)
44+
45+
46+
print(sk.coef_)
47+
print(qh.est.coef_)
48+
49+
# print(f"{'Method':<12} {'Q':<4} {'Coverage':<8} {'Time':<6} "
50+
# f"{'Pinball':<8}")
51+
# print("-" * 55)
52+
# print(f"{'Sklearn':<12} {tau:<4} {sk_cov:<8.3f} {sk_time:<6.3f} "
53+
# f"{sk_pinball:<8.4f}")
54+
# print(f"{'QuantileHuber':<12} {tau:<4} {qh_cov:<8.3f} {qh_time:<6.3f} "
55+
# f"{qh_pinball:<8.4f}")
56+
57+
58+
# quantiles = [0.1, 0.5, 0.9]
59+
# delta = 0.5
60+
# residuals = np.linspace(-3, 3, 500)
61+
# _, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 4))
62+
# for tau in quantiles:
63+
# qh = QuantileHuber(quantile=tau, delta=delta)
64+
# loss = [qh._loss_sample(r) for r in residuals]
65+
# grad = [qh._grad_per_sample(r) for r in residuals]
66+
# ax1.plot(residuals, loss, label=f"τ={tau}")
67+
# ax2.plot(residuals, grad, label=f"τ={tau}")
68+
# ax1.set_title("QuantileHuber Loss")
69+
# ax1.set_xlabel("Residual")
70+
# ax1.set_ylabel("Loss")
71+
# ax1.legend()
72+
# ax2.set_title("QuantileHuber Gradient")
73+
# ax2.set_xlabel("Residual")
74+
# ax2.set_ylabel("Gradient")
75+
# ax2.legend()
76+
# plt.tight_layout()
77+
# plt.show()

0 commit comments

Comments
 (0)