示例#1
0
# 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
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)
# ===========   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
vtrue = vstar + b * (np.log(S.Wealth) - np.log(sstar))
示例#3
0
# Compute and print abandonment point
sstar = (b[0] - a[0]) / b[1]
print('Abandonment Point = %5.2f' % sstar)

# 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')
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)]

# In[13]:

fig1 = demo.figure('Action-Contingent Value Functions',
                   'Net Unit Profit',
                   'Value',
                   figsize=[10, 5])

cc = np.linspace(0.3, 0.9, model.dims.ni)

for a, i in enumerate(dstates):
    plt.plot(S.loc[i, 'value[keep]'],
             color=plt.cm.Blues(cc[a]),
             label='Keep ' + i)
    if pmin < pcrit[a] < pmax: