Submit Link: https://classroom.github.com/a/PIR0phPT

Question 1

Recall that the factorial function \(n\) ! returns \(n !=n \times(n-1) \times \ldots 2 \times 1\). Write a function that executes this called factorial2 that uses a for loop.

Question 2

Consider the polynomial

\[ p(x)=a_{0}+a_{1} x+a_{2} x^{2}+\ldots a_{n} x^{n}=\sum_{i=0}^{n} a_{i} x_{i} \]

Using enumerate() in your loop, write a function \(p\) such that \(p(x, coeff)\) computes the value of the polynomial given a point \(x\) and an array of coefficients \(coeff\).

Question 3

Compute an approximation for \(\pi\) using Monte Carlo. You may only use rand() for random number generation. Hint: If \(U\) is a bivariate uniform random variable on the unit square \((0,1)^{2}\), then the probability that \(U\) lies in a subset \(B\) of \((0,1)^{2}\) is equal to the area of \(B\).

Question 4

Let the data generating process for \(y\) be

\[ y=a x_{1}+b x_{1}^{2}+c x_{2}+d+\sigma w \]

where \(y, x_{1}, x_{2}\) are scalars, \(a, b, c, d\) are parameters to estimate, and \(w \sim N(0,1)\) iid.

  • First, draw \(N=50\) values for \(x_{1}, x_{2}\) from iid normal distributions.
  • Then, draw a \(w\) vector for the \(N\) values to generate \(y\) for the simulated data. Use \(a=0.1, b=0.2, c=0.5, d=1.0\), and \(\sigma=0.1\). Repeat draws of \(w\) until you have 200 different simulations of the \(y\) values.
  • Finally, calculate ordinary least squares manually for each of the 200 simulations. Plot histograms for each parameter \(a, b, c, d\),.

Question 5

Take a random walk starting from \(x_{0}=1\) :

\[ x_{t+1}=\alpha x_{t}+\sigma \epsilon_{t+1}, \]

where \(t=0, \ldots t_{\max }\). Assume that \(x_{t_{\max }}=0\) with certainty and that \(\left\{\epsilon_{t}\right\}\) is drawn from an iid standard normal. Start with \(\sigma=0.2\) and \(\alpha=1.0\). For a given path \(\left\{x_{t}\right\}\) define a first-passage time as \(T_{a}=\min \left\{t \mid x_{t} \leq a\right\}\).

  • Calculate the first-passage time \(T_{0}\) for 100 simulated random walks (up to \(t_{\max }=200\) ) and plot a histogram.
  • Plot the sample mean of \(T_{0}\) from the simulation for \(\alpha \in\{0.8,1.0,1.2\}\).

Question 6

Recall that the root of a univariate function \(f(\cdot)\) is an \(x\) such that \(f(x)=0\). One solution method to find local roots of smooth functions is called Newton’s method. Starting with an \(x_{0}\) guess, a function \(f(\cdot)\) and a first derivative \(f^{\prime}(\cdot)\), the algorithm is to repeat

\[ x^{n+1}=x^{n}-\frac{f\left(x^{n}\right)}{f^{\prime}\left(x^{n}\right.} \]

until \(\left|x^{n+1}-x^{n}\right|\) is below some tolerance threshold. Code a function that implements Newton’s method. The function should accept arguments a function \(\mathrm{f}\), its derivative f_prime, a starting guess x_0, a tolerance tol, and a maximum number of iterations maxiter. Test this function with \(f(x)=(x-1)^{3}\) and another function of your choice where you can analytically find the derivative.

Question 7

We consider the capital investment problem of an infinitely lived household with log preferences over consumption. Production is given by \(Y_{t}=Z_{t} K_{t}^{\theta}\), where \(K_{t}\) is current capital, \(\theta=0.36\), and \(Z_{t} \in\left\{Z_{g}, Z_{b}\right\}\) is the period’s current productivity level. Assume that capital depreciates at rate \(\delta=0.025\), and households discount at rate \(\beta=0.99\). Assume further that the states of productivity are \(Z_{g}=1.25, Z_{b}=0.2\), and that transitions between the two states are given by a two-state Markov process:

\[ \Pi=\left[\begin{array}{ll} 0.977 & 0.023 \\ 0.074 & 0.926 \end{array}\right] \]

so, for instance, \(P\left(Z_{t+1}=Z_{g} \mid Z_{t}=Z_{g}\right)=0.977\). The household’s value function \(V(K, Z)\) is given by

\[ \begin{gathered} V(K, Z)=\max _{K^{\prime}}\left\{u(c)+\beta \sum_{Z^{\prime}} \Pi\left(Z^{\prime}, Z\right) V\left(K^{\prime}, Z^{\prime}\right) \right\} \\ c=Z K^{\theta}+(1-\delta) K-K^{\prime} . \end{gathered} \]

Set the capital grid to be from 0.01 to 45 with 1000 grid points.

A poorly-written version of this model exists in the Homework 1 repository. Update my code to include the stochastic transitions and to run faster than it currently does.