Part 7 · Chapter 28

Stochastic and Dynamic Optimization

Every model so far has assumed the numbers are known: fixed costs, fixed demands, a fixed objective sitting still to be minimised. Reality withholds both of those courtesies. The data is often random — demand, prices, wind, failures are known only as distributions — and decisions often unfold over time, each one made before the next uncertainty is revealed. This chapter takes on both. The first half asks how to decide well when the data is a random variable, through stochastic programming with recourse and chance constraints. The second half asks how to decide well in sequence, when today's choice shapes tomorrow's situation, through Markov decision processes and the Bellman equation. The unifying lesson is blunt: planning for the average is not the same as planning for uncertainty, and the difference has a price you can compute.

Optimization Techniques Prof. Mithun Mondal Reading time ≈ 60 min
i What you'll learn
  • Why optimizing with the average of a random input is not the same as optimizing under uncertainty — the flaw of averages.
  • Two-stage stochastic programming: here-and-now decisions, a random outcome, then recourse.
  • The EVPI and VSS — how much a perfect forecast is worth, and how much modelling uncertainty is worth.
  • Chance-constrained and robust optimization — satisfying constraints with high probability, or against the worst case.
  • Markov decision processes: states, actions, transition probabilities, rewards and policies.
  • The Bellman equation, value iteration and policy iteration, and how discounting sets the planning horizon.
Section 28-1

When the Data Is Random

The most tempting way to handle an uncertain input is to replace it with its average and solve the deterministic problem that results. It feels reasonable, and it is almost always wrong. The trap has a name — the flaw of averages — and its essence is that the optimal decision for the average scenario is generally not the decision that performs best on average across all scenarios. Plan a factory for average demand and it will be idle in the slump and overwhelmed in the boom, and the cost of being wrong in both directions is exactly what averaging throws away.

The reason is structural. Costs are usually asymmetric and nonlinear in the uncertain quantity: running short might cost far more per unit than running long, or vice versa. When the cost of a decision is a curved (convex) function of the random outcome, the expected cost is not the cost at the expected outcome — a fact known as Jensen's inequality. A plan tuned to the mean sits at the bottom of the wrong curve.

Optimize then average, not average then optimize. Deterministic optimization at the mean input answers "what is best if the future is exactly average?" — a future that never actually occurs. Stochastic optimization answers the question that matters: "what single decision minimises my expected cost across every future that might occur?" Example 1 shows the two answers differing by a concrete, computable amount, and every method in this chapter exists to compute the second answer honestly.
Section 28-2

Two-Stage Programming with Recourse

Most decisions under uncertainty share a common shape: you commit to something now, the world then reveals itself, and you adjust as best you can. Two-stage stochastic programming formalises exactly this. The first-stage (or here-and-now) decision \(\mathbf{x}\) is made before the random outcome \(\omega\) is known. Then \(\omega\) is observed, and a second-stage (or recourse) decision \(\mathbf{y}(\omega)\) cleans up — meeting shortfalls, absorbing surpluses, correcting the plan for the outcome that actually happened.

decide x here-and-now ω revealed uncertainty resolves recourse y(ω) wait-and-see
The two-stage decision timeline
x p=0.7 p=0.3 d=40 (low) recourse y₁ d=100 (high) recourse y₂
A two-scenario tree

Because the recourse cost depends on the random outcome, the objective minimises the first-stage cost plus the expected cost of the best recourse:

🔑
The two-stage recourse problem
\[ \min_{\mathbf{x}\in\Omega}\ \ \mathbf{c}^{\mathsf T}\mathbf{x} \;+\; \mathbb{E}_{\omega}\!\big[\,Q(\mathbf{x},\omega)\,\big], \qquad Q(\mathbf{x},\omega) = \min_{\mathbf{y}\ge 0}\ \big\{\, \mathbf{q}^{\mathsf T}\mathbf{y} \; : \; W\mathbf{y} = \mathbf{h}(\omega) - T(\omega)\,\mathbf{x} \,\big\} \]

The inner problem \(Q(\mathbf{x},\omega)\) is the best recourse once \(\omega\) is known; the outer problem chooses \(\mathbf{x}\) to minimise its immediate cost plus the expected recourse. With finitely many scenarios the expectation is a weighted sum, and the whole thing becomes one large deterministic program — one copy of the recourse variables per scenario, all tied to a single first-stage \(\mathbf{x}\).

That last point is what makes stochastic programming practical. A continuous distribution is approximated by a scenario tree — a finite set of outcomes with probabilities — and the expectation collapses into a sum over the leaves. The art is keeping the tree small enough to solve yet rich enough to capture the uncertainty that actually drives the decision.

Section 28-3

The Value of Modelling Uncertainty

Two questions immediately follow. First, how much would a perfect forecast be worth — the ability to know \(\omega\) before deciding? Second, how much does it help to model the uncertainty at all, rather than just planning for the mean? Both answers are numbers you can compute, and they are among the most useful quantities in the whole subject because they justify the modelling effort in the language of money.

Three benchmark solutions frame them. The recourse problem value \(RP\) is the honest stochastic optimum of Section 28-2. The wait-and-see value \(WS\) is the expected cost if you could see each outcome before deciding — the best possible, and generally unattainable. The expected result of the expected-value solution \(EEV\) is what you actually pay if you optimize for the mean and then live in the random world.

🔑
EVPI and VSS
\[ \text{EVPI} = RP - WS, \qquad \text{VSS} = EEV - RP, \qquad WS \;\le\; RP \;\le\; EEV \]

The expected value of perfect information is the most you should ever pay for a perfect forecast — the gap between deciding under uncertainty and deciding with foreknowledge. The value of the stochastic solution is what you save by modelling the uncertainty instead of planning for the average. Both are non-negative; the ordering \(WS \le RP \le EEV\) holds for any minimisation.

The two numbers answer different management questions. A large EVPI says a better forecast — more sensors, a market survey, a weather service — could pay for itself, because uncertainty is genuinely costing you. A large VSS says the stochastic model itself is earning its keep, and that anyone still planning for the average is leaving that much on the table. A small VSS, conversely, is permission to use the simpler deterministic model with a clear conscience. Examples 1–3 compute all of this for one concrete decision.

Section 28-4

Chance Constraints & Robustness

Recourse handles uncertainty in the objective. But sometimes the uncertainty threatens a constraint — a reservoir must not overflow, a network must not exceed capacity, a structure must not fail — and demanding that the constraint hold for every conceivable outcome is often impossibly conservative or literally infeasible. Chance-constrained programming relaxes "always" to "almost always": the constraint need only hold with a specified high probability.

🔑
A chance constraint
\[ \Pr\!\big[\, g(\mathbf{x},\omega) \le 0 \,\big] \;\ge\; 1 - \alpha \]

The constraint may be violated, but only with probability at most \(\alpha\) (a small tolerance such as 0.05). For a requirement like "meet demand," this becomes a service level: choose the capacity so that demand is met at least \(1-\alpha\) of the time. When \(\omega\) is normal, the constraint reduces to a deterministic one involving a quantile \(z_{1-\alpha}\).

The reformulation is clean and worth seeing. If a buffer \(x\) must cover a normal demand \(D \sim \mathcal N(\mu,\sigma^2)\) with probability \(1-\alpha\), then \(\Pr[D \le x] \ge 1-\alpha\) becomes \(x \ge \mu + z_{1-\alpha}\,\sigma\). The uncertainty has been compressed into a single safety margin \(z_{1-\alpha}\sigma\), and tightening the service level simply buys a larger quantile — a cost you can read straight off the normal table, as Example 4 does.

x = μ + z₁₋ₐσ α P(D ≤ x) = 1−α μ
A chance constraint as a quantile
ω₁ ω₂ uncertainty set U nominal worst case feasible for every ω in U
Robust optimization over an uncertainty set

A stricter philosophy abandons probabilities altogether. Robust optimization asks only that the solution remain feasible for every outcome in a bounded uncertainty set \(\mathcal U\), and optimizes against the worst case within it — \(\min_{\mathbf{x}} \max_{\omega\in\mathcal U} f(\mathbf{x},\omega)\). It needs no distribution, only the range of possibilities, which is exactly its appeal when probabilities are unknown or the cost of any failure is unacceptable. The price is conservatism: a robust plan pays a premium to be safe against outcomes that may be extremely unlikely, and the size of \(\mathcal U\) is the dial between safety and that premium.

Section 28-5

Sequential Decisions & MDPs

The second half of the chapter turns from a single uncertain decision to a sequence of them, where each choice not only earns an immediate reward but also moves the system into a new situation that shapes every choice to come. Chapter 17 handled the deterministic version of this with dynamic programming; here the transitions are random, and the right framework is the Markov decision process.

ElementSymbolMeaning
States\(s \in \mathcal S\)The situations the system can be in
Actions\(a \in \mathcal A\)The choices available in a state
Transition\(P(s' \mid s,a)\)Probability of landing in \(s'\) after taking \(a\) in \(s\)
Reward\(r(s,a)\)The immediate payoff for that action
Discount\(\gamma \in [0,1)\)How much a future reward is worth today
Policy\(\pi(s)\)A rule prescribing an action in every state

The defining feature is the Markov property: the transition depends only on the current state and action, not on the path that led there. The state is a sufficient summary of history — everything relevant about the past is baked into where you are now. That single assumption is what makes the problem tractable, because it lets a decision rule depend on the state alone.

The goal is a policy \(\pi\) that maximises the expected discounted sum of rewards, \(\mathbb{E}\big[\sum_{t=0}^{\infty} \gamma^{t} r(s_t, a_t)\big]\). The discount factor \(\gamma\) earns its place twice over: mathematically it keeps an infinite sum finite, and practically it encodes how far ahead the decision maker cares to look — a reward \(t\) steps away is worth \(\gamma^{t}\) of its face value, so the effective horizon is roughly \(1/(1-\gamma)\) steps. At \(\gamma = 0.9\) the planner sees about ten steps ahead; at \(0.99\), a hundred.

s₁ working s₂ broken run: r=10, p=0.8 run: p=0.2 maintain: r=4 repair: r=−5
A two-state maintenance MDP
V(s) max over a P(s′|s,a) V(s′) V(s′) V(s′) r(s,a) + γ · Σ P(s′|s,a) V(s′)
One Bellman backup
Section 28-6

The Bellman Equation

The value of a state under the best policy obeys a single self-referential equation — the stochastic form of Bellman's principle of optimality from Chapter 17. The optimal value of being in state \(s\) is the best immediate reward plus the discounted expected value of wherever that action lands you:

🔑
The Bellman optimality equation
\[ V^{\star}(s) = \max_{a \in \mathcal A}\ \Big[\, r(s,a) \;+\; \gamma \sum_{s'} P(s' \mid s,a)\, V^{\star}(s') \,\Big] \]

Each state's value is defined in terms of its successors' values — a fixed-point equation, not an explicit formula. The optimal policy reads straight off it: \(\pi^{\star}(s)\) is the action \(a\) that achieves the maximum. Solving the MDP means solving this system.

Two algorithms solve it, and both are foundational. Value iteration turns the equation into an update: start from any guess \(V_0\), and repeatedly apply the right-hand side as an assignment, \(V_{k+1}(s) \leftarrow \max_a[\,r(s,a) + \gamma\sum_{s'}P(s'|s,a)V_k(s')\,]\). The map is a contraction whenever \(\gamma < 1\), so the iterates converge geometrically to \(V^{\star}\) from any starting point — a guarantee as clean as the annealing schedule's, and far more usable. Example 5 runs two sweeps by hand.

Policy iteration alternates two steps instead. Given a policy, evaluate it by solving the linear system \(V^{\pi}(s) = r(s,\pi(s)) + \gamma\sum_{s'}P(s'|s,\pi(s))V^{\pi}(s')\); then improve it by making each state greedy with respect to those values. Each round produces a strictly better policy until none changes, and because there are finitely many policies it terminates exactly — usually in very few iterations. Example 6 does one improvement step and watches a lazy policy correct itself.

Value iterationPolicy iteration
Each stepOne Bellman backup per stateFull policy evaluation, then improvement
Cost per stepCheapExpensive (solves a linear system)
Steps to convergeMany (geometric)Few (finite, exact)
ConvergenceTo \(V^{\star}\) in the limitTo the exact optimal policy
When the model is unknown, this becomes reinforcement learning. Value and policy iteration assume the transition probabilities \(P\) and rewards \(r\) are known. When they are not — when an agent must learn them from experience by acting and observing — the same Bellman backup, applied to sampled transitions instead of known ones, becomes the engine of reinforcement learning. The optimization principle of this section is unchanged; only the source of \(P\) and \(r\) differs.
Section 28-7

The Uncertainty Landscape

The methods of this chapter are best organised by two questions: is the uncertainty in the objective or a constraint, and is the decision a single commitment or a sequence over time?

MethodHandles uncertainty inNeedsBest for
Two-stage recourseObjective (expected cost)A distribution / scenario treeCommit now, adjust after the outcome
Chance constraintsA constraint (probabilistically)A distribution + a service level \(1-\alpha\)Reliability and service-level targets
Robust optimizationA constraint (worst-case)Only a bounded uncertainty setUnknown distributions, zero-failure needs
Markov decision processSequential transitionsStates, transitions \(P\), rewards, \(\gamma\)Repeated decisions over time

The through-line of the chapter is a single correction to a natural mistake. The deterministic optimization of the earlier parts silently assumes the future is known — and the moment it is not, planning for the average quietly optimizes the wrong thing. Stochastic programming replaces the average with an expectation over scenarios; chance and robust methods replace "feasible" with "feasible often enough" or "feasible always"; and Markov decision processes replace a one-shot plan with a policy that keeps re-deciding as the world reveals itself. Different machinery, one correction: decide against the distribution of futures, not against a single imagined one.

Section 28-8

Worked Examples

Examples 1–3 share one setup. A firm orders a quantity \(x\) at \(2\) per unit before demand \(D\) is known; \(D = 40\) with probability \(0.7\) and \(D = 100\) with probability \(0.3\). Shortage costs \(5\) per unit (emergency buying), surplus costs \(1\) per unit (holding). The total cost of ordering \(x\) when demand turns out to be \(d\) is \(\;C(x,d) = 2x + 5\max(d-x,0) + \max(x-d,0)\).

1 The flaw of averages

Problem. Solve the deterministic problem at the mean demand, then evaluate that decision in the real random world.

Solution. Mean demand is \(\mathbb E[D] = 0.7(40) + 0.3(100) = 58\). Planning for it, minimise \(C(x,58)\): for \(x \le 58\) the cost is \(2x + 5(58-x) = 290 - 3x\), falling in \(x\); for \(x \ge 58\) it is \(2x + (x-58) = 3x - 58\), rising in \(x\). Both push to \(x_{\text{EV}} = 58\), with an apparent cost of \(2(58) = 116\) — clean and cheap. But \(58\) units are never actually demanded. Living with \(x=58\) in the real world, the expected cost is \(EEV = 0.7\,C(58,40) + 0.3\,C(58,100)\). Here \(C(58,40) = 116 + 18 = 134\) (surplus of 18) and \(C(58,100) = 116 + 5(42) = 326\) (shortage of 42), so \(EEV = 0.7(134) + 0.3(326) = 93.8 + 97.8 = 191.6\). The plan that looked like \(116\) actually costs \(191.6\). That gap — between the cost at the average and the average of the costs — is the flaw of averages, made numerical.

2 The two-stage recourse optimum

Problem. Find the order quantity that minimises expected cost, \(RP = \min_x \mathbb E[C(x,D)]\).

Solution. Because demand is only ever \(40\) or \(100\), the optimum sits at one of those breakpoints; between them, for \(40 \le x \le 100\), \(\mathbb E[C(x,D)] = 2x + 5(0.3)(100-x) + (0.7)(x-40) = 2x + 1.5(100-x) + 0.7(x-40) = 1.2x + 122\), which rises with \(x\) — so the minimum is at the low end, \(x^{\star} = 40\). Evaluate there: \(\mathbb E[C(40,D)] = 2(40) + 5(0.3)(60) + 0 = 80 + 90 = 170\). So \(RP = 170\) at \(x^{\star} = 40\). Notice the stochastic optimum orders for the likely low demand and accepts the occasional expensive shortage — a decision the mean-based plan would never reach, because averaging hid how often demand is actually low. Ordering \(40\) rather than \(58\) is worth real money, quantified next.

3 EVPI and VSS

Problem. Compute the wait-and-see value, then the expected value of perfect information and the value of the stochastic solution.

Solution. With a perfect forecast you would order exactly the demand: order \(40\) if low (cost \(2(40)=80\)) or \(100\) if high (cost \(2(100)=200\)). So \(WS = 0.7(80) + 0.3(200) = 56 + 60 = 116\). Then the expected value of perfect information is \(\text{EVPI} = RP - WS = 170 - 116 = 54\) — the most you should ever pay for a perfect demand forecast. The value of the stochastic solution is \(\text{VSS} = EEV - RP = 191.6 - 170 = 21.6\) — what you save by ordering the stochastic optimum \(40\) instead of the mean-based \(58\). Both are positive, and the benchmark ordering holds: \(WS = 116 \le RP = 170 \le EEV = 191.6\). The two numbers speak to different questions — EVPI values a better forecast, VSS values the model itself — and here both say the uncertainty is well worth taking seriously.

4 A chance constraint sets the buffer

Problem. Demand is \(D \sim \mathcal N(100, 20^2)\). Choose the smallest capacity \(x\) meeting demand with probability at least \(95\%\); then compare \(90\%\) and \(99\%\).

Solution. The chance constraint \(\Pr[D \le x] \ge 1-\alpha\) reduces, for a normal, to \(x \ge \mu + z_{1-\alpha}\,\sigma\). At \(95\%\), \(z_{0.95} = 1.645\), so \(x = 100 + 1.645(20) = 132.9\). The safety margin above the mean is \(1.645(20) = 32.9\) units — the price of the service level. At \(90\%\), \(z = 1.282\) gives \(x = 125.6\); at \(99\%\), \(z = 2.326\) gives \(x = 146.5\). The buffer grows faster than the reliability: going from \(90\%\) to \(99\%\) roughly doubles the safety stock, from \(25.6\) to \(46.5\) units. This convex price of certainty is exactly why "never fail" ("\(100\%\)", i.e. \(z \to \infty\)) is usually neither affordable nor sensible — and why chance constraints, which stop short of it, are the practical tool.

5 Two sweeps of value iteration

Problem. A machine is in state \(s_1\) (working) or \(s_2\) (broken), \(\gamma = 0.9\). In \(s_1\): action run gives \(r=10\) and goes to \(s_1\) w.p. \(0.8\), \(s_2\) w.p. \(0.2\); action maintain gives \(r=4\) and stays in \(s_1\). In \(s_2\): action repair gives \(r=-5\) and returns to \(s_1\). Starting from \(V_0 = 0\), perform two value-iteration sweeps.

Solution. Sweep 1, from \(V_0(s_1)=V_0(s_2)=0\). In \(s_1\): run \(= 10 + 0.9(0.8\cdot0 + 0.2\cdot0) = 10\); maintain \(= 4 + 0.9(0) = 4\). The max is \(V_1(s_1) = 10\) (run). In \(s_2\): repair \(= -5 + 0.9(0) = -5\), so \(V_1(s_2) = -5\). Sweep 2, using \(V_1\). In \(s_1\): run \(= 10 + 0.9(0.8\cdot10 + 0.2\cdot(-5)) = 10 + 0.9(8 - 1) = 10 + 6.3 = 16.3\); maintain \(= 4 + 0.9(10) = 13\). So \(V_2(s_1) = 16.3\) (run still wins). In \(s_2\): repair \(= -5 + 0.9(10) = 4\), so \(V_2(s_2) = 4\). The greedy policy after two sweeps is already run in \(s_1\) and repair in \(s_2\); further sweeps climb toward the fixed point \(V^{\star}(s_1) \approx 77.1\), \(V^{\star}(s_2) \approx 64.4\), but the optimal policy has stopped changing.

6 Policy iteration corrects a lazy policy

Problem. In the same MDP, take the policy "always maintain in \(s_1\), repair in \(s_2\)." Evaluate it, then run one improvement step at \(s_1\).

Solution. Evaluate. Under this policy \(V^{\pi}(s_1) = 4 + 0.9\,V^{\pi}(s_1)\), so \(0.1\,V^{\pi}(s_1) = 4\) and \(V^{\pi}(s_1) = 40\). Then \(V^{\pi}(s_2) = -5 + 0.9(40) = 31\). Improve at \(s_1\). Compare the two actions using these values: maintain gives \(4 + 0.9(40) = 40\) (consistent, as it must be); run gives \(10 + 0.9(0.8\cdot40 + 0.2\cdot31) = 10 + 0.9(32 + 6.2) = 10 + 34.38 = 44.38\). Since \(44.38 > 40\), the greedy action is run, not maintain — the policy improves and \(s_1\) switches to running. This is one turn of policy iteration: evaluate the current rule exactly, then let each state pick the action that looks best against those values. The lazy "always maintain" policy was worth only \(40\) at \(s_1\); the correction pushes it toward the optimal \(77.1\), and a second evaluate–improve round would confirm no further change.

Review

Chapter Summary

Flaw of averages

The optimum for the average scenario is not the best decision on average. Optimize against the distribution, not against the mean.

Two-stage recourse

Decide \(\mathbf{x}\) here-and-now, observe \(\omega\), then recourse. Minimise \(\mathbf{c}^{\mathsf T}\mathbf{x} + \mathbb E[Q(\mathbf{x},\omega)]\) over a scenario tree.

EVPI & VSS

\(\text{EVPI}=RP-WS\) values a perfect forecast; \(\text{VSS}=EEV-RP\) values the model. Always \(WS\le RP\le EEV\).

Chance & robust

Hold a constraint with probability \(1-\alpha\) (a quantile \(\mu+z_{1-\alpha}\sigma\) for normals), or feasibly for every \(\omega\) in an uncertainty set.

MDPs

States, actions, transitions \(P(s'|s,a)\), rewards, discount \(\gamma\). Seek a policy maximising expected discounted reward; horizon \(\approx 1/(1-\gamma)\).

Bellman

\(V^{\star}(s)=\max_a[r(s,a)+\gamma\sum_{s'}P(s'|s,a)V^{\star}(s')]\). Solve by value iteration (geometric) or policy iteration (finite, exact).

Practice

Problems

For the recourse problems, always evaluate a candidate decision across all scenarios before comparing — the average of the costs, not the cost at the average. For the MDP problems, apply the Bellman backup one state at a time, taking the max over actions. Difficulty rises down the list.

  1. Explain the flaw of averages in one or two sentences, and give an everyday example where planning for the mean gives a poor decision.
  2. Define the first-stage and second-stage (recourse) decisions in a two-stage stochastic program, and state what "here-and-now" and "wait-and-see" mean.
  3. A retailer orders \(x\) at \(3\)/unit; demand is \(20\) (p\(=0.6\)) or \(60\) (p\(=0.4\)); shortage costs \(8\)/unit, surplus \(2\)/unit. Find the mean-based order and its true expected cost \(EEV\).
  4. For the data in Problem 3, find the stochastic optimum \(x^{\star}\) and \(RP\), then compute EVPI and VSS and verify \(WS \le RP \le EEV\).
  5. State a chance constraint in words and in symbols, and explain what the tolerance \(\alpha\) controls. Why is a \(100\%\) service level usually impractical?
  6. Demand is \(\mathcal N(500, 80^2)\). Find the capacity meeting it with probability \(90\%\), \(95\%\), and \(99.5\%\), and comment on how the safety margin grows.
  7. Contrast chance-constrained and robust optimization: what does each require as input, and when would you prefer robust optimization despite its conservatism?
  8. Define the six elements of an MDP, and explain the Markov property and why it makes state-based policies sufficient.
  9. Explain how the discount factor \(\gamma\) sets an effective planning horizon. For a constant reward of \(6\) per step, find the total discounted value at \(\gamma = 0.5, 0.9, 0.95\).
  10. Write the Bellman optimality equation and explain why it is a fixed-point equation rather than an explicit formula. How is the optimal policy read from it?
  11. In the maintenance MDP of Examples 5–6 (\(\gamma=0.9\)), perform a third value-iteration sweep from \(V_2(s_1)=16.3\), \(V_2(s_2)=4\), and report the greedy policy.
  12. Compare value iteration and policy iteration on cost per step, number of steps, and what each converges to. When the transition model is unknown, what does the Bellman backup become?