use crate::errors::stats_error::StatsError;
use crate::types::pairwise_sum::PairwiseSum;
use crate::types::ridge_config::RidgeConfig;
use crate::types::ridge_fit::RidgeFit;
use alloc::vec;
use alloc::vec::Vec;
use deep_causality_algebra::RealField;
use deep_causality_linear::{DenseMatrix, DenseVector, solve};
use deep_causality_num::FromPrimitive;
pub fn fit_ridge<T>(
x: &[alloc::vec::Vec<T>],
y: &[T],
config: &RidgeConfig<T>,
) -> Result<RidgeFit<T>, StatsError>
where
T: RealField + FromPrimitive,
{
if x.is_empty() {
return Err(StatsError::EmptyInput("a fit needs at least one row"));
}
if y.len() != x.len() {
return Err(StatsError::DimensionMismatch(
"the response has a different length from the design",
));
}
accumulate_and_solve(
|| x.iter().map(|r| r.as_slice()).zip(y.iter().copied()),
config,
x[0].len(),
)
}
fn accumulate_and_solve<'a, T, I, F>(
rows: F,
config: &RidgeConfig<T>,
columns: usize,
) -> Result<RidgeFit<T>, StatsError>
where
T: RealField + FromPrimitive + 'a,
I: Iterator<Item = (&'a [T], T)>,
F: Fn() -> I,
{
if columns == 0 {
return Err(StatsError::DimensionMismatch(
"a design with no columns has nothing to fit",
));
}
if !config.penalty.is_finite() {
return Err(StatsError::NonFiniteInput(
"a non-finite penalty has no fit: an infinite one drives every coefficient to zero \
and would return that as an estimate",
));
}
let p = columns;
if !config.penalisation.fits_width(p) {
return Err(StatsError::DimensionMismatch(
"the column exempted from the penalty lies outside the design",
));
}
let exempt = config.penalisation.exempt();
let cells = square(p)?;
let mut xtx_acc = vec![PairwiseSum::new(); cells];
let mut xty_acc = vec![PairwiseSum::new(); p];
let mut n = 0usize;
for (row, yi) in rows() {
if row.len() != p {
return Err(StatsError::DimensionMismatch(
"every design row must carry the same number of columns",
));
}
if row.iter().any(|v| !v.is_finite()) || !yi.is_finite() {
return Err(StatsError::NonFiniteInput(
"a non-finite observation has no least-squares fit",
));
}
for a in 0..p {
xty_acc[a].push(row[a] * yi);
let ra = row[a];
for b in 0..p {
xtx_acc[a * p + b].push(ra * row[b]);
}
}
n += 1;
}
if n == 0 {
return Err(StatsError::EmptyInput("a fit needs at least one row"));
}
let mut xtx: Vec<T> = xtx_acc.iter().map(|sum| sum.total()).collect();
let xty: Vec<T> = xty_acc.iter().map(|sum| sum.total()).collect();
for a in 0..p {
if exempt != Some(a) {
xtx[a * p + a] += config.penalty;
}
}
let normal = DenseMatrix::from_vec(xtx, p, p)
.map_err(|_| StatsError::DimensionMismatch("the normal matrix is not square"))?;
let beta = solve(&normal, &DenseVector::from_vec(xty))
.map_err(|_| {
StatsError::RankDeficient(
"the design has no unique solution at this penalty: a pivot vanished",
)
})?
.as_slice()
.to_vec();
if beta.iter().any(|v| !v.is_finite()) {
return Err(StatsError::RankDeficient(
"the solve produced a non-finite coefficient: the design is singular at this penalty",
));
}
let mut rss_acc = PairwiseSum::new();
for (row, yi) in rows() {
let mut fitted = T::zero();
for (a, &r) in row.iter().enumerate() {
fitted += beta[a] * r;
}
let e = yi - fitted;
rss_acc.push(e * e);
}
let rss = rss_acc.total();
let dof = T::from_usize(if n > p { n - p } else { 1 }).ok_or_else(|| {
StatsError::ConversionFailed(
"a degrees-of-freedom count is not representable in the working scalar",
)
})?;
Ok(RidgeFit {
beta,
sigma2: rss / dof,
})
}
fn square(columns: usize) -> Result<usize, StatsError> {
columns.checked_mul(columns).ok_or_else(|| {
StatsError::DimensionMismatch(
"the column count squared overflows a usize: no normal matrix has that shape",
)
})
}
pub fn fit_ridge_streaming<T, I>(
rows: I,
config: &RidgeConfig<T>,
columns: usize,
) -> Result<RidgeFit<T>, StatsError>
where
T: RealField + FromPrimitive,
I: IntoIterator<Item = (Vec<T>, T)> + Clone,
{
if columns == 0 {
return Err(StatsError::DimensionMismatch(
"a design with no columns has nothing to fit",
));
}
if !config.penalty.is_finite() {
return Err(StatsError::NonFiniteInput(
"a non-finite penalty has no fit: an infinite one drives every coefficient to zero \
and would return that as an estimate",
));
}
if !config.penalisation.fits_width(columns) {
return Err(StatsError::DimensionMismatch(
"the column exempted from the penalty lies outside the design",
));
}
let exempt = config.penalisation.exempt();
let cells = square(columns)?;
let mut xtx_acc = vec![PairwiseSum::new(); cells];
let mut xty_acc = vec![PairwiseSum::new(); columns];
let mut n = 0usize;
for (row, yi) in rows.clone() {
if row.len() != columns {
return Err(StatsError::DimensionMismatch(
"every design row must carry the same number of columns",
));
}
if row.iter().any(|v| !v.is_finite()) || !yi.is_finite() {
return Err(StatsError::NonFiniteInput(
"a non-finite observation has no least-squares fit",
));
}
for a in 0..columns {
xty_acc[a].push(row[a] * yi);
let ra = row[a];
for b in 0..columns {
xtx_acc[a * columns + b].push(ra * row[b]);
}
}
n += 1;
}
if n == 0 {
return Err(StatsError::EmptyInput("a fit needs at least one row"));
}
let mut xtx: Vec<T> = xtx_acc.iter().map(|sum| sum.total()).collect();
let xty: Vec<T> = xty_acc.iter().map(|sum| sum.total()).collect();
for a in 0..columns {
if exempt != Some(a) {
xtx[a * columns + a] += config.penalty;
}
}
let normal = DenseMatrix::from_vec(xtx, columns, columns)
.map_err(|_| StatsError::DimensionMismatch("the normal matrix is not square"))?;
let beta = solve(&normal, &DenseVector::from_vec(xty))
.map_err(|_| {
StatsError::RankDeficient(
"the design has no unique solution at this penalty: a pivot vanished",
)
})?
.as_slice()
.to_vec();
if beta.iter().any(|v| !v.is_finite()) {
return Err(StatsError::RankDeficient(
"the solve produced a non-finite coefficient: the design is singular at this penalty",
));
}
let mut rss_acc = PairwiseSum::new();
for (row, yi) in rows {
let mut fitted = T::zero();
for (a, &r) in row.iter().enumerate() {
fitted += beta[a] * r;
}
let e = yi - fitted;
rss_acc.push(e * e);
}
let rss = rss_acc.total();
let dof = T::from_usize(if n > columns { n - columns } else { 1 }).ok_or_else(|| {
StatsError::ConversionFailed(
"a degrees-of-freedom count is not representable in the working scalar",
)
})?;
Ok(RidgeFit {
beta,
sigma2: rss / dof,
})
}