Tuesday , March 4 2025
Home / Lars P. Syll / Solving a simple DP problem (student stuff)

Solving a simple DP problem (student stuff)

Summary:
Solving a simple DP problem (student stuff) .[embedded content] In case you want to solve the problem yourself, here’s a Python script yours truly made (assuming the utility function is of the natural log kind and with some explicit numerical parameterization): import numpy as np import matplotlib.pyplot as plt # Parameters beta = 0.9 # Discount factor r = 0.1 # Depreciation rate T = 3 # Time horizon k0 = 4.0 # Initial capital # Production function: f(k) = k^0.5 def f(k): return k**0.5 # Utility function: u(c) = ln(c), ensuring c > 0 def u(c): return np.log(c) if c > 0 else -np.inf # Discretize capital grid k_vals = np.linspace(0.1, 10, 100) # Initialize value function and policy storage V = [{} for _ in range(T + 1)] policy = [{} for

Topics:
Lars Pålsson Syll considers the following as important:

This could be interesting, too:

Lars Pålsson Syll writes Best advice to an aspiring economist — don’t be an economist!

Lars Pålsson Syll writes Models and evidence in economics

Lars Pålsson Syll writes Keynes and Knight on uncertainty

Dean Baker writes Businesses and DEI: Corporations don’t maximize shareholder value

Solving a simple DP problem (student stuff)

.

In case you want to solve the problem yourself, here’s a Python script yours truly made (assuming the utility function is of the natural log kind and with some explicit numerical parameterization):

import numpy as np
import matplotlib.pyplot as plt

# Parameters
beta = 0.9  # Discount factor
r = 0.1  # Depreciation rate
T = 3  # Time horizon
k0 = 4.0  # Initial capital

# Production function: f(k) = k^0.5
def f(k):
    return k**0.5

# Utility function: u(c) = ln(c), ensuring c > 0
def u(c):
    return np.log(c) if c > 0 else -np.inf

# Discretize capital grid
k_vals = np.linspace(0.1, 10, 100)

# Initialize value function and policy storage
V = [{} for _ in range(T + 1)]
policy = [{} for _ in range(T)]

# Terminal condition at T
for k in k_vals:
    cT = f(k) + (1 - r) * k  # Consume all resources at the last period
    V[T][k] = u(cT) if cT > 0 else -np.inf  # Ensure feasibility

# Backward induction
for t in range(T - 1, -1, -1):  # t = 2, 1, 0
    for k in k_vals:
        best_c = 0
        best_v = -np.inf
        c_vals = np.linspace(0.1, f(k) + (1 - r) * k, 100)  # Consumption choices
        
        for c in c_vals:
            k_next = f(k) + (1 - r) * k - c  # Capital transition
            if k_next < 0:
                continue  # Skip infeasible values

            # Interpolate next period's value function
            k_next_values = np.array(list(V[t + 1].keys()))
            V_next_values = np.array(list(V[t + 1].values()))
            V_next = np.interp(k_next, k_next_values, V_next_values)  

            value = u(c) + beta * V_next  # Bellman equation
            if value > best_v:
                best_v = value
                best_c = c

        V[t][k] = best_v
        policy[t][k] = best_c

# Compute optimal paths starting from k0
opt_c = []
opt_k = [k0]

for t in range(T):
    k_current = opt_k[-1]

    # Interpolate the optimal policy for k_current
    k_policy_values = np.array(list(policy[t].keys()))
    c_policy_values = np.array(list(policy[t].values()))
    c_t = np.interp(k_current, k_policy_values, c_policy_values)

    opt_c.append(c_t)
    opt_k.append(f(k_current) + (1 - r) * k_current - c_t)

# Print results
print("Optimal Consumption Path:", opt_c)
print("Optimal Capital Path:", opt_k)

# Plot results
plt.figure(figsize=(10, 5))
plt.plot(range(T+1), opt_k, marker='o', label='Capital')
plt.plot(range(T), opt_c, marker='s', label='Consumption')
plt.xlabel("Time")
plt.ylabel("Value")
plt.legend()
plt.title("Optimal Capital and Consumption Paths")
plt.grid()
plt.show()

Lars Pålsson Syll
Professor at Malmö University. Primary research interest - the philosophy, history and methodology of economics.

Leave a Reply

Your email address will not be published. Required fields are marked *