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/loglik.py ADDED
@@ -0,0 +1,45 @@
1
+ """The exact likelihood of the cast, on drvarma's compiled AS 311 (`elf_c`).
2
+
3
+ `elf` is called as the C calls it (drvmlest.c:90, 135, 167): sigma2 = 1, and the
4
+ scale concentrated out of the two factors it returns,
5
+
6
+ logL = -0.5 m n (LOG2PI - log m - log n + 1) - 0.5 n (m log f1 + log f2)
7
+ sigma2 = f1 / (n m)
8
+
9
+ with the C's TRUNCATED constant LOG2PI = 1.837877066 (4.1e-10 below log 2 pi;
10
+ drtran-python uses log(2 pi): do not copy that). `xitol` is +1e-3 in every fit
11
+ (the xi sequence truncated, the default `-m 1`) and -1e-3 in the entry gate.
12
+ """
13
+ from __future__ import annotations
14
+
15
+ import math
16
+
17
+ from drvarma._engine import elf_c
18
+
19
+ from .cast import shootx
20
+
21
+ LOG2PI = 1.837877066
22
+ XITOL = 1.0e-3
23
+
24
+
25
+ def concentrated(m, n, f1, f2):
26
+ return (-0.5 * m * n * (LOG2PI - math.log(m) - math.log(n) + 1.0)
27
+ - 0.5 * n * (m * math.log(f1) + math.log(f2)))
28
+
29
+
30
+ def elf(spec, rd, x, xitol=XITOL, atf=False, hold=None):
31
+ """(f1, f2, a, ifault). ifault 6 = the cast refused the point (Sigma)."""
32
+ mu, phi, theta, qq, w, ifc = shootx(spec, rd, x, hold)
33
+ if ifc:
34
+ return None, None, None, 6
35
+ _lg, f1, f2, a, ifa = elf_c(spec.M, rd.n, spec.p, spec.q, mu, phi, theta, qq, w,
36
+ 1.0, xitol, atf)
37
+ return f1, f2, a, ifa
38
+
39
+
40
+ def loglik(spec, rd, x, xitol=XITOL, hold=None):
41
+ """(logL, sigma2, ifault) at x."""
42
+ f1, f2, _a, ifa = elf(spec, rd, x, xitol, True, hold)
43
+ if ifa:
44
+ return float("-inf"), float("nan"), ifa
45
+ return concentrated(spec.M, rd.n, f1, f2), f1 / (rd.n * spec.M), 0
drvec/nla.py ADDED
@@ -0,0 +1,266 @@
1
+ """The small linear algebra of drvec's C, transcribed operation for operation.
2
+
3
+ Why not numpy: the oracle is reproduced BIT FOR BIT (docs/PORT.md §1), and the
4
+ optimiser's trajectory amplifies a last-bit difference into a different point
5
+ on a flat likelihood (on case 1 the four starts end between -1.50 and 6.48).
6
+ So the regressions are solved by the C's normal equations with its LU
7
+ (`nlatools.c:52 ludcp`, `:91 lusol`), the products follow `matrix_multiply`'s
8
+ i-j-k loops (`nlatools.c:1068`), and the Cholesky is the MODIFIED one
9
+ (`nlatools.c:124 choldcp`, Gill-Murray-Wright), not a strict one.
10
+
11
+ Every matrix here is a list of lists, 0-based; the loops run in the C's order.
12
+ """
13
+ from __future__ import annotations
14
+
15
+ import math
16
+
17
+ MACHEPS = 2.0 ** -52 # cmacheps() (nlatools.c:493) on IEEE doubles
18
+
19
+
20
+ def zeros(n, m):
21
+ return [[0.0] * m for _ in range(n)]
22
+
23
+
24
+ def eye(n):
25
+ a = zeros(n, n)
26
+ for i in range(n):
27
+ a[i][i] = 1.0
28
+ return a
29
+
30
+
31
+ def mm(A, B):
32
+ """matrix_multiply: C = A B, sum over k in increasing order."""
33
+ n, m, p = len(A), len(B), len(B[0]) if B else 0
34
+ C = zeros(n, p)
35
+ for i in range(n):
36
+ Ai = A[i]
37
+ Ci = C[i]
38
+ for j in range(p):
39
+ s = 0.0
40
+ for k in range(m):
41
+ s += Ai[k] * B[k][j]
42
+ Ci[j] = s
43
+ return C
44
+
45
+
46
+ def ludcp(a, n):
47
+ """LU with partial pivoting, in place on `a` (n x n, 0-based). Returns ip
48
+ (0-based pivot rows; ip[n-1] carries the sign, 0 if singular, as the C)."""
49
+ ip = [0] * n
50
+ ip[n - 1] = 1
51
+ for k in range(n):
52
+ if k != n - 1:
53
+ kp1 = k + 1
54
+ m = k
55
+ for i in range(kp1, n):
56
+ if abs(a[i][k]) > abs(a[m][k]):
57
+ m = i
58
+ ip[k] = m
59
+ if m != k:
60
+ ip[n - 1] = -ip[n - 1]
61
+ tmp = a[m][k]
62
+ a[m][k] = a[k][k]
63
+ a[k][k] = tmp
64
+ if tmp != 0.0:
65
+ for i in range(kp1, n):
66
+ a[i][k] /= -tmp
67
+ for j in range(kp1, n):
68
+ tmp = a[m][j]
69
+ a[m][j] = a[k][j]
70
+ a[k][j] = tmp
71
+ if tmp != 0.0:
72
+ for i in range(kp1, n):
73
+ a[i][j] += a[i][k] * tmp
74
+ if a[k][k] == 0.0:
75
+ ip[n - 1] = 0
76
+ return ip
77
+
78
+
79
+ def lusol(a, b, n, ip):
80
+ """Solve with ludcp's factors, in place on b (0-based)."""
81
+ if n != 1:
82
+ for k in range(n - 1):
83
+ m = ip[k]
84
+ tmp = b[m]
85
+ b[m] = b[k]
86
+ b[k] = tmp
87
+ for i in range(k + 1, n):
88
+ b[i] += a[i][k] * tmp
89
+ for k1 in range(1, n):
90
+ k = n - k1 # 0-based index of the C's km1 + 1
91
+ b[k] /= a[k][k]
92
+ tmp = -b[k]
93
+ for i in range(k):
94
+ b[i] += a[i][k] * tmp
95
+ b[0] /= a[0][0]
96
+
97
+
98
+ def choldcp(S):
99
+ """nlatools.c:choldcp. Returns (L, ifault): L the lower factor (upper
100
+ triangle zeroed), ifault 1 when a negative pivot exceeds sqrt(eps)*maxoffl.
101
+ It reads the UPPER triangle for the off-diagonal entries, as the C."""
102
+ n = len(S)
103
+ mat = [row[:] for row in S]
104
+ sqrteps = math.sqrt(MACHEPS)
105
+ maxoffl = 0.0
106
+ for j in range(n):
107
+ v = math.sqrt(abs(mat[j][j]))
108
+ if v > maxoffl:
109
+ maxoffl = v
110
+ if maxoffl * maxoffl <= sqrteps:
111
+ return zeros(n, n), 0
112
+ minl = 0.0
113
+ minl2 = sqrteps * maxoffl
114
+ for j in range(n):
115
+ s1 = mat[j][j]
116
+ for i in range(j):
117
+ s1 -= mat[j][i] * mat[j][i]
118
+ if s1 != abs(s1) and abs(s1) > minl2:
119
+ return None, 1
120
+ mat[j][j] = s1
121
+ minljj = 0.0
122
+ for i in range(j + 1, n):
123
+ s1 = mat[j][i]
124
+ for k in range(j):
125
+ s1 -= mat[i][k] * mat[j][k]
126
+ mat[i][j] = s1
127
+ if abs(s1) > minljj:
128
+ minljj = abs(s1)
129
+ minljj = minljj / maxoffl if (minljj / maxoffl) > minl else minl
130
+ if mat[j][j] > minljj * minljj:
131
+ mat[j][j] = math.sqrt(mat[j][j])
132
+ else:
133
+ if minljj < minl2:
134
+ minljj = minl2
135
+ mat[j][j] = minljj
136
+ for i in range(j + 1, n):
137
+ mat[i][j] /= mat[j][j]
138
+ for j in range(1, n):
139
+ for i in range(j):
140
+ mat[i][j] = 0.0
141
+ return mat, 0
142
+
143
+
144
+ def chol_ok(S):
145
+ """Only choldcp's verdict: True when S passes."""
146
+ return choldcp(S)[1] == 0
147
+
148
+
149
+ def choldcp_det(S):
150
+ """choldcp with its determinant (nlatools.c:124-210): -> (L, d1, d2, ifault),
151
+ det = d1 * 2^d2, d1 kept in [1/16, 1) as the C keeps it."""
152
+ n = len(S)
153
+ mat = [row[:] for row in S]
154
+ d1, d2 = 1.0, 0.0
155
+ sqrteps = math.sqrt(MACHEPS)
156
+ maxoffl = 0.0
157
+ for j in range(n):
158
+ v = math.sqrt(abs(mat[j][j]))
159
+ if v > maxoffl:
160
+ maxoffl = v
161
+ if maxoffl * maxoffl <= sqrteps:
162
+ return zeros(n, n), d1, d2, 0
163
+ minl = 0.0
164
+ minl2 = sqrteps * maxoffl
165
+ for j in range(n):
166
+ s1 = mat[j][j]
167
+ for i in range(j):
168
+ s1 -= mat[j][i] * mat[j][i]
169
+ if s1 != abs(s1) and abs(s1) > minl2:
170
+ return None, d1, d2, 1
171
+ mat[j][j] = s1
172
+ minljj = 0.0
173
+ for i in range(j + 1, n):
174
+ s1 = mat[j][i]
175
+ for k in range(j):
176
+ s1 -= mat[i][k] * mat[j][k]
177
+ mat[i][j] = s1
178
+ if abs(s1) > minljj:
179
+ minljj = abs(s1)
180
+ minljj = minljj / maxoffl if (minljj / maxoffl) > minl else minl
181
+ if mat[j][j] > minljj * minljj:
182
+ mat[j][j] = math.sqrt(mat[j][j])
183
+ else:
184
+ if minljj < minl2:
185
+ minljj = minl2
186
+ mat[j][j] = minljj
187
+ d1 *= mat[j][j] * mat[j][j]
188
+ while abs(d1) >= 1.0:
189
+ d1 *= 0.0625
190
+ d2 += 4.0
191
+ while abs(d1) < 0.0625:
192
+ d1 *= 16.0
193
+ d2 -= 4.0
194
+ for i in range(j + 1, n):
195
+ mat[i][j] /= mat[j][j]
196
+ for j in range(1, n):
197
+ for i in range(j):
198
+ mat[i][j] = 0.0
199
+ return mat, d1, d2, 0
200
+
201
+
202
+ # --------------------------------------------------------------------------- #
203
+ # the C's own chi-square (nlatools.c:772-885): what its diagnostics print #
204
+ # --------------------------------------------------------------------------- #
205
+
206
+ _GCOEF = (76.18009172947146, -86.50532032941677, 24.01409824083091,
207
+ -1.231739572450155, 0.1208650973866179e-2, -0.5395239384953e-5)
208
+
209
+
210
+ def _gamma_func(x):
211
+ ser = 1.000000000190015
212
+ tmp = x + 5.5
213
+ tmp -= (x + 0.5) * math.log(tmp)
214
+ for j in range(6):
215
+ ser += _GCOEF[j] / (x + j + 1)
216
+ return math.exp(-tmp + math.log(2.5066282746310005 * ser / x))
217
+
218
+
219
+ def _gammap(a, x):
220
+ if x < a + 1.0:
221
+ ap = a
222
+ dl = 1.0 / a
223
+ sm = dl
224
+ for _n in range(1, 101):
225
+ ap += 1.0
226
+ dl *= x / ap
227
+ sm += dl
228
+ if abs(dl) < abs(sm) * 1e-10:
229
+ break
230
+ return sm * math.exp(-x + a * math.log(x) - math.log(_gamma_func(a)))
231
+ b = x + 1.0 - a
232
+ c = 1.0 / 1e-30
233
+ d = 1.0 / b
234
+ h = d
235
+ for i in range(1, 101):
236
+ an = -i * (i - a)
237
+ b += 2.0
238
+ d = an * d + b
239
+ if abs(d) < 1e-30:
240
+ d = 1e-30
241
+ c = b + an / c
242
+ if abs(c) < 1e-30:
243
+ c = 1e-30
244
+ d = 1.0 / d
245
+ dl = d * c
246
+ h *= dl
247
+ if abs(dl - 1.0) < 1e-10:
248
+ break
249
+ return 1.0 - math.exp(-x + a * math.log(x) - math.log(_gamma_func(a))) * h
250
+
251
+
252
+ def chisq(x, df):
253
+ """nlatools.c:chisq, the CDF (series / continued fraction below 30 df,
254
+ Wilson-Hilferty with Abramowitz-Stegun above). Not scipy's: the C's
255
+ diagnostics print 1 - chisq(), ~1e-4 off the exact tail at df >= 30."""
256
+ if df <= 0 or x <= 0.0:
257
+ return 0.0
258
+ if df < 30:
259
+ return _gammap(df / 2.0, x / 2.0)
260
+ z = (pow(x / df, 1.0 / 3.0) - (1.0 - 2.0 / (9.0 * df))) / math.sqrt(2.0 / (9.0 * df))
261
+ t = 1.0 / (1.0 + 0.2316419 * abs(z))
262
+ d = 0.3989423 * math.exp(-z * z / 2.0)
263
+ prob = d * t * (0.3193815 + t * (-0.3565638 + t * (1.7814779 + t * (-1.821256 + t * 1.330274))))
264
+ if z > 0:
265
+ prob = 1.0 - prob
266
+ return prob
drvec/parallel.py ADDED
@@ -0,0 +1,86 @@
1
+ """The parallel bootstrap: the samples drawn in sequence, the refits in a pool.
2
+
3
+ Why it is bit-identical to the sequential path. The bootstrap's generator
4
+ (rng.Rng, the C's boot_normal) is consumed ONLY by simulate_h0: a refit draws
5
+ no random number -- fit_rank is init_guess, the ladder or the profile, and
6
+ fit_search, whose only generator is -multistart's local one, and a
7
+ replication is fitted with one start (njitter = 1), as the C's fit_ll is. Nor
8
+ does a refit change anything the next sample is drawn from: the sample comes
9
+ from the OBSERVED fit and the observed data, and each replication is fitted
10
+ on a fresh Data (Data.with_raw), with its own rank data and ladder cache. So
11
+ drawing the B samples first, in the generator's order, and fitting them
12
+ anywhere, in any order, gives the same numbers; the results are put back in
13
+ replication order before anything reads them.
14
+
15
+ The pool is a ProcessPoolExecutor in the platform's default start method (fork
16
+ on Linux before Python 3.14, spawn on macOS and Windows): the base data are
17
+ sent once, to each worker's initializer, and every task carries only its
18
+ sample. Under spawn the caller's script must be importable without running
19
+ the analysis (the usual `if __name__ == "__main__":` guard).
20
+ """
21
+ from __future__ import annotations
22
+
23
+ import os
24
+
25
+ _BASE = None
26
+
27
+
28
+ def default_workers():
29
+ """DRVEC_WORKERS when set (an integer >= 1), else the cores less one
30
+ (at least 1): one core is left to the caller and the system."""
31
+ env = os.environ.get("DRVEC_WORKERS", "").strip()
32
+ if env:
33
+ try:
34
+ v = int(env)
35
+ except ValueError:
36
+ raise ValueError(f"DRVEC_WORKERS must be an integer >= 1, got `{env}'") from None
37
+ if v < 1:
38
+ raise ValueError(f"DRVEC_WORKERS must be an integer >= 1, got `{env}'")
39
+ return v
40
+ n = os.cpu_count() or 1
41
+ return max(1, n - 1)
42
+
43
+
44
+ def resolve_workers(workers, ntasks):
45
+ """workers=None -> default_workers(); never more than the tasks."""
46
+ if workers is None:
47
+ workers = default_workers()
48
+ workers = int(workers)
49
+ if workers < 1:
50
+ raise ValueError(f"workers must be >= 1, got {workers}")
51
+ return max(1, min(workers, ntasks))
52
+
53
+
54
+ def _init(base):
55
+ global _BASE
56
+ _BASE = base
57
+
58
+
59
+ def _call(job):
60
+ fn, args = job
61
+ spec, sim = args[0], args[1]
62
+ return fn(spec, _BASE, sim, *args[2:])
63
+
64
+
65
+ def map_refits(fn, base, tasks, workers):
66
+ """Yield fn(spec, base, sim, *rest) for each task (spec, sim, *rest), or
67
+ None for a task that is None (a sample that could not be drawn), IN THE
68
+ TASKS' ORDER, computed in a pool of `workers` processes. fn must be a
69
+ module-level function (it is pickled by name)."""
70
+ from concurrent.futures import ProcessPoolExecutor
71
+ live = [(fn, t) for t in tasks if t is not None]
72
+ if not live:
73
+ for _t in tasks:
74
+ yield None
75
+ return
76
+ with ProcessPoolExecutor(max_workers=min(workers, len(live)), initializer=_init,
77
+ initargs=(_strip(base),)) as ex:
78
+ it = ex.map(_call, live, chunksize=1)
79
+ for t in tasks:
80
+ yield None if t is None else next(it)
81
+
82
+
83
+ def _strip(data):
84
+ """The base data for the workers: a fresh copy (Data.with_raw keeps what a
85
+ replication needs and drops the caches)."""
86
+ return data.with_raw(data.raw)