% this script loads the x/y used to produce figures in lecture 3: load polyfitdata.mat ms=[1 3 5 10]; % this is a "fine grid" set to allow us to plot the estimated functions pts=-4.5:.01:4.5; % note: it's important to have the same range to prevent normalization % screwups due to scaling in degexpand.m % run leave-one-out CV for m=1:length(ms) % fit to the whole training set xx=degexpand(x,ms(m)); wPoly{m}=pinv(xx)*y; % empirical loss: etr(m)=mean((y-xx*wPoly{m}).^2); % plot the model figure; plot(x,y,'bo','MarkerSize',10);hold on; axis([-5 5 -12 6]); % to make figures look exactly like those on the slides plot(pts,degexpand(pts,ms(m))*wPoly{m},'r'); % now run leave-one-out CV for i=1:size(x,2) % exclude i-th example xtr=xx(setdiff(1:10,i),:); ytr=y(setdiff(1:10,i)); % fit to the remaining data wCV=pinv(xtr)*ytr; % test on the i-th example xts=xx(i,:); yts=y(i); err(m,i)=(yts-xts*wCV)^2; end fprintf(2,'%d: empirical %.4f, LOO %.4f\n',ms(m),etr(m),mean(err(m,:))); end