001 package calhoun.analysis.crf.statistics;
002
003 import java.util.Arrays;
004
005 import calhoun.util.Assert;
006
007 public class BasicStats {
008
009 public static double meanDoubleArray( double[] x) {
010 Assert.a(x.length>=1);
011 double sum = 0;
012 for (int j=0; j<x.length; j++) { sum+= x[j]; }
013 return (sum/x.length);
014 }
015
016 public static double medianDoubleArray(double[] x) {
017 Assert.a(x.length > 0);
018 double[] y = x;
019 Arrays.sort(y);
020 return (y[y.length/2]);
021 }
022
023 public static double sumDoubleArray( double[] x) {
024 double ret = 0.00;
025 for (int j=0; j<x.length; j++) {
026 ret += x[j];
027 }
028 return ret;
029 }
030
031 public static double L1Distance(double[] x, double[] y) {
032 Assert.a(x.length == y.length);
033 double ret = 0.00;
034 for (int j=0; j<x.length; j++) {
035 ret += Math.abs(x[j]-y[j]);
036 }
037 return ret;
038 }
039
040 public static int argmax(double[] y) {
041 Assert.a(y.length>0);
042 double val = y[0];
043 int ret = 0;
044 for (int j=1; j<y.length; j++) {
045 if (y[j]>val) {
046 ret = j;
047 val = y[j];
048 }
049 }
050 return ret;
051 }
052
053 public static double max(double[] y) {
054 Assert.a(y.length>0);
055 double ret = y[0];
056 for (int j=1; j<y.length; j++) {
057 if (y[j]>ret) {
058 ret = y[j];
059 }
060 }
061 return ret;
062 }
063
064 }