# Model Structure model = DPmodel(basis, reward, transition, bounds, i=['Low price', 'Average price', 'High Price'], x=['Current production'], discount=delta, q=q) # Check Model Derivatives # dpcheck(model,sstar,sstar) ## SOLUTION # Solve Bellman Equation model.solve() resid, s, v, x = model.solution() """ # Plot Optimal Policy figure plot(s,x) legend('Low Price','Average Price','High Price') legend('Location','Best') legend('boxoff') title('Optimal Production Policy')
discount=delta, e=e, w=w) # Deterministic Steady-State kstar = ((1 - delta * gamma) / (delta * beta))**( 1 / (beta - 1)) # determistic steady-state capital investment sstar = gamma * kstar + kstar**beta # deterministic steady-state wealth # Check Model Derivatives # dpcheck(model,sstar,kstar) ## SOLUTION # Solve Bellman Equation growth.solve() resid, s, v, k = growth.solution() # 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
model = DPmodel(basis, reward, transition, bounds, i=['Low price', 'Average price', 'High Price'], x=['Current production'], discount=delta, q=q) # Check Model Derivatives # dpcheck(model,sstar,sstar) ## SOLUTION # Solve Bellman Equation model.solve() resid, s, v, x = model.solution() """ # Plot Optimal Policy figure plot(s,x) legend('Low Price','Average Price','High Price') legend('Location','Best') legend('boxoff') title('Optimal Production Policy') xlabel('Lagged Production')
return p * 50 - kappa else: return p * (alpha[0] + alpha[1] * a + alpha[2] * a ** 2 ) def transition(p, x, i, j, in_, e): return pbar + gamma * (p - pbar) + e model = DPmodel(basis, profit, transition, # i=['a={}'.format(a+1) for a in range(A)], i=[a + 1 for a in range(A)], j=['keep', 'replace'], discount=delta, e=e, w=w, h=h) # SOLUTION S = model.solve() pr = np.linspace(pmin, pmax, 10 * n) # Plot Action-Contingent Value Functions pp = demo.qplot('unit profit', 'value_j', 'i', data=S, main='Action-Contingent Value Functions', xlab='Net Unit Profit', ylab='Value') print(pp)
# Model model = DPmodel(BasisSpline(n, pmin, pmax, labels=['profit']), profit, transition, i=['idle', 'active'], j=['idle', 'active'], discount=delta, e=e, w=w, h=[[0, 0], [1, 1]]) ## SOLUTION # Solve Bellman Equation S = model.solve(print=True) # Plot Action-Contingent Value Functions S['ij'] = pd.Categorical( S.i.astype(np.ndarray) + '--' + S.j.astype(np.ndarray)) S.ij.cat.categories = [ 'Keep active firm open', 'Shut down', 'Reopen Firm', 'Remain idle' ] print( demo.qplot('profit', 'value_j', 'ij', data=S[S.ij != 'Remain idle'], geom='line')) '''
# Deterministic Steady-State xstar = 1 # deterministic steady-state action sstar = 1 + (a[0] *(1-delta)/b[0]) ** (1 / b[1]) # deterministic steady-state stock # Check Model Derivatives # dpcheck(model,sstar,xstar) ## SOLUTION # Compute Linear-Quadratic Approximation at Collocation Nodes model.lqapprox(sstar, xstar) # Solve Bellman Equation model.solve() # no need to pass LQ to model, it's already there resid, s, v, x = model.solution() # 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
# =========== Compute Analytic Solution at Collocation Nodes vtrue = vstar + b * (np.log(snodes) - np.log(sstar)) ktrue = delta * beta * snodes # Set a refined grid to evaluate the functions #Wealth = np.linspace(smin, smax, n * 10) order = np.atleast_2d([0, 1]) # =========== Solve Bellman Equation options = dict(print=True, algorithm='newton', maxit=253) S = growth_model.solve(vtrue, ktrue, print=True, algorithm='newton', maxit=120) v, pr = growth_model.Value(S.Wealth, order) k = growth_model.Policy(S.Wealth) # resid = growth_model.residuals(s) # ============ Simulate Model T = 20 data = growth_model.simulate(T, np.atleast_2d(smin)) # ============ Compute Linear-Quadratic Approximation growth_model.lqapprox(sstar, kstar) vlq, plq = growth_model.Value(S.Wealth, order) klq = growth_model.Policy(S.Wealth) # ============ Compute Analytic Solution
# Deterministic Steady-State xstar = 1 # deterministic steady-state action sstar = 1 + (a[0] * (1 - delta) / b[0])**(1 / b[1] ) # deterministic steady-state stock # Check Model Derivatives # dpcheck(model,sstar,xstar) ## SOLUTION # Compute Linear-Quadratic Approximation at Collocation Nodes model.lqapprox(sstar, xstar) # Solve Bellman Equation model.solve() # no need to pass LQ to model, it's already there resid, s, v, x = model.solution() # 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
# In[10]: model = DPmodel(basis, profit, transition, i=dstates, j=options, discount=delta, e=e, w=w, h=h) # ### SOLUTION # The ```solve``` method returns a pandas ```DataFrame```. # In[11]: S = model.solve() S.head() # ## Analysis # # ### Plot Action-Contingent Value Functions # In[12]: # Compute and Plot Critical Unit Profit Contributions pcrit = [NLP(lambda s: model.Value_j(s)[i].dot([1,-1])).broyden(0.0)[0] for i in range(A)] vcrit = [model.Value(s)[i] for i, s in enumerate(pcrit)]
model = DPmodel(basis, profit, transition, i=dstates, j=dactions, discount=0.9, e=e, w=w, h=[[0, 0], [1, 1]]) # ## SOLUTION # # To solve the model, we simply call the ```solve``` method on the ```model``` object. # In[8]: S = model.solve(print=True) S.head() # ### Plot Action-Contingent Value Functions # In[9]: fig1,axs = plt.subplots(1,2,sharey=True, figsize=[12,5]) offs = [(4, -8), (-8, 5)] msgs = ['Profit Entry', 'Profit Exit'] for i,iname in enumerate(dstates): plt.axes(axs[i]) subdata = S.loc[iname,['value[close]', 'value[open]']]
growth = DPmodel(basis, reward, transition, bounds, x=['investment'], discount=delta, e=e, w=w) # Deterministic Steady-State kstar = ((1 - delta * gamma) / (delta * beta)) ** (1 / (beta - 1)) # determistic steady-state capital investment sstar = gamma * kstar + kstar ** beta # deterministic steady-state wealth # Check Model Derivatives # dpcheck(model,sstar,kstar) ## SOLUTION # Solve Bellman Equation growth.solve() resid, s, v, k = growth.solution() # 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)