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