Skip to content
Snippets Groups Projects
Commit bb6fc1f2 authored by felmer's avatar felmer
Browse files

LMS-1187 add stdev function

SVN: 12788
parent 43594dc0
No related branches found
No related tags found
No related merge requests found
......@@ -119,13 +119,38 @@ public final class StandardFunctions
List<Double> array = toDoubleArray(values);
assertNotEmpty(array, "avg");
double sum = 0.0;
for (Double value : array)
for (double value : array)
{
sum += value;
}
return sum / array.size();
}
/**
* Calculates the standard deviation of the specified values.
* Blank strings or <code>null</code> values in the list are ignored.
*
* @throws NumberFormatException if an element can not be parsed as a floating-point number.
* @throws IllegalArgumentException if the list is empty.
*/
public static Double stdev(List<Object> values)
{
List<Double> array = toDoubleArray(values);
assertNotEmpty(array, "stdev");
double s1 = 0.0;
double s2 = 0.0;
for (double value : array)
{
s1 += value;
s2 += value * value;
}
int n = array.size();
double m1 = s1 / n;
double m2 = s2 / n;
return Math.sqrt(Math.max(0, m2 - m1 * m1));
}
/**
* Calculates the median of the specified values.
* Blank strings or <code>null</code> values in the list are ignored.
......
......@@ -20,6 +20,7 @@ import static ch.systemsx.cisd.openbis.generic.client.web.server.calculator.Stan
import static ch.systemsx.cisd.openbis.generic.client.web.server.calculator.StandardFunctions.DOUBLE_DEFAULT_VALUE;
import static ch.systemsx.cisd.openbis.generic.client.web.server.calculator.StandardFunctions.choose;
import static ch.systemsx.cisd.openbis.generic.client.web.server.calculator.StandardFunctions.avg;
import static ch.systemsx.cisd.openbis.generic.client.web.server.calculator.StandardFunctions.stdev;
import static ch.systemsx.cisd.openbis.generic.client.web.server.calculator.StandardFunctions.median;
import static ch.systemsx.cisd.openbis.generic.client.web.server.calculator.StandardFunctions.min;
import static ch.systemsx.cisd.openbis.generic.client.web.server.calculator.StandardFunctions.max;
......@@ -130,6 +131,31 @@ public class StandardFunctionsTest extends AssertJUnit
@Test
public void testAvg()
{
assertEquals(0.0, stdev(Arrays.<Object>asList(1.5)));
assertEquals(2.0, stdev(Arrays.<Object>asList(2, 4, 4, 4, 5, 5, 7, 9)));
assertEquals(0.0, stdev(Arrays.<Object>asList(null, 1)));
assertEquals(4.5, stdev(Arrays.<Object>asList(" ", 1, "10")));
try
{
stdev(Arrays.<Object>asList());
fail("IllegalArgumentException expected");
} catch (IllegalArgumentException ex)
{
assertEquals("Argument of function 'stdev' is an empty array.", ex.getMessage());
}
try
{
stdev(Arrays.<Object>asList("a"));
fail("NumberFormatException expected");
} catch (NumberFormatException ex)
{
// ignored
}
}
@Test
public void testStdev()
{
assertEquals(1.5, avg(Arrays.<Object>asList(1.5)));
assertEquals(5.0, avg(Arrays.<Object>asList(1, 4, 10)));
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment