Debian Codegraph @

src/dr/oldevomodel/substmodel/SVSComplexSubstitutionModel.java

raw symbols SCIP
1 /*
2 * SVSComplexSubstitutionModel.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.DataType;
29 import dr.inference.model.BayesianStochasticSearchVariableSelection;
30 import dr.inference.model.Parameter;
31 import dr.inference.model.Variable;
32 import dr.math.matrixAlgebra.Vector;
33
34 /**
35 * @author Marc A. Suchard
36 */
37 public class SVSComplexSubstitutionModel extends ComplexSubstitutionModel implements BayesianStochasticSearchVariableSelection {
38
39 public SVSComplexSubstitutionModel(String name, DataType dataType, FrequencyModel rootFreqModel,
40 Parameter rates, Parameter indicators) {
41 super(name, dataType, rootFreqModel, rates);
42 if (indicators != null) {
43 this.indicators = indicators;
44 addVariable(indicators);
45 } else {
46 this.indicators = new Parameter.Default(rates.getDimension(), 1.0);
47 }
48 }
49
50 protected double[] getRates() {
51 double[] rates = infinitesimalRates.getParameterValues();
52 double[] indies = indicators.getParameterValues();
53 final int dim = rates.length;
54 for (int i = 0; i < dim; i++)
55 rates[i] *= indies[i];
56 return rates;
57 }
58
59 protected void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
60 if (variable == infinitesimalRates && indicators.getParameterValue(index) == 0)
61 return; // ignore, does not affect likelihood
62 super.handleVariableChangedEvent(variable, index, type);
63 }
64
65 public Parameter getIndicators() {
66 return indicators;
67 }
68
69 // public double getLogLikelihood() {
70 // double logL = super.getLogLikelihood();
71 // if (logL == 0 && considerConnectedness && !isStronglyConnected()) { // Also check that graph is connected
72 // logL = Double.NEGATIVE_INFINITY;
73 // }
74 // return logL;
75 // }
76
77 // private boolean hasEdge(int i, int j) {
78 // return i != j && getIndicators().getParameterValue(getEntry(i,j,stateCount)) == 1;
79 // }
80 //
81 // private static int getEntry(int i, int j, int dim) {
82 // int entry = i * (dim - 1) + j;
83 // if( j > i )
84 // entry--;
85 // return entry;
86 // }
87 //
88 // private void depthFirstSearch(int node, BitVector visited) {
89 // visited.set(node);
90 // for(int v=0; v<stateCount; v++) {
91 // if (hasEdge(node,v) && !visited.get(v))
92 // depthFirstSearch(v,visited);
93 // }
94 // }
95
96 // /* Determines if the graph is strongly connected, such that there exists
97 // * a directed path from any vertex to any other vertex
98 // *
99 // */
100 // public boolean isStronglyConnected() {
101 // BitVector visited = new BitVector(stateCount);
102 // boolean connected = true;
103 // for(int i=0; i<stateCount && connected; i++) {
104 // visited.clear();
105 // depthFirstSearch(i,visited);
106 // connected = visited.cardinality() == stateCount;
107 // }
108 // return connected;
109 // }
110
111 public boolean validState() {
112 return getLogLikelihood() == 0;
113 }
114
115 public static void main(String[] arg) {
116
117 double[] rates = new double[]{0.097,0.515,3.346,0.623,0.389,0.631,0.362,1.127,4.262,0.424,0.758,1.297,0.728,0.228,0.003,0.075,0.312,0.356,0.927,4.420,2.719,0.264,4.267,0.741,1.106,1.568,1.215,0.172,0.204,1.493,0.592,0.105,1.583,1.201,0.783,2.224,0.888,1.401,0.137,0.259,1.197,0.056,1.939,1.385,0.690,0.815,0.279,1.100,1.715,0.011,1.509,0.961,0.112,1.305,2.797,0.578,1.177,1.009,0.316,1.143,1.861,0.176,0.140,0.104,0.571,0.521,0.761,1.795,1.065,1.563,2.972,2.295,0.075,1.690,1.011,0.128,0.484,0.355,1.668,1.052,0.089,0.104,0.056,1.591,0.054,0.487,1.034,1.145,0.403,0.254,0.474,0.181,0.124,2.067,2.208,0.120,2.638,0.195,1.897,1.955,1.113,1.399,4.901,3.218,0.361,1.934,1.681,3.572,0.806,0.077,0.042,0.310,0.243,1.526,3.572,4.173,0.740,3.086,0.645,2.755,0.280,2.476,0.476,5.174,0.057,0.225,1.310,0.201,0.491,1.507,0.604,0.404,0.907,0.048,2.439,0.676,0.382,0.062,1.173,2.026,2.813,0.655,1.511,0.452,0.137,0.435,0.685,0.826,1.735,0.935,0.697,1.230,5.416,1.043,0.747,0.945};
118 double[] indicators = new double[]{1.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,1.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,1.000,0.000,0.000,0.000,0.000,1.000,0.000,0.000,0.000,0.000,1.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,1.000,0.000,0.000,0.000,0.000,0.000,1.000,0.000,0.000,1.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,1.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,1.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,1.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,1.000,0.000,0.000,0.000,0.000,1.000,0.000,0.000,0.000,0.000,0.000,1.000,0.000,0.000,0.000,0.000,1.000,0.000,0.000,0.000,1.000,1.000,1.000,0.000,1.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,1.000};
119
120 Parameter ratesP = new Parameter.Default(rates);
121 Parameter indicatorsP = new Parameter.Default(indicators);
122
123 DataType dataType = new DataType() {
124
125 public String getDescription() {
126 return null;
127 }
128
129 public int getType() {
130 return 0;
131 }
132
133 @Override
134 public char[] getValidChars() {
135 return null;
136 }
137
138 public int getStateCount() {
139 return 13;
140 }
141 };
142
143 FrequencyModel freqModel = new FrequencyModel(dataType, new Parameter.Default(400, 1.0/400.0));
144
145 SVSComplexSubstitutionModel substModel = new SVSComplexSubstitutionModel("test",
146 dataType,
147 freqModel,
148 ratesP,indicatorsP);
149 double logL = substModel.getLogLikelihood();
150 System.out.println("Prior = "+logL);
151 if( !Double.isInfinite(logL) ) {
152 double[] finiteTimeProbs = new double[substModel.getDataType().getStateCount() * substModel.getDataType().getStateCount()];
153 double time = 1.0;
154 substModel.getTransitionProbabilities(time,finiteTimeProbs);
155 System.out.println("Probs = "+new Vector(finiteTimeProbs));
156 }
157 }
158
159 private Parameter indicators;
160 // private boolean considerConnectedness = false;
161
162 }