#include <iostream>
#include <vector>
#include <cmath>
#include <boost/random.hpp>
using namespace std;
using namespace boost;
class Kernel {
private:
double X;
public:
Kernel(double x) : X(x) {}
double density(double x, double h) const {
return exp(-pow((x-X) / h, 2) / 2) / sqrt(2 * M_PI);
}
};
double sum_of_kernel_density(const vector<Kernel> &k, double x, double h) {
double sum = 0;
for (int i = 0; i < (int)k.size(); ++i) {
sum += k[i].density(x, h);
}
return sum / (k.size() * h);
}
int main() {
const unsigned int seed = 123;
mt19937 gen(seed);
normal_distribution<double> dst(5.0, 1.0);
variate_generator<mt19937, normal_distribution<double> > r_norm(gen, dst);
const int N = 100;
vector<Kernel> k;
for(int i = 0; i < N; ++i) {
k.push_back(Kernel(r_norm()));
}
const double h = 0.7;
for(int i = 0; i < 10; ++i) {
cout << i << "\t" << sum_of_kernel_density(k, i, h) << endl;
}
return 0;
}