% Copyright 1996 Thomas Mc Avoy and Dong Dong
 function [f]=local1(lmd,data,span)

% function to carry out locally weighted regression. A linear model is
% used and data are weighted in a cubic manner based on how close they
% are to point being modeled.
% function [f]=local(lmd,data,span)
% call routine with data already sorted by distance down curve, lambda.
	[m,n]=size(data);
	h=ones(span+1,2);
	for i=1:m
		if i<=round(span/2)
			K=[lmd(1:span+1)'-h(:,1)*lmd(i),h(:,2)];
			y=data(1:span+1,:);
		elseif i>=round(span/2)&i<=m-round(span/2)
			K=[lmd(i-span/2:i+span/2)'-h(:,1)*lmd(i),h(:,2)];
			y=data(i-span/2:i+span/2,:);
		else
			K=[lmd(m-span:m)'-h(:,1)*lmd(i),h(:,2)];
			y=data(m-span:m,:);
		end
		w=max(abs(K(:,1)));
		scldist(1:span+1)=abs(K(:,1))/w;
		weight(1:span+1)=(1-scldist(1:span+1).^3).^3;
	    ww=diag(weight.^2);
		beta=inv(K'*ww*K)*K'*ww*y;
		f(i,:)=beta(2,:);
	end
		
						
			
		
			
	
	
