variance_simd

Function variance_simd 

Source
pub fn variance_simd<F>(x: &ArrayView1<'_, F>, ddof: usize) -> Option<F>
where F: Float + SimdUnifiedOps,
Expand description

Compute the variance of a 1D array with SIMD acceleration.

This function uses SIMD operations for computing the variance with Welford’s algorithm for numerical stability. The variance is calculated as the average of squared deviations from the mean.

§Arguments

  • x - Input 1D array
  • ddof - Delta degrees of freedom (0 for population variance, 1 for sample variance)

§Returns

  • Some(variance) - The variance of the array
  • None - If the array is empty or has insufficient data (length <= ddof)

§Performance

  • f32: ~2-3x faster than scalar for arrays > 1000 elements
  • f64: ~2-3x faster than scalar for arrays > 1000 elements
  • Uses SIMD operations for mean and sum of squared deviations

§Examples

use scirs2_core::ndarray::array;
use scirs2_core::ndarray_ext::reduction::variance_simd;

let x = array![1.0f64, 2.0, 3.0, 4.0, 5.0];
let var = variance_simd(&x.view(), 1).unwrap(); // Sample variance (ddof=1)
assert!((var - 2.5).abs() < 1e-10);