| 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.*; |
| 29 |
import dr.inference.model.Parameter; |
| 30 |
import dr.math.distributions.GammaDistribution; |
| 31 |
import dr.math.distributions.Distribution; |
| 32 |
import dr.math.MathUtils; |
| 33 |
import dr.xml.*; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public class RegressionGibbsPrecisionOperator extends SimpleMCMCOperator implements GibbsOperator { |
| 40 |
|
| 41 |
public static final String GIBBS_OPERATOR = "regressionGibbsPrecisionOperator"; |
| 42 |
|
| 43 |
private LinearRegression linearModel; |
| 44 |
private Parameter precision; |
| 45 |
private int dim; |
| 46 |
private int N; |
| 47 |
private int[] scaleDesign; |
| 48 |
private Distribution prior; |
| 49 |
|
| 50 |
public RegressionGibbsPrecisionOperator(LinearRegression linearModel, Parameter precision, Distribution prior) { |
| 51 |
|
| 52 |
super(); |
| 53 |
if (!(prior instanceof GammaDistribution || prior instanceof GammaDistributionModel)) |
| 54 |
throw new RuntimeException("Precision prior must be Gamma"); |
| 55 |
this.prior = prior; |
| 56 |
this.linearModel = linearModel; |
| 57 |
this.precision = precision; |
| 58 |
this.dim = precision.getDimension(); |
| 59 |
scaleDesign = linearModel.getScaleDesign(); |
| 60 |
N = linearModel.getDependentVariable().getDimension(); |
| 61 |
} |
| 62 |
|
| 63 |
public int getStepCount() { |
| 64 |
return 1; |
| 65 |
} |
| 66 |
|
| 67 |
public double doOperation() { |
| 68 |
|
| 69 |
double[] Y = linearModel.getTransformedDependentParameter(); |
| 70 |
double[] xBeta = linearModel.getXBeta(); |
| 71 |
|
| 72 |
final double priorMean = prior.mean(); |
| 73 |
final double priorVariance = prior.variance(); |
| 74 |
|
| 75 |
double priorRate; |
| 76 |
double priorShape; |
| 77 |
|
| 78 |
if (priorMean == 0) { |
| 79 |
priorRate = 0; |
| 80 |
priorShape = -0.5; |
| 81 |
} else { |
| 82 |
priorRate = priorMean / priorVariance; |
| 83 |
priorShape = priorMean * priorRate; |
| 84 |
} |
| 85 |
|
| 86 |
for (int k = 0; k < dim; k++) { |
| 87 |
|
| 88 |
|
| 89 |
double SSE = 0; |
| 90 |
int n = 0; |
| 91 |
|
| 92 |
for(int i=0; i<N; i++) { |
| 93 |
if(scaleDesign[i] == k) { |
| 94 |
SSE += (Y[i] - xBeta[i])*(Y[i] - xBeta[i]); |
| 95 |
n++; |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
final double shape = priorShape + n / 2.0; |
| 100 |
final double rate = priorRate + 0.5 * SSE; |
| 101 |
|
| 102 |
final double draw = MathUtils.nextGamma(shape, rate); |
| 103 |
precision.setParameterValue(k, draw); |
| 104 |
} |
| 105 |
|
| 106 |
return 0; |
| 107 |
} |
| 108 |
|
| 109 |
public String getPerformanceSuggestion() { |
| 110 |
return null; |
| 111 |
} |
| 112 |
|
| 113 |
public String getOperatorName() { |
| 114 |
return GIBBS_OPERATOR; |
| 115 |
} |
| 116 |
|
| 117 |
public static XMLObjectParser PARSER = new AbstractXMLObjectParser() { |
| 118 |
|
| 119 |
public String getParserName() { |
| 120 |
return GIBBS_OPERATOR; |
| 121 |
} |
| 122 |
|
| 123 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 124 |
|
| 125 |
double weight = xo.getDoubleAttribute(WEIGHT); |
| 126 |
|
| 127 |
LinearRegression linearModel = (LinearRegression) xo.getChild(LinearRegression.class); |
| 128 |
Parameter precision = (Parameter) xo.getChild(Parameter.class); |
| 129 |
DistributionLikelihood prior = (DistributionLikelihood) xo.getChild(DistributionLikelihood.class); |
| 130 |
|
| 131 |
if (!((prior.getDistribution() instanceof GammaDistribution) || |
| 132 |
(prior.getDistribution() instanceof GammaDistributionModel) |
| 133 |
)) |
| 134 |
throw new XMLParseException("Gibbs operator assumes normal-gamma model"); |
| 135 |
|
| 136 |
RegressionGibbsPrecisionOperator operator = new RegressionGibbsPrecisionOperator(linearModel,precision,prior.getDistribution()); |
| 137 |
operator.setWeight(weight); |
| 138 |
return operator; |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
public String getParserDescription() { |
| 146 |
return "This element returns a multivariate Gibbs operator on an internal node trait."; |
| 147 |
} |
| 148 |
|
| 149 |
public Class getReturnType() { |
| 150 |
return MCMCOperator.class; |
| 151 |
} |
| 152 |
|
| 153 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 154 |
return rules; |
| 155 |
} |
| 156 |
|
| 157 |
private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{ |
| 158 |
AttributeRule.newDoubleRule(WEIGHT), |
| 159 |
new ElementRule(Parameter.class), |
| 160 |
new ElementRule(DistributionLikelihood.class), |
| 161 |
new ElementRule(LinearRegression.class), |
| 162 |
}; |
| 163 |
|
| 164 |
}; |
| 165 |
|
| 166 |
} |