| 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.operators; |
| 27 |
|
| 28 |
import dr.inference.model.Bounds; |
| 29 |
import dr.inference.model.Parameter; |
| 30 |
import dr.inference.model.Variable; |
| 31 |
import dr.math.distributions.RandomGenerator; |
| 32 |
import dr.xml.*; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public class GenericIndependentSampler extends SimpleMCMCOperator { |
| 42 |
|
| 43 |
public static final String OPERATOR_NAME = "genericIndependentSampler"; |
| 44 |
|
| 45 |
private final Variable<Double> variable; |
| 46 |
private final RandomGenerator randomGenerator; |
| 47 |
private final boolean univariate; |
| 48 |
private final int generatorLength; |
| 49 |
|
| 50 |
public GenericIndependentSampler(Variable variable, RandomGenerator randomGenerator, double weight) { |
| 51 |
|
| 52 |
this.variable = variable; |
| 53 |
this.randomGenerator = randomGenerator; |
| 54 |
|
| 55 |
Object draw = randomGenerator.nextRandom(); |
| 56 |
if (draw instanceof Double) { |
| 57 |
generatorLength = 1; |
| 58 |
univariate = true; |
| 59 |
} else if (draw instanceof double[]) { |
| 60 |
generatorLength = ((double[]) draw).length; |
| 61 |
univariate = false; |
| 62 |
} else { |
| 63 |
throw new IllegalArgumentException("Unknown random generator in " + getOperatorName()); |
| 64 |
} |
| 65 |
|
| 66 |
setWeight(weight); |
| 67 |
} |
| 68 |
|
| 69 |
public String getPerformanceSuggestion() { |
| 70 |
return ""; |
| 71 |
} |
| 72 |
|
| 73 |
public String getOperatorName() { |
| 74 |
return "genericIndependentSampler(" + variable.getVariableName() + ")"; |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
public double doOperation() { |
| 81 |
|
| 82 |
double logq = 0; |
| 83 |
|
| 84 |
final Bounds<Double> bounds = variable.getBounds(); |
| 85 |
final int dim = variable.getSize(); |
| 86 |
|
| 87 |
|
| 88 |
int i = 0; |
| 89 |
while (i < dim) { |
| 90 |
Object draw = randomGenerator.nextRandom(); |
| 91 |
if (univariate) { |
| 92 |
double currentValue = variable.getValue(i); |
| 93 |
double newValue = (Double) draw; |
| 94 |
logq += randomGenerator.logPdf(currentValue) - randomGenerator.logPdf(newValue); |
| 95 |
|
| 96 |
if (newValue < bounds.getLowerLimit(i) || newValue > bounds.getUpperLimit(i)) { |
| 97 |
|
| 98 |
return Double.NEGATIVE_INFINITY; |
| 99 |
} |
| 100 |
} else { |
| 101 |
double[] currentValue = new double[generatorLength]; |
| 102 |
double[] newValue = (double[]) draw; |
| 103 |
|
| 104 |
for (int j = 0; j < generatorLength; ++j) { |
| 105 |
final int index = i * generatorLength + j; |
| 106 |
currentValue[j] = variable.getValue(index); |
| 107 |
} |
| 108 |
|
| 109 |
logq += randomGenerator.logPdf(currentValue) - randomGenerator.logPdf(newValue); |
| 110 |
|
| 111 |
for (int j = 0; j < generatorLength; ++j) { |
| 112 |
final int index = i * generatorLength + j; |
| 113 |
|
| 114 |
if (newValue[j] < bounds.getLowerLimit(index) || newValue[j] > bounds.getUpperLimit(index)) { |
| 115 |
|
| 116 |
return Double.NEGATIVE_INFINITY; |
| 117 |
} |
| 118 |
variable.setValue(index, newValue[j]); |
| 119 |
} |
| 120 |
} |
| 121 |
i += generatorLength; |
| 122 |
} |
| 123 |
|
| 124 |
return logq; |
| 125 |
} |
| 126 |
|
| 127 |
public static dr.xml.XMLObjectParser PARSER = new dr.xml.AbstractXMLObjectParser() { |
| 128 |
|
| 129 |
public String getParserName() { |
| 130 |
return OPERATOR_NAME; |
| 131 |
} |
| 132 |
|
| 133 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 134 |
|
| 135 |
double weight = xo.getDoubleAttribute(WEIGHT); |
| 136 |
Parameter parameter = (Parameter) xo.getChild(Parameter.class); |
| 137 |
RandomGenerator generator = (RandomGenerator) xo.getChild(RandomGenerator.class); |
| 138 |
|
| 139 |
return new GenericIndependentSampler(parameter, generator, weight); |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 147 |
return rules; |
| 148 |
} |
| 149 |
|
| 150 |
private final XMLSyntaxRule[] rules = { |
| 151 |
AttributeRule.newDoubleRule(WEIGHT), |
| 152 |
new ElementRule(RandomGenerator.class), |
| 153 |
new ElementRule(Parameter.class) |
| 154 |
}; |
| 155 |
|
| 156 |
public String getParserDescription() { |
| 157 |
return "This element returns an independence sampler from a provided generic distribution generator."; |
| 158 |
} |
| 159 |
|
| 160 |
public Class getReturnType() { |
| 161 |
return MCMCOperator.class; |
| 162 |
} |
| 163 |
}; |
| 164 |
} |