Debian Codegraph @

src/dr/inference/distribution/TwoPieceLocationScaleDistributionModel.java

raw symbols SCIP
1 /*
2 * TwoPieceLocationScaleDistributionModel.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.inferencexml.distribution.TwoPieceLocationScaleDistributionModelParser;
33 import dr.math.UnivariateFunction;
34 import dr.math.distributions.Distribution;
35 import dr.math.distributions.RandomGenerator;
36 import org.w3c.dom.Document;
37 import org.w3c.dom.Element;
38
39 /**
40 * @author Marc A. Suchard
41 * @author Robert E. Weiss
42 */
43
44 public class TwoPieceLocationScaleDistributionModel extends AbstractModel implements ParametricDistributionModel, RandomGenerator {
45
46 public TwoPieceLocationScaleDistributionModel(Parameter locationParam, Distribution distribution,
47 Parameter sigmaParameter,
48 Parameter gammaParameter, Parameterization parameterization) {
49 super(TwoPieceLocationScaleDistributionModelParser.DISTRIBUTION_MODEL);
50 this.locationParameter = locationParam;
51 this.sigmaParameter = sigmaParameter;
52 this.gammaParameter = gammaParameter;
53 this.distribution = distribution;
54
55 addVariable(locationParam);
56 addVariable(sigmaParameter);
57 addVariable(gammaParameter);
58
59 locationParam.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, 1));
60 sigmaParameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1));
61 gammaParameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1));
62
63 this.parameterization = parameterization;
64
65 // TODO Upgrade Distribution to DistributionModel
66 }
67
68 private double getLowerScale() {
69 return parameterization.getLowerScale(sigmaParameter.getParameterValue(0), gammaParameter.getParameterValue(0));
70 }
71
72 private double getUpperScale() {
73 return parameterization.getUpperScale(sigmaParameter.getParameterValue(0), gammaParameter.getParameterValue(0));
74 }
75
76 private double getLocation() {
77 return locationParameter.getParameterValue(0);
78 }
79
80 // *****************************************************************
81 // Interface Distribution
82 // *****************************************************************
83
84 public double pdf(double x) {
85 return Math.exp(logPdf(x));
86 }
87
88 public double logPdf(double x) {
89 final double mu = getLocation();
90 final double lowerScale = getLowerScale();
91 final double upperScale = getUpperScale();
92
93 double t = x - mu;
94 if (x < mu) {
95 t /= lowerScale;
96 } else {
97 t /= upperScale;
98 }
99
100 return Math.log(2.0) - Math.log(lowerScale + upperScale) + distribution.logPdf(t);
101 }
102
103 public double cdf(double x) {
104 throw new RuntimeException("Not yet implemented.");
105 }
106
107 public double quantile(double y) {
108 throw new RuntimeException("Not yet implemented.");
109 }
110
111 public double mean() {
112 throw new RuntimeException("Not yet implemented.");
113 }
114
115 public double variance() {
116 throw new RuntimeException("Not yet implemented.");
117 }
118
119 public final UnivariateFunction getProbabilityDensityFunction() {
120 throw new RuntimeException("Not yet implemented.");
121 }
122
123 public Object nextRandom() {
124 throw new RuntimeException("Not yet implemented.");
125 }
126
127 public double logPdf(Object x) {
128 double v = (Double) x;
129 return logPdf(v);
130 }
131
132 // *****************************************************************
133 // Interface DensityModel
134 // *****************************************************************
135
136 @Override
137 public double logPdf(double[] x) {
138 return logPdf(x[0]);
139 }
140
141 @Override
142 public Variable<Double> getLocationVariable() {
143 throw new UnsupportedOperationException("Not implemented");
144 }
145
146 // *****************************************************************
147 // Interface Model
148 // *****************************************************************
149
150 public void handleModelChangedEvent(Model model, Object object, int index) {
151 // no intermediates need to be recalculated...
152 }
153
154 protected final void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
155 // no intermediates need to be recalculated...
156 }
157
158 protected void storeState() {
159 } // no additional state needs storing
160
161 protected void restoreState() {
162 } // no additional state needs restoring
163
164 protected void acceptState() {
165 } // no additional state needs accepting
166
167 public Element createElement(Document document) {
168 throw new RuntimeException("Not yet implemented!");
169 }
170
171 // **************************************************************
172 // Private instance variables
173 // **************************************************************
174
175 private final Parameter locationParameter;
176 private final Parameter sigmaParameter;
177 private final Parameter gammaParameter;
178
179 private final Distribution distribution;
180 private final Parameterization parameterization;
181
182
183 private interface Scale {
184 double getLowerScale(double sigma, double gamma);
185
186 double getUpperScale(double sigma, double gamma);
187 }
188
189 public enum Parameterization implements Scale {
190
191 TWO_SCALE("twoScale", new Scale() {
192 public double getLowerScale(double sigma, double gamma) {
193 return sigma;
194 }
195
196 public double getUpperScale(double sigma, double gamma) {
197 return gamma;
198 }
199 }),
200
201 ISF("inverseScaleFactors", new Scale() {
202 public double getLowerScale(double sigma, double gamma) {
203 return sigma / gamma;
204 }
205
206 public double getUpperScale(double sigma, double gamma) {
207 return sigma * gamma;
208 }
209 }),
210
211 EPSILON_SKEW("epsilonSkew", new Scale() {
212 public double getLowerScale(double sigma, double gamma) {
213 return sigma * (1.0 - gamma);
214 }
215
216 public double getUpperScale(double sigma, double gamma) {
217 return sigma * (1.0 + gamma);
218 }
219 }),
220
221 LOGISTIC_AG("logisticAG", new Scale() {
222 public double getLowerScale(double sigma, double gamma) {
223 return sigma * (1.0 + Math.exp(-2.0 * gamma));
224 }
225
226 public double getUpperScale(double sigma, double gamma) {
227 return sigma * (1.0 + Math.exp(2.0 * gamma));
228 }
229 });
230
231 Parameterization(String text, Scale scale) {
232 this.text = text;
233 this.scale = scale;
234 }
235
236 private final String text;
237 private final Scale scale;
238
239 public static Parameterization parseFromString(String text) {
240 for (Parameterization scheme : Parameterization.values()) {
241 if (scheme.toString().compareToIgnoreCase(text) == 0)
242 return scheme;
243 }
244 return null;
245 }
246
247 public String toString() {
248 return text;
249 }
250
251 public double getLowerScale(double sigma, double gamma) {
252 return scale.getLowerScale(sigma, gamma);
253 }
254
255 public double getUpperScale(double sigma, double gamma) {
256 return scale.getUpperScale(sigma, gamma);
257 }
258
259 }
260
261 }