pub fn average<T>(data: &[T]) -> StatsResult<f64>where
T: ToPrimitive + Debug,Expand description
Calculate the arithmetic mean (average) of a dataset.
The average is calculated as the sum of all values divided by the number of values.
§Arguments
data- A slice of numeric values implementingToPrimitive
§Returns
StatsResult<f64>- The average as af64, or an error if the input is invalid
§Errors
Returns StatsError::EmptyData if the input slice is empty.
Returns StatsError::ConversionError if any value cannot be converted to f64.
§Examples
use rs_stats::prob::average;
// Calculate average of integers
let int_data = [1, 2, 3, 4, 5];
let avg = average(&int_data)?;
println!("Average of integers: {}", avg);
// Calculate average of floats
let float_data = [1.0, 2.5, 3.0, 4.5, 5.0];
let avg = average(&float_data)?;
println!("Average of floats: {}", avg);
// Handle empty input
let empty_data: &[i32] = &[];
assert!(average(empty_data).is_err());