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/results.py
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
"""The structured results behind the .out (STUDY-vec §4.2 items 1-6): arrays and
|
|
2
|
+
numbers, each computed by the same routine that prints it, so an accessor and
|
|
3
|
+
the .out cannot disagree. Nothing here changes a number the port computes.
|
|
4
|
+
|
|
5
|
+
Conventions:
|
|
6
|
+
* matrices of the VEC (alpha, beta, Pi, Gamma, Theta, Q, Sigma) are in the
|
|
7
|
+
.inp's order, the order of the files: the first s = M - r series are the
|
|
8
|
+
nabla Y2 block, the last r the Y1 block -- as the .out prints them;
|
|
9
|
+
* alpha is in Johansen's sign (negative is error-correcting), as printed;
|
|
10
|
+
* the residuals and the stationary representation are Ybar's, [nabla Y2 ; W],
|
|
11
|
+
labelled as the .out's per-series diagnosis labels them (the Y2 names, then
|
|
12
|
+
ec1..ecr).
|
|
13
|
+
"""
|
|
14
|
+
from __future__ import annotations
|
|
15
|
+
|
|
16
|
+
from dataclasses import dataclass, field
|
|
17
|
+
|
|
18
|
+
import numpy as np
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
@dataclass
|
|
22
|
+
class Stationary:
|
|
23
|
+
"""The stationary VARMA(p, q) on Ybar = [nabla Y2 ; W] that the exact
|
|
24
|
+
likelihood is evaluated on (the C's vec_shootx): Ybar_t - mu =
|
|
25
|
+
sum phi_k (Ybar_{t-k} - mu) + A_t - sum theta_k A_{t-k}, Var A = sigma2 Q."""
|
|
26
|
+
names: list
|
|
27
|
+
mu: np.ndarray # (M,)
|
|
28
|
+
phi: np.ndarray # (p, M, M)
|
|
29
|
+
theta: np.ndarray # (q, M, M)
|
|
30
|
+
qq: np.ndarray # (M, M), Q up to scale, Ybar's order
|
|
31
|
+
sigma2: float
|
|
32
|
+
w: np.ndarray # (n, M), Ybar itself
|
|
33
|
+
|
|
34
|
+
@property
|
|
35
|
+
def Sigma(self):
|
|
36
|
+
return self.sigma2 * self.qq
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
@dataclass
|
|
40
|
+
class LongRun:
|
|
41
|
+
"""The .out's "Long-Run Gain and Mean Lag, in Levels": gain[i, j] is the
|
|
42
|
+
permanent effect on series i of a one-s.d. orthogonalised shock to j;
|
|
43
|
+
mean_lag[i, j] is nan where the gain is zero (the .out says "undefined")."""
|
|
44
|
+
gain: np.ndarray
|
|
45
|
+
mean_lag: np.ndarray
|
|
46
|
+
shock_sd: np.ndarray
|
|
47
|
+
converged: bool
|
|
48
|
+
k_stop: int
|
|
49
|
+
tail: float
|
|
50
|
+
beta_gain: float # max |beta' gain|, the self-check (zero by construction)
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
@dataclass
|
|
54
|
+
class IrfBands:
|
|
55
|
+
"""The -irfboot bands as arrays, [k, i, j] = response of series i to a shock
|
|
56
|
+
to j (the FEVD's k is the horizon minus 1, as in the .out). pct maps the
|
|
57
|
+
percentile (2.5, 5, 50, 95, 97.5) to an array of the point's shape.
|
|
58
|
+
|
|
59
|
+
From the Python engine they are the replications' own percentiles; from
|
|
60
|
+
the C engine they are read back from its <base>.irfboot, so they carry
|
|
61
|
+
that file's 8 significant digits."""
|
|
62
|
+
K: int
|
|
63
|
+
nok: int
|
|
64
|
+
nfail: int
|
|
65
|
+
irf: np.ndarray
|
|
66
|
+
irf_pct: dict
|
|
67
|
+
fevd: np.ndarray
|
|
68
|
+
fevd_pct: dict
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
@dataclass
|
|
72
|
+
class Diagnostics:
|
|
73
|
+
"""The .out's multivariate residual diagnostics. Q's p-value is nan when
|
|
74
|
+
df < 1; the JB numbers are nan when the residual covariance is singular."""
|
|
75
|
+
hosking_lags: int
|
|
76
|
+
hosking_Q: float
|
|
77
|
+
hosking_df: int
|
|
78
|
+
hosking_p: float
|
|
79
|
+
jb: float
|
|
80
|
+
jb_df: int
|
|
81
|
+
jb_p: float
|
|
82
|
+
jb_skewness: float
|
|
83
|
+
jb_kurtosis: float
|
|
84
|
+
jb_part_df: int
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
@dataclass
|
|
88
|
+
class Forecasts:
|
|
89
|
+
"""<base>.forecast as arrays, (H, M) in each series' own units."""
|
|
90
|
+
dates: list # [(period, sub)] of each step
|
|
91
|
+
names: list
|
|
92
|
+
level: np.ndarray
|
|
93
|
+
low: np.ndarray
|
|
94
|
+
high: np.ndarray
|
|
95
|
+
se: np.ndarray
|
|
96
|
+
conf: float
|
|
97
|
+
z: float
|
|
98
|
+
model_level: np.ndarray # in the model's units (before the back-transform)
|
|
99
|
+
model_se: np.ndarray
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
@dataclass
|
|
103
|
+
class RestrictionLR:
|
|
104
|
+
"""The LR the .out prints for a restriction: kind is "alpha" (-alpha,
|
|
105
|
+
-weakex), "fixb2row", "fixb2row+alpha", "xsys", "gzero", "gzero+alpha" or
|
|
106
|
+
"xlink". For -xlink the
|
|
107
|
+
test is of the inputs, so ll_free is the fit WITH them and ll_restricted
|
|
108
|
+
the VEC without them; ll_bridge is the logL at omega = 0."""
|
|
109
|
+
kind: str
|
|
110
|
+
ll_free: float
|
|
111
|
+
ll_restricted: float
|
|
112
|
+
lr: float
|
|
113
|
+
df: int
|
|
114
|
+
pvalue: float
|
|
115
|
+
ll_bridge: float = None
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
@dataclass
|
|
119
|
+
class Provenance:
|
|
120
|
+
"""Who computed a result. engine "python" or "c"; version is drvec-python's
|
|
121
|
+
or the C binary's; path the package's or the binary's; backend the
|
|
122
|
+
numerical backend (python: drvarma's engine and the eigensolver; c: "c");
|
|
123
|
+
tier "exact" on Linux x86_64 (the oracle's platform), "tolerance" elsewhere.
|
|
124
|
+
workers and seed are the bootstrap's; note says why a choice was made."""
|
|
125
|
+
engine: str
|
|
126
|
+
version: str
|
|
127
|
+
path: str
|
|
128
|
+
backend: str
|
|
129
|
+
platform: str
|
|
130
|
+
tier: str
|
|
131
|
+
workers: int = 1
|
|
132
|
+
seed: int = None
|
|
133
|
+
note: str = ""
|
|
134
|
+
extra: dict = field(default_factory=dict)
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def python_provenance(workers=1, seed=None, note=""):
|
|
138
|
+
import os
|
|
139
|
+
import platform as _pf
|
|
140
|
+
import sys
|
|
141
|
+
from . import __version__
|
|
142
|
+
try:
|
|
143
|
+
from drvarma._engine import engine_backend
|
|
144
|
+
eng = engine_backend()
|
|
145
|
+
except Exception: # noqa: BLE001
|
|
146
|
+
eng = "unknown"
|
|
147
|
+
try:
|
|
148
|
+
from ._gsl import backend as gsl_backend
|
|
149
|
+
eig = gsl_backend()
|
|
150
|
+
except Exception: # noqa: BLE001
|
|
151
|
+
eig = "unknown"
|
|
152
|
+
return Provenance("python", __version__, os.path.dirname(os.path.abspath(__file__)),
|
|
153
|
+
f"drvarma engine {eng}, eigensolver {eig}", platform_string(),
|
|
154
|
+
platform_tier(), workers, seed, note)
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
def platform_string():
|
|
158
|
+
import platform as _pf
|
|
159
|
+
import sys
|
|
160
|
+
return f"{sys.platform} {_pf.machine()}"
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
def platform_tier():
|
|
164
|
+
import platform as _pf
|
|
165
|
+
import sys
|
|
166
|
+
return ("exact" if sys.platform == "linux" and _pf.machine() in ("x86_64", "AMD64")
|
|
167
|
+
else "tolerance")
|
drvec/rng.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"""The bootstrap's generator (drvec.c:2203-2223, `boot_normal`).
|
|
2
|
+
|
|
3
|
+
A 64-bit LCG with the PCG constants and Box-Muller with a cached spare:
|
|
4
|
+
|
|
5
|
+
state <- state * 6364136223846793005 + 1442695040888963407 (mod 2^64)
|
|
6
|
+
u = ((state >> 33) & 0x7FFFFFFF) / 2^31
|
|
7
|
+
u1 drawn again while u1 <= 1e-12; u2 = the next u;
|
|
8
|
+
returns sqrt(-2 log u1) cos(2 pi u2), and caches sqrt(-2 log u1) sin(2 pi u2)
|
|
9
|
+
|
|
10
|
+
seeded with 987654321. In the C the state and the spare are STATIC: they are
|
|
11
|
+
never reset, so they run on across replications and across rank pairs; here one
|
|
12
|
+
`Rng` object is threaded through a whole rank test for the same effect.
|
|
13
|
+
|
|
14
|
+
This is the stream of the oracle (Linux, `unsigned long` of 64 bits). On
|
|
15
|
+
Windows `unsigned long` has 32 bits and the C's own stream is a different one
|
|
16
|
+
(TODO.md: a C defect candidate); the port computes the 64-bit stream everywhere.
|
|
17
|
+
"""
|
|
18
|
+
from __future__ import annotations
|
|
19
|
+
|
|
20
|
+
import math
|
|
21
|
+
|
|
22
|
+
_MASK = (1 << 64) - 1
|
|
23
|
+
_A = 6364136223846793005
|
|
24
|
+
_C = 1442695040888963407
|
|
25
|
+
SEED = 987654321
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class Rng:
|
|
29
|
+
def __init__(self, seed=SEED):
|
|
30
|
+
self.state = int(seed) & _MASK
|
|
31
|
+
self.have = False
|
|
32
|
+
self.spare = 0.0
|
|
33
|
+
|
|
34
|
+
def uniform(self):
|
|
35
|
+
self.state = (self.state * _A + _C) & _MASK
|
|
36
|
+
return float((self.state >> 33) & 0x7FFFFFFF) / 2147483648.0
|
|
37
|
+
|
|
38
|
+
def normal(self):
|
|
39
|
+
if self.have:
|
|
40
|
+
self.have = False
|
|
41
|
+
return self.spare
|
|
42
|
+
while True:
|
|
43
|
+
u1 = self.uniform()
|
|
44
|
+
if not (u1 <= 1.0e-12):
|
|
45
|
+
break
|
|
46
|
+
u2 = self.uniform()
|
|
47
|
+
rad = math.sqrt(-2.0 * math.log(u1))
|
|
48
|
+
th = 2.0 * math.pi * u2
|
|
49
|
+
self.spare = rad * math.sin(th)
|
|
50
|
+
self.have = True
|
|
51
|
+
return rad * math.cos(th)
|
drvec/rungtable.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
"""-rungs: the ladder below the rank, emitted (run_rungs, drvec.c:9047-9178).
|
|
2
|
+
|
|
3
|
+
Rungs 0 (F, Theta and Sigma diagonal), 1 (Sigma free) and 2 (F and Theta
|
|
4
|
+
free), all at r = 0, each from the optimum of the one below and fitted with
|
|
5
|
+
the search: exactly the C's r = 0 ladder (fit.fit_r0_ladder), with rung 0's
|
|
6
|
+
gate, and the ordinary nested chi2 LRs between them. The three rungs are
|
|
7
|
+
always fitted, whatever -diagar/-diagma/-diagcov say; rung 2 keeps the MA
|
|
8
|
+
class only when it is -marow or -matri.
|
|
9
|
+
"""
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
import math
|
|
13
|
+
from dataclasses import dataclass
|
|
14
|
+
|
|
15
|
+
NAMES = ("0 F, Theta, Sigma diagonal", "1 Sigma free", "2 F and Theta free")
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
@dataclass
|
|
19
|
+
class Rungs:
|
|
20
|
+
spec: object # the command line's spec (its r is the header's)
|
|
21
|
+
data: object
|
|
22
|
+
ladder: object # fit.Ladder (rungs, gate)
|
|
23
|
+
npar: list
|
|
24
|
+
nobs: int
|
|
25
|
+
|
|
26
|
+
@property
|
|
27
|
+
def ll(self):
|
|
28
|
+
return [r[3] for r in self.ladder.rungs] + [None] * (3 - len(self.ladder.rungs))
|
|
29
|
+
|
|
30
|
+
def lr(self, k):
|
|
31
|
+
a, b = self.ll[k], self.ll[k + 1]
|
|
32
|
+
return None if a is None or b is None else 2.0 * (b - a)
|
|
33
|
+
|
|
34
|
+
def out_text(self, base):
|
|
35
|
+
from .report import _chisq_q, gate_block, header
|
|
36
|
+
o = header(self.data, self.spec, base + ".out")
|
|
37
|
+
o += "\n=== The ladder: rungs at r = 0 ===\n"
|
|
38
|
+
if self.ladder.gate is not None:
|
|
39
|
+
o += gate_block(self.ladder.gate)
|
|
40
|
+
o += ("\n rung npar logL AIC BIC\n"
|
|
41
|
+
" ---------------------------------------------------------------------\n")
|
|
42
|
+
n = self.nobs
|
|
43
|
+
for k in range(3):
|
|
44
|
+
ll = self.ll[k]
|
|
45
|
+
if ll is None:
|
|
46
|
+
o += " %-28s -- estimation failed\n" % NAMES[k]
|
|
47
|
+
continue
|
|
48
|
+
o += " %-28s %4d %12.4f %11.4f %11.4f\n" % (
|
|
49
|
+
NAMES[k], self.npar[k], ll, (-2.0 * ll + 2.0 * self.npar[k]) / n,
|
|
50
|
+
(-2.0 * ll + self.npar[k] * math.log(n)) / n)
|
|
51
|
+
o += ("\n step LR = 2*[L(k+1) - L(k)] df p-value\n"
|
|
52
|
+
" -----------------------------------------------------\n")
|
|
53
|
+
for k in range(2):
|
|
54
|
+
lr = self.lr(k)
|
|
55
|
+
if lr is None:
|
|
56
|
+
o += " %d -> %d not available\n" % (k, k + 1)
|
|
57
|
+
continue
|
|
58
|
+
df = self.npar[k + 1] - self.npar[k]
|
|
59
|
+
o += " %d -> %d %14.4f %3d %9.4f%s\n" % (
|
|
60
|
+
k, k + 1, lr, df, _chisq_q(lr, df) if (df > 0 and lr > 0.0) else 1.0,
|
|
61
|
+
" *** NEGATIVE: the wider fit is worse, so it did not converge"
|
|
62
|
+
if lr < -1.0e-6 else "")
|
|
63
|
+
o += ("\n These are ordinary nested comparisons: the restricted model is\n"
|
|
64
|
+
" an INTERIOR point of the wider one, so the log-likelihood cannot\n"
|
|
65
|
+
" fall and the statistic is chi2 on the stated degrees of freedom.\n"
|
|
66
|
+
" The next rung -- adding the VEC matrix, r = 1 -- is not ordinary\n"
|
|
67
|
+
" in either respect: the null sits ON the boundary of the alternative\n"
|
|
68
|
+
" and B2 is unidentified under it. It is reported by -lrtest, with\n"
|
|
69
|
+
" a parametric bootstrap available for its distribution.\n")
|
|
70
|
+
return o
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def run_rungs(data, spec):
|
|
74
|
+
"""spec: the command line's (p, q, case, MA class, r for the header)."""
|
|
75
|
+
from .fit import fit_r0_ladder
|
|
76
|
+
ma2 = spec.ma if spec.ma in ("marow", "matri") else "free"
|
|
77
|
+
top = spec.but(r=0, diag_ar=False, diag_cov=False, ma=ma2)
|
|
78
|
+
lad = fit_r0_ladder(top, data)
|
|
79
|
+
s0 = top
|
|
80
|
+
npar = [s0.but(diag_ar=True, ma="diag", diag_cov=True).npar(),
|
|
81
|
+
s0.but(diag_ar=True, ma="diag", diag_cov=False).npar(), s0.npar()]
|
|
82
|
+
return Rungs(spec, data, lad, npar, data.at(0).n)
|
drvec/start.py
ADDED
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
"""Starting values: the cold start and Johansen's canonical seed.
|
|
2
|
+
|
|
3
|
+
C: `prelim_b2` (drvec.c:1337), `init_guess` (3715; the default path 3865-4290)
|
|
4
|
+
and `canonical_b2` (1143-1330). Transcribed loop for loop: the cold start is
|
|
5
|
+
bit-identical to the C's (tests/test_trajectory.py), which matters because the
|
|
6
|
+
case-1 optimum is reached from it and from nowhere else.
|
|
7
|
+
"""
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import numpy as np
|
|
11
|
+
|
|
12
|
+
from ._gsl import gensymmv_desc
|
|
13
|
+
from .nla import zeros, ludcp, lusol
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def prelim_b2(spec, rd):
|
|
17
|
+
"""B2 by static OLS of each Y1 on [1, Y2 levels]; B2 = -slope."""
|
|
18
|
+
M, r, s, n = spec.M, spec.r, spec.s, rd.n
|
|
19
|
+
dm, y2 = rd.dm, rd.y2
|
|
20
|
+
B2 = zeros(s, max(r, 1))
|
|
21
|
+
for j in range(r):
|
|
22
|
+
nc = s + 1
|
|
23
|
+
XX = zeros(nc, nc)
|
|
24
|
+
Xy = [0.0] * nc
|
|
25
|
+
for i in range(nc):
|
|
26
|
+
for ii in range(nc):
|
|
27
|
+
ss = 0.0
|
|
28
|
+
for t in range(n):
|
|
29
|
+
xi = 1.0 if i == 0 else y2[t][i - 1]
|
|
30
|
+
xii = 1.0 if ii == 0 else y2[t][ii - 1]
|
|
31
|
+
ss += xi * xii
|
|
32
|
+
XX[i][ii] = ss
|
|
33
|
+
acc = 0.0
|
|
34
|
+
for t in range(n):
|
|
35
|
+
xi = 1.0 if i == 0 else y2[t][i - 1]
|
|
36
|
+
acc += xi * dm[t][s + j]
|
|
37
|
+
Xy[i] = acc
|
|
38
|
+
ip = ludcp(XX, nc)
|
|
39
|
+
lusol(XX, Xy, nc, ip)
|
|
40
|
+
for i in range(s):
|
|
41
|
+
B2[i][j] = -Xy[i + 1]
|
|
42
|
+
return B2
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def canonical_b2(spec, rd):
|
|
46
|
+
"""Johansen's reduced-rank B2 (canonical_b2), or None when it cannot be
|
|
47
|
+
formed. The generalized symmetric eigenproblem is the C's own GSL
|
|
48
|
+
`gsl_eigen_gensymmv`, from drvarma's compiled engine, and scipy's `eigh`
|
|
49
|
+
when that engine is unavailable (`_gsl.py`). scipy's is a few ulps off: 2 ulps
|
|
50
|
+
in B2 on mink-muskrat, which moves the `johansen` start by up to 9.5e-8
|
|
51
|
+
in logL there, and by 0.47 on Lutkepohl's E1 (tests/test_johansen.py)."""
|
|
52
|
+
M, r, s, p, n = spec.M, spec.r, spec.s, spec.p, rd.n
|
|
53
|
+
nf = spec.nf
|
|
54
|
+
T = n - p
|
|
55
|
+
nd = nf * M
|
|
56
|
+
if r <= 0 or T <= nd + M + 1:
|
|
57
|
+
return None
|
|
58
|
+
dm, y2 = rd.dm, rd.y2
|
|
59
|
+
R0 = zeros(T, M)
|
|
60
|
+
R1 = zeros(T, M)
|
|
61
|
+
for t in range(p, n):
|
|
62
|
+
row = t - p
|
|
63
|
+
for j in range(r):
|
|
64
|
+
R0[row][j] = dm[t][s + j] - dm[t - 1][s + j]
|
|
65
|
+
R1[row][j] = dm[t - 1][s + j]
|
|
66
|
+
for i in range(s):
|
|
67
|
+
R0[row][r + i] = dm[t][i]
|
|
68
|
+
R1[row][r + i] = y2[t - 1][i]
|
|
69
|
+
nc = nd + 1
|
|
70
|
+
D = zeros(T, nc)
|
|
71
|
+
for t in range(p, n):
|
|
72
|
+
row = t - p
|
|
73
|
+
c = 0
|
|
74
|
+
D[row][c] = 1.0; c += 1
|
|
75
|
+
for k in range(1, nf + 1):
|
|
76
|
+
for j in range(r):
|
|
77
|
+
D[row][c] = dm[t - k][s + j] - dm[t - k - 1][s + j]; c += 1
|
|
78
|
+
for i in range(s):
|
|
79
|
+
D[row][c] = dm[t - k][i]; c += 1
|
|
80
|
+
XtX = zeros(nc, nc)
|
|
81
|
+
for c in range(nc):
|
|
82
|
+
for c2 in range(nc):
|
|
83
|
+
ss = 0.0
|
|
84
|
+
for t in range(T):
|
|
85
|
+
ss += D[t][c] * D[t][c2]
|
|
86
|
+
XtX[c][c2] = ss
|
|
87
|
+
ip = ludcp(XtX, nc)
|
|
88
|
+
for e in range(M):
|
|
89
|
+
for RR in (R0, R1):
|
|
90
|
+
Xty = [0.0] * nc
|
|
91
|
+
for c in range(nc):
|
|
92
|
+
acc = 0.0
|
|
93
|
+
for t in range(T):
|
|
94
|
+
acc += D[t][c] * RR[t][e]
|
|
95
|
+
Xty[c] = acc
|
|
96
|
+
lusol(XtX, Xty, nc, ip)
|
|
97
|
+
for t in range(T):
|
|
98
|
+
fit = 0.0
|
|
99
|
+
for c in range(nc):
|
|
100
|
+
fit += Xty[c] * D[t][c]
|
|
101
|
+
RR[t][e] -= fit
|
|
102
|
+
S00 = zeros(M, M); S01 = zeros(M, M); S11 = zeros(M, M)
|
|
103
|
+
for i in range(M):
|
|
104
|
+
for j in range(M):
|
|
105
|
+
a0 = a1 = a2 = 0.0
|
|
106
|
+
for t in range(T):
|
|
107
|
+
a0 += R0[t][i] * R0[t][j]
|
|
108
|
+
a1 += R0[t][i] * R1[t][j]
|
|
109
|
+
a2 += R1[t][i] * R1[t][j]
|
|
110
|
+
S00[i][j] = a0 / T; S01[i][j] = a1 / T; S11[i][j] = a2 / T
|
|
111
|
+
cp = [row[:] for row in S00]
|
|
112
|
+
ip2 = ludcp(cp, M)
|
|
113
|
+
S00i = zeros(M, M)
|
|
114
|
+
for j in range(M):
|
|
115
|
+
col = [1.0 if i == j else 0.0 for i in range(M)]
|
|
116
|
+
lusol(cp, col, M, ip2)
|
|
117
|
+
for i in range(M):
|
|
118
|
+
S00i[i][j] = col[i]
|
|
119
|
+
A = zeros(M, M)
|
|
120
|
+
for i in range(M):
|
|
121
|
+
for j in range(M):
|
|
122
|
+
ss = 0.0
|
|
123
|
+
for k in range(M):
|
|
124
|
+
for t in range(M):
|
|
125
|
+
ss += S01[k][i] * S00i[k][t] * S01[t][j]
|
|
126
|
+
A[i][j] = ss
|
|
127
|
+
As = np.array([[0.5 * (A[i][j] + A[j][i]) for j in range(M)] for i in range(M)])
|
|
128
|
+
Bs = np.array([[0.5 * (S11[i][j] + S11[j][i]) for j in range(M)] for i in range(M)])
|
|
129
|
+
V = gensymmv_desc(As, Bs) # GSL_EIGEN_SORT_VAL_DESC
|
|
130
|
+
if V is None:
|
|
131
|
+
return None
|
|
132
|
+
bet = [[float(V[i, j]) for j in range(r)] for i in range(M)]
|
|
133
|
+
Bt = [[bet[j][i] for j in range(r)] for i in range(r)] # Btop'
|
|
134
|
+
ip3 = ludcp(Bt, r)
|
|
135
|
+
if any(abs(Bt[i][i]) < 1.0e-12 for i in range(r)):
|
|
136
|
+
return None
|
|
137
|
+
B2 = zeros(s, max(r, 1))
|
|
138
|
+
for i in range(M):
|
|
139
|
+
z = bet[i][:]
|
|
140
|
+
lusol(Bt, z, r, ip3)
|
|
141
|
+
if i >= r:
|
|
142
|
+
for j in range(r):
|
|
143
|
+
B2[i - r][j] = z[j]
|
|
144
|
+
return B2
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
def init_guess(spec, rd, seedjoh=False, consistent=True):
|
|
148
|
+
"""The conditional (Johansen-style) start: B2 by static OLS (or the canonical
|
|
149
|
+
one), Lambda and F by the conditional regression
|
|
150
|
+
nabla Y_t = sum F_k nabla Y_{t-k} - Lambda (W_{t-1} - E[W]) + e_t,
|
|
151
|
+
Sigma from its residuals divided by their [1][1], Theta at zero."""
|
|
152
|
+
M, r, s, p, q, n = spec.M, spec.r, spec.s, spec.p, spec.q, rd.n
|
|
153
|
+
nf = spec.nf
|
|
154
|
+
dm, y2 = rd.dm, rd.y2
|
|
155
|
+
B2 = prelim_b2(spec, rd)
|
|
156
|
+
if seedjoh and r > 0:
|
|
157
|
+
Bc = canonical_b2(spec, rd)
|
|
158
|
+
if Bc is not None:
|
|
159
|
+
B2 = Bc
|
|
160
|
+
if spec.b2_fix is not None and spec.fixb2_given and r > 0 and consistent:
|
|
161
|
+
# -fixb2 v: the rest of the seed from the W that is fitted (BUG-33);
|
|
162
|
+
# consistent=False is the search's `ols-b2' start (drvec.c:5143)
|
|
163
|
+
for i in range(s):
|
|
164
|
+
for j in range(r):
|
|
165
|
+
B2[i][j] = float(spec.b2_value(i, j))
|
|
166
|
+
elif spec.b2_hold and r > 0: # -fixb2row: held rows BEFORE W (BUG-33)
|
|
167
|
+
for i in range(s):
|
|
168
|
+
v = spec.b2_held(i)
|
|
169
|
+
if v is not None:
|
|
170
|
+
for j in range(r):
|
|
171
|
+
B2[i][j] = float(v)
|
|
172
|
+
W = zeros(n, max(r, 1))
|
|
173
|
+
dY = zeros(n, M) # internal order [nabla Y1 ; nabla Y2]
|
|
174
|
+
for t in range(n):
|
|
175
|
+
for j in range(r):
|
|
176
|
+
w = dm[t][s + j]
|
|
177
|
+
for i in range(s):
|
|
178
|
+
w += B2[i][j] * y2[t][i]
|
|
179
|
+
W[t][j] = w
|
|
180
|
+
for j in range(r):
|
|
181
|
+
dY[t][j] = dm[t][s + j] - dm[t - 1][s + j] if t > 0 else 0.0
|
|
182
|
+
for i in range(s):
|
|
183
|
+
dY[t][r + i] = dm[t][i]
|
|
184
|
+
ET = [0.0] * max(r, 1)
|
|
185
|
+
if spec.trend: # -trend: W detrended on centred t
|
|
186
|
+
tc = 0.5 * (n + 1)
|
|
187
|
+
stt = 0.0
|
|
188
|
+
for t in range(1, n + 1):
|
|
189
|
+
stt += (t - tc) * (t - tc)
|
|
190
|
+
for j in range(r):
|
|
191
|
+
swt = 0.0
|
|
192
|
+
for t in range(1, n + 1):
|
|
193
|
+
swt += W[t - 1][j] * (t - tc)
|
|
194
|
+
ET[j] = swt / stt if stt > 0.0 else 0.0
|
|
195
|
+
for t in range(1, n + 1):
|
|
196
|
+
W[t - 1][j] -= ET[j] * (t - tc)
|
|
197
|
+
EW = [0.0] * max(r, 1)
|
|
198
|
+
EdY2 = [0.0] * s
|
|
199
|
+
for j in range(r):
|
|
200
|
+
sm = 0.0
|
|
201
|
+
for t in range(n):
|
|
202
|
+
sm += W[t][j]
|
|
203
|
+
EW[j] = sm / n
|
|
204
|
+
for i in range(s):
|
|
205
|
+
sm = 0.0
|
|
206
|
+
for t in range(n):
|
|
207
|
+
sm += dY[t][r + i]
|
|
208
|
+
EdY2[i] = sm / n
|
|
209
|
+
nreg = r + nf * M
|
|
210
|
+
T = n - p
|
|
211
|
+
if T < 1:
|
|
212
|
+
T = 1
|
|
213
|
+
X = zeros(T, max(nreg, 1))
|
|
214
|
+
Yd = zeros(T, M)
|
|
215
|
+
for t in range(p, n):
|
|
216
|
+
row = t - p
|
|
217
|
+
col = 0
|
|
218
|
+
for j in range(r):
|
|
219
|
+
X[row][col] = W[t - 1][j] - EW[j]; col += 1
|
|
220
|
+
for k in range(1, nf + 1):
|
|
221
|
+
for i in range(M):
|
|
222
|
+
X[row][col] = dY[t - k][i]; col += 1
|
|
223
|
+
for i in range(M):
|
|
224
|
+
Yd[row][i] = dY[t][i]
|
|
225
|
+
Lam = zeros(M, max(r, 1))
|
|
226
|
+
F = [zeros(M, M) for _ in range(nf)]
|
|
227
|
+
if nreg > 0:
|
|
228
|
+
XtX = zeros(nreg, nreg)
|
|
229
|
+
for i in range(nreg):
|
|
230
|
+
for j in range(nreg):
|
|
231
|
+
ss = 0.0
|
|
232
|
+
for t in range(T):
|
|
233
|
+
ss += X[t][i] * X[t][j]
|
|
234
|
+
XtX[i][j] = ss
|
|
235
|
+
ip = ludcp(XtX, nreg)
|
|
236
|
+
for eq in range(M):
|
|
237
|
+
Xty = [0.0] * nreg
|
|
238
|
+
for i in range(nreg):
|
|
239
|
+
acc = 0.0
|
|
240
|
+
for t in range(T):
|
|
241
|
+
acc += X[t][i] * Yd[t][eq]
|
|
242
|
+
Xty[i] = acc
|
|
243
|
+
lusol(XtX, Xty, nreg, ip)
|
|
244
|
+
for j in range(r):
|
|
245
|
+
Lam[eq][j] = -Xty[j]
|
|
246
|
+
for k in range(nf):
|
|
247
|
+
for i in range(M):
|
|
248
|
+
F[k][eq][i] = Xty[r + k * M + i]
|
|
249
|
+
E = zeros(T, M)
|
|
250
|
+
for t in range(T):
|
|
251
|
+
for i in range(M):
|
|
252
|
+
ei = Yd[t][i]
|
|
253
|
+
for c in range(nreg):
|
|
254
|
+
bi = -Lam[i][c] if c < r else F[(c - r) // M][i][(c - r) % M]
|
|
255
|
+
ei -= bi * X[t][c]
|
|
256
|
+
E[t][i] = ei
|
|
257
|
+
Sig = zeros(M, M)
|
|
258
|
+
for i in range(M):
|
|
259
|
+
for j in range(M):
|
|
260
|
+
ss = 0.0
|
|
261
|
+
for t in range(T):
|
|
262
|
+
ss += E[t][i] * E[t][j]
|
|
263
|
+
Sig[i][j] = ss / T if T > 0 else 1.0
|
|
264
|
+
x = []
|
|
265
|
+
if spec.case == 2:
|
|
266
|
+
x += [EW[j] for j in range(r)]
|
|
267
|
+
elif spec.case == 3:
|
|
268
|
+
x += [EdY2[i] for i in range(s) if spec.mu_free(i)] + [EW[j] for j in range(r)]
|
|
269
|
+
if spec.trend:
|
|
270
|
+
x += [ET[j] for j in range(r)]
|
|
271
|
+
sa = spec.alpha_sa
|
|
272
|
+
if sa:
|
|
273
|
+
# psi = (A'A)^-1 A' Lambda_ols: the free seed projected onto the
|
|
274
|
+
# restriction (drvec.c:4176-4205)
|
|
275
|
+
from .params import RowOrder
|
|
276
|
+
l2i = RowOrder(M, r).lam2inp
|
|
277
|
+
A = spec.alpha_A()
|
|
278
|
+
AtA = zeros(sa, sa)
|
|
279
|
+
for a in range(sa):
|
|
280
|
+
for b in range(sa):
|
|
281
|
+
acc = 0.0
|
|
282
|
+
for i in range(M):
|
|
283
|
+
acc += A[i][a] * A[i][b]
|
|
284
|
+
AtA[a][b] = acc
|
|
285
|
+
ip = ludcp(AtA, sa)
|
|
286
|
+
psi = zeros(sa, max(r, 1))
|
|
287
|
+
for j in range(r):
|
|
288
|
+
AtL = [0.0] * sa
|
|
289
|
+
for a in range(sa):
|
|
290
|
+
acc = 0.0
|
|
291
|
+
for i in range(M):
|
|
292
|
+
acc += A[l2i(i)][a] * Lam[i][j]
|
|
293
|
+
AtL[a] = acc
|
|
294
|
+
lusol(AtA, AtL, sa, ip)
|
|
295
|
+
for a in range(sa):
|
|
296
|
+
psi[a][j] = AtL[a]
|
|
297
|
+
x += [psi[a][j] for a in range(sa) for j in range(r)]
|
|
298
|
+
else:
|
|
299
|
+
x += [Lam[i][j] for i in range(M) for j in range(r)]
|
|
300
|
+
for k in range(nf):
|
|
301
|
+
x += [F[k][i][j] for i in range(M) for j in range(M) if spec.f_free(i, j)]
|
|
302
|
+
from .params import theta_len
|
|
303
|
+
x += [0.0] * theta_len(spec.theta_class(), q, M, r)
|
|
304
|
+
s11 = Sig[0][0] if Sig[0][0] > 1.0e-24 else 1.0e-24
|
|
305
|
+
for i in range(M):
|
|
306
|
+
for j in range(M):
|
|
307
|
+
Sig[i][j] /= s11
|
|
308
|
+
Sig[0][0] = 1.0
|
|
309
|
+
x += [Sig[i][i] for i in range(1, M)]
|
|
310
|
+
if not spec.diag_cov:
|
|
311
|
+
x += [Sig[i][j] for i in range(1, M) for j in range(i)]
|
|
312
|
+
x += [B2[i][j] for j in range(r) for i in range(s) if spec.b2_held(i) is None]
|
|
313
|
+
if spec.xlink: # omega = delta = 0 (drtran's seeding)
|
|
314
|
+
from .xlink import npar as xl_npar
|
|
315
|
+
x += [0.0] * xl_npar(spec.xlink)
|
|
316
|
+
assert len(x) == spec.npar()
|
|
317
|
+
return x
|
drvec/stderr.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"""Standard errors from the BFGS factor (drvmlest.c:117-125).
|
|
2
|
+
|
|
3
|
+
`raxopt` leaves in `b` the lower Cholesky factor of the BFGS Hessian of the
|
|
4
|
+
normalised objective; the C's covariance is
|
|
5
|
+
|
|
6
|
+
cov[:, i] = 2 fk (b b')^{-1} e_i / n, dev_i = sqrt(cov[i][i])
|
|
7
|
+
|
|
8
|
+
with fk the final value of the objective of THAT run (the winner's, in the
|
|
9
|
+
search). The finite-difference Hessian (`-fdhess`, exact_hessian_se 1910) is not
|
|
10
|
+
in milestone 1.
|
|
11
|
+
"""
|
|
12
|
+
from __future__ import annotations
|
|
13
|
+
|
|
14
|
+
import math
|
|
15
|
+
|
|
16
|
+
import numpy as np
|
|
17
|
+
from drvarma._qnewt import cholsol
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def bfgs_covariance(b, fk, npar, n):
|
|
21
|
+
"""b is 1-indexed (npar+1, npar+1), as _qnewt returns it. -> (dev, cov),
|
|
22
|
+
0-based numpy."""
|
|
23
|
+
cov = np.zeros((npar, npar))
|
|
24
|
+
dev = np.zeros(npar)
|
|
25
|
+
for i in range(1, npar + 1):
|
|
26
|
+
v = np.zeros(npar + 1)
|
|
27
|
+
v[i] = 1.0
|
|
28
|
+
cholsol(b, npar, v)
|
|
29
|
+
for j in range(1, npar + 1):
|
|
30
|
+
cov[j - 1, i - 1] = (2.0 * fk * v[j]) / n
|
|
31
|
+
c = cov[i - 1, i - 1]
|
|
32
|
+
dev[i - 1] = math.sqrt(c) if c >= 0.0 else float("nan")
|
|
33
|
+
return dev, cov
|