示例#1
0
'''
demo.figure('Bellman Equation Residual', 'Net Unit Profit', 'Percent Residual')
plt.plot(pr, 100 * (resid / model.Value(pr)).T)
# plot(pr,0*pr,'k--')
plt.legend(model.labels.i, loc='upper right')
'''

## SIMULATION

# Simulate Model

T = 50
nrep = 10000
sinit = np.full(nrep, pbar)
iinit = 0
data = model.simulate(T,sinit,iinit, seed=945)

# Print Ergodic Moments
frm = '\t{:<10s} = {:5.2f}'

print('\nErgodic Means')
print(frm.format('Price', data['unit profit'].mean()))
print(frm.format('Age', data.i.mean()))
print('\nErgodic Standard Deviations')
print(frm.format('Price', data['unit profit'].std()))
print(frm.format('Age', data.i.std()))


# Plot Simulated and Expected Continuous State Path
print(demo.qplot('time', 'unit profit', '_rep',
      data=data[data['_rep'] < 3],
示例#2
0
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)
data.groupby('time')['wealth'].mean().plot(color='k', linestyle='--')
plt.title('Simulated and Expected Wealth')
plt.xlabel('Period')
plt.ylabel('Wealth')

# Plot Simulated Policy Path

subdata = data[data['_rep'] < 3][['time', 'investment', '_rep']]
subdata.pivot(index='time', columns='_rep',
示例#3
0
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')
plt.xlabel('Year')
plt.ylabel('Reservoir Level')

# Plot Simulated Policy Path
D = data[data['_rep'] < 3][['time', 'released', '_rep']]
D.pivot('time', '_rep', 'released').plot(legend=False, lw=1)
data.groupby('time')['released'].mean().plot(color='k', linestyle='--')
plt.title('Simulated and Expected Irrigation')
plt.xlabel('Year')
示例#4
0
scrit = np.interp(0.0, vr[1] - vr[0], sr)
vcrit = np.interp(scrit, sr, vr[0])
demo.annotate(scrit,
              vcrit,
              '$s^* = {:.2f}$'.format(scrit),
              'wo', (-5, 5),
              fs=12)

print('Optimal Biomass Harvesting Level = {:5.2f}'.format(scrit))

# Plot Residual
demo.figure('Bellman Equation Residual', 'Biomass', 'Percent Residual')
plt.plot(sr, 100 * resid.T / vr.max(0).T)

## SIMULATION

# Simulate Model
T = 50  # Number of periods simulated
sinit = 0.0  # Initial value of continuous state
data = model.simulate(T, sinit)

# Compute Optimal Rotation Cycle
print('Optimal Rotation Cycle = ', np.min(data.time[data.j == 'clear cut']))

# Plot State Path

data.plot('time', 'stand biomass', legend=False, title='Rotation cycle')
plt.ylabel('Biomass')

plt.show()
#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
vtrue = vstar + b * (np.log(S.Wealth) - np.log(sstar))


# ==============   Make plots:
Wealth = S.Wealth.T

示例#6
0
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')

plt.show()
示例#7
0
demo.qplot('profit','resid2','i',S)
plt.legend(dactions)


# ## SIMULATION

# We simulate the model 50000 times for a time horizon $T=50$, starting with an operating firm ($d=1$) at the long-term profit mean $\bar{\pi}$. To be able to reproduce these results, we set the random seed at an arbitrary value of 945.

# In[11]:


T = 50
nrep = 50000
p0 = np.tile(pbar, (1, nrep))
d0 = 1
data = model.simulate(T, p0, d0, seed=945)


# ### Print Ergodic Moments

# In[12]:


f = '\t{:21s} = {:5.2f}'
print('\nErgodic Means')
print(f.format('Profit Contribution', data['profit'].mean()))
print(f.format('Activity', (data['i'] == 'active').mean()))
print('\nErgodic Standard Deviations\n')
print(f.format('Profit Contribution', data['profit'].std()))
print(f.format('Activity', (data['i'] == 'active').std()))
示例#8
0
# Plot Shadow Price Function
demo.figure('Shadow Price Function', 'Stock', 'Shadow Price')
plt.plot(s, model.Value(s, 1).T)

# Plot Residual
demo.figure('Bellman Equation Residual','Stock', 'Residual')
plt.plot(s, resid.T)
plt.hlines(0, smin, smax, 'k', '--')


## SIMULATION

# Simulate Model
T = 15
data = model.simulate(T, smin)
print(data)

# Plot State and Policy Paths
opts = dict(spec='r*', offset=(0,-5), fs=11, ha='right')

data[['available', 'harvest']].plot()
demo.annotate(T, sstar, 'steady-state stock = %.2f' % sstar, **opts)
demo.annotate(T, qstar, 'steady-state harvest = %.2f' % qstar, **opts)
plt.xlim([0, T + 0.25])
plt.title('State and Policy Paths')
plt.xlabel('Period')
plt.ylabel('Stock / Harvest')
plt.legend(['Stock','Harvest'], loc='right')

plt.show()
示例#9
0
plt.plot(s, v.T)

# Plot Shadow Price Function
demo.figure('Shadow Price Function', 'Stock', 'Shadow Price')
plt.plot(s, model.Value(s, 1).T)

# Plot Residual
demo.figure('Bellman Equation Residual', 'Stock', 'Residual')
plt.plot(s, resid.T)
plt.hlines(0, smin, smax, 'k', '--')

## SIMULATION

# Simulate Model
T = 15
data = model.simulate(T, smin)
print(data)

# Plot State and Policy Paths
opts = dict(spec='r*', offset=(0, -5), fs=11, ha='right')

data[['available', 'harvest']].plot()
demo.annotate(T, sstar, 'steady-state stock = %.2f' % sstar, **opts)
demo.annotate(T, qstar, 'steady-state harvest = %.2f' % qstar, **opts)
plt.xlim([0, T + 0.25])
plt.title('State and Policy Paths')
plt.xlabel('Period')
plt.ylabel('Stock / Harvest')
plt.legend(['Stock', 'Harvest'], loc='right')

plt.show()
示例#10
0
fig2 = demo.figure('Bellman Equation Residual', 'Potential Profit',
                   'Percent Residual')
demo.qplot('profit', 'resid2', 'i', S)
plt.legend(dactions)

# ## SIMULATION

# We simulate the model 50000 times for a time horizon $T=50$, starting with an operating firm ($d=1$) at the long-term profit mean $\bar{\pi}$. To be able to reproduce these results, we set the random seed at an arbitrary value of 945.

# In[11]:

T = 50
nrep = 50000
p0 = np.tile(pbar, (1, nrep))
d0 = 1
data = model.simulate(T, p0, d0, seed=945)

# ### Print Ergodic Moments

# In[12]:

f = '\t{:21s} = {:5.2f}'
print('\nErgodic Means')
print(f.format('Profit Contribution', data['profit'].mean()))
print(f.format('Activity', (data['i'] == 'active').mean()))
print('\nErgodic Standard Deviations\n')
print(f.format('Profit Contribution', data['profit'].std()))
print(f.format('Activity', (data['i'] == 'active').std()))

# ### Plot Simulated and Expected Continuous State Path
示例#11
0
surf(s1,s2,resid,'FaceColor','interp','EdgeColor','interp')
title('Bellman Equation Residual')
xlabel('GDP Gap')
ylabel('Inflation Rate')
zlabel('Residual')


"""

## SIMULATION

# Simulate Model
#rand('seed',0.945)
nper, nrep = 21, 10000
sinit = np.tile(smax, (nrep, 1)).T
S = bank.simulate(nper, sinit)
print(S.mean())
"""

s1sim = ssim(:,:,1)
s2sim = ssim(:,:,2)

# Compute Ergodic Moments
s1avg = mean(s1sim(:))
s2avg = mean(s2sim(:))
xavg = mean(xsim(:))
s1std = std(s1sim(:))
s2std = std(s2sim(:))
xstd = std(xsim(:))

# Print Steady-State and Ergodic Moments
示例#12
0
# ### Plot Residuals

# In[13]:


S['resid2'] = 100*S.resid / S.value

fig2 = demo.figure('Bellman Equation Residual','','Percent Residual')
S['resid2'].plot(ax=plt.gca())
plt.hlines(0,0,smax,'k')


# ###  Simulation
# 
# The path followed by the biomass is computed by the ```simulate()``` method. Here we simulate 32 periods starting with a biomass level $s_0 = 0$.

# In[14]:


H = model.simulate(32, 0.0)

fig3 = demo.figure('Timber harvesting simulation','Period','Biomass')
H['biomass'].plot(ax=plt.gca())


# In[15]:


demo.savefig([fig1, fig2, fig3])

示例#13
0

# 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)
data.groupby('time')['wealth'].mean().plot(color='k', linestyle='--')
plt.title('Simulated and Expected Wealth')
plt.xlabel('Period')
plt.ylabel('Wealth')


# Plot Simulated Policy Path
示例#14
0
# 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')

plt.show()
示例#15
0
surf(s1,s2,resid,'FaceColor','interp','EdgeColor','interp')
title('Bellman Equation Residual')
xlabel('GDP Gap')
ylabel('Inflation Rate')
zlabel('Residual')


"""

## SIMULATION

# Simulate Model
#rand('seed',0.945)
nper, nrep = 21, 10000
sinit = np.tile(smax, (nrep, 1)).T
S = bank.simulate(nper, sinit)
print(S.mean())
"""

s1sim = ssim(:,:,1)
s2sim = ssim(:,:,2)

# Compute Ergodic Moments
s1avg = mean(s1sim(:))
s2avg = mean(s2sim(:))
xavg = mean(xsim(:))
s1std = std(s1sim(:))
s2std = std(s2sim(:))
xstd = std(xsim(:))

# Print Steady-State and Ergodic Moments