| 1 | /* |
| 2 | * OffsetPositiveDistribution.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 | * A distribution that is offset, so that the origin is greater than 0 |
| 32 | * |
| 33 | * @author Alexei Drummond |
| 34 | */ |
| 35 | public class OffsetPositiveDistribution implements Distribution { |
| 36 | |
| 37 | /** |
| 38 | * Constructor |
| 39 | * |
| 40 | * @param distribution distribution to offset |
| 41 | * @param offset a (positive) location parameter that allows this distribution to start |
| 42 | * at a non-zero location |
| 43 | */ |
| 44 | public OffsetPositiveDistribution(Distribution distribution, double offset) { |
| 45 | |
| 46 | if (offset < 0.0) throw new IllegalArgumentException(); |
| 47 | this.offset = offset; |
| 48 | this.distribution = distribution; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * probability density function of the offset distribution |
| 53 | * |
| 54 | * @param x argument |
| 55 | * @return pdf value |
| 56 | */ |
| 57 | public final double pdf(double x) { |
| 58 | if (offset < 0) return 0.0; |
| 59 | return distribution.pdf(x - offset); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * log probability density function of the offset distribution |
| 64 | * |
| 65 | * @param x argument |
| 66 | * @return pdf value |
| 67 | */ |
| 68 | public final double logPdf(double x) { |
| 69 | if (offset < 0) return Math.log(0.0); |
| 70 | return distribution.logPdf(x - offset); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * cumulative density function of the offset distribution |
| 75 | * |
| 76 | * @param x argument |
| 77 | * @return cdf value |
| 78 | */ |
| 79 | public final double cdf(double x) { |
| 80 | if (offset < 0) return 0.0; |
| 81 | return distribution.cdf(x - offset); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * quantile (inverse cumulative density function) of the (offset) distribution |
| 86 | * |
| 87 | * @param y the p-value |
| 88 | * @return icdf value |
| 89 | */ |
| 90 | public final double quantile(double y) { |
| 91 | return distribution.quantile(y) + offset; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * mean of the offset distribution |
| 96 | * |
| 97 | * @return mean |
| 98 | */ |
| 99 | public final double mean() { |
| 100 | return distribution.mean() + offset; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * variance of the offset distribution |
| 105 | * |
| 106 | * @return variance |
| 107 | */ |
| 108 | public final double variance() { |
| 109 | throw new UnsupportedOperationException(); |
| 110 | } |
| 111 | |
| 112 | public final UnivariateFunction getProbabilityDensityFunction() { |
| 113 | return pdfFunction; |
| 114 | } |
| 115 | |
| 116 | private UnivariateFunction pdfFunction = new UnivariateFunction() { |
| 117 | public final double evaluate(double x) { |
| 118 | return pdf(x); |
| 119 | } |
| 120 | |
| 121 | public final double getLowerBound() { |
| 122 | return offset + distribution.getProbabilityDensityFunction().getLowerBound(); |
| 123 | } |
| 124 | |
| 125 | public final double getUpperBound() { |
| 126 | return offset + distribution.getProbabilityDensityFunction().getUpperBound(); |
| 127 | } |
| 128 | }; |
| 129 | |
| 130 | // the location parameter of the start of the positive distribution |
| 131 | private double offset = 0.0; |
| 132 | |
| 133 | // the distribution to offset |
| 134 | private Distribution distribution; |
| 135 | } |