Debian Codegraph @

src/dr/evomodel/substmodel/SVSGeneralSubstitutionModel.java

raw symbols SCIP
1 /*
2 * SVSGeneralSubstitutionModel.java
3 *
4 * Copyright (c) 2002-2016 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.substmodel;
27
28 import dr.evolution.datatype.DataType;
29 import dr.inference.loggers.LogColumn;
30 import dr.inference.loggers.NumberColumn;
31 import dr.inference.model.*;
32 import dr.util.Citable;
33 import dr.util.Citation;
34 import dr.util.CommonCitations;
35
36 import java.util.*;
37
38 /**
39 * @author Marc Suchard
40 */
41
42 public class SVSGeneralSubstitutionModel extends GeneralSubstitutionModel implements Likelihood,
43 BayesianStochasticSearchVariableSelection, Citable {
44
45 public SVSGeneralSubstitutionModel(String name, DataType dataType, FrequencyModel freqModel,
46 Parameter ratesParameter, Parameter indicatorsParameter) {
47 super(name, dataType, freqModel, ratesParameter, -1);
48
49 if (indicatorsParameter == null) {
50 this.indicatorsParameter = new Parameter.Default(ratesParameter.getDimension(), 1.0);
51 } else {
52 this.indicatorsParameter = indicatorsParameter;
53 addVariable(indicatorsParameter);
54 }
55
56 setupIndicatorDimensionNames(-1);
57 }
58
59 @Override
60 protected void setupRelativeRates(double[] rates) {
61 for (int i = 0; i < rates.length; i++) {
62 rates[i] = ratesParameter.getParameterValue(i) * indicatorsParameter.getParameterValue(i);
63 }
64 }
65
66 protected void setupIndicatorDimensionNames(int relativeTo) {
67 List<String> indicatorNames = new ArrayList<String>();
68
69 String indicatorPrefix = indicatorsParameter.getParameterName();
70
71 for (int i = 0; i < dataType.getStateCount(); ++i) {
72 for (int j = i + 1; j < dataType.getStateCount(); ++j) {
73 indicatorNames.add(getDimensionString(i, j, indicatorPrefix));
74 }
75 }
76
77 String[] tmp = new String[0];
78 indicatorsParameter.setDimensionNames(indicatorNames.toArray(tmp));
79 }
80
81 public Parameter getIndicators() {
82 return indicatorsParameter;
83 }
84
85 public boolean validState() {
86 return !updateMatrix || BayesianStochasticSearchVariableSelection.Utils.connectedAndWellConditioned(probability,this);
87 }
88
89 protected void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
90 if (variable == ratesParameter && indicatorsParameter.getParameterValue(index) == 0)
91 return; // Does not affect likelihood
92 super.handleVariableChangedEvent(variable,index,type);
93 }
94
95 /**
96 * Get the model.
97 *
98 * @return the model.
99 */
100 public Model getModel() {
101 return this;
102 }
103
104 /**
105 * Get the log likelihood.
106 *
107 * @return the log likelihood.
108 */
109 public double getLogLikelihood() {
110 if (updateMatrix) {
111 if (!BayesianStochasticSearchVariableSelection.Utils.connectedAndWellConditioned(probability,this)) {
112 return Double.NEGATIVE_INFINITY;
113 }
114 }
115 return 0;
116 }
117
118 /**
119 * Needs to be evaluated before the corresponding data likelihood.
120 * @return
121 */
122 public boolean evaluateEarly() {
123 return true;
124 }
125
126 /**
127 * Forces a complete recalculation of the likelihood next time getLikelihood is called
128 */
129 public void makeDirty() {
130 updateMatrix = true;
131 }
132
133 /**
134 * @return A detailed name of likelihood for debugging.
135 */
136 public String prettyName() {
137 return "SVSGeneralSubstitutionModel-connectedness";
138 }
139
140 @Override
141 public Set<Likelihood> getLikelihoodSet() {
142 return new HashSet<Likelihood>(Arrays.asList(this));
143 }
144
145 @Override
146 public boolean isUsed() {
147 return super.isUsed() && isUsed;
148 }
149
150 public void setUsed() {
151 isUsed = true;
152 }
153
154 private boolean isUsed = false;
155
156 // **************************************************************
157 // Loggable IMPLEMENTATION
158 // **************************************************************
159
160 public LogColumn[] getColumns() {
161 return new LogColumn[]{
162 new LikelihoodColumn(getId())
163 };
164 }
165
166 @Override
167 public Citation.Category getCategory() {
168 return Citation.Category.SUBSTITUTION_MODELS;
169 }
170
171 @Override
172 public String getDescription() {
173 return "Stochastic search variable selection, reversible substitution model";
174 }
175
176 @Override
177 public List<Citation> getCitations() {
178 return Collections.singletonList(CommonCitations.LEMEY_2009_BAYESIAN);
179 }
180
181 protected class LikelihoodColumn extends NumberColumn {
182 public LikelihoodColumn(String label) {
183 super(label);
184 }
185
186 public double getDoubleValue() {
187 return getLogLikelihood();
188 }
189 }
190
191 private double[] probability = null;
192
193 private final Parameter indicatorsParameter;
194
195
196 }