source: src/main/java/negotiator/boaframework/opponentmodel/nash/StatisticsUtil.java

Last change on this file was 127, checked in by Wouter Pasman, 6 years ago

#41 ROLL BACK of rev.126 . So this version is equal to rev. 125

File size: 2.3 KB
Line 
1package negotiator.boaframework.opponentmodel.nash;
2
3import java.util.ArrayList;
4
5
6/**
7 * This class contains methods that can be used for statistical measurements on lists.
8 *
9 * @author Roland van der Linden
10 *
11 */
12public class StatisticsUtil
13{
14 /**
15 * This method adds the values in the list together and returns the sum.
16 * @param list The list containing the values.
17 * @return The sum of the values.
18 */
19 public static double getSum(ArrayList<Double> list)
20 {
21 if(list == null)
22 throw new IllegalArgumentException("The sum of a list that is null cannot be calculated.");
23
24 double result = 0;
25
26 for(Double d : list)
27 result += d;
28
29 return result;
30 }
31
32 /**
33 * This methods calculates the average value of the values in the list.
34 * @param list The list containing the values.
35 * @return The mean of the values.
36 */
37 public static double getMean(ArrayList<Double> list)
38 {
39 if(list == null)
40 throw new IllegalArgumentException("The mean of a list that is null cannot be calculated.");
41
42 if(list.size() == 0)
43 return 0;
44 else
45 return (getSum(list) / (double)list.size());
46 }
47
48 /**
49 * This method calculates the variance of the values in the list.
50 * @param list The list containing the values.
51 * @return The variance of the values.
52 */
53 public static double getVariance(ArrayList<Double> list)
54 {
55 if(list == null)
56 throw new IllegalArgumentException("The variance of a list that is null cannot be calculated.");
57
58 if(list.size() == 0)
59 return 0;
60 else
61 {
62 double mean = getMean(list);
63 double totalDifference = 0;
64 for(Double d : list)
65 {
66 double absdiff = Math.abs(d - mean);
67 double absdiff_squared = absdiff * absdiff;
68 totalDifference += absdiff_squared;
69 }
70
71 return (totalDifference / (double)list.size());
72 }
73 }
74
75 /**
76 * This method calculates the standard deviation of the values in the list.
77 * @param list The list containing the values.
78 * @return The standard deviation of the values.
79 */
80 public static double getStandardDeviation(ArrayList<Double> list)
81 {
82 if(list == null)
83 throw new IllegalArgumentException("The standardDeviation of a list that is null cannot be calculated.");
84
85 if(list.size() == 0)
86 return 0;
87 else
88 {
89 double variance = getVariance(list);
90 return Math.sqrt(variance);
91 }
92 }
93}
Note: See TracBrowser for help on using the repository browser.