Debian Codegraph @

src/dr/inference/operators/SliceInterval.java

raw symbols SCIP
1 /*
2 * SliceInterval.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.Likelihood;
29 import dr.inference.model.Variable;
30 import dr.math.MathUtils;
31
32 /**
33 * Constructs a univariate slice sampler interval
34 *
35 * @author Marc Suchard
36 */
37 public interface SliceInterval {
38
39 public double drawFromInterval(Likelihood likelihood, double cutoffDensity, double width);
40
41 public void setSliceSampler(SliceOperator sliceSampler);
42
43 public abstract class Abstract implements SliceInterval {
44
45 public double drawFromInterval(Likelihood likelihood, double cutoffDensity, double width) {
46 double x0 = variable.getValue(0);
47 Interval interval = constructInterval(likelihood, x0, cutoffDensity, width);
48 double x1 = x0;
49 boolean found = false;
50 while (!found) {
51 x1 = MathUtils.uniform(interval.lower, interval.upper);
52 if (cutoffDensity < evaluate(likelihood, x1) && test(likelihood, x0, x1,
53 cutoffDensity, width)) {
54 found = true;
55 } else {
56 shrinkInterval(interval, x0, x1);
57 }
58 }
59 return x1;
60 }
61
62 public abstract Interval constructInterval(Likelihood likelihood, double x0,
63 double cutoffDensity, double width);
64
65
66 protected abstract boolean test(Likelihood likelihood, double x0, double x1,
67 double cutoffDensity, double width);
68
69 public void shrinkInterval(Interval interval, double x0, double x1) {
70 // Taken from Fig 5 in Neal (2003)
71 if (x1 < x0) {
72 interval.lower = x1;
73 } else {
74 interval.upper = x1;
75 }
76 }
77
78 protected class Interval {
79 double lower;
80 double upper;
81
82 Interval(double lower, double upper) {
83 this.lower = lower;
84 this.upper = upper;
85 }
86 }
87
88 public void setSliceSampler(SliceOperator sliceSampler) {
89 this.sliceSampler = sliceSampler;
90 this.variable = sliceSampler.getVariable();
91 }
92
93 protected double evaluate(Likelihood likelihood, double x) {
94 variable.setValue(0, x);
95 return sliceSampler.evaluate(likelihood, 1.0);
96 }
97
98 protected Variable<Double> variable;
99 protected SliceOperator sliceSampler;
100 }
101
102 public class SteppingOut extends Abstract {
103
104 public SteppingOut() {
105 this(10); // TODO Pick better default
106 }
107
108 public SteppingOut(int m) {
109 this.m = m;
110 }
111
112 public Interval constructInterval(Likelihood likelihood, double x0,
113 double cutoffDensity, double w) {
114 // Taken from Fig 3 in Neal (2003)
115 double L = x0 - w * MathUtils.nextDouble();
116 double R = L + w;
117 int J = MathUtils.nextInt(m);
118 int K = (m - 1) - J;
119 while (J > 0 && cutoffDensity < evaluate(likelihood, L) ) {
120 L -= w;
121 J--;
122 }
123 while (K > 0 && cutoffDensity < evaluate(likelihood, R) ) {
124 R += w;
125 K--;
126 }
127 return new Interval(L,R);
128 }
129
130 protected boolean test(Likelihood likelihood, double x0, double x1,
131 double cutoffDensity, double width) {
132 return true;
133 }
134
135 private int m; // Maximum number of stepping out intervals to explore
136 }
137
138 public class Doubling extends Abstract {
139
140 public Interval constructInterval(Likelihood likelihood, double x0,
141 double cutoffDensity, double width) {
142 // TODO
143 return new Interval(0,1);
144 }
145
146 protected boolean test(Likelihood likelihood, double x0, double x1,
147 double cutoffDensity, double width) {
148 // TODO
149 return true;
150 }
151 }
152 }