-
Notifications
You must be signed in to change notification settings - Fork 7
/
Uniform.java
90 lines (71 loc) · 3.14 KB
/
Uniform.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
90
package lphy.base.distribution;
import lphy.core.model.GenerativeDistribution1D;
import lphy.core.model.RandomVariable;
import lphy.core.model.Value;
import lphy.core.model.ValueUtils;
import lphy.core.model.annotation.GeneratorCategory;
import lphy.core.model.annotation.GeneratorInfo;
import lphy.core.model.annotation.ParameterInfo;
import org.apache.commons.math3.random.RandomGenerator;
import java.util.Map;
import java.util.TreeMap;
import static lphy.base.distribution.DistributionConstants.lowerParamName;
import static lphy.base.distribution.DistributionConstants.upperParamName;
/**
* Created by Alexei Drummond on 18/12/19.
*/
public class Uniform extends ParametricDistribution<Double> implements GenerativeDistribution1D<Double> {
private Value<Number> lower;
private Value<Number> upper;
public Uniform(@ParameterInfo(name = DistributionConstants.lowerParamName, description = "the lower bound of the uniform distribution.") Value<Number> lower,
@ParameterInfo(name = DistributionConstants.upperParamName, description = "the upper bound of the uniform distribution.") Value<Number> upper) {
super();
this.lower = lower;
this.upper = upper;
// constructDistribution(random);
}
@Override
protected void constructDistribution(RandomGenerator random) {
}
@GeneratorInfo(name = "Uniform",
category = GeneratorCategory.PRIOR, examples = {"simFossilsCompact.lphy"},
description = "The uniform probability distribution.")
public RandomVariable<Double> sample() {
double l = ValueUtils.doubleValue(lower);
double u = ValueUtils.doubleValue(upper);
double x = random.nextDouble() * (u - l) + l;
return new RandomVariable<Double>(null, x, this);
}
public double logDensity(Double x) {
if (x < ValueUtils.doubleValue(lower) || x > ValueUtils.doubleValue(upper)) return Double.NEGATIVE_INFINITY;
return Math.log(1.0) - Math.log(ValueUtils.doubleValue(upper) - ValueUtils.doubleValue(lower));
}
public double density(Double x) {
if (x < ValueUtils.doubleValue(lower) || x > ValueUtils.doubleValue(upper)) return 0.0;
return 1.0 / (ValueUtils.doubleValue(upper) - ValueUtils.doubleValue(lower));
}
public Map<String, Value> getParams() {
return new TreeMap<>() {{
put(DistributionConstants.lowerParamName, lower);
put(DistributionConstants.upperParamName, upper);
}};
}
@Override
public void setParam(String paramName, Value value) {
if (paramName.equals(lowerParamName)) lower = value;
else if (paramName.equals(upperParamName)) upper = value;
else throw new RuntimeException("Unrecognised parameter name: " + paramName);
super.setParam(paramName, value); // constructDistribution
}
private static final Double[] domainBounds = {Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY};
@Override
public Double[] getDomainBounds() {
return domainBounds;
}
public Value<Number> getLower() {
return lower;
}
public Value<Number> getUpper() {
return upper;
}
}