| 1 |
package dr.inference.operators; |
| 2 |
|
| 3 |
import dr.inference.distribution.DistributionLikelihood; |
| 4 |
import dr.inference.distribution.LatentFactorModelInterface; |
| 5 |
import dr.inference.distribution.MomentDistributionModel; |
| 6 |
import dr.inference.model.*; |
| 7 |
import dr.math.MathUtils; |
| 8 |
import dr.math.distributions.NormalDistribution; |
| 9 |
import dr.math.distributions.TruncatedNormalDistribution; |
| 10 |
import dr.math.matrixAlgebra.SymmetricMatrix; |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
public class LoadingsGibbsTruncatedOperator extends SimpleMCMCOperator implements PathDependentOperator, GibbsOperator{ |
| 16 |
Likelihood prior; |
| 17 |
LatentFactorModelInterface LFM; |
| 18 |
double[][] precisionArray; |
| 19 |
double[] meanMidArray; |
| 20 |
double[] meanArray; |
| 21 |
boolean randomScan; |
| 22 |
double pathParameter=1.0; |
| 23 |
final Parameter missingIndicator; |
| 24 |
|
| 25 |
double priorPrecision; |
| 26 |
double priorMeanPrecision; |
| 27 |
MatrixParameterInterface loadings; |
| 28 |
|
| 29 |
DistributionLikelihood cutoffPrior; |
| 30 |
|
| 31 |
public LoadingsGibbsTruncatedOperator(LatentFactorModelInterface LFM, Likelihood prior, double weight, boolean randomScan, MatrixParameterInterface loadings, DistributionLikelihood cutoffPrior) { |
| 32 |
setWeight(weight); |
| 33 |
|
| 34 |
this.loadings=loadings; |
| 35 |
this.prior = prior; |
| 36 |
this.LFM = LFM; |
| 37 |
if(prior instanceof MomentDistributionModel){ |
| 38 |
priorPrecision = (((MomentDistributionModel) this.prior).getScaleMatrix()[0][0]); |
| 39 |
priorMeanPrecision = ((MomentDistributionModel) this.prior).getMean()[0] * priorPrecision;} |
| 40 |
else if (prior instanceof DistributionLikelihood){ |
| 41 |
priorPrecision = 1 / ((DistributionLikelihood) this.prior).getDistribution().variance(); |
| 42 |
priorMeanPrecision = ((DistributionLikelihood) this.prior).getDistribution().mean() * priorPrecision; |
| 43 |
} |
| 44 |
this.cutoffPrior = cutoffPrior; |
| 45 |
missingIndicator = LFM.getMissingIndicator(); |
| 46 |
} |
| 47 |
|
| 48 |
private void getPrecisionOfTruncated(MatrixParameterInterface full, int newRowDimension, int row, double[][] answer) { |
| 49 |
int p = full.getColumnDimension(); |
| 50 |
for (int i = 0; i < newRowDimension; i++) { |
| 51 |
for (int j = i; j < newRowDimension; j++) { |
| 52 |
double sum = 0; |
| 53 |
for (int k = 0; k < p; k++){ |
| 54 |
if(missingIndicator == null || missingIndicator.getParameterValue(k * LFM.getScaledData().getRowDimension() + row) != 1) { |
| 55 |
sum += full.getParameterValue(i, k) * full.getParameterValue(j, k); |
| 56 |
} |
| 57 |
} |
| 58 |
answer[i][j] = sum * LFM.getColumnPrecision().getParameterValue(row, row); |
| 59 |
if (i == j) { |
| 60 |
answer[i][j] =answer[i][j] * pathParameter + priorPrecision; |
| 61 |
} else { |
| 62 |
answer[i][j] *= pathParameter; |
| 63 |
answer[j][i] = answer[i][j]; |
| 64 |
} |
| 65 |
} |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
private void getTruncatedMean(int newRowDimension, int dataColumn, double[][] variance, double[] midMean, double[] mean) { |
| 71 |
MatrixParameterInterface data = LFM.getScaledData(); |
| 72 |
MatrixParameterInterface Left = LFM.getFactors(); |
| 73 |
int p = data.getColumnDimension(); |
| 74 |
for (int i = 0; i < newRowDimension; i++) { |
| 75 |
double sum = 0; |
| 76 |
for (int k = 0; k < p; k++) |
| 77 |
{ |
| 78 |
if(missingIndicator == null || missingIndicator.getParameterValue(k * LFM.getScaledData().getRowDimension() + dataColumn) != 1) |
| 79 |
sum += Left.getParameterValue(i, k) * data.getParameterValue(dataColumn, k); |
| 80 |
} |
| 81 |
sum = sum * LFM.getColumnPrecision().getParameterValue(dataColumn, dataColumn); |
| 82 |
sum += priorMeanPrecision; |
| 83 |
midMean[i] = sum; |
| 84 |
} |
| 85 |
for (int i = 0; i < newRowDimension; i++) { |
| 86 |
double sum = 0; |
| 87 |
for (int k = 0; k < newRowDimension; k++) |
| 88 |
sum += variance[i][k] * midMean[k]; |
| 89 |
mean[i] = sum; |
| 90 |
} |
| 91 |
|
| 92 |
} |
| 93 |
|
| 94 |
private void getPrecision(int i, double[][] answer) { |
| 95 |
int size = loadings.getColumnDimension(); |
| 96 |
getPrecisionOfTruncated(LFM.getFactors(), size, i, answer); |
| 97 |
} |
| 98 |
|
| 99 |
private void getMean(int i, double[][] variance, double[] midMean, double[] mean) { |
| 100 |
int size = loadings.getColumnDimension(); |
| 101 |
getTruncatedMean(size, i, variance, midMean, mean); |
| 102 |
for (int j = 0; j <mean.length ; j++) { |
| 103 |
mean[j]*=pathParameter; |
| 104 |
} |
| 105 |
|
| 106 |
} |
| 107 |
|
| 108 |
private void copy(int i, double[] random) { |
| 109 |
MatrixParameterInterface changing = loadings; |
| 110 |
for (int j = 0; j < random.length; j++) { |
| 111 |
changing.setParameterValueQuietly(i, j, random[j]); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
private double getTruncatedDraw(int row, int column, NormalDistribution truncated, boolean actuallyDraw){ |
| 116 |
double lowCutoff; |
| 117 |
double highCutoff; |
| 118 |
double hastings; |
| 119 |
MatrixParameterInterface cutoff = (MatrixParameterInterface) ((MomentDistributionModel) prior).getCutoff(); |
| 120 |
lowCutoff = - Math.sqrt(cutoff.getParameterValue(row, column)); |
| 121 |
highCutoff = - lowCutoff; |
| 122 |
double low = truncated.cdf(lowCutoff); |
| 123 |
double high = truncated.cdf(highCutoff); |
| 124 |
double split = low / (low + (1-high)); |
| 125 |
double draw = 0; |
| 126 |
int count = 0; |
| 127 |
if(actuallyDraw) { |
| 128 |
while ((draw < highCutoff && draw > lowCutoff || Double.isNaN(draw)) && count < 10) { |
| 129 |
double rand = MathUtils.nextDouble(); |
| 130 |
if (rand < split) { |
| 131 |
draw = MathUtils.nextDouble() * low; |
| 132 |
draw = truncated.quantile(draw); |
| 133 |
} else { |
| 134 |
draw = MathUtils.nextDouble() * (1 - high) + high; |
| 135 |
draw = truncated.quantile(draw); |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
count++; |
| 140 |
} |
| 141 |
if (count < 10) { |
| 142 |
loadings.setParameterValue(row, column, draw); |
| 143 |
} |
| 144 |
} |
| 145 |
else |
| 146 |
draw = loadings.getParameterValue(row, column); |
| 147 |
if(Double.isNaN(draw) || Double.isNaN(Math.log(1- (high - low)))) |
| 148 |
hastings = Double.NEGATIVE_INFINITY; |
| 149 |
else |
| 150 |
hastings = truncated.logPdf(draw) - Math.log(1 - (high - low)); |
| 151 |
|
| 152 |
|
| 153 |
return hastings; |
| 154 |
} |
| 155 |
|
| 156 |
public double drawI(int i, int column, boolean actuallyDraw) { |
| 157 |
double[] draws = null; |
| 158 |
precisionArray = new double[loadings.getColumnDimension()][loadings.getColumnDimension()]; |
| 159 |
double[][] variance; |
| 160 |
meanMidArray = new double[loadings.getColumnDimension()]; |
| 161 |
meanArray = new double[loadings.getColumnDimension()]; |
| 162 |
double[][] cholesky = null; |
| 163 |
NormalDistribution conditioned; |
| 164 |
getPrecision(i, precisionArray); |
| 165 |
if(LFM.getLoadings().getParameterValue(i, column) != 0){ |
| 166 |
variance = (new SymmetricMatrix(precisionArray)).inverse().toComponents(); |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
getMean(i, variance, meanMidArray, meanArray); |
| 176 |
|
| 177 |
if(LFM.getFactorDimension() != 1) |
| 178 |
conditioned = getConditionalDistribution(meanArray, variance, column, i); |
| 179 |
else |
| 180 |
conditioned = new NormalDistribution(meanArray[0], Math.sqrt(variance[0][0])); |
| 181 |
} |
| 182 |
else |
| 183 |
conditioned = new NormalDistribution(0, Math.sqrt(1 / priorPrecision)); |
| 184 |
|
| 185 |
double hastings = 0; |
| 186 |
|
| 187 |
if(prior instanceof MomentDistributionModel){ |
| 188 |
if(MathUtils.nextDouble() < .5) { |
| 189 |
hastings = getTruncatedDraw(i, column, conditioned, actuallyDraw); |
| 190 |
|
| 191 |
} |
| 192 |
else{ |
| 193 |
|
| 194 |
hastings = getTruncatedDraw(i, column, conditioned, actuallyDraw); |
| 195 |
} |
| 196 |
} |
| 197 |
else{ |
| 198 |
loadings.setParameterValue(i, column, conditioned.quantile(MathUtils.nextDouble())); |
| 199 |
} |
| 200 |
return hastings; |
| 201 |
} |
| 202 |
|
| 203 |
private NormalDistribution getConditionalDistribution(double[] meanArray, double[][] variance, int column, int row) { |
| 204 |
double[][] newVariance = new double[meanArray.length - 1][meanArray.length - 1]; |
| 205 |
for (int i = 0; i < meanArray.length; i++) { |
| 206 |
for (int j = 0; j < meanArray.length; j++) { |
| 207 |
if(i < column && j < column){ |
| 208 |
newVariance[i][j] = variance[i][j]; |
| 209 |
} |
| 210 |
else if(i < column && j > column){ |
| 211 |
newVariance[i][j - 1] = variance[i][j]; |
| 212 |
} |
| 213 |
else if(i > column && j < column){ |
| 214 |
newVariance[i - 1][j] = variance[i][j]; |
| 215 |
} |
| 216 |
else if(i > column && j > column){ |
| 217 |
newVariance[i - 1][j - 1] = variance[i][j]; |
| 218 |
} |
| 219 |
else{} |
| 220 |
} |
| 221 |
} |
| 222 |
double[][] smallPrecision = (new SymmetricMatrix(newVariance)).inverse().toComponents(); |
| 223 |
double[] meanStore1 = new double[meanArray.length - 1]; |
| 224 |
double[] meanStore2 = new double[meanArray.length - 1]; |
| 225 |
double[] precStore = new double[meanArray.length - 1]; |
| 226 |
for (int i = 0; i < meanArray.length; i++) { |
| 227 |
if(i < column){ |
| 228 |
meanStore1[i] = LFM.getLoadings().getParameterValue(row, i) - meanArray[i]; |
| 229 |
} |
| 230 |
else if (i > column){ |
| 231 |
meanStore1[i - 1] = LFM.getLoadings().getParameterValue(row, i) - meanArray[i]; |
| 232 |
} |
| 233 |
else{} |
| 234 |
} |
| 235 |
for (int i = 0; i < meanArray.length - 1; i++) { |
| 236 |
for (int j = 0; j < meanArray.length - 1; j++) { |
| 237 |
meanStore2[i] += smallPrecision[i][j] * meanStore1[j]; |
| 238 |
} |
| 239 |
} |
| 240 |
double mean = meanArray[column]; |
| 241 |
for (int i = 0; i < meanArray.length - 1; i++) { |
| 242 |
if(i < column){ |
| 243 |
mean += meanStore2[i] * variance[i][column]; |
| 244 |
} |
| 245 |
else{ |
| 246 |
mean += meanStore2[i] * variance[i + 1][column]; |
| 247 |
} |
| 248 |
} |
| 249 |
for (int i = 0; i < meanArray.length - 1; i++) { |
| 250 |
for (int j = 0; j < meanArray.length - 1; j++) { |
| 251 |
if(i < column) |
| 252 |
precStore[i] += smallPrecision[i][j] * variance[j][column]; |
| 253 |
else |
| 254 |
precStore[i] += smallPrecision[i][j] * variance[j+1][column]; |
| 255 |
} |
| 256 |
|
| 257 |
} |
| 258 |
double varianceElement = variance[column][column]; |
| 259 |
for (int i = 0; i < meanArray.length - 1; i++) { |
| 260 |
if(i < column) |
| 261 |
varianceElement -= precStore[i] * variance[i][column]; |
| 262 |
else |
| 263 |
varianceElement -= precStore[i] * variance[i+1][column]; |
| 264 |
} |
| 265 |
|
| 266 |
return new NormalDistribution(mean, Math.sqrt(varianceElement)); |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
} |
| 271 |
|
| 272 |
void getCutoffDraw(int row, int column, NormalDistribution posteriorLoadings){ |
| 273 |
double loadingsCutoff = Math.abs(loadings.getParameterValue(row, column)); |
| 274 |
double draw = MathUtils.nextDouble() * loadingsCutoff; |
| 275 |
double cutoffVal = Math.sqrt(((MatrixParameterInterface) ((MomentDistributionModel) prior).getCutoff()).getParameterValue(row, column)); |
| 276 |
double top = cutoffPrior.getDistribution().pdf(Math.pow(draw,2)) / (1 - (posteriorLoadings.cdf(draw) - posteriorLoadings.cdf(-draw))); |
| 277 |
double bottom = cutoffPrior.getDistribution().pdf(Math.pow(cutoffVal, 2)) / (1 - (posteriorLoadings.cdf(cutoffVal) - posteriorLoadings.cdf(-cutoffVal))); |
| 278 |
|
| 279 |
|
| 280 |
if(MathUtils.nextDouble() < top / bottom){ |
| 281 |
((MatrixParameterInterface) ((MomentDistributionModel) prior).getCutoff()).setParameterValue(row, column, Math.pow(draw, 2)); |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
public int getStepCount() { |
| 291 |
return 0; |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
@Override |
| 316 |
public String getOperatorName() { |
| 317 |
return "loadingsGibbsTruncatedOperator"; |
| 318 |
} |
| 319 |
|
| 320 |
@Override |
| 321 |
public double doOperation() { |
| 322 |
|
| 323 |
int size = LFM.getLoadings().getRowDimension(); |
| 324 |
int column = MathUtils.nextInt(LFM.getLoadings().getColumnDimension()); |
| 325 |
for (int i = 0; i < size; i++) { |
| 326 |
drawI(i, column, true); |
| 327 |
} |
| 328 |
((Parameter) loadings).fireParameterChangedEvent(); |
| 329 |
return 0; |
| 330 |
} |
| 331 |
|
| 332 |
public void setPathParameter(double beta){ |
| 333 |
pathParameter=beta; |
| 334 |
} |
| 335 |
} |
| 336 |
|