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/diagnose.py
ADDED
|
@@ -0,0 +1,439 @@
|
|
|
1
|
+
"""The per-series residual diagnosis of the .out: diagnose.c (drvec-v0.12,
|
|
2
|
+
engines/drvec/src/diagnose.c, vendored from drvarma), transcribed line for line
|
|
3
|
+
-- the statistics, the ASCII plot, the histogram, the ACF and PACF with their
|
|
4
|
+
Ljung-Box, and the cross-correlation functions. Everything is printed, so the
|
|
5
|
+
arithmetic keeps the C's order (sums in increasing t, pow() for the moments)."""
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
import math
|
|
9
|
+
|
|
10
|
+
GUIONS = "-------------+-------------------------+-------------------------+--------------"
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def round_local(num):
|
|
14
|
+
"""nlatools.c:round_local: half away from zero."""
|
|
15
|
+
t1 = abs(num)
|
|
16
|
+
t2 = math.floor(t1)
|
|
17
|
+
if t1 - t2 >= 0.5:
|
|
18
|
+
t2 = math.ceil(t1)
|
|
19
|
+
i = int(t2)
|
|
20
|
+
return i if num >= 0.0 else -i
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def obs_to_date(beg_per, beg_sub, obs_no, freq):
|
|
24
|
+
"""diagnose.c:ObsToDate -> (per, sub)."""
|
|
25
|
+
if obs_no + beg_sub - 1 <= freq:
|
|
26
|
+
return beg_per, beg_sub + obs_no - 1
|
|
27
|
+
q, rem = divmod(obs_no - (freq - beg_sub + 1), freq)
|
|
28
|
+
if rem > 0:
|
|
29
|
+
return beg_per + q + 1, rem
|
|
30
|
+
return beg_per + q, freq
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def mean(d):
|
|
34
|
+
s = 0.0
|
|
35
|
+
for v in d:
|
|
36
|
+
s += v
|
|
37
|
+
return s / len(d)
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def stdev(d):
|
|
41
|
+
ave = mean(d)
|
|
42
|
+
s = 0.0
|
|
43
|
+
for v in d:
|
|
44
|
+
s += (v - ave) * (v - ave)
|
|
45
|
+
return math.sqrt(s / len(d))
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def skew(d):
|
|
49
|
+
ave, sd = mean(d), stdev(d)
|
|
50
|
+
if sd < 1e-20:
|
|
51
|
+
return 0.0
|
|
52
|
+
s = 0.0
|
|
53
|
+
for v in d:
|
|
54
|
+
s += math.pow((v - ave) / sd, 3.0)
|
|
55
|
+
return s / len(d)
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def kurt(d):
|
|
59
|
+
ave, sd = mean(d), stdev(d)
|
|
60
|
+
if sd < 1e-20:
|
|
61
|
+
return 0.0
|
|
62
|
+
s = 0.0
|
|
63
|
+
for v in d:
|
|
64
|
+
s += math.pow((v - ave) / sd, 4.0)
|
|
65
|
+
return s / len(d) - 3.0
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def maxval(d):
|
|
69
|
+
im, mx = 0, d[0]
|
|
70
|
+
for i in range(1, len(d)):
|
|
71
|
+
if d[i] > mx:
|
|
72
|
+
mx, im = d[i], i
|
|
73
|
+
return im
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def minval(d):
|
|
77
|
+
im, mn = 0, d[0]
|
|
78
|
+
for i in range(1, len(d)):
|
|
79
|
+
if d[i] < mn:
|
|
80
|
+
mn, im = d[i], i
|
|
81
|
+
return im
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def acf(d, lags, mu, var):
|
|
85
|
+
n = len(d)
|
|
86
|
+
out = [0.0] * (lags + 1)
|
|
87
|
+
for j in range(1, lags + 1):
|
|
88
|
+
c = 0.0
|
|
89
|
+
for i in range(n - j):
|
|
90
|
+
c += (d[i] - mu) * (d[i + j] - mu)
|
|
91
|
+
out[j] = c / (n * var)
|
|
92
|
+
return out
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def pacf(corr, lags):
|
|
96
|
+
Mt = [[0.0] * (lags + 1) for _ in range(lags + 1)]
|
|
97
|
+
Mt[1][1] = corr[1]
|
|
98
|
+
for i in range(2, lags + 1):
|
|
99
|
+
s1 = s2 = 0.0
|
|
100
|
+
for j in range(1, i):
|
|
101
|
+
s1 += Mt[i - 1][j] * corr[i - j]
|
|
102
|
+
s2 += Mt[i - 1][j] * corr[j]
|
|
103
|
+
Mt[i][i] = (corr[i] - s1) / (1.0 - s2)
|
|
104
|
+
for j in range(1, i):
|
|
105
|
+
Mt[i][j] = Mt[i - 1][j] - Mt[i][i] * Mt[i - 1][i - j]
|
|
106
|
+
return [0.0] + [Mt[i][i] for i in range(1, lags + 1)]
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def chitest(corr, lags, n):
|
|
110
|
+
c = 0.0
|
|
111
|
+
for i in range(1, lags + 1):
|
|
112
|
+
c += corr[i] * corr[i] / (n - i)
|
|
113
|
+
return c * (n * (n + 2))
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def chitest_c(corr, lags, n):
|
|
117
|
+
c = 0.0
|
|
118
|
+
for i in range(1, lags + 1):
|
|
119
|
+
c += corr[i] * corr[i] / (n - i + 1)
|
|
120
|
+
return c * (n * (n + 2))
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
def plot_cor(corr, lags, isacf, n, freq, npar):
|
|
124
|
+
marcas = (" -1 0 1 L-B Q DF"
|
|
125
|
+
if isacf else " -1 0 1")
|
|
126
|
+
o = "Autocorrelation function (acf " if isacf else "Partial autocorrelation function (pacf "
|
|
127
|
+
o += "bands = %5.3f):\n\n%s\n%s\n" % (2.0 / math.sqrt(n), marcas, GUIONS)
|
|
128
|
+
for i in range(1, lags + 1):
|
|
129
|
+
t = [" "] * 53
|
|
130
|
+
if freq != 1 and i % freq == 0:
|
|
131
|
+
o += "%4d %7.3f +" % (i, corr[i])
|
|
132
|
+
sym = "+"
|
|
133
|
+
t[51] = "+"
|
|
134
|
+
else:
|
|
135
|
+
o += "%4d %7.3f |" % (i, corr[i])
|
|
136
|
+
sym = "*"
|
|
137
|
+
t[51] = "|"
|
|
138
|
+
pos = corr[i] * 25.0
|
|
139
|
+
posi = abs(round_local(pos))
|
|
140
|
+
if pos <= 0.0:
|
|
141
|
+
for j in range(25 - posi, 26):
|
|
142
|
+
t[j] = sym
|
|
143
|
+
else:
|
|
144
|
+
for j in range(25, 26 + posi):
|
|
145
|
+
t[j] = sym
|
|
146
|
+
t[25] = "|"
|
|
147
|
+
posi = round_local(2.0 / math.sqrt(n) * 25.0)
|
|
148
|
+
if t[25 + posi] == " ":
|
|
149
|
+
t[25 + posi] = ":"
|
|
150
|
+
if t[25 - posi] == " ":
|
|
151
|
+
t[25 - posi] = ":"
|
|
152
|
+
o += "".join(t)
|
|
153
|
+
lb = "%6.2f %3d" % (chitest(corr, i, n), i - npar)
|
|
154
|
+
if freq != 1 and i % freq == 0 and isacf and i - npar >= 1:
|
|
155
|
+
o += lb
|
|
156
|
+
if i % freq != 0 and isacf and i == lags and i - npar >= 1:
|
|
157
|
+
o += lb
|
|
158
|
+
if freq == 1 and isacf and i == lags and i - npar >= 1:
|
|
159
|
+
o += lb
|
|
160
|
+
o += "\n"
|
|
161
|
+
return o + "%s\n%s\n\n" % (GUIONS, marcas)
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
def plot_ccf(corr, lags, n):
|
|
165
|
+
marcas = " -1 0 1"
|
|
166
|
+
o = "CCF BANDS 2.0/SQRT(N) = %2.5f:\n" % (2.0 / math.sqrt(n))
|
|
167
|
+
o += "\n%s\n%s\n" % (marcas, GUIONS)
|
|
168
|
+
for i in range(1, 2 * lags + 2):
|
|
169
|
+
t = [" "] * 80
|
|
170
|
+
o += "%4d %7.3f |" % (i - lags - 1, corr[i])
|
|
171
|
+
t[51] = "|"
|
|
172
|
+
pos = corr[i] * 25.0
|
|
173
|
+
posi = int(pos + (0.5 if pos >= 0 else -0.5))
|
|
174
|
+
if pos <= 0.0:
|
|
175
|
+
for j in range(25 - abs(posi), 26):
|
|
176
|
+
t[j] = "*"
|
|
177
|
+
else:
|
|
178
|
+
for j in range(25, 26 + posi):
|
|
179
|
+
t[j] = "*"
|
|
180
|
+
t[25] = "|"
|
|
181
|
+
posi = int(2.0 / math.sqrt(n) * 25.0 + 0.5)
|
|
182
|
+
if t[25 + posi] == " ":
|
|
183
|
+
t[25 + posi] = ":"
|
|
184
|
+
if t[25 - posi] == " ":
|
|
185
|
+
t[25 - posi] = ":"
|
|
186
|
+
o += "".join(t) + "\n"
|
|
187
|
+
return o + "%s\n%s\n" % (GUIONS, marcas)
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
def ccf(d1, d2, lags, m1, m2, sd1, sd2):
|
|
191
|
+
n = len(d1)
|
|
192
|
+
out = [0.0] * (lags + 2)
|
|
193
|
+
for j in range(1, lags + 2):
|
|
194
|
+
c = 0.0
|
|
195
|
+
for i in range(n - j + 1):
|
|
196
|
+
c += (d1[i] - m1) * (d2[i + j - 1] - m2)
|
|
197
|
+
out[j] = c / (n * sd1 * sd2)
|
|
198
|
+
return out
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
def stat_ser(d, freq, by, bt):
|
|
202
|
+
n = len(d)
|
|
203
|
+
ey, et = obs_to_date(by, bt, n, freq)
|
|
204
|
+
mu = mean(d)
|
|
205
|
+
tmp = stdev(d)
|
|
206
|
+
sk, ku = skew(d), kurt(d)
|
|
207
|
+
imax, imin = maxval(d), minval(d)
|
|
208
|
+
o = "Unconditional residuals (seasonal period: %d)\n" % freq
|
|
209
|
+
o += "%d observations: " % n
|
|
210
|
+
o += ("from %d/%d to %d/%d\n" % (bt, by, et, ey)) if freq > 1 else ("from %d to %d\n" % (by, ey))
|
|
211
|
+
o += "\n"
|
|
212
|
+
o += " Mean: %18.6f\n" % mu
|
|
213
|
+
o += "Standard error of mean: %18.6f\n" % (tmp / math.sqrt(n))
|
|
214
|
+
o += " Variance: %18.6f\n" % (tmp * tmp)
|
|
215
|
+
o += " Standard deviation: %18.6f\n" % tmp
|
|
216
|
+
o += " Skewness: %18.6f\n" % sk
|
|
217
|
+
o += " Kurtosis: %18.6f\n" % ku
|
|
218
|
+
My, Mt = obs_to_date(by, bt, imax + 1, freq)
|
|
219
|
+
my, mt = obs_to_date(by, bt, imin + 1, freq)
|
|
220
|
+
if freq > 1:
|
|
221
|
+
o += " Minimum: %18.6f at %2d/%d (observation %3d)\n" % (d[imin], mt, my, imin + 1)
|
|
222
|
+
o += " Maximum: %18.6f at %2d/%d (observation %3d)\n" % (d[imax], Mt, My, imax + 1)
|
|
223
|
+
else:
|
|
224
|
+
o += " Minimum: %18.6f at %d (observation %3d)\n" % (d[imin], my, imin + 1)
|
|
225
|
+
o += " Maximum: %18.6f at %d (observation %3d)\n" % (d[imax], My, imax + 1)
|
|
226
|
+
return o + "\n", mu, tmp * tmp, imax, imin
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
def plot_ser(d, freq, by, bt, mu, var, imax, imin):
|
|
230
|
+
guions = list(GUIONS)
|
|
231
|
+
marcas = list(" 0 ")
|
|
232
|
+
r1, r2, r3, r4 = d[imax], d[imin], mu, math.sqrt(var)
|
|
233
|
+
if not (r4 > 0.0):
|
|
234
|
+
return "(residual series is constant; plot skipped)\n\n"
|
|
235
|
+
absmax = abs((r1 - r3) / r4)
|
|
236
|
+
if abs((r2 - r3) / r4) > absmax:
|
|
237
|
+
absmax = abs((r2 - r3) / r4)
|
|
238
|
+
if absmax <= 2.0:
|
|
239
|
+
absmax = 3.0
|
|
240
|
+
if not (absmax <= 8.0):
|
|
241
|
+
return "Warning: at least one observation above 8 sigmas\n"
|
|
242
|
+
hor = 25.0 / absmax
|
|
243
|
+
b1, b2 = hor, 2.0 * hor
|
|
244
|
+
for i in range(1, 9):
|
|
245
|
+
if absmax >= i:
|
|
246
|
+
c = str(i)[0]
|
|
247
|
+
k = round_local(i * hor)
|
|
248
|
+
guions[39 - k] = "+"
|
|
249
|
+
marcas[39 - k] = c
|
|
250
|
+
marcas[39 - k - 1] = "-"
|
|
251
|
+
guions[39 + k] = "+"
|
|
252
|
+
marcas[39 + k] = c
|
|
253
|
+
marcas[39 + k - 1] = "+"
|
|
254
|
+
G, Mk = "".join(guions), "".join(marcas)
|
|
255
|
+
o = "Standardized time series plot (original values on right-side column):\n\n"
|
|
256
|
+
o += "%s\n%s\n" % (Mk, G)
|
|
257
|
+
for i in range(1, len(d) + 1):
|
|
258
|
+
o += "%4d" % i
|
|
259
|
+
Ap, As = obs_to_date(by, bt, i, freq)
|
|
260
|
+
o += ("%7d " % Ap) if freq == 1 else ("%3d/%4d" % (As, Ap))
|
|
261
|
+
t = [" "] * 55
|
|
262
|
+
if freq != 1 and As == freq:
|
|
263
|
+
t[1] = t[53] = "+"
|
|
264
|
+
else:
|
|
265
|
+
t[1] = t[53] = "|"
|
|
266
|
+
z = (d[i - 1] - r3) / r4
|
|
267
|
+
if abs(z) >= 2.0:
|
|
268
|
+
t[0] = t[54] = ">"
|
|
269
|
+
pos = z * hor
|
|
270
|
+
t[27 + round_local(pos)] = "*"
|
|
271
|
+
if t[27] == " ":
|
|
272
|
+
t[27] = "|"
|
|
273
|
+
for k in (round_local(b1), -round_local(b1), round_local(b2), -round_local(b2)):
|
|
274
|
+
if t[27 + k] == " ":
|
|
275
|
+
t[27 + k] = ":"
|
|
276
|
+
o += "".join(t) + "%13.10f\n" % d[i - 1]
|
|
277
|
+
o += "%s\n%s\n\n" % (G, Mk)
|
|
278
|
+
o += (" +------------------------------------------+\n"
|
|
279
|
+
" | Table of standardized values |\n"
|
|
280
|
+
" | greater than or equal to 2.0 |\n"
|
|
281
|
+
" +------------------------------------------+\n"
|
|
282
|
+
" | |\n"
|
|
283
|
+
" | Observation Date Standardized value |\n"
|
|
284
|
+
" | |\n")
|
|
285
|
+
for i in range(1, len(d) + 1):
|
|
286
|
+
z = (d[i - 1] - r3) / r4
|
|
287
|
+
if abs(z) >= 2.0:
|
|
288
|
+
Ap, As = obs_to_date(by, bt, i, freq)
|
|
289
|
+
o += " |" + "%7d" % i
|
|
290
|
+
o += ("%13d " % Ap) if freq == 1 else ("%9d/%4d" % (As, Ap))
|
|
291
|
+
o += "%13.2f" % z + " |\n"
|
|
292
|
+
o += " +------------------------------------------+\n\n"
|
|
293
|
+
return o
|
|
294
|
+
|
|
295
|
+
|
|
296
|
+
def hist_ser(d, mu, var, imax, imin):
|
|
297
|
+
NumFil, NumCol = 17, 64
|
|
298
|
+
r1, r2 = mu, math.sqrt(var)
|
|
299
|
+
if not (r2 > 0.0):
|
|
300
|
+
return "(residual series is constant; histogram skipped)\n\n"
|
|
301
|
+
xmax = abs((d[imax] - r1) / r2)
|
|
302
|
+
if abs((d[imin] - r1) / r2) > xmax:
|
|
303
|
+
xmax = abs((d[imin] - r1) / r2)
|
|
304
|
+
if not (xmax <= 8.0):
|
|
305
|
+
return "Warning: at least one observation above 8 sigmas\n"
|
|
306
|
+
xmax = 4.0 if xmax <= 4.0 else 8.0
|
|
307
|
+
if xmax == 4.0:
|
|
308
|
+
nphor, no, yes = 4, " ", "...."
|
|
309
|
+
base1 = " +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+"
|
|
310
|
+
base2 = " -4 -3 -2 -1 0 +1 +2 +3 +4"
|
|
311
|
+
else:
|
|
312
|
+
nphor, no, yes = 2, " ", ".."
|
|
313
|
+
base1 = " +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+"
|
|
314
|
+
base2 = " -8 -7 -6 -5 -4 -3 -2 -1 0 +1 +2 +3 +4 +5 +6 +7 +8"
|
|
315
|
+
brk = [0.0, -xmax + 0.5]
|
|
316
|
+
while brk[-1] < xmax:
|
|
317
|
+
brk.append(brk[-1] + 0.5)
|
|
318
|
+
NumCat = len(brk) - 1
|
|
319
|
+
freqs = [0] * (NumCat + 1)
|
|
320
|
+
a1 = a2 = 0
|
|
321
|
+
for v in d:
|
|
322
|
+
num = (v - r1) / r2
|
|
323
|
+
if num <= brk[1]:
|
|
324
|
+
freqs[1] += 1
|
|
325
|
+
else:
|
|
326
|
+
for j in range(2, NumCat + 1):
|
|
327
|
+
if brk[j - 1] < num <= brk[j]:
|
|
328
|
+
freqs[j] += 1
|
|
329
|
+
break
|
|
330
|
+
if abs(num) >= 2.0:
|
|
331
|
+
a2 += 1
|
|
332
|
+
if abs(num) >= 1.0:
|
|
333
|
+
a1 += 1
|
|
334
|
+
fmax = max(freqs[1:NumCat + 1])
|
|
335
|
+
opf = fmax / 16.0
|
|
336
|
+
shist = [""] * NumFil
|
|
337
|
+
aux = [""] * NumFil
|
|
338
|
+
for j in range(2, NumFil + 1):
|
|
339
|
+
row = ""
|
|
340
|
+
for i in range(1, NumCat + 1):
|
|
341
|
+
row += yes if freqs[i] > opf * (NumFil - j) else no
|
|
342
|
+
shist[j - 1] += row + "|"
|
|
343
|
+
chk = [0] * (NumCat + 1)
|
|
344
|
+
for j in range(2, NumFil + 1):
|
|
345
|
+
for i in range(1, NumCat + 1):
|
|
346
|
+
if freqs[i] > opf * (NumFil - j) and chk[i] == 0:
|
|
347
|
+
s1 = "%d" % freqs[i]
|
|
348
|
+
if nphor == 2:
|
|
349
|
+
if len(s1) == 1:
|
|
350
|
+
s1 += " "
|
|
351
|
+
else:
|
|
352
|
+
if len(s1) == 2:
|
|
353
|
+
s1 = " " + s1
|
|
354
|
+
elif len(s1) == 1:
|
|
355
|
+
s1 = " " + s1
|
|
356
|
+
elif len(s1) == 3:
|
|
357
|
+
s1 += " "
|
|
358
|
+
if len(s1) > nphor:
|
|
359
|
+
s1 = "*" * nphor
|
|
360
|
+
aux[j - 2] += s1
|
|
361
|
+
chk[i] = 1
|
|
362
|
+
else:
|
|
363
|
+
aux[j - 2] += no
|
|
364
|
+
# strcpy(shist[0], aux[0]); shist[0][NumCol-1] = '|'; shist[0][NumCol] = 0:
|
|
365
|
+
# a 3-character label in a 4-character cell leaves aux[0] short, and then
|
|
366
|
+
# the '|' lands past the string's terminator and is not printed
|
|
367
|
+
a0 = aux[0]
|
|
368
|
+
if len(a0) >= NumCol - 1:
|
|
369
|
+
shist[0] = a0[:NumCol - 1] + "|"
|
|
370
|
+
else:
|
|
371
|
+
shist[0] = a0
|
|
372
|
+
for j in range(2, NumFil):
|
|
373
|
+
row = list(shist[j - 1])
|
|
374
|
+
for k, ch in enumerate(aux[j - 1]):
|
|
375
|
+
if ch != " ":
|
|
376
|
+
row[k] = ch
|
|
377
|
+
shist[j - 1] = "".join(row)
|
|
378
|
+
o = "Standardized time series histogram:\n\n%s\n%s\n" % (base2, base1)
|
|
379
|
+
for i in range(NumFil):
|
|
380
|
+
o += " |%s\n" % shist[i]
|
|
381
|
+
o += "%s\n%s\n\n" % (base1, base2)
|
|
382
|
+
n = len(d)
|
|
383
|
+
o += "%16d values outside (-1,+1): %5.2f %% (31.74 %% expected)\n" % (a1, (a1 * 100.0) / n)
|
|
384
|
+
o += "%16d values outside (-2,+2): %5.2f %% ( 4.56 %% expected)\n" % (a2, (a2 * 100.0) / n)
|
|
385
|
+
return o + "\n"
|
|
386
|
+
|
|
387
|
+
|
|
388
|
+
def diagnose(a, names, freq, start, offset, npar_ser):
|
|
389
|
+
"""diagnose (diagnose.c:372). a: n x m residuals (lists); names: the m
|
|
390
|
+
labels; start: (year, sub) of the RAW sample; offset: where the residuals
|
|
391
|
+
start in it (drvec passes nobs_raw - nobs)."""
|
|
392
|
+
n, m = len(a), len(a[0])
|
|
393
|
+
o = "\n\n" + "=" * 61 + "\n RESIDUAL DIAGNOSTICS \n" + "=" * 61 + "\n"
|
|
394
|
+
by, bt = obs_to_date(start[0], start[1], offset + 1, freq)
|
|
395
|
+
for i in range(m):
|
|
396
|
+
d = [a[t][i] for t in range(n)]
|
|
397
|
+
o += "\n--- Residual series a[%d] (%s) ---\n" % (i + 1, names[i])
|
|
398
|
+
t, mu, var, imax, imin = stat_ser(d, freq, by, bt)
|
|
399
|
+
o += t
|
|
400
|
+
o += plot_ser(d, freq, by, bt, mu, var, imax, imin)
|
|
401
|
+
o += hist_ser(d, mu, var, imax, imin)
|
|
402
|
+
lags = (n - freq // 2) if n < 3 * (freq + 1) else 3 * (freq + 2)
|
|
403
|
+
if lags > n - 2:
|
|
404
|
+
lags = n - 2
|
|
405
|
+
corr = acf(d, lags, mu, var)
|
|
406
|
+
o += plot_cor(corr, lags, True, n, freq, npar_ser[i])
|
|
407
|
+
corr = pacf(corr, lags)
|
|
408
|
+
o += plot_cor(corr, lags, False, n, freq, npar_ser[i])
|
|
409
|
+
if m > 1:
|
|
410
|
+
o += "\n--- Cross-correlation functions between residuals ---\n"
|
|
411
|
+
lags = n if n < 6 else 9
|
|
412
|
+
if lags > n - 2:
|
|
413
|
+
lags = n - 2
|
|
414
|
+
for i in range(m - 1):
|
|
415
|
+
r1 = [a[t][i] for t in range(n)]
|
|
416
|
+
m1 = mean(r1)
|
|
417
|
+
sd1 = stdev(r1)
|
|
418
|
+
for j in range(i + 1, m):
|
|
419
|
+
r2 = [a[t][j] for t in range(n)]
|
|
420
|
+
m2 = mean(r2)
|
|
421
|
+
sd2 = stdev(r2)
|
|
422
|
+
o += "\nCROSS CORRELATION a[%d] (%s) - a[%d] (%s)\n" % (j + 1, names[j], i + 1, names[i])
|
|
423
|
+
o += " %s --> %s IF k > 0\n" % (names[j], names[i])
|
|
424
|
+
o += " %s --> %s IF k < 0\n" % (names[i], names[j])
|
|
425
|
+
c1 = ccf(r1, r2, lags, m1, m2, sd1, sd2)
|
|
426
|
+
c2 = ccf(r2, r1, lags, m2, m1, sd2, sd1)
|
|
427
|
+
tot = [0.0] * (2 * lags + 2)
|
|
428
|
+
for h in range(1, lags + 2):
|
|
429
|
+
tot[h] = c1[lags + 2 - h]
|
|
430
|
+
for h in range(2, lags + 2):
|
|
431
|
+
tot[lags + h] = c2[h]
|
|
432
|
+
o += plot_ccf(tot, lags, n)
|
|
433
|
+
q2 = chitest_c(c2, lags + 1, n)
|
|
434
|
+
q1 = chitest_c(c1, lags + 1, n)
|
|
435
|
+
o += "Q(%2d) = %6.3f k >= 0 Q(%2d) = %6.3f k > 0\n" % (
|
|
436
|
+
lags + 1, q2, lags, q2 - (c2[1] * c2[1] * (n + 2)))
|
|
437
|
+
o += "Q(%2d) = %6.3f k <= 0 Q(%2d) = %6.3f k < 0\n" % (
|
|
438
|
+
lags + 1, q1, lags, q1 - (c1[1] * c1[1] * (n + 2)))
|
|
439
|
+
return o + "\n" + "=" * 61 + "\n"
|
drvec/fdhess.py
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
"""-fdhess: standard errors from the finite-difference Hessian AT THE OPTIMUM.
|
|
2
|
+
|
|
3
|
+
C: `exact_hessian_se` (drvec.c:1910-2007) with its objective `fdh_obj` (1877),
|
|
4
|
+
and `fdhess` (qnewtopt.c:404), which is drvarma's `_qnewt.fdhess` line for line.
|
|
5
|
+
|
|
6
|
+
The BFGS covariance (stderr.py) is read off the optimiser's last Hessian
|
|
7
|
+
update and so depends on the trajectory; this one depends only on the point.
|
|
8
|
+
The objective is the concentrated likelihood normalised AT THE OPTIMUM,
|
|
9
|
+
|
|
10
|
+
g(x) = (f1(x) / f1(x*))^m (f2(x) / f2(x*)), g(x*) = 1,
|
|
11
|
+
|
|
12
|
+
the Hessian of g by Dennis and Schnabel's A5.6.2 with eta = macheps, and
|
|
13
|
+
|
|
14
|
+
cov = 2 g(x*) H^{-1} / n (the same formula as est's)
|
|
15
|
+
|
|
16
|
+
A point the cast or AS 311 refuses counts as 1e10 and as a rejection. Any
|
|
17
|
+
rejection means the optimum is ON the boundary of the admissible region (an MA
|
|
18
|
+
root on the unit circle, BUG-49): the BFGS standard errors are kept and the
|
|
19
|
+
.out says why (BUG-34). So does a Hessian that fails choldcp with every
|
|
20
|
+
evaluation admissible (the optimiser did not stop at a maximum).
|
|
21
|
+
"""
|
|
22
|
+
from __future__ import annotations
|
|
23
|
+
|
|
24
|
+
import math
|
|
25
|
+
from dataclasses import dataclass
|
|
26
|
+
|
|
27
|
+
import numpy as np
|
|
28
|
+
from drvarma import _qnewt
|
|
29
|
+
|
|
30
|
+
from .cast import shootx
|
|
31
|
+
from .loglik import XITOL
|
|
32
|
+
from .nla import choldcp
|
|
33
|
+
|
|
34
|
+
REFUSED = 1.0e10
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
@dataclass
|
|
38
|
+
class FdHess:
|
|
39
|
+
ok: bool # the s.e. are the Hessian's
|
|
40
|
+
nrej: int # finite-difference evaluations refused (boundary)
|
|
41
|
+
nneg: int = 0 # non-positive eigenvalues of H (when not ok, nrej == 0)
|
|
42
|
+
ratio: float = 0.0 # -min/max eigenvalue
|
|
43
|
+
dev: np.ndarray = None
|
|
44
|
+
cov: np.ndarray = None
|
|
45
|
+
|
|
46
|
+
@property
|
|
47
|
+
def why(self):
|
|
48
|
+
"""The reason written to the .out when the s.e. could not be used."""
|
|
49
|
+
return ("the optimum is on the boundary (finite-difference steps left the "
|
|
50
|
+
"admissible region)" if self.nrej > 0 else
|
|
51
|
+
"the Hessian at the optimum is not positive definite")
|
|
52
|
+
|
|
53
|
+
def warning(self, npar):
|
|
54
|
+
"""The C's stderr warning (drvec.c:1975-1991)."""
|
|
55
|
+
if self.ok:
|
|
56
|
+
return None
|
|
57
|
+
if self.nrej > 0:
|
|
58
|
+
return ("WARNING: -fdhess: the optimum lies ON the boundary of the admissible\n"
|
|
59
|
+
" region -- %d of the finite-difference evaluations fell\n"
|
|
60
|
+
" outside it -- so the unconstrained Hessian is not defined\n"
|
|
61
|
+
" there and the BFGS standard errors are kept. See the roots\n"
|
|
62
|
+
" of the AR and MA operators reported above: a modulus at one\n"
|
|
63
|
+
" identifies the binding direction.\n" % self.nrej)
|
|
64
|
+
return ("WARNING: -fdhess: the Hessian at the reported optimum is not positive\n"
|
|
65
|
+
" definite: %d of %d eigenvalues are non-positive, the most\n"
|
|
66
|
+
" negative being %.3g times the largest positive one. Every\n"
|
|
67
|
+
" evaluation was admissible, so the optimiser did not stop at\n"
|
|
68
|
+
" a maximum. The BFGS standard errors are kept.\n"
|
|
69
|
+
% (self.nneg, npar, self.ratio))
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def _f12(spec, rd, x, xitol):
|
|
73
|
+
mu, phi, theta, qq, w, ifc = shootx(spec, rd, x)
|
|
74
|
+
if ifc > 0:
|
|
75
|
+
return None
|
|
76
|
+
from drvarma._engine import elf_c
|
|
77
|
+
_lg, f1, f2, _a, ifa = elf_c(spec.M, rd.n, spec.p, spec.q, mu, phi, theta, qq, w,
|
|
78
|
+
1.0, xitol, False)
|
|
79
|
+
if ifa > 0:
|
|
80
|
+
return None
|
|
81
|
+
return f1, f2
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def exact_hessian_se(spec, rd, x, xitol=XITOL):
|
|
85
|
+
"""-> FdHess. `rd` is the estimation sample (n = rd.n is the C's nobs)."""
|
|
86
|
+
npar = len(x)
|
|
87
|
+
m = spec.M
|
|
88
|
+
base = _f12(spec, rd, list(x), xitol)
|
|
89
|
+
if base is None:
|
|
90
|
+
return FdHess(ok=False, nrej=0)
|
|
91
|
+
n1, n2 = base
|
|
92
|
+
nrej = [0]
|
|
93
|
+
|
|
94
|
+
def obj(xk1):
|
|
95
|
+
v = _f12(spec, rd, xk1[1:].tolist(), xitol)
|
|
96
|
+
if v is None:
|
|
97
|
+
nrej[0] += 1
|
|
98
|
+
return REFUSED
|
|
99
|
+
return pow(v[0] / n1, m) * (v[1] / n2)
|
|
100
|
+
|
|
101
|
+
xk = np.zeros(npar + 1)
|
|
102
|
+
xk[1:] = x
|
|
103
|
+
f = obj(xk) # = 1 by construction
|
|
104
|
+
nrej[0] = 0
|
|
105
|
+
H = np.zeros((npar + 1, npar + 1))
|
|
106
|
+
_qnewt.fdhess(obj, npar, xk, f, _qnewt.MACHEPS, H)
|
|
107
|
+
Hs = H[1:, 1:].copy()
|
|
108
|
+
ev = np.linalg.eigvals(Hs).real # eigenqr, for the warning only
|
|
109
|
+
mx = max(0.0, float(ev.max())) if npar else 0.0
|
|
110
|
+
mn = min(0.0, float(ev.min())) if npar else 0.0
|
|
111
|
+
nneg = int((ev <= 0.0).sum())
|
|
112
|
+
ratio = -mn / mx if mx > 0.0 else 0.0
|
|
113
|
+
if nrej[0] > 0:
|
|
114
|
+
return FdHess(ok=False, nrej=nrej[0], nneg=nneg, ratio=ratio)
|
|
115
|
+
L, ifc = choldcp([[float(v) for v in row] for row in Hs])
|
|
116
|
+
if ifc > 0:
|
|
117
|
+
return FdHess(ok=False, nrej=0, nneg=nneg, ratio=ratio)
|
|
118
|
+
Lk = np.zeros((npar + 1, npar + 1))
|
|
119
|
+
Lk[1:, 1:] = np.array(L)
|
|
120
|
+
cov = np.zeros((npar, npar))
|
|
121
|
+
dev = np.zeros(npar)
|
|
122
|
+
for i in range(1, npar + 1):
|
|
123
|
+
e = np.zeros(npar + 1)
|
|
124
|
+
e[i] = 1.0
|
|
125
|
+
_qnewt.cholsol(Lk, npar, e)
|
|
126
|
+
for j in range(1, npar + 1):
|
|
127
|
+
cov[j - 1, i - 1] = (2.0 * f * e[j]) / rd.n
|
|
128
|
+
c = cov[i - 1, i - 1]
|
|
129
|
+
dev[i - 1] = math.sqrt(c if c > 0.0 else 0.0)
|
|
130
|
+
return FdHess(ok=True, nrej=0, nneg=nneg, ratio=ratio, dev=dev, cov=cov)
|