|
| 1 | +import numpy as np |
| 2 | +from numba import float64 |
| 3 | +from skglm.datafits.single_task import Huber |
| 4 | +from skglm.utils.sparse_ops import spectral_norm |
| 5 | + |
| 6 | + |
| 7 | +class QuantileHuber(Huber): |
| 8 | + r"""Quantile Huber loss for quantile regression. |
| 9 | +
|
| 10 | + Implements the smoothed pinball loss with quadratic region: |
| 11 | +
|
| 12 | + .. math:: |
| 13 | +
|
| 14 | + \rho_\tau^\delta(r) = |
| 15 | + \begin{cases} |
| 16 | + \tau\, r - \dfrac{\delta}{2}, & \text{if } r \ge \delta,\\ |
| 17 | + \dfrac{\tau r^{2}}{2\delta}, & \text{if } 0 \le r < \delta,\\ |
| 18 | + \dfrac{(1-\tau) r^{2}}{2\delta}, & \text{if } -\delta < r < 0,\\ |
| 19 | + (\tau - 1)\, r - \dfrac{\delta}{2}, & \text{if } r \le -\delta. |
| 20 | + \end{cases} |
| 21 | +
|
| 22 | + Parameters |
| 23 | + ---------- |
| 24 | + quantile : float, default=0.5 |
| 25 | + Desired quantile level between 0 and 1. |
| 26 | + delta : float, default=1.0 |
| 27 | + Width of quadratic region. |
| 28 | +
|
| 29 | + References |
| 30 | + ---------- |
| 31 | + Chen, C. (2007). A Finite Smoothing Algorithm for Quantile Regression. |
| 32 | + Journal of Computational and Graphical Statistics, 16(1), 136–164. |
| 33 | + http://www.jstor.org/stable/27594233 |
| 34 | + """ |
| 35 | + |
| 36 | + def __init__(self, quantile=0.5, delta=1.0): |
| 37 | + if not 0 < quantile < 1: |
| 38 | + raise ValueError("quantile must be between 0 and 1") |
| 39 | + self.delta = float(delta) |
| 40 | + self.quantile = float(quantile) |
| 41 | + |
| 42 | + def get_spec(self): |
| 43 | + return (('delta', float64), ('quantile', float64)) |
| 44 | + |
| 45 | + def params_to_dict(self): |
| 46 | + return dict(delta=self.delta, quantile=self.quantile) |
| 47 | + |
| 48 | + def _loss_and_grad_scalar(self, residual): |
| 49 | + """Calculate loss and gradient for a single residual.""" |
| 50 | + tau = self.quantile |
| 51 | + delta = self.delta |
| 52 | + abs_r = abs(residual) |
| 53 | + |
| 54 | + # Quadratic core: |r| ≤ delta |
| 55 | + if abs_r <= delta: |
| 56 | + if residual >= 0: |
| 57 | + # 0 ≤ r ≤ delta |
| 58 | + loss = tau * residual**2 / (2 * delta) |
| 59 | + grad = tau * residual / delta |
| 60 | + else: |
| 61 | + # -delta ≤ r < 0 |
| 62 | + loss = (1 - tau) * residual**2 / (2 * delta) |
| 63 | + grad = (1 - tau) * residual / delta |
| 64 | + return loss, grad |
| 65 | + |
| 66 | + # Linear tails: |r| > delta |
| 67 | + if residual > delta: |
| 68 | + loss = tau * (residual - delta / 2) |
| 69 | + grad = tau |
| 70 | + return loss, grad |
| 71 | + else: |
| 72 | + loss = (1 - tau) * (-residual - delta / 2) |
| 73 | + grad = tau - 1 |
| 74 | + return loss, grad |
| 75 | + |
| 76 | + def value(self, y, w, Xw): |
| 77 | + """Compute the quantile Huber loss value.""" |
| 78 | + residuals = y - Xw |
| 79 | + loss = np.zeros_like(residuals) |
| 80 | + for i, r in enumerate(residuals): |
| 81 | + loss[i], _ = self._loss_and_grad_scalar(r) |
| 82 | + return np.mean(loss) |
| 83 | + |
| 84 | + def raw_grad(self, y, Xw): |
| 85 | + """Compute gradient of datafit w.r.t Xw.""" |
| 86 | + residuals = y - Xw |
| 87 | + grad = np.zeros_like(residuals) |
| 88 | + for i, r in enumerate(residuals): |
| 89 | + _, grad[i] = self._loss_and_grad_scalar(r) |
| 90 | + return -grad |
| 91 | + |
| 92 | + def get_lipschitz(self, X, y): |
| 93 | + """Compute coordinate-wise Lipschitz constants.""" |
| 94 | + weight = max(self.quantile, 1 - self.quantile) |
| 95 | + return weight * (X ** 2).sum(axis=0) / (len(y) * self.delta) |
| 96 | + |
| 97 | + def get_global_lipschitz(self, X, y): |
| 98 | + """Compute global Lipschitz constant.""" |
| 99 | + weight = max(self.quantile, 1 - self.quantile) |
| 100 | + return weight * np.linalg.norm(X, 2) ** 2 / (len(y) * self.delta) |
| 101 | + |
| 102 | + def get_lipschitz_sparse(self, X_data, X_indptr, X_indices, y): |
| 103 | + """Compute coordinate-wise Lipschitz constants for sparse X.""" |
| 104 | + n_samples = len(y) |
| 105 | + weight = max(self.quantile, 1 - self.quantile) |
| 106 | + n_features = len(X_indptr) - 1 |
| 107 | + lipschitz = np.zeros(n_features, dtype=X_data.dtype) |
| 108 | + for j in range(n_features): |
| 109 | + nrm2 = 0.0 |
| 110 | + for idx in range(X_indptr[j], X_indptr[j + 1]): |
| 111 | + nrm2 += X_data[idx] ** 2 |
| 112 | + lipschitz[j] = weight * nrm2 / (n_samples * self.delta) |
| 113 | + return lipschitz |
| 114 | + |
| 115 | + def get_global_lipschitz_sparse(self, X_data, X_indptr, X_indices, y): |
| 116 | + """Compute global Lipschitz constant for sparse X.""" |
| 117 | + n_samples = len(y) |
| 118 | + weight = max(self.quantile, 1 - self.quantile) |
| 119 | + return weight * spectral_norm( |
| 120 | + X_data, X_indptr, X_indices, n_samples |
| 121 | + ) ** 2 / (n_samples * self.delta) |
| 122 | + |
| 123 | + def intercept_update_step(self, y, Xw): |
| 124 | + return -np.mean(self.raw_grad(y, Xw)) |
0 commit comments