Quality
evaluate_fit_quality(fit_metrics, recipe='nrmse')
Evaluates the quality category of a fit based on a specified metric recipe.
This function maps a numeric fit metric (e.g., NRMSE or AIC) to a qualitative
fit quality category (GREAT, GOOD, ACCEPTABLE, BAD) using predefined thresholds. These
thresholds are stored in the FIT_QUALITY_THRESHOLDS
dictionary and must be
provided for each supported recipe.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fit_metrics
|
dict
|
Dictionary containing computed metrics from a fit. Must include the key
specified by |
required |
recipe
|
str
|
The name of the metric to evaluate quality against. Default is "nrmse". |
'nrmse'
|
Returns:
Type | Description |
---|---|
FitQuality
|
A qualitative classification of the fit (GREAT, GOOD, ACCEPTABLE, BAD), represented
by an enum or constant defined in |
Source code in sqil_core/fit/_quality.py
format_fit_metrics(fit_metrics, keys=None)
Formats and summarizes selected fit metrics with qualitative evaluations.
This function generates a human-readable table that reports selected fit metrics
(e.g., reduced χ², R², NRMSE) alongside their numerical values and qualitative
quality assessments. Quality categories are determined using evaluate_fit_quality
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fit_metrics
|
dict
|
Dictionary of fit metrics to display. Should contain values for keys like "red_chi2", "r2", "nrmse", etc. |
required |
keys
|
list of str
|
Subset of metric keys to include in the output. If None, all available keys
in |
None
|
Returns:
Type | Description |
---|---|
str
|
A plain-text table summarizing the selected metrics with their values and associated quality labels. |
Notes
- Complex-valued R² metrics are skipped.
- Keys are optionally renamed for output formatting (e.g., "red_chi2" → "reduced χ²").
Examples:
>>> metrics = {"red_chi2": 1.2, "r2": 0.97, "nrmse": 0.05}
>>> print(format_fit_metrics(metrics))
reduced χ² 1.200e+00 GOOD
R² 9.700e-01 GOOD
nrmse 5.000e-02 GOOD
Source code in sqil_core/fit/_quality.py
get_best_fit(fit_res_a, fit_res_b, recipe='nrmse_aic')
Selects the better fit result according to a specified selection recipe.
This function acts as a dispatcher to choose between two fit results using a predefined comparison strategy.
Supported recipies: - "nrmse_aic": uses NRMSE as primary metric and adjusts it with AIC if the NRMSE are in the same quality category.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fit_res_a
|
FitResult
|
The first fit result object containing metrics and parameters. |
required |
fit_res_b
|
FitResult
|
The second fit result object containing metrics and parameters. |
required |
recipe
|
Literal['nrmse_aic']
|
The name of the comparison strategy to use. |
'nrmse_aic'
|
Returns:
Type | Description |
---|---|
FitResult
|
The selected fit result, based on the comparison strategy. |
Examples:
Source code in sqil_core/fit/_quality.py
get_best_fit_nrmse_aic(fit_res_a, fit_res_b, aic_rel_tol=0.01)
Selects the better fit result based on NRMSE quality and AIC with complexity penalty.
This function compares two fit results by first evaluating the normalized root mean squared error (NRMSE) using a quality categorization scheme. If the fits differ in NRMSE quality, the one with better quality is selected. If the qualities are equal, the function compares the Akaike Information Criterion (AIC), using a relative tolerance to determine statistical equivalence. When AIC values are within tolerance, the simpler model (with fewer parameters) is preferred.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fit_res_a
|
FitResult
|
The first FitResult object. |
required |
fit_res_b
|
FitResult
|
The second FitResult object. |
required |
aic_rel_tol
|
float
|
The relative tolerance for AIC comparison. If the relative difference in AIC is below this threshold, models are considered equally good, and complexity (number of parameters) is used as a tiebreaker. Default is 0.01. |
0.01
|
Returns:
Type | Description |
---|---|
FitResult
|
The preferred fit result based on NRMSE category, AIC, and model simplicity. |
Notes
- If models are statistically equivalent in AIC and have the same complexity, the first result is returned for consistency.
- If the minimum AIC is zero, relative delta AIC is replaced by its absolute counter part, but still using the aic_rel_tol as tolerance.
Examples:
>>> best_fit = get_best_fit_nrmse_aic(fit1, fit2)
>>> print("Selected model parameters:", best_fit.params)