drvec 0.1.0__py3-none-win_amd64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- drvec/__init__.py +22 -0
- drvec/_bin/drvec.exe +0 -0
- drvec/_gsl.py +83 -0
- drvec/api.py +942 -0
- drvec/c_engine.py +391 -0
- drvec/cast.py +196 -0
- drvec/cli.py +592 -0
- drvec/data.py +295 -0
- drvec/diagnose.py +439 -0
- drvec/fdhess.py +130 -0
- drvec/fit.py +528 -0
- drvec/forecast.py +314 -0
- drvec/irfboot.py +216 -0
- drvec/levels.py +158 -0
- drvec/loglik.py +45 -0
- drvec/nla.py +266 -0
- drvec/parallel.py +86 -0
- drvec/params.py +311 -0
- drvec/rank.py +401 -0
- drvec/report.py +1712 -0
- drvec/results.py +167 -0
- drvec/rng.py +51 -0
- drvec/rungtable.py +82 -0
- drvec/start.py +317 -0
- drvec/stderr.py +33 -0
- drvec/xlink.py +226 -0
- drvec-0.1.0.dist-info/METADATA +205 -0
- drvec-0.1.0.dist-info/RECORD +32 -0
- drvec-0.1.0.dist-info/WHEEL +5 -0
- drvec-0.1.0.dist-info/entry_points.txt +2 -0
- drvec-0.1.0.dist-info/licenses/COPYING +339 -0
- drvec-0.1.0.dist-info/top_level.txt +1 -0
drvec/report.py
ADDED
|
@@ -0,0 +1,1712 @@
|
|
|
1
|
+
"""The .out file, as the C writes it.
|
|
2
|
+
|
|
3
|
+
The aim is byte identity with the oracle after tools/golden.sh's filter (the
|
|
4
|
+
version banner and absolute paths dropped). Each block names the C function and
|
|
5
|
+
lines it transcribes (drvec.c at drvec-v0.12). What is not written yet, and
|
|
6
|
+
which lines differ and why, is in docs/PORT.md §8.
|
|
7
|
+
|
|
8
|
+
The writer reads the result objects; it never re-estimates. The order of the
|
|
9
|
+
blocks is the order in which the C writes them while it fits: the ladder and
|
|
10
|
+
its gate when the ladder is computed (not when it comes from the cache), the
|
|
11
|
+
profile's optimizer banner and the -seedgate block, the search table and the
|
|
12
|
+
winner's banner, then the report proper.
|
|
13
|
+
"""
|
|
14
|
+
from __future__ import annotations
|
|
15
|
+
|
|
16
|
+
import math
|
|
17
|
+
|
|
18
|
+
BAR = "=" * 61
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
# --------------------------------------------------------------------------- #
|
|
22
|
+
# the header (main, drvec.c:11067-11140) #
|
|
23
|
+
# --------------------------------------------------------------------------- #
|
|
24
|
+
|
|
25
|
+
def header(data, spec, out_name, lrtest=False, how=None):
|
|
26
|
+
"""The first line names the port and its version where the C names itself:
|
|
27
|
+
tools/golden.sh drops that line before comparing."""
|
|
28
|
+
from . import __version__
|
|
29
|
+
M, r = data.M, spec.r
|
|
30
|
+
L = [f"Program : DRVEC {__version__} (python)",
|
|
31
|
+
"Input Data File : the .pre files",
|
|
32
|
+
f"Output File : {out_name}"]
|
|
33
|
+
if lrtest:
|
|
34
|
+
L.append(f"Model : VARMA-VECM({spec.p},{spec.q}), M = {M} series, "
|
|
35
|
+
f"cointegration rank r = 0..{M - 1} (sequential test)")
|
|
36
|
+
else:
|
|
37
|
+
L.append(f"Model : VARMA-VECM({spec.p},{spec.q}), M = {M} series, "
|
|
38
|
+
f"cointegration rank r = {r}")
|
|
39
|
+
L.append("Estimation : Exact Unconditional Maximum Likelihood, Algorithm AS 311 "
|
|
40
|
+
"(Mauricio 1997), xi sequence truncated at 1e-3")
|
|
41
|
+
L.append("Transformation : VECM to stationary VARMA (Mauricio 2006)")
|
|
42
|
+
det = {1: "E[nabla Y2] = 0, E[W] = 0", 2: "E[nabla Y2] = 0, E[W] free",
|
|
43
|
+
3: "E[nabla Y2] free, E[W] free"}[spec.case]
|
|
44
|
+
held = spec.case == 3 and spec.drift is not None and not all(spec.drift)
|
|
45
|
+
L.append("Deterministic : case %d -- %s%s%s" % (
|
|
46
|
+
4 if spec.trend else spec.case, det,
|
|
47
|
+
("" if not held else " (drifts as in the .pre files)" if how == "pre"
|
|
48
|
+
else " (some drifts held by -nodrift)"),
|
|
49
|
+
", and a trend in W (restricted to beta)" if spec.trend else ""))
|
|
50
|
+
th = ("absent (q = 0)" if spec.q == 0
|
|
51
|
+
else "= [T11 T12 ; 0 T22] (-matri)" if spec.ma == "matri"
|
|
52
|
+
else "diagonal (-diagma)" if spec.ma == "diag"
|
|
53
|
+
else "= [T11 T12 ; 0 0] (-marow)" if (spec.ma == "marow" and r > 0)
|
|
54
|
+
else "free (the default; -mafree)")
|
|
55
|
+
L.append("Structure : F %s, Theta %s, Sigma %s" % (
|
|
56
|
+
"diagonal" if spec.diag_ar else
|
|
57
|
+
"with entries held at 0 (-gzero, below)" if spec.gzero else "free", th,
|
|
58
|
+
"diagonal" if spec.diag_cov else "free"))
|
|
59
|
+
L.append("Series :" + "".join(" " + n for n in data.names)
|
|
60
|
+
+ f" ({M - r} in the nabla Y2 block, {r} in Y1)")
|
|
61
|
+
y, sub = data.first_estimated()
|
|
62
|
+
n = data.nobs
|
|
63
|
+
when = f"{y}" + (f"/{sub}" if data.freq > 1 else "")
|
|
64
|
+
freq = {1: "annual", 4: "quarterly", 12: "monthly"}.get(data.freq, "irregular")
|
|
65
|
+
L.append(f"Sample : {n} observations from {when}, {freq} (raw {data.nobs_raw})")
|
|
66
|
+
if not lrtest and data.nobs_raw > 2:
|
|
67
|
+
warned = False
|
|
68
|
+
raw = data.raw
|
|
69
|
+
T = data.nobs_raw - 1
|
|
70
|
+
for i in range(M):
|
|
71
|
+
m1 = 0.0
|
|
72
|
+
for t in range(1, data.nobs_raw):
|
|
73
|
+
m1 += float(raw[t, i]) - float(raw[t - 1, i])
|
|
74
|
+
m1 /= T
|
|
75
|
+
v = 0.0
|
|
76
|
+
for t in range(1, data.nobs_raw):
|
|
77
|
+
d = float(raw[t, i]) - float(raw[t - 1, i]) - m1
|
|
78
|
+
v += d * d
|
|
79
|
+
sd = math.sqrt(v / (T - 1 if T > 1 else 1))
|
|
80
|
+
if sd > 0.0 and abs(math.log10(sd)) > 2.0:
|
|
81
|
+
if not warned:
|
|
82
|
+
L.append(" ! SCALE: the optimiser works on changes of order 1, "
|
|
83
|
+
"and these are not (BUG-43):")
|
|
84
|
+
L.append(" %-12s s.d. of its changes %s: multiply it by %s" % (
|
|
85
|
+
data.names[i], cg(sd, 3), cg(10.0 ** (-math.floor(math.log10(sd) + 0.5)))))
|
|
86
|
+
warned = True
|
|
87
|
+
return "\n".join(L) + "\n"
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def cg(v, prec=6):
|
|
91
|
+
"""C's %g (and %.<prec>g)."""
|
|
92
|
+
return "%.*g" % (prec, v)
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
# --------------------------------------------------------------------------- #
|
|
96
|
+
# the seeding stage: the ladder, its gate, the profile #
|
|
97
|
+
# --------------------------------------------------------------------------- #
|
|
98
|
+
|
|
99
|
+
def gate_block(g):
|
|
100
|
+
"""gate_contract (drvec.c:3014-3140), the two-sided certificate."""
|
|
101
|
+
L = ["", "=== Entry gate: the factorisation contract ===", "",
|
|
102
|
+
"At r = 0 with diagonal Phi, Theta and Sigma the likelihood factorises,",
|
|
103
|
+
"so the joint fit must equal the sum of the univariate fits exactly.", ""]
|
|
104
|
+
failed = False
|
|
105
|
+
for i, u in enumerate(g.univariate):
|
|
106
|
+
if u is None:
|
|
107
|
+
L.append(" univariate %d elf ifault = %d" % (i + 1, 1))
|
|
108
|
+
failed = True
|
|
109
|
+
else:
|
|
110
|
+
L.append(" univariate %d (p=%d, q=%d) logL = %18.10f" % (i + 1, g.p, g.q, u))
|
|
111
|
+
if failed:
|
|
112
|
+
L += ["", " Contract NOT verified: a univariate evaluation failed."]
|
|
113
|
+
return "\n".join(L) + "\n"
|
|
114
|
+
L.append(" %-28s sum = %18.10f" % ("", g.sum))
|
|
115
|
+
L.append(" %-28s joint= %18.10f" % ("", g.joint))
|
|
116
|
+
L.append(" (both sides untruncated; the fit's own logL, xi truncated at %.0e, is %.10f)"
|
|
117
|
+
% (1.0e-3, g.fit_ll))
|
|
118
|
+
ok = g.verified
|
|
119
|
+
L.append("")
|
|
120
|
+
L.append(" crossing identity (joint - sum) = %.3e (tolerance %.1e) %s" % (
|
|
121
|
+
g.gap, 1.0e-6, "VERIFIED" if ok else "*** NOT VERIFIED ***"))
|
|
122
|
+
if not ok:
|
|
123
|
+
L += ["", " The two sides must agree at this rung to rounding: both are",
|
|
124
|
+
" evaluated with the whole xi sequence. A gap is then not",
|
|
125
|
+
" truncation, and the fault is upstream of the likelihood --",
|
|
126
|
+
" the transformation, the rank's differencing, the parameter",
|
|
127
|
+
" walk, the deterministic terms or the scaling -- and never in",
|
|
128
|
+
" elf() itself."]
|
|
129
|
+
if g.ll_start is not None:
|
|
130
|
+
# the optimality certificate (drvec.c:3141-3178): a plain fit only,
|
|
131
|
+
# where main() evaluated the start before fit_search
|
|
132
|
+
gap = g.fit_ll - g.ll_start
|
|
133
|
+
L += ["", " --- optimality certificate ---",
|
|
134
|
+
" logL AT the values brought in = %18.10f" % g.ll_start,
|
|
135
|
+
" logL of the diagonal fit = %18.10f" % g.fit_ll,
|
|
136
|
+
" optimality gap (fit - brought) = %+.3e" % gap,
|
|
137
|
+
"", " The gap is >= 0 always, and zero exactly when what came in were",
|
|
138
|
+
" the univariate optima. Here they %s: this input is %s." % (
|
|
139
|
+
("were", "AN OPTIMUM") if gap <= 1.0e-3 else ("were not", "A SPECIFICATION"))]
|
|
140
|
+
if gap < -1.0e-6:
|
|
141
|
+
L += ["", " A NEGATIVE gap cannot happen at a converged fit: the fitted",
|
|
142
|
+
" point is worse than the one it started from, so the optimiser",
|
|
143
|
+
" did not converge here and the fit should not be read."]
|
|
144
|
+
return "\n".join(L) + "\n"
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
def ladder_block(lad):
|
|
148
|
+
"""fit_r0_ladder with echo (drvec.c:5376-5405); nothing when it was cached."""
|
|
149
|
+
if lad is None or lad.cached:
|
|
150
|
+
return ""
|
|
151
|
+
out = "\n=== The ladder below the rank: r = 0, from the gate up ===\n"
|
|
152
|
+
for st, (a, b, c, ll) in enumerate(lad.rungs):
|
|
153
|
+
out += " rung %d (F %s, Theta %s, Sigma %s) logL = %.10f\n" % (
|
|
154
|
+
st, "diag" if a else "free", "diag" if b else "free", "diag" if c else "free", ll)
|
|
155
|
+
if st == 0 and lad.gate is not None:
|
|
156
|
+
out += gate_block(lad.gate)
|
|
157
|
+
if not lad.ok:
|
|
158
|
+
out += " rung %d did not converge (ifault = %d)\n" % (len(lad.rungs), 1)
|
|
159
|
+
return out
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
def profile_block(spec, prof):
|
|
163
|
+
"""gate_profile_seed's report (drvec.c:5750-5808), the profile's own est()
|
|
164
|
+
banner first (est writes it as it returns)."""
|
|
165
|
+
if prof is None:
|
|
166
|
+
return ""
|
|
167
|
+
out = ladder_block(prof.ladder)
|
|
168
|
+
if prof.ladder is not None and not prof.ladder.ok:
|
|
169
|
+
return out + ("\n-seedgate: the ladder below the rank did not converge; "
|
|
170
|
+
"falling back to the cold start.\n")
|
|
171
|
+
if prof.est is None:
|
|
172
|
+
return out + ("\n-seedgate: no admissible starting point for the conditional step "
|
|
173
|
+
"-- every multiple of the conditional-regression Lambda leaves the "
|
|
174
|
+
"transformed system non-stationary or non-invertible. Falling back "
|
|
175
|
+
"to the cold start.\n")
|
|
176
|
+
out += prof.est.banner()
|
|
177
|
+
if not prof.ok:
|
|
178
|
+
return out + ("\n-seedgate: the conditional step for Lambda and B2 did not converge "
|
|
179
|
+
"(ifault = %d); falling back to the cold start.\n" % prof.est.ifault)
|
|
180
|
+
M, r, s = spec.M, spec.r, spec.s
|
|
181
|
+
nmean, nlam, _nmid, _ntail = spec.blocks()
|
|
182
|
+
x2 = prof.x2
|
|
183
|
+
out += ("\n=== -seedgate: the VEC block profiled on the rung below ===\n\n"
|
|
184
|
+
" r = 0 optimum (F, Theta, Sigma) logL = %18.10f\n"
|
|
185
|
+
" admissible entry, Lambda x %-6.2f logL = %18.10f\n"
|
|
186
|
+
" + Lambda and B2 profiled on it logL = %18.10f\n\n"
|
|
187
|
+
" The starting point for the free fit below is that second value, not\n"
|
|
188
|
+
" a conditional regression. Holding the rung below and estimating only\n"
|
|
189
|
+
" Lambda and B2 lands in the INTERIOR by construction: at Lambda = 0 the\n"
|
|
190
|
+
" transformed system has an AR root of modulus exactly one, so the rung\n"
|
|
191
|
+
" below cannot be carried up unchanged, and the step off that boundary\n"
|
|
192
|
+
" is chosen here by the likelihood rather than by a constant.\n"
|
|
193
|
+
% (prof.ladder.ll, prof.mult, prof.ll_start, prof.ll))
|
|
194
|
+
out += "\n the pre-estimates the conditional step produced\n"
|
|
195
|
+
nl = spec.alpha_sa or M
|
|
196
|
+
out += " %s (%d x %d):\n" % ("psi, with Lambda = A*psi" if spec.alpha_sa else "Lambda",
|
|
197
|
+
nl, r)
|
|
198
|
+
for i in range(nl):
|
|
199
|
+
out += " " + "".join(" %12.6f" % x2[nmean + i * r + j] for j in range(r)) + "\n"
|
|
200
|
+
if spec.b2_fix is not None:
|
|
201
|
+
out += " B2 (%d x %d): held fixed by -fixb2\n" % (s, r)
|
|
202
|
+
else:
|
|
203
|
+
out += " B2 (%d x %d)%s:\n" % (s, r, ", rows held by -fixb2row marked *"
|
|
204
|
+
if spec.b2_hold else "")
|
|
205
|
+
b0 = nmean + nlam
|
|
206
|
+
nfr = s - spec.b2_nheld()
|
|
207
|
+
for i in (range(s) if spec.b2_fix is None else ()):
|
|
208
|
+
out += " "
|
|
209
|
+
for j in range(r):
|
|
210
|
+
v = spec.b2_held(i)
|
|
211
|
+
if v is not None:
|
|
212
|
+
out += " %11.6f*" % v
|
|
213
|
+
continue
|
|
214
|
+
cc = sum(1 for ii in range(i) if spec.b2_held(ii) is None)
|
|
215
|
+
out += " %12.6f" % x2[b0 + j * nfr + cc]
|
|
216
|
+
out += "\n"
|
|
217
|
+
if nmean > 0:
|
|
218
|
+
out += " mean block (%d): " % nmean + "".join(" %12.6f" % x2[i]
|
|
219
|
+
for i in range(nmean)) + "\n"
|
|
220
|
+
return out
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
def seed_block(spec, info):
|
|
224
|
+
"""What the seeding stage wrote: the ladder (r = 0) or the profile (r >= 1)."""
|
|
225
|
+
from .fit import Ladder, Profile
|
|
226
|
+
if isinstance(info, Profile):
|
|
227
|
+
return profile_block(spec, info)
|
|
228
|
+
if isinstance(info, Ladder):
|
|
229
|
+
return ladder_block(info)
|
|
230
|
+
return ""
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
def search_block(search):
|
|
234
|
+
"""fit_search with echo (drvec.c:5240-5268): the table when more than one
|
|
235
|
+
start was tried, then the winner's optimizer report."""
|
|
236
|
+
out = ""
|
|
237
|
+
if len(search.candidates) > 1:
|
|
238
|
+
out += search.table() + "\n"
|
|
239
|
+
if search.best is not None and search.best.est is not None:
|
|
240
|
+
out += search.best.est.banner()
|
|
241
|
+
return out
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
# --------------------------------------------------------------------------- #
|
|
245
|
+
# the rank test (run_lrtest, drvec.c:9516-9860) #
|
|
246
|
+
# --------------------------------------------------------------------------- #
|
|
247
|
+
|
|
248
|
+
_LR_NOTE = """
|
|
249
|
+
Distribution is the non-standard Johansen one. That MA terms do
|
|
250
|
+
not affect it (Yap and Reinsel 1995, Thm. 3; Mauricio 2006, Remark 5)
|
|
251
|
+
is proven for the CONDITIONAL likelihood, a trace test and a strictly
|
|
252
|
+
invertible MA -- not for this exact lambda-max, nor at an MA root on
|
|
253
|
+
the unit circle (BUG-27).
|
|
254
|
+
Values: case 1, MacKinnon-Haug-Michelis (1999) with no deterministic
|
|
255
|
+
term; case 2, urca 1.3.4 ca.jo(ecdet="const"), Osterwald-Lenum (1992).
|
|
256
|
+
CAUTION: the tables are for Johansen's CONDITIONAL LR. This one is
|
|
257
|
+
the EXACT likelihood, which carries W's stationary initial state and
|
|
258
|
+
is not nested at Lambda = 0: under H0 it sits about log T below
|
|
259
|
+
Johansen's and can be negative, so against these tables the test is
|
|
260
|
+
UNDERSIZED (case 1, T = 500: 3.5% at a nominal 5%). -bootstrap N
|
|
261
|
+
calibrates the statistic that is actually computed (BUG-26).
|
|
262
|
+
r = 0 is the no-cointegration null (Pi = 0, a plain VARMA on
|
|
263
|
+
nabla Y); r = M would be a stationary process in levels and is not
|
|
264
|
+
expressible here, so the sequence ends at r = M-1. Read it with
|
|
265
|
+
AIC/BIC, and check the optimizer banner of every rank before
|
|
266
|
+
trusting a statistic.
|
|
267
|
+
"""
|
|
268
|
+
|
|
269
|
+
_BOOT_HEAD = """
|
|
270
|
+
=== Parametric bootstrap under H0 (%d replications) ===
|
|
271
|
+
The asymptotic values above can be off in short samples:
|
|
272
|
+
measured on Cavaliere, Rahbek and Taylor's (2012) design (q = 0,
|
|
273
|
+
case 2), the test of the true rank rejects 11%% at T = 50 against
|
|
274
|
+
a nominal 5%%, and these percentiles bring it to 4%%; at T = 100
|
|
275
|
+
the tables are already close to nominal. They are empirical
|
|
276
|
+
percentiles of the statistic simulated FROM THE FITTED MODEL at
|
|
277
|
+
rank r, so they carry the sample size, the deterministic case and
|
|
278
|
+
the MA component that the tables cannot.
|
|
279
|
+
"""
|
|
280
|
+
|
|
281
|
+
_BOOT_TAIL = """
|
|
282
|
+
Monte Carlo error, said rather than hidden:
|
|
283
|
+
- a bootstrap p-value from B replications has standard error
|
|
284
|
+
sqrt(p(1-p)/B); at p = 0.05 and B = %d that is %.4f, so read a
|
|
285
|
+
p-value near a threshold as undecided, not as a decision;
|
|
286
|
+
- and it has a FLOOR of 1/(B+1) = %.4f -- with this B the p-value
|
|
287
|
+
cannot go below that however extreme the statistic is, which is
|
|
288
|
+
why the verdict above is read from the critical values. For a
|
|
289
|
+
p-value that can resolve 1%%, B >= 999.
|
|
290
|
+
- replications where either fit failed are discarded and
|
|
291
|
+
counted in the reps column; a NEGATIVE LR is kept, since the
|
|
292
|
+
observed statistic can be negative too (BUG-26).
|
|
293
|
+
"""
|
|
294
|
+
|
|
295
|
+
|
|
296
|
+
def _verdict(lr, cv):
|
|
297
|
+
if lr > cv[2]:
|
|
298
|
+
return " reject H0 at 1%"
|
|
299
|
+
if lr > cv[1]:
|
|
300
|
+
return " reject H0 at 5%"
|
|
301
|
+
if lr > cv[0]:
|
|
302
|
+
return " reject H0 at 10%"
|
|
303
|
+
return " H0 not rejected"
|
|
304
|
+
|
|
305
|
+
|
|
306
|
+
def rank_test_out(t, data, base):
|
|
307
|
+
"""The whole <base>.out of `-lrtest [-bootstrap N]`."""
|
|
308
|
+
spec = t.spec
|
|
309
|
+
M = spec.M
|
|
310
|
+
out = header(data, spec.but(r=1), base + ".out", lrtest=True, how=t.drift_how)
|
|
311
|
+
out += "\n=== Sequential LR test for the cointegration rank ===\n"
|
|
312
|
+
if any(c != 1.0 for c in t.scale):
|
|
313
|
+
out += (" Internal scaling (the LR does not depend on it; logLs below are in the\n"
|
|
314
|
+
" original units, the search tables in the scaled ones):"
|
|
315
|
+
+ "".join(" %s x %s" % (n, cg(c)) for n, c in zip(data.names, t.scale)) + "\n")
|
|
316
|
+
good = [f.ifault == 0 for f in t.fits]
|
|
317
|
+
for rr in range(M):
|
|
318
|
+
out += seed_block(spec.but(r=rr), t.infos[rr])
|
|
319
|
+
out += "\n--- rank r = %d ---\n" % rr
|
|
320
|
+
out += search_block(t.fits[rr])
|
|
321
|
+
out += "\n r npar logL AIC BIC\n"
|
|
322
|
+
out += " ---------------------------------------------------\n"
|
|
323
|
+
for rr in range(M):
|
|
324
|
+
if not good[rr]:
|
|
325
|
+
out += " %-4d -- estimation failed\n" % rr
|
|
326
|
+
continue
|
|
327
|
+
out += " %-4d %4d %12.4f %11.4f %11.4f\n" % (rr, t.npar[rr], t.ll[rr],
|
|
328
|
+
t.aic(rr), t.bic(rr))
|
|
329
|
+
out += "\n H0: P = r vs H1: P = r+1 LR = 2*[L(r+1) - L(r)]\n"
|
|
330
|
+
out += " (asymptotic critical values: %s)\n" % (
|
|
331
|
+
"case 1, no constant" if spec.case == 1 else
|
|
332
|
+
"case 2, restricted constant" if spec.case == 2 else "case 3 — NOT TABULATED HERE")
|
|
333
|
+
out += "\n r M-r LR 10% 5% 1%\n"
|
|
334
|
+
out += " ---------------------------------------------------\n"
|
|
335
|
+
tcr = [f.best.termcode if (f.ifault == 0 and f.best) else 0 for f in t.fits]
|
|
336
|
+
for rr in range(M - 1):
|
|
337
|
+
if not good[rr] or not good[rr + 1]:
|
|
338
|
+
out += " %-4d -- (a model in the pair failed)\n" % rr
|
|
339
|
+
continue
|
|
340
|
+
lr = t.lr(rr)
|
|
341
|
+
g = M - rr
|
|
342
|
+
line = " %-4d %4d %10.4f" % (rr, g, lr)
|
|
343
|
+
if spec.q > 0 and spec.ma in ("marow", "matri"):
|
|
344
|
+
line += (" - - - (MA class restricted"
|
|
345
|
+
" at r >= 1 only: not nested; -bootstrap)")
|
|
346
|
+
else:
|
|
347
|
+
cv = t.critical_values(rr)
|
|
348
|
+
if cv is not None:
|
|
349
|
+
line += " %8.2f %8.2f %8.2f" % cv + _verdict(lr, cv)
|
|
350
|
+
else:
|
|
351
|
+
line += " - - - (no values)"
|
|
352
|
+
if t.boundary[rr] or t.boundary[rr + 1]:
|
|
353
|
+
line += " [MA on the boundary at rank %d: no known distribution]" % (
|
|
354
|
+
rr if t.boundary[rr] else rr + 1)
|
|
355
|
+
if tcr[rr] > 2 or tcr[rr + 1] > 2:
|
|
356
|
+
line += " [check: rank %d stopped by criterion %d]" % (
|
|
357
|
+
rr if tcr[rr] > 2 else rr + 1, tcr[rr] if tcr[rr] > 2 else tcr[rr + 1])
|
|
358
|
+
if lr < 0.0:
|
|
359
|
+
line += (" [LR < 0: a fit did not reach its optimum]" if (tcr[rr] > 2 or tcr[rr + 1] > 2)
|
|
360
|
+
else " [LR < 0: exact likelihood, not nested at Lambda = 0]")
|
|
361
|
+
out += line + "\n"
|
|
362
|
+
out += _LR_NOTE
|
|
363
|
+
if t.bootstraps or t.boot_n:
|
|
364
|
+
N = t.boot_n
|
|
365
|
+
out += _BOOT_HEAD % N
|
|
366
|
+
out += "\n r M-r LR 10% 5% 1% p-value reps\n"
|
|
367
|
+
out += " -----------------------------------------------------------------\n"
|
|
368
|
+
boots = {b.h0: b for b in t.bootstraps}
|
|
369
|
+
for rr in range(M - 1):
|
|
370
|
+
b = boots.get(rr)
|
|
371
|
+
if b is None:
|
|
372
|
+
out += " %-4d -- (a model in the pair failed)\n" % rr
|
|
373
|
+
continue
|
|
374
|
+
lr = t.lr(rr)
|
|
375
|
+
if b.i1r is not None:
|
|
376
|
+
out += (" %-4d %4d %10.4f the model under H0 is not I(1, %d): not reported\n"
|
|
377
|
+
" smallest AR root of Ybar %.6f (> 1 is stationary); "
|
|
378
|
+
"nabla Y2's long-run\n"
|
|
379
|
+
" rank measure %.2e (0 = a trend lost; Cavaliere, "
|
|
380
|
+
"Rahbek and Taylor 2012)\n" % (rr, M - rr, lr, rr, b.i1r[0], b.i1r[1]))
|
|
381
|
+
continue
|
|
382
|
+
if b.cv is None:
|
|
383
|
+
out += (" %-4d %4d %10.4f only %d usable replications: not reported\n"
|
|
384
|
+
% (rr, M - rr, lr, b.nok))
|
|
385
|
+
continue
|
|
386
|
+
out += " %-4d %4d %10.4f %8.2f %8.2f %8.2f %7.4f %4d" % (
|
|
387
|
+
rr, M - rr, lr, b.cv[0], b.cv[1], b.cv[2], b.pvalue, b.nok)
|
|
388
|
+
out += _verdict(lr, b.cv) + "\n"
|
|
389
|
+
out += "\n Every replication, with its LR*, is in %s.boot.\n" % base
|
|
390
|
+
out += _BOOT_TAIL % (N, math.sqrt(0.05 * 0.95 / N), 1.0 / (N + 1))
|
|
391
|
+
return out
|
|
392
|
+
|
|
393
|
+
|
|
394
|
+
# --------------------------------------------------------------------------- #
|
|
395
|
+
# the report of one fit (report_fit, drvec.c:7797-9045) #
|
|
396
|
+
# --------------------------------------------------------------------------- #
|
|
397
|
+
|
|
398
|
+
DASHBAR = "-" * 72
|
|
399
|
+
|
|
400
|
+
|
|
401
|
+
def banner(title):
|
|
402
|
+
return "\n\n%s\n %s\n%s\n" % (BAR, title, BAR)
|
|
403
|
+
|
|
404
|
+
|
|
405
|
+
def _sig(p):
|
|
406
|
+
return ("***" if p < 0.001 else "**" if p < 0.01 else "*" if p < 0.05
|
|
407
|
+
else "." if p < 0.10 else " ")
|
|
408
|
+
|
|
409
|
+
|
|
410
|
+
def par_row(label, est, se):
|
|
411
|
+
"""par_row (drvec.c:2874): 2 Q(|t|) is GSL's ugaussian_Q, here erfc."""
|
|
412
|
+
if se > 0.0:
|
|
413
|
+
t = est / se
|
|
414
|
+
pv = math.erfc(abs(t) / math.sqrt(2.0))
|
|
415
|
+
return "%-28s %13.6f %12.6f %8.3f %7.4f %s\n" % (label, est, se, t, pv, _sig(pv))
|
|
416
|
+
return "%-28s %13.6f %12s %8s %7s\n" % (label, est, "-", "-", "-")
|
|
417
|
+
|
|
418
|
+
|
|
419
|
+
_CONV = {1: ("clean convergence, on the scaled gradient", None),
|
|
420
|
+
2: ("stopped on steptol, NOT on the gradient",
|
|
421
|
+
"the step collapsed while the gradient may still be appreciable; "
|
|
422
|
+
"treat the standard errors with caution"),
|
|
423
|
+
3: ("NOT a convergence: the line search failed to improve",
|
|
424
|
+
"the estimates are a stationary-ish point, not a demonstrated "
|
|
425
|
+
"maximum. See docs/CONVERGENCE.md"),
|
|
426
|
+
4: ("NOT a convergence: the optimiser gave up",
|
|
427
|
+
"every criterion derived from this fit -- standard errors, "
|
|
428
|
+
"AIC/BIC, any LR -- is unreliable"),
|
|
429
|
+
5: ("NOT a convergence: the optimiser gave up",
|
|
430
|
+
"every criterion derived from this fit -- standard errors, "
|
|
431
|
+
"AIC/BIC, any LR -- is unreliable")}
|
|
432
|
+
|
|
433
|
+
|
|
434
|
+
def _termcode_from_text(text):
|
|
435
|
+
"""termcode_from_out (drvec.c:2598): the LAST criterion line written so far."""
|
|
436
|
+
code = 0
|
|
437
|
+
for line in text.split("\n"):
|
|
438
|
+
if "Convergence criterion:" not in line:
|
|
439
|
+
continue
|
|
440
|
+
code = (1 if "gradtol" in line else 2 if "steptol" in line else
|
|
441
|
+
3 if "lower point" in line else 4 if "iteration limit" in line else
|
|
442
|
+
5 if "maximum length" in line else code)
|
|
443
|
+
return code
|
|
444
|
+
|
|
445
|
+
|
|
446
|
+
class _Fit:
|
|
447
|
+
"""The final structures report_fit reads, in the C's 1-based layout where it
|
|
448
|
+
matters (Lam_m, F_m, Th_m, Qm internal; B2m, mu_m in the .inp's order)."""
|
|
449
|
+
|
|
450
|
+
def __init__(self, res):
|
|
451
|
+
from .cast import shootx, unpack
|
|
452
|
+
spec = res.spec
|
|
453
|
+
self.spec = spec
|
|
454
|
+
rd = res.data.at(spec.r)
|
|
455
|
+
self.rd = rd
|
|
456
|
+
self.mu, self.phi, self.theta, self.qq, self.w, _ = shootx(spec, rd, res.x)
|
|
457
|
+
mu, Lam, F, Th, S, B2, dtr = unpack(spec, res.x)
|
|
458
|
+
self.dtr = dtr
|
|
459
|
+
self.Lam, self.F, self.Th, self.Q, self.B2 = Lam, F, Th, S, B2
|
|
460
|
+
nmean, nlam, _nmid, _nt = spec.blocks()
|
|
461
|
+
mu_m = []
|
|
462
|
+
k = 0
|
|
463
|
+
if spec.case == 2:
|
|
464
|
+
mu_m = list(res.x[:spec.r]); k = spec.r
|
|
465
|
+
elif spec.case == 3:
|
|
466
|
+
for i in range(spec.s):
|
|
467
|
+
if spec.mu_free(i):
|
|
468
|
+
mu_m.append(res.x[k]); k += 1
|
|
469
|
+
else:
|
|
470
|
+
mu_m.append(0.0)
|
|
471
|
+
mu_m += list(res.x[k:k + spec.r]); k += spec.r
|
|
472
|
+
self.mu_m = mu_m
|
|
473
|
+
from .loglik import elf
|
|
474
|
+
_f1, _f2, a, _ifa = elf(spec, rd, res.x, atf=True)
|
|
475
|
+
self.a = [[float(a[t, i]) for i in range(1, spec.M + 1)] for t in range(1, rd.n + 1)]
|
|
476
|
+
|
|
477
|
+
|
|
478
|
+
def fit_out(res, base):
|
|
479
|
+
"""The whole <base>.out of a single fit."""
|
|
480
|
+
data, spec = res.data, res.spec
|
|
481
|
+
out = header(data, spec, base + ".out", how=getattr(res, "drift_how", None))
|
|
482
|
+
if getattr(res, "alpha_free", None) is not None:
|
|
483
|
+
out += res.alpha_free.banner() # H(r)'s est(), into the .out
|
|
484
|
+
if spec.xlink:
|
|
485
|
+
out += "\n=== -xlink: the VEC rung below, without the transfer inputs ===\n"
|
|
486
|
+
out += seed_block(spec.but(xlink=()), res.xl_base_info)
|
|
487
|
+
out += "logL of the VEC without the inputs: %18.10f\n" % res.xl_base_ll
|
|
488
|
+
out += seed_block(spec, res.seed_info)
|
|
489
|
+
out += search_block(res.search)
|
|
490
|
+
out += _report_fit(res, out)
|
|
491
|
+
if getattr(res, "irfboot", None) is not None:
|
|
492
|
+
from .irfboot import bands_text
|
|
493
|
+
out += bands_text(res.irfboot, res.spec, res.names, base)[0]
|
|
494
|
+
return out
|
|
495
|
+
|
|
496
|
+
|
|
497
|
+
def restriction_lr(res):
|
|
498
|
+
"""The LR the .out prints for a restricted fit, as numbers: None when the
|
|
499
|
+
fit carries none. Otherwise a dict: kind ("xlink", "fixb2row", "xsys",
|
|
500
|
+
"gzero", "gzero+alpha" or "alpha"), ll_free (the unrestricted logL; for -xlink the VEC WITHOUT the
|
|
501
|
+
inputs, i.e. the restricted one), ll_restricted, lr, df and pvalue (chi2,
|
|
502
|
+
1.0 when lr <= 0 or df = 0, as the C prints it). The branches and their
|
|
503
|
+
order are _report_fit's."""
|
|
504
|
+
spec = res.spec
|
|
505
|
+
M, r = spec.M, spec.r
|
|
506
|
+
if spec.xlink and res.xl_base_ll is not None:
|
|
507
|
+
from .xlink import npar as xl_npar
|
|
508
|
+
df = xl_npar(spec.xlink)
|
|
509
|
+
lr = 2.0 * (res.loglik - res.xl_base_ll)
|
|
510
|
+
pv = _chisq_q(lr, df) if (df > 0 and lr > 0.0) else 1.0
|
|
511
|
+
return {"kind": "xlink", "ll_free": res.loglik, "ll_restricted": res.xl_base_ll,
|
|
512
|
+
"lr": lr, "df": df, "pvalue": pv, "ll_bridge": res.xl_bridge_ll}
|
|
513
|
+
if spec.b2_hold and getattr(res, "lr_free", None) is not None:
|
|
514
|
+
nh = spec.b2_nheld()
|
|
515
|
+
dfa = (M - spec.alpha_sa) * r if spec.alpha else 0
|
|
516
|
+
df = nh * r + dfa
|
|
517
|
+
kind = "fixb2row+alpha" if spec.alpha else "fixb2row"
|
|
518
|
+
elif spec.xsys and getattr(res, "lr_free", None) is not None:
|
|
519
|
+
df = res.xs_df
|
|
520
|
+
kind = "xsys"
|
|
521
|
+
elif spec.gzero and getattr(res, "lr_free", None) is not None:
|
|
522
|
+
df = res.gz_df
|
|
523
|
+
kind = "gzero+alpha" if spec.alpha else "gzero"
|
|
524
|
+
elif spec.alpha and getattr(res, "lr_free", None) is not None:
|
|
525
|
+
df = (M - spec.alpha_sa) * r
|
|
526
|
+
kind = "alpha"
|
|
527
|
+
else:
|
|
528
|
+
return None
|
|
529
|
+
lr = 2.0 * (res.lr_free - res.loglik)
|
|
530
|
+
pv = _chisq_q(lr, df) if (df > 0 and lr > 0.0) else 1.0
|
|
531
|
+
return {"kind": kind, "ll_free": res.lr_free, "ll_restricted": res.loglik,
|
|
532
|
+
"lr": lr, "df": df, "pvalue": pv}
|
|
533
|
+
|
|
534
|
+
|
|
535
|
+
def _report_fit(res, sofar):
|
|
536
|
+
spec, data = res.spec, res.data
|
|
537
|
+
M, r, s, q = spec.M, spec.r, spec.s, spec.q
|
|
538
|
+
names = data.names
|
|
539
|
+
from .params import RowOrder
|
|
540
|
+
ro = RowOrder(M, r)
|
|
541
|
+
i2l, l2i = ro.inp2lam, ro.lam2inp
|
|
542
|
+
fv = _Fit(res)
|
|
543
|
+
x, dev = res.x, res.se
|
|
544
|
+
n = fv.rd.n
|
|
545
|
+
npar = len(x)
|
|
546
|
+
code = _termcode_from_text(sofar)
|
|
547
|
+
head, note = _CONV.get(code, ("the termination criterion could not be read", None))
|
|
548
|
+
o = ""
|
|
549
|
+
fh = getattr(res, "fdhess", None)
|
|
550
|
+
if fh is not None:
|
|
551
|
+
if fh.ok:
|
|
552
|
+
o += "Standard errors : finite-difference Hessian at the optimum (-fdhess)\n"
|
|
553
|
+
else: # BUG-34: said in the .out too
|
|
554
|
+
o += ("Standard errors : BFGS-accumulated factor -- -fdhess was asked for "
|
|
555
|
+
"and NOT used:\n %s\n" % fh.why)
|
|
556
|
+
o += "Convergence : %s\n" % head
|
|
557
|
+
if note:
|
|
558
|
+
o += " ! %s\n" % note
|
|
559
|
+
o += "sigma2 : %15.10f\n" % res.sigma2
|
|
560
|
+
o += "logelf : %15.10f\n" % res.loglik
|
|
561
|
+
o += "npar : %d\n" % npar
|
|
562
|
+
o += "AIC : %15.10f (-2logL + 2k, /n)\n" % ((-2.0 * res.loglik + 2.0 * npar) / n)
|
|
563
|
+
o += "BIC : %15.10f (-2logL + k log n, /n)\n" % (
|
|
564
|
+
(-2.0 * res.loglik + npar * math.log(n)) / n)
|
|
565
|
+
rlr = restriction_lr(res)
|
|
566
|
+
if spec.xlink and res.xl_base_ll is not None:
|
|
567
|
+
df, lr, pv = rlr["df"], rlr["lr"], rlr["pvalue"]
|
|
568
|
+
o += "\n--- the transfer inputs (-xlink), against the VEC without them ---\n"
|
|
569
|
+
o += "logL VEC without inputs : %15.10f\n" % res.xl_base_ll
|
|
570
|
+
if res.xl_bridge_ll is not None:
|
|
571
|
+
o += ("logL at the bridge (omega=0) : %15.10f (difference %.2e: the crossing "
|
|
572
|
+
"identity of this rung)\n" % (res.xl_bridge_ll,
|
|
573
|
+
res.xl_bridge_ll - res.xl_base_ll))
|
|
574
|
+
o += "logL with the inputs : %15.10f\n" % res.loglik
|
|
575
|
+
o += "LR = 2(inputs - VEC) : %15.10f\n" % lr
|
|
576
|
+
o += "degrees of freedom : %d sum of (s+1+r)\n" % df
|
|
577
|
+
o += "p-value (chi2) : %15.10f\n" % pv
|
|
578
|
+
o += (" The chi2 needs the rank to be the same under both models and X fixed;\n"
|
|
579
|
+
" the rank test itself is NOT recalibrated for exogenous inputs (TODO.md "
|
|
580
|
+
"MEJORA-2).\n")
|
|
581
|
+
if spec.b2_hold and getattr(res, "lr_free", None) is not None:
|
|
582
|
+
df, lr, pv = rlr["df"], rlr["lr"], rlr["pvalue"]
|
|
583
|
+
o += "\n--- beta restricted by -fixb2row%s, against H(r) ---\n" % (
|
|
584
|
+
" and alpha = A*psi" if spec.alpha else "")
|
|
585
|
+
for i in range(s):
|
|
586
|
+
v = spec.b2_held(i)
|
|
587
|
+
if v is not None:
|
|
588
|
+
o += "row %d of B2 (%s) held at %s in every relation\n" % (i + 1, names[i], cg(v))
|
|
589
|
+
o += "logL H(r) free : %15.10f\n" % res.lr_free
|
|
590
|
+
o += "logL restricted : %15.10f\n" % res.loglik
|
|
591
|
+
o += "LR = 2(free - restr.) : %15.10f\n" % lr
|
|
592
|
+
o += "degrees of freedom : %d (rows held)*r%s\n" % (
|
|
593
|
+
df, " + (M - sa)*r" if spec.alpha else "")
|
|
594
|
+
o += "p-value (chi2) : %15.10f\n" % pv
|
|
595
|
+
elif spec.xsys and getattr(res, "lr_free", None) is not None:
|
|
596
|
+
df, lr, pv = rlr["df"], rlr["lr"], rlr["pvalue"]
|
|
597
|
+
o += ("\n--- the inputs embedded by -xsys: strong exogeneity, against the free "
|
|
598
|
+
"VEC ---\n")
|
|
599
|
+
for i in spec.xsys:
|
|
600
|
+
o += ("%s: alpha = 0 and no other series in its short run (Gamma(k) row)\n"
|
|
601
|
+
% names[i])
|
|
602
|
+
o += "logL VEC free : %15.10f\n" % res.lr_free
|
|
603
|
+
o += "logL inputs embedded : %15.10f\n" % res.loglik
|
|
604
|
+
o += "LR = 2(free - restr.) : %15.10f\n" % lr
|
|
605
|
+
o += ("degrees of freedom : %d r*(inputs) + (p-1)*(Gamma entries held)\n"
|
|
606
|
+
% df)
|
|
607
|
+
o += "p-value (chi2) : %15.10f\n" % pv
|
|
608
|
+
o += (" Not rejecting is what licenses reading the rows of the outputs as a\n"
|
|
609
|
+
" transfer from the inputs: their entry in beta (the gain), their lags\n"
|
|
610
|
+
" in Gamma and the covariance Q (omega_0, the SVAR identity).\n")
|
|
611
|
+
elif spec.gzero and getattr(res, "lr_free", None) is not None:
|
|
612
|
+
df, lr, pv = rlr["df"], rlr["lr"], rlr["pvalue"]
|
|
613
|
+
dfa = (M - spec.alpha_sa) * r if spec.alpha else 0
|
|
614
|
+
o += "\n--- Gamma(k) held by -gzero%s, against the free VEC ---\n" % (
|
|
615
|
+
" and alpha = A*psi" if spec.alpha else "")
|
|
616
|
+
for i, j in sorted(spec.gzero):
|
|
617
|
+
o += "D.%s <- D.%s held at 0 for k = 1..%d\n" % (names[i], names[j], spec.p - 1)
|
|
618
|
+
o += "logL VEC free : %15.10f\n" % res.lr_free
|
|
619
|
+
o += "logL restricted : %15.10f\n" % res.loglik
|
|
620
|
+
o += "LR = 2(free - restr.) : %15.10f\n" % lr
|
|
621
|
+
o += "degrees of freedom : %d (p-1)*(entries held) = %d\n" % (df, df - dfa)
|
|
622
|
+
if dfa:
|
|
623
|
+
o += " + (M - sa)*r = %d\n" % dfa
|
|
624
|
+
o += "p-value (chi2) : %15.10f\n" % pv
|
|
625
|
+
elif spec.alpha and getattr(res, "lr_free", None) is not None:
|
|
626
|
+
df, lr, pv = rlr["df"], rlr["lr"], rlr["pvalue"]
|
|
627
|
+
o += "\n--- H1(r): alpha = A*psi, against H(r) ---\n"
|
|
628
|
+
o += "logL H(r) free : %15.10f\n" % res.lr_free
|
|
629
|
+
o += "logL H1(r) restricted : %15.10f\n" % res.loglik
|
|
630
|
+
o += "LR = 2(free - restr.) : %15.10f\n" % lr
|
|
631
|
+
o += "degrees of freedom : %d (M - sa)*r\n" % df
|
|
632
|
+
o += "p-value (chi2) : %15.10f\n" % pv
|
|
633
|
+
|
|
634
|
+
# the walk, with the index of every slot (ix_* of report_fit)
|
|
635
|
+
labs_idx = 0
|
|
636
|
+
nmean, nlam, _nmid, _nt = spec.blocks()
|
|
637
|
+
ix_mu = [] # per entry of mu_m: its slot, or None when held
|
|
638
|
+
idx = 0
|
|
639
|
+
if spec.case == 2:
|
|
640
|
+
ix_mu = list(range(r)); idx = r
|
|
641
|
+
elif spec.case == 3:
|
|
642
|
+
for i in range(s):
|
|
643
|
+
if spec.mu_free(i):
|
|
644
|
+
ix_mu.append(idx); idx += 1
|
|
645
|
+
else:
|
|
646
|
+
ix_mu.append(None)
|
|
647
|
+
ix_mu += list(range(idx, idx + r)); idx += r
|
|
648
|
+
ix_tr = []
|
|
649
|
+
if spec.trend:
|
|
650
|
+
ix_tr = list(range(idx, idx + r)); idx += r
|
|
651
|
+
ix_lam = {}
|
|
652
|
+
ix_psi = []
|
|
653
|
+
if spec.alpha_sa:
|
|
654
|
+
ix_psi = list(range(idx, idx + spec.alpha_sa * r)); idx += spec.alpha_sa * r
|
|
655
|
+
else:
|
|
656
|
+
for i in range(M):
|
|
657
|
+
for j in range(r):
|
|
658
|
+
ix_lam[(i, j)] = idx; idx += 1
|
|
659
|
+
ix_F = {}
|
|
660
|
+
for k in range(spec.nf):
|
|
661
|
+
for i in range(M):
|
|
662
|
+
for j in range(M):
|
|
663
|
+
if spec.f_free(i, j):
|
|
664
|
+
ix_F[(k, i, j)] = idx; idx += 1
|
|
665
|
+
ix_Th = {}
|
|
666
|
+
cls = spec.theta_class()
|
|
667
|
+
for k in range(q):
|
|
668
|
+
if cls == "diag":
|
|
669
|
+
cells = [(i, i) for i in range(M)]
|
|
670
|
+
elif cls in ("marow", "matri"):
|
|
671
|
+
cells = [(i, j) for i in range(r) for j in range(M)]
|
|
672
|
+
if cls == "matri":
|
|
673
|
+
cells += [(i, j) for i in range(r, M) for j in range(r, M)]
|
|
674
|
+
else:
|
|
675
|
+
cells = [(i, j) for i in range(M) for j in range(M)]
|
|
676
|
+
for (i, j) in cells:
|
|
677
|
+
ix_Th[(k, i, j)] = idx; idx += 1
|
|
678
|
+
ix_q0 = idx
|
|
679
|
+
idx += spec.nsig()
|
|
680
|
+
ix_B2 = {}
|
|
681
|
+
for j in range(r):
|
|
682
|
+
for i in range(s):
|
|
683
|
+
if spec.b2_held(i) is None:
|
|
684
|
+
ix_B2[(i, j)] = idx; idx += 1
|
|
685
|
+
del labs_idx
|
|
686
|
+
|
|
687
|
+
o += banner("Estimated Parameters and Standard Deviations")
|
|
688
|
+
o += ("\n Gamma(L) (nabla Y_t - delta) = alpha (beta'Y_{t-1} - E[W]) + Theta(L) A_t,"
|
|
689
|
+
" delta = E[nabla Y]\n" if spec.case == 3 else
|
|
690
|
+
"\n nabla Y_t = alpha (beta'Y_{t-1} - E[W]) + Gamma_1 nabla Y_{t-1} + ... "
|
|
691
|
+
"+ Theta(L) A_t\n")
|
|
692
|
+
o += (" with Pi = alpha beta' of rank r, and beta = [I_r ; beta_2] normalised on Y1.\n"
|
|
693
|
+
" Johansen's notation throughout: a NEGATIVE alpha is error-correcting.\n")
|
|
694
|
+
o += (" Rows read `equation <- regressor`; D.x is nabla x, A.x its innovation,\n"
|
|
695
|
+
" and ec%s the equilibrium error%s beta'Y_{t-1}.\n" % (
|
|
696
|
+
" is" if r == 1 else "1..ec%d are", "" if r == 1 else "s"))
|
|
697
|
+
o += "\n%-28s %13s %12s %8s %7s\n" % ("Parameter", "Estimate", "Std.Error", "t-stat", "p-val")
|
|
698
|
+
o += DASHBAR + "\n"
|
|
699
|
+
if ix_mu:
|
|
700
|
+
o += "\n%s\n" % ("Means: of the drift in nabla Y2, and of the equilibrium error"
|
|
701
|
+
if spec.case == 3 else
|
|
702
|
+
"Mean of the equilibrium error (the constant restricted to the relation)")
|
|
703
|
+
for a in range(len(ix_mu)):
|
|
704
|
+
if spec.case == 3 and a < s:
|
|
705
|
+
lb = " E[D.%s]" % names[a]
|
|
706
|
+
else:
|
|
707
|
+
lb = " E[ec%d]" % ((a - s + 1) if spec.case == 3 else a + 1)
|
|
708
|
+
if ix_mu[a] is not None:
|
|
709
|
+
o += par_row(lb, x[ix_mu[a]], dev[ix_mu[a]])
|
|
710
|
+
else:
|
|
711
|
+
o += "%-28s %13.6f held: no drift (%s)\n" % (
|
|
712
|
+
lb, 0.0, "-case pre" if getattr(res, "drift_how", None) == "pre"
|
|
713
|
+
else "-nodrift")
|
|
714
|
+
if spec.trend:
|
|
715
|
+
o += ("Trend of the equilibrium error (case 4), per period, t centred at "
|
|
716
|
+
"mid-sample\n")
|
|
717
|
+
for a in range(r):
|
|
718
|
+
o += par_row(" trend[ec%d]" % (a + 1), x[ix_tr[a]], dev[ix_tr[a]])
|
|
719
|
+
if r > 0:
|
|
720
|
+
o += "\nAdjustment coefficients, alpha (M x r) [negative = error-correcting]\n"
|
|
721
|
+
if spec.alpha_sa:
|
|
722
|
+
o += " free part psi of alpha = A*psi; Lambda = A*psi is in the model block\n"
|
|
723
|
+
for a in range(spec.alpha_sa):
|
|
724
|
+
for b in range(r):
|
|
725
|
+
ii = ix_psi[a * r + b]
|
|
726
|
+
o += par_row(" psi[%d <- ec%d]" % (a + 1, b + 1), x[ii], dev[ii])
|
|
727
|
+
else:
|
|
728
|
+
for a in range(M):
|
|
729
|
+
for b in range(r):
|
|
730
|
+
ii = ix_lam[(i2l(a), b)]
|
|
731
|
+
o += par_row(" D.%s <- ec%d" % (names[a], b + 1), -x[ii], dev[ii])
|
|
732
|
+
if r > 0 and s > 0:
|
|
733
|
+
o += "\nCointegrating vectors, beta = [I_r ; beta_2], normalised on the Y1 block\n"
|
|
734
|
+
if spec.b2_fix is not None:
|
|
735
|
+
o += " beta_2 held fixed: imposed, not estimated\n"
|
|
736
|
+
for a in (range(s) if spec.b2_fix is None else ()):
|
|
737
|
+
v = spec.b2_held(a)
|
|
738
|
+
if v is not None:
|
|
739
|
+
o += (" %s in ec*: held at %s by -fixb2row (imposed, not estimated)\n"
|
|
740
|
+
% (names[a], cg(v)))
|
|
741
|
+
for b in range(r):
|
|
742
|
+
for a in range(s):
|
|
743
|
+
if (a, b) in ix_B2:
|
|
744
|
+
ii = ix_B2[(a, b)]
|
|
745
|
+
o += par_row(" %s in ec%d" % (names[a], b + 1), x[ii], dev[ii])
|
|
746
|
+
if spec.xlink:
|
|
747
|
+
o += _xlink_rows(res, idx)
|
|
748
|
+
if spec.nf > 0:
|
|
749
|
+
o += "\nShort-run dynamics, Gamma(k) on nabla Y_{t-k}\n"
|
|
750
|
+
for k in range(spec.nf):
|
|
751
|
+
for a in range(M):
|
|
752
|
+
for b in range(M):
|
|
753
|
+
key = (k, i2l(a), i2l(b))
|
|
754
|
+
if key in ix_F:
|
|
755
|
+
ii = ix_F[key]
|
|
756
|
+
o += par_row(" D.%s <- D.%s(-%d)" % (names[a], names[b], k + 1),
|
|
757
|
+
x[ii], dev[ii])
|
|
758
|
+
if q > 0:
|
|
759
|
+
bnd = res.ma_on_boundary
|
|
760
|
+
o += "\nMoving average on the innovations, Theta(k)%s\n" % (
|
|
761
|
+
" [an MA root is ON the unit circle: s.e. not defined, see the roots below]"
|
|
762
|
+
if bnd else "")
|
|
763
|
+
for k in range(q):
|
|
764
|
+
for a in range(M):
|
|
765
|
+
for b in range(M):
|
|
766
|
+
key = (k, i2l(a), i2l(b))
|
|
767
|
+
if key in ix_Th:
|
|
768
|
+
ii = ix_Th[key]
|
|
769
|
+
o += par_row(" D.%s <- A.%s(-%d)" % (names[a], names[b], k + 1),
|
|
770
|
+
x[ii], 0.0 if bnd else dev[ii])
|
|
771
|
+
nm = lambda a: names[l2i(a)] # noqa: E731
|
|
772
|
+
o += "\nInnovation covariance up to scale, Q (Sigma = sigma2 * Q, var %s = 1)\n" % nm(0)
|
|
773
|
+
qi = ix_q0
|
|
774
|
+
Q = fv.Q
|
|
775
|
+
for a in range(1, M):
|
|
776
|
+
o += par_row(" var %s" % nm(a), Q[a][a], dev[qi]); qi += 1
|
|
777
|
+
if not spec.diag_cov:
|
|
778
|
+
for a in range(1, M):
|
|
779
|
+
for b in range(a):
|
|
780
|
+
o += par_row(" cov %s, %s" % (nm(a), nm(b)), Q[a][b], dev[qi]); qi += 1
|
|
781
|
+
o += DASHBAR + "\n"
|
|
782
|
+
o += "Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1\n"
|
|
783
|
+
if fh is None:
|
|
784
|
+
o += (" ! standard errors from the BFGS-accumulated factor; -fdhess uses the Hessian "
|
|
785
|
+
"at the optimum\n")
|
|
786
|
+
elif not fh.ok:
|
|
787
|
+
o += (" ! standard errors from the BFGS-accumulated factor (-fdhess could not be "
|
|
788
|
+
"used, see the header)\n")
|
|
789
|
+
o += _vecm_block(res, fv, ro)
|
|
790
|
+
o += _coint_block(res, fv, ro)
|
|
791
|
+
o += _hypothesis_block(res, ro, ix_lam, ix_B2, ix_F, ix_Th)
|
|
792
|
+
o += level_irf_fevd(res, fv, 10 if fv.rd.n < 40 else 20)
|
|
793
|
+
o += residual_diagnostics(res, fv)
|
|
794
|
+
o += operator_roots(res, fv)
|
|
795
|
+
o += diagnose_ybar(res, fv)
|
|
796
|
+
H = getattr(res, "forecast_h", 0)
|
|
797
|
+
if H > 0:
|
|
798
|
+
from .forecast import forecast_vec
|
|
799
|
+
o += forecast_vec(res, fv, H, getattr(res, "_base", "") or "")[0]
|
|
800
|
+
E = getattr(res, "estwin", 0)
|
|
801
|
+
if E > 0:
|
|
802
|
+
if H < 1:
|
|
803
|
+
o += "\n(-estwin needs a horizon: give -f H)\n"
|
|
804
|
+
else:
|
|
805
|
+
from .forecast import rolling_eval
|
|
806
|
+
rv = rolling_eval(res, E, H)
|
|
807
|
+
o += rv[0] if rv else "\n(-estwin %d leaves no room for %d steps)\n" % (E, H)
|
|
808
|
+
return o
|
|
809
|
+
|
|
810
|
+
|
|
811
|
+
def diagnose_ybar(res, fv):
|
|
812
|
+
"""diagnose_ybar (drvec.c:7763): the residuals are Ybar's, [nabla Y2 ; W],
|
|
813
|
+
labelled with the Y2 names and ec1..ecr; dated from the first estimated
|
|
814
|
+
observation (BUG-37)."""
|
|
815
|
+
from .diagnose import diagnose
|
|
816
|
+
spec, data = res.spec, res.data
|
|
817
|
+
s = spec.s
|
|
818
|
+
names = list(data.names[:s]) + ["ec%d" % (j + 1) for j in range(spec.r)]
|
|
819
|
+
return diagnose(fv.a, names, data.freq, data.start, data.nobs_raw - fv.rd.n,
|
|
820
|
+
diag_npar_ser(spec))
|
|
821
|
+
|
|
822
|
+
|
|
823
|
+
def _vecm_block(res, fv, ro):
|
|
824
|
+
spec, data = res.spec, res.data
|
|
825
|
+
M, r, s, q = spec.M, spec.r, spec.s, spec.q
|
|
826
|
+
names = data.names
|
|
827
|
+
i2l, l2i = ro.inp2lam, ro.lam2inp
|
|
828
|
+
Lam, F, Th, Q, B2 = fv.Lam, fv.F, fv.Th, fv.Q, fv.B2
|
|
829
|
+
o = banner("Vector Error Correction Model")
|
|
830
|
+
if spec.case == 3:
|
|
831
|
+
o += ("\n Gamma(L) (nabla Y_t - delta) = alpha (beta'Y_{t-1} - E[W]) + Theta(L) A_t\n"
|
|
832
|
+
" Gamma(L) = I - Gamma_1 L - ..., Theta(L) = I - Theta1 L - ... - Theta_q L^q,"
|
|
833
|
+
" Pi = alpha beta'\n"
|
|
834
|
+
" delta = E[nabla Y], the drift: E[nabla Y2] on the Y2 block, -beta_2' "
|
|
835
|
+
"E[nabla Y2] on Y1\n\n")
|
|
836
|
+
else:
|
|
837
|
+
o += ("\n nabla Y_t = alpha (beta'Y_{t-1} - E[W]) + Gamma_1 nabla Y_{t-1} + ... "
|
|
838
|
+
"+ Theta(L) A_t\n"
|
|
839
|
+
" Theta(L) = I - Theta1 L - ... - Theta_q L^q, Pi = alpha beta'\n\n")
|
|
840
|
+
mu_m = fv.mu_m
|
|
841
|
+
if mu_m:
|
|
842
|
+
o += "%s vector:\n" % ("E[nablaY2] and E[W]" if spec.case == 3 else "E[W]")
|
|
843
|
+
for v in mu_m:
|
|
844
|
+
o += " %12.6f\n" % v
|
|
845
|
+
if spec.case == 3 and len(mu_m) >= s:
|
|
846
|
+
o += "delta = E[nabla Y] (M), rows in the .inp's order:\n"
|
|
847
|
+
for a in range(M):
|
|
848
|
+
if a < s:
|
|
849
|
+
d = mu_m[a]
|
|
850
|
+
else:
|
|
851
|
+
d = 0.0
|
|
852
|
+
for i3 in range(s):
|
|
853
|
+
d -= B2[i3][a - s] * mu_m[i3]
|
|
854
|
+
if spec.trend:
|
|
855
|
+
d += fv.dtr[a - s]
|
|
856
|
+
o += " %12.6f\n" % d
|
|
857
|
+
o += "alpha matrix (M x r), rows in the .inp's order:\n"
|
|
858
|
+
for a in range(M):
|
|
859
|
+
o += " " + "".join("%12.6f" % -Lam[i2l(a)][b] for b in range(r)) + "\n"
|
|
860
|
+
for k in range(spec.nf):
|
|
861
|
+
o += "Gamma(%d) matrix, rows and columns in the .inp's order:\n" % (k + 1)
|
|
862
|
+
for a in range(M):
|
|
863
|
+
o += " " + "".join("%12.6f" % F[k][i2l(a)][i2l(b)] for b in range(M)) + "\n"
|
|
864
|
+
cls = spec.theta_class()
|
|
865
|
+
for k in range(q):
|
|
866
|
+
o += "theta(%d) matrix%s:\n" % (k + 1, (
|
|
867
|
+
" (the equilibrium errors do not enter the nabla Y2 equations with a lag)"
|
|
868
|
+
if cls == "matri" else
|
|
869
|
+
" (the nabla Y2 block carries no MA of its own)" if cls == "marow" else ""))
|
|
870
|
+
for a in range(M):
|
|
871
|
+
o += " " + "".join("%12.6f" % Th[k][i2l(a)][i2l(b)] for b in range(M)) + "\n"
|
|
872
|
+
o += "beta_2 matrix (s x r)%s:\n" % (
|
|
873
|
+
" [rows held by -fixb2row at the values given]" if spec.b2_hold else
|
|
874
|
+
(" [FIXED at the value given]" if spec.fixb2_given else
|
|
875
|
+
" [FIXED at its static-OLS estimate: data-chosen, so an LR against the free "
|
|
876
|
+
"model is not valid]") if spec.b2_fix is not None else "")
|
|
877
|
+
for a in range(s):
|
|
878
|
+
o += " " + "".join("%12.6f" % B2[a][b] for b in range(r)) + "\n"
|
|
879
|
+
o += "beta matrix (M x r), rows in the .inp's order:\n"
|
|
880
|
+
for row in range(M):
|
|
881
|
+
o += " " + "".join("%12.6f" % (B2[row][c] if row < s else (1.0 if row - s == c else 0.0))
|
|
882
|
+
for c in range(r)) + "\n"
|
|
883
|
+
if r > 0:
|
|
884
|
+
Pi = [[0.0] * M for _ in range(M)]
|
|
885
|
+
for a in range(M):
|
|
886
|
+
for b in range(M):
|
|
887
|
+
acc = 0.0
|
|
888
|
+
for j in range(r):
|
|
889
|
+
Bbj = B2[b][j] if b < s else (1.0 if b - s == j else 0.0)
|
|
890
|
+
acc -= Lam[i2l(a)][j] * Bbj
|
|
891
|
+
Pi[a][b] = acc
|
|
892
|
+
o += "Pi = alpha beta' matrix (M x M), the long run:\n"
|
|
893
|
+
for a in range(M):
|
|
894
|
+
o += " " + "".join("%12.6f" % Pi[a][b] for b in range(M)) + "\n"
|
|
895
|
+
from ._gsl import eigen_nonsymm
|
|
896
|
+
wr, wi = eigen_nonsymm(Pi)
|
|
897
|
+
o += " eigenvalues of Pi:"
|
|
898
|
+
for a in range(M):
|
|
899
|
+
o += (" %.6f" % wr[a]) if abs(wi[a]) < 1.0e-12 else (" %.6f%+.6fi" % (wr[a], wi[a]))
|
|
900
|
+
o += "\n"
|
|
901
|
+
o += (" ! Pi has rank r by construction: the %d zero eigenvalue%s prove%s nothing "
|
|
902
|
+
"about r (use -lrtest)\n" % (M - r, "" if M - r == 1 else "s",
|
|
903
|
+
"s" if M - r == 1 else ""))
|
|
904
|
+
|
|
905
|
+
def qin(a, b):
|
|
906
|
+
return Q[a][b] if a >= b else Q[b][a]
|
|
907
|
+
s2 = res.sigma2
|
|
908
|
+
o += ("Q matrix (lower triangle, rows and columns in the .inp's order; var %s = 1 by "
|
|
909
|
+
"normalisation):\n" % names[l2i(0)])
|
|
910
|
+
for a in range(M):
|
|
911
|
+
o += " " + "".join("%12.6f" % qin(i2l(a), i2l(b)) for b in range(a + 1)) + "\n"
|
|
912
|
+
o += "Sigma = sigma2 * Q, in the .inp's order:\n"
|
|
913
|
+
for a in range(M):
|
|
914
|
+
o += " " + "".join("%12.6f" % (s2 * qin(i2l(a), i2l(b))) for b in range(a + 1)) + "\n"
|
|
915
|
+
from .nla import choldcp_det
|
|
916
|
+
Sm = [[s2 * (Q[i][j] if j <= i else Q[j][i]) for j in range(M)] for i in range(M)]
|
|
917
|
+
_L, d1, d2, ifd = choldcp_det(Sm)
|
|
918
|
+
if ifd == 0:
|
|
919
|
+
logdet = math.log(abs(d1)) + d2 * math.log(2.0)
|
|
920
|
+
o += " |Sigma| = %s log|Sigma| = %.6f\n" % (cg(math.exp(logdet), 10), logdet)
|
|
921
|
+
else:
|
|
922
|
+
o += " |Sigma|: not computed (Sigma is not positive definite here)\n"
|
|
923
|
+
Sg = [[s2 * qin(i2l(a), i2l(b)) for b in range(M)] for a in range(M)]
|
|
924
|
+
P = [[1.0 if a == b else 0.0 for b in range(M)] for a in range(M)]
|
|
925
|
+
D = [0.0] * M
|
|
926
|
+
ok = True
|
|
927
|
+
for b in range(M):
|
|
928
|
+
acc = Sg[b][b]
|
|
929
|
+
for k in range(b):
|
|
930
|
+
acc -= P[b][k] * P[b][k] * D[k]
|
|
931
|
+
D[b] = acc
|
|
932
|
+
if D[b] <= 0.0:
|
|
933
|
+
ok = False
|
|
934
|
+
break
|
|
935
|
+
for a in range(b + 1, M):
|
|
936
|
+
t = Sg[a][b]
|
|
937
|
+
for k in range(b):
|
|
938
|
+
t -= P[a][k] * P[b][k] * D[k]
|
|
939
|
+
P[a][b] = t / D[b]
|
|
940
|
+
if ok:
|
|
941
|
+
o += "P matrix (Sigma = P D P', P unit lower triangular):\n"
|
|
942
|
+
for a in range(M):
|
|
943
|
+
o += " " + "".join("%12.6f" % P[a][b] for b in range(a + 1)) + "\n"
|
|
944
|
+
o += "D vector:\n " + "".join("%12.6f" % d for d in D) + "\n"
|
|
945
|
+
o += " own share D_i/Sigma_ii:\n " + "".join(
|
|
946
|
+
"%11.1f%%" % (100.0 * D[a] / Sg[a][a]) for a in range(M)) + "\n"
|
|
947
|
+
o += " ! the ordering is the .inp's column order: another order gives another P\n"
|
|
948
|
+
else:
|
|
949
|
+
o += "P, D: not computed (Sigma is not positive definite at the optimum)\n"
|
|
950
|
+
return o
|
|
951
|
+
|
|
952
|
+
|
|
953
|
+
RANKADM_TOL = 0.2
|
|
954
|
+
GRANGER_ZERO = 1.0e-6
|
|
955
|
+
|
|
956
|
+
|
|
957
|
+
def granger_smin(Lam, Th, M, r, q):
|
|
958
|
+
"""granger_smin (drvec.c:422): the smallest singular value of
|
|
959
|
+
Lambda_perp' Theta(1). Invariant to the orthonormal basis of Lambda_perp,
|
|
960
|
+
so numpy's QR and SVD stand in for GSL's (to rounding)."""
|
|
961
|
+
import numpy as np
|
|
962
|
+
s = M - r
|
|
963
|
+
if r <= 0 or s <= 0:
|
|
964
|
+
return -1.0
|
|
965
|
+
L = np.array([[Lam[i][j] for j in range(r)] for i in range(M)], float)
|
|
966
|
+
Qf, _R = np.linalg.qr(L, mode="complete")
|
|
967
|
+
T1 = np.eye(M)
|
|
968
|
+
for k in range(q):
|
|
969
|
+
T1 = T1 - np.array(Th[k], float)
|
|
970
|
+
Gt = T1.T @ Qf[:, r:]
|
|
971
|
+
sv = np.linalg.svd(Gt, compute_uv=False)
|
|
972
|
+
return float(sv[-1])
|
|
973
|
+
|
|
974
|
+
|
|
975
|
+
def _coint_block(res, fv, ro):
|
|
976
|
+
spec = res.spec
|
|
977
|
+
M, r, s, q = spec.M, spec.r, spec.s, spec.q
|
|
978
|
+
o = banner("Cointegration Diagnostics")
|
|
979
|
+
if r > 0 and q > 0:
|
|
980
|
+
g = granger_smin(fv.Lam, fv.Th, M, r, q)
|
|
981
|
+
if g >= 0.0:
|
|
982
|
+
o += "\nRank condition (Granger): sigma_s(alpha_perp' Theta(1)) = %.3e\n" % g
|
|
983
|
+
if g < RANKADM_TOL:
|
|
984
|
+
o += (" *** %s the floor %.2f (a convention; -rankadm sets it).\n"
|
|
985
|
+
" That matrix is what makes the long-run impact\n"
|
|
986
|
+
" C(1) = B_perp (Lambda_perp' Gamma B_perp)^-1 Lambda_perp'\n"
|
|
987
|
+
" Theta(1) have rank M-r. Where it degenerates the FITTED\n"
|
|
988
|
+
" model denies the rank it was estimated at -- it says r and\n"
|
|
989
|
+
" its parameters leave no stochastic trend. The estimate is\n"
|
|
990
|
+
" then on the edge of the region the model class allows, so\n"
|
|
991
|
+
" standard errors and LR statistics do not have their usual\n"
|
|
992
|
+
" distributions there. -rankadm refuses such points; -mawarma\n"
|
|
993
|
+
" makes them unreachable by construction. See\n"
|
|
994
|
+
" docs/HOMOLOGATION.md 4h.\n" % (
|
|
995
|
+
"ZERO to working precision, under" if g < GRANGER_ZERO
|
|
996
|
+
else "Not zero, but below", RANKADM_TOL))
|
|
997
|
+
else:
|
|
998
|
+
o += " admissible (tolerance %.2f)\n" % RANKADM_TOL
|
|
999
|
+
if getattr(res, "gate", None) is not None:
|
|
1000
|
+
o += gate_block(res.gate) # gate_contract(vp), drvec.c:8903
|
|
1001
|
+
if r > 0 and s > 0:
|
|
1002
|
+
rd = fv.rd
|
|
1003
|
+
n = rd.n
|
|
1004
|
+
sdY2 = []
|
|
1005
|
+
for a in range(s):
|
|
1006
|
+
m1 = 0.0
|
|
1007
|
+
for t in range(n):
|
|
1008
|
+
m1 += rd.y2[t][a]
|
|
1009
|
+
m1 /= n
|
|
1010
|
+
v = 0.0
|
|
1011
|
+
for t in range(n):
|
|
1012
|
+
v += (rd.y2[t][a] - m1) * (rd.y2[t][a] - m1)
|
|
1013
|
+
sdY2.append(math.sqrt(v / (n - 1 if n > 1 else 1)))
|
|
1014
|
+
o += "\nNormalisation, share of the weight carried by the Y1 block:\n"
|
|
1015
|
+
for j in range(r):
|
|
1016
|
+
m1 = 0.0
|
|
1017
|
+
for t in range(n):
|
|
1018
|
+
m1 += rd.dm[t][s + j]
|
|
1019
|
+
m1 /= n
|
|
1020
|
+
v = 0.0
|
|
1021
|
+
for t in range(n):
|
|
1022
|
+
v += (rd.dm[t][s + j] - m1) * (rd.dm[t][s + j] - m1)
|
|
1023
|
+
w1 = math.sqrt(v / (n - 1 if n > 1 else 1))
|
|
1024
|
+
w2 = 0.0
|
|
1025
|
+
for a in range(s):
|
|
1026
|
+
w2 += abs(fv.B2[a][j]) * sdY2[a]
|
|
1027
|
+
share = w1 / (w1 + w2) if w1 + w2 > 0.0 else 1.0
|
|
1028
|
+
o += " relation %d: the Y1 block carries %5.1f%% of the weight" % (j + 1, 100.0 * share)
|
|
1029
|
+
o += " <-- DUBIOUS\n" if share < 0.05 else "\n"
|
|
1030
|
+
return o
|
|
1031
|
+
|
|
1032
|
+
|
|
1033
|
+
def wald_sub(x, cov, idx):
|
|
1034
|
+
"""wald_sub (drvec.c): th' pinv(S) th with S's rank by SVD at 1e-8 of the
|
|
1035
|
+
largest singular value. -> (chi2, df) or None."""
|
|
1036
|
+
import numpy as np
|
|
1037
|
+
k = len(idx)
|
|
1038
|
+
if k < 1 or cov is None:
|
|
1039
|
+
return None
|
|
1040
|
+
th = np.array([x[i] for i in idx], float)
|
|
1041
|
+
S = np.array([[cov[i][j] for j in idx] for i in idx], float)
|
|
1042
|
+
try:
|
|
1043
|
+
U, w, Vt = np.linalg.svd(S)
|
|
1044
|
+
except np.linalg.LinAlgError:
|
|
1045
|
+
return None
|
|
1046
|
+
tol = w.max() * 1.0e-8 if w.size else 0.0
|
|
1047
|
+
keep = w > tol
|
|
1048
|
+
y = np.where(keep, (U.T @ th) / np.where(keep, w, 1.0), 0.0)
|
|
1049
|
+
acc = float(th @ (Vt.T @ y))
|
|
1050
|
+
rk = int(keep.sum())
|
|
1051
|
+
if not (acc >= 0.0) or rk == 0:
|
|
1052
|
+
return None
|
|
1053
|
+
return acc, rk
|
|
1054
|
+
|
|
1055
|
+
|
|
1056
|
+
def _chisq_q(x, df):
|
|
1057
|
+
from scipy.stats import chi2
|
|
1058
|
+
return float(chi2.sf(x, df))
|
|
1059
|
+
|
|
1060
|
+
|
|
1061
|
+
def emit_wald(x, cov, idx, title, h0, reject, accept):
|
|
1062
|
+
if len(idx) < 1:
|
|
1063
|
+
return ""
|
|
1064
|
+
w = wald_sub(x, cov, idx)
|
|
1065
|
+
if w is None:
|
|
1066
|
+
return "\n%s\n not computable (the covariance of this block is not usable)\n" % title
|
|
1067
|
+
chi2, df = w
|
|
1068
|
+
pv = _chisq_q(chi2, df)
|
|
1069
|
+
o = "\n%s\n" % title
|
|
1070
|
+
if h0:
|
|
1071
|
+
o += " %s\n" % h0
|
|
1072
|
+
o += " Wald chi2(%d) = %.4f, p-value = %.4f\n" % (df, chi2, pv)
|
|
1073
|
+
o += " %s\n" % (reject if pv < 0.05 else accept)
|
|
1074
|
+
return o
|
|
1075
|
+
|
|
1076
|
+
|
|
1077
|
+
def _hypothesis_block(res, ro, ix_lam, ix_B2, ix_F, ix_Th):
|
|
1078
|
+
"""hypothesis_block (drvec.c:6221-6450)."""
|
|
1079
|
+
spec, names = res.spec, res.data.names
|
|
1080
|
+
M, r, s, q, nf = spec.M, spec.r, spec.s, spec.q, spec.nf
|
|
1081
|
+
x, cov = res.x, res.cov
|
|
1082
|
+
i2l = ro.inp2lam
|
|
1083
|
+
o = banner("Joint Hypothesis Tests (Wald)")
|
|
1084
|
+
if r > 0:
|
|
1085
|
+
idx = [ix_lam[(i, j)] for i in range(M) for j in range(r) if (i, j) in ix_lam]
|
|
1086
|
+
w = wald_sub(x, cov, idx) if idx else None
|
|
1087
|
+
if not idx:
|
|
1088
|
+
o += ("\nError-correction term as a whole\n"
|
|
1089
|
+
" alpha is not free (-alpha imposes alpha = A*psi): nothing to test\n")
|
|
1090
|
+
elif w is not None:
|
|
1091
|
+
o += ("\nError-correction term as a whole\n"
|
|
1092
|
+
" H0: alpha = 0\n"
|
|
1093
|
+
" Wald chi2(%d) = %.4f, p-value = %.4f\n"
|
|
1094
|
+
" ! NOT A TEST: under H0, beta is unidentified (Davies). Use -lrtest "
|
|
1095
|
+
"-bootstrap\n" % (w[1], w[0], _chisq_q(w[0], w[1])))
|
|
1096
|
+
o += ("\nWeak exogeneity, one variable at a time [H0: row i of alpha = 0, chi2(%d)]\n"
|
|
1097
|
+
% r)
|
|
1098
|
+
for i in range(M):
|
|
1099
|
+
li = i2l(i)
|
|
1100
|
+
idx = [ix_lam[(li, j)] for j in range(r) if (li, j) in ix_lam]
|
|
1101
|
+
nm = names[i]
|
|
1102
|
+
o += emit_wald(x, cov, idx, "%s (%s block):" % (nm, "nabla Y2" if i < s else "Y1"),
|
|
1103
|
+
"H0: this variable does not adjust",
|
|
1104
|
+
"REJECT H0 -> %s adjusts to the disequilibrium." % nm,
|
|
1105
|
+
"Cannot reject H0 -> %s is weakly exogenous for B." % nm)
|
|
1106
|
+
idx = [ix_B2[(i, j)] for i in range(s) for j in range(r) if (i, j) in ix_B2]
|
|
1107
|
+
if idx:
|
|
1108
|
+
o += ("\nExclusion from the cointegrating relations [H0: row i of beta_2 = 0, "
|
|
1109
|
+
"chi2(%d)]\n only the %d nabla Y2 variable%s can be excluded: beta = "
|
|
1110
|
+
"[I_r ; beta_2] normalises on Y1\n" % (r, s, "" if s == 1 else "s"))
|
|
1111
|
+
for i in range(s):
|
|
1112
|
+
nm = names[i]
|
|
1113
|
+
o += emit_wald(x, cov, [ix_B2[(i, j)] for j in range(r) if (i, j) in ix_B2],
|
|
1114
|
+
"%s:" % nm,
|
|
1115
|
+
"H0: row %d of beta_2 = 0" % (i + 1),
|
|
1116
|
+
"REJECT H0 -> %s belongs in the long-run relation." % nm,
|
|
1117
|
+
"Cannot reject H0 -> %s can be dropped from it." % nm)
|
|
1118
|
+
if s > 1:
|
|
1119
|
+
o += emit_wald(x, cov, idx, "All of B2 jointly:",
|
|
1120
|
+
"H0: B2 = 0 (the relation involves only the Y1 block)",
|
|
1121
|
+
"REJECT H0 -> the nabla Y2 block belongs in it.",
|
|
1122
|
+
"Cannot reject H0 -> the relation is inside Y1 alone.")
|
|
1123
|
+
elif spec.b2_fix is not None:
|
|
1124
|
+
o += ("\nExclusion from the cointegrating relations\n"
|
|
1125
|
+
" beta_2 is held fixed (-fixb2): imposed, not estimated, so there "
|
|
1126
|
+
"is nothing to test\n")
|
|
1127
|
+
else:
|
|
1128
|
+
o += ("\n r = 0: no error-correction term, no Lambda and no B; the short-run "
|
|
1129
|
+
"block only\n")
|
|
1130
|
+
if nf > 0 or q > 0:
|
|
1131
|
+
o += ("\nShort-run dynamics [Gamma on nabla Y, Theta on the innovations]\n"
|
|
1132
|
+
" ! statements about nabla Y, not about the levels: a variable can drive\n"
|
|
1133
|
+
" another's DIFFERENCES and still be tied to it only through ec\n")
|
|
1134
|
+
if nf > 0:
|
|
1135
|
+
idx = [ix_F[(nf - 1, i, j)] for i in range(M) for j in range(M)
|
|
1136
|
+
if (nf - 1, i, j) in ix_F]
|
|
1137
|
+
o += emit_wald(x, cov, idx,
|
|
1138
|
+
"Joint significance of the last short-run lag, Gamma(%d):" % nf,
|
|
1139
|
+
"H0: Gamma(last) = 0",
|
|
1140
|
+
"REJECT H0 -> the last AR lag is significant.",
|
|
1141
|
+
"Cannot reject H0 -> the last AR lag is not significant.")
|
|
1142
|
+
if q > 0:
|
|
1143
|
+
idx = [ix_Th[(q - 1, i, j)] for i in range(M) for j in range(M)
|
|
1144
|
+
if (q - 1, i, j) in ix_Th]
|
|
1145
|
+
o += emit_wald(x, cov, idx, "Joint significance of the last MA lag, Theta(%d):" % q,
|
|
1146
|
+
"H0: Theta(last) = 0",
|
|
1147
|
+
"REJECT H0 -> the last MA lag is significant.",
|
|
1148
|
+
"Cannot reject H0 -> the last MA lag is not significant.")
|
|
1149
|
+
idx = [ix_F[(k, i, j)] for k in range(nf) for i in range(M) for j in range(M)
|
|
1150
|
+
if i != j and (k, i, j) in ix_F]
|
|
1151
|
+
idx += [ix_Th[(k, i, j)] for k in range(q) for i in range(M) for j in range(M)
|
|
1152
|
+
if i != j and (k, i, j) in ix_Th]
|
|
1153
|
+
if idx:
|
|
1154
|
+
o += emit_wald(x, cov, idx, "All cross effects jointly:",
|
|
1155
|
+
"H0: every off-diagonal coefficient of Gamma and Theta = 0",
|
|
1156
|
+
"REJECT H0 -> the cross structure earns its parameters.",
|
|
1157
|
+
"Cannot reject H0 -> a diagonal short run would do "
|
|
1158
|
+
"(-diagar / -diagma).")
|
|
1159
|
+
else:
|
|
1160
|
+
o += ("\nNo free cross effects to test (the short run is already diagonal or "
|
|
1161
|
+
"structured).\n")
|
|
1162
|
+
for i in range(M):
|
|
1163
|
+
li = i2l(i)
|
|
1164
|
+
nm = names[i]
|
|
1165
|
+
idx = [ix_F[(k, li, j)] for k in range(nf) for j in range(M)
|
|
1166
|
+
if j != li and (k, li, j) in ix_F]
|
|
1167
|
+
idx += [ix_Th[(k, li, j)] for k in range(q) for j in range(M)
|
|
1168
|
+
if j != li and (k, li, j) in ix_Th]
|
|
1169
|
+
if idx:
|
|
1170
|
+
o += emit_wald(x, cov, idx, "D.%s: what the others do to it:" % nm,
|
|
1171
|
+
"H0: no other variable enters the equation of D.%s" % nm,
|
|
1172
|
+
"REJECT H0 -> D.%s is driven by the others." % nm,
|
|
1173
|
+
"Cannot reject H0 -> D.%s is not driven by the others." % nm)
|
|
1174
|
+
idx = [ix_F[(k, j, li)] for k in range(nf) for j in range(M)
|
|
1175
|
+
if j != li and (k, j, li) in ix_F]
|
|
1176
|
+
idx += [ix_Th[(k, j, li)] for k in range(q) for j in range(M)
|
|
1177
|
+
if j != li and (k, j, li) in ix_Th]
|
|
1178
|
+
if idx:
|
|
1179
|
+
o += emit_wald(x, cov, idx, "D.%s: what it does to the others:" % nm,
|
|
1180
|
+
"H0: D.%s enters no other equation" % nm,
|
|
1181
|
+
"REJECT H0 -> D.%s drives the others." % nm,
|
|
1182
|
+
"Cannot reject H0 -> D.%s does not drive the others." % nm)
|
|
1183
|
+
if getattr(res, "fdhess", None) is None:
|
|
1184
|
+
o += ("\n ! p-values from the BFGS-accumulated covariance; use -fdhess before "
|
|
1185
|
+
"quoting them\n")
|
|
1186
|
+
return o
|
|
1187
|
+
|
|
1188
|
+
|
|
1189
|
+
# --------------------------------------------------------------------------- #
|
|
1190
|
+
# impulse responses and the decomposition, in levels (drvec.c:7211-7486) #
|
|
1191
|
+
# --------------------------------------------------------------------------- #
|
|
1192
|
+
|
|
1193
|
+
def compute_psi_weights(m, p, q, phi, theta, L):
|
|
1194
|
+
"""compute_psi_weights (drvec.c:5876), loop for loop. phi[i-1], theta[l-1]
|
|
1195
|
+
as lists; -> psi[0..L] (m x m lists)."""
|
|
1196
|
+
psi = [[[0.0] * m for _ in range(m)] for _ in range(L + 1)]
|
|
1197
|
+
for i in range(m):
|
|
1198
|
+
psi[0][i][i] = 1.0
|
|
1199
|
+
for l in range(1, L + 1):
|
|
1200
|
+
P = psi[l]
|
|
1201
|
+
for i in range(1, min(l, p) + 1):
|
|
1202
|
+
ph = phi[i - 1]
|
|
1203
|
+
prev = psi[l - i]
|
|
1204
|
+
for i1 in range(m):
|
|
1205
|
+
row = ph[i1]
|
|
1206
|
+
for j1 in range(m):
|
|
1207
|
+
acc = 0.0
|
|
1208
|
+
for k in range(m):
|
|
1209
|
+
acc += row[k] * prev[k][j1]
|
|
1210
|
+
P[i1][j1] += acc
|
|
1211
|
+
if l <= q:
|
|
1212
|
+
th = theta[l - 1]
|
|
1213
|
+
for i1 in range(m):
|
|
1214
|
+
for j1 in range(m):
|
|
1215
|
+
P[i1][j1] -= th[i1][j1]
|
|
1216
|
+
return psi
|
|
1217
|
+
|
|
1218
|
+
|
|
1219
|
+
def level_error_map(Csum, Psi_m, B2, M, r):
|
|
1220
|
+
s = M - r
|
|
1221
|
+
G = [[0.0] * M for _ in range(M)]
|
|
1222
|
+
for i in range(s):
|
|
1223
|
+
for j in range(M):
|
|
1224
|
+
G[i][j] = Csum[i][j]
|
|
1225
|
+
for i in range(r):
|
|
1226
|
+
for j in range(M):
|
|
1227
|
+
acc = Psi_m[s + i][j]
|
|
1228
|
+
for k in range(s):
|
|
1229
|
+
acc -= B2[k][i] * Csum[k][j]
|
|
1230
|
+
G[s + i][j] = acc
|
|
1231
|
+
return G
|
|
1232
|
+
|
|
1233
|
+
|
|
1234
|
+
def ldl_sigma(sigma2, qq, M):
|
|
1235
|
+
Sg = [[sigma2 * qq[a][b] for b in range(M)] for a in range(M)]
|
|
1236
|
+
P = [[1.0 if a == b else 0.0 for b in range(M)] for a in range(M)]
|
|
1237
|
+
D = [0.0] * M
|
|
1238
|
+
for b in range(M):
|
|
1239
|
+
acc = Sg[b][b]
|
|
1240
|
+
for k in range(b):
|
|
1241
|
+
acc -= P[b][k] * P[b][k] * D[k]
|
|
1242
|
+
D[b] = acc
|
|
1243
|
+
if D[b] <= 0.0:
|
|
1244
|
+
return None
|
|
1245
|
+
for a in range(b + 1, M):
|
|
1246
|
+
t = Sg[a][b]
|
|
1247
|
+
for k in range(b):
|
|
1248
|
+
t -= P[a][k] * P[b][k] * D[k]
|
|
1249
|
+
P[a][b] = t / D[b]
|
|
1250
|
+
return P, D
|
|
1251
|
+
|
|
1252
|
+
|
|
1253
|
+
def level_irf_R(M, r, p, q, phi, theta, B2, K, P, D):
|
|
1254
|
+
psi = compute_psi_weights(M, p, q, phi, theta, K)
|
|
1255
|
+
Csum = [[0.0] * M for _ in range(M)]
|
|
1256
|
+
R = []
|
|
1257
|
+
for k in range(K + 1):
|
|
1258
|
+
for i in range(M):
|
|
1259
|
+
for j in range(M):
|
|
1260
|
+
Csum[i][j] += psi[k][i][j]
|
|
1261
|
+
G = level_error_map(Csum, psi[k], B2, M, r)
|
|
1262
|
+
Rk = [[0.0] * M for _ in range(M)]
|
|
1263
|
+
for i in range(M):
|
|
1264
|
+
for j in range(M):
|
|
1265
|
+
acc = 0.0
|
|
1266
|
+
for l in range(j, M):
|
|
1267
|
+
acc += G[i][l] * P[l][j]
|
|
1268
|
+
Rk[i][j] = acc * math.sqrt(D[j])
|
|
1269
|
+
R.append(Rk)
|
|
1270
|
+
return R
|
|
1271
|
+
|
|
1272
|
+
|
|
1273
|
+
def level_irf_numbers(res, fv, K):
|
|
1274
|
+
"""The numbers of the .out's level sections (level_irf_fevd), as arrays of
|
|
1275
|
+
lists: None when there is no section (K < 1 or s < 1), or a dict with
|
|
1276
|
+
"error" when Sigma is not positive definite. Otherwise: R[k][i][j] (the
|
|
1277
|
+
response of series i to a one-s.d. shock to j at lag k), fevd[k][i][j] in
|
|
1278
|
+
%% (horizon k + 1), gain[i][j] and mean_lag[i][j] (None where the gain is
|
|
1279
|
+
zero), shock_sd[j], converged, k_stop, tail, beta_gain (max |beta' gain|)."""
|
|
1280
|
+
spec = res.spec
|
|
1281
|
+
M, r, s, p, q = spec.M, spec.r, spec.s, spec.p, spec.q
|
|
1282
|
+
if K < 1 or s < 1:
|
|
1283
|
+
return None
|
|
1284
|
+
qq = fv.qq.tolist()
|
|
1285
|
+
ld = ldl_sigma(res.sigma2, qq, M)
|
|
1286
|
+
if ld is None:
|
|
1287
|
+
return {"error": "Sigma is not positive definite"}
|
|
1288
|
+
P, D = ld
|
|
1289
|
+
phi, theta, B2 = fv.phi.tolist(), fv.theta.tolist(), fv.B2
|
|
1290
|
+
R = level_irf_R(M, r, p, q, phi, theta, B2, K, P, D)
|
|
1291
|
+
KL = 400
|
|
1292
|
+
psiL = compute_psi_weights(M, p, q, phi, theta, KL)
|
|
1293
|
+
CsL = [[0.0] * M for _ in range(M)]
|
|
1294
|
+
Gp = [[0.0] * M for _ in range(M)]
|
|
1295
|
+
gain = [[0.0] * M for _ in range(M)]
|
|
1296
|
+
wsum = [[0.0] * M for _ in range(M)]
|
|
1297
|
+
conv, tail, worst = False, 0.0, 0.0
|
|
1298
|
+
sq = [math.sqrt(d) for d in D]
|
|
1299
|
+
k = 0
|
|
1300
|
+
while k <= KL:
|
|
1301
|
+
mx = 0.0
|
|
1302
|
+
for i in range(M):
|
|
1303
|
+
for j in range(M):
|
|
1304
|
+
CsL[i][j] += psiL[k][i][j]
|
|
1305
|
+
GL = level_error_map(CsL, psiL[k], B2, M, r)
|
|
1306
|
+
for i in range(M):
|
|
1307
|
+
for j in range(M):
|
|
1308
|
+
acc = 0.0
|
|
1309
|
+
for l in range(j, M):
|
|
1310
|
+
acc += GL[i][l] * P[l][j]
|
|
1311
|
+
acc *= sq[j]
|
|
1312
|
+
d = acc - Gp[i][j]
|
|
1313
|
+
gain[i][j] += d
|
|
1314
|
+
wsum[i][j] += k * d
|
|
1315
|
+
Gp[i][j] = acc
|
|
1316
|
+
if abs(d) > mx:
|
|
1317
|
+
mx = abs(d)
|
|
1318
|
+
if k > 20 and mx < 1.0e-10:
|
|
1319
|
+
conv, tail = True, mx
|
|
1320
|
+
break
|
|
1321
|
+
tail = mx
|
|
1322
|
+
k += 1
|
|
1323
|
+
mlag = [[(wsum[i][j] / gain[i][j]) if (abs(gain[i][j]) > 1.0e-12 and abs(gain[i][j]) > 1.0e-8)
|
|
1324
|
+
else None for j in range(M)] for i in range(M)]
|
|
1325
|
+
for j in range(M):
|
|
1326
|
+
for kk in range(r):
|
|
1327
|
+
acc = 0.0
|
|
1328
|
+
for i in range(M):
|
|
1329
|
+
bik = B2[i][kk] if i < s else (1.0 if i - s == kk else 0.0)
|
|
1330
|
+
acc += bik * gain[i][j]
|
|
1331
|
+
if abs(acc) > worst:
|
|
1332
|
+
worst = abs(acc)
|
|
1333
|
+
fevd = []
|
|
1334
|
+
acc = [[0.0] * M for _ in range(M)]
|
|
1335
|
+
for kk in range(K + 1):
|
|
1336
|
+
Fk = []
|
|
1337
|
+
for i in range(M):
|
|
1338
|
+
tot = 0.0
|
|
1339
|
+
for j in range(M):
|
|
1340
|
+
acc[i][j] += R[kk][i][j] * R[kk][i][j]
|
|
1341
|
+
tot += acc[i][j]
|
|
1342
|
+
Fk.append([100.0 * acc[i][j] / tot if tot > 0.0 else 0.0 for j in range(M)])
|
|
1343
|
+
fevd.append(Fk)
|
|
1344
|
+
return {"R": R, "fevd": fevd, "gain": gain, "mean_lag": mlag,
|
|
1345
|
+
"shock_sd": [math.sqrt(d) for d in D], "converged": conv, "k_stop": k,
|
|
1346
|
+
"tail": tail, "beta_gain": worst, "P": P, "D": D}
|
|
1347
|
+
|
|
1348
|
+
|
|
1349
|
+
def level_irf_fevd(res, fv, K):
|
|
1350
|
+
spec, names = res.spec, res.data.names
|
|
1351
|
+
M, s = spec.M, spec.s
|
|
1352
|
+
nb = level_irf_numbers(res, fv, K)
|
|
1353
|
+
if nb is None:
|
|
1354
|
+
return ""
|
|
1355
|
+
if "error" in nb:
|
|
1356
|
+
return "\n(impulse responses not computed: Sigma is not positive definite)\n"
|
|
1357
|
+
R = nb["R"]
|
|
1358
|
+
o = banner("Impulse Response of the Levels")
|
|
1359
|
+
o += ("\n Response of Y_{t+k} to a one-s.d. orthogonalized shock at t.\n"
|
|
1360
|
+
" Shocks are orthogonalized by Sigma = P D P', so the ORDERING is the\n"
|
|
1361
|
+
" .inp's column order: a different order gives different shocks.\n"
|
|
1362
|
+
" ! in a cointegrated system the response does not die out: what it\n"
|
|
1363
|
+
" converges to is the permanent effect, of rank M - r = %d.\n" % s)
|
|
1364
|
+
for j in range(M):
|
|
1365
|
+
o += "\nShock to %s:\n k" % names[j] + "".join(" %14s" % nm for nm in names) + "\n"
|
|
1366
|
+
for k in range(K + 1):
|
|
1367
|
+
o += "%5d" % k + "".join(" %14.6f" % R[k][i][j] for i in range(M)) + "\n"
|
|
1368
|
+
gain = nb["gain"]
|
|
1369
|
+
o += banner("Long-Run Gain and Mean Lag, in Levels")
|
|
1370
|
+
o += ("\n gain = lim_k G_k, the PERMANENT effect of a one-s.d. shock\n"
|
|
1371
|
+
" mean lag = sum k delta_k / sum delta_k, with delta_k the increment of\n"
|
|
1372
|
+
" the response: how long the level takes to get there\n")
|
|
1373
|
+
o += " %s at k = %d (largest increment %.2e)\n" % (
|
|
1374
|
+
"converged" if nb["converged"] else "NOT converged", nb["k_stop"], nb["tail"])
|
|
1375
|
+
o += "\nStructural shock standard deviations (one-s.d. shock size):\n"
|
|
1376
|
+
for j in range(M):
|
|
1377
|
+
o += " %-12s : %.6f\n" % (names[j], nb["shock_sd"][j])
|
|
1378
|
+
for j in range(M):
|
|
1379
|
+
o += "\nShock to %s:\n" % names[j]
|
|
1380
|
+
for i in range(M):
|
|
1381
|
+
g = gain[i][j]
|
|
1382
|
+
ml = nb["mean_lag"][i][j]
|
|
1383
|
+
if ml is not None:
|
|
1384
|
+
o += " %-12s : gain = %10.6f mean lag = %7.2f periods\n" % (
|
|
1385
|
+
names[i], g, ml)
|
|
1386
|
+
else:
|
|
1387
|
+
o += (" %-12s : gain = %10.6f mean lag = undefined (the permanent "
|
|
1388
|
+
"effect is zero)\n" % (names[i], g))
|
|
1389
|
+
o += ("\n self-check max |beta' gain| = %.3e\n"
|
|
1390
|
+
" beta'Y_t is stationary, so no shock can move it permanently: this is\n"
|
|
1391
|
+
" zero by construction, and it exercises the weights, the level map and\n"
|
|
1392
|
+
" the orthogonalisation at once. The gain matrix has rank M - r = %d.\n"
|
|
1393
|
+
% (nb["beta_gain"], s))
|
|
1394
|
+
o += banner("Forecast Error Variance Decomposition, in Levels")
|
|
1395
|
+
o += ("\n Percentage of the h-step level forecast error variance due to each shock.\n"
|
|
1396
|
+
" Same orthogonalization, so the same caveat on the ordering.\n")
|
|
1397
|
+
for i in range(M):
|
|
1398
|
+
o += "\n%s:\n h" % names[i] + "".join(" %11s" % nm for nm in names) + "\n"
|
|
1399
|
+
for k in range(K + 1):
|
|
1400
|
+
o += "%5d" % (k + 1) + "".join(
|
|
1401
|
+
" %10.1f%%" % nb["fevd"][k][i][j] for j in range(M)) + "\n"
|
|
1402
|
+
return o
|
|
1403
|
+
|
|
1404
|
+
|
|
1405
|
+
# --------------------------------------------------------------------------- #
|
|
1406
|
+
# the residuals and the roots (drvec.c:1738-2160, lib/diagmv/diagmv.c) #
|
|
1407
|
+
# --------------------------------------------------------------------------- #
|
|
1408
|
+
|
|
1409
|
+
def diag_k(spec):
|
|
1410
|
+
"""diag_k (drvec.c): the dynamic coefficients, Lambda, F and Theta, and the
|
|
1411
|
+
transfer weights of -xlink."""
|
|
1412
|
+
from .xlink import npar as xl_npar
|
|
1413
|
+
_nmean, nlam, nmid, _nt = spec.blocks()
|
|
1414
|
+
M = spec.M
|
|
1415
|
+
return nlam + nmid - ((M if spec.diag_cov else M * (M + 1) // 2) - 1) + xl_npar(spec.xlink)
|
|
1416
|
+
|
|
1417
|
+
|
|
1418
|
+
def _xlink_rows(res, c0):
|
|
1419
|
+
"""The transfer-function block of the parameter table (drvec.c:8530-8580)."""
|
|
1420
|
+
spec, names = res.spec, res.data.names
|
|
1421
|
+
innames = res.data.xl.names
|
|
1422
|
+
x, dev, cov = res.x, res.se, res.cov
|
|
1423
|
+
o = ("\nTransfer-function inputs, on the LEVELS of the output:\n"
|
|
1424
|
+
" nu(B) = omega(B)/delta(B) B^b, omega(B) = w0 - w1 B - ..., delta(B) = 1 - "
|
|
1425
|
+
"d1 B - ...\n")
|
|
1426
|
+
for L in spec.xlink:
|
|
1427
|
+
np_k = L.s + 1 + L.r
|
|
1428
|
+
ixw = []
|
|
1429
|
+
om1, de1 = 0.0, 1.0
|
|
1430
|
+
for jj in range(L.s + 1):
|
|
1431
|
+
o += par_row(" %s <- %s w%d" % (names[L.out], innames[L.inp], jj),
|
|
1432
|
+
x[c0 + jj], dev[c0 + jj])
|
|
1433
|
+
om1 += x[c0] if jj == 0 else -x[c0 + jj]
|
|
1434
|
+
ixw.append(c0 + jj)
|
|
1435
|
+
for jj in range(1, L.r + 1):
|
|
1436
|
+
o += par_row(" %s <- %s d%d" % (names[L.out], innames[L.inp], jj),
|
|
1437
|
+
x[c0 + L.s + jj], dev[c0 + L.s + jj])
|
|
1438
|
+
de1 -= x[c0 + L.s + jj]
|
|
1439
|
+
ixw.append(c0 + L.s + jj)
|
|
1440
|
+
g = om1 / de1
|
|
1441
|
+
gr = [(1.0 if jj == 0 else -1.0) / de1 for jj in range(L.s + 1)]
|
|
1442
|
+
gr += [om1 / (de1 * de1) for _ in range(L.r)]
|
|
1443
|
+
gse = 0.0
|
|
1444
|
+
if cov is not None:
|
|
1445
|
+
for a in range(np_k):
|
|
1446
|
+
for b in range(np_k):
|
|
1447
|
+
gse += gr[a] * cov[ixw[a]][ixw[b]] * gr[b]
|
|
1448
|
+
gse = math.sqrt(gse) if gse > 0.0 else 0.0
|
|
1449
|
+
o += (" gain nu(1) of %s <- %s: %12.6f (s.e. %.6f, delta method) (b, r, s) = "
|
|
1450
|
+
"(%d, %d, %d)\n" % (names[L.out], innames[L.inp], g, gse, L.b, L.r, L.s))
|
|
1451
|
+
o += emit_wald(x, cov, ixw, "Link %s <- %s, whole:" % (names[L.out], innames[L.inp]),
|
|
1452
|
+
"H0: every omega and delta of the link = 0",
|
|
1453
|
+
"REJECT H0 -> the input enters the output.",
|
|
1454
|
+
"Cannot reject H0 -> the link can be dropped.")
|
|
1455
|
+
c0 += np_k
|
|
1456
|
+
return o
|
|
1457
|
+
|
|
1458
|
+
|
|
1459
|
+
def diag_npar_ser(spec):
|
|
1460
|
+
M, r, s, nf, q = spec.M, spec.r, spec.s, spec.nf, spec.q
|
|
1461
|
+
out = []
|
|
1462
|
+
for i in range(M):
|
|
1463
|
+
row = r + i if i < s else i - s
|
|
1464
|
+
w_row = row < r
|
|
1465
|
+
n = sum(1 for _k in range(nf) if spec.f_free(row, row))
|
|
1466
|
+
if spec.theta_class() == "marow" and not w_row:
|
|
1467
|
+
out.append(n)
|
|
1468
|
+
continue
|
|
1469
|
+
out.append(n + q)
|
|
1470
|
+
return out
|
|
1471
|
+
|
|
1472
|
+
|
|
1473
|
+
def _centred(a, n, m):
|
|
1474
|
+
x = [[0.0] * m for _ in range(n)]
|
|
1475
|
+
for j in range(m):
|
|
1476
|
+
mu = 0.0
|
|
1477
|
+
for t in range(n):
|
|
1478
|
+
mu += a[t][j]
|
|
1479
|
+
mu /= n
|
|
1480
|
+
for t in range(n):
|
|
1481
|
+
x[t][j] = a[t][j] - mu
|
|
1482
|
+
return x
|
|
1483
|
+
|
|
1484
|
+
|
|
1485
|
+
def multivariate_numbers(a, n, m, freq, k):
|
|
1486
|
+
"""Hosking's portmanteau and the multivariate Jarque-Bera of the residuals
|
|
1487
|
+
a (n x m), k dynamic coefficients: the numbers multivariate_diagnostics
|
|
1488
|
+
prints. JB, its parts and its p-value are nan when C0 is singular; pQ is nan
|
|
1489
|
+
when df < 1."""
|
|
1490
|
+
import numpy as np
|
|
1491
|
+
from .nla import chisq
|
|
1492
|
+
s = (freq + 2) if freq > 1 else 10
|
|
1493
|
+
if s > n - 2:
|
|
1494
|
+
s = n - 2
|
|
1495
|
+
if s < 1:
|
|
1496
|
+
s = 1
|
|
1497
|
+
x = _centred(a, n, m)
|
|
1498
|
+
X = np.array(x)
|
|
1499
|
+
C0 = (X.T @ X) / n
|
|
1500
|
+
C0i = np.linalg.inv(C0)
|
|
1501
|
+
Q = 0.0
|
|
1502
|
+
for r in range(1, s + 1):
|
|
1503
|
+
Cr = (X[:n - r].T @ X[r:]) / n
|
|
1504
|
+
Q += float(np.trace(Cr.T @ C0i @ Cr @ C0i))
|
|
1505
|
+
Q *= n
|
|
1506
|
+
df = m * m * s - k
|
|
1507
|
+
pQ = 1.0 - chisq(Q, df) if df >= 1 else float("nan")
|
|
1508
|
+
w, V = np.linalg.eigh(C0)
|
|
1509
|
+
JB = sk = ku = pJB = float("nan")
|
|
1510
|
+
if np.all(w > 1e-12 * w.max()):
|
|
1511
|
+
R = (V / np.sqrt(w)) @ V.T
|
|
1512
|
+
v = X @ R.T
|
|
1513
|
+
b1 = (v ** 3).sum(axis=0) / n
|
|
1514
|
+
b2 = (v ** 4).sum(axis=0) / n - 3.0
|
|
1515
|
+
sk = float((b1 ** 2).sum() * (n / 6.0))
|
|
1516
|
+
ku = float((b2 ** 2).sum() * (n / 24.0))
|
|
1517
|
+
JB = sk + ku
|
|
1518
|
+
pJB = 1.0 - chisq(JB, 2 * m)
|
|
1519
|
+
return {"lags": s, "Q": Q, "df": df, "pQ": pQ, "JB": JB, "JB_df": 2 * m,
|
|
1520
|
+
"pJB": pJB, "skewness": sk, "kurtosis": ku, "part_df": m}
|
|
1521
|
+
|
|
1522
|
+
|
|
1523
|
+
def multivariate_diagnostics(a, n, m, freq, k):
|
|
1524
|
+
d = multivariate_numbers(a, n, m, freq, k)
|
|
1525
|
+
s, Q, df, pQ = d["lags"], d["Q"], d["df"], d["pQ"]
|
|
1526
|
+
JB, pJB, sk, ku = d["JB"], d["pJB"], d["skewness"], d["kurtosis"]
|
|
1527
|
+
o = "\n\n" + BAR + "\n MULTIVARIATE RESIDUAL DIAGNOSTICS \n" + BAR + "\n"
|
|
1528
|
+
o += "\nHosking's Multivariate Portmanteau Test (lag %d):\n" % s
|
|
1529
|
+
if df >= 1:
|
|
1530
|
+
o += " Q(%d) = %.4f, p-value = %.4f\n" % (df, Q, pQ)
|
|
1531
|
+
o += " df = m^2 s - k = %d - %d, k = dynamic coefficients estimated\n" % (m * m * s, k)
|
|
1532
|
+
o += (" *** REJECT H0: residuals are not white noise.\n" if pQ < 0.05 else
|
|
1533
|
+
" Cannot reject H0: residuals appear white noise.\n")
|
|
1534
|
+
else:
|
|
1535
|
+
o += (" Q = %.4f; df = m^2 s - k = %d - %d < 1: too few lags for the parameters, "
|
|
1536
|
+
"no p-value.\n" % (Q, m * m * s, k))
|
|
1537
|
+
o += ("\nMultivariate Jarque-Bera Test (normality; Lutkepohl, symmetric square root of "
|
|
1538
|
+
"Sigma):\n")
|
|
1539
|
+
if math.isfinite(JB):
|
|
1540
|
+
o += " JB(%d) = %.4f, p-value = %.4f\n" % (2 * m, JB, pJB)
|
|
1541
|
+
o += " skewness %.4f, kurtosis %.4f, each chi2(%d)\n" % (sk, ku, m)
|
|
1542
|
+
o += (" *** REJECT H0: residuals are not normally distributed.\n" if pJB < 0.05 else
|
|
1543
|
+
" Cannot reject H0: residuals appear normal.\n")
|
|
1544
|
+
else:
|
|
1545
|
+
o += " not computed: the residual covariance is singular.\n"
|
|
1546
|
+
o += BAR + "\n"
|
|
1547
|
+
return o
|
|
1548
|
+
|
|
1549
|
+
|
|
1550
|
+
def residual_diagnostics(res, fv):
|
|
1551
|
+
spec = res.spec
|
|
1552
|
+
a, n, M = fv.a, fv.rd.n, spec.M
|
|
1553
|
+
if n < 20 or M < 1:
|
|
1554
|
+
return ""
|
|
1555
|
+
K = min(n // 4, 12)
|
|
1556
|
+
if K < 1:
|
|
1557
|
+
return ""
|
|
1558
|
+
band = 2.0 / math.sqrt(n)
|
|
1559
|
+
mean = []
|
|
1560
|
+
for i in range(M):
|
|
1561
|
+
sm = 0.0
|
|
1562
|
+
for t in range(n):
|
|
1563
|
+
sm += a[t][i]
|
|
1564
|
+
mean.append(sm / n)
|
|
1565
|
+
C = []
|
|
1566
|
+
for k in range(K + 1):
|
|
1567
|
+
Ck = [[0.0] * M for _ in range(M)]
|
|
1568
|
+
for i in range(M):
|
|
1569
|
+
for j in range(M):
|
|
1570
|
+
sm = 0.0
|
|
1571
|
+
for t in range(k, n):
|
|
1572
|
+
sm += (a[t][i] - mean[i]) * (a[t - k][j] - mean[j])
|
|
1573
|
+
Ck[i][j] = sm / n
|
|
1574
|
+
C.append(Ck)
|
|
1575
|
+
o = multivariate_diagnostics(a, n, M, res.data.freq, diag_k(spec))
|
|
1576
|
+
o += "\n"
|
|
1577
|
+
# BUG-62 (drvec.c residual_diagnostics): with r >= 1 the last r columns are
|
|
1578
|
+
# the innovations of beta'Y, not of the Y1 series; the line names them.
|
|
1579
|
+
names = res.data.names
|
|
1580
|
+
if spec.r > 0 and len(names) == M:
|
|
1581
|
+
s = M - spec.r
|
|
1582
|
+
o += (" the columns:" + "".join(" D.%s" % names[i] for i in range(s))
|
|
1583
|
+
+ "".join(" ec%d" % (j + 1) for j in range(spec.r))
|
|
1584
|
+
+ " (ec = the innovation of beta'Y, not of the Y1 series)\n")
|
|
1585
|
+
o += " residual sd:" + "".join(" %10.6f" % math.sqrt(C[0][i][i]) for i in range(M))
|
|
1586
|
+
o += "\n band = 2/sqrt(n) = %.4f; * marks |r| > band\n" % band
|
|
1587
|
+
o += "\nResidual cross-correlation matrices, R(k)[i][j] = corr(a_i(t), a_j(t-k)):\n"
|
|
1588
|
+
|
|
1589
|
+
def corr(k, i, j):
|
|
1590
|
+
return C[k][i][j] / math.sqrt(C[0][i][i] * C[0][j][j])
|
|
1591
|
+
for k in range(K + 1):
|
|
1592
|
+
o += " k=%-2d " % k
|
|
1593
|
+
for i in range(M):
|
|
1594
|
+
if i > 0:
|
|
1595
|
+
o += "\n "
|
|
1596
|
+
for j in range(M):
|
|
1597
|
+
rr = corr(k, i, j)
|
|
1598
|
+
o += "%8.3f%s" % (rr, "*" if abs(rr) > band else " ")
|
|
1599
|
+
o += "\n"
|
|
1600
|
+
worst, wk, wi, wj, anyx = 0.0, -1, 0, 0, False
|
|
1601
|
+
for k in range(1, K + 1):
|
|
1602
|
+
for i in range(M):
|
|
1603
|
+
for j in range(M):
|
|
1604
|
+
if i == j:
|
|
1605
|
+
continue
|
|
1606
|
+
rr = corr(k, i, j)
|
|
1607
|
+
if abs(rr) > band:
|
|
1608
|
+
anyx = True
|
|
1609
|
+
if abs(rr) > abs(worst):
|
|
1610
|
+
worst, wk, wi, wj = rr, k, i + 1, j + 1
|
|
1611
|
+
r0 = 0.0
|
|
1612
|
+
for i in range(M):
|
|
1613
|
+
for j in range(i):
|
|
1614
|
+
rr = corr(0, i, j)
|
|
1615
|
+
if abs(rr) > abs(r0):
|
|
1616
|
+
r0 = rr
|
|
1617
|
+
o += "\n Contemporaneous correlation, largest |r| at k=0: %+.3f%s\n" % (
|
|
1618
|
+
r0, " ! -diagcov forces Sigma diagonal: an imposed restriction the data do not "
|
|
1619
|
+
"support" if (spec.diag_cov and abs(r0) > band) else " (Sigma carries it)")
|
|
1620
|
+
o += " Cross dynamics left at k >= 1: "
|
|
1621
|
+
o += ("none beyond the band\n" if not anyx else
|
|
1622
|
+
"YES, largest is equation %d against the innovation of %d at lag %d, r = %+.3f\n"
|
|
1623
|
+
% (wi, wj, wk, worst))
|
|
1624
|
+
return o
|
|
1625
|
+
|
|
1626
|
+
|
|
1627
|
+
def report_operator_roots(label, A, m, k, quiet=False):
|
|
1628
|
+
"""-> (text, minmod)."""
|
|
1629
|
+
from ._gsl import eigen_nonsymm
|
|
1630
|
+
if k <= 0:
|
|
1631
|
+
return "", 1.0e12
|
|
1632
|
+
mk = m * k
|
|
1633
|
+
Cm = [[0.0] * mk for _ in range(mk)]
|
|
1634
|
+
for l in range(k):
|
|
1635
|
+
for i in range(m):
|
|
1636
|
+
for j in range(m):
|
|
1637
|
+
Cm[i][j + l * m] = float(A[l][i][j])
|
|
1638
|
+
for l in range(1, k):
|
|
1639
|
+
for j in range(m):
|
|
1640
|
+
Cm[j + l * m][j + (l - 1) * m] = 1.0
|
|
1641
|
+
wr, wi = eigen_nonsymm(Cm)
|
|
1642
|
+
o = " %-11s" % label
|
|
1643
|
+
minmod = 1.0e12
|
|
1644
|
+
for i in range(mk):
|
|
1645
|
+
lam = math.sqrt(wr[i] * wr[i] + wi[i] * wi[i])
|
|
1646
|
+
if lam <= 1.0e-12:
|
|
1647
|
+
o += " %8s " % "inf"
|
|
1648
|
+
continue
|
|
1649
|
+
if 1.0 / lam < minmod:
|
|
1650
|
+
minmod = 1.0 / lam
|
|
1651
|
+
o += " %8.5f%s" % (1.0 / lam, "*" if 1.0 / lam < 1.0001 else " ")
|
|
1652
|
+
return o + "\n", minmod
|
|
1653
|
+
|
|
1654
|
+
|
|
1655
|
+
def operator_roots(res, fv):
|
|
1656
|
+
import numpy as np
|
|
1657
|
+
spec, names = res.spec, res.data.names
|
|
1658
|
+
M, r, s, p, q = spec.M, spec.r, spec.s, spec.p, spec.q
|
|
1659
|
+
if p <= 0 and q <= 0:
|
|
1660
|
+
return ""
|
|
1661
|
+
o = "\nRoots of |phi(B)|=0 and |theta(B)|=0 (moduli; > 1 is stationary/invertible):\n\n"
|
|
1662
|
+
t, min_ar = report_operator_roots("AR (Phi)", fv.phi, M, p)
|
|
1663
|
+
o += t
|
|
1664
|
+
t, min_ma = report_operator_roots("MA (Theta)", fv.theta, M, q)
|
|
1665
|
+
o += t
|
|
1666
|
+
if min_ar < 1.0001:
|
|
1667
|
+
o += ("\n * A root sits on the unit circle -- an AR root: the system is at\n"
|
|
1668
|
+
" the edge of stationarity, which is where Lambda -> 0 puts it (the\n"
|
|
1669
|
+
" rank-r model degenerating into the rank-(r-1) one).\n")
|
|
1670
|
+
if min_ma < 1.0001:
|
|
1671
|
+
T = np.eye(M)
|
|
1672
|
+
for l in range(q):
|
|
1673
|
+
T = T - fv.theta[l].T
|
|
1674
|
+
o += ("\n * A root sits on the unit circle -- an MA root (modulus %.5f). The\n"
|
|
1675
|
+
" likelihood rose towards a non-invertible MA and the optimiser stopped\n"
|
|
1676
|
+
" at the engine's invertibility gate: this is a CONSTRAINED point, not an\n"
|
|
1677
|
+
" interior maximum. The standard errors of the MA are not defined along\n"
|
|
1678
|
+
" that direction, and no LR that uses this fit has its usual\n"
|
|
1679
|
+
" distribution (BUG-49).\n" % min_ma)
|
|
1680
|
+
_U, sv, Vt = np.linalg.svd(T)
|
|
1681
|
+
u = Vt[-1]
|
|
1682
|
+
o += (" The combination the model differences once too often -- the left\n"
|
|
1683
|
+
" null vector u of Theta*(1), smallest singular value %.2e -- has\n"
|
|
1684
|
+
" weights (|u|, on Ybar = [nabla Y2 ; W]):\n" % sv[-1])
|
|
1685
|
+
w2 = ww = 0.0
|
|
1686
|
+
for i in range(M):
|
|
1687
|
+
ui = float(u[i])
|
|
1688
|
+
if i < s:
|
|
1689
|
+
w2 += ui * ui
|
|
1690
|
+
o += " nabla %-12s %8.4f\n" % (names[i], abs(ui))
|
|
1691
|
+
else:
|
|
1692
|
+
ww += ui * ui
|
|
1693
|
+
o += " W%-17d %8.4f\n" % (i - s + 1, abs(ui))
|
|
1694
|
+
if r == 0 or w2 >= 0.8:
|
|
1695
|
+
o += (" It lies in the nabla Y2 block (share %.2f): that combination of the\n"
|
|
1696
|
+
" common-trend series looks OVER-DIFFERENCED -- stationary in levels.\n"
|
|
1697
|
+
" Read it as evidence that the rank is higher than %d, or that the\n"
|
|
1698
|
+
" series in that block are not I(1).\n" % (w2, r))
|
|
1699
|
+
elif ww >= 0.8:
|
|
1700
|
+
o += " It lies in the W block (share %.2f). %s\n" % (ww, (
|
|
1701
|
+
"An AR root is near one as well: an\n"
|
|
1702
|
+
" AR/MA near-cancellation, i.e. a near common factor. Read it as\n"
|
|
1703
|
+
" evidence that the equilibrium error is barely mean-reverting --\n"
|
|
1704
|
+
" that the rank may be LOWER than the one fitted.") if min_ar < 1.02 else (
|
|
1705
|
+
"The equilibrium error's own moving\n"
|
|
1706
|
+
" average is at the boundary: re-examine the MA order of W and the\n"
|
|
1707
|
+
" deterministic case before reading the estimates."))
|
|
1708
|
+
else:
|
|
1709
|
+
o += (" It mixes both blocks (nabla Y2 share %.2f, W share %.2f): no single\n"
|
|
1710
|
+
" reading; re-examine the rank and the orders before reading the\n"
|
|
1711
|
+
" estimates.\n" % (w2, ww))
|
|
1712
|
+
return o
|