Search in sources :

Example 6 with MatrixParameter

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

the class TreeClusterAlgorithmOperator method Propose_YandI.

private double Propose_YandI() {
    //first, find a random Y and walk
    int serum_selected = (int) (Math.floor(Math.random() * getNumSera()));
    MatrixParameter serumLocations = getSerumLocationsParameter();
    Parameter serum = serumLocations.getParameter(serum_selected);
    // assume dimension 2
    int whichDimension = (int) (Math.floor(Math.random() * 2));
    double oldValue = serum.getParameterValue(whichDimension);
    double change = Math.random() * WALK_SIZE - WALK_SIZE / 2;
    double value = oldValue + change;
    //WAIT.. IF REJECT, DOES IT RESET?
    serum.setParameterValue(whichDimension, value);
    int rootNum = treeModel.getRoot().getNumber();
    //second, find a RANDOM "on" breakpoint and multistep it..
    //find an on-node	
    int originalNode1 = findAnOnNodeRandomly();
    //0. Keep a copy of the original state to calculate the backward move
    //System.out.println("Original breakpoint is " + originalNode1 + " and the original AGlikelihood is " + clusterLikelihood.getLogLikelihood());
    int[] numStepsFromI_selected = determineTreeNeighborhood(originalNode1, 100000);
    //1. Select an unoccupied site within some steps away from it.
    LinkedList<Integer> possibilities1 = new LinkedList<Integer>();
    for (int i = 0; i < numNodes; i++) {
        // System.out.println("#steps from I_selected " + numStepsFromI_selected[i]);
        //make sure no self select
        boolean isIn1 = numStepsFromI_selected[i] <= maxNodeLevel && numStepsFromI_selected[i] != 0 && i != rootNum;
        if (isIn1) {
            possibilities1.addLast(new Integer(i));
        }
    }
    //end for
    int numPossibilities1 = possibilities1.size();
    int whichMove = (int) (Math.floor(Math.random() * numPossibilities1));
    int site_add1 = possibilities1.get(whichMove).intValue();
    // System.out.println("selected node = " + site_add1 + " that's " + numStepsFromI_selected[site_add1] + " steps from " + originalNode1);
    //set the old selected index off
    indicators.setParameterValue(originalNode1, 0);
    //set the new selected index on
    indicators.setParameterValue(site_add1, 1);
    //just want to see even if MH ratio is 1, does it ever get accepted..
    return 0;
}
Also used : MatrixParameter(dr.inference.model.MatrixParameter) MatrixParameter(dr.inference.model.MatrixParameter) Parameter(dr.inference.model.Parameter) LinkedList(java.util.LinkedList)

Example 7 with MatrixParameter

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

the class TreeClusterAlgorithmOperator method Propose_YandIandmu.

private double Propose_YandIandmu() {
    int rootNum = treeModel.getRoot().getNumber();
    //first, find a random Y and walk
    int serum_selected = (int) (Math.floor(Math.random() * getNumSera()));
    MatrixParameter serumLocations = getSerumLocationsParameter();
    Parameter serum = serumLocations.getParameter(serum_selected);
    // assume dimension 2
    int whichDimension = (int) (Math.floor(Math.random() * 2));
    double oldValue = serum.getParameterValue(whichDimension);
    double change = Math.random() * WALK_SIZE - WALK_SIZE / 2;
    double value = oldValue + change;
    //WAIT.. IF REJECT, DOES IT RESET?
    serum.setParameterValue(whichDimension, value);
    //change I
    //second, find a RANDOM "on" breakpoint and multistep it..
    //0. Keep a copy of the original state to calculate the backward move
    int originalNode1 = findAnOnNodeRandomly();
    //System.out.println("Original breakpoint is " + originalNode1 + " and the original AGlikelihood is " + clusterLikelihood.getLogLikelihood());
    int[] numStepsFromI_selected = determineTreeNeighborhood(originalNode1, 100000);
    //1. Select an unoccupied site within some steps away from it.
    LinkedList<Integer> possibilities1 = new LinkedList<Integer>();
    for (int i = 0; i < numNodes; i++) {
        // System.out.println("#steps from I_selected " + numStepsFromI_selected[i]);
        //make sure no self select
        boolean isIn1 = numStepsFromI_selected[i] <= maxNodeLevel && numStepsFromI_selected[i] != 0 && i != rootNum;
        if (isIn1) {
            possibilities1.addLast(new Integer(i));
        }
    }
    //end for
    int numPossibilities1 = possibilities1.size();
    int whichMove = (int) (Math.floor(Math.random() * numPossibilities1));
    int site_add1 = possibilities1.get(whichMove).intValue();
    // System.out.println("selected node = " + site_add1 + " that's " + numStepsFromI_selected[site_add1] + " steps from " + originalNode1);
    //set the new selected index to the new node.
    indicators.setParameterValue(site_add1, 1);
    //set the new selected index to the new node.
    indicators.setParameterValue(originalNode1, 0);
    double[] oldValues = mu.getParameter(site_add1).getParameterValues();
    //double[] oldValues = mu.getParameter(site_add1+1).getParameterValues();	
    //System.out.println(oldValues[0]  + ", " + oldValues[1]); 
    double[] mean = new double[2];
    mean[0] = 0;
    mean[1] = 0;
    double[][] precisionM = new double[2][2];
    //double precision = 1/TreeClusterViruses.getSigmaSq();
    double precision = muPrecision.getParameterValue(0);
    precisionM[0][0] = precision;
    precisionM[0][1] = 0;
    precisionM[1][0] = 0;
    precisionM[1][1] = precision;
    double[] values = MultivariateNormalDistribution.nextMultivariateNormalPrecision(mean, precisionM);
    //System.out.println(values[0]  + ", " + values[1]); 
    mu.getParameter(site_add1).setParameterValue(0, values[0]);
    mu.getParameter(site_add1).setParameterValue(1, values[1]);
    //mu.getParameter(site_add1+1).setParameterValue(0,values[0]);
    //mu.getParameter(site_add1+1).setParameterValue(1,values[1]);
    double logHastingRatio = MultivariateNormalDistribution.logPdf(oldValues, mean, precision, 1) - MultivariateNormalDistribution.logPdf(values, mean, precision, 1);
    return logHastingRatio;
}
Also used : MatrixParameter(dr.inference.model.MatrixParameter) MatrixParameter(dr.inference.model.MatrixParameter) Parameter(dr.inference.model.Parameter) LinkedList(java.util.LinkedList)

Example 8 with MatrixParameter

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

the class TreeClusterAlgorithmOperator method Propose_YandMu.

private double Propose_YandMu() {
    //first, find a random Y and walk
    int serum_selected = (int) (Math.floor(Math.random() * getNumSera()));
    MatrixParameter serumLocations = getSerumLocationsParameter();
    Parameter serum = serumLocations.getParameter(serum_selected);
    // assume dimension 2
    int whichDimension = (int) (Math.floor(Math.random() * 2));
    double oldValue = serum.getParameterValue(whichDimension);
    double change = Math.random() * WALK_SIZE - WALK_SIZE / 2;
    double value = oldValue + change;
    //WAIT.. IF REJECT, DOES IT RESET?
    serum.setParameterValue(whichDimension, value);
    //find an on-node	
    int selectedIndex = findAnOnNodeRandomly();
    double[] oldValues = mu.getParameter(selectedIndex).getParameterValues();
    //double[] oldValues = mu.getParameter(selectedIndex+1).getParameterValues();	
    //System.out.println(oldValues[0]  + ", " + oldValues[1]); 
    double[] mean = new double[2];
    mean[0] = 0;
    mean[1] = 0;
    double[][] precisionM = new double[2][2];
    //double precision = 1/TreeClusterViruses.getSigmaSq();
    double precision = muPrecision.getParameterValue(0);
    precisionM[0][0] = precision;
    precisionM[0][1] = 0;
    precisionM[1][0] = 0;
    precisionM[1][1] = precision;
    double[] values = MultivariateNormalDistribution.nextMultivariateNormalPrecision(mean, precisionM);
    //System.out.println(values[0]  + ", " + values[1]);
    mu.getParameter(selectedIndex).setParameterValue(0, values[0]);
    mu.getParameter(selectedIndex).setParameterValue(1, values[1]);
    //mu.getParameter(selectedIndex+1).setParameterValue(0,values[0]);
    //mu.getParameter(selectedIndex+1).setParameterValue(1,values[1]);
    double logHastingRatio = MultivariateNormalDistribution.logPdf(oldValues, mean, precision, 1) - MultivariateNormalDistribution.logPdf(values, mean, precision, 1);
    //System.out.println("The first node selected is " + site_add);
    return logHastingRatio;
}
Also used : MatrixParameter(dr.inference.model.MatrixParameter) MatrixParameter(dr.inference.model.MatrixParameter) Parameter(dr.inference.model.Parameter)

Example 9 with MatrixParameter

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

the class LoadingsHamiltonianMCParser method parseXMLObject.

@Override
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    LatentFactorModel lfm = (LatentFactorModel) xo.getChild(LatentFactorModel.class);
    MomentDistributionModel prior = (MomentDistributionModel) xo.getChild(MomentDistributionModel.class);
    double weight = xo.getDoubleAttribute(WEIGHT);
    CoercionMode mode = CoercionMode.parseMode(xo);
    int nSteps = xo.getIntegerAttribute(N_STEPS);
    double stepSize = xo.getDoubleAttribute(STEP_SIZE);
    double momentumSd = xo.getDoubleAttribute(MOMENTUM_SD);
    MatrixParameter loadings = (MatrixParameter) xo.getChild(MatrixParameter.class);
    return new LoadingsHamiltonianMC(lfm, prior, weight, mode, stepSize, nSteps, momentumSd, loadings);
}
Also used : MatrixParameter(dr.inference.model.MatrixParameter) LoadingsHamiltonianMC(dr.inference.operators.hmc.deprecated.LoadingsHamiltonianMC) LatentFactorModel(dr.inference.model.LatentFactorModel) MomentDistributionModel(dr.inference.distribution.MomentDistributionModel) CoercionMode(dr.inference.operators.CoercionMode)

Example 10 with MatrixParameter

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

the class MaskedMatrixParameterParser method parseXMLObject.

public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    MatrixParameter matrix = (MatrixParameter) xo.getChild(MatrixParameter.class);
    //        System.err.println("colDim " + matrix.getColumnDimension());
    //        System.err.println("rowDim " + matrix.getRowDimension());
    //        System.err.println("parDim " + matrix.getParameterCount());
    Parameter mask;
    XMLObject cxo = xo.getChild(MASKING);
    if (cxo != null) {
        mask = (Parameter) cxo.getChild(Parameter.class);
    } else {
        int from = xo.getAttribute(FROM, 1) - 1;
        int to = xo.getAttribute(TO, matrix.getRowDimension()) - 1;
        int every = xo.getAttribute(EVERY, 1);
        if (from < 0)
            throw new XMLParseException("illegal 'from' attribute in " + MASKED_MATRIX_PARAMETER + " element");
        if (to < 0 || to < from)
            throw new XMLParseException("illegal 'to' attribute in " + MASKED_MATRIX_PARAMETER + " element");
        if (every <= 0)
            throw new XMLParseException("illegal 'every' attribute in " + MASKED_MATRIX_PARAMETER + " element");
        mask = new Parameter.Default(matrix.getRowDimension(), 0.0);
        for (int i = from; i <= to; i += every) {
            mask.setParameterValue(i, 1.0);
        }
    }
    if (mask.getDimension() != matrix.getRowDimension())
        throw new XMLParseException("rowDim(" + matrix.getId() + ") != dim(" + mask.getId() + ")");
    boolean ones = !xo.getAttribute(COMPLEMENT, false);
    MaskedParameter[] maskedParameters = new MaskedParameter[matrix.getColumnDimension()];
    for (int col = 0; col < matrix.getColumnDimension(); ++col) {
        maskedParameters[col] = new MaskedParameter(matrix.getParameter(col));
        maskedParameters[col].addMask(mask, ones);
    }
    MatrixParameter maskedMatrix = new MatrixParameter(matrix.getId() + ".masked", maskedParameters);
    return maskedMatrix;
}
Also used : MaskedParameter(dr.inference.model.MaskedParameter) MatrixParameter(dr.inference.model.MatrixParameter) MaskedParameter(dr.inference.model.MaskedParameter) Parameter(dr.inference.model.Parameter) MatrixParameter(dr.inference.model.MatrixParameter)

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