Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Persistence Test/HeuristicLab.DataAnalysis/3.3/Statistics.cs @ 2491

Last change on this file since 2491 was 1788, checked in by gkronber, 15 years ago

fixed another bug in the calculation of the range of a variable. #615 (Evaluation of HL3 function trees should be equivalent to evaluation in HL2)

File size: 7.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Text;
25
26namespace HeuristicLab.DataAnalysis {
27  public class Statistics {
28
29    /// <summary>
30    /// Minimum returns the smalles entry of values.
31    /// Throws and exception if values is empty.
32    /// </summary>
33    /// <typeparam name="T"></typeparam>
34    /// <param name="values"></param>
35    /// <returns></returns>
36    public static T Minimum<T>(IEnumerable<T> values) where T : struct, IComparable, IComparable<T> {
37      IEnumerator<T> enumerator = values.GetEnumerator();
38
39      // this will throw an exception if the values collection is empty
40      enumerator.MoveNext();
41      T minimum = enumerator.Current;
42
43      while (enumerator.MoveNext()) {
44        T current = enumerator.Current;
45        if (current.CompareTo(minimum) < 0) {
46          minimum = current;
47        }
48      }
49
50      return minimum;
51    }
52
53    /// <summary>
54    /// Maximum returns the largest entry of values.
55    /// Throws an exception if values is empty.
56    /// </summary>
57    /// <typeparam name="T"></typeparam>
58    /// <param name="values"></param>
59    /// <returns></returns>
60    public static T Maximum<T>(IEnumerable<T> values) where T : struct, IComparable, IComparable<T> {
61      IEnumerator<T> enumerator = values.GetEnumerator();
62
63      // this will throw an exception if the values collection is empty
64      enumerator.MoveNext();
65      T maximum = enumerator.Current;
66
67      while (enumerator.MoveNext()) {
68        T current = enumerator.Current;
69        if (current.CompareTo(maximum) > 0) {
70          maximum = current;
71        }
72      }
73
74      return maximum;
75    }
76
77    /// <summary>
78    /// Range calculates the difference between the larges and smallest entry of values.
79    /// </summary>
80    /// <param name="values"></param>
81    /// <returns></returns>
82    public static double Range(double[] values) {
83      return Range(values, 0, values.Length);
84    }
85
86    /// <summary>
87    /// Range calculates the difference between the larges and smallest entry of values.
88    /// </summary>
89    public static double Range(List<double> values) {
90      return Range(values.ToArray(), 0, values.Count);
91    }
92
93    /// <summary>
94    /// Range calculates the difference between the largest and smallest entry of values between start and end.
95    /// </summary>
96    /// <param name="values">collection of values</param>
97    /// <param name="start">start index (inclusive)</param>
98    /// <param name="end">end index (exclusive)</param>
99    /// <returns></returns>
100    public static double Range(double[] values, int start, int end) {
101      if (start < 0 || start > values.Length || end < 0 || end > values.Length || start > end) {
102        throw new InvalidOperationException();
103      }
104
105      double minimum = double.PositiveInfinity;
106      double maximum = double.NegativeInfinity;
107      for (int i = start; i < end; i++) {
108        if (!double.IsNaN(values[i])) {
109          if (values[i] > maximum) {
110            maximum = values[i];
111          }
112          if (values[i] < minimum) {
113            minimum = values[i];
114          }
115        }
116      }
117      return (maximum - minimum);
118    }
119
120    /// <summary>
121    /// calculates the sum of all values.
122    /// </summary>
123    /// <param name="values"></param>
124    /// <returns></returns>
125    public static double Sum(double[] values) {
126      int n = values.Length;
127      double sum = 0.0;
128      for (int i = 0; i < n; i++) {
129        if (double.IsNaN(values[i])) {
130          throw new NotFiniteNumberException();
131        } else {
132          sum += values[i];
133        }
134      }
135      return sum;
136    }
137
138    /// <summary>
139    /// Calculates the mean of all values.
140    /// </summary>
141    /// <param name="values"></param>
142    /// <returns></returns>
143    public static double Mean(List<double> values) {
144      return Mean(values.ToArray(), 0, values.Count);
145    }
146
147    // Calculates the mean of all values.
148    public static double Mean(double[] values) {
149      return Mean(values, 0, values.Length);
150    }
151
152    /// <summary>
153    /// Calculates the mean of the values between start and end.
154    /// </summary>
155    /// <param name="values"></param>
156    /// <param name="start">start index (inclusive)</param>
157    /// <param name="end">end index(exclusive)</param>
158    /// <returns></returns>
159    public static double Mean(double[] values, int start, int end) {
160      if (values.Length == 0) throw new InvalidOperationException();
161      double sum = 0.0;
162      int n = 0;
163      for (int i = start; i < end; i++) {
164        if (!double.IsNaN(values[i])) {
165          sum += values[i];
166          n++;
167        }
168      }
169      if (n == 0) throw new InvalidOperationException();
170      return sum / n;
171    }
172
173    /// <summary>
174    /// Calculates the median of the values.
175    /// </summary>
176    /// <param name="values"></param>
177    /// <returns></returns>
178    public static double Median(double[] values) {
179      if (values.Length == 0) throw new InvalidOperationException();
180      int n = values.Length;
181      if (n == 0)
182        return 0;
183
184      double[] sortedValues = new double[n];
185
186      Array.Copy(values, sortedValues, n);
187      Array.Sort(sortedValues);
188
189      // return the middle element (if n is uneven) or the average of the two middle elements if n is even.
190      if (n % 2 == 1) {
191        return sortedValues[n / 2];
192      } else {
193        return (sortedValues[n / 2] + sortedValues[n / 2 + 1]) / 2.0;
194      }
195    }
196
197
198    /// <summary>
199    /// Calculates the standard deviation of values.
200    /// </summary>
201    /// <param name="values"></param>
202    /// <returns></returns>
203    public static double StandardDeviation(double[] values) {
204      return Math.Sqrt(Variance(values));
205    }
206
207    /// <summary>
208    /// Calculates the variance of values.
209    /// </summary>
210    /// <param name="values"></param>
211    /// <returns></returns>
212    public static double Variance(double[] values) {
213      return Variance(values, 0, values.Length);
214    }
215
216
217    /// <summary>
218    /// Calculates the variance of the entries of values between start and end.
219    /// </summary>
220    /// <param name="values"></param>
221    /// <param name="start">start index (inclusive)</param>
222    /// <param name="end">end index (exclusive)</param>
223    /// <returns></returns>
224    public static double Variance(double[] values, int start, int end) {
225      if (end - start == 1)
226        return 0.0;
227
228      double mean = Mean(values, start, end);
229      double squaredErrorsSum = 0.0;
230
231      int n = 0;
232      for (int i = start; i < end; i++) {
233        if (!double.IsNaN(values[i])) {
234          double d = values[i] - mean;
235          squaredErrorsSum += d * d;
236          n++;
237        }
238      }
239      if (n < 2) {
240        throw new InvalidOperationException();
241      }
242      return squaredErrorsSum / (n - 1);
243    }
244  }
245}
Note: See TracBrowser for help on using the repository browser.