Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceOverhaul/HeuristicLab.Analysis/3.3/Statistics/SampleSizeDetermination.cs @ 13783

Last change on this file since 13783 was 12012, checked in by ascheibe, 10 years ago

#2212 merged r12008, r12009, r12010 back into trunk

File size: 3.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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.Linq;
24using HeuristicLab.Common;
25
26namespace HeuristicLab.Analysis.Statistics {
27  public static class SampleSizeDetermination {
28    /// <summary>
29    /// Determines for a given sample the required sample size as described in
30    /// Göran Kauermann, Helmut Küchenhoff: Stichproben: Methoden und praktische Umsetzung mit R, section 2.27.
31    /// </summary>
32    /// <param name="samples">The pilot sample.</param>
33    /// <param name="conf">Confidence Interval.</param>
34    /// <returns>Number of required samples for the given confidence interval. </returns>
35    public static int DetermineSampleSizeByEstimatingMean(double[] samples, double conf = 0.95) {
36      if (conf < 0.0 || conf > 1.0) throw new ArgumentException("The confidence interval must be between zero and one.");
37
38      var confInterval = samples.ConfidenceIntervals(conf);
39      double e = (confInterval.Item2 - confInterval.Item1) / 2;
40      double s = samples.StandardDeviation();
41      double z = alglib.invnormaldistribution((conf + 1) / 2);
42      double n = samples.Count();
43
44      double result = Math.Pow(s, 2) / ((Math.Pow(e, 2) / Math.Pow(z, 2)) + (Math.Pow(s, 2) / n));
45
46      result = Math.Ceiling(result);
47      if (result > int.MaxValue)
48        return int.MaxValue;
49      else
50        return (int)result;
51    }
52
53    /// <summary>
54    /// Calculates Cohen's d.
55    /// </summary>
56    /// <returns>Cohen's d.
57    /// d = 0.2 means small effect
58    /// d = 0.5 means medium effect
59    /// d = 0.8 means big effect
60    /// According to Wikipedia this means: "A lower Cohen's d indicates a necessity of larger sample sizes, and vice versa."
61    /// </returns>
62    public static double CalculateCohensD(double[] d1, double[] d2) {
63      double x1, x2, s1, s2;
64
65      x1 = d1.Average();
66      x2 = d2.Average();
67      s1 = d1.Variance();
68      s2 = d2.Variance();
69
70      return Math.Abs(x1 - x2) / Math.Sqrt((s1 + s2) / 2);
71    }
72
73    /// <summary>
74    /// Calculates Hedges' g.
75    /// Hedges' g works like Cohen's d but corrects for bias.
76    /// </summary>
77    /// <returns>Hedges' g</returns>
78    public static double CalculateHedgesG(double[] d1, double[] d2) {
79      double x1, x2, s1, s2, n1, n2, s, g, c;
80
81      x1 = d1.Average();
82      x2 = d2.Average();
83      s1 = d1.Variance();
84      s2 = d2.Variance();
85      n1 = d1.Count();
86      n2 = d2.Count();
87
88      s = Math.Sqrt(((n1 - 1) * s1 + (n2 - 1) * s2) / (n1 + n2 - 2));
89      g = Math.Abs(x1 - x2) / s;
90      c = (1 - (3 / (4 * (n1 + n2) - 9))) * g;
91
92      return c;
93    }
94  }
95}
Note: See TracBrowser for help on using the repository browser.