%define sample size
sampleSize = 50;

dist1Samples = zeros(sampleSize,1);
dist2Samples = zeros(sampleSize,1);

% generate points from two normal pdf's
for i = 1:sampleSize
    dist1Samples(i) = normrnd(20,5);
    dist2Samples(i) = normrnd(35,5);
end

% combine points of pdf's
allPoints = [dist1Samples;dist2Samples];

% define window widths
hs = [0.001,1,10]';

% define discrete values of x
xs = [0:1:55]';

% calculate actual pdf values
targetValues1 = zeros(size(xs,1),1);
targetValues2 = zeros(size(xs,1),1);
for i=1:size(xs,1)
targetValues1(i) = normpdf(xs(i),20,5);
targetValues2(i) = normpdf(xs(i),35,5);
end

% start finding p-hat(x) values for each bandwidth
probValues = zeros(size(xs,1),size(hs,1));
for bandwidthCounter = 1:size(hs,1)
    myBandwidth = hs(bandwidthCounter);
    for i = 1:size(xs,1)
        myX = xs(i);
        myProbability = 0;
        for j=1:(sampleSize*2)
            u = (myX - allPoints(j))/myBandwidth;
            myProbability = myProbability + (1/sqrt(2*pi))*exp((-u^2)/2);
        end
        myProbability = myProbability / (sampleSize * myBandwidth);
        probValues(i,bandwidthCounter) = myProbability;
    end
end

% now draw the figure
markerSize = 5;
figure;
hold on;
set(gca,'FontSize', 20,'FontName','Times New Roman','LineWidth',2.5);
h1 = semilogy(xs,probValues(:,1));
h2 = semilogy(xs,probValues(:,2));
h3 = semilogy(xs,probValues(:,3));
h4 = semilogy(xs,targetValues1+targetValues2);
set(h1, 'LineStyle', '--', 'LineWidth', 1.0, 'Color', 'Black');
set(h1, 'Marker', 'o', 'MarkerFaceColor', [0.5 0.5 0.5], 'MarkerEdgeColor', [0 0 0], 'MarkerSize', markerSize);
set(h2, 'LineStyle', '-', 'LineWidth', 1.0, 'Color',' Black');
set(h2, 'Marker', 's', 'MarkerFaceColor', [1 1 1], 'MarkerEdgeColor', [0 0 0], 'MarkerSize', markerSize);
set(h3, 'LineStyle', '-', 'LineWidth', 1.0, 'Color',' Black');
set(h3, 'Marker', 'd', 'MarkerFaceColor', [1 1 1], 'MarkerEdgeColor', [0 0 0], 'MarkerSize', markerSize);
set(h4, 'LineStyle', '-', 'LineWidth', 1.0, 'Color',' Black');
set(h4,'LineWidth', 1.5, 'MarkerFaceColor', [1 1 1], 'MarkerEdgeColor', [0 0 0], 'MarkerSize', markerSize);
title([num2str(sampleSize) ' Samples Simulated From 2 Gaussian PDFs']);
xlabel('Instances IDs');
ylabel('Probability Values in Log Scale');
legend('h=0.001','h=1','h=10','Actual PDFs');