| 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.AbstractModel; |
| 29 |
import dr.inference.model.Model; |
| 30 |
import dr.inference.model.Parameter; |
| 31 |
import dr.inference.model.Variable; |
| 32 |
import dr.math.distributions.GammaDistribution; |
| 33 |
import dr.math.distributions.WishartDistribution; |
| 34 |
import dr.math.distributions.WishartStatistics; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public class WishartGammalDistributionModel extends AbstractModel implements ParametricMultivariateDistributionModel, |
| 49 |
WishartStatistics { |
| 50 |
|
| 51 |
public WishartGammalDistributionModel(Parameter df, Parameter mixing, Parameter scale, boolean randomMixing) { |
| 52 |
super(WishartGammalDistributionModel.TYPE); |
| 53 |
|
| 54 |
this.df = df; |
| 55 |
this.mixing = mixing; |
| 56 |
this.scale = scale; |
| 57 |
|
| 58 |
addVariable(df); |
| 59 |
addVariable(mixing); |
| 60 |
addVariable(scale); |
| 61 |
|
| 62 |
this.randomMixing = randomMixing; |
| 63 |
|
| 64 |
df.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, |
| 65 |
df.getDimension())); |
| 66 |
|
| 67 |
mixing.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, |
| 68 |
mixing.getDimension())); |
| 69 |
|
| 70 |
scale.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, |
| 71 |
scale.getDimension())); |
| 72 |
|
| 73 |
dim = mixing.getDimension(); |
| 74 |
if (dim != scale.getDimension()) { |
| 75 |
throw new IllegalArgumentException(); |
| 76 |
} |
| 77 |
|
| 78 |
wishartDistributionKnown = false; |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
public double logPdf(double[] x) { |
| 87 |
if (x.length != dim * dim) { |
| 88 |
throw new IllegalArgumentException("Data of wrong dimension in " + getId()); |
| 89 |
} |
| 90 |
|
| 91 |
if (!wishartDistributionKnown) { |
| 92 |
wishart = createNewWishartDistribution(); |
| 93 |
wishartDistributionKnown = true; |
| 94 |
} |
| 95 |
|
| 96 |
double logLike = wishart.logPdf(x); |
| 97 |
if (randomMixing) { |
| 98 |
for (int i = 0; i < dim; ++i) { |
| 99 |
double mixing = this.mixing.getParameterValue(i); |
| 100 |
double scale = this.scale.getParameterValue(i); |
| 101 |
logLike += GammaDistribution.logPdf(mixing, 0.5, scale * scale); |
| 102 |
} |
| 103 |
} |
| 104 |
return logLike; |
| 105 |
} |
| 106 |
|
| 107 |
public double[][] getScaleMatrix() { |
| 108 |
double df = this.df.getParameterValue(0); |
| 109 |
double[][] scaleMatrix = new double[dim][dim]; |
| 110 |
for (int i = 0; i < dim; ++i) { |
| 111 |
scaleMatrix[i][i] = 1.0 / (2.0 * df * mixing.getParameterValue(i)); |
| 112 |
} |
| 113 |
return scaleMatrix; |
| 114 |
} |
| 115 |
|
| 116 |
public double getDF() { |
| 117 |
return df.getParameterValue(0) + dim - 1; |
| 118 |
} |
| 119 |
|
| 120 |
public double[] getMean() { |
| 121 |
return new double[dim]; |
| 122 |
} |
| 123 |
|
| 124 |
public String getType() { |
| 125 |
return TYPE; |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
public void handleModelChangedEvent(Model model, Object object, int index) { |
| 133 |
|
| 134 |
} |
| 135 |
|
| 136 |
protected final void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) { |
| 137 |
if (variable == mixing || variable == df) { |
| 138 |
wishartDistributionKnown = false; |
| 139 |
} |
| 140 |
|
| 141 |
if (!randomMixing && variable == mixing) { |
| 142 |
throw new IllegalArgumentException("WishartGamma distribution is not configured for random mixing"); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
protected void storeState() { |
| 147 |
storedWishart = wishart; |
| 148 |
storedWishartDistributionKnown = wishartDistributionKnown; |
| 149 |
} |
| 150 |
|
| 151 |
protected void restoreState() { |
| 152 |
wishartDistributionKnown = storedWishartDistributionKnown; |
| 153 |
wishart = storedWishart; |
| 154 |
} |
| 155 |
|
| 156 |
protected void acceptState() { |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
@Override |
| 165 |
public Variable<Double> getLocationVariable() { |
| 166 |
throw new UnsupportedOperationException("Not implemented"); |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
private WishartDistribution createNewWishartDistribution() { |
| 174 |
return new WishartDistribution(getDF(), getScaleMatrix()); |
| 175 |
} |
| 176 |
|
| 177 |
private final int dim; |
| 178 |
|
| 179 |
private final Parameter df; |
| 180 |
private final Parameter mixing; |
| 181 |
private final Parameter scale; |
| 182 |
|
| 183 |
private final boolean randomMixing; |
| 184 |
|
| 185 |
private WishartDistribution wishart; |
| 186 |
private WishartDistribution storedWishart; |
| 187 |
|
| 188 |
private boolean wishartDistributionKnown; |
| 189 |
private boolean storedWishartDistributionKnown; |
| 190 |
|
| 191 |
public static final String TYPE = "WishartGamma"; |
| 192 |
|
| 193 |
@Override |
| 194 |
public double[] nextRandom() { |
| 195 |
|
| 196 |
return null; |
| 197 |
} |
| 198 |
} |