Debian Codegraph @

src/dr/inference/operators/GenericIndependentSampler.java

raw symbols SCIP
1 /*
2 * GenericIndependentSampler.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.inference.model.Bounds;
29 import dr.inference.model.Parameter;
30 import dr.inference.model.Variable;
31 import dr.math.distributions.RandomGenerator;
32 import dr.xml.*;
33
34 /**
35 * A generic independent distribution sampler to propose new (independent) values.
36 *
37 * @author Mandev Gill
38 * @author Guy Baele
39 * @authur Marc Suchard
40 */
41 public class GenericIndependentSampler extends SimpleMCMCOperator {
42
43 public static final String OPERATOR_NAME = "genericIndependentSampler";
44
45 private final Variable<Double> variable;
46 private final RandomGenerator randomGenerator;
47 private final boolean univariate;
48 private final int generatorLength;
49
50 public GenericIndependentSampler(Variable variable, RandomGenerator randomGenerator, double weight) {
51
52 this.variable = variable;
53 this.randomGenerator = randomGenerator;
54
55 Object draw = randomGenerator.nextRandom();
56 if (draw instanceof Double) {
57 generatorLength = 1;
58 univariate = true;
59 } else if (draw instanceof double[]) {
60 generatorLength = ((double[]) draw).length;
61 univariate = false;
62 } else {
63 throw new IllegalArgumentException("Unknown random generator in " + getOperatorName());
64 }
65
66 setWeight(weight);
67 }
68
69 public String getPerformanceSuggestion() {
70 return "";
71 }
72
73 public String getOperatorName() {
74 return "genericIndependentSampler(" + variable.getVariableName() + ")";
75 }
76
77 /**
78 * change the parameter and return the hastings ratio.
79 */
80 public double doOperation() {
81
82 double logq = 0;
83
84 final Bounds<Double> bounds = variable.getBounds();
85 final int dim = variable.getSize();
86
87
88 int i = 0;
89 while (i < dim) {
90 Object draw = randomGenerator.nextRandom();
91 if (univariate) {
92 double currentValue = variable.getValue(i);
93 double newValue = (Double) draw;
94 logq += randomGenerator.logPdf(currentValue) - randomGenerator.logPdf(newValue);
95
96 if (newValue < bounds.getLowerLimit(i) || newValue > bounds.getUpperLimit(i)) {
97 // throw new OperatorFailedException("Proposed value outside boundaries");
98 return Double.NEGATIVE_INFINITY;
99 }
100 } else {
101 double[] currentValue = new double[generatorLength];
102 double[] newValue = (double[]) draw;
103
104 for (int j = 0; j < generatorLength; ++j) {
105 final int index = i * generatorLength + j;
106 currentValue[j] = variable.getValue(index);
107 }
108
109 logq += randomGenerator.logPdf(currentValue) - randomGenerator.logPdf(newValue);
110
111 for (int j = 0; j < generatorLength; ++j) {
112 final int index = i * generatorLength + j;
113
114 if (newValue[j] < bounds.getLowerLimit(index) || newValue[j] > bounds.getUpperLimit(index)) {
115 // throw new OperatorFailedException("Proposed value outside boundaries");
116 return Double.NEGATIVE_INFINITY;
117 }
118 variable.setValue(index, newValue[j]);
119 }
120 }
121 i += generatorLength;
122 }
123
124 return logq;
125 }
126
127 public static dr.xml.XMLObjectParser PARSER = new dr.xml.AbstractXMLObjectParser() {
128
129 public String getParserName() {
130 return OPERATOR_NAME;
131 }
132
133 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
134
135 double weight = xo.getDoubleAttribute(WEIGHT);
136 Parameter parameter = (Parameter) xo.getChild(Parameter.class);
137 RandomGenerator generator = (RandomGenerator) xo.getChild(RandomGenerator.class);
138
139 return new GenericIndependentSampler(parameter, generator, weight);
140 }
141
142 //************************************************************************
143 // AbstractXMLObjectParser implementation
144 //************************************************************************
145
146 public XMLSyntaxRule[] getSyntaxRules() {
147 return rules;
148 }
149
150 private final XMLSyntaxRule[] rules = {
151 AttributeRule.newDoubleRule(WEIGHT),
152 new ElementRule(RandomGenerator.class),
153 new ElementRule(Parameter.class)
154 };
155
156 public String getParserDescription() {
157 return "This element returns an independence sampler from a provided generic distribution generator.";
158 }
159
160 public Class getReturnType() {
161 return MCMCOperator.class;
162 }
163 };
164 }