Statistics
Univariate Statistical Analysis
To compute descriptive statistical characteristics of a sample set you can either call the extension methods of the Statistics class directly, or create a new DescriptiveStatistics instance and pass your samples to its constructor to compute all the characteristics in one pass.
Code Sample using DescriptiveStatistics:
using MathNet.Numerics.Statistics; var samples = new ChiSquare(5).Samples().Take(1000); var statistics = new DescriptiveStatistics(samples); // Order Statistics var largestElement = statistics.Maximum; var smallestElement = statistics.Minimum; var median = statistics.Median; // Central Tendency var mean = statistics.Mean; // Dispersion var variance = statistics.Variance; var stdDev = statistics.StandardDeviation; // Other Statistics var kurtosis = statistics.Kurtosis; var skewness = statistics.Skewness;
Code Sample using the extensions methods:
using MathNet.Numerics.Statistics; // Extension methods are defined on IEnumerable, // yet we call ToArray so all the methods operate on the same data var samples = new ChiSquare(5).Samples().Take(1000).ToArray(); // Order Statistics var largestElement = samples.Maximum(); var smallestElement = samples.Minimum(); var median = samples.Median(); var 250thOrderStatistic = samples.OrderStatistic(250); // Central Tendency var mean = samples.Mean(); // Dispersion var variance = samples.Variance(); var biasedPopulationVariance = samples.PopulationVariance(); var stdDev = samples.StandardDeviation(); var biasedPopulationStdDev = samples.PopulationStandardDeviation();
Histograms
A histrogram can be computed using the Histogram class. Its constructor takes the samples enumerable. the number of buckets to create, plus optionally the range (minimum, maximum) of the sample data if available.
var histogram = new Histogram(samples, 10); var bucket3count = histogram[2].Count;
Percentiles
Percentiles can be computed using the Percentile class. It supports four methods, which can be chosen using the Methods property:
- Nist: Using the method recommended by NIST. This is the default method.
- Nearest: Using the neares rank method.
- Excel: Using the method that is also used by Microsoft Excel.
- Interpolation: Using linear interpolation between the two nearest ranks, see wikipedia.
var percentile = new Percentile(samples) { Method = PercentileMethod.Nearest };
var percentile90 = percentile.Compute(0.9);
var percentiles = percentile.Compute(new[] { .25, .5, .75 });
Correlation
The Correlation class supports computing Pearson product-momentum correlation coefficients:
Code Sample: Computing the correlation coefficient between 1000 samples of f(x) = 2x and g(x) = x^2:
double[] dataF = SignalGenerator.EquidistantInterval(x => x * 2, 0, 100, 1000); double[] dataG = SignalGenerator.EquidistantInterval(x => x * x, 0, 100, 1000); double correlation = Correlation.Pearson(dataF, dataG);



