function sheet04
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% DO NOT EDIT STARTING HERE, EDIT ONLY BELOW

% Generate some data.
[X, Y] = generate_data(1000, 500);

% and test data
[XE, YE] = generate_data(2000, 1000);

plot_data(X, Y);

% estimate mean and covariance function for the two classes
[C.MP, C.CP] = ml_estimate(X, Y, 1);
[C.MN, C.CN] = ml_estimate(X, Y, -1);

% estimate the class priors
C.PP = prior_estimate(X, Y, 1);
C.PN = prior_estimate(X, Y, -1);

C

% predict class labels for the training and test data
YH = predict(C, X);
YEH = predict(C, XE);

% display errors
fprintf('training error: %.2f%%\n', mean(YH ~= Y)*100);
fprintf('test error: %.2f%%\n', mean(YEH ~= YE)*100);

% plot decision function
[MX, MY] = meshgrid(linspace(-20, 20, 30), linspace(-10, 20, 30));
Z = posterior_grid(C, MX, MY, 1);

hold on;
contour(MX, MY, Z, [0.5]);
colormap([0 0 0]);
hold off;

function [X, Y] = generate_data(n1, n2)
X = [randn(n1, 2)*diag([1, 2])*rotmat(pi/4) + repmat([-7, 0], n1, 1);...
     randn(n2, 2)*diag([3,4])*rotmat(3*pi/4) + repmat([4, 4], n2, 1)];
Y = [ones(n1, 1); -ones(n2, 1)];

function R = rotmat(phi)
R = [cos(phi), sin(phi); -sin(phi), cos(phi)];

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% EDIT BELOW

% 1. Plot the data set by plotting the class for Y = 1 with red crosses and
% the class with Y == -1 with blue circles. Make sure axes are equally
% scaled and add a grid (5 Punkte).
function plot_data(X, Y)
% ...

% 2. Estimate the mean and covariance using the ml-estimator for
% multivariate Gaussians for the given class (5 Punkte).
function [M, C] = ml_estimate(X, Y, class)
% ...

% 3. Estimate the class priors for the given class (5 Punkte)
function P = prior_estimate(X, Y, class)
% ...

% 4. Compute the multivariate density (5 Punkte)
function P = gaussian(M, C, X)
% ...

% 5. Predict the more likely class based on the class posterior. (5 Punkte)
function YH = predict(C, X)
% ...

% 6. Compute class posteriors on a grid. (5 Punkte)
function P = posterior_grid(C, MX, MY, class)
X = [reshape(MX, prod(size(MX)), 1), ...
     reshape(MY, prod(size(MY)), 1)];
% ...
