Here you can find the source of getVariance(Collection extends Number> data, Number mean)
Parameter | Description |
---|---|
data | the data (<code>null</code> or zero length array not permitted). |
public static double getVariance(Collection<? extends Number> data, Number mean)
//package com.java2s; /**//from ww w . jav a 2 s .c o m * This file is part of SADL, a library for learning all sorts of (timed) automata and performing sequence-based anomaly detection. * Copyright (C) 2013-2016 the original author or authors. * * SADL is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. * * SADL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with SADL. If not, see <http://www.gnu.org/licenses/>. */ import java.util.Collection; import java.util.Iterator; public class Main { /** * Returns the standard deviation of a set of numbers. * * @param data * the data (<code>null</code> or zero length array not permitted). * * @return The standard deviation of a set of numbers. */ public static double getVariance(Collection<? extends Number> data, Number mean) { double result = Double.NaN; if (data != null && data.size() > 0 && mean != null) { double sum = 0.0; for (final Number n : data) { final double diff = n.doubleValue() - mean.doubleValue(); sum = sum + (diff * diff); } result = sum / (data.size()); } return result; } public static double getVariance(Collection<? extends Number> data) { final double avg = calculateMean(data); return getVariance(data, avg); } /** * Returns the mean of a collection of {@link Number} objects. * * @param values * {@link Collection} of values; elements may not be {@code null}; {@code NaN} and infinite values will be ignored * * @return The mean of the given {@link Collection} */ public static double calculateMean(Collection<? extends Number> values) { int count = 0; double total = 0.0; final Iterator<? extends Number> iterator = values.iterator(); while (iterator.hasNext()) { final double value = iterator.next().doubleValue(); if (!Double.isNaN(value) && !Double.isInfinite(value)) { total += value; count++; } } return total / count; } }