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.compat.python import lzip 

2 

3from io import StringIO 

4 

5import numpy as np 

6 

7from statsmodels.iolib import SimpleTable 

8 

9mat = np.array 

10 

11_default_table_fmt = dict( 

12 empty_cell = '', 

13 colsep=' ', 

14 row_pre = '', 

15 row_post = '', 

16 table_dec_above='=', 

17 table_dec_below='=', 

18 header_dec_below='-', 

19 header_fmt = '%s', 

20 stub_fmt = '%s', 

21 title_align='c', 

22 header_align = 'r', 

23 data_aligns = 'r', 

24 stubs_align = 'l', 

25 fmt = 'txt' 

26) 

27 

28 

29class VARSummary(object): 

30 default_fmt = dict( 

31 #data_fmts = ["%#12.6g","%#12.6g","%#10.4g","%#5.4g"], 

32 #data_fmts = ["%#10.4g","%#10.4g","%#10.4g","%#6.4g"], 

33 data_fmts = ["%#15.6F","%#15.6F","%#15.3F","%#14.3F"], 

34 empty_cell = '', 

35 #colwidths = 10, 

36 colsep=' ', 

37 row_pre = '', 

38 row_post = '', 

39 table_dec_above='=', 

40 table_dec_below='=', 

41 header_dec_below='-', 

42 header_fmt = '%s', 

43 stub_fmt = '%s', 

44 title_align='c', 

45 header_align = 'r', 

46 data_aligns = 'r', 

47 stubs_align = 'l', 

48 fmt = 'txt' 

49 ) 

50 

51 part1_fmt = dict( 

52 default_fmt, 

53 data_fmts = ["%s"], 

54 colwidths = 15, 

55 colsep=' ', 

56 table_dec_below='', 

57 header_dec_below=None, 

58 ) 

59 part2_fmt = dict( 

60 default_fmt, 

61 data_fmts = ["%#12.6g","%#12.6g","%#10.4g","%#5.4g"], 

62 colwidths = None, 

63 colsep=' ', 

64 table_dec_above='-', 

65 table_dec_below='-', 

66 header_dec_below=None, 

67 ) 

68 

69 def __init__(self, estimator): 

70 self.model = estimator 

71 self.summary = self.make() 

72 

73 def __repr__(self): 

74 return self.summary 

75 

76 def make(self, endog_names=None, exog_names=None): 

77 """ 

78 Summary of VAR model 

79 """ 

80 buf = StringIO() 

81 

82 buf.write(self._header_table() + '\n') 

83 buf.write(self._stats_table() + '\n') 

84 buf.write(self._coef_table() + '\n') 

85 buf.write(self._resid_info() + '\n') 

86 

87 return buf.getvalue() 

88 

89 def _header_table(self): 

90 import time 

91 

92 model = self.model 

93 

94 t = time.localtime() 

95 

96 # TODO: change when we allow coef restrictions 

97 # ncoefs = len(model.beta) 

98 

99 # Header information 

100 part1title = "Summary of Regression Results" 

101 part1data = [[model._model_type], 

102 ["OLS"], #TODO: change when fit methods change 

103 [time.strftime("%a, %d, %b, %Y", t)], 

104 [time.strftime("%H:%M:%S", t)]] 

105 part1header = None 

106 part1stubs = ('Model:', 

107 'Method:', 

108 'Date:', 

109 'Time:') 

110 part1 = SimpleTable(part1data, part1header, part1stubs, 

111 title=part1title, txt_fmt=self.part1_fmt) 

112 

113 return str(part1) 

114 

115 def _stats_table(self): 

116 # TODO: do we want individual statistics or should users just 

117 # use results if wanted? 

118 # Handle overall fit statistics 

119 

120 model = self.model 

121 

122 part2Lstubs = ('No. of Equations:', 

123 'Nobs:', 

124 'Log likelihood:', 

125 'AIC:') 

126 part2Rstubs = ('BIC:', 

127 'HQIC:', 

128 'FPE:', 

129 'Det(Omega_mle):') 

130 part2Ldata = [[model.neqs], [model.nobs], [model.llf], [model.aic]] 

131 part2Rdata = [[model.bic], [model.hqic], [model.fpe], [model.detomega]] 

132 part2Lheader = None 

133 part2L = SimpleTable(part2Ldata, part2Lheader, part2Lstubs, 

134 txt_fmt = self.part2_fmt) 

135 part2R = SimpleTable(part2Rdata, part2Lheader, part2Rstubs, 

136 txt_fmt = self.part2_fmt) 

137 part2L.extend_right(part2R) 

138 

139 return str(part2L) 

140 

141 def _coef_table(self): 

142 model = self.model 

143 k = model.neqs 

144 

145 Xnames = self.model.exog_names 

146 

147 data = lzip(model.params.T.ravel(), 

148 model.stderr.T.ravel(), 

149 model.tvalues.T.ravel(), 

150 model.pvalues.T.ravel()) 

151 

152 header = ('coefficient','std. error','t-stat','prob') 

153 

154 buf = StringIO() 

155 dim = k * model.k_ar + model.k_trend + model.k_exog_user 

156 for i in range(k): 

157 section = "Results for equation %s" % model.names[i] 

158 buf.write(section + '\n') 

159 

160 table = SimpleTable(data[dim * i : dim * (i + 1)], header, 

161 Xnames, title=None, txt_fmt = self.default_fmt) 

162 buf.write(str(table) + '\n') 

163 

164 if i < k - 1: 

165 buf.write('\n') 

166 

167 return buf.getvalue() 

168 

169 def _resid_info(self): 

170 buf = StringIO() 

171 names = self.model.names 

172 

173 buf.write("Correlation matrix of residuals" + '\n') 

174 buf.write(pprint_matrix(self.model.resid_corr, names, names) + '\n') 

175 

176 return buf.getvalue() 

177 

178 

179def normality_summary(results): 

180 title = "Normality skew/kurtosis Chi^2-test" 

181 null_hyp = 'H_0: data generated by normally-distributed process' 

182 return hypothesis_test_table(results, title, null_hyp) 

183 

184 

185def hypothesis_test_table(results, title, null_hyp): 

186 fmt = dict(_default_table_fmt, 

187 data_fmts=["%#15.6F","%#15.6F","%#15.3F", "%s"]) 

188 

189 buf = StringIO() 

190 table = SimpleTable([[results['statistic'], 

191 results['crit_value'], 

192 results['pvalue'], 

193 str(results['df'])]], 

194 ['Test statistic', 'Critical Value', 'p-value', 

195 'df'], [''], title=None, txt_fmt=fmt) 

196 

197 buf.write(title + '\n') 

198 buf.write(str(table) + '\n') 

199 

200 buf.write(null_hyp + '\n') 

201 

202 buf.write("Conclusion: %s H_0" % results['conclusion']) 

203 buf.write(" at %.2f%% significance level" % (results['signif'] * 100)) 

204 

205 return buf.getvalue() 

206 

207 

208def pprint_matrix(values, rlabels, clabels, col_space=None): 

209 buf = StringIO() 

210 

211 T, K = len(rlabels), len(clabels) 

212 

213 if col_space is None: 

214 min_space = 10 

215 col_space = [max(len(str(c)) + 2, min_space) for c in clabels] 

216 else: 

217 col_space = (col_space,) * K 

218 

219 row_space = max([len(str(x)) for x in rlabels]) + 2 

220 

221 head = _pfixed('', row_space) 

222 

223 for j, h in enumerate(clabels): 

224 head += _pfixed(h, col_space[j]) 

225 

226 buf.write(head + '\n') 

227 

228 for i, rlab in enumerate(rlabels): 

229 line = ('%s' % rlab).ljust(row_space) 

230 

231 for j in range(K): 

232 line += _pfixed(values[i,j], col_space[j]) 

233 

234 buf.write(line + '\n') 

235 

236 return buf.getvalue() 

237 

238 

239def _pfixed(s, space, nanRep=None, float_format=None): 

240 if isinstance(s, float): 

241 if float_format: 

242 formatted = float_format(s) 

243 else: 

244 formatted = "%#8.6F" % s 

245 

246 return formatted.rjust(space) 

247 else: 

248 return ('%s' % s)[:space].rjust(space)