Skip to content

Tests and diagnostics

The likelihood-ratio test of the HMM against the PMM (both need \(X\) and \(Y\)), and goodness-of-fit diagnostics on the whitened innovations.

HMM versus PMM

likelihood_ratio_test_hmm_vs_pmm

likelihood_ratio_test_hmm_vs_pmm(x: ArrayLike, y: ArrayLike, *, pmm_x0: tuple[float, float, float, float, float] | None = None, hmm_x0: tuple[float, float] | None = None, method: str = 'Nelder-Mead', max_iter: int = 2000) -> LRTResult

Likelihood-ratio test of HMM (H0) vs PMM (H1) on observed (X, Y).

Fits both an unrestricted PMM and an HMM-constrained model by maximum likelihood, then computes the asymptotic chi-squared p-value with df=3.

Parameters:

Name Type Description Default
x ArrayLike

Observed state and observation trajectories. Auto-standardised to mean 0 / variance 1 (the PMM is defined as zero-mean unit-variance).

required
y ArrayLike

Observed state and observation trajectories. Auto-standardised to mean 0 / variance 1 (the PMM is defined as zero-mean unit-variance).

required
pmm_x0 tuple[float, float, float, float, float] | None

Optional initial guesses. Default to the moments estimator and its projection onto the HMM manifold.

None
hmm_x0 tuple[float, float, float, float, float] | None

Optional initial guesses. Default to the moments estimator and its projection onto the HMM manifold.

None
method str

Forwarded to scipy.optimize.minimize. Nelder-Mead is the default because it tolerates the +∞ wall at the PMM boundary; L-BFGS-B is faster but assumes finite gradients.

'Nelder-Mead'
max_iter str

Forwarded to scipy.optimize.minimize. Nelder-Mead is the default because it tolerates the +∞ wall at the PMM boundary; L-BFGS-B is faster but assumes finite gradients.

'Nelder-Mead'

Returns:

Type Description
class:`LRTResult`

The statistic, the p-value, both fitted models and their log-likelihoods.

Notes

The asymptotic chi-squared approximation requires the data to be long enough for the MLE to be close to its asymptotic Gaussian distribution. For short series (n < 200) prefer the bootstrap alternative — see :func:bootstrap_lrt_p_value and Tutorial 08.

bootstrap_lrt_p_value

bootstrap_lrt_p_value(x: ArrayLike, y: ArrayLike, *, n_bootstrap: int = 200, seed: int = 0, n_jobs: int = 1, method: str = 'Nelder-Mead', max_iter: int = 2000) -> tuple[float, float, np.ndarray]

Parametric-bootstrap p-value for HMM-vs-PMM, robust on short series.

Algorithm:

  1. Compute the observed LRT statistic T_obs via :func:likelihood_ratio_test_hmm_vs_pmm.
  2. Fit the HMM-constrained model to the data; call it hmm_null.
  3. Simulate n_bootstrap synthetic trajectories from hmm_null; recompute the LRT statistic on each.
  4. The bootstrap p-value is the fraction of simulated statistics at least as extreme as T_obs.

Parameters:

Name Type Description Default
x ArrayLike

Observed (X, Y) trajectory.

required
y ArrayLike

Observed (X, Y) trajectory.

required
n_bootstrap int

Number of synthetic trajectories under the HMM null.

200
seed int

Master RNG seed; each iteration draws an independent sub-seed.

0
n_jobs int

Number of parallel processes (default 1, sequential). Set to -1 to use all available CPUs. Each iteration is embarrassingly parallel — a 4-core box gets a ~3.5× speedup on the default n_bootstrap=200.

1
method str

Forwarded to :func:scipy.optimize.minimize.

'Nelder-Mead'
max_iter str

Forwarded to :func:scipy.optimize.minimize.

'Nelder-Mead'

Returns:

Type Description
(t_obs, p_value, t_bootstrap_array)

The observed statistic, the bootstrap p-value, and the array of bootstrap statistics (with NaN for failed iterations) so callers can plot the null distribution.

LRTResult dataclass

LRTResult(statistic: float, p_value: float, log_likelihood_pmm: float, log_likelihood_hmm: float, pmm_params: ParamPMM, hmm_params: ParamPMM, n_obs: int, df: int = _LRT_DOF)

Result of a Likelihood-Ratio Test of HMM (H0) vs PMM (H1).

Attributes:

Name Type Description
statistic float

-2 * (log L_HMM - log L_PMM). Always ≥ 0 (PMM is a superset of HMM); larger means stronger evidence against H0.

p_value float

Asymptotic p-value under H0 from chi^2(df=3).

log_likelihood_pmm, log_likelihood_hmm

Maximised log-likelihoods under each hypothesis.

pmm_params, hmm_params

MLE parameters under each hypothesis. hmm_params.is_hmm() is True by construction.

n_obs int

Number of joint observations (X_n, Y_n) used to compute the likelihoods. Stored so the :attr:aic_pmm, :attr:bic_pmm, :attr:aic_hmm, :attr:bic_hmm properties can produce information-criterion values without re-passing N.

df int

Degrees of freedom of the chi-squared null distribution. Always 3 in the 1-D PMM-vs-HMM case (5 PMM − 2 HMM free params).

aic_pmm property

aic_pmm: float

AIC of the unrestricted PMM fit (2 * 5 - 2 * log L_PMM).

aic_hmm property

aic_hmm: float

AIC of the HMM-constrained fit (2 * 2 - 2 * log L_HMM).

bic_pmm property

bic_pmm: float

BIC of the unrestricted PMM fit.

bic_hmm property

bic_hmm: float

BIC of the HMM-constrained fit.

preferred_by_aic property

preferred_by_aic: str

Return "PMM" or "HMM" depending on which has the lower AIC.

preferred_by_bic property

preferred_by_bic: str

Return "PMM" or "HMM" depending on which has the lower BIC.

Goodness of fit

pmm_goodness_of_fit

pmm_goodness_of_fit(x: ArrayLike, y: ArrayLike, params: ParamPMM, *, n_lags: int = 10) -> GoodnessOfFitResult

Run the full goodness-of-fit suite for a fitted PMM.

Combines :func:compute_innovations with the Ljung-Box and Jarque-Bera tests on each of the two innovation components. The :meth:GoodnessOfFitResult.is_well_specified shortcut returns True when all four p-values exceed alpha (default 0.05).

Parameters:

Name Type Description Default
x ArrayLike

Observed trajectory.

required
y ArrayLike

Observed trajectory.

required
params ParamPMM

Fitted :class:ParamPMM.

required
n_lags int

Lag count for the Ljung-Box test (default 10).

10

GoodnessOfFitResult dataclass

GoodnessOfFitResult(innovations: ndarray, ljung_box_x_p: float, ljung_box_y_p: float, jarque_bera_x_p: float, jarque_bera_y_p: float, n_lags: int, n_obs: int)

Bundle of goodness-of-fit p-values for a fitted PMM.

Attributes:

Name Type Description
innovations ndarray

Whitened innovation series BBᵀ^{-1/2} (z_n - A z_{n-1}) of shape (n-1, 2). Should be i.i.d. N(0, I_2) under H0.

ljung_box_x_p float

p-value of the Ljung-Box autocorrelation test on the X component of the innovations.

ljung_box_y_p float

idem for Y.

jarque_bera_x_p float

p-value of the Jarque-Bera normality test on X.

jarque_bera_y_p float

idem for Y.

n_lags int

Number of lags used in the Ljung-Box test.

n_obs int

Length of the innovation series (= len(x) - 1).

is_well_specified

is_well_specified(alpha: float = 0.05) -> bool

All four p-values above alpha ⇒ no detectable mis-specification.

compute_innovations

compute_innovations(x: ArrayLike, y: ArrayLike, params: ParamPMM) -> np.ndarray

Whitened innovations BBᵀ^{-1/2} (z_n - A z_{n-1}) for n ≥ 2.

Under a well-specified PMM the rows are i.i.d. N(0, I_2). The series is standardised internally to mean 0 / variance 1 to match the PMM's zero-mean unit-variance assumption.

Parameters:

Name Type Description Default
x ArrayLike

Observed state and observation trajectories, equal length.

required
y ArrayLike

Observed state and observation trajectories, equal length.

required
params ParamPMM

Fitted :class:ParamPMM.

required

Returns:

Type Description
ndarray, shape ``(n-1, 2)``

Whitened innovations. The first observation z_1 is dropped because it has no "previous" state to subtract.

ljung_box_test

ljung_box_test(series: ArrayLike, n_lags: int = 10) -> float

Ljung-Box test of joint zero autocorrelation up to n_lags.

Returns the p-value under H0 (i.i.d. white-noise series). The test statistic is Q = n(n+2) Σ_{k=1}^{L} ρ_k² / (n-k) where ρ_k is the lag-k empirical autocorrelation. Under H0, Q ~ χ²(L).

Parameters:

Name Type Description Default
series ArrayLike

1-D array of (residuals or) values to test.

required
n_lags int

Number of lags; default 10. Choose roughly min(20, n/5) for moderate-length series.

10

jarque_bera_test

jarque_bera_test(series: ArrayLike) -> float

Jarque-Bera test of normality (skewness + excess kurtosis).

Returns the p-value under H0 (Gaussian distribution). Statistic: JB = (n/6) (S² + (K - 3)² / 4) where S and K are the empirical skewness and kurtosis. Under H0, JB ~ χ²(2).