| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 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 |
|
| 34 |
|
| 35 |
|
| 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 |
|
| 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); |
| 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 |
|
| 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; |
| 136 |
} |
| 137 |
|
| 138 |
public class Doubling extends Abstract { |
| 139 |
|
| 140 |
public Interval constructInterval(Likelihood likelihood, double x0, |
| 141 |
double cutoffDensity, double width) { |
| 142 |
|
| 143 |
return new Interval(0,1); |
| 144 |
} |
| 145 |
|
| 146 |
protected boolean test(Likelihood likelihood, double x0, double x1, |
| 147 |
double cutoffDensity, double width) { |
| 148 |
|
| 149 |
return true; |
| 150 |
} |
| 151 |
} |
| 152 |
} |