Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.StatisticalAnalysis/3.2/SimpleStatisticsCalculator.cs @ 1745

Last change on this file since 1745 was 1745, checked in by abeham, 15 years ago

Added simple statistics calculator for calculating mean, median, stdDev, min, max, and sum (#573)

File size: 8.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2009 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.Linq;
25using System.Text;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28
29namespace HeuristicLab.StatisticalAnalysis {
30  public class SimpleStatisticsCalculator : OperatorBase {
31
32    public override string Description {
33      get { return @"Takes a DoubleArrayData, IntArrayData, ItemList (containing IntData or DoubleData), ItemList<IntData>, or ItemList<DoubleData> and calculates mean, median, standard deviation, sum, minimum, and maximum."; }
34    }
35
36    public SimpleStatisticsCalculator()
37      : base() {
38      AddVariableInfo(new VariableInfo("Samples", "The array or ItemList containing the samples", typeof(IItem), VariableKind.In));
39      AddVariableInfo(new VariableInfo("Mean", "The mean of the samples", typeof(DoubleData), VariableKind.New | VariableKind.Out));
40      AddVariableInfo(new VariableInfo("Median", "The median of the samples", typeof(DoubleData), VariableKind.New | VariableKind.Out));
41      AddVariableInfo(new VariableInfo("StdDev", "The standard deviation of the samples", typeof(DoubleData), VariableKind.New | VariableKind.Out));
42      AddVariableInfo(new VariableInfo("Sum", "The sum of the samples", typeof(DoubleData), VariableKind.New | VariableKind.Out));
43      AddVariableInfo(new VariableInfo("Minimum", "The smallest of the samples", typeof(DoubleData), VariableKind.New | VariableKind.Out));
44      AddVariableInfo(new VariableInfo("Maximum", "The largest of the samples", typeof(DoubleData), VariableKind.New | VariableKind.Out));
45    }
46
47    public override IOperation Apply(IScope scope) {
48      IItem data = GetVariableValue<IItem>("Samples", scope, false);
49      double[] samples; // put the samples into a double array
50      #region fill samples with data
51      // find out which type it actually is
52      Type t = data.GetType();
53      if (t.Equals(typeof(DoubleArrayData))) {
54        DoubleArrayData dAD = (DoubleArrayData)data;
55        samples = new double[dAD.Data.Length];
56        for (int i = 0; i < samples.Length; i++)
57          samples[i] = dAD.Data[i];
58      } else if (t.Equals(typeof(IntArrayData))) {
59        IntArrayData iAD = (IntArrayData)data;
60        samples = new double[iAD.Data.Length];
61        for (int i = 0; i < samples.Length; i++)
62          samples[i] = (double)iAD.Data[i];
63      } else if (t.Equals(typeof(ItemList<DoubleData>))) {
64        ItemList<DoubleData> iLDD = (ItemList<DoubleData>)data;
65        samples = new double[iLDD.Count];
66        for (int i = 0; i < samples.Length; i++)
67          samples[i] = iLDD[i].Data;
68      } else if (t.Equals(typeof(ItemList<IntData>))) {
69        ItemList<IntData> iLID = (ItemList<IntData>)data;
70        samples = new double[iLID.Count];
71        for (int i = 0; i < samples.Length; i++)
72          samples[i] = iLID[i].Data;
73      } else if (t.Equals(typeof(ItemList))) {
74        ItemList iL = (ItemList)data;
75        samples = new double[iL.Count];
76        for (int i = 0; i < samples.Length; i++) {
77          if (iL[i] is DoubleData) samples[i] = ((DoubleData)(iL[i])).Data;
78          else if (iL[i] is IntData) samples[i] = (double)((IntData)(iL[i])).Data;
79          else throw new ArgumentException("ERROR in SimpleStatisticsCalculator: The ItemList does not contain DoubleData or IntData");
80        }
81      } else throw new ArgumentException("ERROR in SimpleStatisticsCalculator: Samples are not in a recognized data format");
82      #endregion
83
84      Array.Sort<double>(samples);
85      int len = samples.Length;
86      if (len <= 1) throw new ArgumentException("ERROR in SimpleStatisticsCalculator: Sample size is less or equal than 1");
87      double mean = 0.0;
88      double median = ((len % 2 == 0) ? ((samples[len / 2 - 1] + samples[len / 2]) / 2.0) : (samples[len / 2]));
89      double stdDev = 0.0;
90      double sum = 0.0;
91      double min = samples[0];
92      double max = samples[samples.Length - 1];
93      for (int i = 0; i < samples.Length; i++) {
94        sum += samples[i];
95      }
96      mean = sum / (double)len;
97      for (int i = 0; i < samples.Length; i++) {
98        stdDev = Math.Pow(mean - samples[i], 2);
99      }
100      stdDev = Math.Sqrt(stdDev / (double)(len - 1));
101
102      #region output variables
103      if (GetVariableInfo("Mean").Local) {
104        IVariable var = GetVariable(GetVariableInfo("Mean").ActualName);
105        if (var == null) AddVariable(new Variable(GetVariableInfo("Mean").ActualName, new DoubleData(mean)));
106        else (var.Value as DoubleData).Data = mean;
107      } else {
108        string name = scope.TranslateName("Mean");
109        IVariable var = scope.GetVariable(name);
110        if (var == null) scope.AddVariable(new Variable(name, new DoubleData(mean)));
111        else (var.Value as DoubleData).Data = mean;
112      }
113      if (GetVariableInfo("Median").Local) {
114        IVariable var = GetVariable(GetVariableInfo("Median").ActualName);
115        if (var == null) AddVariable(new Variable(GetVariableInfo("Median").ActualName, new DoubleData(median)));
116        else (var.Value as DoubleData).Data = median;
117      } else {
118        string name = scope.TranslateName("Median");
119        IVariable var = scope.GetVariable(name);
120        if (var == null) scope.AddVariable(new Variable(name, new DoubleData(median)));
121        else (var.Value as DoubleData).Data = median;
122      }
123      if (GetVariableInfo("StdDev").Local) {
124        IVariable var = GetVariable(GetVariableInfo("StdDev").ActualName);
125        if (var == null) AddVariable(new Variable(GetVariableInfo("StdDev").ActualName, new DoubleData(stdDev)));
126        else (var.Value as DoubleData).Data = stdDev;
127      } else {
128        string name = scope.TranslateName("StdDev");
129        IVariable var = scope.GetVariable(name);
130        if (var == null) scope.AddVariable(new Variable(name, new DoubleData(stdDev)));
131        else (var.Value as DoubleData).Data = stdDev;
132      }
133      if (GetVariableInfo("Sum").Local) {
134        IVariable var = GetVariable(GetVariableInfo("Sum").ActualName);
135        if (var == null) AddVariable(new Variable(GetVariableInfo("Sum").ActualName, new DoubleData(sum)));
136        else (var.Value as DoubleData).Data = sum;
137      } else {
138        string name = scope.TranslateName("Sum");
139        IVariable var = scope.GetVariable(name);
140        if (var == null) scope.AddVariable(new Variable(name, new DoubleData(sum)));
141        else (var.Value as DoubleData).Data = sum;
142      }
143      if (GetVariableInfo("Minimum").Local) {
144        IVariable var = GetVariable(GetVariableInfo("Minimum").ActualName);
145        if (var == null) AddVariable(new Variable(GetVariableInfo("Minimum").ActualName, new DoubleData(min)));
146        else (var.Value as DoubleData).Data = min;
147      } else {
148        string name = scope.TranslateName("Minimum");
149        IVariable var = scope.GetVariable(name);
150        if (var == null) scope.AddVariable(new Variable(name, new DoubleData(min)));
151        else (var.Value as DoubleData).Data = min;
152      }
153      if (GetVariableInfo("Maximum").Local) {
154        IVariable var = GetVariable(GetVariableInfo("Maximum").ActualName);
155        if (var == null) AddVariable(new Variable(GetVariableInfo("Maximum").ActualName, new DoubleData(max)));
156        else (var.Value as DoubleData).Data = max;
157      } else {
158        string name = scope.TranslateName("Maximum");
159        IVariable var = scope.GetVariable(name);
160        if (var == null) scope.AddVariable(new Variable(name, new DoubleData(max)));
161        else (var.Value as DoubleData).Data = max;
162      }
163      #endregion
164      return null;
165    }
166  }
167}
Note: See TracBrowser for help on using the repository browser.