| Line | Hits | Source |
|---|---|---|
| 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() { | |
| 33 | 4 | this(5); |
| 34 | 4 | } |
| 35 | ||
| 36 | /** | |
| 37 | * General constructor methods. | |
| 38 | * @param n number of moments to accumulate. | |
| 39 | */ | |
| 40 | 4 | public StatisticalMoments(int n) { |
| 41 | 4 | moments = new double[n]; |
| 42 | 4 | reset(); |
| 43 | 4 | } |
| 44 | ||
| 45 | /** | |
| 46 | * statistical moment accumulation up to order 4. | |
| 47 | * @param x double value to accumulate | |
| 48 | */ | |
| 49 | public void accumulate(double x) { | |
| 50 | 1010 | double n = moments[0]; |
| 51 | 1010 | double n1 = n + 1; |
| 52 | 1010 | double n2 = n * n; |
| 53 | 1010 | double delta = (moments[1] - x) / n1; |
| 54 | 1010 | double d2 = delta * delta; |
| 55 | 1010 | double d3 = delta * d2; |
| 56 | 1010 | double r1 = (double) n / (double) n1; |
| 57 | 1010 | moments[4] += 4 * delta * moments[3] + 6 * d2 * moments[2] |
| 58 | + (1 + n * n2) * d2 * d2; | |
| 59 | 1010 | moments[4] *= r1; |
| 60 | 1010 | moments[3] += 3 * delta * moments[2] + (1 - n2) * d3; |
| 61 | 1010 | moments[3] *= r1; |
| 62 | 1010 | moments[2] += (1 + n) * d2; |
| 63 | 1010 | moments[2] *= r1; |
| 64 | 1010 | moments[1] -= delta; |
| 65 | 1010 | moments[0] = n1; |
| 66 | 1010 | return; |
| 67 | } | |
| 68 | ||
| 69 | /** | |
| 70 | * @return double average. | |
| 71 | */ | |
| 72 | public double average() { | |
| 73 | 2 | return moments[1]; |
| 74 | } | |
| 75 | ||
| 76 | /** | |
| 77 | * Returns the number of accumulated counts. | |
| 78 | * @return number of counts. | |
| 79 | */ | |
| 80 | public long count() { | |
| 81 | 0 | 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() { | |
| 89 | 0 | 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 { | |
| 99 | 0 | if (moments[0] < 4) |
| 100 | 0 | return Double.NaN; |
| 101 | 0 | double kFact = (moments[0] - 2) * (moments[0] - 3); |
| 102 | 0 | double n1 = moments[0] - 1; |
| 103 | 0 | double v = variance(); |
| 104 | 0 | 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() { | |
| 112 | 24 | for (int n = 0; n < moments.length; n++) |
| 113 | 20 | moments[n] = 0; |
| 114 | 4 | } |
| 115 | ||
| 116 | /** | |
| 117 | * @return double skewness. | |
| 118 | */ | |
| 119 | public double skewness() throws ArithmeticException { | |
| 120 | 0 | if (moments[0] < 3) |
| 121 | 0 | return Double.NaN; |
| 122 | 0 | double v = variance(); |
| 123 | 0 | 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() { | |
| 133 | 0 | return Math.sqrt(variance()); |
| 134 | } | |
| 135 | ||
| 136 | /** | |
| 137 | * @return double | |
| 138 | */ | |
| 139 | public double unnormalizedVariance() { | |
| 140 | 0 | 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 { | |
| 148 | 0 | if (moments[0] < 2) |
| 149 | 0 | return Double.NaN; |
| 150 | 0 | return unnormalizedVariance() / (moments[0] - 1); |
| 151 | } | |
| 152 | } |
|
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |