Search in sources :

Example 11 with MatrixParameter

use of dr.inference.model.MatrixParameter in project beast-mcmc by beast-dev.

the class MatrixVectorProductParameterParser method parseXMLObject.

public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    MatrixParameter matrix = (MatrixParameter) xo.getChild(MATRIX).getChild(MatrixParameter.class);
    Parameter vector = (Parameter) xo.getChild(VECTOR).getChild(Parameter.class);
    if (matrix.getColumnDimension() != vector.getDimension()) {
        throw new XMLParseException("Wrong matrix-vector dimensions in " + xo.getId());
    }
    return new MatrixVectorProductParameter(matrix, vector);
}
Also used : MatrixParameter(dr.inference.model.MatrixParameter) MatrixVectorProductParameter(dr.inference.model.MatrixVectorProductParameter) MatrixVectorProductParameter(dr.inference.model.MatrixVectorProductParameter) Parameter(dr.inference.model.Parameter) MatrixParameter(dr.inference.model.MatrixParameter)

Example 12 with MatrixParameter

use of dr.inference.model.MatrixParameter in project beast-mcmc by beast-dev.

the class Tree_Clustering_Shared_Routines method updateUndriftedVirusLocations.

public static void updateUndriftedVirusLocations(int numNodes, int numdata, TreeModel treeModel, MatrixParameter virusLocationsTreeNode, Parameter indicators, MatrixParameter mu, MatrixParameter virusLocations, int[] correspondingTreeIndexForVirus) {
    double[][] nodeloc = new double[numNodes][2];
    //process the tree and get the vLoc of the viruses..
    //breadth first depth first..
    NodeRef cNode = treeModel.getRoot();
    LinkedList<NodeRef> visitlist = new LinkedList<NodeRef>();
    visitlist.add(cNode);
    int countProcessed = 0;
    while (visitlist.size() > 0) {
        countProcessed++;
        //assign value to the current node...
        if (treeModel.getParent(cNode) == null) {
            //this means it is a root node
            Parameter curMu = mu.getParameter(cNode.getNumber());
            //Parameter curMu = mu.getParameter(0);
            nodeloc[cNode.getNumber()][0] = curMu.getParameterValue(0);
            nodeloc[cNode.getNumber()][1] = curMu.getParameterValue(1);
            Parameter curVirusLoc = virusLocationsTreeNode.getParameter(cNode.getNumber());
            curVirusLoc.setParameterValue(0, curMu.getParameterValue(0));
            curVirusLoc.setParameterValue(1, curMu.getParameterValue(1));
        } else {
            nodeloc[cNode.getNumber()][0] = nodeloc[treeModel.getParent(cNode).getNumber()][0];
            nodeloc[cNode.getNumber()][1] = nodeloc[treeModel.getParent(cNode).getNumber()][1];
            if ((int) indicators.getParameterValue(cNode.getNumber()) == 1) {
                // no +1 because I don't need another mu- the root's mu takes care of the first cluster's mu 
                Parameter curMu = mu.getParameter(cNode.getNumber());
                //Parameter curMu = mu.getParameter(cNode.getNumber() +1); //+1 because mu0 is reserved for the root.
                nodeloc[cNode.getNumber()][0] += curMu.getParameterValue(0);
                nodeloc[cNode.getNumber()][1] += curMu.getParameterValue(1);
            }
            Parameter curVirusLoc = virusLocationsTreeNode.getParameter(cNode.getNumber());
            curVirusLoc.setParameterValue(0, nodeloc[cNode.getNumber()][0]);
            curVirusLoc.setParameterValue(1, nodeloc[cNode.getNumber()][1]);
        }
        //add all the children to the queue
        for (int childNum = 0; childNum < treeModel.getChildCount(cNode); childNum++) {
            NodeRef node = treeModel.getChild(cNode, childNum);
            visitlist.add(node);
        }
        //now that we have finished visiting this node, pops it out of the queue
        visitlist.pop();
        if (visitlist.size() > 0) {
            //set the new first node in the queue to visit
            cNode = visitlist.getFirst();
        }
    }
    //write the virus locations
    for (int i = 0; i < numdata; i++) {
        Parameter vLocParameter = virusLocations.getParameter(i);
        vLocParameter.setParameterValue(0, nodeloc[correspondingTreeIndexForVirus[i]][0]);
        vLocParameter.setParameterValue(1, nodeloc[correspondingTreeIndexForVirus[i]][1]);
    }
//for(int i=0; i < numdata; i++){
//Parameter vLocP= virusLocations.getParameter(i);
//System.out.println("virus " + vLocP.getId() + "\t" + vLocP.getParameterValue(0) + "," + vLocP.getParameterValue(1)  );	  			    	
//}
}
Also used : NodeRef(dr.evolution.tree.NodeRef) Parameter(dr.inference.model.Parameter) MatrixParameter(dr.inference.model.MatrixParameter) LinkedList(java.util.LinkedList)

Example 13 with MatrixParameter

use of dr.inference.model.MatrixParameter in project beast-mcmc by beast-dev.

the class GMRFSkyrideLikelihoodParser method parseXMLObject.

public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    XMLObject cxo = xo.getChild(POPULATION_PARAMETER);
    Parameter popParameter = (Parameter) cxo.getChild(Parameter.class);
    cxo = xo.getChild(PRECISION_PARAMETER);
    Parameter precParameter = (Parameter) cxo.getChild(Parameter.class);
    cxo = xo.getChild(POPULATION_TREE);
    List<Tree> treeList = new ArrayList<Tree>();
    for (int i = 0; i < cxo.getChildCount(); i++) {
        Object testObject = cxo.getChild(i);
        if (testObject instanceof Tree) {
            treeList.add((TreeModel) testObject);
        }
    }
    //        TreeModel treeModel = (TreeModel) cxo.getChild(TreeModel.class);
    cxo = xo.getChild(GROUP_SIZES);
    Parameter groupParameter = null;
    if (cxo != null) {
        groupParameter = (Parameter) cxo.getChild(Parameter.class);
        if (popParameter.getDimension() != groupParameter.getDimension())
            throw new XMLParseException("Population and group size parameters must have the same length");
    }
    Parameter lambda;
    if (xo.getChild(LAMBDA_PARAMETER) != null) {
        cxo = xo.getChild(LAMBDA_PARAMETER);
        lambda = (Parameter) cxo.getChild(Parameter.class);
    } else {
        lambda = new Parameter.Default(LAMBDA_PARAMETER, 1.0);
    }
    Parameter gridPoints = null;
    if (xo.getChild(GRID_POINTS) != null) {
        cxo = xo.getChild(GRID_POINTS);
        gridPoints = (Parameter) cxo.getChild(Parameter.class);
    }
    Parameter numGridPoints = null;
    if (xo.getChild(NUM_GRID_POINTS) != null) {
        cxo = xo.getChild(NUM_GRID_POINTS);
        numGridPoints = (Parameter) cxo.getChild(Parameter.class);
    }
    Parameter cutOff = null;
    if (xo.getChild(CUT_OFF) != null) {
        cxo = xo.getChild(CUT_OFF);
        cutOff = (Parameter) cxo.getChild(Parameter.class);
    }
    Parameter phi = null;
    if (xo.getChild(PHI_PARAMETER) != null) {
        cxo = xo.getChild(PHI_PARAMETER);
        phi = (Parameter) cxo.getChild(Parameter.class);
    }
    List<Parameter> lastObservedIndex = null;
    if (xo.hasChildNamed(LAST_OBSERVED_INDEX)) {
        lastObservedIndex = new ArrayList<Parameter>();
        cxo = xo.getChild(LAST_OBSERVED_INDEX);
        final int numObsInd = cxo.getChildCount();
        for (int i = 0; i < numObsInd; ++i) {
            lastObservedIndex.add((Parameter) cxo.getChild(i));
        }
    }
    Parameter ploidyFactors = null;
    if (xo.getChild(PLOIDY) != null) {
        cxo = xo.getChild(PLOIDY);
        ploidyFactors = (Parameter) cxo.getChild(Parameter.class);
    } else {
        ploidyFactors = new Parameter.Default(PLOIDY, treeList.size());
        for (int i = 0; i < treeList.size(); i++) {
            ploidyFactors.setParameterValue(i, 1.0);
        }
    }
    Parameter betaParameter = null;
    if (xo.hasChildNamed(SINGLE_BETA)) {
        betaParameter = (Parameter) xo.getElementFirstChild(SINGLE_BETA);
    }
    List<Parameter> betaList = null;
    if (xo.getChild(BETA_PARAMETER) != null) {
        betaList = new ArrayList<Parameter>();
        cxo = xo.getChild(BETA_PARAMETER);
        final int numBeta = cxo.getChildCount();
        for (int i = 0; i < numBeta; ++i) {
            betaList.add((Parameter) cxo.getChild(i));
        }
    }
    MatrixParameter dMatrix = null;
    if (xo.getChild(COVARIATE_MATRIX) != null) {
        cxo = xo.getChild(COVARIATE_MATRIX);
        dMatrix = (MatrixParameter) cxo.getChild(MatrixParameter.class);
    }
    boolean timeAwareSmoothing = GMRFSkyrideLikelihood.TIME_AWARE_IS_ON_BY_DEFAULT;
    if (xo.hasAttribute(TIME_AWARE_SMOOTHING)) {
        timeAwareSmoothing = xo.getBooleanAttribute(TIME_AWARE_SMOOTHING);
    }
    if (dMatrix != null) {
        if (dMatrix.getRowDimension() != popParameter.getDimension())
            throw new XMLParseException("Design matrix row dimension must equal the population parameter length.");
        if (dMatrix.getColumnDimension() != betaParameter.getDimension())
            throw new XMLParseException("Design matrix column dimension must equal the regression coefficient length.");
    }
    List<Parameter> covPrecParam = null;
    if (xo.hasChildNamed(COV_PREC_PARAM)) {
        covPrecParam = new ArrayList<Parameter>();
        cxo = xo.getChild(COV_PREC_PARAM);
        final int numCovPrec = cxo.getChildCount();
        for (int i = 0; i < numCovPrec; ++i) {
            covPrecParam.add((Parameter) cxo.getChild(i));
        }
    }
    List<MatrixParameter> covariates = null;
    if (xo.hasChildNamed(COVARIATES)) {
        covariates = new ArrayList<MatrixParameter>();
        cxo = xo.getChild(COVARIATES);
        final int numCov = cxo.getChildCount();
        for (int i = 0; i < numCov; ++i) {
            covariates.add((MatrixParameter) cxo.getChild(i));
        }
    }
    if ((covariates != null && betaList == null) || (covariates == null && betaList != null))
        throw new XMLParseException("Must specify both a set of regression coefficients and a design matrix.");
    if (xo.getAttribute(RANDOMIZE_TREE, false)) {
        for (Tree tree : treeList) {
            if (tree instanceof TreeModel) {
                GMRFSkyrideLikelihood.checkTree((TreeModel) tree);
            } else {
                throw new XMLParseException("Can not randomize a fixed tree");
            }
        }
    }
    boolean rescaleByRootHeight = xo.getAttribute(RESCALE_BY_ROOT_ISSUE, true);
    Logger.getLogger("dr.evomodel").info("The " + SKYLINE_LIKELIHOOD + " has " + (timeAwareSmoothing ? "time aware smoothing" : "uniform smoothing"));
    if (xo.getAttribute(OLD_SKYRIDE, true) && xo.getName().compareTo(SKYGRID_LIKELIHOOD) != 0) {
        return new GMRFSkyrideLikelihood(treeList, popParameter, groupParameter, precParameter, lambda, betaParameter, dMatrix, timeAwareSmoothing, rescaleByRootHeight);
    } else {
        if (xo.getChild(GRID_POINTS) != null) {
            return new GMRFMultilocusSkyrideLikelihood(treeList, popParameter, groupParameter, precParameter, lambda, betaParameter, dMatrix, timeAwareSmoothing, gridPoints, covariates, ploidyFactors, lastObservedIndex, covPrecParam, betaList);
        } else {
            return new GMRFMultilocusSkyrideLikelihood(treeList, popParameter, groupParameter, precParameter, lambda, betaParameter, dMatrix, timeAwareSmoothing, cutOff.getParameterValue(0), (int) numGridPoints.getParameterValue(0), phi, ploidyFactors);
        }
    }
}
Also used : MatrixParameter(dr.inference.model.MatrixParameter) ArrayList(java.util.ArrayList) GMRFSkyrideLikelihood(dr.evomodel.coalescent.GMRFSkyrideLikelihood) TreeModel(dr.evomodel.tree.TreeModel) Parameter(dr.inference.model.Parameter) MatrixParameter(dr.inference.model.MatrixParameter) Tree(dr.evolution.tree.Tree) GMRFMultilocusSkyrideLikelihood(dr.evomodel.coalescent.GMRFMultilocusSkyrideLikelihood)

Example 14 with MatrixParameter

use of dr.inference.model.MatrixParameter in project beast-mcmc by beast-dev.

the class HierarchicalGraphLikelihoodParser method parseXMLObject.

public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    XMLObject cxo = xo.getChild(HIERARCHICAL_INDICATOR);
    Parameter hierarchicalIndicator = (Parameter) cxo.getChild(Parameter.class);
    cxo = xo.getChild(STRATA_INDICATOR);
    MatrixParameter strataIndicatorMatrix = new MatrixParameter("matrix");
    int dim = 0;
    for (int i = 0; i < cxo.getChildCount(); i++) {
        Parameter parameter = (Parameter) cxo.getChild(i);
        strataIndicatorMatrix.addParameter(parameter);
        if (i == 0)
            dim = parameter.getDimension();
        else if (dim != parameter.getDimension())
            throw new XMLParseException("All parameters must have the same dimension to construct a rectangular matrix");
    }
    if (hierarchicalIndicator.getDimension() != strataIndicatorMatrix.getRowDimension())
        throw new XMLParseException("Hierarchical and starta parameters don't have the same dimentions");
    cxo = xo.getChild(PROB);
    Parameter prob = (Parameter) cxo.getChild(Parameter.class);
    return new HierarchicalGraphLikelihood(hierarchicalIndicator, strataIndicatorMatrix, prob);
}
Also used : MatrixParameter(dr.inference.model.MatrixParameter) Parameter(dr.inference.model.Parameter) MatrixParameter(dr.inference.model.MatrixParameter) HierarchicalGraphLikelihood(dr.inference.distribution.HierarchicalGraphLikelihood)

Example 15 with MatrixParameter

use of dr.inference.model.MatrixParameter in project beast-mcmc by beast-dev.

the class MVOUCovarianceOperatorParser method parseXMLObject.

public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    CoercionMode mode = CoercionMode.parseMode(xo);
    double weight = xo.getDoubleAttribute(MCMCOperator.WEIGHT);
    double mixingFactor = xo.getDoubleAttribute(MIXING_FACTOR);
    int priorDf = xo.getIntegerAttribute(PRIOR_DF);
    if (mixingFactor <= 0.0 || mixingFactor >= 1.0) {
        throw new XMLParseException("mixingFactor must be greater than 0.0 and less thatn 1.0");
    }
    //            Parameter parameter = (Parameter) xo.getChild(Parameter.class);
    //            XMLObject cxo = (XMLObject) xo.getChild(VARIANCE_MATRIX);
    MatrixParameter varMatrix = (MatrixParameter) xo.getChild(MatrixParameter.class);
    if (varMatrix.getColumnDimension() != varMatrix.getRowDimension())
        throw new XMLParseException("The variance matrix is not square");
    return new MVOUCovarianceOperator(mixingFactor, varMatrix, priorDf, weight, mode);
}
Also used : MVOUCovarianceOperator(dr.inference.operators.MVOUCovarianceOperator) MatrixParameter(dr.inference.model.MatrixParameter) CoercionMode(dr.inference.operators.CoercionMode)

Aggregations

MatrixParameter (dr.inference.model.MatrixParameter)15 Parameter (dr.inference.model.Parameter)13 LinkedList (java.util.LinkedList)3 CoercionMode (dr.inference.operators.CoercionMode)2 MultivariateTraitTree (dr.evolution.tree.MultivariateTraitTree)1 NodeRef (dr.evolution.tree.NodeRef)1 Tree (dr.evolution.tree.Tree)1 GMRFMultilocusSkyrideLikelihood (dr.evomodel.coalescent.GMRFMultilocusSkyrideLikelihood)1 GMRFSkyrideLikelihood (dr.evomodel.coalescent.GMRFSkyrideLikelihood)1 TreeModel (dr.evomodel.tree.TreeModel)1 TreeTraitParserUtilities (dr.evomodelxml.treelikelihood.TreeTraitParserUtilities)1 HierarchicalGraphLikelihood (dr.inference.distribution.HierarchicalGraphLikelihood)1 MomentDistributionModel (dr.inference.distribution.MomentDistributionModel)1 MultivariateNormalDistributionModel (dr.inference.distribution.MultivariateNormalDistributionModel)1 CompoundParameter (dr.inference.model.CompoundParameter)1 LatentFactorModel (dr.inference.model.LatentFactorModel)1 MaskedParameter (dr.inference.model.MaskedParameter)1 MatrixMatrixProduct (dr.inference.model.MatrixMatrixProduct)1 MatrixVectorProductParameter (dr.inference.model.MatrixVectorProductParameter)1 MVOUCovarianceOperator (dr.inference.operators.MVOUCovarianceOperator)1