SG++-Doxygen-Documentation
optimize_kde_bandwidth.cpp

This example can be found under datadriven/examples/optimize_kde_bandwidth.cpp.

// Copyright (C) 2008-today The SG++ project
// This file is part of the SG++ project. For conditions of distribution and
// use, please see the copyright notice provided with SG++ or at
// sgpp.sparsegrids.org
#include <sgpp_base.hpp>
#include <iostream>
#include <iterator>
#include <algorithm>
#include <random>
#include <vector>
void printLine() {
std::cout << "----------------------------------------"
"----------------------------------------\n";
}
void randn(DataVector& rvar, std::mt19937& generator) {
std::normal_distribution<double> distribution(0.5, 0.02);
for (size_t j = 0; j < rvar.getSize(); ++j) {
rvar[j] = distribution(generator);
}
}
void randn(DataMatrix& rvar, std::uint64_t seedValue = std::mt19937_64::default_seed) {
size_t nsamples = rvar.getNrows(), ndim = rvar.getNcols();
std::mt19937 generator(seedValue);
DataVector sample(ndim);
for (size_t i = 0; i < nsamples; ++i) {
randn(sample, generator);
rvar.setRow(i, sample);
}
}
int main(int argc, const char* argv[]) {
(void)argc;
(void)argv;
std::cout << "sgpp::optimization kde bandwidth optimization.\n\n";
// dimension of domain
const size_t d = 1;
DataVector bandwidths(d);
// estimate a kernel density
DataMatrix samples(1000, d);
randn(samples);
KernelDensityEstimator kdeRot(samples, KernelType::GAUSSIAN,
BandwidthOptimizationType::SILVERMANSRULE);
kdeRot.getBandwidths(bandwidths);
std::cout << "h_rot = " << bandwidths.toString() << " -> " << kdeRot.crossEntropy(samples)
<< std::endl;
KernelDensityEstimator kdeOpt(samples, KernelType::GAUSSIAN,
BandwidthOptimizationType::MAXIMUMLIKELIHOOD);
kdeOpt.getBandwidths(bandwidths);
std::cout << "h_opt = " << bandwidths.toString() << " -> " << kdeOpt.crossEntropy(samples)
<< std::endl;
return 0;
}