| 1 |
|
| 2 |
package dr.evomodel.antigenic.phyloclustering.statistics; |
| 3 |
|
| 4 |
import dr.evomodel.tree.TreeModel; |
| 5 |
import dr.inference.model.*; |
| 6 |
import dr.xml.*; |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
public class DriftedMuStatistic extends Statistic.Abstract implements VariableListener { |
| 14 |
|
| 15 |
static int MAX_DIM = 30; |
| 16 |
|
| 17 |
|
| 18 |
private MatrixParameter mu; |
| 19 |
private TreeModel treeModel; |
| 20 |
private Parameter indicators; |
| 21 |
|
| 22 |
private Parameter mu1ScaleParameter; |
| 23 |
private Parameter mu2ScaleParameter; |
| 24 |
private Parameter muMeanParameter; |
| 25 |
|
| 26 |
public static final String DRIFTED_MU_STATISTIC = "driftedMuStatistic"; |
| 27 |
|
| 28 |
|
| 29 |
public DriftedMuStatistic( TreeModel tree, MatrixParameter mu, Parameter indicators, Parameter mu1Scale, Parameter mu2Scale, Parameter muMean) { |
| 30 |
|
| 31 |
this.treeModel = tree; |
| 32 |
this.mu = mu; |
| 33 |
this.indicators = indicators; |
| 34 |
|
| 35 |
this.mu1ScaleParameter = mu1Scale; |
| 36 |
mu1ScaleParameter.addParameterListener(this); |
| 37 |
this.mu2ScaleParameter = mu2Scale; |
| 38 |
mu2ScaleParameter.addParameterListener(this); |
| 39 |
|
| 40 |
this.muMeanParameter = muMean; |
| 41 |
muMeanParameter.addParameterListener(this); |
| 42 |
|
| 43 |
mu.addParameterListener(this); |
| 44 |
indicators.addParameterListener(this); |
| 45 |
|
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public int getDimension() { |
| 51 |
return treeModel.getNodeCount()*2; |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
public double getStatisticValue(int dim) { |
| 59 |
|
| 60 |
int curNode = dim/2; |
| 61 |
double value = mu.getParameter(curNode).getParameterValue(dim % 2); |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
if( dim % 2 == 0 ){ |
| 69 |
|
| 70 |
value = value * mu1ScaleParameter.getParameterValue(0); |
| 71 |
} |
| 72 |
else{ |
| 73 |
value = value * mu2ScaleParameter.getParameterValue(0); |
| 74 |
|
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
return ( value ); |
| 79 |
|
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
public String getDimensionName(int dim) { |
| 88 |
String name = "mu_" + ((dim/2) ) + "-" + ((dim %2 ) +1 ); |
| 89 |
return name; |
| 90 |
} |
| 91 |
|
| 92 |
public void variableChangedEvent(Variable variable, int index, Parameter.ChangeType type) { |
| 93 |
|
| 94 |
|
| 95 |
} |
| 96 |
|
| 97 |
public static XMLObjectParser PARSER = new AbstractXMLObjectParser() { |
| 98 |
|
| 99 |
public final static String MU_STRING = "mu"; |
| 100 |
public final static String INDICATORS_STRING = "indicators"; |
| 101 |
|
| 102 |
public final static String MU1_SCALE_PARAMETER = "mu1Scale"; |
| 103 |
public final static String MU2_SCALE_PARAMETER = "mu2Scale"; |
| 104 |
public final static String MU_MEAN_PARAMETER = "muMean"; |
| 105 |
|
| 106 |
public String getParserName() { |
| 107 |
return DRIFTED_MU_STATISTIC; |
| 108 |
} |
| 109 |
|
| 110 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 111 |
TreeModel treeModel = (TreeModel) xo.getChild(TreeModel.class); |
| 112 |
MatrixParameter muParam = (MatrixParameter) xo.getElementFirstChild(MU_STRING); |
| 113 |
Parameter indicators = (Parameter) xo.getElementFirstChild(INDICATORS_STRING); |
| 114 |
|
| 115 |
Parameter mu1Scale = null; |
| 116 |
if (xo.hasChildNamed(MU1_SCALE_PARAMETER)) { |
| 117 |
mu1Scale = (Parameter) xo.getElementFirstChild(MU1_SCALE_PARAMETER); |
| 118 |
} |
| 119 |
|
| 120 |
Parameter mu2Scale = null; |
| 121 |
if (xo.hasChildNamed(MU2_SCALE_PARAMETER)) { |
| 122 |
mu2Scale = (Parameter) xo.getElementFirstChild(MU2_SCALE_PARAMETER); |
| 123 |
} |
| 124 |
|
| 125 |
Parameter muMean = null; |
| 126 |
if(xo.hasChildNamed(MU_MEAN_PARAMETER)){ |
| 127 |
muMean = (Parameter) xo.getElementFirstChild(MU_MEAN_PARAMETER); |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
return new DriftedMuStatistic( treeModel, muParam, indicators, mu1Scale, mu2Scale, muMean); |
| 132 |
|
| 133 |
|
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
public String getParserDescription() { |
| 141 |
return "This element returns a statistic that shifts a matrix of locations by location drift in the first dimension."; |
| 142 |
} |
| 143 |
|
| 144 |
public Class getReturnType() { |
| 145 |
return DriftedMuStatistic.class; |
| 146 |
} |
| 147 |
|
| 148 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 149 |
return rules; |
| 150 |
} |
| 151 |
|
| 152 |
private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{ |
| 153 |
new ElementRule(TreeModel.class), |
| 154 |
new ElementRule(MU_STRING, Parameter.class), |
| 155 |
new ElementRule(INDICATORS_STRING, Parameter.class), |
| 156 |
|
| 157 |
new ElementRule(MU1_SCALE_PARAMETER, Parameter.class, "Optional parameter for scaling the first dimension of mu"), |
| 158 |
new ElementRule(MU2_SCALE_PARAMETER, Parameter.class, "Optional parameter for scaling the second dimension of mu"), |
| 159 |
new ElementRule(MU_MEAN_PARAMETER, Parameter.class) |
| 160 |
}; |
| 161 |
}; |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
} |