/* Custom-made function Overfit eliminating some points */ /* 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]; eliminate={1,2}; /* eliminate these points */ /* sort them out in terms of x */ z=x~y; z=sortc(z,1); x=z[.,1]; y=z[.,2]; /* elimination */ if maxc(eliminate)>rows(x); errorlog "ERROR: Out of range."; 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); start={0,0,0}; fn function(b)=b[1]+exp(b[2]+b[3]*x); /* You can set this. */ fn fct(b)=(y-function(b))'(y-function(b)); screen off; { b,f,g,ret }=QNewton( &fct,start ); screen on; print "b=" b; points=1000; xx=seqa(minc(x),(maxc(x)-minc(x))/(points-1),points-1)|maxc(x); /* graph */ 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(yall)-minc(yall))+minc(yall)|maxc(yall)+0.25*(maxc(yall)-minc(yall))); setwind(1); title("nonlinear function"); _plctrl=-1; _pcolor=15; _psymsiz=1; xy(xall,yall); setwind(1); _plctrl=0; _pcolor=10; x=xx; /* switch data to x's sequence to draw a curve */ xy(xx,function(b)); endwind;