rust_ml/model/core/regression_model.rs
1use crate::bench::regression_metrics::RegressionMetrics;
2use crate::core::error::ModelError;
3use crate::model::core::base::OptimizableModel;
4
5/// A trait defining common metrics and evaluation methods for regression models.
6///
7/// This trait should be implemented by any model that performs regression tasks,
8/// allowing standardized evaluation through common regression metrics.
9///
10/// # Type Parameters
11///
12/// * `Input` - The type of the input data (features).
13/// * `Output` - The type of the output data (target values).
14///
15/// # Methods
16///
17/// * `mse` - Calculates the Mean Squared Error between predictions and actual values.
18/// * `rmse` - Calculates the Root Mean Squared Error between predictions and actual values.
19/// * `r2` - Calculates the coefficient of determination (R²) score.
20/// * `compute_metrics` - Calculates a comprehensive set of regression metrics.
21///
22/// # Errors
23///
24/// Methods return `Result<_, ModelError>` to handle cases where metric calculation
25/// might fail, such as with empty inputs or numerical issues.
26pub trait RegressionModel<Input, Output>: OptimizableModel<Input, Output> {
27 fn mse(&self, x: &Input, y: &Output) -> Result<f64, ModelError>;
28 fn rmse(&self, x: &Input, y: &Output) -> Result<f64, ModelError>;
29 fn r2(&self, x: &Input, y: &Output) -> Result<f64, ModelError>;
30 fn compute_metrics(&self, x: &Input, y: &Output) -> Result<RegressionMetrics, ModelError>;
31}