Local linear regression (MATLAB) - converting a for loop to matrices problem
===

Thanks for the help with my first question. I now have a new problem. This is still writing code for the project that my professor is thinking of, but now this involves writing code for local linear regression and Nadaraya-Watson regression. The first code that I wrote uses a loop for the NW estimator and the local linear regression estimator. 

    % This code simulates the local linear estimator and NW kernel.

    % Generating noisy data. (I use the same as the example in the Lo paper.)
     x=linspace(0,4*pi,100);
     y=sin(x)+0.5*randn(size(x));

    % Set up the space to store the estimates.
     yhatnw=zeros(size(x));
     yhatll=zeros(size(x));
     n=length(x);

    % Set up the bandwidth.
     hx=median(abs(x-median(x)))/0.6745*(4/3/n)^0.2;
     hy=median(abs(y-median(y)))/0.6745*(4/3/n)^0.2;
     h=sqrt(hy*hx);

    % Find smooth at each value of x.
     for i=1:n
        w=wfun(h,x(i),x);
        xc=x-x(i);
        s2=mean(xc.^2.*w);
        s1=mean(xc.*w);
        s0=mean(w);
        yhatnw(i)=sum(w.*y)/sum(w); % Nadaraya-Watson kernel
        yhatll(i)=sum(((s2-s1*xc).*w.*y)/(s2*s0-s1^2))/n; % local linear estimator
     end

    plot(x,y,'.',x,yhatnw,'-',x,yhatll,':')

The corresponding weighting function is:

    function w=wfun(h,mu,x);
    w=exp((-1/2)*(((x-mu)/h).^2))/sqrt(2*pi*h^2);

I tried to convert this from a loop to matrix, and it works for the NW estimator (since I am getting the same graph), but not for the local linear estimator (the graph with the loop is better than that of the matrix). The relevant code is here:

    % Evaluating the weighting matrix.
     xi=ones(n,1)*x; 
     data=x'*ones(1,n);
     w=wfun(h,xi,data);

    % % Local linear regression
     xc=data-xi;
     s2=mean(xc.^2.*w);
     s1=mean(xc.*w);
     s0=mean(w);
     s2i=ones(n,1)*s2;
     s1i=ones(n,1)*s1;
     s0i=ones(n,1)*s0;
     yhatll=sum(((s2i-s1i*xc).*w.*yi)'./(s2i*s0i-(s1i^2)))/n;

I think there is something wrong with the way I am formulating the components of the local linear regression, but I am unsure, so I'd like to ask all of you. Thanks!