Debian Codegraph @

src/dr/inference/operators/OperatorAnalysisPrinter.java

raw symbols SCIP
1 /*
2 * OperatorAnalysisPrinter.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 import dr.util.NumberFormatter;
29
30 import java.io.PrintStream;
31
32 /**
33 * Package: OperatorAnalysisPrinter
34 * Description:
35 * Factoring out OperatorAnalysis from MCMC class. Maybe someone will depricate use in MCMC
36 * <p/>
37 * Created by
38 *
39 * @author Alexander V. Alekseyenko ([email protected])
40 * Date: Jan 12, 2010
41 * Time: 6:02:30 PM
42 */
43 public class OperatorAnalysisPrinter {
44
45 private static final NumberFormatter formatter = new NumberFormatter(8);
46
47 /**
48 * Writes ano operator analysis to the provided print stream
49 *
50 * @param out the print stream to write operator analysis to
51 */
52 public static void showOperatorAnalysis(PrintStream out, OperatorSchedule schedule, boolean useAdaptation) {
53 out.println();
54 out.println("Operator analysis");
55 out.println(formatter.formatToFieldWidth("Operator", 50) +
56 formatter.formatToFieldWidth("Tuning", 9) +
57 formatter.formatToFieldWidth("Count", 11) +
58 formatter.formatToFieldWidth("Time", 9) +
59 formatter.formatToFieldWidth("Time/Op", 9) +
60 formatter.formatToFieldWidth("Pr(accept)", 11) +
61 (useAdaptation ? "" : " Performance suggestion"));
62
63 for (int i = 0; i < schedule.getOperatorCount(); i++) {
64
65 final MCMCOperator op = schedule.getOperator(i);
66 if (op instanceof JointOperator) {
67 JointOperator jointOp = (JointOperator) op;
68 for (int k = 0; k < jointOp.getNumberOfSubOperators(); k++) {
69 out.println(formattedOperatorName(jointOp.getSubOperatorName(k))
70 + formattedParameterString(jointOp.getSubOperator(k))
71 + formattedCountString(op)
72 + formattedTimeString(op)
73 + formattedTimePerOpString(op)
74 + formattedProbString(jointOp)
75 + (useAdaptation ? "" : formattedDiagnostics(jointOp, MCMCOperator.Utils.getAcceptanceProbability(jointOp)))
76 );
77 }
78 } else {
79 out.println(formattedOperatorName(op.getOperatorName())
80 + formattedParameterString(op)
81 + formattedCountString(op)
82 + formattedTimeString(op)
83 + formattedTimePerOpString(op)
84 + formattedProbString(op)
85 + (useAdaptation ? "" : formattedDiagnostics(op, MCMCOperator.Utils.getAcceptanceProbability(op)))
86 );
87 }
88
89 }
90 out.println();
91 }
92
93 private static String formattedOperatorName(String operatorName) {
94 return formatter.formatToFieldWidth(operatorName, 50);
95 }
96
97 private static String formattedParameterString(MCMCOperator op) {
98 String pString = " ";
99 if (op instanceof AdaptableMCMCOperator && ((AdaptableMCMCOperator) op).getMode() != AdaptationMode.ADAPTATION_OFF) {
100 pString = formatter.formatToFieldWidth(formatter.formatDecimal(((AdaptableMCMCOperator) op).getRawParameter(), 3), 8);
101 }
102 return pString;
103 }
104
105 private static String formattedCountString(MCMCOperator op) {
106 final long count = op.getCount();
107 return formatter.formatToFieldWidth(Long.toString(count), 10) + " ";
108 }
109
110 private static String formattedTimeString(MCMCOperator op) {
111 final long time = op.getTotalEvaluationTime();
112 return formatter.formatToFieldWidth(Long.toString(time), 8) + " ";
113 }
114
115 private static String formattedTimePerOpString(MCMCOperator op) {
116 final double time = op.getMeanEvaluationTime();
117 return formatter.formatToFieldWidth(formatter.formatDecimal(time, 2), 8) + " ";
118 }
119
120 private static String formattedProbString(MCMCOperator op) {
121 final double acceptanceProb = MCMCOperator.Utils.getAcceptanceProbability(op);
122 return formatter.formatToFieldWidth(formatter.formatDecimal(acceptanceProb, 4), 11) + " ";
123 }
124
125 private static String formattedDiagnostics(MCMCOperator op, double acceptanceProb) {
126
127 String message = "good";
128 if (acceptanceProb < AbstractAdaptableOperator.MINIMUM_GOOD_ACCEPTANCE_LEVEL) {
129 if (acceptanceProb < (AbstractAdaptableOperator.MINIMUM_ACCEPTANCE_LEVEL / 10.0)) {
130 message = "very low";
131 } else if (acceptanceProb < AbstractAdaptableOperator.MINIMUM_ACCEPTANCE_LEVEL) {
132 message = "low";
133 } else message = "slightly low";
134
135 } else if (acceptanceProb > AbstractAdaptableOperator.MAXIMUM_GOOD_ACCEPTANCE_LEVEL) {
136 double reallyHigh = 1.0 - ((1.0 - AbstractAdaptableOperator.MAXIMUM_ACCEPTANCE_LEVEL) / 10.0);
137 if (acceptanceProb > reallyHigh) {
138 message = "very high";
139 } else if (acceptanceProb > AbstractAdaptableOperator.MAXIMUM_ACCEPTANCE_LEVEL) {
140 message = "high";
141 } else message = "slightly high";
142 }
143
144 String performancesMsg = "";
145 if (op instanceof GibbsOperator) {
146 performancesMsg = "none (Gibbs operator)";
147 } else if (op instanceof AdaptableMCMCOperator){
148 final String suggestion = ((AdaptableMCMCOperator)op).getPerformanceSuggestion();
149 performancesMsg = message + "\t" + suggestion;
150 }
151
152 return performancesMsg;
153 }
154 }