Changeset 14029 for branches/crossvalidation-2434/HeuristicLab.Common
- Timestamp:
- 07/08/16 14:40:02 (8 years ago)
- Location:
- branches/crossvalidation-2434
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/crossvalidation-2434
- Property svn:mergeinfo changed
-
branches/crossvalidation-2434/HeuristicLab.Common/3.3/EnumerableExtensions.cs
r12012 r14029 1 1 #region License Information 2 2 /* HeuristicLab 3 * Copyright (C) 2002-201 5Heuristic and Evolutionary Algorithms Laboratory (HEAL)3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL) 4 4 * 5 5 * This file is part of HeuristicLab. … … 98 98 return sequences.Where(s => s.Any()).Aggregate(result, (current, s) => (from seq in current from item in s select seq.Concat(new[] { item }))); 99 99 } 100 101 /// <summary> 102 /// Compute all k-combinations of elements from the provided collection. 103 /// <param name="elements">The collection of elements</param> 104 /// <param name="k">The combination group size</param> 105 /// <returns>An enumerable sequence of all the possible k-combinations of elements</returns> 106 /// </summary> 107 public static IEnumerable<IEnumerable<T>> Combinations<T>(this IList<T> elements, int k) { 108 if (k > elements.Count) 109 throw new ArgumentException(); 110 111 if (k == 1) { 112 foreach (var element in elements) 113 yield return new[] { element }; 114 yield break; 115 } 116 117 int n = elements.Count; 118 var range = Enumerable.Range(0, k).ToArray(); 119 var length = BinomialCoefficient(n, k); 120 121 for (int i = 0; i < length; ++i) { 122 yield return range.Select(x => elements[x]); 123 124 if (i == length - 1) break; 125 var m = k - 1; 126 var max = n - 1; 127 128 while (range[m] == max) { --m; --max; } 129 range[m]++; 130 for (int j = m + 1; j < k; ++j) { 131 range[j] = range[j - 1] + 1; 132 } 133 } 134 } 135 136 /// <summary> 137 /// This function gets the total number of unique combinations based upon N and K, 138 /// where N is the total number of items and K is the size of the group. 139 /// It calculates the total number of unique combinations C(N, K) = N! / ( K! (N - K)! ) 140 /// using the recursion C(N+1, K+1) = (N+1 / K+1) * C(N, K). 141 /// <remarks>http://blog.plover.com/math/choose.html</remarks> 142 /// <remark>https://en.wikipedia.org/wiki/Binomial_coefficient#Multiplicative_formula</remark> 143 /// <param name="n">The number of elements</param> 144 /// <param name="k">The size of the group</param> 145 /// <returns>The binomial coefficient C(N, K)</returns> 146 /// </summary> 147 public static long BinomialCoefficient(long n, long k) { 148 if (k > n) return 0; 149 if (k == n) return 1; 150 if (k > n - k) 151 k = n - k; 152 long r = 1; 153 for (long d = 1; d <= k; d++) { 154 r *= n--; 155 r /= d; 156 } 157 return r; 158 } 100 159 } 101 160 } -
branches/crossvalidation-2434/HeuristicLab.Common/3.3/EnumerableStatisticExtensions.cs
r12012 r14029 22 22 using System; 23 23 using System.Collections.Generic; 24 using System.Diagnostics.Contracts; 24 25 using System.Linq; 25 26 … … 32 33 /// <returns></returns> 33 34 public static double Median(this IEnumerable<double> values) { 34 // iterate only once 35 // See unit tests for comparison with naive implementation 36 return Quantile(values, 0.5); 37 } 38 39 /// <summary> 40 /// Calculates the alpha-quantile element of the enumeration. 41 /// </summary> 42 /// <param name="values"></param> 43 /// <returns></returns> 44 public static double Quantile(this IEnumerable<double> values, double alpha) { 45 // See unit tests for comparison with naive implementation 35 46 double[] valuesArr = values.ToArray(); 36 47 int n = valuesArr.Length; 37 48 if (n == 0) throw new InvalidOperationException("Enumeration contains no elements."); 38 49 39 Array.Sort(valuesArr); 40 41 // return the middle element (if n is uneven) or the average of the two middle elements if n is even. 42 if (n % 2 == 1) { 43 return valuesArr[n / 2]; 50 // "When N is even, statistics books define the median as the arithmetic mean of the elements k = N/2 51 // and k = N/2 + 1 (that is, N/2 from the bottom and N/2 from the top). 52 // If you accept such pedantry, you must perform two separate selections to find these elements." 53 54 // return the element at Math.Ceiling (if n*alpha is fractional) or the average of two elements if n*alpha is integer. 55 var pos = n * alpha; 56 Contract.Assert(pos >= 0); 57 Contract.Assert(pos < n); 58 bool isInteger = Math.Round(pos).IsAlmost(pos); 59 if (isInteger) { 60 return 0.5 * (Select((int)pos - 1, valuesArr) + Select((int)pos, valuesArr)); 44 61 } else { 45 return (valuesArr[(n / 2) - 1] + valuesArr[n / 2]) / 2.0; 62 return Select((int)Math.Ceiling(pos) - 1, valuesArr); 63 } 64 } 65 66 // Numerical Recipes in C++, §8.5 Selecting the Mth Largest, O(n) 67 // Given k in [0..n-1] returns an array value from array arr[0..n-1] such that k array values are 68 // less than or equal to the one returned. The input array will be rearranged to have this value in 69 // location arr[k], with all smaller elements moved to arr[0..k-1] (in arbitrary order) and all 70 // larger elements in arr[k+1..n-1] (also in arbitrary order). 71 // 72 // Could be changed to Select<T> where T is IComparable but in this case is significantly slower for double values 73 private static double Select(int k, double[] arr) { 74 Contract.Assert(arr.GetLowerBound(0) == 0); 75 Contract.Assert(k >= 0 && k < arr.Length); 76 int i, ir, j, l, mid, n = arr.Length; 77 double a; 78 l = 0; 79 ir = n - 1; 80 for (; ; ) { 81 if (ir <= l + 1) { 82 // Active partition contains 1 or 2 elements. 83 if (ir == l + 1 && arr[ir] < arr[l]) { 84 // if (ir == l + 1 && arr[ir].CompareTo(arr[l]) < 0) { 85 // Case of 2 elements. 86 // SWAP(arr[l], arr[ir]); 87 double temp = arr[l]; 88 arr[l] = arr[ir]; 89 arr[ir] = temp; 90 } 91 return arr[k]; 92 } else { 93 mid = (l + ir) >> 1; // Choose median of left, center, and right elements 94 { 95 // SWAP(arr[mid], arr[l + 1]); // as partitioning element a. Also 96 double temp = arr[mid]; 97 arr[mid] = arr[l + 1]; 98 arr[l + 1] = temp; 99 } 100 101 if (arr[l] > arr[ir]) { 102 // if (arr[l].CompareTo(arr[ir]) > 0) { // rearrange so that arr[l] arr[ir] <= arr[l+1], 103 // SWAP(arr[l], arr[ir]); . arr[ir] >= arr[l+1] 104 double temp = arr[l]; 105 arr[l] = arr[ir]; 106 arr[ir] = temp; 107 } 108 109 if (arr[l + 1] > arr[ir]) { 110 // if (arr[l + 1].CompareTo(arr[ir]) > 0) { 111 // SWAP(arr[l + 1], arr[ir]); 112 double temp = arr[l + 1]; 113 arr[l + 1] = arr[ir]; 114 arr[ir] = temp; 115 } 116 if (arr[l] > arr[l + 1]) { 117 //if (arr[l].CompareTo(arr[l + 1]) > 0) { 118 // SWAP(arr[l], arr[l + 1]); 119 double temp = arr[l]; 120 arr[l] = arr[l + 1]; 121 arr[l + 1] = temp; 122 123 } 124 i = l + 1; // Initialize pointers for partitioning. 125 j = ir; 126 a = arr[l + 1]; // Partitioning element. 127 for (; ; ) { // Beginning of innermost loop. 128 do i++; while (arr[i] < a /* arr[i].CompareTo(a) < 0 */); // Scan up to find element > a. 129 do j--; while (arr[j] > a /* arr[j].CompareTo(a) > 0 */); // Scan down to find element < a. 130 if (j < i) break; // Pointers crossed. Partitioning complete. 131 { 132 // SWAP(arr[i], arr[j]); 133 double temp = arr[i]; 134 arr[i] = arr[j]; 135 arr[j] = temp; 136 } 137 } // End of innermost loop. 138 arr[l + 1] = arr[j]; // Insert partitioning element. 139 arr[j] = a; 140 if (j >= k) ir = j - 1; // Keep active the partition that contains the 141 if (j <= k) l = i; // kth element. 142 } 46 143 } 47 144 } … … 67 164 68 165 /// <summary> 69 /// Calculates the s tandard deviation of values.166 /// Calculates the sample standard deviation of values. 70 167 /// </summary> 71 168 /// <param name="values"></param> … … 76 173 77 174 /// <summary> 78 /// Calculates the variance of values. (sum (x - x_mean)² / n) 175 /// Calculates the population standard deviation of values. 176 /// </summary> 177 /// <param name="values"></param> 178 /// <returns></returns> 179 public static double StandardDeviationPop(this IEnumerable<double> values) { 180 return Math.Sqrt(VariancePop(values)); 181 } 182 183 /// <summary> 184 /// Calculates the sample variance of values. (sum (x - x_mean)² / (n-1)) 79 185 /// </summary> 80 186 /// <param name="values"></param> 81 187 /// <returns></returns> 82 188 public static double Variance(this IEnumerable<double> values) { 189 return Variance(values, true); 190 } 191 192 /// <summary> 193 /// Calculates the population variance of values. (sum (x - x_mean)² / n) 194 /// </summary> 195 /// <param name="values"></param> 196 /// <returns></returns> 197 public static double VariancePop(this IEnumerable<double> values) { 198 return Variance(values, false); 199 } 200 201 private static double Variance(IEnumerable<double> values, bool sampleVariance) { 83 202 int m_n = 0; 84 203 double m_oldM = 0.0; … … 100 219 } 101 220 } 102 return ((m_n > 1) ? m_newS / (m_n - 1) : 0.0); 103 } 104 105 /// <summary> 106 /// Calculates the pth percentile of the values. 107 /// </summary 108 public static double Percentile(this IEnumerable<double> values, double p) { 109 // iterate only once 110 double[] valuesArr = values.ToArray(); 111 int n = valuesArr.Length; 112 if (n == 0) throw new InvalidOperationException("Enumeration contains no elements."); 113 if (n == 1) return values.ElementAt(0); 114 115 if (p.IsAlmost(0.0)) return valuesArr[0]; 116 if (p.IsAlmost(1.0)) return valuesArr[n - 1]; 117 118 double t = p * (n - 1); 119 int index = (int)Math.Floor(t); 120 double percentage = t - index; 121 return valuesArr[index] * (1 - percentage) + valuesArr[index + 1] * percentage; 221 222 if (m_n == 0) return double.NaN; 223 if (m_n == 1) return 0.0; 224 225 if (sampleVariance) return m_newS / (m_n - 1); 226 else return m_newS / m_n; 122 227 } 123 228 -
branches/crossvalidation-2434/HeuristicLab.Common/3.3/ObjectExtensions.cs
r12012 r14029 63 63 /// * System.Delegate 64 64 /// * System.Reflection.Pointer 65 /// * System.RuntimeType 65 66 /// * Primitives (Boolean, Byte, SByte, Int16, UInt16, Int32, UInt32, Int64, UInt64, IntPtr, UIntPtr, Char, Double, Single) 66 67 /// * string, decimal, DateTime 67 68 /// * Arrays of types not collected 69 /// * All types from System.Reflection.Emit 68 70 /// </summary> 69 71 private static bool ExcludeType(Type type) { … … 77 79 typeof(Delegate).IsAssignableFrom(type) || 78 80 typeof(Pointer).IsAssignableFrom(type) || 79 type == typeof(System.Reflection.Emit.SignatureHelper) || 81 type.Namespace == "System.Reflection.Emit" || 82 type.Assembly.GetName().Name == "System.Runtime.Serialization" || 83 typeof(TypeInfo).IsAssignableFrom(type) || 80 84 (type.HasElementType && ExcludeType(type.GetElementType())); 81 85 } -
branches/crossvalidation-2434/HeuristicLab.Common/3.3/Plugin.cs.frame
r12753 r14029 29 29 /// Plugin class for HeuristicLab.Common plugin. 30 30 /// </summary> 31 [Plugin("HeuristicLab.Common", "3.3.1 2.$WCREV$")]31 [Plugin("HeuristicLab.Common", "3.3.13.$WCREV$")] 32 32 [PluginFile("HeuristicLab.Common-3.3.dll", PluginFileType.Assembly)] 33 33 public class HeuristicLabCommonPlugin : PluginBase { -
branches/crossvalidation-2434/HeuristicLab.Common/3.3/Properties/AssemblyInfo.cs.frame
r12753 r14029 54 54 // by using the '*' as shown below: 55 55 [assembly: AssemblyVersion("3.3.0.0")] 56 [assembly: AssemblyFileVersion("3.3.1 2.$WCREV$")]56 [assembly: AssemblyFileVersion("3.3.13.$WCREV$")]
Note: See TracChangeset
for help on using the changeset viewer.