source: src/main/java/agents/ai2014/group11/Statistics.java@ 61

Last change on this file since 61 was 1, checked in by Wouter Pasman, 6 years ago

Initial import : Genius 9.0.0

File size: 1.3 KB
Line 
1package agents.ai2014.group11;
2
3import java.util.Arrays;
4
5/**
6 * Class to use for basic statistics.
7 *
8 * Source:
9 * http://stackoverflow.com/questions/7988486/how-do-you-calculate-the-variance-median-and-standard-deviation-in-c-or-java
10 */
11public class Statistics {
12
13 /**
14 * Get the mean of dataset
15 * @param data
16 * @return
17 */
18 public static double getMean(double[] data) {
19 double sum = 0.0;
20 for (double a : data)
21 sum += a;
22 return sum / data.length;
23 }
24
25 /**
26 * Get the variance of dataset
27 * @param data
28 * @return
29 */
30 public static double getVariance(double[] data) {
31 double mean = getMean(data);
32 double temp = 0;
33 for (double a : data)
34 temp += (mean - a) * (mean - a);
35 return temp / data.length;
36 }
37
38 /**
39 * Get the standard deviation of dataset
40 * @param data
41 * @return
42 */
43 public static double getStdDev(double[] data) {
44 return Math.sqrt(getVariance(data));
45 }
46
47 /**
48 * Get the median of dataset
49 * @param data
50 * @return
51 */
52 public static double median(double[] data) {
53 double[] b = new double[data.length];
54 System.arraycopy(data, 0, b, 0, b.length);
55 Arrays.sort(b);
56
57 if (data.length % 2 == 0) {
58 return (b[(b.length / 2) - 1] + b[b.length / 2]) / 2.0;
59 } else {
60 return b[b.length / 2];
61 }
62 }
63}
Note: See TracBrowser for help on using the repository browser.