| 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.continuous; |
| 27 |
|
| 28 |
import dr.xml.*; |
| 29 |
import dr.inference.model.MatrixParameter; |
| 30 |
import dr.inference.model.Parameter; |
| 31 |
import dr.inference.model.Variable; |
| 32 |
import dr.evolution.tree.Tree; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public class BiasedMultivariateDiffusionModel extends MultivariateDiffusionModel { |
| 39 |
|
| 40 |
public static final String BIASED_DIFFUSION_PROCESS = "biasedMultivariateDiffusionModel"; |
| 41 |
public static final String BIAS_PARAMETER = "biasParameter"; |
| 42 |
public static final String BIAS_TREE_ATTRIBUTE = "bias"; |
| 43 |
|
| 44 |
BiasedMultivariateDiffusionModel(Parameter biasParam, MatrixParameter diffusionParam) { |
| 45 |
super(diffusionParam); |
| 46 |
this.biasParam = biasParam; |
| 47 |
addVariable(biasParam); |
| 48 |
} |
| 49 |
|
| 50 |
protected void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) { |
| 51 |
if (variable == diffusionPrecisionMatrixParameter) |
| 52 |
calculatePrecisionInfo(); |
| 53 |
|
| 54 |
} |
| 55 |
|
| 56 |
@Override |
| 57 |
protected double calculateLogDensity(double[] start, double[] stop, double time) { |
| 58 |
|
| 59 |
final int dim = stop.length; |
| 60 |
double[] bias = biasParam.getParameterValues(); |
| 61 |
for (int i = 0; i < dim; i++) { |
| 62 |
bias[i] *= time; |
| 63 |
bias[i] += start[i]; |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
return super.calculateLogDensity(bias, stop, time); |
| 69 |
} |
| 70 |
|
| 71 |
public String[] getTreeAttributeLabel() { |
| 72 |
return new String[]{ |
| 73 |
PRECISION_TREE_ATTRIBUTE, |
| 74 |
BIAS_TREE_ATTRIBUTE |
| 75 |
}; |
| 76 |
} |
| 77 |
|
| 78 |
public String[] getAttributeForTree(Tree tree) { |
| 79 |
return new String[]{ |
| 80 |
diffusionPrecisionMatrixParameter.toSymmetricString(), |
| 81 |
toParameterString(biasParam) |
| 82 |
}; |
| 83 |
} |
| 84 |
|
| 85 |
private String toParameterString(Parameter param) { |
| 86 |
StringBuffer sb = new StringBuffer("{"); |
| 87 |
int dim = param.getDimension(); |
| 88 |
for (int i = 0; i < dim; i++) { |
| 89 |
sb.append(String.format("%5.4e", param.getParameterValue(i))); |
| 90 |
if (i < dim - 1) |
| 91 |
sb.append(","); |
| 92 |
} |
| 93 |
sb.append("}"); |
| 94 |
return sb.toString(); |
| 95 |
} |
| 96 |
|
| 97 |
public static XMLObjectParser PARSER = new AbstractXMLObjectParser() { |
| 98 |
|
| 99 |
public String getParserName() { |
| 100 |
return BIASED_DIFFUSION_PROCESS; |
| 101 |
} |
| 102 |
|
| 103 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 104 |
|
| 105 |
XMLObject cxo = xo.getChild(BIAS_PARAMETER); |
| 106 |
Parameter biasParam = (Parameter) cxo.getChild(Parameter.class); |
| 107 |
|
| 108 |
cxo = xo.getChild(DIFFUSION_CONSTANT); |
| 109 |
MatrixParameter diffusionParam = (MatrixParameter) cxo.getChild(MatrixParameter.class); |
| 110 |
|
| 111 |
return new BiasedMultivariateDiffusionModel(biasParam, diffusionParam); |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
public String getParserDescription() { |
| 119 |
return "Describes a multivariate normal diffusion process."; |
| 120 |
} |
| 121 |
|
| 122 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 123 |
return rules; |
| 124 |
} |
| 125 |
|
| 126 |
private final XMLSyntaxRule[] rules = { |
| 127 |
new ElementRule(DIFFUSION_CONSTANT, |
| 128 |
new XMLSyntaxRule[]{new ElementRule(MatrixParameter.class)}), |
| 129 |
new ElementRule(BIAS_PARAMETER, |
| 130 |
new XMLSyntaxRule[]{new ElementRule(Parameter.class)}), |
| 131 |
}; |
| 132 |
|
| 133 |
public Class getReturnType() { |
| 134 |
return MultivariateDiffusionModel.class; |
| 135 |
} |
| 136 |
}; |
| 137 |
|
| 138 |
private Parameter biasParam; |
| 139 |
|
| 140 |
} |