StableMetrics

Struct StableMetrics 

Source
pub struct StableMetrics<T> {
    pub epsilon: T,
    pub max_value: T,
    pub clip_values: bool,
    pub use_logsumexp: bool,
}
Expand description

Numerically stable computation of common operations

Fields§

§epsilon: T

Minimum value to avoid division by zero or log of zero

§max_value: T

Maximum value to clip extreme values

§clip_values: bool

Whether to clip values

§use_logsumexp: bool

Whether to use log-sum-exp trick for numerical stability

Implementations§

Source§

impl<T: Float + NumCast> StableMetrics<T>

Source

pub fn new() -> Self

Create a new StableMetrics with default settings

Source

pub fn with_epsilon(self, epsilon: T) -> Self

Set the epsilon value

Source

pub fn with_max_value(self, maxvalue: T) -> Self

Set the maximum value

Source

pub fn with_clip_values(self, clipvalues: bool) -> Self

Set whether to clip values

Source

pub fn with_logsumexp(self, uselogsumexp: bool) -> Self

Set whether to use log-sum-exp trick

Source

pub fn stable_mean(&self, values: &[T]) -> Result<T>

Numerically stable calculation of the mean

Implements Welford’s online algorithm for computing the mean, which is more stable for large datasets.

§Arguments
  • values - Input array of values
§Returns
  • The mean of the values
Source

pub fn stable_variance(&self, values: &[T], ddof: usize) -> Result<T>

Numerically stable calculation of variance

Implements Welford’s online algorithm for computing variance, which is more stable for large datasets.

§Arguments
  • values - Input array of values
  • ddof - Delta degrees of freedom (0 for population variance, 1 for sample variance)
§Returns
  • The variance of the values
Source

pub fn stable_std(&self, values: &[T], ddof: usize) -> Result<T>

Numerically stable calculation of standard deviation

Uses the stable variance calculation and takes the square root.

§Arguments
  • values - Input array of values
  • ddof - Delta degrees of freedom (0 for population std, 1 for sample std)
§Returns
  • The standard deviation of the values
Source

pub fn safe_log(&self, x: T) -> T

Safely compute logarithm

Avoids taking the logarithm of zero by adding a small epsilon value.

§Arguments
  • x - Input value
§Returns
  • The logarithm of the input, with epsilon added to avoid log(0)
Source

pub fn safe_div(&self, numer: T, denom: T) -> T

Safely compute reciprocal

Avoids division by zero by adding a small epsilon value to the denominator.

§Arguments
  • numer - Numerator
  • denom - Denominator
§Returns
  • The result of numer / (denom + epsilon)
Source

pub fn clip(&self, x: T) -> T

Clip values to a reasonable range

Limits extreme values to prevent numerical instability.

§Arguments
  • x - Input value
§Returns
  • The input value, clipped to the range [epsilon, max_value]
Source

pub fn logsumexp(&self, x: &[T]) -> T

Compute log-sum-exp in a numerically stable way

Uses the log-sum-exp trick to prevent overflow in exponentials.

§Arguments
  • x - Array of values
§Returns
  • The log-sum-exp of the values
Source

pub fn softmax(&self, x: &[T]) -> Vec<T>

Compute softmax in a numerically stable way

Uses the log-sum-exp trick to prevent overflow in exponentials.

§Arguments
  • x - Array of values
§Returns
  • Array with softmax values
Source

pub fn cross_entropy(&self, y_true: &[T], ypred: &[T]) -> Result<T>

Compute cross-entropy in a numerically stable way

§Arguments
  • y_true - True probabilities
  • ypred - Predicted probabilities
§Returns
  • The cross-entropy loss
Source

pub fn kl_divergence(&self, p: &[T], q: &[T]) -> Result<T>

Compute Kullback-Leibler divergence in a numerically stable way

§Arguments
  • p - True probability distribution
  • q - Predicted probability distribution
§Returns
  • The KL divergence
Source

pub fn js_divergence(&self, p: &[T], q: &[T]) -> Result<T>

Compute Jensen-Shannon divergence in a numerically stable way

§Arguments
  • p - First probability distribution
  • q - Second probability distribution
§Returns
  • The JS divergence
Source

pub fn wasserstein_distance(&self, u_values: &[T], vvalues: &[T]) -> Result<T>

Compute Wasserstein distance between 1D probability distributions

§Arguments
  • u_values - First distribution sample values
  • u_values - Second distribution sample values
§Returns
  • The Wasserstein distance
Source

pub fn maximum_mean_discrepancy( &self, x: &[T], y: &[T], gamma: Option<T>, ) -> Result<T>

Compute maximum mean discrepancy (MMD) between samples

§Arguments
  • x - First sample
  • y - Second sample
  • gamma - RBF kernel parameter
§Returns
  • The MMD value
Source

pub fn matrix_exp_trace(&self, eigenvalues: &[T]) -> Result<T>

Safely compute matrix exponential trace

Computes tr(exp(A)) in a numerically stable way. This is useful for computing the nuclear norm of a matrix.

§Arguments
  • eigenvalues - Eigenvalues of matrix A
§Returns
  • The trace of the matrix exponential
Source

pub fn matrix_logdet(&self, eigenvalues: &[T]) -> Result<T>

Compute stable matrix logarithm determinant

Computes log(det(A)) in a numerically stable way by summing the logarithms of eigenvalues.

§Arguments
  • eigenvalues - Eigenvalues of matrix A
§Returns
  • The logarithm of the determinant
Source

pub fn log1p(&self, x: T) -> T

Compute numerically stable log1p (log(1+x))

More accurate than log(1+x) for small values of x.

§Arguments
  • x - Input value
§Returns
  • log(1+x) computed in a numerically stable way
Source

pub fn expm1(&self, x: T) -> T

Compute numerically stable expm1 (exp(x)-1)

More accurate than exp(x)-1 for small values of x.

§Arguments
  • x - Input value
§Returns
  • exp(x)-1 computed in a numerically stable way

Trait Implementations§

Source§

impl<T: Clone> Clone for StableMetrics<T>

Source§

fn clone(&self) -> StableMetrics<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for StableMetrics<T>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<T: Float + NumCast> Default for StableMetrics<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<T> Freeze for StableMetrics<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for StableMetrics<T>
where T: RefUnwindSafe,

§

impl<T> Send for StableMetrics<T>
where T: Send,

§

impl<T> Sync for StableMetrics<T>
where T: Sync,

§

impl<T> Unpin for StableMetrics<T>
where T: Unpin,

§

impl<T> UnwindSafe for StableMetrics<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

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

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V