Author: Fridmund K. Shunsaku
I refer to the http://quant-econ.net/py/ifp.html and its corresponding solution notebook.
I have managed to obtain the invariant asset distribution and the invariant consumption distribution, but I am wondering if it is possible to plot a 3D surface of x=asset, y=consumption, z=density.
I wasn’t sure how to even start about going that, so I gave myself an easier task, that is to plot the optimal consumption plan obtained straight from the coleman, Kc.
Specifically, plot a 3D surface of x=asset (a), y=shock (z), z=consumption ©
I have seen many different ways of plotting a 3D surface on python (including the one available on quantecons), so I picked one and tried it.
def f(X, Y):
# 0 is the borrowing constraint, 4 is gridmax, 50 is gridsize
asset_grid = np.linspace(0, 4, 50)
for i in range(len(X[0,:])):
for j in range(len(Y[:,0])):
cf = lambda a, i_z: interp(a, asset_grid, c[:, i_z])
z = cf(X,Y)
return z
fig = plt.figure(figsize=(7, 7))
ax = fig.gca(projection='3d')
# This is range of x (set to 4 since gridmax=4)
x = np.arange(0, 4)
# This is range of y (set to 2 since there were two shocks)
y = np.arange(0, 2)
# I have read around and this seems to form the 2D base
X,Y = np.meshgrid(x, y)
Z = f(X,Y)
surf = ax.plot_surface(X, Y, Z, linewidth=0, antialiased=False)
ax.set_zlim('tight')
# Not sure what this part does, but I suppose it is for formatting the axis
ax.zaxis.set_major_locator(plt.LinearLocator(10))
ax.zaxis.set_major_formatter(plt.FormatStrFormatter('%.02f'))
plt.show()
However, I met with the following error:
ValueError: object too deep for desired array
Is anyone able to see what is the error here? Also I am keen on the original project/task (and if it is even possible)!