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 stages (time periods). Following the convention, we label these stages as . At any stage , the state transition can be expressed as , where is the state of system at stage ;11 is the decision selected at stage ; is a random parameter (also called disturbance).
122Basically the state of next stage is determined as a function of the current state , current decision (the choice we make at stage from the available options) and the random variable (the probability distribution of often depends on and ).
2. A cost (or profit) function that is additive over time.
Except for the last stage , which has a cost/profit depending only on , the costs at all other stages can depend on , , and . So the total cost/profit is .
The goal of optimization is to select strategies/policies for the decision sequences that minimize expected cost (or maximize expected profit):
Dynamic programming (DP) algorithm
The dynamic programming algorithm relies on an idea called the Principle of Optimality: If is the optimal policy for the original dynamic programming problem, then the tail policy must be optimal for the tail subproblem .
DP algorithm: To solve the basic problem , start with , and go backwards minimizing cost-to-go function :
Then the 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.
123Dice game
You can roll a 6-side dice up to 3 times. After the first or the second roll, if you get a number , you can decide either to get 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 dollars if the third number is 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 .
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 .
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, $.
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 represents the state that the Red Sox has won games and the Rockies has won games, and let be our net payoff, which can be negative when we lose money, at state . 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 with payoff and with payoff . 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 , if we bet $ on the Red Sox for the next game, we will have if the Red Sox wins and the state goes to , or if the Red Sox loses and the state goes to . So clearly we have
For example, we have . Let’s set up a table with the columns representing and the rows representing . Now we have all the information to fill in , as well as . Similarly we can also fill in all for the states where or as shown in Figure 5.7. Going further backward, we can fill in the net payoffs at every possible state. Using equation , 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 , and , 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.
125Dynamic 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 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
We have another roll if the expected payoff , which means that we should keep rolling if the money is no more than $14. Considering that we will stop rolling when , the maximum payoff of the game is $19 (the dice rolls a 5 after reaching the state ). We then have the following: and . When , we will keep on rolling, so . Using this equation, we can calculate the value for recursively for all . The results are summarized in Table 5.2. Since , we are willing to pay at most $6.15 for the game.
| 19 | 18 | 17 | 16 | 15 | 14 | 13 | 12 | 11 | 10 | |
| 19.00 | 18.00 | 17.00 | 16.00 | 15.00 | 14.17 | 13.36 | 12.59 | 11.85 | 11.16 | |
| 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | |
| 10.52 | 9.91 | 9.34 | 8.80 | 8.29 | 7.81 | 7.36 | 6.93 | 6.53 | 6.15 |
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 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 =
At each , we face the decision whether to stop or keep on playing. If we ask the dealer to stop at , the payoff is . If we keep on going, there is probability that the next card will be black—in which case the state changes to —and probability that the next card will be red—in which case the state changes to . We will stop if and only if the expected payoff of drawing more cards is less than . That also gives us the system equation:
As shown in Figure 5.9 (next page), using the boundary conditions , and the system equation for , we can recursively calculate for all pairs of and .
The expected payoff at the beginning of the game is .
128Number of Red Cards Left | Number of Black Cards Left | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | |
| 0 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 |
| 1 | 0 | 0.50 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 |
| 2 | 0 | 0.33 | 0.67 | 1.20 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 |
| 3 | 0 | 0.25 | 0.50 | 0.85 | 1.34 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 |
| 4 | 0 | 0.20 | 0.40 | 0.66 | 1.00 | 1.44 | 2.07 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 |
| 5 | 0 | 0.17 | 0.33 | 0.54 | 0.79 | 1.12 | 1.55 | 2.15 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 |
| 6 | 0 | 0.14 | 0.29 | 0.45 | 0.66 | 0.91 | 1.23 | 1.66 | 2.23 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 |
| 7 | 0 | 0.13 | 0.25 | 0.39 | 0.56 | 0.76 | 1.01 | 1.34 | 1.75 | 2.30 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 |
| 8 | 0 | 0.11 | 0.22 | 0.35 | 0.49 | 0.66 | 0.86 | 1.11 | 1.43 | 1.84 | 2.36 | 3.05 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 |
| 9 | 0 | 0.10 | 0.20 | 0.31 | 0.43 | 0.58 | 0.75 | 0.95 | 1.21 | 1.52 | 1.92 | 2.43 | 3.10 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 |
| 10 | 0 | 0.09 | 0.18 | 0.28 | 0.39 | 0.52 | 0.66 | 0.83 | 1.04 | 1.30 | 1.61 | 2.00 | 2.50 | 3.15 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 |
| 11 | 0 | 0.08 | 0.17 | 0.26 | 0.35 | 0.46 | 0.59 | 0.74 | 0.91 | 1.12 | 1.38 | 1.69 | 2.08 | 2.57 | 3.20 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |
| 12 | 0 | 0.08 | 0.15 | 0.24 | 0.32 | 0.42 | 0.54 | 0.66 | 0.81 | 0.99 | 1.20 | 1.46 | 1.77 | 2.15 | 2.63 | 3.24 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 |
| 13 | 0 | 0.07 | 0.14 | 0.22 | 0.30 | 0.39 | 0.49 | 0.60 | 0.73 | 0.89 | 1.06 | 1.28 | 1.53 | 1.84 | 2.22 | 2.70 | 3.28 | 4.03 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 |
| 14 | 0 | 0.07 | 0.13 | 0.20 | 0.28 | 0.36 | 0.45 | 0.55 | 0.67 | 0.80 | 0.95 | 1.13 | 1.35 | 1.60 | 1.91 | 2.29 | 2.75 | 3.33 | 4.06 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
| 15 | 0 | 0.06 | 0.13 | 0.19 | 0.26 | 0.33 | 0.42 | 0.51 | 0.61 | 0.73 | 0.86 | 1.02 | 1.20 | 1.42 | 1.67 | 1.98 | 2.36 | 2.81 | 3.38 | 4.09 | 5 | 6 | 7 | 8 | 9 | 10 | 11 |
| 16 | 0 | 0.06 | 0.12 | 0.18 | 0.24 | 0.31 | 0.39 | 0.47 | 0.57 | 0.67 | 0.79 | 0.93 | 1.08 | 1.27 | 1.48 | 1.74 | 2.05 | 2.42 | 2.87 | 3.43 | 4.13 | 5 | 6 | 7 | 8 | 9 | 10 |
| 17 | 0 | 0.06 | 0.11 | 0.17 | 0.23 | 0.29 | 0.36 | 0.44 | 0.53 | 0.62 | 0.73 | 0.85 | 0.99 | 1.15 | 1.33 | 1.55 | 1.81 | 2.11 | 2.48 | 2.93 | 3.48 | 4.16 | 5 | 6 | 7 | 8 | 9 |
| 18 | 0 | 0.05 | 0.11 | 0.16 | 0.22 | 0.28 | 0.34 | 0.41 | 0.49 | 0.58 | 0.67 | 0.78 | 0.90 | 1.04 | 1.21 | 1.39 | 1.61 | 1.87 | 2.17 | 2.54 | 2.99 | 3.53 | 4.19 | 5 | 6 | 7 | 8 |
| 19 | 0 | 0.05 | 0.10 | 0.15 | 0.20 | 0.26 | 0.32 | 0.39 | 0.46 | 0.54 | 0.63 | 0.73 | 0.84 | 0.96 | 1.10 | 1.26 | 1.45 | 1.67 | 1.93 | 2.24 | 2.60 | 3.04 | 3.57 | 4.22 | 5.01 | 6 | 7 |
| 20 | 0 | 0.05 | 0.10 | 0.14 | 0.19 | 0.25 | 0.31 | 0.37 | 0.43 | 0.51 | 0.59 | 0.68 | 0.78 | 0.89 | 1.01 | 1.16 | 1.32 | 1.51 | 1.73 | 1.99 | 2.30 | 2.66 | 3.09 | 3.62 | 4.25 | 5.03 | 6 |
| 21 | 0 | 0.05 | 0.09 | 0.14 | 0.19 | 0.24 | 0.29 | 0.35 | 0.41 | 0.48 | 0.55 | 0.63 | 0.72 | 0.83 | 0.94 | 1.07 | 1.21 | 1.38 | 1.57 | 1.79 | 2.05 | 2.35 | 2.72 | 3.15 | 3.66 | 4.28 | 5.05 |
| 22 | 0 | 0.04 | 0.09 | 0.13 | 0.18 | 0.23 | 0.28 | 0.33 | 0.39 | 0.45 | 0.52 | 0.60 | 0.68 | 0.77 | 0.87 | 0.99 | 1.12 | 1.26 | 1.43 | 1.62 | 1.85 | 2.11 | 2.41 | 2.77 | 3.20 | 3.71 | 4.32 |
| 23 | 0 | 0.04 | 0.08 | 0.13 | 0.17 | 0.22 | 0.26 | 0.32 | 0.37 | 0.43 | 0.49 | 0.56 | 0.64 | 0.72 | 0.82 | 0.92 | 1.04 | 1.17 | 1.32 | 1.48 | 1.68 | 1.90 | 2.16 | 2.47 | 2.82 | 3.25 | 3.75 |
| 24 | 0 | 0.04 | 0.08 | 0.12 | 0.16 | 0.21 | 0.25 | 0.30 | 0.35 | 0.41 | 0.47 | 0.53 | 0.60 | 0.68 | 0.77 | 0.86 | 0.97 | 1.08 | 1.22 | 1.37 | 1.54 | 1.73 | 1.96 | 2.22 | 2.52 | 2.88 | 3.30 |
| 25 | 0 | 0.04 | 0.08 | 0.12 | 0.16 | 0.20 | 0.24 | 0.29 | 0.34 | 0.39 | 0.45 | 0.51 | 0.57 | 0.64 | 0.72 | 0.81 | 0.90 | 1.01 | 1.13 | 1.26 | 1.42 | 1.59 | 1.78 | 2.01 | 2.27 | 2.57 | 2.93 |
| 26 | 0 | 0.04 | 0.07 | 0.11 | 0.15 | 0.19 | 0.23 | 0.28 | 0.32 | 0.37 | 0.43 | 0.48 | 0.54 | 0.61 | 0.68 | 0.76 | 0.85 | 0.95 | 1.06 | 1.18 | 1.31 | 1.46 | 1.64 | 1.83 | 2.06 | 2.32 | 2.62 |