001/* Copyright (C) 2013 TU Dortmund 002 * This file is part of LearnLib, http://www.learnlib.de/. 003 * 004 * LearnLib is free software; you can redistribute it and/or 005 * modify it under the terms of the GNU Lesser General Public 006 * License version 3.0 as published by the Free Software Foundation. 007 * 008 * LearnLib is distributed in the hope that it will be useful, 009 * but WITHOUT ANY WARRANTY; without even the implied warranty of 010 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 011 * Lesser General Public License for more details. 012 * 013 * You should have received a copy of the GNU Lesser General Public 014 * License along with LearnLib; if not, see 015 * <http://www.gnu.de/documents/lgpl.en.html>. 016 */ 017 018package de.learnlib.statistics; 019 020import java.util.Map.Entry; 021import java.util.SortedMap; 022import java.util.TreeMap; 023 024/** 025 * A simple histogram data set. 026 * 027 * @author falkhowar 028 */ 029public class HistogramDataSet extends StatisticData { 030 031 private SortedMap<Long,Integer> histogram = new TreeMap<>(); 032 033 private long size = 0; 034 035 private long sum = 0; 036 037 private double mean = 0.0; 038 039 public HistogramDataSet(String name, String unit) { 040 super(name, unit); 041 } 042 043 public void addDataPoint(Long value) { 044 Integer i = histogram.get(value); 045 if (i == null) { 046 i = 0; 047 } 048 histogram.put(value, i+1); 049 sum += value; 050 size++; 051 mean = mean + ((((double) value) - mean) / size); 052 } 053 054 055 public SortedMap<Long, Integer> getHistogram() { 056 return histogram; 057 } 058 059 public double getMedian() { 060 long idx = 0; 061 for (Entry<Long,Integer> e : histogram.entrySet()) { 062 int count = e.getValue(); 063 idx += count; 064 if (idx >= size/2) { 065 return e.getValue(); 066 } 067 } 068 return 0.0; 069 } 070 071 public double getMean() { 072 return mean; 073 } 074 075 public long getSize() { 076 return size; 077 } 078 079 public long getSum() { 080 return sum; 081 } 082 083 @Override 084 public String getSummary() { 085 return getName() + " [" + getUnit() + "]: " + 086 size + " (count), " + 087 sum + " (sum), " + 088 mean + " (mean), " + 089 getMedian() + " (median)"; 090 } 091 092 @Override 093 public String getDetails() { 094 StringBuilder sb = new StringBuilder(); 095 sb.append(getSummary()).append(System.getProperty("line.separator")); 096 for (Entry<Long,Integer> e : histogram.entrySet()) { 097 sb.append(" ").append(e.getKey()). 098 append(", ").append(e.getValue()). 099 append(System.getProperty("line.separator")); 100 } 101 return sb.toString(); 102 } 103 104}