Search in sources :

Example 21 with DefaultBranchRateModel

use of dr.evomodel.branchratemodel.DefaultBranchRateModel in project beast-mcmc by beast-dev.

the class BranchRateGradientParser method parseTreeDataLikelihood.

private GradientWrtParameterProvider parseTreeDataLikelihood(TreeDataLikelihood treeDataLikelihood, String traitName, boolean useHessian) throws XMLParseException {
    BranchRateModel branchRateModel = treeDataLikelihood.getBranchRateModel();
    if (branchRateModel instanceof DefaultBranchRateModel || branchRateModel instanceof ArbitraryBranchRates) {
        Parameter branchRates = null;
        if (branchRateModel instanceof ArbitraryBranchRates) {
            branchRates = ((ArbitraryBranchRates) branchRateModel).getRateParameter();
        }
        DataLikelihoodDelegate delegate = treeDataLikelihood.getDataLikelihoodDelegate();
        if (delegate instanceof ContinuousDataLikelihoodDelegate) {
            ContinuousDataLikelihoodDelegate continuousData = (ContinuousDataLikelihoodDelegate) delegate;
            return new BranchRateGradient(traitName, treeDataLikelihood, continuousData, branchRates);
        } else if (delegate instanceof BeagleDataLikelihoodDelegate) {
            BeagleDataLikelihoodDelegate beagleData = (BeagleDataLikelihoodDelegate) delegate;
            if (branchRateModel instanceof LocalBranchRates) {
                return new LocalBranchRateGradientForDiscreteTrait(traitName, treeDataLikelihood, beagleData, branchRates, useHessian);
            } else {
                return new BranchRateGradientForDiscreteTrait(traitName, treeDataLikelihood, beagleData, branchRates, useHessian);
            }
        } else {
            throw new XMLParseException("Unknown likelihood delegate type");
        }
    } else {
        throw new XMLParseException("Only implemented for an arbitrary rates model");
    }
}
Also used : ArbitraryBranchRates(dr.evomodel.branchratemodel.ArbitraryBranchRates) BranchRateModel(dr.evomodel.branchratemodel.BranchRateModel) DefaultBranchRateModel(dr.evomodel.branchratemodel.DefaultBranchRateModel) ContinuousDataLikelihoodDelegate(dr.evomodel.treedatalikelihood.continuous.ContinuousDataLikelihoodDelegate) BeagleDataLikelihoodDelegate(dr.evomodel.treedatalikelihood.BeagleDataLikelihoodDelegate) DataLikelihoodDelegate(dr.evomodel.treedatalikelihood.DataLikelihoodDelegate) BranchRateGradient(dr.evomodel.treedatalikelihood.continuous.BranchRateGradient) BranchRateGradientForDiscreteTrait(dr.evomodel.treedatalikelihood.discrete.BranchRateGradientForDiscreteTrait) LocalBranchRateGradientForDiscreteTrait(dr.evomodel.treedatalikelihood.discrete.LocalBranchRateGradientForDiscreteTrait) LocalBranchRateGradientForDiscreteTrait(dr.evomodel.treedatalikelihood.discrete.LocalBranchRateGradientForDiscreteTrait) Parameter(dr.inference.model.Parameter) BeagleDataLikelihoodDelegate(dr.evomodel.treedatalikelihood.BeagleDataLikelihoodDelegate) ContinuousDataLikelihoodDelegate(dr.evomodel.treedatalikelihood.continuous.ContinuousDataLikelihoodDelegate) DefaultBranchRateModel(dr.evomodel.branchratemodel.DefaultBranchRateModel) LocalBranchRates(dr.evomodel.branchratemodel.LocalBranchRates)

Example 22 with DefaultBranchRateModel

use of dr.evomodel.branchratemodel.DefaultBranchRateModel in project beast-mcmc by beast-dev.

the class MicrosatelliteSimulatorParser method parseXMLObject.

public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    Microsatellite msatDataType = (Microsatellite) xo.getChild(Microsatellite.class);
    Taxa taxa = (Taxa) xo.getChild(Taxa.class);
    Tree tree = (Tree) xo.getChild(Tree.class);
    MicrosatelliteModel msatModel = (MicrosatelliteModel) xo.getChild(MicrosatelliteModel.class);
    BranchRateModel brModel = (BranchRateModel) xo.getChild(BranchRateModel.class);
    if (brModel == null) {
        brModel = new DefaultBranchRateModel();
    }
    MicrosatelliteSimulator msatSim = new MicrosatelliteSimulator(msatDataType, taxa, tree, new GammaSiteModel(msatModel), brModel);
    Patterns patterns = msatSim.simulateMsatPattern();
    String msatPatId = xo.getAttribute("id", "simMsatPat");
    patterns.setId(msatPatId);
    MicrosatellitePatternParser.printDetails(patterns);
    MicrosatellitePatternParser.printMicrosatContent(patterns);
    return patterns;
}
Also used : Microsatellite(dr.evolution.datatype.Microsatellite) Taxa(dr.evolution.util.Taxa) MicrosatelliteModel(dr.oldevomodel.substmodel.MicrosatelliteModel) GammaSiteModel(dr.oldevomodel.sitemodel.GammaSiteModel) BranchRateModel(dr.evomodel.branchratemodel.BranchRateModel) DefaultBranchRateModel(dr.evomodel.branchratemodel.DefaultBranchRateModel) MicrosatelliteSimulator(dr.app.seqgen.MicrosatelliteSimulator) Tree(dr.evolution.tree.Tree) Patterns(dr.evolution.alignment.Patterns) DefaultBranchRateModel(dr.evomodel.branchratemodel.DefaultBranchRateModel)

Example 23 with DefaultBranchRateModel

use of dr.evomodel.branchratemodel.DefaultBranchRateModel in project beast-mcmc by beast-dev.

the class SequenceSimulator method main.

// getDefaultSiteModel
public static void main(String[] args) {
    try {
        int nReplications = 10;
        // create tree
        NewickImporter importer = new NewickImporter("((A:1.0,B:1.0)AB:1.0,(C:1.0,D:1.0)CD:1.0)ABCD;");
        Tree tree = importer.importTree(null);
        // create site model
        SiteModel siteModel = getDefaultSiteModel();
        // create branch rate model
        BranchRateModel branchRateModel = new DefaultBranchRateModel();
        // feed to sequence simulator and generate leaves
        SequenceSimulator treeSimulator = new SequenceSimulator(tree, siteModel, branchRateModel, nReplications);
        Sequence ancestralSequence = new Sequence();
        ancestralSequence.appendSequenceString("TCAGGTCAAG");
        treeSimulator.setAncestralSequence(ancestralSequence);
        System.out.println(treeSimulator.simulate().toString());
    } catch (Exception e) {
        e.printStackTrace();
    }
// END: try-catch block
}
Also used : BranchRateModel(dr.evomodel.branchratemodel.BranchRateModel) DefaultBranchRateModel(dr.evomodel.branchratemodel.DefaultBranchRateModel) NewickImporter(dr.evolution.io.NewickImporter) Tree(dr.evolution.tree.Tree) GammaSiteModel(dr.oldevomodel.sitemodel.GammaSiteModel) SiteModel(dr.oldevomodel.sitemodel.SiteModel) Sequence(dr.evolution.sequence.Sequence) DefaultBranchRateModel(dr.evomodel.branchratemodel.DefaultBranchRateModel)

Example 24 with DefaultBranchRateModel

use of dr.evomodel.branchratemodel.DefaultBranchRateModel in project beast-mcmc by beast-dev.

the class ContinuousDataLikelihoodDelegateTest method testLikelihoodFullIOU.

public void testLikelihoodFullIOU() {
    System.out.println("\nTest Likelihood using Full IOU:");
    // Diffusion
    List<BranchRateModel> optimalTraitsModels = new ArrayList<BranchRateModel>();
    optimalTraitsModels.add(new StrictClockBranchRates(new Parameter.Default("rate.1", new double[] { 1.0 })));
    optimalTraitsModels.add(new StrictClockBranchRates(new Parameter.Default("rate.2", new double[] { 2.0 })));
    optimalTraitsModels.add(new StrictClockBranchRates(new Parameter.Default("rate.3", new double[] { -2.0 })));
    Parameter[] strengthOfSelectionParameters = new Parameter[3];
    strengthOfSelectionParameters[0] = new Parameter.Default(new double[] { 0.5, 0.5, 0.0 });
    strengthOfSelectionParameters[1] = new Parameter.Default(new double[] { 0.2, 5, 0.1 });
    strengthOfSelectionParameters[2] = new Parameter.Default(new double[] { 0.0, 1.0, 10.0 });
    MatrixParameter strengthOfSelectionMatrixParam = new MatrixParameter("strengthOfSelectionMatrix", strengthOfSelectionParameters);
    DiffusionProcessDelegate diffusionProcessDelegate = new IntegratedOUDiffusionModelDelegate(treeModel, diffusionModel, optimalTraitsModels, new MultivariateElasticModel(strengthOfSelectionMatrixParam));
    // Rates
    ContinuousRateTransformation rateTransformation = new ContinuousRateTransformation.Default(treeModel, true, false);
    BranchRateModel rateModel = new DefaultBranchRateModel();
    // CDL
    ContinuousDataLikelihoodDelegate likelihoodDelegate = new ContinuousDataLikelihoodDelegate(treeModel, diffusionProcessDelegate, dataModelIntegrated, rootPriorIntegrated, rateTransformation, rateModel, false);
    // Likelihood Computation
    TreeDataLikelihood dataLikelihood = new TreeDataLikelihood(likelihoodDelegate, treeModel, rateModel);
    testLikelihood("likelihoodFullIOU", dataLikelihood);
// Conditional moments (preorder)
// testConditionalMoments(dataLikelihood, likelihoodDelegate);
}
Also used : MatrixParameter(dr.inference.model.MatrixParameter) ArrayList(java.util.ArrayList) MultivariateElasticModel(dr.evomodel.continuous.MultivariateElasticModel) StrictClockBranchRates(dr.evomodel.branchratemodel.StrictClockBranchRates) DefaultBranchRateModel(dr.evomodel.branchratemodel.DefaultBranchRateModel) TreeDataLikelihood(dr.evomodel.treedatalikelihood.TreeDataLikelihood) BranchRateModel(dr.evomodel.branchratemodel.BranchRateModel) DefaultBranchRateModel(dr.evomodel.branchratemodel.DefaultBranchRateModel) MatrixParameter(dr.inference.model.MatrixParameter) Parameter(dr.inference.model.Parameter)

Example 25 with DefaultBranchRateModel

use of dr.evomodel.branchratemodel.DefaultBranchRateModel in project beast-mcmc by beast-dev.

the class RepeatedMeasureFactorTest method testLikelihoodOU.

public void testLikelihoodOU() {
    System.out.println("\nTest Likelihood using full OU:");
    // Diffusion
    List<BranchRateModel> optimalTraitsModels = new ArrayList<BranchRateModel>();
    optimalTraitsModels.add(new StrictClockBranchRates(new Parameter.Default("rate.1", new double[] { 1.0 })));
    optimalTraitsModels.add(new StrictClockBranchRates(new Parameter.Default("rate.2", new double[] { 2.0 })));
    optimalTraitsModels.add(new StrictClockBranchRates(new Parameter.Default("rate.3", new double[] { -2.0 })));
    optimalTraitsModels.add(new StrictClockBranchRates(new Parameter.Default("rate.4", new double[] { 10.0 })));
    optimalTraitsModels.add(new StrictClockBranchRates(new Parameter.Default("rate.5", new double[] { 20.0 })));
    optimalTraitsModels.add(new StrictClockBranchRates(new Parameter.Default("rate.6", new double[] { -20.0 })));
    Parameter[] strengthOfSelectionParameters = new Parameter[6];
    strengthOfSelectionParameters[0] = new Parameter.Default(new double[] { 1.0, 0.1, 0.0, 0.0, 0.5, 2.0 });
    strengthOfSelectionParameters[1] = new Parameter.Default(new double[] { 0.1, 10., 0.0, 0.0, 0.0, 0.0 });
    strengthOfSelectionParameters[2] = new Parameter.Default(new double[] { 0.0, 0.0, 20., 0.3, 0.0, 0.0 });
    strengthOfSelectionParameters[3] = new Parameter.Default(new double[] { 0.0, 0.0, 0.3, 30., 3.0, 0.0 });
    strengthOfSelectionParameters[4] = new Parameter.Default(new double[] { 1.0, 0.0, 0.0, 3.0, 40., 0.0 });
    strengthOfSelectionParameters[5] = new Parameter.Default(new double[] { 0.0, 0.0, 0.5, 0.0, 0.0, 50. });
    MatrixParameter strengthOfSelectionMatrixParam = new MatrixParameter("strengthOfSelectionMatrix", strengthOfSelectionParameters);
    DiffusionProcessDelegate diffusionProcessDelegate = new OUDiffusionModelDelegate(treeModel, diffusionModel, optimalTraitsModels, new MultivariateElasticModel(strengthOfSelectionMatrixParam));
    // Rates
    ContinuousRateTransformation rateTransformation = new ContinuousRateTransformation.Default(treeModel, false, false);
    BranchRateModel rateModel = new DefaultBranchRateModel();
    // // Factor Model //// *****************************************************************************************
    // CDL
    ContinuousDataLikelihoodDelegate likelihoodDelegateFactors = new ContinuousDataLikelihoodDelegate(treeModel, diffusionProcessDelegate, dataModelFactor, rootPrior, rateTransformation, rateModel, true);
    dataModelFactor.setLikelihoodDelegate(likelihoodDelegateFactors);
    // Likelihood Computation
    TreeDataLikelihood dataLikelihoodFactors = new TreeDataLikelihood(likelihoodDelegateFactors, treeModel, rateModel);
    double logDatumLikelihoodFactor = getLogDatumLikelihood(dataModelFactor);
    double likelihoodFactorData = dataLikelihoodFactors.getLogLikelihood();
    double likelihoodFactorDiffusion = dataModelFactor.getLogLikelihood();
    assertEquals("likelihoodOUFactor", format.format(logDatumLikelihoodFactor), format.format(likelihoodFactorData + likelihoodFactorDiffusion));
    System.out.println("likelihoodOUFactor: " + format.format(logDatumLikelihoodFactor));
    // Simulation
    MathUtils.setSeed(17890826);
    double[] traitsFactors = getConditionalSimulations(dataLikelihoodFactors, likelihoodDelegateFactors, diffusionModel, dataModelFactor, rootPrior, treeModel, rateTransformation);
    System.err.println(new Vector(traitsFactors));
    // // Repeated Measures //// ************************************************************************************
    // CDL
    ContinuousDataLikelihoodDelegate likelihoodDelegateRepMea = new ContinuousDataLikelihoodDelegate(treeModel, diffusionProcessDelegate, dataModelRepeatedMeasures, rootPrior, rateTransformation, rateModel, true);
    // Likelihood Computation
    TreeDataLikelihood dataLikelihoodRepMea = new TreeDataLikelihood(likelihoodDelegateRepMea, treeModel, rateModel);
    double logDatumLikelihoodRepMea = getLogDatumLikelihood(dataLikelihoodRepMea);
    double likelihoodRepMeaDiffusion = dataLikelihoodRepMea.getLogLikelihood();
    assertEquals("likelihoodOURepMea", format.format(logDatumLikelihoodRepMea), format.format(likelihoodRepMeaDiffusion));
    System.out.println("likelihoodOURepMea: " + format.format(logDatumLikelihoodRepMea));
    // Simulation
    MathUtils.setSeed(17890826);
    double[] traitsRepMea = getConditionalSimulations(dataLikelihoodRepMea, likelihoodDelegateRepMea, diffusionModel, dataModelRepeatedMeasures, rootPrior, treeModel, rateTransformation);
    System.err.println(new Vector(traitsRepMea));
    // // Equal ? //// **********************************************************************************************
    assertEquals("likelihoodOURepFactor", format.format(likelihoodFactorData + likelihoodFactorDiffusion), format.format(likelihoodRepMeaDiffusion));
    for (int i = 0; i < traitsFactors.length; i++) {
        assertEquals(format.format(traitsRepMea[i]), format.format(traitsFactors[i]));
    }
    // // Repeated Measures Full //// *******************************************************************************
    // CDL
    ContinuousDataLikelihoodDelegate likelihoodDelegateRepMeaFull = new ContinuousDataLikelihoodDelegate(treeModel, diffusionProcessDelegate, dataModelRepeatedMeasuresFull, rootPrior, rateTransformation, rateModel, true);
    // Likelihood Computation
    TreeDataLikelihood dataLikelihoodRepMeaFull = new TreeDataLikelihood(likelihoodDelegateRepMeaFull, treeModel, rateModel);
    double logDatumLikelihoodRepMeaFull = getLogDatumLikelihood(dataLikelihoodRepMeaFull);
    double likelihoodRepMeaDiffusionFull = dataLikelihoodRepMeaFull.getLogLikelihood();
    assertEquals("likelihoodBMRepMea", format.format(logDatumLikelihoodRepMeaFull), format.format(likelihoodRepMeaDiffusionFull));
    System.out.println("likelihoodBMRepMeaFull: " + format.format(logDatumLikelihoodRepMeaFull));
}
Also used : ArrayList(java.util.ArrayList) MultivariateElasticModel(dr.evomodel.continuous.MultivariateElasticModel) StrictClockBranchRates(dr.evomodel.branchratemodel.StrictClockBranchRates) DefaultBranchRateModel(dr.evomodel.branchratemodel.DefaultBranchRateModel) TreeDataLikelihood(dr.evomodel.treedatalikelihood.TreeDataLikelihood) BranchRateModel(dr.evomodel.branchratemodel.BranchRateModel) DefaultBranchRateModel(dr.evomodel.branchratemodel.DefaultBranchRateModel) Vector(dr.math.matrixAlgebra.Vector)

Aggregations

DefaultBranchRateModel (dr.evomodel.branchratemodel.DefaultBranchRateModel)28 BranchRateModel (dr.evomodel.branchratemodel.BranchRateModel)26 Parameter (dr.inference.model.Parameter)19 GammaSiteRateModel (dr.evomodel.siteratemodel.GammaSiteRateModel)16 FrequencyModel (dr.evomodel.substmodel.FrequencyModel)16 ArrayList (java.util.ArrayList)15 Tree (dr.evolution.tree.Tree)11 HomogeneousBranchModel (dr.evomodel.branchmodel.HomogeneousBranchModel)11 DefaultTreeModel (dr.evomodel.tree.DefaultTreeModel)11 Partition (dr.app.beagle.tools.Partition)9 BeagleSequenceSimulator (dr.app.beagle.tools.BeagleSequenceSimulator)8 NewickImporter (dr.evolution.io.NewickImporter)8 HKY (dr.evomodel.substmodel.nucleotide.HKY)8 ImportException (dr.evolution.io.Importer.ImportException)7 IOException (java.io.IOException)7 SimpleAlignment (dr.evolution.alignment.SimpleAlignment)6 TreeModel (dr.evomodel.tree.TreeModel)6 BranchModel (dr.evomodel.branchmodel.BranchModel)5 TreeDataLikelihood (dr.evomodel.treedatalikelihood.TreeDataLikelihood)5 Sequence (dr.evolution.sequence.Sequence)4