Files
Chronos/tests/Chronos.Core.Tests/Math/LocalRegressionTests.cs
T
Alby96andClaude Opus 5.5 f2b58522b9
CI / Build and test (push) Successful in 8m14s
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 7s
Release / Build, test and publish (push) Successful in 13m19s
CI / PostgreSQL 16 integration (push) Successful in 4m49s
CI / PostgreSQL 17 integration (push) Successful in 5m28s
CI / Docker image (push) Successful in 53s
Release / Server image (push) Failing after 10s
refactor!: rename Titano to Chronos and move CI and releases to Gitea (#6)
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 and the system SQLite. 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; the
  throw-away server runs without durability) and pe-check.
- Timed tests take their budget from TestTiming (CHRONOS_TEST_TIME_FACTOR,
  3 on the shared runner; the weekly performance gates keep theirs).

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>
Co-authored-by: Alberto Balbo <alberto.balbo.96@gmail.com>
2026-10-23 12:45:18 +02:00

122 lines
5.5 KiB
C#

using System.Globalization;
using Chronos.Core.Mathematics;
using Chronos.Testing;
namespace Chronos.Core.Tests.Mathematics;
public sealed class LocalRegressionTests(TestContext context)
{
[Theory]
[Case(1)]
[Case(2)]
public void ReproducesPolynomialsOfItsDegreeIncludingEdges(int degree)
{
var y = Enumerable.Range(0, 60).Select(i => degree == 1 ? 3 - 0.2 * i : 1 + 0.1 * i - 0.004 * i * i).ToArray();
var result = LocalRegression.Smooth(y, window: 11, degree: degree, robustIterations: 2);
for (var i = 0; i < y.Length; i++) Assert.Approximately(y[i], result.Fitted[i], 1e-9);
Assert.True(result.SurvivingFraction.All(f => Math.Abs(f - 1) < 1e-12));
}
[Test]
public void TimeSpaceWindowsHandleUnevenSpacing()
{
var random = new Random(4);
var x = new double[80];
for (var i = 1; i < x.Length; i++) x[i] = x[i - 1] + 1 + 9 * random.NextDouble();
var y = x.Select(v => 5 + 0.01 * v).ToArray();
var result = LocalRegression.Smooth(y, window: 9, x: x);
for (var i = 0; i < y.Length; i++) Assert.Approximately(y[i], result.Fitted[i], 1e-9);
}
[Test]
public void TwentyPercentOutliersBarelyMoveTheRobustFit()
{
var random = new Random(21);
var n = 300;
var clean = Enumerable.Range(0, n).Select(i => Math.Sin(i / 40.0) + 0.02 * (random.NextDouble() - 0.5)).ToArray();
var truth = Enumerable.Range(0, n).Select(i => Math.Sin(i / 40.0)).ToArray();
var contaminated = (double[])clean.Clone();
var outliers = new HashSet<int>();
while (outliers.Count < n / 5) outliers.Add(random.Next(n));
foreach (var i in outliers) contaminated[i] += (random.NextDouble() < 0.5 ? -1 : 1) * (1 + 2 * random.NextDouble());
var reference = LocalRegression.Smooth(clean, window: 31, robustIterations: 3);
var robust = LocalRegression.Smooth(contaminated, window: 31, robustIterations: 3);
var plain = LocalRegression.Smooth(contaminated, window: 31, robustIterations: 0);
var robustShift = Enumerable.Range(0, n).Max(i => Math.Abs(robust.Fitted[i] - reference.Fitted[i]));
var plainShift = Enumerable.Range(0, n).Max(i => Math.Abs(plain.Fitted[i] - reference.Fitted[i]));
var robustError = Enumerable.Range(0, n).Max(i => Math.Abs(robust.Fitted[i] - truth[i]));
context.Log(string.Create(CultureInfo.InvariantCulture,
$"20 % outliers: robust fit moved {robustShift:F4} (error vs truth {robustError:F4}), non-robust fit moved {plainShift:F4}"));
Assert.Less(robustShift, 0.03, "robust fit moved by the outliers");
Assert.Less(robustError, 0.04);
Assert.Greater(plainShift, 10 * robustShift);
// Outliers are rejected; their windows report the lost weight.
Assert.True(outliers.All(i => robust.RobustWeights[i] < 0.05));
var inlierWeight = Enumerable.Range(0, n).Where(i => !outliers.Contains(i)).Average(i => robust.RobustWeights[i]);
Assert.Greater(inlierWeight, 0.8);
Assert.InRange(robust.SurvivingFraction.Average(), 0.6, 0.9);
}
[Test]
public void MissingSamplesAreInterpolatedAndReportedAsRejected()
{
var y = Enumerable.Range(0, 40).Select(i => 0.5 * i).ToArray();
y[0] = double.NaN;
y[10] = double.NaN;
y[11] = double.PositiveInfinity;
y[39] = double.NaN;
var result = LocalRegression.Smooth(y, window: 7);
Assert.Equal(36, result.ValidCount);
foreach (var i in new[] { 0, 10, 11, 39 })
{
Assert.Approximately(0.5 * i, result.Fitted[i], 1e-9);
Assert.Equal(0.0, result.RobustWeights[i]);
}
}
[Test]
public void PriorWeightsExcludeSamples()
{
var y = Enumerable.Range(0, 30).Select(i => (double)i).ToArray();
y[15] = 1000;
var weights = Enumerable.Repeat(1.0, 30).ToArray();
weights[15] = 0;
var result = LocalRegression.Smooth(y, window: 5, robustIterations: 0, priorWeights: weights);
Assert.Approximately(15, result.Fitted[15], 1e-9);
}
[Test]
public void DegenerateInputs()
{
Assert.True(double.IsNaN(LocalRegression.Smooth([double.NaN, double.NaN], 3).Fitted[1]));
Assert.Equal(4.0, LocalRegression.Smooth([4.0], 5).Fitted[0]);
var two = LocalRegression.Smooth([1.0, 3.0, double.NaN], 5);
Assert.Approximately(5, two.Fitted[2], 1e-12);
Assert.Throws<ArgumentOutOfRangeException>(() => LocalRegression.Smooth([1.0], 0));
Assert.Throws<ArgumentException>(() => LocalRegression.Smooth([1.0, 2.0], 3, x: [1.0, 0.0]));
}
[Test]
public void RandomInputsGiveFiniteDeterministicOutputs()
{
var random = new Random(99);
for (var trial = 0; trial < 20; trial++)
{
var n = 1 + random.Next(400);
var y = Enumerable.Range(0, n).Select(_ => random.NextDouble() < 0.15 ? double.NaN : 100 * (random.NextDouble() - 0.5)).ToArray();
if (y.All(double.IsNaN)) y[0] = 1;
var window = 1 + random.Next(60);
var degree = 1 + random.Next(2);
var a = LocalRegression.Smooth(y, window, degree, robustIterations: 3);
var b = LocalRegression.Smooth(y, window, degree, robustIterations: 3);
Assert.True(a.Fitted.All(double.IsFinite), $"trial {trial}");
Assert.True(a.SurvivingFraction.All(f => f is >= 0 and <= 1 + 1e-12));
Assert.SequenceEqual(a.Fitted, b.Fitted);
Assert.SequenceEqual(a.RobustWeights, b.RobustWeights);
}
}
}