| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 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 |
|
| 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) { |
| 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) { |
| 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 |
|
| 106 |
|
| 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 |
} |