Skip to main content

MetricsExtractor

Trait MetricsExtractor 

Source
pub trait MetricsExtractor: Len {
    // Required methods
    fn compute_basic_metrics(&self) -> Result<BasicMetrics, MetricsError>;
    fn compute_shape_metrics(&self) -> Result<ShapeMetrics, MetricsError>;
    fn compute_range_metrics(&self) -> Result<RangeMetrics, MetricsError>;
    fn compute_trend_metrics(&self) -> Result<TrendMetrics, MetricsError>;
    fn compute_risk_metrics(&self) -> Result<RiskMetrics, MetricsError>;

    // Provided methods
    fn compute_curve_metrics(&self) -> Result<Metrics, MetricsError> { ... }
    fn compute_surface_metrics(&self) -> Result<Metrics, MetricsError> { ... }
}
Expand description

A trait for extracting comprehensive metrics from a curve.

§Overview

The CurveMetricsExtractor trait provides methods to compute various statistical, analytical, and risk-related metrics for a given curve. It allows for a systematic and extensible approach to curve analysis across different curve types and contexts.

§Methods

§Metric Computation Methods

  • compute_basic_metrics: Calculates fundamental statistical measures.
  • compute_shape_metrics: Analyzes curve shape characteristics.
  • compute_range_metrics: Determines range and distribution properties.
  • compute_trend_metrics: Evaluates directional and regression-related metrics.
  • compute_risk_metrics: Quantifies financial and statistical risk indicators.

§Comprehensive Metrics

  • compute_curve_metrics: Computes all metrics and combines them into a CurveMetrics struct.

§Usage

Implement this trait for specific curve types or analysis strategies to provide custom metric computation logic tailored to different domains or requirements.

Required Methods§

Source

fn compute_basic_metrics(&self) -> Result<BasicMetrics, MetricsError>

Computes basic statistical metrics for the curve.

§Returns
  • Ok(BasicMetrics): Struct containing mean, median, mode, and standard deviation.
  • Err(CurvesError): If metrics computation fails.
§Errors

Returns MetricsError::BasicError when the sample is empty, when the variance cannot be computed in Decimal precision, or when the mode aggregation is ambiguous for the provided samples.

Source

fn compute_shape_metrics(&self) -> Result<ShapeMetrics, MetricsError>

Computes shape-related metrics for the curve.

§Returns
  • Ok(ShapeMetrics): Struct containing skewness, kurtosis, peaks, valleys, and inflection points.
  • Err(CurvesError): If metrics computation fails.
§Errors

Returns MetricsError::ShapeError when the sample has fewer than three points (skewness/kurtosis undefined) or when the central-moment computation overflows the Decimal range.

Source

fn compute_range_metrics(&self) -> Result<RangeMetrics, MetricsError>

Computes range-related metrics for the curve.

§Returns
  • Ok(RangeMetrics): Struct containing min/max points, range, quartiles, and interquartile range.
  • Err(CurvesError): If metrics computation fails.
§Errors

Returns MetricsError::RangeError when the sample is empty (no min/max available) or when the quartile interpolation fails for a sparse sample.

Source

fn compute_trend_metrics(&self) -> Result<TrendMetrics, MetricsError>

Computes trend-related metrics for the curve.

§Returns
  • Ok(TrendMetrics): Struct containing slope, intercept, R-squared, and moving average.
  • Err(CurvesError): If metrics computation fails.
§Errors

Returns MetricsError::TrendError when the linear regression cannot be fit (fewer than two samples or zero variance on the independent axis).

Source

fn compute_risk_metrics(&self) -> Result<RiskMetrics, MetricsError>

Computes risk-related metrics for the curve.

§Returns
  • Ok(RiskMetrics): Struct containing volatility, VaR, expected shortfall, coefficient of variation, and Sharpe ratio.
  • Err(CurvesError): If metrics computation fails.
§Errors

Returns MetricsError::RiskError when the sample cannot support VaR/ES estimation (fewer than the required quantile sample count), when the Sharpe ratio denominator is zero, or when a checked step (including a square root) fails, and MetricsError::ZeroMean when the values have a zero mean, where the coefficient of variation is undefined (#824).

Provided Methods§

Source

fn compute_curve_metrics(&self) -> Result<Metrics, MetricsError>

Computes and aggregates all curve metrics into a comprehensive CurveMetrics struct.

§Returns
  • Ok(CurveMetrics): A struct containing all computed metrics.
  • Err(CurvesError): If any metrics computation fails.
§Errors

Propagates any MetricsError returned by compute_basic_metrics, compute_shape_metrics, compute_range_metrics, compute_trend_metrics or compute_risk_metrics (the first failing call short-circuits).

Source

fn compute_surface_metrics(&self) -> Result<Metrics, MetricsError>

Computes comprehensive metrics for a surface representation.

This method aggregates multiple specialized metric calculations into a single cohesive result. It collects basic statistical properties, shape characteristics, range information, trend analysis, and risk assessments to provide a complete analytical view of the surface.

§Returns
  • Ok(Metrics): A composite structure containing all computed metrics categories:
    • Basic metrics (statistical fundamentals)
    • Shape metrics (geometric properties)
    • Range metrics (distribution characteristics)
    • Trend metrics (directional patterns)
    • Risk metrics (uncertainty measurements)
  • Err(MetricsError): If any of the individual metric computations fails
§Errors

This method can return various error types depending on which computation fails:

  • MetricsError::BasicError - If basic metrics computation fails
  • MetricsError::ShapeError - If shape metrics computation fails
  • MetricsError::RangeError - If range metrics computation fails
  • MetricsError::TrendError - If trend metrics computation fails
  • MetricsError::RiskError - If risk metrics computation fails
  • MetricsError::ZeroMean - If the values have a zero mean, where the coefficient of variation of the risk metrics is undefined

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl MetricsExtractor for Curve

A default implementation for the Curve type using a provided default strategy.

This implementation provides a basic approach to computing curve metrics by using interpolation and statistical methods available in the standard curve analysis library.

§Note

This is a minimal implementation that may need to be customized or enhanced based on specific requirements or domain-specific analysis needs.

Source§

impl MetricsExtractor for Surface