Attachment 'sheet04.m'

Download

   1 function sheet04
   2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   3 % DO NOT EDIT STARTING HERE, EDIT ONLY BELOW
   4 
   5 % Generate some data.
   6 [X, Y] = generate_data(1000, 500);
   7 
   8 % and test data
   9 [XE, YE] = generate_data(2000, 1000);
  10 
  11 plot_data(X, Y);
  12 
  13 % estimate mean and covariance function for the two classes
  14 [C.MP, C.CP] = ml_estimate(X, Y, 1);
  15 [C.MN, C.CN] = ml_estimate(X, Y, -1);
  16 
  17 % estimate the class priors
  18 C.PP = prior_estimate(X, Y, 1);
  19 C.PN = prior_estimate(X, Y, -1);
  20 
  21 C
  22 
  23 % predict class labels for the training and test data
  24 YH = predict(C, X);
  25 YEH = predict(C, XE);
  26 
  27 % display errors
  28 fprintf('training error: %.2f%%\n', mean(YH ~= Y)*100);
  29 fprintf('test error: %.2f%%\n', mean(YEH ~= YE)*100);
  30 
  31 % plot decision function
  32 [MX, MY] = meshgrid(linspace(-20, 20, 30), linspace(-10, 20, 30));
  33 Z = posterior_grid(C, MX, MY, 1);
  34 
  35 hold on;
  36 contour(MX, MY, Z, [0.5]);
  37 colormap([0 0 0]);
  38 hold off;
  39 
  40 function [X, Y] = generate_data(n1, n2)
  41 X = [randn(n1, 2)*diag([1, 2])*rotmat(pi/4) + repmat([-7, 0], n1, 1);...
  42      randn(n2, 2)*diag([3,4])*rotmat(3*pi/4) + repmat([4, 4], n2, 1)];
  43 Y = [ones(n1, 1); -ones(n2, 1)];
  44 
  45 function R = rotmat(phi)
  46 R = [cos(phi), sin(phi); -sin(phi), cos(phi)];
  47 
  48 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  49 % EDIT BELOW
  50 
  51 % 1. Plot the data set by plotting the class for Y = 1 with red crosses and
  52 % the class with Y == -1 with blue circles. Make sure axes are equally
  53 % scaled and add a grid (5 Punkte).
  54 function plot_data(X, Y)
  55 % ...
  56 
  57 % 2. Estimate the mean and covariance using the ml-estimator for
  58 % multivariate Gaussians for the given class (5 Punkte).
  59 function [M, C] = ml_estimate(X, Y, class)
  60 % ...
  61 
  62 % 3. Estimate the class priors for the given class (5 Punkte)
  63 function P = prior_estimate(X, Y, class)
  64 % ...
  65 
  66 % 4. Compute the multivariate density (5 Punkte)
  67 function P = gaussian(M, C, X)
  68 % ...
  69 
  70 % 5. Predict the more likely class based on the class posterior. (5 Punkte)
  71 function YH = predict(C, X)
  72 % ...
  73 
  74 % 6. Compute class posteriors on a grid. (5 Punkte)
  75 function P = posterior_grid(C, MX, MY, class)
  76 X = [reshape(MX, prod(size(MX)), 1), ...
  77      reshape(MY, prod(size(MY)), 1)];
  78 % ...

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2009-03-12 14:03:18, 318.2 KB) [[attachment:BayesianDecisionTheory.pdf]]
  • [get | view] (2009-03-12 14:03:18, 247.3 KB) [[attachment:crossvalidation.pdf]]
  • [get | view] (2009-03-12 14:03:18, 1740.8 KB) [[attachment:mlintro.pdf]]
  • [get | view] (2009-03-12 14:03:18, 665.3 KB) [[attachment:probtheo.pdf]]
  • [get | view] (2009-03-12 14:03:18, 52.7 KB) [[attachment:sheet01.pdf]]
  • [get | view] (2009-03-12 14:03:18, 61.7 KB) [[attachment:sheet02.pdf]]
  • [get | view] (2009-03-12 14:03:18, 65.8 KB) [[attachment:sheet03.pdf]]
  • [get | view] (2009-03-12 14:03:18, 2.1 KB) [[attachment:sheet04.m]]
  • [get | view] (2009-03-12 14:03:18, 80.3 KB) [[attachment:sheet04.pdf]]
  • [get | view] (2009-03-12 14:03:18, 106.7 KB) [[attachment:sheet05.pdf]]
  • [get | view] (2009-03-12 14:03:18, 1.6 KB) [[attachment:sheet05_01.m]]
  • [get | view] (2009-03-12 14:03:18, 1.6 KB) [[attachment:sheet05_02.m]]
  • [get | view] (2009-03-12 14:03:18, 0.8 KB) [[attachment:sheet06.m]]
  • [get | view] (2009-03-12 14:03:18, 93.2 KB) [[attachment:sheet06.pdf]]
  • [get | view] (2009-03-12 14:03:18, 1.4 KB) [[attachment:sheet07.m]]
  • [get | view] (2009-03-12 14:03:18, 100.6 KB) [[attachment:sheet07.pdf]]
  • [get | view] (2009-03-12 14:03:18, 2.7 KB) [[attachment:sheet08.m]]
  • [get | view] (2009-03-12 14:03:18, 85.4 KB) [[attachment:sheet08.pdf]]
  • [get | view] (2009-03-12 14:03:18, 45.2 KB) [[attachment:sheet09.pdf]]
  • [get | view] (2009-03-12 14:03:18, 1.5 KB) [[attachment:sheet10.m]]
  • [get | view] (2009-03-12 14:03:18, 59.4 KB) [[attachment:sheet10.pdf]]
  • [get | view] (2009-03-12 14:03:18, 3.0 KB) [[attachment:sheet11.m]]
  • [get | view] (2009-03-12 14:03:18, 68.6 KB) [[attachment:sheet11.pdf]]
  • [get | view] (2009-03-12 14:03:18, 1.7 KB) [[attachment:sheet12.m]]
  • [get | view] (2009-03-12 14:03:18, 62.6 KB) [[attachment:sheet12.pdf]]
  • [get | view] (2009-03-12 14:03:18, 2.4 KB) [[attachment:sheet12_data.m]]
  • [get | view] (2009-03-12 14:03:18, 63.1 KB) [[attachment:sheet13.pdf]]
  • [get | view] (2009-03-12 14:03:18, 1.6 KB) [[attachment:sheet14.m]]
  • [get | view] (2009-03-12 14:03:18, 73.7 KB) [[attachment:sheet14.pdf]]
  • [get | view] (2009-03-12 14:03:18, 4.8 KB) [[attachment:sheet14_data.m]]
  • [get | view] (2009-03-12 14:03:18, 120.2 KB) [[attachment:summary.pdf]]
  • [get | view] (2009-03-12 14:03:18, 1505.5 KB) [[attachment:vl_ica_tub-08.pdf]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.