Refactored the logProb function to remove comment and hopefully make it clearer what's going on
@@ -43,22 +43,24 @@ public class Pareto implements ContinuousDistribution {
@Override
public DoubleTensor logProb(DoubleTensor x) {
- DoubleTensor result;
+ if (checkParamsAreValid()) {
+ DoubleTensor result = scale.log().plusInPlace(location.log().timesInPlace(scale))
+ .minusInPlace(scale.plus(1.0).timesInPlace(x.log()));
- /*
- * If we've been passed invalid values, then return Negative Infinity for all values, else just return the
- * calculated value
- */
- if (!location.greaterThan(0.0).allTrue() || !scale.greaterThan(0.0).allTrue()) {
- result = DoubleTensor.create(Double.NEGATIVE_INFINITY, x.getShape());
+ return setProbToZeroForInvalidX(x, result);
} else {
- DoubleTensor invalids = x.getLessThanOrEqualToMask(location);
- result = scale.log().plusInPlace(location.log().timesInPlace(scale))
- .minusInPlace(scale.plus(1.0).timesInPlace(x.log()));
+ return DoubleTensor.create(Double.NEGATIVE_INFINITY, x.getShape());
+ }
+ }
- result.setWithMaskInPlace(invalids, Double.NEGATIVE_INFINITY);
+ private boolean checkParamsAreValid() {
+ return location.greaterThan(0.0).allTrue() && scale.greaterThan(0.0).allTrue();
}
+ private DoubleTensor setProbToZeroForInvalidX(DoubleTensor x, DoubleTensor result) {
+ DoubleTensor invalids = x.getLessThanOrEqualToMask(location);
+ result.setWithMaskInPlace(invalids, Double.NEGATIVE_INFINITY);
+
return result;
}
}
