Shrinkage

shrinkage::AxisPenalty

Shrinkage for Rust · rust · 0.3.0

pub struct AxisPenalty<P>

A complete vector penalty applied along an explicit coefficient axis.

Features separates responses; Responses can couple their coefficients at each feature. All can couple the full matrix. Each group uses the supplied strength without dividing by the group or response count. The prox is exact because the groups are disjoint. Intercepts are never included. Strided response groups use owned reusable CPU scratch buffers.

Compose crate::Scaled and crate::Masked inside the axis penalty. With Responses, each feature row receives the same vector penalty and a mask has one entry per response. With Features, a mask has one entry per feature and excludes those feature rows across all responses. Masks require an inner crate::RestrictedProximalPenalty.

use shrinkage::{
    AxisPenalty, CoefficientLayout, Masked, Penalty, PenaltyAxis,
    ProximalPenalty, Ridge, Scaled,
};

let layout = CoefficientLayout::new(3, 2)?;
let penalty = AxisPenalty::new(
    layout,
    PenaltyAxis::Responses,
    Scaled::new(0.5, Masked::new(vec![true, false], Ridge::new(2.0))),
);
// Each response's feature vector is contiguous, so the rows are [1, 4],
// [2, 5], and [3, 6]. Only the second response is penalized in each row.
let input = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0];
assert!((penalty.value(&input)? - 38.5).abs() < 1e-12);
let mut output = [0.0; 6];
penalty.prox(&input, 1.0, &mut output)?;
assert_eq!(output, [1.0, 2.0, 3.0, 2.0, 2.5, 3.0]);

Inherent implementation

impl<P> AxisPenalty<P>

axis

pub fn axis(&self) -> PenaltyAxis

Axis along which the complete vector penalty applies.

layout

pub fn layout(&self) -> CoefficientLayout

Feature and response dimensions on which this penalty is defined.

new

pub fn new(layout: CoefficientLayout, axis: PenaltyAxis, penalty: P) -> Self

Apply a vector penalty to the selected axis of the given layout.

penalty

pub fn penalty(&self) -> &P

Borrow the complete vector penalty applied to each group.

Implementation of Clone

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

clone

fn clone(&self) -> Self

Implementation of Debug

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

fmt

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

Implementation of PathPenalty

impl<P: PathPenalty> PathPenalty for shrinkage::AxisPenalty<P>

AtStrength

type AtStrength = AxisPenalty<<P as PathPenalty>::AtStrength>;

at_strength

fn at_strength(
    &self,
    lambda: f64
) -> Result<<Self as PathPenalty>::AtStrength, FitError>

maximum_penalty

fn maximum_penalty(&self, gradient: &[f64]) -> Result<PenaltyMaximum, FitError>

path_anchor

fn path_anchor(&self, gradient: &[f64]) -> Result<f64, FitError>

Implementation of PathSolver

impl<M, P, Q, V> PathSolver<Problem<&M, MultivariateGaussian<'_>, AxisPenalty<P>>, AxisPenalty<Q>> for shrinkage::ProximalGradient<V> where M: RawColumns<f64> + ColumnStats<f64> + MatVecInto<V> + MatTransposeVecInto<V> + ?Sized, Q: ProximalPenalty, V: ProximalVector

Error

type Error = FitError<<M as MatrixErrorType>::Error>;

Fit

type Fit = MultivariateFit;

solve_path

fn solve_path(
    &self,
    problem: &Problem<&M, MultivariateGaussian<'_>, AxisPenalty<P>>,
    penalties: Vec<AxisPenalty<Q>>
) -> Result<PathFit<MultivariateFit, AxisPenalty<Q>>, <Self as PathSolver>::Error>

Implementation of PathSolver

impl<P, Q: ProximalPenalty> PathSolver<Problem<Box<dyn RuntimeDesign + '_>, MultivariateGaussian<'_>, AxisPenalty<P>>, AxisPenalty<Q>> for shrinkage::ProximalGradient<Vec<f64>>

Error

type Error = FitError<BackendError>;

Fit

type Fit = MultivariateFit;

solve_path

fn solve_path(
    &self,
    problem: &Problem<Box<dyn RuntimeDesign + '_>, MultivariateGaussian<'_>, AxisPenalty<P>>,
    penalties: Vec<AxisPenalty<Q>>
) -> Result<PathFit<MultivariateFit, AxisPenalty<Q>>, <Self as PathSolver>::Error>

Implementation of Penalty

impl<P: Penalty<Error = shrinkage::FitError>> Penalty for AxisPenalty<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 AxisPenalty<P>

prox

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

Implementation of Freeze

impl<P> !Freeze for AxisPenalty<P>

Implementation of RefUnwindSafe

impl<P> !RefUnwindSafe for AxisPenalty<P>

Implementation of Send

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

Implementation of Sync

impl<P> !Sync for AxisPenalty<P>

Implementation of Unpin

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

Implementation of UnsafeUnpin

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

Implementation of UnwindSafe

impl<P> UnwindSafe for AxisPenalty<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>