function temple_abm_kde
%TEMPLE_ABM_KDE
% Three ways of constructing a density field from point
% positions: (a) histogram (i.e., piecewise constant)
% via Matlab's hist3; (b) histgram by hand; and
% (c) kernel density estimation via Gaussian kernels
% (i.e., smooth).
%
% 02/2018 by Benjamin Seibold
% http://www.math.temple.edu/~seibold/
% Parameters
N = 2e4; % number of points (agents)
nch = 20; % number of histogram cells in each dimension
nck = 50; % number of cell in each dimension for KDE
w = 3/nck; % kernel width
P = [.1+.7*rand(N,1),.4+randn(N,1)/5]; % position of points (non-uniform)
% Initialization
h = 1/nch; % cell size for histogram
xb = 0:h:1; % x-coordinates of cell boundaries
yb = xb; % y-coordinates of cell boundaries
x = h/2:h:1-h/2; % x-coordinates of cell centers
y = x; % y-coordinates of cell centers
hk = 1/nck; % cell size of KDE
xk = hk/2:hk:1-hk/2; % x-coordinates of cell centers for KDE
yk = xk; % y-coordinates of cell centers for KDE
[XK,YK] = meshgrid(xk,yk); % position matrices
% Compute histogram by hand
H = zeros(nch,nch);
for jx = 1:nch % loop over
for jy = 1:nch % all cells
H(jy,jx) = sum(xb(jx)