use super::kernels::RBFKernel;
use crate::device::WgpuDevice;
use crate::error::{BarracudaError, Result};
use crate::linalg::solve_f64;
use crate::ops::cdist_wgsl::{DistanceMetric, compute_distances_f64_gpu};
use std::sync::Arc;
#[derive(Debug)]
pub struct RBFSurrogate {
device: Arc<WgpuDevice>,
train_x: Vec<f64>,
train_y: Vec<f64>,
weights: Vec<f64>,
poly_coeffs: Vec<f64>,
n_train: usize,
n_dim: usize,
kernel: RBFKernel,
smoothing: f64,
}
pub struct RbfTrainingData {
pub train_x: Vec<f64>,
pub train_y: Vec<f64>,
pub n_train: usize,
pub n_dim: usize,
}
pub struct RbfTrainedModel {
pub weights: Vec<f64>,
pub poly_coeffs: Vec<f64>,
pub kernel: RBFKernel,
pub smoothing: f64,
}
#[cfg(test)]
mod tests;
impl RBFSurrogate {
pub(crate) fn from_parts(
device: Arc<WgpuDevice>,
training_data: RbfTrainingData,
trained_model: RbfTrainedModel,
) -> Self {
Self {
device,
train_x: training_data.train_x,
train_y: training_data.train_y,
n_train: training_data.n_train,
n_dim: training_data.n_dim,
weights: trained_model.weights,
poly_coeffs: trained_model.poly_coeffs,
kernel: trained_model.kernel,
smoothing: trained_model.smoothing,
}
}
pub fn train(
device: Arc<WgpuDevice>,
x_data: &[Vec<f64>],
y_data: &[f64],
kernel: RBFKernel,
smoothing: f64,
) -> Result<Self> {
let n_train = x_data.len();
if n_train == 0 {
return Err(BarracudaError::InvalidInput {
message: "Training data cannot be empty".to_string(),
});
}
if y_data.len() != n_train {
return Err(BarracudaError::InvalidInput {
message: format!(
"x_data and y_data length mismatch: {} vs {}",
n_train,
y_data.len()
),
});
}
let n_dim = x_data[0].len();
let train_x: Vec<f64> = x_data.iter().flat_map(|row| row.iter().copied()).collect();
let distances = compute_distances_f64_gpu(
device.as_ref(),
&train_x,
n_train,
&train_x,
n_train,
n_dim,
DistanceMetric::Euclidean,
)?;
let n_poly = n_dim + 1; let n_total = n_train + n_poly;
let mut a = vec![0.0; n_total * n_total];
let mut b = vec![0.0; n_total];
for i in 0..n_train {
for j in 0..n_train {
let k_ij = kernel.eval(distances[i * n_train + j]);
let smooth = if i == j { smoothing } else { 0.0 };
a[i * n_total + j] = k_ij + smooth;
}
}
for i in 0..n_train {
a[i * n_total + n_train] = 1.0;
a[n_train * n_total + i] = 1.0;
for d in 0..n_dim {
a[i * n_total + (n_train + 1 + d)] = train_x[i * n_dim + d];
a[(n_train + 1 + d) * n_total + i] = train_x[i * n_dim + d];
}
}
b[..n_train].copy_from_slice(y_data);
let solution = solve_f64(device.clone(), &a, &b, n_total)?;
let weights = solution[..n_train].to_vec();
let poly_coeffs = solution[n_train..].to_vec();
Ok(Self {
device,
train_x,
train_y: y_data.to_vec(),
weights,
poly_coeffs,
n_train,
n_dim,
kernel,
smoothing,
})
}
pub fn predict(&self, x_eval: &[Vec<f64>]) -> Result<Vec<f64>> {
let n_eval = x_eval.len();
if n_eval == 0 {
return Ok(Vec::new());
}
if x_eval[0].len() != self.n_dim {
return Err(BarracudaError::InvalidInput {
message: format!(
"Dimension mismatch: expected {}, got {}",
self.n_dim,
x_eval[0].len()
),
});
}
let eval_x: Vec<f64> = x_eval.iter().flat_map(|row| row.iter().copied()).collect();
let distances = compute_distances_f64_gpu(
self.device.as_ref(),
&eval_x,
n_eval,
&self.train_x,
self.n_train,
self.n_dim,
DistanceMetric::Euclidean,
)?;
let mut predictions = Vec::with_capacity(n_eval);
for i in 0..n_eval {
let mut pred = 0.0;
for j in 0..self.n_train {
let dist = distances[i * self.n_train + j];
let phi = self.kernel.eval(dist);
pred = self.weights[j].mul_add(phi, pred);
}
pred += self.poly_coeffs[0]; for d in 0..self.n_dim {
pred = self.poly_coeffs[1 + d].mul_add(eval_x[i * self.n_dim + d], pred);
}
predictions.push(pred);
}
Ok(predictions)
}
pub fn loo_cv_rmse(&self) -> Result<f64> {
let loo_residuals = self.loo_cv_errors()?;
let mse = loo_residuals.iter().map(|r| r * r).sum::<f64>() / self.n_train as f64;
Ok(mse.sqrt())
}
pub fn loo_cv_errors(&self) -> Result<Vec<f64>> {
if self.n_train == 0 {
return Ok(Vec::new());
}
let train_points: Vec<Vec<f64>> = (0..self.n_train)
.map(|i| {
let start = i * self.n_dim;
self.train_x[start..start + self.n_dim].to_vec()
})
.collect();
let predictions = self.predict(&train_points)?;
let h_diag = self.compute_hat_diagonal()?;
let mut loo_residuals = Vec::with_capacity(self.n_train);
for i in 0..self.n_train {
let residual = self.train_y[i] - predictions[i];
let denom = 1.0 - h_diag[i];
let loo = if denom.abs() < 1e-10 {
0.0 } else {
residual / denom
};
loo_residuals.push(loo);
}
Ok(loo_residuals)
}
fn compute_hat_diagonal(&self) -> Result<Vec<f64>> {
let n = self.n_train;
let distances = compute_distances_f64_gpu(
self.device.as_ref(),
&self.train_x,
n,
&self.train_x,
n,
self.n_dim,
DistanceMetric::Euclidean,
)?;
let mut k_smooth = vec![0.0; n * n];
for i in 0..n {
for j in 0..n {
k_smooth[i * n + j] = self.kernel.eval(distances[i * n + j]);
}
k_smooth[i * n + i] += self.smoothing;
}
let mut h_diag = Vec::with_capacity(n);
for i in 0..n {
let mut e_i = vec![0.0; n];
e_i[i] = 1.0;
let w = solve_f64(self.device.clone(), &k_smooth, &e_i, n)?;
let h_ii: f64 = (0..n)
.map(|j| {
let k_raw_ij = if i == j {
k_smooth[i * n + j] - self.smoothing
} else {
k_smooth[i * n + j]
};
k_raw_ij * w[j]
})
.sum();
h_diag.push(h_ii);
}
Ok(h_diag)
}
#[must_use]
pub fn n_train(&self) -> usize {
self.n_train
}
#[must_use]
pub fn n_dim(&self) -> usize {
self.n_dim
}
}
pub struct LooSmoothing {
pub smoothing: f64,
pub rmse: f64,
pub grid_results: Vec<(f64, f64)>,
}
pub fn loo_cv_optimal_smoothing(
device: Arc<WgpuDevice>,
x_data: &[Vec<f64>],
y_data: &[f64],
kernel: RBFKernel,
smoothing_grid: Option<&[f64]>,
) -> Result<LooSmoothing> {
let default_grid: Vec<f64> = (-10..=0).map(|i| 10.0_f64.powi(i)).collect();
let grid = smoothing_grid.unwrap_or(&default_grid);
if grid.is_empty() {
return Err(BarracudaError::InvalidInput {
message: "smoothing_grid cannot be empty".into(),
});
}
use rayon::prelude::*;
let results: Vec<(f64, f64)> = grid
.par_iter()
.filter_map(|&s| {
let surrogate = RBFSurrogate::train(device.clone(), x_data, y_data, kernel, s).ok()?;
let rmse = surrogate.loo_cv_rmse().ok().filter(|r| r.is_finite())?;
Some((s, rmse))
})
.collect();
if results.is_empty() {
return Err(BarracudaError::ExecutionError {
message: "No valid smoothing values found during grid search".into(),
});
}
let (best_smoothing, best_rmse) = results
.iter()
.copied()
.min_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(std::cmp::Ordering::Greater))
.unwrap_or((grid[0], f64::INFINITY));
Ok(LooSmoothing {
smoothing: best_smoothing,
rmse: best_rmse,
grid_results: results,
})
}