/* * File Uniform.java * * Copyright (C) 2010 Remco Bouckaert remco@cs.auckland.ac.nz * * This file is part of BEAST2. * See the NOTICE file distributed with this work for additional * information regarding copyright ownership and licensing. * * BEAST is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * BEAST is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with BEAST; if not, write to the * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, * Boston, MA 02110-1301 USA */ /* * UniformOperator.java * * Copyright (C) 2002-2006 Alexei Drummond and Andrew Rambaut * * This file is part of BEAST. * See the NOTICE file distributed with this work for additional * information regarding copyright ownership and licensing. * * BEAST is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * BEAST is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with BEAST; if not, write to the * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, * Boston, MA 02110-1301 USA */ package beast.evolution.operators; import beast.core.Description; import beast.evolution.tree.Node; import beast.evolution.tree.Tree; import beast.util.Randomizer; @Description("Randomly selects true internal tree node (i.e. not the root) and move node height uniformly in interval " + "restricted by the nodes parent and children.") public class Uniform extends TreeOperator { // empty constructor to facilitate construction by XML + initAndValidate public Uniform() { } public Uniform(Tree tree) { try { initByName(treeInput.getName(), tree); } catch (Exception e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. throw new RuntimeException("Failed to construct Uniform Tree Operator."); } } @Override public void initAndValidate() { } /** * change the parameter and return the hastings ratio. * * @return log of Hastings Ratio, or Double.NEGATIVE_INFINITY if proposal should not be accepted * */ @Override public double proposal() { final Tree tree = treeInput.get(this); // randomly select internal node final int nodeCount = tree.getNodeCount(); // Abort if no non-root internal nodes if (tree.getInternalNodeCount()==1) return Double.NEGATIVE_INFINITY; Node node; do { final int nodeNr = nodeCount / 2 + 1 + Randomizer.nextInt(nodeCount / 2); node = tree.getNode(nodeNr); } while (node.isRoot() || node.isLeaf()); final double upper = node.getParent().getHeight(); final double lower = Math.max(node.getLeft().getHeight(), node.getRight().getHeight()); final double newValue = (Randomizer.nextDouble() * (upper - lower)) + lower; node.setHeight(newValue); return 0.0; } }