-
Notifications
You must be signed in to change notification settings - Fork 4
/
ExcludablePrior.java
89 lines (80 loc) · 3.31 KB
/
ExcludablePrior.java
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
* Copyright (C) 2012 Tim Vaughan <tgvaughan@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package beastlabs.math.distributions;
import beast.base.core.Description;
import beast.base.core.Input;
import beast.base.core.Input.Validate;
import beast.base.core.Function;
import beast.base.inference.distribution.Prior;
import beast.base.inference.parameter.BooleanParameter;
import beast.base.inference.parameter.IntegerParameter;
import beast.base.inference.parameter.RealParameter;
/**
* @author Tim Vaughan <tgvaughan@gmail.com>
*/
@Description("Just as with Prior, produces log probability of the parameter x. "
+ "This variant however allows one to explicitly exclude individual "
+ "elements of multidimensional parameters from the result.")
public class ExcludablePrior extends Prior {
public Input<BooleanParameter> xIncludeInput = new Input<BooleanParameter>(
"xInclude", "Array of true/false values specifying which elements"
+ " of x to include", Validate.REQUIRED);
@Override
public void initAndValidate() {
super.initAndValidate();
Function x = m_x.get();
if (x instanceof RealParameter || x instanceof IntegerParameter) {
if (x.getDimension() != xIncludeInput.get().getDimension())
throw new IllegalArgumentException("Length of xInclude does "
+ "not match length of x.");
}
}
@Override
public double calculateLogP() {
Function x = m_x.get();
if (x instanceof RealParameter || x instanceof IntegerParameter) {
// test that parameter is inside its bounds
double l = 0.0;
double h = 0.0;
if (x instanceof RealParameter) {
l = ((RealParameter) x).getLower();
h = ((RealParameter) x).getUpper();
} else {
l = ((IntegerParameter) x).getLower();
h = ((IntegerParameter) x).getUpper();
}
for (int i = 0; i < x.getDimension(); i++) {
if (!xIncludeInput.get().getValue(i))
continue;
double value = x.getArrayValue(i);
if (value < l || value > h) {
return Double.NEGATIVE_INFINITY;
}
}
}
// Inline modified version of ParametricDistribution.calcLogP()
final double fOffset = dist.offsetInput.get();
logP = 0;
for (int i = 0; i < x.getDimension(); i++) {
if (!xIncludeInput.get().getValue(i))
continue;
final double fX = x.getArrayValue(i) - fOffset;
logP += dist.logDensity(fX);
}
return logP;
}
}