|
| 1 | + |
| 2 | +import warnings |
| 3 | +from time import time |
| 4 | + |
| 5 | +import numpy as np |
| 6 | +from scipy.optimize import linprog |
| 7 | +from scipy.optimize import OptimizeWarning |
| 8 | +from scipy.linalg import LinAlgWarning |
| 9 | + |
| 10 | + |
| 11 | +def solve_lin_prog(X, y, fit_intercept=True, quantile=0.5, lasso_pen=1, |
| 12 | + sample_weights=None, |
| 13 | + lasso_weights=None, |
| 14 | + tol=None, |
| 15 | + solver='highs'): |
| 16 | + """ |
| 17 | + Solves the L1 penalized quantile regression problem using scipy's linprog solver. The code is adapted from https://github.com/benchopt/benchmark_quantile_regression and https://github.com/scikit-learn/scikit-learn/blob/0d064cfd4eda6dd4f7c8711a4870d2f02fda52fb/sklearn/linear_model/_quantile.py#L195-L209 |
| 18 | +
|
| 19 | + Parameters |
| 20 | + ---------- |
| 21 | + X: array-like, shape (n_samples, n_features) |
| 22 | + The training covariate data. |
| 23 | +
|
| 24 | + y: array-like, shape (n_samples, ) |
| 25 | + The training response data. |
| 26 | +
|
| 27 | + fit_intercept: bool |
| 28 | + Whether or not to fit an intercept. |
| 29 | +
|
| 30 | + quantile: float |
| 31 | + Which quantile. |
| 32 | +
|
| 33 | + lasso_pen: float |
| 34 | + The multiplicated penalty strength parameter. |
| 35 | +
|
| 36 | + sample_weights: None, array-like shape (n_features, ) |
| 37 | + Sample weights |
| 38 | +
|
| 39 | + lasso_weights: None, array-like shape (n_features, ) |
| 40 | + Feature weights for the L1 norm. |
| 41 | +
|
| 42 | + tol: None, float |
| 43 | + Tolerance for stopping criteria. |
| 44 | +
|
| 45 | + solver: str |
| 46 | + Which linprog solver to use, see scipy.optimize.linprog |
| 47 | +
|
| 48 | + Output |
| 49 | + ------ |
| 50 | + coef, intercept, opt_out |
| 51 | + """ |
| 52 | + start_time = time() |
| 53 | + |
| 54 | + A_eq, b_eq, c, n_params = \ |
| 55 | + get_lin_prog_data(X=X, y=y, |
| 56 | + fit_intercept=fit_intercept, |
| 57 | + quantile=quantile, |
| 58 | + lasso_pen=lasso_pen, |
| 59 | + sample_weights=sample_weights, |
| 60 | + lasso_weights=lasso_weights) |
| 61 | + |
| 62 | + if 'highs' in solver: |
| 63 | + options = {'primal_feasibility_tolerance': tol} |
| 64 | + else: |
| 65 | + options = {'tol': tol} |
| 66 | + |
| 67 | + warnings.filterwarnings('ignore', category=OptimizeWarning) |
| 68 | + warnings.filterwarnings('ignore', category=LinAlgWarning) |
| 69 | + |
| 70 | + result = linprog( |
| 71 | + c=c, |
| 72 | + A_eq=A_eq, |
| 73 | + b_eq=b_eq, |
| 74 | + method=solver, |
| 75 | + options=options |
| 76 | + ) |
| 77 | + |
| 78 | + coef, intercept = get_coef_inter(solution=result.x, |
| 79 | + n_params=n_params, |
| 80 | + fit_intercept=fit_intercept) |
| 81 | + |
| 82 | + opt_out = scipy_result_to_dict(result) |
| 83 | + opt_out['runtime'] = time() - start_time |
| 84 | + |
| 85 | + return coef, intercept, opt_out |
| 86 | + |
| 87 | + |
| 88 | +def get_coef_inter(solution, n_params, fit_intercept): |
| 89 | + # positive slack - negative slack |
| 90 | + # solution is an array with (params_pos, params_neg, u, v) |
| 91 | + params = solution[:n_params] - solution[n_params:2 * n_params] |
| 92 | + |
| 93 | + if fit_intercept: |
| 94 | + coef = params[1:] |
| 95 | + intercept = params[0] |
| 96 | + else: |
| 97 | + coef = params |
| 98 | + intercept = None |
| 99 | + |
| 100 | + return coef, intercept |
| 101 | + |
| 102 | + |
| 103 | +def scipy_result_to_dict(result): |
| 104 | + return {'opt_val': result.fun, |
| 105 | + 'success': result.success, |
| 106 | + 'status': result.status, |
| 107 | + 'nit': result.nit, |
| 108 | + 'message': result.message} |
| 109 | + |
| 110 | + |
| 111 | +def get_lin_prog_data(X, y, fit_intercept=True, quantile=0.5, lasso_pen=1, |
| 112 | + sample_weights=None, |
| 113 | + lasso_weights=None): |
| 114 | + """ |
| 115 | +
|
| 116 | + Output |
| 117 | + ------ |
| 118 | + A_eq, b_eq, c, n_params |
| 119 | + """ |
| 120 | + |
| 121 | + n_samples, n_features = X.shape |
| 122 | + |
| 123 | + # TODO: perhaps filter zero sample weights as in https://github.com/scikit-learn/scikit-learn/blob/0d064cfd4eda6dd4f7c8711a4870d2f02fda52fb/sklearn/linear_model/_quantile.py#L195-L209 |
| 124 | + |
| 125 | + # format sample weights vec |
| 126 | + if sample_weights is None: |
| 127 | + sample_weights = np.ones(n_samples) / n_samples |
| 128 | + else: |
| 129 | + sample_weights = np.array(sample_weights).copy() / n_samples |
| 130 | + |
| 131 | + # format the L1_vec |
| 132 | + if lasso_weights is None: |
| 133 | + L1_vec = np.ones(n_features) |
| 134 | + |
| 135 | + else: |
| 136 | + assert len(lasso_weights) == n_features |
| 137 | + L1_vec = np.array(lasso_weights) |
| 138 | + |
| 139 | + if fit_intercept: |
| 140 | + n_params = n_features + 1 |
| 141 | + L1_vec = np.concatenate([[0], L1_vec, # 0 = do not penalize intercept |
| 142 | + [0], L1_vec]) |
| 143 | + else: |
| 144 | + n_params = n_features |
| 145 | + L1_vec = np.concatenate([L1_vec, L1_vec]) |
| 146 | + |
| 147 | + # the linear programming formulation of quantile regression |
| 148 | + # follows https://stats.stackexchange.com/questions/384909/ |
| 149 | + |
| 150 | + c = np.concatenate([ |
| 151 | + L1_vec * lasso_pen, |
| 152 | + sample_weights * quantile, |
| 153 | + sample_weights * (1 - quantile), |
| 154 | + ]) |
| 155 | + |
| 156 | + if fit_intercept: |
| 157 | + |
| 158 | + A_eq = np.concatenate([ |
| 159 | + np.ones((n_samples, 1)), |
| 160 | + X, |
| 161 | + -np.ones((n_samples, 1)), |
| 162 | + -X, |
| 163 | + np.eye(n_samples), |
| 164 | + -np.eye(n_samples), |
| 165 | + ], axis=1) |
| 166 | + |
| 167 | + else: |
| 168 | + A_eq = np.concatenate([ |
| 169 | + X, |
| 170 | + -X, |
| 171 | + np.eye(n_samples), |
| 172 | + -np.eye(n_samples), |
| 173 | + ], axis=1) |
| 174 | + |
| 175 | + return A_eq, y, c, n_params |
0 commit comments