Search in sources :

Example 1 with Exchange

use of beast.evolution.operators.Exchange in project beast2 by CompEvol.

the class ExchangeOperatorTest method testNarrowExchange.

void testNarrowExchange(String sourceTree, String targetTree, int runs, Alignment data) throws Exception {
    // first test going from source to target
    double match = 0;
    for (int i = 0; i < runs; i++) {
        TreeParser tree = new TreeParser();
        tree.initByName("taxa", data, "newick", sourceTree, "IsLabelledNewick", true);
        State state = new State();
        state.initByName("stateNode", tree);
        state.initialise();
        Exchange operator = new Exchange();
        operator.initByName("isNarrow", true, "tree", tree, "weight", 1.0);
        double logHR = operator.proposal();
        String treeString = tree.getRoot().toNewick();
        if (treeString.equals(targetTree) && !Double.isInfinite(logHR)) {
            // proportion of accepts equals min(HR, 1.0)
            match += Math.min(Math.exp(logHR), 1.0);
        }
    }
    System.out.println(" Matches: " + match * 100.0 / runs + "%");
    // now test going from target to source
    double match2 = 0;
    for (int i = 0; i < runs; i++) {
        TreeParser tree = new TreeParser();
        tree.initByName("taxa", data, "newick", targetTree, "IsLabelledNewick", true);
        State state = new State();
        state.initByName("stateNode", tree);
        state.initialise();
        Exchange operator = new Exchange();
        operator.initByName("isNarrow", true, "tree", tree, "weight", 1.0);
        double logHR = operator.proposal();
        String treeString = tree.getRoot().toNewick();
        if (treeString.equals(sourceTree) && !Double.isInfinite(logHR)) {
            // proportion of accepts equals min(HR, 1.0)
            match2 += Math.min(Math.exp(logHR), 1.0);
        }
    }
    System.out.println(" Matches: " + match2 * 100.0 / runs + "%");
    assertTrue("difference(" + 100 * (match - match2) / runs + ") exceeds 1.0%", 100.0 * Math.abs(match - match2) / runs < 1.0);
}
Also used : Exchange(beast.evolution.operators.Exchange) TreeParser(beast.util.TreeParser) State(beast.core.State)

Aggregations

State (beast.core.State)1 Exchange (beast.evolution.operators.Exchange)1 TreeParser (beast.util.TreeParser)1