use of dr.inference.distribution.MomentDistributionModel 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.distribution.MomentDistributionModel in project beast-mcmc by beast-dev.
the class LoadingsGibbsTruncatedOperator method drawI.
public double drawI(int i, int column, boolean actuallyDraw) {
double[] draws = null;
precisionArray = new double[loadings.getColumnDimension()][loadings.getColumnDimension()];
double[][] variance;
meanMidArray = new double[loadings.getColumnDimension()];
meanArray = new double[loadings.getColumnDimension()];
double[][] cholesky = null;
NormalDistribution conditioned;
getPrecision(i, precisionArray);
if (LFM.getLoadings().getParameterValue(i, column) != 0) {
variance = (new SymmetricMatrix(precisionArray)).inverse().toComponents();
// try {
// cholesky = new CholeskyDecomposition(variance).getL();
// } catch (IllegalDimension illegalDimension) {
// illegalDimension.printStackTrace();
// }
getMean(i, variance, meanMidArray, meanArray);
if (LFM.getFactorDimension() != 1)
conditioned = getConditionalDistribution(meanArray, variance, column, i);
else
conditioned = new NormalDistribution(meanArray[0], Math.sqrt(variance[0][0]));
} else
// TODO generify
conditioned = new NormalDistribution(0, Math.sqrt(1 / priorPrecision));
double hastings = 0;
if (prior instanceof MomentDistributionModel) {
if (MathUtils.nextDouble() < .5) {
hastings = getTruncatedDraw(i, column, conditioned, actuallyDraw);
// getCutoffDraw(i, column, conditioned);
} else {
// getCutoffDraw(i, column, conditioned);
hastings = getTruncatedDraw(i, column, conditioned, actuallyDraw);
}
} else {
loadings.setParameterValue(i, column, conditioned.quantile(MathUtils.nextDouble()));
}
return hastings;
}
use of dr.inference.distribution.MomentDistributionModel in project beast-mcmc by beast-dev.
the class MomentDistributionModelParser method parseXMLObject.
@Override
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
Parameter mean = (Parameter) xo.getChild(MEAN).getChild(0);
Parameter prec = (Parameter) xo.getChild(PREC).getChild(0);
Parameter cutoff = null;
if (xo.getChild(CUTOFF) != null) {
cutoff = (Parameter) xo.getChild(CUTOFF).getChild(0);
}
Parameter data = (Parameter) xo.getChild(DATA).getChild(0);
String id = xo.getId();
return new MomentDistributionModel(id, mean, prec, cutoff, data);
}
use of dr.inference.distribution.MomentDistributionModel in project beast-mcmc by beast-dev.
the class LoadingsGibbsTruncatedOperator method drawI.
private void drawI(int i, int column) {
double[] draws = null;
precisionArray = new double[loadings.getColumnDimension()][loadings.getColumnDimension()];
double[][] variance;
meanMidArray = new double[loadings.getColumnDimension()];
meanArray = new double[loadings.getColumnDimension()];
double[][] cholesky = null;
NormalDistribution conditioned;
getPrecision(i, precisionArray);
if (LFM.getLoadings().getParameterValue(i, column) != 0) {
variance = (new SymmetricMatrix(precisionArray)).inverse().toComponents();
// try {
// cholesky = new CholeskyDecomposition(variance).getL();
// } catch (IllegalDimension illegalDimension) {
// illegalDimension.printStackTrace();
// }
getMean(i, variance, meanMidArray, meanArray);
if (LFM.getFactorDimension() != 1)
conditioned = getConditionalDistribution(meanArray, variance, column, i);
else
conditioned = new NormalDistribution(meanArray[0], Math.sqrt(variance[0][0]));
} else
//TODO generify
conditioned = new NormalDistribution(0, Math.sqrt(1 / priorPrecision));
if (prior instanceof MomentDistributionModel) {
if (MathUtils.nextDouble() < .5) {
getTruncatedDraw(i, column, conditioned);
getCutoffDraw(i, column, conditioned);
} else {
getCutoffDraw(i, column, conditioned);
getTruncatedDraw(i, column, conditioned);
}
} else {
loadings.setParameterValue(i, column, conditioned.quantile(MathUtils.nextDouble()));
}
}
use of dr.inference.distribution.MomentDistributionModel in project beast-mcmc by beast-dev.
the class LoadingsHamiltonianMCParser method parseXMLObject.
@Override
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
LatentFactorModel lfm = (LatentFactorModel) xo.getChild(LatentFactorModel.class);
MomentDistributionModel prior = (MomentDistributionModel) xo.getChild(MomentDistributionModel.class);
double weight = xo.getDoubleAttribute(WEIGHT);
AdaptationMode mode = AdaptationMode.parseMode(xo);
int nSteps = xo.getIntegerAttribute(N_STEPS);
double stepSize = xo.getDoubleAttribute(STEP_SIZE);
double momentumSd = xo.getDoubleAttribute(MOMENTUM_SD);
MatrixParameter loadings = (MatrixParameter) xo.getChild(MatrixParameter.class);
return new LoadingsHamiltonianMC(lfm, prior, weight, mode, stepSize, nSteps, momentumSd, loadings);
}
Aggregations