| 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.evomodel.substmodel; |
| 27 |
|
| 28 |
import dr.evolution.datatype.DataType; |
| 29 |
import dr.inference.glm.GeneralizedLinearModel; |
| 30 |
import dr.inference.loggers.LogColumn; |
| 31 |
import dr.inference.model.BayesianStochasticSearchVariableSelection; |
| 32 |
import dr.inference.model.Model; |
| 33 |
import dr.util.Citation; |
| 34 |
import dr.util.CommonCitations; |
| 35 |
|
| 36 |
import java.util.Collections; |
| 37 |
import java.util.List; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
public class GLMSubstitutionModel extends ComplexSubstitutionModel { |
| 43 |
|
| 44 |
public GLMSubstitutionModel(String name, DataType dataType, FrequencyModel rootFreqModel, |
| 45 |
GeneralizedLinearModel glm) { |
| 46 |
|
| 47 |
super(name, dataType, rootFreqModel, null); |
| 48 |
this.glm = glm; |
| 49 |
addModel(glm); |
| 50 |
testProbabilities = new double[stateCount*stateCount]; |
| 51 |
|
| 52 |
} |
| 53 |
|
| 54 |
public double[] getRates() { |
| 55 |
return glm.getXBeta(); |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
protected void handleModelChangedEvent(Model model, Object object, int index) { |
| 60 |
if (model == glm) { |
| 61 |
updateMatrix = true; |
| 62 |
fireModelChanged(); |
| 63 |
} else |
| 64 |
super.handleModelChangedEvent(model, object, index); |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
public LogColumn[] getColumns() { |
| 73 |
|
| 74 |
LogColumn[] aggregated = new LogColumn[glm.getColumns().length + 2]; |
| 75 |
int index = 0; |
| 76 |
for (LogColumn col : glm.getColumns()) { |
| 77 |
aggregated[index] = col; |
| 78 |
index++; |
| 79 |
} |
| 80 |
aggregated[index++] = new LikelihoodColumn(getId() + ".L"); |
| 81 |
aggregated[index++] = new NormalizationColumn(getId() + ".Norm"); |
| 82 |
|
| 83 |
return aggregated; |
| 84 |
|
| 85 |
} |
| 86 |
|
| 87 |
public double getLogLikelihood() { |
| 88 |
double logL = super.getLogLikelihood(); |
| 89 |
if (logL == 0 && |
| 90 |
BayesianStochasticSearchVariableSelection.Utils.connectedAndWellConditioned(testProbabilities, this)) { |
| 91 |
|
| 92 |
return 0; |
| 93 |
} |
| 94 |
return Double.NEGATIVE_INFINITY; |
| 95 |
} |
| 96 |
|
| 97 |
@Override |
| 98 |
public String getDescription() { |
| 99 |
return "Generalized linear (model, GLM) substitution model"; |
| 100 |
} |
| 101 |
|
| 102 |
@Override |
| 103 |
public List<Citation> getCitations() { |
| 104 |
|
| 105 |
return Collections.singletonList(CommonCitations.LEMEY_2014_UNIFYING); |
| 106 |
} |
| 107 |
|
| 108 |
private GeneralizedLinearModel glm; |
| 109 |
private double[] testProbabilities; |
| 110 |
} |