Free cookie consent management tool by TermsFeed Policy Generator

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

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

simplified code (#573)

File size: 6.0 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      int len = samples.Length;
85      if (len < 1) throw new ArgumentException("ERROR in SimpleStatisticsCalculator: Sample size is less than 1");
86
87      Array.Sort<double>(samples);
88      double mean = 0.0;
89      double median = ((len % 2 == 0) ? ((samples[len / 2 - 1] + samples[len / 2]) / 2.0) : (samples[len / 2]));
90      double stdDev = 0.0;
91      double sum = 0.0;
92      double min = samples[0];
93      double max = samples[samples.Length - 1];
94      for (int i = 0; i < samples.Length; i++) {
95        sum += samples[i];
96      }
97      mean = sum / (double)len;
98      if (len > 1) {
99        for (int i = 0; i < samples.Length; i++) {
100          stdDev = Math.Pow(mean - samples[i], 2);
101        }
102        stdDev = Math.Sqrt(stdDev / (double)(len - 1));
103      }
104
105      #region output variables
106      WriteVariable(GetVariableInfo("Mean"), mean, scope);
107      WriteVariable(GetVariableInfo("Median"), median, scope);
108      WriteVariable(GetVariableInfo("StdDev"), stdDev, scope);
109      WriteVariable(GetVariableInfo("Sum"), sum, scope);
110      WriteVariable(GetVariableInfo("Minimum"), min, scope);
111      WriteVariable(GetVariableInfo("Maximum"), max, scope);
112      #endregion
113      return null;
114    }
115
116    private void WriteVariable(IVariableInfo info, double value, IScope scope) {
117      if (info.Local) {
118        IVariable var = GetVariable(info.ActualName);
119        if (var == null) AddVariable(new Variable(info.ActualName, new DoubleData(value)));
120        else (var.Value as DoubleData).Data = value;
121      } else {
122        string name = scope.TranslateName(info.FormalName);
123        IVariable var = scope.GetVariable(name);
124        if (var == null) scope.AddVariable(new Variable(name, new DoubleData(value)));
125        else (var.Value as DoubleData).Data = value;
126      }
127    }
128  }
129}
Note: See TracBrowser for help on using the repository browser.