# Plot Optimal Policy demo.figure('Optimal Investment Policy', 'Wealth', 'Investment') plt.plot(s, k.T) # Plot Value Function demo.figure('Value Function', 'Wealth', 'Value') plt.plot(s, v.T) # Plot Shadow Price Function demo.figure('Shadow Price Function', 'Wealth', 'Shadow Price') plt.plot(s, growth.Value(s, order=1).T) # Plot Residual demo.figure('Bellman Equation Residual', 'Wealth', 'Residual') plt.plot(s, resid.T) plt.hlines(0, smin, smax, 'k', '--') ## SIMULATION # Simulate Model T = 20 nrep = 50000 sinit = np.full((1, nrep), smin) data = growth.simulate(T, sinit) # Plot Simulated State Path subdata = data[data['_rep'] < 3][['time', 'wealth', '_rep']] subdata.pivot(index='time', columns='_rep', values='wealth').plot(legend=False, lw=1)
# Plot Optimal Policy demo.figure('Optimal Extraction', 'Ore Stock', 'Ore Extracted') plt.plot(s, q.T) # Plot Value Function demo.figure('Value Function', 'Ore Stock', 'Value') plt.plot(s, v.T) # Plot Shadow Price Function demo.figure('Shadow Price Function', 'Ore Stock', 'Shadow Price') plt.plot(s, model.Value(s, 1)) # Plot Residual demo.figure('Bellman Equation Residual', 'Ore Stock', 'Residual') plt.plot(s, resid.T) plt.hlines(0, 0, smax, 'k', '--') ## SIMULATION # Simulate Model T = 20 data = model.simulate(T, smax) # Plot State and Policy Paths data[['stock', 'extracted']].plot() plt.title('State and Policy Paths') plt.legend(['Stock', 'Extraction']) plt.hlines(sstar, 0, T, 'k', '--') plt.xlabel('Period') plt.ylabel('Stock / Extraction')
# Plot Optimal Policy demo.figure('Optimal Irrigation Policy', 'Reservoir Level', 'Irrigation') plt.plot(s, x.T) # Plot Value Function demo.figure('Value Function', 'Reservoir Level', 'Value') plt.plot(s, v.T) # Plot Shadow Price Function demo.figure('Shadow Price Function', 'Reservoir Level', 'Shadow Price') plt.plot(s, model.Value(s, 1).T) # Plot Residual demo.figure('Bellman Equation Residual', 'Reservoir Level', 'Residual') plt.plot(s,resid.T) plt.hlines(0, smin, smax, 'k', '--') ## SIMULATION # Simulate Model T = 30 nrep = 100000 sinit = np.full((1, nrep), smin) data = model.simulate(T, sinit, seed=945) # Plot Simulated State Path D = data[data['_rep'] < 3][['time', 'reservoir', '_rep']] D.pivot(index='time', columns='_rep', values='reservoir').plot(legend=False, lw=1) data.groupby('time')['reservoir'].mean().plot(color='k', linestyle='--') plt.title('Simulated and Expected Reservoir Level')
# In[8]: figs = [] figs.append(demo.figure("Runge's Function", '', 'y')) plt.plot(x, y) plt.text(-0.8, 0.8, r'$y = \frac{1}{1+25x^2}$', fontsize=18) plt.xticks=[] # In[9]: figs.append(demo.figure("Runge's Function $11^{th}$-Degree\nPolynomial Approximation Error.",'x', 'Error')) plt.hlines(0, a, b, 'gray', '--') plt.plot(x, errcheb[4], label='Chebychev Nodes') plt.plot(x, errunif[4], label='Uniform Nodes') plt.legend(loc='upper center') # Plot approximation error per degree of approximation # In[10]: figs.append(demo.figure("Log10 Polynomial Approximation Error for Runge's Function",'', 'Log10 Error')) plt.plot(n, nrmcheb, label='Chebychev Nodes') plt.plot(n, nrmunif, label='Uniform Nodes') plt.legend(loc='upper left') plt.xticks=[]
q = Q(p) return p + q / (-3.5 * p**(-4.5)) - np.sqrt(q) - q**2 # Approximation structure n, a, b = 21, 0.5, 2.5 Q = BasisChebyshev(n, a, b) c0 = np.zeros(n) c0[0] = 2 p = Q.nodes # Solve for effective supply function monopoly = NLP(resid) Q.c = monopoly.broyden(c0) # Setup plot nplot = 1000 p = nodeunif(nplot, a, b) rplot = resid(Q.c) # Plot effective supply demo.figure("Monopolist's Effective Supply Curve", 'Quantity', 'Price') plt.plot(Q(p), p) # Plot residual demo.figure('Functional Equation Residual', 'Price', 'Residual') plt.hlines(0, a, b, 'k', '--') plt.plot(p, rplot) plt.show()
# Plot Optimal Policy demo.figure('Optimal Extraction', 'Ore Stock', 'Ore Extracted') plt.plot(s, q.T) # Plot Value Function demo.figure('Value Function', 'Ore Stock', 'Value') plt.plot(s, v.T) # Plot Shadow Price Function demo.figure('Shadow Price Function', 'Ore Stock', 'Shadow Price') plt.plot(s, model.Value(s, 1)) # Plot Residual demo.figure('Bellman Equation Residual', 'Ore Stock', 'Residual') plt.plot(s, resid.T) plt.hlines(0, 0, smax,'k', '--') ## SIMULATION # Simulate Model T = 20 data = model.simulate(T, smax) # Plot State and Policy Paths data[['stock', 'extracted']].plot() plt.title('State and Policy Paths') plt.legend(['Stock','Extraction']) plt.hlines(sstar, 0, T, 'k', '--') plt.xlabel('Period') plt.ylabel('Stock / Extraction')
# In[8]: figs = [] figs.append(demo.figure("Runge's Function", '', 'y')) plt.plot(x, y) plt.text(-0.8, 0.8, r'$y = \frac{1}{1+25x^2}$', fontsize=18) plt.xticks = [] # In[9]: figs.append( demo.figure( "Runge's Function $11^{th}$-Degree\nPolynomial Approximation Error.", 'x', 'Error')) plt.hlines(0, a, b, 'gray', '--') plt.plot(x, errcheb[4], label='Chebychev Nodes') plt.plot(x, errunif[4], label='Uniform Nodes') plt.legend(loc='upper center') # Plot approximation error per degree of approximation # In[10]: figs.append( demo.figure("Log10 Polynomial Approximation Error for Runge's Function", '', 'Log10 Error')) plt.plot(n, nrmcheb, label='Chebychev Nodes') plt.plot(n, nrmunif, label='Uniform Nodes') plt.legend(loc='upper left') plt.xticks = []
# Print Ergodic Moments ff = '\t{:12s} = {:5.2f}' print('\nErgodic Means') print(ff.format('Wage', data['wage'].mean())) print(ff.format('Employment', (data['i'] == 'employed').mean())) print('\nErgodic Standard Deviations') print(ff.format('Wage', data['wage'].std())) print(ff.format('Employment', (data['i'] == 'employed').std())) # Plot Expected Discrete State Path subdata = data[['time', 'i']] subdata['i'] = subdata['i'] == 'employed' subdata.groupby('time').mean().plot(legend=False) plt.title('Probability of Employment') plt.xlabel('Period') plt.ylabel('Probability') # Plot Simulated and Expected Continuous State Path subdata = data[data['_rep'] < 3][['time', 'wage', '_rep']] subdata.pivot(index='time', columns='_rep', values='wage').plot(legend=False, lw=1) plt.hlines(data['wage'].mean(), 0, T) plt.title('Simulated and Expected Wage') plt.xlabel('Period') plt.ylabel('Wage') plt.show()