mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-22 04:28:04 +00:00
docs: remove C# Implementation Considerations sections, clean up temp scripts, reorganize test files
- Remove 'C# Implementation Considerations' sections from 34 indicator .md files - Delete 29 temp PowerShell scripts (_fix_mojibake.ps1, _hex_scan.ps1, etc.) - Move test files into tests/ subdirectories for consistent project structure - Add trader-focused bullet points to indicator documentation
This commit is contained in:
@@ -0,0 +1,285 @@
|
||||
using Xunit;
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
public class ReluTests
|
||||
{
|
||||
private const double Tolerance = 1e-10;
|
||||
|
||||
[Fact]
|
||||
public void Constructor_SetsProperties()
|
||||
{
|
||||
var indicator = new Relu();
|
||||
Assert.Equal("ReLU", indicator.Name);
|
||||
Assert.Equal(0, indicator.WarmupPeriod);
|
||||
Assert.True(indicator.IsHot); // Always hot (no warmup)
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_ReturnsRelu()
|
||||
{
|
||||
var indicator = new Relu();
|
||||
var time = DateTime.UtcNow;
|
||||
|
||||
indicator.Update(new TValue(time, 5.0));
|
||||
Assert.Equal(5.0, indicator.Last.Value, Tolerance); // max(0, 5) = 5
|
||||
|
||||
indicator.Update(new TValue(time.AddMinutes(1), -3.0));
|
||||
Assert.Equal(0.0, indicator.Last.Value, Tolerance); // max(0, -3) = 0
|
||||
|
||||
indicator.Update(new TValue(time.AddMinutes(2), 0.0));
|
||||
Assert.Equal(0.0, indicator.Last.Value, Tolerance); // max(0, 0) = 0
|
||||
|
||||
indicator.Update(new TValue(time.AddMinutes(3), 100.5));
|
||||
Assert.Equal(100.5, indicator.Last.Value, Tolerance); // max(0, 100.5) = 100.5
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_KnownValues()
|
||||
{
|
||||
var indicator = new Relu();
|
||||
var time = DateTime.UtcNow;
|
||||
|
||||
// Positive values pass through
|
||||
indicator.Update(new TValue(time, 10.0));
|
||||
Assert.Equal(10.0, indicator.Last.Value, Tolerance);
|
||||
|
||||
// Negative values become zero
|
||||
indicator.Update(new TValue(time.AddMinutes(1), -10.0));
|
||||
Assert.Equal(0.0, indicator.Last.Value, Tolerance);
|
||||
|
||||
// Zero stays zero
|
||||
indicator.Update(new TValue(time.AddMinutes(2), 0.0));
|
||||
Assert.Equal(0.0, indicator.Last.Value, Tolerance);
|
||||
|
||||
// Small positive value
|
||||
indicator.Update(new TValue(time.AddMinutes(3), 0.001));
|
||||
Assert.Equal(0.001, indicator.Last.Value, Tolerance);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_IsNewFalse_CorrectsPreviousValue()
|
||||
{
|
||||
var indicator = new Relu();
|
||||
var time = DateTime.UtcNow;
|
||||
|
||||
indicator.Update(new TValue(time, 5.0));
|
||||
indicator.Update(new TValue(time.AddMinutes(1), -2.0));
|
||||
Assert.Equal(0.0, indicator.Last.Value, Tolerance);
|
||||
|
||||
// Correct last value
|
||||
indicator.Update(new TValue(time.AddMinutes(1), 3.0), isNew: false);
|
||||
Assert.Equal(3.0, indicator.Last.Value, Tolerance);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_IterativeCorrection_RestoresState()
|
||||
{
|
||||
var indicator = new Relu();
|
||||
var time = DateTime.UtcNow;
|
||||
double[] values = { 5.0, -3.0, 2.5, -1.0, 0.0, 8.0, -5.0 };
|
||||
|
||||
// Process all values
|
||||
foreach (var v in values)
|
||||
{
|
||||
indicator.Update(new TValue(time, v));
|
||||
time = time.AddMinutes(1);
|
||||
}
|
||||
double finalResult = indicator.Last.Value;
|
||||
|
||||
// Reset and process with corrections
|
||||
indicator.Reset();
|
||||
time = DateTime.UtcNow;
|
||||
foreach (var v in values)
|
||||
{
|
||||
// Submit wrong value first
|
||||
indicator.Update(new TValue(time, 999.0));
|
||||
// Correct it
|
||||
indicator.Update(new TValue(time, v), isNew: false);
|
||||
time = time.AddMinutes(1);
|
||||
}
|
||||
|
||||
Assert.Equal(finalResult, indicator.Last.Value, Tolerance);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_NaN_UsesLastValidValue()
|
||||
{
|
||||
var indicator = new Relu();
|
||||
var time = DateTime.UtcNow;
|
||||
|
||||
indicator.Update(new TValue(time, 7.0));
|
||||
double beforeNaN = indicator.Last.Value;
|
||||
|
||||
indicator.Update(new TValue(time.AddMinutes(1), double.NaN));
|
||||
Assert.Equal(beforeNaN, indicator.Last.Value, Tolerance);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_Infinity_UsesLastValidValue()
|
||||
{
|
||||
var indicator = new Relu();
|
||||
var time = DateTime.UtcNow;
|
||||
|
||||
indicator.Update(new TValue(time, 3.5));
|
||||
double beforeInf = indicator.Last.Value;
|
||||
|
||||
indicator.Update(new TValue(time.AddMinutes(1), double.PositiveInfinity));
|
||||
Assert.Equal(beforeInf, indicator.Last.Value, Tolerance);
|
||||
|
||||
indicator.Update(new TValue(time.AddMinutes(2), double.NegativeInfinity));
|
||||
Assert.Equal(beforeInf, indicator.Last.Value, Tolerance);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Reset_ClearsState()
|
||||
{
|
||||
var indicator = new Relu();
|
||||
var time = DateTime.UtcNow;
|
||||
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
indicator.Update(new TValue(time.AddMinutes(i), i - 5));
|
||||
}
|
||||
|
||||
Assert.True(indicator.IsHot);
|
||||
indicator.Reset();
|
||||
Assert.True(indicator.IsHot); // Still hot (no warmup)
|
||||
Assert.Equal(default, indicator.Last);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Pub_EventFires()
|
||||
{
|
||||
var indicator = new Relu();
|
||||
int eventCount = 0;
|
||||
indicator.Pub += (object? sender, in TValueEventArgs args) => eventCount++;
|
||||
|
||||
indicator.Update(new TValue(DateTime.UtcNow, 5.0));
|
||||
Assert.Equal(1, eventCount);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Chaining_Constructor_Works()
|
||||
{
|
||||
var source = new TSeries();
|
||||
var indicator = new Relu(source);
|
||||
|
||||
source.Add(new TValue(DateTime.UtcNow, 5.0), true);
|
||||
Assert.Equal(5.0, indicator.Last.Value, Tolerance);
|
||||
|
||||
source.Add(new TValue(DateTime.UtcNow.AddMinutes(1), -3.0), true);
|
||||
Assert.Equal(0.0, indicator.Last.Value, Tolerance);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Calculate_TSeries_MatchesStreaming()
|
||||
{
|
||||
int count = 50;
|
||||
var gbm = new GBM(startPrice: 100, mu: 0.05, sigma: 0.2, seed: 42000);
|
||||
var bars = gbm.Fetch(count, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
// Use returns (which can be negative) for meaningful ReLU test
|
||||
var source = Change.Batch(bars.Close);
|
||||
|
||||
// Streaming
|
||||
var streaming = new Relu();
|
||||
var streamingResults = new List<double>();
|
||||
for (int i = 0; i < source.Count; i++)
|
||||
{
|
||||
streaming.Update(source[i]);
|
||||
streamingResults.Add(streaming.Last.Value);
|
||||
}
|
||||
|
||||
// Batch
|
||||
var batch = Relu.Batch(source);
|
||||
|
||||
// Compare all values
|
||||
for (int i = 0; i < source.Count; i++)
|
||||
{
|
||||
Assert.Equal(streamingResults[i], batch[i].Value, Tolerance);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Calculate_Span_MatchesTSeries()
|
||||
{
|
||||
int count = 50;
|
||||
var gbm = new GBM(startPrice: 100, mu: 0.05, sigma: 0.2, seed: 42001);
|
||||
var bars = gbm.Fetch(count, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
var source = Change.Batch(bars.Close);
|
||||
|
||||
// TSeries batch
|
||||
var batchResult = Relu.Batch(source);
|
||||
|
||||
// Span calculation
|
||||
var values = source.Values.ToArray();
|
||||
var output = new double[count];
|
||||
Relu.Batch(values, output);
|
||||
|
||||
for (int i = 0; i < source.Count; i++)
|
||||
{
|
||||
Assert.Equal(batchResult[i].Value, output[i], Tolerance);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Calculate_Span_ValidatesArguments()
|
||||
{
|
||||
Assert.Throws<ArgumentException>(() =>
|
||||
{
|
||||
Span<double> output = stackalloc double[10];
|
||||
Relu.Batch(ReadOnlySpan<double>.Empty, output);
|
||||
});
|
||||
|
||||
Assert.Throws<ArgumentException>(() =>
|
||||
{
|
||||
ReadOnlySpan<double> source = stackalloc double[10];
|
||||
Span<double> output = stackalloc double[5];
|
||||
Relu.Batch(source, output);
|
||||
});
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Relu_PositivePassthrough()
|
||||
{
|
||||
var indicator = new Relu();
|
||||
var time = DateTime.UtcNow;
|
||||
|
||||
// All positive values should pass through unchanged
|
||||
for (double v = 0.1; v <= 100.0; v += 10.0)
|
||||
{
|
||||
indicator.Update(new TValue(time, v));
|
||||
Assert.Equal(v, indicator.Last.Value, Tolerance);
|
||||
time = time.AddMinutes(1);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Relu_NegativeBecomesZero()
|
||||
{
|
||||
var indicator = new Relu();
|
||||
var time = DateTime.UtcNow;
|
||||
|
||||
// All negative values should become zero
|
||||
for (double v = -0.1; v >= -100.0; v -= 10.0)
|
||||
{
|
||||
indicator.Update(new TValue(time, v));
|
||||
Assert.Equal(0.0, indicator.Last.Value, Tolerance);
|
||||
time = time.AddMinutes(1);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Relu_AlwaysNonNegative()
|
||||
{
|
||||
var indicator = new Relu();
|
||||
var time = DateTime.UtcNow;
|
||||
|
||||
// ReLU output should always be >= 0
|
||||
for (int i = -50; i <= 50; i++)
|
||||
{
|
||||
indicator.Update(new TValue(time.AddMinutes(i + 50), i));
|
||||
Assert.True(indicator.Last.Value >= 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,153 @@
|
||||
using Xunit;
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// ReLU validation tests - validates against known mathematical properties
|
||||
/// since no external library implementations exist for this activation function.
|
||||
/// </summary>
|
||||
public class ReluValidationTests
|
||||
{
|
||||
private const double Tolerance = 1e-10;
|
||||
|
||||
[Fact]
|
||||
public void Relu_MathematicalDefinition_Streaming()
|
||||
{
|
||||
// ReLU: f(x) = max(0, x)
|
||||
var indicator = new Relu();
|
||||
var time = DateTime.UtcNow;
|
||||
double[] testValues = { -10.0, -5.0, -1.0, -0.5, 0.0, 0.5, 1.0, 5.0, 10.0 };
|
||||
|
||||
foreach (var x in testValues)
|
||||
{
|
||||
indicator.Update(new TValue(time, x));
|
||||
double expected = Math.Max(0.0, x);
|
||||
Assert.Equal(expected, indicator.Last.Value, Tolerance);
|
||||
time = time.AddMinutes(1);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Relu_MathematicalDefinition_Batch()
|
||||
{
|
||||
double[] testValues = { -10.0, -5.0, -1.0, -0.5, 0.0, 0.5, 1.0, 5.0, 10.0 };
|
||||
var source = new TSeries();
|
||||
var time = DateTime.UtcNow;
|
||||
foreach (var v in testValues)
|
||||
{
|
||||
source.Add(new TValue(time, v), true);
|
||||
time = time.AddMinutes(1);
|
||||
}
|
||||
|
||||
var result = Relu.Batch(source);
|
||||
|
||||
for (int i = 0; i < testValues.Length; i++)
|
||||
{
|
||||
double expected = Math.Max(0.0, testValues[i]);
|
||||
Assert.Equal(expected, result[i].Value, Tolerance);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Relu_MathematicalDefinition_Span()
|
||||
{
|
||||
double[] testValues = { -10.0, -5.0, -1.0, -0.5, 0.0, 0.5, 1.0, 5.0, 10.0 };
|
||||
double[] output = new double[testValues.Length];
|
||||
|
||||
Relu.Batch(testValues, output);
|
||||
|
||||
for (int i = 0; i < testValues.Length; i++)
|
||||
{
|
||||
double expected = Math.Max(0.0, testValues[i]);
|
||||
Assert.Equal(expected, output[i], Tolerance);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Relu_Property_NonNegative()
|
||||
{
|
||||
// Property: ReLU output is always >= 0
|
||||
int count = 100;
|
||||
var gbm = new GBM(startPrice: 100, mu: 0.0, sigma: 0.5, seed: 43000);
|
||||
var bars = gbm.Fetch(count, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
var source = Change.Batch(bars.Close);
|
||||
|
||||
var result = Relu.Batch(source);
|
||||
|
||||
for (int i = 0; i < result.Count; i++)
|
||||
{
|
||||
Assert.True(result[i].Value >= 0, $"ReLU output at index {i} should be non-negative");
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Relu_Property_PositivePassthrough()
|
||||
{
|
||||
// Property: For x > 0, ReLU(x) = x
|
||||
double[] positiveValues = { 0.001, 0.1, 1.0, 10.0, 100.0, 1000.0 };
|
||||
double[] output = new double[positiveValues.Length];
|
||||
|
||||
Relu.Batch(positiveValues, output);
|
||||
|
||||
for (int i = 0; i < positiveValues.Length; i++)
|
||||
{
|
||||
Assert.Equal(positiveValues[i], output[i], Tolerance);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Relu_Property_NegativeZero()
|
||||
{
|
||||
// Property: For x < 0, ReLU(x) = 0
|
||||
double[] negativeValues = { -0.001, -0.1, -1.0, -10.0, -100.0, -1000.0 };
|
||||
double[] output = new double[negativeValues.Length];
|
||||
|
||||
Relu.Batch(negativeValues, output);
|
||||
|
||||
for (int i = 0; i < negativeValues.Length; i++)
|
||||
{
|
||||
Assert.Equal(0.0, output[i], Tolerance);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Relu_Property_ZeroAtZero()
|
||||
{
|
||||
// Property: ReLU(0) = 0
|
||||
var indicator = new Relu();
|
||||
indicator.Update(new TValue(DateTime.UtcNow, 0.0));
|
||||
Assert.Equal(0.0, indicator.Last.Value, Tolerance);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Relu_StreamingVsBatch_Consistency()
|
||||
{
|
||||
int count = 100;
|
||||
var gbm = new GBM(startPrice: 100, mu: 0.05, sigma: 0.3, seed: 43001);
|
||||
var bars = gbm.Fetch(count, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
var source = Change.Batch(bars.Close);
|
||||
|
||||
// Streaming
|
||||
var streaming = new Relu();
|
||||
var streamingResults = new double[source.Count];
|
||||
for (int i = 0; i < source.Count; i++)
|
||||
{
|
||||
streaming.Update(source[i]);
|
||||
streamingResults[i] = streaming.Last.Value;
|
||||
}
|
||||
|
||||
// Batch
|
||||
var batch = Relu.Batch(source);
|
||||
|
||||
// Span
|
||||
var spanOutput = new double[source.Count];
|
||||
Relu.Batch(source.Values.ToArray(), spanOutput);
|
||||
|
||||
// All three should match
|
||||
for (int i = 0; i < source.Count; i++)
|
||||
{
|
||||
Assert.Equal(streamingResults[i], batch[i].Value, Tolerance);
|
||||
Assert.Equal(streamingResults[i], spanOutput[i], Tolerance);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user