from compecon import BasisSpline, NLP, qnwnorm, demo # ## FORMULATION # In what follows, we are going to solve the problem in term of the log-price, defining $p\equiv\log(P)$. We assume that log-price of the commodity follows a random walk: # \begin{equation} # p_{t+1} = p_t + \epsilon_{t+1} # \end{equation} # # where $\epsilon$ is a normal $(\mu, \sigma^2)$ shock. We discretize this distribution by using ```qnwnorm```, assuming that $\mu=0.0001,\quad\sigma=0.008$ and setting up $m=15$ nodes. # In[3]: mu, sigma = 0.0001, 0.0080 m = 15 [e, w] = qnwnorm(m, mu, sigma**2) # We are going to compute the critical exercise price in terms of the time to expiration, up to an horizon of $T=300$ periods. First we allocate memory for the critical prices: # In[4]: T = 300 pcrit = np.empty(T + 1) # The critical exercise price is the price at which the value of exercising the option $K-\exp(p)$ equals the discounted expected value of keeping the option one more period $\delta E_\epsilon V(p + \epsilon)$. To find it, we set it as a nonlinear rootfinding problem by using the ```NLP``` class; here we assume that the option strike price is $K=1$ and that the discount factor is $\delta=0.9998$ # In[5]: K = 1.0 delta = 0.9998 f = NLP(lambda p: K - np.exp(p) - delta * Value(p))
# ## FORMULATION # In what follows, we are going to solve the problem in term of the log-price, defining $p\equiv\log(P)$. We assume that log-price of the commodity follows a random walk: # \begin{equation} # p_{t+1} = p_t + \epsilon_{t+1} # \end{equation} # # where $\epsilon$ is a normal $(\mu, \sigma^2)$ shock. We discretize this distribution by using ```qnwnorm```, assuming that $\mu=0.0001,\quad\sigma=0.008$ and setting up $m=15$ nodes. # In[3]: mu, sigma = 0.0001, 0.0080 m = 15 [e,w] = qnwnorm(m,mu,sigma ** 2) # We are going to compute the critical exercise price in terms of the time to expiration, up to an horizon of $T=300$ periods. First we allocate memory for the critical prices: # In[4]: T = 300 pcrit = np.empty(T + 1) # The critical exercise price is the price at which the value of exercising the option $K-\exp(p)$ equals the discounted expected value of keeping the option one more period $\delta E_\epsilon V(p + \epsilon)$. To find it, we set it as a nonlinear rootfinding problem by using the ```NLP``` class; here we assume that the option strike price is $K=1$ and that the discount factor is $\delta=0.9998$ # In[5]:
wbar = 100 gamma = 0.40 def transition(w, x, i, j, in_, e): return wbar + gamma * (w - wbar) + e # Here, $\epsilon$ is normal $(0,\sigma^2)$ wage shock, where $\sigma=5$. We discretize this distribution with the function ```qnwnorm```. # In[5]: sigma = 5 m = 15 e, w = qnwnorm(m, 0, sigma**2) # ### Approximation Structure # # To discretize the continuous state variable, we use a cubic spline basis with $n=150$ nodes between $w_\min=0$ and $w_\max=200$. # In[6]: n = 150 wmin = 0 wmax = 200 basis = BasisSpline(n, wmin, wmax, labels=['wage']) # ## SOLUTION # # To represent the model, we create an instance of ```DPmodel```. Here, we assume a discout factor of $\delta=0.95$.
f = np.log(sk) fx= - sk ** -1 fxx = - sk ** -2 return f, fx, fxx def transition(s, k, i, j, in_, e): g = k ** beta gx = beta * k **(beta - 1) gxx = (beta - 1) * beta * k ** (beta - 2) return g, gx, gxx sigma = 0.1 m=5 e,w = qnwnorm(m, -sigma**2 / 2, sigma**2) growth_model = DPmodel(basis, reward, transition, bounds, x=['Investment'], discount=delta) # ======== Steady-State sstar = (beta * delta) ** (beta / (1 - beta)) # steady-state wealth kstar = beta * delta * sstar # steady-state capital investment vstar = np.log(sstar - kstar) / (1 - delta) # steady-state value pstar = 1 / (sstar * (1 - beta * delta)) # steady-state shadow price b = 1 / (1 - delta * beta)
# where $\epsilon \sim N(0,\sigma^2)$. # In[7]: def transition(p, x, i, j, in_, e): return pbar + gamma * (p - pbar) + e # The continuous shock must be discretized. Here we use Gauss-Legendre quadrature to obtain nodes and weights defining a discrete distribution that matches the first 10 moments of the Normal distribution (this is achieved with $m=5$ nodes and weights). # In[8]: m = 5 e, w = qnwnorm(m,0,sigma ** 2) # On the other hand, the age of the asset is deterministic: if it is replaced now it will be new ($a=0$) next period; otherwise its age will be $a+1$ next period if current age is $a$. # In[9]: h = np.zeros((2, A),int) h[0, :-1] = np.arange(1, A) # ## Model Structure # In[10]:
pbar = 1.0 gamma = 0.7 def transition(p, x, d, j, in_, e): return pbar + gamma * (p - pbar) + e # In the transition function $\epsilon_t$ is an i.i.d. normal(0, $σ^2$), with $\sigma=1$. We discretize this distribution by using a discrete distribution, matching the first 10 moments of the normal distribution. # In[4]: m = 5 # number of profit shocks sigma = 1.0 [e,w] = qnwnorm(m,0,sigma **2) # The collocation method calls for the analyst to select $n$ basis functions $\varphi_j$ and $n$ collocation nodes $(\pi_i,d_i)$, and form the value function approximant $V(\pi,d) \approx \sum_{j=1}^{n} c_j\varphi_j(\pi,d)$ whose coefficients $c_j$ solve the collocation equation # # \begin{equation} # \sum_{j=1}^{n} c_j\varphi_j(\pi_i,d_i) = \max_{x\in\{0,1\}}\left\{\pi_i x − K_1(1−d_i)x − K_0 d_i(1−x) + \delta\sum_{k=1}^{m}\sum_{j=1}^{n}w_k c_j \varphi_j(\hat{\pi}_{ik},x)\right\} # \end{equation} # # where $\hat\pi_{ik}=g(\pi_i,\epsilon_k)$ and where $\epsilon_k$ and $w_k$ represent quadrature nodes and weights for # the normal shock. # # For the approximation, we use a cubic spline basis with $n=250$ nodes between $p_{\min}=-20$ and $p_\max=20$. # In[5]:
pbar = 1.0 gamma = 0.7 def transition(p, x, d, j, in_, e): return pbar + gamma * (p - pbar) + e # In the transition function $\epsilon_t$ is an i.i.d. normal(0, $σ^2$), with $\sigma=1$. We discretize this distribution by using a discrete distribution, matching the first 10 moments of the normal distribution. # In[4]: m = 5 # number of profit shocks sigma = 1.0 [e, w] = qnwnorm(m, 0, sigma**2) # The collocation method calls for the analyst to select $n$ basis functions $\varphi_j$ and $n$ collocation nodes $(\pi_i,d_i)$, and form the value function approximant $V(\pi,d) \approx \sum_{j=1}^{n} c_j\varphi_j(\pi,d)$ whose coefficients $c_j$ solve the collocation equation # # \begin{equation} # \sum_{j=1}^{n} c_j\varphi_j(\pi_i,d_i) = \max_{x\in\{0,1\}}\left\{\pi_i x − K_1(1−d_i)x − K_0 d_i(1−x) + \delta\sum_{k=1}^{m}\sum_{j=1}^{n}w_k c_j \varphi_j(\hat{\pi}_{ik},x)\right\} # \end{equation} # # where $\hat\pi_{ik}=g(\pi_i,\epsilon_k)$ and where $\epsilon_k$ and $w_k$ represent quadrature nodes and weights for # the normal shock. # # For the approximation, we use a cubic spline basis with $n=250$ nodes between $p_{\min}=-20$ and $p_\max=20$. # In[5]: n = 250