| 1 | /* |
| 2 | * ErrorFunction.java |
| 3 | * |
| 4 | * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard |
| 5 | * |
| 6 | * This file is part of BEAST. |
| 7 | * See the NOTICE file distributed with this work for additional |
| 8 | * information regarding copyright ownership and licensing. |
| 9 | * |
| 10 | * BEAST is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU Lesser General Public License as |
| 12 | * published by the Free Software Foundation; either version 2 |
| 13 | * of the License, or (at your option) any later version. |
| 14 | * |
| 15 | * BEAST is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU Lesser General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU Lesser General Public |
| 21 | * License along with BEAST; if not, write to the |
| 22 | * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, |
| 23 | * Boston, MA 02110-1301 USA |
| 24 | */ |
| 25 | |
| 26 | package dr.math; |
| 27 | |
| 28 | |
| 29 | /** |
| 30 | * error function and related stuff |
| 31 | * |
| 32 | * @version $Id: ErrorFunction.java,v 1.3 2005/05/24 20:26:00 rambaut Exp $ |
| 33 | * |
| 34 | * @author Korbinian Strimmer |
| 35 | */ |
| 36 | public class ErrorFunction |
| 37 | { |
| 38 | // |
| 39 | // Public stuff |
| 40 | // |
| 41 | |
| 42 | /** |
| 43 | * error function |
| 44 | * |
| 45 | * @param x argument |
| 46 | * |
| 47 | * @return function value |
| 48 | */ |
| 49 | public static double erf(double x) |
| 50 | { |
| 51 | if (x > 0.0) |
| 52 | { |
| 53 | return GammaFunction.incompleteGammaP(0.5, x*x); |
| 54 | } |
| 55 | else if (x < 0.0) |
| 56 | { |
| 57 | return -GammaFunction.incompleteGammaP(0.5, x*x); |
| 58 | } |
| 59 | else |
| 60 | { |
| 61 | return 0.0; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * complementary error function = 1-erf(x) |
| 67 | * |
| 68 | * @param x argument |
| 69 | * |
| 70 | * @return function value |
| 71 | */ |
| 72 | public static double erfc(double x) |
| 73 | { |
| 74 | return 1.0-erf(x); |
| 75 | } |
| 76 | |
| 77 | |
| 78 | /** |
| 79 | * inverse error function |
| 80 | * |
| 81 | * @param z argument |
| 82 | * |
| 83 | * @return function value |
| 84 | */ |
| 85 | public static double inverseErf(double z) |
| 86 | { |
| 87 | return pointNormal(0.5*z+0.5)/Math.sqrt(2.0); |
| 88 | } |
| 89 | |
| 90 | |
| 91 | // Private |
| 92 | |
| 93 | // Returns z so that Prob{x<z}=prob where x ~ N(0,1) and (1e-12) < prob<1-(1e-12) |
| 94 | private static double pointNormal(double prob) |
| 95 | { |
| 96 | // Odeh RE & Evans JO (1974) The percentage points of the normal distribution. |
| 97 | // Applied Statistics 22: 96-97 (AS70) |
| 98 | |
| 99 | // Newer methods: |
| 100 | // Wichura MJ (1988) Algorithm AS 241: the percentage points of the |
| 101 | // normal distribution. 37: 477-484. |
| 102 | // Beasley JD & Springer SG (1977). Algorithm AS 111: the percentage |
| 103 | // points of the normal distribution. 26: 118-121. |
| 104 | |
| 105 | double a0 = -0.322232431088, a1 = -1, a2 = -0.342242088547, a3 = -0.0204231210245; |
| 106 | double a4 = -0.453642210148e-4, b0 = 0.0993484626060, b1 = 0.588581570495; |
| 107 | double b2 = 0.531103462366, b3 = 0.103537752850, b4 = 0.0038560700634; |
| 108 | double y, z = 0, p = prob, p1; |
| 109 | |
| 110 | p1 = (p < 0.5 ? p : 1-p); |
| 111 | if (p1 < 1e-20) |
| 112 | { |
| 113 | new IllegalArgumentException("Argument prob out of range"); |
| 114 | } |
| 115 | |
| 116 | y = Math.sqrt(Math.log(1/(p1*p1))); |
| 117 | z = y + ((((y*a4+a3)*y+a2)*y+a1)*y+a0)/((((y*b4+b3)*y+b2)*y+b1)*y+b0); |
| 118 | return (p < 0.5 ? -z : z); |
| 119 | } |
| 120 | } |