forked from ranielin/Rust-1987-Replication
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompute_EV.py
More file actions
90 lines (69 loc) · 2.29 KB
/
compute_EV.py
File metadata and controls
90 lines (69 loc) · 2.29 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import numpy as np
def compute_EV(x, P, theta, beta, tol):
"""
solve the single-agent DP problem, computing the expected value (EV)
function for given values of model parameters by finding the fixed point
of the Bellman operator contraction
inputs:
x, state space vector
P, S x S transition matrix
theta, vector of parameters associated with the utility/cost
functions
beta, discount factor
tol, tolerance at which to stop the iteration
output:
EV, length S vector encoding the expected value function for each
state in x at the given parameters theta
"""
def B(EV):
"""
Bellman operator to iterate on
input:
EV, length S vector encoding the expected value function
output:
B, length S vector encoding the value B(EV)
"""
# utility and value from continuing (without the error term)
u_0 = u(x, 0, theta)
v_0 = u_0 + beta * EV
# utility and value from replacing (without the error term)
u_1 = u(x, 1, theta)
v_1 = u_1 + beta * EV[0]
# subtract and re-add EV to avoid overflow issues
G = np.exp(v_0 - EV) + np.exp(v_1 - EV) # social surplus function
B = P @ (np.log(G) + EV) # Bellman operator
return B
EV_old = EV = np.zeros(P.shape[0]) # initial EV guess
error = 1
while error > tol:
EV_old = EV
EV = B(EV_old)
error = np.max(np.abs(EV - EV_old))
return EV
def u(x, i, theta):
"""
compute current-period utility, less the structural error
inputs:
x, state variable
i, decision variable
theta, vector of parameters associated with the utility/cost
functions
output:
u, utility from choosing action i in state x
"""
theta_1_1 = theta[0] # linear cost parameter
RC = theta[1] # replacement cost
def c(x, theta_1_1):
"""
compute cost function
inputs:
x, state variable
theta_1_1, linear cost parameter
output:
c, cost
"""
return -0.001 * theta_1_1 * x
if i == 0:
return c(x, theta_1_1)
elif i == 1:
return c(0, theta_1_1) - RC