Part I · Foundations of Sequential Decision-Making

2.The Mathematical Toolkit

S&B prerequisitesKober §1.3Pendle
The relationship between disciplines has sufficient promise to be likened to that between physics and mathematics.
Jens Kober, J. Andrew Bagnell & Jan Peters · On the relationship between robotics and reinforcement learning
Reinforcement Learning in Robotics — A Survey, IJRR 2013

Chapter 1 left three debts: the loop is not yet mathematics, reward is not yet a random variable, and 'eventually finds the best policy' is not yet a theorem. This chapter pays all three. We build the probability that makes expectations meaningful, the contraction machinery that every convergence proof in this book eventually invokes, the discretization that turns a robot's continuous physics into discrete decisions, and the Robbins–Monro theorem — the single result that explains why learning from noisy samples works at all. Pendle arrives to carry it, because he is simple enough that every calculation can be checked by hand.

Foundation

Probability spaces, conditional expectation and the tower property, contraction mappings with Banach proved in full, ODE discretization with error orders, and Robbins–Monro convergence.

Conceptual

A contraction you can iterate at any γ, and Pendle's dynamics integrated three ways so you can watch explicit Euler manufacture energy from nothing.

Practical

Pendle's dynamics and integrators in Rust with nalgebra, plus a Robbins–Monro estimator you can drive with noisy samples.

After this chapter you can

  • State what a random variable and a conditional expectation actually are, and use the tower property fluently
  • Prove the Banach fixed-point theorem and explain why it guarantees a unique optimal value function
  • Discretize a continuous robot dynamics with Euler and RK4, and predict which one will explode and when
  • State the Robbins–Monro conditions and recognize them in every learning rate you will meet later
  • Read the incremental update rule New ← Old + α(Target − Old) as an instance of stochastic approximation

2.1 Probability, quickly but honestly

Chapter 1 said the world "advances" after an action. Making that precise requires almost no machinery, but it does require the right machinery.

A probability space is a triple (Ω,F,P)(\Omega, \mathcal{F}, \mathbb{P}): a set of outcomes Ω\Omega, a collection F\mathcal{F} of events we can assign probabilities to, and a measure P\mathbb{P} assigning each event a number in [0,1][0,1] with P(Ω)=1\mathbb{P}(\Omega) = 1. A random variable XX is a function from Ω\Omega to R\mathbb{R} — not a number, a function. When we write X=3X = 3 we mean the event {ω:X(ω)=3}\{\omega : X(\omega) = 3\}.

This matters for us because Rusty's next position is exactly such a function: it depends on the wheel slip that happened to occur, which is the outcome ω\omega. Two identical commands from two identical positions produce two different results, and the difference is not error — it is the environment.

The expectation of XX is its probability-weighted average, E[X]=ωP(ω)X(ω)\mathbb{E}[X] = \sum_\omega \mathbb{P}(\omega) X(\omega) in the discrete case. Expectation is linear, which is the property we will lean on constantly:

E[aX+bY]=aE[X]+bE[Y]\mathbb{E}[aX + bY] = a\,\mathbb{E}[X] + b\,\mathbb{E}[Y]

— and note this holds whether or not XX and YY are independent. A surprising amount of reinforcement learning is linearity of expectation applied patiently.

Definition 2.1Conditional expectation

For random variables XX and YY, the conditional expectation E[XY]\mathbb{E}[X \mid Y] is a random variable — a function of YY — whose value at Y=yY = y is the average of XX over the outcomes where Y=yY = y:

E[XY=y]=ωP(ωY=y)X(ω).\mathbb{E}[X \mid Y = y] = \sum_\omega \mathbb{P}(\omega \mid Y = y)\, X(\omega).

The single most useful fact about conditional expectation is that averaging it recovers the unconditional average.

Theorem 2.2Tower property (law of total expectation)

E[E[XY]]=E[X].\mathbb{E}\big[\mathbb{E}[X \mid Y]\big] = \mathbb{E}[X].

Proof

Write the outer expectation as a sum over values of YY, then expand the inner one:

E[E[XY]]=yP(Y=y)E[XY=y]=yP(Y=y)ωP(ωY=y)X(ω).\mathbb{E}\big[\mathbb{E}[X \mid Y]\big] = \sum_y \mathbb{P}(Y = y)\, \mathbb{E}[X \mid Y = y] = \sum_y \mathbb{P}(Y = y) \sum_\omega \mathbb{P}(\omega \mid Y = y) X(\omega).

By the definition of conditional probability, P(Y=y)P(ωY=y)=P(ω,Y=y)\mathbb{P}(Y = y)\,\mathbb{P}(\omega \mid Y=y) = \mathbb{P}(\omega,\, Y = y). Substituting and exchanging the order of summation:

=ωX(ω)yP(ω,Y=y)=ωX(ω)P(ω)=E[X].= \sum_\omega X(\omega) \sum_y \mathbb{P}(\omega,\, Y = y) = \sum_\omega X(\omega)\, \mathbb{P}(\omega) = \mathbb{E}[X]. \qquad \blacksquare

Every Bellman equation in this book is the tower property applied to a return, conditioned on the first action. That is genuinely all it is. When Chapter 4 derives

vπ(s)=Eπ ⁣[Rt+1+γvπ(St+1)|St=s],v_\pi(s) = \mathbb{E}_\pi\!\left[R_{t+1} + \gamma\, v_\pi(S_{t+1}) \,\middle|\, S_t = s\right],

the derivation will consist of splitting the return into "first reward" plus "the rest", conditioning on where you land, and invoking Theorem 2.2.

2.2 Markov chains and the memory assumption

A sequence of random variables S0,S1,S2,S_0, S_1, S_2, \ldots has the Markov property if the future is conditionally independent of the past given the present:

P(St+1=sSt,St1,,S0)=P(St+1=sSt).\mathbb{P}(S_{t+1} = s' \mid S_t, S_{t-1}, \ldots, S_0) = \mathbb{P}(S_{t+1} = s' \mid S_t).

The state is then a sufficient statistic for prediction: knowing it, the history adds nothing.

2.3 Contraction mappings and the theorem behind everything

Here is the mathematical heart of the chapter — the result that will justify value iteration, Q-learning, and half of Part I.

Definition 2.3Contraction mapping

Let (X,d)(\mathcal{X}, d) be a metric space. An operator T:XXT : \mathcal{X} \to \mathcal{X} is a γ\gamma-contraction for γ[0,1)\gamma \in [0,1) if for all x,yXx, y \in \mathcal{X},

d(Tx,Ty)γd(x,y).d(T x, T y) \le \gamma\, d(x, y).

Applying TT moves any two points strictly closer together. Intuitively, repeated application must crush everything toward a single point. The theorem says exactly that, and adds a computable error bound.

Theorem 2.4Banach fixed-point theorem

Let (X,d)(\mathcal{X}, d) be a complete metric space and TT a γ\gamma-contraction. Then:

  1. TT has exactly one fixed point xx^* with Tx=xT x^* = x^*;
  2. for any starting point x0x_0, the sequence xk+1=Txkx_{k+1} = T x_k converges to xx^*;
  3. the convergence is geometric, with the a-priori bound

d(xk,x)γk1γd(x1,x0).d(x_k, x^*) \le \frac{\gamma^k}{1 - \gamma}\, d(x_1, x_0).

Proof

The sequence is Cauchy. From the contraction property, consecutive iterates satisfy d(xk+1,xk)γd(xk,xk1)d(x_{k+1}, x_k) \le \gamma\, d(x_k, x_{k-1}), and by induction

d(xk+1,xk)γkd(x1,x0).d(x_{k+1}, x_k) \le \gamma^k\, d(x_1, x_0).

For any m>km > k, the triangle inequality and the geometric series give

d(xm,xk)i=km1d(xi+1,xi)i=km1γid(x1,x0)γk1γd(x1,x0).d(x_m, x_k) \le \sum_{i=k}^{m-1} d(x_{i+1}, x_i) \le \sum_{i=k}^{m-1} \gamma^i\, d(x_1, x_0) \le \frac{\gamma^k}{1-\gamma}\, d(x_1, x_0).

Since γ<1\gamma < 1, the right-hand side goes to zero as kk \to \infty, so the sequence is Cauchy. Completeness gives a limit xx^*.

The limit is a fixed point. A contraction is continuous (take δ=ε\delta = \varepsilon in the definition), so

Tx=T(limkxk)=limkTxk=limkxk+1=x.T x^* = T\big(\lim_k x_k\big) = \lim_k T x_k = \lim_k x_{k+1} = x^*.

Uniqueness. Suppose Tx=xTx^* = x^* and Ty=yTy^* = y^*. Then d(x,y)=d(Tx,Ty)γd(x,y)d(x^*, y^*) = d(Tx^*, Ty^*) \le \gamma\, d(x^*, y^*), so (1γ)d(x,y)0(1-\gamma)\, d(x^*, y^*) \le 0. Since γ<1\gamma < 1, this forces d(x,y)=0d(x^*, y^*) = 0, hence x=yx^* = y^*.

The bound. Let mm \to \infty in the Cauchy estimate above. \qquad \blacksquare

Three payoffs, all collected later: existence (an optimal value function exists), uniqueness (there is only one, so "the" optimal value function is well defined), and an algorithm with a stopping rule (iterate, and the bound tells you when to stop).

A contraction, iterated

ch02-contraction-map

T(x) = γx + 2 — every application shrinks distances by a factor γ, so the iterates must converge to a single point.

  • iterate xₖ
  • fixed point x*

Fixed point x*

10.0

c / (1 − γ)

Actual error

0.005

|x24 − x*|

A-priori bound

0.005

γᵏ‖x₁−x₀‖/(1−γ)

The bound never dips below the error — as the theorem guarantees.

Move x₀ anywhere: the iterates always land on the same x*. That is uniqueness. Push γ toward 1 and convergence slows to a crawl — the same slowdown you will feel in Chapter 5 when a far-sighted discount makes value iteration take hundreds of sweeps, and the reason γ is a modeling decision rather than a free parameter.

2.4 From continuous physics to discrete decisions

Robots obey differential equations. Reinforcement learning acts at discrete time steps. Something must bridge them, and the bridge is not free.

Meet Pendle, a torque-actuated pendulum, measured with θ=0\theta = 0 at the upright position:

m2θ¨=mgsinθbθ˙+τ.m \ell^2 \ddot\theta = m g \ell \sin\theta - b\,\dot\theta + \tau.

Writing the state as x=[θ,θ˙]x = [\theta, \dot\theta]^\top turns this into a first-order system x˙=f(x,τ)\dot x = f(x, \tau). To simulate it we need to advance xx by a finite step Δt\Delta t. The simplest choice is explicit Euler:

xk+1=xk+Δtf(xk,τk),x_{k+1} = x_k + \Delta t \cdot f(x_k, \tau_k),

which is exact if ff is constant over the interval and wrong otherwise, with local error O(Δt2)O(\Delta t^2) and global error O(Δt)O(\Delta t). Fourth-order Runge–Kutta samples the derivative four times per step:

xk+1=xk+Δt6(k1+2k2+2k3+k4),x_{k+1} = x_k + \frac{\Delta t}{6}\left(k_1 + 2k_2 + 2k_3 + k_4\right),

with global error O(Δt4)O(\Delta t^4) — four extra derivative evaluations buying three extra orders of accuracy.

The difference is not academic. With τ=0\tau = 0 the true pendulum conserves total mechanical energy exactly. Explicit Euler does not merely approximate that conservation — it systematically violates it, injecting energy at every step until the pendulum spins up out of nothing.

Pendle: continuous dynamics, discrete steps

ch02-integrator-playground

m ℓ² θ̈ = m g ℓ sin θ − b θ̇ + τ, integrated three ways. θ = 0 is upright.

Integrator
Torque
drag meθ = 171.4° · θ̇ = 0.00 rad/s

Energy

-9.70J

should stay flat at τ=0

Δt

20.0ms

50 Hz control rate

  • explicit Euler
  • semi-implicit Euler
  • RK4
Total mechanical energy under each integrator from the same initial state, τ = 0. A flat line is correct physics.
Grab the bob and fling it, or knock it with the button — then switch the torque to energy swing-up and watch the controller fight its way back to upright. With τ = 0 the true system conserves energy exactly. Explicit Euler's energy climbs without bound (the pendulum spins up out of nothing), semi-implicit Euler oscillates around the truth, and RK4 tracks it to fourth order. Chapter 15 shows the same arithmetic deciding whether a policy trained in simulation survives contact with a real robot.
Rustrl-envs/src/pendle.rs
rust
use nalgebra::Vector2;
 
pub struct PendleParams {
    pub mass: f64,
    pub length: f64,
    pub gravity: f64,
    pub damping: f64,
}
 
/// ẋ = f(x, τ) for the torque-actuated pendulum, θ = 0 at upright.
pub fn dynamics(x: &Vector2<f64>, tau: f64, p: &PendleParams) -> Vector2<f64> {
    let (theta, omega) = (x[0], x[1]);
    let inertia = p.mass * p.length * p.length;
    let alpha = (p.mass * p.gravity * p.length * theta.sin()
                 - p.damping * omega
                 + tau) / inertia;
    Vector2::new(omega, alpha)
}
 
pub fn euler_step(x: &Vector2<f64>, tau: f64, dt: f64, p: &PendleParams) -> Vector2<f64> {
    x + dt * dynamics(x, tau, p)
}
 
pub fn rk4_step(x: &Vector2<f64>, tau: f64, dt: f64, p: &PendleParams) -> Vector2<f64> {
    let k1 = dynamics(x, tau, p);
    let k2 = dynamics(&(x + 0.5 * dt * k1), tau, p);
    let k3 = dynamics(&(x + 0.5 * dt * k2), tau, p);
    let k4 = dynamics(&(x + dt * k3), tau, p);
    x + (dt / 6.0) * (k1 + 2.0 * k2 + 2.0 * k3 + k4)
}
Pendle's dynamics and two integrators. The state is [θ, θ̇]; note that rk4_step is a drop-in replacement for euler_step, which is exactly how the widget above swaps them.

2.5 Learning from noisy samples: Robbins–Monro

Now the result that makes learning possible at all.

Suppose you want to find the root of a function h(θ)=0h(\theta) = 0, but you can never evaluate hh — only a noisy sample H(θ)=h(θ)+noiseH(\theta) = h(\theta) + \text{noise}. This is precisely our situation: we want the expected return, and we only ever observe one noisy trajectory at a time.

Robbins and Monro's answer, from 1951, is to take small steps in the direction each noisy sample suggests, with a step size that shrinks:

θk+1=θk+αkH(θk).\theta_{k+1} = \theta_k + \alpha_k\, H(\theta_k).

Theorem 2.5Robbins–Monro conditions

Under regularity conditions on hh and bounded noise variance, the iterates θk\theta_k converge almost surely to the root θ\theta^* provided the step sizes satisfy

k=1αk=andk=1αk2<.\sum_{k=1}^{\infty} \alpha_k = \infty \qquad \text{and} \qquad \sum_{k=1}^{\infty} \alpha_k^2 < \infty.

The two conditions have clean interpretations, and it is worth being able to recite them:

  • αk=\sum \alpha_k = \inftythe steps must be able to travel infinitely far. If they sum to something finite, the iterate can never reach a root that lies beyond that distance, no matter how many samples arrive.
  • αk2<\sum \alpha_k^2 < \inftythe steps must shrink fast enough to average out the noise. Otherwise the iterate rattles around the root forever without settling.

The canonical choice αk=1/k\alpha_k = 1/k satisfies both: the harmonic series diverges, and the sum of 1/k21/k^2 converges to π2/6\pi^2/6. A constant α\alpha satisfies the first but not the second — which is why constant learning rates track a moving target instead of converging, exactly the trade you want when the environment is non-stationary.

2.6 Linear algebra and gradients, in the amount we need

Two more tools, stated without ceremony because they will be used constantly.

Matrices as operators. Given a policy on a finite state space, the transition probabilities form a matrix PπP_\pi, and expected rewards a vector rπr_\pi. Chapter 4 will show the value function satisfies vπ=rπ+γPπvπv_\pi = r_\pi + \gamma P_\pi v_\pi, whose solution is

vπ=(IγPπ)1rπ.v_\pi = (I - \gamma P_\pi)^{-1} r_\pi.

The inverse exists because γPπ\gamma P_\pi has spectral radius at most γ<1\gamma < 1, so the Neumann series k(γPπ)k\sum_k (\gamma P_\pi)^k converges — the same geometric argument as the Banach bound, wearing matrix clothes.

Gradients. For J:RnRJ : \mathbb{R}^n \to \mathbb{R}, the gradient J(θ)\nabla J(\theta) points in the direction of steepest increase, and gradient ascent takes θθ+αJ(θ)\theta \leftarrow \theta + \alpha \nabla J(\theta). Chapter 10 spends its entire length computing one specific gradient — that of expected return with respect to policy parameters — which is hard precisely because the distribution you are averaging over depends on the parameters you are differentiating.

2.7 Chapter bridge

The debts of Chapter 1 are paid. "The world advances" is a conditional distribution. "Reward" is a random variable with an expectation. "Eventually finds the best policy" will be Banach's theorem applied to an operator we have not yet built.

Chapter 3 puts the toolkit to work on the smallest problem that still contains the essential difficulty: a single decision, repeated, with no state at all. Stripping away state exposes the exploration–exploitation dilemma in its pure form, and lets us derive real regret bounds with the concentration inequalities we now have the vocabulary for. Reacher joins the cast there, choosing among grasp primitives where every attempt costs hardware wear.

  1. 01Foundation●●Tower property by hand

    Let X be the total reward from a two-step episode and Y the first action. Compute E[X] two ways — directly, and via E[E[X|Y]] — for a small example you construct. They must agree; make sure you see why.

  2. 02Foundation●●Is the operator a contraction?

    Show that T(x) = γx + c is a γ-contraction on ℝ with the usual metric, and find its fixed point in closed form. Then show that T(x) = x + c is not a contraction for any c ≠ 0, and explain what goes wrong with Banach’s conclusion.

  3. 03Foundation●●Robbins–Monro schedules

    Which of these satisfy both Robbins–Monro conditions? (a) α_k = 1/k, (b) α_k = 1/k², (c) α_k = 1/√k, (d) α_k = 0.1. For each failure, say which condition breaks and what behaviour that produces in practice.

  4. 04Conceptual●●The γ → 1 slowdown

    In the contraction widget, record the number of iterations needed to reach an error below 0.01 for γ = 0.5, 0.9, 0.95, 0.99. Plot iterations against 1/(1−γ). What relationship do you find, and why does the a-priori bound predict it?

  5. 05Conceptual●●Find the stability boundary

    In the Pendle playground with τ = 0, find the largest Δt at which explicit Euler keeps energy within 10% over ten seconds. Repeat for RK4. Express the ratio — that is the compute budget RK4 buys you per step.

  6. 06Practical●●Implement Robbins–Monro

    Write a seeded estimator that finds the mean of a noisy signal using α_k = 1/k, and a second using constant α = 0.1. Feed both a signal whose mean jumps halfway through the run. Plot both trajectories and explain which you would deploy on a robot whose payload changes.

  7. 07Practical●●●Verify the error orders

    Empirically confirm that Euler’s global error is O(Δt) and RK4’s is O(Δt⁴): simulate Pendle for a fixed horizon at halving step sizes, measure error against a very-fine-step reference, and fit the slope on a log–log plot. The slopes should come out near 1 and 4.

References

Baseline references

  • Sutton, R. S. & Barto, A. G. (2018). Reinforcement Learning: An Introduction. MIT Press, 2nd edition
    §2.4 incremental implementation — the Robbins–Monro special case that opens Chapter 3; §3.1 for the Markov property.
  • Kober, J., Bagnell, J. A. & Peters, J. (2013). Reinforcement Learning in Robotics: A Survey. International Journal of Robotics Research 32(11)
    §1.3 for the continuous state–action setting that motivates the discretization material here.

Further reading & modern sources

  • Robbins, H. & Monro, S. (1951). A Stochastic Approximation Method. Annals of Mathematical Statistics 22(3)
    The original paper. Short, readable, and the ancestor of every learning rate in this book.
  • Banach, S. (1922). Sur les opérations dans les ensembles abstraits et leur application aux équations intégrales. Fundamenta Mathematicae 3
    The fixed-point theorem, in the original.
  • Bertsekas, D. P. & Tsitsiklis, J. N. (1996). Neuro-Dynamic Programming. Athena Scientific
    The rigorous treatment of stochastic approximation applied to dynamic programming. Where to go when this book says "the proof lives elsewhere".
  • Hairer, E., Nørsett, S. P. & Wanner, G. (1993). Solving Ordinary Differential Equations I: Nonstiff Problems. Springer
    The reference for integrator error orders and stability regions — including why symplectic integrators behave so much better on mechanical systems.