% 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); % Set the window width. h = 1; % get diff array (the case i=1 is a bit singular) ... df = [h, diff(x)]; % find smooth at each value in x for i = 1:n w = wfun(h,x(i),x); % <- the kernel centered at the point x(i) ... evaluate at ALL x's yhatpc(i) = sum(df.*w.*y)/h; end plot( x, y, 'ok' ); hold on; plot( x, yhatpc, '-rx' ); axis tight; xlabel( 'independent variable' ); ylabel( 'dependent variable' ); title( 'the Priesley-Chao Kernel Estimator' ); saveas( gcf, ['../../WriteUp/Graphics/Chapter10/prob_10_8_pc_kernel'], 'epsc' );