σμimplied vol · KKmax(S−K,0)udβρᵢⱼfat tailsPnL · drawdownyield · TdSₜ = μSₜ dt + σSₜ dWₜC = S·N(d₁) − K·e⁻ʳᵀ·N(d₂)σₚ² = wᵀΣwd₁ = [ln(S/K) + (r + σ²⁄2)T] ⁄ σ√T∂V/∂t + ½σ²S²·∂²V/∂S² + rS·∂V/∂S − rV = 0Sharpe = (Rₚ − R_f) ⁄ σₚVaR₉₉ = μ − 2.33σρ = Cov(X,Y) ⁄ σₓσᵧE[Rₚ] = Σ wᵢ·E[Rᵢ]φ(x) = e^(−x²/2) ⁄ √2πE[Rᵢ] = R_f + βᵢ(E[Rₘ] − R_f)dXₜ = θ(μ − Xₜ)dt + σ dWₜσ²ₜ = ω + α·ε²ₜ₋₁ + β·σ²ₜ₋₁C − P = S − K·e⁻ʳᵀΔ = ∂V/∂S Γ = ∂²V/∂S²p = (e^(rΔt) − d) ⁄ (u − d)rₜ = ln(Pₜ ⁄ Pₜ₋₁)E[Xₜ₊₁ | ℱₜ] = Xₜf* = (bp − q) ⁄ bz = (x − μ) ⁄ σP(N=k) = λᵏe^(−λ) ⁄ k!Θ = −∂V/∂Tν = S·φ(d₁)·√Tmin wᵀΣw s.t. wᵀμ = μ*∫Σ∂∞λ
uantCraft AcademyRoadmapsSkills
Ctrl K
AO
+1.2k Learners
LoginJoin ↗
RoadmapsSkills
AO
+1.2k Learners
Login
  • English
  • Français
  • Español
  • العربية
  • Português
  • Deutsch
  • 日本語
  • 中文
σμimplied vol · KKmax(S−K,0)udβρᵢⱼfat tailsPnL · drawdownyield · TdSₜ = μSₜ dt + σSₜ dWₜC = S·N(d₁) − K·e⁻ʳᵀ·N(d₂)σₚ² = wᵀΣwd₁ = [ln(S/K) + (r + σ²⁄2)T] ⁄ σ√T∂V/∂t + ½σ²S²·∂²V/∂S² + rS·∂V/∂S − rV = 0Sharpe = (Rₚ − R_f) ⁄ σₚVaR₉₉ = μ − 2.33σρ = Cov(X,Y) ⁄ σₓσᵧE[Rₚ] = Σ wᵢ·E[Rᵢ]φ(x) = e^(−x²/2) ⁄ √2πE[Rᵢ] = R_f + βᵢ(E[Rₘ] − R_f)dXₜ = θ(μ − Xₜ)dt + σ dWₜσ²ₜ = ω + α·ε²ₜ₋₁ + β·σ²ₜ₋₁C − P = S − K·e⁻ʳᵀΔ = ∂V/∂S Γ = ∂²V/∂S²p = (e^(rΔt) − d) ⁄ (u − d)rₜ = ln(Pₜ ⁄ Pₜ₋₁)E[Xₜ₊₁ | ℱₜ] = Xₜf* = (bp − q) ⁄ bz = (x − μ) ⁄ σP(N=k) = λᵏe^(−λ) ⁄ k!Θ = −∂V/∂Tν = S·φ(d₁)·√Tmin wᵀΣw s.t. wᵀμ = μ*∫Σ∂∞λ
uantCraft AcademyRoadmapsSkills
Ctrl K
AO
+1.2k Learners
LoginJoin ↗
RoadmapsSkills
AO
+1.2k Learners
Login
  • English
  • Français
  • Español
  • العربية
  • Português
  • Deutsch
  • 日本語
  • 中文
Optimization skill›Module 4 · Constraints and regularisation›Topic 4.3·~50 minIntermediate

L1 and L2 regularisation as optimization

Skill positionModule 4 of 5 · Topic 3 of 4
4.3 L1 and L2 regularisation as optimization
—Lab score— / 100Total XP—Streak—

Penalties that shrink

Regularisation adds a penalty to a fitting loss so that large coefficients cost something: min⁡w ∥y−Xw∥2+λ Ω(w)\min_{\mathbf w}\ \lVert\mathbf y-X\mathbf w\rVert^2+\lambda\,\Omega(\mathbf w)minw​ ∥y−Xw∥2+λΩ(w). Ridge uses Ω=∥w∥22\Omega=\lVert\mathbf w\rVert_2^2Ω=∥w∥22​ and the lasso uses Ω=∥w∥1\Omega=\lVert\mathbf w\rVert_1Ω=∥w∥1​. This is an optimization problem like any other, and it has an equivalent constrained form, min⁡\minmin loss subject to Ω(w)≤t\Omega(\mathbf w)\le tΩ(w)≤t, in which λ\lambdaλ is exactly the KKT multiplier of the budget ttt. A larger λ\lambdaλ means a smaller budget.

min⁡w 12(w−z)2+λ∣w∣ ⇒ w=sign(z)max⁡(∣z∣−λ, 0),min⁡w 12(w−z)2+λ2w2 ⇒ w=z1+λ\min_{w}\ \tfrac12(w-z)^{2}+\lambda\lvert w\rvert\ \Rightarrow\ w=\mathrm{sign}(z)\max(\lvert z\rvert-\lambda,\,0),\qquad \min_{w}\ \tfrac12(w-z)^{2}+\tfrac{\lambda}{2}w^{2}\ \Rightarrow\ w=\frac{z}{1+\lambda}wmin​ 21​(w−z)2+λ∣w∣ ⇒ w=sign(z)max(∣z∣−λ,0),wmin​ 21​(w−z)2+2λ​w2 ⇒ w=1+λz​(1)

The one-variable problems show the difference. Ridge divides the unpenalised value zzz by 1+λ1+\lambda1+λ: every coefficient shrinks by the same proportion but none reaches zero. The lasso subtracts λ\lambdaλ from ∣z∣|z|∣z∣ and clips at zero, so every coefficient with ∣z∣≤λ|z|\le\lambda∣z∣≤λ becomes exactly zero. This is why the lasso selects variables. Geometrically, the ℓ1\ell_1ℓ1​ ball has corners on the axes and the loss contours tend to touch it at a corner, while the round ℓ2\ell_2ℓ2​ ball has no corners. Analytically, ∣w∣|w|∣w∣ has a kink at zero where the slope jumps from −λ-\lambda−λ to +λ+\lambda+λ, and a minimum can sit on that kink.

Numbers for both penaltiesTake λ=1\lambda=1λ=1. For z=3z=3z=3 the lasso gives max⁡(3−1,0)=2\max(3-1,0)=2max(3−1,0)=2 and ridge gives 3/2=1.53/2=1.53/2=1.5. For z=0.8z=0.8z=0.8 the lasso gives 000, because 0.8≤10.8\le10.8≤1, while ridge still gives 0.40.40.4. A full regression: for x=(1,2)x=(1,2)x=(1,2) and y=(1,3)y=(1,3)y=(1,3) we have xTx=5x^{T}x=5xTx=5 and xTy=7x^{T}y=7xTy=7, and ridge gives w=7/(5+λ)w=7/(5+\lambda)w=7/(5+λ): 1.41.41.4 at λ=0\lambda=0λ=0 and exactly 111 at λ=2\lambda=2λ=2.

In finance the ridge penalty stabilises regressions on correlated predictors, a cousin of covariance shrinkage, and the lasso builds sparse index-tracking portfolios. One trap: in a long-only, fully invested portfolio ∑i∣wi∣=∑iwi=1\sum_i|w_i|=\sum_i w_i=1∑i​∣wi​∣=∑i​wi​=1, so the penalty λ∥w∥1\lambda\lVert\mathbf w\rVert_1λ∥w∥1​ is the constant λ\lambdaλ and changes nothing. To get few non-zero positions or few trades, penalise the trades ∥w−w′∥1\lVert\mathbf w-\mathbf w'\rVert_1∥w−w′∥1​, which is exactly what turnover and cost limits do in the capstone. In Bayesian terms ridge corresponds to a Gaussian prior on the coefficients and the lasso to a Laplace prior.

  • Ridge: w=z/(1+λ)w=z/(1+\lambda)w=z/(1+λ) in one variable; shrinks smoothly, never exactly to zero.
  • Lasso: soft threshold sign(z)max⁡(∣z∣−λ,0)\mathrm{sign}(z)\max(|z|-\lambda,0)sign(z)max(∣z∣−λ,0); sets small coefficients exactly to zero.
  • Constrained form: penalty weight λ\lambdaλ is the KKT multiplier of the budget Ω(w)≤t\Omega(\mathbf w)\le tΩ(w)≤t.
Choose λ out of sampleA bigger λ\lambdaλ trades variance for bias: coefficients get smaller and more stable but systematically pulled toward zero. Pick λ\lambdaλ by cross-validation or a walk-forward test, never by the in-sample fit, which always prefers λ=0\lambda=0λ=0. With time series use blocks that respect time order.

Lab · what's graded

  • A lasso coefficient10 pts
  • A ridge coefficient10 pts
  • A coefficient set to zero15 pts
  • A one-variable ridge regression15 pts
  • Why the lasso makes exact zeros15 pts
  • A useless penalty15 pts
  • Priors and effects10 pts
  • What a larger lambda does10 pts
0/ 8 answered
Tasks
1Numeric answer10 ptsNot answered

The unpenalised value is z=3z=3z=3 and λ=1\lambda=1λ=1. What does the lasso (soft threshold) give?

lasso=lasso =lasso=accepted: ± 0.01
2Numeric answer10 ptsNot answered

For the same z=3z=3z=3 and λ=1\lambda=1λ=1, what does ridge give, z/(1+λ)z/(1+\lambda)z/(1+λ)?

ridge=ridge =ridge=accepted: ± 0.01
3Numeric answer15 ptsNot answered

Now the unpenalised value is z=0.8z=0.8z=0.8 with λ=1\lambda=1λ=1. What does the lasso give?

zero=zero =zero=accepted: ± 0.001
4Numeric answer15 ptsNot answered

For x=(1,2)x=(1,2)x=(1,2) and y=(1,3)y=(1,3)y=(1,3), ridge gives w=xTy/(xTx+λ)w=x^{T}y/(x^{T}x+\lambda)w=xTy/(xTx+λ). What is www at λ=2\lambda=2λ=2?

w=w =w=accepted: ± 0.01
5Choice15 ptsNot answered

Which statement explains why the lasso sets some coefficients exactly to zero while ridge does not?

6Choice15 ptsNot answered

A portfolio is long-only (wi≥0w_i\ge0wi​≥0) and fully invested (∑iwi=1\sum_i w_i=1∑i​wi​=1). Why does adding λ∥w∥1\lambda\lVert\mathbf w\rVert_1λ∥w∥1​ change nothing?

7Match10 ptsNot answered

Match each item with its description.

Pick the partner of each item on the left.

Ridge
Lasso
Large λ\lambdaλ
Small λ\lambdaλ
8Explain in words10 ptsNot answered

In one or two sentences, say what happens to the coefficients, and to the bias and variance of the fit, when the penalty weight lambda grows.

Math modeKeyboard or LaTeX inputTask 1 of 8 · Ctrl ⏎ to check

Log in to run your code.

Every lecture is free to read. Create an account or log in to run code, run tests and submit this lab.

  • In-browser IDE with a bash terminal — nothing to install
  • Instant grading, XP and your place on the skill
Create an accountLog in
OR
Continue with GitHub