Debian Codegraph @

src/dr/oldevomodel/sitemodel/GammaSiteBMA.java

raw symbols SCIP
1 /*
2 * GammaSiteBMA.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.SubstitutionModel;
29 import dr.oldevomodel.substmodel.FrequencyModel;
30 import dr.inference.model.*;
31 import dr.math.distributions.GammaDistribution;
32
33 /**
34 * @author Chieh-Hsi Wu
35 *
36 * BSSVS on Gamma site model
37 *
38 */
39 public class GammaSiteBMA extends AbstractModel implements SiteModel {
40
41 private SubstitutionModel substitutionModel = null;
42 private Parameter muParameter =null;
43 private Variable<Integer> modelChoose = null;
44 private Variable<Double> logShape = null;
45 private Variable<Double> logitInvar = null;
46 private int categoryCount = -1;
47 private double[] categoryRates;
48 private double[] categoryProportions;
49 private boolean ratesKnown;
50 public static final int SHAPE_INDEX = 0;
51 public static final int INVAR_INDEX = 1;
52 public static final int PRESENT = 1;
53 public static final int ABSENT = 0;
54
55 public GammaSiteBMA(SubstitutionModel substitutionModel,
56 Parameter muParameter,
57 Variable<Double> logitInvar,
58 Variable<Double> logShape,
59 int categoryCount,
60 Variable<Integer> modelChoose){
61 super("GammaSiteBMA");
62
63 this.substitutionModel = substitutionModel;
64 addModel(substitutionModel);
65
66 addVariable(muParameter);
67 muParameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1));
68 this.muParameter = muParameter;
69
70 addVariable(logShape);
71 logShape.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, 1));
72 this.logShape = logShape;
73
74 addVariable(logitInvar);
75 logitInvar.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, 1));
76 this.logitInvar = logitInvar;
77
78
79
80 //the modelChoose integer variable is of length = 2,
81 //where indices 0 and 1 indicates the presence or absence of alpha and pInvar repectively.
82 addVariable(modelChoose);
83 modelChoose.addBounds(new Bounds.Int(modelChoose,0,1));
84 this.modelChoose = modelChoose;
85 this.categoryCount = categoryCount +1;
86
87 categoryRates = new double[this.categoryCount];
88 categoryProportions = new double[this.categoryCount];
89
90 ratesKnown = false;
91
92 }
93
94 public boolean integrateAcrossCategories() {
95 return true;
96 }
97
98 public int getCategoryCount() {
99 return categoryCount;
100 }
101
102 public int getCategoryOfSite(int site) {
103 throw new IllegalArgumentException("Integrating across categories");
104 }
105
106 public double getRateForCategory(int category) {
107 synchronized (this) {
108 if (!ratesKnown) {
109 calculateCategoryRates();
110 }
111 }
112
113 final double mu = (muParameter != null) ? muParameter.getParameterValue(0) : 1.0;
114
115 return categoryRates[category] * mu;
116 }
117
118
119 public double[] getCategoryRates() {
120 synchronized (this) {
121 if (!ratesKnown) {
122 calculateCategoryRates();
123 }
124 }
125
126 final double mu = (muParameter != null) ? muParameter.getParameterValue(0) : 1.0;
127
128 final double[] rates = new double[categoryRates.length];
129 for (int i = 0; i < rates.length; i++) {
130 rates[i] = categoryRates[i] * mu;
131 }
132
133 return rates;
134 }
135
136 public void getTransitionProbabilities(double substitutions, double[] matrix) {
137 substitutionModel.getTransitionProbabilities(substitutions, matrix);
138 }
139
140 /**
141 * Get the expected proportion of sites in this category.
142 *
143 * @param category the category number
144 * @return the proportion.
145 */
146 public double getProportionForCategory(int category) {
147 synchronized (this) {
148 if (!ratesKnown) {
149 calculateCategoryRates();
150 }
151 }
152
153 return categoryProportions[category];
154 }
155
156 /**
157 * Get an array of the expected proportion of sites in this category.
158 *
159 * @return an array of the proportion.
160 */
161 public double[] getCategoryProportions() {
162 synchronized (this) {
163 if (!ratesKnown) {
164 calculateCategoryRates();
165 }
166 }
167
168 return categoryProportions;
169 }
170
171 /**
172 * discretization of gamma distribution with equal proportions in each
173 * category
174 */
175 private void calculateCategoryRates() {
176
177 double propVariable = 1.0;
178 int cat = 0;
179 //System.out.println("modelChoose: "+ modelChoose.getValue(0)+" "+modelChoose.getValue(1));
180 //If including the site invariant parameter.
181
182 categoryRates[0] = 0.0;
183 //back transform from logit space
184 categoryProportions[0] = modelChoose.getValue(INVAR_INDEX)*(1/(1+Math.exp(-logitInvar.getValue(0))));
185
186 propVariable = 1.0 - categoryProportions[0];
187 cat = 1;
188
189
190 //If including the gamma shape parameter.
191 if (modelChoose.getValue(SHAPE_INDEX) == PRESENT) {
192
193 //back transform from log-space
194 final double a = Math.exp(logShape.getValue(0));
195 double mean = 0.0;
196 final int gammaCatCount = categoryCount - cat;
197
198 for (int i = 0; i < gammaCatCount; i++) {
199
200 categoryRates[i + cat] = GammaDistribution.quantile((2.0 * i + 1.0) / (2.0 * gammaCatCount), a, 1.0 / a);
201 //sum of the gamma categorical rates
202 mean += categoryRates[i + cat];
203
204 categoryProportions[i + cat] = propVariable / gammaCatCount;
205 }
206
207 //mean rate over all categories.
208 mean = (propVariable * mean) / gammaCatCount;
209
210 for (int i = 0; i < gammaCatCount; i++) {
211 //divide rates by the mean so that the average across all sites equals to 1.0
212 categoryRates[i + cat] /= mean;
213
214 }
215 } else {
216
217 final int gammaCatCount = categoryCount - cat;
218 for (int i = 0; i < gammaCatCount; i++) {
219 categoryRates[i + cat] = 1.0 / propVariable;
220 categoryProportions[i + cat] = propVariable/gammaCatCount;
221 }
222 }
223
224 /*for(int i = 0; i < categoryRates.length;i++){
225 System.out.print(categoryRates[i]+" ");
226 }
227 System.out.println();*/
228 ratesKnown = true;
229 }
230
231 /**
232 * Get the frequencyModel for this SiteModel.
233 *
234 * @return the frequencyModel.
235 */
236 public FrequencyModel getFrequencyModel() {
237 return substitutionModel.getFrequencyModel();
238 }
239
240 /**
241 * Get the substitutionModel for this SiteModel.
242 *
243 * @return the substitutionModel.
244 */
245 public SubstitutionModel getSubstitutionModel() {
246 return substitutionModel;
247 }
248
249 // *****************************************************************
250 // Interface ModelComponent
251 // *****************************************************************
252
253 protected void handleModelChangedEvent(Model model, Object object, int index) {
254 // Substitution model has changed so fire model changed event
255 listenerHelper.fireModelChanged(this, object, index);
256 }
257
258 protected final void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
259 if (variable == logShape) {
260 ratesKnown = false;
261 } else if (variable == logitInvar) {
262 ratesKnown = false;
263 } else if (variable == modelChoose) {
264 //System.out.println("Changing model");
265 ratesKnown = false;
266 } else {
267 // is the muParameter and nothing needs to be done
268 }
269 listenerHelper.fireModelChanged(this, variable, index);
270 }
271
272 protected void storeState() {
273 /*System.out.println("store, modelChoose: "+ modelChoose.getValue(0)+" "+modelChoose.getValue(1) );
274 for(int i = 0; i < categoryRates.length;i++){
275 System.out.print(categoryRates[i]+" ");
276 }
277 System.out.println();*/
278 } // no additional state needs storing
279
280 protected void restoreState() {
281 //System.out.println("restore, modelChoose: "+ modelChoose.getValue(0)+" "+modelChoose.getValue(1) );
282 ratesKnown = false;
283 }
284
285 protected void acceptState() {
286 } // no additional state needs accepting
287 }