Quantile Regression with Pinball Loss

medium
Checkpoint challenge
Solve this to show you know the material and jump straight to Unit 12. Everything before it counts as cleared.

Ordinary regression predicts the mean. To forecast "demand will be at most X with 90% probability", predict a quantile instead by minimising the pinball loss:

Lq(y,y^)=1n∑max⁡(q (y−y^),  (q−1)(y−y^))L_q(y, \hat y) = \frac{1}{n}\sum \max\big(q\,(y - \hat y),\; (q - 1)(y - \hat y)\big)

Implement:

  • pinball_loss(y, y_hat, q)
  • fit_quantile(X, y, q, lr=0.05, epochs=3000): a linear model X @ w + b, starting from zeros, trained by full-batch subgradient descent. The gradient of the loss with respect to each prediction is ((y_hat >= y) - q) / n. Return (w, b).
  • coverage(y, y_hat): the fraction of targets at or below the prediction
#regression#uncertainty#loss-functions2 visible + 1 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Quantile Regression with Pinball Loss · TechGradify