/* LOESS eliminating some points */ /* Notice that this method needs enough points for small alpha. */ /* Data from Holton(2003)"Value-at-Risk" */ new; cls; let data[10,2]= 1.1 2.14 1.4 2.60 2.5 1.15 2.7 1.19 3.2 1.88 3.6 1.55 4.1 2.65 4.3 3.80 4.5 4.46 4.9 6.35 ; x=data[.,1]; y=data[.,2]; points=9; /* # of points between x[k] and x[k+1] */ alpha=0.8; /* 0rows(x); errorlog "ERROR: Out of range."; retp("."); elseif eliminate==0 or eliminate==zeros(rows(eliminate),1); errorlog "No elimination:"; xall=x; yall=y; else; xall=x; yall=y; eindex=zeros(rows(x),1); eindex[eliminate]=ones(rows(eliminate),1); x=delif(x,eindex); y=delif(y,eindex); endif; n=rows(x); /* graph */ xx=zeros((points+1)*(n-1)+1,1); yy=zeros((points+1)*(n-1)+1,1); i=1; do while i<=n-1; x1=seqa(x[i],(x[i+1]-x[i])/(points+1),points+1); xx[(i-1)*(points+1)+1:i*(points+1)]=x1; i=i+1; endo; xx[rows(xx)]=x[n]; i=1; do while i<=rows(yy); yy[i]=loess1(y,x,alpha,xx[i]); i=i+1; endo; library pgraph; graphset; pqgwin auto; begwind; window(1,1,0); scale(-0.05*(maxc(xall)-minc(xall))+minc(xall)|maxc(xall)+0.05*(maxc(xall)-minc(xall)),-0.25*(maxc(yy)-minc(yy))+minc(yy)|maxc(yy)+0.25*(maxc(yy)-minc(yy))); setwind(1); title("LOESS"); xy(xx,yy); setwind(1); _plctrl=-1; _pcolor=15; _psymsiz=1; xy(xall,yall); endwind; retp(xx~yy); endp; proc loess1(y,x,alpha,x0); local z,n,k,xs,h,w,i,u,x1,P,beta,y0; /* sort them out in terms of x */ z=x~y; z=sortc(z,1); x=z[.,1]; y=z[.,2]; n=rows(x); /* main */ k=floor(alpha*n); xs=sortc(abs(x-x0),1); h=xs[k]; w=zeros(n,1); i=1; do while i<=n; u=abs(x[i]-x0)/h; if u>=0 and u<=1; w[i]=(1-u^3)^3; else; w[i]=0; endif; i=i+1; endo; x1=ones(n,1)~x; P=zeros(rows(x),rows(x)); P=diagrv(P,w); beta=inv(x1'*P*x1)*(x1'*P*y); y0=beta[1]+beta[2]*x0; retp(y0); endp;