| 1 | package test.dr.evomodel.branchratemodel; |
| 2 | |
| 3 | import dr.inference.model.Parameter; |
| 4 | import test.dr.inference.trace.TraceCorrelationAssert; |
| 5 | |
| 6 | /** |
| 7 | * @author Walter Xie |
| 8 | * convert testUncorrelatedRelaxedClock.xml in the folder /example |
| 9 | */ |
| 10 | public class UncorrelatedRelaxedClockTest extends TraceCorrelationAssert { |
| 11 | |
| 12 | private Parameter meanParam; |
| 13 | private Parameter stdevParam; |
| 14 | |
| 15 | public UncorrelatedRelaxedClockTest(String name) { |
| 16 | super(name); |
| 17 | } |
| 18 | |
| 19 | // public void setUp() throws Exception { |
| 20 | // super.setUp(); |
| 21 | // |
| 22 | // MathUtils.setSeed(666); |
| 23 | // |
| 24 | // createAlignment(DENGUE4_TAXON_SEQUENCE, Nucleotides.INSTANCE); |
| 25 | // } |
| 26 | // |
| 27 | // public void testLogNormal() throws Exception { |
| 28 | // meanParam = new Parameter.Default(LogNormalDistributionModelParser.MEAN, 2.3E-5, 0, 100.0); |
| 29 | // stdevParam = new Parameter.Default(LogNormalDistributionModelParser.STDEV, 0.1, 0, 10.0); |
| 30 | // ParametricDistributionModel distributionModel = new LogNormalDistributionModel(meanParam, stdevParam, 0.0, true, false); // meanInRealSpace="true" |
| 31 | // |
| 32 | // ArrayTraceList traceList = UncorrelatedRelaxedClock(distributionModel); |
| 33 | // |
| 34 | //// <expectation name="posterior" value="-3927.81"/> |
| 35 | //// <expectation name="ucld.mean" value="8.28472E-4"/> |
| 36 | //// <expectation name="ucld.stdev" value="0.17435"/> |
| 37 | //// <expectation name="meanRate" value="8.09909E-4"/> |
| 38 | //// <expectation name="coefficientOfVariation" value="0.15982"/> |
| 39 | //// <expectation name="covariance" value="-3.81803E-2"/> |
| 40 | //// <expectation name="constant.popSize" value="37.3524"/> |
| 41 | //// <expectation name="hky.kappa" value="18.3053"/> |
| 42 | //// <expectation name="treeModel.rootHeight" value="69.2953"/> |
| 43 | //// <expectation name="treeLikelihood" value="-3855.78"/> |
| 44 | //// <expectation name="skyline" value="-72.0313"/> ??? |
| 45 | // |
| 46 | // TraceCorrelation likelihoodStats = traceList.getCorrelationStatistics(traceList.getTraceIndex(CompoundLikelihoodParser.POSTERIOR)); |
| 47 | // assertExpectation(CompoundLikelihoodParser.POSTERIOR, likelihoodStats, -3927.81); |
| 48 | // |
| 49 | // likelihoodStats = traceList.getCorrelationStatistics(traceList.getTraceIndex(TreeLikelihoodParser.TREE_LIKELIHOOD)); |
| 50 | // assertExpectation(TreeLikelihoodParser.TREE_LIKELIHOOD, likelihoodStats, -3855.78); |
| 51 | // |
| 52 | // TraceCorrelation treeHeightStats = traceList.getCorrelationStatistics(traceList.getTraceIndex(TREE_HEIGHT)); |
| 53 | // assertExpectation(TREE_HEIGHT, treeHeightStats, 69.2953); |
| 54 | // |
| 55 | // TraceCorrelation kappaStats = traceList.getCorrelationStatistics(traceList.getTraceIndex(HKYParser.KAPPA)); |
| 56 | // assertExpectation(HKYParser.KAPPA, kappaStats, 18.06518); |
| 57 | // |
| 58 | // TraceCorrelation ucldStats = traceList.getCorrelationStatistics(traceList.getTraceIndex(LogNormalDistributionModelParser.MEAN)); |
| 59 | // assertExpectation(LogNormalDistributionModelParser.MEAN, ucldStats, 8.0591451486E-4); |
| 60 | // |
| 61 | // ucldStats = traceList.getCorrelationStatistics(traceList.getTraceIndex(LogNormalDistributionModelParser.STDEV)); |
| 62 | // assertExpectation(LogNormalDistributionModelParser.STDEV, ucldStats, 0.16846023066431434); |
| 63 | // |
| 64 | // TraceCorrelation rateStats = traceList.getCorrelationStatistics(traceList.getTraceIndex("meanRate")); |
| 65 | // assertExpectation("meanRate", rateStats, 8.010906E-4); |
| 66 | // |
| 67 | // TraceCorrelation coefficientOfVariationStats = traceList.getCorrelationStatistics(traceList.getTraceIndex(RateStatisticParser.COEFFICIENT_OF_VARIATION)); |
| 68 | // assertExpectation(RateStatisticParser.COEFFICIENT_OF_VARIATION, coefficientOfVariationStats, 0.15982); |
| 69 | // |
| 70 | // TraceCorrelation covarianceStats = traceList.getCorrelationStatistics(traceList.getTraceIndex("covariance")); |
| 71 | // assertExpectation("covariance", covarianceStats, -0.0260333026); |
| 72 | // |
| 73 | // TraceCorrelation popStats = traceList.getCorrelationStatistics(traceList.getTraceIndex(ConstantPopulationModelParser.POPULATION_SIZE)); |
| 74 | // assertExpectation(ConstantPopulationModelParser.POPULATION_SIZE, popStats, 37.3524); |
| 75 | // |
| 76 | // TraceCorrelation coalescentStats = traceList.getCorrelationStatistics(traceList.getTraceIndex("coalescent")); |
| 77 | // assertExpectation("coalescent", coalescentStats, -72.0313); |
| 78 | // } |
| 79 | // |
| 80 | // public void testExponential() throws Exception { |
| 81 | // meanParam = new Parameter.Default(1.0); |
| 82 | // meanParam.setId(DistributionModelParser.MEAN); |
| 83 | // stdevParam = null; |
| 84 | // ParametricDistributionModel distributionModel = new ExponentialDistributionModel(meanParam); // offset = 0 |
| 85 | // |
| 86 | // ArrayTraceList traceList = UncorrelatedRelaxedClock(distributionModel); |
| 87 | // |
| 88 | // TraceCorrelation likelihoodStats = traceList.getCorrelationStatistics(traceList.getTraceIndex(CompoundLikelihoodParser.POSTERIOR)); |
| 89 | // assertExpectation(CompoundLikelihoodParser.POSTERIOR, likelihoodStats, -3958.7409); |
| 90 | //// System.out.println("likelihoodStats = " + likelihoodStats.getMean()); |
| 91 | // |
| 92 | // likelihoodStats = traceList.getCorrelationStatistics(traceList.getTraceIndex(TreeLikelihoodParser.TREE_LIKELIHOOD)); |
| 93 | // assertExpectation(TreeLikelihoodParser.TREE_LIKELIHOOD, likelihoodStats, -3885.26939); |
| 94 | //// System.out.println("treelikelihoodStats = " + likelihoodStats.getMean()); |
| 95 | // |
| 96 | // TraceCorrelation treeHeightStats = traceList.getCorrelationStatistics(traceList.getTraceIndex(TREE_HEIGHT)); |
| 97 | // assertExpectation(TREE_HEIGHT, treeHeightStats, 84.3529526); |
| 98 | // |
| 99 | // TraceCorrelation kappaStats = traceList.getCorrelationStatistics(traceList.getTraceIndex(HKYParser.KAPPA)); |
| 100 | // assertExpectation(HKYParser.KAPPA, kappaStats, 18.38065); |
| 101 | // |
| 102 | // TraceCorrelation ucedStats = traceList.getCorrelationStatistics(traceList.getTraceIndex(DistributionModelParser.MEAN)); |
| 103 | // assertExpectation(DistributionModelParser.MEAN, ucedStats, 0.0019344134887784579); |
| 104 | //// System.out.println("ucedStats = " + ucedStats.getMean()); |
| 105 | // |
| 106 | // TraceCorrelation rateStats = traceList.getCorrelationStatistics(traceList.getTraceIndex("meanRate")); |
| 107 | // assertExpectation("meanRate", rateStats, 0.0020538802366337084); |
| 108 | //// System.out.println("rateStats = " + rateStats.getMean()); |
| 109 | // |
| 110 | // TraceCorrelation coefficientOfVariationStats = traceList.getCorrelationStatistics(traceList.getTraceIndex(RateStatisticParser.COEFFICIENT_OF_VARIATION)); |
| 111 | // assertExpectation(RateStatisticParser.COEFFICIENT_OF_VARIATION, coefficientOfVariationStats, 0.773609960455); |
| 112 | //// System.out.println("coefficientOfVariationStats = " + coefficientOfVariationStats.getMean()); |
| 113 | // |
| 114 | // TraceCorrelation covarianceStats = traceList.getCorrelationStatistics(traceList.getTraceIndex("covariance")); |
| 115 | // assertExpectation("covariance", covarianceStats, -0.07042030641301375); |
| 116 | //// System.out.println("covarianceStats = " + covarianceStats.getMean()); |
| 117 | // |
| 118 | // TraceCorrelation popStats = traceList.getCorrelationStatistics(traceList.getTraceIndex(ConstantPopulationModelParser.POPULATION_SIZE)); |
| 119 | // assertExpectation(ConstantPopulationModelParser.POPULATION_SIZE, popStats, 43.4478); |
| 120 | //// System.out.println("popStats = " + popStats.getMean()); |
| 121 | // |
| 122 | // TraceCorrelation coalescentStats = traceList.getCorrelationStatistics(traceList.getTraceIndex("coalescent")); |
| 123 | // assertExpectation("coalescent", coalescentStats, -73.4715); |
| 124 | //// System.out.println("coalescentStats = " + coalescentStats.getMean()); |
| 125 | // } |
| 126 | // |
| 127 | // private ArrayTraceList UncorrelatedRelaxedClock(ParametricDistributionModel distributionModel) throws Exception { |
| 128 | // Parameter popSize = new Parameter.Default(ConstantPopulationModelParser.POPULATION_SIZE, 380.0, 0, 38000.0); |
| 129 | // ConstantPopulationModel constantModel = createRandomInitialTree(popSize); |
| 130 | // |
| 131 | // CoalescentLikelihood coalescent = new CoalescentLikelihood(treeModel, null, new ArrayList<TaxonList>(), constantModel); |
| 132 | // coalescent.setId("coalescent"); |
| 133 | // |
| 134 | // // clock model |
| 135 | // Parameter rateCategoryParameter = new Parameter.Default(32); |
| 136 | // rateCategoryParameter.setId(DiscretizedBranchRates.BRANCH_RATES); |
| 137 | // |
| 138 | // DiscretizedBranchRates branchRateModel = new DiscretizedBranchRates(treeModel, rateCategoryParameter, |
| 139 | // distributionModel, 1, false, Double.NaN, false, false); |
| 140 | // |
| 141 | // RateStatistic meanRate = new RateStatistic("meanRate", treeModel, branchRateModel, true, true, RateStatisticParser.MEAN); |
| 142 | // RateStatistic coefficientOfVariation = new RateStatistic(RateStatisticParser.COEFFICIENT_OF_VARIATION, treeModel, branchRateModel, |
| 143 | // true, true, RateStatisticParser.COEFFICIENT_OF_VARIATION); |
| 144 | // RateCovarianceStatistic covariance = new RateCovarianceStatistic("covariance", treeModel, branchRateModel); |
| 145 | // |
| 146 | // // Sub model |
| 147 | // Parameter freqs = new Parameter.Default(alignment.getStateFrequencies()); |
| 148 | // Parameter kappa = new Parameter.Default(HKYParser.KAPPA, 1.0, 0, 100.0); |
| 149 | // |
| 150 | // FrequencyModel f = new FrequencyModel(Nucleotides.INSTANCE, freqs); |
| 151 | // HKY hky = new HKY(kappa, f); |
| 152 | // |
| 153 | // //siteModel |
| 154 | // GammaSiteModel siteModel = new GammaSiteModel(hky); |
| 155 | // Parameter mu = new Parameter.Default(GammaSiteModelParser.MUTATION_RATE, 1.0, 0, Double.POSITIVE_INFINITY); |
| 156 | // siteModel.setMutationRateParameter(mu); |
| 157 | // |
| 158 | // //treeLikelihood |
| 159 | // SitePatterns patterns = new SitePatterns(alignment, null, 0, -1, 1, true); |
| 160 | // |
| 161 | // TreeLikelihood treeLikelihood = new TreeLikelihood(patterns, treeModel, siteModel, branchRateModel, null, |
| 162 | // false, false, true, false, false); |
| 163 | // treeLikelihood.setId(TreeLikelihoodParser.TREE_LIKELIHOOD); |
| 164 | // |
| 165 | // // Operators |
| 166 | // OperatorSchedule schedule = new SimpleOperatorSchedule(); |
| 167 | // |
| 168 | // MCMCOperator operator = new ScaleOperator(kappa, 0.75); |
| 169 | // operator.setWeight(1.0); |
| 170 | // schedule.addOperator(operator); |
| 171 | // |
| 172 | // operator = new ScaleOperator(meanParam, 0.75); |
| 173 | // operator.setWeight(3.0); |
| 174 | // schedule.addOperator(operator); |
| 175 | // |
| 176 | // if (stdevParam != null) { |
| 177 | // operator = new ScaleOperator(stdevParam, 0.75); |
| 178 | // operator.setWeight(3.0); |
| 179 | // schedule.addOperator(operator); |
| 180 | // } |
| 181 | // |
| 182 | // Parameter allInternalHeights = treeModel.createNodeHeightsParameter(true, true, false); |
| 183 | // operator = new UpDownOperator(new Scalable[]{new Scalable.Default(meanParam)}, |
| 184 | // new Scalable[] {new Scalable.Default(allInternalHeights)}, 0.75, 3.0, AdaptationMode.ADAPTATION_ON); |
| 185 | // schedule.addOperator(operator); |
| 186 | // |
| 187 | // operator = new SwapOperator(rateCategoryParameter, 10); |
| 188 | // operator.setWeight(1.0); |
| 189 | // schedule.addOperator(operator); |
| 190 | // |
| 191 | // operator = new RandomWalkIntegerOperator(rateCategoryParameter, 1, 10.0); |
| 192 | // schedule.addOperator(operator); |
| 193 | // |
| 194 | // operator = new UniformIntegerOperator(rateCategoryParameter, (int) (double)rateCategoryParameter.getBounds().getLowerLimit(0), |
| 195 | // (int) (double)rateCategoryParameter.getBounds().getUpperLimit(0), 10.0); |
| 196 | // schedule.addOperator(operator); |
| 197 | // |
| 198 | // operator = new ScaleOperator(popSize, 0.75); |
| 199 | // operator.setWeight(3.0); |
| 200 | // schedule.addOperator(operator); |
| 201 | // |
| 202 | // Parameter rootHeight = treeModel.getRootHeightParameter(); |
| 203 | // rootHeight.setId(TREE_HEIGHT); |
| 204 | // operator = new ScaleOperator(rootHeight, 0.75); |
| 205 | // operator.setWeight(3.0); |
| 206 | // schedule.addOperator(operator); |
| 207 | // |
| 208 | // Parameter internalHeights = treeModel.createNodeHeightsParameter(false, true, false); |
| 209 | // operator = new UniformOperator(internalHeights, 30.0); |
| 210 | // schedule.addOperator(operator); |
| 211 | // |
| 212 | // operator = new SubtreeSlideOperator(treeModel, 15.0, 38.0, true, false, false, false, AdaptationMode.ADAPTATION_ON); |
| 213 | // schedule.addOperator(operator); |
| 214 | // |
| 215 | // operator = new ExchangeOperator(ExchangeOperator.NARROW, treeModel, 15.0); |
| 216 | //// operator.doOperation(); |
| 217 | // schedule.addOperator(operator); |
| 218 | // |
| 219 | // operator = new ExchangeOperator(ExchangeOperator.WIDE, treeModel, 3.0); |
| 220 | //// operator.doOperation(); |
| 221 | // schedule.addOperator(operator); |
| 222 | // |
| 223 | // operator = new WilsonBalding(treeModel, 3.0); |
| 224 | //// operator.doOperation(); |
| 225 | // schedule.addOperator(operator); |
| 226 | // |
| 227 | // //CompoundLikelihood |
| 228 | // List<Likelihood> likelihoods = new ArrayList<Likelihood>(); |
| 229 | // likelihoods.add(coalescent); |
| 230 | // Likelihood prior = new CompoundLikelihood(0, likelihoods); |
| 231 | // prior.setId(CompoundLikelihoodParser.PRIOR); |
| 232 | // |
| 233 | // likelihoods.clear(); |
| 234 | // likelihoods.add(treeLikelihood); |
| 235 | // Likelihood likelihood = new CompoundLikelihood(-1, likelihoods); |
| 236 | // |
| 237 | // likelihoods.clear(); |
| 238 | // likelihoods.add(prior); |
| 239 | // likelihoods.add(likelihood); |
| 240 | // Likelihood posterior = new CompoundLikelihood(0, likelihoods); |
| 241 | // posterior.setId(CompoundLikelihoodParser.POSTERIOR); |
| 242 | // |
| 243 | // // Log |
| 244 | // ArrayLogFormatter formatter = new ArrayLogFormatter(false); |
| 245 | // |
| 246 | // MCLogger[] loggers = new MCLogger[2]; |
| 247 | // loggers[0] = new MCLogger(formatter, 10000, false); |
| 248 | // loggers[0].add(posterior); |
| 249 | // loggers[0].add(treeLikelihood); |
| 250 | // loggers[0].add(rootHeight); |
| 251 | // loggers[0].add(meanParam); |
| 252 | // if (stdevParam != null) loggers[0].add(stdevParam); |
| 253 | // loggers[0].add(meanRate); |
| 254 | // loggers[0].add(coefficientOfVariation); |
| 255 | // loggers[0].add(covariance); |
| 256 | // loggers[0].add(popSize); |
| 257 | // loggers[0].add(kappa); |
| 258 | // loggers[0].add(coalescent); |
| 259 | // |
| 260 | // loggers[1] = new MCLogger(new TabDelimitedFormatter(System.out), 100000, false); |
| 261 | // loggers[1].add(posterior); |
| 262 | // loggers[1].add(treeLikelihood); |
| 263 | // loggers[1].add(rootHeight); |
| 264 | // loggers[1].add(meanRate); |
| 265 | // loggers[1].add(coalescent); |
| 266 | // |
| 267 | // // MCMC |
| 268 | // MCMC mcmc = new MCMC("mcmc1"); |
| 269 | // MCMCOptions options = new MCMCOptions(10000000); |
| 270 | // |
| 271 | // mcmc.setShowOperatorAnalysis(true); |
| 272 | // mcmc.init(options, posterior, schedule, loggers); |
| 273 | // mcmc.run(); |
| 274 | // |
| 275 | // // time |
| 276 | // System.out.println(mcmc.getTimer().toString()); |
| 277 | // |
| 278 | // // Tracer |
| 279 | // List<Trace> traces = formatter.getTraces(); |
| 280 | // ArrayTraceList traceList = new ArrayTraceList("RandomLocalClockTest", traces, 0); |
| 281 | // |
| 282 | // for (int i = 1; i < traces.size(); i++) { |
| 283 | // traceList.analyseTrace(i); |
| 284 | // } |
| 285 | // |
| 286 | // return traceList; |
| 287 | // } |
| 288 | // |
| 289 | // public static Test suite() { |
| 290 | // return new TestSuite(UncorrelatedRelaxedClockTest.class); |
| 291 | // } |
| 292 | } |
| 293 |