Files
QuanTAlib/lib/errors/Rsquared.cs
T

125 lines
4.5 KiB
C#
Raw Normal View History

2024-10-27 16:11:08 -07:00
using System.Runtime.CompilerServices;
2024-10-11 18:02:09 -07:00
namespace QuanTAlib;
2024-10-27 09:38:53 -07:00
/// <summary>
/// R-squared: Coefficient of Determination
/// A statistical measure that represents the proportion of variance in the dependent
/// variable that is predictable from the independent variable. R-squared provides
/// a measure of how well the predictions approximate the actual data.
/// </summary>
/// <remarks>
/// The R-squared calculation process:
/// 1. Calculates total sum of squares (variance from mean)
/// 2. Calculates residual sum of squares (prediction errors)
/// 3. Computes 1 - (residual SS / total SS)
///
/// Key characteristics:
/// - Range is typically 0 to 1
/// - 1 indicates perfect prediction
/// - 0 indicates prediction no better than mean
/// - Scale-independent
/// - Widely used in regression analysis
///
/// Formula:
/// R² = 1 - (Σ(actual - predicted)² / Σ(actual - mean(actual))²)
///
/// Sources:
/// https://en.wikipedia.org/wiki/Coefficient_of_determination
/// https://www.statisticshowto.com/probability-and-statistics/coefficient-of-determination-r-squared/
///
/// Note: Can be negative if predictions are worse than using the mean
/// </remarks>
2024-10-27 16:11:08 -07:00
[SkipLocalsInit]
public sealed class Rsquared : AbstractBase
2024-10-11 18:02:09 -07:00
{
private readonly CircularBuffer _actualBuffer;
private readonly CircularBuffer _predictedBuffer;
2024-10-27 09:38:53 -07:00
/// <param name="period">The number of points over which to calculate the R-squared value.</param>
/// <exception cref="ArgumentOutOfRangeException">Thrown when period is less than 1.</exception>
2024-10-27 16:11:08 -07:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-10-11 18:02:09 -07:00
public Rsquared(int period)
{
2024-10-13 11:19:27 -07:00
if (period < 1)
2024-10-11 18:02:09 -07:00
{
2024-10-13 11:19:27 -07:00
throw new ArgumentOutOfRangeException(nameof(period), "Period must be greater than or equal to 1.");
2024-10-11 18:02:09 -07:00
}
WarmupPeriod = period;
_actualBuffer = new CircularBuffer(period);
_predictedBuffer = new CircularBuffer(period);
Name = $"Rsquared(period={period})";
Init();
}
2024-10-27 09:38:53 -07:00
/// <param name="source">The data source object that publishes updates.</param>
/// <param name="period">The number of points over which to calculate the R-squared value.</param>
2024-10-27 16:11:08 -07:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-10-11 18:02:09 -07:00
public Rsquared(object source, int period) : this(period)
{
var pubEvent = source.GetType().GetEvent("Pub");
pubEvent?.AddEventHandler(source, new ValueSignal(Sub));
}
2024-10-27 16:11:08 -07:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-10-11 18:02:09 -07:00
public override void Init()
{
base.Init();
_actualBuffer.Clear();
_predictedBuffer.Clear();
}
2024-10-27 16:11:08 -07:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-10-11 18:02:09 -07:00
protected override void ManageState(bool isNew)
{
if (isNew)
{
_lastValidValue = Input.Value;
_index++;
}
}
2024-10-27 16:11:08 -07:00
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
private static (double squaredResidual, double squaredTotal) CalculateSquaredErrors(double actual, double predicted, double meanActual)
{
double deviation = actual - meanActual;
double error = actual - predicted;
return (error * error, deviation * deviation);
}
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
2024-10-11 18:02:09 -07:00
protected override double Calculation()
{
ManageState(Input.IsNew);
double actual = Input.Value;
_actualBuffer.Add(actual, Input.IsNew);
2024-10-27 09:38:53 -07:00
// If no predicted value provided, use mean of actual values
2024-10-11 18:02:09 -07:00
double predicted = double.IsNaN(Input2.Value) ? _actualBuffer.Average() : Input2.Value;
_predictedBuffer.Add(predicted, Input.IsNew);
double rsquared = 0;
2024-10-13 11:19:27 -07:00
if (_actualBuffer.Count > 0)
2024-10-11 18:02:09 -07:00
{
2024-10-27 16:11:08 -07:00
ReadOnlySpan<double> actualValues = _actualBuffer.GetSpan();
ReadOnlySpan<double> predictedValues = _predictedBuffer.GetSpan();
2024-10-11 18:02:09 -07:00
2024-10-27 16:11:08 -07:00
double meanActual = _actualBuffer.Average();
2024-10-13 11:19:27 -07:00
double sumSquaredTotal = 0;
double sumSquaredResidual = 0;
2024-10-11 18:02:09 -07:00
2024-10-27 16:11:08 -07:00
for (int i = 0; i < actualValues.Length; i++)
2024-10-11 18:02:09 -07:00
{
2024-10-27 16:11:08 -07:00
var (squaredResidual, squaredTotal) = CalculateSquaredErrors(actualValues[i], predictedValues[i], meanActual);
sumSquaredResidual += squaredResidual;
sumSquaredTotal += squaredTotal;
2024-10-11 18:02:09 -07:00
}
2024-10-27 16:11:08 -07:00
rsquared = sumSquaredTotal != 0 ? 1 - (sumSquaredResidual / sumSquaredTotal) : 0;
2024-10-11 18:02:09 -07:00
}
IsHot = _index >= WarmupPeriod;
return rsquared;
}
}