Java BigDecimal stdDevMaxFromMapField(List> dataList, String fieldName, BigDecimal stdDevMultiplier)

Here you can find the source of stdDevMaxFromMapField(List> dataList, String fieldName, BigDecimal stdDevMultiplier)

Description

Returns Map with total, squaredTotal, count, average, stdDev, maximum; fieldName field in Maps must have type BigDecimal; if count of non-null fields is less than 2 returns null as cannot calculate a standard deviation

License

Creative Commons License

Declaration

public static Map<String, BigDecimal> stdDevMaxFromMapField(List<Map<String, Object>> dataList,
        String fieldName, BigDecimal stdDevMultiplier) 

Method Source Code


//package com.java2s;
/*//from ww w.  ja  v  a2  s . com
 * This software is in the public domain under CC0 1.0 Universal plus a
 * Grant of Patent License.
 *
 * To the extent possible under law, the author(s) have dedicated all
 * copyright and related and neighboring rights to this software to the
 * public domain worldwide. This software is distributed without any
 * warranty.
 *
 * You should have received a copy of the CC0 Public Domain Dedication
 * along with this software (see the LICENSE.md file). If not, see
 * <http://creativecommons.org/publicdomain/zero/1.0/>.
 */

import java.math.BigDecimal;

import java.util.*;

public class Main {
    /** Returns Map with total, squaredTotal, count, average, stdDev, maximum; fieldName field in Maps must have type BigDecimal;
     * if count of non-null fields is less than 2 returns null as cannot calculate a standard deviation */
    public static Map<String, BigDecimal> stdDevMaxFromMapField(List<Map<String, Object>> dataList,
            String fieldName, BigDecimal stdDevMultiplier) {
        BigDecimal total = BigDecimal.ZERO;
        BigDecimal squaredTotal = BigDecimal.ZERO;
        int count = 0;
        for (Map<String, Object> dataMap : dataList) {
            if (dataMap == null)
                continue;
            BigDecimal value = (BigDecimal) dataMap.get(fieldName);
            if (value == null)
                continue;
            total = total.add(value);
            squaredTotal = squaredTotal.add(value.multiply(value));
            count++;
        }
        if (count < 2)
            return null;

        BigDecimal countBd = new BigDecimal(count);
        BigDecimal average = total.divide(countBd, BigDecimal.ROUND_HALF_UP);
        double totalDouble = total.doubleValue();
        BigDecimal stdDev = new BigDecimal(Math
                .sqrt(Math.abs(squaredTotal.doubleValue() - ((totalDouble * totalDouble) / count)) / (count - 1)));

        Map<String, BigDecimal> retMap = new HashMap<>(6);
        retMap.put("total", total);
        retMap.put("squaredTotal", squaredTotal);
        retMap.put("count", countBd);
        retMap.put("average", average);
        retMap.put("stdDev", stdDev);

        if (stdDevMultiplier != null)
            retMap.put("maximum", average.add(stdDev.multiply(stdDevMultiplier)));

        return retMap;
    }
}

Related

  1. sine(BigDecimal x)
  2. sortByValues( Map map)
  3. sortMapByBDValue(Map oriMap)
  4. stdBigDecimal(List vector, BigDecimal avg)
  5. stddev(List numbers, boolean biasCorrected, MathContext context)
  6. Str2BigDecimal(String a)
  7. str2BigDicimal(String strbigdecimal)
  8. stripAndOrRescale(BigDecimal value)
  9. stripTrailingZeros(BigDecimal bigDecimal)