using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace Brutal.Concurrency.Statistics;
///
/// Provides a moving average over a fixed window of samples.
///
/// The underlying sample type
public class MovingAverage
where TNumber : INumber,
IDivisionOperators,
IModulusOperators
{
///
/// Create a moving average sampler with a fixed window size.
///
/// The maximum number of samples to compute the average over
/// Thrown if maxWindowSampleCount <= 0
public MovingAverage(uint maxWindowSampleCount)
{
if (maxWindowSampleCount <= 0)
throw new ArgumentOutOfRangeException(
nameof(maxWindowSampleCount),
"Must be greater than 0"
);
_maxWindowSampleCount = maxWindowSampleCount;
_currentWindowSampleCount = 0;
_values = new TNumber[maxWindowSampleCount];
}
///
/// Clears all samples from the moving average.
///
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
public void Reset()
{
Array.Clear(_values);
_index = 0;
_currentWindowSampleCount = 0;
_sum = TNumber.Zero;
}
///
/// Adds a sample to the moving average window and returns the updated average.
///
///
///
public TNumber Update(TNumber sample)
{
_sum = _sum - _values[_index] + sample;
_values[_index] = sample;
_index = (_index + 1) % _maxWindowSampleCount;
_currentWindowSampleCount = Math.Min(_currentWindowSampleCount + 1, _maxWindowSampleCount);
return _sum / TNumber.CreateTruncating(_currentWindowSampleCount);
}
///
/// Returns the number of samples currently in the moving average window.
///
///
/// This is not the total number of samples added, but the number that are
/// being considered for the moving average. i.e. Up until max window sample count is reached,
/// this will be equal to the number of samples added, but once the max window sample count is reached
/// this value will be equal to the max window sample count.
///
/// The number of samples in the moving average window.
public uint SampleCount => _currentWindowSampleCount;
///
/// Returns the current average of all samples, or TNumber.Zero if no samples have been added.
///
public TNumber CurrentAverage =>
_currentWindowSampleCount == 0
? TNumber.Zero
: _sum / TNumber.CreateTruncating(_currentWindowSampleCount);
private readonly uint _maxWindowSampleCount;
private uint _currentWindowSampleCount;
private readonly TNumber[] _values;
private uint _index;
private TNumber _sum = TNumber.Zero;
}