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 |
|
---|
22 | using System;
|
---|
23 | using System.Linq;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 |
|
---|
26 | namespace HeuristicLab.Analysis.Statistics {
|
---|
27 | public class SampleSizeDetermination {
|
---|
28 | /// <summary>
|
---|
29 | /// Determines for a given sample the sample size by estimating the means.
|
---|
30 | /// </summary>
|
---|
31 | /// <param name="samples">The pilot sample.</param>
|
---|
32 | /// <param name="e">Precision. </param>
|
---|
33 | /// <param name="conf">Confidence Interval.</param>
|
---|
34 | /// <returns>Number of required samples for the given confidence interval and precision. </returns>
|
---|
35 | public static int DetermineSampleSizeByEstimatingMean(double[] samples, double e, double conf = 0.95) {
|
---|
36 | if (e < 0) throw new ArgumentException("e needs to be a positive number.");
|
---|
37 | if (conf < 0 || conf > 1) throw new ArgumentException("The confidence Interval must be between zero and one.");
|
---|
38 | double result = 0;
|
---|
39 |
|
---|
40 | double var = samples.StandardDeviation();
|
---|
41 | double n = alglib.invnormaldistribution((conf + 1) / 2);
|
---|
42 | result = Math.Pow(n, 2) * Math.Pow(var, 2) / Math.Pow(e, 2);
|
---|
43 | result = Math.Ceiling(result);
|
---|
44 | if (result > int.MaxValue)
|
---|
45 | return int.MaxValue;
|
---|
46 | else
|
---|
47 | return (int)result;
|
---|
48 | }
|
---|
49 |
|
---|
50 | /// <summary>
|
---|
51 | /// Calculates Cohen's d.
|
---|
52 | /// </summary>
|
---|
53 | /// <returns>Cohen's d.
|
---|
54 | /// d = 0.2 means small effect
|
---|
55 | /// d = 0.5 means medium effect
|
---|
56 | /// d = 0.8 means big effect
|
---|
57 | /// According to Wikipedia this means: "A lower Cohen's d indicates a necessity of larger sample sizes, and vice versa."
|
---|
58 | /// </returns>
|
---|
59 | public static double CalculateCohensD(double[] d1, double[] d2) {
|
---|
60 | double x1, x2, s1, s2;
|
---|
61 |
|
---|
62 | x1 = d1.Average();
|
---|
63 | x2 = d2.Average();
|
---|
64 | s1 = d1.Variance();
|
---|
65 | s2 = d2.Variance();
|
---|
66 |
|
---|
67 | return (x1 - x2) / Math.Sqrt((s1 + s2) / 2);
|
---|
68 | }
|
---|
69 |
|
---|
70 | /// <summary>
|
---|
71 | /// Calculates Hedges' g.
|
---|
72 | /// Hedges' g works like Cohen's d but corrects for bias.
|
---|
73 | /// </summary>
|
---|
74 | /// <returns>Hedges' g</returns>
|
---|
75 | public static double CalculateHedgesG(double[] d1, double[] d2) {
|
---|
76 | double x1, x2, s1, s2, n1, n2, s, g, c;
|
---|
77 |
|
---|
78 | x1 = d1.Average();
|
---|
79 | x2 = d2.Average();
|
---|
80 | s1 = d1.Variance();
|
---|
81 | s2 = d2.Variance();
|
---|
82 | n1 = d1.Count();
|
---|
83 | n2 = d2.Count();
|
---|
84 |
|
---|
85 | s = Math.Sqrt(((n1 - 1) * s1 + (n2 - 1) * s2) / (n1 + n2 - 2));
|
---|
86 | g = (x1 - x2) / s;
|
---|
87 | c = (1 - (3 / (4 * (n1 + n2) - 9))) * g;
|
---|
88 |
|
---|
89 | return c;
|
---|
90 | }
|
---|
91 | }
|
---|
92 | }
|
---|