Index: /trunk/sources/HeuristicLab.Common/3.3/EnumerableStatisticExtensions.cs
===================================================================
--- /trunk/sources/HeuristicLab.Common/3.3/EnumerableStatisticExtensions.cs (revision 13024)
+++ /trunk/sources/HeuristicLab.Common/3.3/EnumerableStatisticExtensions.cs (revision 13025)
@@ -22,4 +22,5 @@
using System;
using System.Collections.Generic;
+using System.Diagnostics.Contracts;
using System.Linq;
@@ -44,4 +45,31 @@
} else {
return (valuesArr[(n / 2) - 1] + valuesArr[n / 2]) / 2.0;
+ }
+ }
+
+ ///
+ /// Calculates the alpha-quantile element of the enumeration.
+ ///
+ ///
+ ///
+ public static double Quantile(this IEnumerable values, double alpha) {
+ Contract.Assert(alpha > 0 && alpha < 1);
+ // iterate only once
+ double[] valuesArr = values.ToArray();
+ int n = valuesArr.Length;
+ if (n == 0) throw new InvalidOperationException("Enumeration contains no elements.");
+
+ Array.Sort(valuesArr);
+ // starts at 0
+
+ // return the element at Math.Ceiling (if n*alpha is fractional) or the average of two elements if n*alpha is integer.
+ var pos = n * alpha;
+ Contract.Assert(pos >= 0);
+ Contract.Assert(pos < n);
+ bool isInteger = Math.Round(pos).IsAlmost(pos);
+ if (isInteger) {
+ return 0.5 * (valuesArr[(int)pos - 1] + valuesArr[(int)pos]);
+ } else {
+ return valuesArr[(int)Math.Ceiling(pos) - 1];
}
}
Index: /trunk/sources/HeuristicLab.Tests/HeuristicLab.Common-3.3/EnumerableStatisticExtensions.cs
===================================================================
--- /trunk/sources/HeuristicLab.Tests/HeuristicLab.Common-3.3/EnumerableStatisticExtensions.cs (revision 13025)
+++ /trunk/sources/HeuristicLab.Tests/HeuristicLab.Common-3.3/EnumerableStatisticExtensions.cs (revision 13025)
@@ -0,0 +1,53 @@
+#region License Information
+/* HeuristicLab
+ * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
+ *
+ * This file is part of HeuristicLab.
+ *
+ * HeuristicLab is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * HeuristicLab is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with HeuristicLab. If not, see .
+ */
+#endregion
+
+using System.Linq;
+using HeuristicLab.Common;
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+
+namespace HeuristicLab.Tests {
+ [TestClass]
+ public class EnumerableStatisticExtensionsTest {
+ [TestMethod]
+ [TestCategory("General")]
+ [TestProperty("Time", "short")]
+ public void QuantileTest() {
+ var xs = new double[] { 1, 1, 1, 3, 4, 7, 9, 11, 13, 13 };
+ {
+ var q = xs.Quantile(0.3);
+ Assert.AreEqual(q, 2.0, 1E-6);
+ }
+ {
+ var q = xs.Quantile(0.75);
+ Assert.AreEqual(q, 11.0, 1E-6);
+ }
+ // quantile = 0.5 is equivalent to median
+ {
+ // even number of elements
+ Assert.AreEqual(xs.Quantile(0.5), xs.Median(), 1E-6);
+ }
+ {
+ // odd number of elements
+ Assert.AreEqual(xs.Take(9).Quantile(0.5), xs.Take(9).Median(), 1E-6);
+ }
+ }
+ }
+}
Index: /trunk/sources/HeuristicLab.Tests/HeuristicLab.Tests.csproj
===================================================================
--- /trunk/sources/HeuristicLab.Tests/HeuristicLab.Tests.csproj (revision 13024)
+++ /trunk/sources/HeuristicLab.Tests/HeuristicLab.Tests.csproj (revision 13025)
@@ -468,4 +468,5 @@
+
@@ -536,6 +537,4 @@
-
-
@@ -629,5 +628,4 @@
-