Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2865-Tourplanning/HeuristicLab.Analysis/3.3/Statistics/KernelDensityEstimator.cs @ 15834

Last change on this file since 15834 was 14185, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

File size: 2.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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 KernelDensityEstimator {
29    public static double[] Density(double[] x, double mean, double stdDev) {
30      return x.Select(xi => Density(xi, mean, stdDev)).ToArray();
31    }
32
33    public static double Density(double x, double mean, double stdDev) {
34      return (1.0 / (stdDev * Math.Sqrt(2.0 * Math.PI))) *
35                  Math.Exp(-((Math.Pow(x - mean, 2.0)) /
36                             (2.0 * Math.Pow(stdDev, 2.0))));
37    }
38
39    // the scale (sigma) of the kernel is a parameter
40    public static List<Tuple<double, double>> Density(double[] x, int nrOfPoints, double stepWidth, double sigma = 1.0) {
41      // calculate grid for which to estimate the density
42      double[] newX = new double[nrOfPoints];
43      double margin = stepWidth * 2;
44
45      double dataMin = x.Min() - margin;
46      double dataMax = x.Max() + margin;
47      double diff = (dataMax - dataMin) / nrOfPoints;
48      double cur = dataMin;
49      newX[0] = cur;
50      for (int i = 1; i < nrOfPoints; i++) {
51        cur += diff;
52        newX[i] = cur;
53      }
54
55      // for each of the points for which we want to calculate the density
56      // we sum up all the densities of the observed points assuming they are at the center of a normal distribution
57      var y = from xi in newX
58              select (from obsX in x
59                      select Density(xi, obsX, sigma)).Sum();
60
61      return newX.Zip(y, Tuple.Create).ToList();
62    }
63
64    //Silverman's rule of thumb for bandwidth estimation (sigma)
65    public static double EstimateBandwidth(double[] x) {
66      return 1.06 * x.StandardDeviation() * Math.Pow(x.Length, -0.2);
67    }
68  }
69}
Note: See TracBrowser for help on using the repository browser.