Debian Codegraph @

src/dr/oldevomodel/substmodel/AsymmetricQuadraticModel.java

raw symbols SCIP
1 /*
2 * AsymmetricQuadraticModel.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.substmodel;
27
28
29 import dr.evolution.datatype.Microsatellite;
30 import dr.inference.model.Parameter;
31 import dr.inference.model.Variable;
32
33 /**
34 * @author Chieh-Hsi Wu
35 *
36 * Implements the Asymmetric Quadratic Model
37 */
38 public class AsymmetricQuadraticModel extends OnePhaseModel{
39
40 public static final String ASYMQUAD_MODEL = "ASYMQUADModel";
41
42 /*
43 *Parameters for setting up the infinitesimal rate matrix.
44 */
45 private Variable<Double> expanConst;
46 private Variable<Double> expanLin;
47 private Variable<Double> expanQuad;
48 private Variable<Double> contractConst;
49 private Variable<Double> contractLin;
50 private Variable<Double> contractQuad;
51
52
53 /**
54 * Constructor
55 *
56 * @param microsatellite Mirosatellite data type
57 * @param freqModel Frequency model
58 */
59 public AsymmetricQuadraticModel(Microsatellite microsatellite, FrequencyModel freqModel){
60 this(
61 microsatellite,
62 freqModel,
63 null, null, null, null, null, null,
64 false);
65
66 }
67
68
69 public AsymmetricQuadraticModel(Microsatellite microsatellite, FrequencyModel freqModel, boolean isNested){
70 this(
71 microsatellite,
72 freqModel,
73 null, null, null, null, null, null,
74 isNested
75 );
76
77 }
78
79
80 /**
81 * Constructor
82 *
83 * @param microsatellite Mirosatellite data type
84 * @param freqModel Frequency model
85 * @param expanConst Expansion constant
86 * @param expanLin Expansion linear coefficient
87 * @param expanQuad Expansion quadratic coefficient
88 * @param contractConst Contraction constant
89 * @param contractLin Contraction linear coefficient
90 * @param contractQuad Contraction quadratic coefficient
91 * @param isNested boolean indicating whether this object is a submodel of another microsatellite model
92 */
93 public AsymmetricQuadraticModel(
94 Microsatellite microsatellite,
95 FrequencyModel freqModel,
96 Variable<Double> expanConst,
97 Variable<Double> expanLin,
98 Variable<Double> expanQuad,
99 Variable<Double> contractConst,
100 Variable<Double> contractLin,
101 Variable<Double> contractQuad,
102 boolean isNested){
103
104 super(ASYMQUAD_MODEL, microsatellite, freqModel,null);
105
106
107 //The default setting of the parameters gives the same infinitesimal rates
108 // as the StepwiseMutaionalModel class.
109 this.expanConst = overrideDefault(new Parameter.Default(1.0), expanConst);
110 this.expanLin = overrideDefault(new Parameter.Default(0.0), expanLin);
111 this.expanQuad = overrideDefault(new Parameter.Default(0.0), expanQuad);
112 this.contractConst = overrideDefault(this.expanConst, contractConst);
113 this.contractLin = overrideDefault(this.expanLin, contractLin);
114 this.contractQuad = overrideDefault(this.expanQuad, contractQuad);
115 this.isNested = isNested;
116 addParameters();
117
118 //printDetails();
119
120 setupInfinitesimalRates();
121
122 //calculate the default frequencies when not provieded by the user.
123 if(freqModel == null){
124 useStationaryFreqs = true;
125 computeStationaryDistribution();
126 }else{
127 this.freqModel = freqModel;
128 }
129
130 addModel(this.freqModel);
131
132 }
133
134 private void addParameters(){
135 addParam(this.expanConst);
136 addParam(this.expanLin);
137 addParam(this.expanQuad);
138 if(this.contractConst != this.expanConst)
139 addParam(this.contractConst);
140 if(this.contractLin != this.expanLin)
141 addParam(this.contractLin);
142 if(this.contractQuad != this.expanQuad)
143 addParam(this.contractQuad);
144 }
145
146
147
148
149 /*
150 * This method will override the default value of the parameter using the value specified by the user.
151 */
152 private Variable<Double> overrideDefault(Variable<Double> defaultParam, Variable<Double> providedParam){
153 if(providedParam != null && providedParam != defaultParam)
154 return providedParam;
155 return defaultParam;
156 }
157
158
159 /*
160 * Setting up the infinitesimal Rates
161 * The rates are defined by the following equations:
162 * X -> X + 1 at rate u0 + u1(X - k) + u2(X - k)^2
163 * X -> X - 1 at rate d0 + d1(X - k) + d2(X - k)^2
164 */
165 public void setupInfinitesimalRates(){
166
167
168 double u0 = expanConst.getValue(0);
169 double u1 = expanLin.getValue(0);
170 double u2 = expanQuad.getValue(0);
171
172 double d0 = contractConst.getValue(0);
173 double d1 = contractLin.getValue(0);
174 double d2 = contractQuad.getValue(0);
175
176 double rowSum;
177 for(int i = 0; i < stateCount;i++){
178 rowSum = 0.0;
179 if(i - 1 > -1){
180 infinitesimalRateMatrix[i][i - 1] =d0+d1*i+d2*i*i;
181 rowSum = rowSum + infinitesimalRateMatrix[i][i - 1];
182
183 }
184
185 if(i + 1 < stateCount){
186 infinitesimalRateMatrix[i][i + 1] = u0+u1*i+u2*i*i;
187 rowSum = rowSum + infinitesimalRateMatrix[i][i + 1];
188
189 }
190
191 infinitesimalRateMatrix[i][i] = rowSum*-1;
192
193 }
194
195
196 }
197
198
199 public Variable<Double> getExpansionConstant(){
200 return expanConst;
201 }
202
203 public Variable<Double> getExpansionLinear(){
204 return expanLin;
205 }
206
207 public Variable<Double> getExpansionQuad(){
208 return expanQuad;
209 }
210
211 public Variable<Double> getContractionConstant(){
212 return contractConst;
213 }
214
215 public Variable<Double> getContractionLinear(){
216 return contractLin;
217 }
218
219 public Variable<Double> getContractionQuad(){
220 return contractQuad;
221 }
222
223
224
225 public void printDetails(){
226 System.out.println("\n");
227 System.out.println("Details of the asymmetric quadratic model and its parameters:");
228 System.out.println("expansion constant: "+expanConst.getValue(0));
229 System.out.println("expansion linear: "+ expanLin.getValue(0));
230 System.out.println("expansion quadratic: "+expanQuad.getValue(0));
231 System.out.println("contraction constant: "+contractConst.getValue(0));
232 System.out.println("contraction linear: "+contractLin.getValue(0));
233 System.out.println("contraction quadratc: "+contractQuad.getValue(0));
234 System.out.println("a submodel: "+isNested);
235 System.out.println("\n");
236 }
237
238 }