1 16 package org.apache.commons.math.stat.descriptive.moment; 17 18 import java.io.Serializable ; 19 20 import org.apache.commons.math.stat.descriptive.AbstractStorelessUnivariateStatistic; 21 22 50 public class Variance extends AbstractStorelessUnivariateStatistic implements Serializable { 51 52 53 static final long serialVersionUID = -9111962718267217978L; 54 55 56 protected SecondMoment moment = null; 57 58 63 protected boolean incMoment = true; 64 65 70 private boolean isBiasCorrected = true; 71 72 76 public Variance() { 77 moment = new SecondMoment(); 78 } 79 80 86 public Variance(final SecondMoment m2) { 87 incMoment = false; 88 this.moment = m2; 89 } 90 91 99 public Variance(boolean isBiasCorrected) { 100 moment = new SecondMoment(); 101 this.isBiasCorrected = isBiasCorrected; 102 } 103 104 113 public Variance(boolean isBiasCorrected, SecondMoment m2) { 114 incMoment = false; 115 this.moment = m2; 116 this.isBiasCorrected = isBiasCorrected; 117 } 118 119 122 public void increment(final double d) { 123 if (incMoment) { 124 moment.increment(d); 125 } 126 } 127 128 131 public double getResult() { 132 if (moment.n == 0) { 133 return Double.NaN; 134 } else if (moment.n == 1) { 135 return 0d; 136 } else { 137 if (isBiasCorrected) { 138 return moment.m2 / ((double) moment.n - 1d); 139 } else { 140 return moment.m2 / ((double) moment.n); 141 } 142 } 143 } 144 145 148 public long getN() { 149 return moment.getN(); 150 } 151 152 155 public void clear() { 156 if (incMoment) { 157 moment.clear(); 158 } 159 } 160 161 177 public double evaluate(final double[] values) { 178 if (values == null) { 179 throw new IllegalArgumentException ("input values array is null"); 180 } 181 return evaluate(values, 0, values.length); 182 } 183 184 204 public double evaluate(final double[] values, final int begin, final int length) { 205 206 double var = Double.NaN; 207 208 if (test(values, begin, length)) { 209 clear(); 210 if (length == 1) { 211 var = 0.0; 212 } else if (length > 1) { 213 Mean mean = new Mean(); 214 double m = mean.evaluate(values, begin, length); 215 var = evaluate(values, m, begin, length); 216 } 217 } 218 return var; 219 } 220 221 247 public double evaluate(final double[] values, final double mean, 248 final int begin, final int length) { 249 250 double var = Double.NaN; 251 252 if (test(values, begin, length)) { 253 if (length == 1) { 254 var = 0.0; 255 } else if (length > 1) { 256 double accum = 0.0; 257 double accum2 = 0.0; 258 for (int i = begin; i < begin + length; i++) { 259 accum += Math.pow((values[i] - mean), 2.0); 260 accum2 += (values[i] - mean); 261 } 262 if (isBiasCorrected) { 263 var = (accum - (Math.pow(accum2, 2) / ((double) length))) / 264 (double) (length - 1); 265 } else { 266 var = (accum - (Math.pow(accum2, 2) / ((double) length))) / 267 (double) length; 268 } 269 } 270 } 271 return var; 272 } 273 274 299 public double evaluate(final double[] values, final double mean) { 300 return evaluate(values, mean, 0, values.length); 301 } 302 303 306 public boolean isBiasCorrected() { 307 return isBiasCorrected; 308 } 309 310 313 public void setBiasCorrected(boolean isBiasCorrected) { 314 this.isBiasCorrected = isBiasCorrected; 315 } 316 317 } 318 | Popular Tags |