diff --git a/src/Numerics/Numerics.csproj b/src/Numerics/Numerics.csproj
index 63aed875..4b20d23b 100644
--- a/src/Numerics/Numerics.csproj
+++ b/src/Numerics/Numerics.csproj
@@ -234,6 +234,7 @@
+
diff --git a/src/Numerics/Statistics/ArrayStatistics.Single.cs b/src/Numerics/Statistics/ArrayStatistics.Single.cs
index 2d40a22d..d7274f7d 100644
--- a/src/Numerics/Statistics/ArrayStatistics.Single.cs
+++ b/src/Numerics/Statistics/ArrayStatistics.Single.cs
@@ -322,5 +322,443 @@ namespace MathNet.Numerics.Statistics
return Math.Sqrt(mean);
}
+
+ ///
+ /// Returns the order statistic (order 1..N) from the unsorted data array.
+ /// WARNING: Works inplace and can thus causes the data array to be reordered.
+ ///
+ /// Sample array, no sorting is assumed. Will be reordered.
+ /// One-based order of the statistic, must be between 1 and N (inclusive).
+ public static float OrderStatisticInplace(float[] data, int order)
+ {
+ if (order < 1 || order > data.Length)
+ {
+ return float.NaN;
+ }
+
+ if (order == 1)
+ {
+ return Minimum(data);
+ }
+
+ if (order == data.Length)
+ {
+ return Maximum(data);
+ }
+
+ return SelectInplace(data, order - 1);
+ }
+
+ ///
+ /// Estimates the median value from the unsorted data array.
+ /// WARNING: Works inplace and can thus causes the data array to be reordered.
+ ///
+ /// Sample array, no sorting is assumed. Will be reordered.
+ public static float MedianInplace(float[] data)
+ {
+ var k = data.Length / 2;
+ return data.Length.IsOdd()
+ ? SelectInplace(data, k)
+ : (SelectInplace(data, k - 1) + SelectInplace(data, k)) / 2.0f;
+ }
+
+ ///
+ /// Estimates the p-Percentile value from the unsorted data array.
+ /// If a non-integer Percentile is needed, use Quantile instead.
+ /// Approximately median-unbiased regardless of the sample distribution (R8).
+ /// WARNING: Works inplace and can thus causes the data array to be reordered.
+ ///
+ /// Sample array, no sorting is assumed. Will be reordered.
+ /// Percentile selector, between 0 and 100 (inclusive).
+ public static float PercentileInplace(float[] data, int p)
+ {
+ return QuantileInplace(data, p/100.0d);
+ }
+
+ ///
+ /// Estimates the first quartile value from the unsorted data array.
+ /// Approximately median-unbiased regardless of the sample distribution (R8).
+ /// WARNING: Works inplace and can thus causes the data array to be reordered.
+ ///
+ /// Sample array, no sorting is assumed. Will be reordered.
+ public static float LowerQuartileInplace(float[] data)
+ {
+ return QuantileInplace(data, 0.25d);
+ }
+
+ ///
+ /// Estimates the third quartile value from the unsorted data array.
+ /// Approximately median-unbiased regardless of the sample distribution (R8).
+ /// WARNING: Works inplace and can thus causes the data array to be reordered.
+ ///
+ /// Sample array, no sorting is assumed. Will be reordered.
+ public static float UpperQuartileInplace(float[] data)
+ {
+ return QuantileInplace(data, 0.75d);
+ }
+
+ ///
+ /// Estimates the inter-quartile range from the unsorted data array.
+ /// Approximately median-unbiased regardless of the sample distribution (R8).
+ /// WARNING: Works inplace and can thus causes the data array to be reordered.
+ ///
+ /// Sample array, no sorting is assumed. Will be reordered.
+ public static float InterquartileRangeInplace(float[] data)
+ {
+ return QuantileInplace(data, 0.75d) - QuantileInplace(data, 0.25d);
+ }
+
+ ///
+ /// Estimates {min, lower-quantile, median, upper-quantile, max} from the unsorted data array.
+ /// Approximately median-unbiased regardless of the sample distribution (R8).
+ /// WARNING: Works inplace and can thus causes the data array to be reordered.
+ ///
+ /// Sample array, no sorting is assumed. Will be reordered.
+ public static float[] FiveNumberSummaryInplace(float[] data)
+ {
+ if (data.Length == 0)
+ {
+ return new[] { float.NaN, float.NaN, float.NaN, float.NaN, float.NaN };
+ }
+
+ // TODO: Benchmark: is this still faster than sorting the array then using SortedArrayStatistics instead?
+ return new[] { Minimum(data), QuantileInplace(data, 0.25d), MedianInplace(data), QuantileInplace(data, 0.75d), Maximum(data) };
+ }
+
+ ///
+ /// Estimates the tau-th quantile from the unsorted data array.
+ /// The tau-th quantile is the data value where the cumulative distribution
+ /// function crosses tau.
+ /// Approximately median-unbiased regardless of the sample distribution (R8).
+ /// WARNING: Works inplace and can thus causes the data array to be reordered.
+ ///
+ /// Sample array, no sorting is assumed. Will be reordered.
+ /// 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.
+ /// When tau < (2/3) / (N + 1/3), use x1. When tau >= (N - 1/3) / (N + 1/3), use xN.
+ ///
+ public static float QuantileInplace(float[] data, double tau)
+ {
+ if (tau < 0d || tau > 1d || data.Length == 0)
+ {
+ return float.NaN;
+ }
+
+ double h = (data.Length + 1d / 3d) * tau + 1d / 3d;
+ var hf = (int)h;
+
+ if (hf <= 0 || tau == 0d)
+ {
+ return Minimum(data);
+ }
+
+ if (hf >= data.Length || tau == 1d)
+ {
+ return Maximum(data);
+ }
+
+ var a = SelectInplace(data, hf - 1);
+ var b = SelectInplace(data, hf);
+ return (float)(a + (h - hf) * (b - a));
+ }
+
+ ///
+ /// Estimates the tau-th quantile from the unsorted data array.
+ /// The tau-th quantile is the data value where the cumulative distribution
+ /// function crosses tau. The quantile defintion can be specified
+ /// by 4 parameters a, b, c and d, consistent with Mathematica.
+ /// WARNING: Works inplace and can thus causes the data array to be reordered.
+ ///
+ /// Sample array, no sorting is assumed. Will be reordered.
+ /// Quantile selector, between 0.0 and 1.0 (inclusive)
+ /// a-parameter
+ /// b-parameter
+ /// c-parameter
+ /// d-parameter
+ public static float QuantileCustomInplace(float[] data, double tau, double a, double b, double c, double d)
+ {
+ if (tau < 0d || tau > 1d || data.Length == 0)
+ {
+ return float.NaN;
+ }
+
+ var x = a + (data.Length + b) * tau - 1;
+#if PORTABLE
+ var ip = (int)x;
+#else
+ var ip = Math.Truncate(x);
+#endif
+ var fp = x - ip;
+
+ if (Math.Abs(fp) < 1e-9)
+ {
+ return SelectInplace(data, (int)ip);
+ }
+
+ var lower = SelectInplace(data, (int)Math.Floor(x));
+ var upper = SelectInplace(data, (int)Math.Ceiling(x));
+ return (float)(lower + (upper - lower) * (c + d * fp));
+ }
+
+ ///
+ /// Estimates the tau-th quantile from the unsorted data array.
+ /// The tau-th quantile is the data value where the cumulative distribution
+ /// function crosses tau. The quantile definition can be specified to be compatible
+ /// with an existing system.
+ /// WARNING: Works inplace and can thus causes the data array to be reordered.
+ ///
+ /// Sample array, no sorting is assumed. Will be reordered.
+ /// Quantile selector, between 0.0 and 1.0 (inclusive)
+ /// Quantile definition, to choose what product/definition it should be consistent with
+ public static float QuantileCustomInplace(float[] data, double tau, QuantileDefinition definition)
+ {
+ if (tau < 0d || tau > 1d || data.Length == 0)
+ {
+ return float.NaN;
+ }
+
+ if (tau == 0d || data.Length == 1)
+ {
+ return Minimum(data);
+ }
+
+ if (tau == 1d)
+ {
+ return Maximum(data);
+ }
+
+ switch (definition)
+ {
+ case QuantileDefinition.R1:
+ {
+ double h = data.Length * tau + 0.5d;
+ return SelectInplace(data, (int)Math.Ceiling(h - 0.5d) - 1);
+ }
+
+ case QuantileDefinition.R2:
+ {
+ double h = data.Length * tau + 0.5d;
+ return (SelectInplace(data, (int)Math.Ceiling(h - 0.5d) - 1) + SelectInplace(data, (int)(h + 0.5d) - 1)) * 0.5f;
+ }
+
+ case QuantileDefinition.R3:
+ {
+ double h = data.Length * tau;
+ return SelectInplace(data, (int)Math.Round(h) - 1);
+ }
+
+ case QuantileDefinition.R4:
+ {
+ double h = data.Length * tau;
+ var hf = (int)h;
+ var lower = SelectInplace(data, hf - 1);
+ var upper = SelectInplace(data, hf);
+ return (float)(lower + (h - hf) * (upper - lower));
+ }
+
+ case QuantileDefinition.R5:
+ {
+ double h = data.Length * tau + 0.5d;
+ var hf = (int)h;
+ var lower = SelectInplace(data, hf - 1);
+ var upper = SelectInplace(data, hf);
+ return (float)(lower + (h - hf) * (upper - lower));
+ }
+
+ case QuantileDefinition.R6:
+ {
+ double h = (data.Length + 1) * tau;
+ var hf = (int)h;
+ var lower = SelectInplace(data, hf - 1);
+ var upper = SelectInplace(data, hf);
+ return (float)(lower + (h - hf) * (upper - lower));
+ }
+
+ case QuantileDefinition.R7:
+ {
+ double h = (data.Length - 1) * tau + 1d;
+ var hf = (int)h;
+ var lower = SelectInplace(data, hf - 1);
+ var upper = SelectInplace(data, hf);
+ return (float)(lower + (h - hf) * (upper - lower));
+ }
+
+ case QuantileDefinition.R8:
+ {
+ double h = (data.Length + 1 / 3d) * tau + 1 / 3d;
+ var hf = (int)h;
+ var lower = SelectInplace(data, hf - 1);
+ var upper = SelectInplace(data, hf);
+ return (float)(lower + (h - hf) * (upper - lower));
+ }
+
+ case QuantileDefinition.R9:
+ {
+ double h = (data.Length + 0.25d) * tau + 0.375d;
+ var hf = (int)h;
+ var lower = SelectInplace(data, hf - 1);
+ var upper = SelectInplace(data, hf);
+ return (float)(lower + (h - hf) * (upper - lower));
+ }
+
+ default:
+ throw new NotSupportedException();
+ }
+ }
+
+ static float SelectInplace(float[] workingData, int rank)
+ {
+ // Numerical Recipes: select
+ // http://en.wikipedia.org/wiki/Selection_algorithm
+ if (rank <= 0)
+ {
+ return Minimum(workingData);
+ }
+
+ if (rank >= workingData.Length - 1)
+ {
+ return Maximum(workingData);
+ }
+
+ float[] a = workingData;
+ int low = 0;
+ int high = a.Length - 1;
+
+ while (true)
+ {
+ if (high <= low + 1)
+ {
+ if (high == low + 1 && a[high] < a[low])
+ {
+ var tmp = a[low];
+ a[low] = a[high];
+ a[high] = tmp;
+ }
+
+ return a[rank];
+ }
+
+ int middle = (low + high) >> 1;
+
+ var tmp1 = a[middle];
+ a[middle] = a[low + 1];
+ a[low + 1] = tmp1;
+
+ if (a[low] > a[high])
+ {
+ var tmp = a[low];
+ a[low] = a[high];
+ a[high] = tmp;
+ }
+
+ if (a[low + 1] > a[high])
+ {
+ var tmp = a[low + 1];
+ a[low + 1] = a[high];
+ a[high] = tmp;
+ }
+
+ if (a[low] > a[low + 1])
+ {
+ var tmp = a[low];
+ a[low] = a[low + 1];
+ a[low + 1] = tmp;
+ }
+
+ int begin = low + 1;
+ int end = high;
+ float pivot = a[begin];
+
+ while (true)
+ {
+ do
+ {
+ begin++;
+ }
+ while (a[begin] < pivot);
+
+ do
+ {
+ end--;
+ }
+ while (a[end] > pivot);
+
+ if (end < begin)
+ {
+ break;
+ }
+
+ var tmp = a[begin];
+ a[begin] = a[end];
+ a[end] = tmp;
+ }
+
+ a[low + 1] = a[end];
+ a[end] = pivot;
+
+ if (end >= rank)
+ {
+ high = end - 1;
+ }
+
+ if (end <= rank)
+ {
+ low = begin;
+ }
+ }
+ }
+
+ ///
+ /// Evaluates the rank of each entry of the unsorted data array.
+ /// The rank definition can be specified to be compatible
+ /// with an existing system.
+ /// WARNING: Works inplace and can thus causes the data array to be reordered.
+ ///
+ public static double[] RanksInplace(float[] data, RankDefinition definition = RankDefinition.Default)
+ {
+ var ranks = new double[data.Length];
+ var index = new int[data.Length];
+ for (int i = 0; i < index.Length; i++)
+ {
+ index[i] = i;
+ }
+
+ if (definition == RankDefinition.First)
+ {
+ Sorting.SortAll(data, index);
+ for (int i = 0; i < ranks.Length; i++)
+ {
+ ranks[index[i]] = i + 1;
+ }
+
+ return ranks;
+ }
+
+ Sorting.Sort(data, index);
+ int previousIndex = 0;
+ for (int i = 1; i < data.Length; i++)
+ {
+ if (Math.Abs(data[i] - data[previousIndex]) <= 0d)
+ {
+ continue;
+ }
+
+ if (i == previousIndex + 1)
+ {
+ ranks[index[previousIndex]] = i;
+ }
+ else
+ {
+ RanksTies(ranks, index, previousIndex, i, definition);
+ }
+
+ previousIndex = i;
+ }
+
+ RanksTies(ranks, index, previousIndex, data.Length, definition);
+ return ranks;
+ }
}
}
diff --git a/src/Numerics/Statistics/SortedArrayStatistics.Single.cs b/src/Numerics/Statistics/SortedArrayStatistics.Single.cs
new file mode 100644
index 00000000..5864affb
--- /dev/null
+++ b/src/Numerics/Statistics/SortedArrayStatistics.Single.cs
@@ -0,0 +1,484 @@
+//
+// Math.NET Numerics, part of the Math.NET Project
+// http://numerics.mathdotnet.com
+// http://github.com/mathnet/mathnet-numerics
+// http://mathnetnumerics.codeplex.com
+//
+// Copyright (c) 2009-2015 Math.NET
+//
+// Permission is hereby granted, free of charge, to any person
+// obtaining a copy of this software and associated documentation
+// files (the "Software"), to deal in the Software without
+// restriction, including without limitation the rights to use,
+// copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the
+// Software is furnished to do so, subject to the following
+// conditions:
+//
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+// OTHER DEALINGS IN THE SOFTWARE.
+//
+
+using System;
+
+namespace MathNet.Numerics.Statistics
+{
+ public static partial class SortedArrayStatistics
+ {
+ ///
+ /// Returns the smallest value from the sorted data array (ascending).
+ ///
+ /// Sample array, must be sorted ascendingly.
+ public static float Minimum(float[] data)
+ {
+ if (data.Length == 0)
+ {
+ return float.NaN;
+ }
+
+ return data[0];
+ }
+
+ ///
+ /// Returns the largest value from the sorted data array (ascending).
+ ///
+ /// Sample array, must be sorted ascendingly.
+ public static float Maximum(float[] data)
+ {
+ if (data.Length == 0)
+ {
+ return float.NaN;
+ }
+
+ return data[data.Length - 1];
+ }
+
+ ///
+ /// Returns the order statistic (order 1..N) from the sorted data array (ascending).
+ ///
+ /// Sample array, must be sorted ascendingly.
+ /// One-based order of the statistic, must be between 1 and N (inclusive).
+ public static float OrderStatistic(float[] data, int order)
+ {
+ if (order < 1 || order > data.Length)
+ {
+ return float.NaN;
+ }
+
+ return data[order - 1];
+ }
+
+ ///
+ /// Estimates the median value from the sorted data array (ascending).
+ /// Approximately median-unbiased regardless of the sample distribution (R8).
+ ///
+ /// Sample array, must be sorted ascendingly.
+ public static float Median(float[] data)
+ {
+ if (data.Length == 0)
+ {
+ return float.NaN;
+ }
+
+ var k = data.Length/2;
+ return data.Length.IsOdd()
+ ? data[k]
+ : (data[k - 1] + data[k])/2.0f;
+ }
+
+ ///
+ /// Estimates the p-Percentile value from the sorted data array (ascending).
+ /// If a non-integer Percentile is needed, use Quantile instead.
+ /// Approximately median-unbiased regardless of the sample distribution (R8).
+ ///
+ /// Sample array, must be sorted ascendingly.
+ /// Percentile selector, between 0 and 100 (inclusive).
+ public static float Percentile(float[] data, int p)
+ {
+ return Quantile(data, p/100d);
+ }
+
+ ///
+ /// Estimates the first quartile value from the sorted data array (ascending).
+ /// Approximately median-unbiased regardless of the sample distribution (R8).
+ ///
+ /// Sample array, must be sorted ascendingly.
+ public static float LowerQuartile(float[] data)
+ {
+ return Quantile(data, 0.25d);
+ }
+
+ ///
+ /// Estimates the third quartile value from the sorted data array (ascending).
+ /// Approximately median-unbiased regardless of the sample distribution (R8).
+ ///
+ /// Sample array, must be sorted ascendingly.
+ public static float UpperQuartile(float[] data)
+ {
+ return Quantile(data, 0.75d);
+ }
+
+ ///
+ /// Estimates the inter-quartile range from the sorted data array (ascending).
+ /// Approximately median-unbiased regardless of the sample distribution (R8).
+ ///
+ /// Sample array, must be sorted ascendingly.
+ public static float InterquartileRange(float[] data)
+ {
+ return Quantile(data, 0.75d) - Quantile(data, 0.25d);
+ }
+
+ ///
+ /// Estimates {min, lower-quantile, median, upper-quantile, max} from the sorted data array (ascending).
+ /// Approximately median-unbiased regardless of the sample distribution (R8).
+ ///
+ /// Sample array, must be sorted ascendingly.
+ public static float[] FiveNumberSummary(float[] data)
+ {
+ if (data.Length == 0)
+ {
+ return new[] { float.NaN, float.NaN, float.NaN, float.NaN, float.NaN };
+ }
+
+ return new[] { data[0], Quantile(data, 0.25), Median(data), 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.
+ /// Approximately median-unbiased regardless of the sample distribution (R8).
+ ///
+ /// 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.
+ /// When tau < (2/3) / (N + 1/3), use x1. When tau >= (N - 1/3) / (N + 1/3), use xN.
+ ///
+ public static float Quantile(float[] data, double tau)
+ {
+ if (tau < 0d || tau > 1d || data.Length == 0)
+ {
+ return float.NaN;
+ }
+
+ if (tau == 0d || data.Length == 1)
+ {
+ return data[0];
+ }
+
+ if (tau == 1d)
+ {
+ return data[data.Length - 1];
+ }
+
+ double h = (data.Length + 1/3d)*tau + 1/3d;
+ var hf = (int)h;
+ return hf < 1
+ ? data[0]
+ : hf >= data.Length
+ ? data[data.Length - 1]
+ : (float)(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 defintion can be specified
+ /// by 4 parameters a, b, c and d, consistent with Mathematica.
+ ///
+ /// Sample array, must be sorted ascendingly.
+ /// Quantile selector, between 0.0 and 1.0 (inclusive).
+ /// a-parameter
+ /// b-parameter
+ /// c-parameter
+ /// d-parameter
+ public static float QuantileCustom(float[] data, double tau, double a, double b, double c, double d)
+ {
+ if (tau < 0d || tau > 1d || data.Length == 0)
+ {
+ return float.NaN;
+ }
+
+ var x = a + (data.Length + b)*tau - 1;
+#if PORTABLE
+ var ip = (int) x;
+#else
+ var ip = Math.Truncate(x);
+#endif
+ var fp = x - ip;
+
+ if (Math.Abs(fp) < 1e-9)
+ {
+ return data[Math.Min(Math.Max((int)ip, 0), data.Length - 1)];
+ }
+
+ var lower = data[Math.Max((int)Math.Floor(x), 0)];
+ var upper = data[Math.Min((int)Math.Ceiling(x), data.Length - 1)];
+ return (float)(lower + (upper - lower)*(c + d*fp));
+ }
+
+ ///
+ /// 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 definition can be specified to be compatible
+ /// with an existing system.
+ ///
+ /// Sample array, must be sorted ascendingly.
+ /// Quantile selector, between 0.0 and 1.0 (inclusive).
+ /// Quantile definition, to choose what product/definition it should be consistent with
+ public static float QuantileCustom(float[] data, double tau, QuantileDefinition definition)
+ {
+ if (tau < 0d || tau > 1d || data.Length == 0)
+ {
+ return float.NaN;
+ }
+
+ if (tau == 0d || data.Length == 1)
+ {
+ return data[0];
+ }
+
+ if (tau == 1d)
+ {
+ return data[data.Length - 1];
+ }
+
+ switch (definition)
+ {
+ case QuantileDefinition.R1:
+ {
+ double h = data.Length*tau + 0.5d;
+ return data[(int)Math.Ceiling(h - 0.5d) - 1];
+ }
+
+ case QuantileDefinition.R2:
+ {
+ double h = data.Length*tau + 0.5d;
+ return (data[(int)Math.Ceiling(h - 0.5d) - 1] + data[(int)(h + 0.5d) - 1])*0.5f;
+ }
+
+ case QuantileDefinition.R3:
+ {
+ double h = data.Length*tau;
+ return data[Math.Max((int)Math.Round(h) - 1, 0)];
+ }
+
+ case QuantileDefinition.R4:
+ {
+ double h = data.Length*tau;
+ var hf = (int)h;
+ var lower = data[Math.Max(hf - 1, 0)];
+ var upper = data[Math.Min(hf, data.Length - 1)];
+ return (float)(lower + (h - hf)*(upper - lower));
+ }
+
+ case QuantileDefinition.R5:
+ {
+ double h = data.Length*tau + 0.5d;
+ var hf = (int)h;
+ var lower = data[Math.Max(hf - 1, 0)];
+ var upper = data[Math.Min(hf, data.Length - 1)];
+ return (float)(lower + (h - hf)*(upper - lower));
+ }
+
+ case QuantileDefinition.R6:
+ {
+ double h = (data.Length + 1)*tau;
+ var hf = (int)h;
+ var lower = data[Math.Max(hf - 1, 0)];
+ var upper = data[Math.Min(hf, data.Length - 1)];
+ return (float)(lower + (h - hf)*(upper - lower));
+ }
+
+ case QuantileDefinition.R7:
+ {
+ double h = (data.Length - 1)*tau + 1d;
+ var hf = (int)h;
+ var lower = data[Math.Max(hf - 1, 0)];
+ var upper = data[Math.Min(hf, data.Length - 1)];
+ return (float)(lower + (h - hf)*(upper - lower));
+ }
+
+ case QuantileDefinition.R8:
+ {
+ double h = (data.Length + 1/3d)*tau + 1/3d;
+ var hf = (int)h;
+ var lower = data[Math.Max(hf - 1, 0)];
+ var upper = data[Math.Min(hf, data.Length - 1)];
+ return (float)(lower + (h - hf)*(upper - lower));
+ }
+
+ case QuantileDefinition.R9:
+ {
+ double h = (data.Length + 0.25d)*tau + 0.375d;
+ var hf = (int)h;
+ var lower = data[Math.Max(hf - 1, 0)];
+ var upper = data[Math.Min(hf, data.Length - 1)];
+ return (float)(lower + (h - hf)*(upper - lower));
+ }
+
+ default:
+ throw new NotSupportedException();
+ }
+ }
+
+ ///
+ /// Estimates the empirical cumulative distribution function (CDF) at x from the sorted data array (ascending).
+ ///
+ /// The data sample sequence.
+ /// The value where to estimate the CDF at.
+ public static double EmpiricalCDF(float[] data, float x)
+ {
+ if (x < data[0])
+ {
+ return 0.0;
+ }
+
+ if (x >= data[data.Length - 1])
+ {
+ return 1.0;
+ }
+
+ int right = Array.BinarySearch(data, x);
+ if (right >= 0)
+ {
+ while (right < data.Length - 1 && data[right + 1] == data[right])
+ {
+ right++;
+ }
+
+ return (right + 1)/(double)data.Length;
+ }
+
+ return (~right)/(double)data.Length;
+ }
+
+ ///
+ /// Estimates the quantile tau from the sorted data array (ascending).
+ /// The tau-th quantile is the data value where the cumulative distribution
+ /// function crosses tau. The quantile definition can be specified to be compatible
+ /// with an existing system.
+ ///
+ /// The data sample sequence.
+ /// Quantile value.
+ /// Rank definition, to choose how ties should be handled and what product/definition it should be consistent with
+ public static double QuantileRank(float[] data, float x, RankDefinition definition = RankDefinition.Default)
+ {
+ if (x < data[0])
+ {
+ return 0.0;
+ }
+
+ if (x >= data[data.Length - 1])
+ {
+ return 1.0;
+ }
+
+ int right = Array.BinarySearch(data, x);
+ if (right >= 0)
+ {
+ int left = right;
+
+ while (left > 0 && data[left - 1] == data[left])
+ {
+ left--;
+ }
+
+ while (right < data.Length - 1 && data[right + 1] == data[right])
+ {
+ right++;
+ }
+
+ switch (definition)
+ {
+ case RankDefinition.EmpiricalCDF:
+ return (right + 1)/(double)data.Length;
+
+ case RankDefinition.Max:
+ return right/(double)(data.Length - 1);
+
+ case RankDefinition.Min:
+ return left/(double)(data.Length - 1);
+
+ case RankDefinition.Average:
+ return (left/(double)(data.Length - 1) + right/(double)(data.Length - 1))/2;
+
+ default:
+ throw new NotSupportedException();
+ }
+ }
+ else
+ {
+ right = ~right;
+ int left = right - 1;
+
+ switch (definition)
+ {
+ case RankDefinition.EmpiricalCDF:
+ return (left + 1)/(double)data.Length;
+
+ default:
+ {
+ var a = left/(double)(data.Length - 1);
+ var b = right/(double)(data.Length - 1);
+ return ((data[right] - x)*a + (x - data[left])*b)/(data[right] - data[left]);
+ }
+ }
+ }
+ }
+
+ ///
+ /// Evaluates the rank of each entry of the sorted data array (ascending).
+ /// The rank definition can be specified to be compatible
+ /// with an existing system.
+ ///
+ public static double[] Ranks(float[] data, RankDefinition definition = RankDefinition.Default)
+ {
+ var ranks = new double[data.Length];
+
+ if (definition == RankDefinition.First)
+ {
+ for (int i = 0; i < ranks.Length; i++)
+ {
+ ranks[i] = i + 1;
+ }
+
+ return ranks;
+ }
+
+ int previousIndex = 0;
+ for (int i = 1; i < data.Length; i++)
+ {
+ if (Math.Abs(data[i] - data[previousIndex]) <= 0d)
+ {
+ continue;
+ }
+
+ if (i == previousIndex + 1)
+ {
+ ranks[previousIndex] = i;
+ }
+ else
+ {
+ RanksTies(ranks, previousIndex, i, definition);
+ }
+
+ previousIndex = i;
+ }
+
+ RanksTies(ranks, previousIndex, data.Length, definition);
+ return ranks;
+ }
+ }
+}
diff --git a/src/Numerics/Statistics/SortedArrayStatistics.cs b/src/Numerics/Statistics/SortedArrayStatistics.cs
index 1c105b32..6d9ea85f 100644
--- a/src/Numerics/Statistics/SortedArrayStatistics.cs
+++ b/src/Numerics/Statistics/SortedArrayStatistics.cs
@@ -4,7 +4,7 @@
// http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com
//
-// Copyright (c) 2009-2013 Math.NET
+// Copyright (c) 2009-2015 Math.NET
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
@@ -38,7 +38,7 @@ namespace MathNet.Numerics.Statistics
///
///
///
- public static class SortedArrayStatistics
+ public static partial class SortedArrayStatistics
{
///
/// Returns the smallest value from the sorted data array (ascending).
@@ -54,20 +54,6 @@ namespace MathNet.Numerics.Statistics
return data[0];
}
- ///
- /// Returns the smallest value from the sorted data array (ascending).
- ///
- /// Sample array, must be sorted ascendingly.
- public static float Minimum(float[] data)
- {
- if (data.Length == 0)
- {
- return float.NaN;
- }
-
- return data[0];
- }
-
///
/// Returns the largest value from the sorted data array (ascending).
///
@@ -82,20 +68,6 @@ namespace MathNet.Numerics.Statistics
return data[data.Length - 1];
}
- ///
- /// Returns the largest value from the sorted data array (ascending).
- ///
- /// Sample array, must be sorted ascendingly.
- public static float Maximum(float[] data)
- {
- if (data.Length == 0)
- {
- return float.NaN;
- }
-
- return data[data.Length - 1];
- }
-
///
/// Returns the order statistic (order 1..N) from the sorted data array (ascending).
///
diff --git a/src/Numerics/Statistics/Statistics.cs b/src/Numerics/Statistics/Statistics.cs
index 6d78f174..2e2cd9ee 100644
--- a/src/Numerics/Statistics/Statistics.cs
+++ b/src/Numerics/Statistics/Statistics.cs
@@ -483,6 +483,21 @@ namespace MathNet.Numerics.Statistics
: StreamingStatistics.MeanVariance(samples);
}
+ ///
+ /// Estimates the sample mean and the unbiased population variance from the provided samples.
+ /// On a dataset of size N will use an N-1 normalizer (Bessel's correction).
+ /// Returns NaN for mean if data is empty or if any entry is NaN and NaN for variance if data has less than two entries or if any entry is NaN.
+ ///
+ /// The data to calculate the mean of.
+ /// The mean of the sample.
+ public static Tuple MeanVariance(this IEnumerable samples)
+ {
+ var array = samples as float[];
+ return array != null
+ ? ArrayStatistics.MeanVariance(array)
+ : StreamingStatistics.MeanVariance(samples);
+ }
+
///
/// Estimates the sample mean and the unbiased population standard deviation from the provided samples.
/// On a dataset of size N will use an N-1 normalizer (Bessel's correction).
@@ -498,6 +513,21 @@ namespace MathNet.Numerics.Statistics
: StreamingStatistics.MeanStandardDeviation(samples);
}
+ ///
+ /// Estimates the sample mean and the unbiased population standard deviation from the provided samples.
+ /// On a dataset of size N will use an N-1 normalizer (Bessel's correction).
+ /// Returns NaN for mean if data is empty or if any entry is NaN and NaN for standard deviation if data has less than two entries or if any entry is NaN.
+ ///
+ /// The data to calculate the mean of.
+ /// The mean of the sample.
+ public static Tuple MeanStandardDeviation(this IEnumerable samples)
+ {
+ var array = samples as float[];
+ return array != null
+ ? ArrayStatistics.MeanStandardDeviation(array)
+ : StreamingStatistics.MeanStandardDeviation(samples);
+ }
+
///
/// Estimates the unbiased population skewness and kurtosis from the provided samples in a single pass.
/// Uses a normalizer (Bessel's correction; type 2).
@@ -623,6 +653,19 @@ namespace MathNet.Numerics.Statistics
: StreamingStatistics.RootMeanSquare(data);
}
+ ///
+ /// Evaluates the root mean square (RMS) also known as quadratic mean.
+ /// Returns NaN if data is empty or if any entry is NaN.
+ ///
+ /// The data to calculate the RMS of.
+ public static double RootMeanSquare(this IEnumerable data)
+ {
+ var array = data as float[];
+ return array != null
+ ? ArrayStatistics.RootMeanSquare(array)
+ : StreamingStatistics.RootMeanSquare(data);
+ }
+
///
/// Evaluates the root mean square (RMS) also known as quadratic mean.
/// Returns NaN if data is empty or if any entry is NaN.
@@ -640,7 +683,17 @@ namespace MathNet.Numerics.Statistics
/// The data sample sequence.
public static double Median(this IEnumerable data)
{
- var array = data.ToArray();
+ double[] array = data.ToArray();
+ return ArrayStatistics.MedianInplace(array);
+ }
+
+ ///
+ /// Estimates the sample median from the provided samples (R8).
+ ///
+ /// The data sample sequence.
+ public static float Median(this IEnumerable data)
+ {
+ float[] array = data.ToArray();
return ArrayStatistics.MedianInplace(array);
}
@@ -650,7 +703,7 @@ namespace MathNet.Numerics.Statistics
/// The data sample sequence.
public static double Median(this IEnumerable data)
{
- var array = data.Where(d => d.HasValue).Select(d => d.Value).ToArray();
+ double[] array = data.Where(d => d.HasValue).Select(d => d.Value).ToArray();
return ArrayStatistics.MedianInplace(array);
}
@@ -664,7 +717,21 @@ namespace MathNet.Numerics.Statistics
/// Quantile selector, between 0.0 and 1.0 (inclusive).
public static double Quantile(this IEnumerable data, double tau)
{
- var array = data.ToArray();
+ double[] array = data.ToArray();
+ return ArrayStatistics.QuantileInplace(array, tau);
+ }
+
+ ///
+ /// Estimates the tau-th quantile from the provided samples.
+ /// The tau-th quantile is the data value where the cumulative distribution
+ /// function crosses tau.
+ /// Approximately median-unbiased regardless of the sample distribution (R8).
+ ///
+ /// The data sample sequence.
+ /// Quantile selector, between 0.0 and 1.0 (inclusive).
+ public static float Quantile(this IEnumerable data, double tau)
+ {
+ float[] array = data.ToArray();
return ArrayStatistics.QuantileInplace(array, tau);
}
@@ -678,7 +745,7 @@ namespace MathNet.Numerics.Statistics
/// Quantile selector, between 0.0 and 1.0 (inclusive).
public static double Quantile(this IEnumerable data, double tau)
{
- var array = data.Where(d => d.HasValue).Select(d => d.Value).ToArray();
+ double[] array = data.Where(d => d.HasValue).Select(d => d.Value).ToArray();
return ArrayStatistics.QuantileInplace(array, tau);
}
@@ -691,7 +758,21 @@ namespace MathNet.Numerics.Statistics
/// The data sample sequence.
public static Func QuantileFunc(this IEnumerable data)
{
- var array = data.ToArray();
+ double[] array = data.ToArray();
+ Array.Sort(array);
+ return tau => SortedArrayStatistics.Quantile(array, tau);
+ }
+
+ ///
+ /// Estimates the tau-th quantile from the provided samples.
+ /// The tau-th quantile is the data value where the cumulative distribution
+ /// function crosses tau.
+ /// Approximately median-unbiased regardless of the sample distribution (R8).
+ ///
+ /// The data sample sequence.
+ public static Func QuantileFunc(this IEnumerable data)
+ {
+ float[] array = data.ToArray();
Array.Sort(array);
return tau => SortedArrayStatistics.Quantile(array, tau);
}
@@ -705,7 +786,7 @@ namespace MathNet.Numerics.Statistics
/// The data sample sequence.
public static Func QuantileFunc(this IEnumerable data)
{
- var array = data.Where(d => d.HasValue).Select(d => d.Value).ToArray();
+ double[] array = data.Where(d => d.HasValue).Select(d => d.Value).ToArray();
Array.Sort(array);
return tau => SortedArrayStatistics.Quantile(array, tau);
}
@@ -721,7 +802,22 @@ namespace MathNet.Numerics.Statistics
/// Quantile definition, to choose what product/definition it should be consistent with
public static double QuantileCustom(this IEnumerable data, double tau, QuantileDefinition definition)
{
- var array = data.ToArray();
+ double[] array = data.ToArray();
+ return ArrayStatistics.QuantileCustomInplace(array, tau, definition);
+ }
+
+ ///
+ /// Estimates the tau-th quantile from the provided samples.
+ /// The tau-th quantile is the data value where the cumulative distribution
+ /// function crosses tau. The quantile definition can be specified to be compatible
+ /// with an existing system.
+ ///
+ /// The data sample sequence.
+ /// Quantile selector, between 0.0 and 1.0 (inclusive).
+ /// Quantile definition, to choose what product/definition it should be consistent with
+ public static float QuantileCustom(this IEnumerable data, double tau, QuantileDefinition definition)
+ {
+ float[] array = data.ToArray();
return ArrayStatistics.QuantileCustomInplace(array, tau, definition);
}
@@ -736,7 +832,7 @@ namespace MathNet.Numerics.Statistics
/// Quantile definition, to choose what product/definition it should be consistent with
public static double QuantileCustom(this IEnumerable data, double tau, QuantileDefinition definition)
{
- var array = data.Where(d => d.HasValue).Select(d => d.Value).ToArray();
+ double[] array = data.Where(d => d.HasValue).Select(d => d.Value).ToArray();
return ArrayStatistics.QuantileCustomInplace(array, tau, definition);
}
@@ -750,7 +846,22 @@ namespace MathNet.Numerics.Statistics
/// Quantile definition, to choose what product/definition it should be consistent with
public static Func QuantileCustomFunc(this IEnumerable data, QuantileDefinition definition)
{
- var array = data.ToArray();
+ double[] array = data.ToArray();
+ Array.Sort(array);
+ return tau => SortedArrayStatistics.QuantileCustom(array, tau, definition);
+ }
+
+ ///
+ /// Estimates the tau-th quantile from the provided samples.
+ /// The tau-th quantile is the data value where the cumulative distribution
+ /// function crosses tau. The quantile definition can be specified to be compatible
+ /// with an existing system.
+ ///
+ /// The data sample sequence.
+ /// Quantile definition, to choose what product/definition it should be consistent with
+ public static Func QuantileCustomFunc(this IEnumerable data, QuantileDefinition definition)
+ {
+ float[] array = data.ToArray();
Array.Sort(array);
return tau => SortedArrayStatistics.QuantileCustom(array, tau, definition);
}
@@ -765,7 +876,7 @@ namespace MathNet.Numerics.Statistics
/// Quantile definition, to choose what product/definition it should be consistent with
public static Func QuantileCustomFunc(this IEnumerable data, QuantileDefinition definition)
{
- var array = data.Where(d => d.HasValue).Select(d => d.Value).ToArray();
+ double[] array = data.Where(d => d.HasValue).Select(d => d.Value).ToArray();
Array.Sort(array);
return tau => SortedArrayStatistics.QuantileCustom(array, tau, definition);
}
@@ -779,7 +890,20 @@ namespace MathNet.Numerics.Statistics
/// Percentile selector, between 0 and 100 (inclusive).
public static double Percentile(this IEnumerable data, int p)
{
- var array = data.ToArray();
+ double[] array = data.ToArray();
+ return ArrayStatistics.PercentileInplace(array, p);
+ }
+
+ ///
+ /// Estimates the p-Percentile value from the provided samples.
+ /// If a non-integer Percentile is needed, use Quantile instead.
+ /// Approximately median-unbiased regardless of the sample distribution (R8).
+ ///
+ /// The data sample sequence.
+ /// Percentile selector, between 0 and 100 (inclusive).
+ public static float Percentile(this IEnumerable data, int p)
+ {
+ float[] array = data.ToArray();
return ArrayStatistics.PercentileInplace(array, p);
}
@@ -792,7 +916,7 @@ namespace MathNet.Numerics.Statistics
/// Percentile selector, between 0 and 100 (inclusive).
public static double Percentile(this IEnumerable data, int p)
{
- var array = data.Where(d => d.HasValue).Select(d => d.Value).ToArray();
+ double[] array = data.Where(d => d.HasValue).Select(d => d.Value).ToArray();
return ArrayStatistics.PercentileInplace(array, p);
}
@@ -804,7 +928,20 @@ namespace MathNet.Numerics.Statistics
/// The data sample sequence.
public static Func PercentileFunc(this IEnumerable data)
{
- var array = data.ToArray();
+ double[] array = data.ToArray();
+ Array.Sort(array);
+ return p => SortedArrayStatistics.Percentile(array, p);
+ }
+
+ ///
+ /// Estimates the p-Percentile value from the provided samples.
+ /// If a non-integer Percentile is needed, use Quantile instead.
+ /// Approximately median-unbiased regardless of the sample distribution (R8).
+ ///
+ /// The data sample sequence.
+ public static Func PercentileFunc(this IEnumerable data)
+ {
+ float[] array = data.ToArray();
Array.Sort(array);
return p => SortedArrayStatistics.Percentile(array, p);
}
@@ -817,7 +954,7 @@ namespace MathNet.Numerics.Statistics
/// The data sample sequence.
public static Func PercentileFunc(this IEnumerable data)
{
- var array = data.Where(d => d.HasValue).Select(d => d.Value).ToArray();
+ double[] array = data.Where(d => d.HasValue).Select(d => d.Value).ToArray();
Array.Sort(array);
return p => SortedArrayStatistics.Percentile(array, p);
}
@@ -829,7 +966,18 @@ namespace MathNet.Numerics.Statistics
/// The data sample sequence.
public static double LowerQuartile(this IEnumerable data)
{
- var array = data.ToArray();
+ double[] array = data.ToArray();
+ return ArrayStatistics.LowerQuartileInplace(array);
+ }
+
+ ///
+ /// Estimates the first quartile value from the provided samples.
+ /// Approximately median-unbiased regardless of the sample distribution (R8).
+ ///
+ /// The data sample sequence.
+ public static float LowerQuartile(this IEnumerable data)
+ {
+ float[] array = data.ToArray();
return ArrayStatistics.LowerQuartileInplace(array);
}
@@ -840,7 +988,7 @@ namespace MathNet.Numerics.Statistics
/// The data sample sequence.
public static double LowerQuartile(this IEnumerable data)
{
- var array = data.Where(d => d.HasValue).Select(d => d.Value).ToArray();
+ double[] array = data.Where(d => d.HasValue).Select(d => d.Value).ToArray();
return ArrayStatistics.LowerQuartileInplace(array);
}
@@ -851,7 +999,18 @@ namespace MathNet.Numerics.Statistics
/// The data sample sequence.
public static double UpperQuartile(this IEnumerable data)
{
- var array = data.ToArray();
+ double[] array = data.ToArray();
+ return ArrayStatistics.UpperQuartileInplace(array);
+ }
+
+ ///
+ /// Estimates the third quartile value from the provided samples.
+ /// Approximately median-unbiased regardless of the sample distribution (R8).
+ ///
+ /// The data sample sequence.
+ public static float UpperQuartile(this IEnumerable data)
+ {
+ float[] array = data.ToArray();
return ArrayStatistics.UpperQuartileInplace(array);
}
@@ -862,7 +1021,7 @@ namespace MathNet.Numerics.Statistics
/// The data sample sequence.
public static double UpperQuartile(this IEnumerable data)
{
- var array = data.Where(d => d.HasValue).Select(d => d.Value).ToArray();
+ double[] array = data.Where(d => d.HasValue).Select(d => d.Value).ToArray();
return ArrayStatistics.UpperQuartileInplace(array);
}
@@ -873,7 +1032,18 @@ namespace MathNet.Numerics.Statistics
/// The data sample sequence.
public static double InterquartileRange(this IEnumerable data)
{
- var array = data.ToArray();
+ double[] array = data.ToArray();
+ return ArrayStatistics.InterquartileRangeInplace(array);
+ }
+
+ ///
+ /// Estimates the inter-quartile range from the provided samples.
+ /// Approximately median-unbiased regardless of the sample distribution (R8).
+ ///
+ /// The data sample sequence.
+ public static float InterquartileRange(this IEnumerable data)
+ {
+ float[] array = data.ToArray();
return ArrayStatistics.InterquartileRangeInplace(array);
}
@@ -884,7 +1054,7 @@ namespace MathNet.Numerics.Statistics
/// The data sample sequence.
public static double InterquartileRange(this IEnumerable data)
{
- var array = data.Where(d => d.HasValue).Select(d => d.Value).ToArray();
+ double[] array = data.Where(d => d.HasValue).Select(d => d.Value).ToArray();
return ArrayStatistics.InterquartileRangeInplace(array);
}
@@ -895,7 +1065,18 @@ namespace MathNet.Numerics.Statistics
/// The data sample sequence.
public static double[] FiveNumberSummary(this IEnumerable data)
{
- var array = data.ToArray();
+ double[] array = data.ToArray();
+ return ArrayStatistics.FiveNumberSummaryInplace(array);
+ }
+
+ ///
+ /// Estimates {min, lower-quantile, median, upper-quantile, max} from the provided samples.
+ /// Approximately median-unbiased regardless of the sample distribution (R8).
+ ///
+ /// The data sample sequence.
+ public static float[] FiveNumberSummary(this IEnumerable data)
+ {
+ float[] array = data.ToArray();
return ArrayStatistics.FiveNumberSummaryInplace(array);
}
@@ -906,7 +1087,7 @@ namespace MathNet.Numerics.Statistics
/// The data sample sequence.
public static double[] FiveNumberSummary(this IEnumerable data)
{
- var array = data.Where(d => d.HasValue).Select(d => d.Value).ToArray();
+ double[] array = data.Where(d => d.HasValue).Select(d => d.Value).ToArray();
return ArrayStatistics.FiveNumberSummaryInplace(array);
}
@@ -917,7 +1098,18 @@ namespace MathNet.Numerics.Statistics
/// One-based order of the statistic, must be between 1 and N (inclusive).
public static double OrderStatistic(IEnumerable data, int order)
{
- var array = data.ToArray();
+ double[] array = data.ToArray();
+ return ArrayStatistics.OrderStatisticInplace(array, order);
+ }
+
+ ///
+ /// Returns the order statistic (order 1..N) from the provided samples.
+ ///
+ /// The data sample sequence.
+ /// One-based order of the statistic, must be between 1 and N (inclusive).
+ public static float OrderStatistic(IEnumerable data, int order)
+ {
+ float[] array = data.ToArray();
return ArrayStatistics.OrderStatisticInplace(array, order);
}
@@ -927,7 +1119,18 @@ namespace MathNet.Numerics.Statistics
/// The data sample sequence.
public static Func OrderStatisticFunc(IEnumerable data)
{
- var array = data.ToArray();
+ double[] array = data.ToArray();
+ Array.Sort(array);
+ return order => SortedArrayStatistics.OrderStatistic(array, order);
+ }
+
+ ///
+ /// Returns the order statistic (order 1..N) from the provided samples.
+ ///
+ /// The data sample sequence.
+ public static Func OrderStatisticFunc(IEnumerable data)
+ {
+ float[] array = data.ToArray();
Array.Sort(array);
return order => SortedArrayStatistics.OrderStatistic(array, order);
}
@@ -941,7 +1144,20 @@ namespace MathNet.Numerics.Statistics
/// Rank definition, to choose how ties should be handled and what product/definition it should be consistent with
public static double[] Ranks(this IEnumerable data, RankDefinition definition = RankDefinition.Default)
{
- var array = data.ToArray();
+ double[] array = data.ToArray();
+ return ArrayStatistics.RanksInplace(array, definition);
+ }
+
+ ///
+ /// Evaluates the rank of each entry of the provided samples.
+ /// The rank definition can be specified to be compatible
+ /// with an existing system.
+ ///
+ /// The data sample sequence.
+ /// Rank definition, to choose how ties should be handled and what product/definition it should be consistent with
+ public static double[] Ranks(this IEnumerable data, RankDefinition definition = RankDefinition.Default)
+ {
+ float[] array = data.ToArray();
return ArrayStatistics.RanksInplace(array, definition);
}
@@ -968,7 +1184,23 @@ namespace MathNet.Numerics.Statistics
/// Rank definition, to choose how ties should be handled and what product/definition it should be consistent with
public static double QuantileRank(this IEnumerable data, double x, RankDefinition definition = RankDefinition.Default)
{
- var array = data.ToArray();
+ double[] array = data.ToArray();
+ Array.Sort(array);
+ return SortedArrayStatistics.QuantileRank(array, x, definition);
+ }
+
+ ///
+ /// Estimates the quantile tau from the provided samples.
+ /// The tau-th quantile is the data value where the cumulative distribution
+ /// function crosses tau. The quantile definition can be specified to be compatible
+ /// with an existing system.
+ ///
+ /// The data sample sequence.
+ /// Quantile value.
+ /// Rank definition, to choose how ties should be handled and what product/definition it should be consistent with
+ public static double QuantileRank(this IEnumerable data, float x, RankDefinition definition = RankDefinition.Default)
+ {
+ float[] array = data.ToArray();
Array.Sort(array);
return SortedArrayStatistics.QuantileRank(array, x, definition);
}
@@ -997,7 +1229,22 @@ namespace MathNet.Numerics.Statistics
/// Rank definition, to choose how ties should be handled and what product/definition it should be consistent with
public static Func QuantileRankFunc(this IEnumerable data, RankDefinition definition = RankDefinition.Default)
{
- var array = data.ToArray();
+ double[] array = data.ToArray();
+ Array.Sort(array);
+ return x => SortedArrayStatistics.QuantileRank(array, x, definition);
+ }
+
+ ///
+ /// Estimates the quantile tau from the provided samples.
+ /// The tau-th quantile is the data value where the cumulative distribution
+ /// function crosses tau. The quantile definition can be specified to be compatible
+ /// with an existing system.
+ ///
+ /// The data sample sequence.
+ /// Rank definition, to choose how ties should be handled and what product/definition it should be consistent with
+ public static Func QuantileRankFunc(this IEnumerable data, RankDefinition definition = RankDefinition.Default)
+ {
+ float[] array = data.ToArray();
Array.Sort(array);
return x => SortedArrayStatistics.QuantileRank(array, x, definition);
}
@@ -1022,7 +1269,19 @@ namespace MathNet.Numerics.Statistics
/// The value where to estimate the CDF at.
public static double EmpiricalCDF(this IEnumerable data, double x)
{
- var array = data.ToArray();
+ double[] array = data.ToArray();
+ Array.Sort(array);
+ return SortedArrayStatistics.EmpiricalCDF(array, x);
+ }
+
+ ///
+ /// Estimates the empirical cumulative distribution function (CDF) at x from the provided samples.
+ ///
+ /// The data sample sequence.
+ /// The value where to estimate the CDF at.
+ public static double EmpiricalCDF(this IEnumerable data, float x)
+ {
+ float[] array = data.ToArray();
Array.Sort(array);
return SortedArrayStatistics.EmpiricalCDF(array, x);
}
@@ -1043,7 +1302,18 @@ namespace MathNet.Numerics.Statistics
/// The data sample sequence.
public static Func EmpiricalCDFFunc(this IEnumerable data)
{
- var array = data.ToArray();
+ double[] array = data.ToArray();
+ Array.Sort(array);
+ return x => SortedArrayStatistics.EmpiricalCDF(array, x);
+ }
+
+ ///
+ /// Estimates the empirical cumulative distribution function (CDF) at x from the provided samples.
+ ///
+ /// The data sample sequence.
+ public static Func EmpiricalCDFFunc(this IEnumerable data)
+ {
+ float[] array = data.ToArray();
Array.Sort(array);
return x => SortedArrayStatistics.EmpiricalCDF(array, x);
}
@@ -1064,7 +1334,18 @@ namespace MathNet.Numerics.Statistics
/// Quantile selector, between 0.0 and 1.0 (inclusive).
public static double EmpiricalInvCDF(this IEnumerable data, double tau)
{
- var array = data.ToArray();
+ double[] array = data.ToArray();
+ return ArrayStatistics.QuantileCustomInplace(array, tau, QuantileDefinition.EmpiricalInvCDF);
+ }
+
+ ///
+ /// Estimates the empirical inverse CDF at tau from the provided samples.
+ ///
+ /// The data sample sequence.
+ /// Quantile selector, between 0.0 and 1.0 (inclusive).
+ public static float EmpiricalInvCDF(this IEnumerable data, double tau)
+ {
+ float[] array = data.ToArray();
return ArrayStatistics.QuantileCustomInplace(array, tau, QuantileDefinition.EmpiricalInvCDF);
}
@@ -1084,7 +1365,18 @@ namespace MathNet.Numerics.Statistics
/// The data sample sequence.
public static Func EmpiricalInvCDFFunc(this IEnumerable data)
{
- var array = data.ToArray();
+ double[] array = data.ToArray();
+ Array.Sort(array);
+ return tau => SortedArrayStatistics.QuantileCustom(array, tau, QuantileDefinition.EmpiricalInvCDF);
+ }
+
+ ///
+ /// Estimates the empirical inverse CDF at tau from the provided samples.
+ ///
+ /// The data sample sequence.
+ public static Func EmpiricalInvCDFFunc(this IEnumerable data)
+ {
+ float[] array = data.ToArray();
Array.Sort(array);
return tau => SortedArrayStatistics.QuantileCustom(array, tau, QuantileDefinition.EmpiricalInvCDF);
}