use of dr.inference.model.MatrixParameterInterface in project beast-mcmc by beast-dev.
the class LoadingsGibbsOperatorParser method parseXMLObject.
@Override
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
String weightTemp = (String) xo.getAttribute(WEIGHT);
double weight = Double.parseDouble(weightTemp);
LatentFactorModel LFM = (LatentFactorModel) xo.getChild(LatentFactorModel.class);
DistributionLikelihood prior = (DistributionLikelihood) xo.getChild(DistributionLikelihood.class);
MomentDistributionModel prior2 = (MomentDistributionModel) xo.getChild(MomentDistributionModel.class);
DistributionLikelihood cutoffPrior = null;
if (xo.hasChildNamed(CUTOFF_PRIOR)) {
cutoffPrior = (DistributionLikelihood) xo.getChild(CUTOFF_PRIOR).getChild(DistributionLikelihood.class);
}
boolean randomScan = xo.getAttribute(RANDOM_SCAN, true);
int numThreads = xo.getAttribute(NUM_THREADS, 4);
MatrixParameterInterface loadings = null;
if (xo.getChild(MatrixParameterInterface.class) != null) {
loadings = (MatrixParameterInterface) xo.getChild(MatrixParameterInterface.class);
}
DistributionLikelihood WorkingPrior = null;
if (xo.getChild(WORKING_PRIOR) != null) {
System.out.println("here");
WorkingPrior = (DistributionLikelihood) xo.getChild(WORKING_PRIOR).getChild(DistributionLikelihood.class);
}
boolean multiThreaded = xo.getAttribute(MULTI_THREADED, false);
if (prior != null)
//To change body of implemented methods use File | Settings | File Templates.
return new LoadingsGibbsOperator(LFM, prior, weight, randomScan, WorkingPrior, multiThreaded, numThreads);
else
return new LoadingsGibbsTruncatedOperator(LFM, prior2, weight, randomScan, loadings, cutoffPrior);
}
use of dr.inference.model.MatrixParameterInterface in project beast-mcmc by beast-dev.
the class ModeFindOperator method doOperation.
public double doOperation() {
if (executeTimes < maxTimes) {
MatrixParameterInterface parameter = mm.getLikelihood().getMatrixParameter();
boolean sample = mode == CoercionMode.COERCION_ON || mode == CoercionMode.DEFAULT;
// double[] original = null;
//
// if (sample) {
// original = parameter.getParameterValues();
// }
// System.err.println("START");
mm.run(maxSteps);
// System.err.println("END");
++executeTimes;
// Always accept, breaks target distribution if called infinitely often
double logHR = Double.POSITIVE_INFINITY;
if (sample) {
System.err.println("Adaptive sampling: " + scaleFactor);
// double[] center = parameter.getParameterValues();
//
// double balance = 0.0;
// for (int i = 0; i < center.length; ++i) {
// final double epsilon = scaleFactor * MathUtils.nextGaussian();
// double x = center[i] + epsilon;
// final double old = original[i] - center[i];
// balance += epsilon * epsilon - old * old;
// }
//
// logHR = 0.5 * balance / (scaleFactor * scaleFactor);
// TODO Detailed balance is only met when mode finder run to convergence
logHR = 0.0;
}
return logHR;
} else {
throw new RuntimeException("Finished max times");
}
}
use of dr.inference.model.MatrixParameterInterface in project beast-mcmc by beast-dev.
the class LoadingsGibbsOperator method getTruncatedMean.
private void getTruncatedMean(int newRowDimension, int dataColumn, double[][] variance, double[] midMean, double[] mean) {
// MatrixParameter answer=new MatrixParameter(null);
// answer.setDimensions(this.getRowDimension(), Right.getRowDimension());
// System.out.println(answer.getRowDimension());
// System.out.println(answer.getColumnDimension());
MatrixParameterInterface data = LFM.getScaledData();
MatrixParameterInterface Left = LFM.getFactors();
int p = data.getColumnDimension();
for (int i = 0; i < newRowDimension; i++) {
double sum = 0;
for (int k = 0; k < p; k++) {
if (missingIndicator == null || missingIndicator.getParameterValue(k * LFM.getScaledData().getRowDimension() + dataColumn) != 1)
sum += Left.getParameterValue(i, k) * data.getParameterValue(dataColumn, k);
}
sum = sum * LFM.getColumnPrecision().getParameterValue(dataColumn, dataColumn);
sum += priorMeanPrecision;
midMean[i] = sum;
}
for (int i = 0; i < newRowDimension; i++) {
double sum = 0;
for (int k = 0; k < newRowDimension; k++) sum += variance[i][k] * midMean[k];
mean[i] = sum;
}
}
use of dr.inference.model.MatrixParameterInterface in project beast-mcmc by beast-dev.
the class LatentFactorLiabilityGibbsOperator method doUnorderedOperation.
void doUnorderedOperation() {
double[] LxF = lfm.getLxF();
DiagonalMatrix colPrec = (DiagonalMatrix) lfm.getColumnPrecision();
Parameter continuous = lfm.getContinuous();
MatrixParameterInterface lfmData = lfm.getScaledData();
for (int i = 0; i < lfmData.getColumnDimension(); i++) {
int LLpointer = 0;
int[] data = liabilityLikelihood.getData(i);
for (int index = 0; index < data.length; ++index) {
int datum = data[index];
Parameter numClasses = liabilityLikelihood.numClasses;
int dim = (int) numClasses.getParameterValue(index);
if (datum >= dim && continuous.getParameterValue(LLpointer) == 0) {
double draw = drawTruncatedNormalDistribution(LxF[i * lfmData.getRowDimension() + LLpointer], colPrec.getParameterValue(LLpointer, LLpointer), Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY);
lfmData.setParameterValue(LLpointer, i, draw);
}
if (dim == 1.0) {
if (continuous.getParameterValue(LLpointer) == 0) {
double draw = drawTruncatedNormalDistribution(LxF[i * lfmData.getRowDimension() + LLpointer], colPrec.getParameterValue(LLpointer, LLpointer), Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY);
lfmData.setParameterValue(LLpointer, i, draw);
}
LLpointer++;
} else if (dim == 2.0) {
if (datum == 0) {
double draw = drawTruncatedNormalDistribution(LxF[i * lfmData.getRowDimension() + LLpointer], colPrec.getParameterValue(LLpointer, LLpointer), Double.NEGATIVE_INFINITY, 0);
lfmData.setParameterValue(LLpointer, i, draw);
} else {
double draw = drawTruncatedNormalDistribution(LxF[i * lfmData.getRowDimension() + LLpointer], colPrec.getParameterValue(LLpointer, LLpointer), 0, Double.POSITIVE_INFINITY);
lfmData.setParameterValue(LLpointer, i, draw);
}
LLpointer++;
} else {
double[] trait = new double[dim];
trait[0] = 0.0;
if (datum == 0) {
for (int l = 0; l < dim - 1; l++) {
lfmData.setParameterValue(LLpointer + l, i, drawTruncatedNormalDistribution(LxF[i * lfmData.getRowDimension() + (LLpointer + l)], colPrec.getParameterValue((LLpointer + l), (LLpointer + l)), Double.NEGATIVE_INFINITY, 0));
}
} else {
trait[datum] = drawTruncatedNormalDistribution(LxF[i * lfmData.getRowDimension() + (LLpointer + datum - 1)], colPrec.getParameterValue((LLpointer + datum - 1), (LLpointer + datum - 1)), 0, Double.POSITIVE_INFINITY);
lfmData.setParameterValue(LLpointer + datum - 1, i, trait[datum]);
for (int l = 1; l < dim; l++) {
if (l != datum) {
// System.out.println("Free Rolls");
// System.out.println(LxF[i * lfmData.getRowDimension() + (LLpointer + l - 1)]);
trait[l] = drawTruncatedNormalDistribution(LxF[i * lfmData.getRowDimension() + (LLpointer + l - 1)], colPrec.getParameterValue((LLpointer + l - 1), (LLpointer + l - 1)), Double.NEGATIVE_INFINITY, trait[datum]);
lfmData.setParameterValue(LLpointer + l - 1, i, trait[l]);
}
}
// double max = Double.NEGATIVE_INFINITY;
// for (int j = 0; j < trait.length; j++) {
// if(max < trait[j]){
// max = trait[j];
// }
// }
// System.out.println("Constrained");
// System.out.println(LxF[i * lfmData.getRowDimension() + (LLpointer + datum - 1)]);
}
// valid = isMax(trait, datum);
LLpointer += dim - 1;
}
}
}
}
use of dr.inference.model.MatrixParameterInterface in project beast-mcmc by beast-dev.
the class LatentFactorLiabilityGibbsOperator method doOrderedOperation.
void doOrderedOperation() {
double[] LxF = lfm.getLxF();
DiagonalMatrix colPrec = (DiagonalMatrix) lfm.getColumnPrecision();
Parameter continuous = lfm.getContinuous();
MatrixParameterInterface lfmData = lfm.getScaledData();
Parameter threshold = liabilityLikelihood.getThreshold();
for (int i = 0; i < lfmData.getColumnDimension(); i++) {
int Thresholdpointer = 0;
int[] data = liabilityLikelihood.getData(i);
for (int index = 0; index < data.length; ++index) {
int datum = data[index];
Parameter numClasses = liabilityLikelihood.numClasses;
int dim = (int) numClasses.getParameterValue(index);
if (datum >= dim && continuous.getParameterValue(index) == 0) {
double draw = drawTruncatedNormalDistribution(LxF[i * lfmData.getRowDimension() + index], colPrec.getParameterValue(index, index), Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY);
lfmData.setParameterValue(index, i, draw);
} else {
if (dim == 1.0) {
if (continuous.getParameterValue(index) == 0) {
double draw = drawTruncatedNormalDistribution(LxF[i * lfmData.getRowDimension() + index], colPrec.getParameterValue(index, index), Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY);
lfmData.setParameterValue(index, i, draw);
}
} else if (dim == 2.0) {
if (datum == 0) {
double draw = drawTruncatedNormalDistribution(LxF[i * lfmData.getRowDimension() + index], colPrec.getParameterValue(index, index), Double.NEGATIVE_INFINITY, 0);
lfmData.setParameterValue(index, i, draw);
} else {
double draw = drawTruncatedNormalDistribution(LxF[i * lfmData.getRowDimension() + index], colPrec.getParameterValue(index, index), 0, Double.POSITIVE_INFINITY);
lfmData.setParameterValue(index, i, draw);
}
} else {
double[] thresholdList = new double[dim + 1];
thresholdList[0] = Double.NEGATIVE_INFINITY;
thresholdList[1] = 0;
thresholdList[dim] = Double.POSITIVE_INFINITY;
for (int j = 0; j < thresholdList.length - 3; j++) {
thresholdList[j + 2] = threshold.getParameterValue(Thresholdpointer + j);
}
Thresholdpointer += dim - 2;
double draw = drawTruncatedNormalDistribution(LxF[i * lfmData.getRowDimension() + index], colPrec.getParameterValue(index, index), thresholdList[datum], thresholdList[datum + 1]);
lfmData.setParameterValue(index, i, draw);
}
}
// valid = isMax(trait, datum);
}
}
}
Aggregations