[11610] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17180] | 3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[11610] | 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.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
| 26 |
|
---|
| 27 | namespace HeuristicLab.Analysis.Statistics {
|
---|
[12139] | 28 | public static class KernelDensityEstimator {
|
---|
[11610] | 29 | public static double[] Density(double[] x, double mean, double stdDev) {
|
---|
[12134] | 30 | return x.Select(xi => Density(xi, mean, stdDev)).ToArray();
|
---|
| 31 | }
|
---|
[11610] | 32 |
|
---|
[12134] | 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))));
|
---|
[11610] | 37 | }
|
---|
| 38 |
|
---|
[12135] | 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) {
|
---|
[12134] | 41 | // calculate grid for which to estimate the density
|
---|
[11610] | 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 |
|
---|
[12134] | 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();
|
---|
[11610] | 60 |
|
---|
[12134] | 61 | return newX.Zip(y, Tuple.Create).ToList();
|
---|
[11610] | 62 | }
|
---|
[12139] | 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 | }
|
---|
[11610] | 68 | }
|
---|
| 69 | }
|
---|