| 1 | /* |
| 2 | * NumericalDerivative.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 | * approximates numerically the first and second derivatives of a |
| 31 | * function of a single variable and approximates gradient and |
| 32 | * diagonal of Hessian for multivariate functions |
| 33 | * |
| 34 | * Known bugs and limitations: |
| 35 | * - the sparse number of function evaluations used can potentially |
| 36 | * |
| 37 | * @author Korbinian Strimmer |
| 38 | */ |
| 39 | public class NumericalDerivative |
| 40 | { |
| 41 | // |
| 42 | // Public stuff |
| 43 | // |
| 44 | |
| 45 | |
| 46 | /** |
| 47 | * determine first derivative |
| 48 | * |
| 49 | * @param f univariate function |
| 50 | * @param x argument |
| 51 | * |
| 52 | * @return first derivate at x |
| 53 | */ |
| 54 | public static double firstDerivative(UnivariateFunction f, double x) |
| 55 | { |
| 56 | double h = MachineAccuracy.SQRT_EPSILON*(Math.abs(x) + 1.0); |
| 57 | |
| 58 | // Centered first derivative |
| 59 | return (f.evaluate(x + h) - f.evaluate(x - h))/(2.0*h); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * determine second derivative |
| 64 | * |
| 65 | * @param f univariate function |
| 66 | * @param x argument |
| 67 | * |
| 68 | * @return second derivate at x |
| 69 | */ |
| 70 | public static double secondDerivative(UnivariateFunction f, double x) |
| 71 | { |
| 72 | double h = MachineAccuracy.SQRT_SQRT_EPSILON*(Math.abs(x) + 1.0); |
| 73 | |
| 74 | // Centered second derivative |
| 75 | return (f.evaluate(x + h) - 2.0*f.evaluate(x) + f.evaluate(x - h))/(h*h); |
| 76 | } |
| 77 | |
| 78 | |
| 79 | /** |
| 80 | * determine gradient |
| 81 | * |
| 82 | * @param f multivariate function |
| 83 | * @param x argument vector |
| 84 | * |
| 85 | * @return gradient at x |
| 86 | */ |
| 87 | public static double[] gradient(MultivariateFunction f, double[] x) |
| 88 | { |
| 89 | double[] result = new double[x.length]; |
| 90 | |
| 91 | gradient(f, x, result); |
| 92 | |
| 93 | return result; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * determine gradient |
| 98 | * |
| 99 | * @param f multivariate function |
| 100 | * @param x argument vector |
| 101 | * @param grad vector for gradient |
| 102 | */ |
| 103 | public static void gradient(MultivariateFunction f, double[] x, double[] grad) |
| 104 | { |
| 105 | for (int i = 0; i < f.getNumArguments(); i++) |
| 106 | { |
| 107 | double h = MachineAccuracy.SQRT_EPSILON*(Math.abs(x[i]) + 1.0); |
| 108 | |
| 109 | double oldx = x[i]; |
| 110 | x[i] = oldx + h; |
| 111 | double fxplus = f.evaluate(x); |
| 112 | x[i] = oldx - h; |
| 113 | double fxminus = f.evaluate(x); |
| 114 | x[i] = oldx; |
| 115 | |
| 116 | // Centered first derivative |
| 117 | grad[i] = (fxplus-fxminus)/(2.0*h); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * determine diagonal of Hessian |
| 123 | * |
| 124 | * @param f multivariate function |
| 125 | * @param x argument vector |
| 126 | * |
| 127 | * @return vector with diagonal entries of Hessian |
| 128 | */ |
| 129 | public static double[] diagonalHessian(MultivariateFunction f, double[] x) |
| 130 | { |
| 131 | int len = f.getNumArguments(); |
| 132 | double[] result = new double[len]; |
| 133 | |
| 134 | for (int i = 0; i < len; i++) |
| 135 | { |
| 136 | double h = MachineAccuracy.SQRT_SQRT_EPSILON*(Math.abs(x[i]) + 1.0); |
| 137 | |
| 138 | double oldx = x[i]; |
| 139 | x[i] = oldx + h; |
| 140 | double fxplus = f.evaluate(x); |
| 141 | x[i] = oldx - h; |
| 142 | double fxminus = f.evaluate(x); |
| 143 | x[i] = oldx; |
| 144 | double fx = f.evaluate(x); |
| 145 | |
| 146 | // Centered second derivative |
| 147 | result[i] = (fxplus - 2.0*fx + fxminus)/(h*h); |
| 148 | } |
| 149 | |
| 150 | return result; |
| 151 | } |
| 152 | } |