-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
acbfbe1
commit 4d217b1
Showing
18 changed files
with
482 additions
and
399 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
386 changes: 156 additions & 230 deletions
386
src/Perfolizer/Perfolizer.Tests/Mathematics/SignificanceTesting/MannWhitneyTests.cs
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
src/Perfolizer/Perfolizer/Mathematics/SignificanceTesting/MannWhitney/IMannWhitneyCdf.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace Perfolizer.Mathematics.SignificanceTesting.MannWhitney; | ||
|
||
public interface IMannWhitneyCdf | ||
{ | ||
double Cdf(int n, int m, int u); | ||
} |
49 changes: 49 additions & 0 deletions
49
...izer/Perfolizer/Mathematics/SignificanceTesting/MannWhitney/MannWhitneyClassicExactCdf.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using Perfolizer.Mathematics.Common; | ||
|
||
namespace Perfolizer.Mathematics.SignificanceTesting.MannWhitney; | ||
|
||
public class MannWhitneyClassicExactCdf : IMannWhitneyCdf | ||
{ | ||
public static readonly MannWhitneyClassicExactCdf Instance = new(); | ||
|
||
public double Cdf(int n, int m, int u) | ||
{ | ||
u -= 1; | ||
|
||
int q = (int)Floor(u + 1e-9); | ||
int nm = Max(n, m); | ||
long[,,] w = new long[nm + 1, nm + 1, q + 1]; | ||
for (int i = 0; i <= nm; i++) | ||
for (int j = 0; j <= nm; j++) | ||
for (int k = 0; k <= q; k++) | ||
{ | ||
if (i == 0 || j == 0 || k == 0) | ||
w[i, j, k] = k == 0 ? 1 : 0; | ||
else if (k > i * j) | ||
w[i, j, k] = 0; | ||
else if (i > j) | ||
w[i, j, k] = w[j, i, k]; | ||
else if (j > 0 && k < j) | ||
w[i, j, k] = w[i, k, k]; | ||
else | ||
w[i, j, k] = w[i - 1, j, k - j] + w[i, j - 1, k]; | ||
} | ||
|
||
long denominator = BinomialCoefficientHelper.BinomialCoefficient(n + m, m); | ||
long p = 0; | ||
if (q <= n * m / 2) | ||
{ | ||
for (int i = 0; i <= q; i++) | ||
p += w[n, m, i]; | ||
} | ||
else | ||
{ | ||
q = n * m - q; | ||
for (int i = 0; i < q; i++) | ||
p += w[n, m, i]; | ||
p = denominator - p; | ||
} | ||
|
||
return p * 1.0 / denominator; | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
...r/Perfolizer/Mathematics/SignificanceTesting/MannWhitney/MannWhitneyEdgeworthApproxCdf.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
using Perfolizer.Mathematics.Common; | ||
using Perfolizer.Mathematics.Distributions.ContinuousDistributions; | ||
using Perfolizer.Mathematics.Functions; | ||
|
||
namespace Perfolizer.Mathematics.SignificanceTesting.MannWhitney; | ||
|
||
/// <summary> | ||
/// https://aakinshin.net/posts/mw-edgeworth2/ | ||
/// </summary> | ||
public class MannWhitneyEdgeworthApproxCdf : IMannWhitneyCdf | ||
{ | ||
public static readonly MannWhitneyEdgeworthApproxCdf Instance = new(); | ||
|
||
[SuppressMessage("ReSharper", "InconsistentNaming")] | ||
public double Cdf(int n, int m, int u) | ||
{ | ||
double mu = n * m / 2.0; | ||
double su = Sqrt(n * m * (n + m + 1) / 12.0); | ||
double z = (u - mu - 0.5) / su; | ||
double phi = NormalDistribution.Standard.Pdf(z); | ||
double Phi = NormalDistribution.Standard.Cdf(z); | ||
|
||
double mu2 = n * m * (n + m + 1) / 12.0; | ||
double mu4 = | ||
n * m * (n + m + 1) * | ||
(0 | ||
+ 5 * m * n * (m + n) | ||
- 2 * (m.Pow(2) + n.Pow(2)) | ||
+ 3 * m * n | ||
- 2 * (n + m) | ||
) / 240.0; | ||
|
||
double mu6 = | ||
n * m * (n + m + 1) * | ||
(0 | ||
+ 35 * m.Pow(2) * n.Pow(2) * (m.Pow(2) + n.Pow(2)) | ||
+ 70 * m.Pow(3) * n.Pow(3) | ||
- 42 * m * n * (m.Pow(3) + n.Pow(3)) | ||
- 14 * m.Pow(2) * n.Pow(2) * (n + m) | ||
+ 16 * (n.Pow(4) + m.Pow(4)) | ||
- 52 * n * m * (n.Pow(2) + m.Pow(2)) | ||
- 43 * n.Pow(2) * m.Pow(2) | ||
+ 32 * (m.Pow(3) + n.Pow(3)) | ||
+ 14 * m * n * (n + m) | ||
+ 8 * (n.Pow(2) + m.Pow(2)) | ||
+ 16 * n * m | ||
- 8 * (n + m) | ||
) / 4032.0; | ||
|
||
double e3 = (mu4 / Pow(mu2, 2) - 3) / Factorial(4); | ||
double e5 = (mu6 / Pow(mu2, 3) - 15 * mu4 / Pow(mu2, 2) + 30) / Factorial(6); | ||
double e7 = 35 * Pow((mu4 / Pow(mu2, 2) - 3), 2) / Factorial(8); | ||
|
||
double f3 = -phi * H3(z); | ||
double f5 = -phi * H5(z); | ||
double f7 = -phi * H7(z); | ||
|
||
double edgeworth = Phi + e3 * f3 + e5 * f5 + e7 * f7; | ||
return Min(Max(edgeworth, 0), 1); | ||
|
||
double H3(double x) => x.Pow(3) - 3 * x; | ||
double H5(double x) => x.Pow(5) - 10 * x.Pow(3) + 15 * x; | ||
double H7(double x) => x.Pow(7) - 21 * x.Pow(5) + 105 * x.Pow(3) - 105 * x; | ||
double Factorial(int x) => FactorialFunction.Value(x); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
...zer/Perfolizer/Mathematics/SignificanceTesting/MannWhitney/MannWhitneyLoefflerExactCdf.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using Perfolizer.Mathematics.Common; | ||
|
||
namespace Perfolizer.Mathematics.SignificanceTesting.MannWhitney; | ||
|
||
/// <summary> | ||
/// https://aakinshin.net/posts/mw-loeffler/ | ||
/// </summary> | ||
public class MannWhitneyLoefflerExactCdf : IMannWhitneyCdf | ||
{ | ||
public static readonly MannWhitneyLoefflerExactCdf Instance = new(); | ||
|
||
public double Cdf(int n, int m, int u) | ||
{ | ||
return SumCdf(n, m, u) * 1.0 / BinomialCoefficientHelper.BinomialCoefficient(n + m, m); | ||
} | ||
|
||
private long SumCdf(int n, int m, int u) => u <= 0 ? 0 : FullCdf(n, m, u).Sum(); | ||
|
||
// TODO: support big numbers | ||
// TODO: research the maximum values of n and m that we can handle | ||
private long[] FullCdf(int n, int m, int u) | ||
{ | ||
u -= 1; | ||
|
||
int[] sigma = new int[u + 1]; | ||
for (int d = 1; d <= n; d++) | ||
for (int i = d; i <= u; i += d) | ||
sigma[i] += d; | ||
for (int d = m + 1; d <= m + n; d++) | ||
for (int i = d; i <= u; i += d) | ||
sigma[i] -= d; | ||
|
||
long[] p = new long[u + 1]; | ||
p[0] = 1; | ||
for (int a = 1; a <= u; a++) | ||
{ | ||
for (int i = 0; i < a; i++) | ||
p[a] += p[i] * sigma[a - i]; | ||
p[a] /= a; | ||
} | ||
|
||
return p; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...izer/Perfolizer/Mathematics/SignificanceTesting/MannWhitney/MannWhitneyNormalApproxCdf.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using Perfolizer.Mathematics.Distributions.ContinuousDistributions; | ||
|
||
namespace Perfolizer.Mathematics.SignificanceTesting.MannWhitney; | ||
|
||
public class MannWhitneyNormalApproxCdf : IMannWhitneyCdf | ||
{ | ||
public static readonly MannWhitneyNormalApproxCdf Instance = new(); | ||
|
||
public double Cdf(int n, int m, int u) | ||
{ | ||
double mu = n * m / 2.0; | ||
double su = Sqrt(n * m * (n + m + 1) / 12.0); | ||
double z = (u - mu) / su; | ||
return NormalDistribution.Gauss(z); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/Perfolizer/Perfolizer/Mathematics/SignificanceTesting/MannWhitney/MannWhitneyStrategy.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace Perfolizer.Mathematics.SignificanceTesting.MannWhitney; | ||
|
||
public enum MannWhitneyStrategy | ||
{ | ||
Auto, | ||
ClassicExact, | ||
LoefflerExact, | ||
NormalApprox, | ||
EdgeworthApprox | ||
} |
Oops, something went wrong.