Files
Chronos/tests/Chronos.Core.Tests/Math/StatisticsTests.cs
T
Alby96andClaude Opus 5.5 5f27a86406
CI / Build and test (pull_request) Failing after 2m3s
CI / PostgreSQL 16 integration (pull_request) Skipped
CI / PostgreSQL 17 integration (pull_request) Skipped
CI / Docker image (pull_request) Skipped
CI / Windows tests and installer (pull_request) Skipped
CI / YAML check (pull_request) Successful in 4s
refactor!: rename Titano to Chronos and move CI and releases to Gitea
The owner renamed the product and moved the repository to the self-hosted
Gitea (https://gitea.encke-hake.ts.net/BalboSoft/Chronos) for more control,
and asked for Actions and a first automatic release with the server package
and the Windows exe (docs/requests/2026-10-23-chronos-gitea.md).

- Chronos everywhere: a byte-exact rename of contents and paths, then a
  per-language review of the forms the old name's ending had dictated.
- Gitea instead of GitHub: the update check, licence, issue and support
  links read ChronosReleasesUrl; the update check takes the highest stable
  version, offers a pre-release build its final release and reports a
  repository it cannot see as a failed check. EULA, security policy and
  Gitea issue forms live at the repository root.
- Gitea Actions for the Linux act_runner (.gitea/workflows): CI, release,
  nightly image and performance gates in the .NET SDK container with git
  checkout. The release cross-compiles the Windows programs, checks their
  icon, version and DPI manifest, packs installers and zips, publishes the
  Gitea release and pushes the server image (REGISTRY_TOKEN). Windows-only
  tests run in an opt-in job on a Windows runner (WINDOWS_RUNNER).
- New Chronos.Tools commands: zip, gitea-release, postgres-ci (replaces the
  docker-exec script, which Gitea's service containers do not allow) and
  pe-check.

BREAKING CHANGE: programs, settings, data folders, file types, environment
variables and the Docker image use the new name; nothing is migrated from
Titano, which never had a release.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 11:34:48 +02:00

137 lines
5.5 KiB
C#

using Chronos.Core.Mathematics;
using Chronos.Testing;
namespace Chronos.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) + Statistics.Rms(values);
var first = Run();
Assert.True(double.IsFinite(first));
Assert.Equal(first, Run());
}
[Test]
public void MomentsOfAKnownSet()
{
double[] values = [2, 4, double.NaN, 4, 4, 5, 5, 7, 9];
Assert.Approximately(5, Statistics.Mean(values), 1e-12);
Assert.Approximately(Math.Sqrt(32.0 / 7), Statistics.StandardDeviation(values), 1e-12);
Assert.Approximately(Math.Sqrt(232.0 / 8), Statistics.Rms(values), 1e-12);
}
}