Search in sources :

Example 6 with DistributionLikelihood

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

the class Tutorial1 method main.

public static void main(String[] arg) throws IOException, TraceException {
    // constructing random variable representing mean of normal distribution
    Variable.D mean = new Variable.D("mean", 1.0);
    // give mean a uniform prior [-1000, 1000]
    mean.addBounds(new Parameter.DefaultBounds(1000, -1000, 1));
    // constructing random variable representing stdev of normal distribution
    Variable.D stdev = new Variable.D("stdev", 1.0);
    // give stdev a uniform prior [0, 1000]
    stdev.addBounds(new Parameter.DefaultBounds(1000, 0, 1));
    // construct normal distribution model
    NormalDistributionModel normal = new NormalDistributionModel(mean, stdev);
    // construct a likelihood for normal distribution
    DistributionLikelihood likelihood = new DistributionLikelihood(normal);
    // construct data
    Attribute.Default<double[]> d = new Attribute.Default<double[]>("x", new double[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 });
    // add data (representing 9 independent observations) to likelihood
    likelihood.addData(d);
    // construct two "operators" to be used as the proposal distribution
    MCMCOperator meanMove = new ScaleOperator(mean, 0.75);
    MCMCOperator stdevMove = new ScaleOperator(stdev, 0.75);
    // construct a logger to log progress of MCMC run to stdout (screen)
    MCLogger logger1 = new MCLogger(100);
    logger1.add(mean);
    logger1.add(stdev);
    // construct a logger to log to a log file for later analysis
    MCLogger logger2 = new MCLogger("tutorial1.log", 100, false, 0);
    logger2.add(mean);
    logger2.add(stdev);
    // construct MCMC object
    MCMC mcmc = new MCMC("tutorial1:normal");
    // initialize MCMC with chain length, likelihood, operators and loggers
    mcmc.init(100000, likelihood, new MCMCOperator[] { meanMove, stdevMove }, new Logger[] { logger1, logger2 });
    // run the mcmc
    mcmc.chain();
    // perform post-analysis
    TraceAnalysis.report("tutorial1.log");
}
Also used : Variable(dr.inference.model.Variable) Attribute(dr.util.Attribute) MCMC(dr.inference.mcmc.MCMC) NormalDistributionModel(dr.inference.distribution.NormalDistributionModel) Parameter(dr.inference.model.Parameter) ScaleOperator(dr.inference.operators.ScaleOperator) DistributionLikelihood(dr.inference.distribution.DistributionLikelihood) MCMCOperator(dr.inference.operators.MCMCOperator) MCLogger(dr.inference.loggers.MCLogger)

Example 7 with DistributionLikelihood

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

the class TruncatedDistributionLikelihoodParser method parseXMLObject.

@Override
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    DistributionLikelihood likelihood = (DistributionLikelihood) xo.getChild(DistributionLikelihood.class);
    Parameter low;
    Parameter high;
    if (xo.getChild(LOW) != null) {
        low = (Parameter) xo.getChild(LOW).getChild(Parameter.class);
    } else {
        low = new Parameter.Default("low", Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY);
    }
    if (xo.getChild(HIGH) != null) {
        high = (Parameter) xo.getChild(HIGH).getChild(Parameter.class);
    } else {
        high = new Parameter.Default("high", Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY);
    }
    return new TruncatedDistributionLikelihood(likelihood, low, high);
}
Also used : TruncatedDistributionLikelihood(dr.inference.model.TruncatedDistributionLikelihood) Parameter(dr.inference.model.Parameter) TruncatedDistributionLikelihood(dr.inference.model.TruncatedDistributionLikelihood) DistributionLikelihood(dr.inference.distribution.DistributionLikelihood)

Example 8 with DistributionLikelihood

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

the class TwoPartsDistributionLikelihoodParser method parseXMLObject.

public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    DistributionLikelihood priorLikelihood = (DistributionLikelihood) xo.getElementFirstChild(PRIOR);
    DistributionLikelihood pseudoPriorLikelihood = (DistributionLikelihood) xo.getElementFirstChild(PSEUDO_PRIOR);
    Distribution prior = priorLikelihood.getDistribution();
    Distribution pseudoPrior = pseudoPriorLikelihood.getDistribution();
    Parameter bitVector = (Parameter) xo.getElementFirstChild(PARAMETER_VECTOR);
    int paramIndex = xo.getIntegerAttribute(PARAMETER_INDEX);
    Parameter selectedVariable = (Parameter) xo.getElementFirstChild(SELECTED_VARIABLE);
    TwoPartsDistributionLikelihood likelihood = new TwoPartsDistributionLikelihood(prior, pseudoPrior, bitVector, paramIndex);
    likelihood.addData(selectedVariable);
    return likelihood;
}
Also used : Distribution(dr.math.distributions.Distribution) Parameter(dr.inference.model.Parameter) TwoPartsDistributionLikelihood(dr.inference.distribution.TwoPartsDistributionLikelihood) TwoPartsDistributionLikelihood(dr.inference.distribution.TwoPartsDistributionLikelihood) DistributionLikelihood(dr.inference.distribution.DistributionLikelihood)

Example 9 with DistributionLikelihood

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

the class AppendedPotentialDerivativeParser method parseXMLObject.

@Override
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    List<GradientWrtParameterProvider> gradList = new ArrayList<GradientWrtParameterProvider>();
    // TODO Remove?
    List<Likelihood> likelihoodList = new ArrayList<Likelihood>();
    for (int i = 0; i < xo.getChildCount(); ++i) {
        Object obj = xo.getChild(i);
        GradientWrtParameterProvider grad;
        Likelihood likelihood;
        if (obj instanceof DistributionLikelihood) {
            DistributionLikelihood dl = (DistributionLikelihood) obj;
            if (!(dl.getDistribution() instanceof GradientProvider)) {
                throw new XMLParseException("Not a gradient provider");
            }
            throw new RuntimeException("Not yet implemented");
        } else if (obj instanceof MultivariateDistributionLikelihood) {
            final MultivariateDistributionLikelihood mdl = (MultivariateDistributionLikelihood) obj;
            if (!(mdl.getDistribution() instanceof GradientProvider)) {
                throw new XMLParseException("Not a gradient provider");
            }
            final GradientProvider provider = (GradientProvider) mdl.getDistribution();
            final Parameter parameter = mdl.getDataParameter();
            likelihood = mdl;
            grad = new GradientWrtParameterProvider.ParameterWrapper(provider, parameter, mdl);
        } else if (obj instanceof GradientWrtParameterProvider) {
            grad = (GradientWrtParameterProvider) obj;
            likelihood = grad.getLikelihood();
        } else {
            throw new XMLParseException("Not a Gaussian process");
        }
        gradList.add(grad);
        likelihoodList.add(likelihood);
    }
    return new CompoundGradient(gradList);
}
Also used : CompoundGradient(dr.inference.hmc.CompoundGradient) MultivariateDistributionLikelihood(dr.inference.distribution.MultivariateDistributionLikelihood) DistributionLikelihood(dr.inference.distribution.DistributionLikelihood) MultivariateDistributionLikelihood(dr.inference.distribution.MultivariateDistributionLikelihood) ArrayList(java.util.ArrayList) GradientWrtParameterProvider(dr.inference.hmc.GradientWrtParameterProvider) DistributionLikelihood(dr.inference.distribution.DistributionLikelihood) MultivariateDistributionLikelihood(dr.inference.distribution.MultivariateDistributionLikelihood)

Example 10 with DistributionLikelihood

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

the class GradientWrapperParser method parseXMLObject.

@Override
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    Object obj = xo.getChild(0);
    if (obj instanceof MultivariateDistributionLikelihood) {
        final MultivariateDistributionLikelihood mdl = (MultivariateDistributionLikelihood) xo.getChild(MultivariateDistributionLikelihood.class);
        if (!(mdl.getDistribution() instanceof GradientProvider)) {
            throw new XMLParseException("Not a gradient provider");
        }
        final GradientProvider provider = (GradientProvider) mdl.getDistribution();
        final Parameter parameter = mdl.getDataParameter();
        return new GradientWrtParameterProvider.ParameterWrapper(provider, parameter, mdl);
    } else {
        DistributionLikelihood dl = (DistributionLikelihood) obj;
        if (!(dl.getDistribution() instanceof GradientProvider)) {
            throw new XMLParseException("Not a gradient provider");
        }
        throw new RuntimeException("Not yet implemented");
    }
}
Also used : MultivariateDistributionLikelihood(dr.inference.distribution.MultivariateDistributionLikelihood) GradientProvider(dr.inference.model.GradientProvider) Parameter(dr.inference.model.Parameter) DistributionLikelihood(dr.inference.distribution.DistributionLikelihood) MultivariateDistributionLikelihood(dr.inference.distribution.MultivariateDistributionLikelihood)

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