KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hammurapi > inspectors > metrics > statistics > DescriptiveStatistic


1 /*
2 Copyright © 1999 CERN - European Organization for Nuclear Research.
3 Permission to use, copy, modify, distribute and sell this software and its documentation for any purpose
4 is hereby granted without fee, provided that the above copyright notice appear in all copies and
5 that both that copyright notice and this permission notice appear in supporting documentation.
6 CERN makes no representations about the suitability of this software for any purpose.
7 It is provided "as is" without expressed or implied warranty.
8 */

9
10 package org.hammurapi.inspectors.metrics.statistics;
11
12
13 //!! job: average, median
14

15 /**
16  * @author mucbj0
17  *
18  * To change the template for this generated type comment go to
19  * Window - Preferences - Java - Code Generation - Code and Comments
20  */

21 public class DescriptiveStatistic {
22
23
24     /**
25      * Computes the frequency (number of occurances, count) of each distinct value in the given sorted data.
26      * After this call returns both <tt>distinctValues</tt> and <tt>frequencies</tt> have a new size (which is equal for both), which is the number of distinct values in the sorted data.
27      * <p>
28      * Distinct values are filled into <tt>distinctValues</tt>, starting at index 0.
29      * The frequency of each distinct value is filled into <tt>frequencies</tt>, starting at index 0.
30      * As a result, the smallest distinct value (and its frequency) can be found at index 0, the second smallest distinct value (and its frequency) at index 1, ..., the largest distinct value (and its frequency) at index <tt>distinctValues.size()-1</tt>.
31      *
32      * <b>Example:</b>
33      * <br>
34      * <tt>elements = (5,6,6,7,8,8) --> distinctValues = (5,6,7,8), frequencies = (1,2,1,2)</tt>
35      *
36      * @param sortedData the data; must be sorted ascending.
37      * @param distinctValues a list to be filled with the distinct values; can have any size.
38      * @param frequencies a list to be filled with the frequencies; can have any size; set this parameter to <tt>null</tt> to ignore it.
39      */

40     public void frequencies(IntVector sortedData, IntVector distinctValues, IntVector frequencies) {
41
42
43         int size = sortedData.size();
44         int i=0;
45         sortedData.sort();
46
47         while (i<size) {
48             int element = sortedData.elementAt(i);
49             int cursor = i;
50
51             // determine run length (number of equal elements)
52
while (++i < size && sortedData.elementAt(i)==element);
53
54             int runLength = i - cursor;
55             distinctValues.addElement(element );
56             if (frequencies!=null) frequencies.addElement( runLength);
57         }
58     }
59     /**
60      * Returns the arithmetic mean of a data sequence;
61      * That is <tt>Sum( data[i] ) / data.size()</tt>.
62      */

63     public static double mean(IntVector data) {
64         if (data != null && data.size()>0 ){
65             return sum(data) / data.size();
66         } else {
67             return 0;
68         }
69     }
70
71     public static int sum(IntVector data) {
72         int sum = 0;
73         for ( int i= 0; i<data.size(); i++){
74             sum += data.elementAt(i);
75         }
76         return sum;
77
78     }
79     }
80
Popular Tags