Search in sources :

Example 1 with DistributionLikelihood

use of dr.inference.distribution.DistributionLikelihood in project beast-mcmc by beast-dev.

the class LatentFactorModelPrecisionGibbsOperatorParser 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);
    boolean randomScan = xo.getAttribute(RANDOM_SCAN, true);
    return new LatentFactorModelPrecisionGibbsOperator(LFM, prior, weight, randomScan);
}
Also used : LatentFactorModel(dr.inference.model.LatentFactorModel) LatentFactorModelPrecisionGibbsOperator(dr.inference.operators.LatentFactorModelPrecisionGibbsOperator) DistributionLikelihood(dr.inference.distribution.DistributionLikelihood)

Example 2 with DistributionLikelihood

use of dr.inference.distribution.DistributionLikelihood 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 3 with DistributionLikelihood

use of dr.inference.distribution.DistributionLikelihood in project beast-mcmc by beast-dev.

the class LoadingsIndependenceOperatorParser method parseXMLObject.

@Override
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    CoercionMode mode = CoercionMode.parseMode(xo);
    String scaleFactorTemp = (String) xo.getAttribute(SCALE_FACTOR);
    double scaleFactor = Double.parseDouble(scaleFactorTemp);
    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);
    boolean randomScan = xo.getAttribute(RANDOM_SCAN, true);
    //To change body of implemented methods use File | Settings | File Templates.
    return new LoadingsIndependenceOperator(LFM, prior, weight, randomScan, scaleFactor, mode);
}
Also used : LoadingsIndependenceOperator(dr.inference.operators.LoadingsIndependenceOperator) LatentFactorModel(dr.inference.model.LatentFactorModel) CoercionMode(dr.inference.operators.CoercionMode) DistributionLikelihood(dr.inference.distribution.DistributionLikelihood)

Example 4 with DistributionLikelihood

use of dr.inference.distribution.DistributionLikelihood in project beast-mcmc by beast-dev.

the class EllipticalSliceOperator method main.

public static void main(String[] arg) {
    // Define normal model
    // Starting values
    Parameter thetaParameter = new Parameter.Default(new double[] { 1.0, 0.0 });
    MaskedParameter meanParameter = new MaskedParameter(thetaParameter, new Parameter.Default(new double[] { 1.0, 0.0 }), true);
    TransformedParameter precParameter = new TransformedParameter(new MaskedParameter(thetaParameter, new Parameter.Default(new double[] { 0.0, 1.0 }), true), new Transform.LogTransform(), true);
    //        System.err.println(thetaParameter);
    //        System.err.println(meanParameter);
    //        System.err.println(precParameter);
    ParametricDistributionModel densityModel = new NormalDistributionModel(meanParameter, precParameter, true);
    DistributionLikelihood likelihood = new DistributionLikelihood(densityModel);
    // Define prior
    MultivariateNormalDistribution priorDistribution = new MultivariateNormalDistribution(new double[] { 0.0, 0.0 }, new double[][] { { 0.001, 0.0 }, { 0.0, 0.001 } });
    MultivariateDistributionLikelihood prior = new MultivariateDistributionLikelihood(priorDistribution);
    prior.addData(thetaParameter);
    // Define data
    //        likelihood.addData(new Attribute.Default<double[]>("Data", new double[] {0.0, 2.0, 4.0}));
    likelihood.addData(new Attribute.Default<double[]>("Data", new double[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }));
    List<Likelihood> list = new ArrayList<Likelihood>();
    list.add(likelihood);
    list.add(prior);
    CompoundLikelihood posterior = new CompoundLikelihood(0, list);
    EllipticalSliceOperator sliceSampler = new EllipticalSliceOperator(thetaParameter, priorDistribution, false, true);
    final int dim = thetaParameter.getDimension();
    final int length = 100000;
    double[] mean = new double[dim];
    double[] variance = new double[dim];
    Parameter[] log = new Parameter[dim];
    log[0] = meanParameter;
    log[1] = precParameter;
    for (int i = 0; i < length; i++) {
        sliceSampler.doOperation(posterior);
        for (int j = 0; j < dim; ++j) {
            double x = log[j].getValue(0);
            mean[j] += x;
            variance[j] += x * x;
        }
    }
    for (int j = 0; j < dim; ++j) {
        mean[j] /= length;
        variance[j] /= length;
        variance[j] -= mean[j] * mean[j];
    }
    System.out.println("E(x)\tStErr(x)");
    for (int j = 0; j < dim; ++j) {
        System.out.println(mean[j] + " " + Math.sqrt(variance[j]));
    }
}
Also used : MultivariateDistributionLikelihood(dr.inference.distribution.MultivariateDistributionLikelihood) Attribute(dr.util.Attribute) DistributionLikelihood(dr.inference.distribution.DistributionLikelihood) MultivariateDistributionLikelihood(dr.inference.distribution.MultivariateDistributionLikelihood) ArrayList(java.util.ArrayList) MultivariateNormalDistribution(dr.math.distributions.MultivariateNormalDistribution) ParametricDistributionModel(dr.inference.distribution.ParametricDistributionModel) NormalDistributionModel(dr.inference.distribution.NormalDistributionModel) Transform(dr.util.Transform) DistributionLikelihood(dr.inference.distribution.DistributionLikelihood) MultivariateDistributionLikelihood(dr.inference.distribution.MultivariateDistributionLikelihood)

Example 5 with DistributionLikelihood

use of dr.inference.distribution.DistributionLikelihood in project beast-mcmc by beast-dev.

the class SliceOperator method main.

public static void main(String[] arg) {
    // Define normal model
    // Starting value
    Parameter meanParameter = new Parameter.Default(1.0);
    // Fixed value
    Variable<Double> stdev = new Variable.D(1.0, 1);
    ParametricDistributionModel densityModel = new NormalDistributionModel(meanParameter, stdev);
    DistributionLikelihood likelihood = new DistributionLikelihood(densityModel);
    // Define prior
    // Hyper-priors
    DistributionLikelihood prior = new DistributionLikelihood(new NormalDistribution(0.0, 1.0));
    prior.addData(meanParameter);
    // Define data
    likelihood.addData(new Attribute.Default<double[]>("Data", new double[] { 0.0, 1.0, 2.0 }));
    List<Likelihood> list = new ArrayList<Likelihood>();
    list.add(likelihood);
    list.add(prior);
    CompoundLikelihood posterior = new CompoundLikelihood(0, list);
    SliceOperator sliceSampler = new SliceOperator(meanParameter);
    final int length = 10000;
    double mean = 0;
    double variance = 0;
    for (int i = 0; i < length; i++) {
        sliceSampler.doOperation(posterior);
        double x = meanParameter.getValue(0);
        mean += x;
        variance += x * x;
    }
    mean /= length;
    variance /= length;
    variance -= mean * mean;
    System.out.println("E(x) = " + mean);
    System.out.println("V(x) = " + variance);
}
Also used : Attribute(dr.util.Attribute) CompoundLikelihood(dr.inference.model.CompoundLikelihood) Likelihood(dr.inference.model.Likelihood) DistributionLikelihood(dr.inference.distribution.DistributionLikelihood) CompoundLikelihood(dr.inference.model.CompoundLikelihood) ArrayList(java.util.ArrayList) NormalDistribution(dr.math.distributions.NormalDistribution) ParametricDistributionModel(dr.inference.distribution.ParametricDistributionModel) NormalDistributionModel(dr.inference.distribution.NormalDistributionModel) Parameter(dr.inference.model.Parameter) DistributionLikelihood(dr.inference.distribution.DistributionLikelihood)

Aggregations

DistributionLikelihood (dr.inference.distribution.DistributionLikelihood)18 ArrayList (java.util.ArrayList)9 Parameter (dr.inference.model.Parameter)7 MCLogger (dr.inference.loggers.MCLogger)5 MCMC (dr.inference.mcmc.MCMC)5 Likelihood (dr.inference.model.Likelihood)5 MultivariateDistributionLikelihood (dr.inference.distribution.MultivariateDistributionLikelihood)4 ArrayLogFormatter (dr.inference.loggers.ArrayLogFormatter)4 MCMCOptions (dr.inference.mcmc.MCMCOptions)4 CompoundLikelihood (dr.inference.model.CompoundLikelihood)4 NormalDistributionModel (dr.inference.distribution.NormalDistributionModel)3 ParametricDistributionModel (dr.inference.distribution.ParametricDistributionModel)3 TabDelimitedFormatter (dr.inference.loggers.TabDelimitedFormatter)3 LatentFactorModel (dr.inference.model.LatentFactorModel)3 ArrayTraceList (dr.inference.trace.ArrayTraceList)3 Trace (dr.inference.trace.Trace)3 TraceCorrelation (dr.inference.trace.TraceCorrelation)3 Attribute (dr.util.Attribute)3 Taxa (dr.evolution.util.Taxa)2 Taxon (dr.evolution.util.Taxon)2