Filtering and forecasting¶
The Bayesian filter, which also handles missing observations (NaN in \(Y\):
prediction without update), its full-state output, and the predictor used in
the paper's simulations.
Filter¶
PMMFilter1D ¶
PMMFilter1D(simul_params: ParamPMM | None, filter_params: ParamPMM, n_samples: int)
Online Bayesian filter for a 1D PMM with zero mean and unit variance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
simul_params
|
ParamPMM | None
|
Model used to simulate the trajectory. Pass |
required |
filter_params
|
ParamPMM
|
Model used by the filter (typically equal to |
required |
n_samples
|
int
|
Length of the trajectory. |
required |
var_filter_generator ¶
var_filter_generator() -> Iterator[tuple[float, np.ndarray]]
Yield (Var[X_n | Y_1^n], S_n) for n = 0, ..., n_samples-1.
Pure deterministic — no observations are consumed, only the
model's A and BBt drive the recursion. Raises
:class:NumericError if any step produces a NaN/Inf.
filter_observations ¶
filter_observations(y_array: Iterable[float]) -> tuple[np.ndarray, np.ndarray, np.ndarray]
Filter an externally-supplied Y trajectory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y_array
|
Iterable[float]
|
Iterable of length :attr: |
required |
Returns:
| Type | Description |
|---|---|
(e_filter, var_filter, s_array)
|
All of length :attr: |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
NumericError
|
If |
Notes
Does not require an internal simulator: a
PMMFilter1D(None, params, n).filter_observations(y) call works
as long as len(y) == n.
filter_state ¶
filter_state(y_array: Iterable[float]) -> FilterState
Filter Y (NaN = missing) and return the full state of Z = (X, Y).
At an observed step this is the usual PMM filter. At a missing step
Y_n is unknown too, so the filter only predicts :
E[Z_n | .] = A E[Z_{n-1} | .] and
Var[Z_n | .] = A Var[Z_{n-1} | .] A^T + B B^T. The predicted
value of a missing Y_n is mean[n, 1].
Raises the same exceptions as :meth:filter_observations.
forecast ¶
forecast(y_array: Iterable[float], horizon: int) -> tuple[np.ndarray, np.ndarray]
Forecast Z_{N+1}, ..., Z_{N+horizon} given the observed Y_1^N.
y_array has length :attr:n_samples (= N) and may contain
NaN (missing values, including at the end). Returns
(mean, cov) of shapes (horizon, 2) and (horizon, 2, 2) ;
mean[k - 1, 1] is the k-step forecast of Y and
cov[k - 1, 1, 1] its variance.
filter_generator ¶
filter_generator() -> Iterator[tuple[float, float, float, float, np.ndarray]]
Yield (x_n, y_n, E[X_n | Y_1^n], Var[X_n | Y_1^n], S_n) per step.
Drives the filter from the internal simulator. Requires
simul_params to have been passed at construction. Raises
:class:NumericError if any step produces a NaN/Inf.
run ¶
run() -> np.ndarray
Run one full pass and return the per-step filtering MSE.
After the warm-up region (n >= _MSE_MONOTONICITY_WARMUP) the
deterministic filter variance is checked for monotonicity and is
compared to the marginal variance 1.0; either violation logs a
WARNING.
FilterState
dataclass
¶
FilterState(pred_mean: ndarray, pred_cov: ndarray, mean: ndarray, cov: ndarray, observed: ndarray, nll: float)
Filtering output for the state Z_n = (X_n, Y_n), given the
observed Y values (NaN = missing).
Attributes:
| Name | Type | Description |
|---|---|---|
pred_mean, pred_cov |
|
|
mean, cov |
|
|
observed |
ndarray
|
Boolean mask, |
nll |
float
|
Negative log-likelihood of the observed values,
|
Predictor¶
PMMPredictor1D ¶
PMMPredictor1D(simul_params: ParamPMM, filter_params: ParamPMM, predict_params: ParamPMM, n_samples: int, start_predict_index: int)
Filter then forecast for a 1D PMM.
var_predict_generator ¶
var_predict_generator(n: int, s_start: ndarray) -> Iterator[np.ndarray]
Yield n successive prediction covariance matrices.
Raises :class:NumericError if any step produces a NaN/Inf.
predict_generator ¶
predict_generator() -> Iterator[tuple[float, float, float, float, float, float, float]]
Yield (x_n, y_n, E[X_n|Y_1^n] (or prediction), var_xx, cov_xy, var_yy, E[Y_n|Y_1^n] (or prediction)).
Up to start_predict_index the last column is y_n itself
(E[Y_n | Y_1^n] = y_n). In the forecast region, with split index
n and Z = (X, Y), E[Z_{n+k} | Y_1^n] = A^k (E[X_n | Y_1^n], y_n):
column 2 is its X row and column 6 its Y row, and columns 3–5 are the
entries of Var[Z_{n+k} | Y_1^n].
run ¶
run(exp_id: int, plotter: PlotManager | None = None) -> tuple[float, np.ndarray, np.ndarray]
Run one filter+forecast pass, return (mse_predict, mse_x, mse_y).
Per-step diagnostics are emitted at DEBUG level — set the
package logger to DEBUG (setup_logging(verbose=2)) to see
them. The legacy verbose parameter was removed in v0.11.0 since
it duplicated the log level.
check_num_pmm_hmm ¶
check_num_pmm_hmm(pmm_params: ParamPMM, hmm_params: ParamPMM, n_samples: int = 5, start_predict_index: int = 2) -> bool
Numerical sanity-check: make sure the model can be filtered+predicted.
Returns True when one short experiment runs to completion. Returns
False and logs a WARNING if a :class:NumericError,
:class:ParamPMMError, or any other Exception interrupts the run.
Used by the grid scan to skip degenerate tuples without aborting the
whole sweep.
Monte-Carlo experiments¶
run_one_experiment ¶
run_one_experiment(pmm_params: ParamPMM, hmm_params: ParamPMM, n_samples: int) -> tuple[np.ndarray, np.ndarray]
Filter the same simulated trajectory with PMM and HMM parameters.
Logs the seed used by the experiment at DEBUG level (so a CLI run
with --verbose 2 records the exact reproducibility data).
run_one_experiment ¶
run_one_experiment(pmm_params: ParamPMM, hmm_params: ParamPMM, n_samples: int, start_predict_index: int, exp_id: int = -1, plotter: PlotManager | None = None) -> tuple[float, np.ndarray, np.ndarray, float, np.ndarray, np.ndarray]
Run filter+forecast for the same trajectory under PMM and HMM assumptions.
Logs the seed and final prediction MSE for each pass at DEBUG level.