HSG-MCS-HS21_Julia/Problemsets/jlFiles/OlsNW.jl

113 lines
2.6 KiB
Julia
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#------------------------------------------------------------------------------
"""
OlsGMFn(Y,X)
LS of Y on X; for one dependent variable, Gauss-Markov assumptions
# Usage
(b,u,Yhat,V,R2) = OlsGMFn(Y,X)
# Input
- `Y::Vector`: T-vector, the dependent variable
- `X::Matrix`: Txk matrix of regressors (including deterministic ones)
# Output
- `b::Vector`: k-vector, regression coefficients
- `u::Vector`: T-vector, residuals Y - yhat
- `Yhat::Vector`: T-vector, fitted values X*b
- `V::Matrix`: kxk matrix, covariance matrix of b
- `R2::Number`: scalar, R2 value
"""
function OlsGMFn(Y,X)
T = size(Y,1)
b = X\Y
Yhat = X*b
u = Y - Yhat
σ2 = var(u)
V = inv(X'X)*σ2
R2 = 1 - σ2/var(Y)
return b, u, Yhat, V, R2
end
#------------------------------------------------------------------------------
#------------------------------------------------------------------------------
"""
OlsNWFn(Y,X,m=0)
LS of Y on X; for one dependent variable, using Newey-West covariance matrix
# Usage
(b,u,Yhat,V,R2) = OlsNWFn(Y,X,m)
# Input
- `Y::Array`: Tx1, the dependent variable
- `X::Array`: Txk matrix of regressors (including deterministic ones)
- `m::Int`: scalar, bandwidth in Newey-West
# Output
- `b::Array`: kx1, regression coefficients
- `u::Array`: Tx1, residuals Y - Yhat
- `Yhat::Vector`: Tx1, fitted values X*b
- `V::Array`: kxk matrix, covariance matrix of b
- `R2::Number`: scalar, R2 value
"""
function OlsNWFn(Y,X,m=0)
T = size(Y,1)
b = X\Y
Yhat = X*b
u = Y - Yhat
S = CovNWFn(X.*u,m) #Newey-West covariance matrix
Sxx = X'X
V = inv(Sxx)'S*inv(Sxx) #covariance matrix of b
R2 = 1 - var(u)/var(Y)
return b, u, Yhat, V, R2
end
#------------------------------------------------------------------------------
#------------------------------------------------------------------------------
"""
CovNWFn(g0,m=0)
Calculates covariance matrix of sample average.
# Input
- `g0::Matrix`: Txq Matrix of q moment conditions
- `m:int`: scalar, number of lags to use
# Output
- `S::Matrix`: qxq covariance matrix(average g0)
"""
function CovNWFn(g0,m=0)
T = size(g0,1) #g0 is Txq
m = min(m,T-1) #number of lags
g = g0 .- mean(g0,dims=1) #normalizing to zero means
S = g'g #(qxT)*(Txq)
for s = 1:m
Λ_s = g[s+1:T,:]'g[1:T-s,:] #same as Sum[g_t*g_{t-s}',t=s+1,T]
S = S + (1 - s/(m+1))*(Λ_s + Λ_s')
end
return S
end
#------------------------------------------------------------------------------