1 16 package org.apache.commons.math.stat.descriptive.moment; 17 18 import org.apache.commons.math.stat.descriptive.AbstractStorelessUnivariateStatistic; 19 20 40 public class Kurtosis extends AbstractStorelessUnivariateStatistic { 41 42 43 static final long serialVersionUID = 2784465764798260919L; 44 45 46 protected FourthMoment moment; 47 48 54 protected boolean incMoment; 55 56 59 public Kurtosis() { 60 incMoment = true; 61 moment = new FourthMoment(); 62 } 63 64 69 public Kurtosis(final FourthMoment m4) { 70 incMoment = false; 71 this.moment = m4; 72 } 73 74 77 public void increment(final double d) { 78 if (incMoment) { 79 moment.increment(d); 80 } else { 81 throw new IllegalStateException 82 ("Statistics constructed from external moments cannot be incremented"); 83 } 84 } 85 86 89 public double getResult() { 90 double kurtosis = Double.NaN; 91 if (moment.getN() > 3) { 92 double variance = moment.m2 / (double) (moment.n - 1); 93 if (moment.n <= 3 || variance < 10E-20) { 94 kurtosis = 0.0; 95 } else { 96 double n = (double) moment.n; 97 kurtosis = 98 (n * (n + 1) * moment.m4 - 99 3 * moment.m2 * moment.m2 * (n - 1)) / 100 ((n - 1) * (n -2) * (n -3) * variance * variance); 101 } 102 } 103 return kurtosis; 104 } 105 106 109 public void clear() { 110 if (incMoment) { 111 moment.clear(); 112 } else { 113 throw new IllegalStateException 114 ("Statistics constructed from external moments cannot be cleared"); 115 } 116 } 117 118 121 public long getN() { 122 return moment.getN(); 123 } 124 125 126 127 143 public double evaluate(final double[] values,final int begin, final int length) { 144 double kurt = Double.NaN; 146 147 if (test(values, begin, length) && length > 3) { 148 149 Variance variance = new Variance(); 151 variance.incrementAll(values, begin, length); 152 double mean = variance.moment.m1; 153 double stdDev = Math.sqrt(variance.getResult()); 154 155 double accum3 = 0.0; 158 for (int i = begin; i < begin + length; i++) { 159 accum3 += Math.pow((values[i] - mean), 4.0); 160 } 161 accum3 /= Math.pow(stdDev, 4.0d); 162 163 double n0 = length; 165 166 double coefficientOne = 167 (n0 * (n0 + 1)) / ((n0 - 1) * (n0 - 2) * (n0 - 3)); 168 double termTwo = 169 ((3 * Math.pow(n0 - 1, 2.0)) / ((n0 - 2) * (n0 - 3))); 170 171 kurt = (coefficientOne * accum3) - termTwo; 173 } 174 return kurt; 175 } 176 177 } 178 | Popular Tags |