Skip to content

Commit

Permalink
williamfiset#338 added standard deviation and tests for it
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmadMusawi committed Oct 30, 2022
1 parent c399405 commit 0b9e5a0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.williamfiset.algorithms.math;

public class StandardDeviation {

public static double SD(double arr[]) {
double sum = 0, StandardDeviation = 0;
int length = arr.length;
for (double num : arr) {
sum += num;
}
double mean = sum / length;
for (double num : arr) {
StandardDeviation = StandardDeviation + Math.pow(num - mean, 2);
}
return Math.sqrt((StandardDeviation / length));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.williamfiset.algorithms.math;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.Test;
public class StandardDeviationTest {

@Test
public void StandardDeviation4(){
double[] array = {-1, -2, -3, -4, -5};
double result = StandardDeviation.SD(array);
assertEquals(1.4142135623730951, result);
}

@Test
public void StandardDeviation2(){
double[] array = {3, 5, 7, 20, 55, 12, 1, 3, 5, 4};
double result = StandardDeviation.SD(array);
assertEquals(15.428869044748549, result);
}
}

0 comments on commit 0b9e5a0

Please sign in to comment.