Incorporate new unstable rate algo

This commit is contained in:
Andrei Zavatski 2024-02-02 22:28:47 +03:00
parent ef2e2302d4
commit dde7e068a4
2 changed files with 26 additions and 14 deletions

View File

@ -11,7 +11,7 @@ namespace osu.Game.Rulesets.Scoring
public static class HitEventExtensions
{
/// <summary>
/// Calculates the "unstable rate" for a sequence of <see cref="HitEvent"/>s.
/// Calculates the "unstable rate" for a sequence of <see cref="HitEvent"/>s using Welford's online algorithm.
/// </summary>
/// <returns>
/// A non-null <see langword="double"/> value if unstable rate could be calculated,
@ -21,9 +21,30 @@ namespace osu.Game.Rulesets.Scoring
{
Debug.Assert(hitEvents.All(ev => ev.GameplayRate != null));
// Division by gameplay rate is to account for TimeOffset scaling with gameplay rate.
double[] timeOffsets = hitEvents.Where(affectsUnstableRate).Select(ev => ev.TimeOffset / ev.GameplayRate!.Value).ToArray();
return 10 * standardDeviation(timeOffsets);
double currentValue;
int k = 0;
double m = 0;
double s = 0;
double mNext;
foreach (var e in hitEvents)
{
if (!affectsUnstableRate(e))
continue;
// Division by gameplay rate is to account for TimeOffset scaling with gameplay rate.
currentValue = e.TimeOffset / e.GameplayRate!.Value;
k++;
mNext = m + (currentValue - m) / k;
s += (currentValue - m) * (currentValue - mNext);
m = mNext;
}
if (k == 0)
return null;
return 10.0 * Math.Sqrt(s / k);
}
/// <summary>
@ -44,15 +65,5 @@ namespace osu.Game.Rulesets.Scoring
}
private static bool affectsUnstableRate(HitEvent e) => !(e.HitObject.HitWindows is HitWindows.EmptyHitWindows) && e.Result.IsHit();
private static double? standardDeviation(double[] timeOffsets)
{
if (timeOffsets.Length == 0)
return null;
double mean = timeOffsets.Average();
double squares = timeOffsets.Select(offset => Math.Pow(offset - mean, 2)).Sum();
return Math.Sqrt(squares / timeOffsets.Length);
}
}
}

View File

@ -1041,4 +1041,5 @@ private void load()
<s:Boolean x:Key="/Default/UserDictionary/Words/=Unplayed/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Unproxy/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Unranked/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Welford_0027s/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Zoomable/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>