Rev 40674 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
CC Modified from the SMART package by J.H. Friedman, 10/10/84C Main change is to add spline smoothing modified from BRUTO,C calling code written for smooth.spline in S.CC B.D. Ripley (ripley@stats.ox.ac.uk) 1994-7.CCsubroutine smart(m,mu,p,q,n, w,x,y,ww,smod,nsmod,& sp,nsp,dp,ndp,edf)integer m,mu,p,q,n, nsmod, nsp,ndpdouble precision x(p,n),y(q,n),w(n),ww(q),smod(nsmod),& sp(nsp),edf(m),dp(ndp)smod(1)=msmod(2)=psmod(3)=qsmod(4)=ncall smart1(m,mu,p,q,n, w,x,y,ww, smod(6),smod(q+6),& smod(q+7),smod(q+7+p*m),smod(q+7+m*(p+q)),& smod(q+7+m*(p+q+n)),smod(q+7+m*(p+q+2*n)),& sp,sp(q*n+1),sp(n*(q+15)+1),sp(n*(q+15)+q+1),& dp,smod(5),edf)returnendsubroutine smart1(m,mu,p,q,n, w,x,y,ww, yb,ys,& a,b,f,& t,asr,& r,sc,bt,g,& dp,flm,edf)integer m,mu,p,q,ndouble precision w(n),x(p,n),y(*),ww(q), yb(q), ysdouble precision a(p,m),b(q,m),f(n,m),t(n,m), asrdouble precision r(q,n),sc(n,15),bt(q),g(p,3)double precision dp(*), flm,edf(m)C ^^^ really (ndb) of smart(.)integer i,j,l, lmdouble precision sw,sc Common Varsdouble precision span,alpha,biginteger ifl,lfcommon /pprpar/ ifl,lf,span,alpha,bigdouble precision conv, cutmin,fdel,cjepsinteger maxit,mitone, mitcjcommon /pprz01/ conv,maxit,mitone,cutmin,fdel,cjeps,mitcjsw=0d0do 161 j=1,n161 sw=sw+w(j)do 201 j=1,ndo 201 i=1,qr(i,j)=y(q*(j-1)+i)201 continuedo 241 i=1,qs=0d0do 251 j=1,ns=s+w(j)*r(i,j)251 continueyb(i)=s/sw241 continuec yb is vector of meansdo 261 j=1,ndo 261 i=1,q261 r(i,j)=r(i,j)-yb(i)ys=0.do 281 i=1,qs=0.do 291 j=1,n291 s=s+w(j)*r(i,j)**2ys=ys+ww(i)*s/sw281 continueif(ys .gt. 0d0) goto 311c ys is the overall standard deviation -- quit if zeroreturn311 continueys=sqrt(ys)s=1./ysdo 331 j=1,ndo 331 i=1,q331 r(i,j)=r(i,j)*sc r is now standardized residualsc subfit adds up to m terms one at time; lm is the number fitted.call subfit(m,p,q,n,w,sw,x,r,ww,lm,a,b,f,t,asr,sc,bt,g,dp,edf)if(lf.le.0) go to 9999call fulfit(lm,lf,p,q,n,w,sw,x,r,ww,a,b,f,t,asr,sc,bt,g,dp,edf)C REPEAT371 continuedo 381 l=1,lmsc(l,1)=l+.1s=0d0do 391 i=1,q391 s=s+ww(i)*abs(b(i,l))sc(l,2)=-s381 continuecall sort(sc(1,2),sc,1,lm)do 461 j=1,ndo 461 i=1,q461 r(i,j)=y(q*(j-1)+i)do 521 i=1,qdo 531 j=1,nr(i,j)=r(i,j)-yb(i)s=0.do 541 l=1,lm541 s=s+b(i,l)*f(j,l)r(i,j)=r(i,j)/ys-s531 continue521 continueif(lm.le.mu) goto 9999c back to integer:l=sc(lm,1)asr=0d0do 561 j=1,ndo 561 i=1,qr(i,j)=r(i,j)+b(i,l)*f(j,l)561 asr=asr+w(j)*ww(i)*r(i,j)**2asr=asr/swif(l .ge. lm) goto 591do 601 i=1,p601 a(i,l)=a(i,lm)do 611 i=1,q611 b(i,l)=b(i,lm)do 621 j=1,nf(j,l)=f(j,lm)621 t(j,l)=t(j,lm)591 continuelm=lm-1call fulfit(lm,lf,p,q,n,w,sw,x,r,ww,a,b,f,t,asr,sc,bt,g,dp,edf)goto 371C END REPEAT9999 continueflm=lmreturnendsubroutine subfit(m,p,q,n,w,sw,x,r,ww,lm,a,b,f,t,asr,sc,& bt,g,dp,edf)c Argsinteger m,p,q,n, lmdouble precision w(n),sw, x(p,n),r(q,n),ww(q),a(p,m),b(q,m),& f(n,m), t(n,m), asr, sc(n,15), bt(q), g(p,3), edf(m)double precision dp(*)c Varinteger i,j,l, iflsvdouble precision asroldc Common Varsdouble precision span,alpha,biginteger ifl,lfcommon /pprpar/ ifl,lf,span,alpha,bigdouble precision conv, cutmin,fdel,cjepsinteger maxit,mitone, mitcjcommon /pprz01/ conv,maxit,mitone,cutmin,fdel,cjeps,mitcjasr=biglm=0do 100 l=1,mcall rchkusr()lm=lm+1asrold=asrcall newb(lm,q,ww,b)call onetrm(0,p,q,n,w,sw,x,r,ww,a(1,lm),b(1,lm),& f(1,lm),t(1,lm),asr,sc,g,dp,edf)do 10 j=1,ndo 10 i=1,q10 r(i,j)=r(i,j)-b(i,lm)*f(j,lm)if(lm.eq.1) goto 100if(lf.gt.0) thenif(lm.eq.m) returniflsv=iflifl=0call fulfit(lm,1,p,q,n,w,sw,x,r,ww,a,b,f,t,asr,sc,bt,& g,dp, edf)ifl=iflsvendifif(asr.le.0d0.or.(asrold-asr)/asrold.lt.conv) return100 continuereturnendsubroutine fulfit(lm,lbf,p,q,n,w,sw,x,r,ww,a,b,f,t,& asr,sc,bt,g,dp,edf)c Argsinteger lm,lbf,p,q,ndouble precision w(n),sw,x(p,n),r(q,n),ww(q),a(p,lm),b(q,lm),& f(n,lm),t(n,lm),asr(1+lm), sc(n,15),bt(q),g(p,3), edf(lm)double precision dp(*)c Vardouble precision asri, fsv, asroldinteger i,j,iter,lp,isvc Common Varsdouble precision span,alpha,biginteger ifl,lfcommon /pprpar/ ifl,lf,span,alpha,bigdouble precision conv, cutmin,fdel,cjepsinteger maxit,mitone, mitcjcommon /pprz01/ conv,maxit,mitone,cutmin,fdel,cjeps,mitcjif(lbf.le.0) returnasri=asr(1)fsv=cutminisv=mitoneif(lbf .lt. 3) thencutmin=1d0mitone=lbf-1endifiter=0C Outer loop:1000 continueasrold=asriiter=iter+1do 100 lp=1,lmdo 1 i=1,q1 bt(i)=b(i,lp)do 2 i=1,p2 g(i,3)=a(i,lp)do 3 j=1,ndo 3 i=1,q3 r(i,j)=r(i,j)+bt(i)*f(j,lp)call onetrm(1,p,q,n,w,sw,x,r,ww,g(1,3),bt,sc(1,14),sc(1,15),& asri,sc,g,dp,edf(lp))if(asri .lt. asrold) thendo 4 i=1,q4 b(i,lp)=bt(i)do 5 i=1,p5 a(i,lp)=g(i,3)do 6 j=1,nf(j,lp)=sc(j,14)t(j,lp)=sc(j,15)6 continueelseasri=asroldendifdo 8 j=1,ndo 8 i=1,q8 r(i,j)=r(i,j)-b(i,lp)*f(j,lp)100 continueif((iter .le. maxit) .and. ((asri .gt. 0d0)& .and. ((asrold-asri)/asrold .ge. conv))) goto 1000cutmin=fsvmitone=isvif(ifl .gt. 0) thenasr(1+lm) = asriasr(1) = asriendifreturnendsubroutine onetrm(jfl,p,q,n,w,sw,x,y,ww,a,b,f,t,asr,& sc,g,dp,edf)c Argsinteger jfl,p,q,ndouble precision w(n),sw, x(p,n),y(q,n),ww(q),a(p),b(q),f(n),t(n),& asr, sc(n,13),g(p,2), edfdouble precision dp(*)c Vardouble precision asrold,sinteger i,j,iterc Common Varsdouble precision span,alpha,biginteger ifl,lfcommon /pprpar/ ifl,lf,span,alpha,bigdouble precision conv, cutmin,fdel,cjepsinteger maxit,mitone, mitcjcommon /pprz01/ conv,maxit,mitone,cutmin,fdel,cjeps,mitcjiter=0asr=bigC REPEAT1000 continueiter=iter+1asrold=asrdo 11 j=1,ns=0d0do 21 i=1,q21 s=s+ww(i)*b(i)*y(i,j)sc(j,13)=s11 continuecall oneone(max0(jfl,iter-1),p,n,w,sw,sc(1,13),x,a,f,t,& asr,sc,g,dp,edf)do 31 i=1,qs=0d0do 41 j=1,n41 s=s+w(j)*y(i,j)*f(j)b(i)=s/sw31 continueasr=0d0do 51 i=1,qs=0d0do 61 j=1,n61 s=s+w(j)*(y(i,j)-b(i)*f(j))**2asr=asr+ww(i)*s/sw51 continueif((q .ne. 1) .and. (iter .le. maxit) .and. (asr .gt. 0d0)& .and. (asrold-asr)/asrold .ge. conv) goto 1000returnendsubroutine oneone(ist,p,n, w,sw,y,x,a,f,t,asr,sc,g,dp,edf)c Argsinteger ist,p,ndouble precision w(n),sw,y(n),x(p,n),a(p),f(n),t(n),asr,& sc(n,12), g(p,2), edf, dp(*)c Varinteger i,j,k,iterdouble precision sml, s,v,cut,asroldc Common Varsdouble precision span,alpha,biginteger ifl,lfcommon /pprpar/ ifl,lf,span,alpha,bigdouble precision conv, cutmin,fdel,cjepsinteger maxit,mitone, mitcjcommon /pprz01/ conv,maxit,mitone,cutmin,fdel,cjeps,mitcjsml=1d0/bigif(ist .le. 0) thenif(p .le. 1) a(1)=1d0do 10 j=1,nsc(j,2)=1d010 continuecall pprdir(p,n,w,sw,y,x,sc(1,2),a,dp)endifs=0d0do 20 i=1,pg(i,1)=0d0s=s+a(i)**220 continues=1d0/sqrt(s)do 30 i=1,pa(i)=a(i)*s30 continueiter=0asr=bigcut=1d0C REPEAT -----------------------------100 continueiter=iter+1asrold=asrC REPEAT [inner loop] -----60 continues=0d0do 70 i=1,pg(i,2)=a(i)+g(i,1)s=s+g(i,2)**270 continues=1./sqrt(s)do 80 i=1,pg(i,2)=g(i,2)*s80 continuedo 90 j=1,nsc(j,1)=j+.1s=0.do 91 i=1,ps=s+g(i,2)*x(i,j)91 continuesc(j,11)=s90 continuecall sort(sc(1,11),sc,1,n)do 110 j=1,nk=sc(j,1)sc(j,2)=y(k)sc(j,3)=max(w(k),sml)110 continuecall supsmu(n,sc(1,11),sc(1,2),sc(1,3),1,span,alpha,& sc(1,12),sc(1,4), edf)s=0d0do 120 j=1,ns=s+sc(j,3)*(sc(j,2)-sc(j,12))**2120 continues=s/swif(s .lt. asr) goto 140cut=cut*0.5if(cut.lt.cutmin) goto 199do 150 i=1,pg(i,1)=g(i,1)*cut150 continuego to 60C --------140 continueasr=scut=1d0do 160 i=1,pa(i)=g(i,2)160 continuedo 170 j=1,nk=sc(j,1)t(k)=sc(j,11)f(k)=sc(j,12)170 continueif(asr.le.0d0.or.(asrold-asr)/asrold.lt.conv) goto 199if(iter.gt.mitone.or.p.le.1) goto 199call pprder(n,sc(1,11),sc(1,12),sc(1,3),fdel,sc(1,4),sc(1,5))do 180 j=1,nk=sc(j,1)sc(j,5)=y(j)-f(j)sc(k,6)=sc(j,4)180 continuecall pprdir(p,n,w,sw,sc(1,5),x,sc(1,6),g,dp)goto 100c--------------199 continuec--------------s=0d0v=sdo 210 j=1,ns=s+w(j)*f(j)210 continues=s/swdo 220 j=1,nf(j)=f(j)-sv=v+w(j)*f(j)**2220 continueif(v .gt. 0d0) thenv=1d0/sqrt(v/sw)do 230 j=1,n230 f(j)=f(j)*vendifreturnendsubroutine pprdir(p,n,w,sw,r,x,d,e,g)integer p,ndouble precision w(n),sw,r(n),x(p,n),d(n),e(p), g(*)double precision sinteger i,j,k,l,m1,m2double precision conv, cutmin,fdel,cjepsinteger maxit,mitone, mitcjcommon /pprz01/ conv,maxit,mitone,cutmin,fdel,cjeps,mitcjdo 10 i=1,ps=0d0do 15 j=1,ns=s+w(j)*d(j)*x(i,j)15 continuee(i)=s/sw10 continuek=0m1=p*(p+1)/2m2=m1+pdo 20 j=1,ps=0d0do 22 l=1,ns=s+w(l)*r(l)*(d(l)*x(j,l)-e(j))22 continueg(m1+j)=s/swdo 25 i=1,js=0d0do 27 l=1,ns=s+w(l)*(d(l)*x(i,l)-e(i))*(d(l)*x(j,l)-e(j))27 continuek=k+1g(k)=s/sw25 continue20 continuecall ppconj(p,g,g(m1+1),g(m2+1),cjeps,mitcj,g(m2+p+1))do 30 i=1,pe(i)=g(m2+i)30 continuereturnendsubroutine ppconj(p,g,c,x,eps,maxit,sc)integer p,maxitdouble precision g(*),c(p),x(p),eps,sc(p,4)integer i,j,im1,iter,nitdouble precision beta,h,s,alpha,tdo 1 i=1,px(i)=0d0sc(i,2)=0d01 continuenit=0C REPEAT11321 continuenit=nit+1h=0d0beta=0d0do 11331 i=1,psc(i,4)=x(i)s=g(i*(i-1)/2+i)*x(i)im1=i-1j=1goto 1134311341 j=j+111343 if((j).gt.(im1)) goto 11342s=s+g(i*(i-1)/2+j)*x(j)goto 1134111342 continuej=i+1goto 1135311351 j=j+111353 if((j).gt.(p)) goto 11352s=s+g(j*(j-1)/2+i)*x(j)goto 1135111352 continuesc(i,1)=s-c(i)h=h+sc(i,1)**211331 continueif(h.le.0d0) goto 11322do 11361 iter=1,pdo 11371 i=1,psc(i,2)=beta*sc(i,2)-sc(i,1)11371 continuet=0d0do 11381 i=1,ps=g(i*(i-1)/2+i)*sc(i,2)im1=i-1j=1goto 1139311391 j=j+111393 if((j).gt.(im1)) goto 11392s=s+g(i*(i-1)/2+j)*sc(j,2)goto 1139111392 continuej=i+1goto 1140311401 j=j+111403 if((j).gt.(p)) goto 11402s=s+g(j*(j-1)/2+i)*sc(j,2)goto 1140111402 continuesc(i,3)=st=t+s*sc(i,2)11381 continuealpha=h/ts=0d0do 11411 i=1,px(i)=x(i)+alpha*sc(i,2)sc(i,1)=sc(i,1)+alpha*sc(i,3)s=s+sc(i,1)**211411 continueif(s.le.0d0) goto 11362beta=s/hh=s11361 continue11362 continues=0d0do 11421 i=1,ps=dmax1(s,dabs(x(i)-sc(i,4)))11421 continueif((s .ge. eps) .and. (nit .lt. maxit)) goto 1132111322 continuereturnendsubroutine pprder (n,x,s,w,fdel,d,sc)integer ndouble precision x(n),s(n),w(n), fdel, d(n),sc(n,3)integer i,j,bl,el,bc,ec,br,erdouble precision scale, delcc unnecessary initialization of bl el ec to keep g77 -Wall happycbl = 0el = 0ec = 0cif(x(n) .gt. x(1)) goto 11441do 11451 j=1,nd(j)=0d011451 continuereturn11441 continuei=n/4j=3*iscale=x(j)-x(i)11461 if(scale.gt.0d0) goto 11462if(j.lt.n) j=j+1if(i.gt.1) i=i-1scale=x(j)-x(i)goto 1146111462 continuedel=fdel*scale*2d0do 11471 j=1,nsc(j,1)=x(j)sc(j,2)=s(j)sc(j,3)=w(j)11471 continuecall pool (n,sc,sc(1,2),sc(1,3),del)bc=0br=bcer=br11481 continuebr=er+1er=br11491 if(er .ge. n) goto 11492if(sc(br,1) .ne. sc(er+1,1)) goto 11511er=er+1goto 1152111511 continuegoto 1149211521 continuegoto 1149111492 continueif(br .ne. 1) goto 11541bl=brel=ergoto 1148111541 continueif(bc .ne. 0) goto 11561bc=brec=erdo 11571 j=bl,eld(j)=(sc(bc,2)-sc(bl,2))/(sc(bc,1)-sc(bl,1))11571 continuegoto 1148111561 continuedo 11581 j=bc,ecd(j)=(sc(br,2)-sc(bl,2))/(sc(br,1)-sc(bl,1))11581 continueif(er .ne. n) goto 11601do 11611 j=br,erd(j)=(sc(br,2)-sc(bc,2))/(sc(br,1)-sc(bc,1))11611 continuegoto 1148211601 continuebl=bcel=ecbc=brec=ergoto 1148111482 continuereturnendsubroutine pool (n,x,y,w,del)integer ndouble precision x(n),y(n),w(n),delinteger i,bb,eb,br,er,bl,eldouble precision px, py, pwbb=0eb=bb11621 if(eb.ge.n) goto 11622bb=eb+1eb=bb11631 if(eb .ge. n) goto 11632if(x(bb) .ne. x(eb+1)) goto 11651eb=eb+1goto 1166111651 continuegoto 1163211661 continuegoto 1163111632 continueif(eb .ge. n) goto 11681if(x(eb+1)-x(eb) .ge. del) goto 11701br=eb+1er=br11711 if(er .ge. n) goto 11712if(x(er+1) .ne. x(br)) goto 11731er=er+1goto 1174111731 continuegoto 1171211741 continuegoto 1171111712 continueif(er.lt.n .and. x(er+1)-x(er).lt.x(eb+1)-x(eb)) goto 11621eb=erpw=w(bb)+w(eb)px=(x(bb)*w(bb)+x(eb)*w(eb))/pwpy=(y(bb)*w(bb)+y(eb)*w(eb))/pwdo 11751 i=bb,ebx(i)=pxy(i)=pyw(i)=pw11751 continue11701 continue11681 continue11761 continueif(bb.le.1) goto 11762if(x(bb)-x(bb-1).ge.del) goto 11762bl=bb-1el=bl11771 if(bl .le. 1) goto 11772if(x(bl-1) .ne. x(el)) goto 11791bl=bl-1goto 1180111791 continuegoto 1177211801 continuegoto 1177111772 continuebb=blpw=w(bb)+w(eb)px=(x(bb)*w(bb)+x(eb)*w(eb))/pwpy=(y(bb)*w(bb)+y(eb)*w(eb))/pwdo 11811 i=bb,ebx(i)=pxy(i)=pyw(i)=pw11811 continuegoto 1176111762 continuegoto 1162111622 continuereturnendsubroutine newb(lm,q,ww,b)integer lm, qdouble precision ww(q),b(q,lm)integer i,lm1,l,l1double precision s,t,smlc Commondouble precision span,alpha,biginteger ifl,lfcommon /pprpar/ ifl,lf,span,alpha,bigsml=1d0/bigif(q .ne. 1) goto 11831b(1,lm)=1d0return11831 continueif(lm .ne. 1) goto 11851do 11861 i=1,qb(i,lm)=i11861 continuereturn11851 continuelm1=lm-1do 11871 i=1,qb(i,lm)=0d011871 continuet=0d0do 11881 i=1,qs=0d0do 11891 l=1,lm1s=s+abs(b(i,l))11891 continueb(i,lm)=st=t+s11881 continuedo 11901 i=1,qb(i,lm)=ww(i)*(t-b(i,lm))11901 continuel1=1if(lm.gt.q) l1=lm-q+1do 11911 l=l1,lm1s=0d0t=sdo 11921 i=1,qs=s+ww(i)*b(i,lm)*b(i,l)t=t+ww(i)*b(i,l)**211921 continues=s/sqrt(t)do 11931 i=1,qb(i,lm)=b(i,lm)-s*b(i,l)11931 continue11911 continuedo 11941 i=2,qif(abs(b(i-1,lm)-b(i,lm)).gt.sml) return11941 continuedo 11951 i=1,qb(i,lm)=i11951 continuereturnendblock data bkpprc Common Varsdouble precision span,alpha,biginteger ifl,lfcommon /pprpar/ ifl,lf,span,alpha,bigdouble precision conv, cutmin,fdel,cjepsinteger maxit,mitone, mitcjcommon /pprz01/ conv,maxit,mitone,cutmin,fdel,cjeps,mitcjdouble precision df, gcvpeninteger ismethodcommon /spsmooth/ df, gcvpen, ismethoddata ifl,maxit, conv, mitone, cutmin, fdel,& span,alpha, big, cjeps, mitcj, lf& /6, 20, .005, 20, 0.1, 0.02,& 0.0, 0.0,1.0e20,0.001, 1, 2/data df, gcvpen, ismethod /4d0, 1d0, 0/endsubroutine setppr(span1, alpha1, optlevel, ism, df1, gcvpen1)c Put `parameters' into Common blocksinteger optlevel,ismdouble precision span1,alpha1, df1, gcvpen1double precision span,alpha,biginteger ifl,lfcommon /pprpar/ ifl,lf,span,alpha,bigdouble precision df, gcvpeninteger ismethodcommon /spsmooth/ df, gcvpen, ismethodspan = span1lf = optlevelalpha = alpha1ismethod = ismdf = df1gcvpen = gcvpen1returnendsubroutine fsort(mu,n,f,t,sp)cinteger mu, ndouble precision f(n,mu),t(n,mu),sp(n,2)cinteger l,j,kdo 100 l=1,mudo 10 j=1,nsp(j,1)=j+.1sp(j,2)=f(j,l)10 continuecall sort(t(1,l),sp,1,n)do 20 j=1,nk=sp(j,1)f(j,l)=sp(k,2)20 continue100 continuereturnendsubroutine pppred(np,x,smod,y,sc)integer npdouble precision x(np,*),y(np,*),smod(*), sc(*)integer p,q, place,low,high, i,j,l,m,n,+ inp,ja,jb,jf,jt,jfl,jfh,jtl,jth, mudouble precision ys, s, tm=smod(1)+.1p=smod(2)+.1q=smod(3)+.1n=smod(4)+.1mu=smod(5)+.1ys=smod(q+6)ja=q+6jb=ja+p*mjf=jb+m*qjt=jf+n*mcall fsort(mu,n,smod(jf+1),smod(jt+1),sc)do 100 inp = 1, npja=q+6jb=ja+p*mjf=jb+m*qjt=jf+n*mdo 81 i=1,qy(inp,i)=0d081 continuedo 91 l=1,mus=0d0do 12201 j=1,ps=s+smod(ja+j)*x(inp,j)12201 continueif(s .gt. smod(jt+1)) goto 12221place=1go to 1223012221 continueif(s .lt. smod(jt+n)) goto 12251place=ngo to 1223012251 continuelow=0high=n+1C WHILE12261 if(low+1.ge.high) goto 12262place=(low+high)/2t=smod(jt+place)if(s.eq.t) goto 12230if(s .lt. t) thenhigh=placeelselow=placeendifgoto 12261C END12262 continuejfl=jf+lowjfh=jf+highjtl=jt+lowjth=jt+hight=smod(jfl)+(smod(jfh)-smod(jfl))*(s-smod(jtl)) /& (smod(jth)-smod(jtl))go to 1230012230 continuet=smod(jf+place)12300 continuedo 12311 i=1,qy(inp,i)=y(inp,i)+smod(jb+i)*t12311 continueja=ja+pjb=jb+qjf=jf+njt=jt+n91 continuedo 12321 i=1,qy(inp,i)=ys*y(inp,i)+smod(i+5)12321 continue100 continuereturnendsubroutine setsmudouble precision df, gcvpeninteger ismethodcommon /spsmooth/ df, gcvpen, ismethodismethod = 0returnendsubroutine supsmu (n,x,y,w,iper,span,alpha,smo,sc,edf)cc------------------------------------------------------------------cc super smoother (Friedman, 1984).cc version 10/10/84cc coded and copywrite (c) 1984 by:cc Jerome H. Friedmanc department of statisticsc andc stanford linear accelerator centerc stanford universitycc all rights reserved.ccc input:c n : number of observations (x,y - pairs).c x(n) : ordered abscissa values.c y(n) : corresponding ordinate (response) values.c w(n) : weight for each (x,y) observation.c iper : periodic variable flag.c iper=1 => x is ordered interval variable.c iper=2 => x is a periodic variable with valuesc in the range (0.0,1.0) and period 1.0.c span : smoother span (fraction of observations in window).c span=0.0 => automatic (variable) span selection.c alpha : controls high frequency (small span) penalityc used with automatic span selection (bass tone control).c (alpha.le.0.0 or alpha.gt.10.0 => no effect.)c output:c smo(n) : smoothed ordinate (response) values.c scratch:c sc(n,7) : internal working storage.cc note:c for small samples (n < 40) or if there are substantial serialc correlations between observations close in x - value, thenc a prespecified fixed span smoother (span > 0) should bec used. reasonable span values are 0.2 to 0.4.cc------------------------------------------------------------------c Argsinteger n, iperdouble precision x(n),y(n),w(n), smo(n),sc(n,7)double precision span, alpha, edfc Vardouble precision sy,sw, a,h,f, scale,vsmlsq,resmininteger i,j, jperdouble precision spans(3), big,sml,epscommon /spans/ spans /consts/ big,sml,epsdouble precision df, gcvpeninteger ismethodcommon /spsmooth/ df, gcvpen, ismethodif (x(n).gt.x(1)) go to 30sy=0d0sw=sydo 10 j=1,nsy=sy+w(j)*y(j)sw=sw+w(j)10 continuea=0d0if (sw.gt.0d0) a=sy/swdo 20 j=1,nsmo(j)=a20 continuereturn30 continueC change byif (ismethod .ne. 0) thencall spline(n, x, y, w, smo, edf)elsei=n/4j=3*iscale=x(j)-x(i)40 if (scale.gt.0d0) go to 50if (j.lt.n) j=j+1if (i.gt.1) i=i-1scale=x(j)-x(i)go to 4050 vsmlsq=(eps*scale)**2jper=iperif (iper.eq.2.and.(x(1).lt.0d0.or.x(n).gt.1d0)) jper=1if (jper.lt.1.or.jper.gt.2) jper=1if (span.le.0d0) go to 60call smooth (n,x,y,w,span,jper,vsmlsq,smo,sc)return60 do 70 i=1,3call smooth(n,x,y,w,spans(i),jper,vsmlsq,& sc(1,2*i-1),sc(1,7))call smooth(n,x,sc(1,7),w,spans(2),-jper,vsmlsq,& sc(1,2*i),h)70 continuedo 90 j=1,nresmin=bigdo 80 i=1,3if (sc(j,2*i).ge.resmin) go to 80resmin=sc(j,2*i)sc(j,7)=spans(i)80 continueif (alpha.gt.0d0.and.alpha.le.10d0 .and.& resmin.lt.sc(j,6).and.resmin.gt.0d0)& sc(j,7)= sc(j,7)+(spans(3)-sc(j,7)) *& max(sml,resmin/sc(j,6))**(10d0-alpha)90 continuecall smooth (n,x,sc(1,7),w,spans(2),-jper,vsmlsq,sc(1,2),h)do 110 j=1,nif (sc(j,2).le.spans(1)) sc(j,2)=spans(1)if (sc(j,2).ge.spans(3)) sc(j,2)=spans(3)f=sc(j,2)-spans(2)if (f.ge.0d0) go to 100f=-f/(spans(2)-spans(1))sc(j,4)=(1d0-f)*sc(j,3)+f*sc(j,1)go to 110100 f=f/(spans(3)-spans(2))sc(j,4)=(1d0-f)*sc(j,3)+f*sc(j,5)110 continuecall smooth (n,x,sc(1,4),w,spans(1),-jper,vsmlsq,smo,h)edf = 0endifreturnendsubroutine smooth (n,x,y,w,span,iper,vsmlsq,smo,acvr)c Argsinteger n, iperdouble precision x(n),y(n),w(n), span,vsmlsq, smo(n),acvr(n)c Varinteger i,j, in,out, jper,ibw,it, j0double precision xm,ym,var,cvar, fbw,fbo,xti,xto,tmp, a,h,sy,wtxm=0d0ym=xmvar=ymcvar=varfbw=cvarjper=iabs(iper)ibw=0.5*span*n+0.5if (ibw.lt.2) ibw=2it=2*ibw+1do 20 i=1,itj=iif (jper.eq.2) j=i-ibw-1if (j.ge.1) xti=x(j)if (j.ge.1) go to 10j=n+jxti=x(j)-1d010 wt=w(j)fbo=fbwfbw=fbw+wtif (fbw.gt.0d0) xm=(fbo*xm+wt*xti)/fbwif (fbw.gt.0d0) ym=(fbo*ym+wt*y(j))/fbwtmp=0d0if (fbo.gt.0d0) tmp=fbw*wt*(xti-xm)/fbovar=var+tmp*(xti-xm)cvar=cvar+tmp*(y(j)-ym)20 continuedo 80 j=1,nout=j-ibw-1in=j+ibwif ((jper.ne.2).and.(out.lt.1.or.in.gt.n)) go to 60if (out.ge.1) go to 30out=n+outxto=x(out)-1d0xti=x(in)go to 5030 if (in.le.n) go to 40in=in-nxti=x(in)+1d0xto=x(out)go to 5040 xto=x(out)xti=x(in)50 wt=w(out)fbo=fbwfbw=fbw-wttmp=0d0if (fbw.gt.0d0) tmp=fbo*wt*(xto-xm)/fbwvar=var-tmp*(xto-xm)cvar=cvar-tmp*(y(out)-ym)if (fbw.gt.0d0) xm=(fbo*xm-wt*xto)/fbwif (fbw.gt.0d0) ym=(fbo*ym-wt*y(out))/fbwwt=w(in)fbo=fbwfbw=fbw+wtif (fbw.gt.0d0) xm=(fbo*xm+wt*xti)/fbwif (fbw.gt.0d0) ym=(fbo*ym+wt*y(in))/fbwtmp=0d0if (fbo.gt.0d0) tmp=fbw*wt*(xti-xm)/fbovar=var+tmp*(xti-xm)cvar=cvar+tmp*(y(in)-ym)60 a=0d0if (var.gt.vsmlsq) a=cvar/varsmo(j)=a*(x(j)-xm)+ymif (iper.le.0) go to 80h=0d0if (fbw.gt.0d0) h=1d0/fbwif (var.gt.vsmlsq) h=h+(x(j)-xm)**2/varacvr(j)=0d0a=1d0-w(j)*hif (a.le.0d0) go to 70acvr(j)=abs(y(j)-smo(j))/ago to 8070 if (j.le.1) go to 80acvr(j)=acvr(j-1)80 continuej=1c--90 j0=jsy=smo(j)*w(j)fbw=w(j)if (j.ge.n) go to 110100 if (x(j+1).gt.x(j)) go to 110j=j+1sy=sy+w(j)*smo(j)fbw=fbw+w(j)if (j.lt.n) go to 100110 if (j.le.j0) go to 130a=0d0if (fbw.gt.0d0) a=sy/fbwdo 120 i=j0,jsmo(i)=a120 continue130 j=j+1if (j.le.n) go to 90returnendblock data bksupsmudouble precision spans(3), big,sml,epscommon /spans/ spans /consts/ big,sml,epsdata spans, big,sml,eps /0.05,0.2,0.5, 1.0e20,1.0e-7,1.0e-3/endc---------------------------------------------------------------cc this sets the compile time (default) values for variousc internal parameters :cc spans : span values for the three running linear smoothers.c spans(1) : tweeter span.c spans(2) : midrange span.c spans(3) : woofer span.c (these span values should be changed only with care.)c big : a large representable floating point number.c sml : a small number. should be set so that (sml)**(10.0) doesc not cause floating point underflow.c eps : used to numerically stabilize slope calculations forc running linear fits.cc these parameter values can be changed by declaring thec relevant labeled common in the main program and resettingc them with executable statements.cc-----------------------------------------------------------------subroutine spline (n, x, y, w, smo, edf)cc------------------------------------------------------------------cc input:c n : number of observations.c x(n) : ordered abscissa values.c y(n) : corresponding ordinate (response) values.c w(n) : weight for each (x,y) observation.c output:c smo(n) : smoothed ordinate (response) values.c edf : equivalent degrees of freedomcc------------------------------------------------------------------c Argsinteger ndouble precision x(n), y(n), w(n), smo(n), edfc Vardouble precision knot(29), coef(25), work((17+25)*25)double precision dx(2500),dy(2500), dw(2500),dsmo(2500), lev(2500)double precision param(4), df1, lambda, crit, p, sinteger iparms(3), i, nk, ip, isetup,ierdouble precision df, gcvpeninteger ismethodcommon /spsmooth/ df, gcvpen, ismethodif (n .gt. 2500) call bdrsplerr()do 10 i = 1,ndx(i) = (x(i)-x(1))/(x(n)-x(1))dy(i) = y(i)10 dw(i) = w(i)nk = min(n,15)knot(1) = dx(1)knot(2) = dx(1)knot(3) = dx(1)knot(4) = dx(1)knot(nk+1) = dx(n)knot(nk+2) = dx(n)knot(nk+3) = dx(n)knot(nk+4) = dx(n)do 40 i = 5, nkp = (n-1)*real(i-4)/real(nk-3)ip = int(p)p = p-ipknot(i) = (1-p)*dx(ip+1) + p*dx(ip+2)40 continuec call dblepr('knots', 5, knot, nk+4)C iparms(1:2) := (icrit, ispar) for ./sbart.fif (iabs(ismethod) .eq. 1) theniparms(1) = 3df1 = dfelseiparms(1) = 1df1 = 0d0endifc ispar := 0 <==> estimate `spar' :iparms(2) = 0c maxit = 500 :iparms(3) = 500param(1) = 0d0param(2) = 1.5d0c tol for `spar' estimation:param(3) = 1d-2c this was `eps' (=? sqrt(machine eps)) in ./sbart.f :param(4) = .000244isetup = 0ier = 1call qsbart(gcvpen,df1,dx,dy,dw,0.0d0,n,knot,nk,coef,dsmo,lev,& crit,iparms,lambda,param,isetup, work,4,1,ier)if(ier .gt. 0) call intpr('TROUBLE:',8, ier, 1)do 50 i = 1,n50 smo(i) = dsmo(i)c call dblepr('smoothed',8, dsmo, n)s = 0do 60 i = 1, n60 s = s + lev(i)edf = sif(ismethod.lt.0) thencall dblepr('lambda', 6, lambda, 1)call dblepr('df', 2, s, 1)endifreturnend***********************************************************************C=== This was 'sort()' in gamfit's mysort.f [or sortdi() in sortdi.f ] :CC=== FIXME: Translate to C and add to ../../../main/sort.c <<<<<CC why on earth is a() double precision ????subroutine sort (v,a, ii,jj)cc Puts into a the permutation vector which sorts v intoc increasing order. Only elements from ii to jj are considered.c Arrays iu(k) and il(k) permit sorting up to 2**(k+1)-1 elementscc This is a modification of CACM algorithm #347 by R. C. Singleton,c which is a modified Hoare quicksort.cinteger ii,jjdouble precision v(*), a(jj)cinteger iu(20),il(20)integer t,tt, m,i,j,ij,k,ldouble precision vt, vttm=1i=iij=jj10 if (i.ge.j) go to 8020 k=iij=(j+i)/2t=a(ij)vt=v(ij)if (v(i).le.vt) go to 30a(ij)=a(i)a(i)=tt=a(ij)v(ij)=v(i)v(i)=vtvt=v(ij)30 l=jif (v(j).ge.vt) go to 50a(ij)=a(j)a(j)=tt=a(ij)v(ij)=v(j)v(j)=vtvt=v(ij)if (v(i).le.vt) go to 50a(ij)=a(i)a(i)=tt=a(ij)v(ij)=v(i)v(i)=vtvt=v(ij)go to 5040 a(l)=a(k)a(k)=ttv(l)=v(k)v(k)=vtt50 l=l-1if (v(l).gt.vt) go to 50tt=a(l)vtt=v(l)60 k=k+1if (v(k).lt.vt) go to 60if (k.le.l) go to 40if (l-i.le.j-k) go to 70il(m)=iiu(m)=li=km=m+1go to 9070 il(m)=kiu(m)=jj=lm=m+1go to 9080 m=m-1if (m.eq.0) returni=il(m)j=iu(m)90 if (j-i.gt.10) go to 20if (i.eq.ii) go to 10i=i-1100 i=i+1if (i.eq.j) go to 80t=a(i+1)vt=v(i+1)if (v(i).le.vt) go to 100k=i110 a(k+1)=a(k)v(k+1)=v(k)k=k-1if (vt.lt.v(k)) go to 110a(k+1)=tv(k+1)=vtgo to 100end