bpp-core  2.2.0
AdaptiveKernelDensityEstimation.cpp
Go to the documentation of this file.
1 //
2 // File: AdaptiveKernelDensityEstimation.cpp
3 // Created by: Julien Dutheil
4 // Created on: Thu Nov 05 13:25:07 2009
5 //
6 
7 /*
8 Copyright or © or Copr. Bio++ Development Team, (November 17, 2004)
9 
10 This software is a computer program whose purpose is to provide classes
11 for numerical calculus. This file is part of the Bio++ project.
12 
13 This software is governed by the CeCILL license under French law and
14 abiding by the rules of distribution of free software. You can use,
15 modify and/ or redistribute the software under the terms of the CeCILL
16 license as circulated by CEA, CNRS and INRIA at the following URL
17 "http://www.cecill.info".
18 
19 As a counterpart to the access to the source code and rights to copy,
20 modify and redistribute granted by the license, users are provided only
21 with a limited warranty and the software's author, the holder of the
22 economic rights, and the successive licensors have only limited
23 liability.
24 
25 In this respect, the user's attention is drawn to the risks associated
26 with loading, using, modifying and/or developing or reproducing the
27 software by the user in light of its specific status of free software,
28 that may mean that it is complicated to manipulate, and that also
29 therefore means that it is reserved for developers and experienced
30 professionals having in-depth computer knowledge. Users are therefore
31 encouraged to load and test the software's suitability as regards their
32 requirements in conditions enabling the security of their systems and/or
33 data to be ensured and, more generally, to use and operate it in the
34 same conditions as regards security.
35 
36 The fact that you are presently reading this means that you have had
37 knowledge of the CeCILL license and that you accept its terms.
38 */
39 
41 #include "Matrix/MatrixTools.h"
42 #include "NumConstants.h"
43 
44 using namespace bpp;
45 using namespace std;
46 
48 {
49  //Compute the covariance matrix of the sample:
50  MatrixTools::covar(x_, covar_);
51 
52  //Compute the mean vector
53  sampleMean_(x_, xMean_);
54 
55  //Compute the inverse of the square root of the covariance matrix:
56  MatrixTools::pow<double>(covar_, -0.5, invSqrtCovar_);
57 
58  //Compute the bandwidth:
59  h_ = std::pow(4. / ((2 * static_cast<double>(r_) + 1.) * static_cast<double>(n_)), 1. / (static_cast<double>(r_) + 4.));
60  //Compute as much as we can in advance to simplify the density calculation:
61  c1_ = 1. / (std::sqrt(MatrixTools::det(covar_)) * static_cast<double>(n_) * std::pow(h_, static_cast<int>(r_)));
62 
63  //Now compute the local tuning of the bandwidth.
64  //First estimate the pilot density:
65  vector<double> xi(r_);
66  LinearMatrix<double> diff_xi(r_, 1);
67  LinearMatrix<double> std_xi(r_, 1);
68  for (unsigned int i = 0; i < n_; i++)
69  {
70  //Get the current xi point to evaluate:
71  for(unsigned int k = 0; k < r_; k++)
72  xi[k] = x_(k, i);
73 
74  //Sum loop, over all xi's:
75  double sum = 0;
76  for (unsigned int j = 0; j < n_; j++)
77  {
78  for (unsigned int k = 0; k < r_; k++)
79  diff_xi(k, 0) = xi[k] - x_(k, j);
80  MatrixTools::mult(invSqrtCovar_, diff_xi, std_xi);
81  MatrixTools::scale(std_xi, 1. / h_);
82  sum += kernel_(std_xi);
83  }
84  pilot_[i] = c1_ * sum;
85  }
86 
87  //Compute the tuning parameters:
88  double g = 0;
89  for (unsigned int i = 0; i < n_; i++)
90  g += std::log(pilot_[i]);
91  g = std::exp(g / static_cast<double>(n_));
92  for (unsigned int i = 0; i < n_; i++)
93  lambda_[i] = std::pow(g / pilot_[i], gamma_);
94 
95  //Compute as much as we can in advance to simplify the density calculation:
96  for (unsigned int i = 0; i < n_; i++)
97  c2_[i] = std::pow(lambda_[i], - static_cast<double>(r_));
98 }
99 
100 void AdaptiveKernelDensityEstimation::sampleMean_(const Matrix<double>& x, std::vector<double>& mean)
101 {
102  size_t nc = x.getNumberOfColumns();
103  size_t nr = x.getNumberOfRows();
104  mean.resize(nr);
105  for (size_t i = 0; i < nr; i++)
106  {
107  mean[i] = 0;
108  for (size_t j = 0; j < nc; j++)
109  mean[i] += x(i, j);
110  mean[i] /= static_cast<double>(nc);
111  }
112 }
113 
115 {
116  //x is supposed to have only one column and r_ rows.
117  //We compute the scalar product of the column with itself:
118  double scalar = 0;
119  for (size_t i = 0; i < r_; i++)
120  scalar += std::pow(x(i, 0), 2.);
121 
122  return std::pow(2. * NumConstants::PI(), -static_cast<double>(r_) / 2.) * std::exp(-0.5 * scalar);
123 }
124 
125 double AdaptiveKernelDensityEstimation::kDensity(const std::vector<double>& x)
126 {
127  LinearMatrix<double> diff_xi(r_, 1);
128  LinearMatrix<double> std_xi(r_, 1);
129  //Sum loop, over all xi's:
130  double sum = 0;
131  for(unsigned int j = 0; j < n_; j++)
132  {
133  for(unsigned int k = 0; k < r_; k++)
134  diff_xi(k, 0) = x[k] - x_(k, j);
135  MatrixTools::mult(invSqrtCovar_, diff_xi, std_xi);
136  MatrixTools::scale(std_xi, 1. / (h_ * lambda_[j]));
137  sum += kernel_(std_xi) * c2_[j];
138  }
139  return c1_ * sum;
140 }
141 
The matrix template interface.
Definition: Matrix.h:58
This class allows to perform a correspondence analysis.
STL namespace.
Matrix storage in one vector.
Definition: Matrix.h:345
virtual size_t getNumberOfColumns() const =0
static void mult(const Matrix< Scalar > &A, const Matrix< Scalar > &B, Matrix< Scalar > &O)
Definition: MatrixTools.h:190
void sampleMean_(const Matrix< double > &x, std::vector< double > &mean)
static void scale(Matrix &A, Scalar a, Scalar b=0)
Multiply all elements of a matrix by a given value, and add a constant.
Definition: MatrixTools.h:173
double kernel_(const Matrix< double > &x)
The kernel function.
double kDensity(const std::vector< double > &x)
static double PI()
Definition: NumConstants.h:96
virtual size_t getNumberOfRows() const =0
static void covar(const Matrix< Scalar > &A, Matrix< Scalar > &O)
Compute the variance-covariance matrix of an input matrix.
Definition: MatrixTools.h:731
static double det(const Matrix< Scalar > &A)
Get determinant of a square matrix.
Definition: MatrixTools.h:694