Search in sources :

Example 16 with IntegerParameter

use of beast.core.parameter.IntegerParameter in project bacter by tgvaughan.

the class SkylinePopulationFunction method main.

/**
 * Main method for testing.
 *
 * @param args command line arguments (unused)
 */
public static void main(String[] args) throws Exception {
    String acgString = "[&15,0,1.3905355989030808,31,770,1.597708055397074] " + "[&30,931,2.4351280458424904,36,2486,3.78055549386568] " + "[&15,941,2.0439957300083322,38,2364,6.911056700367016] " + "[&36,1091,4.285505683622974,38,2589,9.867725913197855] " + "((((10:0.5385300170206817,(17:0.116794353049212," + "((3:0.039229346597297564,12:0.039229346597297564)23:0.04582913870888949," + "13:0.08505848530618705)24:0.03173586774302495)26:0.4217356639714697)28:1.8114199763246093," + "((8:0.10883006062265468,2:0.10883006062265468)25:0.556428062025291," + "(6:0.5393311342677402,11:0.5393311342677402)29:0.12592698838020555)31:1.6846918706973453)34:1.4536824928125807," + "(1:0.47184545557390367,14:0.47184545557390367)27:3.331787030583968)37:2.9704369411362554," + "(((15:2.0624287390593707,((16:0.01825347077733299,19:0.01825347077733299)21:0.7668749128372041," + "(7:0.008018731329538273,9:0.008018731329538273)20:0.7771096522849988)32:1.2773003554448337)33:0.7487092404613747," + "4:2.8111379795207454)35:0.1331794525400949,((0:0.0243537216663141," + "5:0.0243537216663141)22:0.5681537100482162,18:0.5925074317145304)30:2.35181000034631)36:3.829751995233287)38:0.0";
    ConversionGraph acg = new ConversionGraph();
    acg.initByName("siteCount", 10000, "fromString", acgString);
    SkylinePopulationFunction skyline = new SkylinePopulationFunction();
    skyline.initByName("acg", acg, "popSizes", new RealParameter("1.0 1.0 5.0 1.0 2.0"), "groupSizes", new IntegerParameter("0"), "piecewiseLinear", true);
    try (PrintStream ps = new PrintStream("out.txt")) {
        ps.println("t N intensity intensityInv");
        double t = 0.0;
        while (t < 10) {
            ps.format("%g %g %g %g\n", t, skyline.getPopSize(t), skyline.getIntensity(t), skyline.getInverseIntensity(skyline.getIntensity(t)));
            t += 0.001;
        }
        ps.close();
    }
}
Also used : IntegerParameter(beast.core.parameter.IntegerParameter) PrintStream(java.io.PrintStream) RealParameter(beast.core.parameter.RealParameter) ConversionGraph(bacter.ConversionGraph)

Example 17 with IntegerParameter

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

the class ExcludablePrior method initAndValidate.

@Override
public void initAndValidate() {
    super.initAndValidate();
    Function x = m_x.get();
    if (x instanceof RealParameter || x instanceof IntegerParameter) {
        if (x.getDimension() != xIncludeInput.get().getDimension())
            throw new IllegalArgumentException("Length of xInclude does " + "not match length of x.");
    }
}
Also used : Function(beast.core.Function) IntegerParameter(beast.core.parameter.IntegerParameter) RealParameter(beast.core.parameter.RealParameter)

Example 18 with IntegerParameter

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

the class ExcludablePrior method calculateLogP.

@Override
public double calculateLogP() {
    Function x = m_x.get();
    if (x instanceof RealParameter || x instanceof IntegerParameter) {
        // test that parameter is inside its bounds
        double l = 0.0;
        double h = 0.0;
        if (x instanceof RealParameter) {
            l = ((RealParameter) x).getLower();
            h = ((RealParameter) x).getUpper();
        } else {
            l = ((IntegerParameter) x).getLower();
            h = ((IntegerParameter) x).getUpper();
        }
        for (int i = 0; i < x.getDimension(); i++) {
            if (!xIncludeInput.get().getValue(i))
                continue;
            double value = x.getArrayValue(i);
            if (value < l || value > h) {
                return Double.NEGATIVE_INFINITY;
            }
        }
    }
    // Inline modified version of ParametricDistribution.calcLogP()
    final double fOffset = distInput.get().offsetInput.get();
    logP = 0;
    for (int i = 0; i < x.getDimension(); i++) {
        if (!xIncludeInput.get().getValue(i))
            continue;
        final double fX = x.getArrayValue(i) - fOffset;
        logP += distInput.get().logDensity(fX);
    }
    return logP;
}
Also used : Function(beast.core.Function) IntegerParameter(beast.core.parameter.IntegerParameter) RealParameter(beast.core.parameter.RealParameter)

Example 19 with IntegerParameter

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

the class StructuredCoalescentMultiTypeTree method main.

/**
 * Generates an ensemble of trees from the structured coalescent for testing
 * coloured tree-space samplers.
 *
 * @param argv
 * @throws java.lang.Exception
 */
public static void main(String[] argv) throws Exception {
    // Set up migration model.
    RealParameter rateMatrix = new RealParameter();
    rateMatrix.initByName("value", "0.05", "dimension", "12");
    RealParameter popSizes = new RealParameter();
    popSizes.initByName("value", "7.0", "dimension", "4");
    SCMigrationModel migrationModel = new SCMigrationModel();
    migrationModel.initByName("rateMatrix", rateMatrix, "popSizes", popSizes);
    // Specify leaf types:
    IntegerParameter leafTypes = new IntegerParameter();
    leafTypes.initByName("value", "0 0 0");
    // Generate ensemble:
    int reps = 1000000;
    double[] heights = new double[reps];
    double[] changes = new double[reps];
    long startTime = System.currentTimeMillis();
    StructuredCoalescentMultiTypeTree sctree;
    sctree = new StructuredCoalescentMultiTypeTree();
    for (int i = 0; i < reps; i++) {
        if (i % 1000 == 0)
            System.out.format("%d reps done\n", i);
        sctree.initByName("migrationModel", migrationModel, "leafTypes", leafTypes, "nTypes", 4);
        heights[i] = sctree.getRoot().getHeight();
        changes[i] = sctree.getTotalNumberOfChanges();
    }
    long time = System.currentTimeMillis() - startTime;
    System.out.printf("E[T] = %1.4f +/- %1.4f\n", DiscreteStatistics.mean(heights), DiscreteStatistics.stdev(heights) / Math.sqrt(reps));
    System.out.printf("V[T] = %1.4f\n", DiscreteStatistics.variance(heights));
    System.out.printf("E[C] = %1.4f +/- %1.4f\n", DiscreteStatistics.mean(changes), DiscreteStatistics.stdev(changes) / Math.sqrt(reps));
    System.out.printf("V[C] = %1.4f\n", DiscreteStatistics.variance(changes));
    System.out.printf("Took %1.2f seconds\n", time / 1000.0);
    try (PrintStream outStream = new PrintStream("heights.txt")) {
        outStream.println("h c");
        for (int i = 0; i < reps; i++) outStream.format("%g %g\n", heights[i], changes[i]);
    }
}
Also used : IntegerParameter(beast.core.parameter.IntegerParameter) PrintStream(java.io.PrintStream) RealParameter(beast.core.parameter.RealParameter)

Example 20 with IntegerParameter

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

the class STX_NR_MTU_TS_Test method test.

@Test
public void test() throws Exception {
    System.out.println("STX_NR_MTU_TS test");
    // Test passing locally, not on Travis.  WHY!?
    // Fix seed.
    Randomizer.setSeed(53);
    // Assemble migration model:
    RealParameter rateMatrix = new RealParameter("0.1 0.1");
    RealParameter popSizes = new RealParameter("7.0 7.0");
    SCMigrationModel migModel = new SCMigrationModel();
    migModel.initByName("rateMatrix", rateMatrix, "popSizes", popSizes, "typeSet", new TypeSet("A", "B"));
    // Assemble initial MultiTypeTree
    MultiTypeTree mtTree = new StructuredCoalescentMultiTypeTree();
    mtTree.initByName("typeLabel", "deme", "migrationModel", migModel, "leafTypes", "1 1 0 0");
    // Set up state:
    State state = new State();
    state.initByName("stateNode", mtTree);
    // Assemble distribution:
    StructuredCoalescentTreeDensity distribution = new StructuredCoalescentTreeDensity();
    distribution.initByName("migrationModel", migModel, "multiTypeTree", mtTree);
    // Set up operators:
    Operator operatorSTX = new TypedSubtreeExchange();
    operatorSTX.initByName("weight", 1.0, "multiTypeTree", mtTree, "migrationModel", migModel);
    Operator operatorNR = new NodeRetype();
    operatorNR.initByName("weight", 1.0, "multiTypeTree", mtTree, "migrationModel", migModel);
    Operator operatorMTU = new MultiTypeUniform();
    operatorMTU.initByName("weight", 1.0, "migrationModel", migModel, "multiTypeTree", mtTree);
    Operator operatorMTTS = new MultiTypeTreeScale();
    operatorMTTS.initByName("weight", 1.0, "multiTypeTree", mtTree, "migrationModel", migModel, "scaleFactor", 1.5, "useOldTreeScaler", false);
    // Set up stat analysis logger:
    MultiTypeTreeStatLogger logger = new MultiTypeTreeStatLogger();
    logger.initByName("multiTypeTree", mtTree, "burninFrac", 0.1, "logEvery", 1000);
    // Set up MCMC:
    MCMC mcmc = new MCMC();
    mcmc.initByName("chainLength", "1000000", "state", state, "distribution", distribution, "operator", operatorSTX, "operator", operatorNR, "operator", operatorMTU, "operator", operatorMTTS, "logger", logger);
    // Run MCMC:
    mcmc.run();
    System.out.format("height mean = %s\n", logger.getHeightMean());
    System.out.format("height var = %s\n", logger.getHeightVar());
    System.out.format("height ESS = %s\n", logger.getHeightESS());
    // Direct simulation:
    double[] heights = UtilMethods.getSimulatedHeights(migModel, new IntegerParameter("1 1 0 0"));
    double simHeightMean = DiscreteStatistics.mean(heights);
    double simHeightVar = DiscreteStatistics.variance(heights);
    // Compare analysis results with truth:
    boolean withinTol = (logger.getHeightESS() > 500) && (Math.abs(logger.getHeightMean() - simHeightMean) < 2.0) && (Math.abs(logger.getHeightVar() - simHeightVar) < 50);
    Assert.assertTrue(withinTol);
}
Also used : Operator(beast.core.Operator) MultiTypeTreeStatLogger(multitypetree.util.MultiTypeTreeStatLogger) IntegerParameter(beast.core.parameter.IntegerParameter) MCMC(beast.core.MCMC) RealParameter(beast.core.parameter.RealParameter) StructuredCoalescentMultiTypeTree(beast.evolution.tree.StructuredCoalescentMultiTypeTree) MultiTypeTree(beast.evolution.tree.MultiTypeTree) SCMigrationModel(beast.evolution.tree.SCMigrationModel) State(beast.core.State) TypeSet(beast.evolution.tree.TypeSet) StructuredCoalescentTreeDensity(multitypetree.distributions.StructuredCoalescentTreeDensity) StructuredCoalescentMultiTypeTree(beast.evolution.tree.StructuredCoalescentMultiTypeTree) Test(org.junit.Test)

Aggregations

IntegerParameter (beast.core.parameter.IntegerParameter)31 RealParameter (beast.core.parameter.RealParameter)23 Test (org.junit.Test)13 State (beast.core.State)6 SCMigrationModel (beast.evolution.tree.SCMigrationModel)4 TypeSet (beast.evolution.tree.TypeSet)4 PrintStream (java.io.PrintStream)4 CFEventList (bacter.CFEventList)3 Function (beast.core.Function)3 MCMC (beast.core.MCMC)3 Operator (beast.core.Operator)3 StructuredCoalescentTreeDensity (multitypetree.distributions.StructuredCoalescentTreeDensity)3 MultiTypeTreeStatLogger (multitypetree.util.MultiTypeTreeStatLogger)3 ConversionGraph (bacter.ConversionGraph)2 BooleanParameter (beast.core.parameter.BooleanParameter)2 Alignment (beast.evolution.alignment.Alignment)2 DeltaExchangeOperator (beast.evolution.operators.DeltaExchangeOperator)2 MultiTypeTreeFromNewick (beast.evolution.tree.MultiTypeTreeFromNewick)2 BEASTObjectDialog (beast.app.draw.BEASTObjectDialog)1 InputEditor (beast.app.draw.InputEditor)1