Skip to content

Models and simulation

The PMM parameter set, its validity checks, the reference models used by the tests and tutorials, and the trajectory simulator.

Parameters

ParamPMM

ParamPMM(a: float, b: float, c: float, d: float, e: float)

Covariance matrix of a 1D PMM with zero mean and unit variance.

Parameters (a, b, c, d, e) are independent covariances; see the module docstring for the matrix layout. The full set of derived quantities (transition matrix A, noise covariance BBt, B factor, initial law) is computed at construction time. A degenerate or non-PD tuple raises :class:~pmmforecast.utils.exceptions.ParamPMMError.

is_valid

is_valid() -> bool

Always True for a constructed instance.

Kept for backwards compatibility with callers that still ask before using the model. Since v0.4.0 the constructor raises :class:~pmmforecast.utils.exceptions.ParamPMMError on invalid tuples, so a successful ParamPMM(...) already guarantees validity.

power_a_generator

power_a_generator(n: int) -> Iterator[np.ndarray]

Yield A, A^2, ..., A^n.

ParamPMMGenerator

ParamPMMGenerator(filename: str | None = None, param: tuple[float, float, float, float, float] | None = None, param_set_number: int | None = None)

Generator yielding a sequence of ParamPMM instances.

Three sources are supported:

  • param=(a, b, c, d, e) — yields a single model.
  • param_set_number=k — yields one of the hardcoded reference tuples.
  • filename="path.pkl" — yields one model per tuple in pickle.load(filename)['ListeTupleOK'].

.. warning::

The ``filename=`` path deserialises a pickle. Pickle files can
execute arbitrary code on load — **only open files you produced
yourself** or that you obtained from a trusted source. The class
issues a :class:`UserWarning` whenever it falls back to that
path, with the file's location.

check_th_pmm_hmm

check_th_pmm_hmm(a: float, b: float, c: float, d: float, e: float) -> tuple[ParamPMM | None, ParamPMM | None]

Return (param_pmm, param_hmm) if both models are theoretically valid.

The HMM is built from the same (a, b) with the constraint c = a*b**2, d = a*b, e = a*b. Returns (None, None) and logs a WARNING if either model fails to construct (e.g. b == ±1, non-PD covariance). Use this in grid scans where the natural mode is to skip bad tuples; use the constructor directly when an exception is the appropriate response.

check_cov_matrix

check_cov_matrix(matrix: ndarray) -> bool

Return True if matrix is a valid (real, positive-definite) covariance matrix.

Uses :func:numpy.linalg.eigvalsh rather than the generic :func:scipy.linalg.eig: a covariance matrix is symmetric by definition, so eigvalsh is both numerically more accurate (it exploits symmetry) and faster (~2x) and the eigenvalues are guaranteed real, removing the need for a real/complex check.

Reference models

ReferenceModel dataclass

ReferenceModel(name: str, tuple: tuple[float, float, float, float, float], rationale: str)

A named (a, b, c, d, e) tuple with a documented rationale.

Attributes:

Name Type Description
name str

Short identifier, used as the pytest parameter id.

tuple tuple[float, float, float, float, float]

The five-parameter tuple. Must be constructible by :class:ParamPMM without raising.

rationale str

One-line explanation of why this tuple is interesting, typically referencing the paper or the LaTeX companion.

to_param_pmm

to_param_pmm() -> ParamPMM

Materialise the tuple into a :class:ParamPMM instance.

REFERENCE_MODELS module-attribute

REFERENCE_MODELS: Final[dict[str, ReferenceModel]] = {model.name: model for model in (_SHOWCASE, _PAPER_SECTION1, _STRICT_HMM, _MODERATE_PMM, _NEGATIVE_B)}

get_reference_model

get_reference_model(name: str) -> ReferenceModel

Look up a reference model by name.

Raises:

Type Description
KeyError

If name is not one of the registered reference models. The error message lists the valid names.

Simulation

PMMSimulator1D

PMMSimulator1D(simul_params: ParamPMM, n_samples: int, seed: int | None = None)

Generate samples from a 1D PMM with zero mean and unit variance.

Parameters:

Name Type Description Default
simul_params ParamPMM

Five-tuple-built model that drives the simulation.

required
n_samples int

Length of the trajectory.

required
seed int | None

Optional deterministic seed. When None (default) the RNG is seeded lazily on first use — this avoids burning OS entropy on __init__ for the typical pattern PMMSimulator1D(...).set_seed(k).

None
Notes

Lazy seeding (added in v0.9.0) means get_seed() triggers the auto-seed if no seed has been set yet, so the contract that get_seed() always returns an int is preserved.

get_seed

get_seed() -> int

Return the current seed, auto-generating one if necessary.

simulate_generator

simulate_generator() -> Iterator[np.ndarray]

Yield n_samples consecutive 2-vectors (x_n, y_n).

Raises :class:NumericError if the AR-style recursion produces a NaN/Inf — diagnostic for severely ill-conditioned models.