Debian Codegraph @

src/dr/inference/operators/InvariantOperator.java

raw symbols SCIP
1 /*
2 * InvariantOperator.java
3 *
4 * Copyright (c) 2002-2017 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.Likelihood;
29 import dr.inference.model.Parameter;
30 import dr.inferencexml.operators.DirtyLikelihoodOperatorParser;
31
32 /**
33 * @author Marc Suchard
34 */
35 public abstract class InvariantOperator extends SimpleMCMCOperator implements GibbsOperator {
36
37 private InvariantOperator(Parameter parameter, Likelihood likelihood, double weight,
38 boolean checkLikelihood) {
39 this.parameter = parameter;
40 this.likelihood = likelihood;
41 this.checkLikelihood = checkLikelihood || DEBUG;
42
43 setWeight(weight);
44 }
45
46 public String getPerformanceSuggestion() {
47 return null;
48 }
49
50 public String getOperatorName() {
51 return DirtyLikelihoodOperatorParser.TOUCH_OPERATOR;
52 }
53
54 @Override
55 public double doOperation() {
56
57 double logLikelihood = 0;
58 if (checkLikelihood) {
59 if (likelihood != null) {
60 logLikelihood = likelihood.getLogLikelihood();
61 }
62 }
63
64 if (pathParameter == 1.0) {
65 transform(parameter);
66 }
67
68 if (checkLikelihood) {
69 if (likelihood != null) {
70 double newLogLikelihood = likelihood.getLogLikelihood();
71
72 if (Math.abs(logLikelihood - newLogLikelihood) > tolerance) {
73 String sb = "Likelihood is not invariant to transformation:\n" +
74 "Before: " + logLikelihood + "\n" +
75 "After : " + newLogLikelihood + "\n";
76 throw new RuntimeException(sb);
77 }
78 }
79 }
80
81 return 0;
82 }
83
84 @Override
85 public void setPathParameter(double beta) {
86 pathParameter = beta;
87 }
88
89 private double pathParameter = 1.0;
90
91 protected abstract void transform(Parameter parameter);
92
93 public int getStepCount() {
94 return 1;
95 }
96
97 private final Parameter parameter;
98 private final Likelihood likelihood;
99 private final boolean checkLikelihood;
100
101 private final static boolean DEBUG = false;
102 private final static double tolerance = 1E-1;
103
104 public static class Rotation extends InvariantOperator {
105
106 private final boolean translationInvariant;
107 private final boolean rotationInvariant;
108 private final int dim;
109
110 public Rotation(Parameter parameter, int dim,
111 double weight, Likelihood likelihood,
112 boolean translate, boolean rotate,
113 boolean checkLikelihood) {
114 super(parameter, likelihood, weight, checkLikelihood);
115
116 this.dim = dim;
117 this.translationInvariant = translate;
118 this.rotationInvariant = rotate;
119 }
120
121 @Override
122 protected void transform(Parameter parameter) {
123
124 double[] x = parameter.getParameterValues();
125
126 EllipticalSliceOperator.transformPoint(x, translationInvariant, rotationInvariant, dim);
127
128 final int len = x.length;
129 for (int i = 0; i < len; ++i) {
130 parameter.setParameterValueQuietly(i, x[i]);
131 }
132 parameter.fireParameterChangedEvent();
133 }
134 }
135 }