Shrinkage

shrinkage::Scaled

Shrinkage for Rust · rust · 0.3.0

pub struct Scaled<P>

A complete penalty multiplied by a finite, nonnegative scalar.

Scaled::new(scale, penalty) evaluates scale * penalty.value(coefficients). Parameters are checked during evaluation and fitting. For a positive scale, the exact proximal map delegates to the inner map with step * scale. Zero scaling returns zero value and the identity proximal map, while still checking the wrapped penalty through Penalty::validate_input. The wrapper adds no allocations or coefficient storage.

use shrinkage::{ElasticNet, Penalty, ProximalPenalty, Scaled};

let penalty = Scaled::new(0.5, ElasticNet::new(2.0, 4.0));
assert_eq!(penalty.value(&[-2.0])?, 6.0);
let mut output = [0.0];
penalty.prox(&[-4.0], 0.5, &mut output)?;
assert_eq!(output, [-1.75]);

Scaling a value-only term does not supply a complete proximal map:

use shrinkage::{FitError, Penalty, ProximalPenalty, Scaled};
struct ValueOnly;
impl Penalty for ValueOnly {
    type Error = FitError;
    fn value(&self, _: &[f64]) -> Result<f64, FitError> { Ok(1.0) }
}
let penalty = Scaled::new(0.5, ValueOnly);
penalty.prox(&[1.0], 1.0, &mut [0.0]);

Inherent implementation

impl<P> Scaled<P>

new

pub fn new(scale: f64, penalty: P) -> Self

Set the scalar and complete inner penalty, checked when used.

penalty

pub fn penalty(&self) -> &P

Borrow the complete inner penalty, including its own strengths.

scale

pub fn scale(&self) -> f64

Configured multiplier of the complete penalty.

Implementation of Clone

impl<P: core::clone::Clone> Clone for Scaled<P>

clone

fn clone(&self) -> Self

Implementation of Copy

impl<P: core::marker::Copy> Copy for Scaled<P>

Implementation of Debug

impl<P: core::fmt::Debug> Debug for Scaled<P>

fmt

fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result

Implementation of Penalty

impl<P: Penalty<Error = shrinkage::FitError>> Penalty for Scaled<P>

Error

type Error = FitError;

validate_input

fn validate_input(&self, coefficients: &[f64]) -> Result<(), FitError>

value

fn value(&self, coefficients: &[f64]) -> Result<f64, FitError>

Implementation of ProximalPenalty

impl<P: ProximalPenalty> ProximalPenalty for Scaled<P>

prox

fn prox(
    &self,
    input: &[f64],
    step: f64,
    output: &mut [f64]
) -> Result<(), FitError>

Implementation of RestrictedProximalPenalty

impl<P: RestrictedProximalPenalty> RestrictedProximalPenalty for Scaled<P>

prox_restricted

fn prox_restricted(
    &self,
    input: &[f64],
    unpenalized: &[bool],
    step: f64,
    output: &mut [f64]
) -> Result<(), FitError>

validate_restricted_input

fn validate_restricted_input(
    &self,
    coefficients: &[f64],
    unpenalized: &[bool]
) -> Result<(), FitError>

value_restricted

fn value_restricted(
    &self,
    coefficients: &[f64],
    unpenalized: &[bool]
) -> Result<f64, FitError>

Implementation of Freeze

impl<P> Freeze for Scaled<P> where P: Freeze

Implementation of RefUnwindSafe

impl<P> RefUnwindSafe for Scaled<P> where P: RefUnwindSafe

Implementation of Send

impl<P> Send for Scaled<P> where P: Send

Implementation of Sync

impl<P> Sync for Scaled<P> where P: Sync

Implementation of Unpin

impl<P> Unpin for Scaled<P> where P: Unpin

Implementation of UnsafeUnpin

impl<P> UnsafeUnpin for Scaled<P> where P: UnsafeUnpin

Implementation of UnwindSafe

impl<P> UnwindSafe for Scaled<P> where P: UnwindSafe

Implementation of Any

impl<T> Any for T where T: 'static + ?Sized

Implementation of Borrow

impl<T> Borrow<T> for T where T: ?Sized

Implementation of BorrowMut

impl<T> BorrowMut<T> for T where T: ?Sized

Implementation of ByRef

impl<T> ByRef<T> for T

Implementation of CastableFrom

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT where ST: ?Sized, DT: ?Sized

Implementation of CastableFrom

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT where ST: ?Sized, DT: ?Sized

Implementation of CloneToUninit

impl<T> CloneToUninit for T where T: Clone

Implementation of From

impl<T> From<T> for T

Implementation of Imply

impl<T, U> Imply<T> for U where T: ?Sized, U: ?Sized

Implementation of Into

impl<T, U> Into<U> for T where U: From<T>

Implementation of IntoEither

impl<T> IntoEither for T

Implementation of Pointable

impl<T> Pointable for T

Implementation of Read

impl<T> Read<Exclusive, BecauseExclusive> for T where T: ?Sized

Implementation of Same

impl<T> Same for T

Implementation of SupersetOf

impl<SS, SP> SupersetOf<SS> for SP where SS: SubsetOf<SP>

Implementation of ToOwned

impl<T> ToOwned for T where T: Clone

Implementation of TryFrom

impl<T, U> TryFrom<U> for T where U: Into<T>

Implementation of TryInto

impl<T, U> TryInto<U> for T where U: TryFrom<T>