Search in sources :

Example 1 with NormalDistributionModel

use of dr.inference.distribution.NormalDistributionModel 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 2 with NormalDistributionModel

use of dr.inference.distribution.NormalDistributionModel 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)

Example 3 with NormalDistributionModel

use of dr.inference.distribution.NormalDistributionModel 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 4 with NormalDistributionModel

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

the class NormalDistributionModelParser method parseXMLObject.

public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    Parameter meanParam;
    Parameter scaleParam;
    Parameter precParam;
    XMLObject cxo = xo.getChild(MEAN);
    if (cxo.getChild(0) instanceof Parameter) {
        meanParam = (Parameter) cxo.getChild(Parameter.class);
    } else {
        meanParam = new Parameter.Default(cxo.getDoubleChild(0));
    }
    cxo = xo.getChild(STDEV);
    if (cxo == null) {
        cxo = xo.getChild(SCALE);
    }
    if (cxo != null) {
        if (cxo.getChild(0) instanceof Parameter) {
            scaleParam = (Parameter) cxo.getChild(Parameter.class);
        } else {
            scaleParam = new Parameter.Default(cxo.getDoubleChild(0));
        }
        return new NormalDistributionModel(meanParam, scaleParam);
    } else {
        cxo = xo.getChild(PREC);
        if (cxo.getChild(0) instanceof Parameter) {
            precParam = (Parameter) cxo.getChild(Parameter.class);
        } else {
            precParam = new Parameter.Default(cxo.getDoubleChild(0));
        }
    }
    return new NormalDistributionModel(meanParam, precParam, true);
}
Also used : NormalDistributionModel(dr.inference.distribution.NormalDistributionModel) Parameter(dr.inference.model.Parameter)

Aggregations

NormalDistributionModel (dr.inference.distribution.NormalDistributionModel)4 DistributionLikelihood (dr.inference.distribution.DistributionLikelihood)3 Parameter (dr.inference.model.Parameter)3 Attribute (dr.util.Attribute)3 ParametricDistributionModel (dr.inference.distribution.ParametricDistributionModel)2 ArrayList (java.util.ArrayList)2 MultivariateDistributionLikelihood (dr.inference.distribution.MultivariateDistributionLikelihood)1 MCLogger (dr.inference.loggers.MCLogger)1 MCMC (dr.inference.mcmc.MCMC)1 CompoundLikelihood (dr.inference.model.CompoundLikelihood)1 Likelihood (dr.inference.model.Likelihood)1 Variable (dr.inference.model.Variable)1 MCMCOperator (dr.inference.operators.MCMCOperator)1 ScaleOperator (dr.inference.operators.ScaleOperator)1 MultivariateNormalDistribution (dr.math.distributions.MultivariateNormalDistribution)1 NormalDistribution (dr.math.distributions.NormalDistribution)1 Transform (dr.util.Transform)1