| 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.distribution.DistributionLikelihood; |
| 29 |
import dr.inference.distribution.GammaDistributionModel; |
| 30 |
import dr.inference.distribution.LogNormalDistributionModel; |
| 31 |
import dr.inference.distribution.NormalDistributionModel; |
| 32 |
import dr.inference.model.Parameter; |
| 33 |
import dr.math.MathUtils; |
| 34 |
import dr.math.distributions.Distribution; |
| 35 |
import dr.math.distributions.GammaDistribution; |
| 36 |
import dr.util.Attribute; |
| 37 |
import dr.xml.*; |
| 38 |
|
| 39 |
import java.util.List; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public class NormalGammaPrecisionGibbsOperator extends SimpleMCMCOperator implements GibbsOperator { |
| 46 |
|
| 47 |
public static final String OPERATOR_NAME = "normalGammaPrecisionGibbsOperator"; |
| 48 |
public static final String LIKELIHOOD = "likelihood"; |
| 49 |
public static final String PRIOR = "prior"; |
| 50 |
|
| 51 |
public NormalGammaPrecisionGibbsOperator(DistributionLikelihood inLikelihood, Distribution prior, |
| 52 |
double weight) { |
| 53 |
|
| 54 |
if (!(prior instanceof GammaDistribution || prior instanceof GammaDistributionModel)) |
| 55 |
throw new RuntimeException("Precision prior must be Gamma"); |
| 56 |
|
| 57 |
Distribution likelihood = inLikelihood.getDistribution(); |
| 58 |
this.dataList = inLikelihood.getDataList(); |
| 59 |
|
| 60 |
if (likelihood instanceof NormalDistributionModel) { |
| 61 |
this.precisionParameter = (Parameter) ((NormalDistributionModel) likelihood).getPrecision(); |
| 62 |
this.meanParameter = (Parameter) ((NormalDistributionModel) likelihood).getMean(); |
| 63 |
} else if (likelihood instanceof LogNormalDistributionModel) { |
| 64 |
if (((LogNormalDistributionModel) likelihood).getParameterization() == LogNormalDistributionModel.Parameterization.MU_PRECISION) { |
| 65 |
this.meanParameter = ((LogNormalDistributionModel) likelihood).getMuParameter(); |
| 66 |
} else { |
| 67 |
throw new RuntimeException("Must characterize likelihood in terms of mu and precision parameters"); |
| 68 |
} |
| 69 |
this.precisionParameter = ((LogNormalDistributionModel) likelihood).getPrecisionParameter(); |
| 70 |
isLog = true; |
| 71 |
} else |
| 72 |
throw new RuntimeException("Likelihood must be Normal or log Normal"); |
| 73 |
|
| 74 |
if (precisionParameter == null) |
| 75 |
throw new RuntimeException("Must characterize likelihood in terms of a precision parameter"); |
| 76 |
|
| 77 |
this.prior = prior; |
| 78 |
setWeight(weight); |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
public String getPerformanceSuggestion() { |
| 85 |
return null; |
| 86 |
} |
| 87 |
|
| 88 |
public String getOperatorName() { |
| 89 |
return OPERATOR_NAME; |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
public double doOperation() { |
| 98 |
|
| 99 |
final double priorMean = prior.mean(); |
| 100 |
final double priorVariance = prior.variance(); |
| 101 |
|
| 102 |
double priorRate; |
| 103 |
double priorShape; |
| 104 |
|
| 105 |
if (priorMean == 0) { |
| 106 |
priorRate = 0; |
| 107 |
priorShape = -0.5; |
| 108 |
} else { |
| 109 |
priorRate = priorMean / priorVariance; |
| 110 |
priorShape = priorMean * priorRate; |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
final double mu = meanParameter.getParameterValue(0); |
| 115 |
double SSE = 0; |
| 116 |
int n = 0; |
| 117 |
for (Attribute<double[]> statistic : dataList) { |
| 118 |
for (double x : statistic.getAttributeValue()) { |
| 119 |
if (isLog) { |
| 120 |
final double logX = Math.log(x); |
| 121 |
SSE += (logX - mu) * (logX - mu); |
| 122 |
} else { |
| 123 |
SSE += (x - mu) * (x - mu); |
| 124 |
} |
| 125 |
n++; |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
final double shape = priorShape + n / 2.0; |
| 130 |
final double rate = priorRate + 0.5 * SSE; |
| 131 |
|
| 132 |
final double draw = MathUtils.nextGamma(shape, rate); |
| 133 |
precisionParameter.setParameterValue(0, draw); |
| 134 |
|
| 135 |
return 0; |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
public int getStepCount() { |
| 142 |
return 1; |
| 143 |
} |
| 144 |
|
| 145 |
public static dr.xml.XMLObjectParser PARSER = new dr.xml.AbstractXMLObjectParser() { |
| 146 |
|
| 147 |
public String getParserName() { |
| 148 |
return OPERATOR_NAME; |
| 149 |
} |
| 150 |
|
| 151 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 152 |
|
| 153 |
double weight = xo.getDoubleAttribute(WEIGHT); |
| 154 |
|
| 155 |
DistributionLikelihood likelihood = (DistributionLikelihood) ((XMLObject) xo.getChild(LIKELIHOOD)).getChild(DistributionLikelihood.class); |
| 156 |
DistributionLikelihood prior = (DistributionLikelihood) ((XMLObject) xo.getChild(PRIOR)).getChild(DistributionLikelihood.class); |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
if (!((prior.getDistribution() instanceof GammaDistribution) || |
| 161 |
(prior.getDistribution() instanceof GammaDistributionModel) |
| 162 |
) || |
| 163 |
!((likelihood.getDistribution() instanceof NormalDistributionModel) || |
| 164 |
(likelihood.getDistribution() instanceof LogNormalDistributionModel) |
| 165 |
)) |
| 166 |
throw new XMLParseException("Gibbs operator assumes normal-gamma model"); |
| 167 |
|
| 168 |
return new NormalGammaPrecisionGibbsOperator(likelihood, prior.getDistribution(), weight); |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
public String getParserDescription() { |
| 176 |
return "This element returns a operator on the precision parameter of a normal model with gamma prior."; |
| 177 |
} |
| 178 |
|
| 179 |
public Class getReturnType() { |
| 180 |
return MCMCOperator.class; |
| 181 |
} |
| 182 |
|
| 183 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 184 |
return rules; |
| 185 |
} |
| 186 |
|
| 187 |
private final XMLSyntaxRule[] rules = { |
| 188 |
AttributeRule.newDoubleRule(WEIGHT), |
| 189 |
new ElementRule(LIKELIHOOD, |
| 190 |
new XMLSyntaxRule[]{ |
| 191 |
new ElementRule(DistributionLikelihood.class) |
| 192 |
}), |
| 193 |
new ElementRule(PRIOR, |
| 194 |
new XMLSyntaxRule[]{ |
| 195 |
new ElementRule(DistributionLikelihood.class) |
| 196 |
}), |
| 197 |
}; |
| 198 |
|
| 199 |
}; |
| 200 |
|
| 201 |
private final Distribution prior; |
| 202 |
private boolean isLog = false; |
| 203 |
|
| 204 |
private final List<Attribute<double[]>> dataList; |
| 205 |
private final Parameter meanParameter; |
| 206 |
private final Parameter precisionParameter; |
| 207 |
} |