Debian Codegraph @

src/dr/inference/distribution/ExponentialDistributionModel.java

raw symbols SCIP
1 /*
2 * ExponentialDistributionModel.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.AbstractModel;
29 import dr.inference.model.Model;
30 import dr.inference.model.Parameter;
31 import dr.inference.model.Variable;
32 import dr.math.UnivariateFunction;
33 import dr.math.distributions.ExponentialDistribution;
34 import org.w3c.dom.Document;
35 import org.w3c.dom.Element;
36
37 /**
38 * A class that acts as a model for exponentially distributed data.
39 *
40 * @author Andrew Rambaut
41 * @author Alexei Drummond
42 * @version $Id: ExponentialDistributionModel.java,v 1.12 2005/05/24 20:25:59 rambaut Exp $
43 */
44
45 public class ExponentialDistributionModel extends AbstractModel implements ParametricDistributionModel {
46
47 public static final String EXPONENTIAL_DISTRIBUTION_MODEL = "exponentialDistributionModel";
48
49 /**
50 * Constructor.
51 */
52 public ExponentialDistributionModel(Variable<Double> mean) {
53
54 this(mean, 0.0);
55 }
56
57
58 /**
59 * Constructor.
60 */
61 public ExponentialDistributionModel(Variable<Double> mean, double offset) {
62
63 super(EXPONENTIAL_DISTRIBUTION_MODEL);
64
65 this.mean = mean;
66 this.offset = offset;
67
68 addVariable(mean);
69 mean.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1));
70 }
71
72 // *****************************************************************
73 // Interface Distribution
74 // *****************************************************************
75
76 public double pdf(double x) {
77 if (x < offset) return 0.0;
78 return ExponentialDistribution.pdf(x - offset, 1.0 / getMean());
79 }
80
81 public double logPdf(double x) {
82 if (x < offset) return Double.NEGATIVE_INFINITY;
83 return ExponentialDistribution.logPdf(x - offset, 1.0 / getMean());
84 }
85
86 public double cdf(double x) {
87 if (x < offset) return 0.0;
88 return ExponentialDistribution.cdf(x - offset, 1.0 / getMean());
89 }
90
91 public double quantile(double y) {
92 return ExponentialDistribution.quantile(y, 1.0 / getMean()) + offset;
93 }
94
95 public double mean() {
96 return ExponentialDistribution.mean(1.0 / getMean()) + offset;
97 }
98
99 public double variance() {
100 return ExponentialDistribution.variance(1.0 / getMean());
101 }
102
103 public final UnivariateFunction getProbabilityDensityFunction() {
104 return pdfFunction;
105 }
106
107 private final UnivariateFunction pdfFunction = new UnivariateFunction() {
108 public final double evaluate(double x) {
109 return pdf(x);
110 }
111
112 public final double getLowerBound() {
113 return offset;
114 }
115
116 public final double getUpperBound() {
117 return Double.POSITIVE_INFINITY;
118 }
119 };
120
121 // *****************************************************************
122 // Interface DensityModel
123 // *****************************************************************
124
125 @Override
126 public double logPdf(double[] x) {
127 return logPdf(x[0]);
128 }
129
130 @Override
131 public Variable<Double> getLocationVariable() {
132 return mean;
133 }
134
135 // *****************************************************************
136 // Interface Model
137 // *****************************************************************
138
139 public void handleModelChangedEvent(Model model, Object object, int index) {
140 // no intermediates need to be recalculated...
141 }
142
143 public void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
144 // no intermediates need to be recalculated...
145 }
146
147 protected void storeState() {
148 } // no additional state needs storing
149
150 protected void restoreState() {
151 } // no additional state needs restoring
152
153 protected void acceptState() {
154 } // no additional state needs accepting
155
156 // **************************************************************
157 // XMLElement IMPLEMENTATION
158 // **************************************************************
159
160 public Element createElement(Document document) {
161 throw new RuntimeException("Not implemented!");
162 }
163
164 // **************************************************************
165 // Private methods
166 // **************************************************************
167
168 private double getMean() {
169 return mean.getValue(0);
170 }
171
172 // **************************************************************
173 // Private instance variables
174 // **************************************************************
175
176 private Variable<Double> mean = null;
177 private double offset = 0.0;
178
179 }
180