Debian Codegraph @

src/dr/evomodel/arg/ARGRatePrior.java

raw symbols SCIP
1 /*
2 * ARGRatePrior.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.evomodel.arg.ARGModel.Node;
29 import dr.inference.model.AbstractModelLikelihood;
30 import dr.inference.model.Model;
31 import dr.inference.model.Parameter;
32 import dr.inference.model.Variable;
33 import dr.inference.model.Variable.ChangeType;
34 import dr.math.MathUtils;
35 import dr.math.distributions.GammaDistribution;
36 import dr.xml.*;
37
38 public class ARGRatePrior extends AbstractModelLikelihood {
39
40 public static final String ARG_RATE_PRIOR = "argRatePrior";
41 public static final String SIGMA = "sigma";
42
43 private final ARGModel arg;
44 private final Parameter logNormalSigma;
45
46
47 public ARGRatePrior(String name, ARGModel arg, Parameter sigma) {
48 super(name);
49
50 this.arg = arg;
51 this.logNormalSigma = sigma;
52
53 addModel(arg);
54 addVariable(sigma);
55
56 }
57
58 public double[] generateValues() {
59
60 double[] values = new double[arg.getNumberOfPartitions()];
61
62 double sigma = logNormalSigma.getParameterValue(0);
63
64 double oneOverSigma = 1.0 / sigma;
65
66 for (int i = 0; i < values.length; i++) {
67 values[i] = MathUtils.nextGamma(oneOverSigma, oneOverSigma);
68 }
69
70
71 return values;
72 }
73
74 public double getLogLikelihood() {
75 return calculateLogLikelihood();
76 }
77
78 public double getAddHastingsRatio(double[] values) {
79 return -calculateLogLikelihood(values);
80 }
81
82
83 private double calculateLogLikelihood(double[] values) {
84 double logLike = 0;
85
86 double sigma = logNormalSigma.getParameterValue(0);
87 double oneOverSigma = 1.0 / sigma;
88
89 for (double d : values) {
90 logLike += GammaDistribution.logPdf(d, oneOverSigma, sigma);
91 }
92
93 return logLike;
94 }
95
96 private double calculateLogLikelihood() {
97 double logLike = 0;
98
99
100 for (int i = 0, n = arg.getNodeCount(); i < n; i++) {
101 Node x = (Node) arg.getNode(i);
102
103 if (!x.isRoot() && x.isBifurcation()) {
104
105 double[] values = x.rateParameter.getParameterValues();
106
107 logLike += calculateLogLikelihood(values);
108 }
109 }
110
111
112 return logLike;
113 }
114
115
116 public Model getModel() {
117 return this;
118 }
119
120 public void makeDirty() {
121
122 }
123
124 public String getId() {
125 return super.getId();
126 }
127
128 public void setId(String id) {
129 super.setId(id);
130 }
131
132 protected void acceptState() {
133 }
134
135 protected void handleModelChangedEvent(Model model, Object object, int index) {
136 }
137
138 protected void handleVariableChangedEvent(Variable variable, int index,
139 ChangeType type) {
140 }
141
142 protected void restoreState() {
143
144 }
145
146 protected void storeState() {
147 }
148
149 public static XMLObjectParser PARSER = new AbstractXMLObjectParser() {
150
151 public String getParserDescription() {
152 return null;
153 }
154
155 public Class getReturnType() {
156 return ARGRatePrior.class;
157 }
158
159 public XMLSyntaxRule[] getSyntaxRules() {
160 return null;
161 }
162
163 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
164 String id = xo.getAttribute(XMLParser.ID, "");
165
166
167 Parameter sigma = (Parameter) xo.getChild(Parameter.class);
168
169
170 ARGModel arg = (ARGModel) xo.getChild(ARGModel.class);
171
172 return new ARGRatePrior(id, arg, sigma);
173 }
174
175 public String getParserName() {
176 return ARG_RATE_PRIOR;
177 }
178
179 };
180
181
182 }