| 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.DataType; |
| 29 |
import dr.inference.model.BayesianStochasticSearchVariableSelection; |
| 30 |
import dr.inference.model.Parameter; |
| 31 |
import dr.inference.model.Variable; |
| 32 |
import dr.math.matrixAlgebra.Vector; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public class SVSComplexSubstitutionModel extends ComplexSubstitutionModel implements BayesianStochasticSearchVariableSelection { |
| 38 |
|
| 39 |
public SVSComplexSubstitutionModel(String name, DataType dataType, FrequencyModel rootFreqModel, |
| 40 |
Parameter rates, Parameter indicators) { |
| 41 |
super(name, dataType, rootFreqModel, rates); |
| 42 |
if (indicators != null) { |
| 43 |
this.indicators = indicators; |
| 44 |
addVariable(indicators); |
| 45 |
} else { |
| 46 |
this.indicators = new Parameter.Default(rates.getDimension(), 1.0); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
protected double[] getRates() { |
| 51 |
double[] rates = infinitesimalRates.getParameterValues(); |
| 52 |
double[] indies = indicators.getParameterValues(); |
| 53 |
final int dim = rates.length; |
| 54 |
for (int i = 0; i < dim; i++) |
| 55 |
rates[i] *= indies[i]; |
| 56 |
return rates; |
| 57 |
} |
| 58 |
|
| 59 |
protected void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) { |
| 60 |
if (variable == infinitesimalRates && indicators.getParameterValue(index) == 0) |
| 61 |
return; |
| 62 |
super.handleVariableChangedEvent(variable, index, type); |
| 63 |
} |
| 64 |
|
| 65 |
public Parameter getIndicators() { |
| 66 |
return indicators; |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
public boolean validState() { |
| 112 |
return getLogLikelihood() == 0; |
| 113 |
} |
| 114 |
|
| 115 |
public static void main(String[] arg) { |
| 116 |
|
| 117 |
double[] rates = new double[]{0.097,0.515,3.346,0.623,0.389,0.631,0.362,1.127,4.262,0.424,0.758,1.297,0.728,0.228,0.003,0.075,0.312,0.356,0.927,4.420,2.719,0.264,4.267,0.741,1.106,1.568,1.215,0.172,0.204,1.493,0.592,0.105,1.583,1.201,0.783,2.224,0.888,1.401,0.137,0.259,1.197,0.056,1.939,1.385,0.690,0.815,0.279,1.100,1.715,0.011,1.509,0.961,0.112,1.305,2.797,0.578,1.177,1.009,0.316,1.143,1.861,0.176,0.140,0.104,0.571,0.521,0.761,1.795,1.065,1.563,2.972,2.295,0.075,1.690,1.011,0.128,0.484,0.355,1.668,1.052,0.089,0.104,0.056,1.591,0.054,0.487,1.034,1.145,0.403,0.254,0.474,0.181,0.124,2.067,2.208,0.120,2.638,0.195,1.897,1.955,1.113,1.399,4.901,3.218,0.361,1.934,1.681,3.572,0.806,0.077,0.042,0.310,0.243,1.526,3.572,4.173,0.740,3.086,0.645,2.755,0.280,2.476,0.476,5.174,0.057,0.225,1.310,0.201,0.491,1.507,0.604,0.404,0.907,0.048,2.439,0.676,0.382,0.062,1.173,2.026,2.813,0.655,1.511,0.452,0.137,0.435,0.685,0.826,1.735,0.935,0.697,1.230,5.416,1.043,0.747,0.945}; |
| 118 |
double[] indicators = new double[]{1.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,1.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,1.000,0.000,0.000,0.000,0.000,1.000,0.000,0.000,0.000,0.000,1.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,1.000,0.000,0.000,0.000,0.000,0.000,1.000,0.000,0.000,1.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,1.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,1.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,1.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,1.000,0.000,0.000,0.000,0.000,1.000,0.000,0.000,0.000,0.000,0.000,1.000,0.000,0.000,0.000,0.000,1.000,0.000,0.000,0.000,1.000,1.000,1.000,0.000,1.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,1.000}; |
| 119 |
|
| 120 |
Parameter ratesP = new Parameter.Default(rates); |
| 121 |
Parameter indicatorsP = new Parameter.Default(indicators); |
| 122 |
|
| 123 |
DataType dataType = new DataType() { |
| 124 |
|
| 125 |
public String getDescription() { |
| 126 |
return null; |
| 127 |
} |
| 128 |
|
| 129 |
public int getType() { |
| 130 |
return 0; |
| 131 |
} |
| 132 |
|
| 133 |
@Override |
| 134 |
public char[] getValidChars() { |
| 135 |
return null; |
| 136 |
} |
| 137 |
|
| 138 |
public int getStateCount() { |
| 139 |
return 13; |
| 140 |
} |
| 141 |
}; |
| 142 |
|
| 143 |
FrequencyModel freqModel = new FrequencyModel(dataType, new Parameter.Default(400, 1.0/400.0)); |
| 144 |
|
| 145 |
SVSComplexSubstitutionModel substModel = new SVSComplexSubstitutionModel("test", |
| 146 |
dataType, |
| 147 |
freqModel, |
| 148 |
ratesP,indicatorsP); |
| 149 |
double logL = substModel.getLogLikelihood(); |
| 150 |
System.out.println("Prior = "+logL); |
| 151 |
if( !Double.isInfinite(logL) ) { |
| 152 |
double[] finiteTimeProbs = new double[substModel.getDataType().getStateCount() * substModel.getDataType().getStateCount()]; |
| 153 |
double time = 1.0; |
| 154 |
substModel.getTransitionProbabilities(time,finiteTimeProbs); |
| 155 |
System.out.println("Probs = "+new Vector(finiteTimeProbs)); |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
private Parameter indicators; |
| 160 |
|
| 161 |
|
| 162 |
} |