| 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.GammaDistribution; |
| 32 |
import dr.xml.AttributeRule; |
| 33 |
import dr.xml.ElementRule; |
| 34 |
import dr.xml.XMLObject; |
| 35 |
import dr.xml.XMLParseException; |
| 36 |
import dr.xml.XMLSyntaxRule; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public class GibbsIndependentGammaOperator extends SimpleMCMCOperator implements GibbsOperator { |
| 45 |
|
| 46 |
public static final String OPERATOR_NAME = "GibbsIndependentGammaOperator"; |
| 47 |
public static final String SHAPE = "shape"; |
| 48 |
public static final String SCALE = "scale"; |
| 49 |
|
| 50 |
private Variable<Double> variable = null; |
| 51 |
private GammaDistribution gamma = null; |
| 52 |
private boolean updateAllIndependently = true; |
| 53 |
|
| 54 |
public GibbsIndependentGammaOperator(Variable variable, GammaDistribution gamma) { |
| 55 |
|
| 56 |
this(variable, gamma, 1.0); |
| 57 |
|
| 58 |
} |
| 59 |
|
| 60 |
public GibbsIndependentGammaOperator(Variable variable, GammaDistribution gamma, double weight) { |
| 61 |
|
| 62 |
this(variable, gamma, weight, true); |
| 63 |
|
| 64 |
} |
| 65 |
|
| 66 |
public GibbsIndependentGammaOperator(Variable variable, GammaDistribution gamma, double weight, boolean updateAllIndependently) { |
| 67 |
|
| 68 |
this.variable = variable; |
| 69 |
this.gamma = gamma; |
| 70 |
this.updateAllIndependently = updateAllIndependently; |
| 71 |
setWeight(weight); |
| 72 |
|
| 73 |
} |
| 74 |
|
| 75 |
public String getPerformanceSuggestion() { |
| 76 |
return ""; |
| 77 |
} |
| 78 |
|
| 79 |
public String getOperatorName() { |
| 80 |
return "GibbsIndependentGamma(" + variable.getVariableName() + ")"; |
| 81 |
} |
| 82 |
|
| 83 |
public int getStepCount() { |
| 84 |
return 1; |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
public double doOperation() { |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
double newValue; |
| 96 |
|
| 97 |
final Bounds<Double> bounds = variable.getBounds(); |
| 98 |
final int dim = variable.getSize(); |
| 99 |
|
| 100 |
if (updateAllIndependently) { |
| 101 |
for (int i = 0; i < dim; i++) { |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
newValue = gamma.nextGamma(); |
| 107 |
while (newValue == 0.0) { |
| 108 |
newValue = gamma.nextGamma(); |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
if (newValue < bounds.getLowerLimit(i) || newValue > bounds.getUpperLimit(i)) { |
| 116 |
throw new RuntimeException("proposed value outside boundaries"); |
| 117 |
} |
| 118 |
|
| 119 |
variable.setValue(i, newValue); |
| 120 |
|
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
return 0; |
| 126 |
} |
| 127 |
|
| 128 |
public static dr.xml.XMLObjectParser PARSER = new dr.xml.AbstractXMLObjectParser() { |
| 129 |
|
| 130 |
public String getParserName() { |
| 131 |
return OPERATOR_NAME; |
| 132 |
} |
| 133 |
|
| 134 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 135 |
|
| 136 |
double weight = xo.getDoubleAttribute(WEIGHT); |
| 137 |
double shape = xo.getDoubleAttribute(SHAPE); |
| 138 |
double scale = xo.getDoubleAttribute(SCALE); |
| 139 |
|
| 140 |
if (! (shape > 0 && scale > 0)) { |
| 141 |
throw new XMLParseException("Shape and scale must be positive values."); |
| 142 |
} |
| 143 |
|
| 144 |
Parameter parameter = (Parameter) xo.getChild(Parameter.class); |
| 145 |
|
| 146 |
return new GibbsIndependentGammaOperator(parameter, new GammaDistribution(shape, scale), weight); |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 154 |
return rules; |
| 155 |
} |
| 156 |
|
| 157 |
private final XMLSyntaxRule[] rules = { |
| 158 |
AttributeRule.newDoubleRule(WEIGHT), |
| 159 |
AttributeRule.newDoubleRule(SHAPE), |
| 160 |
AttributeRule.newDoubleRule(SCALE), |
| 161 |
new ElementRule(Parameter.class) |
| 162 |
}; |
| 163 |
|
| 164 |
public String getParserDescription() { |
| 165 |
return "This element returns an independence sampler, disguised as a Gibbs operator, from a provided gamma prior."; |
| 166 |
} |
| 167 |
|
| 168 |
public Class getReturnType() { |
| 169 |
return MCMCOperator.class; |
| 170 |
} |
| 171 |
|
| 172 |
}; |
| 173 |
|
| 174 |
} |