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 ADDED
@@ -0,0 +1,22 @@
1
+ """drvec: VARMA-VEC cointegration models by exact maximum likelihood.
2
+
3
+ The Python port of the C program drvec (atsw-gui, engines/drvec). Its oracle
4
+ is drvec C 0.13 (atsw-gui ae95446; the tag drvec-v0.13 comes with the
5
+ release): every fit is homologated against that program's output (logL to
6
+ 1e-6, estimates to 1e-4).
7
+ """
8
+ try:
9
+ from importlib.metadata import version as _version
10
+ __version__ = _version("drvec")
11
+ except Exception: # noqa: BLE001
12
+ __version__ = "0.0.0.dev0"
13
+
14
+ from .api import FitResult, Param, fit, rank_test, rungs # noqa: E402,F401
15
+ from .data import Data, read_pre # noqa: E402,F401
16
+ from .fit import LineSearchRoundoff # noqa: E402,F401
17
+ from .params import Spec # noqa: E402,F401
18
+ from .c_engine import CEngineError, CEngineUnavailable # noqa: E402,F401
19
+ from .rank import RankTest # noqa: E402,F401
20
+ from .results import (Diagnostics, Forecasts, IrfBands, LongRun, # noqa: E402,F401
21
+ Provenance, RestrictionLR, Stationary)
22
+ from .levels import LevelsVarma, to_levels_varma # noqa: E402,F401
drvec/_gsl.py ADDED
@@ -0,0 +1,83 @@
1
+ """GSL's eigensolvers, from drvarma's compiled engine: the C's own
2
+ `gsl_eigen_gensymmv` + `gsl_eigen_gensymmv_sort` (canonical_b2, drvec.c:1265-1272),
3
+ so that Johansen's seed is the C's bit for bit, and `gsl_eigen_nonsymm` (eigenqr,
4
+ nlatools.c:270), so that the eigenvalues the reports print come in the C's order.
5
+
6
+ Why (docs/PORT.md, decision D2 revised): scipy's `eigh` is a few ulps off GSL,
7
+ and on the two-series cases that only moved the `johansen` start by 1e-7 in
8
+ logL. On the three-series case (Lutkepohl's E1) it moves that start's optimum by
9
+ 0.47 in logL and one bootstrap LR* by 3.5e-5 -- beyond the 1e-6 tolerance.
10
+ With GSL the whole case is byte-identical (the fit's search table, the 40
11
+ replications of the .boot).
12
+
13
+ Where GSL comes from (decided 2026-10-15, TODO.md): drvarma >= 0.3.0 exports
14
+ both routines from its compiled engine (`drvarma._engine.gensymmv`,
15
+ `eigen_nonsymm`), which links GSL on every platform it ships a binary wheel for.
16
+ Before, this module looked for a system libgsl through ctypes, which Windows
17
+ and macOS without Homebrew do not have; it no longer does.
18
+
19
+ When drvarma's engine is unavailable (its pure-Python fallback, or
20
+ DRVARMA_NO_ENGINE), the seed is scipy's `eigh` and the eigenvalues numpy's, as
21
+ before without libgsl: the same values to rounding, possibly in another order.
22
+ `DRVEC_EIGEN=scipy` forces scipy, `DRVEC_EIGEN=gsl` makes a missing engine an
23
+ error; the default (`auto`) uses GSL whenever drvarma's engine provides it.
24
+ """
25
+ from __future__ import annotations
26
+
27
+ import os
28
+
29
+ import numpy as np
30
+
31
+
32
+ def _engine():
33
+ """drvarma's `_engine` module when it can run GSL's routines, else None."""
34
+ try:
35
+ from drvarma import _engine as E
36
+ return E if E.gsl_available() else None
37
+ except (ImportError, AttributeError): # AttributeError: drvarma < 0.3.0
38
+ return None
39
+
40
+
41
+ def backend():
42
+ """'gsl' or 'scipy': what canonical_b2 will use."""
43
+ mode = os.environ.get("DRVEC_EIGEN", "auto").lower()
44
+ if mode == "scipy":
45
+ return "scipy"
46
+ if _engine() is not None:
47
+ return "gsl"
48
+ if mode == "gsl":
49
+ raise RuntimeError("DRVEC_EIGEN=gsl and drvarma's compiled engine cannot run "
50
+ "GSL's eigensolvers (drvarma >= 0.3.0 with its engine)")
51
+ return "scipy"
52
+
53
+
54
+ def gensymmv_desc(A, B):
55
+ """Eigenvectors of A v = l B v, columns sorted by DECREASING eigenvalue, or
56
+ None when the problem cannot be solved (B not positive definite)."""
57
+ if backend() == "scipy":
58
+ import scipy.linalg as sl
59
+ try:
60
+ _ev, V = sl.eigh(A, B)
61
+ except (np.linalg.LinAlgError, ValueError):
62
+ return None
63
+ return V[:, ::-1]
64
+ try:
65
+ _ev, V = _engine().gensymmv(A, B, sort="val_desc") # GSL_EIGEN_SORT_VAL_DESC
66
+ except np.linalg.LinAlgError:
67
+ return None
68
+ return V
69
+
70
+
71
+ def eigen_nonsymm(A):
72
+ """eigenqr (nlatools.c:270): the eigenvalues of a real general matrix in
73
+ GSL's order (balanced, gsl_eigen_nonsymm), as (wr, wi) lists. Without GSL,
74
+ numpy's eigvals: the same values to rounding, possibly in another order."""
75
+ A = np.array(A, float)
76
+ if backend() == "gsl":
77
+ try:
78
+ wr, wi = _engine().eigen_nonsymm(A, balance=True)
79
+ return [float(x) for x in wr], [float(x) for x in wi]
80
+ except np.linalg.LinAlgError: # QR without convergence: the C aborts
81
+ pass
82
+ ev = np.linalg.eigvals(A)
83
+ return [float(e.real) for e in ev], [float(e.imag) for e in ev]