| 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 cern.jet.random.Normal; |
| 29 |
import cern.jet.random.engine.MersenneTwister; |
| 30 |
import cern.jet.random.engine.RandomEngine; |
| 31 |
import dr.inference.distribution.NormalDistributionModel; |
| 32 |
import dr.inference.model.Bounds; |
| 33 |
import dr.inference.model.Parameter; |
| 34 |
import dr.inference.model.Variable; |
| 35 |
import dr.math.MathUtils; |
| 36 |
import dr.math.distributions.GammaDistribution; |
| 37 |
import dr.xml.*; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public class GibbsIndependentJointNormalGammaOperator extends SimpleMCMCOperator implements GibbsOperator { |
| 46 |
|
| 47 |
public static final String OPERATOR_NAME = "GibbsIndependentJointNormalGammaOperator"; |
| 48 |
public static final String MEAN = "mean"; |
| 49 |
public static final String PREC = "precision"; |
| 50 |
public static final String SHAPE = "shape"; |
| 51 |
public static final String SCALE = "scale"; |
| 52 |
|
| 53 |
private Variable<Double> mean = null; |
| 54 |
private Variable<Double> precision = null; |
| 55 |
private NormalDistributionModel model = null; |
| 56 |
private GammaDistribution gamma = null; |
| 57 |
private boolean updateAllIndependently = true; |
| 58 |
|
| 59 |
private static final boolean TRY_COLT = true; |
| 60 |
private static final boolean DEBUG = false; |
| 61 |
private static RandomEngine randomEngine; |
| 62 |
private static Normal coltNormal; |
| 63 |
|
| 64 |
|
| 65 |
public GibbsIndependentJointNormalGammaOperator(Variable mean, Variable precision, NormalDistributionModel model, GammaDistribution gamma) { |
| 66 |
|
| 67 |
this(mean, precision, model, gamma, 1.0); |
| 68 |
|
| 69 |
} |
| 70 |
|
| 71 |
public GibbsIndependentJointNormalGammaOperator(Variable mean, Variable precision, NormalDistributionModel model, GammaDistribution gamma, double weight) { |
| 72 |
|
| 73 |
this(mean, precision, model, gamma, weight, true); |
| 74 |
|
| 75 |
} |
| 76 |
|
| 77 |
public GibbsIndependentJointNormalGammaOperator(Variable mean, Variable precision, NormalDistributionModel model, GammaDistribution gamma, double weight, boolean updateAllIndependently) { |
| 78 |
|
| 79 |
this.mean = mean; |
| 80 |
this.precision = precision; |
| 81 |
this.model = model; |
| 82 |
this.gamma = gamma; |
| 83 |
this.updateAllIndependently = updateAllIndependently; |
| 84 |
setWeight(weight); |
| 85 |
|
| 86 |
if (TRY_COLT) { |
| 87 |
randomEngine = new MersenneTwister(MathUtils.nextInt()); |
| 88 |
|
| 89 |
|
| 90 |
coltNormal = new Normal(0.0, 1.0, randomEngine); |
| 91 |
|
| 92 |
} else { |
| 93 |
|
| 94 |
throw new RuntimeException("Normal distribution in BEAST still needs a random sampler."); |
| 95 |
} |
| 96 |
|
| 97 |
} |
| 98 |
|
| 99 |
public String getPerformanceSuggestion() { |
| 100 |
return ""; |
| 101 |
} |
| 102 |
|
| 103 |
public String getOperatorName() { |
| 104 |
return "GibbsIndependentJointNormalGamma(" + mean.getVariableName() + "," + precision.getVariableName() + ")"; |
| 105 |
} |
| 106 |
|
| 107 |
public int getStepCount() { |
| 108 |
return 1; |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
public double doOperation() { |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
double newValue; |
| 120 |
|
| 121 |
final Bounds<Double> meanBounds = mean.getBounds(); |
| 122 |
final Bounds<Double> precBounds = precision.getBounds(); |
| 123 |
final int dim = mean.getSize(); |
| 124 |
|
| 125 |
if (updateAllIndependently) { |
| 126 |
for (int i = 0; i < dim; i++) { |
| 127 |
|
| 128 |
if (DEBUG) { |
| 129 |
System.out.println("old precision value: " + precision.getValue(i)); |
| 130 |
System.out.println("old mean value: " + mean.getValue(i)); |
| 131 |
System.out.println("model mean check: " + model.getMean().getValue(i)); |
| 132 |
System.out.println("model precision check: " + model.getPrecision().getValue(i) + "\n"); |
| 133 |
} |
| 134 |
|
| 135 |
newValue = gamma.nextGamma(); |
| 136 |
|
| 137 |
while (newValue == 0.0) { |
| 138 |
newValue = gamma.nextGamma(); |
| 139 |
|
| 140 |
} |
| 141 |
|
| 142 |
if (newValue < precBounds.getLowerLimit(i) || newValue > precBounds.getUpperLimit(i)) { |
| 143 |
throw new RuntimeException("proposed value from gamma distribution outside boundaries"); |
| 144 |
} |
| 145 |
|
| 146 |
precision.setValue(i, newValue); |
| 147 |
|
| 148 |
if (DEBUG) { |
| 149 |
System.out.println("new precision value: " + precision.getValue(i)); |
| 150 |
System.out.println("old mean value: " + mean.getValue(i)); |
| 151 |
System.out.println("model mean check: " + model.getMean().getValue(i)); |
| 152 |
System.out.println("model precision check: " + model.getPrecision().getValue(i) + "\n"); |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
newValue = coltNormal.nextDouble(model.getMean().getValue(i), 1.0 / Math.sqrt(model.getPrecision().getValue(i))); |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
if (newValue < meanBounds.getLowerLimit(i) || newValue > meanBounds.getUpperLimit(i)) { |
| 167 |
throw new RuntimeException("proposed value from normal distribution outside boundaries"); |
| 168 |
} |
| 169 |
|
| 170 |
mean.setValue(i, newValue); |
| 171 |
|
| 172 |
if (DEBUG) { |
| 173 |
System.out.println("new precision value: " + precision.getValue(i)); |
| 174 |
System.out.println("new mean value: " + mean.getValue(i)); |
| 175 |
System.out.println("model mean check: " + model.getMean().getValue(i)); |
| 176 |
System.out.println("model precision check: " + model.getPrecision().getValue(i) + "\n"); |
| 177 |
} |
| 178 |
|
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
return 0; |
| 184 |
|
| 185 |
} |
| 186 |
|
| 187 |
public static dr.xml.XMLObjectParser PARSER = new dr.xml.AbstractXMLObjectParser() { |
| 188 |
|
| 189 |
public String getParserName() { |
| 190 |
return OPERATOR_NAME; |
| 191 |
} |
| 192 |
|
| 193 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 194 |
|
| 195 |
double weight = xo.getDoubleAttribute(WEIGHT); |
| 196 |
double shape = xo.getDoubleAttribute(SHAPE); |
| 197 |
double scale = xo.getDoubleAttribute(SCALE); |
| 198 |
|
| 199 |
NormalDistributionModel model = (NormalDistributionModel) xo.getChild(NormalDistributionModel.class); |
| 200 |
|
| 201 |
XMLObject cxo = xo.getChild(MEAN); |
| 202 |
Parameter mean = (Parameter) cxo.getChild(Parameter.class); |
| 203 |
|
| 204 |
cxo = xo.getChild(PREC); |
| 205 |
Parameter precision = (Parameter) cxo.getChild(Parameter.class); |
| 206 |
|
| 207 |
return new GibbsIndependentJointNormalGammaOperator(mean, precision, model, new GammaDistribution(shape, scale), weight); |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 215 |
return rules; |
| 216 |
} |
| 217 |
|
| 218 |
private final XMLSyntaxRule[] rules = { |
| 219 |
AttributeRule.newDoubleRule(WEIGHT), |
| 220 |
AttributeRule.newDoubleRule(SHAPE), |
| 221 |
AttributeRule.newDoubleRule(SCALE), |
| 222 |
new ElementRule(NormalDistributionModel.class), |
| 223 |
new ElementRule(MEAN, new XMLSyntaxRule[]{new ElementRule(Parameter.class)}), |
| 224 |
new ElementRule(PREC, new XMLSyntaxRule[]{new ElementRule(Parameter.class)}) |
| 225 |
}; |
| 226 |
|
| 227 |
public String getParserDescription() { |
| 228 |
return "This element returns an independence sampler, disguised as a Gibbs operator, from provided normal and gamma distributions."; |
| 229 |
} |
| 230 |
|
| 231 |
public Class getReturnType() { |
| 232 |
return MCMCOperator.class; |
| 233 |
} |
| 234 |
|
| 235 |
}; |
| 236 |
|
| 237 |
} |