Skip to content

Commit

Permalink
add calculation in MathUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
Schaudge committed Oct 21, 2021
1 parent 35e7be8 commit acecb7b
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/main/java/org/broadinstitute/hellbender/utils/MathUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)));
}

Expand Down

0 comments on commit acecb7b

Please sign in to comment.