Search in sources :

Example 6 with IntervalList

use of dr.evolution.coalescent.IntervalList in project beast-mcmc by beast-dev.

the class BigFastTreeTreeIntervalsTest method testHandelHeightChange.

public void testHandelHeightChange() throws TreeUtils.MissingTaxonException {
    IntervalList bigFastIntervals = new BigFastTreeIntervals(tree);
    bigFastIntervals.calculateIntervals();
    double[] intervals = { 0, 0.5, 0.49, 0.01, 0.5, 0, 0.5, 0.5, 0.5, 1.5 };
    // node             1-2, 4  , 5  , 6  , 0  , 3, 7  , 9  , 8  , 10
    boolean pass = true;
    tree.beginTreeEdit();
    tree.setNodeHeight(tree.getNode(10), 4.5);
    tree.endTreeEdit();
    bigFastIntervals.calculateIntervals();
    for (int j = 0; j < bigFastIntervals.getIntervalCount(); j++) {
        if (Math.abs(bigFastIntervals.getInterval(j) - intervals[j]) > 1E-3) {
            System.out.println(bigFastIntervals.getInterval(j) - intervals[j]);
            System.out.println("expected: " + intervals[j] + " got: " + bigFastIntervals.getInterval(j));
            pass = false;
            break;
        }
    }
    assertTrue(pass);
}
Also used : BigFastTreeIntervals(dr.evomodel.bigfasttree.BigFastTreeIntervals) IntervalList(dr.evolution.coalescent.IntervalList)

Example 7 with IntervalList

use of dr.evolution.coalescent.IntervalList in project beast-mcmc by beast-dev.

the class BigFastTreeTreeIntervalsTest method testOrderChange.

// 0.5
// 1.0           +------ 0 * [1.5]
// +----------------------- |(7) *  [2]
// 1.0           |                        |                                             +-- 1 * [0]
// +----------------------- |(8) *  [3]              +---------------------------------------------|(6) [0.1]
// |                        |                                                                      +--- 2 * [0]
// |                        |                1.5
// |(10) &  [4]             +-------------------------------- 3 * [1.5]
// |                                                       2.0
// |        1.5                   +----------------------------------------------------- 4 & [0.5]
// +-----------------------------|(9) & [2.5]     1.51
// +-------------------------------------------- 5 & [0.99]
public void testOrderChange() throws TreeUtils.MissingTaxonException, IOException, Importer.ImportException {
    NewickImporter importer = new NewickImporter("(((0:0.5,(1:1.0,2:1.0)n6:1.0)n7:1.0,3:1.5)n8:1.0,(4:2.0,5:1.51)n9:1.5)n10;");
    tree = new BigFastTreeModel(importer.importTree(null));
    IntervalList bigFastIntervals = new BigFastTreeIntervals(tree);
    IntervalList intervals = new TreeIntervals(tree, null, null);
    bigFastIntervals.calculateIntervals();
    // node             1-2,6,  4  , 5  ,  0  ,  3, 7  , 9  , 8  , 10
    boolean pass = true;
    NodeRef parent = tree.getNode(6);
    NodeRef sibling = tree.getNode(2);
    NodeRef grandParent = tree.getParent(parent);
    NodeRef j = tree.getNode(2);
    tree.beginTreeEdit();
    tree.removeChild(grandParent, parent);
    tree.removeChild(parent, sibling);
    tree.addChild(grandParent, sibling);
    NodeRef jParent = tree.getParent(j);
    tree.removeChild(jParent, j);
    tree.addChild(jParent, parent);
    tree.addChild(parent, j);
    tree.setNodeHeight(parent, 0.1);
    tree.endTreeEdit();
    bigFastIntervals.calculateIntervals();
    List<Integer> missed = new ArrayList<>();
    for (int i = 0; i < bigFastIntervals.getIntervalCount(); i++) {
        if (bigFastIntervals.getInterval(i) != intervals.getInterval(i)) {
            System.out.println("expected: " + intervals.getInterval(i) + " got: " + bigFastIntervals.getInterval(i));
            missed.add(i);
        }
    }
    System.out.println(missed);
    assertEquals(0, missed.size());
}
Also used : NodeRef(dr.evolution.tree.NodeRef) BigFastTreeIntervals(dr.evomodel.bigfasttree.BigFastTreeIntervals) IntervalList(dr.evolution.coalescent.IntervalList) NewickImporter(dr.evolution.io.NewickImporter) BigFastTreeModel(dr.evomodel.bigfasttree.BigFastTreeModel) ArrayList(java.util.ArrayList) TreeIntervals(dr.evomodel.coalescent.TreeIntervals) BigFastTreeIntervals(dr.evomodel.bigfasttree.BigFastTreeIntervals)

Example 8 with IntervalList

use of dr.evolution.coalescent.IntervalList in project beast-mcmc by beast-dev.

the class CoalescentLikelihood method getCoalescentEventsStatisticValue.

@Override
public double getCoalescentEventsStatisticValue(int i) {
    if (i == 0) {
        IntervalList intervals = getIntervalList();
        final int intervalCount = intervals.getIntervalCount();
        for (int j = 0; j < coalescentEventStatisticValues.length; j++) {
            coalescentEventStatisticValues[j] = 0.0;
        }
        int counter = 0;
        for (int j = 0; j < intervalCount; j++) {
            if (intervals.getIntervalType(j) == IntervalType.COALESCENT) {
                this.coalescentEventStatisticValues[counter] += intervals.getInterval(j) * (intervals.getLineageCount(j) * (intervals.getLineageCount(j) - 1.0)) / 2.0;
                counter++;
            } else {
                this.coalescentEventStatisticValues[counter] += intervals.getInterval(j) * (intervals.getLineageCount(j) * (intervals.getLineageCount(j) - 1.0)) / 2.0;
            }
        }
    }
    return coalescentEventStatisticValues[i];
}
Also used : IntervalList(dr.evolution.coalescent.IntervalList)

Example 9 with IntervalList

use of dr.evolution.coalescent.IntervalList in project beast-mcmc by beast-dev.

the class CoalescentLikelihood method calculateLogLikelihood.

/**
 * Calculates the log likelihood of this set of coalescent intervals,
 * given a demographic model.
 */
protected double calculateLogLikelihood(DemographicModel demographicModel) {
    double logL = 0.0;
    IntervalList intervals = getIntervalList();
    final int n = intervals.getIntervalCount();
    if (n == 0) {
        return 0.0;
    }
    double absoluteStartTime = intervals.getStartTime();
    demographicModel.setTimeOffset(absoluteStartTime);
    DemographicFunction demographicFunction = demographicModel.getDemographicFunction();
    double startTime = 0;
    for (int i = 0; i < n; i++) {
        final double duration = intervals.getInterval(i);
        final double finishTime = startTime + duration;
        final double intervalArea = demographicFunction.getIntegral(startTime, finishTime);
        if (intervalArea == 0 && duration != 0) {
            return Double.NEGATIVE_INFINITY;
        }
        final int lineageCount = intervals.getLineageCount(i);
        final double kChoose2 = Binomial.choose2(lineageCount);
        // common part
        logL += -kChoose2 * intervalArea;
        if (intervals.getIntervalType(i) == IntervalType.COALESCENT) {
            final double demographicAtCoalPoint = demographicFunction.getDemographic(finishTime);
            if (duration == 0.0 || demographicAtCoalPoint * (intervalArea / duration) >= demographicFunction.getThreshold()) {
                // if( duration == 0.0 || demographicAtCoalPoint >= threshold * (duration/intervalArea) ) {
                logL -= Math.log(demographicAtCoalPoint);
            } else {
                // System.err.println("Warning: " + i + " " + demographicAtCoalPoint + " " + (intervalArea/duration) );
                return Double.NEGATIVE_INFINITY;
            }
        }
        startTime = finishTime;
    }
    return logL;
}
Also used : IntervalList(dr.evolution.coalescent.IntervalList) DemographicFunction(dr.evolution.coalescent.DemographicFunction)

Example 10 with IntervalList

use of dr.evolution.coalescent.IntervalList in project beast-mcmc by beast-dev.

the class BigFastTreeTreeIntervalsTest method testsmallTree.

// clade * 0-3 root 8
// clade & 4-5 root 10
// 0.5
// 1.0           +------ 0 * [1.5]
// +----------------------- |(7) *  [2]                          1.0
// 1.0           |                        |        1.0             +----------------------- 1 * [0]
// +----------------------- |(8) *  [3]              +----------------------- |(6) [1]    1.0
// |                        |                                                 +----------------------- 2 * [0]
// |                        |                1.5
// |(10) &  [4]             +-------------------------------- 3 * [1.5]
// |                                                       2.0
// |        1.5                   +----------------------------------------------------- 4 & [0.5]
// +-----------------------------|(9) & [2.5]     1.51
// +-------------------------------------------- 5 & [0.99]
public void testsmallTree() throws TreeUtils.MissingTaxonException {
    IntervalList bigFastIntervals = new BigFastTreeIntervals(tree);
    bigFastIntervals.calculateIntervals();
    double[] intervals = { 0, 0.5, 0.49, 0.01, 0.5, 0, 0.5, 0.5, 0.5, 1.0 };
    // node             1-2, 4  , 5  , 6  , 0  , 3, 7  , 9  , 8  , 10
    boolean pass = true;
    for (int j = 0; j < bigFastIntervals.getIntervalCount(); j++) {
        if (Math.abs(bigFastIntervals.getInterval(j) - intervals[j]) > 1E-3) {
            System.out.println(bigFastIntervals.getInterval(j) - intervals[j]);
            System.out.println("expected: " + intervals[j] + " got: " + bigFastIntervals.getInterval(j));
            pass = false;
            break;
        }
    }
    assertTrue(pass);
}
Also used : BigFastTreeIntervals(dr.evomodel.bigfasttree.BigFastTreeIntervals) IntervalList(dr.evolution.coalescent.IntervalList)

Aggregations

IntervalList (dr.evolution.coalescent.IntervalList)13 BigFastTreeIntervals (dr.evomodel.bigfasttree.BigFastTreeIntervals)5 ArrayList (java.util.ArrayList)4 DemographicFunction (dr.evolution.coalescent.DemographicFunction)2 NewickImporter (dr.evolution.io.NewickImporter)2 TreeIntervals (dr.evomodel.coalescent.TreeIntervals)2 TreeModel (dr.evomodel.tree.TreeModel)2 Parameter (dr.inference.model.Parameter)2 TreeIntervals (dr.evolution.coalescent.TreeIntervals)1 NodeRef (dr.evolution.tree.NodeRef)1 Tree (dr.evolution.tree.Tree)1 TreeUtils (dr.evolution.tree.TreeUtils)1 TaxonList (dr.evolution.util.TaxonList)1 BigFastTreeModel (dr.evomodel.bigfasttree.BigFastTreeModel)1 DemographicModel (dr.evomodel.coalescent.demographicmodel.DemographicModel)1 SmoothSkygridLikelihood (dr.evomodel.coalescent.smooth.SmoothSkygridLikelihood)1 NodeHeightOperator (dr.evomodel.operators.NodeHeightOperator)1 SubtreeLeapOperator (dr.evomodel.operators.SubtreeLeapOperator)1 DefaultTreeModel (dr.evomodel.tree.DefaultTreeModel)1 GeneralizedLinearModel (dr.inference.glm.GeneralizedLinearModel)1