Exemplo n.º 1
0
def test_auto_bandwidth_smoke(data, kernel):
    # TODO: This should be improved from a smoke test
    u = data.e.copy()
    for i in range(1, u.shape[0]):
        u[i] = 0.8 * u[i - 1] + data.e[i]
    res = kernel_optimal_bandwidth(u, kernel.kernel)
    assert res > 0
Exemplo n.º 2
0
    def weight_matrix(self, x, z, eps):
        """
        Parameters
        ----------
        x : ndarray
            Model regressors (exog and endog), (nobs by nvar)
        z : ndarray
            Model instruments (exog and instruments), (nobs by ninstr)
        eps : ndarray
            Model errors (nobs by 1)

        Returns
        -------
        weight : ndarray
            Covariance of GMM moment conditions.
        """
        nobs, nvar = x.shape
        ze = z * eps
        mu = ze.mean(axis=0) if self._center else 0
        ze -= mu

        if self._orig_bandwidth is None and self._optimal_bw:
            g = ze / ze.std(0)[None, :]
            g = g.sum(1)
            self._bandwidth = kernel_optimal_bandwidth(g, self._kernel)
        elif self._orig_bandwidth is None:
            self._bandwidth = nobs - 2
        bw = self._bandwidth
        w = self._kernels[self._kernel](bw, nobs - 1)

        s = _cov_kernel(ze, w)
        s *= 1 if not self._debiased else nobs / (nobs - nvar)

        return s
Exemplo n.º 3
0
 def bandwidth(self):
     """Estimator bandwidth"""
     if self._bandwidth is None:
         e = self._all_params - self._params.T
         e = e[np.all(np.isfinite(e), 1)]
         stde = np.sum(e / e.std(0)[None, :], 1)
         self._bandwidth = kernel_optimal_bandwidth(stde, self._kernel)
     return self._bandwidth
Exemplo n.º 4
0
    def bandwidth(self):
        """Bandwidth used in estimation"""
        if self._bandwidth is None:
            moments = self._moments
            m = moments / moments.std(0)[None, :]
            m = m.sum(1)
            bw = kernel_optimal_bandwidth(m, kernel=self.kernel)
            self._bandwidth = int(bw)

        return self._bandwidth
Exemplo n.º 5
0
    def bandwidth(self):
        """Bandwidth used in estimation"""
        if self._bandwidth is None:
            xe = self._xe
            x = xe / xe.std(0)[None, :]
            x = x.sum(1)
            bw = kernel_optimal_bandwidth(x, kernel=self.kernel)
            self._bandwidth = int(bw)

        return self._bandwidth
Exemplo n.º 6
0
 def bandwidth(self) -> float:
     """Estimator bandwidth"""
     if self._bandwidth is None:
         all_params = np.asarray(self._all_params)
         e = all_params - self._params.T
         e = e[np.all(np.isfinite(e), 1)]
         stde = np.sum(e / e.std(0)[None, :], 1)
         self._bandwidth = kernel_optimal_bandwidth(stde, self._kernel)
     assert self._bandwidth is not None
     return self._bandwidth
Exemplo n.º 7
0
    def bandwidth(self) -> float:
        """Bandwidth used in estimation"""
        if self._bandwidth is None:
            assert self._moments is not None
            moments = self._moments
            m = moments / moments.std(0)[None, :]
            m = m.sum(1)
            bw = kernel_optimal_bandwidth(m, kernel=self.kernel)
            self._bandwidth = bw

        return self._bandwidth
Exemplo n.º 8
0
 def _optimal_bandwidth(self, moments):
     """Compute optimal bandwidth used in estimation if needed"""
     if self._predefined_bw is not None:
         return self._predefined_bw
     elif not self._optimal_bw:
         self._bandwidth = moments.shape[0] - 2
     else:
         m = moments / moments.std(0)[None, :]
         m = m.sum(1)
         self._bandwidth = kernel_optimal_bandwidth(m, kernel=self.kernel)
     return self._bandwidth
Exemplo n.º 9
0
def test_linear_model_parameters(data):
    mod = LinearFactorModel(data.portfolios, data.factors)
    res = mod.fit()
    f = mod.factors.ndarray
    p = mod.portfolios.ndarray
    n = f.shape[0]
    moments = np.zeros(
        (n, p.shape[1] * (f.shape[1] + 1) + f.shape[1] + p.shape[1]))
    fc = np.c_[np.ones((n, 1)), f]
    betas = lstsq(fc, p)[0]
    eps = p - fc @ betas
    loc = 0
    for i in range(eps.shape[1]):
        for j in range(fc.shape[1]):
            moments[:, loc] = eps[:, i] * fc[:, j]
            loc += 1
    b = betas[1:, :].T
    lam = lstsq(b, p.mean(0)[:, None])[0]
    pricing_errors = p - (b @ lam).T
    for i in range(lam.shape[0]):
        lam_error = (p - (b @ lam).T) @ b[:, [i]]
        moments[:, loc] = lam_error.squeeze()
        loc += 1
    alphas = pricing_errors.mean(0)[:, None]
    moments[:, loc:] = pricing_errors - alphas.T
    mod_moments = mod._moments(eps, b, lam, alphas, pricing_errors)

    assert_allclose(res.betas, b)
    assert_allclose(res.risk_premia, lam.squeeze())
    assert_allclose(res.alphas, alphas.squeeze())
    assert_allclose(moments, mod_moments)

    m = moments.shape[1]
    jac = np.eye(m)
    block1 = p.shape[1] * (f.shape[1] + 1)
    # 1,1

    jac[:block1, :block1] = np.kron(np.eye(p.shape[1]), fc.T @ fc / n)
    # 2, 1
    loc = 0
    nport, nf = p.shape[1], f.shape[1]
    block2 = block1 + nf
    for i in range(nport):
        block = np.zeros((nf, nf + 1))
        for j in range(nf):  # rows
            for k in range(1, nf + 1):  # cols
                block[j, k] = b[i][j] * lam[k - 1]
                if j + 1 == k:
                    block[j, k] -= alphas[i]
        jac[block1:block2, loc:loc + nf + 1] = block
        loc += nf + 1
    # 2, 2
    jac[block1:block2, block1:block2] = b.T @ b
    # 3,1
    block = np.zeros((nport, nport * (nf + 1)))
    row = col = 0
    for i in range(nport):
        for j in range(nf + 1):
            if j != 0:
                block[row, col] = lam[j - 1]
            col += 1
        row += 1
    jac[-nport:, :(nport * (nf + 1))] = block
    # 3, 2
    jac[-nport:, (nport * (nf + 1)):(nport * (nf + 1)) + nf] = b
    # 3, 3: already done since eye
    mod_jac = mod._jacobian(b, lam, alphas)
    assert_allclose(mod_jac[:block1], jac[:block1])
    assert_allclose(mod_jac[block1:block2, :block1],
                    jac[block1:block2, :block1])
    assert_allclose(mod_jac[block1:block2, block1:block2], jac[block1:block2,
                                                               block1:block2])
    assert_allclose(mod_jac[block1:block2, block2:], jac[block1:block2,
                                                         block2:])
    assert_allclose(mod_jac[block2:], jac[block2:])

    s = moments.T @ moments / (n - (nf + 1))
    ginv = np.linalg.inv(jac)
    cov = ginv @ s @ ginv.T / n
    order = np.zeros((nport, nf + 1), dtype=np.int64)
    order[:, 0] = np.arange(block2, block2 + nport)
    for i in range(nf):
        order[:, i + 1] = (nf + 1) * np.arange(nport) + (i + 1)
    order = np.r_[order.ravel(), block1:block2]
    cov = cov[order][:, order]
    cov = (cov + cov.T) / 2
    assert_allclose(cov, res.cov)

    acov = cov[:block1:(nf + 1), :block1:(nf + 1)]
    jstat = float(alphas.T @ np.linalg.pinv(acov) @ alphas)
    assert_allclose(res.j_statistic.stat, jstat)
    assert_allclose(res.j_statistic.pval,
                    1 - stats.chi2(nport - nf).cdf(jstat))

    get_all(res)

    res = LinearFactorModel(data.portfolios,
                            data.factors).fit(cov_type='kernel',
                                              debiased=False)
    std_mom = moments / moments.std(0)[None, :]
    mom = std_mom.sum(1)
    bw = kernel_optimal_bandwidth(mom)
    w = kernel_weight_bartlett(bw, n - 1)
    s = _cov_kernel(moments, w)
    cov = ginv @ s @ ginv.T / n
    cov = cov[order][:, order]
    cov = (cov + cov.T) / 2
    assert_allclose(cov, res.cov)
Exemplo n.º 10
0
def test_auto_bandwidth_unknown_kernel(data, kernel):
    with pytest.raises(ValueError):
        kernel_optimal_bandwidth(data.e, kernel.kernel + "_unknown")
Exemplo n.º 11
0
 def test_unknown_kernel(self, data, kernel):
     with pytest.raises(ValueError):
         kernel_optimal_bandwidth(data.e, kernel.kernel + '_unknown')