Debian Codegraph @

src/dr/inference/model/BayesianStochasticSearchVariableSelection.java

raw symbols SCIP
1 /*
2 * BayesianStochasticSearchVariableSelection.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.inference.model;
27
28 import cern.colt.bitvector.BitVector;
29 import dr.math.MathUtils;
30 import dr.oldevomodel.substmodel.SubstitutionModel;
31
32 /**
33 * @author Marc Suchard
34 */
35
36 public interface BayesianStochasticSearchVariableSelection {
37
38 public Parameter getIndicators();
39
40 public boolean validState();
41
42 public class Utils {
43
44 public static boolean connectedAndWellConditioned(double[] probability,
45 dr.evomodel.substmodel.SubstitutionModel substModel) {
46 if (probability == null) {
47 int stateCount = substModel.getDataType().getStateCount();
48 probability = new double[stateCount*stateCount];
49 }
50 try {
51 substModel.getTransitionProbabilities(defaultExpectedMutations,probability);
52 return connectedAndWellConditioned(probability);
53 } catch (Exception e) { // Any numerical error is bad news
54 return false;
55 }
56 }
57
58 public static boolean connectedAndWellConditioned(double[] probability,
59 SubstitutionModel substModel) {
60 if (probability == null) {
61 int stateCount = substModel.getDataType().getStateCount();
62 probability = new double[stateCount*stateCount];
63 }
64 try {
65 substModel.getTransitionProbabilities(defaultExpectedMutations,probability);
66 return connectedAndWellConditioned(probability);
67 } catch (Exception e) { // Any numerical error is bad news
68 return false;
69 }
70 }
71
72 public static boolean connectedAndWellConditioned(double[] probability) {
73 for(double prob : probability) {
74 if(prob < tolerance || prob >= 1.0) {
75 return false;
76 }
77 }
78 return true;
79 }
80
81 public static void randomize(Parameter indicators,int dim, boolean reversible) {
82 do {
83 for (int i = 0; i < indicators.getDimension(); i++)
84 indicators.setParameterValue(i,
85 (MathUtils.nextDouble() < 0.5) ? 0.0 : 1.0);
86 } while (!(isStronglyConnected(indicators.getParameterValues(),
87 dim, reversible)));
88 }
89
90 public static void setTolerance(double newTolerance) {
91 tolerance = newTolerance;
92 }
93
94 public static double getTolerance() {
95 return tolerance;
96 }
97
98 public static void setScalar(double newScalar) {
99 defaultExpectedMutations = newScalar;
100 }
101 public static double getScalar() {
102 return defaultExpectedMutations;
103 }
104
105 /* Determines if the graph is strongly connected, such that there exists
106 * a directed path from any vertex to any other vertex
107 *
108 */
109 public static boolean isStronglyConnected(double[] indicatorValues, int dim, boolean reversible) {
110 BitVector visited = new BitVector(dim);
111 boolean connected = true;
112 for (int i = 0; i < dim && connected; i++) {
113 visited.clear();
114 depthFirstSearch(i, visited, indicatorValues, dim, reversible);
115 connected = visited.cardinality() == dim;
116 }
117 return connected;
118 }
119
120 private static boolean hasEdge(int i, int j, double[] indicatorValues,
121 int dim, boolean reversible) {
122 return i != j && indicatorValues[getEntry(i, j, dim, reversible)] == 1;
123 }
124
125 private static int getEntry(int i, int j, int dim, boolean reversible) {
126 if (reversible) {
127 if (j < i) {
128 return getEntry(j,i,dim,reversible);
129 }
130 int entry = i * dim - i * (i + 1) / 2 + j - 1 -i;
131 return entry;
132 }
133
134 int entry = i * (dim - 1) + j;
135 if (j > i)
136 entry--;
137 return entry;
138 }
139
140 private static void depthFirstSearch(int node, BitVector visited, double[] indicatorValues,
141 int dim, boolean reversible) {
142 visited.set(node);
143 for (int v = 0; v < dim; v++) {
144 if (hasEdge(node, v, indicatorValues, dim, reversible) && !visited.get(v))
145 depthFirstSearch(v, visited, indicatorValues, dim, reversible);
146 }
147 }
148
149 private static double defaultExpectedMutations = 1.0;
150 private static double tolerance = 1E-20;
151 }
152 }