Module_Statistics_Basic - vorcyc/Vorcyc.Mathematics GitHub Wiki
Location: Home / Statistics module / Basic statistics functions
Vorcyc.Mathematics.Statistics.Basic is a class that provides basic statistical functions, including methods for mean, median, mode, variance, standard deviation, and coefficient of variation.
All methods below are located in class: Vorcyc.Mathematics.Statistics.Basic
πContents
- π Sum method
- π Average method
- π Median method
- π Mode method
- π Variance method
- π StandardDeviation method
- π CoefficientOfVariation method
- π CalculateAllStatistics method
Vorcyc.Mathematics.Statistics.Basic is a class that provides basic statistical functions, including methods for mean, median, mode, variance, standard deviation, and coefficient of variation.
-
public static T Sum<T>(this Span<T> values) where T : INumber<T>- Computes the sum of elements in a set of values, optimized with SIMD.
- Parameters:
-
values: The set of values to sum.
-
- Returns: The sum of elements in the set of values.
- Exceptions:
-
ArgumentException: Thrown whenvaluesis empty.
-
-
public static T Sum<T>(this Span<T> values, Func<T, T> selector) where T : INumber<T>- Computes the sum of elements in a set of values using the specified selector function.
- Parameters:
-
values: The set of values to sum. -
selector: A function used to select values.
-
- Returns: The sum of elements in the set of values.
- Exceptions:
-
ArgumentException: Thrown whenvaluesis empty.
-
-
public static T Average<T>(this Span<T> values) where T : INumber<T>- Computes the average of elements in a set of values.
- Parameters:
-
values: The set of values to average.
-
- Returns: The average of elements in the set of values.
- Exceptions:
-
ArgumentException: Thrown whenvaluesis empty.
-
-
public static T Median<T>(this Span<T> values) where T : INumber<T>- Computes the median of elements in a set of values.
- Parameters:
-
values: The set of values for which to compute the median.
-
- Returns: The median of elements in the set of values.
- Exceptions:
-
ArgumentException: Thrown whenvaluesis empty.
-
-
public static T Mode<T>(this Span<T> values) where T : INumber<T>- Computes the mode of elements in a set of values.
- Parameters:
-
values: The set of values for which to compute the mode.
-
- Returns: The element with the highest frequency in the set of values.
- Exceptions:
-
ArgumentException: Thrown whenvaluesis empty.
-
-
public static (T average, T variance) Variance<T>(this Span<T> values) where T : INumber<T>- Computes the average and variance of elements in a set of values.
- Parameters:
-
values: The set of values for which to compute average and variance.
-
- Returns: A tuple containing the average and variance of elements in the set of values.
- Exceptions:
-
ArgumentException: Thrown whenvaluesis empty.
-
-
public static T StandardDeviation<T>(this Span<T> values) where T : IFloatingPointIeee754<T>- Computes the standard deviation of elements in a set of values.
- Parameters:
-
values: The set of values for which to compute standard deviation.
-
- Returns: The standard deviation of elements in the set of values.
- Exceptions:
-
ArgumentException: Thrown whenvaluesis empty.
-
-
public static T CoefficientOfVariation<T>(this Span<T> values) where T : IFloatingPointIeee754<T>- Computes the coefficient of variation of elements in a set of values.
- Parameters:
-
values: The set of values for which to compute the coefficient of variation.
-
- Returns: The coefficient of variation of elements in the set of values.
- Exceptions:
-
ArgumentException: Thrown whenvaluesis empty.
-
-
public static (T Mean, T Median, T Mode, T Variance, T StandardDeviation, T CoefficientOfVariation) CalculateAllStatistics<T>(this Span<T> values) where T : IFloatingPointIeee754<T>- Computes all statistics for a set of values, including mean, median, mode, variance, standard deviation, and coefficient of variation.
- Parameters:
-
values: The set of values for which to compute statistics.
-
- Returns: A tuple containing mean, median, mode, variance, standard deviation, and coefficient of variation.
- Exceptions:
-
ArgumentException: Thrown whenvaluesis empty.
-
The following example uses several methods from the Basic class, with comments in the sample:
using System;
using System.Collections.Generic;
using Vorcyc.Mathematics.Statistics;
using static System.Runtime.InteropServices.JavaScript.JSType;
public class BasicStatisticsExample
{
public static void Main()
{
// Define dataset
Span<double> data = new double[] { 1.0, 2.0, 2.0, 3.0, 4.0, 5.0 };
// Compute sum
double sum = data.Sum();
Console.WriteLine("Sum: " + sum);
// Compute average
double average = data.Average();
Console.WriteLine("Average: " + average);
// Compute median
double median = data.Median();
Console.WriteLine("Median: " + median);
// Compute mode
double mode = data.Mode();
Console.WriteLine("Mode: " + mode);
// Compute variance
var (mean, variance) = data.Variance();
Console.WriteLine("Variance: " + variance);
// Compute standard deviation
double standardDeviation = data.StandardDeviation();
Console.WriteLine("Standard Deviation: " + standardDeviation);
// Compute coefficient of variation
double coefficientOfVariation = data.CoefficientOfVariation();
Console.WriteLine("Coefficient of Variation: " + coefficientOfVariation);
// Compute all statistics
var (meanAll, medianAll, modeAll, varianceAll, standardDeviationAll, coefficientOfVariationAll) = data.CalculateAllStatistics();
Console.WriteLine($"Mean: {meanAll}, Median: {medianAll}, Mode: {modeAll}, Variance: {varianceAll}, Standard Deviation: {standardDeviationAll}, Coefficient of Variation: {coefficientOfVariationAll}");
}
}