| 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.evomodel.epidemiology.casetocase.periodpriors; |
| 27 |
|
| 28 |
import dr.inference.loggers.LogColumn; |
| 29 |
import dr.inference.model.Parameter; |
| 30 |
import dr.math.distributions.NormalDistribution; |
| 31 |
import dr.xml.*; |
| 32 |
|
| 33 |
import java.util.ArrayList; |
| 34 |
import java.util.Arrays; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
public class KnownVarianceNormalPeriodPriorDistribution extends AbstractPeriodPriorDistribution { |
| 43 |
|
| 44 |
public static final String NORMAL = "knownVarianceNormalPeriodPriorDistribution"; |
| 45 |
public static final String LOG = "log"; |
| 46 |
public static final String ID = "id"; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public static final String MU_0 = "mu0"; |
| 52 |
public static final String SIGMA = "sigma"; |
| 53 |
public static final String SIGMA_0 = "sigma0"; |
| 54 |
|
| 55 |
private NormalDistribution hyperprior; |
| 56 |
|
| 57 |
private Parameter posteriorMean; |
| 58 |
private Parameter posteriorVariance; |
| 59 |
private double sigma; |
| 60 |
|
| 61 |
private ArrayList<Double> dataValues; |
| 62 |
private double[] currentParameters; |
| 63 |
|
| 64 |
public KnownVarianceNormalPeriodPriorDistribution(String name, boolean log, double sigma, |
| 65 |
NormalDistribution hyperprior){ |
| 66 |
super(name, log); |
| 67 |
this.hyperprior = hyperprior; |
| 68 |
posteriorVariance = new Parameter.Default(1); |
| 69 |
posteriorMean = new Parameter.Default(1); |
| 70 |
addVariable(posteriorVariance); |
| 71 |
addVariable(posteriorMean); |
| 72 |
this.sigma = sigma; |
| 73 |
} |
| 74 |
|
| 75 |
public KnownVarianceNormalPeriodPriorDistribution(String name, boolean log, double sigma, |
| 76 |
double mu_0, double sigma_0){ |
| 77 |
this(name, log, sigma, new NormalDistribution(mu_0, sigma_0)); |
| 78 |
} |
| 79 |
|
| 80 |
public void reset(){ |
| 81 |
dataValues = new ArrayList<Double>(); |
| 82 |
currentParameters[0] = hyperprior.getMean(); |
| 83 |
currentParameters[1] = hyperprior.getSD(); |
| 84 |
logL = 0; |
| 85 |
} |
| 86 |
|
| 87 |
public double calculateLogPosteriorProbability(double newValue, double minValue){ |
| 88 |
double out = calculateLogPosteriorPredictiveProbability(newValue); |
| 89 |
if(minValue != Double.NEGATIVE_INFINITY){ |
| 90 |
out -= calculateLogPosteriorPredictiveCDF(minValue, true); |
| 91 |
} |
| 92 |
logL += out; |
| 93 |
update(newValue); |
| 94 |
return out; |
| 95 |
} |
| 96 |
|
| 97 |
public double calculateLogPosteriorCDF(double limit, boolean upper) { |
| 98 |
return calculateLogPosteriorPredictiveCDF(limit, upper); |
| 99 |
} |
| 100 |
|
| 101 |
public double calculateLogPosteriorPredictiveProbability(double value){ |
| 102 |
double mean = currentParameters[0]; |
| 103 |
double sd = currentParameters[1]; |
| 104 |
|
| 105 |
return NormalDistribution.logPdf(value, mean, Math.sqrt(Math.pow(sd, 2) + Math.pow(sigma, 2))); |
| 106 |
} |
| 107 |
|
| 108 |
public double calculateLogPosteriorPredictiveCDF(double value, boolean upperTail){ |
| 109 |
double mean = currentParameters[0]; |
| 110 |
double sd = currentParameters[1]; |
| 111 |
|
| 112 |
double scaledValue = (value - mean)/Math.sqrt(Math.pow(sd, 2) + Math.pow(sigma, 2)); |
| 113 |
|
| 114 |
return upperTail ? NormalDistribution.standardCDF(-scaledValue, true) : |
| 115 |
NormalDistribution.standardCDF(scaledValue, true); |
| 116 |
} |
| 117 |
|
| 118 |
private void update(double newData){ |
| 119 |
dataValues.add(newData); |
| 120 |
|
| 121 |
double originalMean = hyperprior.getMean(); |
| 122 |
double originalSD = hyperprior.getSD(); |
| 123 |
|
| 124 |
double count = dataValues.size(); |
| 125 |
|
| 126 |
double dataMean = 0; |
| 127 |
for(double value: dataValues){ |
| 128 |
dataMean += value; |
| 129 |
} |
| 130 |
dataMean /= count; |
| 131 |
|
| 132 |
double newSD = Math.sqrt(1/(count/Math.pow(sigma,2) + 1/Math.pow(originalSD,2))); |
| 133 |
|
| 134 |
double newMean = Math.pow(newSD,2)*(originalMean/Math.pow(originalSD,2) + count*dataMean/Math.pow(sigma,2)); |
| 135 |
|
| 136 |
currentParameters = new double[]{newMean, newSD}; |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
public double calculateLogLikelihood(double[] values){ |
| 141 |
|
| 142 |
int count = values.length; |
| 143 |
|
| 144 |
double mu_0 = hyperprior.getMean(); |
| 145 |
double sigma_0 = hyperprior.getSD(); |
| 146 |
|
| 147 |
double var = Math.pow(sigma, 2); |
| 148 |
double var_0 = Math.pow(sigma_0, 2); |
| 149 |
|
| 150 |
double sum = 0; |
| 151 |
double sumOfSquares = 0; |
| 152 |
for (Double infPeriod : values) { |
| 153 |
sum += infPeriod; |
| 154 |
sumOfSquares += Math.pow(infPeriod, 2); |
| 155 |
} |
| 156 |
double mean = sum/count; |
| 157 |
|
| 158 |
posteriorMean.setParameterValue(0, ((mu_0/var_0) + sum/var)/(1/var_0 + count/var)); |
| 159 |
posteriorVariance.setParameterValue(0, 1/(1/var_0 + count/var)); |
| 160 |
|
| 161 |
logL = Math.log(sigma) |
| 162 |
- count * Math.log(Math.sqrt(2*Math.PI)*sigma) |
| 163 |
- Math.log(Math.sqrt(count*var_0 + var)) |
| 164 |
+ -sumOfSquares/(2*var) - Math.pow(mu_0, 2)/(2*var_0) |
| 165 |
+ (Math.pow(sigma_0*count*mean/sigma, 2) + Math.pow(sigma*mu_0/sigma_0, 2) + 2*count*mean*mu_0) |
| 166 |
/(2*(count*var_0 + var)); |
| 167 |
|
| 168 |
|
| 169 |
return logL; |
| 170 |
} |
| 171 |
|
| 172 |
public LogColumn[] getColumns() { |
| 173 |
ArrayList<LogColumn> columns = new ArrayList<LogColumn>(Arrays.asList(super.getColumns())); |
| 174 |
|
| 175 |
columns.add(new LogColumn.Abstract(getModelName()+"_posteriorMean"){ |
| 176 |
protected String getFormattedValue() { |
| 177 |
return String.valueOf(posteriorMean.getParameterValue(0)); |
| 178 |
} |
| 179 |
}); |
| 180 |
|
| 181 |
columns.add(new LogColumn.Abstract(getModelName()+"_posteriorVariance"){ |
| 182 |
protected String getFormattedValue() { |
| 183 |
return String.valueOf(posteriorVariance.getParameterValue(0)); |
| 184 |
} |
| 185 |
}); |
| 186 |
|
| 187 |
return columns.toArray(new LogColumn[columns.size()]); |
| 188 |
} |
| 189 |
|
| 190 |
public static XMLObjectParser PARSER = new AbstractXMLObjectParser() { |
| 191 |
|
| 192 |
public String getParserName() { |
| 193 |
return NORMAL; |
| 194 |
} |
| 195 |
|
| 196 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 197 |
String id = (String) xo.getAttribute(ID); |
| 198 |
|
| 199 |
double mu_0 = xo.getDoubleAttribute(MU_0); |
| 200 |
double sigma = xo.getDoubleAttribute(SIGMA); |
| 201 |
double sigma_0 = xo.getDoubleAttribute(SIGMA_0); |
| 202 |
|
| 203 |
boolean log; |
| 204 |
log = xo.hasAttribute(LOG) ? xo.getBooleanAttribute(LOG) : false; |
| 205 |
|
| 206 |
return new KnownVarianceNormalPeriodPriorDistribution(id, log, sigma, mu_0, sigma_0); |
| 207 |
|
| 208 |
} |
| 209 |
|
| 210 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 211 |
return rules; |
| 212 |
} |
| 213 |
|
| 214 |
private final XMLSyntaxRule[] rules = { |
| 215 |
AttributeRule.newBooleanRule(LOG, true), |
| 216 |
AttributeRule.newStringRule(ID, false), |
| 217 |
AttributeRule.newDoubleRule(MU_0, false), |
| 218 |
AttributeRule.newDoubleRule(SIGMA, false), |
| 219 |
AttributeRule.newDoubleRule(SIGMA_0, false), |
| 220 |
}; |
| 221 |
|
| 222 |
public String getParserDescription() { |
| 223 |
return "Calculates the probability of a set of doubles being drawn from the prior posterior distribution" + |
| 224 |
"of a normal distribution of unknown mean and known standard deviation sigma"; |
| 225 |
} |
| 226 |
|
| 227 |
public Class getReturnType() { |
| 228 |
return KnownVarianceNormalPeriodPriorDistribution.class; |
| 229 |
} |
| 230 |
}; |
| 231 |
|
| 232 |
} |