| 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.inference.model; |
| 27 |
|
| 28 |
import dr.stats.DiscreteStatistics; |
| 29 |
import dr.xml.*; |
| 30 |
import org.w3c.dom.Document; |
| 31 |
import org.w3c.dom.Element; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
public class DesignMatrix extends MatrixParameter { |
| 37 |
public static final String DESIGN_MATRIX = "designMatrix"; |
| 38 |
public static final String ADD_INTERCEPT = "addIntercept"; |
| 39 |
public static final String FORM = "form"; |
| 40 |
public static final String ROW_DIMENSION = "rowDimension"; |
| 41 |
public static final String COL_DIMENSION = "colDimension"; |
| 42 |
public static final String CHECK_IDENTIFABILITY = "checkIdentifiability"; |
| 43 |
public static final String STANDARDIZE = "standardize"; |
| 44 |
public static final String DYNAMIC_STANDARDIZATION = "dynamicStandardization"; |
| 45 |
|
| 46 |
public static final String INTERCEPT = "intercept"; |
| 47 |
|
| 48 |
public DesignMatrix(String name, boolean dynamicStandardization) { |
| 49 |
super(name); |
| 50 |
this.dynamicStandardization = dynamicStandardization; |
| 51 |
init(); |
| 52 |
} |
| 53 |
|
| 54 |
public void variableChangedEvent(Variable variable, int index, Parameter.ChangeType type) { |
| 55 |
super.variableChangedEvent(variable, index, type); |
| 56 |
standardizationKnown = false; |
| 57 |
} |
| 58 |
|
| 59 |
protected double getRawParameterValue(int row, int col) { |
| 60 |
return super.getParameterValue(row, col); |
| 61 |
} |
| 62 |
|
| 63 |
public double getParameterValue(int row, int col) { |
| 64 |
double value = getRawParameterValue(row, col); |
| 65 |
if (dynamicStandardization) { |
| 66 |
if (!standardizationKnown) { |
| 67 |
computeStandarization(); |
| 68 |
standardizationKnown = true; |
| 69 |
} |
| 70 |
value = (value - standardizationMean[col]) / standardizationStDev[col]; |
| 71 |
} |
| 72 |
return value; |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
public void addParameter(Parameter param) { |
| 80 |
super.addParameter(param); |
| 81 |
clearCache(); |
| 82 |
} |
| 83 |
|
| 84 |
public void removeParameter(Parameter param) { |
| 85 |
super.removeParameter(param); |
| 86 |
clearCache(); |
| 87 |
} |
| 88 |
|
| 89 |
private void clearCache() { |
| 90 |
standardizationMean = null; |
| 91 |
standardizationStDev = null; |
| 92 |
|
| 93 |
storedStandardizationMean = null; |
| 94 |
storedStandardizationStDev = null; |
| 95 |
} |
| 96 |
|
| 97 |
private void computeStandarization() { |
| 98 |
if (standardizationMean == null) { |
| 99 |
standardizationMean = new double[getColumnDimension()]; |
| 100 |
} |
| 101 |
if (standardizationStDev == null) { |
| 102 |
standardizationStDev = new double[getColumnDimension()]; |
| 103 |
} |
| 104 |
for (int col = 0; col < getColumnDimension(); col++) { |
| 105 |
if ((getParameter(col).getId()).toLowerCase().indexOf(INTERCEPT) >= 0) { |
| 106 |
standardizationMean[col] = 0.0; |
| 107 |
standardizationStDev[col] = 1.0; |
| 108 |
} else { |
| 109 |
double[] vector = getParameter(col).getParameterValues(); |
| 110 |
standardizationMean[col] = DiscreteStatistics.mean(vector); |
| 111 |
standardizationStDev[col] = Math.sqrt(DiscreteStatistics.variance(vector, standardizationMean[col])); |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
protected void storeValues() { |
| 117 |
super.storeValues(); |
| 118 |
|
| 119 |
if (dynamicStandardization) { |
| 120 |
if (storedStandardizationMean == null) { |
| 121 |
storedStandardizationMean = new double[standardizationMean.length]; |
| 122 |
} |
| 123 |
System.arraycopy(standardizationMean, 0, storedStandardizationMean, 0, standardizationMean.length); |
| 124 |
|
| 125 |
if (storedStandardizationStDev == null) { |
| 126 |
storedStandardizationStDev = new double[standardizationStDev.length]; |
| 127 |
} |
| 128 |
System.arraycopy(standardizationStDev, 0, storedStandardizationStDev, 0, standardizationStDev.length); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
protected void restoreValues() { |
| 133 |
super.restoreValues(); |
| 134 |
|
| 135 |
if (dynamicStandardization) { |
| 136 |
double[] tmp = standardizationMean; |
| 137 |
standardizationMean = storedStandardizationMean; |
| 138 |
storedStandardizationMean = tmp; |
| 139 |
|
| 140 |
tmp = standardizationStDev; |
| 141 |
standardizationStDev = storedStandardizationStDev; |
| 142 |
storedStandardizationStDev = tmp; |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
public DesignMatrix(String name, Parameter[] parameters, boolean dynamicStandardization) { |
| 147 |
super(name, parameters); |
| 148 |
this.dynamicStandardization = dynamicStandardization; |
| 149 |
init(); |
| 150 |
} |
| 151 |
|
| 152 |
private void init() { |
| 153 |
standardizationKnown = false; |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
public Element createElement(Document d) { |
| 161 |
throw new RuntimeException("Not implemented yet!"); |
| 162 |
} |
| 163 |
|
| 164 |
public static void standardize(double[] vector) { |
| 165 |
double mean = DiscreteStatistics.mean(vector); |
| 166 |
double stDev = Math.sqrt(DiscreteStatistics.variance(vector, mean)); |
| 167 |
for (int i = 0; i < vector.length; ++i) { |
| 168 |
vector[i] = (vector[i] - mean) / stDev; |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
public static XMLObjectParser PARSER = new AbstractXMLObjectParser() { |
| 173 |
|
| 174 |
|
| 175 |
public String getParserName() { |
| 176 |
return DESIGN_MATRIX; |
| 177 |
} |
| 178 |
|
| 179 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 180 |
|
| 181 |
boolean dynamicStandardization = xo.getAttribute(DYNAMIC_STANDARDIZATION, false); |
| 182 |
String name = (xo.hasId() ? xo.getId() : DESIGN_MATRIX); |
| 183 |
|
| 184 |
DesignMatrix designMatrix = new DesignMatrix(name, dynamicStandardization); |
| 185 |
boolean addIntercept = xo.getAttribute(ADD_INTERCEPT, false); |
| 186 |
boolean standardize = xo.getAttribute(STANDARDIZE, false); |
| 187 |
|
| 188 |
int dim = 0; |
| 189 |
|
| 190 |
if (xo.hasAttribute(FORM)) { |
| 191 |
String type = xo.getStringAttribute(FORM); |
| 192 |
if (type.compareTo("J") == 0) { |
| 193 |
int rowDim = xo.getAttribute(ROW_DIMENSION, 1); |
| 194 |
int colDim = xo.getAttribute(COL_DIMENSION, 1); |
| 195 |
for (int i = 0; i < colDim; i++) { |
| 196 |
Parameter parameter = new Parameter.Default(rowDim); |
| 197 |
designMatrix.addParameter(parameter); |
| 198 |
} |
| 199 |
} else |
| 200 |
throw new XMLParseException("Unknown designMatrix form."); |
| 201 |
} else { |
| 202 |
|
| 203 |
for (int i = 0; i < xo.getChildCount(); i++) { |
| 204 |
Parameter parameter = (Parameter) xo.getChild(i); |
| 205 |
designMatrix.addParameter(parameter); |
| 206 |
if (i == 0) |
| 207 |
dim = parameter.getDimension(); |
| 208 |
else if (dim != parameter.getDimension()) |
| 209 |
throw new XMLParseException("Parameter " + (i+1) +" has dimension "+ parameter.getDimension()+ " and not "+dim+". "+ |
| 210 |
"All parameters must have the same dimension to construct a rectangular design matrix"); |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
if (standardize) { |
| 215 |
|
| 216 |
for (int j = 0; j < designMatrix.getColumnDimension(); ++j) { |
| 217 |
Parameter columnParameter = designMatrix.getParameter(j); |
| 218 |
double[] column = columnParameter.getParameterValues(); |
| 219 |
standardize(column); |
| 220 |
for (int i = 0; i < column.length; ++i) { |
| 221 |
columnParameter.setParameterValueQuietly(i, column[i]); |
| 222 |
} |
| 223 |
columnParameter.setParameterValueNotifyChangedAll(0, columnParameter.getParameterValue(0)); |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
if (addIntercept) { |
| 228 |
Parameter intercept = new Parameter.Default(dim); |
| 229 |
intercept.setId(INTERCEPT); |
| 230 |
designMatrix.addParameter(intercept); |
| 231 |
} |
| 232 |
|
| 233 |
return designMatrix; |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
public String getParserDescription() { |
| 241 |
return "A matrix parameter constructed from its component parameters."; |
| 242 |
} |
| 243 |
|
| 244 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 245 |
return rules; |
| 246 |
} |
| 247 |
|
| 248 |
private final XMLSyntaxRule[] rules = { |
| 249 |
AttributeRule.newBooleanRule(ADD_INTERCEPT, true), |
| 250 |
AttributeRule.newBooleanRule(CHECK_IDENTIFABILITY, true), |
| 251 |
new ElementRule(Parameter.class, 0, Integer.MAX_VALUE), |
| 252 |
AttributeRule.newStringRule(FORM, true), |
| 253 |
AttributeRule.newIntegerRule(COL_DIMENSION, true), |
| 254 |
AttributeRule.newIntegerRule(ROW_DIMENSION, true), |
| 255 |
AttributeRule.newBooleanRule(STANDARDIZE, true), |
| 256 |
}; |
| 257 |
|
| 258 |
public Class getReturnType() { |
| 259 |
return DesignMatrix.class; |
| 260 |
} |
| 261 |
}; |
| 262 |
|
| 263 |
private final boolean dynamicStandardization; |
| 264 |
private boolean standardizationKnown = false; |
| 265 |
|
| 266 |
private double[] standardizationMean = null; |
| 267 |
private double[] standardizationStDev = null; |
| 268 |
private double[] storedStandardizationMean = null; |
| 269 |
private double[] storedStandardizationStDev = null; |
| 270 |
} |