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/c_engine.py
ADDED
|
@@ -0,0 +1,391 @@
|
|
|
1
|
+
"""drvec C as the bootstrap engine (PLAN-vec §B).
|
|
2
|
+
|
|
3
|
+
The C program drvec is the port's oracle: on Linux x86_64 the port is
|
|
4
|
+
byte-identical to it. Running it for the bootstraps is therefore not a second
|
|
5
|
+
implementation but the same model, run faster (the port is 6-15x slower). Only
|
|
6
|
+
the BOOTSTRAPS go to the C -- the rank test's (`-lrtest -bootstrap N`) and the
|
|
7
|
+
IRF bands' (`-irfboot N`). The fits, the restrictions and every structure come
|
|
8
|
+
from the port's own objects; the C's replications are read back into them:
|
|
9
|
+
|
|
10
|
+
* rank_test: the port fits the observed ranks (while the C runs, in parallel);
|
|
11
|
+
the C's <base>.boot gives every replication, its status and its LR* to 17
|
|
12
|
+
digits, which round-trip exactly, and the critical values and the p-value are
|
|
13
|
+
computed from them by the port's own code (rank.summarise). The result is the
|
|
14
|
+
same RankTest the Python engine returns, and its .out and .boot the same
|
|
15
|
+
bytes.
|
|
16
|
+
* fit(irfboot=N): the port fits the model; the C's <base>.irfboot gives the
|
|
17
|
+
bands (to its 8 significant digits) and its .out the band section, which the
|
|
18
|
+
port's .out then carries verbatim (the replications themselves are in no file
|
|
19
|
+
of the C's).
|
|
20
|
+
|
|
21
|
+
Finding the binary, in this order:
|
|
22
|
+
1. DRVEC_BIN (a path);
|
|
23
|
+
2. the binary bundled in the wheel, drvec/_bin/drvec (drvec.exe on Windows),
|
|
24
|
+
built from the vendored sources (csrc/) by setup.py, when present (the
|
|
25
|
+
platform wheels; not the pure wheel);
|
|
26
|
+
3. `drvec` on the PATH;
|
|
27
|
+
4. the ATSW install directory (Linux: /usr/lib/atsw/bin, /usr/local/lib/atsw/bin;
|
|
28
|
+
macOS: /Applications/ATSW.app/Contents/Resources/bin and the same under
|
|
29
|
+
~/Applications; Windows: %LOCALAPPDATA%\\Programs\\ATSW\\bin and
|
|
30
|
+
%ProgramFiles%\\ATSW\\bin).
|
|
31
|
+
The first candidate that exists is the one used, and its banner (`drvec
|
|
32
|
+
--version`, first line `drvec X.Y`) must name the version the port mirrors,
|
|
33
|
+
C_VERSION; otherwise it is refused with the reason.
|
|
34
|
+
|
|
35
|
+
The C's bootstrap generator is fixed at the port's default seed: a call with
|
|
36
|
+
another seed cannot go to the C.
|
|
37
|
+
"""
|
|
38
|
+
from __future__ import annotations
|
|
39
|
+
|
|
40
|
+
import os
|
|
41
|
+
import re
|
|
42
|
+
import shutil
|
|
43
|
+
import subprocess
|
|
44
|
+
import sys
|
|
45
|
+
import tempfile
|
|
46
|
+
import time
|
|
47
|
+
from dataclasses import dataclass
|
|
48
|
+
|
|
49
|
+
C_VERSION = "0.13" # the oracle the port mirrors (atsw-gui ae95446; csrc/ is 800a540: + BUG-61 to 64, -gzero, the I(1, r) check)
|
|
50
|
+
_EXE = "drvec.exe" if sys.platform == "win32" else "drvec"
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
class CEngineUnavailable(RuntimeError):
|
|
54
|
+
"""No usable drvec C binary: the message says why."""
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
class CEngineError(RuntimeError):
|
|
58
|
+
"""The C ran and failed, or its products disagree with the port's fits."""
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
@dataclass
|
|
62
|
+
class CBinary:
|
|
63
|
+
path: str
|
|
64
|
+
version: str
|
|
65
|
+
source: str # DRVEC_BIN, bundled, PATH or ATSW
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def _bundled():
|
|
69
|
+
try:
|
|
70
|
+
from importlib.resources import files
|
|
71
|
+
p = files("drvec").joinpath("_bin").joinpath(_EXE)
|
|
72
|
+
if not p.is_file():
|
|
73
|
+
return None
|
|
74
|
+
p = str(p)
|
|
75
|
+
if sys.platform != "win32" and not os.access(p, os.X_OK):
|
|
76
|
+
# an installer that dropped the wheel's executable bit
|
|
77
|
+
try:
|
|
78
|
+
os.chmod(p, os.stat(p).st_mode | 0o111)
|
|
79
|
+
except OSError:
|
|
80
|
+
pass
|
|
81
|
+
return p
|
|
82
|
+
except Exception: # noqa: BLE001
|
|
83
|
+
return None
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def _atsw_dirs():
|
|
87
|
+
home = os.path.expanduser("~")
|
|
88
|
+
if sys.platform == "win32":
|
|
89
|
+
out = []
|
|
90
|
+
for env in ("LOCALAPPDATA",):
|
|
91
|
+
if os.environ.get(env):
|
|
92
|
+
out.append(os.path.join(os.environ[env], "Programs", "ATSW", "bin"))
|
|
93
|
+
for env in ("ProgramFiles", "ProgramFiles(x86)"):
|
|
94
|
+
if os.environ.get(env):
|
|
95
|
+
out.append(os.path.join(os.environ[env], "ATSW", "bin"))
|
|
96
|
+
return out
|
|
97
|
+
if sys.platform == "darwin":
|
|
98
|
+
return ["/Applications/ATSW.app/Contents/Resources/bin",
|
|
99
|
+
os.path.join(home, "Applications", "ATSW.app", "Contents", "Resources", "bin")]
|
|
100
|
+
return ["/usr/lib/atsw/bin", "/usr/local/lib/atsw/bin"]
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def candidates():
|
|
104
|
+
"""[(source, path)] in the search order, only those that exist (DRVEC_BIN
|
|
105
|
+
is listed even when it does not, so that the refusal can say so)."""
|
|
106
|
+
out = []
|
|
107
|
+
env = os.environ.get("DRVEC_BIN", "").strip()
|
|
108
|
+
if env:
|
|
109
|
+
out.append(("DRVEC_BIN", env))
|
|
110
|
+
b = _bundled()
|
|
111
|
+
if b:
|
|
112
|
+
out.append(("bundled", b))
|
|
113
|
+
w = shutil.which("drvec")
|
|
114
|
+
if w:
|
|
115
|
+
out.append(("PATH", w))
|
|
116
|
+
for d in _atsw_dirs():
|
|
117
|
+
p = os.path.join(d, _EXE)
|
|
118
|
+
if os.path.isfile(p):
|
|
119
|
+
out.append(("ATSW", p))
|
|
120
|
+
return out
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
_CACHE = {}
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def banner_version(path):
|
|
127
|
+
"""The version in `drvec --version`'s first line (`drvec 0.13`), or raise
|
|
128
|
+
CEngineUnavailable."""
|
|
129
|
+
try:
|
|
130
|
+
st = os.stat(path)
|
|
131
|
+
except OSError as e:
|
|
132
|
+
raise CEngineUnavailable(f"{path}: {e.strerror}") from None
|
|
133
|
+
key = (path, st.st_mtime_ns, st.st_size)
|
|
134
|
+
if key in _CACHE:
|
|
135
|
+
return _CACHE[key]
|
|
136
|
+
try:
|
|
137
|
+
cp = subprocess.run([path, "--version"], capture_output=True, text=True, timeout=30)
|
|
138
|
+
except (OSError, subprocess.SubprocessError) as e:
|
|
139
|
+
raise CEngineUnavailable(f"{path} does not run: {e}") from None
|
|
140
|
+
first = (cp.stdout or "").strip().splitlines()[:1]
|
|
141
|
+
m = re.match(r"^drvec\s+(\S+)", first[0]) if first else None
|
|
142
|
+
if not m:
|
|
143
|
+
raise CEngineUnavailable(f"{path} is not drvec C: its banner is "
|
|
144
|
+
f"`{first[0] if first else ''}'")
|
|
145
|
+
_CACHE[key] = m.group(1)
|
|
146
|
+
return m.group(1)
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
def find():
|
|
150
|
+
"""The binary to use (CBinary), or raise CEngineUnavailable with the
|
|
151
|
+
reason: none found, or the first one found is not drvec C_VERSION."""
|
|
152
|
+
cands = candidates()
|
|
153
|
+
if not cands:
|
|
154
|
+
raise CEngineUnavailable(
|
|
155
|
+
"drvec C not found: set DRVEC_BIN, put `drvec' on the PATH or install "
|
|
156
|
+
"ATSW with it")
|
|
157
|
+
source, path = cands[0]
|
|
158
|
+
if not os.path.isfile(path):
|
|
159
|
+
raise CEngineUnavailable(f"{source} = {path}: no such file")
|
|
160
|
+
v = banner_version(path)
|
|
161
|
+
if v != C_VERSION:
|
|
162
|
+
raise CEngineUnavailable(
|
|
163
|
+
f"{path} ({source}) is drvec C {v}; drvec-python mirrors {C_VERSION}, and "
|
|
164
|
+
"only that version gives the port's numbers")
|
|
165
|
+
return CBinary(path, v, source)
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
def available():
|
|
169
|
+
"""CBinary or None (no reason given; find() gives it)."""
|
|
170
|
+
try:
|
|
171
|
+
return find()
|
|
172
|
+
except CEngineUnavailable:
|
|
173
|
+
return None
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
# --------------------------------------------------------------------------- #
|
|
177
|
+
# The translation: a port call -> the C's argv (the inverse of cli.parse) #
|
|
178
|
+
# --------------------------------------------------------------------------- #
|
|
179
|
+
|
|
180
|
+
_MA_FLAG = {"free": None, "diag": "-diagma", "marow": "-marow", "matri": "-matri"}
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
def _num(v):
|
|
184
|
+
"""A float the C's strtod/atof reads back to the same double."""
|
|
185
|
+
return repr(float(v))
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
def _common(files, p, q, r, case, ma, nodrift, multistart):
|
|
189
|
+
argv = [os.path.abspath(f) for f in files] + [str(int(p)), str(int(q)), str(int(r))]
|
|
190
|
+
argv += ["-case", "pre" if case == "pre" else str(int(case))]
|
|
191
|
+
if ma not in _MA_FLAG:
|
|
192
|
+
raise ValueError(f"ma = {ma!r} has no drvec C flag")
|
|
193
|
+
if _MA_FLAG[ma]:
|
|
194
|
+
argv.append(_MA_FLAG[ma])
|
|
195
|
+
for i in (nodrift or ()):
|
|
196
|
+
argv += ["-nodrift", str(int(i))]
|
|
197
|
+
if multistart and multistart > 1:
|
|
198
|
+
argv += ["-multistart", str(int(multistart))]
|
|
199
|
+
return argv
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
def rank_argv(files, p, q, case=2, ma="free", bootstrap=0, nodrift=None, multistart=0,
|
|
203
|
+
base="drvecjob"):
|
|
204
|
+
"""drvec.rank_test(...) -> the C's arguments (without the program)."""
|
|
205
|
+
argv = _common(files, p, q, 0, case, ma, nodrift, multistart)
|
|
206
|
+
argv += ["-lrtest"]
|
|
207
|
+
if bootstrap:
|
|
208
|
+
argv += ["-bootstrap", str(int(bootstrap))]
|
|
209
|
+
return argv + ["-name", base]
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
def fit_argv(files, p, q, r, folder, case=2, ma="free", diag_ar=False, diag_cov=False,
|
|
213
|
+
fixb2row=None, nodrift=None, xsys=None, alpha=None, weakex=None, fixb2=None,
|
|
214
|
+
gzero=None,
|
|
215
|
+
multistart=0, irfboot=0, base="drvecjob"):
|
|
216
|
+
"""drvec.fit(...) for -irfboot -> the C's arguments. Only what moves the
|
|
217
|
+
fitted model (and so the samples) is passed: -fdhess and -f do not, and
|
|
218
|
+
-trend, -estwin, -xpre/-xlink are refused with -irfboot by both programs.
|
|
219
|
+
An alpha given as a matrix is written to <folder>/<base>.alpha."""
|
|
220
|
+
argv = _common(files, p, q, r, case, ma, nodrift, multistart)
|
|
221
|
+
if diag_ar:
|
|
222
|
+
argv.append("-diagar")
|
|
223
|
+
if diag_cov:
|
|
224
|
+
argv.append("-diagcov")
|
|
225
|
+
for i, v in sorted((fixb2row or {}).items()):
|
|
226
|
+
argv += ["-fixb2row", str(int(i)), _num(v)]
|
|
227
|
+
for i in (xsys or ()):
|
|
228
|
+
argv += ["-xsys", str(int(i))]
|
|
229
|
+
for i, j in (gzero or ()):
|
|
230
|
+
argv += ["-gzero", str(int(i)), str(int(j))]
|
|
231
|
+
if weakex:
|
|
232
|
+
argv += ["-weakex", str(int(weakex))]
|
|
233
|
+
elif alpha is not None:
|
|
234
|
+
if isinstance(alpha, (str, bytes)) or hasattr(alpha, "__fspath__"):
|
|
235
|
+
argv += ["-alpha", os.path.abspath(os.fsdecode(alpha))]
|
|
236
|
+
else:
|
|
237
|
+
A = [list(row) for row in alpha]
|
|
238
|
+
path = os.path.join(folder, base + ".alpha")
|
|
239
|
+
with open(path, "w") as f:
|
|
240
|
+
f.write("%d %d\n" % (len(A), len(A[0])))
|
|
241
|
+
for row in A:
|
|
242
|
+
f.write(" ".join(_num(v) for v in row) + "\n")
|
|
243
|
+
argv += ["-alpha", path]
|
|
244
|
+
if fixb2 is True:
|
|
245
|
+
argv.append("-fixb2")
|
|
246
|
+
elif fixb2 is not None and fixb2 is not False:
|
|
247
|
+
argv += ["-fixb2", _num(fixb2)]
|
|
248
|
+
if irfboot:
|
|
249
|
+
argv += ["-irfboot", str(int(irfboot))]
|
|
250
|
+
return argv + ["-name", base]
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
# --------------------------------------------------------------------------- #
|
|
254
|
+
# Running it, and reading its files back #
|
|
255
|
+
# --------------------------------------------------------------------------- #
|
|
256
|
+
|
|
257
|
+
class Job:
|
|
258
|
+
"""One run of the C in `folder`, started at once (Popen); wait() returns
|
|
259
|
+
when it is done. stdout and stderr go to <base>.stdout and <base>.stderr."""
|
|
260
|
+
|
|
261
|
+
def __init__(self, binary, argv, folder, base):
|
|
262
|
+
self.binary, self.argv, self.folder, self.base = binary, argv, folder, base
|
|
263
|
+
self.t0 = time.time()
|
|
264
|
+
self._out = open(os.path.join(folder, base + ".stdout"), "w")
|
|
265
|
+
self._err = open(os.path.join(folder, base + ".stderr"), "w")
|
|
266
|
+
self.proc = subprocess.Popen([binary.path] + argv, cwd=folder, stdout=self._out,
|
|
267
|
+
stderr=self._err, stdin=subprocess.DEVNULL)
|
|
268
|
+
|
|
269
|
+
def path(self, ext):
|
|
270
|
+
return os.path.join(self.folder, self.base + ext)
|
|
271
|
+
|
|
272
|
+
def wait(self, on_boot_line=None, poll=0.5):
|
|
273
|
+
"""Wait for the C; on_boot_line(line) is called for each complete line
|
|
274
|
+
of <base>.boot as it appears (the C writes one per replication)."""
|
|
275
|
+
seen = 0
|
|
276
|
+
buf = ""
|
|
277
|
+
while True:
|
|
278
|
+
rc = self.proc.poll()
|
|
279
|
+
if on_boot_line is not None and os.path.exists(self.path(".boot")):
|
|
280
|
+
# binary, and the offset in bytes: on Windows the C writes
|
|
281
|
+
# CRLF, and text mode's "\n" for "\r\n" put `seen` behind the
|
|
282
|
+
# file (bugs/py-boot-tail-crlf)
|
|
283
|
+
with open(self.path(".boot"), "rb") as f:
|
|
284
|
+
f.seek(seen)
|
|
285
|
+
chunk = f.read()
|
|
286
|
+
seen += len(chunk)
|
|
287
|
+
buf += chunk.decode("ascii", "replace")
|
|
288
|
+
*lines, buf = buf.split("\n")
|
|
289
|
+
for line in lines:
|
|
290
|
+
on_boot_line(line.rstrip("\r"))
|
|
291
|
+
if rc is not None:
|
|
292
|
+
break
|
|
293
|
+
time.sleep(poll)
|
|
294
|
+
self._out.close()
|
|
295
|
+
self._err.close()
|
|
296
|
+
self.elapsed = time.time() - self.t0
|
|
297
|
+
if rc != 0:
|
|
298
|
+
with open(self.path(".stderr")) as f:
|
|
299
|
+
tail = f.read()[-2000:]
|
|
300
|
+
raise CEngineError(f"drvec C exited with {rc} in {self.folder}:\n{tail}")
|
|
301
|
+
return rc
|
|
302
|
+
|
|
303
|
+
|
|
304
|
+
def parse_boot_line(line):
|
|
305
|
+
"""`h0,rep,status,lr` -> rank.Replication, or None for a header line."""
|
|
306
|
+
from .rank import Replication
|
|
307
|
+
if not line or not line[0].isdigit():
|
|
308
|
+
return None
|
|
309
|
+
h0, rep, st, lr = line.strip().split(",")
|
|
310
|
+
return Replication(int(h0), int(rep), st, float(lr) if (st == "ok" and lr) else float("nan"))
|
|
311
|
+
|
|
312
|
+
|
|
313
|
+
def read_boot(path):
|
|
314
|
+
"""<base>.boot -> {h0: [Replication]} in the file's order."""
|
|
315
|
+
out = {}
|
|
316
|
+
with open(path) as f:
|
|
317
|
+
for line in f:
|
|
318
|
+
r = parse_boot_line(line)
|
|
319
|
+
if r is not None:
|
|
320
|
+
out.setdefault(r.h0, []).append(r)
|
|
321
|
+
return out
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
BANDS_BANNER = "Bands for the Level Responses, Parametric Bootstrap"
|
|
325
|
+
|
|
326
|
+
|
|
327
|
+
def read_irfboot(out_text, file_text, names, K, c_base):
|
|
328
|
+
"""The C's band section of its .out and its <base>.irfboot ->
|
|
329
|
+
(nok, nfail, band section text, results.IrfBands or None)."""
|
|
330
|
+
import numpy as np
|
|
331
|
+
from .report import banner
|
|
332
|
+
from .results import IrfBands
|
|
333
|
+
b = banner(BANDS_BANNER)
|
|
334
|
+
k = out_text.find(b)
|
|
335
|
+
if k < 0:
|
|
336
|
+
raise CEngineError("the C's .out has no band section")
|
|
337
|
+
sect = out_text[k:]
|
|
338
|
+
m = re.search(r"(\d+) replications simulated from the fitted model and REFITTED as the\n"
|
|
339
|
+
r" data were \(same rank, restrictions and ladder start\); (\d+) failed", sect)
|
|
340
|
+
if not m:
|
|
341
|
+
raise CEngineError("the C's band section does not say how many replications it kept")
|
|
342
|
+
nok, nfail = int(m.group(1)), int(m.group(2))
|
|
343
|
+
if file_text is None:
|
|
344
|
+
return nok, nfail, sect, None
|
|
345
|
+
M = len(names)
|
|
346
|
+
ix = {nm: i for i, nm in enumerate(names)}
|
|
347
|
+
irf = np.zeros((K + 1, M, M))
|
|
348
|
+
fev = np.zeros((K + 1, M, M))
|
|
349
|
+
keys = (2.5, 5.0, 50.0, 95.0, 97.5)
|
|
350
|
+
ip = {p: np.zeros((K + 1, M, M)) for p in keys}
|
|
351
|
+
fp = {p: np.zeros((K + 1, M, M)) for p in keys}
|
|
352
|
+
for line in file_text.splitlines()[2:]:
|
|
353
|
+
if not line.strip():
|
|
354
|
+
continue
|
|
355
|
+
what, shock, resp, kk, pt, *q = line.split(",")
|
|
356
|
+
j, i, kk = ix[shock], ix[resp], int(kk)
|
|
357
|
+
if what == "irf":
|
|
358
|
+
irf[kk, i, j] = float(pt)
|
|
359
|
+
for p, v in zip(keys, q):
|
|
360
|
+
ip[p][kk, i, j] = float(v)
|
|
361
|
+
else:
|
|
362
|
+
fev[kk - 1, i, j] = float(pt)
|
|
363
|
+
for p, v in zip(keys, q):
|
|
364
|
+
fp[p][kk - 1, i, j] = float(v)
|
|
365
|
+
return nok, nfail, sect, IrfBands(K, nok, nfail, irf, ip, fev, fp)
|
|
366
|
+
|
|
367
|
+
|
|
368
|
+
def provenance(binary, folder, argv, elapsed=None, note=""):
|
|
369
|
+
from .results import Provenance, platform_string, platform_tier
|
|
370
|
+
return Provenance("c", binary.version, binary.path, "c", platform_string(),
|
|
371
|
+
platform_tier(), 1, None, note,
|
|
372
|
+
{"source": binary.source, "folder": folder, "argv": list(argv),
|
|
373
|
+
"elapsed": elapsed})
|
|
374
|
+
|
|
375
|
+
|
|
376
|
+
class Folder:
|
|
377
|
+
"""The job's folder: the caller's (kept, created when missing) or a
|
|
378
|
+
temporary one (removed on exit)."""
|
|
379
|
+
|
|
380
|
+
def __init__(self, folder=None):
|
|
381
|
+
self.given = folder is not None
|
|
382
|
+
self.path = os.path.abspath(folder) if folder else tempfile.mkdtemp(prefix="drvec-c-")
|
|
383
|
+
os.makedirs(self.path, exist_ok=True)
|
|
384
|
+
|
|
385
|
+
def __enter__(self):
|
|
386
|
+
return self.path
|
|
387
|
+
|
|
388
|
+
def __exit__(self, *exc):
|
|
389
|
+
if not self.given:
|
|
390
|
+
shutil.rmtree(self.path, ignore_errors=True)
|
|
391
|
+
return False
|
drvec/cast.py
ADDED
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
"""The VEC cast: the parameter vector -> the stationary VARMA on Ybar.
|
|
2
|
+
|
|
3
|
+
C: `vec_shootx` (drvec.c:4303-4790), default parameterisation (no -warma,
|
|
4
|
+
-alpha, -xlink, -trend, -mawarma). Mauricio (2006), eqs. 10-18:
|
|
5
|
+
|
|
6
|
+
Cbar = [0_{s x r} I_s ; I_r B2'] Cinv = [-B2' I_r ; I_s 0]
|
|
7
|
+
Hbar = [0 0 ; 0 I_r] LamBar = [0_{M x s}, Lambda]
|
|
8
|
+
PhiBar_1 = Cinv Hbar - LamBar + F_1 Cinv
|
|
9
|
+
PhiBar_k = F_k Cinv - F_{k-1} Cinv Hbar (k = 2..p-1)
|
|
10
|
+
PhiBar_p = -F_{p-1} Cinv Hbar
|
|
11
|
+
phi*_k = Cbar PhiBar_k, theta*_k = Cbar Theta_k Cinv, qq = Cbar Sigma Cbar'
|
|
12
|
+
mu = [E[nabla Y2] ; E[W]], w_t = [nabla Y2_t ; Y1_t + B2' Y2_t]
|
|
13
|
+
|
|
14
|
+
Every product is `nla.mm` (the C's loop order); W is built column by column,
|
|
15
|
+
adding B2[i][j] * Y2_i in increasing i, which is the C's order of additions --
|
|
16
|
+
so the cast is bit-identical to the C's (docs/PORT.md §1) and costs ~130 us per
|
|
17
|
+
evaluation with drvarma's compiled `elf`.
|
|
18
|
+
|
|
19
|
+
`hold` is the C's `prof_hold` (the route-(B) profile, gate_profile_seed): x then
|
|
20
|
+
carries only [mean, Lambda, B2], and F, Theta and Sigma are read from the hold
|
|
21
|
+
(internal order, full M x M).
|
|
22
|
+
"""
|
|
23
|
+
from __future__ import annotations
|
|
24
|
+
|
|
25
|
+
import numpy as np
|
|
26
|
+
|
|
27
|
+
from .nla import mm, zeros, chol_ok
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def unpack(spec, x, hold=None):
|
|
31
|
+
"""x -> (mu (.inp/Ybar order), Lambda, F, Theta, Sigma, B2) as lists; the walk
|
|
32
|
+
of vec_shootx [3]."""
|
|
33
|
+
M, r, s, q = spec.M, spec.r, spec.s, spec.q
|
|
34
|
+
nf = spec.nf
|
|
35
|
+
idx = 0
|
|
36
|
+
mu = [0.0] * M
|
|
37
|
+
if spec.case == 2:
|
|
38
|
+
for j in range(r):
|
|
39
|
+
mu[s + j] = x[idx]; idx += 1
|
|
40
|
+
elif spec.case == 3:
|
|
41
|
+
for i in range(s):
|
|
42
|
+
if spec.mu_free(i):
|
|
43
|
+
mu[i] = x[idx]; idx += 1
|
|
44
|
+
for j in range(r):
|
|
45
|
+
mu[s + j] = x[idx]; idx += 1
|
|
46
|
+
dtr = [0.0] * max(r, 1)
|
|
47
|
+
if spec.trend:
|
|
48
|
+
for j in range(r):
|
|
49
|
+
dtr[j] = x[idx]; idx += 1
|
|
50
|
+
Lam = zeros(M, max(r, 1))
|
|
51
|
+
sa = spec.alpha_sa
|
|
52
|
+
if sa: # alpha = A psi (-xsys), drvec.c:4448
|
|
53
|
+
from .params import RowOrder
|
|
54
|
+
l2i = RowOrder(M, r).lam2inp
|
|
55
|
+
A = spec.alpha_A()
|
|
56
|
+
psi = zeros(sa, max(r, 1))
|
|
57
|
+
for i in range(sa):
|
|
58
|
+
for j in range(r):
|
|
59
|
+
psi[i][j] = x[idx]; idx += 1
|
|
60
|
+
for i in range(M):
|
|
61
|
+
Ai = A[l2i(i)]
|
|
62
|
+
for j in range(r):
|
|
63
|
+
acc = 0.0
|
|
64
|
+
for kk in range(sa):
|
|
65
|
+
acc += Ai[kk] * psi[kk][j]
|
|
66
|
+
Lam[i][j] = acc
|
|
67
|
+
else:
|
|
68
|
+
for i in range(M):
|
|
69
|
+
for j in range(r):
|
|
70
|
+
Lam[i][j] = x[idx]; idx += 1
|
|
71
|
+
if hold is not None:
|
|
72
|
+
F = [[row[:] for row in Fk] for Fk in hold["F"][:nf]]
|
|
73
|
+
Th = [[row[:] for row in Tk] for Tk in hold["Th"][:q]]
|
|
74
|
+
S = [row[:] for row in hold["S"]]
|
|
75
|
+
else:
|
|
76
|
+
F = [zeros(M, M) for _ in range(nf)]
|
|
77
|
+
for k in range(nf):
|
|
78
|
+
for i in range(M):
|
|
79
|
+
for j in range(M):
|
|
80
|
+
if spec.f_free(i, j):
|
|
81
|
+
F[k][i][j] = x[idx]; idx += 1
|
|
82
|
+
from .params import theta_unpack
|
|
83
|
+
Th, idx = theta_unpack(x, idx, spec.theta_class(), q, M, r)
|
|
84
|
+
S = zeros(M, M)
|
|
85
|
+
S[0][0] = 1.0
|
|
86
|
+
for i in range(1, M):
|
|
87
|
+
S[i][i] = x[idx]; idx += 1
|
|
88
|
+
if not spec.diag_cov:
|
|
89
|
+
for i in range(1, M):
|
|
90
|
+
for j in range(i):
|
|
91
|
+
S[i][j] = x[idx]; S[j][i] = S[i][j]; idx += 1
|
|
92
|
+
B2 = zeros(s, max(r, 1))
|
|
93
|
+
for j in range(r):
|
|
94
|
+
for i in range(s):
|
|
95
|
+
if spec.b2_held(i) is None:
|
|
96
|
+
B2[i][j] = x[idx]; idx += 1
|
|
97
|
+
else:
|
|
98
|
+
B2[i][j] = float(spec.b2_value(i, j))
|
|
99
|
+
if spec.xlink:
|
|
100
|
+
from .xlink import npar as xl_npar
|
|
101
|
+
xp = list(x[idx:idx + xl_npar(spec.xlink)])
|
|
102
|
+
idx += len(xp)
|
|
103
|
+
else:
|
|
104
|
+
xp = None
|
|
105
|
+
if idx != len(x):
|
|
106
|
+
raise ValueError(f"the vector has {len(x)} entries and the walk consumed {idx}")
|
|
107
|
+
unpack.xp = xp
|
|
108
|
+
return mu, Lam, F, Th, S, B2, dtr
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def shootx(spec, rd, x, hold=None):
|
|
112
|
+
"""-> (mu, phi (p,M,M), theta (q,M,M), qq (M,M), w (n,M), ifault) as numpy,
|
|
113
|
+
0-based, ready for `elf_c`. ifault = 1 when Sigma fails choldcp (4563-4577)."""
|
|
114
|
+
M, r, s, p, q = spec.M, spec.r, spec.s, spec.p, spec.q
|
|
115
|
+
mu, Lam, F, Th, S, B2, dtr = unpack(spec, x, hold)
|
|
116
|
+
xp = unpack.xp
|
|
117
|
+
ifault = 0 if chol_ok(S) else 1
|
|
118
|
+
xT = None
|
|
119
|
+
if spec.xlink:
|
|
120
|
+
from .xlink import compute
|
|
121
|
+
xT = compute(spec.xlink, rd.xl, xp, rd.nobs_raw, M)
|
|
122
|
+
if xT is None:
|
|
123
|
+
ifault = 1 # a delta(B) that is not stable
|
|
124
|
+
|
|
125
|
+
Cb = zeros(M, M); Ci = zeros(M, M); H = zeros(M, M); LB = zeros(M, M)
|
|
126
|
+
for i in range(s):
|
|
127
|
+
Cb[i][r + i] = 1.0
|
|
128
|
+
for j in range(r):
|
|
129
|
+
Cb[s + j][j] = 1.0
|
|
130
|
+
for j in range(r):
|
|
131
|
+
for i in range(s):
|
|
132
|
+
Cb[s + j][r + i] = B2[i][j]
|
|
133
|
+
for i in range(r):
|
|
134
|
+
for j in range(s):
|
|
135
|
+
Ci[i][j] = -B2[j][i]
|
|
136
|
+
for i in range(r):
|
|
137
|
+
Ci[i][s + i] = 1.0
|
|
138
|
+
for i in range(s):
|
|
139
|
+
Ci[r + i][i] = 1.0
|
|
140
|
+
for i in range(r):
|
|
141
|
+
H[s + i][s + i] = 1.0
|
|
142
|
+
for i in range(M):
|
|
143
|
+
for j in range(r):
|
|
144
|
+
LB[i][s + j] = Lam[i][j]
|
|
145
|
+
|
|
146
|
+
PB = [zeros(M, M) for _ in range(p + 1)]
|
|
147
|
+
T1 = mm(Ci, H)
|
|
148
|
+
for i in range(M):
|
|
149
|
+
for j in range(M):
|
|
150
|
+
PB[1][i][j] = T1[i][j] - LB[i][j]
|
|
151
|
+
if p >= 2:
|
|
152
|
+
T2 = mm(F[0], Ci)
|
|
153
|
+
for i in range(M):
|
|
154
|
+
for j in range(M):
|
|
155
|
+
PB[1][i][j] += T2[i][j]
|
|
156
|
+
for k in range(2, p): # PhiBar_k, k = 2..p-1
|
|
157
|
+
T1 = mm(F[k - 1], Ci)
|
|
158
|
+
T3 = mm(mm(F[k - 2], Ci), H)
|
|
159
|
+
for i in range(M):
|
|
160
|
+
for j in range(M):
|
|
161
|
+
PB[k][i][j] = T1[i][j] - T3[i][j]
|
|
162
|
+
if p >= 2:
|
|
163
|
+
T2 = mm(mm(F[p - 2], Ci), H)
|
|
164
|
+
for i in range(M):
|
|
165
|
+
for j in range(M):
|
|
166
|
+
PB[p][i][j] = -T2[i][j]
|
|
167
|
+
|
|
168
|
+
phi = np.array([mm(Cb, PB[k]) for k in range(1, p + 1)], float).reshape(p, M, M)
|
|
169
|
+
theta = (np.array([mm(mm(Cb, Th[k]), Ci) for k in range(q)], float).reshape(q, M, M)
|
|
170
|
+
if q else np.zeros((0, M, M)))
|
|
171
|
+
T1 = mm(Cb, S)
|
|
172
|
+
qq = np.zeros((M, M))
|
|
173
|
+
for i in range(M):
|
|
174
|
+
for j in range(M):
|
|
175
|
+
ss = 0.0
|
|
176
|
+
for k1 in range(M):
|
|
177
|
+
ss += T1[i][k1] * Cb[j][k1]
|
|
178
|
+
qq[i, j] = ss
|
|
179
|
+
|
|
180
|
+
w = rd.dm_np.copy()
|
|
181
|
+
tc = np.arange(1, rd.n + 1, dtype=float) - 0.5 * (rd.n + 1)
|
|
182
|
+
for j in range(r):
|
|
183
|
+
col = w[:, s + j]
|
|
184
|
+
for i in range(s):
|
|
185
|
+
col += B2[i][j] * rd.y2_np[:, i]
|
|
186
|
+
if spec.trend:
|
|
187
|
+
col -= dtr[j] * tc # w - dtr (t - (n+1)/2), drvec.c:4766
|
|
188
|
+
if xT is not None: # the inputs, by subtraction (4768-4775)
|
|
189
|
+
for i in range(s):
|
|
190
|
+
w[:, i] -= xT[1:, i] - xT[:-1, i]
|
|
191
|
+
for j in range(r):
|
|
192
|
+
dw = xT[1:, s + j].copy()
|
|
193
|
+
for i in range(s):
|
|
194
|
+
dw += B2[i][j] * xT[1:, i]
|
|
195
|
+
w[:, s + j] -= dw
|
|
196
|
+
return np.array(mu, float), phi, theta, qq, w, ifault
|