% % Written by: % -- % John L. Weatherwax 2008-02-20 % % email: wax@alum.mit.edu % % Please send comments and especially bug reports to the % above email address. % %----- clear all; close all; drawnow; clc; addpath( '../../Code/eda_toolbox' ); randn('seed',0); M=200; x = linspace( 0, +1, M ); % our dependent function "y": % % -- a function that is IN the space of functions we are able to approximate i.e. a polynomial %y_truth = 4*x.^3 + 6*x.^2 - 1; % -- a function that is NOT in the space of functions we are able to approximate y_truth = 4*x.^5 + 6*x.^2 - 1 + (1./(x-1.1)); %y_truth = 4*x.^5 + 6*x.^2 - 1 + sin( 2*pi*x ); % create some noise to add to "y": sigma = 1.5; noise_eps = sigma*randn(size(y_truth)); y = y_truth + noise_eps; % plot the original and noised data: fd=figure; ht = plot( x, y_truth, '-g', 'LineWidth', 2 ); hold on; %grid on; ho = plot( x, y, 'xr' ); line_styles={'-',':','--'}; line_colors='brc'; data_plt_h = []; % try three polynomial models to "explain" this data: % for ii=1:3, % assume we *know* the dimension of the polynomial to fit: [pcoef,s] = polyfit(x,y,ii); % evaluate this polynomial model: yp = polyval(pcoef,x); % plot this polynomial model: figure(fd); tph=plot( x, yp, ['-',line_colors(ii)] ); data_plt_h(end+1) = tph; xlabel( 'predictor x' ); ylabel( 'residual' ); title( ['residual dependence plot for a polynomial model of degree', num2str(ii)] ); %axis( [0 1 -2.5 2.5] ); end % finally construct a loess plot: alpha = 0.5; deg = 2; yl = loess( x, y, x, alpha, deg ); figure(fd); data_plt_h(end+1) = plot( x, yl, '-k', 'LineWidth', 1 ); figure(fd); legend([ht,data_plt_h], {'truth', 'poly deg 1','poly deg 2','poly deg 3','loess plot'}, 'location', 'northwest' ); saveas( gcf, ['../../WriteUp/Graphics/Chapter7/prob_7_1_M_',num2str(M)], 'epsc' );