Search in sources :

Example 61 with RealParameter

use of beast.core.parameter.RealParameter in project MultiTypeTree by tgvaughan.

the class SCSimTest method test.

@Test
public void test() throws Exception {
    System.out.println("SCSim test");
    Randomizer.setSeed(42);
    // Set up migration model.
    RealParameter rateMatrix = new RealParameter();
    rateMatrix.initByName("dimension", 2, "value", "0.1 0.1");
    RealParameter popSizes = new RealParameter();
    popSizes.initByName("value", "7.0 7.0");
    SCMigrationModel migrationModel = new SCMigrationModel();
    migrationModel.initByName("rateMatrix", rateMatrix, "popSizes", popSizes, "typeSet", new TypeSet("A", "B"));
    // Specify leaf types:
    IntegerParameter leafTypes = new IntegerParameter();
    leafTypes.initByName("value", "0 0 0");
    // Generate ensemble:
    int reps = 100000;
    double[] heights = new double[reps];
    for (int i = 0; i < reps; i++) {
        beast.evolution.tree.StructuredCoalescentMultiTypeTree sctree;
        sctree = new beast.evolution.tree.StructuredCoalescentMultiTypeTree();
        sctree.initByName("migrationModel", migrationModel, "leafTypes", leafTypes);
        heights[i] = sctree.getRoot().getHeight();
    }
    double meanHeights = DiscreteStatistics.mean(heights);
    double varHeights = DiscreteStatistics.variance(heights);
    boolean withinTol = (Math.abs(meanHeights - 19.2) < 0.2) && (Math.abs(varHeights - 310) < 20);
    Assert.assertTrue(withinTol);
}
Also used : IntegerParameter(beast.core.parameter.IntegerParameter) TypeSet(beast.evolution.tree.TypeSet) RealParameter(beast.core.parameter.RealParameter) SCMigrationModel(beast.evolution.tree.SCMigrationModel) Test(org.junit.Test)

Example 62 with RealParameter

use of beast.core.parameter.RealParameter in project beast2 by CompEvol.

the class CompoundPopulationFunction method prepare.

// why do we need this additional level on top of initAndValidate - does not seem to do anything?
@Override
public void prepare() {
    getParams();
    // is that safe???
    trees = treesInput.get();
    useMid = useMiddleInput.get();
    // used to work without upper case ???
    // errors?
    type = Type.valueOf(demographicTypeInput.get().toUpperCase());
    // set lengths
    int events = 0;
    for (TreeIntervals ti : trees) {
        // number of coalescent events
        events += ti.treeInput.get().getLeafNodeCount() - 1;
    }
    // all trees share time 0, need fixing for serial data
    events += type == Type.STEPWISE ? 0 : 1;
    try {
        if (popSizeParameter.getDimension() != events) {
            final RealParameter p = new RealParameter();
            p.initByName("value", popSizeParameter.getValue() + "", "upper", popSizeParameter.getUpper(), "lower", popSizeParameter.getLower(), "dimension", events);
            p.setID(popSizeParameter.getID());
            popSizeParameter.assignFromWithoutID(p);
        }
        if (indicatorsParameter.getDimension() != events - 1) {
            final BooleanParameter p = new BooleanParameter();
            p.initByName("value", "" + indicatorsParameter.getValue(), "dimension", events - 1);
            p.setID(indicatorsParameter.getID());
            indicatorsParameter.assignFrom(p);
        }
    } catch (Exception e) {
        // what to do?
        e.printStackTrace();
    }
    initInternals();
    for (int nt = 0; nt < trees.size(); ++nt) {
        setTreeTimes(nt);
    }
    mergeTreeTimes();
    setDemographicArrays();
    shadow = new Shadow();
}
Also used : RealParameter(beast.core.parameter.RealParameter) BooleanParameter(beast.core.parameter.BooleanParameter)

Example 63 with RealParameter

use of beast.core.parameter.RealParameter in project beast2 by CompEvol.

the class SampleOffValues method proposal.

@Override
public double proposal() {
    final BooleanParameter indicators = indicatorsInput.get(this);
    final RealParameter data = valuesInput.get(this);
    final ParametricDistribution distribution = distInput.get();
    final int idim = indicators.getDimension();
    final int offset = (data.getDimension() - 1) == idim ? 1 : 0;
    assert offset == 1 || data.getDimension() == idim : "" + idim + " (?+1) != " + data.getDimension();
    double hr = Double.NEGATIVE_INFINITY;
    if (scaleAll.get()) {
        for (int i = offset; i < idim; ++i) {
            if (!indicators.getValue(i - offset)) {
                try {
                    final double val = distribution.inverseCumulativeProbability(Randomizer.nextDouble());
                    hr += distribution.logDensity(data.getValue(i));
                    data.setValue(i, val);
                } catch (Exception e) {
                    // some distributions fail on extreme values - currently gamma
                    return Double.NEGATIVE_INFINITY;
                }
            }
        }
    } else {
        // available locations for direct sampling
        int[] loc = new int[idim];
        int locIndex = 0;
        for (int i = 0; i < idim; ++i) {
            if (!indicators.getValue(i)) {
                loc[locIndex] = i + offset;
                ++locIndex;
            }
        }
        if (locIndex > 0) {
            final int index = loc[Randomizer.nextInt(locIndex)];
            try {
                final double val = distribution.inverseCumulativeProbability(Randomizer.nextDouble());
                hr = distribution.logDensity(data.getValue(index));
                data.setValue(index, val);
            } catch (Exception e) {
                // some distributions fail on extreme values - currently gamma
                return Double.NEGATIVE_INFINITY;
            // throw new OperatorFailedException(e.getMessage());
            }
        } else {
        // no non-active indicators
        // return Double.NEGATIVE_INFINITY;
        }
    }
    return hr;
}
Also used : ParametricDistribution(beast.math.distributions.ParametricDistribution) RealParameter(beast.core.parameter.RealParameter) BooleanParameter(beast.core.parameter.BooleanParameter)

Example 64 with RealParameter

use of beast.core.parameter.RealParameter in project beast2 by CompEvol.

the class LoggerTest method testScreenLog.

@Test
public void testScreenLog() throws Exception {
    logger = new Logger();
    logger.initByName("fileName", "", "log", new RealParameter(new Double[] { 0.3, 0.7 }));
    logger.init();
    assertTrue("m_out is System.out", logger.getM_out() == System.out);
    logger.log(1);
    // TODO cannot extract content
    // close all file, except stdout
    logger.close();
    assertTrue("m_out is still System.out after close", logger.getM_out() == System.out);
}
Also used : RealParameter(beast.core.parameter.RealParameter) Logger(beast.core.Logger)

Example 65 with RealParameter

use of beast.core.parameter.RealParameter in project beast2 by CompEvol.

the class LoggerTest method testFileLog.

@Test
public void testFileLog() throws Exception {
    logger = new Logger();
    logger.initByName("fileName", "beast.log", "log", new RealParameter(new Double[] { 0.3, 0.7 }));
    File f_log = new File(logger.fileNameInput.get());
    if (f_log.exists()) {
        boolean log_deleted = f_log.delete();
        System.out.println("Delete log : " + f_log.getAbsolutePath() + " for testFileLog.");
    }
    logger.init();
    assertTrue("beast.log created successfully", f_log.exists());
    // rI >= 0
    int rI = new Random().nextInt(10000000);
    logger.log(-1);
    logger.log(rI);
    logger.close();
    // TODO cannot get "closing" status from PrintStream
    // assertNull("m_out is beast.log after close", logger.getM_out());
    BufferedReader in = new BufferedReader(new FileReader(f_log));
    // column names
    String line = in.readLine();
    // 1st sample
    String sample1 = in.readLine();
    String[] sp = sample1.split("\t", -1);
    assertFalse("check beast.log -1 not logged", sp[0].equals("-1"));
    assertEquals("check beast.log 1st sample", Integer.toString(rI), sp[0]);
}
Also used : Random(java.util.Random) RealParameter(beast.core.parameter.RealParameter) Logger(beast.core.Logger)

Aggregations

RealParameter (beast.core.parameter.RealParameter)97 Test (org.junit.Test)39 IntegerParameter (beast.core.parameter.IntegerParameter)23 Tree (beast.evolution.tree.Tree)16 State (beast.core.State)14 SCMigrationModel (beast.evolution.tree.SCMigrationModel)13 Alignment (beast.evolution.alignment.Alignment)11 TypeSet (beast.evolution.tree.TypeSet)11 MCMC (beast.core.MCMC)10 SiteModel (beast.evolution.sitemodel.SiteModel)10 Frequencies (beast.evolution.substitutionmodel.Frequencies)10 ConstantPopulation (beast.evolution.tree.coalescent.ConstantPopulation)10 StructuredCoalescentTreeDensity (multitypetree.distributions.StructuredCoalescentTreeDensity)10 TaxonSet (beast.evolution.alignment.TaxonSet)9 MultiTypeTreeStatLogger (multitypetree.util.MultiTypeTreeStatLogger)9 MultiTypeTreeFromNewick (beast.evolution.tree.MultiTypeTreeFromNewick)8 Node (beast.evolution.tree.Node)8 Operator (beast.core.Operator)7 RandomTree (beast.evolution.tree.RandomTree)7 Locus (bacter.Locus)6