pod.fit_all_robust_mean_models
fit_all_robust_mean_models(X, y, max_degree=MAX_POLY_DEGREE, n_folds=N_CV_FOLDS)Fits all polynomial models (and optionally Kriging) and returns them for caching.
Instead of fitting models, selecting the best, and throwing the rest away, this function evaluates all candidates via k-fold Cross Validation (CV) and then fits every model to the full dataset. This allows the application to instantly swap between different model structures without recalculating.
The procedure combines both references:
- Polynomials of degree 1 to
max_degreeare scored by k-fold CV MSE ([M25] Eq. 2.7-2.8, k = 10). - Kriging (only for N <= 1000, [digiqual]) is fitted once to the full data for each kernel from
build_kriging_candidate_kernels. The kernel with the lowest normalised LOO MSE ([M26] Eq. 10-14, hyperparameters from the full fit) is kept, as in [M26] Sec. 2.2.2. - The chosen kernel alone is then scored by the same k-fold CV as the polynomials, with its hyperparameters re-optimised in every fold, so that Kriging and the polynomials are compared on equal terms ([M25] Fig. 5).
- The overall winner is picked by the one-standard-error rule in
select_cv_winner([M25] Sec. 4b(i), formalised by [digiqual]).
The fitted models carry the extra attributes cv_se_ (standard error of every CV score) and, for Kriging, kernel_loo_scores_, best_kernel_name_, loo_means_, loo_residuals_ and outlier_scale_factor_.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| X | np.ndarray | 1D array or 2D matrix of input variable values. | required |
| y | np.ndarray | 1D array of outcome values (e.g., signal response). | required |
| max_degree | int | The maximum polynomial degree to test. Defaults to 10. | MAX_POLY_DEGREE |
| n_folds | int | Number of folds for Cross Validation. Defaults to 10. | N_CV_FOLDS |
Returns
| Name | Type | Description |
|---|---|---|
| tuple[dict[tuple[str, Any], Any], dict[tuple[str, Any], float], tuple[str, Any]] | tuple[dict, dict, tuple]: - fitted_models: A dictionary mapping a key like ('Polynomial', 3) to the fully trained scikit-learn model. - cv_scores: A dictionary mapping the same keys to their Cross-Validation MSE scores. - cv_winner_key: The key of the model selected by the one-standard-error rule. |
Examples
import numpy as np
from digiqual.pod import fit_all_robust_mean_models
X = np.linspace(0, 10, 50)
y = 3 * X + np.random.normal(0, 1, 50)
models, scores, best_key = fit_all_robust_mean_models(X, y)
print(f"The best model was: {best_key}")
# Instantly retrieve the degree-4 polynomial without refitting
poly_4 = models[('Polynomial', 4)]