drvec 0.1.0__py3-none-any.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/_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 +31 -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/cli.py
ADDED
|
@@ -0,0 +1,592 @@
|
|
|
1
|
+
"""drvec-py: the command line of the C's .pre route.
|
|
2
|
+
|
|
3
|
+
drvec-py s1.pre s2.pre [... sM.pre] p q r [options]
|
|
4
|
+
|
|
5
|
+
C: `parse_cli` (drvec.c:10318), `validate_cli` (10214) and `main` (10878). The
|
|
6
|
+
syntax, the option table, the checks on p, q and r, the refusals and the
|
|
7
|
+
products are the C's: <base>.out always, <base>.boot with -lrtest -bootstrap,
|
|
8
|
+
<base>.irfboot with -irfboot, <base>.forecast with -f and <base>.recursive (or
|
|
9
|
+
-C FILE) with -estwin; <base> is -name's, or the files' stems joined by `_`.
|
|
10
|
+
|
|
11
|
+
What the port does not have is refused with exit 2 and says so: the .inp
|
|
12
|
+
route, and the options of docs/PORT.md §10 that are not ported. The terminal
|
|
13
|
+
output is a summary, not the C's progress log; the files are the C's.
|
|
14
|
+
|
|
15
|
+
Exit codes, as the C's: 0 done (and -h, --version), 1 a malformed command
|
|
16
|
+
line or an unreadable input, 2 a combination refused or an estimation failed.
|
|
17
|
+
"""
|
|
18
|
+
from __future__ import annotations
|
|
19
|
+
|
|
20
|
+
import os
|
|
21
|
+
import sys
|
|
22
|
+
|
|
23
|
+
PROG = "drvec-py"
|
|
24
|
+
|
|
25
|
+
# (name, argument kind, what the value is called, ported)
|
|
26
|
+
OPT_TABLE = [
|
|
27
|
+
("-mean", "none", None, True),
|
|
28
|
+
("-case", "case", "1|2|3|pre", True),
|
|
29
|
+
("-nodrift", "int", "i", True),
|
|
30
|
+
("-m", "method", "1|2", True),
|
|
31
|
+
("-diagar", "none", None, True),
|
|
32
|
+
("-trend", "none", None, True),
|
|
33
|
+
("-diagma", "none", None, True),
|
|
34
|
+
("-diagcov", "none", None, True),
|
|
35
|
+
("-levels", "none", None, True),
|
|
36
|
+
("-differenced", "none", None, True),
|
|
37
|
+
("-fixb2", "real_opt", "[v]", True),
|
|
38
|
+
("-fixb2row", "int_real", "i v", True),
|
|
39
|
+
("-xpre", "str", "FILE", True),
|
|
40
|
+
("-xlink", "str", "FILE", True),
|
|
41
|
+
("-xsys", "int", "i", True),
|
|
42
|
+
("-gzero", "int_int", "i j", True),
|
|
43
|
+
("-lrtest", "none", None, True),
|
|
44
|
+
("-bootstrap", "int", "N", True),
|
|
45
|
+
("-irfboot", "int", "N", True),
|
|
46
|
+
("-rungs", "none", None, True),
|
|
47
|
+
("-specs", "none", None, False),
|
|
48
|
+
("-warma", "none", None, False),
|
|
49
|
+
("-mawarma", "none", None, False),
|
|
50
|
+
("-marow", "none", None, True),
|
|
51
|
+
("-mafree", "none", None, True),
|
|
52
|
+
("-f", "int", "H", True),
|
|
53
|
+
("-estwin", "int", "E", True),
|
|
54
|
+
("-C", "str", "FILE", True),
|
|
55
|
+
("-matri", "none", None, True),
|
|
56
|
+
("-rankadm", "tol_opt", "[tol]", False),
|
|
57
|
+
("-matest", "int", "N", False),
|
|
58
|
+
("-artest", "int", "N", False),
|
|
59
|
+
("-alpha", "str", "FILE", True),
|
|
60
|
+
("-weakex", "int", "i", True),
|
|
61
|
+
("-multistart", "int", "n", True),
|
|
62
|
+
("-eval", "none", None, False),
|
|
63
|
+
("-fdhess", "none", None, True),
|
|
64
|
+
("-seed", "str", "pfx", False),
|
|
65
|
+
("-seedybar", "str", "pfx", False),
|
|
66
|
+
("-seedgate", "none", None, False),
|
|
67
|
+
("-noladder", "none", None, False),
|
|
68
|
+
("-seedjoh", "none", None, False),
|
|
69
|
+
("-seedb2", "real", "v", False),
|
|
70
|
+
("-interv", "str", "pfx", True),
|
|
71
|
+
("-writeres", "str", "pfx", False),
|
|
72
|
+
("-writeinp", "str", "pfx", False),
|
|
73
|
+
("-name", "str", "NAME", True),
|
|
74
|
+
]
|
|
75
|
+
_OPTS = {o[0]: o for o in OPT_TABLE}
|
|
76
|
+
B2ROW_MAX = 64
|
|
77
|
+
GZ_MAX = 16
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
# the C's words for the combinations it refuses (parse_cli, drvec.c:10634-10741)
|
|
81
|
+
MSG_XLINK = ("-xlink needs the .pre route in the levels layout, and a\n"
|
|
82
|
+
" single fit: it cannot be combined with -differenced, -warma,\n"
|
|
83
|
+
" -lrtest, -specs, -rungs, -matest, -artest, -eval, -f, -estwin,\n"
|
|
84
|
+
" -writeinp or -writeres.")
|
|
85
|
+
MSG_IRFBOOT = ("-irfboot bootstraps the responses of a single fit in the levels\n"
|
|
86
|
+
" layout: it cannot be combined with -differenced, -xlink, -trend,\n"
|
|
87
|
+
" -warma, -estwin, -writeinp, -writeres or the modes -lrtest,\n"
|
|
88
|
+
" -specs, -rungs, -matest, -artest and -eval (not yet).")
|
|
89
|
+
MSG_MIXED = ("WARNING: with some drifts held (-nodrift / -case pre) the rank test's\n"
|
|
90
|
+
" asymptotic tables are case 3's, which do not apply to the\n"
|
|
91
|
+
" mixed case: read the bootstrap (-bootstrap N), not the table.\n")
|
|
92
|
+
MSG_TREND = ("-trend (case 4) goes on case 3's drifts (-case 3 or -case pre)\n"
|
|
93
|
+
" for a single fit: it cannot be combined with -warma, -f,\n"
|
|
94
|
+
" -estwin, -writeinp, -writeres or the modes -lrtest, -specs,\n"
|
|
95
|
+
" -rungs, -matest, -artest and -eval (not yet).")
|
|
96
|
+
MSG_XSYS = ("-xsys embeds an input in the system for a single fit with\n"
|
|
97
|
+
" q = 0. It cannot be combined with -alpha/-weakex (it sets\n"
|
|
98
|
+
" alpha itself), -xlink, -warma, a moving average (q > 0, not\n"
|
|
99
|
+
" yet), or the modes -lrtest, -specs, -rungs, -matest, -artest\n"
|
|
100
|
+
" and -eval.")
|
|
101
|
+
MSG_FIXB2ROW = ("-fixb2row holds rows of B2 at a single fit. It cannot be\n"
|
|
102
|
+
" combined with -fixb2 (which holds every row), -warma, or the\n"
|
|
103
|
+
" modes -lrtest, -specs, -rungs, -matest, -artest and -eval.")
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
class CliError(Exception):
|
|
107
|
+
def __init__(self, msg, code=1, usage=False):
|
|
108
|
+
super().__init__(msg)
|
|
109
|
+
self.code = code
|
|
110
|
+
self.usage = usage
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def _option_list():
|
|
114
|
+
out = ["", "Every option the C accepts (* = not ported yet; refused):", " "]
|
|
115
|
+
col = 0
|
|
116
|
+
for name, _kind, val, ported in OPT_TABLE:
|
|
117
|
+
item = (name + (" " + val if val else "")) + ("" if ported else "*")
|
|
118
|
+
if col and col + len(item) + 2 > 72:
|
|
119
|
+
out.append(" ")
|
|
120
|
+
col = 0
|
|
121
|
+
out[-1] += " %-*s" % (len(item) + 1, item)
|
|
122
|
+
col += len(item) + 2
|
|
123
|
+
return "\n".join(out) + "\n"
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def usage():
|
|
127
|
+
from . import __version__
|
|
128
|
+
return f"""
|
|
129
|
+
{PROG} {__version__} -- VEC model EML estimation (Mauricio 2006), the Python port of drvec
|
|
130
|
+
|
|
131
|
+
{PROG} s1.pre s2.pre [... sM.pre] p q r [options]
|
|
132
|
+
ONE UNIVARIATE MODEL PER SERIES, as in drtran: each .pre
|
|
133
|
+
brings its series, its Box-Cox and rescaling. The files
|
|
134
|
+
are lined up by DATE and the common sample is used. The
|
|
135
|
+
column order is the .inp's: the first M-r files are the
|
|
136
|
+
nabla Y2 block and the last r the Y1 block. Products are
|
|
137
|
+
named <stem1>_<stem2>...; -name NAME overrides that.
|
|
138
|
+
(The C's .inp route, `drvec file p q r', is not ported.)
|
|
139
|
+
|
|
140
|
+
p : AR order of stationary VARMA on Ybar_t
|
|
141
|
+
q : MA order
|
|
142
|
+
r : cointegration rank (0 <= r < M; 0 is the diagonal rung;
|
|
143
|
+
ignored with -lrtest)
|
|
144
|
+
|
|
145
|
+
Deterministic cases (Mauricio 2006, Remark 6):
|
|
146
|
+
-case 1 : E[nabla Y2]=0, E[W]=0 (default)
|
|
147
|
+
-case 2 : E[nabla Y2]=0, E[W]!=0 (-mean alone also selects it)
|
|
148
|
+
-case 3 : E[nabla Y2]!=0, E[W]!=0
|
|
149
|
+
-case pre : case 3 with each drift as its .pre says
|
|
150
|
+
-nodrift i : hold E[nabla Y2_i] = 0 inside case 3 (repeatable)
|
|
151
|
+
-trend : case 4, a linear trend in W (restricted to beta)
|
|
152
|
+
|
|
153
|
+
Restrictions (each but -fixb2 with its LR against the free model):
|
|
154
|
+
-fixb2 [v] hold B2 fixed: at v (an a-priori value, a valid LR) or,
|
|
155
|
+
without a value, at its static-OLS estimate
|
|
156
|
+
-fixb2row i v hold ROW i of B2 at v in every relation (repeatable)
|
|
157
|
+
-alpha FILE alpha = A*psi, A read from FILE (`M sa', then M rows)
|
|
158
|
+
-weakex i series i weakly exogenous (A = I without column i)
|
|
159
|
+
-xsys i series i an input embedded in the system (q = 0)
|
|
160
|
+
-gzero i j Gamma_k[i][j] = 0 for every k: j out of i's short run (q = 0)
|
|
161
|
+
-xpre FILE -xlink FILE exogenous transfer-function inputs
|
|
162
|
+
|
|
163
|
+
Modes and products:
|
|
164
|
+
-lrtest [-bootstrap N] the sequential LR test for the rank (<base>.boot)
|
|
165
|
+
-rungs the ladder below the rank: rungs 0, 1 and 2 at r = 0,
|
|
166
|
+
with the gate and their chi2 LRs
|
|
167
|
+
-irfboot N bands for the level responses and the FEVD (<base>.irfboot)
|
|
168
|
+
-f H forecast H steps in LEVELS, with bands (<base>.forecast)
|
|
169
|
+
-estwin E estimate on observations 1..E and roll the origin (needs
|
|
170
|
+
-f H; per-origin errors to <base>.recursive, or -C FILE)
|
|
171
|
+
-fdhess standard errors from the finite-difference Hessian at
|
|
172
|
+
the optimum: use it before quoting a standard error
|
|
173
|
+
-multistart n n - 1 jittered starts added to the search (the C's generator)
|
|
174
|
+
-diagar, -diagcov, -diagma, -marow, -matri, -mafree: the structure
|
|
175
|
+
""" + _option_list()
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
def _int(s):
|
|
179
|
+
try:
|
|
180
|
+
if s is None or s.strip() != s or s == "":
|
|
181
|
+
return None
|
|
182
|
+
return int(s, 10)
|
|
183
|
+
except ValueError:
|
|
184
|
+
return None
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
def _real(s):
|
|
188
|
+
try:
|
|
189
|
+
if s is None or s == "" or s.strip() != s:
|
|
190
|
+
return None
|
|
191
|
+
return float(s)
|
|
192
|
+
except ValueError:
|
|
193
|
+
return None
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
def _nearest(name):
|
|
197
|
+
best, hit = 0, None
|
|
198
|
+
for o in _OPTS:
|
|
199
|
+
k = 0
|
|
200
|
+
while k < len(name) and k < len(o) and name[k] == o[k]:
|
|
201
|
+
k += 1
|
|
202
|
+
if k > best:
|
|
203
|
+
best, hit = k, o
|
|
204
|
+
return hit if best >= 4 else None
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
def validate(argv, first):
|
|
208
|
+
"""validate_cli: the preliminary pass, which stops at the first problem."""
|
|
209
|
+
i = first
|
|
210
|
+
while i < len(argv):
|
|
211
|
+
a = argv[i]
|
|
212
|
+
if not a.startswith("-"):
|
|
213
|
+
raise CliError(f"unexpected argument `{a}' -- options start with `-'")
|
|
214
|
+
o = _OPTS.get(a)
|
|
215
|
+
if o is None:
|
|
216
|
+
near = _nearest(a)
|
|
217
|
+
msg = f"unknown option `{a}'" + (f"\n did you mean `{near}'?" if near else "")
|
|
218
|
+
raise CliError(msg, usage=True)
|
|
219
|
+
kind, val = o[1], o[2]
|
|
220
|
+
nxt = argv[i + 1] if i + 1 < len(argv) else None
|
|
221
|
+
if kind in ("str", "int", "case", "method", "real") and nxt is None:
|
|
222
|
+
raise CliError(f"{a} needs {val}")
|
|
223
|
+
if kind == "str":
|
|
224
|
+
i += 1
|
|
225
|
+
elif kind == "int":
|
|
226
|
+
v = _int(nxt)
|
|
227
|
+
if v is None or v < 1:
|
|
228
|
+
raise CliError(f"{a} needs an integer >= 1, got `{nxt}'")
|
|
229
|
+
i += 1
|
|
230
|
+
elif kind == "case":
|
|
231
|
+
v = _int(nxt)
|
|
232
|
+
if nxt != "pre" and (v is None or not 1 <= v <= 3):
|
|
233
|
+
raise CliError(f"{a} must be 1, 2, 3 (Mauricio 2006, Remark 6) or pre "
|
|
234
|
+
f"(case 4 is -trend), got `{nxt}'")
|
|
235
|
+
i += 1
|
|
236
|
+
elif kind == "method":
|
|
237
|
+
v = _int(nxt)
|
|
238
|
+
if v is None or not 1 <= v <= 2:
|
|
239
|
+
raise CliError(f"{a} must be 1 (exact ML) or 2 (approximate), got `{nxt}'")
|
|
240
|
+
i += 1
|
|
241
|
+
elif kind == "real":
|
|
242
|
+
if _real(nxt) is None:
|
|
243
|
+
raise CliError(f"{a} needs a number, got `{nxt}'")
|
|
244
|
+
i += 1
|
|
245
|
+
elif kind == "int_int":
|
|
246
|
+
if i + 2 >= len(argv):
|
|
247
|
+
raise CliError(f"{a} needs {val}")
|
|
248
|
+
if _int(argv[i + 1]) is None or not 1 <= _int(argv[i + 1]) <= GZ_MAX:
|
|
249
|
+
raise CliError(f"{a} needs an equation 1..{GZ_MAX}, got `{argv[i + 1]}'")
|
|
250
|
+
if _int(argv[i + 2]) is None or not 1 <= _int(argv[i + 2]) <= GZ_MAX:
|
|
251
|
+
raise CliError(f"{a} needs a regressor 1..{GZ_MAX}, got `{argv[i + 2]}'")
|
|
252
|
+
i += 2
|
|
253
|
+
elif kind == "int_real":
|
|
254
|
+
if i + 2 >= len(argv):
|
|
255
|
+
raise CliError(f"{a} needs {val}")
|
|
256
|
+
v = _int(argv[i + 1])
|
|
257
|
+
if v is None or not 1 <= v <= B2ROW_MAX:
|
|
258
|
+
raise CliError(f"{a} needs a row number >= 1, got `{argv[i + 1]}'")
|
|
259
|
+
if _real(argv[i + 2]) is None:
|
|
260
|
+
raise CliError(f"{a} needs a value for the row, got `{argv[i + 2]}'")
|
|
261
|
+
i += 2
|
|
262
|
+
elif kind == "real_opt":
|
|
263
|
+
if nxt is not None and _real(nxt) is not None:
|
|
264
|
+
i += 1
|
|
265
|
+
elif kind == "tol_opt":
|
|
266
|
+
if nxt is not None and not nxt.startswith("-"):
|
|
267
|
+
v = _real(nxt)
|
|
268
|
+
if v is None or v <= 0.0:
|
|
269
|
+
raise CliError(f"{a} needs a tolerance > 0, got `{nxt}'")
|
|
270
|
+
i += 1
|
|
271
|
+
elif nxt is not None and _real(nxt) is not None:
|
|
272
|
+
raise CliError(f"{a} needs a tolerance > 0, got `{nxt}'")
|
|
273
|
+
i += 1
|
|
274
|
+
|
|
275
|
+
|
|
276
|
+
def parse(argv):
|
|
277
|
+
"""argv without the program name -> a dict of the run, or raise CliError."""
|
|
278
|
+
if len(argv) < 4:
|
|
279
|
+
raise CliError("", usage=True)
|
|
280
|
+
k = 0
|
|
281
|
+
while k < len(argv) and argv[k].endswith(".pre"):
|
|
282
|
+
k += 1
|
|
283
|
+
if k == 0:
|
|
284
|
+
raise CliError("the .inp route (`drvec file p q r') is not ported: give one .pre "
|
|
285
|
+
"per series,\n `drvec-py s1.pre s2.pre ... p q r [options]'",
|
|
286
|
+
code=2)
|
|
287
|
+
if len(argv) < k + 3:
|
|
288
|
+
raise CliError("ERROR: after the .pre files come p, q and r", usage=True)
|
|
289
|
+
files = argv[:k]
|
|
290
|
+
first = k + 3
|
|
291
|
+
p, q, r = (_int(v) for v in argv[k:k + 3])
|
|
292
|
+
if p is None or p < 1:
|
|
293
|
+
raise CliError(f"p (the AR order) must be an integer >= 1, got `{argv[k]}'")
|
|
294
|
+
if q is None or q < 0:
|
|
295
|
+
raise CliError(f"q (the MA order) must be an integer >= 0, got `{argv[k + 1]}'")
|
|
296
|
+
if r is None or r < 0:
|
|
297
|
+
raise CliError(f"r (the cointegration rank) must be an integer >= 0, got "
|
|
298
|
+
f"`{argv[k + 2]}'")
|
|
299
|
+
validate(argv, first)
|
|
300
|
+
o = dict(files=files, p=p, q=q, r=r, case=1, case_given=False, mean=False,
|
|
301
|
+
nodrift=[], diag_ar=False, diag_cov=False, ma=None, fixb2=None, fixb2row={},
|
|
302
|
+
xpre=[], xlink=None, xsys=[], gzero=[], lrtest=False, bootstrap=0, irfboot=0,
|
|
303
|
+
forecast=0, estwin=0, csv=None, alpha=None, weakex=0, fdhess=False,
|
|
304
|
+
name=None, method=1, multistart=0, rungs=False, differenced=False, interv=False, unported=[])
|
|
305
|
+
i = first
|
|
306
|
+
while i < len(argv):
|
|
307
|
+
a = argv[i]
|
|
308
|
+
nxt = argv[i + 1] if i + 1 < len(argv) else None
|
|
309
|
+
kind = _OPTS[a][1]
|
|
310
|
+
if not _OPTS[a][3]:
|
|
311
|
+
o["unported"].append(a)
|
|
312
|
+
if a == "-mean":
|
|
313
|
+
o["mean"] = True
|
|
314
|
+
elif a == "-case":
|
|
315
|
+
if nxt == "pre":
|
|
316
|
+
o["case"] = "pre"
|
|
317
|
+
else:
|
|
318
|
+
o["case"] = int(nxt)
|
|
319
|
+
o["case_given"] = True
|
|
320
|
+
elif a == "-nodrift":
|
|
321
|
+
o["nodrift"].append(int(nxt))
|
|
322
|
+
elif a == "-m":
|
|
323
|
+
o["method"] = int(nxt)
|
|
324
|
+
elif a == "-diagar":
|
|
325
|
+
o["diag_ar"] = True
|
|
326
|
+
elif a == "-trend":
|
|
327
|
+
o["trend"] = True
|
|
328
|
+
elif a == "-diagma":
|
|
329
|
+
o["ma"] = "diag"
|
|
330
|
+
elif a == "-diagcov":
|
|
331
|
+
o["diag_cov"] = True
|
|
332
|
+
elif a == "-marow":
|
|
333
|
+
o["ma"] = "marow"
|
|
334
|
+
elif a == "-matri":
|
|
335
|
+
o["ma"] = "matri"
|
|
336
|
+
elif a == "-mafree":
|
|
337
|
+
o["ma"] = o["ma"] or "free"
|
|
338
|
+
elif a == "-differenced":
|
|
339
|
+
o["differenced"] = True
|
|
340
|
+
elif a == "-levels":
|
|
341
|
+
o["differenced"] = False
|
|
342
|
+
elif a == "-fixb2":
|
|
343
|
+
v = _real(nxt) if nxt is not None else None
|
|
344
|
+
if v is not None:
|
|
345
|
+
o["fixb2"] = v
|
|
346
|
+
i += 1
|
|
347
|
+
else:
|
|
348
|
+
o["fixb2"] = True
|
|
349
|
+
elif a == "-fixb2row":
|
|
350
|
+
o["fixb2row"][int(argv[i + 1])] = float(argv[i + 2])
|
|
351
|
+
i += 2
|
|
352
|
+
elif a == "-gzero":
|
|
353
|
+
o["gzero"].append((int(argv[i + 1]), int(argv[i + 2])))
|
|
354
|
+
i += 2
|
|
355
|
+
elif a == "-xpre":
|
|
356
|
+
o["xpre"].append(nxt)
|
|
357
|
+
elif a == "-xlink":
|
|
358
|
+
o["xlink"] = nxt
|
|
359
|
+
elif a == "-xsys":
|
|
360
|
+
o["xsys"].append(int(nxt))
|
|
361
|
+
elif a == "-lrtest":
|
|
362
|
+
o["lrtest"] = True
|
|
363
|
+
elif a == "-rungs":
|
|
364
|
+
o["rungs"] = True
|
|
365
|
+
elif a == "-bootstrap":
|
|
366
|
+
o["bootstrap"] = int(nxt)
|
|
367
|
+
elif a == "-irfboot":
|
|
368
|
+
o["irfboot"] = int(nxt)
|
|
369
|
+
elif a == "-f":
|
|
370
|
+
o["forecast"] = int(nxt)
|
|
371
|
+
elif a == "-estwin":
|
|
372
|
+
o["estwin"] = int(nxt)
|
|
373
|
+
elif a == "-C":
|
|
374
|
+
o["csv"] = nxt
|
|
375
|
+
elif a == "-alpha":
|
|
376
|
+
o["alpha"] = nxt
|
|
377
|
+
elif a == "-weakex":
|
|
378
|
+
o["weakex"] = int(nxt)
|
|
379
|
+
elif a == "-fdhess":
|
|
380
|
+
o["fdhess"] = True
|
|
381
|
+
elif a == "-multistart":
|
|
382
|
+
o["multistart"] = int(nxt)
|
|
383
|
+
elif a == "-interv":
|
|
384
|
+
o["interv"] = True
|
|
385
|
+
elif a == "-name":
|
|
386
|
+
o["name"] = nxt
|
|
387
|
+
if kind in ("str", "int", "case", "method", "real"):
|
|
388
|
+
i += 1
|
|
389
|
+
elif kind == "tol_opt" and nxt is not None and not nxt.startswith("-"):
|
|
390
|
+
i += 1
|
|
391
|
+
i += 1
|
|
392
|
+
o.setdefault("trend", False)
|
|
393
|
+
# -mean is case 2 unless a case was given (BUG-45: -case 1 -mean refused)
|
|
394
|
+
if o["mean"] and o["case"] == 1:
|
|
395
|
+
if o["case_given"]:
|
|
396
|
+
raise CliError("ERROR: -case 1 has no deterministic term and -mean asks for "
|
|
397
|
+
"one. Drop one of them\n (-mean alone means case 2).",
|
|
398
|
+
code=2)
|
|
399
|
+
o["case"] = 2
|
|
400
|
+
if o["name"]:
|
|
401
|
+
o["base"] = o["name"]
|
|
402
|
+
else:
|
|
403
|
+
stems = []
|
|
404
|
+
for f in files:
|
|
405
|
+
b = os.path.basename(f)
|
|
406
|
+
stems.append(b[:b.rfind(".")] if "." in b else b)
|
|
407
|
+
o["base"] = "_".join(stems)
|
|
408
|
+
return o
|
|
409
|
+
|
|
410
|
+
|
|
411
|
+
def _refusals(o):
|
|
412
|
+
"""The C's checks of combinations (parse_cli's tail), in its order."""
|
|
413
|
+
if o["interv"]:
|
|
414
|
+
raise CliError("ERROR: -interv does nothing on the .pre route -- the deterministic\n"
|
|
415
|
+
" terms are already subtracted, from those same models.\n"
|
|
416
|
+
" Accepting it would subtract them twice.")
|
|
417
|
+
if o["differenced"]:
|
|
418
|
+
raise CliError("ERROR: -differenced does not apply to the .pre route. A .pre\n"
|
|
419
|
+
" carries the series in levels and says how it is differenced;\n"
|
|
420
|
+
" drvec forms nabla Y2 itself.")
|
|
421
|
+
if o["unported"]:
|
|
422
|
+
raise CliError("ERROR: %s %s not ported to drvec-py yet (docs/PORT.md §10);\n"
|
|
423
|
+
" the C program drvec has %s." % (
|
|
424
|
+
", ".join(o["unported"]),
|
|
425
|
+
"is" if len(o["unported"]) == 1 else "are",
|
|
426
|
+
"it" if len(o["unported"]) == 1 else "them"), code=2)
|
|
427
|
+
if o["method"] == 2:
|
|
428
|
+
raise CliError("ERROR: -m 2 (the xi sequence not truncated) is not ported to "
|
|
429
|
+
"drvec-py yet.", code=2)
|
|
430
|
+
for i, j in o["gzero"]:
|
|
431
|
+
if i == j:
|
|
432
|
+
raise CliError(f"ERROR: -gzero {i} {j}: the diagonal of Gamma is the series' own "
|
|
433
|
+
"model\n (the ladder's rung), and is never held.", code=2)
|
|
434
|
+
if o["gzero"] and (o["lrtest"] or o["rungs"] or o["xsys"] or o["xlink"] or o["xpre"]
|
|
435
|
+
or o["fixb2row"] or o["fixb2"] is not None or o["diag_ar"]
|
|
436
|
+
or o["q"] > 0 or o["p"] < 2):
|
|
437
|
+
from .api import MSG_GZERO
|
|
438
|
+
raise CliError("ERROR: " + MSG_GZERO, code=2)
|
|
439
|
+
if o["lrtest"] and o["rungs"]:
|
|
440
|
+
raise CliError("ERROR: one mode per run: -lrtest, -specs, -rungs, -matest/-artest "
|
|
441
|
+
"and -eval\n cannot be combined.", code=2)
|
|
442
|
+
if o["rungs"] and (o["forecast"] or o["estwin"]):
|
|
443
|
+
raise CliError("ERROR: -f and -estwin act on a single fit; the mode asked for "
|
|
444
|
+
"does not\n produce one. Run them separately.", code=2)
|
|
445
|
+
if o["rungs"]:
|
|
446
|
+
for on, msg in ((o["xlink"] or o["xpre"], MSG_XLINK), (o["irfboot"], MSG_IRFBOOT),
|
|
447
|
+
(o["trend"], MSG_TREND), (o["xsys"], MSG_XSYS),
|
|
448
|
+
(o["fixb2row"], MSG_FIXB2ROW)):
|
|
449
|
+
if on:
|
|
450
|
+
raise CliError("ERROR: " + msg, code=2)
|
|
451
|
+
late = [n for n, on in (("-alpha", o["alpha"]), ("-weakex", o["weakex"]),
|
|
452
|
+
("-fixb2", o["fixb2"] is not None),
|
|
453
|
+
("-nodrift", o["nodrift"]), ("-case pre", o["case"] == "pre"))
|
|
454
|
+
if on]
|
|
455
|
+
if late:
|
|
456
|
+
raise CliError("ERROR: -rungs with %s is not ported to drvec-py yet."
|
|
457
|
+
% ", ".join(late), code=2)
|
|
458
|
+
if o["lrtest"]:
|
|
459
|
+
if o["forecast"] or o["estwin"]:
|
|
460
|
+
raise CliError("ERROR: -f and -estwin act on a single fit; the mode asked for "
|
|
461
|
+
"does not\n produce one. Run them separately.", code=2)
|
|
462
|
+
if o["irfboot"]:
|
|
463
|
+
raise CliError("ERROR: " + MSG_IRFBOOT, code=2)
|
|
464
|
+
if o["fixb2row"]:
|
|
465
|
+
raise CliError("ERROR: " + MSG_FIXB2ROW, code=2)
|
|
466
|
+
late = [n for n, on in (("-alpha", o["alpha"]), ("-weakex", o["weakex"]),
|
|
467
|
+
("-fixb2", o["fixb2"] is not None),
|
|
468
|
+
("-diagar", o["diag_ar"]), ("-diagcov", o["diag_cov"]))
|
|
469
|
+
if on]
|
|
470
|
+
if late:
|
|
471
|
+
raise CliError("ERROR: -lrtest with %s is not ported to drvec-py yet (the C "
|
|
472
|
+
"program drvec has it)." % ", ".join(late), code=2)
|
|
473
|
+
if o["bootstrap"] > 0 and not o["lrtest"]:
|
|
474
|
+
raise CliError("ERROR: -bootstrap calibrates -lrtest and does nothing without it.",
|
|
475
|
+
code=2)
|
|
476
|
+
|
|
477
|
+
|
|
478
|
+
def run(argv, out=sys.stdout, err=sys.stderr):
|
|
479
|
+
from . import __version__
|
|
480
|
+
if argv and argv[0] in ("-h", "--help"):
|
|
481
|
+
out.write(usage())
|
|
482
|
+
return 0
|
|
483
|
+
if argv and argv[0] in ("--version", "-version"):
|
|
484
|
+
out.write(f"{PROG} {__version__}\nVEC model EML estimation (Mauricio 2006), "
|
|
485
|
+
"the Python port of drvec, on the drvarma engine\nGPL v2 or later\n")
|
|
486
|
+
return 0
|
|
487
|
+
try:
|
|
488
|
+
o = parse(argv)
|
|
489
|
+
_refusals(o)
|
|
490
|
+
except CliError as e:
|
|
491
|
+
if str(e):
|
|
492
|
+
msg = str(e)
|
|
493
|
+
err.write((msg if msg.startswith("ERROR") else f"drvec-py: {msg}") + "\n")
|
|
494
|
+
if not e.usage and not msg.startswith("ERROR"):
|
|
495
|
+
err.write(f" `{PROG} -h' lists every option.\n")
|
|
496
|
+
if e.usage:
|
|
497
|
+
err.write(usage())
|
|
498
|
+
return e.code
|
|
499
|
+
return _execute(o, out, err)
|
|
500
|
+
|
|
501
|
+
|
|
502
|
+
def _execute(o, out, err):
|
|
503
|
+
import drvec
|
|
504
|
+
from . import __version__
|
|
505
|
+
base = o["base"]
|
|
506
|
+
for f in o["files"]:
|
|
507
|
+
if not os.path.exists(f):
|
|
508
|
+
err.write(f"ERROR: cannot open {f}\n")
|
|
509
|
+
return 1
|
|
510
|
+
out.write(f"\nDRVEC {__version__} (python) -- VEC model EML estimation (Mauricio 2006)\n"
|
|
511
|
+
f"Input : the .pre files\nOutput : {base}.out\n"
|
|
512
|
+
f"Model : VEC({o['r']}) with stationary VARMA({o['p']},{o['q']}) on Ybar_t\n"
|
|
513
|
+
f"Case : {o['case']}\n")
|
|
514
|
+
try:
|
|
515
|
+
if o["rungs"]:
|
|
516
|
+
t = drvec.rungs(o["files"], o["p"], o["q"], o["r"], case=o["case"],
|
|
517
|
+
ma=o["ma"] or "free")
|
|
518
|
+
_write(base + ".out", t.out_text(base))
|
|
519
|
+
for k in range(3):
|
|
520
|
+
out.write(" rung %d : %s\n" % (k, "ok" if t.ll[k] is not None
|
|
521
|
+
else "estimation failed"))
|
|
522
|
+
out.write(f"Done. Output written to {base}.out\n")
|
|
523
|
+
return 0
|
|
524
|
+
if o["lrtest"]:
|
|
525
|
+
if o["nodrift"] or o["case"] == "pre":
|
|
526
|
+
err.write(MSG_MIXED)
|
|
527
|
+
boot = base + ".boot" if o["bootstrap"] > 0 else None
|
|
528
|
+
t = drvec.rank_test(o["files"], o["p"], o["q"], case=o["case"],
|
|
529
|
+
ma=o["ma"] or "free", bootstrap=o["bootstrap"],
|
|
530
|
+
boot_path=boot, trend=o["trend"], xsys=o["xsys"] or None,
|
|
531
|
+
xpre=o["xpre"] or None, xlink=o["xlink"],
|
|
532
|
+
multistart=o["multistart"], nodrift=o["nodrift"] or None)
|
|
533
|
+
text = t.out_text(base)
|
|
534
|
+
_write(base + ".out", text)
|
|
535
|
+
for rr in range(t.spec.M - 1):
|
|
536
|
+
out.write(" r = %d: LR = %.6f\n" % (rr, t.lr(rr)))
|
|
537
|
+
if boot:
|
|
538
|
+
out.write(f"Bootstrap replications written to {boot}\n")
|
|
539
|
+
out.write(f"Done. Output written to {base}.out\n")
|
|
540
|
+
return 0
|
|
541
|
+
res = drvec.fit(o["files"], o["p"], o["q"], o["r"], case=o["case"],
|
|
542
|
+
ma=o["ma"] or "free", diag_ar=o["diag_ar"], diag_cov=o["diag_cov"],
|
|
543
|
+
fixb2row=o["fixb2row"] or None, nodrift=o["nodrift"] or None,
|
|
544
|
+
trend=o["trend"], irfboot=o["irfboot"], xsys=o["xsys"] or None,
|
|
545
|
+
xpre=o["xpre"] or None, xlink=o["xlink"],
|
|
546
|
+
forecast=o["forecast"], fdhess=o["fdhess"], alpha=o["alpha"],
|
|
547
|
+
weakex=o["weakex"] or None, fixb2=o["fixb2"], estwin=o["estwin"],
|
|
548
|
+
multistart=o["multistart"], gzero=o["gzero"] or None)
|
|
549
|
+
except (ValueError, NotImplementedError) as e:
|
|
550
|
+
msg = str(e)
|
|
551
|
+
err.write((msg if msg.startswith("ERROR") else "ERROR: " + msg) + "\n")
|
|
552
|
+
return 2
|
|
553
|
+
except RuntimeError as e:
|
|
554
|
+
err.write(f"ERROR: {e}\n")
|
|
555
|
+
return 2
|
|
556
|
+
except OSError as e:
|
|
557
|
+
err.write(f"ERROR: {e}\n")
|
|
558
|
+
return 1
|
|
559
|
+
if res.fdhess is not None and not res.fdhess.ok:
|
|
560
|
+
err.write(res.fdhess.warning(res.npar))
|
|
561
|
+
_write(base + ".out", res.out_text(base))
|
|
562
|
+
out.write(" logL = %.10f npar = %d %s\n" % (res.loglik, res.npar,
|
|
563
|
+
res.search.best.label))
|
|
564
|
+
ib = res.irfboot_text(base) if res.irfboot is not None else None
|
|
565
|
+
if ib is not None:
|
|
566
|
+
# too few replications for bands: the C writes no .irfboot, and its
|
|
567
|
+
# .out says so (bugs/py-cli-irfboot-few)
|
|
568
|
+
_write(base + ".irfboot", ib)
|
|
569
|
+
out.write(f"Bands written to {base}.irfboot\n")
|
|
570
|
+
if res.forecast_h:
|
|
571
|
+
_write(base + ".forecast", res.forecast_text(base))
|
|
572
|
+
out.write(f"Forecasts written to {base}.forecast\n")
|
|
573
|
+
rec = res.recursive_text()
|
|
574
|
+
if rec is not None:
|
|
575
|
+
path = o["csv"] or base + ".recursive"
|
|
576
|
+
_write(path, rec)
|
|
577
|
+
out.write(f"Per-origin errors: {path}\n")
|
|
578
|
+
out.write(f"Done. Output written to {base}.out\n")
|
|
579
|
+
return 0
|
|
580
|
+
|
|
581
|
+
|
|
582
|
+
def _write(path, text):
|
|
583
|
+
with open(path, "w") as f:
|
|
584
|
+
f.write(text)
|
|
585
|
+
|
|
586
|
+
|
|
587
|
+
def main():
|
|
588
|
+
sys.exit(run(sys.argv[1:]))
|
|
589
|
+
|
|
590
|
+
|
|
591
|
+
if __name__ == "__main__":
|
|
592
|
+
main()
|