Skip to content

Commit

Permalink
Misc updates
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreyAkinshin committed Mar 8, 2024
1 parent 3483cbf commit 343750e
Show file tree
Hide file tree
Showing 35 changed files with 282 additions and 212 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -437,8 +437,8 @@ public void MannWhitneyEquivalenceTest04()
[AssertionMethod]
private void CheckEquivalence(Sample x, Sample y, Threshold t, bool expected = true)
{
var test = new TostEquivalenceTest<MannWhitneyResult>(MannWhitneyTest.Instance);
bool actual = test.AreEquivalent(x, y, t, SignificanceLevel.P1E5);
var test = new SimpleEquivalenceTest(MannWhitneyTest.Instance);
bool actual = test.Perform(x, y, t, SignificanceLevel.P1E5) == ComparisonResult.Indistinguishable;
Assert.Equal(expected, actual);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Perfolizer.Mathematics.Common;
using Perfolizer.Mathematics.SignificanceTesting;
using Perfolizer.Mathematics.SignificanceTesting.MannWhitney;
using Perfolizer.Metrology;

namespace Perfolizer.Tests.Mathematics.SignificanceTesting;

public class SimpleEquivalenceTests
{
[Theory]
[InlineData("[1,2,3,4,5,6,7,8,9,10]", "[1,2,3,4,5,6,7,8,9,10]", "1", ComparisonResult.Indistinguishable)]
[InlineData("[195,195,196,196]", "[200.3279,200.3178,200.4046]", "1", ComparisonResult.Indistinguishable)]
[InlineData(
"[0.819,0.62,-0.742,0.572,1.43,-0.272,-0.34]",
"[-0.539,-0.311,-0.174,1.186,-1.455,1.021,0.021]",
"0.1", ComparisonResult.Indistinguishable)]
[InlineData(
"[200.3279, 200.3178, 200.4046, 200.3279, 200.3178, 200.4046, 200.3279, 200.3178, 200.4046, 200.3279, 200.3178, 200.4046]",
"[195, 196, 195, 196, 195, 196, 195, 196, 195, 196, 195, 196, 195, 196, 195, 196, 195, 196, 195, 196]",
"2%", ComparisonResult.Greater)]
[InlineData(
"[10070400,10073300,10073500]",
"[9.7031,9.2344,10.1719,8.6094,9.5469,8.1406,8.6094,8.4531,7.9844,7.2031,8.1406,8.6094,9.0781,8.9219,9.2344,8.9219]",
"2%", ComparisonResult.Greater)]
public void TostTest(string x, string y, string threshold, ComparisonResult expected)
{
var test = new SimpleEquivalenceTest(MannWhitneyTest.Instance);
var actual = test.Perform(Sample.Parse(x), Sample.Parse(y), Threshold.Parse(threshold), SignificanceLevel.P1E5);
Assert.Equal(expected, actual);
}
}

This file was deleted.

14 changes: 14 additions & 0 deletions src/Perfolizer/Perfolizer.Tests/SampleTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace Perfolizer.Tests;

public class SampleTests
{
[Theory]
[InlineData("[1,2,3]ms", "[4]s", "[1,2,3,4000]ms")]
[InlineData("[2000]KB", "[4]B", "[2048000,4]B")]
public void SampleConcatTest(string a, string b, string c)
{
string actual = Sample.Parse(a).Concat(Sample.Parse(b)).ToString();
string expected = Sample.Parse(c).ToString();
Assert.Equal(expected, actual);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
using Perfolizer.Metrology;

namespace Perfolizer.Exceptions;

public class InvalidMeasurementUnitExceptions(MeasurementUnit expected, MeasurementUnit actual)
: InvalidOperationException($"Invalid measurement unit: expected {expected}, but was {actual}");
15 changes: 8 additions & 7 deletions src/Perfolizer/Perfolizer/Horology/Frequency.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
using System.Globalization;
using JetBrains.Annotations;
using Perfolizer.Common;
using Perfolizer.Exceptions;
using Perfolizer.Metrology;

namespace Perfolizer.Horology;

[PublicAPI]
public readonly struct Frequency(double hertz)
: IEquatable<Frequency>, IComparable<Frequency>, IApplicableMeasurementUnit
: IEquatable<Frequency>, IComparable<Frequency>, IAbsoluteMeasurementValue
{
private const string DefaultFormat = "G";

Expand Down Expand Up @@ -100,13 +101,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 Sample? Apply(Sample sample)
public MeasurementUnit Unit => FrequencyUnit.Hz;

public double GetShift(Sample sample)
{
var sampleUnit = sample.MeasurementUnit;
if (sampleUnit is not FrequencyUnit frequencyUnit)
return null;
double shift = FrequencyUnit.Convert(Hertz, FrequencyUnit.Hz, frequencyUnit);
return MeasurementValueHelper.Apply(sample, x => x + shift);
if (sample.Unit is not FrequencyUnit frequencyUnit)
throw new InvalidMeasurementUnitExceptions(Unit, sample.Unit);
return FrequencyUnit.Convert(Hertz, FrequencyUnit.Hz, frequencyUnit);
}

public override string ToString() => ToString(DefaultFormat);
Expand Down
2 changes: 1 addition & 1 deletion src/Perfolizer/Perfolizer/Horology/FrequencyUnit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class FrequencyUnit(string abbreviation, string fullName, long baseUnits)
public static readonly FrequencyUnit KHz = new("KHz", "Kilohertz", 1000);
public static readonly FrequencyUnit MHz = new("MHz", "Megahertz", 1000.PowInt(2));
public static readonly FrequencyUnit GHz = new("GHz", "Gigahertz", 1000.PowInt(3));
public static readonly FrequencyUnit[] All = { Hz, KHz, MHz, GHz };
public static readonly FrequencyUnit[] All = [Hz, KHz, MHz, GHz];

public Frequency ToFrequency(long value = 1) => new(value, this);

Expand Down
16 changes: 8 additions & 8 deletions src/Perfolizer/Perfolizer/Horology/TimeInterval.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using System.Diagnostics.CodeAnalysis;
using JetBrains.Annotations;
using Perfolizer.Common;
using Perfolizer.Exceptions;
using Perfolizer.Metrology;

namespace Perfolizer.Horology;

public readonly struct TimeInterval(double nanoseconds)
: IEquatable<TimeInterval>, IComparable<TimeInterval>, IApplicableMeasurementUnit
: IEquatable<TimeInterval>, IComparable<TimeInterval>, IAbsoluteMeasurementValue
{
private const string DefaultFormat = "0.####";

Expand Down Expand Up @@ -90,12 +90,12 @@ public bool Equals(TimeInterval other, double nanosecondEpsilon) =>
public override bool Equals([NotNullWhen(true)] object? obj) => obj is TimeInterval other && Equals(other);
public override int GetHashCode() => Nanoseconds.GetHashCode();

public Sample? Apply(Sample sample)
public MeasurementUnit Unit => TimeUnit.Nanosecond;

public double GetShift(Sample sample)
{
var sampleUnit = sample.MeasurementUnit;
if (sampleUnit is not TimeUnit timeUnit)
return null;
double shift = TimeUnit.Convert(Nanoseconds, TimeUnit.Nanosecond, timeUnit);
return MeasurementValueHelper.Apply(sample, x => x + shift);
if (sample.Unit is not TimeUnit timeUnit)
throw new InvalidMeasurementUnitExceptions(Unit, sample.Unit);
return TimeUnit.Convert(Nanoseconds, TimeUnit.Nanosecond, timeUnit);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ namespace Perfolizer.Mathematics.Common;
public enum ComparisonResult
{
Greater,
Equivalent,
Indistinguishable,
Lesser
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace Perfolizer.Mathematics.Common;

internal static class MathExtensions
{
public static double Abs(this double x) => Math.Abs(x);
public static double Sqr(this double x) => x * x;
public static double Sqrt(this double x) => Math.Sqrt(x);
public static double Pow(this int x, double k) => Math.Pow(x, k);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public static double Estimate(
int k = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
values[k++] = func(x.Values[j], y.Values[i]);
values[k++] = func(x.Values[i], y.Values[j]);
return estimator.Quantile(new Sample(values), p);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using Perfolizer.Common;
using Perfolizer.Mathematics.Functions;
using Perfolizer.Mathematics.GenericEstimators;
using Perfolizer.Mathematics.ScaleEstimators;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using Perfolizer.Common;

namespace Perfolizer.Mathematics.EffectSizes;

public interface IEffectSizeEstimator
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using Perfolizer.Common;
using Perfolizer.Mathematics.Common;
using Perfolizer.Metrology;

namespace Perfolizer.Mathematics.SignificanceTesting.Base;

public interface IEquivalenceTest
{
bool AreEquivalent(Sample x, Sample y, Threshold threshold, SignificanceLevel alpha);
ComparisonResult Perform(Sample x, Sample y, Threshold threshold, SignificanceLevel alpha);
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Perfolizer.Mathematics.Common;
using Perfolizer.Mathematics.GenericEstimators;
using Perfolizer.Mathematics.SignificanceTesting.Base;
using Perfolizer.Metrology;

namespace Perfolizer.Mathematics.SignificanceTesting;

// TODO: replace the dummy implementation with a reliable one
public class SimpleEquivalenceTest(ISignificanceTwoSampleTest oneSidedTest) : IEquivalenceTest
{
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));

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

// Statistical significance (TOST)
const AlternativeHypothesis alternative = AlternativeHypothesis.Greater;
var greaterPValue = oneSidedTest.GetPValue(x, y, alternative, threshold);
var lesserPValue = oneSidedTest.GetPValue(y, x, alternative, threshold);

var comparisionResult = ComparisonResult.Indistinguishable;
if (greaterPValue < alpha)
comparisionResult = ComparisonResult.Greater;
else if (lesserPValue < alpha)
comparisionResult = ComparisonResult.Lesser;

return comparisionResult;
}
}

This file was deleted.

11 changes: 4 additions & 7 deletions src/Perfolizer/Perfolizer/Metrology/EffectSizeValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Perfolizer.Metrology;

public readonly struct EffectSizeValue(double value) : IApplicableMeasurementUnit
public readonly struct EffectSizeValue(double value) : IAbsoluteMeasurementValue
{
private const string DefaultFormat = "G";
[PublicAPI] public double Value { get; } = value;
Expand All @@ -20,10 +20,7 @@ public string ToString(
return measurementValue.ToString(format, formatProvider, unitPresentation);
}

public Sample Apply(Sample sample)
{
double scale = ShamosEstimator.Instance.Scale(sample);
double shift = scale * Value;
return MeasurementValueHelper.Apply(sample, x => x + shift);
}
public MeasurementUnit Unit => EffectSizeUnit.Instance;

public double GetShift(Sample sample) => ShamosEstimator.Instance.Scale(sample) * Value;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Perfolizer.Metrology;

public interface IAbsoluteMeasurementValue : ISpecificMeasurementValue
{
double GetShift(Sample sample);
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Perfolizer.Metrology;

public interface IRelativeMeasurementValue : ISpecificMeasurementValue
{
double GetRatio();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace Perfolizer.Metrology;

public interface ISpecificMeasurementValue : IWithUnits
{
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace Perfolizer.Metrology;

public interface IFormattableUnit
public interface IWithUnits
{
MeasurementUnit Unit { get; }
string ToString(string? format, IFormatProvider? formatProvider = null, UnitPresentation? unitPresentation = null);
}
1 change: 1 addition & 0 deletions src/Perfolizer/Perfolizer/Metrology/MeasurementUnit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public static IEnumerable<MeasurementUnit> GetAll()
public long BaseUnits { get; } = baseUnits;

public string AbbreviationAscii => Abbreviation.ConvertToAscii();
public virtual string GetFlavor() => GetType().Name.Replace("Unit", "");

public override string ToString() => Abbreviation;

Expand Down
Loading

0 comments on commit 343750e

Please sign in to comment.