import numpy as np
Linear regression
Consider the following linear equation:
Assume you have access to data of the independent variables (, ) and the dependent variable () for individuals, where indexes individuals. The variable is a mean-zero stochastic shock.
Assume the data generating process is given by:
def DGP(N):
""" data generating process
Args:
N (int): number of observations
Returns:
x1 (ndarray): independent variable x1
x2 (ndarray): independent variable x2
y (ndarray): dependent varialbe y
"""
# a. independent variables
x1 = np.random.normal(0,1,size=N)
x2 = np.random.normal(0,1,size=N)
# b. errors
eps = np.random.normal(0,1,size=N)
extreme = np.random.uniform(0,1,size=N)
eps[extreme < 0.05] += np.random.normal(-5,1,size=N)[extreme < 0.05]
eps[extreme > 0.95] += np.random.normal(5,1,size=N)[extreme > 0.95]
# c. dependent variable
y = 0.1 + 0.3*x1 + 0.5*x2 + eps
return x1, x2, y
The data you have access to is:
np.random.seed(2020)
x1,x2,y = DGP(10000)
Question 1: Estimate the vector of coefficients using ordinary least squares (OLS) implemented with matrix algebra by
where is the transpose of and
Question 2: Construct a 3D plot, where the data is plotted as scattered points, and the prediction of the model is given by the plane
Question 3: Esimtate the vector of coefficients using a numerical solver to solve the ordinary least square problem, shown below, directly. Compare your results with the matrix algebra results.
Question 4: Estimate the vector of coefficients using least absolute deviations (LAD) using a numerical solver to solve the following problem directly:
where is the absolute value of .
Question 5: Set . Repeat the estimation using the OLS and LAD methods times, drawing a new random sample from the data generating process each time. Compare the estimates from each method using histograms. Which method do you prefer? Explain your choice.
Durable purchases
Consider a household living in two periods.
In the second period it gets utility from non-durable consumption, , and durable consumption, :
where
- is cash-on-hand in the beginning of period 2
- is non-durable consumption
- is pre-commited durable consumption
- is extra durable consumption
- is the risk aversion coefficient
- is the utility weight on non-durable consumption
- implies that extra durable consumption is less valuable than pre-comitted durable consumption
- the second constraint ensures the household cannot die in debt
The value function measures the household's value of having at the beginning of period 2 with precomitted durable consumption of . The optimal choice of non-durable consumption is denoted . The optimal extra durable consumption function is .
Define the so-called end-of-period 1 value function as:
where
and
- is assets at the end of period 1
- is the discount factor
- is the expectation operator conditional on information in period 1
- is income in period 2
- is the level of income risk (mean-preserving)
- is the return on savings
In the first period, the household chooses it's pre-comitted level of durable consumption for the next-period,
where is cash-on-hand in period 1. The second constraint ensures the household cannot borrow. The value function measures the household's value of having at the beginning of period 1. The optimal choice of pre-committed durable consumption is denoted .
The parameters and grids for , and should be:
# a. parameters
rho = 2
alpha = 0.8
chi = 0.9
beta = 0.96
r = 0.04
Delta = 0.25
# b. grids
m1_vec = np.linspace(1e-8,10,100)
m2_vec = np.linspace(1e-8,10,100)
d_vec = np.linspace(1e-8,5,100)
Question 1: Find and plot the functions , , and . Comment.
Question 2: Find and plot the functions and . Comment.
Hint: For interpolation of consider using interpolate.RegularGridInterpolator([GRID-VECTOR1,GRID-VECTOR2],VALUE-MATRIX,bounds_error=False,fill_value=None)
.
Next, consider an extension of the model, where there is also a period 0. In this period, the household makes a choice whether to stick with the level of durables it has, , or adjust its stock of durables, . If adjusting, the household loses a part of the value of its durable stock; more specificaly it incurs a proportional loss of .
Mathematically, the household problem in period 0 is:
The parameters and grids for and should be:
Lambda = 0.2
m0_vec = np.linspace(1e-8,6,100)
d0_vec = np.linspace(1e-8,3,100)
Question 3: For which values of and is the optimal choice not to adjust, i.e. ? Show this in a plot. Give an interpretion of your results.
Gradient descent
Let be a two-dimensional vector. Consider the following algorithm:
Algorithm: gradient_descent()
Goal: Minimize the function .
- Choose a tolerance , a scale factor , and a small number
- Guess on and set
Compute a numerical approximation of the jacobian for by
Stop if the maximum element in is less than
- Set
- Compute
- If continue to step 9
- Set and return to step 6
- Set
- Set and return to step 3
Question: Implement the algorithm above such that the code below can run.
Optimizer function:
def gradient_descent(f,x0,epsilon=1e-6,Theta=0.1,Delta=1e-8,max_iter=10_000):
pass
Test case:
def rosen(x):
return (1.0-x[0])**2+2*(x[1]-x[0]**2)**2
x0 = np.array([1.1,1.1])
try:
x,it = gradient_descent(rosen,x0)
print(f'minimum found at ({x[0]:.4f},{x[1]:.4f}) after {it} iterations')
assert np.allclose(x,[1,1])
except:
print('not implemented yet')
not implemented yet