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
demo.figure('Optimal State Path', 'Year', 'Wealth')
plt.plot(t, S[st])

# Compute Steady State Distribution and Mean
pi = model.markov()
avgstock = np.dot(S, pi)
print('Steady-state Wealth     {:8.2f},  {:8.2f}'.format(*avgstock))

plt.show()
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)
print('Steady-state Age          = {:8.2f}'.format(avgage))
print('Steady-state Productivity = {:8.2f}'.format(avgpri))

# 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)
print('Steady-state Age          = {:8.2f}'.format(avgage))
print('Steady-state Productivity = {:8.2f}'.format(avgpri))

plt.show()
## 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
demo.figure('Optimal State Path', 'Year', 'Wealth')
plt.plot(t, S[st])

# Compute Steady State Distribution and Mean
pi = model.markov()
avgstock = np.dot(S, pi)
print('Steady-state Wealth     {:8.2f},  {:8.2f}'.format(*avgstock))

plt.show()