| 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 |
import dr.inference.model.*; |
| 29 |
import dr.inferencexml.distribution.MultivariateNormalDistributionModelParser; |
| 30 |
import dr.math.distributions.GaussianProcessRandomGenerator; |
| 31 |
import dr.math.distributions.MultivariateNormalDistribution; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public class MultivariateNormalDistributionModel extends AbstractModel implements ParametricMultivariateDistributionModel, |
| 41 |
GaussianProcessRandomGenerator, GradientProvider { |
| 42 |
|
| 43 |
public MultivariateNormalDistributionModel(Parameter meanParameter, MatrixParameter precParameter) { |
| 44 |
super(MultivariateNormalDistributionModelParser.NORMAL_DISTRIBUTION_MODEL); |
| 45 |
this.mean = meanParameter; |
| 46 |
addVariable(meanParameter); |
| 47 |
|
| 48 |
if (!(meanParameter instanceof DuplicatedParameter)) { |
| 49 |
meanParameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, |
| 50 |
meanParameter.getDimension())); |
| 51 |
} |
| 52 |
this.precision = precParameter; |
| 53 |
addVariable(precParameter); |
| 54 |
|
| 55 |
Parameter single = null; |
| 56 |
if (precParameter instanceof DiagonalMatrix) { |
| 57 |
DiagonalMatrix dm = (DiagonalMatrix) precParameter; |
| 58 |
if (dm.getDiagonalParameter() instanceof DuplicatedParameter) { |
| 59 |
single = dm.getDiagonalParameter(); |
| 60 |
} |
| 61 |
} |
| 62 |
hasSinglePrecision = (single != null); |
| 63 |
singlePrecision = single; |
| 64 |
|
| 65 |
distribution = createNewDistribution(); |
| 66 |
distributionKnown = true; |
| 67 |
} |
| 68 |
|
| 69 |
public MatrixParameter getPrecisionMatrixParameter() { |
| 70 |
return precision; |
| 71 |
} |
| 72 |
|
| 73 |
public Parameter getMeanParameter() { |
| 74 |
return mean; |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
private void checkDistribution() { |
| 83 |
if (!distributionKnown) { |
| 84 |
distribution = createNewDistribution(); |
| 85 |
distributionKnown = true; |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
public double logPdf(double[] x) { |
| 90 |
checkDistribution(); |
| 91 |
return distribution.logPdf(x); |
| 92 |
} |
| 93 |
|
| 94 |
public double[][] getScaleMatrix() { |
| 95 |
return precision.getParameterAsMatrix(); |
| 96 |
} |
| 97 |
|
| 98 |
public double[] getMean() { |
| 99 |
return mean.getParameterValues(); |
| 100 |
} |
| 101 |
|
| 102 |
public String getType() { |
| 103 |
return distribution.getType(); |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
public void handleModelChangedEvent(Model model, Object object, int index) { |
| 111 |
|
| 112 |
} |
| 113 |
|
| 114 |
public Likelihood getLikelihood() { |
| 115 |
return null; |
| 116 |
} |
| 117 |
|
| 118 |
protected final void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) { |
| 119 |
distributionKnown = false; |
| 120 |
} |
| 121 |
|
| 122 |
protected void storeState() { |
| 123 |
storedDistribution = distribution; |
| 124 |
storedDistributionKnown = distributionKnown; |
| 125 |
} |
| 126 |
|
| 127 |
protected void restoreState() { |
| 128 |
distributionKnown = storedDistributionKnown; |
| 129 |
distribution = storedDistribution; |
| 130 |
} |
| 131 |
|
| 132 |
protected void acceptState() { |
| 133 |
} |
| 134 |
|
| 135 |
@Override |
| 136 |
public int getDimension() { |
| 137 |
return mean.getDimension(); |
| 138 |
} |
| 139 |
|
| 140 |
@Override |
| 141 |
public double[][] getPrecisionMatrix() { |
| 142 |
return precision.getParameterAsMatrix(); |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
@Override |
| 150 |
public Variable<Double> getLocationVariable() { |
| 151 |
return mean; |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
private MultivariateNormalDistribution createNewDistribution() { |
| 159 |
if (hasSinglePrecision) { |
| 160 |
return new MultivariateNormalDistribution(getMean(), singlePrecision.getParameterValue(0)); |
| 161 |
} else { |
| 162 |
return new MultivariateNormalDistribution(getMean(), getScaleMatrix()); |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
private final Parameter mean; |
| 167 |
private final MatrixParameter precision; |
| 168 |
private final boolean hasSinglePrecision; |
| 169 |
private final Parameter singlePrecision; |
| 170 |
private MultivariateNormalDistribution distribution; |
| 171 |
private MultivariateNormalDistribution storedDistribution; |
| 172 |
|
| 173 |
private boolean distributionKnown; |
| 174 |
private boolean storedDistributionKnown; |
| 175 |
|
| 176 |
|
| 177 |
public double[] nextRandom() { |
| 178 |
checkDistribution(); |
| 179 |
return distribution.nextMultivariateNormal(); |
| 180 |
} |
| 181 |
|
| 182 |
public double logPdf(Object x) { |
| 183 |
checkDistribution(); |
| 184 |
return distribution.logPdf(x); |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
@Override |
| 189 |
public double[] getGradientLogDensity(Object x) { |
| 190 |
checkDistribution(); |
| 191 |
return distribution.getGradientLogDensity(x); |
| 192 |
} |
| 193 |
|
| 194 |
} |