def test_ols(self): self.model = OLS( solver='pinv', fit_intercept=True, est_stderr=True, reg_method=None, alpha=0, l1_ratio=0, tol=1e-4, max_iter=100, coef=None, stderr=None, dispersion=None) self.model.fit(self.data_longley.exog, self.data_longley.endog) # coefficient self.assertEqual(self.model.coef.shape, (1, 7)) np.testing.assert_array_almost_equal( self.model.coef, np.array([-3482258.63459582, 15.0618722713733, -0.358191792925910E-01, -2.02022980381683, -1.03322686717359, -0.511041056535807E-01, 1829.15146461355]).reshape(1, -1), decimal=3) # std.err of coefficient (calibrated by df_resid) self.assertEqual(self.model.stderr.shape, (1, 7)) np.testing.assert_array_almost_equal( old_div(self.model.stderr, np.sqrt(old_div(9., self.data_longley.exog.shape[0]))), np.array([890420.383607373, 84.9149257747669, 0.03349, 0.488399681651699, 0.214274163161675, 0.226073200069370, 455.478499142212]).reshape(1, -1), decimal=2) # scale self.assertEqual(self.model.dispersion.shape, (1, 1)) np.testing.assert_array_almost_equal( old_div(self.model.dispersion, (old_div(9., self.data_longley.exog.shape[0]))), np.array([[92936.0061673238]]), decimal=3) # predict np.testing.assert_array_almost_equal( self.data_longley.endog.reshape(-1, 1) - self.model.predict(self.data_longley.exog), np.array([267.34003, -94.01394, 46.28717, -410.11462, 309.71459, -249.31122, -164.04896, -13.18036, 14.30477, 455.39409, -17.26893, -39.05504, -155.54997, -85.67131, 341.93151, -206.75783]).reshape(-1, 1), decimal=3) # loglike/_per_sample self.assertAlmostEqual( self.model.loglike(self.data_longley.exog, self.data_longley.endog), -109.61743480849013, places=3) # to_json json_dict = self.model.to_json('./tests/linear_models/OLS/UnivariateOLS/') self.assertEqual(json_dict['properties']['solver'], 'pinv') # from_json self.model_from_json = OLS.from_json(json_dict) np.testing.assert_array_almost_equal( self.model.coef, self.model_from_json.coef, decimal=3) np.testing.assert_array_almost_equal( self.model.stderr, self.model_from_json.stderr, decimal=3) self.assertEqual( self.model.dispersion, self.model_from_json.dispersion)
def test_ols(self): self.model = OLS( solver='pinv', fit_intercept=True, est_stderr=True, reg_method=None, alpha=0, l1_ratio=0, tol=1e-4, max_iter=100, coef=None, stderr=None, dispersion=None) self.model.fit(self.X, self.Y) # coefficient print(self.model.coef) print(self.model.dispersion) print(self.model.stderr) print(self.Y - self.model.predict(self.X)) print(self.model.loglike(self.X, self.Y)) self.assertEqual(self.model.coef.shape, (2, 2)) np.testing.assert_array_almost_equal( self.model.coef, np.array([[-0.02924966, -0.03484827], [-0.00978688, 0.00336316]]).reshape(2, -1), decimal=3) # std.err of coefficient (calibrated by df_resid) self.assertEqual(self.model.stderr.shape, (2, 2)) np.testing.assert_array_almost_equal( self.model.stderr, np.array([[0.03083908, 0.03121143], [0.03002101, 0.03038348]]).reshape(2, -1), decimal=2) # scale self.assertEqual(self.model.dispersion.shape, (2, 2)) np.testing.assert_array_almost_equal( self.model.dispersion, np.array([[0.94905363, 0.0164185], [0.0164185, 0.89937019]]), decimal=3) # loglike/_per_sample self.assertAlmostEqual( self.model.loglike(self.X, self.Y), -2758.54387369, places=3) # to_json json_dict = self.model.to_json('./tests/linear_models/OLS/MultivariateOLS/') self.assertEqual(json_dict['properties']['solver'], 'pinv') # from_json self.model_from_json = OLS.from_json(json_dict) np.testing.assert_array_almost_equal( self.model.coef, self.model_from_json.coef, decimal=3) np.testing.assert_array_almost_equal( self.model.stderr, self.model_from_json.stderr, decimal=3) np.testing.assert_array_almost_equal( self.model.dispersion, self.model_from_json.dispersion, decimal=3)