| 1 |
package test.dr.inference.prior; |
| 2 |
|
| 3 |
import dr.evolution.util.Units; |
| 4 |
import dr.evomodel.coalescent.ConstantPopulationModel; |
| 5 |
import dr.evomodelxml.coalescent.ConstantPopulationModelParser; |
| 6 |
import dr.inference.distribution.DistributionLikelihood; |
| 7 |
import dr.inference.loggers.ArrayLogFormatter; |
| 8 |
import dr.inference.loggers.MCLogger; |
| 9 |
import dr.inference.loggers.TabDelimitedFormatter; |
| 10 |
import dr.inference.mcmc.MCMC; |
| 11 |
import dr.inference.mcmc.MCMCOptions; |
| 12 |
import dr.inference.model.CompoundLikelihood; |
| 13 |
import dr.inference.model.DummyLikelihood; |
| 14 |
import dr.inference.model.Likelihood; |
| 15 |
import dr.inference.model.Parameter; |
| 16 |
import dr.inference.operators.MCMCOperator; |
| 17 |
import dr.inference.operators.OperatorSchedule; |
| 18 |
import dr.inference.operators.ScaleOperator; |
| 19 |
import dr.inference.operators.SimpleOperatorSchedule; |
| 20 |
import dr.inference.trace.ArrayTraceList; |
| 21 |
import dr.inference.trace.Trace; |
| 22 |
import dr.inference.trace.TraceCorrelation; |
| 23 |
import dr.math.MathUtils; |
| 24 |
import dr.math.distributions.LogNormalDistribution; |
| 25 |
import junit.framework.Test; |
| 26 |
import junit.framework.TestSuite; |
| 27 |
import test.dr.inference.trace.TraceCorrelationAssert; |
| 28 |
|
| 29 |
import java.util.ArrayList; |
| 30 |
import java.util.List; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public class LognormalPriorTest extends TraceCorrelationAssert { |
| 38 |
|
| 39 |
public LognormalPriorTest(String name) { |
| 40 |
super(name); |
| 41 |
} |
| 42 |
|
| 43 |
public void setUp() throws Exception { |
| 44 |
super.setUp(); |
| 45 |
|
| 46 |
MathUtils.setSeed(666); |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
public void testLognormalPrior() { |
| 51 |
|
| 52 |
|
| 53 |
Parameter popSize = new Parameter.Default(6.0); |
| 54 |
popSize.setId(ConstantPopulationModelParser.POPULATION_SIZE); |
| 55 |
ConstantPopulationModel demo = new ConstantPopulationModel(popSize, Units.Type.YEARS); |
| 56 |
|
| 57 |
|
| 58 |
Likelihood dummyLikelihood = new DummyLikelihood(demo); |
| 59 |
|
| 60 |
|
| 61 |
OperatorSchedule schedule = new SimpleOperatorSchedule(); |
| 62 |
|
| 63 |
MCMCOperator operator = new ScaleOperator(popSize, 0.75); |
| 64 |
operator.setWeight(1.0); |
| 65 |
schedule.addOperator(operator); |
| 66 |
|
| 67 |
|
| 68 |
ArrayLogFormatter formatter = new ArrayLogFormatter(false); |
| 69 |
|
| 70 |
MCLogger[] loggers = new MCLogger[2]; |
| 71 |
loggers[0] = new MCLogger(formatter, 1000, false); |
| 72 |
|
| 73 |
loggers[0].add(popSize); |
| 74 |
|
| 75 |
loggers[1] = new MCLogger(new TabDelimitedFormatter(System.out), 100000, false); |
| 76 |
|
| 77 |
loggers[1].add(popSize); |
| 78 |
|
| 79 |
|
| 80 |
MCMC mcmc = new MCMC("mcmc1"); |
| 81 |
MCMCOptions options = new MCMCOptions(1000000); |
| 82 |
|
| 83 |
DistributionLikelihood logNormalLikelihood = new DistributionLikelihood(new LogNormalDistribution(1.0, 1.0), 0); |
| 84 |
logNormalLikelihood.addData(popSize); |
| 85 |
|
| 86 |
List<Likelihood> likelihoods = new ArrayList<Likelihood>(); |
| 87 |
likelihoods.add(logNormalLikelihood); |
| 88 |
Likelihood prior = new CompoundLikelihood(0, likelihoods); |
| 89 |
|
| 90 |
likelihoods.clear(); |
| 91 |
likelihoods.add(dummyLikelihood); |
| 92 |
Likelihood likelihood = new CompoundLikelihood(-1, likelihoods); |
| 93 |
|
| 94 |
likelihoods.clear(); |
| 95 |
likelihoods.add(prior); |
| 96 |
likelihoods.add(likelihood); |
| 97 |
Likelihood posterior = new CompoundLikelihood(0, likelihoods); |
| 98 |
|
| 99 |
mcmc.setShowOperatorAnalysis(true); |
| 100 |
mcmc.init(options, posterior, schedule, loggers); |
| 101 |
mcmc.run(); |
| 102 |
|
| 103 |
|
| 104 |
System.out.println(mcmc.getTimer().toString()); |
| 105 |
|
| 106 |
|
| 107 |
List<Trace> traces = formatter.getTraces(); |
| 108 |
ArrayTraceList traceList = new ArrayTraceList("LognormalPriorTest", traces, 0); |
| 109 |
|
| 110 |
for (int i = 1; i < traces.size(); i++) { |
| 111 |
traceList.analyseTrace(i); |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
TraceCorrelation popSizeStats = traceList.getCorrelationStatistics(traceList.getTraceIndex(ConstantPopulationModelParser.POPULATION_SIZE)); |
| 117 |
|
| 118 |
System.out.println("Expectation of Log-Normal(1,1) is e^(M+S^2/2) = e^(1.5) = " + Math.exp(1.5)); |
| 119 |
assertExpectation(ConstantPopulationModelParser.POPULATION_SIZE, popSizeStats, Math.exp(1.5)); |
| 120 |
} |
| 121 |
|
| 122 |
public static Test suite() { |
| 123 |
return new TestSuite(LognormalPriorTest.class); |
| 124 |
} |
| 125 |
} |