Regression Metrics¶
Regression metrics are used to evaluate the performance of models predicting continuous numerical values. These metrics measure the distance between the predicted values $\hat{\mathbf{y}}$ and the ground truth $\mathbf{y}$.
In sorix, these metrics are implemented to support both standard NumPy arrays and sorix.tensor objects, maintaining consistency with the framework's automatic differentiation capabilities.
Mean Squared Error (MSE)¶
The Mean Squared Error is the average of the squared differences between predicted and actual values. It penalizes larger errors more heavily due to the squaring operation.
$$ \text{MSE}(\mathbf{y}, \hat{\mathbf{y}}) = \frac{1}{n} \sum_{i=1}^{n} (y_i - \hat{y}_i)^2 $$
Root Mean Squared Error (RMSE)¶
The Root Mean Squared Error is the square root of the MSE. It has the same units as the target variable, making it more interpretable than MSE while still penalizing outliers.
$$ \text{RMSE}(\mathbf{y}, \hat{\mathbf{y}}) = \sqrt{\frac{1}{n} \sum_{i=1}^{n} (y_i - \hat{y}_i)^2} $$
Mean Absolute Error (MAE)¶
The Mean Absolute Error measures the average magnitude of the errors without considering their direction. It is less sensitive to outliers compared to MSE.
$$ \text{MAE}(\mathbf{y}, \hat{\mathbf{y}}) = \frac{1}{n} \sum_{i=1}^{n} |y_i - \hat{y}_i| $$
Mean Absolute Percentage Error (MAPE)¶
MAPE measures the accuracy as a percentage. It is scale-independent and useful for comparing performance across different datasets, though it can be problematic if $y_i = 0$.
$$ \text{MAPE}(\mathbf{y}, \hat{\mathbf{y}}) = \frac{1}{n} \sum_{i=1}^{n} \left| \frac{y_i - \hat{y}_i}{y_i} \right| $$
Coefficient of Determination ($R^2$ Score)¶
The $R^2$ score represents the proportion of variance for a dependent variable that's explained by an independent variable. A score of 1 indicates a perfect fit, while 0 indicates the model performs no better than the mean of the data.
$$ R^2 = 1 - \frac{\text{SS}_{res}}{\text{SS}_{tot}} = 1 - \frac{\sum_{i=1}^{n} (y_i - \hat{y}_i)^2}{\sum_{i=1}^{n} (y_i - \bar{y})^2} $$
where $\bar{y} = \frac{1}{n} \sum_{i=1}^n y_i$ is the mean of the true values.
# Uncomment the next line and run this cell to install sorix
#!pip install 'sorix @ git+https://github.com/Mitchell-Mirano/sorix.git@main'
import numpy as np
from sorix import tensor
from sorix.metrics import (
mean_squared_error,
root_mean_squared_error,
mean_absolute_error,
mean_absolute_percentage_error,
r2_score,
regression_report
)
Implementation Example¶
We will evaluate a simple prediction vs. ground truth scenario using both standard lists/arrays and sorix.tensor objects.
# Ground truth and predictions
y_true = tensor([3.0, -0.5, 2.0, 7.0])
y_pred = tensor([2.5, 0.0, 2.0, 8.0])
print(f"MSE: {mean_squared_error(y_true, y_pred):.4f}")
print(f"RMSE: {root_mean_squared_error(y_true, y_pred):.4f}")
print(f"MAE: {mean_absolute_error(y_true, y_pred):.4f}")
print(f"R2: {r2_score(y_true, y_pred):.4f}")
print(f"MAPE: {mean_absolute_percentage_error(y_true, y_pred) * 100:.2f}%")
MSE: 0.3750 RMSE: 0.6124 MAE: 0.5000 R2: 0.9486 MAPE: 32.74%
Comprehensive Report¶
The regression_report provides an overview of all metrics in a formatted string, similar to common machine learning libraries.
report = regression_report(y_true, y_pred)
print(report)
Metric | Score | Range ----------------------------- R2 | 0.9486 | [0, 1] MAE | 0.5000 | [0, ∞) MSE | 0.3750 | [0, ∞) RMSE | 0.6124 | [0, ∞) MAPE | 32.7381 | [0, 100]