
% FEATURE SPACE
%
% generate a random point in feature space using an rbf kernel.
%
% generate and plot a blob centred on each sample point.
% plot the (non-linear) decision boundary between the two distributions.
 
% to do this, first generate a sample from each of two gaussian 
% distributions whose decision boundary is a horizontal line.
% then distort each sample point by applying a polynomial to it.
% the decision boundary between the two distorted gaussians is thus a
% polynomial.


% prepare the axes.
hold off;
range = 10;
axis([0,range,0,range]); 
hold on

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% randomly generate points in input space.
n = 20;    % number of points from input space
x = rand(n,1)*range;
y = rand(n,1)*range;

% % plot the points as big circles.
% plot(x,y,'r.','MarkerSize',15);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% A blob is a random sample from a small gaussian distribution.
sigma = diag([1,1]);    % variance matrix of each blob
m = 2000;    % number of points in each blob
 
% % generate and plot one blob around each input point. 
% for i = 1:n
%     mu = [x(i),y(i)];    % blob mean
%     blob = mvnrnd(mu,sigma,m);    % generate the points in the blob.
%     plot(blob(:,1),blob(:,2),'r.','MarkerSize',2);    % plot the blob.
% end;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% generate a weight for each blob.
w = rand(n,1)*2 - 1;    % each weight is between -1 and 1.
% w = rand(n,1)*2;    % each weight is between 0 and 2.

% % plot the weighted blobs, putting more points in heavier blobs.
% for i = 1:n
%     mu = [x(i),y(i)];    % blob mean
%     k = ceil(m*w(i));    % number of points in the blob
%     blob = mvnrnd(mu,sigma,k);    % generate a blob.
%     plot(blob(:,1),blob(:,2),'r.','MarkerSize',2);    % plot the blob.
% end;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% generate a contour plot of the blobs.
m = 100;    % mesh size;
grid = range * (0:m)/m;    % grid values
[X,Y] = meshgrid(grid,grid);
k = m+1;    % number of x and y values
X = reshape(X,k*k,1);
Y = reshape(Y,k*k,1);
points = zeros(k*k,2);
points(:,1) = X;
points(:,2) = Y;

% generate the blobs and add them together.
% each blob is a small gaussian.
Z = zeros(k*k,1);    % running sum of the blobs
for i = 1:n
    % add a weighted blob the running sum
    mu = [x(i),y(i)];    % blob mean
    Z = Z + w(i)*mvnpdf(points,mu,sigma);
end;

% generate a contour plot of the blobs.
X = reshape(X,k,k);
Y = reshape(Y,k,k);
Z = reshape(Z,k,k);
contour(X,Y,Z,50);    % full contour plot
%contour(X,Y,Z,[0,0]);    % decision boundary
%contour(X,Y,Z,[-.03,0,.03]);    % decision boundary and margins
%contour(X,Y,Z,[-.003,0,.003]);    % decision boundary and margins


