| 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.distribution; |
| 27 |
|
| 28 |
|
| 29 |
import dr.inference.model.*; |
| 30 |
import dr.inference.model.Parameter; |
| 31 |
import dr.inferencexml.distribution.MomentDistributionModelParser; |
| 32 |
import dr.math.distributions.RandomGenerator; |
| 33 |
|
| 34 |
|
| 35 |
public class MomentDistributionModel extends AbstractModelLikelihood implements ParametricMultivariateDistributionModel, RandomGenerator { |
| 36 |
|
| 37 |
public MomentDistributionModel(String id, Parameter mean, Parameter precision, Parameter cutoff, Parameter data) { |
| 38 |
super(id); |
| 39 |
|
| 40 |
this.mean=mean; |
| 41 |
this.precision=precision; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
addVariable(mean); |
| 48 |
mean.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, 1)); |
| 49 |
addVariable(precision); |
| 50 |
|
| 51 |
this.cutoff=cutoff; |
| 52 |
if(cutoff!=null){ |
| 53 |
addVariable(cutoff); |
| 54 |
int boundsDim; |
| 55 |
if(cutoff.getBounds()!=null) |
| 56 |
boundsDim = cutoff.getBounds().getBoundsDimension(); |
| 57 |
else boundsDim = cutoff.getDimension(); |
| 58 |
cutoff.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, boundsDim));} |
| 59 |
addVariable(data); |
| 60 |
this.data=data; |
| 61 |
untruncated=new NormalDistributionModel(mean, precision, true); |
| 62 |
sumKnown=false; |
| 63 |
untruncatedKnown=false; |
| 64 |
} |
| 65 |
|
| 66 |
private final Parameter mean; |
| 67 |
private final Parameter precision; |
| 68 |
|
| 69 |
|
| 70 |
private final Parameter cutoff; |
| 71 |
private NormalDistributionModel untruncated; |
| 72 |
private double sum; |
| 73 |
private boolean sumKnown; |
| 74 |
|
| 75 |
private boolean storedSumKnown; |
| 76 |
private double storedSum; |
| 77 |
private boolean untruncatedKnown; |
| 78 |
private boolean storedUntruncatedKnown; |
| 79 |
private NormalDistributionModel storedUntruncated; |
| 80 |
private Parameter data; |
| 81 |
|
| 82 |
public double logPdf(Parameter data) { |
| 83 |
|
| 84 |
|
| 85 |
checkDistribution(); |
| 86 |
if(sumKnown) |
| 87 |
return sum; |
| 88 |
else |
| 89 |
{ |
| 90 |
sum=0; |
| 91 |
} |
| 92 |
|
| 93 |
if(cutoff!=null){ |
| 94 |
if(data.getDimension()!=cutoff.getDimension()){ |
| 95 |
throw new RuntimeException("Incorrect number of cutoffs"); |
| 96 |
} |
| 97 |
for (int i = 0; i <data.getDimension() ; i++) { |
| 98 |
if (Math.sqrt(cutoff.getParameterValue(i)) - .05 > Math.abs(data.getParameterValue(i)) && data.getParameterValue(i)!=0){ |
| 99 |
return Double.NEGATIVE_INFINITY; } |
| 100 |
else if(data.getParameterValue(i)==0){ |
| 101 |
sum += 0; |
| 102 |
} |
| 103 |
|
| 104 |
else { |
| 105 |
sum+=untruncated.logPdf(data.getParameterValue(i)) |
| 106 |
+ getNormalizingConstant(i) |
| 107 |
; |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
else{ |
| 112 |
for (int i = 0; i <data.getDimension() ; i++) { |
| 113 |
sum+= untruncated.logPdf(data.getParameterValue(i)) + 2 * StrictMath.log(Math.abs(data.getParameterValue(i))) + StrictMath.log(precision.getParameterValue(0)); |
| 114 |
} |
| 115 |
} |
| 116 |
sumKnown=true; |
| 117 |
return sum; |
| 118 |
|
| 119 |
} |
| 120 |
|
| 121 |
public double getNormalizingConstant(int i){ |
| 122 |
return -Math.log(1 - (untruncated.cdf(Math.sqrt(cutoff.getParameterValue(i))) - untruncated.cdf(-Math.sqrt(cutoff.getParameterValue(i))))); |
| 123 |
} |
| 124 |
|
| 125 |
@Override |
| 126 |
public double logPdf(double[] x) { |
| 127 |
return 0; |
| 128 |
} |
| 129 |
|
| 130 |
public Parameter getCutoff(){return cutoff;} |
| 131 |
|
| 132 |
@Override |
| 133 |
public double[][] getScaleMatrix() { |
| 134 |
double[][] temp=new double[1][1]; |
| 135 |
temp[0][0]=precision.getParameterValue(0); |
| 136 |
return temp; |
| 137 |
|
| 138 |
} |
| 139 |
|
| 140 |
@Override |
| 141 |
public double[] getMean() { |
| 142 |
return mean.getParameterValues(); |
| 143 |
} |
| 144 |
|
| 145 |
@Override |
| 146 |
public String getType() { |
| 147 |
return "Moment Distribution Model"; |
| 148 |
} |
| 149 |
|
| 150 |
@Override |
| 151 |
protected void handleModelChangedEvent(Model model, Object object, int index) { |
| 152 |
|
| 153 |
} |
| 154 |
|
| 155 |
@Override |
| 156 |
protected void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) { |
| 157 |
sumKnown=false; |
| 158 |
if(variable==mean || variable==precision) |
| 159 |
{untruncatedKnown=false;} |
| 160 |
|
| 161 |
} |
| 162 |
|
| 163 |
@Override |
| 164 |
protected void storeState() { |
| 165 |
storedSumKnown=sumKnown; |
| 166 |
storedSum=sum; |
| 167 |
storedUntruncated=untruncated; |
| 168 |
storedUntruncatedKnown=untruncatedKnown; |
| 169 |
} |
| 170 |
|
| 171 |
@Override |
| 172 |
protected void restoreState() { |
| 173 |
sumKnown=storedSumKnown; |
| 174 |
sum=storedSum; |
| 175 |
untruncated=storedUntruncated; |
| 176 |
untruncatedKnown=storedUntruncatedKnown; |
| 177 |
|
| 178 |
} |
| 179 |
|
| 180 |
@Override |
| 181 |
protected void acceptState() { |
| 182 |
|
| 183 |
} |
| 184 |
|
| 185 |
private NormalDistributionModel createNewDistribution() { |
| 186 |
return new NormalDistributionModel(mean, precision, true); |
| 187 |
|
| 188 |
} |
| 189 |
|
| 190 |
private void checkDistribution() { |
| 191 |
if (!untruncatedKnown) { |
| 192 |
untruncated = createNewDistribution(); |
| 193 |
untruncatedKnown = true; |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
@Override |
| 198 |
public double[] nextRandom() { |
| 199 |
return new double[0]; |
| 200 |
} |
| 201 |
|
| 202 |
@Override |
| 203 |
public double logPdf(Object x) { |
| 204 |
if(x instanceof Parameter) |
| 205 |
return logPdf((Parameter) x); |
| 206 |
else |
| 207 |
return 0; |
| 208 |
} |
| 209 |
|
| 210 |
@Override |
| 211 |
public Model getModel() { |
| 212 |
return this; |
| 213 |
} |
| 214 |
|
| 215 |
@Override |
| 216 |
public double getLogLikelihood() { |
| 217 |
return logPdf(data); |
| 218 |
} |
| 219 |
|
| 220 |
@Override |
| 221 |
public void makeDirty() { |
| 222 |
sumKnown=false; |
| 223 |
untruncatedKnown=false; |
| 224 |
|
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
@Override |
| 232 |
public Variable<Double> getLocationVariable() { |
| 233 |
throw new UnsupportedOperationException("Not implemented"); |
| 234 |
} |
| 235 |
|
| 236 |
} |