Debian Codegraph @

src/dr/oldevomodel/sitemodel/GammaSiteModel.java

raw symbols SCIP
1 /*
2 * GammaSiteModel.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.oldevomodel.sitemodel;
27
28 import dr.oldevomodel.substmodel.FrequencyModel;
29 import dr.oldevomodel.substmodel.SubstitutionModel;
30 import dr.inference.model.AbstractModel;
31 import dr.inference.model.Model;
32 import dr.inference.model.Parameter;
33 import dr.inference.model.Variable;
34 import dr.math.distributions.GammaDistribution;
35
36 /**
37 * GammaSiteModel - A SiteModel that has a gamma distributed rates across sites.
38 *
39 * @author Andrew Rambaut
40 * @version $Id: GammaSiteModel.java,v 1.31 2005/09/26 14:27:38 rambaut Exp $
41 */
42
43 public class GammaSiteModel extends AbstractModel implements SiteModel {
44
45 public GammaSiteModel(SubstitutionModel substitutionModel) {
46 this(substitutionModel,
47 null,
48 null,
49 0,
50 null);
51 }
52
53 public GammaSiteModel(SubstitutionModel substitutionModel, double alpha, int categoryCount) {
54 this(substitutionModel,
55 null,
56 new Parameter.Default(alpha),
57 categoryCount,
58 null);
59 }
60
61 public GammaSiteModel(SubstitutionModel substitutionModel, double pInvar) {
62 this(substitutionModel,
63 null,
64 null,
65 0,
66 new Parameter.Default(pInvar));
67 }
68
69 public GammaSiteModel(SubstitutionModel substitutionModel, double alpha, int categoryCount, double pInvar) {
70 this(substitutionModel,
71 null,
72 new Parameter.Default(alpha),
73 categoryCount,
74 new Parameter.Default(pInvar));
75 }
76
77 /**
78 * Constructor for gamma+invar distributed sites. Either shapeParameter or
79 * invarParameter (or both) can be null to turn off that feature.
80 */
81 public GammaSiteModel(SubstitutionModel substitutionModel,
82 Parameter muParameter,
83 Parameter shapeParameter, int gammaCategoryCount,
84 Parameter invarParameter) {
85
86 super(SITE_MODEL);
87
88 this.substitutionModel = substitutionModel;
89 addModel(substitutionModel);
90
91 this.muParameter = muParameter;
92 if (muParameter != null) {
93 addVariable(muParameter);
94 muParameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1));
95 }
96
97 this.shapeParameter = shapeParameter;
98 if (shapeParameter != null) {
99 this.categoryCount = gammaCategoryCount;
100
101 addVariable(shapeParameter);
102
103 // The quantile calculator fails when the shape parameter goes much below
104 // 1E-3 so we have put a hard lower bound on it. If this is not there then
105 // the category rates can go to 0 and cause a -Inf likelihood (whilst this
106 // is not a problem as the state will be rejected, it could mask other issues
107 // and this seems the better approach.
108 shapeParameter.addBounds(new Parameter.DefaultBounds(1.0E3, 1.0E-3, 1));
109 } else {
110 this.categoryCount = 1;
111 }
112
113 this.invarParameter = invarParameter;
114 if (invarParameter != null) {
115 this.categoryCount += 1;
116
117 addVariable(invarParameter);
118 invarParameter.addBounds(new Parameter.DefaultBounds(1.0, 0.0, 1));
119 }
120
121 categoryRates = new double[this.categoryCount];
122 categoryProportions = new double[this.categoryCount];
123
124 ratesKnown = false;
125 }
126
127 /**
128 * set mu
129 */
130 public void setMu(double mu) {
131 muParameter.setParameterValue(0, mu);
132 }
133
134 /**
135 * @return mu
136 */
137 public final double getMu() {
138 return muParameter.getParameterValue(0);
139 }
140
141 /**
142 * set alpha
143 */
144 public void setAlpha(double alpha) {
145 shapeParameter.setParameterValue(0, alpha);
146 ratesKnown = false;
147 }
148
149 /**
150 * @return alpha
151 */
152 public final double getAlpha() {
153 return shapeParameter.getParameterValue(0);
154 }
155
156 public Parameter getMutationRateParameter() {
157 return muParameter;
158 }
159
160 public Parameter getAlphaParameter() {
161 return shapeParameter;
162 }
163
164 public Parameter getPInvParameter() {
165 return invarParameter;
166 }
167
168 public void setMutationRateParameter(Parameter parameter) {
169 if (muParameter != null) removeVariable(muParameter);
170 muParameter = parameter;
171 if (muParameter != null) addVariable(muParameter);
172 }
173
174 public void setAlphaParameter(Parameter parameter) {
175 if (shapeParameter != null) removeVariable(shapeParameter);
176 shapeParameter = parameter;
177 if (shapeParameter != null) addVariable(shapeParameter);
178 }
179
180 public void setPInvParameter(Parameter parameter) {
181 if (invarParameter != null) removeVariable(invarParameter);
182 invarParameter = parameter;
183 if (invarParameter != null) addVariable(invarParameter);
184 }
185
186 // *****************************************************************
187 // Interface SiteModel
188 // *****************************************************************
189
190 public boolean integrateAcrossCategories() {
191 return true;
192 }
193
194 public int getCategoryCount() {
195 return categoryCount;
196 }
197
198 public int getCategoryOfSite(int site) {
199 throw new IllegalArgumentException("Integrating across categories");
200 }
201
202 public double getRateForCategory(int category) {
203 synchronized (this) {
204 if (!ratesKnown) {
205 calculateCategoryRates();
206 }
207 }
208
209 final double mu = (muParameter != null) ? muParameter.getParameterValue(0) : 1.0;
210
211 return categoryRates[category] * mu;
212 }
213
214 public double[] getCategoryRates() {
215 synchronized (this) {
216 if (!ratesKnown) {
217 calculateCategoryRates();
218 }
219 }
220
221 final double mu = (muParameter != null) ? muParameter.getParameterValue(0) : 1.0;
222
223 final double[] rates = new double[categoryRates.length];
224 for (int i = 0; i < rates.length; i++) {
225 rates[i] = categoryRates[i] * mu;
226 }
227
228 return rates;
229 }
230
231 public void getTransitionProbabilities(double substitutions, double[] matrix) {
232 substitutionModel.getTransitionProbabilities(substitutions, matrix);
233 }
234
235 /**
236 * Get the expected proportion of sites in this category.
237 *
238 * @param category the category number
239 * @return the proportion.
240 */
241 public double getProportionForCategory(int category) {
242 synchronized (this) {
243 if (!ratesKnown) {
244 calculateCategoryRates();
245 }
246 }
247
248 return categoryProportions[category];
249 }
250
251 /**
252 * Get an array of the expected proportion of sites in this category.
253 *
254 * @return an array of the proportion.
255 */
256 public double[] getCategoryProportions() {
257 synchronized (this) {
258 if (!ratesKnown) {
259 calculateCategoryRates();
260 }
261 }
262
263 return categoryProportions;
264 }
265
266 /**
267 * discretization of gamma distribution with equal proportions in each
268 * category
269 */
270 private void calculateCategoryRates() {
271
272 double propVariable = 1.0;
273 int cat = 0;
274
275 if (invarParameter != null) {
276 categoryRates[0] = 0.0;
277 categoryProportions[0] = invarParameter.getParameterValue(0);
278
279 propVariable = 1.0 - categoryProportions[0];
280 cat = 1;
281 }
282
283 if (shapeParameter != null) {
284
285 final double a = shapeParameter.getParameterValue(0);
286 double mean = 0.0;
287 final int gammaCatCount = categoryCount - cat;
288
289 for (int i = 0; i < gammaCatCount; i++) {
290
291 categoryRates[i + cat] = GammaDistribution.quantile((2.0 * i + 1.0) / (2.0 * gammaCatCount), a, 1.0 / a);
292 mean += categoryRates[i + cat];
293
294 categoryProportions[i + cat] = propVariable / gammaCatCount;
295 }
296
297 mean = (propVariable * mean) / gammaCatCount;
298
299 for (int i = 0; i < gammaCatCount; i++) {
300
301 categoryRates[i + cat] /= mean;
302 }
303 } else {
304 categoryRates[cat] = 1.0 / propVariable;
305 categoryProportions[cat] = propVariable;
306 }
307
308
309 ratesKnown = true;
310 }
311
312 /**
313 * Get the frequencyModel for this SiteModel.
314 *
315 * @return the frequencyModel.
316 */
317 public FrequencyModel getFrequencyModel() {
318 return substitutionModel.getFrequencyModel();
319 }
320
321 /**
322 * Get the substitutionModel for this SiteModel.
323 *
324 * @return the substitutionModel.
325 */
326 public SubstitutionModel getSubstitutionModel() {
327 return substitutionModel;
328 }
329
330 // *****************************************************************
331 // Interface ModelComponent
332 // *****************************************************************
333
334 protected void handleModelChangedEvent(Model model, Object object, int index) {
335 // Substitution model has changed so fire model changed event
336 listenerHelper.fireModelChanged(this, object, index);
337 }
338
339 protected final void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
340 if (variable == shapeParameter) {
341 ratesKnown = false;
342 } else if (variable == invarParameter) {
343 ratesKnown = false;
344 } else {
345 // is the muParameter and nothing needs to be done
346 }
347 listenerHelper.fireModelChanged(this, variable, index);
348 }
349
350 protected void storeState() {
351 } // no additional state needs storing
352
353 protected void restoreState() {
354 ratesKnown = false;
355 }
356
357 protected void acceptState() {
358 } // no additional state needs accepting
359
360 /**
361 * the substitution model for these sites
362 */
363 private SubstitutionModel substitutionModel = null;
364
365 /**
366 * mutation rate parameter
367 */
368 private Parameter muParameter;
369
370 /**
371 * shape parameter
372 */
373 private Parameter shapeParameter;
374
375 /**
376 * invariant sites parameter
377 */
378 private Parameter invarParameter;
379
380 private boolean ratesKnown;
381
382 private int categoryCount;
383
384 private double[] categoryRates;
385
386 private double[] categoryProportions;
387 }