Debian Codegraph @

src/dr/inference/operators/HierarchicalBitFlipOperator.java

raw symbols SCIP
1 /*
2 * HierarchicalBitFlipOperator.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 import dr.inference.model.Parameter;
30 import dr.math.MathUtils;
31
32 /**
33 * An operator that flips bits in same position for all strata and hieararchical level.
34 *
35 *
36 * @author Gabriela Cybis
37 */
38 public class HierarchicalBitFlipOperator extends SimpleMCMCOperator {
39
40 public HierarchicalBitFlipOperator(Parameter hParameter, Parameter[] strataParameters, int NStrata, double weight, boolean usesPriorOnSum) {
41
42 this.Nstrata = NStrata;
43 this.hParameter = hParameter;
44 this.strataParameters = strataParameters;
45 this.usesPriorOnSum = usesPriorOnSum;
46 setWeight(weight);
47
48 }
49
50
51
52
53 /**
54 * Change the parameter and return the hastings ratio.
55 * Flip (Switch a 0 to 1 or 1 to 0) for a random bit in a bit vector.
56 * Return the hastings ratio which makes all subsets of vectors with the same number of 1 bits
57 * equiprobable
58 */
59 public final double doOperation() {
60 final int dim = hParameter.getDimension();
61 double logq = 0.0;
62
63 //comeca novo
64 int rep= MathUtils.nextInt(4);
65
66 for (int repete=0; repete<rep+1;repete++ ){
67 // double sum = 0.0;
68
69 if(usesPriorOnSum) {
70 for (int i = 0; i < dim; i++) {
71 // sum += hParameter.getParameterValue(i);
72 }
73 }
74
75
76
77 final int pos = MathUtils.nextInt(dim);
78
79 int value = (int) hParameter.getParameterValue(pos);
80
81 if (value == 0) {
82 hParameter.setParameterValue(pos, 1.0);
83
84 // if(usesPriorOnSum)
85 // logq = -Math.log((dim - sum) / (sum + 1));
86
87 } else if (value == 1) {
88 hParameter.setParameterValue(pos, 0.0);
89 // if(usesPriorOnSum)
90 // logq = -Math.log(sum / (dim - sum + 1));
91
92 } else {
93 throw new RuntimeException("expected 1 or 0");
94 }
95
96 for (int j=0; j<Nstrata ; j++){
97
98 value = (int) strataParameters[j].getParameterValue(pos);
99
100 if (value == 0) {
101 strataParameters[j].setParameterValue(pos, 1.0);
102
103 } else if (value == 1) {
104 strataParameters[j].setParameterValue(pos, 0.0);
105
106 } else {
107 throw new RuntimeException("expected 1 or 0");
108 }
109
110
111 }
112 }
113
114 return logq;
115 }
116
117 // Interface MCMCOperator
118 public final String getOperatorName() {
119 return "bitFlip(" + hParameter.getParameterName() + ")";
120 }
121
122 public final String getPerformanceSuggestion() {
123 return "no performance suggestion";
124 }
125
126 public String toString() {
127 return getOperatorName();
128 }
129
130 // Private instance variables
131
132 private Parameter hParameter = null;
133 private boolean usesPriorOnSum = false;
134 private Parameter[] strataParameters = null ;
135 int Nstrata;
136 }
137
138