From c500524fce5043b4719670d75b4a40f9b88ef68b Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Thu, 21 Mar 2013 09:38:08 +0100 Subject: [PATCH] Statistics: add min, max, percentile, quaritle, iqr, fivenum to sorted-array stats --- .../Statistics/SortedArrayStatistics.cs | 95 +++++++++++++++++++ 1 file changed, 95 insertions(+) diff --git a/src/Numerics/Statistics/SortedArrayStatistics.cs b/src/Numerics/Statistics/SortedArrayStatistics.cs index 00641e57..76030d79 100644 --- a/src/Numerics/Statistics/SortedArrayStatistics.cs +++ b/src/Numerics/Statistics/SortedArrayStatistics.cs @@ -45,6 +45,96 @@ namespace MathNet.Numerics.Statistics const double Third = 1d / 3d; const double Half = 1d / 2d; + /// + /// Returns the smallest value from the sorted data array (ascending). + /// + /// Sample array, must be sorted ascendingly. + public static double Minimum(double[] data) + { + if (data == null || data.Length == 0) return double.NaN; + return data[0]; + } + + /// + /// Returns the largest value from the sorted data array (ascending). + /// + /// Sample array, must be sorted ascendingly. + public static double Maximum(double[] data) + { + if (data == null || data.Length == 0) return double.NaN; + return data[data.Length - 1]; + } + + /// + /// Estimates the median value from the sorted data array (ascending). + /// Applies a linear interpolation, consistent with Quantile and R-8. + /// + /// Sample array, must be sorted ascendingly. + public static double Median(double[] data) + { + return Quantile(data, 0.5d); + } + + /// + /// Estimates the p-Percentile value from the sorted data array (ascending). + /// Applies a linear interpolation, consistent with Quantile and R-8. + /// If a non-integer Percentile is needed, use Quantile instead. + /// + /// Sample array, must be sorted ascendingly. + /// Percentile selector, between 0 and 100 (inclusive). + public static double Percentile(double[] data, int p) + { + return Quantile(data, p / 100d); + } + + /// + /// Estimates the first quartile value from the sorted data array (ascending). + /// Applies a linear interpolation, consistent with Quantile and R-8. + /// + /// Sample array, must be sorted ascendingly. + public static double LowerQuartile(double[] data) + { + return Quantile(data, 0.25d); + } + + /// + /// Estimates the third quartile value from the sorted data array (ascending). + /// Applies a linear interpolation, consistent with Quantile and R-8. + /// + /// Sample array, must be sorted ascendingly. + public static double UpperQuartile(double[] data) + { + return Quantile(data, 0.75d); + } + + /// + /// Estimates the inter-quartile range from the sorted data array (ascending). + /// Applies a linear interpolation, consistent with Quantile and R-8. + /// + /// Sample array, must be sorted ascendingly. + public static double InterquartileRange(double[] data) + { + return Quantile(data, 0.75d) - Quantile(data, 0.25d); + } + + /// + /// Estimates {min, lower-quantile, median, upper-quantile, max} from the sorted data array (ascending). + /// Applies a linear interpolation, consistent with Quantile and R-8. + /// + /// Sample array, must be sorted ascendingly. + public static double[] FiveNumberSummary(double[] data) + { + if (data == null || data.Length == 0) return new[] {double.NaN, double.NaN, double.NaN, double.NaN, double.NaN}; + return new[] {data[0], Quantile(data, 0.25), Quantile(data, 0.50), Quantile(data, 0.75), data[data.Length - 1]}; + } + + /// + /// Estimates the tau-th quantile from the sorted data array (ascending). + /// The tau-th quantile is the data value where the cumulative distribution + /// function crosses tau. Applies a linear interpolation, compatible with R-8. + /// + /// Sample array, must be sorted ascendingly. + /// Quantile selector, between 0.0 and 1.0 (inclusive). /// /// R-8, SciPy-(1/3,1/3): /// Linear interpolation of the approximate medians for order statistics. @@ -61,6 +151,11 @@ namespace MathNet.Numerics.Statistics return data[hf - 1] + (h - hf)*(data[hf] - data[hf - 1]); } + /// + /// Estimates the tau-th quantile from the sorted data array (ascending). + /// The tau-th quantile is the data value where the cumulative distribution + /// function crosses tau. The quantile algorithm can be chosen by the compatibility argument. + /// public static double QuantileCompatible(double[] data, double tau, QuantileCompatibility compatibility) { if (tau < 0d || tau > 1d || data == null || data.Length == 0) return double.NaN;