% Modified from Example 10.6 % % epage 415 % % 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; % Generate some noisy data. x = linspace(0, 4 * pi,100); y = sin(x) + 0.75*randn(size(x)); % Create an inline function to evaluate the weights. mystrg='(2*pi*h^2)^(-1/2)*exp(-0.5*((x - mu)/h).^2)'; wfun = inline(mystrg); % Set up the space to store the estimated values. % We will get the estimate at all values of x. yhatnw = zeros(size(x)); n = length(x); h_array = [0.1, 0.2, 0.5, 0.75, 1.0, 1.3, 2.0]; fh=figure; plot( x, y, 'ko', 'MarkerFaceColor', 'Black' ); hold on; ln_colors = 'rbgcmk'; nc = length(ln_colors); ph = []; tt = {}; for hi=1:length(h_array), % Set the window width. h = h_array(hi); % find smooth at each value in x for i = 1:n w = wfun(h,x(i),x); yhatnw(i) = sum(w.*y)/sum(w); end figure(fh); ph(end+1) = plot( x, yhatnw, ['-',ln_colors(mod(hi,nc)+1)]); tt{end+1} = ['h = ',num2str(h)]; end axis tight; grid on; xlabel( 'independent variable' ); ylabel( 'dependent variable' ); title( 'the Nadaraya-Watson Kernel Estimator for various h' ); legend( ph, tt, 'location', 'northeast' ); saveas( gcf, ['../../WriteUp/Graphics/Chapter10/prob_10_9_kernels_vs_h'], 'epsc' );