| 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.*; |
| 29 |
import dr.inference.loggers.LogColumn; |
| 30 |
import dr.inference.loggers.NumberColumn; |
| 31 |
import dr.inference.model.*; |
| 32 |
import dr.util.Citable; |
| 33 |
import dr.util.Citation; |
| 34 |
import dr.util.CommonCitations; |
| 35 |
|
| 36 |
import java.util.*; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public class SVSGeneralSubstitutionModel extends GeneralSubstitutionModel implements Likelihood, |
| 47 |
BayesianStochasticSearchVariableSelection, Citable { |
| 48 |
|
| 49 |
|
| 50 |
public SVSGeneralSubstitutionModel(DataType dataType, FrequencyModel freqModel, Parameter parameter, |
| 51 |
Parameter indicator) { |
| 52 |
super(dataType, freqModel, parameter, 1); |
| 53 |
|
| 54 |
if (indicator != null) { |
| 55 |
rateIndicator = indicator; |
| 56 |
addVariable(rateIndicator); |
| 57 |
} else { |
| 58 |
rateIndicator = new Parameter.Default(parameter.getDimension(), 1.0); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
protected SVSGeneralSubstitutionModel(String name, DataType dataType, FrequencyModel freqModel, int relativeTo) { |
| 63 |
super(name, dataType, freqModel, relativeTo); |
| 64 |
} |
| 65 |
|
| 66 |
public Parameter getIndicators() { |
| 67 |
return rateIndicator; |
| 68 |
} |
| 69 |
|
| 70 |
public boolean validState() { |
| 71 |
return !updateMatrix || BayesianStochasticSearchVariableSelection.Utils.connectedAndWellConditioned(probability,this); |
| 72 |
} |
| 73 |
|
| 74 |
protected void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) { |
| 75 |
if (variable == ratesParameter && rateIndicator.getParameterValue(index) == 0) |
| 76 |
return; |
| 77 |
super.handleVariableChangedEvent(variable,index,type); |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
public Model getModel() { |
| 86 |
return this; |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
public double getLogLikelihood() { |
| 95 |
if (updateMatrix) { |
| 96 |
if (!BayesianStochasticSearchVariableSelection.Utils.connectedAndWellConditioned(probability,this)) { |
| 97 |
return Double.NEGATIVE_INFINITY; |
| 98 |
} |
| 99 |
} |
| 100 |
return 0; |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
public boolean evaluateEarly() { |
| 108 |
return true; |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
public void makeDirty() { |
| 115 |
updateMatrix = true; |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
public String prettyName() { |
| 122 |
return "SVSGeneralSubstitutionModel-connectedness"; |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
public LogColumn[] getColumns() { |
| 130 |
return new LogColumn[]{ |
| 131 |
new LikelihoodColumn(getId()) |
| 132 |
}; |
| 133 |
} |
| 134 |
|
| 135 |
protected class LikelihoodColumn extends NumberColumn { |
| 136 |
public LikelihoodColumn(String label) { |
| 137 |
super(label); |
| 138 |
} |
| 139 |
|
| 140 |
public double getDoubleValue() { |
| 141 |
return getLogLikelihood(); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
private double[] probability = null; |
| 146 |
|
| 147 |
protected void setupRelativeRates() { |
| 148 |
|
| 149 |
for (int i = 0; i < relativeRates.length; i++) { |
| 150 |
relativeRates[i] = ratesParameter.getParameterValue(i) * rateIndicator.getParameterValue(i); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
void normalize(double[][] matrix, double[] pi) { |
| 155 |
double subst = 0.0; |
| 156 |
int dimension = pi.length; |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
for (int i = 0; i < dimension; i++) |
| 165 |
subst += -matrix[i][i] * pi[i]; |
| 166 |
|
| 167 |
for (int i = 0; i < dimension; i++) { |
| 168 |
for (int j = 0; j < dimension; j++) { |
| 169 |
matrix[i][j] = matrix[i][j] / subst; |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
@Override |
| 175 |
public Set<Likelihood> getLikelihoodSet() { |
| 176 |
return new HashSet<Likelihood>(Arrays.asList(this)); |
| 177 |
} |
| 178 |
|
| 179 |
@Override |
| 180 |
public boolean isUsed() { |
| 181 |
return super.isUsed() && isUsed; |
| 182 |
} |
| 183 |
|
| 184 |
public void setUsed() { |
| 185 |
isUsed = true; |
| 186 |
} |
| 187 |
|
| 188 |
private boolean isUsed = false; |
| 189 |
|
| 190 |
private Parameter rateIndicator; |
| 191 |
|
| 192 |
@Override |
| 193 |
public Citation.Category getCategory() { |
| 194 |
return Citation.Category.SUBSTITUTION_MODELS; |
| 195 |
} |
| 196 |
|
| 197 |
@Override |
| 198 |
public String getDescription() { |
| 199 |
return "Stochastic search variable selection, reversible substitution model"; |
| 200 |
} |
| 201 |
|
| 202 |
@Override |
| 203 |
public List<Citation> getCitations() { |
| 204 |
return Collections.singletonList(CommonCitations.LEMEY_2009_BAYESIAN); |
| 205 |
} |
| 206 |
} |