| 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.coalescent; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
import dr.evolution.tree.NodeRef; |
| 32 |
import dr.evolution.tree.Tree; |
| 33 |
import dr.evomodel.tree.TreeChangedEvent; |
| 34 |
import dr.evomodel.tree.TreeModel; |
| 35 |
import dr.evomodelxml.coalescent.GaussianProcessSkytrackLikelihoodParser; |
| 36 |
import dr.inference.loggers.LogColumn; |
| 37 |
import dr.inference.model.Model; |
| 38 |
import dr.inference.model.Parameter; |
| 39 |
import dr.inference.model.Variable; |
| 40 |
import dr.math.MathUtils; |
| 41 |
|
| 42 |
|
| 43 |
import no.uib.cipr.matrix.*; |
| 44 |
|
| 45 |
|
| 46 |
import java.util.ArrayList; |
| 47 |
import java.util.List; |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
public class GaussianProcessSkytrackLikelihood extends OldAbstractCoalescentLikelihood { |
| 61 |
|
| 62 |
|
| 63 |
public static final double LOG_TWO_TIMES_PI = 1.837877; |
| 64 |
protected Parameter precisionParameter; |
| 65 |
protected Parameter lambda_boundParameter; |
| 66 |
|
| 67 |
protected Parameter lambdaParameter; |
| 68 |
protected Parameter betaParameter; |
| 69 |
protected Parameter alphaParameter; |
| 70 |
protected Parameter GPtype; |
| 71 |
protected Parameter GPcounts; |
| 72 |
protected Parameter coalfactor; |
| 73 |
protected Parameter popSizeParameter; |
| 74 |
protected Parameter changePoints; |
| 75 |
protected Parameter Tmrca; |
| 76 |
|
| 77 |
protected Parameter CoalCounts; |
| 78 |
protected Parameter numPoints; |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
protected double [] GPcoalfactor; |
| 83 |
protected double [] storedGPcoalfactor; |
| 84 |
protected double [] GPCoalInterval; |
| 85 |
protected double [] storedGPCoalInterval; |
| 86 |
protected double [] backupIntervals; |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
protected int [] CoalPosIndicator; |
| 93 |
protected int [] storedCoalPosIndicator; |
| 94 |
protected double [] CoalTime; |
| 95 |
protected double [] storedCoalTime; |
| 96 |
protected int numintervals; |
| 97 |
protected int numcoalpoints; |
| 98 |
protected double constlik; |
| 99 |
protected double storedconstlik; |
| 100 |
|
| 101 |
|
| 102 |
protected double logGPLikelihood; |
| 103 |
|
| 104 |
protected SymmTridiagMatrix weightMatrix; |
| 105 |
|
| 106 |
protected boolean rescaleByRootHeight; |
| 107 |
|
| 108 |
|
| 109 |
private static List<Tree> wrapTree(Tree tree) { |
| 110 |
List<Tree> treeList = new ArrayList<Tree>(); |
| 111 |
treeList.add(tree); |
| 112 |
return treeList; |
| 113 |
} |
| 114 |
|
| 115 |
public GaussianProcessSkytrackLikelihood(Tree tree, |
| 116 |
Parameter precParameter, |
| 117 |
boolean rescaleByRootHeight, Parameter lambda_bound, Parameter lambda_parameter, Parameter popParameter, Parameter alpha_parameter, Parameter beta_parameter, Parameter change_points, Parameter GPtype, Parameter GPcounts, Parameter coalfactor, Parameter CoalCounts, Parameter numPoints, Parameter Tmrca) { |
| 118 |
this(wrapTree(tree), precParameter, rescaleByRootHeight, lambda_bound, lambda_parameter, popParameter, alpha_parameter, beta_parameter, change_points,GPtype,GPcounts,coalfactor,CoalCounts, numPoints, Tmrca); |
| 119 |
|
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
public GaussianProcessSkytrackLikelihood(String name) { |
| 124 |
super(name); |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
public GaussianProcessSkytrackLikelihood(List<Tree> treeList, |
| 131 |
Parameter precParameter, |
| 132 |
boolean rescaleByRootHeight, Parameter lambda_bound, Parameter lambda_parameter, Parameter popParameter, Parameter alpha_parameter, Parameter beta_parameter, Parameter change_points, Parameter GPtype, Parameter GPcounts, Parameter coalfactor, Parameter CoalCounts, Parameter numPoints, Parameter Tmrca) { |
| 133 |
super(GaussianProcessSkytrackLikelihoodParser.SKYTRACK_LIKELIHOOD); |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
this.popSizeParameter = popParameter; |
| 138 |
this.Tmrca=Tmrca; |
| 139 |
|
| 140 |
this.changePoints=change_points; |
| 141 |
this.numPoints=numPoints; |
| 142 |
|
| 143 |
this.precisionParameter = precParameter; |
| 144 |
this.lambdaParameter = lambda_parameter; |
| 145 |
this.betaParameter = beta_parameter; |
| 146 |
this.alphaParameter=alpha_parameter; |
| 147 |
|
| 148 |
this.rescaleByRootHeight = rescaleByRootHeight; |
| 149 |
this.lambda_boundParameter= lambda_bound; |
| 150 |
this.GPcounts=GPcounts; |
| 151 |
this.GPtype=GPtype; |
| 152 |
this.coalfactor=coalfactor; |
| 153 |
this.CoalCounts=CoalCounts; |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
addVariable(precisionParameter); |
| 158 |
addVariable(popSizeParameter); |
| 159 |
addVariable(changePoints); |
| 160 |
addVariable(numPoints); |
| 161 |
|
| 162 |
addVariable(GPcounts); |
| 163 |
|
| 164 |
addVariable(GPtype); |
| 165 |
|
| 166 |
addVariable(coalfactor); |
| 167 |
addVariable(lambda_boundParameter); |
| 168 |
addVariable(CoalCounts); |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
setTree(treeList); |
| 179 |
|
| 180 |
wrapSetupIntervals(); |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
numintervals= getIntervalCount(); |
| 188 |
numcoalpoints=getCorrectFieldLength(); |
| 189 |
|
| 190 |
GPcoalfactor = new double[numintervals]; |
| 191 |
backupIntervals=new double[numintervals]; |
| 192 |
GPCoalInterval=new double[numcoalpoints]; |
| 193 |
storedGPCoalInterval=new double[numcoalpoints]; |
| 194 |
CoalPosIndicator= new int[numcoalpoints]; |
| 195 |
storedCoalPosIndicator=new int[numcoalpoints]; |
| 196 |
CoalTime=new double[numcoalpoints]; |
| 197 |
storedCoalTime=new double[numcoalpoints]; |
| 198 |
|
| 199 |
|
| 200 |
storedGPcoalfactor = new double[numintervals]; |
| 201 |
GPcounts.setDimension(numintervals); |
| 202 |
CoalCounts.setDimension(numcoalpoints); |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
GPtype.setDimension(numcoalpoints); |
| 207 |
numPoints.setParameterValue(0,numcoalpoints); |
| 208 |
|
| 209 |
popSizeParameter.setDimension(numcoalpoints); |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
changePoints.setDimension(numcoalpoints); |
| 219 |
coalfactor.setDimension(numcoalpoints); |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
initializationReport(); |
| 226 |
setupSufficientStatistics(); |
| 227 |
setupGPvalues(); |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
private boolean flagForJulia = false; |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
protected void handleModelChangedEvent(Model model, Object object, int index) { |
| 251 |
super.handleModelChangedEvent(model, object, index); |
| 252 |
if (model == tree) { |
| 253 |
|
| 254 |
if (object instanceof TreeChangedEvent) { |
| 255 |
TreeChangedEvent tce = (TreeChangedEvent) object; |
| 256 |
|
| 257 |
|
| 258 |
flagForJulia = true; |
| 259 |
} else { |
| 260 |
if (object instanceof Parameter){ |
| 261 |
|
| 262 |
flagForJulia=true; |
| 263 |
} |
| 264 |
else { |
| 265 |
throw new IllegalArgumentException("Not sure what type of model changed event occurred: " + object.getClass().toString()); |
| 266 |
} |
| 267 |
} |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
public LogColumn[] getColumns() { |
| 272 |
|
| 273 |
return new LogColumn[]{ |
| 274 |
new VariableLengthColumn("changePoints", changePoints), new VariableLengthColumn("Gvalues",popSizeParameter) |
| 275 |
}; |
| 276 |
} |
| 277 |
|
| 278 |
private class VariableLengthColumn extends LogColumn.Abstract { |
| 279 |
|
| 280 |
private final Parameter param; |
| 281 |
|
| 282 |
public VariableLengthColumn(String label, Parameter param) { |
| 283 |
super(label); |
| 284 |
this.param = param; |
| 285 |
} |
| 286 |
|
| 287 |
protected String getFormattedValue() { |
| 288 |
return convertToDelimited(param.getParameterValues()); |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
private static final String OPEN = "{"; |
| 294 |
private static final String CLOSE = "}"; |
| 295 |
private static final String DELIMIT = ","; |
| 296 |
|
| 297 |
private String convertToDelimited(double[] x) { |
| 298 |
StringBuilder sb = new StringBuilder(OPEN); |
| 299 |
final int dim = x.length; |
| 300 |
for (int i = 0; i < dim; ++i) { |
| 301 |
sb.append(Double.toString(x[i])); |
| 302 |
if (i < dim - 1) { |
| 303 |
sb.append(DELIMIT); |
| 304 |
} |
| 305 |
} |
| 306 |
sb.append(CLOSE); |
| 307 |
return sb.toString(); |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
protected void setTree(List<Tree> treeList) { |
| 312 |
if (treeList.size() != 1) { |
| 313 |
throw new RuntimeException("GP-based method only implemented for one tree"); |
| 314 |
} |
| 315 |
this.tree = treeList.get(0); |
| 316 |
this.treesSet = null; |
| 317 |
if (tree instanceof TreeModel) { |
| 318 |
addModel((TreeModel) tree); |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
protected void wrapSetupIntervals() { |
| 325 |
setupIntervals(); |
| 326 |
intervalsKnown = true; |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
public double calculateLogLikelihood(Parameter Gfunction, Parameter latentCounts, Parameter eventType, Parameter upper_Bound, double [] Gfactor) { |
| 334 |
double upperBound = upper_Bound.getParameterValue(0); |
| 335 |
logGPLikelihood=-upperBound*getConstlik(); |
| 336 |
|
| 337 |
for (int i=0; i<latentCounts.getSize(); i++){ |
| 338 |
if (Gfactor[i]>0) { |
| 339 |
if (latentCounts.getParameterValue(i)<0){System.err.println("WARNING");} |
| 340 |
logGPLikelihood+=latentCounts.getParameterValue(i)*Math.log(upperBound*Gfactor[i]); |
| 341 |
} |
| 342 |
} |
| 343 |
|
| 344 |
double[] currentGfunction = Gfunction.getParameterValues(); |
| 345 |
for (int i=0; i<Gfunction.getSize();i++){ |
| 346 |
logGPLikelihood+= -Math.log(1+Math.exp(-eventType.getParameterValue(i)*currentGfunction[i])); |
| 347 |
} |
| 348 |
return logGPLikelihood; |
| 349 |
} |
| 350 |
|
| 351 |
|
| 352 |
public double getConstlik(){ |
| 353 |
return constlik; |
| 354 |
} |
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
public double getLogLikelihood() { |
| 359 |
if (!likelihoodKnown) { |
| 360 |
if(flagForJulia) { |
| 361 |
System.err.println("recalculating intervals and counts"); |
| 362 |
wrapSetupIntervals(); |
| 363 |
recomputeValues(); |
| 364 |
flagForJulia=false; |
| 365 |
} |
| 366 |
logLikelihood = |
| 367 |
calculateLogLikelihood(popSizeParameter,GPcounts,GPtype,lambda_boundParameter,GPcoalfactor)+calculateLogGP() |
| 368 |
+getLogPriorLambda(lambdaParameter.getParameterValue(0),0.01,lambda_boundParameter.getParameterValue(0)); |
| 369 |
likelihoodKnown = true; |
| 370 |
} |
| 371 |
return logLikelihood; |
| 372 |
} |
| 373 |
|
| 374 |
protected SymmTridiagMatrix getQmatrix(double precision, double[] x ) { |
| 375 |
SymmTridiagMatrix res; |
| 376 |
double trick=0.00000000001; |
| 377 |
double[] offdiag = new double[x.length - 1]; |
| 378 |
double[] diag = new double[x.length]; |
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
for (int i = 0; i < x.length - 1; i++) { |
| 383 |
offdiag[i] = precision*(-1.0 / (x[i+1]-x[i])); |
| 384 |
|
| 385 |
if (i< x.length-2){ |
| 386 |
diag[i+1]= -offdiag[i]+precision*(1.0/(x[i+2]-x[i+1])+trick); |
| 387 |
} |
| 388 |
} |
| 389 |
|
| 390 |
|
| 391 |
diag[0] = -offdiag[0]+precision*trick; |
| 392 |
|
| 393 |
diag[x.length - 1] = -offdiag[x.length - 2]+precision*(trick); |
| 394 |
res = new SymmTridiagMatrix(diag, offdiag); |
| 395 |
return res; |
| 396 |
} |
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
protected double calculateLogGP() { |
| 401 |
SymmTridiagMatrix currentQ = getQmatrix(precisionParameter.getParameterValue(0), changePoints.getParameterValues()); |
| 402 |
double currentLike; |
| 403 |
DenseVector diagonal1 = new DenseVector(popSizeParameter.getSize()); |
| 404 |
DenseVector currentGamma = new DenseVector(popSizeParameter.getParameterValues()); |
| 405 |
currentQ.mult(currentGamma, diagonal1); |
| 406 |
currentLike = -0.5 * logGeneralizedDeterminant(currentQ) - 0.5 * currentGamma.dot(diagonal1) - 0.5 * (popSizeParameter.getSize() - 1) * LOG_TWO_TIMES_PI; |
| 407 |
|
| 408 |
return currentLike; |
| 409 |
} |
| 410 |
|
| 411 |
|
| 412 |
private double getLogPriorLambda(double lambdaMean, double epsilon, double lambdaValue){ |
| 413 |
double res; |
| 414 |
if (lambdaValue < lambdaMean) {res=epsilon*(1/lambdaMean);} |
| 415 |
else {res=Math.log(1-epsilon)*(1/lambdaMean)*Math.exp(-(1/lambdaMean)*(lambdaValue-lambdaMean)); } |
| 416 |
return res; |
| 417 |
} |
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
public static double logGeneralizedDeterminant(SymmTridiagMatrix X) { |
| 422 |
|
| 423 |
SymmTridiagEVD eigen = new SymmTridiagEVD(X.numRows(), false); |
| 424 |
|
| 425 |
try { |
| 426 |
eigen.factor(X); |
| 427 |
} catch (NotConvergedException e) { |
| 428 |
throw new RuntimeException("Not converged error in generalized determinate calculation.\n" + e.getMessage()); |
| 429 |
} |
| 430 |
|
| 431 |
|
| 432 |
double[] x = eigen.getEigenvalues(); |
| 433 |
|
| 434 |
double a = 0; |
| 435 |
for (double d : x) { |
| 436 |
if (d > 0.00001) |
| 437 |
a += Math.log(d); |
| 438 |
} |
| 439 |
|
| 440 |
return a; |
| 441 |
} |
| 442 |
|
| 443 |
protected void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type){ |
| 444 |
likelihoodKnown = false; |
| 445 |
} |
| 446 |
|
| 447 |
|
| 448 |
protected void restoreState() { |
| 449 |
super.restoreState(); |
| 450 |
System.arraycopy(storedGPcoalfactor, 0, GPcoalfactor, 0, storedGPcoalfactor.length); |
| 451 |
System.arraycopy(storedCoalTime,0,CoalTime,0,storedCoalTime.length); |
| 452 |
System.arraycopy(storedGPCoalInterval,0,GPCoalInterval,0,storedGPCoalInterval.length); |
| 453 |
System.arraycopy(storedCoalPosIndicator,0,CoalPosIndicator,0,storedCoalPosIndicator.length); |
| 454 |
constlik=storedconstlik; |
| 455 |
} |
| 456 |
|
| 457 |
protected void storeState() { |
| 458 |
super.storeState(); |
| 459 |
System.arraycopy(GPcoalfactor, 0, storedGPcoalfactor, 0, GPcoalfactor.length); |
| 460 |
System.arraycopy(CoalTime,0,storedCoalTime,0,CoalTime.length); |
| 461 |
System.arraycopy(GPCoalInterval, 0, storedGPCoalInterval, 0, GPCoalInterval.length); |
| 462 |
System.arraycopy(CoalPosIndicator, 0, storedCoalPosIndicator,0,CoalPosIndicator.length); |
| 463 |
storedconstlik=constlik; |
| 464 |
} |
| 465 |
|
| 466 |
public String toString() { |
| 467 |
return getId() + "(" + Double.toString(getLogLikelihood()) + ")"; |
| 468 |
} |
| 469 |
|
| 470 |
public void initializationReport() { |
| 471 |
|
| 472 |
System.out.println("Creating a GP based estimation of effective population trajectories:"); |
| 473 |
System.out.println("\tIf you publish results using this model, please reference: Minin, Palacios, Suchard (XXXX), AAA"); |
| 474 |
|
| 475 |
} |
| 476 |
|
| 477 |
public static void checkTree(TreeModel treeModel) { |
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
for (int i = 0; i < treeModel.getInternalNodeCount(); i++) { |
| 483 |
NodeRef node = treeModel.getInternalNode(i); |
| 484 |
if (node != treeModel.getRoot()) { |
| 485 |
double parentHeight = treeModel.getNodeHeight(treeModel.getParent(node)); |
| 486 |
double childHeight0 = treeModel.getNodeHeight(treeModel.getChild(node, 0)); |
| 487 |
double childHeight1 = treeModel.getNodeHeight(treeModel.getChild(node, 1)); |
| 488 |
double maxChild = childHeight0; |
| 489 |
if (childHeight1 > maxChild) |
| 490 |
maxChild = childHeight1; |
| 491 |
double newHeight = maxChild + MathUtils.nextDouble() * (parentHeight - maxChild); |
| 492 |
treeModel.setNodeHeight(node, newHeight); |
| 493 |
} |
| 494 |
} |
| 495 |
treeModel.pushTreeChangedEvent(); |
| 496 |
|
| 497 |
} |
| 498 |
|
| 499 |
protected void recomputeValues() { |
| 500 |
double length = 0.0; |
| 501 |
double prevLength=0.0; |
| 502 |
double count=0.0; |
| 503 |
int countcoal = 0; |
| 504 |
int pointer=0; |
| 505 |
int pointer2=0; |
| 506 |
constlik= 0; |
| 507 |
for (int i = 0; i < getIntervalCount(); i++) { |
| 508 |
length += getInterval(i); |
| 509 |
count=0.0; |
| 510 |
for (int j=pointer;j<changePoints.getSize();j++){ |
| 511 |
|
| 512 |
if (changePoints.getParameterValue(j)<=length){ |
| 513 |
|
| 514 |
pointer++; |
| 515 |
count++; |
| 516 |
|
| 517 |
} |
| 518 |
} |
| 519 |
GPcounts.setParameterValue(i,count); |
| 520 |
GPcoalfactor[i] =getLineageCount(i)*(getLineageCount(i)-1.0) / 2.0; |
| 521 |
constlik+=GPcoalfactor[i]*getInterval(i); |
| 522 |
|
| 523 |
if (getIntervalType(i) == CoalescentEventType.COALESCENT) { |
| 524 |
CoalPosIndicator[countcoal]=i; |
| 525 |
count=0; |
| 526 |
for (int s=pointer2;s<changePoints.getSize();s++){ |
| 527 |
if (changePoints.getParameterValue(s)<=length){ |
| 528 |
pointer2++; |
| 529 |
count++; |
| 530 |
} else |
| 531 |
s=changePoints.getSize(); |
| 532 |
} |
| 533 |
CoalCounts.setParameterValue(countcoal,count-1); |
| 534 |
CoalTime[countcoal]=length; |
| 535 |
GPCoalInterval[countcoal]=length-prevLength; |
| 536 |
coalfactor.setParameterValue(countcoal,getLineageCount(i)*(getLineageCount(i)-1)/2.0); |
| 537 |
countcoal++; |
| 538 |
prevLength=length; |
| 539 |
} |
| 540 |
} |
| 541 |
|
| 542 |
|
| 543 |
int sumcoal=0; |
| 544 |
int sumlat=0; |
| 545 |
|
| 546 |
for (int j=0;j<changePoints.getSize();j++){ |
| 547 |
if( GPtype.getParameterValue(j)==1) {sumcoal++;} |
| 548 |
|
| 549 |
} |
| 550 |
for (int j=0;j<CoalCounts.getSize();j++){ |
| 551 |
sumlat+=CoalCounts.getParameterValue(j); |
| 552 |
} |
| 553 |
if (sumcoal!=CoalCounts.getSize()){ System.err.println("WARNING CONSISTENCY 1"); } |
| 554 |
if (sumlat!=(changePoints.getSize()-CoalCounts.getSize())){ System.err.println("WARNING CONSISTENCY 2:"+sumlat+"and changePts size"+changePoints.getSize());} |
| 555 |
Tmrca.setParameterValue(0,CoalTime[countcoal-1]); |
| 556 |
} |
| 557 |
|
| 558 |
protected void setupSufficientStatistics() { |
| 559 |
double length = 0.0; |
| 560 |
double prevLength=0.0; |
| 561 |
int countcoal = 0; |
| 562 |
constlik= 0; |
| 563 |
for (int i = 0; i < getIntervalCount(); i++) { |
| 564 |
|
| 565 |
length += getInterval(i); |
| 566 |
GPcounts.setParameterValue(i,0.0); |
| 567 |
GPcoalfactor[i] =getLineageCount(i)*(getLineageCount(i)-1.0) / 2.0; |
| 568 |
constlik+=GPcoalfactor[i]*getInterval(i); |
| 569 |
if (getIntervalType(i) == CoalescentEventType.COALESCENT) { |
| 570 |
GPcounts.setParameterValue(i,1.0); |
| 571 |
GPtype.setParameterValue(countcoal,1.0); |
| 572 |
CoalPosIndicator[countcoal]=i; |
| 573 |
changePoints.setParameterValue(countcoal,length); |
| 574 |
CoalCounts.setParameterValue(countcoal,0.0); |
| 575 |
CoalTime[countcoal]=length; |
| 576 |
GPCoalInterval[countcoal]=length-prevLength; |
| 577 |
coalfactor.setParameterValue(countcoal,getLineageCount(i)*(getLineageCount(i)-1)/2.0); |
| 578 |
countcoal++; |
| 579 |
prevLength=length; |
| 580 |
} |
| 581 |
} |
| 582 |
Tmrca.setParameterValue(0,CoalTime[countcoal-1]); |
| 583 |
|
| 584 |
} |
| 585 |
|
| 586 |
|
| 587 |
protected int getCorrectFieldLength() { |
| 588 |
return tree.getExternalNodeCount() - 1; |
| 589 |
} |
| 590 |
|
| 591 |
protected void setupQmatrix(double precision) { |
| 592 |
double trick=0.000001; |
| 593 |
double[] offdiag = new double[changePoints.getSize() - 1]; |
| 594 |
double[] diag = new double[changePoints.getSize()]; |
| 595 |
for (int i = 0; i < changePoints.getSize() - 1; i++) { |
| 596 |
offdiag[i] = precision*(-1.0 / (changePoints.getParameterValue(i+1)-changePoints.getParameterValue(i))); |
| 597 |
if (i<getCorrectFieldLength()-2){ |
| 598 |
diag[i+1]= -offdiag[i]+precision*(1.0/(changePoints.getParameterValue(i+2)-changePoints.getParameterValue(i+1))+trick); |
| 599 |
|
| 600 |
} |
| 601 |
} |
| 602 |
|
| 603 |
|
| 604 |
diag[0] = -offdiag[0]+precision*trick; |
| 605 |
|
| 606 |
diag[getCorrectFieldLength() - 1] = -offdiag[getCorrectFieldLength() - 2]+precision*(trick); |
| 607 |
weightMatrix = new SymmTridiagMatrix(diag, offdiag); |
| 608 |
} |
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
protected void setupGPvalues() { |
| 613 |
setupQmatrix(precisionParameter.getParameterValue(0)); |
| 614 |
int length = getCorrectFieldLength(); |
| 615 |
DenseVector StandNorm = new DenseVector(length); |
| 616 |
DenseVector MultiNorm = new DenseVector(length); |
| 617 |
for (int i=0; i<length;i++){ |
| 618 |
StandNorm.set(i,MathUtils.nextGaussian()); |
| 619 |
} |
| 620 |
UpperSPDBandMatrix Qcurrent = new UpperSPDBandMatrix(weightMatrix, 1); |
| 621 |
BandCholesky U = new BandCholesky(length,1,true); |
| 622 |
U.factor(Qcurrent); |
| 623 |
UpperTriangBandMatrix CholeskyUpper = U.getU(); |
| 624 |
CholeskyUpper.solve(StandNorm,MultiNorm); |
| 625 |
for (int i=0; i<length;i++){ |
| 626 |
popSizeParameter.setParameterValue(i,1.0); |
| 627 |
} |
| 628 |
} |
| 629 |
|
| 630 |
public Parameter getPrecisionParameter() { |
| 631 |
return precisionParameter; |
| 632 |
} |
| 633 |
|
| 634 |
public Parameter getPopSizeParameter() { |
| 635 |
return popSizeParameter; |
| 636 |
} |
| 637 |
|
| 638 |
|
| 639 |
public Parameter getNumPoints() { |
| 640 |
return numPoints; |
| 641 |
} |
| 642 |
|
| 643 |
public Parameter getLambdaParameter() { |
| 644 |
return lambdaParameter; |
| 645 |
} |
| 646 |
|
| 647 |
public Parameter getLambdaBoundParameter() { |
| 648 |
return lambda_boundParameter; |
| 649 |
} |
| 650 |
|
| 651 |
public Parameter getChangePoints() { |
| 652 |
return changePoints; |
| 653 |
} |
| 654 |
|
| 655 |
public double getAlphaParameter(){ |
| 656 |
return alphaParameter.getParameterValue(0); |
| 657 |
} |
| 658 |
|
| 659 |
public double getBetaParameter(){ |
| 660 |
return betaParameter.getParameterValue(0); |
| 661 |
} |
| 662 |
|
| 663 |
|
| 664 |
public double [] getGPcoalfactor(){ |
| 665 |
return GPcoalfactor; |
| 666 |
} |
| 667 |
|
| 668 |
|
| 669 |
public Parameter getcoalfactor(){ |
| 670 |
return coalfactor; |
| 671 |
} |
| 672 |
|
| 673 |
public Parameter getCoalCounts(){ |
| 674 |
return CoalCounts; |
| 675 |
} |
| 676 |
|
| 677 |
|
| 678 |
public Parameter getGPtype(){ |
| 679 |
return GPtype; |
| 680 |
} |
| 681 |
|
| 682 |
|
| 683 |
public Parameter getGPcounts(){ |
| 684 |
return GPcounts; |
| 685 |
} |
| 686 |
|
| 687 |
|
| 688 |
public SymmTridiagMatrix getWeightMatrix() { |
| 689 |
return weightMatrix.copy(); |
| 690 |
} |
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
public double [] getGPCoalInterval(){ |
| 695 |
return GPCoalInterval; |
| 696 |
} |
| 697 |
|
| 698 |
public double [] getCoalTime(){ |
| 699 |
return CoalTime; |
| 700 |
} |
| 701 |
|
| 702 |
public double getGPCoalInterval(int j){ |
| 703 |
return GPCoalInterval[j]; |
| 704 |
} |
| 705 |
|
| 706 |
public int [] getCoalPosIndicator() { |
| 707 |
return CoalPosIndicator; |
| 708 |
} |
| 709 |
|
| 710 |
} |
| 711 |
|