MCPcopy Create free account
hub / github.com/cortex-js/compute-engine / erfInv

Function erfInv

src/compute-engine/numerics/special-functions.ts:332–359  ·  view source on GitHub ↗
(x: number)

Source from the content-addressed store, hash-verified

330
331/**
332 * Winitzki's approximation for the inverse error function, accurate to
333 * ~2e-3 relative over (-1, 1). Used as the Newton seed for `erfInv()` and
334 * `bigErfInv()`.
335 */
336function erfInvApprox(x: number): number {
337 const a = 0.147;
338 const ln1mx2 = Math.log(1 - x * x);
339 const b = 2 / (Math.PI * a) + ln1mx2 / 2;
340 return Math.sign(x) * Math.sqrt(Math.sqrt(b * b - ln1mx2 / a) - b);
341}
342
343/**
344 * Inverse Error Function, accurate to full machine (double) precision.
345 *
346 * Winitzki's approximation (~3 correct digits) refined with Newton's
347 * method on the full-precision `erf()`:
348 * y ← y − (erf(y) − x)·(√π/2)·e^{y²}
349 * Each iteration doubles the number of correct digits, so 4 iterations
350 * reach machine precision.
351 *
352 * (Previously used a 6-term truncated Maclaurin series, which was only
353 * ~4-digit accurate at x = 0.5 and diverged badly for |x| → 1.)
354 */
355export function erfInv(x: number): number {
356 if (Number.isNaN(x) || x < -1 || x > 1) return NaN;
357 if (x === 0) return 0;
358 if (x === 1) return Infinity;
359 if (x === -1) return -Infinity;
360
361 const sign = x < 0 ? -1 : 1;
362 const ax = Math.abs(x);

Callers 4

statistics.tsFile · 0.90
discreteQuantileFunction · 0.90

Calls 7

erfInvApproxFunction · 0.85
erfcFunction · 0.70
erfFunction · 0.70
absMethod · 0.65
sqrtMethod · 0.65
expMethod · 0.65
isNaNMethod · 0.45

Tested by

no test coverage detected