Search in sources :

Example 56 with Parameter

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

the class TreeClusterAlgorithmOperator method Propose_changeMuAndBalance.

private double Propose_changeMuAndBalance() {
    //System.out.println("root: " + mu.getParameter(0).getParameterValue(0) + "," + mu.getParameter(0).getParameterValue(1));
    //for(int i=0; i < numNodes; i++){
    //	if( (int) indicators.getParameterValue(i) == 1){
    //		System.out.println(i + ": " + mu.getParameter(i+1).getParameterValue(0) + "," + mu.getParameter(i+1).getParameterValue(1));
    //	}
    //}
    //first, randomly select an "on" node to overwrite
    //find an on-node	
    int originalNode = findAnOnNodeIncludingRootRandomly();
    //originalNode = 673;
    //if(originalNode == 802){
    //System.out.println(treeModel.getRoot().getNumber());
    //System.out.println("I am walking 802!");
    //}
    //unbounded walk
    int dimSelect = (int) Math.floor(Math.random() * 2);
    double change = Math.random() * WALK_SIZE - WALK_SIZE / 2;
    //dimSelect = 0;
    //change = 10;	
    //double originalValue = mu.getParameter(originalNode +1).getParameterValue(dimSelect);
    double originalValue = mu.getParameter(originalNode).getParameterValue(dimSelect);
    //System.out.println("originalValue = " + originalValue);
    mu.getParameter(originalNode).setParameterValue(dimSelect, originalValue + change);
    //mu.getParameter(originalNode + 1).setParameterValue(dimSelect, originalValue + change);
    //System.out.println("original node = " + originalNode);
    //a. by removing the selected node, each child of this node should be updated to keep the absolute location of 
    //the child cluster fixed as before
    LinkedList<Integer> childrenOriginalNode = findActiveBreakpointsChildren(originalNode);
    //}
    for (int i = 0; i < childrenOriginalNode.size(); i++) {
        //int muIndexNum = childrenOriginalNode.get(i).intValue() + 1;
        int muIndexNum = childrenOriginalNode.get(i).intValue();
        //if(originalNode == 802){
        //System.out.println(" " + muIndexNum + " is a child");
        //}
        Parameter curMu = mu.getParameter(muIndexNum);
        double curMu_original = curMu.getParameterValue(dimSelect);
        mu.getParameter(muIndexNum).setParameterValue(dimSelect, curMu_original - change);
    //System.out.println( " " + ( muIndexNum - 1) + " is a child");
    }
    return (0);
}
Also used : MatrixParameter(dr.inference.model.MatrixParameter) Parameter(dr.inference.model.Parameter)

Example 57 with Parameter

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

the class RandomWalkOnActiveMu method doOperation.

public double doOperation() {
    //first, randomly select an "on" node to overwrite
    //find an on-node	
    int originalNode = Tree_Clustering_Shared_Routines.findAnOnNodeIncludingRootRandomly(numNodes, indicators);
    //unbounded walk
    int dimSelect = (int) Math.floor(Math.random() * 2);
    double change = (2.0 * MathUtils.nextDouble() - 1.0) * windowSize;
    double originalValue = mu.getParameter(originalNode).getParameterValue(dimSelect);
    mu.getParameter(originalNode).setParameterValue(dimSelect, originalValue + change);
    //a. by removing the selected node, each child of this node should be updated to keep the absolute location of 
    //the child cluster fixed as before
    LinkedList<Integer> childrenOriginalNode = Tree_Clustering_Shared_Routines.findActiveBreakpointsChildren(originalNode, numNodes, treeModel, indicators);
    for (int i = 0; i < childrenOriginalNode.size(); i++) {
        int muIndexNum = childrenOriginalNode.get(i).intValue();
        Parameter curMu = mu.getParameter(muIndexNum);
        double curMu_original = curMu.getParameterValue(dimSelect);
        mu.getParameter(muIndexNum).setParameterValue(dimSelect, curMu_original - change);
    }
    //the virus location needs to be updated because the mu's are updated 	  				
    Tree_Clustering_Shared_Routines.updateUndriftedVirusLocations(numNodes, numdata, treeModel, virusLocationsTreeNode, indicators, mu, virusLocations, correspondingTreeIndexForVirus);
    return 0.0;
}
Also used : MatrixParameter(dr.inference.model.MatrixParameter) Parameter(dr.inference.model.Parameter)

Example 58 with Parameter

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

the class TreeClusterAlgorithmOperator method Proposal_flipIBalance.

private double Proposal_flipIBalance() {
    //System.out.println("hi it got run");
    //System.exit(0);
    //System.out.println("root: " + mu.getParameter(0).getParameterValue(0) + "," + mu.getParameter(0).getParameterValue(1));
    //for(int i=0; i < numNodes; i++){
    //if( (int) indicators.getParameterValue(i) == 1){
    //System.out.println(i + ": " + mu.getParameter(i+1).getParameterValue(0) + "," + mu.getParameter(i+1).getParameterValue(1));
    //}
    //}
    int node = findNodeRandomly();
    //int node = (int) (Math.floor(Math.random()*numNodes));
    //node = 785;
    //System.out.println("selected node " + node);
    //double[] originalValues = mu.getParameter(node +1).getParameterValues();	
    double[] originalValues = mu.getParameter(node).getParameterValues();
    //System.out.println(originalValues[0] + " and " + originalValues[1]);
    //a. by turning on the selected node, each child of this node should be updated to keep the absolute location of 
    //the child cluster fixed as before
    LinkedList<Integer> childrenOriginalNode = findActiveBreakpointsChildren(node);
    if ((int) indicators.getParameterValue(node) == 0) {
        indicators.setParameterValue(node, 1);
        for (int i = 0; i < childrenOriginalNode.size(); i++) {
            int muIndexNum = childrenOriginalNode.get(i).intValue();
            //int muIndexNum = childrenOriginalNode.get(i).intValue() + 1;
            Parameter curMu = mu.getParameter(muIndexNum);
            double curMu_original0 = curMu.getParameterValue(0);
            mu.getParameter(muIndexNum).setParameterValue(0, curMu_original0 - originalValues[0]);
            double curMu_original1 = curMu.getParameterValue(1);
            mu.getParameter(muIndexNum).setParameterValue(1, curMu_original1 - originalValues[1]);
        //System.out.println( " " + ( muIndexNum - 1) + " is a child");
        }
    } else {
        indicators.setParameterValue(node, 0);
        //System.out.println("turn it off");
        for (int i = 0; i < childrenOriginalNode.size(); i++) {
            int muIndexNum = childrenOriginalNode.get(i).intValue();
            //int muIndexNum = childrenOriginalNode.get(i).intValue() + 1;
            Parameter curMu = mu.getParameter(muIndexNum);
            double curMu_original0 = curMu.getParameterValue(0);
            mu.getParameter(muIndexNum).setParameterValue(0, curMu_original0 + originalValues[0]);
            double curMu_original1 = curMu.getParameterValue(1);
            mu.getParameter(muIndexNum).setParameterValue(1, curMu_original1 + originalValues[1]);
        //System.out.println( " " + ( muIndexNum - 1) + " is a child");
        }
    }
    double coord1 = mu1Scale.getParameterValue(0) * originalValues[0];
    double coord2 = mu2Scale.getParameterValue(0) * originalValues[1];
    muDistance = Math.sqrt(coord1 * coord1 + coord2 * coord2);
    return (0);
}
Also used : MatrixParameter(dr.inference.model.MatrixParameter) Parameter(dr.inference.model.Parameter)

Example 59 with Parameter

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

the class TreeClusterAlgorithmOperator method Propose_branchOffFlip.

private double Propose_branchOffFlip() {
    //first, randomly select an "on" node to overwrite
    //find an on-node	
    int originalNode = findAnOnNodeRandomly();
    //second, randomly select a destination
    //sample a node that's not in the cluster.
    int site_add = findAnOffNodeRandomly();
    //existing mu
    Parameter selectedMu = mu.getParameter(originalNode);
    //Parameter selectedMu = mu.getParameter(originalNode +1) ;
    double selectedMu0 = selectedMu.getParameterValue(0);
    double selectedMu1 = selectedMu.getParameterValue(1);
    //a. by removing the selected node, each child of this node should be updated to keep the absolute location of 
    //the child cluster fixed as before
    LinkedList<Integer> childrenOriginalNode = findActiveBreakpointsChildren(originalNode);
    for (int i = 0; i < childrenOriginalNode.size(); i++) {
        int muIndexNum = childrenOriginalNode.get(i).intValue();
        //int muIndexNum = childrenOriginalNode.get(i).intValue() + 1;
        Parameter curMu = mu.getParameter(muIndexNum);
        double mu0 = curMu.getParameterValue(0) + selectedMu0;
        double mu1 = curMu.getParameterValue(1) + selectedMu1;
        mu.getParameter(muIndexNum).setParameterValue(0, mu0);
        mu.getParameter(muIndexNum).setParameterValue(1, mu1);
    }
    //set indicators AND NEW MU
    indicators.setParameterValue(site_add, 1);
    //indicators.setParameterValue(originalNode, 0); //just flip it on. do not replace
    //I think this generate a situation where if a mu walks off, then it gets the breakpoint that doesn't partition.
    //this creates a scenario where a breakpoint is lost
    //double change = Math.random()*WALK_SIZE - WALK_SIZE ; 
    //double newMu0 = selectedMu0 + change;
    //double change2 = Math.random()*WALK_SIZE- WALK_SIZE ; 
    //double newMu1 = selectedMu1 + change2;
    double[] oldValues = mu.getParameter(site_add).getParameterValues();
    //double[] oldValues = mu.getParameter(site_add+1).getParameterValues();
    //System.out.println(oldValues[0]  + ", " + oldValues[1]);
    //instead, sample from the normal distribution
    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_add).setParameterValue(0, values[0]);
    mu.getParameter(site_add).setParameterValue(1, values[1]);
    //mu.getParameter(site_add+1).setParameterValue(0,values[0]);
    //mu.getParameter(site_add+1).setParameterValue(1,values[1]);
    selectedMu.setParameterValue(0, values[0]);
    selectedMu.setParameterValue(1, values[1]);
    //b. by adding the new selected node, each child of this new node should be updated to keep the absolute location of 
    //the child cluster fixed as before
    LinkedList<Integer> childrenNewNode = findActiveBreakpointsChildren(site_add);
    for (int i = 0; i < childrenNewNode.size(); i++) {
        //int muIndexNum = childrenNewNode.get(i).intValue() + 1;
        int muIndexNum = childrenNewNode.get(i).intValue();
        Parameter curMu = mu.getParameter(muIndexNum);
        double mu0 = curMu.getParameterValue(0) - values[0];
        double mu1 = curMu.getParameterValue(1) - values[1];
        mu.getParameter(muIndexNum).setParameterValue(0, mu0);
        mu.getParameter(muIndexNum).setParameterValue(1, mu1);
    }
    double logHastingRatio = MultivariateNormalDistribution.logPdf(oldValues, mean, precision, 1) - MultivariateNormalDistribution.logPdf(values, mean, precision, 1);
    return (logHastingRatio);
}
Also used : MatrixParameter(dr.inference.model.MatrixParameter) Parameter(dr.inference.model.Parameter)

Example 60 with Parameter

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

the class TreeClusterAlgorithmOperator method PrintsetMembershipTreeToVirusIndexes.

/*
	private void setMembershipTreeToVirusIndexes(){

  	   //I suspect this is an expensive operation, so I don't want to do it many times,
  	   //which is also unnecessary  - MAY have to update whenever a different tree is used.
         correspondingTreeIndexForVirus = new int[numdata]; 
         for(int i=0; i < numdata; i++){
  		   Parameter v = virusLocations.getParameter(i);
  		   String curName = v.getParameterName();
  		  // System.out.println(curName);
  		   int isFound = 0;
      	   for(int j=0; j < numNodes; j++){
      		   String treeId = treeModel.getTaxonId(j);
      		   if(curName.equals(treeId) ){
      		//	   System.out.println("  isFound at j=" + j);
      			   correspondingTreeIndexForVirus[i] = j;
      			   isFound=1;
      			   break;
      		   }	   
      	   }
      	   if(isFound ==0){
      		   System.out.println("not found. Exit now.");
      		   System.exit(0);
      	   }     	   
         }
    }
*/
private void PrintsetMembershipTreeToVirusIndexes() {
    //I suspect this is an expensive operation, so I don't want to do it many times,
    //which is also unnecessary  - MAY have to update whenever a different tree is used.
    correspondingTreeIndexForVirus = new int[numdata];
    for (int i = 0; i < numdata; i++) {
        Parameter v = virusLocations.getParameter(i);
        String curName = v.getParameterName();
        System.out.print(curName);
        int isFound = 0;
        for (int j = 0; j < numNodes; j++) {
            String treeId = treeModel.getTaxonId(j);
            if (curName.equals(treeId)) {
                System.out.print("  isFound at j=" + j);
                correspondingTreeIndexForVirus[i] = j;
                System.out.println(" has clusterLabel = " + clusterLabelsTreeNode.getParameterValue(j));
                isFound = 1;
                break;
            }
        }
        if (isFound == 0) {
            System.out.println("not found. Exit now.");
            System.exit(0);
        }
    }
}
Also used : MatrixParameter(dr.inference.model.MatrixParameter) Parameter(dr.inference.model.Parameter)

Aggregations

Parameter (dr.inference.model.Parameter)397 TreeModel (dr.evomodel.tree.TreeModel)62 MatrixParameter (dr.inference.model.MatrixParameter)46 ArrayList (java.util.ArrayList)44 FrequencyModel (dr.oldevomodel.substmodel.FrequencyModel)43 FrequencyModel (dr.evomodel.substmodel.FrequencyModel)41 Units (dr.evolution.util.Units)36 XMLUnits (dr.evoxml.util.XMLUnits)36 BranchRateModel (dr.evomodel.branchratemodel.BranchRateModel)30 Tree (dr.evolution.tree.Tree)25 DataType (dr.evolution.datatype.DataType)24 GammaSiteRateModel (dr.evomodel.siteratemodel.GammaSiteRateModel)23 CompoundParameter (dr.inference.model.CompoundParameter)23 GammaSiteModel (dr.oldevomodel.sitemodel.GammaSiteModel)21 SitePatterns (dr.evolution.alignment.SitePatterns)20 HKY (dr.evomodel.substmodel.nucleotide.HKY)17 Likelihood (dr.inference.model.Likelihood)17 HomogeneousBranchModel (dr.evomodel.branchmodel.HomogeneousBranchModel)16 DefaultBranchRateModel (dr.evomodel.branchratemodel.DefaultBranchRateModel)16 ParametricDistributionModel (dr.inference.distribution.ParametricDistributionModel)16