| 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.LinearRegression; |
| 29 |
import dr.inference.distribution.MultivariateDistributionLikelihood; |
| 30 |
import dr.inference.model.Parameter; |
| 31 |
import dr.inferencexml.distribution.GeneralizedLinearModelParser; |
| 32 |
import dr.math.distributions.MultivariateDistribution; |
| 33 |
import dr.math.distributions.MultivariateNormalDistribution; |
| 34 |
import dr.math.matrixAlgebra.SymmetricMatrix; |
| 35 |
import dr.xml.*; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public class RegressionGibbsEffectOperator extends SimpleMCMCOperator implements GibbsOperator { |
| 42 |
|
| 43 |
public static final String GIBBS_OPERATOR = "regressionGibbsEffectOperator"; |
| 44 |
|
| 45 |
private LinearRegression linearModel; |
| 46 |
private Parameter effect; |
| 47 |
private Parameter indicators; |
| 48 |
private boolean hasNoIndicators = true; |
| 49 |
private MultivariateDistribution effectPrior; |
| 50 |
private int dim; |
| 51 |
private int effectNumber; |
| 52 |
private int N; |
| 53 |
private int numEffects; |
| 54 |
private double[][] X; |
| 55 |
|
| 56 |
private double[] mean = null; |
| 57 |
private double[][] variance = null; |
| 58 |
private double[][] precision = null; |
| 59 |
|
| 60 |
public RegressionGibbsEffectOperator(LinearRegression linearModel, Parameter effect, Parameter indicators, |
| 61 |
MultivariateDistributionLikelihood effectPrior) { |
| 62 |
super(); |
| 63 |
this.linearModel = linearModel; |
| 64 |
this.effect = effect; |
| 65 |
this.indicators = indicators; |
| 66 |
if (indicators != null) { |
| 67 |
hasNoIndicators = false; |
| 68 |
if (indicators.getDimension() != effect.getDimension()) |
| 69 |
throw new RuntimeException("Indicator and effect dimensions must match"); |
| 70 |
} |
| 71 |
effectNumber = linearModel.getEffectNumber(effect); |
| 72 |
this.effectPrior = effectPrior.getDistribution(); |
| 73 |
dim = effect.getDimension(); |
| 74 |
N = linearModel.getDependentVariable().getDimension(); |
| 75 |
numEffects = linearModel.getNumberOfFixedEffects(); |
| 76 |
X = linearModel.getX(effectNumber); |
| 77 |
} |
| 78 |
|
| 79 |
public int getStepCount() { |
| 80 |
return 1; |
| 81 |
} |
| 82 |
|
| 83 |
public void computeForwardDensity(double[] outMean, double[][] outVariance, double[][] outPrecision) { |
| 84 |
|
| 85 |
double[] W = linearModel.getTransformedDependentParameter(); |
| 86 |
double[] P = linearModel.getScale(); |
| 87 |
|
| 88 |
for (int k = 0; k < numEffects; k++) { |
| 89 |
if (k != effectNumber) { |
| 90 |
double[] thisXBeta = linearModel.getXBeta(k); |
| 91 |
for (int i = 0; i < N; i++) |
| 92 |
W[i] -= thisXBeta[i]; |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
double[] priorBetaMean = effectPrior.getMean(); |
| 97 |
double[][] priorBetaScale = effectPrior.getScaleMatrix(); |
| 98 |
|
| 99 |
double[][] XtP = new double[dim][N]; |
| 100 |
for (int j = 0; j < dim; j++) { |
| 101 |
if (hasNoIndicators || indicators.getParameterValue(j) == 1) { |
| 102 |
for (int i = 0; i < N; i++) |
| 103 |
XtP[j][i] = X[i][j] * P[i]; |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
double[][] XtPX = new double[dim][dim]; |
| 108 |
for (int i = 0; i < dim; i++) { |
| 109 |
if (hasNoIndicators || indicators.getParameterValue(i) == 1) { |
| 110 |
for (int j = i; j < dim; j++) { |
| 111 |
if (hasNoIndicators || indicators.getParameterValue(j) == 1) { |
| 112 |
for (int k = 0; k < N; k++) |
| 113 |
XtPX[i][j] += XtP[i][k] * X[k][j]; |
| 114 |
XtPX[j][i] = XtPX[i][j]; |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
double[][] XtPX_plus_P0 = new double[dim][dim]; |
| 121 |
for (int i = 0; i < dim; i++) { |
| 122 |
for (int j = i; j < dim; j++) |
| 123 |
XtPX_plus_P0[j][i] = XtPX_plus_P0[i][j] = XtPX[i][j] + priorBetaScale[i][j]; |
| 124 |
} |
| 125 |
|
| 126 |
double[] XtPW = new double[dim]; |
| 127 |
for (int i = 0; i < dim; i++) { |
| 128 |
for (int j = 0; j < N; j++) |
| 129 |
XtPW[i] += XtP[i][j] * W[j]; |
| 130 |
} |
| 131 |
|
| 132 |
double[] P0Mean0 = new double[dim]; |
| 133 |
for (int i = 0; i < dim; i++) { |
| 134 |
for (int j = 0; j < dim; j++) |
| 135 |
P0Mean0[i] += priorBetaScale[i][j] * priorBetaMean[j]; |
| 136 |
} |
| 137 |
|
| 138 |
double[] unscaledMean = new double[dim]; |
| 139 |
for (int i = 0; i < dim; i++) |
| 140 |
unscaledMean[i] = P0Mean0[i] + XtPW[i]; |
| 141 |
|
| 142 |
double[][] variance = new SymmetricMatrix(XtPX_plus_P0).inverse().toComponents(); |
| 143 |
|
| 144 |
for (int i = 0; i < dim; i++) { |
| 145 |
outMean[i] = 0.0; |
| 146 |
for (int j = 0; j < dim; j++) { |
| 147 |
outMean[i] += variance[i][j] * unscaledMean[j]; |
| 148 |
outVariance[i][j] = variance[i][j]; |
| 149 |
outPrecision[i][j] = XtPX_plus_P0[i][j]; |
| 150 |
} |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
public double[] getLastMean() { return mean; } |
| 155 |
|
| 156 |
public double[][] getLastVariance() { return variance; } |
| 157 |
|
| 158 |
public double[][] getLastPrecision() { return precision; } |
| 159 |
|
| 160 |
public double doOperation() { |
| 161 |
|
| 162 |
if (mean == null) |
| 163 |
mean = new double[dim]; |
| 164 |
|
| 165 |
if (variance == null) |
| 166 |
variance = new double[dim][dim]; |
| 167 |
|
| 168 |
if (precision == null) |
| 169 |
precision = new double[dim][dim]; |
| 170 |
|
| 171 |
|
| 172 |
computeForwardDensity(mean,variance,precision); |
| 173 |
|
| 174 |
double[] draw = MultivariateNormalDistribution.nextMultivariateNormalVariance( |
| 175 |
mean, variance); |
| 176 |
|
| 177 |
for (int i = 0; i < dim; i++) |
| 178 |
effect.setParameterValue(i, draw[i]); |
| 179 |
|
| 180 |
return 0; |
| 181 |
} |
| 182 |
|
| 183 |
public String getPerformanceSuggestion() { |
| 184 |
return null; |
| 185 |
} |
| 186 |
|
| 187 |
public String getOperatorName() { |
| 188 |
return GIBBS_OPERATOR; |
| 189 |
} |
| 190 |
|
| 191 |
public static dr.xml.XMLObjectParser PARSER = new dr.xml.AbstractXMLObjectParser() { |
| 192 |
|
| 193 |
public String getParserName() { |
| 194 |
return GIBBS_OPERATOR; |
| 195 |
} |
| 196 |
|
| 197 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 198 |
|
| 199 |
double weight = xo.getDoubleAttribute(WEIGHT); |
| 200 |
|
| 201 |
LinearRegression linearModel = (LinearRegression) xo.getChild(LinearRegression.class); |
| 202 |
Parameter effect = (Parameter) xo.getChild(Parameter.class); |
| 203 |
MultivariateDistributionLikelihood prior = (MultivariateDistributionLikelihood) xo.getChild(MultivariateDistributionLikelihood.class); |
| 204 |
|
| 205 |
if (prior.getDistribution().getType().compareTo(MultivariateNormalDistribution.TYPE) != 0) |
| 206 |
throw new XMLParseException("Only a multivariate normal prior is conjugate"); |
| 207 |
|
| 208 |
XMLObject cxo = xo.getChild(GeneralizedLinearModelParser.INDICATOR); |
| 209 |
Parameter indicators = null; |
| 210 |
if (cxo != null) { |
| 211 |
indicators = (Parameter) cxo.getChild(Parameter.class); |
| 212 |
} |
| 213 |
|
| 214 |
RegressionGibbsEffectOperator operator = new RegressionGibbsEffectOperator(linearModel, effect, indicators, prior); |
| 215 |
operator.setWeight(weight); |
| 216 |
return operator; |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
public String getParserDescription() { |
| 224 |
return "This element returns a multivariate Gibbs operator on an internal node trait."; |
| 225 |
} |
| 226 |
|
| 227 |
public Class getReturnType() { |
| 228 |
return MCMCOperator.class; |
| 229 |
} |
| 230 |
|
| 231 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 232 |
return rules; |
| 233 |
} |
| 234 |
|
| 235 |
private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{ |
| 236 |
AttributeRule.newDoubleRule(WEIGHT), |
| 237 |
new ElementRule(Parameter.class), |
| 238 |
new ElementRule(MultivariateDistributionLikelihood.class), |
| 239 |
new ElementRule(LinearRegression.class), |
| 240 |
new ElementRule(GeneralizedLinearModelParser.INDICATOR, |
| 241 |
new XMLSyntaxRule[] { |
| 242 |
new ElementRule(Parameter.class) |
| 243 |
},true) |
| 244 |
}; |
| 245 |
|
| 246 |
}; |
| 247 |
|
| 248 |
} |