Debian Codegraph @

src/dr/oldevomodel/substmodel/SVSGeneralSubstitutionModel.java

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