Skip to content

Commit

Permalink
Introduce EffecticeShift
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreyAkinshin committed Mar 8, 2024
1 parent 1ed6de0 commit 37cc04d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 28 deletions.
25 changes: 12 additions & 13 deletions src/Perfolizer/Perfolizer/Horology/Frequency.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@ public readonly struct Frequency(double hertz)

[PublicAPI] public double Hertz { get; } = hertz;

[PublicAPI] public Frequency(double value, FrequencyUnit unit) : this(value * unit.BaseUnits)
{
}
[PublicAPI] public Frequency(double value, FrequencyUnit unit) : this(value * unit.BaseUnits) { }

[PublicAPI] public static readonly Frequency Zero = new(0);
[PublicAPI] public static readonly Frequency Zero = new (0);
[PublicAPI] public static readonly Frequency Hz = FrequencyUnit.Hz.ToFrequency();
[PublicAPI] public static readonly Frequency KHz = FrequencyUnit.KHz.ToFrequency();
[PublicAPI] public static readonly Frequency MHz = FrequencyUnit.MHz.ToFrequency();
Expand All @@ -37,16 +35,16 @@ [PublicAPI] public Frequency(double value, FrequencyUnit unit) : this(value * un
[PublicAPI] public static Frequency FromMHz(double value) => MHz * value;
[PublicAPI] public static Frequency FromGHz(double value) => GHz * value;

[PublicAPI] public static implicit operator Frequency(double value) => new(value);
[PublicAPI] public static implicit operator Frequency(double value) => new (value);
[PublicAPI] public static implicit operator double(Frequency property) => property.Hertz;

[PublicAPI] public static double operator /(Frequency a, Frequency b) => 1.0 * a.Hertz / b.Hertz;
[PublicAPI] public static Frequency operator /(Frequency a, double k) => new(a.Hertz / k);
[PublicAPI] public static Frequency operator /(Frequency a, int k) => new(a.Hertz / k);
[PublicAPI] public static Frequency operator *(Frequency a, double k) => new(a.Hertz * k);
[PublicAPI] public static Frequency operator *(Frequency a, int k) => new(a.Hertz * k);
[PublicAPI] public static Frequency operator *(double k, Frequency a) => new(a.Hertz * k);
[PublicAPI] public static Frequency operator *(int k, Frequency a) => new(a.Hertz * k);
[PublicAPI] public static Frequency operator /(Frequency a, double k) => new (a.Hertz / k);
[PublicAPI] public static Frequency operator /(Frequency a, int k) => new (a.Hertz / k);
[PublicAPI] public static Frequency operator *(Frequency a, double k) => new (a.Hertz * k);
[PublicAPI] public static Frequency operator *(Frequency a, int k) => new (a.Hertz * k);
[PublicAPI] public static Frequency operator *(double k, Frequency a) => new (a.Hertz * k);
[PublicAPI] public static Frequency operator *(int k, Frequency a) => new (a.Hertz * k);
[PublicAPI] public static bool operator <(Frequency a, Frequency b) => a.Hertz < b.Hertz;
[PublicAPI] public static bool operator >(Frequency a, Frequency b) => a.Hertz > b.Hertz;
[PublicAPI] public static bool operator <=(Frequency a, Frequency b) => a.Hertz <= b.Hertz;
Expand Down Expand Up @@ -101,12 +99,13 @@ public static bool TryParseGHz([NotNullWhen(true)] string? s, NumberStyles numbe
IFormatProvider formatProvider, out Frequency freq)
=> TryParse(s, FrequencyUnit.GHz, numberStyle, formatProvider, out freq);

public MeasurementUnit Unit => FrequencyUnit.Hz;
// Explicit implementation allows keeping backward compatibility with the current serialization format of BenchmarkDotNet
MeasurementUnit IWithUnits.Unit => FrequencyUnit.Hz;

public double GetShift(Sample sample)
{
if (sample.Unit is not FrequencyUnit frequencyUnit)
throw new InvalidMeasurementUnitExceptions(Unit, sample.Unit);
throw new InvalidMeasurementUnitExceptions(FrequencyUnit.Hz, sample.Unit);
return FrequencyUnit.Convert(Hertz, FrequencyUnit.Hz, frequencyUnit);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ public class SimpleEquivalenceTest(ISignificanceTwoSampleTest oneSidedTest) : IE
public ComparisonResult Perform(Sample x, Sample y, Threshold threshold, SignificanceLevel alpha)
{
var deltas = DeltasEstimator.HodgesLehmannShamos.Deltas(x, y);
double thresholdShift = Max(threshold.GetMaxShift(x), threshold.GetMaxShift(y));
double thresholdShift = Max(threshold.EffectiveShift(x), threshold.EffectiveShift(y));

// Practical significance
if (deltas.Shift.Abs() > thresholdShift * 10)
if (thresholdShift > 0 && deltas.Shift.Abs() > thresholdShift * 10)
return deltas.Shift > 0 ? ComparisonResult.Greater : ComparisonResult.Lesser;

// Statistical significance (TOST)
Expand Down
27 changes: 14 additions & 13 deletions src/Perfolizer/Perfolizer/Metrology/Threshold.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Text;
using JetBrains.Annotations;
using Perfolizer.Collections;
using Perfolizer.Mathematics.GenericEstimators;

namespace Perfolizer.Metrology;

Expand All @@ -9,7 +10,7 @@ namespace Perfolizer.Metrology;
/// </summary>
public class Threshold(params ISpecificMeasurementValue[] thresholdValues) : IWithUnits, IEquatable<Threshold>
{
public static readonly Threshold Zero = new();
public static readonly Threshold Zero = new ();

private const char Separator = '|';

Expand All @@ -36,18 +37,18 @@ public Sample ApplyMax(Sample sample)
: new Sample(values, sample.Unit);
}

public IEnumerable<double> GetShifts(Sample sample) => thresholdValues
.OfType<IAbsoluteMeasurementValue>()
.Select(value => value.GetShift(sample));

public IEnumerable<double> GetRatios() => thresholdValues
.OfType<IRelativeMeasurementValue>()
.Select(value => value.GetRatio());

public double GetMaxShift(Sample sample, double defaultShift = 0) =>
GetShifts(sample).DefaultIfEmpty(defaultShift).Max();

public double GetMaxRatio(double defaultRatio = 1.0) => GetRatios().DefaultIfEmpty(defaultRatio).Max();
public double EffectiveShift(Sample sample)
{
var basicShifts = thresholdValues
.OfType<IAbsoluteMeasurementValue>()
.Select(value => value.GetShift(sample));
var ratioShifts = thresholdValues
.OfType<IRelativeMeasurementValue>()
.Select(value => value.Apply(sample))
.WhereNotNull()
.Select(sample2 => HodgesLehmannEstimator.Instance.Shift(sample2, sample));
return basicShifts.Concat(ratioShifts).DefaultIfEmpty(0).Max();
}

public override string ToString() => presentation;
public MeasurementUnit Unit => throw new NotSupportedException("Threshold does not have a measurement unit");
Expand Down

0 comments on commit 37cc04d

Please sign in to comment.