Attachment 'sheet10.m'

Download

   1 function sheet10
   2 
   3 % generate some data
   4 N = 50;
   5 X = 2 * pi * rand(N, 1);
   6 Y = fct1(X) + randn(N, 1);
   7 
   8 % plot the data
   9 hold off;
  10 plot(X, Y, '.');
  11 grid;
  12 
  13 % plot the linear fit without centering
  14 hold on;
  15 W = lsr(X, Y)
  16 plot_fit(X, Y, W, 0, 'r-');
  17 
  18 % ... and with centering
  19 [W, B] = lsr_with_centering(X, Y);
  20 plot_fit(X, Y, W, B, 'b-');
  21 
  22 % plot a polynomial fit for 4 different regularization constants
  23 for lambda = [1, 10, 100, 1000]
  24   W = lsr_poly(4, lambda, X, Y);
  25   plot_poly_fit(4, W, X, Y, 'k-');
  26 end
  27 
  28 hold off;
  29 
  30 legend('data', 'without centering', 'with centering', 'poly')
  31 
  32 function Y = fct1(X)
  33 Y = 0.2 * (X - pi - 1) .* (X - pi - 2.5) .* (X - pi + 1) .* X - 3;
  34 
  35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  36 % fill in your solutions below
  37 
  38 % 1. Center the data
  39 function X = center(X)
  40 % ...
  41 
  42 % 2. Compute the least-squares fit from X and Y
  43 function W = lsr(X, Y)
  44 % ...
  45 
  46 % 3. Compute the centered version. Compute W and B accordingly.
  47 function [W, B] = lsr_with_centering(X, Y)
  48 XM = mean(X);
  49 YM = mean(Y);
  50 X = center(X);
  51 Y = center(Y);
  52 
  53 % ...
  54 
  55 function plot_fit(X, Y, W, B, style)
  56 X = sort(X);
  57 Yh = W'*X + B;
  58 plot(X, Yh, style);
  59 
  60 % 4. Compute the least-squares fit using the function
  61 % poly_design_matrix. 
  62 function W = lsr_poly(K, lambda, X, Y)
  63 % ...
  64 
  65 % 5. Compute the design matrix for polynomials from degree 0 to K
  66 function PSI = poly_design_matrix(K, X)
  67 % ...
  68 
  69 % 6. Compute the prediction using for polynomials from degree 0 to K
  70 function W = plot_poly_fit(K, W, X, Y, style)
  71 X = sort(X);
  72 % ...
  73 plot(X, Yh, style);

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.