| 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.inferencexml.distribution; |
| 27 |
|
| 28 |
import dr.inference.distribution.GeneralizedLinearModel; |
| 29 |
import dr.oldevomodel.substmodel.PositiveDefiniteSubstitutionModel; |
| 30 |
import dr.oldevomodel.substmodel.SubstitutionModel; |
| 31 |
import dr.inference.model.MatrixParameter; |
| 32 |
import dr.inference.model.Model; |
| 33 |
import dr.inference.model.Parameter; |
| 34 |
import dr.inference.model.Statistic; |
| 35 |
import dr.math.distributions.MultivariateNormalDistribution; |
| 36 |
import dr.math.matrixAlgebra.IllegalDimension; |
| 37 |
import dr.math.matrixAlgebra.Matrix; |
| 38 |
import dr.math.matrixAlgebra.Vector; |
| 39 |
|
| 40 |
import java.util.ArrayList; |
| 41 |
import java.util.List; |
| 42 |
import java.util.logging.Logger; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
@Deprecated |
| 48 |
public class MultivariateOUModel extends GeneralizedLinearModel implements Statistic { |
| 49 |
|
| 50 |
private SubstitutionModel Q; |
| 51 |
private MatrixParameter gamma; |
| 52 |
private double[] time; |
| 53 |
private double[] deltaTime; |
| 54 |
private double[] design; |
| 55 |
private double[] W; |
| 56 |
private double[] initialPriorMean; |
| 57 |
private int K; |
| 58 |
private int Ksquared; |
| 59 |
private int numTimeSteps; |
| 60 |
private double[][] GminusWGWt; |
| 61 |
|
| 62 |
private MultivariateNormalDistribution initialPrior; |
| 63 |
|
| 64 |
private boolean likelihoodKnown = false; |
| 65 |
private boolean storedLikelihoodKnown; |
| 66 |
private double logLikelihood; |
| 67 |
private double storedLogLikelihood; |
| 68 |
private boolean conditionalPrecisionKnown = false; |
| 69 |
private boolean storedConditionPrecisionKnown; |
| 70 |
|
| 71 |
private double[] storedWt; |
| 72 |
private double[] Wt; |
| 73 |
private double[] conditionPrecisionVector; |
| 74 |
private double[] storedConditionPrecisionVector; |
| 75 |
|
| 76 |
private int[] mapTime; |
| 77 |
private List<Double> deltaTimeList; |
| 78 |
|
| 79 |
public MultivariateOUModel(SubstitutionModel substitutionModel, Parameter dependentParam, |
| 80 |
MatrixParameter gamma, double[] time, double[] design) { |
| 81 |
super(dependentParam); |
| 82 |
this.Q = substitutionModel; |
| 83 |
this.time = time; |
| 84 |
this.design = design; |
| 85 |
this.gamma = gamma; |
| 86 |
|
| 87 |
K = substitutionModel.getDataType().getStateCount(); |
| 88 |
Ksquared = K * K; |
| 89 |
|
| 90 |
W = new double[Ksquared]; |
| 91 |
|
| 92 |
initialPriorMean = new double[K]; |
| 93 |
|
| 94 |
StringBuffer sb = new StringBuffer("Constructing a multivariate OU model:\n"); |
| 95 |
sb.append("\tOutcome dimension = "); |
| 96 |
sb.append(K); |
| 97 |
Logger.getLogger("dr.inference.distribution").info(sb.toString()); |
| 98 |
|
| 99 |
setupTimes(); |
| 100 |
addVariable(gamma); |
| 101 |
addModel(substitutionModel); |
| 102 |
|
| 103 |
} |
| 104 |
|
| 105 |
private void setupTimes() { |
| 106 |
deltaTimeList = new ArrayList<Double>(); |
| 107 |
numTimeSteps = time.length / K - 1; |
| 108 |
deltaTime = new double[numTimeSteps]; |
| 109 |
mapTime = new int[numTimeSteps]; |
| 110 |
double currentTime = time[0]; |
| 111 |
int index = 0; |
| 112 |
for (int i = 0; i < numTimeSteps; i++) { |
| 113 |
index += K; |
| 114 |
deltaTime[i] = time[index] - currentTime; |
| 115 |
currentTime = time[index]; |
| 116 |
if (!deltaTimeList.contains(deltaTime[i])) { |
| 117 |
deltaTimeList.add(deltaTime[i]); |
| 118 |
} |
| 119 |
mapTime[i] = deltaTimeList.indexOf(deltaTime[i]); |
| 120 |
((PositiveDefiniteSubstitutionModel) Q).addPrecalculatedTime(-deltaTime[i]); |
| 121 |
} |
| 122 |
Logger.getLogger("dr.inference.distribution").info( |
| 123 |
"\tTime increments: " + new Vector(deltaTime) |
| 124 |
); |
| 125 |
|
| 126 |
Wt = new double[Ksquared * deltaTimeList.size()]; |
| 127 |
storedWt = new double[Ksquared * deltaTimeList.size()]; |
| 128 |
conditionPrecisionVector = new double[Ksquared * deltaTimeList.size()]; |
| 129 |
storedConditionPrecisionVector = new double[Ksquared * deltaTimeList.size()]; |
| 130 |
|
| 131 |
calculateConditionPrecision(); |
| 132 |
} |
| 133 |
|
| 134 |
private void calculateConditionPrecision() { |
| 135 |
|
| 136 |
int index = 0; |
| 137 |
double[] tempW = new double[Ksquared]; |
| 138 |
double[][] G = gamma.getParameterAsMatrix(); |
| 139 |
|
| 140 |
for (double deltaTime : deltaTimeList) { |
| 141 |
|
| 142 |
Q.getTransitionProbabilities(-deltaTime, tempW); |
| 143 |
|
| 144 |
System.arraycopy(tempW, 0, Wt, Ksquared * index, Ksquared); |
| 145 |
|
| 146 |
double[][] WG = new double[K][K]; |
| 147 |
for (int i = 0; i < K; i++) { |
| 148 |
for (int j = 0; j < K; j++) { |
| 149 |
for (int k = 0; k < K; k++) |
| 150 |
WG[i][j] += tempW[i * K + k] * G[k][j]; |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
double[][] WGWt = new double[K][K]; |
| 155 |
for (int i = 0; i < K; i++) { |
| 156 |
for (int j = 0; j < K; j++) { |
| 157 |
for (int k = 0; k < K; k++) |
| 158 |
WGWt[i][j] += WG[i][k] * tempW[j * K + k]; |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
for (int i = 0; i < K; i++) { |
| 163 |
for (int j = 0; j < K; j++) |
| 164 |
WGWt[i][j] = G[i][j] - WGWt[i][j]; |
| 165 |
} |
| 166 |
|
| 167 |
WGWt = new Matrix(WGWt).inverse().toComponents(); |
| 168 |
|
| 169 |
for (int i = 0; i < K; i++) |
| 170 |
System.arraycopy(WGWt[i], 0, conditionPrecisionVector, Ksquared * index + K * i, K); |
| 171 |
|
| 172 |
index++; |
| 173 |
} |
| 174 |
|
| 175 |
conditionalPrecisionKnown = true; |
| 176 |
} |
| 177 |
|
| 178 |
public final double getLogLikelihood() { |
| 179 |
if (!likelihoodKnown) { |
| 180 |
logLikelihood = calculateLogLikelihood(); |
| 181 |
likelihoodKnown = true; |
| 182 |
} |
| 183 |
return logLikelihood; |
| 184 |
} |
| 185 |
|
| 186 |
public double calculateLogLikelihood(double[] x) { |
| 187 |
return calculateLogLikelihood(); |
| 188 |
} |
| 189 |
|
| 190 |
public double calculateLogLikelihood() { |
| 191 |
|
| 192 |
double logLikelihood = 0; |
| 193 |
double[] previous = new double[K]; |
| 194 |
double[] current = new double[K]; |
| 195 |
|
| 196 |
double[] tmpHolder; |
| 197 |
double[][] G = gamma.getParameterAsMatrix(); |
| 198 |
double[] theta = dependentParam.getParameterValues(); |
| 199 |
double[] Xbeta = null; |
| 200 |
boolean hasEffects = getNumberOfFixedEffects() > 0; |
| 201 |
|
| 202 |
if (!conditionalPrecisionKnown) |
| 203 |
calculateConditionPrecision(); |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
try { |
| 208 |
if (new Matrix(G).determinant() < 0.01) |
| 209 |
return Double.NEGATIVE_INFINITY; |
| 210 |
|
| 211 |
} catch (IllegalDimension illegalDimension) { |
| 212 |
illegalDimension.printStackTrace(); |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
int index = 0; |
| 217 |
|
| 218 |
if (!hasEffects) { |
| 219 |
for (int i = 0; i < K; i++) |
| 220 |
previous[i] = theta[index++]; |
| 221 |
} else { |
| 222 |
Xbeta = getXBeta(); |
| 223 |
for (int i = 0; i < K; i++) { |
| 224 |
previous[i] = theta[index] - Xbeta[index]; |
| 225 |
index++; |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
initialPrior = new MultivariateNormalDistribution(initialPriorMean, new Matrix(G).inverse().toComponents()); |
| 230 |
logLikelihood += initialPrior.logPdf(previous); |
| 231 |
|
| 232 |
double save = logLikelihood; |
| 233 |
double save2 = 0; |
| 234 |
int oldMapTime = -1; |
| 235 |
|
| 236 |
double[][] conditionalPrecision = new double[K][K]; |
| 237 |
|
| 238 |
for (int timeStep = 0; timeStep < numTimeSteps; timeStep++) { |
| 239 |
|
| 240 |
int thisMapTime = mapTime[timeStep]; |
| 241 |
|
| 242 |
if (thisMapTime != oldMapTime) { |
| 243 |
|
| 244 |
System.arraycopy(Wt, Ksquared * thisMapTime, W, 0, Ksquared); |
| 245 |
|
| 246 |
for (int i = 0; i < K; i++) |
| 247 |
System.arraycopy(conditionPrecisionVector, Ksquared * thisMapTime + K * i, conditionalPrecision[i], 0, K); |
| 248 |
|
| 249 |
oldMapTime = thisMapTime; |
| 250 |
} |
| 251 |
|
| 252 |
double[] mean = new double[K]; |
| 253 |
int u = 0; |
| 254 |
for (int i = 0; i < K; i++) { |
| 255 |
for (int j = 0; j < K; j++) |
| 256 |
mean[i] += W[u++] * previous[j]; |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
if (!hasEffects) { |
| 295 |
for (int i = 0; i < K; i++) |
| 296 |
current[i] = theta[index++]; |
| 297 |
} else { |
| 298 |
for (int i = 0; i < K; i++) { |
| 299 |
current[i] = theta[index] - Xbeta[index]; |
| 300 |
index++; |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
MultivariateNormalDistribution density = new MultivariateNormalDistribution( |
| 305 |
mean, conditionalPrecision); |
| 306 |
|
| 307 |
double partialLogLikelihood = density.logPdf(current); |
| 308 |
|
| 309 |
if (partialLogLikelihood > 10) { |
| 310 |
|
| 311 |
return Double.NEGATIVE_INFINITY; |
| 312 |
|
| 313 |
} |
| 314 |
|
| 315 |
logLikelihood += partialLogLikelihood; |
| 316 |
|
| 317 |
|
| 318 |
tmpHolder = previous; |
| 319 |
previous = current; |
| 320 |
current = tmpHolder; |
| 321 |
|
| 322 |
} |
| 323 |
|
| 324 |
if (logLikelihood > 100) { |
| 325 |
System.err.println("got here end"); |
| 326 |
System.err.println("save1 = " + save); |
| 327 |
System.err.println("save2 = " + save2); |
| 328 |
System.exit(-1); |
| 329 |
} |
| 330 |
|
| 331 |
likelihoodKnown = true; |
| 332 |
|
| 333 |
return logLikelihood; |
| 334 |
} |
| 335 |
|
| 336 |
protected boolean confirmIndependentParameters() { |
| 337 |
return true; |
| 338 |
} |
| 339 |
|
| 340 |
public boolean requiresScale() { |
| 341 |
return true; |
| 342 |
} |
| 343 |
|
| 344 |
protected void handleModelChangedEvent(Model model, Object object, int index) { |
| 345 |
conditionalPrecisionKnown = false; |
| 346 |
likelihoodKnown = false; |
| 347 |
} |
| 348 |
|
| 349 |
protected void handleParameterChangedEvent(Parameter parameter, int index) { |
| 350 |
|
| 351 |
if (parameter == gamma) { |
| 352 |
conditionalPrecisionKnown = false; |
| 353 |
} |
| 354 |
likelihoodKnown = false; |
| 355 |
|
| 356 |
} |
| 357 |
|
| 358 |
protected void storeState() { |
| 359 |
System.arraycopy(Wt, 0, storedWt, 0, Wt.length); |
| 360 |
System.arraycopy(conditionPrecisionVector, 0, storedConditionPrecisionVector, 0, conditionPrecisionVector.length); |
| 361 |
storedLogLikelihood = logLikelihood; |
| 362 |
storedLikelihoodKnown = likelihoodKnown; |
| 363 |
storedConditionPrecisionKnown = conditionalPrecisionKnown; |
| 364 |
} |
| 365 |
|
| 366 |
protected void restoreState() { |
| 367 |
double[] holder = Wt; |
| 368 |
Wt = storedWt; |
| 369 |
storedWt = holder; |
| 370 |
|
| 371 |
holder = conditionPrecisionVector; |
| 372 |
conditionPrecisionVector = storedConditionPrecisionVector; |
| 373 |
storedConditionPrecisionVector = holder; |
| 374 |
|
| 375 |
logLikelihood = storedLogLikelihood; |
| 376 |
likelihoodKnown = storedLikelihoodKnown; |
| 377 |
conditionalPrecisionKnown = storedConditionPrecisionKnown; |
| 378 |
} |
| 379 |
|
| 380 |
protected void acceptState() { |
| 381 |
} |
| 382 |
|
| 383 |
public String getStatisticName() { |
| 384 |
return getId(); |
| 385 |
} |
| 386 |
|
| 387 |
public String getDimensionName(int dim) { |
| 388 |
return getId() + dim; |
| 389 |
} |
| 390 |
|
| 391 |
public void setDimensionNames(String[] names) { |
| 392 |
|
| 393 |
} |
| 394 |
|
| 395 |
public int getDimension() { |
| 396 |
return W.length; |
| 397 |
} |
| 398 |
|
| 399 |
public double getStatisticValue(int dim) { |
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
if (W != null) |
| 407 |
return W[dim]; |
| 408 |
return 0; |
| 409 |
} |
| 410 |
|
| 411 |
@Override |
| 412 |
public double getValueSum() { |
| 413 |
double sum = 0.0; |
| 414 |
for (int i = 0; i < getDimension(); i++) { |
| 415 |
sum += getStatisticValue(i); |
| 416 |
} |
| 417 |
return sum; |
| 418 |
} |
| 419 |
|
| 420 |
public String getAttributeName() { |
| 421 |
return null; |
| 422 |
} |
| 423 |
|
| 424 |
public double[] getAttributeValue() { |
| 425 |
return null; |
| 426 |
} |
| 427 |
} |