Debian Codegraph @

src/test/dr/integration/PathSampling.java

raw symbols SCIP
1 package test.dr.integration;
2
3 import dr.evolution.sequence.Sequence;
4 import dr.evolution.datatype.Nucleotides;
5 import dr.evolution.datatype.DataType;
6 import dr.inference.model.Parameter;
7 import dr.oldevomodel.substmodel.HKY;
8 import dr.oldevomodel.substmodel.FrequencyModel;
9 import dr.math.distributions.GammaDistribution;
10 import dr.math.LogTricks;
11
12 /**
13 * @author Marc A. Suchard
14 */
15 public class PathSampling {
16
17 public PathSampling() {
18
19 int simulationLength = 10000000;
20 double kappaPriorShape = 0.1;
21 double kappaPriorScale = 1.0;
22 double rootPriorShape = 1.0;
23 double rootPriorScale = 1.0;
24
25 // Computes the marginal likelihood of two aligned DNA sequences in a tree with height T under the Kimura model
26 Sequence seq1 = new Sequence("AGAGCTCTAAGAGCTCTAAGAGCTCTAAGAGCTCTA");
27 Sequence seq2 = new Sequence("AAGGCCTTTAAGGCCTTTAAGGCCTTTAAGGCCTTT");
28 if (seq1.getLength() != seq2.getLength()) {
29 throw new RuntimeException("Data sequences must be the same length");
30 }
31 DataType dataType = Nucleotides.INSTANCE;
32 seq1.setDataType(dataType);
33 seq2.setDataType(dataType);
34 final int seqLength = seq1.getLength();
35 final int stateCount = dataType.getStateCount();
36 final double[] transitionProbabilities = new double[stateCount*stateCount];
37 final double[] frequencies = new double[stateCount];
38 for(int i = 0; i < stateCount; i++) {
39 frequencies[i] = 1.0 / stateCount;
40 }
41
42 Parameter kappa = new Parameter.Default(10.0);
43 HKY hky = new HKY(kappa, new FrequencyModel(dataType, frequencies));
44 Parameter root = new Parameter.Default(1.0);
45
46 GammaDistribution kappaPrior = new GammaDistribution(kappaPriorShape, kappaPriorScale);
47 GammaDistribution rootPrior = new GammaDistribution(rootPriorShape, rootPriorScale);
48
49 double logMarginalLikelihood = 0;
50 for(int i = 0; i < simulationLength; i++) {
51
52 if (i % 100000 == 0) {
53 System.err.println("iter: "+i);
54 }
55
56 // Draw new parameters from prior
57 kappa.setParameterValue(0,kappaPrior.nextGamma());
58 root.setParameterValue(0,rootPrior.nextGamma());
59
60 // Compute likelihood * prior
61 double logPosterior = 0;
62 hky.getTransitionProbabilities(root.getParameterValue(0), transitionProbabilities);
63 for(int j = 0; j < seqLength; j++) {
64 int char1 = seq1.getState(j);
65 int char2 = seq2.getState(j);
66 double prob = 0;
67 for(int k = 0; k < stateCount; k++ ) {
68 prob += frequencies[k] * transitionProbabilities[k * stateCount + char1] *
69 transitionProbabilities[k * stateCount + char2];
70 }
71 logPosterior += Math.log(prob);
72 }
73
74 // Add to logMarginalLikelihood on the unit-scale
75 if (i == 0) {
76 logMarginalLikelihood = logPosterior;
77 } else {
78 logMarginalLikelihood = LogTricks.logSum(logMarginalLikelihood,logPosterior);
79 }
80 }
81
82 // Normalize
83 logMarginalLikelihood -= Math.log(simulationLength);
84 System.out.println("logMarginalLikelihood = "+logMarginalLikelihood);
85 }
86
87 public static void main(String[] args) {
88 new PathSampling();
89 }
90
91 }