Class ErfPrecision

java.lang.Object
nl.tudelft.simulation.jstats.math.ErfPrecision

public final class ErfPrecision extends Object
Erf function precision test.

Copyright (c) 2021-2024 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See for project information DSOL Manual. The DSOL project is distributed under a three-clause BSD-style license, which can be found at DSOL License.

Author:
Alexander Verbraeck
  • Method Summary

    Modifier and Type
    Method
    Description
    (package private) static double
    erf(double z)
    Calculate erf(z), where three different algorithms are used for small, medium, and large values.
    (package private) static double
    erfBig(double z)
    Calculate erf(z) for large values using the Taylor series:
        erf(z) = 1 - (exp(-z2) / √π) Σ [ (-1)n (2n - 1)!! z-(2n + 1) / 2n]
    where the !! operator is the 'double factorial' operator which is (n).(n-2)...8.4.2 for even n, and (n).(n-2)...3.5.1 for odd n.
    (package private) static double
    erfInv(double y)
    Approximates erf-1(p) using a Taylor series.
    The Taylor series for erf-1(z) is:
        erf-1(p) = √π Σ [ 1/2 p + 1/24 π p3 + 7/960 π2 p5 + 127/80640 π3 p7 + ...
    (package private) static double
    erfSmall(double z)
    The Taylor series for erf(z) for abs(z) < 0.5 that is used is:
        erf(z) = (exp(-z2) / √π) Σ [ 2z2n + 1 / (2n + 1)!!]
    where the !! operator is the 'double factorial' operator which is (n).(n-2)...8.4.2 for even n, and (n).(n-2)...3.5.1 for odd n.
    (package private) static double
    erfTaylor(double z)
    Calculate erf(z) using the Taylor series:
        erf(z) = (2/√π) (z - z3/3 + z5/10 - z7/42 + z9/216 - ...)
    The factors are given by https://oeis.org/A007680, which evaluates to a(n) = (2n+1)n!.
    (package private) static double
    inverseErf(double y)
    Calculate based on http://www.naic.edu/~jeffh/inverse_cerf.c code.
    static void
    main(String[] args)
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • erf

      static double erf(double z)
      Calculate erf(z), where three different algorithms are used for small, medium, and large values.
      Parameters:
      z - double; the value to calculate erf(z) for
      Returns:
      double; erf(z)
    • erfSmall

      static double erfSmall(double z)
      The Taylor series for erf(z) for abs(z) < 0.5 that is used is:
          erf(z) = (exp(-z2) / √π) Σ [ 2z2n + 1 / (2n + 1)!!]
      where the !! operator is the 'double factorial' operator which is (n).(n-2)...8.4.2 for even n, and (n).(n-2)...3.5.1 for odd n. See https://mathworld.wolfram.com/Erf.html formula (9) and (10). This function would work well for z < 0.5.
      Parameters:
      z - double; the parameter
      Returns:
      double; erf(x)
    • erfBig

      static double erfBig(double z)
      Calculate erf(z) for large values using the Taylor series:
          erf(z) = 1 - (exp(-z2) / √π) Σ [ (-1)n (2n - 1)!! z-(2n + 1) / 2n]
      where the !! operator is the 'double factorial' operator which is (n).(n-2)...8.4.2 for even n, and (n).(n-2)...3.5.1 for odd n. See https://mathworld.wolfram.com/Erf.html formula (18) to (20). This function would work well for z > 3.7.
      Parameters:
      z - double; the argument
      Returns:
      double; erf(z)
    • erfTaylor

      static double erfTaylor(double z)
      Calculate erf(z) using the Taylor series:
          erf(z) = (2/√π) (z - z3/3 + z5/10 - z7/42 + z9/216 - ...)
      The factors are given by https://oeis.org/A007680, which evaluates to a(n) = (2n+1)n!. See https://en.wikipedia.org/wiki/Error_function. This works pretty well on the interval [0.5,3.7].
      Parameters:
      z - double; the argument
      Returns:
      double; erf(z)
    • erfInv

      static double erfInv(double y)
      Approximates erf-1(p) using a Taylor series.
      The Taylor series for erf-1(z) is:
          erf-1(p) = √π Σ [ 1/2 p + 1/24 π p3 + 7/960 π2 p5 + 127/80640 π3 p7 + ... ]
      See https://mathworld.wolfram.com/InverseErf.html.
      The factors are given by https://oeis.org/A002067 which evaluates to and https://oeis.org/A122551
      Parameters:
      y - double; the cumulative probability to calculate the inverse error function for
      Returns:
      erf-1(y)
    • inverseErf

      static double inverseErf(double y)
      Calculate based on http://www.naic.edu/~jeffh/inverse_cerf.c code.
      Parameters:
      y - double; value to calculate erf-1(y) for
      Returns:
      double; erf-1(x)
    • main

      public static void main(String[] args)
      Parameters:
      args - not used