inverse_error_function.js

/**
 * 逆[高斯误差函数](http://en.wikipedia.org/wiki/Error_function)
 * 返回一个数值近似值,该值会导致`errorFunction()`返回x。
 *
 * @param {number} x 误差函数的值
 * @returns {number} 估计的逆值
 */
function inverseErrorFunction(x) {
    const a = (8 * (Math.PI - 3)) / (3 * Math.PI * (4 - Math.PI));

    const inv = Math.sqrt(
        Math.sqrt(
            Math.pow(2 / (Math.PI * a) + Math.log(1 - x * x) / 2, 2) -
                Math.log(1 - x * x) / a
        ) -
            (2 / (Math.PI * a) + Math.log(1 - x * x) / 2)
    );

    if (x >= 0) {
        return inv;
    } else {
        return -inv;
    }
}

export default inverseErrorFunction;