qmri.errors¶
errors ¶
Error metrics and uncertainty propagation.
This module provides:
- R-squared (coefficient of determination)
- RMSE (root mean square error)
- Normalised RMSE
- Residuals
- Uncertainty propagation utilities
- Covariance matrix handling
Example
normalized_rmse
module-attribute
¶
normalized_rmse = normalised_rmse
Alias for :func:normalised_rmse using American English spelling.
normalised_rmse ¶
normalised_rmse(
observed: NDArray[floating],
predicted: NDArray[floating],
*,
axis: int | None = None,
method: str = "range",
) -> NDArray[floating] | float
Calculate Normalised Root Mean Square Error (NRMSE).
NRMSE is RMSE normalised by a characteristic value of the observed data, making it dimensionless and comparable across different scales.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
observed
|
NDArray[floating]
|
Observed/measured values. |
required |
predicted
|
NDArray[floating]
|
Model-predicted values. |
required |
axis
|
int | None
|
Axis along which to compute NRMSE. If None, computed over flattened arrays. Default is None. |
None
|
method
|
str
|
Normalisation method (default "range"):
|
'range'
|
Returns:
| Type | Description |
|---|---|
NDArray[floating] | float
|
NRMSE value(s). Returns float if axis is None or input is 1D. |
NRMSE is calculated as:
where \(y_{norm}\) depends on the normalisation method:
- range: \(y_{norm} = y_{max} - y_{min}\)
- mean: \(y_{norm} = \bar{y}\)
- std: \(y_{norm} = \sigma_y\)
When the normalisation factor is zero, returns infinity.
Example
Source code in packages/qmri/src/qmri/errors/metrics.py
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 | |
r_squared ¶
r_squared(
observed: NDArray[floating],
predicted: NDArray[floating],
*,
axis: int | None = None,
) -> NDArray[floating] | float
Calculate coefficient of determination (R-squared).
The R-squared value indicates how well the predicted values explain the variance in the observed values. Values range from 0 to 1, where 1 indicates a perfect fit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
observed
|
NDArray[floating]
|
Observed/measured values. |
required |
predicted
|
NDArray[floating]
|
Model-predicted values. |
required |
axis
|
int | None
|
Axis along which to compute R-squared. If None, computed over flattened arrays. Default is None. |
None
|
Returns:
| Type | Description |
|---|---|
NDArray[floating] | float
|
R-squared value(s). Returns float if axis is None or input is 1D. |
The coefficient of determination is calculated as:
where \(SS_{res} = \sum_i (y_i - \hat{y}_i)^2\) is the residual sum of squares and \(SS_{tot} = \sum_i (y_i - \bar{y})^2\) is the total sum of squares.
When \(SS_{tot} = 0\) (constant observed values), returns 0.0.
Example
import numpy as np
from qmri.errors.metrics import r_squared
observed = np.array([1.0, 2.0, 3.0, 4.0])
predicted = np.array([1.1, 1.9, 3.2, 3.8])
print(f"R-squared: {r_squared(observed, predicted):.4f}")
# R-squared: 0.9800
# For multi-dimensional arrays:
observed_2d = np.array([[1, 2, 3], [4, 5, 6]])
predicted_2d = np.array([[1.1, 1.9, 3.1], [4.2, 4.8, 6.1]])
r2 = r_squared(observed_2d, predicted_2d, axis=1)
print(r2.shape)
# (2,)
Source code in packages/qmri/src/qmri/errors/metrics.py
residuals ¶
residuals(
observed: NDArray[floating] | float,
predicted: NDArray[floating] | float,
*,
axis: int | None = None,
) -> NDArray[floating] | float
Calculate residuals (observed - predicted).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
observed
|
NDArray[floating] | float
|
Observed/measured values. |
required |
predicted
|
NDArray[floating] | float
|
Model-predicted values. |
required |
axis
|
int | None
|
Axis along which to compute. Not used for simple residuals but included for API consistency. |
None
|
Returns:
| Type | Description |
|---|---|
NDArray[floating] | float
|
Residuals with same shape as input. |
Example
Source code in packages/qmri/src/qmri/errors/metrics.py
rmse ¶
rmse(
observed: NDArray[floating],
predicted: NDArray[floating],
*,
axis: int | None = None,
) -> NDArray[floating] | float
Calculate Root Mean Square Error (RMSE).
RMSE measures the average magnitude of the errors between observed and predicted values, with the same units as the input data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
observed
|
NDArray[floating]
|
Observed/measured values. |
required |
predicted
|
NDArray[floating]
|
Model-predicted values. |
required |
axis
|
int | None
|
Axis along which to compute RMSE. If None, computed over flattened arrays. Default is None. |
None
|
Returns:
| Type | Description |
|---|---|
NDArray[floating] | float
|
RMSE value(s). Returns float if axis is None or input is 1D. |
RMSE is calculated as: