<p>I'm trying to implement a solution to a dynamic programmig exercise (cake eating) in Python.</p>
<p>Consider the following problem. An individual lives for 20 periods. In the first 15 periods he receives an endowment of a non perishable good, say a cake, equal <span class="math-container">$y(t) = 1 + 0.1 \cdot t$</span>. In the remaining 5 periods he receives nothing. The good is not perishable and does not produce fruits, so that it can only be either consumed or stored for consumption in the following periods. The consumption of the – obviously non-negative – amount <span class="math-container">$c$</span> gives utility <span class="math-container">$c^{1/3}$</span>. Future utility is discounted by the factor <span class="math-container">$\beta = 0.98$</span> yearly.</p>
<p>My result seems to be wrong. Here is the code:</p>
<pre><code>import numpy as np
import time
import matplotlib.pyplot as plt
import seaborn as sns

beta = 0.98
nper = 20
T = 20

t = np.arange(1, T + 1)
y_vec = np.where(t &lt;= 15, 1 + 0.1 * t, 0)

a_bounds = [0.0, 0]
n_p = 1000
a_grid = np.linspace(a_bounds[0], a_bounds[1] + np.sum(y_vec), n_p)

V_fun = np.zeros((nper, n_p))
V_new = np.zeros(n_p)
Cons = np.zeros((nper, n_p))
Anext = np.zeros((nper, n_p))
u_temp = np.zeros(n_p)

i_time = time.time()
for iter in range(nper):
    gridt = a_grid + y_vec[iter]
    Cons[iter, 0] = 0.0
    V_new[0] = 0.0
    
    for i in range(1, n_p):
        for j in range(0, i + 1):
            xt1 = a_grid[j]
            xt = gridt[i]
            if xt1 &lt;= xt:
                cons_u = xt - xt1
                u_temp[j] = cons_u**(1/3) + beta * V_fun[iter, j]
        ic = np.argmax(u_temp[0:i+1])
        Cons[iter, i] = a_grid[i] - a_grid[ic]
        Anext[iter, i] = a_grid[ic]
        V_new[i] = u_temp[ic]

    V_fun[iter] = V_new

</code></pre>
<p>I think there is a problem in how i defined the grid but i'm not sure.</p>
<p>I expect that the individual smooth consumption over time because the income is certain at time 0. So the consumption should not be zero from t = 15 to t = 20 .</p>

CC BY-SA 4.0
2024-04-20T10:18:38.393Z
Hi! Could you please explain what it is that you are trying to do? :) What do you think this program should achieve? Solve a dynamic programming exercise?
Giskard
CC BY-SA 4.0
2024-04-20T10:22:02.397Z
Just a few comments until you clarify: 'np' is undefined, and 'gridt' and the following lines should be indented if they are inside the for cycle. The code as it is above simply does not run.
Giskard
CC BY-SA 4.0
2024-04-20T10:33:57.130Z
Yeah I'm trying to solve a dynamic programmig exercise. It is the following: Consider the following problem. An individual lives for 20 periods. In the first 15 periods he
receives an endowment of a non perishable good, say a cake, equal y(t) = 1 + 0.1·t. In the
remaining 5 periods he receives nothing. The good is not perishable and does not produce
fruits, so that it can only be either consumed or stored for consumption in the following
periods. The consumption of the – obviously non-negative – amount c gives utility c^(1/3).
Future utility is discounted by the factor β = 0.98 yearly.
Calogero Sortino
CC BY-SA 4.0
2024-04-20T10:34:19.990Z
For the code, I messed up with the writing here. Now it should be fine
Calogero Sortino
<macroeconomics><dynamic-programming><python>
Cake eating problem with income over time
Calogero Sortino
# Cake eating problem with income over time

(Asked by: username_0 on 2024-04-20)

I'm trying to implement a solution to a dynamic programmig exercise (cake eating) in Python.

Consider the following problem. An individual lives for 20 periods. In the first 15 periods he receives an endowment of a non perishable good, say a cake, equal $y(t) = 1 + 0.1 \cdot t$. In the remaining 5 periods he receives nothing. The good is not perishable and does not produce fruits, so that it can only be either consumed or stored for consumption in the following periods. The consumption of the – obviously non-negative – amount $c$ gives utility $c^{1/3}$. Future utility is discounted by the factor $\beta = 0.98$ yearly.

My result seems to be wrong. Here is the code:

```
import numpy as np
import time
import matplotlib.pyplot as plt
import seaborn as sns

beta = 0.98
nper = 20
T = 20

t = np.arange(1, T + 1)
y_vec = np.where(t <= 15, 1 + 0.1 * t, 0)

a_bounds = [0.0, 0]
n_p = 1000
a_grid = np.linspace(a_bounds[0], a_bounds[1] + np.sum(y_vec), n_p)

V_fun = np.zeros((nper, n_p))
V_new = np.zeros(n_p)
Cons = np.zeros((nper, n_p))
Anext = np.zeros((nper, n_p))
u_temp = np.zeros(n_p)

i_time = time.time()
for iter in range(nper):
    gridt = a_grid + y_vec[iter]
    Cons[iter, 0] = 0.0
    V_new[0] = 0.0
    
    for i in range(1, n_p):
        for j in range(0, i + 1):
            xt1 = a_grid[j]
            xt = gridt[i]
            if xt1 <= xt:
                cons_u = xt - xt1
                u_temp[j] = cons_u**(1/3) + beta * V_fun[iter, j]
        ic = np.argmax(u_temp[0:i+1])
        Cons[iter, i] = a_grid[i] - a_grid[ic]
        Anext[iter, i] = a_grid[ic]
        V_new[i] = u_temp[ic]

    V_fun[iter] = V_new

```

I think there is a problem in how i defined the grid but i'm not sure.

I expect that the individual smooth consumption over time because the income is certain at time 0. So the consumption should not be zero from t = 15 to t = 20 .

**Tags:** macroeconomics, dynamic-programming, python

> **Comments:**
> **username_1**: Hi! Could you please explain what it is that you are trying to do? :) What do you think this program should achieve? Solve a dynamic programming exercise?  
> **username_1**: Just a few comments until you clarify: 'np' is undefined, and 'gridt' and the following lines should be indented if they are inside the for cycle. The code as it is above simply does not run.  
> **username_0**: Yeah I'm trying to solve a dynamic programmig exercise. It is the following: Consider the following problem. An individual lives for 20 periods. In the first 15 periods he receives an endowment of a non perishable good, say a cake, equal y(t) = 1 + 0.1·t. In the remaining 5 periods he receives nothing. The good is not perishable and does not produce fruits, so that it can only be either consumed or stored for consumption in the following periods. The consumption of the – obviously non-negative – amount c gives utility c^(1/3). Future utility is discounted by the factor β = 0.98 yearly.  
> **username_0**: For the code, I messed up with the writing here. Now it should be fine  

---

## 0 answers

