Free cookie consent management tool by TermsFeed Policy Generator

source: branches/StatisticalTesting/HeuristicLab.Analysis.Statistics/3.3/NormalDistribution.cs @ 11601

Last change on this file since 11601 was 11601, checked in by ascheibe, 9 years ago

#2031

  • fixed column width of p-values
  • started working on drawing a normal distribution over the histogram
File size: 2.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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 HeuristicLab.Common;
26
27namespace HeuristicLab.Analysis.Statistics {
28  public static class NormalDistribution {
29    public static double[] Density(double[] x, double mean, double stdDev) {
30      double[] result = new double[x.Length];
31
32      for (int i = 0; i < x.Length; i++) {
33        result[i] = (1.0 / (stdDev * Math.Sqrt(2.0 * Math.PI))) *
34                    Math.Exp(-((Math.Pow(x[i] - mean, 2.0)) /
35                               (2.0 * Math.Pow(stdDev, 2.0))));
36      }
37
38      return result;
39    }
40
41    // based on the idea from http://www.statmethods.net/graphs/density.html
42    public static List<Tuple<double, double>> Density(double[] x, int nrOfPoints, double stepWidth) {
43      double[] newX = new double[nrOfPoints];
44      double mean = x.Average();
45      double stdDev = x.StandardDeviation();
46
47      double dataMin = x.Min();
48      double dataMax = x.Max();
49      double stepWith = (dataMax - dataMin) / nrOfPoints;
50      double cur = x.Min();
51      newX[0] = cur;
52      for (int i = 1; i < nrOfPoints; i++) {
53        cur += stepWith;
54        newX[i] = cur;
55      }
56
57      var y = Density(newX, mean, stdDev).Select(k => k * stepWidth * x.Length).ToList();
58
59      var points = new List<Tuple<double, double>>();
60      for (int i = 0; i < newX.Length; i++) {
61        points.Add(new Tuple<double, double>(newX[i], y[i]));
62      }
63
64      return points;
65    }
66  }
67}
Note: See TracBrowser for help on using the repository browser.