def S_crosssection(x, group): '''inner covariance matrix for White on group sums sandwich I guess for a single categorical group only, categorical group, can also be the product/intersection of groups This is used by cov_cluster and indirectly verified ''' x_group_sums = group_sums(x, group).T #TODO: why transposed return S_white_simple(x_group_sums)
def test_group_sums(): # Moved from grouputils __main__ section g = np.array([0, 0, 1, 2, 1, 1, 2, 0]) group_sums(np.arange(len(g)*3*2).reshape(len(g), 3, 2), g, use_bincount=False).T group_sums(np.arange(len(g)*3*2).reshape(len(g), 3, 2)[:, :, 0], g) group_sums(np.arange(len(g)*3*2).reshape(len(g), 3, 2)[:, :, 1], g)
def S_hac_groupsum(x, time, nlags=None, weights_func=weights_bartlett): '''inner covariance matrix for HAC over group sums sandwich This assumes we have complete equal spaced time periods. The number of time periods per group need not be the same, but we need at least one observation for each time period For a single categorical group only, or a everything else but time dimension. This first aggregates x over groups for each time period, then applies HAC on the sum per period. Parameters ---------- x : ndarray (nobs,) or (nobs, k_var) data, for HAC this is array of x_i * u_i time : ndarray, (nobs,) timeindes, assumed to be integers range(n_periods) nlags : int or None highest lag to include in kernel window. If None, then nlags = floor[4(T/100)^(2/9)] is used. weights_func : callable weights_func is called with nlags as argument to get the kernel weights. default are Bartlett weights Returns ------- S : ndarray, (k_vars, k_vars) inner covariance matrix for sandwich References ---------- Daniel Hoechle, xtscc paper Driscoll and Kraay ''' #needs groupsums x_group_sums = group_sums(x, time).T #TODO: transpose return in grou_sum return S_hac_simple(x_group_sums, nlags=nlags, weights_func=weights_func)