use crate::errors::stats_error::StatsError;
use crate::types::logistic_config::LogisticConfig;
use crate::types::logistic_fit::LogisticFit;
use crate::types::pairwise_sum::PairwiseSum;
use alloc::vec;
use alloc::vec::Vec;
use deep_causality_algebra::{RealField, Scalar};
use deep_causality_linear::{DenseMatrix, DenseVector, solve};
use deep_causality_num::FromPrimitive;
pub fn sigmoid<T: Scalar>(x: T) -> T {
let one = T::one();
if x >= T::zero() {
one / (one + (-x).exp())
} else {
let e = x.exp();
e / (one + e)
}
}
pub fn fit_logistic<T>(
x: &[Vec<T>],
y: &[T],
config: &LogisticConfig<T>,
) -> Result<LogisticFit<T>, StatsError>
where
T: RealField + FromPrimitive,
{
if x.is_empty() {
return Err(StatsError::EmptyInput(
"a logistic fit needs at least one row",
));
}
if y.len() != x.len() {
return Err(StatsError::DimensionMismatch(
"the label vector has a different length from the design",
));
}
let p = x[0].len();
if p == 0 || x.iter().any(|r| r.len() != p) {
return Err(StatsError::DimensionMismatch(
"every design row must carry the same non-zero number of columns",
));
}
if x.iter().flatten().any(|v| !v.is_finite()) || y.iter().any(|v| !v.is_finite()) {
return Err(StatsError::NonFiniteInput(
"a non-finite observation has no logistic fit",
));
}
if !config.penalty.is_finite() || !config.tolerance.is_finite() {
return Err(StatsError::NonFiniteInput(
"a non-finite penalty or tolerance does not define a stopping test",
));
}
let one = T::one();
if y.iter().any(|&v| v < T::zero() || v > one) {
return Err(StatsError::NegativeProbability(
"a logistic label lies in [0, 1]: it is a probability, and nothing outside that \
interval is one",
));
}
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 mut beta = vec![T::zero(); p];
for iteration in 1..=config.max_iterations {
let mut grad_acc = vec![PairwiseSum::new(); p];
let mut hess_acc = vec![PairwiseSum::new(); p * p];
for (row, &yi) in x.iter().zip(y.iter()) {
let mut eta = T::zero();
for (a, &r) in row.iter().enumerate() {
eta += beta[a] * r;
}
let pi = sigmoid(eta);
let w = pi * (one - pi);
let resid = yi - pi;
for a in 0..p {
grad_acc[a].push(row[a] * resid);
let ra = row[a];
for b in 0..p {
hess_acc[a * p + b].push(ra * w * row[b]);
}
}
}
let mut grad: Vec<T> = grad_acc.iter().map(|sum| sum.total()).collect();
let mut hess: Vec<T> = hess_acc.iter().map(|sum| sum.total()).collect();
for a in 0..p {
if exempt != Some(a) {
grad[a] -= config.penalty * beta[a];
hess[a * p + a] += config.penalty;
}
}
let hessian = DenseMatrix::from_vec(hess, p, p)
.map_err(|_| StatsError::DimensionMismatch("the Hessian is not square"))?;
let step = solve(&hessian, &DenseVector::from_vec(grad))
.map(|v| v.as_slice().to_vec())
.map_err(|_| {
if iteration == 1 {
StatsError::RankDeficient(
"the design is rank deficient at this penalty: the Hessian is singular before \
the first step, where the weights are still uniform",
)
} else {
StatsError::NotConverged(
iteration,
"the fitted probabilities saturated and the weights collapsed: the data are \
separable, so the likelihood has no finite maximum and any coefficient \
returned here would be an artefact of the stopping point",
)
}
})?;
let mut largest = T::zero();
for a in 0..p {
beta[a] += step[a];
let m = step[a].abs();
if m > largest {
largest = m;
}
}
if !beta.iter().all(|v| v.is_finite()) {
return Err(StatsError::NotConverged(
iteration,
"the iterate left the representable range: the likelihood has no finite maximum here",
));
}
if largest <= config.tolerance {
return Ok(LogisticFit {
beta,
iterations: iteration,
});
}
}
Err(StatsError::NotConverged(
config.max_iterations,
"the step never fell below the tolerance",
))
}