Search in sources :

Example 1 with MomentDistributionModel

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);
}
Also used : MatrixParameterInterface(dr.inference.model.MatrixParameterInterface) LatentFactorModel(dr.inference.model.LatentFactorModel) MomentDistributionModel(dr.inference.distribution.MomentDistributionModel) LoadingsGibbsOperator(dr.inference.operators.LoadingsGibbsOperator) DistributionLikelihood(dr.inference.distribution.DistributionLikelihood) LoadingsGibbsTruncatedOperator(dr.inference.operators.LoadingsGibbsTruncatedOperator)

Example 2 with MomentDistributionModel

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;
}
Also used : NormalDistribution(dr.math.distributions.NormalDistribution) MomentDistributionModel(dr.inference.distribution.MomentDistributionModel) SymmetricMatrix(dr.math.matrixAlgebra.SymmetricMatrix)

Example 3 with MomentDistributionModel

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);
}
Also used : Parameter(dr.inference.model.Parameter) MomentDistributionModel(dr.inference.distribution.MomentDistributionModel)

Example 4 with MomentDistributionModel

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()));
    }
}
Also used : NormalDistribution(dr.math.distributions.NormalDistribution) MomentDistributionModel(dr.inference.distribution.MomentDistributionModel) SymmetricMatrix(dr.math.matrixAlgebra.SymmetricMatrix)

Example 5 with MomentDistributionModel

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);
}
Also used : AdaptationMode(dr.inference.operators.AdaptationMode) MatrixParameter(dr.inference.model.MatrixParameter) LatentFactorModel(dr.inference.model.LatentFactorModel) MomentDistributionModel(dr.inference.distribution.MomentDistributionModel)

Aggregations

MomentDistributionModel (dr.inference.distribution.MomentDistributionModel)6 LatentFactorModel (dr.inference.model.LatentFactorModel)3 DistributionLikelihood (dr.inference.distribution.DistributionLikelihood)2 MatrixParameterInterface (dr.inference.model.MatrixParameterInterface)2 NormalDistribution (dr.math.distributions.NormalDistribution)2 SymmetricMatrix (dr.math.matrixAlgebra.SymmetricMatrix)2 TreeDataLikelihood (dr.evomodel.treedatalikelihood.TreeDataLikelihood)1 IntegratedFactorAnalysisLikelihood (dr.evomodel.treedatalikelihood.continuous.IntegratedFactorAnalysisLikelihood)1 MatrixParameter (dr.inference.model.MatrixParameter)1 Parameter (dr.inference.model.Parameter)1 AdaptationMode (dr.inference.operators.AdaptationMode)1 LoadingsGibbsOperator (dr.inference.operators.LoadingsGibbsOperator)1 LoadingsGibbsTruncatedOperator (dr.inference.operators.LoadingsGibbsTruncatedOperator)1 FactorAnalysisOperatorAdaptor (dr.inference.operators.factorAnalysis.FactorAnalysisOperatorAdaptor)1 LoadingsGibbsTruncatedOperator (dr.inference.operators.factorAnalysis.LoadingsGibbsTruncatedOperator)1 NewLoadingsGibbsOperator (dr.inference.operators.factorAnalysis.NewLoadingsGibbsOperator)1