001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2020, by Object Refinery Limited and Contributors.
006 *
007 * Project Info:  http://www.jfree.org/jfreechart/index.html
008 *
009 * This library is free software; you can redistribute it and/or modify it
010 * under the terms of the GNU Lesser General Public License as published by
011 * the Free Software Foundation; either version 2.1 of the License, or
012 * (at your option) any later version.
013 *
014 * This library is distributed in the hope that it will be useful, but
015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017 * License for more details.
018 *
019 * You should have received a copy of the GNU Lesser General Public
020 * License along with this library; if not, write to the Free Software
021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
022 * USA.
023 *
024 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 
025 * Other names may be trademarks of their respective owners.]
026 *
027 * ----------------------------
028 * BoxAndWhiskerCalculator.java
029 * ----------------------------
030 * (C) Copyright 2003-2016,  by Object Refinery Limited and Contributors.
031 *
032 * Original Author:  David Gilbert (for Object Refinery Limited);
033 * Contributor(s):   -;
034 *
035 * Changes
036 * -------
037 * 28-Aug-2003 : Version 1 (DG);
038 * 17-Nov-2003 : Fixed bug in calculations of outliers and median (DG);
039 * 10-Jan-2005 : Removed deprecated methods in preparation for 1.0.0
040 *               release (DG);
041 * ------------- JFREECHART 1.0.x ---------------------------------------------
042 * 15-Nov-2006 : Cleaned up handling of null arguments, and null or NaN items
043 *               in the list (DG);
044 * 02-Jul-2013 : Use ParamChecks (DG);
045 *
046 */
047
048package org.jfree.data.statistics;
049
050import java.util.ArrayList;
051import java.util.Collections;
052import java.util.Iterator;
053import java.util.List;
054import org.jfree.chart.util.Args;
055
056/**
057 * A utility class that calculates the mean, median, quartiles Q1 and Q3, plus
058 * a list of outlier values...all from an arbitrary list of
059 * {@code Number} objects.
060 */
061public abstract class BoxAndWhiskerCalculator {
062
063    /**
064     * Calculates the statistics required for a {@link BoxAndWhiskerItem}
065     * from a list of {@code Number} objects.  Any items in the list
066     * that are {@code null}, not an instance of {@code Number}, or
067     * equivalent to {@code Double.NaN}, will be ignored.
068     *
069     * @param values  a list of numbers (a {@code null} list is not
070     *                permitted).
071     *
072     * @return A box-and-whisker item.
073     */
074    public static BoxAndWhiskerItem calculateBoxAndWhiskerStatistics(
075                                        List values) {
076        return calculateBoxAndWhiskerStatistics(values, true);
077    }
078
079    /**
080     * Calculates the statistics required for a {@link BoxAndWhiskerItem}
081     * from a list of {@code Number} objects.  Any items in the list
082     * that are {@code null}, not an instance of {@code Number}, or
083     * equivalent to {@code Double.NaN}, will be ignored.
084     *
085     * @param values  a list of numbers (a {@code null} list is not
086     *                permitted).
087     * @param stripNullAndNaNItems  a flag that controls the handling of null
088     *     and NaN items.
089     *
090     * @return A box-and-whisker item.
091     *
092     * @since 1.0.3
093     */
094    public static BoxAndWhiskerItem calculateBoxAndWhiskerStatistics(
095            List values, boolean stripNullAndNaNItems) {
096
097        Args.nullNotPermitted(values, "values");
098
099        List vlist;
100        if (stripNullAndNaNItems) {
101            vlist = new ArrayList(values.size());
102            Iterator iterator = values.listIterator();
103            while (iterator.hasNext()) {
104                Object obj = iterator.next();
105                if (obj instanceof Number) {
106                    Number n = (Number) obj;
107                    double v = n.doubleValue();
108                    if (!Double.isNaN(v)) {
109                        vlist.add(n);
110                    }
111                }
112            }
113        }
114        else {
115            vlist = values;
116        }
117        Collections.sort(vlist);
118
119        double mean = Statistics.calculateMean(vlist, false);
120        double median = Statistics.calculateMedian(vlist, false);
121        double q1 = calculateQ1(vlist);
122        double q3 = calculateQ3(vlist);
123
124        double interQuartileRange = q3 - q1;
125
126        double upperOutlierThreshold = q3 + (interQuartileRange * 1.5);
127        double lowerOutlierThreshold = q1 - (interQuartileRange * 1.5);
128
129        double upperFaroutThreshold = q3 + (interQuartileRange * 2.0);
130        double lowerFaroutThreshold = q1 - (interQuartileRange * 2.0);
131
132        double minRegularValue = Double.POSITIVE_INFINITY;
133        double maxRegularValue = Double.NEGATIVE_INFINITY;
134        double minOutlier = Double.POSITIVE_INFINITY;
135        double maxOutlier = Double.NEGATIVE_INFINITY;
136        List outliers = new ArrayList();
137
138        Iterator iterator = vlist.iterator();
139        while (iterator.hasNext()) {
140            Number number = (Number) iterator.next();
141            double value = number.doubleValue();
142            if (value > upperOutlierThreshold) {
143                outliers.add(number);
144                if (value > maxOutlier && value <= upperFaroutThreshold) {
145                    maxOutlier = value;
146                }
147            }
148            else if (value < lowerOutlierThreshold) {
149                outliers.add(number);
150                if (value < minOutlier && value >= lowerFaroutThreshold) {
151                    minOutlier = value;
152                }
153            }
154            else {
155                minRegularValue = Math.min(minRegularValue, value);
156                maxRegularValue = Math.max(maxRegularValue, value);
157            }
158            minOutlier = Math.min(minOutlier, minRegularValue);
159            maxOutlier = Math.max(maxOutlier, maxRegularValue);
160        }
161
162        return new BoxAndWhiskerItem(mean, median, q1, q3, minRegularValue,
163                maxRegularValue, minOutlier, maxOutlier, outliers);
164
165    }
166
167    /**
168     * Calculates the first quartile for a list of numbers in ascending order.
169     * If the items in the list are not in ascending order, the result is
170     * unspecified.  If the list contains items that are {@code null}, not
171     * an instance of {@code Number}, or equivalent to
172     * {@code Double.NaN}, the result is unspecified.
173     *
174     * @param values  the numbers in ascending order ({@code null} not
175     *     permitted).
176     *
177     * @return The first quartile.
178     */
179    public static double calculateQ1(List values) {
180        Args.nullNotPermitted(values, "values");
181
182        double result = Double.NaN;
183        int count = values.size();
184        if (count > 0) {
185            if (count % 2 == 1) {
186                if (count > 1) {
187                    result = Statistics.calculateMedian(values, 0, count / 2);
188                }
189                else {
190                    result = Statistics.calculateMedian(values, 0, 0);
191                }
192            }
193            else {
194                result = Statistics.calculateMedian(values, 0, count / 2 - 1);
195            }
196
197        }
198        return result;
199    }
200
201    /**
202     * Calculates the third quartile for a list of numbers in ascending order.
203     * If the items in the list are not in ascending order, the result is
204     * unspecified.  If the list contains items that are {@code null}, not
205     * an instance of {@code Number}, or equivalent to
206     * {@code Double.NaN}, the result is unspecified.
207     *
208     * @param values  the list of values ({@code null} not permitted).
209     *
210     * @return The third quartile.
211     */
212    public static double calculateQ3(List values) {
213        Args.nullNotPermitted(values, "values");
214        double result = Double.NaN;
215        int count = values.size();
216        if (count > 0) {
217            if (count % 2 == 1) {
218                if (count > 1) {
219                    result = Statistics.calculateMedian(values, count / 2,
220                            count - 1);
221                }
222                else {
223                    result = Statistics.calculateMedian(values, 0, 0);
224                }
225            }
226            else {
227                result = Statistics.calculateMedian(values, count / 2,
228                        count - 1);
229            }
230        }
231        return result;
232    }
233
234}