| 1 | /* |
| 2 | * DiscreteUniformDistribution.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.distributions; |
| 27 | |
| 28 | import dr.math.UnivariateFunction; |
| 29 | |
| 30 | /** |
| 31 | * Created by mandevgill on 11/15/14. |
| 32 | */ |
| 33 | public class DiscreteUniformDistribution implements Distribution { |
| 34 | |
| 35 | public DiscreteUniformDistribution(double lower, double upper) { |
| 36 | this.lower = lower; |
| 37 | this.upper = upper; |
| 38 | this.n = upper - lower + 1; |
| 39 | } |
| 40 | |
| 41 | public double pdf(double x) { |
| 42 | if (x < lower) { |
| 43 | return 0; |
| 44 | } else if (x > upper) { |
| 45 | return 0; |
| 46 | } else { |
| 47 | return 1 / n; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | public double logPdf(double x) { |
| 52 | /* |
| 53 | if (x < 0) return Double.NEGATIVE_INFINITY; |
| 54 | double r = -1 * (mean*mean) / (mean - stdev*stdev); |
| 55 | double p = mean / (stdev*stdev); |
| 56 | return Math.log(Math.pow(1-p,x)) + Math.log(Math.pow(p, r)) + GammaFunction.lnGamma(r + x) - GammaFunction.lnGamma(r) - GammaFunction.lnGamma(x+1); |
| 57 | */ |
| 58 | if (x < lower) { |
| 59 | return Double.NEGATIVE_INFINITY; |
| 60 | } else if (x > upper) { |
| 61 | return Double.NEGATIVE_INFINITY; |
| 62 | } else { |
| 63 | return -Math.log(n); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | public double cdf(double x) { |
| 68 | return (x - lower + 1) / n; |
| 69 | } |
| 70 | |
| 71 | public double quantile(double y) { |
| 72 | // fill in |
| 73 | return Double.NaN; |
| 74 | } |
| 75 | |
| 76 | public double mean() { |
| 77 | return (lower + upper) / 2; |
| 78 | } |
| 79 | |
| 80 | public double variance() { |
| 81 | return (n * n - 1) / 12; |
| 82 | } |
| 83 | |
| 84 | public UnivariateFunction getProbabilityDensityFunction() { |
| 85 | throw new RuntimeException(); |
| 86 | } |
| 87 | |
| 88 | double lower; |
| 89 | double upper; |
| 90 | double n; |
| 91 | |
| 92 | } |