A Practical Guide to Quantitative Finance Interviews
Contents

5.3 Dynamic Programming

Dynamic Programming refers to a collection of general methods developed to solve sequential, or multi-stage, decision problems.10 It is an extremely versatile tool with applications in fields such as finance, supply chain management and airline scheduling. Although theoretically simple, mastering dynamic programming algorithms requires extensive mathematical prerequisites and rigorous logic. As a result, it is often perceived to be one of the most difficult graduate level courses.

Fortunately, the dynamic programming problems you are likely to encounter in interviews—although you often may not recognize them as such—are rudimentary problems. So in this section we will focus on the basic logic used in dynamic programming and apply it to several interview problems. Hopefully the solutions to these examples will convey the gist and the power of dynamic programming.

A discrete-time dynamic programming model includes two inherent components:

1. The underlying discrete-time dynamic system

A dynamic programming problem can always be divided into stages with a decision required at each stage. Each stage has a number of states associated with it. The decision at one stage transforms the current state into a state in the next stage (at some stages and states, the decision may be trivial if there is only one choice).

Assume that the problem has N+1N+1 stages (time periods). Following the convention, we label these stages as 0,1,⋯ ,N−1,N0,1,\cdots,N-1,N. At any stage k, 0≤k≤N−1k,\ 0\le k\le N-1, the state transition can be expressed as xk+1=f(xk,uk,wk)x_{k+1}=f(x_k,u_k,w_k), where xkx_k is the state of system at stage kk;11 uku_k is the decision selected at stage kk; wkw_k is a random parameter (also called disturbance).

122

Basically the state of next stage xk+1x_{k+1} is determined as a function of the current state xkx_k, current decision uku_k (the choice we make at stage kk from the available options) and the random variable wkw_k (the probability distribution of wkw_k often depends on xkx_k and uku_k).

2. A cost (or profit) function that is additive over time.

Except for the last stage (N)(N), which has a cost/profit gN(xN)g_N(x_N) depending only on xNx_N, the costs at all other stages gk(xk,uk,wk)g_k(x_k,u_k,w_k) can depend on xkx_k, uku_k, and wkw_k. So the total cost/profit is gN(xN)+∑k=iN−1gk(xk,uk,wk)}\displaystyle g_N(x_N)+\sum_{k=i}^{N-1}g_k(x_k,u_k,w_k)\}.

The goal of optimization is to select strategies/policies for the decision sequences π∗={u0∗,⋯ ,uN−1∗}\pi^*=\{u_0^*,\cdots,u_{N-1}^*\} that minimize expected cost (or maximize expected profit):

Jπ∗(x0)=min⁡πE{gN(xN)+∑k=0N−1gk(xk,uk,wk)}.J_{\pi^*}(x_0)=\min_\pi E\left\{g_N(x_N)+\sum_{k=0}^{N-1}g_k(x_k,u_k,w_k)\right\}.

Dynamic programming (DP) algorithm

The dynamic programming algorithm relies on an idea called the Principle of Optimality: If π∗={u0∗,⋯ ,uN−1∗}\pi^*=\{u_0^*,\cdots,u_{N-1}^*\} is the optimal policy for the original dynamic programming problem, then the tail policy πi∗={ui∗,⋯ ,uN−1∗}\pi_i^*=\{u_i^*,\cdots,u_{N-1}^*\} must be optimal for the tail subproblem E{gN(xN)+∑k=iN−1gk(xk,uk,wk)}\displaystyle E\left\{g_N(x_N)+\sum_{k=i}^{N-1}g_k(x_k,u_k,w_k)\right\}.

DP algorithm: To solve the basic problem Jπ∗(x0)=min⁡πE{gN(xN)+∑k=0N−1gk(xk,uk,wk)}\displaystyle J_{\pi^*}(x_0)=\min_\pi E\left\{g_N(x_N)+\sum_{k=0}^{N-1}g_k(x_k,u_k,w_k)\right\}, start with JN(xN)=gN(xN)J_N(x_N)=g_N(x_N), and go backwards minimizing cost-to-go function Jk(xk)J_k(x_k):

Jk(xk)=min⁡uk∈Uk(xk)Ewk{gk(xk,uk,wk)+Jk+1(f(xk,uk,wk))},k=0,⋯ ,N−1.J_k(x_k)=\min_{u_k\in U_k(x_k)}E_{w_k}\{g_k(x_k,u_k,w_k)+J_{k+1}(f(x_k,u_k,w_k))\},\quad k=0,\cdots,N-1.

Then the J0(x0)J_0(x_0) generated from this algorithm is the expected optimal cost.

Although the algorithm looks complicated, the intuition is straightforward. For dynamic programming problems, we should start with optimal policy for every possible state of the final stage (which has the highest amount of information and least amount of uncertainty) first and then work backward towards earlier stages by applying the tail policies and cost-to-go functions until you reach the initial stage.

Now let’s use several examples to show how the DP algorithm is applied.

123

Dice game

You can roll a 6-side dice up to 3 times. After the first or the second roll, if you get a number xx, you can decide either to get xx dollars or to choose to continue rolling. But once you decide to continue, you forgo the number you just rolled. If you get to the third roll, you’ll just get xx dollars if the third number is xx and the game stops. What is the game worth and what is your strategy?

Solution: This is a simple dynamic programming strategy game. As all dynamic programming questions, the key is to start with the final stage and work backwards. For this question, it is the stage where you have forgone the first two rolls. It becomes a simple dice game with one roll. Face values 1, 2, 3, 4, 5, and 6 each have a 1/6 probability and your expected payoff is $3.5.

Now let’s go back one step. Imagine that you are at the point after the second roll, for which you can choose either to have a third roll with an expected payoff of $3.5 or keep the current face value. Surely you will keep the face value if it is larger than 3.5; in other words, when you get 4, 5 or 6, you stop rolling. When you get 1, 2 or 3, you keep rolling. So your expected payoff before the second roll is 3/6×3.5+1/6×(4+5+6)=$4.253/6\times3.5+1/6\times(4+5+6)=\$4.25.

Now let’s go back one step further. Imagine that you are at the point after the first roll, for which you can choose either to have a second roll with expected payoff $4.25 (when face value is 1, 2, 3 or 4) or keep the current face value. Surely you will keep the face value if it is larger than 4.25; In other words, when you get 5 or 6, you stop rolling. So your expected payoff before the first roll is 4/6×4.25+1/6×(5+6)=$14/34/6\times4.25+1/6\times(5+6)=\$14/3.

This backward approach—called tail policy in dynamic programming—gives us the strategy and also the expected value of the game at the initial stage, $14/314/3.

World series

The Boston Red Sox and the Colorado Rockies are playing in the World Series finals. In case you are not familiar with the World Series, there are a maximum of 7 games and the first team that wins 4 games claims the championship. You have $100 dollars to place a double-or-nothing bet on the Red Sox.

Unfortunately, you can only bet on each individual game, not the series as a whole. How much should you bet on each game so that if the Red Sox wins the whole series, you win exactly $100, and if Red Sox loses, you lose exactly $100?

Solution: Let (i,j)(i,j) represents the state that the Red Sox has won ii games and the Rockies has won jj games, and let f(i,j)f(i,j) be our net payoff, which can be negative when we lose money, at state (i,j)(i,j). From the rules of the game, we know that there may be between 4 and 7 games in total. We need to decide on a strategy so that whenever the 124 series is over, our final net payoff is either +100—when Red Sox wins the championship—or -100—when Red Sox loses. In other words, the state space of the final stage includes {(4,0),(4,1),(4,2),(4,3)}\{(4,0),(4,1),(4,2),(4,3)\} with payoff f(i,j)=100f(i,j)=100 and {(0,4),(1,4),(2,4),(3,4)}\{(0,4),(1,4),(2,4),(3,4)\} with payoff f(i,j)=−100f(i,j)=-100. As all dynamic programming questions, the key is to start with the final stage and work backwards—even though in this case the number of stages is not fixed. For each state (i,j)(i,j), if we bet $yy on the Red Sox for the next game, we will have (f(i,j)+y)(f(i,j)+y) if the Red Sox wins and the state goes to (i+1,j)(i+1,j), or (f(i,j)−y)(f(i,j)-y) if the Red Sox loses and the state goes to (i,j+1)(i,j+1). So clearly we have

f(i+1,j)=f(i,j)+yf(i,j+1)=f(i,j)−y}⇒{f(i,j)=(f(i+1,j)+f(i,j+1))/2y=(f(i+1,j)−f(i,j+1))/2.\left.\begin{aligned}f(i+1,j)&=f(i,j)+y\\f(i,j+1)&=f(i,j)-y\end{aligned}\right\}\Rightarrow\begin{cases}f(i,j)=(f(i+1,j)+f(i,j+1))/2\\y=(f(i+1,j)-f(i,j+1))/2.\end{cases}

For example, we have f(3,3)=f(4,3)+f(3,4)2=100−1002=0\displaystyle f(3,3)=\frac{f(4,3)+f(3,4)}{2}=\frac{100-100}{2}=0. Let’s set up a table with the columns representing ii and the rows representing jj. Now we have all the information to fill in f(4,0),f(4,0), f(4,1),f(4,1), f(4,3),f(4,3), f(4,2),f(4,2), f(0,4),f(0,4), f(1,4),f(1,4), f(2,4),f(2,4), f(3,4)f(3,4), as well as f(3,3)f(3,3). Similarly we can also fill in all f(i,j)f(i,j) for the states where i=3i=3 or j=3j=3 as shown in Figure 5.7. Going further backward, we can fill in the net payoffs at every possible state. Using equation y=(f(i+1,j)−f(i,j+1))/2y=(f(i+1,j)-f(i,j+1))/2, we can also calculate the bet we need to place at each state, which is essentially our strategy.

If you are not accustomed to the table format, Figure 5.8 redraws it as a binomial tree, a format you should be familiar with. If you consider that the boundary conditions are f(4,0),f(4,0), f(4,1),f(4,1), f(4,3),f(4,3), f(4,2),f(4,2), f(0,4),f(0,4), f(1,4),f(1,4), f(2,4)f(2,4), and f(3,4)f(3,4), the underlying asset either increases by 1 or decrease by 1 after each step, and there is no interest, then the problem becomes a simple binomial tree problem and the bet we place each time is the delta in dynamic hedging. In fact, both European options and American options can be solved numerically using dynamic programming approaches.

125
Four grids show the backward calculation of World Series payoffs, then the corresponding bets. Columns show Red Sox wins and rows show Colorado Rockies wins.
Figure 5.7 Payoffs and bets at different states
Binomial tree of World Series payoffs, beginning at zero in state(0,0), ending at plus100 when the Red Sox wins four games and minus100 when the Rockies wins four.
Figure 5.8 Payoff at different states expressed in a binomial tree
126

Dynamic dice game

A casino comes up with a fancy dice game. It allows you to roll a dice as many times as you want unless a 6 appears. After each roll, if 1 appears, you will win $1; if 2 appears, you will win $2; …; if 5 appears, you win $5; but if 6 appears all the moneys you have won in the game is lost and the game stops. After each roll, if the dice number is 1-5, you can decide whether to keep the money or keep on rolling. How much are you willing to pay to play the game (if you are risk neutral)?12

Solution: Assuming that we have accumulated nn dollars, the decision to have another roll or not depends on the expected profit versus expected loss. If we decide to have an extra roll, our expected payoff will become

16(n+1)+16(n+2)+16(n+3)+16(n+4)+16(n+5)+16×0=56n+2.5.\frac16(n+1)+\frac16(n+2)+\frac16(n+3)+\frac16(n+4)+\frac16(n+5)+\frac16\times0=\frac56n+2.5.

We have another roll if the expected payoff 56n+2.5>n\frac56n+2.5>n, which means that we should keep rolling if the money is no more than $14. Considering that we will stop rolling when n≥15n\ge15, the maximum payoff of the game is $19 (the dice rolls a 5 after reaching the state n=14n=14). We then have the following: f(19)=19,f(19)=19, f(18)=18,f(18)=18, f(17)=17,f(17)=17, f(16)=16,f(16)=16, and f(15)=15f(15)=15. When n≤14n\le14, we will keep on rolling, so E[f(n)∣n≤14]=16∑i=15E[f(n+i)]\displaystyle E[f(n)\mid n\le14]=\frac16\sum_{i=1}^5E[f(n+i)]. Using this equation, we can calculate the value for E[f(n)]E[f(n)] recursively for all n=14,13,⋯ ,0n=14,13,\cdots,0. The results are summarized in Table 5.2. Since E[f(0)]=6.15E[f(0)]=6.15, we are willing to pay at most $6.15 for the game.

Table 5.2 Expected payoff of the game when the player has accumulated nn dollars
nn19181716151413121110
E[f(n)]E[f(n)]19.0018.0017.0016.0015.0014.1713.3612.5911.8511.16
nn9876543210
E[f(n)]E[f(n)]10.529.919.348.808.297.817.366.936.536.15
127

Dynamic card game

A casino offers yet another card game with the standard 52 cards (26 red, 26 black). The cards are thoroughly shuffled and the dealer draws cards one by one. (Drawn cards are not returned to the deck.) You can ask the dealer to stop at any time you like. For each red card drawn, you win $1; for each black card drawn, you lose $1. What is the optimal stopping rule in terms of maximizing expected payoff and how much are you willing to pay for this game?

Solution: It is another problem perceived to be difficult by many interviewees. Yet it is a simple dynamic programming problem. Let (b,r)(b,r) represent the number of black and red cards left in the deck, respectively. By symmetry, we have

red cards drawn − black cards drawn = black cards left − red cards left = b−rb-r

At each (b,r)(b,r), we face the decision whether to stop or keep on playing. If we ask the dealer to stop at (b,r)(b,r), the payoff is b−rb-r. If we keep on going, there is bb+r\frac{b}{b+r} probability that the next card will be black—in which case the state changes to (b−1,r)(b-1,r)—and rb+r\frac{r}{b+r} probability that the next card will be red—in which case the state changes to (b,r−1)(b,r-1). We will stop if and only if the expected payoff of drawing more cards is less than b−rb-r. That also gives us the system equation:

E[f(b,r)]=max⁡(b−r,bb+rE[f(b−1,r)]+rb+r[f(b,r−1)]).E[f(b,r)]=\max\left(b-r,\frac{b}{b+r}E[f(b-1,r)]+\frac{r}{b+r}[f(b,r-1)]\right). 13

As shown in Figure 5.9 (next page), using the boundary conditions f(0,r)=0, f(b,0)=b, ∀b,r=0,1,⋯ ,26f(0,r)=0,\ f(b,0)=b,\ \forall b,r=0,1,\cdots,26, and the system equation for E[f(b,r)]E[f(b,r)], we can recursively calculate E[f(b,r)]E[f(b,r)] for all pairs of bb and rr.

The expected payoff at the beginning of the game is E[f(26,26)]=$2.62E[f(26,26)]=\$2.62.

128
Figure 5.9 Expected payoffs at different states (b,r)(b,r)
f(b,r)f(b,r)
Number of Red Cards Left
Number of Black Cards Left
01234567891011121314151617181920212223242526
001234567891011121314151617181920212223242526
100.5012345678910111213141516171819202122232425
200.330.671.2023456789101112131415161718192021222324
300.250.500.851.34234567891011121314151617181920212223
400.200.400.661.001.442.07345678910111213141516171819202122
500.170.330.540.791.121.552.153456789101112131415161718192021
600.140.290.450.660.911.231.662.2334567891011121314151617181920
700.130.250.390.560.761.011.341.752.30345678910111213141516171819
800.110.220.350.490.660.861.111.431.842.363.05456789101112131415161718
900.100.200.310.430.580.750.951.211.521.922.433.104567891011121314151617
1000.090.180.280.390.520.660.831.041.301.612.002.503.1545678910111213141516
1100.080.170.260.350.460.590.740.911.121.381.692.082.573.20456789101112131415
1200.080.150.240.320.420.540.660.810.991.201.461.772.152.633.244567891011121314
1300.070.140.220.300.390.490.600.730.891.061.281.531.842.222.703.284.035678910111213
1400.070.130.200.280.360.450.550.670.800.951.131.351.601.912.292.753.334.0656789101112
1500.060.130.190.260.330.420.510.610.730.861.021.201.421.671.982.362.813.384.09567891011
1600.060.120.180.240.310.390.470.570.670.790.931.081.271.481.742.052.422.873.434.135678910
1700.060.110.170.230.290.360.440.530.620.730.850.991.151.331.551.812.112.482.933.484.1656789
1800.050.110.160.220.280.340.410.490.580.670.780.901.041.211.391.611.872.172.542.993.534.195678
1900.050.100.150.200.260.320.390.460.540.630.730.840.961.101.261.451.671.932.242.603.043.574.225.0167
2000.050.100.140.190.250.310.370.430.510.590.680.780.891.011.161.321.511.731.992.302.663.093.624.255.036
2100.050.090.140.190.240.290.350.410.480.550.630.720.830.941.071.211.381.571.792.052.352.723.153.664.285.05
2200.040.090.130.180.230.280.330.390.450.520.600.680.770.870.991.121.261.431.621.852.112.412.773.203.714.32
2300.040.080.130.170.220.260.320.370.430.490.560.640.720.820.921.041.171.321.481.681.902.162.472.823.253.75
2400.040.080.120.160.210.250.300.350.410.470.530.600.680.770.860.971.081.221.371.541.731.962.222.522.883.30
2500.040.080.120.160.200.240.290.340.390.450.510.570.640.720.810.901.011.131.261.421.591.782.012.272.572.93
2600.040.070.110.150.190.230.280.320.370.430.480.540.610.680.760.850.951.061.181.311.461.641.832.062.322.62