def test_calc_nodewise_weight(): np.random.seed(435265) X = np.random.normal(size=(50, 3)) ghat = np.random.normal(size=2) that = _calc_nodewise_weight(X, ghat, 0, 0.01) assert_(isinstance(that, float))
def test_calc_approx_inv_cov(): np.random.seed(435265) X = np.random.normal(size=(50, 3)) ghat_l = [] that_l = [] for i in range(3): ghat = _calc_nodewise_row(X, i, 0.01) that = _calc_nodewise_weight(X, ghat, i, 0.01) ghat_l.append(ghat) that_l.append(that) theta_hat = _calc_approx_inv_cov(np.array(ghat_l), np.array(that_l)) assert_equal(theta_hat.shape, (3, 3))
def _est_regularized_debiased(mod, mnum, partitions, fit_kwds=None, score_kwds=None, hess_kwds=None): """estimates the regularized fitted parameters, is the default estimation_method for class DistributedModel. Parameters ---------- mod : statsmodels model class instance The model for the current partition. mnum : scalar Index of current partition. partitions : scalar Total number of partitions. fit_kwds : dict-like or None Keyword arguments to be given to fit_regularized score_kwds : dict-like or None Keyword arguments for the score function. hess_kwds : dict-like or None Keyword arguments for the Hessian function. Returns ------- A tuple of parameters for regularized fit An array-like object of the fitted parameters, params An array-like object for the gradient A list of array like objects for nodewise_row A list of array like objects for nodewise_weight """ score_kwds = {} if score_kwds is None else score_kwds hess_kwds = {} if hess_kwds is None else hess_kwds if fit_kwds is None: raise ValueError("_est_regularized_debiased currently " + "requires that fit_kwds not be None.") else: alpha = fit_kwds["alpha"] if "L1_wt" in fit_kwds: L1_wt = fit_kwds["L1_wt"] else: L1_wt = 1 nobs, p = mod.exog.shape p_part = int(np.ceil((1. * p) / partitions)) params = mod.fit_regularized(**fit_kwds).params grad = _calc_grad(mod, params, alpha, L1_wt, score_kwds) / nobs wexog = _calc_wdesign_mat(mod, params, hess_kwds) nodewise_row_l = [] nodewise_weight_l = [] for idx in range(mnum * p_part, min((mnum + 1) * p_part, p)): nodewise_row = _calc_nodewise_row(wexog, idx, alpha) nodewise_row_l.append(nodewise_row) nodewise_weight = _calc_nodewise_weight(wexog, nodewise_row, idx, alpha) nodewise_weight_l.append(nodewise_weight) return params, grad, nodewise_row_l, nodewise_weight_l