pmmforecast 0.25.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.
Files changed (46) hide show
  1. pmmforecast/__init__.py +9 -0
  2. pmmforecast/analysis/__init__.py +26 -0
  3. pmmforecast/analysis/grid_analysis.py +155 -0
  4. pmmforecast/analysis/pmm_theoretical.py +560 -0
  5. pmmforecast/classes/__init__.py +91 -0
  6. pmmforecast/classes/estimation.py +8 -0
  7. pmmforecast/classes/grid_analysis.py +8 -0
  8. pmmforecast/classes/hypothesis_test.py +8 -0
  9. pmmforecast/classes/param_pmm.py +8 -0
  10. pmmforecast/classes/pmm_filter.py +8 -0
  11. pmmforecast/classes/pmm_predictor.py +8 -0
  12. pmmforecast/classes/pmm_simulator.py +8 -0
  13. pmmforecast/classes/pmm_theoretical.py +8 -0
  14. pmmforecast/classes/reference_models.py +8 -0
  15. pmmforecast/gui/__init__.py +16 -0
  16. pmmforecast/gui/__main__.py +63 -0
  17. pmmforecast/gui/main_window.py +626 -0
  18. pmmforecast/inference/__init__.py +64 -0
  19. pmmforecast/inference/diagnostics.py +230 -0
  20. pmmforecast/inference/estimation.py +862 -0
  21. pmmforecast/inference/hypothesis_test.py +387 -0
  22. pmmforecast/inference/pmm_filter.py +451 -0
  23. pmmforecast/inference/pmm_predictor.py +228 -0
  24. pmmforecast/models/__init__.py +37 -0
  25. pmmforecast/models/param_pmm.py +462 -0
  26. pmmforecast/models/pmm_simulator.py +115 -0
  27. pmmforecast/models/reference_models.py +124 -0
  28. pmmforecast/run_filter.py +108 -0
  29. pmmforecast/run_grid_analysis.py +136 -0
  30. pmmforecast/run_predictor.py +201 -0
  31. pmmforecast/run_simulator.py +98 -0
  32. pmmforecast/run_theoretical_error.py +195 -0
  33. pmmforecast/utils/__init__.py +51 -0
  34. pmmforecast/utils/cli.py +66 -0
  35. pmmforecast/utils/config.py +204 -0
  36. pmmforecast/utils/exceptions.py +64 -0
  37. pmmforecast/utils/io.py +18 -0
  38. pmmforecast/utils/log.py +102 -0
  39. pmmforecast/utils/parser.py +129 -0
  40. pmmforecast/utils/plot.py +376 -0
  41. pmmforecast/utils/plot_settings.py +20 -0
  42. pmmforecast-0.25.0.dist-info/METADATA +1269 -0
  43. pmmforecast-0.25.0.dist-info/RECORD +46 -0
  44. pmmforecast-0.25.0.dist-info/WHEEL +4 -0
  45. pmmforecast-0.25.0.dist-info/entry_points.txt +7 -0
  46. pmmforecast-0.25.0.dist-info/licenses/LICENSE +661 -0
@@ -0,0 +1,9 @@
1
+ """pmmforecast — Forecasting with Pairwise Gaussian Markov Models.
2
+
3
+ Reference paper:
4
+ Marc Escudier, Ikram Abdelkefi, Clément Fernandes, Wojciech Pieczynski.
5
+ "Forecasting with Pairwise Gaussian Markov Models", IEEE CMCSI 2023.
6
+ DOI 10.1109/MCSI60294.2023.00009.
7
+ """
8
+
9
+ __version__ = "0.25.0"
@@ -0,0 +1,26 @@
1
+ """Closed-form analysis of Pairwise Markov Models.
2
+
3
+ This subpackage groups two complementary modules:
4
+
5
+ * :mod:`~pmmforecast.analysis.pmm_theoretical` — :class:`TheoreticalMSE`, the
6
+ closed-form recursion implementing equations (18)-(24) of the paper,
7
+ plus the :class:`FilterLimitQuadratic` dataclass and the
8
+ ``run_one_experiment`` orchestrator.
9
+ * :mod:`~pmmforecast.analysis.grid_analysis` — :class:`GridAnalysis`, the
10
+ post-hoc cohort filter for the 5-D ``(a, b, c, d, e)`` grid scans
11
+ produced by :mod:`pmmforecast.run_theoretical_error`.
12
+ """
13
+
14
+ from pmmforecast.analysis.grid_analysis import GridAnalysis
15
+ from pmmforecast.analysis.pmm_theoretical import (
16
+ FilterLimitQuadratic,
17
+ TheoreticalMSE,
18
+ run_one_experiment,
19
+ )
20
+
21
+ __all__ = [
22
+ "FilterLimitQuadratic",
23
+ "GridAnalysis",
24
+ "TheoreticalMSE",
25
+ "run_one_experiment",
26
+ ]
@@ -0,0 +1,155 @@
1
+ """Read and analyse grid-scan results saved by the theoretical-error sweep.
2
+
3
+ The natural source is the LZMA-compressed pickle produced by
4
+ :mod:`pmmforecast.run_theoretical_error` (when invoked with ``--grid-samples > 0``)
5
+ — use :meth:`GridAnalysis.from_lzma` to load it.
6
+
7
+ For interactive use (notebooks, ad-hoc scans built from
8
+ :func:`~pmmforecast.analysis.pmm_theoretical.run_one_experiment`) the constructor
9
+ also accepts a plain in-memory ``results`` dict.
10
+
11
+ .. warning::
12
+
13
+ :meth:`GridAnalysis.from_lzma` deserialises a pickle. Pickle files
14
+ can execute arbitrary code on load — **only open files you produced
15
+ yourself** or that you obtained from a trusted source. The method
16
+ issues a :class:`UserWarning` at every call to make that visible.
17
+ """
18
+
19
+ from __future__ import annotations
20
+
21
+ import lzma
22
+ import pickle
23
+ import warnings
24
+ from collections.abc import Sequence
25
+ from pathlib import Path
26
+ from typing import Any, TypeVar
27
+
28
+ from pmmforecast.utils.parser import EPS
29
+
30
+ _T = TypeVar("_T")
31
+
32
+ # Single source of truth for the pickle-load warning text — re-used by
33
+ # ParamPMMGenerator so the security message stays consistent.
34
+ _PICKLE_WARNING = (
35
+ "loading pickle data from {path!s}: pickle can execute arbitrary "
36
+ "code; only open files you produced yourself or trust. Set "
37
+ "PYTHONWARNINGS=ignore::UserWarning to silence."
38
+ )
39
+
40
+
41
+ class GridAnalysis:
42
+ """Inspect a grid scan of theoretical PMM/HMM results.
43
+
44
+ Parameters
45
+ ----------
46
+ results
47
+ Mapping ``exp_id -> result_dict`` as produced by
48
+ :func:`~pmmforecast.analysis.pmm_theoretical.run_one_experiment`. Use
49
+ :meth:`from_lzma` to load such a mapping from disk.
50
+ """
51
+
52
+ def __init__(self, results: dict[Any, dict[str, Any]]) -> None:
53
+ self._results = results
54
+
55
+ @classmethod
56
+ def from_lzma(
57
+ cls,
58
+ root: str | Path,
59
+ grid_samples: int,
60
+ n_samples: int,
61
+ start_predict_index: int,
62
+ ) -> GridAnalysis:
63
+ """Load a grid scan from
64
+ ``<root>/lzma/listResultsTh_<grid>_<n>_<start>.lzma``.
65
+
66
+ .. warning::
67
+
68
+ Reads a pickle file. **Only call this on files you produced
69
+ yourself or fully trust** — pickle can execute arbitrary
70
+ Python on load. The method emits a :class:`UserWarning`
71
+ stating the path so the call is auditable.
72
+ """
73
+ lzma_dir = (Path(root) / "lzma").resolve()
74
+ filename = lzma_dir / f"listResultsTh_{grid_samples}_{n_samples}_{start_predict_index}.lzma"
75
+ warnings.warn(
76
+ _PICKLE_WARNING.format(path=filename),
77
+ UserWarning,
78
+ stacklevel=2,
79
+ )
80
+ with lzma.open(filename, "rb") as handle:
81
+ results: dict[Any, dict[str, Any]] = pickle.load(handle)
82
+ return cls(results)
83
+
84
+ # ------------------------------------------------------------------ basic accessors
85
+
86
+ def __len__(self) -> int:
87
+ return len(self._results)
88
+
89
+ def get_pmm_models(
90
+ self,
91
+ keys: Sequence[tuple[int, _T]],
92
+ ) -> list[tuple[float, float, float, float, float]]:
93
+ """Return the (a, b, c, d, e) tuples of the listed cohort entries."""
94
+ return [self._results[exp_id]["tuple_param_pmm"] for exp_id, _ in keys]
95
+
96
+ def get_parameter(
97
+ self,
98
+ keys: Sequence[tuple[int, _T]],
99
+ param: str,
100
+ ) -> list[Any]:
101
+ """Look up ``param`` in each listed result.
102
+
103
+ Returns ``list[Any]`` because the result-dict's value type depends
104
+ on the key (numpy arrays for matrices, floats for determinants,
105
+ bool for sigma-equality flags, …); callers cast to the concrete
106
+ type they expect.
107
+ """
108
+ return [self._results[exp_id][param] for exp_id, _ in keys]
109
+
110
+ # ------------------------------------------------------------------ filters
111
+
112
+ def models_with_det_bbt_zero(self) -> list[tuple[int, float]]:
113
+ out = [(k, v["det_bbt_pmm"]) for k, v in self._results.items() if v["det_bbt_pmm"] < 10 * EPS]
114
+ return sorted(out, key=lambda x: x[1])
115
+
116
+ def models_with_det_gamma_zero(self) -> list[tuple[int, float]]:
117
+ out = [(k, v["det_gamma_pmm"]) for k, v in self._results.items() if v["det_gamma_pmm"] < 10 * EPS]
118
+ return sorted(out, key=lambda x: x[1])
119
+
120
+ def models_with_det_a_zero(self) -> list[tuple[int, float]]:
121
+ out = [(k, v["det_a_pmm"]) for k, v in self._results.items() if v["det_a_pmm"] < 10 * EPS]
122
+ return sorted(out, key=lambda x: x[1])
123
+
124
+ def models_with_det_lambda_zero(self) -> list[tuple[int, float]]:
125
+ out = [(k, v["det_lambda_pmm"]) for k, v in self._results.items() if v["det_lambda_pmm"] < 10 * EPS]
126
+ return sorted(out, key=lambda x: x[1])
127
+
128
+ def models_with_l1_or_l2_zero(self, tol: float = 1e-7) -> list[tuple[int, Any]]:
129
+ """Return ``(exp_id, FilterLimitQuadratic)`` for models where one of the
130
+ quadratic roots is approximately zero.
131
+
132
+ ``filter_limit_pmm`` was a 6-tuple before v0.7.0; it is now a
133
+ :class:`~pmmforecast.analysis.pmm_theoretical.FilterLimitQuadratic` dataclass
134
+ and the access pattern is ``limit.l1, limit.l2``. The legacy 6-tuple
135
+ layout is still recognised for older lzma artefacts.
136
+ """
137
+ out: list[tuple[int, Any]] = []
138
+ for k, v in self._results.items():
139
+ limit = v["filter_limit_pmm"]
140
+ l_1 = limit.l1 if hasattr(limit, "l1") else limit[4]
141
+ l_2 = limit.l2 if hasattr(limit, "l2") else limit[5]
142
+ if abs(l_1) < tol or abs(l_2) < tol:
143
+ out.append((k, limit))
144
+ return out
145
+
146
+ def models_with_constant_filtering_mse(self) -> list[tuple[int, bool]]:
147
+ return [(k, v["sigma_12345_equal"]) for k, v in self._results.items() if v["sigma_12345_equal"]]
148
+
149
+ def models_with_zero_filtering_mse(self, threshold: float) -> list[tuple[int, float]]:
150
+ out = [(k, v["last_th_filter_pmm"]) for k, v in self._results.items() if v["last_th_filter_pmm"] < threshold]
151
+ return sorted(out, key=lambda x: x[1])
152
+
153
+ def models_with_non_one_prediction_mse(self, threshold: float) -> list[tuple[int, float]]:
154
+ out = [(k, v["last_th_predict_pmm"]) for k, v in self._results.items() if v["last_th_predict_pmm"] < threshold]
155
+ return sorted(out, key=lambda x: x[1])