use faer::Mat;
use shrinkage::{
Binomial, ChainFusedLasso, ClusteredLasso, HybridCoordinateDescent, Irls, L1, PathOptions,
Problem, Slope, Termination,
};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let x = Mat::from_fn(8, 2, |i, j| {
if j == 0 {
i as f64 - 3.5
} else {
(i % 3) as f64
}
});
let y = [0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 1.0];
let problem = Problem::new(&x, Binomial::new(&y), L1::new(0.05));
let solver = Irls::new().tolerance(1e-9);
let fit = problem.fit_with(&solver)?;
assert_eq!(fit.termination(), Termination::Converged);
println!(
"Coefficients: {:?}; intercept: {}",
fit.coefficients(),
fit.intercept()
);
println!(
"Outer steps: {}; coordinate sweeps: {}; KKT violation: {}",
fit.iterations(),
fit.coordinate_sweeps(),
fit.kkt_violation()
);
let probabilities: Vec<_> = fit
.predict(&x)?
.into_iter()
.map(|logit| {
let tail = (-logit.abs()).exp();
if logit >= 0.0 {
1.0 / (1.0 + tail)
} else {
tail / (1.0 + tail)
}
})
.collect();
println!("Probabilities: {probabilities:?}");
let path = problem.fit_regularization_path_with(&solver, &PathOptions::new().nlambda(5))?;
for (&lambda, fit) in path.lambdas().unwrap().iter().zip(path.fits()) {
assert_eq!(fit.termination(), Termination::Converged);
println!("lambda={lambda:.5}: {:?}", fit.coefficients());
}
let hybrid = HybridCoordinateDescent::<faer::Col<f64>>::new().tolerance(1e-9);
for fit in [
Problem::new(&x, Binomial::new(&y), ClusteredLasso::new(0.02, 0.03)).fit_with(&hybrid)?,
Problem::new(&x, Binomial::new(&y), ChainFusedLasso::new(0.02, 0.03)).fit_with(&hybrid)?,
Problem::new(&x, Binomial::new(&y), Slope::new(vec![0.04, 0.02])).fit_with(&hybrid)?,
] {
assert_eq!(fit.termination(), Termination::Converged);
println!(
"HybridCoordinateDescent: outer={}, PG={}, sweeps={}, logistic mapping={}",
fit.outer_iterations(),
fit.pg_steps(),
fit.coordinate_sweeps(),
fit.stopping_value()
);
}
Ok(())
}