use crate::error::{finite, invalid};
use crate::{ElasticNet, FitError, L1, Penalty, ProximalPenalty, Ridge};
pub trait RestrictedProximalPenalty: ProximalPenalty {
fn value_restricted(&self, coefficients: &[f64], unpenalized: &[bool])
-> Result<f64, FitError>;
fn validate_restricted_input(
&self,
coefficients: &[f64],
unpenalized: &[bool],
) -> Result<(), FitError> {
if coefficients.len() != unpenalized.len() {
return Err(invalid(
"unpenalized mask length must match the coefficients",
));
}
self.validate_input(coefficients)
}
fn prox_restricted(
&self,
input: &[f64],
unpenalized: &[bool],
step: f64,
output: &mut [f64],
) -> Result<(), FitError>;
}
#[derive(Clone, Debug)]
pub struct Masked<P> {
unpenalized: Vec<bool>,
penalty: P,
}
impl<P: RestrictedProximalPenalty> Masked<P> {
pub fn new(unpenalized: Vec<bool>, penalty: P) -> Self {
Self {
unpenalized,
penalty,
}
}
pub fn unpenalized(&self) -> &[bool] {
&self.unpenalized
}
pub fn penalty(&self) -> &P {
&self.penalty
}
}
impl<P: RestrictedProximalPenalty> Penalty for Masked<P> {
type Error = FitError;
fn value(&self, coefficients: &[f64]) -> Result<f64, FitError> {
self.penalty
.value_restricted(coefficients, &self.unpenalized)
}
fn validate_input(&self, coefficients: &[f64]) -> Result<(), FitError> {
self.penalty
.validate_restricted_input(coefficients, &self.unpenalized)
}
}
impl<P: RestrictedProximalPenalty> ProximalPenalty for Masked<P> {
fn prox(&self, input: &[f64], step: f64, output: &mut [f64]) -> Result<(), FitError> {
self.penalty
.prox_restricted(input, &self.unpenalized, step, output)
}
}
impl RestrictedProximalPenalty for L1 {
fn value_restricted(
&self,
coefficients: &[f64],
unpenalized: &[bool],
) -> Result<f64, FitError> {
self.validate_restricted_input(coefficients, unpenalized)?;
let value = if self.lambda() == 0.0 {
0.0
} else {
coefficients
.iter()
.zip(unpenalized)
.filter(|(_, excluded)| !**excluded)
.map(|(value, _)| self.lambda() * value.abs())
.sum()
};
Ok(finite(value, "computing the restricted penalty", 0)?)
}
fn prox_restricted(
&self,
input: &[f64],
unpenalized: &[bool],
step: f64,
output: &mut [f64],
) -> Result<(), FitError> {
ElasticNet::new(self.lambda(), 0.0).prox_restricted(input, unpenalized, step, output)
}
}
impl RestrictedProximalPenalty for Ridge {
fn value_restricted(
&self,
coefficients: &[f64],
unpenalized: &[bool],
) -> Result<f64, FitError> {
ElasticNet::new(0.0, self.lambda()).value_restricted(coefficients, unpenalized)
}
fn prox_restricted(
&self,
input: &[f64],
unpenalized: &[bool],
step: f64,
output: &mut [f64],
) -> Result<(), FitError> {
ElasticNet::new(0.0, self.lambda()).prox_restricted(input, unpenalized, step, output)
}
}
impl RestrictedProximalPenalty for ElasticNet {
fn value_restricted(
&self,
coefficients: &[f64],
unpenalized: &[bool],
) -> Result<f64, FitError> {
self.validate_restricted_input(coefficients, unpenalized)?;
let l1 = L1::new(self.l1()).value_restricted(coefficients, unpenalized)?;
let l2 = if self.l2() == 0.0 {
0.0
} else {
coefficients
.iter()
.zip(unpenalized)
.filter(|(_, excluded)| !**excluded)
.map(|(&coefficient, _)| {
let scaled = self.l2().sqrt() * coefficient;
0.5 * scaled * scaled
})
.sum()
};
Ok(finite(l1 + l2, "computing the restricted penalty", 0)?)
}
fn prox_restricted(
&self,
input: &[f64],
unpenalized: &[bool],
step: f64,
output: &mut [f64],
) -> Result<(), FitError> {
self.validate_restricted_input(input, unpenalized)?;
if input.len() != output.len() {
return Err(invalid("proximal input and output lengths must match"));
}
if !step.is_finite() || step <= 0.0 {
return Err(invalid(
"proximal step must be finite and strictly positive",
));
}
if unpenalized.iter().all(|&excluded| excluded) {
output.copy_from_slice(input);
return Ok(());
}
let threshold = finite(step * self.l1(), "computing a proximal threshold", 0)?;
let denominator = finite(1.0 + step * self.l2(), "computing proximal curvature", 0)?;
for ((&value, &excluded), target) in input.iter().zip(unpenalized).zip(output) {
*target = if excluded {
value
} else {
value.signum() * (value.abs() - threshold).max(0.0) / denominator
};
}
Ok(())
}
}
impl<P: RestrictedProximalPenalty + ?Sized> RestrictedProximalPenalty for Box<P> {
fn value_restricted(
&self,
coefficients: &[f64],
unpenalized: &[bool],
) -> Result<f64, FitError> {
(**self).value_restricted(coefficients, unpenalized)
}
fn validate_restricted_input(
&self,
coefficients: &[f64],
unpenalized: &[bool],
) -> Result<(), FitError> {
(**self).validate_restricted_input(coefficients, unpenalized)
}
fn prox_restricted(
&self,
input: &[f64],
unpenalized: &[bool],
step: f64,
output: &mut [f64],
) -> Result<(), FitError> {
(**self).prox_restricted(input, unpenalized, step, output)
}
}