| 1 | /* |
| 2 | * Binomial.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 | * Binomial coefficients |
| 30 | * |
| 31 | * @version $Id: Binomial.java,v 1.11 2005/05/24 20:26:00 rambaut Exp $ |
| 32 | * |
| 33 | * @author Andrew Rambaut |
| 34 | * @author Alexei Drummond |
| 35 | * @author Korbinian Strimmer |
| 36 | */ |
| 37 | public class Binomial |
| 38 | { |
| 39 | // |
| 40 | // Public stuff; lnGamma is used, so parameters can be doubles. |
| 41 | // |
| 42 | |
| 43 | public static double logChoose(double n, double k){ |
| 44 | return GammaFunction.lnGamma(n + 1.0) - GammaFunction.lnGamma(k + 1.0) |
| 45 | - GammaFunction.lnGamma(n - k + 1.0); |
| 46 | } |
| 47 | |
| 48 | |
| 49 | /** |
| 50 | * @return Binomial coefficient n choose k |
| 51 | * @param n total elements |
| 52 | * @param k chosen elements |
| 53 | */ |
| 54 | public static double choose(double n, double k) |
| 55 | { |
| 56 | n = Math.floor(n + 0.5); |
| 57 | k = Math.floor(k + 0.5); |
| 58 | |
| 59 | double lchoose = GammaFunction.lnGamma(n + 1.0) - |
| 60 | GammaFunction.lnGamma(k + 1.0) - GammaFunction.lnGamma(n - k + 1.0); |
| 61 | |
| 62 | return Math.floor(Math.exp(lchoose) + 0.5); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * @param n # elements |
| 67 | * @return n choose 2 (number of distinct ways to choose a pair from n elements) |
| 68 | */ |
| 69 | public static double choose2(int n) |
| 70 | { |
| 71 | // not sure how much overhead there is with try-catch blocks |
| 72 | // i.e. would an if statement be better? |
| 73 | |
| 74 | try { |
| 75 | return choose2LUT[n]; |
| 76 | } catch (ArrayIndexOutOfBoundsException e) { |
| 77 | if( n < 0 ) { |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | while (maxN < n) { |
| 82 | maxN += 1000; |
| 83 | } |
| 84 | |
| 85 | initialize(); |
| 86 | return choose2LUT[n]; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | private static void initialize() { |
| 91 | choose2LUT = new double[maxN+1]; |
| 92 | choose2LUT[0] = 0; |
| 93 | choose2LUT[1] = 0; |
| 94 | choose2LUT[2] = 1; |
| 95 | for (int i = 3; i <= maxN; i++) { |
| 96 | choose2LUT[i] = ((double) (i*(i-1))) * 0.5; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | private static int maxN = 5000; |
| 101 | private static double[] choose2LUT; |
| 102 | |
| 103 | static { |
| 104 | initialize(); |
| 105 | } |
| 106 | } |