Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ALPS/HeuristicLab.Analysis/3.3/Statistics/NormalDistribution.cs @ 12018

Last change on this file since 12018 was 12018, checked in by pfleck, 10 years ago

#2269

  • merged trunk after 3.3.11 release
  • updated copyright and plugin version in ALPS plugin
  • removed old ALPS samples based on an userdefined alg
File size: 2.3 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.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      double margin = stepWidth * 2;
47
48      double dataMin = x.Min() - margin;
49      double dataMax = x.Max() + margin;
50      double diff = (dataMax - dataMin) / nrOfPoints;
51      double cur = dataMin;
52      newX[0] = cur;
53      for (int i = 1; i < nrOfPoints; i++) {
54        cur += diff;
55        newX[i] = cur;
56      }
57
58      var y = Density(newX, mean, stdDev).Select(k => k * stepWidth * x.Length).ToList();
59
60      var points = new List<Tuple<double, double>>();
61      for (int i = 0; i < newX.Length; i++) {
62        points.Add(new Tuple<double, double>(newX[i], y[i]));
63      }
64
65      return points;
66    }
67  }
68}
Note: See TracBrowser for help on using the repository browser.