StdDevFunction.java
Go to the documentation of this file.00001 package edu.rice.cs.hpc.data.experiment.metric;
00002
00003 import com.graphbuilder.math.func.Function;
00004
00008 public class StdDevFunction implements Function {
00009
00010 public StdDevFunction() {}
00011
00015 public double of(double[] d, int numParam) {
00016 double sum = 0, sqrsum = 0;
00017 double mean, stdev;
00018
00019 for (int i = 0; i < numParam; i++) {
00020 sum += d[i];
00021 sqrsum += (d[i] * d[i]);
00022 }
00023
00024 mean = sum / numParam;
00025 stdev = (sqrsum) / numParam - (mean * mean);
00026 return java.lang.Math.sqrt(stdev);
00027 }
00028
00032 public boolean acceptNumParam(int numParam) {
00033 return numParam > 0;
00034 }
00035
00036 public String toString() {
00037 return "stdev(x1, x2, ..., xn)";
00038 }
00039
00040 static public void main(String args[]) {
00041 double num[] = {1,2,4,5};
00042 final double answer = 1.83;
00043 StdDevFunction f = new StdDevFunction();
00044 double result = f.of(num, num.length);
00045 System.out.println("Result = "+result+" correct answer = "+answer+" test: " + (result == answer));
00046 }
00047 }