Search in sources :

Example 6 with MCMCOperator

use of dr.inference.operators.MCMCOperator in project beast-mcmc by beast-dev.

the class MCMCMC method swapChainTemperatures.

private int swapChainTemperatures() {
    if (DEBUG) {
        System.out.print("Current scores: ");
        for (int i = 0; i < chains.length; i++) {
            System.out.print("\t");
            if (i == coldChain) {
                System.out.print("[");
            }
            System.out.print(chains[i].getCurrentScore());
            if (i == coldChain) {
                System.out.print("]");
            }
        }
        System.out.println();
    }
    int newColdChain = coldChain;
    int index1 = MathUtils.nextInt(chains.length);
    int index2 = MathUtils.nextInt(chains.length);
    while (index1 == index2) {
        index2 = MathUtils.nextInt(chains.length);
    }
    double score1 = chains[index1].getCurrentScore();
    MCMCCriterion acceptor1 = ((MCMCCriterion) chains[index1].getAcceptor());
    double temperature1 = acceptor1.getTemperature();
    double score2 = chains[index2].getCurrentScore();
    MCMCCriterion acceptor2 = ((MCMCCriterion) chains[index2].getAcceptor());
    double temperature2 = acceptor2.getTemperature();
    double logRatio = ((score2 - score1) * temperature1) + ((score1 - score2) * temperature2);
    boolean swap = (Math.log(MathUtils.nextDouble()) < logRatio);
    if (swap) {
        if (DEBUG) {
            System.out.println("Swapping chain " + index1 + " and chain " + index2);
        }
        acceptor1.setTemperature(temperature2);
        acceptor2.setTemperature(temperature1);
        OperatorSchedule schedule1 = schedules[index1];
        OperatorSchedule schedule2 = schedules[index2];
        for (int i = 0; i < schedule1.getOperatorCount(); i++) {
            MCMCOperator operator1 = schedule1.getOperator(i);
            MCMCOperator operator2 = schedule2.getOperator(i);
            long tmp = operator1.getAcceptCount();
            operator1.setAcceptCount(operator2.getAcceptCount());
            operator2.setAcceptCount(tmp);
            tmp = operator1.getRejectCount();
            operator1.setRejectCount(operator2.getRejectCount());
            operator2.setRejectCount(tmp);
            double tmp2 = operator1.getSumDeviation();
            operator1.setSumDeviation(operator2.getSumDeviation());
            operator2.setSumDeviation(tmp2);
            if (operator1 instanceof AdaptableMCMCOperator) {
                tmp2 = ((AdaptableMCMCOperator) operator1).getAdaptableParameter();
                ((AdaptableMCMCOperator) operator1).setAdaptableParameter(((AdaptableMCMCOperator) operator2).getAdaptableParameter());
                ((AdaptableMCMCOperator) operator2).setAdaptableParameter(tmp2);
            }
        }
        if (index1 == coldChain) {
            newColdChain = index2;
        } else if (index2 == coldChain) {
            newColdChain = index1;
        }
    }
    return newColdChain;
}
Also used : AdaptableMCMCOperator(dr.inference.operators.AdaptableMCMCOperator) OperatorSchedule(dr.inference.operators.OperatorSchedule) MCMCCriterion(dr.inference.mcmc.MCMCCriterion) MCMCOperator(dr.inference.operators.MCMCOperator) AdaptableMCMCOperator(dr.inference.operators.AdaptableMCMCOperator)

Example 7 with MCMCOperator

use of dr.inference.operators.MCMCOperator in project beast-mcmc by beast-dev.

the class SimpleOperatorScheduleParser method parseXMLObject.

public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    int useThreshold = xo.getAttribute(USE_THRESHOLD, 1000);
    double acceptanceThreshold = xo.getAttribute(ACCEPTANCE_THRESHOLD, 0.0);
    SimpleOperatorSchedule schedule = new SimpleOperatorSchedule(useThreshold, acceptanceThreshold);
    if (xo.hasAttribute(SEQUENTIAL)) {
        schedule.setSequential(xo.getBooleanAttribute(SEQUENTIAL));
    }
    if (xo.hasAttribute(OPTIMIZATION_SCHEDULE)) {
        String type = xo.getStringAttribute(OPTIMIZATION_SCHEDULE);
        Logger.getLogger("dr.inference").info("Optimization Schedule: " + type);
        try {
            if (type.equalsIgnoreCase("default")) {
                schedule.setOptimizationTransform(OperatorSchedule.DEFAULT_TRANSFORM);
            } else {
                schedule.setOptimizationTransform(OperatorSchedule.OptimizationTransform.valueOf(type.toUpperCase()));
            }
        } catch (IllegalArgumentException iae) {
            throw new RuntimeException("Unsupported optimization schedule");
        }
    }
    for (int i = 0; i < xo.getChildCount(); i++) {
        Object child = xo.getChild(i);
        if (child instanceof MCMCOperator) {
            schedule.addOperator((MCMCOperator) child);
        }
    }
    return schedule;
}
Also used : SimpleOperatorSchedule(dr.inference.operators.SimpleOperatorSchedule) MCMCOperator(dr.inference.operators.MCMCOperator)

Example 8 with MCMCOperator

use of dr.inference.operators.MCMCOperator in project beast-mcmc by beast-dev.

the class CheckPointModifier method readStateFromFile.

protected long readStateFromFile(File file, MarkovChain markovChain, double[] lnL) {
    OperatorSchedule operatorSchedule = markovChain.getSchedule();
    long state = -1;
    this.traitModels = new ArrayList<TreeParameterModel>();
    try {
        FileReader fileIn = new FileReader(file);
        BufferedReader in = new BufferedReader(fileIn);
        int[] rngState = null;
        String line = in.readLine();
        String[] fields = line.split("\t");
        if (fields[0].equals("rng")) {
            // if there is a random number generator state present then load it...
            try {
                rngState = new int[fields.length - 1];
                for (int i = 0; i < rngState.length; i++) {
                    rngState[i] = Integer.parseInt(fields[i + 1]);
                }
            } catch (NumberFormatException nfe) {
                throw new RuntimeException("Unable to read state number from state file");
            }
            line = in.readLine();
            fields = line.split("\t");
        }
        try {
            if (!fields[0].equals("state")) {
                throw new RuntimeException("Unable to read state number from state file");
            }
            state = Long.parseLong(fields[1]);
        } catch (NumberFormatException nfe) {
            throw new RuntimeException("Unable to read state number from state file");
        }
        line = in.readLine();
        fields = line.split("\t");
        try {
            if (!fields[0].equals("lnL")) {
                throw new RuntimeException("Unable to read lnL from state file");
            }
            if (lnL != null) {
                lnL[0] = Double.parseDouble(fields[1]);
            }
        } catch (NumberFormatException nfe) {
            throw new RuntimeException("Unable to read lnL from state file");
        }
        line = in.readLine();
        // System.out.println(line);
        fields = line.split("\t");
        // Tree nodes have numbers as parameter ids
        for (Parameter parameter : Parameter.CONNECTED_PARAMETER_SET) {
            // numbers should be positive but can include zero
            if (isTreeNode(parameter.getId()) && isTreeNode(fields[1]) || parameter.getId().equals(fields[1])) {
                int dimension = Integer.parseInt(fields[2]);
                if (dimension != parameter.getDimension() && !fields[1].equals("branchRates.categories")) {
                    System.err.println("Unable to match state parameter dimension: " + dimension + ", expecting " + parameter.getDimension() + " for parameter: " + parameter.getParameterName());
                    System.err.print("Read from file: ");
                    for (int i = 0; i < fields.length; i++) {
                        System.err.print(fields[i] + "\t");
                    }
                    System.err.println();
                }
                if (fields[1].equals("branchRates.categories.rootNodeNumber")) {
                    // System.out.println("eek");
                    double value = Double.parseDouble(fields[3]);
                    parameter.setParameterValue(0, value);
                    if (DEBUG) {
                        System.out.println("restoring " + fields[1] + " with value " + value);
                    }
                } else {
                    if (DEBUG) {
                        System.out.print("restoring " + fields[1] + " with values ");
                    }
                    if (fields[1].equals("branchRates.categories")) {
                        for (int dim = 0; dim < (fields.length - 3); dim++) {
                            // System.out.println("dim " + dim);
                            parameter.setParameterValue(dim, Double.parseDouble(fields[dim + 3]));
                            if (DEBUG) {
                                System.out.print(Double.parseDouble(fields[dim + 3]) + " ");
                            }
                        }
                    } else {
                        for (int dim = 0; dim < parameter.getDimension(); dim++) {
                            parameter.setParameterValue(dim, Double.parseDouble(fields[dim + 3]));
                            if (DEBUG) {
                                System.out.print(Double.parseDouble(fields[dim + 3]) + " ");
                            }
                        }
                    }
                    if (DEBUG) {
                        System.out.println();
                    }
                }
                line = in.readLine();
                // System.out.println(line);
                fields = line.split("\t");
            } else {
                if (DEBUG) {
                    System.out.println("  unable to match " + parameter.getId() + " with " + fields[1] + " (moving on ...)");
                }
            // there will be more parameters in the connected set than there are lines in the checkpoint file
            // TODO keep track of these parameters and print a list of those parameters to screen
            }
        }
        // TODO remove else-clause (and boolean) after multiple rounds of testing
        if (IN_MEMORY) {
            // first read in all the operator lines from the checkpoint file
            // store them in a HashMap (or other structure that allows easy look-up)
            HashMap<String, String[]> operatorMap = new HashMap<String, String[]>();
            while (fields[0].equals("operator")) {
                operatorMap.put(fields[1], fields);
                line = in.readLine();
                fields = line.split("\t");
            }
            // then iterate over the operator schedule and look into the HashMap for the information
            for (int i = 0; i < operatorSchedule.getOperatorCount(); i++) {
                MCMCOperator operator = operatorSchedule.getOperator(i);
                String[] lookup = operatorMap.get(operator.getOperatorName());
                if (lookup == null) {
                    // could be additional operator so not necessarily a problem that warrants an exception
                    if (DEBUG) {
                        System.out.println("No information found in checkpoint file for operator " + operator.getOperatorName());
                    }
                } else {
                    // entry was found in stored information
                    if (DEBUG) {
                        System.out.println("restoring operator " + operator.getOperatorName() + " with settings from " + lookup[1]);
                    }
                    if (lookup.length < 4) {
                        throw new RuntimeException("Operator missing values: " + lookup[1] + ", length=" + lookup.length);
                    }
                    operator.setAcceptCount(Integer.parseInt(lookup[2]));
                    operator.setRejectCount(Integer.parseInt(lookup[3]));
                    if (operator instanceof AdaptableMCMCOperator) {
                        if (lookup.length != 6) {
                            throw new RuntimeException("Coercable operator missing parameter: " + lookup[1]);
                        }
                        ((AdaptableMCMCOperator) operator).setAdaptableParameter(Double.parseDouble(lookup[4]));
                    }
                }
                // don't forget to remove the entry from the hash map
                operatorMap.remove(operator.getOperatorName());
            }
            if (DEBUG) {
                System.out.println("Number of entries left in stored operator map: " + operatorMap.size());
            }
        } else {
            // No changes needed for loading in operators
            for (int i = 0; i < operatorSchedule.getOperatorCount(); i++) {
                MCMCOperator operator = operatorSchedule.getOperator(i);
                if (DEBUG) {
                    System.out.println("restoring operator " + operator.getOperatorName() + " with settings from " + fields[1]);
                }
                if (!fields[1].equals(operator.getOperatorName())) {
                    throw new RuntimeException("Unable to match operator: " + fields[1] + " vs. " + operator.getOperatorName());
                }
                if (fields.length < 4) {
                    throw new RuntimeException("Operator missing values: " + fields[1] + ", length=" + fields.length);
                }
                operator.setAcceptCount(Integer.parseInt(fields[2]));
                operator.setRejectCount(Integer.parseInt(fields[3]));
                if (operator instanceof AdaptableMCMCOperator) {
                    if (fields.length != 6) {
                        throw new RuntimeException("Coercable operator missing parameter: " + fields[1]);
                    }
                    ((AdaptableMCMCOperator) operator).setAdaptableParameter(Double.parseDouble(fields[4]));
                }
                line = in.readLine();
                fields = line.split("\t");
            }
        }
        // load the tree models last as we get the node heights from the tree (not the parameters which
        // which may not be associated with the right node
        Set<String> expectedTreeModelNames = new HashSet<String>();
        for (Model model : Model.CONNECTED_MODEL_SET) {
            if (model instanceof TreeModel) {
                expectedTreeModelNames.add(model.getModelName());
            }
            if (model instanceof TreeParameterModel) {
                this.traitModels.add((TreeParameterModel) model);
            }
            if (model instanceof BranchRates) {
                this.rateModel = (BranchRates) model;
            }
            // e.g. MixtureModelBranchRates uses an array of ParametricDistributionModel
            if (model instanceof DiscretizedBranchRates) {
                parDistMod = ((DiscretizedBranchRates) model).getParametricDistributionModel();
            }
        }
        while (fields[0].equals("tree")) {
            for (Model model : Model.CONNECTED_MODEL_SET) {
                if (model instanceof TreeModel && fields[1].equals(model.getModelName())) {
                    // AR: Can we not just add them to a Flexible tree and then make a new TreeModel
                    // taking that in the constructor?
                    // internally, we have a tree with all the taxa
                    // externally, i.e. in the checkpoint file, we have a tree representation comprising
                    // a subset of the full taxa set
                    // write method that adjusts the internal representation, i.e. the one in the connected
                    // set, according to the checkpoint file and a distance-based approach to position
                    // the additional taxa
                    // first read in all the data from the checkpoint file
                    line = in.readLine();
                    line = in.readLine();
                    fields = line.split("\t");
                    // read number of nodes
                    int nodeCount = Integer.parseInt(fields[0]);
                    double[] nodeHeights = new double[nodeCount];
                    String[] taxaNames = new String[(nodeCount + 1) / 2];
                    for (int i = 0; i < nodeCount; i++) {
                        line = in.readLine();
                        fields = line.split("\t");
                        nodeHeights[i] = Double.parseDouble(fields[1]);
                        if (i < taxaNames.length) {
                            taxaNames[i] = fields[2];
                        }
                    }
                    // on to reading edge information
                    line = in.readLine();
                    line = in.readLine();
                    line = in.readLine();
                    fields = line.split("\t");
                    int edgeCount = Integer.parseInt(fields[0]);
                    // create data matrix of doubles to store information from list of TreeParameterModels
                    double[][] traitValues = new double[traitModels.size()][edgeCount];
                    // create array to store whether a node is left or right child of its parent
                    // can be important for certain tree transition kernels
                    int[] childOrder = new int[edgeCount];
                    for (int i = 0; i < childOrder.length; i++) {
                        childOrder[i] = -1;
                    }
                    int[] parents = new int[edgeCount];
                    for (int i = 0; i < edgeCount; i++) {
                        parents[i] = -1;
                    }
                    for (int i = 0; i < edgeCount; i++) {
                        line = in.readLine();
                        if (line != null) {
                            fields = line.split("\t");
                            parents[Integer.parseInt(fields[0])] = Integer.parseInt(fields[1]);
                            childOrder[i] = Integer.parseInt(fields[2]);
                            for (int j = 0; j < traitModels.size(); j++) {
                                traitValues[j][i] = Double.parseDouble(fields[3 + j]);
                            }
                        }
                    }
                    // perform magic with the acquired information
                    // CheckPointTreeModifier modifyTree = new CheckPointTreeModifier((TreeModel) model);
                    this.modifyTree = new CheckPointTreeModifier((TreeModel) model, parDistMod);
                    // this.modifyTree = new CheckPointTreeModifier((TreeModel) model);
                    modifyTree.adoptTreeStructure(parents, nodeHeights, childOrder, taxaNames);
                    if (traitModels.size() > 0) {
                        modifyTree.adoptTraitData(parents, this.traitModels, traitValues);
                    }
                    // adopt the loaded tree structure; this does not yet copy the traits on the branches
                    // ((TreeModel) model).beginTreeEdit();
                    // ((TreeModel) model).adoptTreeStructure(parents, nodeHeights, childOrder);
                    // ((TreeModel) model).endTreeEdit();
                    expectedTreeModelNames.remove(model.getModelName());
                }
            }
            line = in.readLine();
            if (line != null) {
                fields = line.split("\t");
            }
        }
        if (expectedTreeModelNames.size() > 0) {
            StringBuilder sb = new StringBuilder();
            for (String notFoundName : expectedTreeModelNames) {
                sb.append("Expecting, but unable to match state parameter:" + notFoundName + "\n");
            }
            throw new RuntimeException(sb.toString());
        }
        in.close();
        fileIn.close();
    } catch (IOException ioe) {
        throw new RuntimeException("Unable to read file: " + ioe.getMessage());
    }
    return state;
}
Also used : HashMap(java.util.HashMap) TreeModel(dr.evomodel.tree.TreeModel) DiscretizedBranchRates(dr.evomodel.branchratemodel.DiscretizedBranchRates) FileReader(java.io.FileReader) HashSet(java.util.HashSet) OperatorSchedule(dr.inference.operators.OperatorSchedule) TreeParameterModel(dr.evomodel.tree.TreeParameterModel) IOException(java.io.IOException) AdaptableMCMCOperator(dr.inference.operators.AdaptableMCMCOperator) BufferedReader(java.io.BufferedReader) TreeParameterModel(dr.evomodel.tree.TreeParameterModel) Model(dr.inference.model.Model) TreeModel(dr.evomodel.tree.TreeModel) ParametricDistributionModel(dr.inference.distribution.ParametricDistributionModel) Parameter(dr.inference.model.Parameter) BranchRates(dr.evolution.tree.BranchRates) DiscretizedBranchRates(dr.evomodel.branchratemodel.DiscretizedBranchRates) MCMCOperator(dr.inference.operators.MCMCOperator) AdaptableMCMCOperator(dr.inference.operators.AdaptableMCMCOperator)

Aggregations

MCMCOperator (dr.inference.operators.MCMCOperator)8 Parameter (dr.inference.model.Parameter)5 OperatorSchedule (dr.inference.operators.OperatorSchedule)5 AdaptableMCMCOperator (dr.inference.operators.AdaptableMCMCOperator)4 TreeModel (dr.evomodel.tree.TreeModel)3 TreeParameterModel (dr.evomodel.tree.TreeParameterModel)3 Model (dr.inference.model.Model)3 DefaultTreeModel (dr.evomodel.tree.DefaultTreeModel)2 DistributionLikelihood (dr.inference.distribution.DistributionLikelihood)2 MCLogger (dr.inference.loggers.MCLogger)2 MCMC (dr.inference.mcmc.MCMC)2 Likelihood (dr.inference.model.Likelihood)2 ScaleOperator (dr.inference.operators.ScaleOperator)2 SimpleOperatorSchedule (dr.inference.operators.SimpleOperatorSchedule)2 BranchRates (dr.evolution.tree.BranchRates)1 NodeRef (dr.evolution.tree.NodeRef)1 DiscretizedBranchRates (dr.evomodel.branchratemodel.DiscretizedBranchRates)1 ConstantPopulationModel (dr.evomodel.coalescent.demographicmodel.ConstantPopulationModel)1 NormalDistributionModel (dr.inference.distribution.NormalDistributionModel)1 ParametricDistributionModel (dr.inference.distribution.ParametricDistributionModel)1