| 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.xml.AttributeRule; |
| 37 |
import dr.xml.ElementRule; |
| 38 |
import dr.xml.XMLObject; |
| 39 |
import dr.xml.XMLParseException; |
| 40 |
import dr.xml.XMLSyntaxRule; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public class IndependentNormalDistributionSampler extends SimpleMCMCOperator { |
| 49 |
|
| 50 |
public static final String OPERATOR_NAME = "independentNormalDistributionSampler"; |
| 51 |
|
| 52 |
private Variable<Double> variable = null; |
| 53 |
private NormalDistributionModel model = null; |
| 54 |
private boolean updateAllIndependently = true; |
| 55 |
|
| 56 |
private static final boolean TRY_COLT = true; |
| 57 |
private static RandomEngine randomEngine; |
| 58 |
private static Normal coltNormal; |
| 59 |
|
| 60 |
public IndependentNormalDistributionSampler(Variable variable, NormalDistributionModel model) { |
| 61 |
|
| 62 |
this(variable, model, 1.0); |
| 63 |
|
| 64 |
} |
| 65 |
|
| 66 |
public IndependentNormalDistributionSampler(Variable variable, NormalDistributionModel model, double weight) { |
| 67 |
|
| 68 |
this(variable, model, weight, true); |
| 69 |
|
| 70 |
} |
| 71 |
|
| 72 |
public IndependentNormalDistributionSampler(Variable variable, NormalDistributionModel model, double weight, boolean updateAllIndependently) { |
| 73 |
|
| 74 |
this.variable = variable; |
| 75 |
this.model = model; |
| 76 |
this.updateAllIndependently = updateAllIndependently; |
| 77 |
setWeight(weight); |
| 78 |
|
| 79 |
if (TRY_COLT) { |
| 80 |
randomEngine = new MersenneTwister(MathUtils.nextInt()); |
| 81 |
|
| 82 |
coltNormal = new Normal(0.0, 1.0, randomEngine); |
| 83 |
} else { |
| 84 |
|
| 85 |
throw new RuntimeException("Normal distribution in BEAST still needs a random sampler."); |
| 86 |
} |
| 87 |
|
| 88 |
} |
| 89 |
|
| 90 |
public String getPerformanceSuggestion() { |
| 91 |
return ""; |
| 92 |
} |
| 93 |
|
| 94 |
public String getOperatorName() { |
| 95 |
return "independentNormalDistribution(" + variable.getVariableName() + ")"; |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
public double doOperation() { |
| 102 |
|
| 103 |
double logq = 0; |
| 104 |
|
| 105 |
double currentValue; |
| 106 |
double newValue; |
| 107 |
|
| 108 |
final Bounds<Double> bounds = variable.getBounds(); |
| 109 |
final int dim = variable.getSize(); |
| 110 |
|
| 111 |
if (updateAllIndependently) { |
| 112 |
for (int i = 0; i < dim; i++) { |
| 113 |
|
| 114 |
|
| 115 |
currentValue = variable.getValue(i); |
| 116 |
|
| 117 |
|
| 118 |
newValue = coltNormal.nextDouble(model.getMean().getValue(i), 1.0 / Math.sqrt(model.getPrecision().getValue(i))); |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
logq += (model.logPdf(currentValue) - model.logPdf(newValue)); |
| 124 |
|
| 125 |
if (newValue < bounds.getLowerLimit(i) || newValue > bounds.getUpperLimit(i)) { |
| 126 |
|
| 127 |
return Double.NEGATIVE_INFINITY; |
| 128 |
} |
| 129 |
|
| 130 |
variable.setValue(i, newValue); |
| 131 |
|
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
return logq; |
| 136 |
} |
| 137 |
|
| 138 |
public static dr.xml.XMLObjectParser PARSER = new dr.xml.AbstractXMLObjectParser() { |
| 139 |
|
| 140 |
public String getParserName() { |
| 141 |
return OPERATOR_NAME; |
| 142 |
} |
| 143 |
|
| 144 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 145 |
|
| 146 |
double weight = xo.getDoubleAttribute(WEIGHT); |
| 147 |
|
| 148 |
NormalDistributionModel model = (NormalDistributionModel) xo.getChild(NormalDistributionModel.class); |
| 149 |
Parameter parameter = (Parameter) xo.getChild(Parameter.class); |
| 150 |
|
| 151 |
return new IndependentNormalDistributionSampler(parameter, model, weight); |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 159 |
return rules; |
| 160 |
} |
| 161 |
|
| 162 |
private final XMLSyntaxRule[] rules = { |
| 163 |
AttributeRule.newDoubleRule(WEIGHT), |
| 164 |
new ElementRule(NormalDistributionModel.class), |
| 165 |
new ElementRule(Parameter.class) |
| 166 |
}; |
| 167 |
|
| 168 |
public String getParserDescription() { |
| 169 |
return "This element returns an independence sampler from a provided normal distribution model."; |
| 170 |
} |
| 171 |
|
| 172 |
public Class getReturnType() { |
| 173 |
return MCMCOperator.class; |
| 174 |
} |
| 175 |
|
| 176 |
}; |
| 177 |
|
| 178 |
} |