| 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.inferencexml.model.MaskedParameterParser; |
| 33 |
import dr.math.MathUtils; |
| 34 |
import dr.math.distributions.MultivariateNormalDistribution; |
| 35 |
import dr.xml.*; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public class RegressionMetropolizedIndicatorOperator extends SimpleMCMCOperator { |
| 41 |
|
| 42 |
public static final String MH_OPERATOR = "regressionMetropolizedIndicatorOperator"; |
| 43 |
|
| 44 |
public RegressionMetropolizedIndicatorOperator(LinearRegression linearModel, Parameter effect, |
| 45 |
Parameter indicators, MultivariateDistributionLikelihood effectPrior, |
| 46 |
Parameter mask) { |
| 47 |
|
| 48 |
effectOperator = new RegressionGibbsEffectOperator(linearModel, effect, indicators, effectPrior); |
| 49 |
this.effect = effect; |
| 50 |
this.indicators = indicators; |
| 51 |
this.mask = mask; |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public String getPerformanceSuggestion() { |
| 58 |
return null; |
| 59 |
} |
| 60 |
|
| 61 |
public String getOperatorName() { |
| 62 |
return MH_OPERATOR; |
| 63 |
} |
| 64 |
|
| 65 |
public double doOperation() { |
| 66 |
|
| 67 |
double logHastingsRatio = 0.0; |
| 68 |
|
| 69 |
if (mask != null) { |
| 70 |
int sum = 0; |
| 71 |
for(int i=0; i<mask.getDimension(); i++) |
| 72 |
sum += mask.getParameterValue(i); |
| 73 |
if (sum == 0) |
| 74 |
throw new RuntimeException("Mask parameter has all zeros"); |
| 75 |
} |
| 76 |
|
| 77 |
if (mean == null) { |
| 78 |
final int dim = effect.getDimension(); |
| 79 |
mean = new double[dim]; |
| 80 |
variance = new double[dim][dim]; |
| 81 |
precision = new double[dim][dim]; |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
effectOperator.computeForwardDensity(mean, variance,precision); |
| 86 |
logHastingsRatio += MultivariateNormalDistribution.logPdf(effect.getParameterValues(), |
| 87 |
mean, precision, |
| 88 |
Math.log(MultivariateNormalDistribution.calculatePrecisionMatrixDeterminate(precision)), |
| 89 |
1.0); |
| 90 |
|
| 91 |
|
| 92 |
int index; |
| 93 |
do { |
| 94 |
index = MathUtils.nextInt(indicators.getDimension()); |
| 95 |
} while( (mask != null) && (mask.getParameterValue(index) == 0)); |
| 96 |
|
| 97 |
indicators.setParameterValue(index, 1 - indicators.getParameterValue(index)); |
| 98 |
|
| 99 |
effectOperator.doOperation(); |
| 100 |
|
| 101 |
|
| 102 |
mean = effectOperator.getLastMean(); |
| 103 |
variance = effectOperator.getLastVariance(); |
| 104 |
precision = effectOperator.getLastPrecision(); |
| 105 |
logHastingsRatio -= MultivariateNormalDistribution.logPdf(effect.getParameterValues(), |
| 106 |
mean, precision, |
| 107 |
Math.log(MultivariateNormalDistribution.calculatePrecisionMatrixDeterminate(precision)), |
| 108 |
1.0); |
| 109 |
|
| 110 |
return logHastingsRatio; |
| 111 |
} |
| 112 |
|
| 113 |
public static XMLObjectParser PARSER = new AbstractXMLObjectParser() { |
| 114 |
|
| 115 |
public String getParserName() { |
| 116 |
return MH_OPERATOR; |
| 117 |
} |
| 118 |
|
| 119 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 120 |
|
| 121 |
double weight = xo.getDoubleAttribute(WEIGHT); |
| 122 |
|
| 123 |
LinearRegression linearModel = (LinearRegression) xo.getChild(LinearRegression.class); |
| 124 |
Parameter effect = (Parameter) xo.getChild(Parameter.class); |
| 125 |
MultivariateDistributionLikelihood prior = (MultivariateDistributionLikelihood) |
| 126 |
xo.getChild(MultivariateDistributionLikelihood.class); |
| 127 |
|
| 128 |
if (prior.getDistribution().getType().compareTo(MultivariateNormalDistribution.TYPE) != 0) |
| 129 |
throw new XMLParseException("Only a multivariate normal prior is conjugate"); |
| 130 |
|
| 131 |
XMLObject cxo = (XMLObject) xo.getChild(GeneralizedLinearModelParser.INDICATOR); |
| 132 |
Parameter indicators = (Parameter) cxo.getChild(Parameter.class); |
| 133 |
|
| 134 |
cxo = (XMLObject) xo.getChild(MaskedParameterParser.MASKING); |
| 135 |
Parameter mask = null; |
| 136 |
if (cxo != null) { |
| 137 |
mask = (Parameter) cxo.getChild(Parameter.class); |
| 138 |
if (mask.getDimension() != indicators.getDimension()) |
| 139 |
throw new XMLParseException("Indicator and mask parameter must have the same dimension"); |
| 140 |
} |
| 141 |
|
| 142 |
RegressionMetropolizedIndicatorOperator operator = new RegressionMetropolizedIndicatorOperator(linearModel, effect, |
| 143 |
indicators, prior, mask); |
| 144 |
operator.setWeight(weight); |
| 145 |
return operator; |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
public String getParserDescription() { |
| 153 |
return "This element returns a multivariate Gibbs operator on an internal node trait."; |
| 154 |
} |
| 155 |
|
| 156 |
public Class getReturnType() { |
| 157 |
return MCMCOperator.class; |
| 158 |
} |
| 159 |
|
| 160 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 161 |
return rules; |
| 162 |
} |
| 163 |
|
| 164 |
private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{ |
| 165 |
AttributeRule.newDoubleRule(WEIGHT), |
| 166 |
new ElementRule(Parameter.class), |
| 167 |
new ElementRule(MultivariateDistributionLikelihood.class), |
| 168 |
new ElementRule(LinearRegression.class), |
| 169 |
new ElementRule(GeneralizedLinearModelParser.INDICATOR, |
| 170 |
new XMLSyntaxRule[]{ |
| 171 |
new ElementRule(Parameter.class) |
| 172 |
}), |
| 173 |
new ElementRule(MaskedParameterParser.MASKING, |
| 174 |
new XMLSyntaxRule[]{ |
| 175 |
new ElementRule(Parameter.class) |
| 176 |
}, true) |
| 177 |
}; |
| 178 |
|
| 179 |
}; |
| 180 |
|
| 181 |
private Parameter mask; |
| 182 |
private Parameter indicators; |
| 183 |
private Parameter effect; |
| 184 |
private RegressionGibbsEffectOperator effectOperator; |
| 185 |
private double[] mean = null; |
| 186 |
private double[][] variance = null; |
| 187 |
private double[][] precision = null; |
| 188 |
|
| 189 |
} |