use ruthril::core::statistics::Statistics;
pub trait Predictor {
type Input;
type Output;
type Error;
fn predict(&self, input: &Self::Input) -> Result<Self::Output, Self::Error>;
fn predict_batch(&self, inputs: &[Self::Input]) -> Result<Vec<Self::Output>, Self::Error>;
fn calculate_confidence(&self, predictions: &[f64]) -> PredictionConfidence {
PredictionConfidence {
mean_prediction: Statistics::mean(predictions).unwrap_or(0.0),
prediction_variance: Statistics::variance(predictions).unwrap_or(0.0),
prediction_std: Statistics::std_deviation(predictions).unwrap_or(0.0),
}
}
fn detect_outliers(&self, predictions: &[f64], threshold_std: f64) -> Vec<usize> {
if let (Some(mean), Some(std)) = (Statistics::mean(predictions), Statistics::std_deviation(predictions)) {
predictions.iter()
.enumerate()
.filter_map(|(idx, &pred)| {
if (pred - mean).abs() > threshold_std * std {
Some(idx)
} else {
None
}
})
.collect()
} else {
Vec::new()
}
}
}
#[derive(Debug, Clone)]
pub struct PredictionConfidence {
pub mean_prediction: f64,
pub prediction_variance: f64,
pub prediction_std: f64,
}