Attachment 'sheet08.m'

Download

   1 function sheet08
   2 
   3 % The parameters:
   4 M = 1000; % number of evaluation points
   5 X = linspace(-4, 4, M)'; % the evaluation points
   6 ITERS = 100; % number of resamples 
   7 WIDTHS = logspace(-3, 1, 9); % the kernel widths
   8 N = 100; % number of data points
   9 
  10 % set up some empty vectors to collect the information
  11 BIAS2 = zeros(length(WIDTHS), 1);
  12 VARIANCE = zeros(length(WIDTHS), 1);
  13 ERR = zeros(length(WIDTHS), 1);
  14 
  15 % plot individual means and variances for 9
  16 % different widths
  17 figure(1)
  18 for WI = 1:length(WIDTHS)
  19   W = WIDTHS(WI)
  20   
  21   % collect predictions for ITERS many resamples
  22   Y = zeros(M, ITERS);
  23   for I = 1:ITERS
  24     [DX, DY] = generate_data(N);
  25     Y(:, I) = kernel_regression(W, DX, DY, X);
  26   end
  27 
  28   % compute mean and variance
  29   ME = mean(Y, 2);
  30   VA = variance_per_point(Y);
  31 
  32   % plot the result
  33   subplot(3, 3, WI)
  34   plot(DX, DY, '.', ... % the last data set
  35        X, f(X), 'b-', ... % the true function 
  36        X, ME, 'r-', ... % the mean of the predictions
  37        X, ME + 2*sqrt(VA), 'r--', ... % +- 2*sqrt(variance)
  38        X, ME - 2*sqrt(VA), 'r--', ...
  39        X, Y(:, end), 'g-'); % the last learned function
  40 
  41   % compute mean squared bias, variance, and error
  42   BIAS2(WI) = mean(squared_bias_per_point(Y, f(X)));
  43   VARIANCE(WI) = mean(VA);
  44   ERR(WI) = mean(squared_error_per_point(Y, f(X)));
  45   title(sprintf('width = %f\n bias^2 = %f, var = %f', ...
  46                 W, BIAS2(WI), VARIANCE(WI)));
  47   drawnow
  48 end
  49 
  50 % plot squared bias, variance, and error
  51 figure(2)
  52 semilogx(WIDTHS, BIAS2, WIDTHS, VARIANCE, WIDTHS, ERR)
  53 legend('bias^2', 'variance', 'error')
  54 
  55 % Generate data 
  56 function [X, Y] = generate_data(N)
  57 X = sort(rand(N, 1) * 8 - 4);
  58 Y = f(X) + 0.5 * randn(N, 1);
  59 
  60 % the true function (sinc function + linear offset)
  61 function Y = f(X)
  62 Y = sin(4*X)./(4*X) + X/2;
  63 
  64 % Compute kernel regression estimates
  65 function YE = kernel_regression(W, X, Y, XE)
  66 K = rbfkern(W, X, XE);
  67 YE = Y'*K ./ sum(K);
  68 
  69 % Compute a Gaussian kernel
  70 function K = rbfkern(W, X1, X2)
  71 D = pwdist(X1, X2);
  72 K = exp(-D / W);
  73 
  74 % Compute all pairwise distances
  75 function D = pwdist(X, Y)
  76 D = size(X, 2);
  77 N = size(X, 1);
  78 M = size(Y, 1);
  79   
  80 XX = sum(X.*X, 2);
  81 YY = sum(Y.*Y, 2);
  82 D = repmat(XX, 1, M) + repmat(YY', N, 1) - 2*X*Y';
  83 
  84 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  85 % Your solutions below
  86 
  87 % 1. Compute the variance per point
  88 %
  89 % Arguments:
  90 %  Y: matrix where the columns are the predictions for a single
  91 %     version of the data set
  92 function V = variance_per_point(Y)
  93 % ...
  94 
  95 % 2. Compute the squared bias per point
  96 %
  97 % Arguments:
  98 %  Y: see 1
  99 %  F: a column vector of the true function
 100 function B2 = squared_bias_per_point(Y, F)
 101 % ...
 102 
 103 % 3. Compute the squared error per point.
 104 %
 105 % Arguments:
 106 %  Y: see 1
 107 %  F: see 2
 108 function E = squared_error_per_point(Y, F)
 109 % ...

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] (2010-12-03 11:00:13, 73.5 KB) [[attachment:FDA and LSR.pdf]]
  • [get | view] (2010-11-18 14:22:56, 1407.4 KB) [[attachment:clustering_slides.pdf]]
  • [get | view] (2011-01-25 08:30:15, 499.5 KB) [[attachment:efficient_backprop.pdf]]
  • [get | view] (2010-10-28 10:55:22, 123.9 KB) [[attachment:full_sheet01.pdf]]
  • [get | view] (2010-11-04 14:04:25, 118.5 KB) [[attachment:full_sheet02.pdf]]
  • [get | view] (2010-11-11 10:52:24, 60.2 KB) [[attachment:full_sheet03.pdf]]
  • [get | view] (2010-11-18 15:23:26, 97.5 KB) [[attachment:full_sheet04.pdf]]
  • [get | view] (2010-11-25 15:14:31, 92.0 KB) [[attachment:full_sheet05.pdf]]
  • [get | view] (2010-12-02 08:50:14, 108.9 KB) [[attachment:full_sheet06.pdf]]
  • [get | view] (2010-12-09 13:30:45, 161.5 KB) [[attachment:full_sheet07.pdf]]
  • [get | view] (2010-12-16 13:33:01, 84.4 KB) [[attachment:full_sheet08.pdf]]
  • [get | view] (2011-01-05 17:12:33, 127.5 KB) [[attachment:full_sheet09.pdf]]
  • [get | view] (2011-01-12 18:08:23, 136.1 KB) [[attachment:full_sheet10.pdf]]
  • [get | view] (2011-01-19 19:10:49, 143.6 KB) [[attachment:full_sheet11.pdf]]
  • [get | view] (2011-01-26 11:33:22, 136.7 KB) [[attachment:full_sheet12.pdf]]
  • [get | view] (2011-02-02 20:35:04, 134.9 KB) [[attachment:full_sheet13.pdf]]
  • [get | view] (2010-11-25 15:08:04, 1505.5 KB) [[attachment:ica_slides.pdf]]
  • [get | view] (2011-01-08 10:49:41, 504.7 KB) [[attachment:kernel_intro.pdf]]
  • [get | view] (2011-01-25 08:30:11, 1221.6 KB) [[attachment:lect_kernels.pdf]]
  • [get | view] (2011-02-13 09:07:55, 1391.7 KB) [[attachment:rde-tutorial.pdf]]
  • [get | view] (2010-11-18 15:23:34, 1.4 KB) [[attachment:sheet04.m]]
  • [get | view] (2010-11-25 15:14:53, 0.8 KB) [[attachment:sheet05.m]]
  • [get | view] (2010-11-30 21:18:03, 1.1 KB) [[attachment:sheet06.m]]
  • [get | view] (2010-12-09 13:31:05, 1.9 KB) [[attachment:sheet07.m]]
  • [get | view] (2010-12-16 13:33:09, 2.7 KB) [[attachment:sheet08.m]]
  • [get | view] (2011-01-12 17:38:01, 2.0 KB) [[attachment:sheet10.m]]
  • [get | view] (2011-01-12 17:38:14, 4.8 KB) [[attachment:sheet10_data.m]]
  • [get | view] (2011-01-19 19:11:02, 1.5 KB) [[attachment:sheet11.m]]
  • [get | view] (2011-01-26 11:33:28, 1.7 KB) [[attachment:sheet12.m]]
 All files | Selected Files: delete move to page copy to page

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