Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _multivariate_ls_fit(self):
wy, wx, wxhat = self._wy, self._wx, self._wxhat
k = len(wxhat)
xpx = blocked_inner_prod(wxhat, np.eye(len(wxhat)))
xpy = []
for i in range(k):
xpy.append(wxhat[i].T @ wy[i])
xpy = np.vstack(xpy)
beta = _parameters_from_xprod(xpx, xpy, constraints=self.constraints)
loc = 0
eps = []
for i in range(k):
nb = wx[i].shape[1]
b = beta[loc:loc + nb]
eps.append(wy[i] - wx[i] @ b)
loc += nb
eps = np.hstack(eps)
return beta, eps
def _mvreg_cov(self):
x = self._x
xeex = blocked_inner_prod(x, self._sigma)
xpx = blocked_inner_prod(self._x, eye(len(x)))
if self._constraints is None:
xpxi = inv(xpx)
cov = xpxi @ xeex @ xpxi
else:
cons = self._constraints
xpx = cons.t.T @ xpx @ cons.t
xpxi = inv(xpx)
xeex = cons.t.T @ xeex @ cons.t
cov = cons.t @ (xpxi @ xeex @ xpxi) @ cons.t.T
cov = (cov + cov.T) / 2
return cov
x : ndarray
List of containing model regressors for each equation in the system
z : ndarray
List of containing instruments for each equation in the system
eps : ndarray
Model errors (nobs by neqn)
sigma : ndarray
Fixed covariance of model errors
Returns
-------
weight : ndarray
Covariance of GMM moment conditions.
"""
nobs = z[0].shape[0]
w = blocked_inner_prod(z, sigma) / nobs
return w
def _mvreg_cov(self):
x = self._x
xeex = blocked_inner_prod(x, self._sigma)
xpx = blocked_inner_prod(self._x, eye(len(x)))
if self._constraints is None:
xpxi = inv(xpx)
cov = xpxi @ xeex @ xpxi
else:
cons = self._constraints
xpx = cons.t.T @ xpx @ cons.t
xpxi = inv(xpx)
xeex = cons.t.T @ xeex @ cons.t
cov = cons.t @ (xpxi @ xeex @ xpxi) @ cons.t.T
cov = (cov + cov.T) / 2
return cov
def _omega(self):
z = self._z
nobs = z[0].shape[0]
sigma = self._sigma
omega = blocked_inner_prod(z, sigma)
omega /= nobs
return omega
def _gls_estimate(self, eps, nobs, total_cols, ci, full_cov, debiased):
"""Core estimation routine for iterative GLS"""
wy, wx, wxhat = self._wy, self._wx, self._wxhat
sigma = self._sigma
if sigma is None:
sigma = eps.T @ eps / nobs
sigma *= self._sigma_scale(debiased)
if not full_cov:
sigma = np.diag(np.diag(sigma))
sigma_inv = inv(sigma)
k = len(wy)
xpx = blocked_inner_prod(wxhat, sigma_inv)
xpy = np.zeros((total_cols, 1))
for i in range(k):
sy = np.zeros((nobs, 1))
for j in range(k):
sy += sigma_inv[i, j] * wy[j]
xpy[ci[i]:ci[i + 1]] = wxhat[i].T @ sy
beta = _parameters_from_xprod(xpx, xpy, constraints=self.constraints)
loc = 0
for j in range(k):
_wx = wx[j]
_wy = wy[j]
kx = _wx.shape[1]
eps[:, [j]] = _wy - _wx @ beta[loc:loc + kx]
loc += kx
def _gls_cov(self):
x = self._x
sigma = self._sigma
sigma_inv = inv(sigma)
xpx = blocked_inner_prod(x, sigma_inv)
# Handles case where sigma_inv is not inverse of full_sigma
xeex = blocked_inner_prod(x, sigma_inv @ self._full_sigma @ sigma_inv)
if self._constraints is None:
xpxi = inv(xpx)
cov = xpxi @ xeex @ xpxi
else:
cons = self._constraints
xpx = cons.t.T @ xpx @ cons.t
xpxi = inv(xpx)
xeex = cons.t.T @ xeex @ cons.t
cov = cons.t @ (xpxi @ xeex @ xpxi) @ cons.t.T
cov = (cov + cov.T) / 2
return cov
def _cov(self, gls):
x = self._x
nobs = x[0].shape[0]
k = len(x)
sigma = self.sigma
weights = inv(sigma) if gls else eye(k)
xpx = blocked_inner_prod(x, weights) / nobs
xeex = self._xeex()
if self._constraints is None:
xpxi = inv(xpx)
cov = xpxi @ xeex @ xpxi
else:
cons = self._constraints
xpx = cons.t.T @ xpx @ cons.t
xpxi = inv(xpx)
xeex = cons.t.T @ xeex @ cons.t
cov = cons.t @ (xpxi @ xeex @ xpxi) @ cons.t.T
cov = (cov + cov.T) / 2
return cov / nobs