Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
kill another out of date script
[simgrid.git] / contrib / psg / src / peersim / util / MomentStats.java
1 /*
2  * Copyright (c) 2003-2005 The BISON Project
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16  *
17  */
18
19 package peersim.util;
20
21 /**
22  * This class provides extended statistical informations about the inspected 
23  * distribution. In particular, it provides functions to compute the skewness
24  * (the 3rd degree moment) and the kurtosis (4th degree moment).
25  *
26  * @author  Gian Paolo Jesi
27  */
28 public class MomentStats extends IncrementalStats {
29     
30     private double cubicsum, quadsum; // incremental sums
31     
32     /** Calls {@link #reset} */
33     public MomentStats() {
34         reset();
35     }
36     
37     public void reset() {
38         super.reset();
39         cubicsum = quadsum = 0.0;
40     }
41     
42     public void add(double item, int k) {
43         for(int i=0; i<k; ++i)
44         {
45                 super.add(item,1);
46                 cubicsum += item * item * item;
47                 quadsum += item * cubicsum;
48         }
49     }
50    
51     /** Outputs on a single line the superclass statistics postfixed by the 
52      * current skewness and kurtosis.
53      */
54     public String toString() {
55         return super.toString()+" "+getSkewness()+" "+getKurtosis();
56     }
57     
58     /** Computes the skewness on the node values distribution and 
59      * returns the asymmetry coefficient. It gives an indication about the 
60      * distribution symmetry compared to the average.
61      *
62      *@return The skewness value as a double.
63      */ 
64     public double getSkewness() {
65         int n = this.getN();
66         double m3 = (((double)n) / (n-1)) * (cubicsum/n - Math.pow(getAverage(), 3) );
67         return ( m3 / Math.pow(getStD(), 3 ) );
68     }
69     
70     /** Computes the kurtosis on the node values distribution and 
71      *  returns the flatness coefficient. It gives an indication about the 
72      *  distribution sharpness or flatness.
73      *
74      * @return The kurtosis momentus value as a double.
75      */ 
76     public double getKurtosis(){
77         int n = this.getN();
78         double m4 = (((double)n) / (n-1)) * (quadsum/n - Math.pow(getAverage(), 4) );
79         return ( m4 / Math.pow(getStD(), 4) )-3;
80     }
81
82 }