/* Polynomial OLS Overfit (order=1 through 8 or 9) */ /* 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]; call polynols(y,x,1); call polynols(y,x,2); call polynols(y,x,3); call polynols(y,x,7); proc polynols(y,x,order); local n,x1,i,beta,xx,xs,points,fmat; /* creating X(=x1) */ n=rows(x); x1=ones(n,1); i=1; do while i<=order; x1=x1~x^i; i=i+1; endo; /* OLS beta=inv(X'X)*X'y */ beta=inv(x1'x1)*x1'y; /* display the coefficients */ print/lz "Polynomial order=" order; print "beta=" beta; print; /* graph */ points=200; xx=seqa(minc(x),(maxc(x)-minc(x))/(points-1),points-1)|maxc(x); xs=ones(points,1); i=1; do while i<=order; xs=xs~xx^i; i=i+1; endo; library pgraph; graphset; pqgwin auto; begwind; window(1,1,0); scale(-0.05*(maxc(x)-minc(x))+minc(x)|maxc(x)+0.05*(maxc(x)-minc(x)),-0.25*(maxc(y)-minc(y))+minc(y)|maxc(y)+0.25*(maxc(y)-minc(y))); setwind(1); fmat="Polynomial (order= %*.*lG )"; title(ftos(order,fmat,1,0)); xy(xx,xs*beta); setwind(1); _plctrl=-1; _pcolor=15; _psymsiz=1; xy(x,y); endwind; retp(xx~xs*beta); endp;