Browse Source

Statistics: single precision order statistics #235

pull/362/head
Christoph Ruegg 11 years ago
parent
commit
3a0d55fd2e
  1. 1
      src/Numerics/Numerics.csproj
  2. 438
      src/Numerics/Statistics/ArrayStatistics.Single.cs
  3. 484
      src/Numerics/Statistics/SortedArrayStatistics.Single.cs
  4. 32
      src/Numerics/Statistics/SortedArrayStatistics.cs
  5. 354
      src/Numerics/Statistics/Statistics.cs

1
src/Numerics/Numerics.csproj

@ -234,6 +234,7 @@
<Compile Include="Statistics\RunningStatistics.cs" />
<Compile Include="Statistics\QuantileDefinition.cs" />
<Compile Include="Statistics\RankDefinition.cs" />
<Compile Include="Statistics\SortedArrayStatistics.Single.cs" />
<Compile Include="Statistics\StreamingStatistics.cs" />
<Compile Include="Statistics\SortedArrayStatistics.cs" />
<Compile Include="LinearAlgebra\Storage\SparseVectorStorage.cs" />

438
src/Numerics/Statistics/ArrayStatistics.Single.cs

@ -322,5 +322,443 @@ namespace MathNet.Numerics.Statistics
return Math.Sqrt(mean);
}
/// <summary>
/// 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.
/// </summary>
/// <param name="data">Sample array, no sorting is assumed. Will be reordered.</param>
/// <param name="order">One-based order of the statistic, must be between 1 and N (inclusive).</param>
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);
}
/// <summary>
/// Estimates the median value from the unsorted data array.
/// WARNING: Works inplace and can thus causes the data array to be reordered.
/// </summary>
/// <param name="data">Sample array, no sorting is assumed. Will be reordered.</param>
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;
}
/// <summary>
/// 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.
/// </summary>
/// <param name="data">Sample array, no sorting is assumed. Will be reordered.</param>
/// <param name="p">Percentile selector, between 0 and 100 (inclusive).</param>
public static float PercentileInplace(float[] data, int p)
{
return QuantileInplace(data, p/100.0d);
}
/// <summary>
/// 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.
/// </summary>
/// <param name="data">Sample array, no sorting is assumed. Will be reordered.</param>
public static float LowerQuartileInplace(float[] data)
{
return QuantileInplace(data, 0.25d);
}
/// <summary>
/// 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.
/// </summary>
/// <param name="data">Sample array, no sorting is assumed. Will be reordered.</param>
public static float UpperQuartileInplace(float[] data)
{
return QuantileInplace(data, 0.75d);
}
/// <summary>
/// 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.
/// </summary>
/// <param name="data">Sample array, no sorting is assumed. Will be reordered.</param>
public static float InterquartileRangeInplace(float[] data)
{
return QuantileInplace(data, 0.75d) - QuantileInplace(data, 0.25d);
}
/// <summary>
/// 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.
/// </summary>
/// <param name="data">Sample array, no sorting is assumed. Will be reordered.</param>
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) };
}
/// <summary>
/// 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.
/// </summary>
/// <param name="data">Sample array, no sorting is assumed. Will be reordered.</param>
/// <param name="tau">Quantile selector, between 0.0 and 1.0 (inclusive).</param>
/// <remarks>
/// R-8, SciPy-(1/3,1/3):
/// Linear interpolation of the approximate medians for order statistics.
/// When tau &lt; (2/3) / (N + 1/3), use x1. When tau &gt;= (N - 1/3) / (N + 1/3), use xN.
/// </remarks>
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));
}
/// <summary>
/// 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.
/// </summary>
/// <param name="data">Sample array, no sorting is assumed. Will be reordered.</param>
/// <param name="tau">Quantile selector, between 0.0 and 1.0 (inclusive)</param>
/// <param name="a">a-parameter</param>
/// <param name="b">b-parameter</param>
/// <param name="c">c-parameter</param>
/// <param name="d">d-parameter</param>
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));
}
/// <summary>
/// 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.
/// </summary>
/// <param name="data">Sample array, no sorting is assumed. Will be reordered.</param>
/// <param name="tau">Quantile selector, between 0.0 and 1.0 (inclusive)</param>
/// <param name="definition">Quantile definition, to choose what product/definition it should be consistent with</param>
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;
}
}
}
/// <summary>
/// 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.
/// </summary>
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;
}
}
}

484
src/Numerics/Statistics/SortedArrayStatistics.Single.cs

@ -0,0 +1,484 @@
// <copyright file="SortedArrayStatistics.Single.cs" company="Math.NET">
// 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.
// </copyright>
using System;
namespace MathNet.Numerics.Statistics
{
public static partial class SortedArrayStatistics
{
/// <summary>
/// Returns the smallest value from the sorted data array (ascending).
/// </summary>
/// <param name="data">Sample array, must be sorted ascendingly.</param>
public static float Minimum(float[] data)
{
if (data.Length == 0)
{
return float.NaN;
}
return data[0];
}
/// <summary>
/// Returns the largest value from the sorted data array (ascending).
/// </summary>
/// <param name="data">Sample array, must be sorted ascendingly.</param>
public static float Maximum(float[] data)
{
if (data.Length == 0)
{
return float.NaN;
}
return data[data.Length - 1];
}
/// <summary>
/// Returns the order statistic (order 1..N) from the sorted data array (ascending).
/// </summary>
/// <param name="data">Sample array, must be sorted ascendingly.</param>
/// <param name="order">One-based order of the statistic, must be between 1 and N (inclusive).</param>
public static float OrderStatistic(float[] data, int order)
{
if (order < 1 || order > data.Length)
{
return float.NaN;
}
return data[order - 1];
}
/// <summary>
/// Estimates the median value from the sorted data array (ascending).
/// Approximately median-unbiased regardless of the sample distribution (R8).
/// </summary>
/// <param name="data">Sample array, must be sorted ascendingly.</param>
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;
}
/// <summary>
/// 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).
/// </summary>
/// <param name="data">Sample array, must be sorted ascendingly.</param>
/// <param name="p">Percentile selector, between 0 and 100 (inclusive).</param>
public static float Percentile(float[] data, int p)
{
return Quantile(data, p/100d);
}
/// <summary>
/// Estimates the first quartile value from the sorted data array (ascending).
/// Approximately median-unbiased regardless of the sample distribution (R8).
/// </summary>
/// <param name="data">Sample array, must be sorted ascendingly.</param>
public static float LowerQuartile(float[] data)
{
return Quantile(data, 0.25d);
}
/// <summary>
/// Estimates the third quartile value from the sorted data array (ascending).
/// Approximately median-unbiased regardless of the sample distribution (R8).
/// </summary>
/// <param name="data">Sample array, must be sorted ascendingly.</param>
public static float UpperQuartile(float[] data)
{
return Quantile(data, 0.75d);
}
/// <summary>
/// Estimates the inter-quartile range from the sorted data array (ascending).
/// Approximately median-unbiased regardless of the sample distribution (R8).
/// </summary>
/// <param name="data">Sample array, must be sorted ascendingly.</param>
public static float InterquartileRange(float[] data)
{
return Quantile(data, 0.75d) - Quantile(data, 0.25d);
}
/// <summary>
/// Estimates {min, lower-quantile, median, upper-quantile, max} from the sorted data array (ascending).
/// Approximately median-unbiased regardless of the sample distribution (R8).
/// </summary>
/// <param name="data">Sample array, must be sorted ascendingly.</param>
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] };
}
/// <summary>
/// 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).
/// </summary>
/// <param name="data">Sample array, must be sorted ascendingly.</param>
/// <param name="tau">Quantile selector, between 0.0 and 1.0 (inclusive).</param>
/// <remarks>
/// R-8, SciPy-(1/3,1/3):
/// Linear interpolation of the approximate medians for order statistics.
/// When tau &lt; (2/3) / (N + 1/3), use x1. When tau &gt;= (N - 1/3) / (N + 1/3), use xN.
/// </remarks>
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]));
}
/// <summary>
/// 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.
/// </summary>
/// <param name="data">Sample array, must be sorted ascendingly.</param>
/// <param name="tau">Quantile selector, between 0.0 and 1.0 (inclusive).</param>
/// <param name="a">a-parameter</param>
/// <param name="b">b-parameter</param>
/// <param name="c">c-parameter</param>
/// <param name="d">d-parameter</param>
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));
}
/// <summary>
/// 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.
/// </summary>
/// <param name="data">Sample array, must be sorted ascendingly.</param>
/// <param name="tau">Quantile selector, between 0.0 and 1.0 (inclusive).</param>
/// <param name="definition">Quantile definition, to choose what product/definition it should be consistent with</param>
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();
}
}
/// <summary>
/// Estimates the empirical cumulative distribution function (CDF) at x from the sorted data array (ascending).
/// </summary>
/// <param name="data">The data sample sequence.</param>
/// <param name="x">The value where to estimate the CDF at.</param>
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;
}
/// <summary>
/// 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.
/// </summary>
/// <param name="data">The data sample sequence.</param>
/// <param name="x">Quantile value.</param>
/// <param name="definition">Rank definition, to choose how ties should be handled and what product/definition it should be consistent with</param>
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]);
}
}
}
}
/// <summary>
/// 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.
/// </summary>
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;
}
}
}

32
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
/// <seealso cref="ArrayStatistics"/>
/// <seealso cref="StreamingStatistics"/>
/// <seealso cref="Statistics"/>
public static class SortedArrayStatistics
public static partial class SortedArrayStatistics
{
/// <summary>
/// Returns the smallest value from the sorted data array (ascending).
@ -54,20 +54,6 @@ namespace MathNet.Numerics.Statistics
return data[0];
}
/// <summary>
/// Returns the smallest value from the sorted data array (ascending).
/// </summary>
/// <param name="data">Sample array, must be sorted ascendingly.</param>
public static float Minimum(float[] data)
{
if (data.Length == 0)
{
return float.NaN;
}
return data[0];
}
/// <summary>
/// Returns the largest value from the sorted data array (ascending).
/// </summary>
@ -82,20 +68,6 @@ namespace MathNet.Numerics.Statistics
return data[data.Length - 1];
}
/// <summary>
/// Returns the largest value from the sorted data array (ascending).
/// </summary>
/// <param name="data">Sample array, must be sorted ascendingly.</param>
public static float Maximum(float[] data)
{
if (data.Length == 0)
{
return float.NaN;
}
return data[data.Length - 1];
}
/// <summary>
/// Returns the order statistic (order 1..N) from the sorted data array (ascending).
/// </summary>

354
src/Numerics/Statistics/Statistics.cs

@ -483,6 +483,21 @@ namespace MathNet.Numerics.Statistics
: StreamingStatistics.MeanVariance(samples);
}
/// <summary>
/// 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.
/// </summary>
/// <param name="samples">The data to calculate the mean of.</param>
/// <returns>The mean of the sample.</returns>
public static Tuple<double, double> MeanVariance(this IEnumerable<float> samples)
{
var array = samples as float[];
return array != null
? ArrayStatistics.MeanVariance(array)
: StreamingStatistics.MeanVariance(samples);
}
/// <summary>
/// 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);
}
/// <summary>
/// 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.
/// </summary>
/// <param name="samples">The data to calculate the mean of.</param>
/// <returns>The mean of the sample.</returns>
public static Tuple<double, double> MeanStandardDeviation(this IEnumerable<float> samples)
{
var array = samples as float[];
return array != null
? ArrayStatistics.MeanStandardDeviation(array)
: StreamingStatistics.MeanStandardDeviation(samples);
}
/// <summary>
/// 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);
}
/// <summary>
/// Evaluates the root mean square (RMS) also known as quadratic mean.
/// Returns NaN if data is empty or if any entry is NaN.
/// </summary>
/// <param name="data">The data to calculate the RMS of.</param>
public static double RootMeanSquare(this IEnumerable<float> data)
{
var array = data as float[];
return array != null
? ArrayStatistics.RootMeanSquare(array)
: StreamingStatistics.RootMeanSquare(data);
}
/// <summary>
/// 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
/// <param name="data">The data sample sequence.</param>
public static double Median(this IEnumerable<double> data)
{
var array = data.ToArray();
double[] array = data.ToArray();
return ArrayStatistics.MedianInplace(array);
}
/// <summary>
/// Estimates the sample median from the provided samples (R8).
/// </summary>
/// <param name="data">The data sample sequence.</param>
public static float Median(this IEnumerable<float> data)
{
float[] array = data.ToArray();
return ArrayStatistics.MedianInplace(array);
}
@ -650,7 +703,7 @@ namespace MathNet.Numerics.Statistics
/// <param name="data">The data sample sequence.</param>
public static double Median(this IEnumerable<double?> 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
/// <param name="tau">Quantile selector, between 0.0 and 1.0 (inclusive).</param>
public static double Quantile(this IEnumerable<double> data, double tau)
{
var array = data.ToArray();
double[] array = data.ToArray();
return ArrayStatistics.QuantileInplace(array, tau);
}
/// <summary>
/// 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).
/// </summary>
/// <param name="data">The data sample sequence.</param>
/// <param name="tau">Quantile selector, between 0.0 and 1.0 (inclusive).</param>
public static float Quantile(this IEnumerable<float> data, double tau)
{
float[] array = data.ToArray();
return ArrayStatistics.QuantileInplace(array, tau);
}
@ -678,7 +745,7 @@ namespace MathNet.Numerics.Statistics
/// <param name="tau">Quantile selector, between 0.0 and 1.0 (inclusive).</param>
public static double Quantile(this IEnumerable<double?> 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
/// <param name="data">The data sample sequence.</param>
public static Func<double, double> QuantileFunc(this IEnumerable<double> data)
{
var array = data.ToArray();
double[] array = data.ToArray();
Array.Sort(array);
return tau => SortedArrayStatistics.Quantile(array, tau);
}
/// <summary>
/// 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).
/// </summary>
/// <param name="data">The data sample sequence.</param>
public static Func<float, float> QuantileFunc(this IEnumerable<float> data)
{
float[] array = data.ToArray();
Array.Sort(array);
return tau => SortedArrayStatistics.Quantile(array, tau);
}
@ -705,7 +786,7 @@ namespace MathNet.Numerics.Statistics
/// <param name="data">The data sample sequence.</param>
public static Func<double, double> QuantileFunc(this IEnumerable<double?> 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
/// <param name="definition">Quantile definition, to choose what product/definition it should be consistent with</param>
public static double QuantileCustom(this IEnumerable<double> data, double tau, QuantileDefinition definition)
{
var array = data.ToArray();
double[] array = data.ToArray();
return ArrayStatistics.QuantileCustomInplace(array, tau, definition);
}
/// <summary>
/// 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.
/// </summary>
/// <param name="data">The data sample sequence.</param>
/// <param name="tau">Quantile selector, between 0.0 and 1.0 (inclusive).</param>
/// <param name="definition">Quantile definition, to choose what product/definition it should be consistent with</param>
public static float QuantileCustom(this IEnumerable<float> data, double tau, QuantileDefinition definition)
{
float[] array = data.ToArray();
return ArrayStatistics.QuantileCustomInplace(array, tau, definition);
}
@ -736,7 +832,7 @@ namespace MathNet.Numerics.Statistics
/// <param name="definition">Quantile definition, to choose what product/definition it should be consistent with</param>
public static double QuantileCustom(this IEnumerable<double?> 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
/// <param name="definition">Quantile definition, to choose what product/definition it should be consistent with</param>
public static Func<double, double> QuantileCustomFunc(this IEnumerable<double> data, QuantileDefinition definition)
{
var array = data.ToArray();
double[] array = data.ToArray();
Array.Sort(array);
return tau => SortedArrayStatistics.QuantileCustom(array, tau, definition);
}
/// <summary>
/// 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.
/// </summary>
/// <param name="data">The data sample sequence.</param>
/// <param name="definition">Quantile definition, to choose what product/definition it should be consistent with</param>
public static Func<float, float> QuantileCustomFunc(this IEnumerable<float> 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
/// <param name="definition">Quantile definition, to choose what product/definition it should be consistent with</param>
public static Func<double, double> QuantileCustomFunc(this IEnumerable<double?> 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
/// <param name="p">Percentile selector, between 0 and 100 (inclusive).</param>
public static double Percentile(this IEnumerable<double> data, int p)
{
var array = data.ToArray();
double[] array = data.ToArray();
return ArrayStatistics.PercentileInplace(array, p);
}
/// <summary>
/// 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).
/// </summary>
/// <param name="data">The data sample sequence.</param>
/// <param name="p">Percentile selector, between 0 and 100 (inclusive).</param>
public static float Percentile(this IEnumerable<float> data, int p)
{
float[] array = data.ToArray();
return ArrayStatistics.PercentileInplace(array, p);
}
@ -792,7 +916,7 @@ namespace MathNet.Numerics.Statistics
/// <param name="p">Percentile selector, between 0 and 100 (inclusive).</param>
public static double Percentile(this IEnumerable<double?> 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
/// <param name="data">The data sample sequence.</param>
public static Func<int, double> PercentileFunc(this IEnumerable<double> data)
{
var array = data.ToArray();
double[] array = data.ToArray();
Array.Sort(array);
return p => SortedArrayStatistics.Percentile(array, p);
}
/// <summary>
/// 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).
/// </summary>
/// <param name="data">The data sample sequence.</param>
public static Func<int, float> PercentileFunc(this IEnumerable<float> data)
{
float[] array = data.ToArray();
Array.Sort(array);
return p => SortedArrayStatistics.Percentile(array, p);
}
@ -817,7 +954,7 @@ namespace MathNet.Numerics.Statistics
/// <param name="data">The data sample sequence.</param>
public static Func<int, double> PercentileFunc(this IEnumerable<double?> 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
/// <param name="data">The data sample sequence.</param>
public static double LowerQuartile(this IEnumerable<double> data)
{
var array = data.ToArray();
double[] array = data.ToArray();
return ArrayStatistics.LowerQuartileInplace(array);
}
/// <summary>
/// Estimates the first quartile value from the provided samples.
/// Approximately median-unbiased regardless of the sample distribution (R8).
/// </summary>
/// <param name="data">The data sample sequence.</param>
public static float LowerQuartile(this IEnumerable<float> data)
{
float[] array = data.ToArray();
return ArrayStatistics.LowerQuartileInplace(array);
}
@ -840,7 +988,7 @@ namespace MathNet.Numerics.Statistics
/// <param name="data">The data sample sequence.</param>
public static double LowerQuartile(this IEnumerable<double?> 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
/// <param name="data">The data sample sequence.</param>
public static double UpperQuartile(this IEnumerable<double> data)
{
var array = data.ToArray();
double[] array = data.ToArray();
return ArrayStatistics.UpperQuartileInplace(array);
}
/// <summary>
/// Estimates the third quartile value from the provided samples.
/// Approximately median-unbiased regardless of the sample distribution (R8).
/// </summary>
/// <param name="data">The data sample sequence.</param>
public static float UpperQuartile(this IEnumerable<float> data)
{
float[] array = data.ToArray();
return ArrayStatistics.UpperQuartileInplace(array);
}
@ -862,7 +1021,7 @@ namespace MathNet.Numerics.Statistics
/// <param name="data">The data sample sequence.</param>
public static double UpperQuartile(this IEnumerable<double?> 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
/// <param name="data">The data sample sequence.</param>
public static double InterquartileRange(this IEnumerable<double> data)
{
var array = data.ToArray();
double[] array = data.ToArray();
return ArrayStatistics.InterquartileRangeInplace(array);
}
/// <summary>
/// Estimates the inter-quartile range from the provided samples.
/// Approximately median-unbiased regardless of the sample distribution (R8).
/// </summary>
/// <param name="data">The data sample sequence.</param>
public static float InterquartileRange(this IEnumerable<float> data)
{
float[] array = data.ToArray();
return ArrayStatistics.InterquartileRangeInplace(array);
}
@ -884,7 +1054,7 @@ namespace MathNet.Numerics.Statistics
/// <param name="data">The data sample sequence.</param>
public static double InterquartileRange(this IEnumerable<double?> 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
/// <param name="data">The data sample sequence.</param>
public static double[] FiveNumberSummary(this IEnumerable<double> data)
{
var array = data.ToArray();
double[] array = data.ToArray();
return ArrayStatistics.FiveNumberSummaryInplace(array);
}
/// <summary>
/// Estimates {min, lower-quantile, median, upper-quantile, max} from the provided samples.
/// Approximately median-unbiased regardless of the sample distribution (R8).
/// </summary>
/// <param name="data">The data sample sequence.</param>
public static float[] FiveNumberSummary(this IEnumerable<float> data)
{
float[] array = data.ToArray();
return ArrayStatistics.FiveNumberSummaryInplace(array);
}
@ -906,7 +1087,7 @@ namespace MathNet.Numerics.Statistics
/// <param name="data">The data sample sequence.</param>
public static double[] FiveNumberSummary(this IEnumerable<double?> 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
/// <param name="order">One-based order of the statistic, must be between 1 and N (inclusive).</param>
public static double OrderStatistic(IEnumerable<double> data, int order)
{
var array = data.ToArray();
double[] array = data.ToArray();
return ArrayStatistics.OrderStatisticInplace(array, order);
}
/// <summary>
/// Returns the order statistic (order 1..N) from the provided samples.
/// </summary>
/// <param name="data">The data sample sequence.</param>
/// <param name="order">One-based order of the statistic, must be between 1 and N (inclusive).</param>
public static float OrderStatistic(IEnumerable<float> data, int order)
{
float[] array = data.ToArray();
return ArrayStatistics.OrderStatisticInplace(array, order);
}
@ -927,7 +1119,18 @@ namespace MathNet.Numerics.Statistics
/// <param name="data">The data sample sequence.</param>
public static Func<int, double> OrderStatisticFunc(IEnumerable<double> data)
{
var array = data.ToArray();
double[] array = data.ToArray();
Array.Sort(array);
return order => SortedArrayStatistics.OrderStatistic(array, order);
}
/// <summary>
/// Returns the order statistic (order 1..N) from the provided samples.
/// </summary>
/// <param name="data">The data sample sequence.</param>
public static Func<int, float> OrderStatisticFunc(IEnumerable<float> data)
{
float[] array = data.ToArray();
Array.Sort(array);
return order => SortedArrayStatistics.OrderStatistic(array, order);
}
@ -941,7 +1144,20 @@ namespace MathNet.Numerics.Statistics
/// <param name="definition">Rank definition, to choose how ties should be handled and what product/definition it should be consistent with</param>
public static double[] Ranks(this IEnumerable<double> data, RankDefinition definition = RankDefinition.Default)
{
var array = data.ToArray();
double[] array = data.ToArray();
return ArrayStatistics.RanksInplace(array, definition);
}
/// <summary>
/// Evaluates the rank of each entry of the provided samples.
/// The rank definition can be specified to be compatible
/// with an existing system.
/// </summary>
/// <param name="data">The data sample sequence.</param>
/// <param name="definition">Rank definition, to choose how ties should be handled and what product/definition it should be consistent with</param>
public static double[] Ranks(this IEnumerable<float> data, RankDefinition definition = RankDefinition.Default)
{
float[] array = data.ToArray();
return ArrayStatistics.RanksInplace(array, definition);
}
@ -968,7 +1184,23 @@ namespace MathNet.Numerics.Statistics
/// <param name="definition">Rank definition, to choose how ties should be handled and what product/definition it should be consistent with</param>
public static double QuantileRank(this IEnumerable<double> data, double x, RankDefinition definition = RankDefinition.Default)
{
var array = data.ToArray();
double[] array = data.ToArray();
Array.Sort(array);
return SortedArrayStatistics.QuantileRank(array, x, definition);
}
/// <summary>
/// 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.
/// </summary>
/// <param name="data">The data sample sequence.</param>
/// <param name="x">Quantile value.</param>
/// <param name="definition">Rank definition, to choose how ties should be handled and what product/definition it should be consistent with</param>
public static double QuantileRank(this IEnumerable<float> 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
/// <param name="definition">Rank definition, to choose how ties should be handled and what product/definition it should be consistent with</param>
public static Func<double, double> QuantileRankFunc(this IEnumerable<double> data, RankDefinition definition = RankDefinition.Default)
{
var array = data.ToArray();
double[] array = data.ToArray();
Array.Sort(array);
return x => SortedArrayStatistics.QuantileRank(array, x, definition);
}
/// <summary>
/// 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.
/// </summary>
/// <param name="data">The data sample sequence.</param>
/// <param name="definition">Rank definition, to choose how ties should be handled and what product/definition it should be consistent with</param>
public static Func<float, double> QuantileRankFunc(this IEnumerable<float> 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
/// <param name="x">The value where to estimate the CDF at.</param>
public static double EmpiricalCDF(this IEnumerable<double> data, double x)
{
var array = data.ToArray();
double[] array = data.ToArray();
Array.Sort(array);
return SortedArrayStatistics.EmpiricalCDF(array, x);
}
/// <summary>
/// Estimates the empirical cumulative distribution function (CDF) at x from the provided samples.
/// </summary>
/// <param name="data">The data sample sequence.</param>
/// <param name="x">The value where to estimate the CDF at.</param>
public static double EmpiricalCDF(this IEnumerable<float> data, float x)
{
float[] array = data.ToArray();
Array.Sort(array);
return SortedArrayStatistics.EmpiricalCDF(array, x);
}
@ -1043,7 +1302,18 @@ namespace MathNet.Numerics.Statistics
/// <param name="data">The data sample sequence.</param>
public static Func<double, double> EmpiricalCDFFunc(this IEnumerable<double> data)
{
var array = data.ToArray();
double[] array = data.ToArray();
Array.Sort(array);
return x => SortedArrayStatistics.EmpiricalCDF(array, x);
}
/// <summary>
/// Estimates the empirical cumulative distribution function (CDF) at x from the provided samples.
/// </summary>
/// <param name="data">The data sample sequence.</param>
public static Func<float, double> EmpiricalCDFFunc(this IEnumerable<float> data)
{
float[] array = data.ToArray();
Array.Sort(array);
return x => SortedArrayStatistics.EmpiricalCDF(array, x);
}
@ -1064,7 +1334,18 @@ namespace MathNet.Numerics.Statistics
/// <param name="tau">Quantile selector, between 0.0 and 1.0 (inclusive).</param>
public static double EmpiricalInvCDF(this IEnumerable<double> data, double tau)
{
var array = data.ToArray();
double[] array = data.ToArray();
return ArrayStatistics.QuantileCustomInplace(array, tau, QuantileDefinition.EmpiricalInvCDF);
}
/// <summary>
/// Estimates the empirical inverse CDF at tau from the provided samples.
/// </summary>
/// <param name="data">The data sample sequence.</param>
/// <param name="tau">Quantile selector, between 0.0 and 1.0 (inclusive).</param>
public static float EmpiricalInvCDF(this IEnumerable<float> data, double tau)
{
float[] array = data.ToArray();
return ArrayStatistics.QuantileCustomInplace(array, tau, QuantileDefinition.EmpiricalInvCDF);
}
@ -1084,7 +1365,18 @@ namespace MathNet.Numerics.Statistics
/// <param name="data">The data sample sequence.</param>
public static Func<double, double> EmpiricalInvCDFFunc(this IEnumerable<double> data)
{
var array = data.ToArray();
double[] array = data.ToArray();
Array.Sort(array);
return tau => SortedArrayStatistics.QuantileCustom(array, tau, QuantileDefinition.EmpiricalInvCDF);
}
/// <summary>
/// Estimates the empirical inverse CDF at tau from the provided samples.
/// </summary>
/// <param name="data">The data sample sequence.</param>
public static Func<double, float> EmpiricalInvCDFFunc(this IEnumerable<float> data)
{
float[] array = data.ToArray();
Array.Sort(array);
return tau => SortedArrayStatistics.QuantileCustom(array, tau, QuantileDefinition.EmpiricalInvCDF);
}

Loading…
Cancel
Save