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 |
'Nelder-Mead'
|
max_iter
|
str
|
Forwarded to |
'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:
- Compute the observed LRT statistic
T_obsvia :func:likelihood_ratio_test_hmm_vs_pmm. - Fit the HMM-constrained model to the data; call it
hmm_null. - Simulate
n_bootstrapsynthetic trajectories fromhmm_null; recompute the LRT statistic on each. - 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
|
method
|
str
|
Forwarded to :func: |
'Nelder-Mead'
|
max_iter
|
str
|
Forwarded to :func: |
'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
|
|
p_value |
float
|
Asymptotic p-value under H0 from |
log_likelihood_pmm, log_likelihood_hmm |
Maximised log-likelihoods under each hypothesis. |
|
pmm_params, hmm_params |
MLE parameters under each hypothesis. |
|
n_obs |
int
|
Number of joint observations |
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). |
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: |
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 |
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 ( |
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: |
required |
Returns:
| Type | Description |
|---|---|
ndarray, shape ``(n-1, 2)``
|
Whitened innovations. The first observation |
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 |
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).