% % Examples 9.15 and 9.17 % % epage 432 % % 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; clc; addpath('../../Code/CSTool'); randn('seed',0); x = linspace( 0, +1, 1000 ); % our spatially dependent variance: %var = ones(size(x)); % <- a constant variance of ONE var = 20*abs(x)+1; % generate noise with this variance: noise_eps = zeros(1,length(x)); for ii=1:length(x), noise_eps(ii) = normrnd(0,sqrt(var(ii))); end % our dependent function "y": y_truth = 4*x.^3 + 6*x.^2 - 1; y = y_truth + noise_eps; %fr=figure; hold on; line_styles={'-',':','--'}; line_colors='cmk'; data_plt_h = []; res_plt_h = []; 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 the residuals v.s. the fitted value yp: res = y-yp; figure; hist( res, 20 ); xlabel( 'predictor x' ); ylabel( 'residual' ); title( ['histogram residual dependence plot for a polynomial model of degree', num2str(ii)] ); figure; boxplot( res ); xlabel( 'predictor x' ); ylabel( 'residual' ); title( ['residual boxplot for a polynomial model of degree', num2str(ii)] ); saveas( gcf, ['../../WriteUp/Graphics/Chapter10/prob_10_3_boxplot_poly_d_',num2str(ii)], 'epsc' ); figure; qqplot( res ); title( ['a qq plot for the polynomial model of degree', num2str(ii)] ); saveas( gcf, ['../../WriteUp/Graphics/Chapter10/prob_10_3_qq_poly_d_',num2str(ii)], 'epsc' ); end clear functions;