Module_Statistics_Advanced - vorcyc/Vorcyc.Mathematics GitHub Wiki

Location: Home / Statistics module / Advanced statistics functions

Statistics module

Advanced statistics functions

Vorcyc.Mathematics.Statistics.Advanced is a class that provides advanced statistical functions, including methods for percentiles, quartiles, skewness, kurtosis, confidence intervals, hypothesis tests, analysis of variance, chi-square tests, and nonparametric tests.

All methods below are located in class: Vorcyc.Mathematics.Statistics.Advanced


📒Contents


Vorcyc.Mathematics.Statistics.Advanced class

Vorcyc.Mathematics.Statistics.Advanced is a class that provides advanced statistical functions, including methods for percentiles, quartiles, skewness, kurtosis, confidence intervals, hypothesis tests, analysis of variance, chi-square tests, and nonparametric tests.

Methods

1. Percentile method

  • public static T Percentile<T>(this Span<T> sequence, double percentile) where T : IFloatingPointIeee754<T>
    • Computes the specified percentile of a dataset.
    • Parameters:
      • sequence: The dataset.
      • percentile: The percentile (between 0 and 1).
    • Returns: The value at the specified percentile.

2. Quartiles method

  • public static (T Q1, T Q2, T Q3) Quartiles<T>(this Span<T> sequence) where T : IFloatingPointIeee754<T>
    • Computes the quartiles of a dataset.
    • Parameters:
      • sequence: The dataset.
    • Returns: A tuple containing the first, second, and third quartiles.

3. Skewness method

  • public static T Skewness<T>(this Span<T> sequence) where T : IFloatingPointIeee754<T>
    • Computes the skewness of a dataset, measuring the symmetry of the distribution.
    • Parameters:
      • sequence: The dataset.
    • Returns: The skewness value of the dataset.

4. Kurtosis method

  • public static T Kurtosis<T>(this Span<T> sequence) where T : IFloatingPointIeee754<T>
    • Computes the kurtosis of a dataset, measuring the peakedness of the distribution.
    • Parameters:
      • sequence: The dataset.
    • Returns: The kurtosis value of the dataset.

5. ConfidenceInterval method

  • public static (T Lower, T Upper) ConfidenceInterval<T>(this Span<T> sequence, double confidenceLevel) where T : IFloatingPointIeee754<T>
    • Computes a confidence interval for the mean or proportion.
    • Parameters:
      • sequence: The dataset.
      • confidenceLevel: The confidence level (for example, 0.95 for 95%).
    • Returns: A tuple containing the lower and upper bounds of the confidence interval.

6. TTest method

  • public static T TTest<T>(this Span<T> sample, T populationMean) where T : IFloatingPointIeee754<T>
    • Implements hypothesis tests such as z-test, t-test, chi-square test, and others.
    • Parameters:
      • sample: The sample dataset.
      • populationMean: The population mean.
    • Returns: The t-test statistic.

7. Anova method

  • public static T Anova<T>(this IEnumerable<ArraySegment<T>> groups) where T : IFloatingPointIeee754<T>
    • Implements one-way and multi-way analysis of variance.
    • Parameters:
      • groups: A collection of data groups.
    • Returns: The F-value from analysis of variance.

8. ChiSquaredTest method

  • public static T ChiSquaredTest<T>(this Span<T> observed, Span<T> expected) where T : IFloatingPointIeee754<T>
    • Implements chi-square tests of independence and goodness of fit.
    • Parameters:
      • observed: The observed data.
      • expected: The expected data.
    • Returns: The chi-square test statistic.

9. MannWhitneyUTest method

  • public static T MannWhitneyUTest<T>(this Span<T> sample1, Span<T> sample2) where T : IFloatingPointIeee754<T>
    • Implements nonparametric tests such as the Mann–Whitney U test and Kruskal–Wallis test.
    • Parameters:
      • sample1: The first sample dataset.
      • sample2: The second sample dataset.
    • Returns: The Mann–Whitney U test statistic.

Code example

The following example uses several methods from the Advanced 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 AdvancedStatisticsExample
{
    public static void Main()
    {
        // Define dataset
        Span<double> data = new double[] { 1.0, 2.0, 3.0, 4.0, 5.0 };

        // Compute percentile
        double percentile = data.Percentile(0.9);
        Console.WriteLine("90th Percentile: " + percentile);

        // Compute quartiles
        var (Q1, Q2, Q3) = data.Quartiles();
        Console.WriteLine($"Q1: {Q1}, Q2: {Q2}, Q3: {Q3}");

        // Compute skewness
        double skewness = data.Skewness();
        Console.WriteLine("Skewness: " + skewness);

        // Compute kurtosis
        double kurtosis = data.Kurtosis();
        Console.WriteLine("Kurtosis: " + kurtosis);

        // Compute confidence interval
        var (lower, upper) = data.ConfidenceInterval(0.95);
        Console.WriteLine($"95% Confidence Interval: ({lower}, {upper})");

        // Perform t-test
        double tTest = data.TTest(3.0);
        Console.WriteLine("T-Test: " + tTest);

        // Perform analysis of variance
        var groups = new List<ArraySegment<double>>
    {
        new ArraySegment<double>(new double[] { 1.0, 2.0, 3.0 }),
        new ArraySegment<double>(new double[] { 4.0, 5.0, 6.0 }),
        new ArraySegment<double>(new double[] { 7.0, 8.0, 9.0 })
    };
        double anova = groups.Anova();
        Console.WriteLine("ANOVA: " + anova);

        // Perform chi-square test
        Span<double> observed = new double[] { 10, 20, 30 };
        Span<double> expected = new double[] { 15, 25, 35 };
        double chiSquared = observed.ChiSquaredTest(expected);
        Console.WriteLine("Chi-Squared Test: " + chiSquared);

        // Perform Mann–Whitney U test
        Span<double> sample1 = new double[] { 1.0, 2.0, 3.0 };
        Span<double> sample2 = new double[] { 4.0, 5.0, 6.0 };
        double mannWhitneyU = sample1.MannWhitneyUTest(sample2);
        Console.WriteLine("Mann-Whitney U Test: " + mannWhitneyU);
    }
}

Robust statistics (Robust static class)

Outlier-resistant descriptive helpers (Robust static methods on Span<T> / ReadOnlySpan<T>):

Method Description
InterquartileRange Q3 − Q1
MedianAbsoluteDeviation MAD from median
Winsorize(ReadOnlySpan<T>, lower=0.05, upper=0.95) Static method; clamps values to the percentile band and returns a new T[]
HuberMean(this Span<T>, T delta, maxIterations=50) Extension method; iteratively reweighted robust mean (delta required)

Online moments (RunningMoments<T> struct)

Welford single-pass mean/variance accumulator:

Member Description
Push(T) Incorporate one sample
Mean / Variance / PopulationVariance / StandardDeviation Current statistics
Count Samples seen
Reset() Clear state
⚠️ **GitHub.com Fallback** ⚠️