Changeset 13034
- Timestamp:
- 10/19/15 16:17:04 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Common/3.3/EnumerableStatisticExtensions.cs
r13033 r13034 202 202 /// </summary> 203 203 public static double Percentile(this IEnumerable<double> values, double p) { 204 // iterate only once 205 double[] valuesArr = values.ToArray(); 206 int n = valuesArr.Length; 207 if (n == 0) throw new InvalidOperationException("Enumeration contains no elements."); 208 if (n == 1) return values.ElementAt(0); 209 210 if (p.IsAlmost(0.0)) return valuesArr[0]; 211 if (p.IsAlmost(1.0)) return valuesArr[n - 1]; 212 213 double t = p * (n - 1); 214 int index = (int)Math.Floor(t); 215 double percentage = t - index; 216 return valuesArr[index] * (1 - percentage) + valuesArr[index + 1] * percentage; 204 // percentiles are actually quantiles where alpha is constrained to integer percentage values from 1% to 99% 205 return Quantile(values, p); 217 206 } 218 207
Note: See TracChangeset
for help on using the changeset viewer.