Search in sources :

Example 1 with CompoundGradient

use of dr.inference.hmc.CompoundGradient in project beast-mcmc by beast-dev.

the class AppendedPotentialDerivativeParser method parseXMLObject.

@Override
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    List<GradientWrtParameterProvider> gradList = new ArrayList<GradientWrtParameterProvider>();
    // TODO Remove?
    List<Likelihood> likelihoodList = new ArrayList<Likelihood>();
    for (int i = 0; i < xo.getChildCount(); ++i) {
        Object obj = xo.getChild(i);
        GradientWrtParameterProvider grad;
        Likelihood likelihood;
        if (obj instanceof DistributionLikelihood) {
            DistributionLikelihood dl = (DistributionLikelihood) obj;
            if (!(dl.getDistribution() instanceof GradientProvider)) {
                throw new XMLParseException("Not a gradient provider");
            }
            throw new RuntimeException("Not yet implemented");
        } else if (obj instanceof MultivariateDistributionLikelihood) {
            final MultivariateDistributionLikelihood mdl = (MultivariateDistributionLikelihood) obj;
            if (!(mdl.getDistribution() instanceof GradientProvider)) {
                throw new XMLParseException("Not a gradient provider");
            }
            final GradientProvider provider = (GradientProvider) mdl.getDistribution();
            final Parameter parameter = mdl.getDataParameter();
            likelihood = mdl;
            grad = new GradientWrtParameterProvider.ParameterWrapper(provider, parameter, mdl);
        } else if (obj instanceof GradientWrtParameterProvider) {
            grad = (GradientWrtParameterProvider) obj;
            likelihood = grad.getLikelihood();
        } else {
            throw new XMLParseException("Not a Gaussian process");
        }
        gradList.add(grad);
        likelihoodList.add(likelihood);
    }
    return new CompoundGradient(gradList);
}
Also used : CompoundGradient(dr.inference.hmc.CompoundGradient) MultivariateDistributionLikelihood(dr.inference.distribution.MultivariateDistributionLikelihood) DistributionLikelihood(dr.inference.distribution.DistributionLikelihood) MultivariateDistributionLikelihood(dr.inference.distribution.MultivariateDistributionLikelihood) ArrayList(java.util.ArrayList) GradientWrtParameterProvider(dr.inference.hmc.GradientWrtParameterProvider) DistributionLikelihood(dr.inference.distribution.DistributionLikelihood) MultivariateDistributionLikelihood(dr.inference.distribution.MultivariateDistributionLikelihood)

Example 2 with CompoundGradient

use of dr.inference.hmc.CompoundGradient in project beast-mcmc by beast-dev.

the class DiffusionGradientParser method parseXMLObject.

@Override
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    String traitName = xo.getAttribute(TRAIT_NAME, DEFAULT_TRAIT_NAME);
    List<ContinuousTraitGradientForBranch.ContinuousProcessParameterGradient.DerivationParameter> derivationParametersList = new ArrayList<ContinuousTraitGradientForBranch.ContinuousProcessParameterGradient.DerivationParameter>();
    CompoundParameter compoundParameter = new CompoundParameter(null);
    List<GradientWrtParameterProvider> derivativeList = new ArrayList<GradientWrtParameterProvider>();
    List<AbstractDiffusionGradient> diffGradients = xo.getAllChildren(AbstractDiffusionGradient.class);
    if (diffGradients != null) {
        for (AbstractDiffusionGradient grad : diffGradients) {
            derivationParametersList.add(grad.getDerivationParameter());
            compoundParameter.addParameter(grad.getRawParameter());
            derivativeList.add(grad);
        }
    }
    CompoundGradient parametersGradients = new CompoundDerivative(derivativeList);
    // testSameModel(precisionGradient, attenuationGradient);
    TreeDataLikelihood treeDataLikelihood = ((TreeDataLikelihood) diffGradients.get(0).getLikelihood());
    DataLikelihoodDelegate delegate = treeDataLikelihood.getDataLikelihoodDelegate();
    int dim = treeDataLikelihood.getDataLikelihoodDelegate().getTraitDim();
    Tree tree = treeDataLikelihood.getTree();
    ContinuousDataLikelihoodDelegate continuousData = (ContinuousDataLikelihoodDelegate) delegate;
    ContinuousTraitGradientForBranch.ContinuousProcessParameterGradient traitGradient = new ContinuousTraitGradientForBranch.ContinuousProcessParameterGradient(dim, tree, continuousData, derivationParametersList);
    BranchSpecificGradient branchSpecificGradient = new BranchSpecificGradient(traitName, treeDataLikelihood, continuousData, traitGradient, compoundParameter);
    return new DiffusionParametersGradient(branchSpecificGradient, parametersGradients);
}
Also used : CompoundGradient(dr.inference.hmc.CompoundGradient) BranchSpecificGradient(dr.evomodel.treedatalikelihood.continuous.BranchSpecificGradient) AbstractDiffusionGradient(dr.evomodel.treedatalikelihood.hmc.AbstractDiffusionGradient) ArrayList(java.util.ArrayList) CompoundParameter(dr.inference.model.CompoundParameter) DiffusionParametersGradient(dr.evomodel.treedatalikelihood.hmc.DiffusionParametersGradient) TreeDataLikelihood(dr.evomodel.treedatalikelihood.TreeDataLikelihood) ContinuousDataLikelihoodDelegate(dr.evomodel.treedatalikelihood.continuous.ContinuousDataLikelihoodDelegate) DataLikelihoodDelegate(dr.evomodel.treedatalikelihood.DataLikelihoodDelegate) CompoundDerivative(dr.inference.hmc.CompoundDerivative) GradientWrtParameterProvider(dr.inference.hmc.GradientWrtParameterProvider) Tree(dr.evolution.tree.Tree) ContinuousTraitGradientForBranch(dr.evomodel.treedatalikelihood.continuous.ContinuousTraitGradientForBranch) ContinuousDataLikelihoodDelegate(dr.evomodel.treedatalikelihood.continuous.ContinuousDataLikelihoodDelegate)

Aggregations

CompoundGradient (dr.inference.hmc.CompoundGradient)2 GradientWrtParameterProvider (dr.inference.hmc.GradientWrtParameterProvider)2 ArrayList (java.util.ArrayList)2 Tree (dr.evolution.tree.Tree)1 DataLikelihoodDelegate (dr.evomodel.treedatalikelihood.DataLikelihoodDelegate)1 TreeDataLikelihood (dr.evomodel.treedatalikelihood.TreeDataLikelihood)1 BranchSpecificGradient (dr.evomodel.treedatalikelihood.continuous.BranchSpecificGradient)1 ContinuousDataLikelihoodDelegate (dr.evomodel.treedatalikelihood.continuous.ContinuousDataLikelihoodDelegate)1 ContinuousTraitGradientForBranch (dr.evomodel.treedatalikelihood.continuous.ContinuousTraitGradientForBranch)1 AbstractDiffusionGradient (dr.evomodel.treedatalikelihood.hmc.AbstractDiffusionGradient)1 DiffusionParametersGradient (dr.evomodel.treedatalikelihood.hmc.DiffusionParametersGradient)1 DistributionLikelihood (dr.inference.distribution.DistributionLikelihood)1 MultivariateDistributionLikelihood (dr.inference.distribution.MultivariateDistributionLikelihood)1 CompoundDerivative (dr.inference.hmc.CompoundDerivative)1 CompoundParameter (dr.inference.model.CompoundParameter)1