% 'hist_demo.m'
% Demo of the histogram method and the KNN density estimation method
% Dr. David Pan, UAH

close all;
clear all;
M = 1000;
X = [randn(M, 1); 5+randn(M, 1)];
h = histogram(X, 20, 'Normalization', 'pdf');
h.BinLimits
sum(h.Values)
h.BinWidth
sum(h.Values)*h.BinWidth

% The true pdf 
x = -6: 0.01: 10;
f = 1/2*(normpdf(x, 0, 1) + normpdf(x, 5, 1));
% Check the area under the curve
trapz(x,f)

hold on;
plot(x,f)

N = 2*M;    % Total number of data points
% KNN density estimation of f(x) = K/NV
% For this dataset, V is the“bin” width, 
% where the “bin” contains the nearest K data points.

K = 150;
% For each point, find the distance between this point and all other points
Y = unique(sort(X));

Density = zeros(length(Y), 1);
Dist = zeros(N, 1);

for i = 1: length(Y)
    Dist = (Y(i) - X).^2;
    [Dist_sorted, Index] = sort(Dist);
   
    % Among the K nearest neighbors, find the range of these value
    V = range(X(Index(1:K)));
    Density(i) = K/(N*V);
end

figure;
plot(Y, Density)
hold on;
plot(x,f)

    
    





