| 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.model.Parameter; |
| 29 |
import dr.inference.regression.SelfControlledCaseSeries; |
| 30 |
import dr.math.MathUtils; |
| 31 |
import dr.math.Poisson; |
| 32 |
import dr.math.distributions.NormalDistribution; |
| 33 |
import dr.xml.*; |
| 34 |
|
| 35 |
import java.util.HashSet; |
| 36 |
import java.util.Set; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public class MultivariateNormalIndependenceSampler extends AbstractAdaptableOperator { |
| 45 |
|
| 46 |
public static final String OPERATOR_NAME = "multivariateNormalIndependenceSampler"; |
| 47 |
public static final String SCALE_FACTOR = "scaleFactor"; |
| 48 |
public static final String SET_SIZE_MEAN = "setSizeMean"; |
| 49 |
|
| 50 |
private double scaleFactor; |
| 51 |
private final Parameter parameter; |
| 52 |
private final int dim; |
| 53 |
private double setSizeMean; |
| 54 |
private final SelfControlledCaseSeries sccs; |
| 55 |
|
| 56 |
public MultivariateNormalIndependenceSampler(Parameter parameter, |
| 57 |
SelfControlledCaseSeries sccs, |
| 58 |
double setSizeMean, |
| 59 |
double weight, double scaleFactor, AdaptationMode mode) { |
| 60 |
super(mode); |
| 61 |
this.scaleFactor = scaleFactor; |
| 62 |
this.parameter = parameter; |
| 63 |
setWeight(weight); |
| 64 |
dim = parameter.getDimension(); |
| 65 |
setWeight(weight); |
| 66 |
this.sccs = sccs; |
| 67 |
this.setSizeMean = setSizeMean; |
| 68 |
} |
| 69 |
|
| 70 |
public String getAdaptableParameterName() { |
| 71 |
return "scaleFactor"; |
| 72 |
} |
| 73 |
|
| 74 |
public String getOperatorName() { |
| 75 |
return "independentNormalDistribution(" + parameter.getVariableName() + ")"; |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
public double doOperation() { |
| 82 |
|
| 83 |
double[] mean = sccs.getMode(); |
| 84 |
double[] currentValue = parameter.getParameterValues(); |
| 85 |
double[] newValue = new double[dim]; |
| 86 |
|
| 87 |
Set<Integer> updateSet = new HashSet<Integer>(); |
| 88 |
|
| 89 |
if (setSizeMean != -1.0) { |
| 90 |
final int listLength = Poisson.nextPoisson(setSizeMean); |
| 91 |
while (updateSet.size() < listLength) { |
| 92 |
int newInt = MathUtils.nextInt(parameter.getDimension()); |
| 93 |
if (!updateSet.contains(newInt)) { |
| 94 |
updateSet.add(newInt); |
| 95 |
} |
| 96 |
} |
| 97 |
} else { |
| 98 |
for (int i = 0; i < dim; ++i) { |
| 99 |
updateSet.add(i); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
double logq = 0; |
| 104 |
for (Integer i : updateSet) { |
| 105 |
newValue[i] = mean[i] + scaleFactor * MathUtils.nextGaussian(); |
| 106 |
if (UPDATE_ALL) { |
| 107 |
parameter.setParameterValueQuietly(i, newValue[i]); |
| 108 |
} else { |
| 109 |
parameter.setParameterValue(i, newValue[i]); |
| 110 |
} |
| 111 |
|
| 112 |
logq += (NormalDistribution.logPdf(currentValue[i], mean[i], scaleFactor) - |
| 113 |
NormalDistribution.logPdf(newValue[i], mean[i], scaleFactor)); |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
if (UPDATE_ALL) { |
| 121 |
parameter.setParameterValueNotifyChangedAll(0, parameter.getParameterValue(0)); |
| 122 |
} |
| 123 |
|
| 124 |
return logq; |
| 125 |
} |
| 126 |
|
| 127 |
private static final boolean UPDATE_ALL = false; |
| 128 |
|
| 129 |
public static XMLObjectParser PARSER = new AbstractXMLObjectParser() { |
| 130 |
|
| 131 |
public String getParserName() { |
| 132 |
return OPERATOR_NAME; |
| 133 |
} |
| 134 |
|
| 135 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 136 |
|
| 137 |
AdaptationMode mode = AdaptationMode.parseMode(xo); |
| 138 |
|
| 139 |
double weight = xo.getDoubleAttribute(WEIGHT); |
| 140 |
double scaleFactor = xo.getDoubleAttribute(SCALE_FACTOR); |
| 141 |
|
| 142 |
if (scaleFactor <= 0.0) { |
| 143 |
throw new XMLParseException("scaleFactor must be greater than 0.0"); |
| 144 |
} |
| 145 |
|
| 146 |
Parameter parameter = (Parameter) xo.getChild(Parameter.class); |
| 147 |
|
| 148 |
SelfControlledCaseSeries sccs = (SelfControlledCaseSeries) xo.getChild(SelfControlledCaseSeries.class); |
| 149 |
|
| 150 |
double setSizeMean = xo.getAttribute(SET_SIZE_MEAN, -1.0); |
| 151 |
|
| 152 |
return new MultivariateNormalIndependenceSampler(parameter, sccs, setSizeMean, weight, scaleFactor, mode); |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 160 |
return rules; |
| 161 |
} |
| 162 |
|
| 163 |
private final XMLSyntaxRule[] rules = { |
| 164 |
AttributeRule.newDoubleRule(SCALE_FACTOR), |
| 165 |
AttributeRule.newDoubleRule(WEIGHT), |
| 166 |
AttributeRule.newBooleanRule(AUTO_OPTIMIZE, true), |
| 167 |
AttributeRule.newDoubleRule(SET_SIZE_MEAN, true), |
| 168 |
new ElementRule(SelfControlledCaseSeries.class), |
| 169 |
new ElementRule(Parameter.class), |
| 170 |
}; |
| 171 |
|
| 172 |
public String getParserDescription() { |
| 173 |
return "This element returns an independence sampler from a provided normal distribution model."; |
| 174 |
} |
| 175 |
|
| 176 |
public Class getReturnType() { |
| 177 |
return MultivariateNormalIndependenceSampler.class; |
| 178 |
} |
| 179 |
|
| 180 |
}; |
| 181 |
|
| 182 |
public double getAdaptableParameter() { |
| 183 |
return Math.log(scaleFactor); |
| 184 |
} |
| 185 |
|
| 186 |
public void setAdaptableParameter(double value) { |
| 187 |
scaleFactor = Math.exp(value); |
| 188 |
} |
| 189 |
|
| 190 |
public double getRawParameter() { |
| 191 |
return scaleFactor; |
| 192 |
} |
| 193 |
|
| 194 |
} |