Debian Codegraph @

src/dr/evomodel/branchmodel/lineagespecific/BranchLikelihood.java

raw symbols SCIP
1 /*
2 * BranchLikelihood.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.branchmodel.lineagespecific;
27
28 import java.util.*;
29
30 import dr.evomodel.treelikelihood.BeagleTreeLikelihood;
31 import dr.evomodel.branchratemodel.CountableBranchCategoryProvider;
32 import dr.evomodel.tree.TreeModel;
33 import dr.inference.loggers.LogColumn;
34 import dr.inference.loggers.NumberColumn;
35 import dr.inference.model.CompoundModel;
36 import dr.inference.model.CompoundParameter;
37 import dr.inference.model.Likelihood;
38 import dr.inference.model.Model;
39 import dr.inference.model.Parameter;
40
41 @SuppressWarnings("serial")
42 public class BranchLikelihood implements Likelihood {
43
44 // Constructor fields
45 // private PatternList patternList;
46 private TreeModel treeModel;
47 // private BranchModel branchModel;
48 // private SiteRateModel siteRateModel;
49 // private FrequencyModel freqModel;
50 // private BranchRateModel branchRateModel;
51
52 private BeagleTreeLikelihood likelihood;
53 private CountableBranchCategoryProvider categoriesProvider;
54
55 private Parameter categoriesParameter;
56 private CompoundParameter uniqueParameters;
57 private CountableRealizationsParameter allParameters;
58
59 private final CompoundModel compoundModel = new CompoundModel(
60 "compoundModel");
61
62 public BranchLikelihood(
63 // PatternList patternList, //
64 TreeModel treeModel, //
65 // BranchModel branchModel, //
66 // SiteRateModel siteRateModel, //
67 // FrequencyModel freqModel, //
68 // BranchRateModel branchRateModel, //
69 BeagleTreeLikelihood likelihood,
70 Parameter categoriesParameter,
71 CompoundParameter uniqueParameters,
72 CountableRealizationsParameter allParameters
73 ) {
74
75 // this.patternList = patternList;
76 // this.treeModel = treeModel;
77 // this.branchModel = branchModel;
78 // this.siteRateModel = siteRateModel;
79 // this.freqModel = freqModel;
80 // this.branchRateModel = branchRateModel;
81 this.treeModel = treeModel;
82 this.categoriesParameter = categoriesParameter;
83 this.uniqueParameters = uniqueParameters;
84 this.allParameters = allParameters;
85
86 this.categoriesProvider = new CountableBranchCategoryProvider.IndependentBranchCategoryModel(
87 this.treeModel, categoriesParameter);
88
89 }// END: Constructor
90
91
92
93
94 @Override
95 public double getLogLikelihood() {
96
97 double loglike = 0;
98
99 for (int i = 0; i < categoriesParameter.getDimension(); i++) {
100
101 int category = (int) categoriesParameter.getParameterValue(i);
102 double value = uniqueParameters.getParameterValue(category);
103
104 allParameters.setParameterValue(i, value);
105
106 }//END: i loop
107
108 likelihood.makeDirty();
109 loglike = likelihood.getLogLikelihood();
110
111 return loglike;
112 }// END: getLogLikelihood
113
114 public double getLogLikelihood(int index) {
115
116
117 // likelihood.getBranchModel().getSubstitutionModels().get(index).getVariable(0).setValue(index, value)
118
119 double tmp1 = likelihood.getLogLikelihood();
120
121 int category = (int) categoriesParameter.getParameterValue(index);
122 double value = uniqueParameters.getParameterValue(category);
123 allParameters.setParameterValue(index, value);
124
125 likelihood.makeDirty();
126
127
128 double tmp2 = likelihood.getLogLikelihood();
129
130
131 return tmp2-tmp1;
132 }
133
134 @Override
135 public LogColumn[] getColumns() {
136 return new dr.inference.loggers.LogColumn[] { new LikelihoodColumn(
137 getId() == null ? "likelihood" : getId()) };
138 }// END: getColumns
139
140 @Override
141 public String getId() {
142 return"";
143 }// END: getId
144
145 @Override
146 public void setId(String id) {
147 }// END: setId
148
149 @Override
150 public Model getModel() {
151 return compoundModel;
152 }
153
154 @Override
155 public void makeDirty() {
156
157
158 }// END: makeDirty
159
160 @Override
161 public String prettyName() {
162 return Abstract.getPrettyName(this);
163 }// END: prettyName
164
165 @Override
166 public Set<Likelihood> getLikelihoodSet() {
167 return new HashSet<Likelihood>(Arrays.asList(this));
168 }
169
170 @Override
171 public boolean isUsed() {
172 return true;
173 }// END: isUsed
174
175 @Override
176 public void setUsed() {
177 }// END: setUsed
178
179 @Override
180 public boolean evaluateEarly() {
181 return false;
182 }
183
184 private class LikelihoodColumn extends NumberColumn {
185
186 public LikelihoodColumn(String label) {
187 super(label);
188 }// END: Constructor
189
190 public double getDoubleValue() {
191 return getLogLikelihood();
192 }
193
194 }// END: LikelihoodColumn class
195
196
197
198
199
200
201
202
203 // private LinkedList<Likelihood> getBranchLikelihoods() {
204 //
205 // // linked list preserves order
206 // LinkedList<Likelihood> loglikes = new LinkedList<Likelihood>();
207 //
208 // for (NodeRef branch : treeModel.getNodes()) {
209 //
210 // if (!treeModel.isRoot(branch)) {
211 //
212 // int branchCategory = categoriesProvider.getBranchCategory(
213 // treeModel, branch);
214 // int index = (int) categoriesParameter
215 // .getParameterValue(branchCategory);
216 //
217 // Likelihood branchLikelihood = uniqueLikelihoods.get(index);
218 //
219 // loglikes.add(branchLikelihood);
220 // compoundModel.addModel(branchLikelihood.getModel());
221 //
222 // }
223 // }// END: branch loop
224 //
225 // return loglikes;
226 // }// END: getBranchLikelihoods
227
228
229
230
231 }// END: class