Free cookie consent management tool by TermsFeed Policy Generator

source: branches/StatisticalTesting/HeuristicLab.Analysis.Statistics/3.3/SampleSizeChecker.cs @ 9706

Last change on this file since 9706 was 9706, checked in by ascheibe, 11 years ago

#2031

  • added exponential fitting
  • added logarithmic fitting
  • refactored fitting code
  • updated license headers
File size: 6.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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.Windows.Forms;
26using HeuristicLab.Core.Views;
27using HeuristicLab.Data;
28using HeuristicLab.MainForm;
29using HeuristicLab.Optimization;
30
31namespace HeuristicLab.Analysis.Statistics {
32  [View("Sample Size Checker View")]
33  [Content(typeof(RunCollection), false)]
34  public sealed partial class SampleSizeChecker : ItemView {
35
36    private double[] data;
37
38    public SampleSizeChecker() {
39      InitializeComponent();
40
41      double prec = 0.01;
42      double conf = 0.95;
43      precisionTextBox.Text = prec.ToString();
44      confIntervalTextBox.Text = conf.ToString();
45    }
46
47    public new RunCollection Content {
48      get { return (RunCollection)base.Content; }
49      set { base.Content = value; }
50    }
51
52    public override bool ReadOnly {
53      get { return true; }
54      set { /*not needed because results are always readonly */}
55    }
56
57    protected override void OnContentChanged() {
58      base.OnContentChanged();
59      resultComboBox.Items.Clear();
60
61      if (Content != null) {
62        UpdateResultComboBox();
63      }
64    }
65
66    #region events
67    protected override void RegisterContentEvents() {
68      base.RegisterContentEvents();
69      Content.ItemsAdded += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
70      Content.ItemsRemoved += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
71      Content.CollectionReset += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
72
73      RegisterRunEvents(Content);
74    }
75
76    private void RegisterRunEvents(IEnumerable<IRun> runs) {
77      foreach (IRun run in runs)
78        run.Changed += new EventHandler(run_Changed);
79    }
80
81    protected override void DeregisterContentEvents() {
82      base.DeregisterContentEvents();
83      Content.ItemsAdded -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
84      Content.ItemsRemoved -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
85      Content.CollectionReset -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
86
87      DeregisterRunEvents(Content);
88    }
89
90    private void DeregisterRunEvents(IEnumerable<IRun> runs) {
91      foreach (IRun run in runs)
92        run.Changed -= new EventHandler(run_Changed);
93    }
94
95    private void Content_CollectionReset(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
96      DeregisterRunEvents(e.OldItems);
97      RegisterRunEvents(e.Items);
98      //RebuildInfluenceDataTable();
99    }
100
101    private void Content_ItemsRemoved(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
102      DeregisterRunEvents(e.Items);
103      //RebuildInfluenceDataTable();
104    }
105
106    private void Content_ItemsAdded(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
107      RegisterRunEvents(e.Items);
108      //RebuildInfluenceDataTable();
109    }
110
111    private void run_Changed(object sender, EventArgs e) {
112      if (InvokeRequired)
113        this.Invoke(new EventHandler(run_Changed), sender, e);
114      else {
115        IRun run = (IRun)sender;
116        UpdateRun(run);
117      }
118    }
119    #endregion
120
121    private void UpdateRun(IRun run) {
122      //TODO: hacky di hack... this is baaaadddd
123      RebuildDataTable();
124    }
125
126    private void UpdateResultComboBox() {
127      resultComboBox.Items.Clear();
128      var results = (from run in Content
129                     where run.Visible
130                     from result in run.Results
131                     where result.Value is IntValue || result.Value is DoubleValue
132                     select result.Key).Distinct().ToArray();
133
134      resultComboBox.Items.AddRange(results);
135      if (resultComboBox.Items.Count > 0) resultComboBox.SelectedItem = resultComboBox.Items[0];
136    }
137
138    private void RebuildDataTable() {
139      string resultName = (string)resultComboBox.SelectedItem;
140
141      var runs = Content.Where(x => x.Results.ContainsKey(resultName) && x.Visible);
142      var vals = runs.Select(x => (double)((dynamic)x.Results[resultName]).Value);
143      data = vals.ToArray();
144
145      DoubleMatrix dt = new DoubleMatrix(runs.Count(), 1);
146      dt.ColumnNames = new string[] { resultName };
147
148      int i = 0;
149      foreach (double val in vals) {
150        dt[i++, 0] = val;
151      }
152
153      stringConvertibleMatrixView.Content = dt;
154    }
155
156    private void estimateSampleSizeButton_Click(object sender, EventArgs e) {
157      double precision = double.Parse(precisionTextBox.Text);
158      double confInterval = double.Parse(confIntervalTextBox.Text);
159
160      int nrSamplesReq = SampleSizeDetermination.DetermineSampleSizeByEstimatingMean(data, precision, confInterval);
161      int curNrSamples = data.Count();
162      int diff = curNrSamples - nrSamplesReq;
163
164      IntMatrix m = new IntMatrix(1, 3);
165      m[0, 0] = curNrSamples;
166      m[0, 1] = nrSamplesReq;
167      m[0, 2] = diff;
168      m.ColumnNames = new string[] { "Nr. of current samples", "Minimum nr. of required samples", "Difference" };
169
170      MainFormManager.MainForm.ShowContent(m);
171    }
172
173    private void resultComboBox_SelectedValueChanged(object sender, EventArgs e) {
174      RebuildDataTable();
175    }
176  }
177}
Note: See TracBrowser for help on using the repository browser.