当前位置: 首页>>代码示例>>Java>>正文


Java DistributionLikelihood.addData方法代码示例

本文整理汇总了Java中dr.inference.distribution.DistributionLikelihood.addData方法的典型用法代码示例。如果您正苦于以下问题:Java DistributionLikelihood.addData方法的具体用法?Java DistributionLikelihood.addData怎么用?Java DistributionLikelihood.addData使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在dr.inference.distribution.DistributionLikelihood的用法示例。


在下文中一共展示了DistributionLikelihood.addData方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: main

import dr.inference.distribution.DistributionLikelihood; //导入方法依赖的package包/类
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");
    }
 
开发者ID:beast-dev,项目名称:beast-mcmc,代码行数:52,代码来源:Tutorial1.java

示例2: main

import dr.inference.distribution.DistributionLikelihood; //导入方法依赖的package包/类
public static void main(String[] arg) {

        // Define normal model
        Parameter meanParameter = new Parameter.Default(1.0); // Starting value
        Variable<Double> stdev = new Variable.D(1.0, 1); // Fixed value
        ParametricDistributionModel densityModel = new NormalDistributionModel(meanParameter, stdev);
        DistributionLikelihood likelihood = new DistributionLikelihood(densityModel);

        // Define prior
        DistributionLikelihood prior = new DistributionLikelihood(new NormalDistribution(0.0, 1.0)); // Hyper-priors
        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);
    }
 
开发者ID:beast-dev,项目名称:beast-mcmc,代码行数:38,代码来源:SliceOperator.java

示例3: parseXMLObject

import dr.inference.distribution.DistributionLikelihood; //导入方法依赖的package包/类
public Object parseXMLObject(XMLObject xo) throws XMLParseException {

            double mean = xo.getDoubleAttribute(MEAN);
            double offset = xo.getDoubleAttribute(OFFSET);

            DistributionLikelihood likelihood = new DistributionLikelihood(new PoissonDistribution(mean), offset);
            for (int j = 0; j < xo.getChildCount(); j++) {
                if (xo.getChild(j) instanceof Statistic) {
                    likelihood.addData((Statistic) xo.getChild(j));
                } else {
                    throw new XMLParseException("illegal element in " + xo.getName() + " element");
                }
            }

            return likelihood;
        }
 
开发者ID:whdc,项目名称:ieo-beast,代码行数:17,代码来源:PriorParsers.java

示例4: parseXMLObject

import dr.inference.distribution.DistributionLikelihood; //导入方法依赖的package包/类
public Object parseXMLObject(XMLObject xo) throws XMLParseException {

            double mean = xo.getDoubleAttribute(MEAN);
            double stdev = xo.getDoubleAttribute(STDEV);

            DistributionLikelihood likelihood = new DistributionLikelihood(new NormalDistribution(mean, stdev));
            for (int j = 0; j < xo.getChildCount(); j++) {
                if (xo.getChild(j) instanceof Statistic) {
                    likelihood.addData((Statistic) xo.getChild(j));
                } else if (!(xo.getChild(j) instanceof LatentTruncation)) {
                    throw new XMLParseException("Illegal element in " + xo.getName() + " element");
                }
            }

            LatentTruncation truncation = (LatentTruncation) xo.getChild(LatentTruncation.class);

            return new TruncatedWorkingDistribution(likelihood, truncation);
        }
 
开发者ID:beast-dev,项目名称:beast-mcmc,代码行数:19,代码来源:TruncatedWorkingDistribution.java

示例5: testLognormalPrior

import dr.inference.distribution.DistributionLikelihood; //导入方法依赖的package包/类
public void testLognormalPrior() {
//        ConstantPopulation constant = new ConstantPopulation(Units.Type.YEARS);
//        constant.setN0(popSize); // popSize
        Parameter popSize = new Parameter.Default(6.0);
        popSize.setId(ConstantPopulationModelParser.POPULATION_SIZE);
        ConstantPopulationModel demo = new ConstantPopulationModel(popSize, Units.Type.YEARS);

        //Likelihood
        Likelihood dummyLikelihood = new DummyLikelihood(demo);

        // Operators
        OperatorSchedule schedule = new SimpleOperatorSchedule();

        MCMCOperator operator = new ScaleOperator(popSize, 0.75);
        operator.setWeight(1.0);
        schedule.addOperator(operator);

        // Log
        ArrayLogFormatter formatter = new ArrayLogFormatter(false);

        MCLogger[] loggers = new MCLogger[2];
        loggers[0] = new MCLogger(formatter, 1000, false);
//        loggers[0].add(treeLikelihood);
        loggers[0].add(popSize);

        loggers[1] = new MCLogger(new TabDelimitedFormatter(System.out), 100000, false);
//        loggers[1].add(treeLikelihood);
        loggers[1].add(popSize);

        // MCMC
        MCMC mcmc = new MCMC("mcmc1");
        MCMCOptions options = new MCMCOptions();
        options.setChainLength(1000000);
        options.setUseCoercion(true); // autoOptimize = true
        options.setCoercionDelay(100);
        options.setTemperature(1.0);
        options.setFullEvaluationCount(2000);

        DistributionLikelihood logNormalLikelihood = new DistributionLikelihood(new LogNormalDistribution(1.0, 1.0), 0); // meanInRealSpace="false"
        logNormalLikelihood.addData(popSize);

        List<Likelihood> likelihoods = new ArrayList<Likelihood>();
        likelihoods.add(logNormalLikelihood);
        Likelihood prior = new CompoundLikelihood(0, likelihoods);

        likelihoods.clear();
        likelihoods.add(dummyLikelihood);
        Likelihood likelihood = new CompoundLikelihood(-1, likelihoods);

        likelihoods.clear();
        likelihoods.add(prior);
        likelihoods.add(likelihood);
        Likelihood posterior = new CompoundLikelihood(0, likelihoods);

        mcmc.setShowOperatorAnalysis(true);
        mcmc.init(options, posterior, schedule, loggers);
        mcmc.run();

        // time
        System.out.println(mcmc.getTimer().toString());

        // Tracer
        List<Trace> traces = formatter.getTraces();
        ArrayTraceList traceList = new ArrayTraceList("LognormalPriorTest", traces, 0);

        for (int i = 1; i < traces.size(); i++) {
            traceList.analyseTrace(i);
        }

//      <expectation name="param" value="4.48168907"/>

        TraceCorrelation popSizeStats = traceList.getCorrelationStatistics(traceList.getTraceIndex(ConstantPopulationModelParser.POPULATION_SIZE));

        System.out.println("Expectation of Log-Normal(1,1) is e^(M+S^2/2) = e^(1.5) = " + Math.exp(1.5));
        assertExpectation(ConstantPopulationModelParser.POPULATION_SIZE, popSizeStats, Math.exp(1.5));
    }
 
开发者ID:whdc,项目名称:ieo-beast,代码行数:77,代码来源:LognormalPriorTest.java

示例6: parseXMLObject

import dr.inference.distribution.DistributionLikelihood; //导入方法依赖的package包/类
public Object parseXMLObject(XMLObject xo) throws XMLParseException {

        final XMLObject cxo = xo.getChild(DISTRIBUTION);
        ParametricDistributionModel model = (ParametricDistributionModel) cxo.getChild(ParametricDistributionModel.class);

        DistributionLikelihood likelihood = new DistributionLikelihood(model);

        XMLObject cxo1 = xo.getChild(DATA);
        final int from = cxo1.getAttribute(FROM, -1);
        int to = cxo1.getAttribute(TO, -1);
        if (from >= 0 || to >= 0) {
            if (to < 0) {
                to = Integer.MAX_VALUE;
            }
            if (!(from >= 0 && to >= 0 && from < to)) {
                throw new XMLParseException("ill formed from-to");
            }
            likelihood.setRange(from, to);
        }

        for (int j = 0; j < cxo1.getChildCount(); j++) {
            if (cxo1.getChild(j) instanceof Statistic) {

                likelihood.addData((Statistic) cxo1.getChild(j));
            } else {
                throw new XMLParseException("illegal element in " + cxo1.getName() + " element");
            }
        }

        return likelihood;
    }
 
开发者ID:beast-dev,项目名称:beast-mcmc,代码行数:32,代码来源:DistributionLikelihoodParser.java

示例7: main

import dr.inference.distribution.DistributionLikelihood; //导入方法依赖的package包/类
/**
 * Test KDE construction from a BEAST log file
 * @param args 0: BEAST log file; 1: trace to construct KDE; 2: burn-in
 */
public static void main(String[] args) {

    File file = new File(args[0]);
    String parent = file.getParent();
    file = new File(parent, args[0]);
    String fileName = file.getAbsolutePath();

    try {
        LogFileTraces traces = new LogFileTraces(fileName, file);
        traces.loadTraces();
        long maxState = traces.getMaxState();

        long burnin = Long.parseLong(args[2]);
        traces.setBurnIn(burnin);

        int traceIndexParameter = -1;
        for (int i = 0; i < traces.getTraceCount(); i++) {
            String traceName = traces.getTraceName(i);
            if (traceName.trim().equals(args[1])) {
                traceIndexParameter = i;
            }
        }

        Double[] parameterSamples = new Double[traces.getStateCount()];
        DistributionLikelihood likelihood = new DistributionLikelihood(new LogTransformedNormalKDEDistribution((Double[]) traces.getValues(traceIndexParameter).toArray(parameterSamples)));
        //DistributionLikelihood likelihood = new DistributionLikelihood(new NormalKDEDistribution((Double[]) traces.getValues(traceIndexParameter).toArray(parameterSamples)));
        //DistributionLikelihood likelihood = new DistributionLikelihood(new GammaKDEDistribution((Double[]) traces.getValues(traceIndexParameter).toArray(parameterSamples)));

        Parameter parameter = new Parameter.Default(args[1]);
        parameter.setDimension(1);

        likelihood.addData(parameter);

        for (Double d : parameterSamples) {
            parameter.setParameterValue(0, d);
            System.out.println(d + " > " + likelihood.calculateLogLikelihood());
        }

    } catch(IOException ioe) {
        System.out.println(ioe);
    } catch(TraceException te) {
        System.out.println(te);
    }

}
 
开发者ID:beast-dev,项目名称:beast-mcmc,代码行数:50,代码来源:KDEDistributionTest.java

示例8: testLognormalPrior

import dr.inference.distribution.DistributionLikelihood; //导入方法依赖的package包/类
public void testLognormalPrior() {
//        ConstantPopulation constant = new ConstantPopulation(Units.Type.YEARS);
//        constant.setN0(popSize); // popSize
        Parameter popSize = new Parameter.Default(6.0);
        popSize.setId(ConstantPopulationModelParser.POPULATION_SIZE);
        ConstantPopulationModel demo = new ConstantPopulationModel(popSize, Units.Type.YEARS);

        //Likelihood
        Likelihood dummyLikelihood = new DummyLikelihood(demo);

        // Operators
        OperatorSchedule schedule = new SimpleOperatorSchedule();

        MCMCOperator operator = new ScaleOperator(popSize, 0.75);
        operator.setWeight(1.0);
        schedule.addOperator(operator);

        // Log
        ArrayLogFormatter formatter = new ArrayLogFormatter(false);

        MCLogger[] loggers = new MCLogger[2];
        loggers[0] = new MCLogger(formatter, 1000, false);
//        loggers[0].add(treeLikelihood);
        loggers[0].add(popSize);

        loggers[1] = new MCLogger(new TabDelimitedFormatter(System.out), 100000, false);
//        loggers[1].add(treeLikelihood);
        loggers[1].add(popSize);

        // MCMC
        MCMC mcmc = new MCMC("mcmc1");
        MCMCOptions options = new MCMCOptions(1000000);

        DistributionLikelihood logNormalLikelihood = new DistributionLikelihood(new LogNormalDistribution(1.0, 1.0), 0); // meanInRealSpace="false"
        logNormalLikelihood.addData(popSize);

        List<Likelihood> likelihoods = new ArrayList<Likelihood>();
        likelihoods.add(logNormalLikelihood);
        Likelihood prior = new CompoundLikelihood(0, likelihoods);

        likelihoods.clear();
        likelihoods.add(dummyLikelihood);
        Likelihood likelihood = new CompoundLikelihood(-1, likelihoods);

        likelihoods.clear();
        likelihoods.add(prior);
        likelihoods.add(likelihood);
        Likelihood posterior = new CompoundLikelihood(0, likelihoods);

        mcmc.setShowOperatorAnalysis(true);
        mcmc.init(options, posterior, schedule, loggers);
        mcmc.run();

        // time
        System.out.println(mcmc.getTimer().toString());

        // Tracer
        List<Trace> traces = formatter.getTraces();
        ArrayTraceList traceList = new ArrayTraceList("LognormalPriorTest", traces, 0);

        for (int i = 1; i < traces.size(); i++) {
            traceList.analyseTrace(i);
        }

//      <expectation name="param" value="4.48168907"/>

        TraceCorrelation popSizeStats = traceList.getCorrelationStatistics(traceList.getTraceIndex(ConstantPopulationModelParser.POPULATION_SIZE));

        System.out.println("Expectation of Log-Normal(1,1) is e^(M+S^2/2) = e^(1.5) = " + Math.exp(1.5));
        assertExpectation(ConstantPopulationModelParser.POPULATION_SIZE, popSizeStats, Math.exp(1.5));
    }
 
开发者ID:beast-dev,项目名称:beast-mcmc,代码行数:72,代码来源:LognormalPriorTest.java

示例9: main

import dr.inference.distribution.DistributionLikelihood; //导入方法依赖的package包/类
public static void main(String[] arg) {

        // Define normal model
        Parameter thetaParameter = new Parameter.Default(new double[]{1.0, 0.0});  // Starting values
        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]));
        }
    }
 
开发者ID:beast-dev,项目名称:beast-mcmc,代码行数:67,代码来源:EllipticalSliceOperator.java

示例10: main

import dr.inference.distribution.DistributionLikelihood; //导入方法依赖的package包/类
public static void main(String[] arg) {

        // Define normal model
        Parameter meanParameter = new Parameter.Default(1.0); // Starting value
        Variable<Double> stdev = new Variable.D(1.0, 1); // Fixed value
        ParametricDistributionModel densityModel = new NormalDistributionModel(meanParameter, stdev);
        DistributionLikelihood likelihood = new DistributionLikelihood(densityModel);

        // Define prior
        DistributionLikelihood prior = new DistributionLikelihood(new NormalDistribution(0.0, 1.0)); // Hyper-priors
        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++) {
            try {
                sliceSampler.doOperation(null, posterior);
            } catch (OperatorFailedException e) {
                System.err.println(e.getMessage());
            }
            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);
    }
 
开发者ID:whdc,项目名称:ieo-beast,代码行数:42,代码来源:SliceOperator.java


注:本文中的dr.inference.distribution.DistributionLikelihood.addData方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。