Search in sources :

Example 16 with RealParameter

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

the class RandomLocalClockModel method recalculateScaleFactor.

private void recalculateScaleFactor() {
    BooleanParameter indicators = indicatorParamInput.get();
    RealParameter rates = rateParamInput.get();
    calculateUnscaledBranchRates(m_tree.getRoot(), 1.0, indicators, rates);
    double timeTotal = 0.0;
    double branchTotal = 0.0;
    for (int i = 0; i < m_tree.getNodeCount(); i++) {
        Node node = m_tree.getNode(i);
        if (!node.isRoot()) {
            double branchInTime = node.getParent().getHeight() - node.getHeight();
            double branchLength = branchInTime * unscaledBranchRates[node.getNr()];
            timeTotal += branchInTime;
            branchTotal += branchLength;
        }
    }
    scaleFactor = timeTotal / branchTotal;
    scaleFactor *= meanRate.getValue();
}
Also used : Node(beast.evolution.tree.Node) RealParameter(beast.core.parameter.RealParameter) BooleanParameter(beast.core.parameter.BooleanParameter)

Example 17 with RealParameter

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

the class ParameterTest method testParamter.

@Test
public void testParamter() throws Exception {
    RealParameter parameter = new RealParameter();
    parameter.initByName("value", "1.27 1.9");
    assertEquals(parameter.getDimension(), 2);
    parameter.setDimension(5);
    assertEquals(parameter.getDimension(), 5);
    assertEquals(parameter.getValue(0), parameter.getValue(2));
    assertEquals(parameter.getValue(0), parameter.getValue(4));
    assertEquals(parameter.getValue(1), parameter.getValue(3));
    assertNotSame(parameter.getValue(0), parameter.getValue(1));
    try {
        // this will throw an exception
        parameter.setValue(2, 2.0);
        assertNotSame(parameter.getValue(0), parameter.getValue(2));
    } catch (Exception e) {
    // setValue is not allowed for StateNode not in State
    }
    Double[] x = { 1.0, 2.0, 3.0, 2.0, 4.0, 5.5 };
    parameter = new RealParameter(x);
    assertEquals(parameter.getDimension(), 6);
}
Also used : RealParameter(beast.core.parameter.RealParameter) Test(org.junit.Test)

Example 18 with RealParameter

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

the class SumTest method testSum.

@Test
public void testSum() {
    RealParameter p1 = new RealParameter("1.0 2.0");
    Sum sum = new Sum();
    // single argument sum
    sum.initByName("arg", p1);
    double v = sum.getArrayValue();
    assertEquals(3.0, v, 1e-10);
    // multiple argument sum
    sum = new Sum();
    RealParameter p2 = new RealParameter("2.0 2.5");
    sum.initByName("arg", p1, "arg", p2);
    v = sum.getArrayValue();
    assertEquals(7.5, v, 1e-10);
    // multiple same argument sum
    sum = new Sum();
    sum.initByName("arg", p1, "arg", p1);
    v = sum.getArrayValue();
    assertEquals(6.0, v, 1e-10);
    // sum of integers
    IntegerParameter p3 = new IntegerParameter("1 2 5");
    sum = new Sum();
    sum.initByName("arg", p3);
    v = sum.getArrayValue();
    assertEquals(8.0, v, 1e-10);
    // sum of boolean
    BooleanParameter p4 = new BooleanParameter("true false false true true");
    sum = new Sum();
    sum.initByName("arg", p4);
    v = sum.getArrayValue();
    assertEquals(3.0, v, 1e-10);
    // sum of booleans and integer
    sum = new Sum();
    sum.initByName("arg", p4, "arg", p3);
    v = sum.getArrayValue();
    assertEquals(11.0, v, 1e-10);
    // sum of booleans and real
    sum = new Sum();
    sum.initByName("arg", p1, "arg", p4);
    v = sum.getArrayValue();
    assertEquals(6.0, v, 1e-10);
}
Also used : IntegerParameter(beast.core.parameter.IntegerParameter) RealParameter(beast.core.parameter.RealParameter) Sum(beast.core.util.Sum) BooleanParameter(beast.core.parameter.BooleanParameter) Test(org.junit.Test)

Example 19 with RealParameter

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

the class TreeLikelihoodTest method testMarginalisationOfLikelihoodNucleotide.

@Test
public void testMarginalisationOfLikelihoodNucleotide() throws Exception {
    // test summation over all patterns adds to 1 for nucleotide data
    Sequence German_ST = new Sequence("German_ST", "           AAAAAAAAAAAAAAAA CCCCCCCCCCCCCCCC GGGGGGGGGGGGGGGG TTTTTTTTTTTTTTTT");
    Sequence Dutch_List = new Sequence("Dutch_List", "          AAAACCCCGGGGTTTT AAAACCCCGGGGTTTT AAAACCCCGGGGTTTT AAAACCCCGGGGTTTT");
    Sequence English_ST = new Sequence("English_ST", "          ACGTACGTACGTACGT ACGTACGTACGTACGT ACGTACGTACGTACGT ACGTACGTACGTACGT");
    Alignment data = new Alignment();
    data.initByName("sequence", German_ST, "sequence", Dutch_List, "sequence", English_ST, "dataType", "nucleotide");
    Tree tree = BEASTTestCase.getTree(data, "(English_ST:0.22743347188019544,(German_ST:0.10557648379843088,Dutch_List:0.10557648379843088):0.12185698808176457):0.0;");
    RealParameter frequencies = new RealParameter("0.2 0.3 0.4 0.1");
    Frequencies freqs = new Frequencies();
    freqs.initByName("frequencies", frequencies);
    GeneralSubstitutionModel gsm = new GeneralSubstitutionModel();
    gsm.initByName("rates", "1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0", "frequencies", freqs);
    SiteModel siteModel = new SiteModel();
    siteModel.initByName("mutationRate", "1.0", "gammaCategoryCount", 1, "substModel", gsm);
    TreeLikelihood likelihood = newTreeLikelihood();
    likelihood.initByName("data", data, "tree", tree, "siteModel", siteModel);
    likelihood.initByName("useAmbiguities", false, "data", data, "tree", tree, "siteModel", siteModel);
    likelihood.calculateLogP();
    double[] logPs = likelihood.getPatternLogLikelihoods();
    double P = 0;
    for (double d : logPs) {
        P += Math.exp(d);
    }
    assertEquals(P, 1.0, BEASTTestCase.PRECISION);
}
Also used : Alignment(beast.evolution.alignment.Alignment) BeagleTreeLikelihood(beast.evolution.likelihood.BeagleTreeLikelihood) TreeLikelihood(beast.evolution.likelihood.TreeLikelihood) Tree(beast.evolution.tree.Tree) RealParameter(beast.core.parameter.RealParameter) GeneralSubstitutionModel(beast.evolution.substitutionmodel.GeneralSubstitutionModel) SiteModel(beast.evolution.sitemodel.SiteModel) Sequence(beast.evolution.alignment.Sequence) Frequencies(beast.evolution.substitutionmodel.Frequencies) UncertainAlignmentTest(test.beast.evolution.alignment.UncertainAlignmentTest) Test(org.junit.Test)

Example 20 with RealParameter

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

the class BirthDeathGernhard08ModelTest method testJC69Likelihood.

@Test
public void testJC69Likelihood() throws Exception {
    // Set up JC69 model: uniform freqs, kappa = 1, 0 gamma categories
    Alignment data = BEASTTestCase.getAlignment();
    Tree tree = BEASTTestCase.getTree(data);
    RealParameter birthDiffRate = new RealParameter("1.0");
    RealParameter relativeDeathRate = new RealParameter("0.5");
    RealParameter originHeight = new RealParameter("0.1");
    BirthDeathGernhard08Model likelihood = new BirthDeathGernhard08Model();
    likelihood.initByName("type", "unscaled", "tree", tree, "birthDiffRate", birthDiffRate, "relativeDeathRate", relativeDeathRate);
    double logP = 0;
    // -3.520936119641363
    logP = likelihood.calculateLogP();
    assertEquals(logP, 2.5878899503981287, BEASTTestCase.PRECISION);
    likelihood.initByName("type", "timesonly", "tree", tree, "birthDiffRate", birthDiffRate, "relativeDeathRate", relativeDeathRate);
    logP = likelihood.calculateLogP();
    assertEquals(logP, 9.16714116240823, BEASTTestCase.PRECISION);
    likelihood.initByName("type", "oriented", "tree", tree, "birthDiffRate", birthDiffRate, "relativeDeathRate", relativeDeathRate);
    logP = likelihood.calculateLogP();
    assertEquals(logP, 4.379649419626184, BEASTTestCase.PRECISION);
    likelihood.initByName("type", "labeled", "tree", tree, "birthDiffRate", birthDiffRate, "relativeDeathRate", relativeDeathRate);
    logP = likelihood.calculateLogP();
    assertEquals(logP, 1.2661341104158121, BEASTTestCase.PRECISION);
    likelihood.initByName("type", "labeled", "tree", tree, "birthDiffRate", birthDiffRate, "relativeDeathRate", relativeDeathRate, "originHeight", originHeight);
    logP = likelihood.calculateLogP();
    assertEquals(logP, 8.41413452832378, BEASTTestCase.PRECISION);
}
Also used : Alignment(beast.evolution.alignment.Alignment) BirthDeathGernhard08Model(beast.evolution.speciation.BirthDeathGernhard08Model) Tree(beast.evolution.tree.Tree) RealParameter(beast.core.parameter.RealParameter) Test(org.junit.Test)

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