Debian Codegraph @

src/dr/oldevomodel/substmodel/LinearBiasModel.java

raw symbols SCIP
1 /*
2 * LinearBiasModel.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 import dr.evolution.datatype.Microsatellite;
29 import dr.inference.model.Parameter;
30 import dr.inference.model.Variable;
31
32 import java.util.ArrayList;
33
34 /**
35 * @author Chieh-Hsi Wu
36 *
37 * Implements the LinearBiasModel of microsatellites.
38 */
39 public class LinearBiasModel extends OnePhaseModel{
40
41 /*
42 *Parameters for setting up the infinitesimal rate matrix.
43 */
44 private Parameter biasConst;
45 private Parameter biasLin;
46 private ArrayList<Variable<Double>> submodelParameters = null;
47 private boolean estimateSubmodelParams = false;
48 private boolean updateSubmodelRates = false;
49 private boolean inLogitSpace = false;
50
51 public static final double delta = 1e-15;
52
53 public static final String LINEAR_BIAS_MODEL = "LINEARBIASModel";
54
55
56 /**
57 * Constructor
58 *
59 * @param microsatellite microsatellite data type
60 * @param freqModel user specified initial equilibrium frequencies
61 * @param submodel submodel of this linear bias model
62 * @param biasConst bias constant parameter
63 * @param biasLinear bias linear parameter
64 * @param inLogitSpace indicates whether the bias parameters are in the logit space
65 * @param estimateSubmodelParams inidicate whether the parameters of submodel will be estimated
66 * @param isSubmodel inidicate whether this model is a submodel of another microsatellite model
67 */
68 public LinearBiasModel(
69 Microsatellite microsatellite,
70 FrequencyModel freqModel,
71 OnePhaseModel submodel,
72 Parameter biasConst,
73 Parameter biasLinear,
74 boolean inLogitSpace,
75 boolean estimateSubmodelParams,
76 boolean isSubmodel){
77
78 super(LINEAR_BIAS_MODEL, microsatellite, freqModel, null);
79
80 isNested = isSubmodel;
81 this.subModel = submodel;
82 this.estimateSubmodelParams = estimateSubmodelParams;
83 if(this.estimateSubmodelParams){
84 submodelParameters = new ArrayList<Variable<Double>>();
85 for(int i = 0; i < subModel.getNestedParameterCount(); i++){
86
87 if(isNested){
88 addVariable(subModel.getNestedParameter(i));
89 }
90
91 addParam(subModel.getNestedParameter(i));
92 submodelParameters.add(subModel.getNestedParameter(i));
93 }
94 updateSubmodelRates = true;
95 }
96
97
98
99 //The default setting of the parameters gives infinitesimal rates with no directional bias.
100 if(biasConst != null){
101 this.biasConst = biasConst;
102 }else{
103 this.biasConst = new Parameter.Default(0.5);
104 }
105
106
107 if(biasLinear != null){
108 biasLin = biasLinear;
109 }else{
110 biasLin = new Parameter.Default(0.0);
111 }
112 addParam(this.biasConst);
113 addParam(this.biasLin);
114
115 this.inLogitSpace = inLogitSpace;
116
117 //printDetails();
118
119 setupInfinitesimalRates();
120
121 if(freqModel == null){
122 useStationaryFreqs = true;
123 computeStationaryDistribution();
124 }else{
125 this.freqModel = freqModel;
126
127 }
128 addModel(this.freqModel);
129
130 }
131 protected void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
132 if(submodelParameters !=null && submodelParameters.indexOf((Parameter)variable) != -1){
133 updateSubmodelRates = true;
134 }
135 updateMatrix = true;
136
137 }
138
139 public void setupInfinitesimalRates(){
140 if(updateSubmodelRates){
141 subModel.setupInfinitesimalRates();
142 updateSubmodelRates = false;
143 }
144
145 double biasConst = this.biasConst.getParameterValue(0);
146 double biasLin = this.biasLin.getParameterValue(0);
147 setupInfinitesimalRates(
148 infinitesimalRateMatrix,
149 subModel.getInfinitesimalRates(),
150 biasConst,
151 biasLin,
152 stateCount,
153 inLogitSpace
154 );
155
156 }
157
158
159 public static void setupInfinitesimalRates(
160 double[][] rates,
161 double[][] subModelRateMatrix,
162 double biasConst,
163 double biasLin,
164 int stateCount,
165 boolean inLogitSpace){
166 double rowSum;
167 double expansionProb = 0.5;
168 for(int i = 0; i < stateCount;i++){
169 rowSum = 0.0;
170 expansionProb = computeExpansionProb(biasConst,biasLin,i, inLogitSpace);
171 if(expansionProb < delta){
172 System.out.println("changing expan prob from " + expansionProb+ " to " + delta
173 +"\nbiasConst: "+biasConst+", biasLin: "+biasLin);
174 expansionProb = delta;
175
176 }else if (expansionProb > (1.0-delta)){
177
178 System.out.println("changing expan prob from " + expansionProb+ " to " + (1.0-delta)
179 +"\nbiasConst: "+biasConst+", biasLin: "+biasLin);
180 expansionProb = 1.0-delta;
181 }
182 if(i - 1 > -1){
183 rates[i][i - 1] = subModelRateMatrix[i][i-1]*(1.0 - expansionProb);
184 rowSum = rowSum+rates[i][i - 1];
185 }
186 if(i + 1 < stateCount){
187 rates[i][i + 1] = subModelRateMatrix[i][i+1]*expansionProb;
188 rowSum = rowSum + rates[i][i + 1];
189 }
190
191 rates[i][i] = rowSum*-1;
192
193 }
194 }
195
196 public static double computeExpansionProb(double biasConst, double biasLin, int length, boolean inLogitSpace){
197 double expanProb = 0.5;
198 if(inLogitSpace){
199 double numerator = Math.exp(biasConst+biasLin*length);
200 expanProb = numerator/(1+numerator);
201 }else{
202 expanProb = biasConst+biasLin*length;
203 }
204 return expanProb;
205
206 }
207
208
209 public Parameter getBiasConstant(){
210 return biasConst;
211 }
212
213 public Parameter getBiasLinearPercent(){
214 return biasLin;
215 }
216
217 public boolean isEstimatingSubmodelParams(){
218 return estimateSubmodelParams;
219 }
220
221 public boolean isInLogitSpace(){
222 return inLogitSpace;
223 }
224
225 public void printDetails(){
226 System.out.println("Details of the Linear Bias Model and its paramters:");
227 System.out.println("a submodel: "+isNested);
228 System.out.println("in logit space: "+inLogitSpace);
229 System.out.println("has submodel: "+hasSubmodel());
230 if(hasSubmodel()){
231 System.out.println("submodel class: "+subModel.getClass());
232 }
233 System.out.println("esitmating submodel parameters: "+estimateSubmodelParams);
234 System.out.println("bias constant: "+biasConst.getParameterValue(0));
235 System.out.println("bias linear coefficient: "+biasLin.getParameterValue(0));
236 }
237
238 }