|
|
|
@ -45,6 +45,96 @@ namespace MathNet.Numerics.Statistics |
|
|
|
const double Third = 1d / 3d; |
|
|
|
const double Half = 1d / 2d; |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Returns the smallest value from the sorted data array (ascending).
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="data">Sample array, must be sorted ascendingly.</param>
|
|
|
|
public static double Minimum(double[] data) |
|
|
|
{ |
|
|
|
if (data == null || data.Length == 0) return double.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 double Maximum(double[] data) |
|
|
|
{ |
|
|
|
if (data == null || data.Length == 0) return double.NaN; |
|
|
|
return data[data.Length - 1]; |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Estimates the median value from the sorted data array (ascending).
|
|
|
|
/// Applies a linear interpolation, consistent with Quantile and R-8.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="data">Sample array, must be sorted ascendingly.</param>
|
|
|
|
public static double Median(double[] data) |
|
|
|
{ |
|
|
|
return Quantile(data, 0.5d); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Estimates the p-Percentile value from the sorted data array (ascending).
|
|
|
|
/// Applies a linear interpolation, consistent with Quantile and R-8.
|
|
|
|
/// If a non-integer Percentile is needed, use Quantile instead.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="data">Sample array, must be sorted ascendingly.</param>
|
|
|
|
/// <param name="p">Percentile selector, between 0 and 100 (inclusive).</param>
|
|
|
|
public static double Percentile(double[] data, int p) |
|
|
|
{ |
|
|
|
return Quantile(data, p / 100d); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Estimates the first quartile value from the sorted data array (ascending).
|
|
|
|
/// Applies a linear interpolation, consistent with Quantile and R-8.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="data">Sample array, must be sorted ascendingly.</param>
|
|
|
|
public static double LowerQuartile(double[] data) |
|
|
|
{ |
|
|
|
return Quantile(data, 0.25d); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Estimates the third quartile value from the sorted data array (ascending).
|
|
|
|
/// Applies a linear interpolation, consistent with Quantile and R-8.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="data">Sample array, must be sorted ascendingly.</param>
|
|
|
|
public static double UpperQuartile(double[] data) |
|
|
|
{ |
|
|
|
return Quantile(data, 0.75d); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Estimates the inter-quartile range from the sorted data array (ascending).
|
|
|
|
/// Applies a linear interpolation, consistent with Quantile and R-8.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="data">Sample array, must be sorted ascendingly.</param>
|
|
|
|
public static double InterquartileRange(double[] 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).
|
|
|
|
/// Applies a linear interpolation, consistent with Quantile and R-8.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="data">Sample array, must be sorted ascendingly.</param>
|
|
|
|
public static double[] FiveNumberSummary(double[] data) |
|
|
|
{ |
|
|
|
if (data == null || data.Length == 0) return new[] {double.NaN, double.NaN, double.NaN, double.NaN, double.NaN}; |
|
|
|
return new[] {data[0], Quantile(data, 0.25), Quantile(data, 0.50), Quantile(data, 0.75), data[data.Length - 1]}; |
|
|
|
} |
|
|
|
|
|
|
|
/// <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. Applies a linear interpolation, compatible with R-8.
|
|
|
|
/// </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.
|
|
|
|
@ -61,6 +151,11 @@ namespace MathNet.Numerics.Statistics |
|
|
|
return 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 algorithm can be chosen by the compatibility argument.
|
|
|
|
/// </summary>
|
|
|
|
public static double QuantileCompatible(double[] data, double tau, QuantileCompatibility compatibility) |
|
|
|
{ |
|
|
|
if (tau < 0d || tau > 1d || data == null || data.Length == 0) return double.NaN; |
|
|
|
|