def _initialize(cls): y, x = cls.y, cls.x # adding 10 to avoid strict rtol at predicted values close to zero y = y + 10 k = x.shape[1] cov_type = 'HC0' restriction = np.eye(k)[2:] modp = TheilGLS(y, x, r_matrix=restriction) # the corresponding Theil penweight seems to be 2 * nobs / sigma2_e cls.res2 = modp.fit(pen_weight=120.74564413221599 * 1000, use_t=False) pen = smpen.L2ContraintsPenalty(restriction=restriction) mod = GLMPenalized(y, x, family=family.Gaussian(), penal=pen) # use default weight for GLMPenalized mod.pen_weight *= 1 cls.res1 = mod.fit(cov_type=cov_type, method='bfgs', maxiter=100, disp=0, trim=False) cls.k_nonzero = k cls.exog_index = slice(None, cls.k_nonzero, None) cls.k_params = x.shape[1] cls.atol = 1e-5 cls.rtol = 1e-5
def _initialize(cls): y, x = cls.y, cls.x # adding 10 to avoid strict rtol at predicted values close to zero y = y + 10 cov_type = 'HC0' modp = GLM(y, x[:, :cls.k_nonzero], family=family.Gaussian()) cls.res2 = modp.fit(cov_type=cov_type, method='bfgs', maxiter=100, disp=0) weights = (np.arange(x.shape[1]) >= 4).astype(float) mod = GLMPenalized(y, x, family=family.Gaussian(), penal=smpen.L2ContraintsPenalty(weights=weights)) # make pen_weight large to force redundant to close to zero mod.pen_weight *= 500 cls.res1 = mod.fit(cov_type=cov_type, method='bfgs', maxiter=100, disp=0, trim=False) cls.exog_index = slice(None, cls.k_nonzero, None) cls.k_params = x.shape[1] cls.atol = 1e-5 cls.rtol = 1e-5
def test_equivalence(self): # compare plain penalty with included weights or restriction pen = self.pen x = self.params k = x.shape[1] pen2 = smpen.L2ContraintsPenalty(weights=np.ones(k)) pen3 = smpen.L2ContraintsPenalty(restriction=np.eye(k)) f = pen.func(x.T) d = pen.deriv(x.T) d2 = np.array([pen.deriv2(np.atleast_1d(xi)) for xi in x]) for pen_ in [pen2, pen3]: assert_allclose(pen_.func(x.T), f, rtol=1e-7, atol=1e-8) assert_allclose(pen_.deriv(x.T), d, rtol=1e-7, atol=1e-8) d2_ = np.array([pen.deriv2(np.atleast_1d(xi)) for xi in x]) assert_allclose(d2_, d2, rtol=1e-10, atol=1e-8)
def setup_class(cls): exog, penalty_matrix, restriction = cls._init() endog = data_mcycle['accel'] pen = smpen.L2ContraintsPenalty(restriction=restriction) mod = GLMPenalized(endog, exog, family=family.Gaussian(), penal=pen) # scaling of penweight in R mgcv s_scale_r = 0.02630734 # set pen_weight to correspond to R mgcv example cls.pw = mod.pen_weight = 1 / s_scale_r / 2 cls.res1 = mod.fit(cov_type=cls.cov_type, method='bfgs', maxiter=100, disp=0, trim=False, scale='x2') cls.res2 = results_pls.pls5 cls.rtol_fitted = 1e-5 # edf is currently not available with PenalizedMixin # need correction for difference in scale denominator cls.covp_corrfact = 1.0025464444310588
def setup_class(cls): x0 = np.linspace(-0.2, 0.2, 11) cls.params = np.column_stack((x0, x0)) cls.pen = smpen.L2ContraintsPenalty(restriction=[[1, 0], [1, 1]])