| 1 | /* |
| 2 | * SplineInterpolatedLikelihood.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.inference.distribution; |
| 27 | |
| 28 | import dr.inference.model.SplineBasis; |
| 29 | |
| 30 | /** |
| 31 | * @author Marc A. Suchard |
| 32 | * @author Andrew Rambaut |
| 33 | */ |
| 34 | public class SplineInterpolatedLikelihood extends EmpiricalDistributionLikelihood { |
| 35 | |
| 36 | protected static double outsideLogDensity = Double.NEGATIVE_INFINITY; // Use for a proper posterior |
| 37 | |
| 38 | public SplineInterpolatedLikelihood(String fileName, int degree, boolean inverse, boolean byColumn) { |
| 39 | super(fileName, inverse, byColumn); |
| 40 | |
| 41 | // Set-up spline basis, could be degree = 1 for linear interpolation |
| 42 | // splineBasis = new SplineBasis(getId(),new Variable.D(values), new Variable.D(density), degree); |
| 43 | |
| 44 | // Something is wrong with the spline basis routines... just do simple linear interpolation |
| 45 | |
| 46 | } |
| 47 | |
| 48 | @Override |
| 49 | protected double logPDF(double x) { |
| 50 | // return splineBasis.evaluate(x); |
| 51 | |
| 52 | final int len = values.length; |
| 53 | |
| 54 | if (x < values[0] || x > values[len - 1]) |
| 55 | return outsideLogDensity; |
| 56 | |
| 57 | double rtnValue = 0; |
| 58 | |
| 59 | for(int i=1; i<len; i++) { |
| 60 | if (values[i] > x) { // first largest point |
| 61 | final double diffValue = values[i] - values[i-1]; |
| 62 | final double diffDensity = density[i] - density[i-1]; |
| 63 | rtnValue = density[i] - (values[i]-x) / diffValue * diffDensity; |
| 64 | break; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | rtnValue = Math.log(rtnValue); |
| 69 | if (inverse) |
| 70 | rtnValue *= -1; |
| 71 | |
| 72 | return rtnValue; |
| 73 | } |
| 74 | |
| 75 | private SplineBasis splineBasis = null; |
| 76 | } |