001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.numbers.gamma;
018
019/**
020 * Inverse of the <a href="http://mathworld.wolfram.com/Erf.html">error function</a>.
021 * <p>
022 * This implementation is described in the paper:
023 * <a href="http://people.maths.ox.ac.uk/gilesm/files/gems_erfinv.pdf">Approximating
024 * the erfinv function</a> by Mike Giles, Oxford-Man Institute of Quantitative Finance,
025 * which was published in GPU Computing Gems, volume 2, 2010.
026 * The source code is available <a href="http://gpucomputing.net/?q=node/1828">here</a>.
027 * </p>
028 */
029public final class InverseErf {
030    /** Private constructor. */
031    private InverseErf() {
032        // intentionally empty.
033    }
034
035    /**
036     * Returns the inverse error function.
037     *
038     * @param x Value.
039     * @return t such that {@code x =} {@link Erf#value(double) Erf.value(t)}.
040     */
041    public static double value(final double x) {
042        // Beware that the logarithm argument must be
043        // computed as (1 - x) * (1 + x),
044        // it must NOT be simplified as 1 - x * x as this
045        // would induce rounding errors near the boundaries +/-1
046        double w = -Math.log((1 - x) * (1 + x));
047        double p;
048
049        if (w < 6.25) {
050            w -= 3.125;
051            p =  -3.6444120640178196996e-21;
052            p =   -1.685059138182016589e-19 + p * w;
053            p =   1.2858480715256400167e-18 + p * w;
054            p =    1.115787767802518096e-17 + p * w;
055            p =   -1.333171662854620906e-16 + p * w;
056            p =   2.0972767875968561637e-17 + p * w;
057            p =   6.6376381343583238325e-15 + p * w;
058            p =  -4.0545662729752068639e-14 + p * w;
059            p =  -8.1519341976054721522e-14 + p * w;
060            p =   2.6335093153082322977e-12 + p * w;
061            p =  -1.2975133253453532498e-11 + p * w;
062            p =  -5.4154120542946279317e-11 + p * w;
063            p =    1.051212273321532285e-09 + p * w;
064            p =  -4.1126339803469836976e-09 + p * w;
065            p =  -2.9070369957882005086e-08 + p * w;
066            p =   4.2347877827932403518e-07 + p * w;
067            p =  -1.3654692000834678645e-06 + p * w;
068            p =  -1.3882523362786468719e-05 + p * w;
069            p =    0.0001867342080340571352 + p * w;
070            p =  -0.00074070253416626697512 + p * w;
071            p =   -0.0060336708714301490533 + p * w;
072            p =      0.24015818242558961693 + p * w;
073            p =       1.6536545626831027356 + p * w;
074        } else if (w < 16.0) {
075            w = Math.sqrt(w) - 3.25;
076            p =   2.2137376921775787049e-09;
077            p =   9.0756561938885390979e-08 + p * w;
078            p =  -2.7517406297064545428e-07 + p * w;
079            p =   1.8239629214389227755e-08 + p * w;
080            p =   1.5027403968909827627e-06 + p * w;
081            p =   -4.013867526981545969e-06 + p * w;
082            p =   2.9234449089955446044e-06 + p * w;
083            p =   1.2475304481671778723e-05 + p * w;
084            p =  -4.7318229009055733981e-05 + p * w;
085            p =   6.8284851459573175448e-05 + p * w;
086            p =   2.4031110387097893999e-05 + p * w;
087            p =   -0.0003550375203628474796 + p * w;
088            p =   0.00095328937973738049703 + p * w;
089            p =   -0.0016882755560235047313 + p * w;
090            p =    0.0024914420961078508066 + p * w;
091            p =   -0.0037512085075692412107 + p * w;
092            p =     0.005370914553590063617 + p * w;
093            p =       1.0052589676941592334 + p * w;
094            p =       3.0838856104922207635 + p * w;
095        } else if (w < Double.POSITIVE_INFINITY) {
096            w = Math.sqrt(w) - 5;
097            p =  -2.7109920616438573243e-11;
098            p =  -2.5556418169965252055e-10 + p * w;
099            p =   1.5076572693500548083e-09 + p * w;
100            p =  -3.7894654401267369937e-09 + p * w;
101            p =   7.6157012080783393804e-09 + p * w;
102            p =  -1.4960026627149240478e-08 + p * w;
103            p =   2.9147953450901080826e-08 + p * w;
104            p =  -6.7711997758452339498e-08 + p * w;
105            p =   2.2900482228026654717e-07 + p * w;
106            p =  -9.9298272942317002539e-07 + p * w;
107            p =   4.5260625972231537039e-06 + p * w;
108            p =  -1.9681778105531670567e-05 + p * w;
109            p =   7.5995277030017761139e-05 + p * w;
110            p =  -0.00021503011930044477347 + p * w;
111            p =  -0.00013871931833623122026 + p * w;
112            p =       1.0103004648645343977 + p * w;
113            p =       4.8499064014085844221 + p * w;
114        } else if (w == Double.POSITIVE_INFINITY) {
115            // this branch does not appears in the original code, it
116            // was added because the previous branch does not handle
117            // x = +/-1 correctly. In this case, w is positive infinity
118            // and as the first coefficient (-2.71e-11) is negative.
119            // Once the first multiplication is done, p becomes negative
120            // infinity and remains so throughout the polynomial evaluation.
121            // So the branch above incorrectly returns negative infinity
122            // instead of the correct positive infinity.
123            p = Double.POSITIVE_INFINITY;
124        } else {
125            // this branch does not appears in the original code, it
126            // occurs when the input is NaN or not in the range [-1, 1].
127            return Double.NaN;
128        }
129
130        return p * x;
131    }
132}
133