Debian Codegraph @

src/dr/evomodel/arg/HierarchicalPartitionLikelihood.java

raw symbols SCIP
1 /*
2 * HierarchicalPartitionLikelihood.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.evomodel.arg;
27
28 import dr.inference.model.Model;
29 import dr.inference.model.Parameter;
30 import dr.inference.model.Variable;
31 import dr.inference.model.Variable.ChangeType;
32 import dr.math.MathUtils;
33 import dr.xml.*;
34
35 public class HierarchicalPartitionLikelihood extends ARGPartitionLikelihood {
36
37 public static final String HIERARCHICAL_PARTITION_LIKELIHOOD = "hierarchicalPartitionLikelihood";
38
39 private Parameter probabilities;
40
41 public HierarchicalPartitionLikelihood(String id, ARGModel arg, Parameter probs) {
42 super(id, arg);
43
44 this.probabilities = probs;
45
46 addVariable(probs);
47 addModel(arg);
48 }
49
50 public double[] generatePartition() {
51 double[] partition = new double[getNumberOfPartitionsMinusOne() + 1];
52
53 partition[0] = 0.0;
54
55 for (int i = 0; i < partition.length; i++)
56 partition[i] = 0.0;
57
58 while (UniformPartitionLikelihood.arraySum(partition) == 0.0) {
59 for (int i = 1; i < partition.length; i++) {
60 if (MathUtils.nextDouble() < probabilities.getParameterValue(i - 1)) {
61 partition[i] = 1.0;
62 } else {
63 partition[i] = 0.0;
64 }
65 }
66 }
67
68
69 return partition;
70 }
71
72 public double getLogLikelihood(double[] partition) {
73 double logLike = 0;
74
75 for (int i = 1; i < partition.length; i++) {
76 if (partition[i] == 1.0) {
77 logLike += Math.log(probabilities.getParameterValue(i - 1));
78 } else {
79 logLike += Math.log(1 - probabilities.getParameterValue(i - 1));
80 }
81 }
82
83 // return 1;
84
85 return logLike;
86 }
87
88 protected void acceptState() {
89 // nothing to do!
90 }
91
92 protected void handleModelChangedEvent(Model model, Object object, int index) {
93 // i'm lazy
94
95 }
96
97 protected void handleVariableChangedEvent(Variable variable, int index,
98 ChangeType type) {
99 // I'm lazy, so I compute after each step :)
100 }
101
102 protected void restoreState() {
103 //nothing to restore!
104 }
105
106 @Override
107 protected void storeState() {
108 //nothing to store
109
110 }
111
112 public static XMLObjectParser PARSER = new AbstractXMLObjectParser() {
113
114 public String getParserDescription() {
115 return null;
116 }
117
118 public Class getReturnType() {
119 return PoissonPartitionLikelihood.class;
120 }
121
122 public XMLSyntaxRule[] getSyntaxRules() {
123 return new XMLSyntaxRule[]{
124
125 new ElementRule(ARGModel.class, false),
126 };
127 }
128
129 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
130 String id = "";
131 if (xo.hasId())
132 id = xo.getId();
133
134 ARGModel arg = (ARGModel) xo.getChild(ARGModel.class);
135
136 Parameter values = (Parameter) xo.getChild(Parameter.class);
137
138 if (values.getDimension() != arg.getNumberOfPartitions() - 1) {
139 throw new XMLParseException("The dimension of the parameter must equal the number of partitions minus 1 ");
140 }
141
142 if (arg.isRecombinationPartitionType()) {
143 throw new XMLParseException(ARGModel.TREE_MODEL + " must be of type " + ARGModel.REASSORTMENT_PARTITION);
144 }
145
146 return new HierarchicalPartitionLikelihood(id, arg, values);
147 }
148
149 public String getParserName() {
150 return HIERARCHICAL_PARTITION_LIKELIHOOD;
151 }
152
153 };
154
155 }