PINE LIBRARY
Telah dikemas kini

ErrorFunctions

90
Library "ErrorFunctions"
A collection of functions used to approximate the area beneath a Gaussian curve.

Because an ERF (Error Function) is an integral, there is no closed-form solution to calculating the area beneath the curve. Meaning all ERFs are approximations; precisely wrong, but mostly accurate. How close you need to get to the actual area depends entirely on your use case, with more precision being less efficient.

The internal precision of floats in Pine Script is 1e-16 (16 decimals, aka. double precision). This library adapts well known algorithms designed to efficiently reach double precision. Single precision alternates are also included. All of them were made free to use, modify, and distribute by their original authors.

HASTINGS
Adaptation of a single precision ERF by Cecil Hastings Jr, published through Princeton University in 1955. It was later documented by Abramowitz and Stegun as equation 7.1.26 in their 1972 Handbook of Mathematical Functions. Fast, efficient, and ideal when precision beyond a few decimals is unnecessary.

GILES
Adaptation of a single precision Inverse ERF by Michael Giles, published through the University of Oxford in 2012. It reverses the ERF, estimating an X coordinate from an area. It too is fast, efficient, and ideal when precision beyond a few decimals is unnecessary.

LIBC
Adaptation of the double precision ERF & ERFC in the standard C library (aka. libc). It is also the same ERF & ERFC that SciPy uses. While not quite as efficient as the Hastings approximation, it's still very fast and fully maximizes Pines precision.

BOOST
Adaptation of the double precision Inverse ERF & Inverse ERFC in the Boost Math C++ library. SciPy uses these as well. These reverse the ERF & ERFC, estimating an X coordinate from an area. It too isn't quite as efficient as the Giles approximation, but still fast and fully maximizes Pines precision.

While these algorithms are not exported directly, they are available through their exported counterparts.

- - -

ERROR FUNCTIONS

erf(x, precise)
  An Error Function estimates the theoretical error of a measurement.
  Parameters:
    x (float): (float) Upper limit of the integration.
    precise (bool): Double precision (true) or single precision (false).
  Returns: (float) Between -1 and 1.

erfc(x, precise)
  A Complementary Error Function estimates the difference between a theoretical error and infinity.
  Parameters:
    x (float): (float) Lower limit of the integration.
    precise (bool): Double precision (true) or single precision (false).
  Returns: (float) Between 0 and 2.

erfinv(x, precise)
  An Inverse Error Function reverses the erf() by estimating the original measurement from the theoretical error.
  Parameters:
    x (float): (float) Theoretical error.
    precise (bool): Double precision (true) or single precision (false).
  Returns: (float) Between 0 and ± infinity.

erfcinv(x, precise)
  An Inverse Complementary Error Function reverses the erfc() by estimating the original measurement from the difference between the theoretical error and infinity.
  Parameters:
    x (float): (float) Difference between the theoretical error and infinity.
    precise (bool): Double precision (true) or single precision (false).
  Returns: (float) Between 0 and ± infinity.

- - -

DISTRIBUTION FUNCTIONS

pdf(x, m, s)
  A Probability Density Function estimates the probability density. For clarity, density is not a probability.
  Parameters:
    x (float): (float) X coordinate for which a density will be estimated.
    m (float): (float) Mean
    s (float): (float) Sigma
  Returns: (float) Between 0 and ∞.

cdf(z, precise)
  A Cumulative Distribution Function estimates the area under a Gaussian curve between negative infinity and the Z Score.
  Parameters:
    z (float): (float) Z Score.
    precise (bool): Double precision (true) or single precision (false).
  Returns: (float) Between 0 and 1.

cdfinv(a, precise)
  An Inverse Cumulative Distribution Function reverses the cdf() by estimating the Z Score from an area.
  Parameters:
    a (float): (float) Area between 0 and 1.
    precise (bool): Double precision (true) or single precision (false).
  Returns: (float) Between -∞ and +∞

cdfab(z1, z2, precise)
  A Cumulative Distribution Function from A to B estimates the area under a Gaussian curve between two Z Scores (A and B).
  Parameters:
    z1 (float): (float) First Z Score.
    z2 (float): (float) Second Z Score.
    precise (bool): Double precision (true) or single precision (false).
  Returns: (float) Between 0 and 1.

ttt(z, precise)
  A Two-Tailed Test estimates the area under a Gaussian curve between symmetrical ± Z scores and ± infinity.
  Parameters:
    z (float): (float) One of the symmetrical Z Scores.
    precise (bool): Double precision (true) or single precision (false).
  Returns: (float) Between 0 and 1.

tttinv(a, precise)
  An Inverse Two-Tailed Test reverses the ttt() by estimating the absolute Z Score from an area.
  Parameters:
    a (float): (float) Area between 0 and 1.
    precise (bool): Double precision (true) or single precision (false).
  Returns: (float) Between 0 and ∞.

ott(z, precise)
  A One-Tailed Test estimates the area under a Gaussian curve between an absolute Z Score and infinity.
  Parameters:
    z (float): (float) Z Score.
    precise (bool): Double precision (true) or single precision (false).
  Returns: (float) Between 0 and 1.

ottinv(a, precise)
  An Inverse One-Tailed Test Reverses the ott() by estimating the Z Score from a an area.
  Parameters:
    a (float): (float) Area between 0 and 1.
    precise (bool): Double precision (true) or single precision (false).
  Returns: (float) Between 0 and ∞.
Nota Keluaran
v2
Removed thumbnail code form library.
Nota Keluaran
v3
Updated double precision ERF to remain within Pine Scripts precision.

Penafian

Maklumat dan penerbitan adalah tidak dimaksudkan untuk menjadi, dan tidak membentuk, nasihat untuk kewangan, pelaburan, perdagangan dan jenis-jenis lain atau cadangan yang dibekalkan atau disahkan oleh TradingView. Baca dengan lebih lanjut di Terma Penggunaan.