forked from QuantEcon/QuantEcon.py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution_ifp_ex3.py
33 lines (29 loc) · 1.03 KB
/
solution_ifp_ex3.py
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
from matplotlib import pyplot as plt
import numpy as np
from ifp import consumerProblem, coleman_operator, initialize
from compute_fp import compute_fixed_point
from scipy import interp
import mc_sample
def compute_asset_series(cp, T=500000):
"""
Simulates a time series of length T for assets, given optimal savings
behavior. Parameter cp is an instance of consumerProblem
"""
Pi, z_vals, R = cp.Pi, cp.z_vals, cp.R # Simplify names
v_init, c_init = initialize(cp)
c = compute_fixed_point(coleman_operator, cp, c_init)
cf = lambda a, i_z: interp(a, cp.asset_grid, c[:, i_z])
a = np.zeros(T+1)
z_seq = mc_sample.sample_path(Pi, sample_size=T)
for t in range(T):
i_z = z_seq[t]
a[t+1] = R * a[t] + z_vals[i_z] - cf(a[t], i_z)
return a
if __name__ == '__main__':
cp = consumerProblem(r=0.03, grid_max=4)
a = compute_asset_series(cp)
fig, ax = plt.subplots()
ax.hist(a, bins=20, alpha=0.5, normed=True)
ax.set_xlabel('assets')
ax.set_xlim(-0.05, 0.75)
fig.show()