Debian Codegraph @

src/dr/evomodel/arg/UniformPartitionLikelihood.java

raw symbols SCIP
1 /*
2 * UniformPartitionLikelihood.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.Likelihood;
29 import dr.inference.model.Model;
30 import dr.inference.model.Variable;
31 import dr.inference.model.Variable.ChangeType;
32 import dr.math.MathUtils;
33 import dr.xml.*;
34
35
36 public class UniformPartitionLikelihood extends ARGPartitionLikelihood implements Likelihood {
37
38 public static final String UNIFORM_PARTITION_LIKELIHOOD = "uniformPartitionLikelihood";
39 private double logStateCount;
40 private boolean isRecombination;
41
42
43 public UniformPartitionLikelihood(String id, ARGModel arg) {
44 super(id, arg);
45
46 isRecombination = arg.isRecombinationPartitionType();
47
48 int numberOfPartitionsMinusOne = getNumberOfPartitionsMinusOne();
49
50 if (arg.isRecombinationPartitionType()) {
51 //For example, if we have five partitions, we can have the following
52 // 0 1 1 1 1
53 // 0 0 1 1 1 <-- Equates to four possibilities
54 // 0 0 0 1 1
55 // 0 0 0 0 1
56
57 logStateCount = Math.log(numberOfPartitionsMinusOne);
58 } else {
59 //You basically choose a subset from all possible subset of the final four
60 //there are 2^numberOfPartitionsMinusOne of these
61 //
62 //Except! you cannot choose the empty set.
63
64
65 double rValue = Math.pow(2.0, numberOfPartitionsMinusOne) - 1;
66
67 logStateCount = Math.log(rValue);
68 }
69 }
70
71 public double[] generatePartition() {
72 if (isRecombination) {
73 return generateRecombinationPartition();
74 }
75 return generateReassortmentPartition();
76 }
77
78 private double[] generateRecombinationPartition() {
79 int numberOfPartitionsMinusOne = getNumberOfPartitionsMinusOne();
80
81 int cut = MathUtils.nextInt(numberOfPartitionsMinusOne);
82
83 int leftValue = 0; //At one time, these values could switch.
84 int rightValue = 1;
85
86 double[] partition = new double[numberOfPartitionsMinusOne + 1];
87
88 for (int i = 0; i < cut + 1; i++)
89 partition[i] = leftValue;
90 for (int i = cut + 1; i < partition.length; i++)
91 partition[i] = rightValue;
92
93 return partition;
94 }
95
96 public static double arraySum(double[] x) {
97 double a = 0;
98 for (double b : x)
99 a += b;
100 return a;
101 }
102
103 private double[] generateReassortmentPartition() {
104 int numberOfPartitions = getNumberOfPartitionsMinusOne() + 1;
105
106 double[] partition = new double[numberOfPartitions];
107
108 while (arraySum(partition) == 0) {
109 for (int i = 1; i < partition.length; i++) {
110 if (MathUtils.nextBoolean()) {
111 partition[i] = 1.0;
112 } else {
113 partition[i] = 0.0;
114 }
115 }
116 }
117
118 partition[0] = 0.0;
119
120 return partition;
121 }
122
123
124 public double getLogLikelihood(double[] partition) {
125 return -logStateCount;
126 }
127
128 public static XMLObjectParser PARSER = new AbstractXMLObjectParser() {
129
130 public String getParserDescription() {
131 return null;
132 }
133
134 public Class getReturnType() {
135 return UniformPartitionLikelihood.class;
136 }
137
138 public XMLSyntaxRule[] getSyntaxRules() {
139 return new XMLSyntaxRule[]{
140 new ElementRule(ARGModel.class, false),
141 };
142 }
143
144 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
145 ARGModel arg = (ARGModel) xo.getChild(ARGModel.class);
146
147 String id = "";
148 if (xo.hasId())
149 id = xo.getId();
150
151 return new UniformPartitionLikelihood(id, arg);
152 }
153
154 public String getParserName() {
155 return UNIFORM_PARTITION_LIKELIHOOD;
156 }
157
158 };
159
160 protected void acceptState() {
161 //nothing to do
162 }
163
164
165 protected void handleModelChangedEvent(Model model, Object object, int index) {
166 //has no submodels
167 }
168
169 protected void handleVariableChangedEvent(Variable variable, int index,
170 ChangeType type) {
171 //has no parameters
172 }
173
174 protected void restoreState() {
175 //nothing to restore
176 }
177
178 protected void storeState() {
179 // nothing to store
180 }
181
182 }