Faster Lasso regression
Fit a Lasso regression on a 1990 × 3980 random design faster than scikit-learn, reaching the same objective value to within 1e-5.
| 1 | """What the Lasso returns: a solution that is mostly exact zeros. |
| 2 | |
| 3 | No data file: the instance is the task's own generator (README, "Task"), at a size that fits on a card. With n = 12 |
| 4 | it draws X (m = 5n by d = 10n, standard normal), beta with n standard normal non-zeros at random positions and |
| 5 | y = X beta + 0.01 eps, then minimises f(w) = 1/(2m)||y - Xw||^2 + 0.1||w||_1 by cyclic coordinate descent, the |
| 6 | reference solver's method. The figure checks the solution with the optimality conditions of that objective, so the |
| 7 | zeros drawn are exact zeros, not small numbers. |
| 8 | """ |
| 9 | import random |
| 10 | from pathlib import Path |
| 11 | |
| 12 | import kit |
| 13 | |
| 14 | ALPHA = 0.1 |
| 15 | n = 12 |
| 16 | m, d = 5 * n, 10 * n |
| 17 | rnd = random.Random(0) |
| 18 | X = [[rnd.gauss(0, 1) for _ in range(d)] for _ in range(m)] |
| 19 | beta = [0.0] * d |
| 20 | for j in rnd.sample(range(d), n): |
| 21 | beta[j] = rnd.gauss(0, 1) |
| 22 | y = [sum(X[i][j] * beta[j] for j in range(d)) + 0.01 * rnd.gauss(0, 1) for i in range(m)] |
| 23 | |
| 24 | col = [[X[i][j] for i in range(m)] for j in range(d)] |
| 25 | nrm = [sum(v * v for v in c) / m for c in col] |
| 26 | w = [0.0] * d |
| 27 | r = list(y) # r = y - Xw |
| 28 | for _ in range(2000): |
| 29 | shift = 0.0 |
| 30 | for j in range(d): |
| 31 | rho = sum(col[j][i] * r[i] for i in range(m)) / m + nrm[j] * w[j] |
| 32 | new = (abs(rho) - ALPHA) / nrm[j] * (1 if rho > 0 else -1) if abs(rho) > ALPHA else 0.0 |
| 33 | if new != w[j]: |
| 34 | shift = max(shift, abs(new - w[j])) |
| 35 | for i in range(m): |
| 36 | r[i] -= col[j][i] * (new - w[j]) |
| 37 | w[j] = new |
| 38 | if shift < 1e-12: |
| 39 | break |
| 40 | else: |
| 41 | raise SystemExit('coordinate descent did not converge') |
| 42 | |
| 43 | # optimality of the Lasso objective: |grad| <= alpha where w_j = 0, = alpha sign(w_j) where it is not |
| 44 | for j in range(d): |
| 45 | g = sum(col[j][i] * r[i] for i in range(m)) / m |
| 46 | assert abs(g) <= ALPHA + 1e-9 if w[j] == 0 else abs(g - ALPHA * (1 if w[j] > 0 else -1)) < 1e-9 |
| 47 | nz = [j for j in range(d) if w[j] != 0] |
| 48 | assert 0 < len(nz) < d |
| 49 | |
| 50 | base, top = 214.0, 30.0 |
| 51 | X_ = lambda j: 26 + j * (588 / (d - 1)) |
| 52 | hi = max(abs(v) for v in w) |
| 53 | body = kit.line(20, base, 620, base, 'faint-s', 2) |
| 54 | body += ''.join(kit.circle(X_(j), base, 3, 'faint') for j in range(d) if w[j] == 0) |
| 55 | for j in nz: |
| 56 | yv = base - abs(w[j]) / hi * (base - top) |
| 57 | body += kit.line(X_(j), base, X_(j), yv, 'f-s', 4) |
| 58 | body += kit.dot(X_(j), yv, 6, 'f') |
| 59 | label = f'{d} coefficients, {d - len(nz)} of them exactly zero' |
| 60 | kit.write(__file__, 'computer-science', body, label, |
| 61 | f'The Lasso solution for an instance of the task at n = {n}: {len(nz)} non-zero coefficients ' |
| 62 | f'of {d}, the other {d - len(nz)} exactly zero') |
| 63 | print(label, 'nonzero at', nz) |