diff --git a/src/main/java/org/broadinstitute/hellbender/utils/MathUtils.java b/src/main/java/org/broadinstitute/hellbender/utils/MathUtils.java index edd19e5686c..64ff97fbbe2 100644 --- a/src/main/java/org/broadinstitute/hellbender/utils/MathUtils.java +++ b/src/main/java/org/broadinstitute/hellbender/utils/MathUtils.java @@ -6,7 +6,9 @@ import org.apache.commons.math3.exception.NumberIsTooLargeException; import org.apache.commons.math3.random.RandomGenerator; import org.apache.commons.math3.special.Gamma; +import org.apache.commons.math3.stat.descriptive.moment.Mean; import org.apache.commons.math3.stat.descriptive.rank.Median; +import org.apache.commons.math3.stat.descriptive.summary.Sum; import org.apache.commons.math3.stat.descriptive.rank.Percentile; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.MathArrays; @@ -164,13 +166,26 @@ public static int median(final int[] values) { Utils.nonNull(values); return (int) FastMath.round(new Median().evaluate(Arrays.stream(values).mapToDouble(n -> n).toArray())); } + + public double sd(final int[] values) { + Utils.nonNull(values); + if ( values.length < 2 ) return 0.0; + double mean = new Mean().evaluate(Arrays.stream(values).mapToDouble(n -> n).toArray()); + double square_sum = new Sum().evaluate(Arrays.stream(values).mapToDouble(n -> n - mean).toArray()); + return Math.sqrt(square_sum / (values.length - 1)); + } + + public static int mad(final int[] values) { + double median = median(values); + return (int) FastMath.round(new Median().evaluate(Arrays.stream(values).mapToDouble(n -> n - median).toArray())); + } public static int median(final int[] values, final Percentile.EstimationType type) { Utils.nonNull(values); return (int) FastMath.round(new Median().withEstimationType(type).evaluate(Arrays.stream(values).mapToDouble(n -> n).toArray())); } - public static double dotProduct(double[] a, double[] b){ + public static double dotProduct(double[] a, double[] b) { return sum(MathArrays.ebeMultiply(Utils.nonNull(a), Utils.nonNull(b))); }