Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1from statsmodels.iolib.table import SimpleTable 

2import numpy as np 

3 

4 

5class HypothesisTestResults(object): 

6 """ 

7 Results class for hypothesis tests. 

8 

9 Parameters 

10 ---------- 

11 test_statistic : float 

12 crit_value : float 

13 pvalue : float, 0 <= `pvalue` <= 1 

14 df : int 

15 Degrees of freedom. 

16 signif : float, 0 < `signif` < 1 

17 Significance level. 

18 method : str 

19 The kind of test (e.g. ``"f"`` for F-test, ``"wald"`` for Wald-test). 

20 title : str 

21 A title describing the test. It will be part of the summary. 

22 h0 : str 

23 A string describing the null hypothesis. It will be used in the 

24 summary. 

25 """ 

26 def __init__(self, test_statistic, crit_value, pvalue, df, 

27 signif, method, title, h0): 

28 self.test_statistic = test_statistic 

29 self.crit_value = crit_value 

30 self.pvalue = pvalue 

31 self.df = df 

32 self.signif = signif 

33 self.method = method.capitalize() 

34 if test_statistic < crit_value: 

35 self.conclusion = "fail to reject" 

36 else: 

37 self.conclusion = "reject" 

38 self.title = title 

39 self.h0 = h0 

40 self.conclusion_str = "Conclusion: %s H_0" % self.conclusion 

41 self.signif_str = " at {:.0%} significance level".format(self.signif) 

42 

43 def summary(self): 

44 """Return summary""" 

45 title = self.title + ". " + self.h0 + ". " \ 

46 + self.conclusion_str + self.signif_str + "." 

47 data_fmt = {"data_fmts": ["%#0.4g", "%#0.4g", "%#0.3F", "%s"]} 

48 html_data_fmt = dict(data_fmt) 

49 html_data_fmt["data_fmts"] = ["<td>" + i + "</td>" 

50 for i in html_data_fmt["data_fmts"]] 

51 return SimpleTable(data=[[self.test_statistic, self.crit_value, 

52 self.pvalue, str(self.df)]], 

53 headers=['Test statistic', 'Critical value', 

54 'p-value', 'df'], 

55 title=title, 

56 txt_fmt=data_fmt, 

57 html_fmt=html_data_fmt, 

58 ltx_fmt=data_fmt) 

59 

60 def __str__(self): 

61 return "<" + self.__module__ + "." + self.__class__.__name__ \ 

62 + " object. " + self.h0 + ": " + self.conclusion \ 

63 + self.signif_str \ 

64 + ". Test statistic: {:.3f}".format(self.test_statistic) \ 

65 + ", critical value: {:.3f}>".format(self.crit_value) \ 

66 + ", p-value: {:.3f}>".format(self.pvalue) 

67 

68 def __eq__(self, other): 

69 if not isinstance(other, self.__class__): 

70 return False 

71 return np.allclose(self.test_statistic, other.test_statistic) \ 

72 and np.allclose(self.crit_value, other.crit_value) \ 

73 and np.allclose(self.pvalue, other.pvalue) \ 

74 and np.allclose(self.signif, other.signif) 

75 

76 

77class CausalityTestResults(HypothesisTestResults): 

78 """ 

79 Results class for Granger-causality and instantaneous causality. 

80 

81 Parameters 

82 ---------- 

83 causing : list of str 

84 This list contains the potentially causing variables. 

85 caused : list of str 

86 This list contains the potentially caused variables. 

87 test_statistic : float 

88 crit_value : float 

89 pvalue : float 

90 df : int 

91 Degrees of freedom. 

92 signif : float 

93 Significance level. 

94 test : str {``"granger"``, ``"inst"``}, default: ``"granger"`` 

95 If ``"granger"``, Granger-causality has been tested. If ``"inst"``, 

96 instantaneous causality has been tested. 

97 method : str {``"f"``, ``"wald"``} 

98 The kind of test. ``"f"`` indicates an F-test, ``"wald"`` indicates a 

99 Wald-test. 

100 """ 

101 def __init__(self, causing, caused, test_statistic, crit_value, pvalue, df, 

102 signif, test="granger", method=None): 

103 self.causing = causing 

104 self.caused = caused 

105 self.test = test 

106 if method is None or method.lower() not in ["f", "wald"]: 

107 raise ValueError('The method ("f" for F-test, "wald" for ' 

108 "Wald-test) must not be None.") 

109 method = method.capitalize() 

110 # attributes used in summary and string representation: 

111 title = "Granger" if self.test == "granger" else "Instantaneous" 

112 title += " causality %s-test" % method 

113 h0 = "H_0: " 

114 if len(self.causing) == 1: 

115 h0 += "{} does not ".format(self.causing[0]) 

116 else: 

117 h0 += "%s do not ".format(self.causing) 

118 h0 += "Granger-" if self.test == "granger" else "instantaneously " 

119 h0 += "cause " 

120 if len(self.caused) == 1: 

121 h0 += self.caused[0] 

122 else: 

123 h0 += "[" + ", ".join(caused) + "]" 

124 

125 super(CausalityTestResults, self).__init__(test_statistic, crit_value, 

126 pvalue, df, signif, method, 

127 title, h0) 

128 

129 def __eq__(self, other): 

130 basic_test = super(CausalityTestResults, self).__eq__(other) 

131 if not basic_test: 

132 return False 

133 test = self.test == other.test 

134 variables = (self.causing == other.causing and 

135 self.caused == other.caused) 

136 # instantaneous causality is a symmetric relation ==> causing and 

137 # caused may be swapped 

138 if not variables and self.test == "inst": 

139 variables = (self.causing == other.caused and 

140 self.caused == other.causing) 

141 return test and variables 

142 

143 

144class NormalityTestResults(HypothesisTestResults): 

145 """ 

146 Results class for the Jarque-Bera-test for nonnormality. 

147 

148 Parameters 

149 ---------- 

150 test_statistic : float 

151 The test's test statistic. 

152 crit_value : float 

153 The test's critical value. 

154 pvalue : float 

155 The test's p-value. 

156 df : int 

157 Degrees of freedom. 

158 signif : float 

159 Significance level. 

160 """ 

161 def __init__(self, test_statistic, crit_value, pvalue, df, signif): 

162 method = "Jarque-Bera" 

163 title = "normality (skew and kurtosis) test" 

164 h0 = 'H_0: data generated by normally-distributed process' 

165 super(NormalityTestResults, self).__init__(test_statistic, crit_value, 

166 pvalue, df, signif, 

167 method, title, h0) 

168 

169 

170class WhitenessTestResults(HypothesisTestResults): 

171 """ 

172 Results class for the Portmanteau-test for residual autocorrelation. 

173 

174 Parameters 

175 ---------- 

176 test_statistic : float 

177 The test's test statistic. 

178 crit_value : float 

179 The test's critical value. 

180 pvalue : float 

181 The test's p-value. 

182 df : int 

183 Degrees of freedom. 

184 signif : float 

185 Significance level. 

186 nlags : int 

187 Number of lags tested. 

188 """ 

189 def __init__(self, test_statistic, crit_value, pvalue, df, signif, nlags, 

190 adjusted): 

191 self.lags = nlags 

192 self.adjusted = adjusted 

193 method = "Portmanteau" 

194 title = "{}-test for residual autocorrelation".format(method) 

195 if adjusted: 

196 title = "Adjusted " + title 

197 h0 = "H_0: residual autocorrelation up to lag {} is zero".format(nlags) 

198 super(WhitenessTestResults, self).__init__(test_statistic, crit_value, 

199 pvalue, df, signif, 

200 method, title, h0)