σμ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 2 · Gradient methods›Topic 2.2·~55 minIntermediate

Stochastic gradient descent and mini-batches

Skill positionModule 2 of 5 · Topic 2 of 3
2.2 Stochastic gradient descent and mini-batches
—Lab score— / 100Total XP—Streak—

Estimating the gradient from a few samples

Most fitting problems minimise an average over data, f(w)=1n∑i=1nℓi(w)f(\mathbf w)=\frac1n\sum_{i=1}^{n}\ell_i(\mathbf w)f(w)=n1​∑i=1n​ℓi​(w), for instance squared errors over nnn observations. The exact gradient needs all nnn terms at every step, which is too slow when nnn is millions of ticks. Stochastic gradient descent draws a random mini-batch BBB of bbb observations and uses their average gradient gBg_BgB​ instead. This estimate is unbiased, meaning that on average it equals the true gradient.

gB=1b∑i∈B∇ℓi(w),w←w−η gB,E[gB]=∇f,Var(gB)=σ2bg_B=\frac1b\sum_{i\in B}\nabla\ell_i(\mathbf w),\qquad \mathbf w\leftarrow\mathbf w-\eta\,g_B,\qquad \mathbb E[g_B]=\nabla f,\qquad \mathrm{Var}(g_B)=\frac{\sigma^{2}}{b}gB​=b1​i∈B∑​∇ℓi​(w),w←w−ηgB​,E[gB​]=∇f,Var(gB​)=bσ2​(1)

The price of the shortcut is noise. The variance of the batch gradient falls like 1/b1/b1/b, so halving its standard deviation needs a four times larger batch, a steeply diminishing return. One epoch is a full pass over the data and holds n/bn/bn/b updates. With a constant step the iterates never settle: they keep jittering in a small ball around the minimum, because even at the minimum the batch gradient is not zero. The fix is a step that shrinks. The classical Robbins-Monro conditions ask that ∑tηt=∞\sum_t\eta_t=\infty∑t​ηt​=∞, so the walk can travel as far as needed, and ∑tηt2<∞\sum_t\eta_t^{2}<\infty∑t​ηt2​<∞, so the noise dies out. The schedule ηt=η0/t\eta_t=\eta_0/tηt​=η0​/t satisfies both.

Four data pointsFit a level www to the data x=2,4,6,12x=2,4,6,12x=2,4,6,12 with the loss ℓi=12(w−xi)2\ell_i=\tfrac12(w-x_i)^2ℓi​=21​(w−xi​)2, whose gradient is w−xiw-x_iw−xi​. At w=0w=0w=0 the four single-sample gradients are −2,−4,−6,−12-2,-4,-6,-12−2,−4,−6,−12, and their average, the full gradient, is −6-6−6. The single-sample gradients have variance 16+4+0+364=14\frac{16+4+0+36}{4}=14416+4+0+36​=14 around that average. A batch of b=7b=7b=7 drawn with replacement therefore has gradient variance 14/7=214/7=214/7=2.
  • Unbiased: a random batch gradient equals the full gradient on average.
  • Noisy: variance σ2/b\sigma^2/bσ2/b, so four times the batch halves the standard deviation.
  • Shrinking steps: ∑ηt=∞\sum\eta_t=\infty∑ηt​=∞ and ∑ηt2<∞\sum\eta_t^2<\infty∑ηt2​<∞, e.g. ηt=η0/t\eta_t=\eta_0/tηt​=η0​/t, give convergence.
Time-ordered data is not a random sampleThe unbiasedness above relies on drawing the batch at random. A batch of consecutive days comes from one market regime, so its gradient is biased toward that regime, and training on the data in time order can drift with the regime. Shuffle the sample indices at the start of each epoch. When the order itself matters, as in a time-series model, keep the sequence inside each example and shuffle the examples.

Lab · what's graded

  • The full gradient15 pts
  • The noise of one sample15 pts
  • The noise of a batch15 pts
  • Updates per epoch15 pts
  • A step schedule that converges15 pts
  • The mini-batch loop10 pts
  • Why a constant step does not settle15 pts
0/ 7 answered
Tasks
1Numeric answer15 ptsNot answered

Data x=2,4,6,12x=2,4,6,12x=2,4,6,12, loss ℓi=12(w−xi)2\ell_i=\tfrac12(w-x_i)^2ℓi​=21​(w−xi​)2. What is the full gradient 14∑i(w−xi)\frac14\sum_i(w-x_i)41​∑i​(w−xi​) at w=0w=0w=0?

full=full =full=accepted: ± 0.01
2Numeric answer15 ptsNot answered

The single-sample gradients at w=0w=0w=0 are −2,−4,−6,−12-2,-4,-6,-12−2,−4,−6,−12 with mean −6-6−6. What is their variance (average squared deviation from the mean)?

var1=var1 =var1=accepted: ± 0.1
3Numeric answer15 ptsNot answered

A mini-batch of b=7b=7b=7 samples drawn with replacement averages those gradients. What is the variance of the batch gradient?

var7=var7 =var7=accepted: ± 0.02
4Numeric answer15 ptsNot answered

A data set has n=10,000n=10{,}000n=10,000 observations and the mini-batch size is b=250b=250b=250. How many updates make up one epoch?

updates=updates =updates=accepted: ± 0.5
5Choice15 ptsNot answered

Which step schedule satisfies both Robbins-Monro conditions, ∑ηt=∞\sum\eta_t=\infty∑ηt​=∞ and ∑ηt2<∞\sum\eta_t^2<\infty∑ηt2​<∞?

6Order the steps10 ptsNot answered

Put one epoch of mini-batch SGD in order.

Drag to reorder, or focus an item, press Space to lift it, the arrow keys to move it and Space to drop it.

  1. 1Update the weights with w←w−ηgB\mathbf w\leftarrow\mathbf w-\eta g_Bw←w−ηgB​
  2. 2Repeat for the next batch until all batches are used
  3. 3Shuffle the data and split it into batches
  4. 4Compute the average gradient gBg_BgB​ on one batch
7Explain in words15 ptsNot answered

In one or two sentences, explain why SGD with a constant step size keeps jittering around the minimum and how to reduce this.

Math modeKeyboard or LaTeX inputTask 1 of 7 · 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