tensor smooth uniform logpdf and dLogpdf
@@ -48,12 +48,58 @@ public class TensorSmoothUniform {
plus(secondConditionalFalseNestedFalse.times(secondConditionalFalseNestedFalseResult));
}
+ public static DoubleTensor logPdf(DoubleTensor xMin, DoubleTensor xMax, DoubleTensor shoulderWidth, DoubleTensor x) {
+
+ final DoubleTensor bodyWidth = xMax.minus(xMin);
+ final DoubleTensor rightCutoff = xMax.plus(shoulderWidth);
+ final DoubleTensor leftCutoff = xMin.minus(shoulderWidth);
+
+ final DoubleTensor firstConditional = x.getGreaterThanOrEqualToMask(xMin);
+ firstConditional.timesInPlace(x.getLessThanOrEqualToMask(xMax));
+ final DoubleTensor firstConditionalResult = bodyHeight(shoulderWidth, bodyWidth);
+
+ final DoubleTensor secondConditional = x.getLessThanMask(xMin);
+ secondConditional.timesInPlace(x.getGreaterThanMask(leftCutoff));
+ final DoubleTensor secondConditionalResult = shoulder(shoulderWidth, bodyWidth, x.minus(leftCutoff));
+
+ final DoubleTensor thirdConditional = x.getGreaterThanMask(xMax);
+ thirdConditional.timesInPlace(x.getLessThanMask(rightCutoff));
+ final DoubleTensor thirdConditionalResult = shoulder(shoulderWidth, bodyWidth, shoulderWidth.minus(x).plus(xMax));
+
+ return firstConditional.times(firstConditionalResult).
+ plus(secondConditional.times(secondConditionalResult)).
+ plus(thirdConditional.times(thirdConditionalResult));
+ }
+
+ public static DoubleTensor dlnPdf(DoubleTensor xMin, DoubleTensor xMax, DoubleTensor shoulderWidth, DoubleTensor x) {
+ final DoubleTensor bodyWidth = xMax.minus(xMin);
+ final DoubleTensor leftCutoff = xMin.minus(shoulderWidth);
+ final DoubleTensor rightCutoff = xMax.plus(shoulderWidth);
+
+ final DoubleTensor firstConditional = x.getLessThanMask(xMin);
+ firstConditional.timesInPlace(x.getGreaterThanMask(leftCutoff));
+ final DoubleTensor firstConditionalResult = dShoulder(shoulderWidth, bodyWidth, x.minus(leftCutoff));
+
+ final DoubleTensor secondConditional = x.getGreaterThanMask(xMax);
+ secondConditional.times(x.getGreaterThanMask(rightCutoff));
+ final DoubleTensor secondConditionalResult = dShoulder(shoulderWidth, bodyWidth, shoulderWidth.minus(x).plus(rightCutoff)).unaryMinus();
+
+ return firstConditional.times(firstConditionalResult).
+ plus(secondConditional.times(secondConditionalResult));
+ }
+
private static DoubleTensor shoulder(DoubleTensor Sw, DoubleTensor Bw, DoubleTensor x) {
final DoubleTensor A = getCubeCoefficient(Sw, Bw);
final DoubleTensor B = getSquareCoefficient(Sw, Bw);
return A.times(x.pow(3)).plus(B.times(x.pow(2)));
}
+ private static DoubleTensor dShoulder(DoubleTensor Sw, DoubleTensor Bw, DoubleTensor x) {
+ final DoubleTensor A = getCubeCoefficient(Sw, Bw);
+ final DoubleTensor B = getSquareCoefficient(Sw, Bw);
+ return A.times(3).times(x.pow(2)).plus(B.times(x).times(2));
+ }
+
private static DoubleTensor getCubeCoefficient(DoubleTensor Sw, DoubleTensor Bw) {
return (Sw.pow(3).times(Sw.plus(Bw))).reciprocal().times(-2);
}
