Skip to content

Theoretical MSE

Closed-form filtering and prediction MSE (equations (18)–(24) of the paper), the excess MSE of the mismatched HMM filter, the filter-MSE asymptote and the grid scans over the parameter space.

TheoreticalMSE

TheoreticalMSE(pmm_params: ParamPMM, n_samples: int, start_predict_index: int)

Closed-form MSE recursion for one PMM model.

Computes the covariance table Gamma_{i,n} between filtered/predicted estimators, then the corresponding instantaneous MSE.

Cost: :meth:compute_th_mse_for_pmm and :meth:compute_instant_mse_diff_vs run in O(n_samples) time and memory. The paper-faithful pair :meth:compute_gamma + :meth:compute_instant_mse_diff (equations (18)–(24)) builds a (start_predict_index + 1) x n_samples table and a dense E[Y_i Y_j] Toeplitz matrix on first use (8 n_samples**2 bytes: 3.7 GB at n_samples = 21 600) ; prefer :meth:compute_instant_mse_diff_vs for long series.

compute_gamma_n_n

compute_gamma_n_n(var_filter_gen: Iterator[tuple[float, ndarray]]) -> float

Equation (19) of the paper.

compute_gamma_n_nm1

compute_gamma_n_nm1(gamma_n_n: float, gamma_nm1_nm1: float) -> float

Equation (20) of the paper.

compute_gamma_i_n

compute_gamma_i_n(gamma_n_n: float, gamma_i_nm1: float) -> float

Equation (21) of the paper.

compute_gamma_n_npk

compute_gamma_n_npk(gamma_n_n: float, a_k: ndarray) -> float

Equation (23) of the paper.

compute_gamma_i_npk

compute_gamma_i_npk(gamma_i_n: float, a_k: ndarray) -> float

Equation (24) of the paper.

compute_gamma

compute_gamma() -> np.ndarray

Build the (min_index x n_samples) Gamma table.

compute_instant_mse_diff

compute_instant_mse_diff(diff_gamma: ndarray) -> np.ndarray

MSE difference (HMM - PMM) per instant from the PMM/HMM Gamma diff.

Vectorised since v0.12.0: uses the Toeplitz matrix self._yy (built on first call, O(n²) memory) so the cost is O(n²) per column — a single d.T @ YY @ d quadratic form — instead of the original triple-loop with np.linalg.matrix_power calls inside, which was O(n³ log n). The numerical result is mathematically identical (a re-association of the same sum); see test_theoretical_matches_numerical_filtering.

compute_instant_mse_diff_vs

compute_instant_mse_diff_vs(other_params: ParamPMM) -> np.ndarray

Excess MSE, per instant, of the filter/predictor built on other_params.

The data follow this instance's model. Returns MSE_other - MSE_self for the estimate of X (filtering up to start_predict_index, then forecasting), i.e. the same quantity as self.compute_instant_mse_diff(self.compute_gamma() - other.compute_gamma()) with other = TheoreticalMSE(other_params, ...), but in O(n) time and memory.

Both filters are linear with deterministic gains, so V_n = (X_n, Y_n, e_self_n, e_other_n) follows a linear recursion whose 4 x 4 covariance is propagated step by step. Since the estimate of self is the conditional expectation, the excess MSE is Var[e_other_n - e_self_n] (orthogonality). In the forecast region each estimate is the X row of its own A^k applied to (e_split, y_split).

compute_th_mse_for_pmm

compute_th_mse_for_pmm() -> tuple[np.ndarray, np.ndarray]

Return (th_mse, th_mse_S) — instantaneous theoretical MSE.

  • th_mse (shape (n,)) is the squared error of the X estimate only: Var[X_n | Y_1^n] up to the split (filtering), then the k-step forecast variance of X after it.
  • th_mse_S (shape (n, 2, 2)) is the covariance of Z = (X, Y): Var[Z_n | Y_1^{n-1}] in the filtering region and Var[Z_{n+k} | Y_1^n] in the forecast region. Its (Y, Y) entry th_mse_S[:, 1, 1] is therefore the theoretical MSE of the Y forecast. Before the split it is the one-step-ahead predictive variance of Y_n, not 0 as in the empirical mse_y of :meth:PMMPredictor1D.run (where Y is observed).

FilterLimitQuadratic dataclass

FilterLimitQuadratic(o: float, p: float, q: float, delta: float, l1: float, l2: float)

Closed-form roots of the filter-MSE quadratic O*l^2 + P*l + Q = 0.

See Section 5.1 of the paper. The two real roots l1 and l2 are candidate asymptotes for Var[X_n | Y_1^n] as n -> infty; NaN when the discriminant is negative or both leading coefficients vanish.

Attributes:

Name Type Description
o, p, q

Quadratic coefficients computed from (alpha_1, alpha_3, beta_*).

delta float

Discriminant p**2 - 4 * o * q.

l1, l2

Real roots, or NaN when the equation has no real solution.

run_one_experiment

run_one_experiment(pmm_params: ParamPMM, hmm_params: ParamPMM, n_samples: int, start_predict_index: int, predict_threshold: float, data_dir: str, plotter: PlotManager) -> dict

Compute theoretical MSE for both PMM and HMM, plot, and return a result dict.

Coordinator only — most logic lives in the helpers above so each piece is independently testable.

GridAnalysis

GridAnalysis(results: dict[Any, dict[str, Any]])

Inspect a grid scan of theoretical PMM/HMM results.

Parameters:

Name Type Description Default
results dict[Any, dict[str, Any]]

Mapping exp_id -> result_dict as produced by :func:~pmmforecast.analysis.pmm_theoretical.run_one_experiment. Use :meth:from_lzma to load such a mapping from disk.

required

from_lzma classmethod

from_lzma(root: str | Path, grid_samples: int, n_samples: int, start_predict_index: int) -> GridAnalysis

Load a grid scan from <root>/lzma/listResultsTh_<grid>_<n>_<start>.lzma.

.. warning::

Reads a pickle file. **Only call this on files you produced
yourself or fully trust** — pickle can execute arbitrary
Python on load. The method emits a :class:`UserWarning`
stating the path so the call is auditable.

get_pmm_models

get_pmm_models(keys: Sequence[tuple[int, _T]]) -> list[tuple[float, float, float, float, float]]

Return the (a, b, c, d, e) tuples of the listed cohort entries.

get_parameter

get_parameter(keys: Sequence[tuple[int, _T]], param: str) -> list[Any]

Look up param in each listed result.

Returns list[Any] because the result-dict's value type depends on the key (numpy arrays for matrices, floats for determinants, bool for sigma-equality flags, …); callers cast to the concrete type they expect.

models_with_l1_or_l2_zero

models_with_l1_or_l2_zero(tol: float = 1e-07) -> list[tuple[int, Any]]

Return (exp_id, FilterLimitQuadratic) for models where one of the quadratic roots is approximately zero.

filter_limit_pmm was a 6-tuple before v0.7.0; it is now a :class:~pmmforecast.analysis.pmm_theoretical.FilterLimitQuadratic dataclass and the access pattern is limit.l1, limit.l2. The legacy 6-tuple layout is still recognised for older lzma artefacts.