<jupyter_start><jupyter_text>Deterministic Nonrenewable Resource Model**<NAME>, PhD**This demo is based on the original Matlab demo accompanying the  Computational Economics and Finance 2001 textbook by <NAME> and <NAME>.Original (Matlab) CompEcon file: **demdoc03.m**Running this file requires the Python version of CompEcon. This can be installed with pip by running    !pip install compecon --upgradeLast updated: 2021-Oct-01  AboutWelfare maximizing social planner must decide the rate at which a nonrenewable resource should be harvested.* State  - s     resource stock* Control  - q     harvest rate* Parameters  - κ     harvest unit cost scale factor  - γ     harvest unit cost elasticity  - η     inverse elasticity of demand  - 𝜌     continuous discount rate Preliminary tasks Import relevant packages<jupyter_code>import pandas as pd
import matplotlib.pyplot as plt
from compecon import BasisChebyshev, OCmodel<jupyter_output><empty_output><jupyter_text>Model parameters<jupyter_code>κ = 10     # harvest unit cost scale factor
γ = 1      # harvest unit cost elasticity
η = 1.5    # inverse elasticity of demand
𝜌 = 0.05   # continuous discount rate<jupyter_output><empty_output><jupyter_text>Approximation structure<jupyter_code>n = 20          # number of basis functions
smin = 0.1        # minimum state
smax = 1.0        # maximum state
basis = BasisChebyshev(n, smin, smax, labels=['q'])  # basis functions<jupyter_output><empty_output><jupyter_text>Solve HJB equation by collocation<jupyter_code>def control(s, Vs, κ, γ, η, 𝜌):
    k = κ * s**(-γ)
    return (Vs + k)**(-1/η)

def reward(s, q, κ, γ, η, 𝜌):
    u = (1/(1-η)) * q **(1-η)
    k = κ * s**(-γ)
    return u - k*q

def transition(s, q, κ, γ, η, 𝜌):
    return  -q

model = OCmodel(basis, control, reward, transition, rho=𝜌, params=[κ, γ, η, 𝜌])
data = model.solve()<jupyter_output>Solving optimal control model
iter change       time    
------------------------------
   0       5.4e+02    0.0030
   1       1.2e+02    0.0030
   2       3.2e+01    0.0030
   3       9.2e+00    0.0040
   4       1.0e+00    0.0040
   5       7.2e-03    0.0050
   6       1.6e-06    0.0050
   7       4.5e-09    0.0060
Elapsed Time =    0.01 Seconds<jupyter_text>Plots Optimal policy<jupyter_code>fig, ax = plt.subplots()
data['control'].plot(ax=ax)
ax.set(title='Optimal Harvest Policy',
       xlabel='Resource Stock',
       ylabel='Rate of Harvest',
       xlim=[smin, smax])
ax.set_ylim(bottom=0)<jupyter_output><empty_output><jupyter_text>Value function<jupyter_code>fig, ax = plt.subplots()
data['value'].plot(ax=ax)
ax.set(title='Value Function',
       xlabel='Resource Stock',
       ylabel='Social Welfare',
       xlim=[smin, smax])<jupyter_output><empty_output><jupyter_text>Shadow price<jupyter_code>data['shadow'] = model.Value(data.index, 1)

fig, ax = plt.subplots()
data['shadow'].plot(ax=ax)
ax.set(title='Shadow Price Function',
       xlabel='Resource Stock',
       ylabel='Shadow Price',
       xlim=[smin, smax])<jupyter_output><empty_output><jupyter_text>Residual<jupyter_code>fig, ax = plt.subplots()
data['resid'].plot(ax=ax)
ax.set(title='HJB Equation Residual',
       xlabel='Capital Stock',
       ylabel='Residual',
       xlim=[smin, smax]);<jupyter_output><empty_output><jupyter_text>Simulate the model  Initial state and time horizon<jupyter_code>s0 = smax  # initial capital stock
T  = 40    # time horizon<jupyter_output><empty_output><jupyter_text>Simulation and plot<jupyter_code>fig, ax = plt.subplots()
model.simulate([s0], T).plot(ax=ax)
ax.set(title='Simulated Resource Stock and Rate of Harvest',
       xlabel='Time',
       ylabel='Quantity',
       xlim=[0, T])

#ax.legend([]);<jupyter_output>PARAMETER xnames NO LONGER VALID. SET labels= AT OBJECT CREATION
{'fraction_non_alphanumeric': 0.12612851832182687, 'fraction_numerical': 0.03186404673393521, 'mean_word_length': 3.6506172839506172, 'pattern_counts': {'":': 0, '<': 42, '<?xml version=': 0, '>': 42, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '456902', 'license': 'MIT', 'n_tokens_mistral': 1424, 'n_tokens_neox': 1314, 'n_words': 343, 'path': '_build/jupyter_execute/notebooks/doc/03 Deterministic Nonrenewable Resource Model.ipynb', 'repo_name': 'randall-romero/CompEcon-python', 'size': '3795'}