| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 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 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public class LinearBiasModel extends OnePhaseModel{ |
| 40 |
|
| 41 |
|
| 42 |
|
| 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 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 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 |
|
| 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 |
|
| 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 |
} |