Linear-time selection, median, MAD, trimmed mean, type-7 and weighted percentiles, histogram helpers, Tukey resistant line + IRLS biweight fit and sample autocorrelation. Namespace Titano.Core.Mathematics: a Titano.Core.Math namespace would shadow System.Math in every Titano.Core file. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
128 lines
5.1 KiB
C#
128 lines
5.1 KiB
C#
using Titano.Core.Mathematics;
|
|
using Titano.Testing;
|
|
|
|
namespace Titano.Core.Tests.Mathematics;
|
|
|
|
public sealed class StatisticsTests
|
|
{
|
|
[Test]
|
|
public void SelectMatchesSortedOrderOnRandomInput()
|
|
{
|
|
var random = new Random(7);
|
|
for (var trial = 0; trial < 200; trial++)
|
|
{
|
|
var n = 1 + random.Next(300);
|
|
// Few distinct values exercise the equal-to-pivot path.
|
|
var values = Enumerable.Range(0, n).Select(_ => (double)random.Next(trial % 2 == 0 ? 5 : 1000)).ToArray();
|
|
var sorted = values.OrderBy(v => v).ToArray();
|
|
var k = random.Next(n);
|
|
var work = (double[])values.Clone();
|
|
Assert.Equal(sorted[k], Statistics.Select(work, k));
|
|
for (var i = 0; i < k; i++) Assert.True(work[i] <= work[k]);
|
|
for (var i = k + 1; i < n; i++) Assert.True(work[i] >= work[k]);
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public void MedianHandlesOddEvenAndNaN()
|
|
{
|
|
Assert.Equal(3.0, Statistics.Median([5, 1, 3, double.NaN, 4, 2]));
|
|
Assert.Equal(2.5, Statistics.Median([4, 1, 3, 2]));
|
|
Assert.True(double.IsNaN(Statistics.Median([double.NaN])));
|
|
Assert.True(double.IsNaN(Statistics.Median([])));
|
|
}
|
|
|
|
[Test]
|
|
public void MadAndTrimmedMean()
|
|
{
|
|
double[] values = [1, 2, 3, 4, 100];
|
|
Assert.Equal(1.0, Statistics.MedianAbsoluteDeviation(values));
|
|
Assert.Equal(3.0, Statistics.MedianAbsoluteDeviation(values, 0));
|
|
// 20 % trimmed each side of 1..10 keeps 3..8.
|
|
var oneToTen = Enumerable.Range(1, 10).Select(i => (double)i).ToArray();
|
|
Assert.Approximately(5.5, Statistics.TrimmedMean(oneToTen, 0.2, 0.2), 1e-12);
|
|
Assert.Approximately(2.5, Statistics.TrimmedMean([1, 2, 3, 4, 1000], 0, 0.2), 1e-12);
|
|
Assert.Throws<ArgumentOutOfRangeException>(() => Statistics.TrimmedMean(values, 0.6, 0.4));
|
|
}
|
|
|
|
[Theory]
|
|
[Case(0.0, 1.0)]
|
|
[Case(0.25, 1.75)]
|
|
[Case(0.5, 2.5)]
|
|
[Case(1.0, 4.0)]
|
|
public void PercentileIsTypeSeven(double p, double expected) =>
|
|
Assert.Approximately(expected, Statistics.Percentile([4, 2, 1, 3], p), 1e-12);
|
|
|
|
[Test]
|
|
public void WeightedPercentileWithEqualWeightsIsHazen()
|
|
{
|
|
double[] v = [10, 20, 30, 40];
|
|
double[] w = [1, 1, 1, 1];
|
|
Assert.Approximately(25, Statistics.WeightedPercentile(v, w, 0.5), 1e-12);
|
|
Assert.Approximately(10, Statistics.WeightedPercentile(v, w, 0.1), 1e-12);
|
|
Assert.Approximately(40, Statistics.WeightedPercentile(v, w, 0.95), 1e-12);
|
|
// A heavy weight drags the median towards its value: centres 0.5, 1.5, 2.5, 8 of 13 → 30 + 10·4/5.5.
|
|
Assert.Approximately(30 + 40 / 5.5, Statistics.WeightedPercentile(v, [1, 1, 1, 10], 0.5), 1e-12);
|
|
Assert.Approximately(40, Statistics.WeightedPercentile(v, [1, 1, 1, 10], 0.7), 1e-12);
|
|
}
|
|
|
|
[Test]
|
|
public void HistogramPercentileAndDistance()
|
|
{
|
|
var bins = new double[10];
|
|
var values = Enumerable.Range(0, 1000).Select(i => i / 100.0).ToArray();
|
|
Statistics.AccumulateHistogram(values, 0, 10, bins);
|
|
Assert.True(bins.All(b => b == 100));
|
|
Assert.Approximately(5.0, Statistics.HistogramPercentile(bins, 0, 10, 0.5), 1e-12);
|
|
Assert.Approximately(0.0, Statistics.ChiSquareDistance(bins, bins), 1e-15);
|
|
Assert.Approximately(1.0, Statistics.ChiSquareDistance([1, 0], [0, 3]), 1e-15);
|
|
}
|
|
|
|
[Test]
|
|
public void RobustLineIgnoresTwentyPercentOutliers()
|
|
{
|
|
var random = new Random(3);
|
|
var n = 200;
|
|
var x = new double[n];
|
|
var y = new double[n];
|
|
for (var i = 0; i < n; i++)
|
|
{
|
|
x[i] = i * 0.5;
|
|
y[i] = 2.0 + 0.75 * x[i] + 0.05 * (random.NextDouble() - 0.5);
|
|
if (i % 5 == 0) y[i] += 20 + 10 * random.NextDouble();
|
|
}
|
|
var fit = Statistics.RobustLinearFit(x, y);
|
|
Assert.Approximately(0.75, fit.Slope, 0.002);
|
|
Assert.Approximately(2.0, fit.Intercept, 0.05);
|
|
Assert.Less(fit.Scale, 0.05);
|
|
}
|
|
|
|
[Test]
|
|
public void AutocorrelationPeaksAtThePeriod()
|
|
{
|
|
var series = Enumerable.Range(0, 240).Select(i => Math.Sin(2 * Math.PI * i / 12)).ToArray();
|
|
var r = new double[40];
|
|
Statistics.Autocorrelation(series, r);
|
|
Assert.Equal(1.0, r[0]);
|
|
Assert.Less(r[6], -0.9);
|
|
Assert.Greater(r[12], 0.9);
|
|
Assert.True(r[12] > r[11] && r[12] > r[13]);
|
|
|
|
Statistics.Autocorrelation([2, 2, 2, double.NaN, 2], r);
|
|
Assert.Equal(1.0, r[0]);
|
|
Assert.True(r.Skip(1).All(v => v == 0));
|
|
}
|
|
|
|
[Test]
|
|
public void RandomInputsAreDeterministicAndFinite()
|
|
{
|
|
var random = new Random(11);
|
|
var values = Enumerable.Range(0, 501).Select(_ => random.NextDouble() < 0.1 ? double.NaN : random.NextDouble() * 10 - 5).ToArray();
|
|
double Run() => Statistics.Median(values) + Statistics.MedianAbsoluteDeviation(values) + Statistics.TrimmedMean(values, 0.02, 0.02)
|
|
+ Statistics.Percentile(values, 0.9) + Statistics.Mean(values) + Statistics.StandardDeviation(values);
|
|
var first = Run();
|
|
Assert.True(double.IsFinite(first));
|
|
Assert.Equal(first, Run());
|
|
}
|
|
}
|