def test_het_goldfeldquandt(self): #TODO: test options missing #> gq = gqtest(fm, alternative='greater') #> mkhtest_f(gq, 'het_gq_greater', 'f') het_gq_greater = dict(statistic=0.5313259064778423, pvalue=0.9990217851193723, parameters=(98, 98), distr='f') #> gq = gqtest(fm, alternative='less') #> mkhtest_f(gq, 'het_gq_less', 'f') het_gq_less = dict(statistic=0.5313259064778423, pvalue=0.000978214880627621, parameters=(98, 98), distr='f') #> gq = gqtest(fm, alternative='two.sided') #> mkhtest_f(gq, 'het_gq_two_sided', 'f') het_gq_two_sided = dict(statistic=0.5313259064778423, pvalue=0.001956429761255241, parameters=(98, 98), distr='f') #> gq = gqtest(fm, fraction=0.1, alternative='two.sided') #> mkhtest_f(gq, 'het_gq_two_sided_01', 'f') het_gq_two_sided_01 = dict(statistic=0.5006976835928314, pvalue=0.001387126702579789, parameters=(88, 87), distr='f') #> gq = gqtest(fm, fraction=0.5, alternative='two.sided') #> mkhtest_f(gq, 'het_gq_two_sided_05', 'f') het_gq_two_sided_05 = dict(statistic=0.434815645134117, pvalue=0.004799321242905568, parameters=(48, 47), distr='f') endogg, exogg = self.endog, self.exog #tests gq = smsdia.het_goldfeldquandt(endogg, exogg, split=0.5) compare_t_est(gq, het_gq_greater, decimal=(14, 14)) assert_equal(gq[-1], 'increasing') gq = smsdia.het_goldfeldquandt(endogg, exogg, split=0.5, alternative='decreasing') compare_t_est(gq, het_gq_less, decimal=(14, 14)) assert_equal(gq[-1], 'decreasing') gq = smsdia.het_goldfeldquandt(endogg, exogg, split=0.5, alternative='two-sided') compare_t_est(gq, het_gq_two_sided, decimal=(14, 14)) assert_equal(gq[-1], 'two-sided') #TODO: forcing the same split as R 202-90-90-1=21 gq = smsdia.het_goldfeldquandt(endogg, exogg, split=90, drop=21, alternative='two-sided') compare_t_est(gq, het_gq_two_sided_01, decimal=(14, 14)) assert_equal(gq[-1], 'two-sided')
def test_het_goldfeldquandt(self): #TODO: test options missing #> gq = gqtest(fm, alternative='greater') #> mkhtest_f(gq, 'het_gq_greater', 'f') het_gq_greater = dict(statistic=0.5313259064778423, pvalue=0.9990217851193723, parameters=(98, 98), distr='f') #> gq = gqtest(fm, alternative='less') #> mkhtest_f(gq, 'het_gq_less', 'f') het_gq_less = dict(statistic=0.5313259064778423, pvalue=0.000978214880627621, parameters=(98, 98), distr='f') #> gq = gqtest(fm, alternative='two.sided') #> mkhtest_f(gq, 'het_gq_two_sided', 'f') het_gq_two_sided = dict(statistic=0.5313259064778423, pvalue=0.001956429761255241, parameters=(98, 98), distr='f') #> gq = gqtest(fm, fraction=0.1, alternative='two.sided') #> mkhtest_f(gq, 'het_gq_two_sided_01', 'f') het_gq_two_sided_01 = dict(statistic=0.5006976835928314, pvalue=0.001387126702579789, parameters=(88, 87), distr='f') #> gq = gqtest(fm, fraction=0.5, alternative='two.sided') #> mkhtest_f(gq, 'het_gq_two_sided_05', 'f') het_gq_two_sided_05 = dict(statistic=0.434815645134117, pvalue=0.004799321242905568, parameters=(48, 47), distr='f') endogg, exogg = self.endog, self.exog #tests gq = smsdia.het_goldfeldquandt(endogg, exogg, split=0.5) compare_t_est(gq, het_gq_greater, decimal=(14, 14)) assert_equal(gq[-1], 'increasing') gq = smsdia.het_goldfeldquandt(endogg, exogg, split=0.5, alternative='decreasing') compare_t_est(gq, het_gq_less, decimal=(14, 14)) assert_equal(gq[-1], 'decreasing') gq = smsdia.het_goldfeldquandt(endogg, exogg, split=0.5, alternative='two-sided') compare_t_est(gq, het_gq_two_sided, decimal=(14, 14)) assert_equal(gq[-1], 'two-sided') #TODO: forcing the same split as R 202-90-90-1=21 gq = smsdia.het_goldfeldquandt(endogg, exogg, split=90, drop=21, alternative='two-sided') compare_t_est(gq, het_gq_two_sided_01, decimal=(14, 14)) assert_equal(gq[-1], 'two-sided')
def notyet_atst(): d = macrodata.load().data realinv = d['realinv'] realgdp = d['realgdp'] realint = d['realint'] endog = realinv exog = add_constant(np.c_[realgdp, realint],prepend=True) res_ols1 = OLS(endog, exog).fit() #growth rates gs_l_realinv = 400 * np.diff(np.log(d['realinv'])) gs_l_realgdp = 400 * np.diff(np.log(d['realgdp'])) lint = d['realint'][:-1] tbilrate = d['tbilrate'][:-1] endogg = gs_l_realinv exogg = add_constant(np.c_[gs_l_realgdp, lint], prepend=True) exogg2 = add_constant(np.c_[gs_l_realgdp, tbilrate], prepend=True) res_ols = OLS(endogg, exogg).fit() res_ols2 = OLS(endogg, exogg2).fit() #the following were done accidentally with res_ols1 in R, #with original Greene data params = np.array([-272.3986041341653, 0.1779455206941112, 0.2149432424658157]) cov_hac_4 = np.array([1321.569466333051, -0.2318836566017612, 37.01280466875694, -0.2318836566017614, 4.602339488102263e-05, -0.0104687835998635, 37.012804668757, -0.0104687835998635, 21.16037144168061]).reshape(3,3, order='F') cov_hac_10 = np.array([2027.356101193361, -0.3507514463299015, 54.81079621448568, -0.350751446329901, 6.953380432635583e-05, -0.01268990195095196, 54.81079621448564, -0.01268990195095195, 22.92512402151113]).reshape(3,3, order='F') #goldfeld-quandt het_gq_greater = dict(statistic=13.20512768685082, df1=99, df2=98, pvalue=1.246141976112324e-30, distr='f') het_gq_less = dict(statistic=13.20512768685082, df1=99, df2=98, pvalue=1.) het_gq_2sided = dict(statistic=13.20512768685082, df1=99, df2=98, pvalue=1.246141976112324e-30, distr='f') #goldfeld-quandt, fraction = 0.5 het_gq_greater_2 = dict(statistic=87.1328934692124, df1=48, df2=47, pvalue=2.154956842194898e-33, distr='f') gq = smsdia.het_goldfeldquandt(endog, exog, split=0.5) compare_t_est(gq, het_gq_greater, decimal=(13, 14)) assert_equal(gq[-1], 'increasing') harvey_collier = dict(stat=2.28042114041313, df=199, pvalue=0.02364236161988260, distr='t') #hc = harvtest(fm, order.by=ggdp , data = list()) harvey_collier_2 = dict(stat=0.7516918462158783, df=199, pvalue=0.4531244858006127, distr='t')
def notyet_atst(): d = macrodata.load(as_pandas=False).data realinv = d['realinv'] realgdp = d['realgdp'] realint = d['realint'] endog = realinv exog = add_constant(np.c_[realgdp, realint]) res_ols1 = OLS(endog, exog).fit() #growth rates gs_l_realinv = 400 * np.diff(np.log(d['realinv'])) gs_l_realgdp = 400 * np.diff(np.log(d['realgdp'])) lint = d['realint'][:-1] tbilrate = d['tbilrate'][:-1] endogg = gs_l_realinv exogg = add_constant(np.c_[gs_l_realgdp, lint]) exogg2 = add_constant(np.c_[gs_l_realgdp, tbilrate]) res_ols = OLS(endogg, exogg).fit() res_ols2 = OLS(endogg, exogg2).fit() #the following were done accidentally with res_ols1 in R, #with original Greene data params = np.array([-272.3986041341653, 0.1779455206941112, 0.2149432424658157]) cov_hac_4 = np.array([1321.569466333051, -0.2318836566017612, 37.01280466875694, -0.2318836566017614, 4.602339488102263e-05, -0.0104687835998635, 37.012804668757, -0.0104687835998635, 21.16037144168061]).reshape(3,3, order='F') cov_hac_10 = np.array([2027.356101193361, -0.3507514463299015, 54.81079621448568, -0.350751446329901, 6.953380432635583e-05, -0.01268990195095196, 54.81079621448564, -0.01268990195095195, 22.92512402151113]).reshape(3,3, order='F') #goldfeld-quandt het_gq_greater = dict(statistic=13.20512768685082, df1=99, df2=98, pvalue=1.246141976112324e-30, distr='f') het_gq_less = dict(statistic=13.20512768685082, df1=99, df2=98, pvalue=1.) het_gq_2sided = dict(statistic=13.20512768685082, df1=99, df2=98, pvalue=1.246141976112324e-30, distr='f') #goldfeld-quandt, fraction = 0.5 het_gq_greater_2 = dict(statistic=87.1328934692124, df1=48, df2=47, pvalue=2.154956842194898e-33, distr='f') gq = smsdia.het_goldfeldquandt(endog, exog, split=0.5) compare_t_est(gq, het_gq_greater, decimal=(13, 14)) assert_equal(gq[-1], 'increasing') harvey_collier = dict(stat=2.28042114041313, df=199, pvalue=0.02364236161988260, distr='t') #hc = harvtest(fm, order.by=ggdp , data = list()) harvey_collier_2 = dict(stat=0.7516918462158783, df=199, pvalue=0.4531244858006127, distr='t')
def print_goldsfeltquandt(self): linear_model = sm.OLS(self.y_train, self.X_train).fit() print( diagnostic.het_goldfeldquandt(linear_model.resid, linear_model.model.exog))
plt.xlabel('NINO3.4 ($^{\circ}$C)') plt.ylabel('Residuals (mm day$^{-1}$)') plt.title('Scatter Plot: Residuals vs. NINO3.4') # It's a bit hard to tell from this plot. Let's try the Goldfeld-Quandt test. To use this test we need to import a new python package: `statsmodels`. # In[7]: # test for homoscedasticity from statsmodels.stats.diagnostic import het_goldfeldquandt nino34ex = np.expand_dims(nino34, axis=1) # we need to reshape nino34 to use this test # the output of the het_goldfeldquandt test is the F-statistic and the p-value. GQtest = het_goldfeldquandt(precip_djfm, nino34ex, alternative='increasing') print(GQtest[1]) # Thus, we can accept the null hypothesis of homoscedasticity given the large p-value. # ## 2. No Autocorrelation of Residuals (Durbin-Watson test) # # The condition that the residuals should be independent means that one residual should not be able to predict the next. But, if the residuals contain a trend or periodicity, then the residuals are not truly independent and are **autocorrelated** (see [Figure 15](autocorr)). # # ```{figure} autocorr.png # --- # scale: 50% # name: autocorr # --- # Scatter plots of the residuals versus $x$ for residuals the have (left) no autocorrelation and (right) autocorrelation. # ```
def smf_het_goldfeldquandt(self): f_stat, p_val, inc_dec = het_goldfeldquandt(self.model1.resid, self.model1.model.exog) print( f'For smf model 1 het goldfeldquandt test, the f stat is {f_stat} and the p value is {p_val}' )
diag.het_goldfeldquandt(modelMR2.resid, modelMR2.model.exog) diag.het_breuschpagan(modelMR4.resid, modelMR4.model.exog) diag.het_white(modelMR2.resid, modelMR2.model.exog, retres = False) diag.(modelMR2.resid, modelMR2.model.exog) diag.acorr_ljungbox(modelMR2.resid)