| 1 |
package dr.inference.operators; |
| 2 |
|
| 3 |
import dr.inference.distribution.*; |
| 4 |
import dr.inference.model.Parameter; |
| 5 |
import dr.inference.model.TransformedParameter; |
| 6 |
import dr.math.MathUtils; |
| 7 |
import dr.math.distributions.Distribution; |
| 8 |
import dr.math.distributions.GammaDistribution; |
| 9 |
import dr.util.Attribute; |
| 10 |
import dr.util.Transform; |
| 11 |
import dr.xml.*; |
| 12 |
|
| 13 |
import java.util.Arrays; |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
public class RandomWalkGammaPrecisionGibbsOperator extends SimpleMCMCOperator implements GibbsOperator { |
| 19 |
public static final String OPERATOR_NAME = "randomWalkGammaPrecisionGibbsOperator"; |
| 20 |
public static final String DATA = "data"; |
| 21 |
public static final String PRECISION = "precision"; |
| 22 |
public static final String PRIOR = "prior"; |
| 23 |
|
| 24 |
public RandomWalkGammaPrecisionGibbsOperator(Parameter data, Parameter precision, Distribution prior, double weight) { |
| 25 |
if (!(prior instanceof GammaDistribution || prior instanceof GammaDistributionModel)) |
| 26 |
throw new RuntimeException("Precision prior must be Gamma"); |
| 27 |
|
| 28 |
this.precision = precision; |
| 29 |
this.data = data; |
| 30 |
this.prior = prior; |
| 31 |
|
| 32 |
setWeight(weight); |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public String getPerformanceSuggestion() { |
| 39 |
return null; |
| 40 |
} |
| 41 |
|
| 42 |
public String getOperatorName() { |
| 43 |
return OPERATOR_NAME; |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public double doOperation() { |
| 52 |
|
| 53 |
final double priorMean = prior.mean(); |
| 54 |
final double priorVariance = prior.variance(); |
| 55 |
|
| 56 |
double priorRate; |
| 57 |
double priorShape; |
| 58 |
|
| 59 |
if (priorMean == 0) { |
| 60 |
priorRate = 0; |
| 61 |
priorShape = -0.5; |
| 62 |
} else { |
| 63 |
priorRate = priorMean / priorVariance; |
| 64 |
priorShape = priorMean * priorRate; |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
double SSE = 0; |
| 70 |
double x; |
| 71 |
int n = data.getDimension(); |
| 72 |
for (int i = 1; i < n; i++) { |
| 73 |
x = data.getParameterValue(i) - data.getParameterValue(i-1); |
| 74 |
SSE += x * x; |
| 75 |
} |
| 76 |
|
| 77 |
final double shape = priorShape + n / 2.0; |
| 78 |
final double rate = priorRate + 0.5 * SSE; |
| 79 |
|
| 80 |
final double draw = MathUtils.nextGamma(shape, rate); |
| 81 |
precision.setParameterValue(0, draw); |
| 82 |
|
| 83 |
return 0; |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
public int getStepCount() { |
| 90 |
return 1; |
| 91 |
} |
| 92 |
|
| 93 |
public static dr.xml.XMLObjectParser PARSER = new dr.xml.AbstractXMLObjectParser() { |
| 94 |
|
| 95 |
public String getParserName() { |
| 96 |
return OPERATOR_NAME; |
| 97 |
} |
| 98 |
|
| 99 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 100 |
|
| 101 |
double weight = xo.getDoubleAttribute(WEIGHT); |
| 102 |
|
| 103 |
Parameter data = (Parameter) ((XMLObject) xo.getChild(DATA)).getChild(Parameter.class); |
| 104 |
Parameter precision = (Parameter) ((XMLObject) xo.getChild(PRECISION)).getChild(Parameter.class); |
| 105 |
GammaDistributionModel prior = (GammaDistributionModel) ((XMLObject) xo.getChild(PRIOR)).getChild(GammaDistributionModel.class); |
| 106 |
|
| 107 |
return new RandomWalkGammaPrecisionGibbsOperator(data, precision, prior, weight); |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
public String getParserDescription() { |
| 115 |
return "This element returns an operator on the precision parameter of a normal random walk model with gamma prior."; |
| 116 |
} |
| 117 |
|
| 118 |
public Class getReturnType() { |
| 119 |
return MCMCOperator.class; |
| 120 |
} |
| 121 |
|
| 122 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 123 |
return rules; |
| 124 |
} |
| 125 |
|
| 126 |
private final XMLSyntaxRule[] rules = { |
| 127 |
AttributeRule.newDoubleRule(WEIGHT), |
| 128 |
new ElementRule(DATA, |
| 129 |
new XMLSyntaxRule[]{ |
| 130 |
new ElementRule(Parameter.class) |
| 131 |
}), |
| 132 |
new ElementRule(PRECISION, |
| 133 |
new XMLSyntaxRule[]{ |
| 134 |
new ElementRule(Parameter.class) |
| 135 |
}), |
| 136 |
new ElementRule(PRIOR, |
| 137 |
new XMLSyntaxRule[]{ |
| 138 |
new ElementRule(GammaDistributionModel.class) |
| 139 |
}), |
| 140 |
}; |
| 141 |
|
| 142 |
}; |
| 143 |
|
| 144 |
public static void main(String[] args) { |
| 145 |
Parameter logPop = new Parameter.Default(new double[]{1.0, 2.0, 3.0, 4.0, 5.0}); |
| 146 |
Transform tr = new Transform.LogTransform(); |
| 147 |
Parameter effPop = new TransformedParameter(logPop, tr, true); |
| 148 |
|
| 149 |
|
| 150 |
} |
| 151 |
|
| 152 |
private final Distribution prior; |
| 153 |
private final Parameter data; |
| 154 |
private final Parameter precision; |
| 155 |
} |