Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions src/Microsoft.ML.CpuMath/ProbabilityFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,24 @@ public static double Erf(double x)
return x >= 0 ? ev : -ev;
}

#pragma warning disable CA2207 // Here we want lazy allocation for performance purposes
private readonly struct ErfInvSeriesCoefficients
{
public static readonly double[] SeriesCoefficients;

static ErfInvSeriesCoefficients()
{
SeriesCoefficients = new double[1000];
SeriesCoefficients[0] = 1;
for (int k = 1; k < SeriesCoefficients.Length; ++k)
{
for (int m = 0; m < k; ++m)
SeriesCoefficients[k] += SeriesCoefficients[m] * SeriesCoefficients[k - 1 - m] / (m + 1) / (m + m + 1);
}
}
}
#pragma warning restore CA2207

/// <summary>
/// The inverse error function.
/// </summary>
Expand All @@ -71,20 +89,12 @@ public static double Erfinv(double x)
if (x == -1.0)
return Double.NegativeInfinity;

// This is very inefficient... fortunately we only need to compute it very infrequently.
double[] c = new double[1000];
c[0] = 1;
for (int k = 1; k < c.Length; ++k)
{
for (int m = 0; m < k; ++m)
c[k] += c[m] * c[k - 1 - m] / (m + 1) / (m + m + 1);
}

double cc = Math.Sqrt(Math.PI) / 2.0;
double ccinc = Math.PI / 4.0;
double zz = x;
double zzinc = x * x;
double ans = 0.0;
double[] c = ErfInvSeriesCoefficients.SeriesCoefficients;
for (int k = 0; k < c.Length; ++k)
{
ans += c[k] * cc * zz / (2 * k + 1);
Expand Down