Search

Search papers and lessons

Stochastic Approximation

Level 3 · 12 min read

Stochastic approximation finds the input where a function is zero when you can only see that function through noisy measurements, by taking small corrective steps that shrink over time.

On this page

  • Colour = topic mix
  • Discrete math
  • Calculus
  • Probability
  • Optimizers
  • ReadReady to readNot yet

TL;DR

Many problems ask for the input θ⋆\theta^\star at which some function gg equals zero, but the only way to learn about gg is through noisy measurements of it. Stochastic approximation solves this by starting from a guess and repeatedly stepping in the direction the noisy measurement suggests, with steps that shrink over time. Under the right step sizes the guesses settle on the true zero, even though no single measurement is accurate.

Builds on: Random Variable, Expected Value, Limit of a Sequence · Leads to: Simultaneous Perturbation Stochastic Approximation, Stochastic Gradient Descent

Where it's used

  • Training large models. Stochastic Gradient Descent is a stochastic approximation method. Its gradient is computed from a random mini-batch, so each step is a noisy measurement of the true gradient. The Learning Rate schedule plays the role of the step sizes ana_n in the theorem below.
  • Tuning a physical process. A heater or a chemical reactor has a setting that should produce a target temperature or yield, but each reading from the sensor carries noise. An operator who adjusts the setting by small amounts, guided by the noisy readings, is doing stochastic approximation, whether or not they call it that.
  • Adaptive filters and control. Signal processing systems adjust filter weights from noisy error signals on every sample. The least-mean-squares filter is a stochastic gradient update of this kind. Its step size must stay small enough for stability, which is a different requirement from the step-size conditions discussed below.

How it works

Take one tiny instance. We want the zero of g(θ)=θ−2g(\theta) = \theta - 2, which is θ⋆=2\theta^\star = 2. Suppose we could measure gg exactly, but we use noise-free measurements to keep the arithmetic clean. Start at θ1=0\theta_1 = 0 and use step sizes an=1/na_n = 1/n.

  1. Measure at the current guess. At θ1=0\theta_1 = 0 the measurement is Y1=g(0)=−2Y_1 = g(0) = -2. Check: the guess is left of the zero at 2, and the measurement is negative, which says "move right".
  2. Step against the measurement. Update θ2=θ1−a1Y1=0−1⋅(−2)=2\theta_2 = \theta_1 - a_1 Y_1 = 0 - 1 \cdot (-2) = 2. Check: the step is 2 to the right, and the guess lands exactly on the zero.
  3. Measure again. At θ2=2\theta_2 = 2 the measurement is Y2=g(2)=0Y_2 = g(2) = 0, so the step is zero and the guess stays put. Check: θ2\theta_2 is the zero, so no further move is needed.

That tiny example finishes at once, so we add noise to see the real behaviour. Keep the same gg and θ1=0\theta_1 = 0, but let each measurement be Yn=g(θn)+εnY_n = g(\theta_n) + \varepsilon_n, with noise values ε1=+1\varepsilon_1 = +1, ε2=−1\varepsilon_2 = -1, ε3=+1\varepsilon_3 = +1, ε4=−1\varepsilon_4 = -1.

  1. Step 1. Y1=−2+1=−1Y_1 = -2 + 1 = -1, so θ2=0−1⋅(−1)=1\theta_2 = 0 - 1\cdot(-1) = 1. Check: the noise pushed the measurement toward zero, so the step is smaller than the noise-free step of 2.
  2. Step 2. Y2=(1−2)−1=−2Y_2 = (1 - 2) - 1 = -2, so θ3=1−12(−2)=2\theta_3 = 1 - \tfrac12(-2) = 2. Check: the guess reaches 2 again.
  3. Step 3. Y3=(2−2)+1=1Y_3 = (2 - 2) + 1 = 1, so θ4=2−13⋅1=53\theta_4 = 2 - \tfrac13 \cdot 1 = \tfrac53. Check: the noise made the measurement say "move left", which is wrong, so the guess leaves the zero and sits left of it at 53\tfrac53.
  4. Step 4. Y4=(53−2)−1=−43Y_4 = (\tfrac53 - 2) - 1 = -\tfrac43, so θ5=53−14(−43)=2\theta_5 = \tfrac53 - \tfrac14\left(-\tfrac43\right) = 2. Check: the guess is exactly on the zero again.

The noise made the path wander, but the shrinking step sizes pulled the guesses back. Each step is smaller than the last, so later noise cannot push the guess far.

−0.500.511.522.53−2−1012thetag(theta)
  • g(theta) = theta - 2
The line is the function whose zero we want. The dots mark the guesses from the noisy run (0, 1, 5/3 and 2) at their true values of g, which walk in toward the zero at 2.

The math

Definition: Stochastic approximation (Robbins-Monro iteration)

Let g(θ)=E[Y∣θ]g(\theta) = \mathbb{E}[Y \mid \theta] be an unknown function that we can only sample. We want θ⋆\theta^\star with g(θ⋆)=0g(\theta^\star) = 0. Each measurement is Yn=g(θn)+εnY_n = g(\theta_n) + \varepsilon_n, where εn\varepsilon_n is noise with mean zero. The iteration is

θn+1=θn−anYn\theta_{n+1} = \theta_n - a_n Y_n

with step sizes an>0a_n > 0.

The step sizes control everything, and the theorem about them is the heart of the method.

Step 1 in symbols: the measurement

Write Yn=g(θn)+εnY_n = g(\theta_n) + \varepsilon_n. The term g(θn)g(\theta_n) is the signal, the part we want. The term εn\varepsilon_n is the noise. Because E[εn]=0\mathbb{E}[\varepsilon_n] = 0, the average measurement is g(θn)g(\theta_n), which is the reason a noisy measurement carries useful information on average.

Step 2 in symbols: the error

Subtract θ⋆\theta^\star from both sides of the update:

θn+1−θ⋆=(θn−θ⋆)−an(g(θn)+εn)\theta_{n+1} - \theta^\star = (\theta_n - \theta^\star) - a_n\bigl(g(\theta_n) + \varepsilon_n\bigr)

The term g(θn)g(\theta_n) pulls the error toward zero when gg has the right sign. If gg is increasing, it is positive to the right of θ⋆\theta^\star and negative to the left, so g(θ)(θ−θ⋆)>0g(\theta)(\theta - \theta^\star) > 0 for θ≠θ⋆\theta \neq \theta^\star. The update subtracts ang(θn)a_n g(\theta_n), so on average each step moves the guess closer to θ⋆\theta^\star. The term anεna_n \varepsilon_n is the random kick that the rest of the analysis must control.

Step 3 in symbols: the step-size conditions

The Robbins-Monro theorem requires two conditions on the step sizes:

∑n=1∞an=∞and∑n=1∞an2<∞\sum_{n=1}^{\infty} a_n = \infty \qquad\text{and}\qquad \sum_{n=1}^{\infty} a_n^2 < \infty

The first condition says the steps add up to an infinite total, so the guess can travel from any starting point to θ⋆\theta^\star however far away it begins. The second says the squares of the steps add up to a finite total. The noise kicks have variance an2Var⁡(εn)a_n^2 \operatorname{Var}(\varepsilon_n), so their total variance is finite, and the random wandering settles instead of growing without limit.

Under these two conditions, plus a bound on the noise variance and the sign and growth conditions on gg from Step 2, the guesses converge to θ⋆\theta^\star in mean square and almost surely. Mean-square convergence also implies Convergence in Probability.

Step 4 in symbols: the standard choice

The choice an=1/na_n = 1/n satisfies both conditions. The harmonic series ∑1/n\sum 1/n diverges, while ∑1/n2\sum 1/n^2 converges to π2/6≈1.645\pi^2/6 \approx 1.645.

For a concrete link, take g(θ)=θ−μg(\theta) = \theta - \mu, so θ⋆=μ\theta^\star = \mu, and observe XnX_n with E[Xn]=μ\mathbb{E}[X_n] = \mu. Set Yn=θn−XnY_n = \theta_n - X_n. Then the update with an=1/na_n = 1/n becomes

θn+1=θn+1n(Xn−θn)\theta_{n+1} = \theta_n + \frac{1}{n}(X_n - \theta_n)

If θn\theta_n is the average of the first n−1n - 1 observations, this update gives the average of the first nn. So the sample mean is the Robbins-Monro method applied to the simplest possible gg.

Worked examples

Example
1. Four steps by hand (tiny)

Let g(θ)=θ−2g(\theta) = \theta - 2, with no noise, θ1=0\theta_1 = 0, and step sizes a1=12a_1 = \tfrac12, a2=14a_2 = \tfrac14.

  1. The first measurement is Y1=g(0)=−2Y_1 = g(0) = -2.
  2. The update is θ2=0−12⋅(−2)=1\theta_2 = 0 - \tfrac12 \cdot (-2) = 1. The error fell from 2 to 1.
  3. The second measurement is Y2=g(1)=−1Y_2 = g(1) = -1.
  4. The update is θ3=1−14⋅(−1)=1.25\theta_3 = 1 - \tfrac14 \cdot (-1) = 1.25. The error fell from 1 to 0.75.
  5. Each step moves the guess toward 2 by a fraction of the error. The fraction is ana_n.

Interpretation: with exact measurements the method is a step toward the zero that gets shorter as the steps shrink. The error does not reach zero in a finite number of steps unless the step size is exactly right.

Example
2. The running mean (standard)

The observations are X1=4X_1 = 4, X2=8X_2 = 8, X3=6X_3 = 6, X4=2X_4 = 2. We want their mean μ\mu, so use g(θ)=θ−μg(\theta) = \theta - \mu, measurements Yn=θn−XnY_n = \theta_n - X_n, an=1/na_n = 1/n and θ1=0\theta_1 = 0.

  1. The update is θn+1=θn+1n(Xn−θn)\theta_{n+1} = \theta_n + \frac{1}{n}(X_n - \theta_n).
  2. Step 1: θ2=0+11(4−0)=4\theta_2 = 0 + \tfrac11(4 - 0) = 4. This is the average of one value, 4.
  3. Step 2: θ3=4+12(8−4)=6\theta_3 = 4 + \tfrac12(8 - 4) = 6. This is the average of 4 and 8, which is 6.
  4. Step 3: θ4=6+13(6−6)=6\theta_4 = 6 + \tfrac13(6 - 6) = 6. This is the average of 4, 8 and 6, which is 6.
  5. Step 4: θ5=6+14(2−6)=5\theta_5 = 6 + \tfrac14(2 - 6) = 5. This is the average of all four values, (4+8+6+2)/4=5(4 + 8 + 6 + 2)/4 = 5.
  6. The sequence of guesses is 0, 4, 6, 6, 5. The final guess equals the sample mean exactly.

Interpretation: the Robbins-Monro update with an=1/na_n = 1/n is an online way to compute an average. Each new observation nudges the estimate by its share of the total.

Example
3. A noisy thermometer and a constant step (realistic)

A heater setting θ\theta should make the temperature reading equal 2 units above a target. The reading is g(θ)=3θ−6g(\theta) = 3\theta - 6, so the setting that works is θ⋆=2\theta^\star = 2. Each reading is noisy. Start at θ1=5\theta_1 = 5 and use the constant step an=0.1a_n = 0.1. The noise values are +0.5+0.5 at the first step, −0.2-0.2 at the second, and 00 at the third.

  1. Step 1: g(5)=9g(5) = 9, so Y1=9+0.5=9.5Y_1 = 9 + 0.5 = 9.5, and θ2=5−0.1⋅9.5=4.05\theta_2 = 5 - 0.1 \cdot 9.5 = 4.05.
  2. Step 2: g(4.05)=6.15g(4.05) = 6.15, so Y2=6.15−0.2=5.95Y_2 = 6.15 - 0.2 = 5.95, and θ3=4.05−0.1⋅5.95=3.455\theta_3 = 4.05 - 0.1 \cdot 5.95 = 3.455.
  3. Step 3: g(3.455)=4.365g(3.455) = 4.365, so Y3=4.365Y_3 = 4.365, and θ4=3.455−0.1⋅4.365=3.0185\theta_4 = 3.455 - 0.1 \cdot 4.365 = 3.0185.
  4. The distance to the target fell from 3 to 2.05, then 1.455, then 1.0185. Each step closes about 30 percent of the gap, since the slope 3 times the step 0.1 is 0.3.
  5. The constant step does not meet the condition ∑an2<∞\sum a_n^2 < \infty. Noise keeps adding kicks of size about 0.10.1 forever, so the guesses will never settle exactly. They will hover near 2 within a band set by the step size.

Interpretation: a constant step gives fast early progress and a permanent wobble. Shrinking the step, for example to an=1/(n+9)a_n = 1/(n+9), trades speed for a smaller wobble.

Try it: the next step is n=4n = 4, with noise 00 and θ4=3.0185\theta_4 = 3.0185. What is θ5\theta_5?

Answer: g(3.0185)=9.0555−6=3.0555g(3.0185) = 9.0555 - 6 = 3.0555, so Y4=3.0555Y_4 = 3.0555 and θ5=3.0185−0.1⋅3.0555=2.71295\theta_5 = 3.0185 - 0.1 \cdot 3.0555 = 2.71295, which is about 2.7132.713. The guess is closer to 2 again.

Common mistakes and tradeoffs

It is tempting to think that noisy steps should always be small. In fact small constant steps are not enough. The step sizes must shrink, with ∑an2<∞\sum a_n^2 < \infty, because a constant step keeps the noise alive forever.

It is tempting to think the steps should shrink as fast as possible. In fact shrinking too fast is a failure too. If ∑an<∞\sum a_n < \infty, for example an=1/n2a_n = 1/n^2, the steps add up to a finite total, and the guesses can freeze before they reach θ⋆\theta^\star. The condition ∑an=∞\sum a_n = \infty is what guarantees arrival from any start.

It is tempting to think stochastic approximation is only gradient descent with noise. In fact it solves any root-finding problem, g(θ)=0g(\theta) = 0, and it needs only noisy values of gg. Gradient descent is the special case where gg is the gradient of a loss.

It is tempting to think that a small error in the final guess means a small error in the answer for every run. In fact the guarantee is about the limit, and any finite run has some error. The error can be reduced by averaging the guesses over time, a standard refinement.

When to use it: when each evaluation of the function is noisy or expensive, and you can afford many cheap, noisy steps. It fits online settings where data arrive one at a time. When not to use it: when the function can be evaluated exactly and cheaply, a case where Newton-Raphson or bisection finds the zero in far fewer steps. When the parameter has many dimensions, estimating a full gradient at each step is costly, and simultaneous perturbation methods reduce the cost by perturbing all parameters at once.

History

Herbert Robbins and Sutton Monro published "A Stochastic Approximation Method" in 1951. They posed the problem of finding the root of a function observed only with random error, and they proved that the iteration converges under step-size conditions like the ones above. A year later Jack Kiefer and Jacob Wolfowitz treated the related problem of finding a maximum, using finite differences in place of a direct derivative. Their work is the ancestor of the modern gradient methods used in training. The method spread through statistics and control engineering in the decades after, because it needs nothing more than noisy readings, which experiments and sensors always provide.

Recap

  • The goal is an input θ⋆\theta^\star with g(θ⋆)=0g(\theta^\star) = 0, where gg is observed only through noisy measurements.
  • The update θn+1=θn−anYn\theta_{n+1} = \theta_n - a_n Y_n steps against each noisy measurement.
  • The step sizes must satisfy ∑an=∞\sum a_n = \infty, so the guess can reach the zero, and ∑an2<∞\sum a_n^2 < \infty, so the noise settles.
  • With an=1/na_n = 1/n and g(θ)=θ−μg(\theta) = \theta - \mu, the method computes the sample mean.
  • Stochastic gradient descent is this method applied to a gradient, and the constant-step version wobbles near the answer.

Read next: Simultaneous Perturbation Stochastic Approximation, which estimates a gradient cheaply when the parameter has many coordinates, and Stochastic Gradient Descent, which applies these ideas to training.

Check yourself

QuestionState the two conditions on the step sizes ana_n that the Robbins-Monro theorem requires.Show answer
The steps must add up to infinity, ∑an=∞\sum a_n = \infty, so the guess can reach the zero from any start. The squares of the steps must add up to a finite total, ∑an2<∞\sum a_n^2 < \infty, so the noise settles.
QuestionLet g(θ)=4θ−8g(\theta) = 4\theta - 8, with no noise, θ1=3\theta_1 = 3 and a1=0.25a_1 = 0.25. What is θ2\theta_2?Show answer
The measurement is Y1=g(3)=12−8=4Y_1 = g(3) = 12 - 8 = 4. The update is θ2=3−0.25⋅4=2\theta_2 = 3 - 0.25 \cdot 4 = 2.
QuestionWhy does the second step-size condition, ∑an2<∞\sum a_n^2 < \infty, matter for the noise?Show answer
Each noise term enters the update multiplied by ana_n, so its variance is an2Var⁡(εn)a_n^2 \operatorname{Var}(\varepsilon_n). If the sum of these variances is finite, the total random kick from all steps is bounded, and the guesses cannot wander forever.
QuestionAn engineer uses a constant step an=0.1a_n = 0.1 to tune a setting against noisy readings. The guess settles within 0.3 of the answer but keeps moving. Explain why, and name one change that would make it settle.Show answer
A constant step does not satisfy ∑an2<∞\sum a_n^2 < \infty, so the noise keeps adding kicks of about 0.1 forever. The guess hovers in a band set by the step size. Making the step shrink over time, for example an=c/na_n = c/n, would let the guess settle.

Sources