Coverage details for edu.uci.ics.jung.statistics.StatisticalMoments

LineHitsSource
1 /*
2 * Copyright (c) 2003, the JUNG Project and the Regents of the University
3 * of California
4 * All rights reserved.
5 *
6 * This software is open-source under the BSD license; see either
7 * "license.txt" or
8 * http://jung.sourceforge.net/license.txt for a description.
9 */
10 package edu.uci.ics.jung.statistics;
11  
12 /**
13  * A data structure representing the central moments of a distribution including: <ul>
14  * <li> the mean </li>
15  * <li> the variance </li>
16  * <li> the skewness</li>
17  * <li> the kurtosis </li></ul> <br>
18  * Data values that are observed are passed into this data structure via the accumulate(...) method
19  * and the corresponding central moments are updated on each call
20  *
21  * @author Didier H. Besset (modified by Scott White)
22  */
23 public class StatisticalMoments {
24     /**
25      * Vector containing the points.
26      */
27     protected double[] moments;
28  
29     /**
30      * Default constructor methods: declare space for 5 moments.
31      */
32     public StatisticalMoments() {
334        this(5);
344    }
35  
36     /**
37      * General constructor methods.
38      * @param n number of moments to accumulate.
39      */
404    public StatisticalMoments(int n) {
414        moments = new double[n];
424        reset();
434    }
44  
45     /**
46      * statistical moment accumulation up to order 4.
47      * @param x double value to accumulate
48      */
49     public void accumulate(double x) {
501010        double n = moments[0];
511010        double n1 = n + 1;
521010        double n2 = n * n;
531010        double delta = (moments[1] - x) / n1;
541010        double d2 = delta * delta;
551010        double d3 = delta * d2;
561010        double r1 = (double) n / (double) n1;
571010        moments[4] += 4 * delta * moments[3] + 6 * d2 * moments[2]
58                 + (1 + n * n2) * d2 * d2;
591010        moments[4] *= r1;
601010        moments[3] += 3 * delta * moments[2] + (1 - n2) * d3;
611010        moments[3] *= r1;
621010        moments[2] += (1 + n) * d2;
631010        moments[2] *= r1;
641010        moments[1] -= delta;
651010        moments[0] = n1;
661010        return;
67     }
68  
69     /**
70      * @return double average.
71      */
72     public double average() {
732        return moments[1];
74     }
75  
76     /**
77      * Returns the number of accumulated counts.
78      * @return number of counts.
79      */
80     public long count() {
810        return (long) moments[0];
82     }
83  
84     /**
85      * Returns the error on average. May throw divide by zero exception.
86      * @return error on average.
87      */
88     public double errorOnAverage() {
890        return Math.sqrt(variance() / moments[0]);
90     }
91  
92     /**
93      * The kurtosis measures the sharpness of the distribution near
94      * the maximum.
95      * Note: The kurtosis of the Normal distribution is 0 by definition.
96      * @return double kurtosis or NaN.
97      */
98     public double kurtosis() throws ArithmeticException {
990        if (moments[0] < 4)
1000            return Double.NaN;
1010        double kFact = (moments[0] - 2) * (moments[0] - 3);
1020        double n1 = moments[0] - 1;
1030        double v = variance();
1040        return (moments[4] * moments[0] * moments[0] * (moments[0] + 1)
105                 / (v * v * n1) - n1 * n1 * 3) / kFact;
106     }
107  
108     /**
109      * Reset all counters.
110      */
111     public void reset() {
11224        for (int n = 0; n < moments.length; n++)
11320            moments[n] = 0;
1144    }
115  
116     /**
117      * @return double skewness.
118      */
119     public double skewness() throws ArithmeticException {
1200        if (moments[0] < 3)
1210            return Double.NaN;
1220        double v = variance();
1230        return moments[3] * moments[0] * moments[0]
124                 / (Math.sqrt(v) * v * (moments[0] - 1)
125                 * (moments[0] - 2));
126     }
127  
128     /**
129      * Returns the standard deviation. May throw divide by zero exception.
130      * @return double standard deviation.
131      */
132     public double standardDeviation() {
1330        return Math.sqrt(variance());
134     }
135  
136     /**
137      * @return double
138      */
139     public double unnormalizedVariance() {
1400        return moments[2] * moments[0];
141     }
142  
143     /**
144      * Note: the variance includes the Bessel correction factor.
145      * @return double variance.
146      */
147     public double variance() throws ArithmeticException {
1480        if (moments[0] < 2)
1490            return Double.NaN;
1500        return unnormalizedVariance() / (moments[0] - 1);
151     }
152 }

this report was generated by version 1.0.5 of jcoverage.
visit www.jcoverage.com for updates.

copyright © 2003, jcoverage ltd. all rights reserved.
Java is a trademark of Sun Microsystems, Inc. in the United States and other countries.