Debian Codegraph @

src/dr/inference/operators/GaussianProcessDrawOperator.java

raw symbols SCIP
1 /*
2 * GaussianProcessDrawOperator.java
3 *
4 * Copyright (c) 2002-2016 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.Parameter;
29 import dr.inferencexml.operators.EllipticalSliceOperatorParser;
30 import dr.math.distributions.GaussianProcessRandomGenerator;
31 import dr.xml.*;
32
33
34 /**
35 * @author Marc Suchard
36 */
37 public class GaussianProcessDrawOperator extends AbstractAdaptableOperator {
38
39 public static final String GAUSSIAN_PROCESS_OPERATOR = "gaussianProcessOperator";
40 public static final String SCALE_FACTOR = "scaleFactor";
41 public static final String TRANSLATION_INVARIANT = EllipticalSliceOperatorParser.TRANSLATION_INVARIANT;
42 public static final String ROTATION_INVARIANT = EllipticalSliceOperatorParser.ROTATION_INVARIANT;
43
44 private double scaleFactor;
45 private final Parameter parameter;
46 private final GaussianProcessRandomGenerator gaussianProcess;
47 private final int dim;
48 private final boolean translationInvariant;
49 private final boolean rotationInvariant;
50
51 public GaussianProcessDrawOperator(Parameter parameter, double scaleFactor, double weight,
52 AdaptationMode mode, GaussianProcessRandomGenerator gaussianProcess,
53 boolean translationInvariant, boolean rotationInvariant) {
54
55 super(mode);
56 this.scaleFactor = scaleFactor;
57 this.parameter = parameter;
58 this.gaussianProcess = gaussianProcess;
59 setWeight(weight);
60
61 this.translationInvariant = translationInvariant;
62 this.rotationInvariant = rotationInvariant;
63
64 if (gaussianProcess.getDimension() != parameter.getDimension()) {
65 throw new IllegalArgumentException("Dimension mismatch");
66 }
67
68 // if (!gaussianProcess.isTranslationInvariant()) {
69 // throw new IllegalArgumentException("Must be translationally invariant");
70 // }
71
72 this.dim = parameter.getDimension();
73
74 }
75
76 public double doOperation() {
77
78 double[] x = parameter.getParameterValues();
79 double[] epsilon = (double[]) gaussianProcess.nextRandom();
80
81 for (int i = 0; i < dim; ++i) {
82 x[i] += scaleFactor * epsilon[i];
83 }
84
85 EllipticalSliceOperator.transformPoint(x, translationInvariant, rotationInvariant,
86 2); // TODO Make generic for dim != 2
87
88 for (int i = 0; i < dim; i++) {
89 parameter.setParameterValueQuietly(i, x[i] + scaleFactor * epsilon[i]);
90 }
91 parameter.fireParameterChangedEvent();
92
93 // System.err.println("DONE");
94
95 return 0; // TODO Not 0 if gaussianProcess mean != 0
96 }
97
98 //MCMCOperator INTERFACE
99 public final String getOperatorName() {
100 return parameter.getParameterName();
101 }
102
103 public double getAdaptableParameter() {
104 return Math.log(scaleFactor);
105 }
106
107 public void setAdaptableParameter(double value) {
108 scaleFactor = Math.exp(value);
109 }
110
111 public double getRawParameter() {
112 return scaleFactor;
113 }
114
115 public double getScaleFactor() {
116 return scaleFactor;
117 }
118
119 public String getAdaptableParameterName() {
120 return "scaleFactor";
121 }
122
123 public static XMLObjectParser PARSER = new AbstractXMLObjectParser() {
124
125 public String getParserName() {
126 return GAUSSIAN_PROCESS_OPERATOR;
127 }
128
129 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
130
131 AdaptationMode mode = AdaptationMode.parseMode(xo);
132
133 double weight = xo.getDoubleAttribute(WEIGHT);
134 double scaleFactor = xo.getDoubleAttribute(SCALE_FACTOR);
135
136 if (scaleFactor <= 0.0) {
137 throw new XMLParseException("scaleFactor must be greater than 0.0");
138 }
139
140 Parameter parameter = (Parameter) xo.getChild(Parameter.class);
141 GaussianProcessRandomGenerator generator =
142 (GaussianProcessRandomGenerator) xo.getChild(GaussianProcessRandomGenerator.class);
143
144 boolean translationInvariant = xo.getAttribute(TRANSLATION_INVARIANT, false);
145 boolean rotationInvariant = xo.getAttribute(ROTATION_INVARIANT, false);
146
147 return new GaussianProcessDrawOperator(parameter, scaleFactor, weight, mode, generator,
148 translationInvariant, rotationInvariant);
149 }
150
151 //************************************************************************
152 // AbstractXMLObjectParser implementation
153 //************************************************************************
154
155 public String getParserDescription() {
156 return "This element returns a multivariate normal random walk operator on a given parameter.";
157 }
158
159 public Class getReturnType() {
160 return MCMCOperator.class;
161 }
162
163 public XMLSyntaxRule[] getSyntaxRules() {
164 return rules;
165 }
166
167 private final XMLSyntaxRule[] rules = {
168 AttributeRule.newDoubleRule(SCALE_FACTOR),
169 AttributeRule.newDoubleRule(WEIGHT),
170 AttributeRule.newBooleanRule(AUTO_OPTIMIZE, true),
171 AttributeRule.newBooleanRule(TRANSLATION_INVARIANT, true),
172 AttributeRule.newBooleanRule(ROTATION_INVARIANT, true),
173 new ElementRule(Parameter.class),
174 new ElementRule(GaussianProcessRandomGenerator.class),
175 };
176
177 };
178 }