Debian Codegraph @

src/dr/inference/operators/OperatorUtils.java

raw symbols SCIP
1 /*
2 * OperatorUtils.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.inference.operators;
27
28 /**
29 * Utility functions to aid in operator optimization etc.
30 *
31 * @author Alexei Drummond
32 * @version $Id: OperatorUtils.java,v 1.3 2005/05/24 20:26:00 rambaut Exp $
33 */
34 public class OperatorUtils {
35
36 /**
37 * Given a current window size and an acceptance level and target acceptance level,
38 * returns a new window size.
39 *
40 * @param delta the current delta (half the window size)
41 * @param currentLevel the current acceptance probability
42 * @param targetLevel the target acceptance probability
43 * @return a new window size
44 */
45 public static double optimizeWindowSize(double delta, double currentLevel, double targetLevel) {
46 return optimizeWindowSize(delta, Double.MAX_VALUE, currentLevel, targetLevel);
47 }
48
49 /**
50 * Given a current delta (+-) and an acceptance level and target acceptance level,
51 * returns a new delta.
52 *
53 * @param delta the current delta (half the window size)
54 * @param maxDelta the maximum delta allowed
55 * @param currentLevel the current acceptance probability
56 * @param targetLevel the target acceptance probability
57 * @return a new window size
58 */
59 public static double optimizeWindowSize(double delta, double maxDelta, double currentLevel, double targetLevel) {
60
61 if (delta <= 0.0) {
62 throw new IllegalArgumentException("random walk window size cannot be negative: " + delta);
63 }
64
65 double ratio = currentLevel / targetLevel;
66
67 if (ratio > 2.0) ratio = 2.0;
68 if (ratio < 0.5) ratio = 0.5;
69
70 double newDelta = delta * ratio;
71
72
73 if (newDelta > maxDelta) newDelta = maxDelta;
74
75 return newDelta;
76 }
77
78 /**
79 * Given a current scale factor and acceptance level and a target acceptance level,
80 * returns a new scale factor.
81 *
82 * @param scaleFactor the current scale factor
83 * @param currentLevel the current acceptance probability
84 * @param targetLevel the target acceptance probability
85 * @return a new scale factor
86 */
87 public static double optimizeScaleFactor(double scaleFactor, double currentLevel, double targetLevel) {
88
89 if (scaleFactor <= 0.0 || scaleFactor >= 1.0) {
90 throw new IllegalArgumentException("scale factor was " + scaleFactor + "!");
91 }
92
93 double ratio = currentLevel / targetLevel;
94
95 if (ratio > 2.0) ratio = 2.0;
96 if (ratio < 0.5) ratio = 0.5;
97
98 // new scale factor
99 return Math.pow(scaleFactor, ratio);
100 }
101 }