示例#1
0
''' Randomly generate starting point '''
problem.x0 = 10 * np.random.randn(1)
''' Compute root using Newton method '''
t0 = tic()
x1 = problem.newton()
t1 = 100 * toc(t0)
n1, x_newton = problem.fnorm, problem.x_sequence
''' Compute root using Broyden method '''
t0 = tic()
x2 = problem.broyden()
t2 = 100 * toc(t0)
n2, x_broyden = problem.fnorm, problem.x_sequence
''' Print results '''
print('Hundredths of seconds required to compute root of exp(-x)-1,')
print('via Newton and Broyden methods, starting at x = %4.2f.' % problem.x0)
print('\nMethod      Time   Norm of f   Final x')
print('Newton  %8.2f    %8.0e     %5.2f' % (t1, n1, x1))
print('Broyden %8.2f    %8.0e     %5.2f' % (t2, n2, x2))
''' View current options for solver '''
print(problem.opts)
''' Describe the options '''
print(problem.opts.__doc__)
''' Plot the convergence '''
b = -abs(problem.x0)
a = -b
xx = np.linspace(a, b, 100)

fig = plt.figure()
plt.plot(xx, f(xx)[0], 'b-')
plt.plot(x_newton, f(x_newton)[0], 'ro:')
plt.show()
    (p_0, p_T), (s_0, s_T) = F([0, T])
    return np.r_[d.flatten(), s_0 - s0, s_T]


storage = NLP(resid, F.c.flatten(), tnodes, T, n, F, r, k, eta, s0)
c = storage.broyden(print=True)
F.c = np.reshape(c, (2, n))

nplot = 501
t = np.linspace(0, T, nplot)
(p, s), (dp, ds) = F(t, [[0, 1]])
res_p = dp - r * p - k
res_s = ds + p ** -eta
plt.figure()
plt.subplot(2, 1, 1)
plt.plot(t, res_p)
plt.title('Residuals')
plt.ylabel('d(price) residual')

plt.subplot(2, 1, 2)
plt.plot(t, res_s)
plt.xlabel('time')
plt.ylabel('d(storage) residual')


plt.figure()
plt.subplot(2, 1, 1)
plt.plot(t, p)
plt.title('Solution')
plt.ylabel('Price')

## Analysis

# Display Optimal Policy
xtemp = model.policy.reshape((n1, n2))
header = '{:^8s} {:^8s}  {:^8s}  {:^8s}'.format('Age','Lo', 'Med', 'Hi')

print('Optimal Policy')
print(header)
print(*('{:^8d} {:^8s}  {:^8s}  {:^8s}\n'.format(s, *X[x]) for s, x in zip(s1, xtemp)))


# Plot Value Function
demo.figure('Optimal Replacement Value', 'Age', 'Optimal Value (thousands)')
plt.plot(s1, model.value.reshape((n1,n2)) / 1000)
plt.legend(['Low','Med','Hi'])



# Compute Steady-State distribution
pi = model.markov().reshape((n1, n2))

# Display Steady-State distribution
print('          Invariant Distribution     ')
print(header)
print(*('{:^8d} {:8.3f}  {:8.3f}  {:8.3f}\n'.format(s, *x) for s, x in zip(s1, pi)))


# Compute Steady-State Mean Cow Age and Productivity
pi = pi.flatten()
    yapprox = BasisChebyshev(n[i], a, b, f=runge)
    yfit = yapprox(x)  # [0] no longer needed?  # index zero is to eliminate one dimension
    phi = yapprox.Phi()
    errcheb[i] = yfit - y
    nrmcheb[i] = np.log10(norm(yfit - y, np.inf))
    concheb[i] = np.log10(cond(phi, 2))


# Plot Chebychev- and uniform node polynomial approximation errors

# 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
示例#5
0
from demos.setup import np, plt

from compecon.quad import qnwlogn
from compecon.tools import nodeunif
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm

# Univariate Taylor approximation

x = np.linspace(-1, 1, 100)
y = (x + 1) * np.exp(2 * x)
y1 = 1 + 3 * x
y2 = 1 + 3 * x + 8 * x**2

plt.figure(figsize=[6, 6])
plt.plot(x, y, 'k', linewidth=3, label='Function')
plt.plot(x, y1, 'b', linewidth=3, label='1st order approximation')
plt.plot(x, y2, 'r', linewidth=3, label='2nd order approximation')
plt.legend()
plt.xticks([-1, 0, 1])
plt.show()

## Bivariate Taylor approximation
nplot = [101, 101]
a = [0, -1]
b = [2, 1]
x1, x2 = nodeunif(nplot, a, b)
x1.shape = nplot
x2.shape = nplot

y = np.exp(x2) * x1**2
示例#6
0
    for i in range(n):
        for j in range(r.size):
            snext = min(S[i] - X[k] + r[j], maxcap)
            inext = getindex(snext, S)
            P[k, i, inext] = P[k, i, inext] + p[j]

# Model Structure
model = DDPmodel(f, P, delta)
model.solve()

## Analysis

# Plot Optimal Policy
demo.figure('Optimal Irrigation Policy', 'Water Level', 'Irrigation', [-1, 31],
            [0, 6])
plt.plot(S, X[model.policy], '*')

# Plot Value Function
demo.figure('Optimal Value Function', 'Water Level', 'Value')
plt.plot(S, model.value)

# Simulate Model
sinit = np.zeros(10000)
nyrs = 30
t = np.arange(1 + nyrs)
spath, xpath = model.simulate(sinit, nyrs)

# Plot State Path
demo.figure('Optimal State Path', 'Year', 'Water Level')
plt.plot(t, S[spath].mean(1))
    # Chebychev-node Chebychev-basis approximant
    yapprox = BasisChebyshev(n[i], a, b, f=runge)
    yfit = yapprox(
        x)  # [0] no longer needed?  # index zero is to eliminate one dimension
    phi = yapprox.Phi()
    errcheb[i] = yfit - y
    nrmcheb[i] = np.log10(norm(yfit - y, np.inf))
    concheb[i] = np.log10(cond(phi, 2))

# Plot Chebychev- and uniform node polynomial approximation errors

# 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
    S = BasisSpline(n, a, b, f=ff)
    L = BasisSpline(n, a, b, k=1, f=ff)

    # Compute actual and fitted values on grid
    y = ff(x)  # actual
    yc = C(x)  # Chebychev approximant
    ys = S(x)  # cubic spline approximant
    yl = L(x)  # linear spline approximant

    # Plot function approximations
    plt.figure()
    ymin = np.floor(y.min())
    ymax = np.ceil(y.max())
    xlim = [a, b]
    ylim = [-0.2, 1.2] if ifunc == 2 else [ymin, ymax]

    subfig(1, x, y, xlim, ylim, 'Function')
    subfig(2, x, yc, xlim, ylim, 'Chebyshev')
    subfig(3, x, ys, xlim, ylim, 'Cubic Spline')
    subfig(4, x, yl, xlim, ylim, 'Linear Spline')

    # Plot function approximation error
    plt.figure()
    plt.plot(x, np.c_[yc - y, ys - y], linewidth=3)
    plt.xlabel('x')
    plt.ylabel('Approximation Error')
    plt.legend(['Chebychev Polynomial', 'Cubic Spline'])
    # pltlegend('boxoff')

plt.show()

# State Transition Function
g = np.empty_like(f)
g[0] = getindex(np.c_[S1 + 1, S2], S)
g[1] = getindex(np.c_[S1 + 1, S2 + 1], S)
g[2] = getindex(np.c_[1, 0], S)

# Model Structure
model = DDPmodel(f, g, delta)
model.solve()
   


## Analysis

# Simulate Model
sinit = 0
nyrs = 12
t = np.arange(nyrs + 1)
spath, xpath = model.simulate(sinit, nyrs)

# Plot State Path (Age)
demo.figure('Optimal State Path', 'Year', 'Age of Asset', [0, 12])
plt.plot(t, S1[spath])

# Plot State Path (Servicings)
demo.figure('Optimal State Path', 'Year', 'Number of Servicings', [0, 12], [0, 2.25])
plt.plot(t, S2[spath])

plt.show()
示例#10
0
m = len(X)               	# number of actions

# Reward Function
f = np.zeros((m,n))
f[1] = strike - price

# State Transition Probability Matrix
P = np.zeros((m, n, n))

for i in range(n):
    P[0, i, min(i + 1, n - 1)] = q
    P[0, i, max(i - 1, 0)] = 1 - q

# Model Structure
model = DDPmodel(f, P, delta, horizon=N)
model.solve()
   
## Analysis

# Plot Optimal Exercise Boundary
i, j = np.where(np.diff(model.policy[:-1], 1))
temp = (i * tau)[::-1]
demo.figure('Put Option Optimal Exercise Boundary', 'Time to Maturity', 'Asset Price')
plt.plot(temp, price[j])

# Plot Option Premium vs. Asset Price
demo.figure('Put Option Value', 'Asset Price', 'Premium', [0, 2 * strike])
plt.plot([0, strike],[strike, 0], 'k--', lw=2)
plt.plot(price, model.value[0], lw=3)

plt.show()
示例#11
0
C = BasisChebyshev(n, a, b, f=f)    # chose basis functions
yc = C(x)                           # values
dc = C(x, 1)                        # first derivative
sc = C(x, 2)                        # second derivative


# Construct and evaluate cubic spline interpolant
S = BasisSpline(n, a, b, f=f)       # chose basis functions
ys = S(x)                           # values
ds = S(x, 1)                        # first derivative
ss = S(x, 2)                        # second derivative

# Plot function approximation error
plt.figure()
plt.subplot(2, 1, 1),
plt.plot(x, y - yc[0])
plt.ylabel('Chebychev')
plt.title('Function Approximation Error')

plt.subplot(2, 1, 2)
plt.plot(x, y - ys[0])
plt.ylabel('Cubic Spline')
plt.xlabel('x')


# Plot first derivative approximation error
plt.figure()
plt.subplot(2, 1, 1),
plt.plot(x, d - dc[0])
plt.ylabel('Chebychev')
plt.title('First Derivative Approximation Error')
示例#12
0
#resid, sr, vr = model.residuals(10)

# Compute and Print Critical Action Wages

wcrit0 = np.interp(0, vr[0, 1] - vr[0, 0], sr)
vcrit0 = np.interp(wcrit0, sr, vr[0, 0])
print('Critical Search Wage = {:5.1f}'.format(wcrit0))

wcrit1 = np.interp(0, vr[1, 1] - vr[1, 0], sr)
vcrit1 = np.interp(wcrit1, sr, vr[1, 0])
print('Critical Quit Wage   = {:5.1f}'.format(wcrit1))

# Plot Action-Contingent Value Function - Unemployed

demo.figure('Action-Contingent Value, Unemployed', 'Wage', 'Value')
plt.plot(sr, vr[0].T)
demo.annotate(wcrit0,
              vcrit0,
              '$w^*_0 = {:.1f}$'.format(wcrit0),
              'wo', (5, -5),
              fs=12)
plt.legend(['Do Not Search', 'Search'], loc='upper left')

# Plot Action-Contingent Value Function - Unemployed

demo.figure('Action-Contingent Value, Employed', 'Wage', 'Value')
plt.plot(sr, vr[1].T)
demo.annotate(wcrit1,
              vcrit1,
              '$w^*_0 = {:.1f}$'.format(wcrit1),
              'wo', (5, -5),
示例#13
0
def subfig(k, x, y, xlim, ylim, title):
    plt.subplot(2, 2, k)
    plt.plot(x, y)
    plt.xlim(xlim)
    plt.ylim(ylim)
    plt.title(title)
示例#14
0
# ============  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


# Plot Optimal Policy
demo.figure('Optimal Investment Policy', 'Wealth', 'Investment')
plt.plot(Wealth, np.c_[k, klq])
demo.annotate(sstar, kstar,'$s^*$ = %.2f\n$k^*$ = %.2f' % (sstar, kstar), 'bo', (10, -7))
plt.legend(['Chebychev Collocation','L-Q Approximation'], loc = 'upper left')


# Plot Value Function
demo.figure('Value Function', 'Wealth', 'Value')
plt.plot(Wealth, np.c_[v, vlq])
demo.annotate(sstar, vstar,'$s^*$ = %.2f\n$V^*$ = %.2f' % (sstar, vstar),'bo', (10, -7))
plt.legend(['Chebychev Collocation','L-Q Approximation'], loc= 'upper left')



# Plot Shadow Price Function
demo.figure('Shadow Price Function', 'Wealth', 'Shadow Price')
plt.plot(Wealth, np.c_[pr, plq])
示例#15
0
# dpcheck(model,smax,0)


## SOLUTION

# Solve Bellman Equation
model.solve()
resid, s, v, q = model.solution()

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

示例#16
0
# Check Model Derivatives
# dpcheck(model,smax,0)

## SOLUTION

# Solve Bellman Equation
model.solve()
resid, s, v, q = model.residuals()

# 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
示例#17
0
    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()
示例#18
0
        return s + gamma * (smax - s)


model = DPmodel(basis,
                reward,
                transition,
                discount=delta,
                j=['keep', 'clear cut'])

model.solve()
resid, sr, vr = model.residuals()

# Plot Action-Contingent Value Functions

demo.figure('Action-Contingent Value Functions', 'Biomass', 'Value of Stand')
plt.plot(sr, vr.T)
plt.legend(model.labels.j, loc='upper center')

# Compute and Plot Optimal Harvesting Stock Level
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')
示例#19
0
    (p_0, p_T), (s_0, s_T) = F([0, T])
    return np.r_[d.flatten(), s_0 - s0, s_T]


storage = NLP(resid, F.c.flatten(), tnodes, T, n, F, r, k, eta, s0)
c = storage.broyden(print=True)
F.c = np.reshape(c, (2, n))

nplot = 501
t = np.linspace(0, T, nplot)
(p, s), (dp, ds) = F(t, [[0, 1]])
res_p = dp - r * p - k
res_s = ds + p**-eta
plt.figure()
plt.subplot(2, 1, 1)
plt.plot(t, res_p)
plt.title('Residuals')
plt.ylabel('d(price) residual')

plt.subplot(2, 1, 2)
plt.plot(t, res_s)
plt.xlabel('time')
plt.ylabel('d(storage) residual')

plt.figure()
plt.subplot(2, 1, 1)
plt.plot(t, p)
plt.title('Solution')
plt.ylabel('Price')

plt.subplot(2, 1, 2)
示例#20
0
    L = BasisSpline(n, a, b, k=1, f=ff)

    # Compute actual and fitted values on grid
    y = ff(x)  # actual
    yc = C(x)  # Chebychev approximant
    ys = S(x)  # cubic spline approximant
    yl = L(x)  # linear spline approximant

    # Plot function approximations
    plt.figure()
    ymin = np.floor(y.min())
    ymax = np.ceil(y.max())
    xlim = [a, b]
    ylim = [-0.2, 1.2] if ifunc==2 else [ymin, ymax]

    subfig(1, x, y, xlim, ylim, 'Function')
    subfig(2, x, yc, xlim, ylim, 'Chebyshev')
    subfig(3, x, ys, xlim, ylim, 'Cubic Spline')
    subfig(4, x, yl, xlim, ylim, 'Linear Spline')

    # Plot function approximation error
    plt.figure()
    plt.plot(x, np.c_[yc - y, ys - y], linewidth=3)
    plt.xlabel('x')
    plt.ylabel('Approximation Error')
    plt.legend(['Chebychev Polynomial','Cubic Spline'])
    # pltlegend('boxoff')

plt.show()

示例#21
0
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
demo.figure('Bellman Equation Residual', 'Wealth', 'Residual')
plt.plot(s, resid.T)
plt.hlines(0, smin, smax, 'k', '--')
示例#22
0
# Intialize Value Function
# c = zeros(n,1)                         # conditional value function basis coefficients

# Solve Bellman Equation and Compute Critical Exercise Prices
f = NLP(lambda p: K - np.exp(p) - delta * Value(p))

pcrit = np.empty(N + 1)

pcrit[0] = f.zero(0.0)

for t in range(N):
    v = np.zeros((1, n))
    for k in range(m):
        pnext = Value.nodes + e[k]
        v += w[k] * np.maximum(K - np.exp(pnext), delta * Value(pnext))

    Value[:] = v
    pcrit[t + 1] = f.broyden(pcrit[t])

# Print Critical Exercise Price 300 Periods to Expiration

print('Critical Exercise Price 300 Periods to Expiration')
print('   Critical Price  = {:5.2f}'.format(np.exp(pcrit[-1])))

# Plot Critical Exercise Prices
demo.figure('American Put Option Optimal Exercise Boundary',
            'Periods Remaining Until Expiration', 'Exercise Price')
plt.plot(np.exp(pcrit))
plt.show()
示例#23
0

# State Transition Function
g = np.empty_like(f)
for k in range(m):
    snext = gamma * X[k] * S + (X[k] * S) ** beta
    g[k] = getindex(snext, S)

# Model Structure
model = DDPmodel(f, g, delta).solve()

## Analysis

# Plot Optimal Policy
demo.figure('Optimal Investment', 'Wealth', 'Investment')
plt.plot(S, X[model.policy] * S)

# Plot Optimal Policy
demo.figure('Optimal Consumption', 'Wealth', 'Consumption')
plt.plot(S, S - X[model.policy] * S)

# Plot Value Function
demo.figure('Optimal Value Function', 'Wealth', 'Value')
plt.plot(S, model.value)


# Simulate Model
nyrs = 20
t = np.arange(0, nyrs + 1)
st, xt = model.simulate(smin, nyrs)
示例#24
0
model = DDPmodel(f, P, delta).solve(print=True)

## Analysis

# Display Optimal Policy
xtemp = model.policy.reshape((n1, n2))
header = '{:^8s} {:^8s}  {:^8s}  {:^8s}'.format('Age', 'Lo', 'Med', 'Hi')

print('Optimal Policy')
print(header)
print(*('{:^8d} {:^8s}  {:^8s}  {:^8s}\n'.format(s, *X[x])
        for s, x in zip(s1, xtemp)))

# Plot Value Function
demo.figure('Optimal Replacement Value', 'Age', 'Optimal Value (thousands)')
plt.plot(s1, model.value.reshape((n1, n2)) / 1000)
plt.legend(['Low', 'Med', 'Hi'])

# Compute Steady-State distribution
pi = model.markov().reshape((n1, n2))

# Display Steady-State distribution
print('          Invariant Distribution     ')
print(header)
print(*('{:^8d} {:8.3f}  {:8.3f}  {:8.3f}\n'.format(s, *x)
        for s, x in zip(s1, pi)))

# Compute Steady-State Mean Cow Age and Productivity
pi = pi.flatten()
avgage = np.dot(pi.T, S1)
avgpri = np.dot(pi.T, S2)
示例#25
0
X = ['Keep', 'Replace']      # keep or replace
m = len(X)                   # number of actions

# Reward Function
f = np.empty((m, n))
y = -0.2 * S ** 2 + 2 * S + 8  # yield per lactation
f[0] = price * y
f[1] = f[0] - cost
f[0, -1] = -np.inf               # force replace at lactation 10

# State Transition Function
g = np.ones_like(f)
g[0] = np.minimum(np.arange(n) + 1, n - 1)  # Raise lactation number by 1, if keep


# Model Structure

model = DDPmodel(f, g, delta)
model.solve()

# Plot Optimal Policy
demo.figure('Optimal Replacement', 'Age', 'Optimal Decision', [0, n + 1], [-0.5, 1.5])
plt.plot(S, model.policy, '*', markersize=15)
plt.yticks((0, 1), X)

# Plot Value Function
demo.figure('Optimal Value in Cow Replacement', 'Age', 'Value (thousands)')
plt.plot(S, model.value / 1000)

plt.show()
示例#26
0
for r, x in enumerate(X):
    snext = S - x
    g[r] = getindex(snext, S)


# Model Structure
model = DDPmodel(f, g, delta)
model.solve()

# Analysis

# Simulate Model
sinit = S.max()
nyrs = 15
t = np.arange(nyrs + 1)
spath, xpath = model.simulate(sinit, nyrs)

# Plot Optimal Policy
demo.figure('Optimal Extraction Policy', 'Stock', 'Extraction')
plt.plot(S, X[model.policy])

# Plot Value Function
demo.figure('Optimal Value Function', 'Stock', 'Value')
plt.plot(S, model.value)

# Plot State Path
demo.figure('Optimal State Path', 'Year', 'Stock')
plt.plot(t, S[spath])

plt.show()
示例#27
0
# Reward Function
f = np.zeros((m, n))
f[0] = 50 - 2.5 * S - 2.5 * S**2
f[1] = 50 - repcost
f[0, -1] = -np.inf

# State Transition Function
g = np.zeros_like(f)
g[0] = np.arange(1, n + 1)
g[0, -1] = n - 1  # adjust last state so it doesn't go out of bounds

# Model Structure
model = DDPmodel(f, g, delta)
model.solve()

## Analysis

# Simulate Model
sinit, nyrs = S.min() - 1, 12
t = np.arange(1 + nyrs)
spath, xpath = model.simulate(sinit, nyrs)

# Plot Optimal Value
demo.figure('Optimal Value Function', 'Age of Machine', 'Value')
plt.plot(S, model.value)

# Plot State Path
demo.figure('Optimal State Path', 'Year', 'Age of Machine', [0, 12])
plt.plot(t, S[spath])

plt.show()
示例#28
0
    f[k] = ((S * (1 - X[k]))**(1 - alpha)) / (1 - alpha)

# State Transition Function
g = np.empty_like(f)
for k in range(m):
    snext = gamma * X[k] * S + (X[k] * S)**beta
    g[k] = getindex(snext, S)

# Model Structure
model = DDPmodel(f, g, delta).solve()

## Analysis

# Plot Optimal Policy
demo.figure('Optimal Investment', 'Wealth', 'Investment')
plt.plot(S, X[model.policy] * S)

# Plot Optimal Policy
demo.figure('Optimal Consumption', 'Wealth', 'Consumption')
plt.plot(S, S - X[model.policy] * S)

# Plot Value Function
demo.figure('Optimal Value Function', 'Wealth', 'Value')
plt.plot(S, model.value)

# Simulate Model
nyrs = 20
t = np.arange(0, nyrs + 1)
st, xt = model.simulate(smin, nyrs)

# Plot State Path
示例#29
0
# Compute Function Metrics
a, b = 0, 2
f = lambda x: x**3 + x**2 + 1
g = lambda x: x**3 + 2
p1, p2 = 1, 2
q1 = quad(lambda x: np.abs(f(x)-g(x)) ** p1, a, b)[0] ** (1 / p1)
q2 = quad(lambda x: np.abs(f(x)-g(x)) ** p2, a, b)[0] ** (1 / p2)
print('\nCompute function metrics')
print('\tnorm 1 = {:6.4f},   norm 2 = {:6.4f}'.format(q1, q2))

# Illustrate function metrics
x = np.linspace(a, b, 200)
plt.figure(figsize=[12, 4])
plt.subplot(1, 2, 1)
plt.plot([0, 2], [0, 0], 'k:', linewidth=4)
plt.plot(x, f(x) - g(x), 'b', linewidth=4, label='f - g')
plt.xlabel('x')
plt.ylabel('y')
plt.xticks([0, 1, 2])
plt.yticks([-1, 0, 1, 2, 3])
plt.title('f - g')

plt.subplot(1, 2, 2)
plt.plot(x, np.abs(f(x) - g(x)), 'b', linewidth=4, label='f - g')
plt.xlabel('x')
plt.ylabel('y')
plt.xticks([0, 1, 2])
plt.yticks([0, 1, 2, 3])
plt.title('|f - g|')
示例#30
0
# 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
demo.figure('Bellman Equation Residual', 'Reservoir Level', 'Residual')
plt.plot(s, resid.T)
plt.hlines(0, smin, smax, 'k', '--')

## SIMULATION
示例#31
0
# ... and one may even evaluate the approximant's definite integral between the left endpoint a and x:
int = F(x, -1)
print('\nThe approximate integral of exp(-x) between x=-1 and x=0 is {:.15f}'.
      format(int))
print("The  'exact'  integral  of  exp(-x) between x=-1 and x=0 is {:.15f}".
      format(np.exp(1) - 1))

# One may evaluate the accuracy of the Chebychev polynomial approximant by
# computing the approximation error on a highly refined grid of points:
ngrid = 5001  # number of grid nodes
xgrid = np.linspace(a, b, ngrid)  # generate refined grid for plotting
yapp = F(xgrid)  # approximant values at grid nodes
yact = f(xgrid)  # actual function values at grid points

demo.figure('Chebychev Approximation Error for exp(-x)', 'x', 'Error')
plt.plot(xgrid, yapp - yact)
plt.plot(xgrid, np.zeros(ngrid), 'k--', linewidth=2)

# The plot indicates that an order 10 Chebychev approximation scheme, produces approximation errors
# no bigger in magnitude than 6x10^-10. The approximation error exhibits the "Chebychev equioscillation
# property", oscilating relatively uniformly throughout the approximation domain.
#
# This commonly occurs when function being approximated is very smooth, as is the case here but should not
# be expected when the function is not smooth.  Further notice how the approximation error is exactly 0 at the
# approximation nodes --- which is true by contruction.

# Let us repeat the approximation exercise, this time constructing a
# 21-function cubic spline approximant:
n = 21  # order of approximation
S = BasisSpline(n, a, b, f=f)  # define basis
yapp = S(xgrid)  # approximant values at grid nodes
示例#32
0
for k in range(m):
    snext = alpha * (S - X[k]) - 0.5 * beta * (S - X[k]) ** 2
    g[k] = getindex(snext, S)



# Model Structure
model = DDPmodel(f, g, delta)
model.solve()
   

## Analysis

# Plot Optimal Policy
demo.figure('Optimal Harvest Policy', 'Stock', 'Harvest')
plt.plot(S,X[model.policy])


# Plot Value Function
demo.figure('Optimal Value Function', 'Stock', 'Value')
plt.plot(S,model.value)


# Simulate Model
nyrs = 20
t = np.arange(nyrs + 1)
spath, xpath = model.simulate(n - 1, nyrs)

# Plot State Path
demo.figure('Optimal State Path', 'Year', 'Stock')
plt.plot(t, S[spath])
示例#33
0
        if abs(i - j) > 1:
            AA[i,j] = 0

n = np.hstack((np.arange(50, 250, 50), np.arange(300, 1100, 100)))
ratio = np.empty(n.size)
for k in range(n.size):
    A = AA[:n[k], :n[k]]
    b = bb[:n[k]]
    tt = tic()
    for i in range(100):
        x = solve(A, b)

    toc1 = toc(tt)

    S = csc_matrix(A)
    tt = tic()
    for i in range(100):
        x = spsolve(S, b)

    toc2 = toc(tt)
    ratio[k] = toc2 / toc1

# Plot effort ratio
plt.figure(figsize=[6, 6])
plt.plot(n, ratio)
plt.xlabel('n')
plt.ylabel('Ratio')
plt.title('Ratio of Sparse Solve Time to Full Solve Time')
plt.show()

示例#34
0
# 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
demo.figure('Bellman Equation Residual', 'Reservoir Level', 'Residual')
plt.plot(s,resid.T)
plt.hlines(0, smin, smax, 'k', '--')

示例#35
0
# Construct and evaluate Chebychev interpolant
C = BasisChebyshev(n, a, b, f=f)  # chose basis functions
yc = C(x)  # values
dc = C(x, 1)  # first derivative
sc = C(x, 2)  # second derivative

# Construct and evaluate cubic spline interpolant
S = BasisSpline(n, a, b, f=f)  # chose basis functions
ys = S(x)  # values
ds = S(x, 1)  # first derivative
ss = S(x, 2)  # second derivative

# Plot function approximation error
plt.figure()
plt.subplot(2, 1, 1),
plt.plot(x, y - yc[0])
plt.ylabel('Chebychev')
plt.title('Function Approximation Error')

plt.subplot(2, 1, 2)
plt.plot(x, y - ys[0])
plt.ylabel('Cubic Spline')
plt.xlabel('x')

# Plot first derivative approximation error
plt.figure()
plt.subplot(2, 1, 1),
plt.plot(x, d - dc[0])
plt.ylabel('Chebychev')
plt.title('First Derivative Approximation Error')
示例#36
0
def subfig(k, x, y, xlim, ylim, title):
    plt.subplot(2, 2, k)
    plt.plot(x, y)
    plt.xlim(xlim)
    plt.ylim(ylim)
    plt.title(title)
示例#37
0
m = len(X)  # number of actions

# Reward Function
f = np.empty((m, n))
y = -0.2 * S**2 + 2 * S + 8  # yield per lactation
f[0] = price * y
f[1] = f[0] - cost
f[0, -1] = -np.inf  # force replace at lactation 10

# State Transition Function
g = np.ones_like(f)
g[0] = np.minimum(np.arange(n) + 1,
                  n - 1)  # Raise lactation number by 1, if keep

# Model Structure

model = DDPmodel(f, g, delta)
model.solve()

# Plot Optimal Policy
demo.figure('Optimal Replacement', 'Age', 'Optimal Decision', [0, n + 1],
            [-0.5, 1.5])
plt.plot(S, model.policy, '*', markersize=15)
plt.yticks((0, 1), X)

# Plot Value Function
demo.figure('Optimal Value in Cow Replacement', 'Age', 'Value (thousands)')
plt.plot(S, model.value / 1000)

plt.show()
示例#38
0
# State Transition Function

g = np.zeros_like(f)
for k in range(m):
    snext = alpha * (S - X[k]) - 0.5 * beta * (S - X[k])**2
    g[k] = getindex(snext, S)

# Model Structure
model = DDPmodel(f, g, delta)
model.solve()

## Analysis

# Plot Optimal Policy
demo.figure('Optimal Harvest Policy', 'Stock', 'Harvest')
plt.plot(S, X[model.policy])

# Plot Value Function
demo.figure('Optimal Value Function', 'Stock', 'Value')
plt.plot(S, model.value)

# Simulate Model
nyrs = 20
t = np.arange(nyrs + 1)
spath, xpath = model.simulate(n - 1, nyrs)

# Plot State Path
demo.figure('Optimal State Path', 'Year', 'Stock')
plt.plot(t, S[spath])

# Plot Optimal Transition Function
示例#39
0

''' Compute root using Broyden method '''
t0 = tic()
x2 = problem.broyden()
t2 = 100 * toc(t0)
n2, x_broyden = problem.fnorm, problem.x_sequence


''' Print results '''
print('Hundredths of seconds required to compute root of exp(-x)-1,')
print('via Newton and Broyden methods, starting at x = %4.2f.' % problem.x0)
print('\nMethod      Time   Norm of f   Final x')
print('Newton  %8.2f    %8.0e     %5.2f' % (t1, n1, x1))
print('Broyden %8.2f    %8.0e     %5.2f' % (t2, n2, x2))

''' View current options for solver '''
print(problem.opts)

''' Describe the options '''
print(problem.opts.__doc__)

''' Plot the convergence '''
b = -abs(problem.x0)
a = -b
xx = np.linspace(a, b, 100)

fig = plt.figure()
plt.plot(xx, f(xx)[0], 'b-')
plt.plot(x_newton,f(x_newton)[0],'ro:')
plt.show()
示例#40
0
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
demo.figure('Bellman Equation Residual', 'Wealth', 'Residual')
plt.plot(s, resid.T)
plt.hlines(0, smin, smax, 'k', '--')

## SIMULATION
示例#41
0
f = np.zeros((m, n))
f[0] = 50 - 2.5 * S - 2.5 * S ** 2
f[1] = 50 - repcost
f[0, -1] = -np.inf

# State Transition Function
g = np.zeros_like(f)
g[0] = np.arange(1, n + 1)
g[0, -1] = n - 1  # adjust last state so it doesn't go out of bounds

# Model Structure
model = DDPmodel(f, g, delta)
model.solve()


## Analysis

# Simulate Model
sinit, nyrs = S.min() - 1, 12
t = np.arange(1 + nyrs)
spath, xpath = model.simulate(sinit, nyrs)

# Plot Optimal Value
demo.figure('Optimal Value Function', 'Age of Machine', 'Value')
plt.plot(S, model.value)

# Plot State Path
demo.figure('Optimal State Path', 'Year', 'Age of Machine', [0, 12])
plt.plot(t, S[spath])

plt.show()