2024-10-27 16:11:08 -07:00
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Runtime.CompilerServices;
|
2024-09-22 17:31:24 -07:00
|
|
|
namespace QuanTAlib;
|
|
|
|
|
|
2024-10-05 15:20:13 -07:00
|
|
|
/// <summary>
|
2024-10-27 09:38:53 -07:00
|
|
|
/// MODE: Most Frequent Value Measure
|
|
|
|
|
/// A statistical measure that identifies the most frequently occurring value(s)
|
|
|
|
|
/// in a dataset. When multiple values share the highest frequency, it returns
|
|
|
|
|
/// their average to provide a representative central value.
|
2024-10-05 15:20:13 -07:00
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
2024-10-27 09:38:53 -07:00
|
|
|
/// The Mode calculation process:
|
|
|
|
|
/// 1. Groups values by frequency
|
|
|
|
|
/// 2. Identifies highest frequency group(s)
|
|
|
|
|
/// 3. Averages multiple modes if present
|
|
|
|
|
/// 4. Uses mean until period filled
|
2024-10-11 18:02:09 -07:00
|
|
|
///
|
2024-10-27 09:38:53 -07:00
|
|
|
/// Key characteristics:
|
|
|
|
|
/// - Identifies most common values
|
|
|
|
|
/// - Handles multiple modes
|
|
|
|
|
/// - Robust to distribution shape
|
|
|
|
|
/// - Useful for discrete data
|
|
|
|
|
/// - Returns actual data points
|
|
|
|
|
///
|
|
|
|
|
/// Formula:
|
|
|
|
|
/// mode = value with highest frequency count
|
|
|
|
|
/// if multiple modes: average of mode values
|
|
|
|
|
///
|
|
|
|
|
/// Market Applications:
|
|
|
|
|
/// - Identify common price levels
|
|
|
|
|
/// - Detect support/resistance zones
|
|
|
|
|
/// - Analyze volume clusters
|
|
|
|
|
/// - Find price congestion areas
|
|
|
|
|
/// - Pattern recognition
|
|
|
|
|
///
|
|
|
|
|
/// Sources:
|
|
|
|
|
/// https://en.wikipedia.org/wiki/Mode_(statistics)
|
|
|
|
|
/// "Statistical Analysis in Financial Markets"
|
|
|
|
|
///
|
|
|
|
|
/// Note: Particularly useful for price level analysis
|
2024-10-05 15:20:13 -07:00
|
|
|
/// </remarks>
|
2024-10-27 16:11:08 -07:00
|
|
|
[SkipLocalsInit]
|
|
|
|
|
public sealed class Mode : AbstractBase
|
2024-10-06 06:59:26 +00:00
|
|
|
{
|
2024-09-30 15:53:48 -07:00
|
|
|
private readonly int Period;
|
2024-09-23 22:08:40 -07:00
|
|
|
private readonly CircularBuffer _buffer;
|
2024-10-27 16:11:08 -07:00
|
|
|
private readonly Dictionary<double, int> _frequencies;
|
|
|
|
|
private readonly List<double> _modes;
|
|
|
|
|
private const double Epsilon = 1e-10;
|
2024-09-22 17:31:24 -07:00
|
|
|
|
2024-10-27 09:38:53 -07:00
|
|
|
/// <param name="period">The number of points to consider for mode calculation.</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-06 14:44:43 -07:00
|
|
|
public Mode(int period)
|
2024-10-06 06:59:26 +00:00
|
|
|
{
|
|
|
|
|
if (period < 1)
|
|
|
|
|
{
|
2024-09-22 17:31:24 -07:00
|
|
|
throw new ArgumentOutOfRangeException(nameof(period), "Period must be greater than or equal to 1.");
|
|
|
|
|
}
|
|
|
|
|
Period = period;
|
|
|
|
|
WarmupPeriod = period;
|
|
|
|
|
_buffer = new CircularBuffer(period);
|
2024-10-27 16:11:08 -07:00
|
|
|
_frequencies = new Dictionary<double, int>();
|
|
|
|
|
_modes = new List<double>();
|
2024-09-22 17:31:24 -07:00
|
|
|
Name = $"Mode(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 to consider for mode calculation.</param>
|
2024-10-27 16:11:08 -07:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2024-10-06 06:59:26 +00:00
|
|
|
public Mode(object source, int period) : this(period)
|
|
|
|
|
{
|
2024-09-22 17:31:24 -07:00
|
|
|
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();
|
|
|
|
|
_buffer.Clear();
|
2024-10-27 16:11:08 -07:00
|
|
|
_frequencies.Clear();
|
|
|
|
|
_modes.Clear();
|
2024-10-11 18:02:09 -07:00
|
|
|
}
|
|
|
|
|
|
2024-10-27 16:11:08 -07:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2024-10-06 06:59:26 +00:00
|
|
|
protected override void ManageState(bool isNew)
|
|
|
|
|
{
|
|
|
|
|
if (isNew)
|
|
|
|
|
{
|
2024-09-22 17:31:24 -07:00
|
|
|
_lastValidValue = Input.Value;
|
|
|
|
|
_index++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-27 16:11:08 -07:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
|
|
|
|
|
private void CountFrequencies(ReadOnlySpan<double> values)
|
|
|
|
|
{
|
|
|
|
|
_frequencies.Clear();
|
|
|
|
|
for (int i = 0; i < values.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
_frequencies[values[i]] = _frequencies.TryGetValue(values[i], out int count) ? count + 1 : 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
|
|
|
|
|
private void FindModes()
|
|
|
|
|
{
|
|
|
|
|
_modes.Clear();
|
|
|
|
|
int maxCount = 0;
|
|
|
|
|
|
|
|
|
|
foreach (var kvp in _frequencies)
|
|
|
|
|
{
|
|
|
|
|
if (kvp.Value > maxCount)
|
|
|
|
|
{
|
|
|
|
|
maxCount = kvp.Value;
|
|
|
|
|
_modes.Clear();
|
|
|
|
|
_modes.Add(kvp.Key);
|
|
|
|
|
}
|
|
|
|
|
else if (kvp.Value == maxCount)
|
|
|
|
|
{
|
|
|
|
|
_modes.Add(kvp.Key);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
|
|
|
|
|
private double CalculateAverageMode()
|
|
|
|
|
{
|
|
|
|
|
double sum = 0;
|
|
|
|
|
for (int i = 0; i < _modes.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
sum += _modes[i];
|
|
|
|
|
}
|
|
|
|
|
return sum / _modes.Count;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
|
2024-10-06 06:59:26 +00:00
|
|
|
protected override double Calculation()
|
|
|
|
|
{
|
2024-09-22 17:31:24 -07:00
|
|
|
ManageState(Input.IsNew);
|
|
|
|
|
_buffer.Add(Input.Value, Input.IsNew);
|
|
|
|
|
|
|
|
|
|
double mode;
|
2024-10-06 06:59:26 +00:00
|
|
|
if (_index >= Period)
|
|
|
|
|
{
|
2024-10-27 16:11:08 -07:00
|
|
|
ReadOnlySpan<double> values = _buffer.GetSpan();
|
|
|
|
|
CountFrequencies(values);
|
|
|
|
|
FindModes();
|
|
|
|
|
mode = CalculateAverageMode();
|
2024-10-06 06:59:26 +00:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-10-27 09:38:53 -07:00
|
|
|
// Use average until we have enough data points
|
|
|
|
|
mode = _buffer.Average();
|
2024-09-22 17:31:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IsHot = _index >= WarmupPeriod;
|
|
|
|
|
return mode;
|
|
|
|
|
}
|
|
|
|
|
}
|