liblfev.c 100 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355
  1. /*
  2. * Copyright 1996-2006 Catherine Loader.
  3. */
  4. #include "mex.h"
  5. /*
  6. * Copyright 1996-2006 Catherine Loader.
  7. */
  8. #include "lfev.h"
  9. extern void fitoptions();
  10. static double hmin, gmin, sig2, pen, vr, tb;
  11. static lfit *blf;
  12. static design *bdes;
  13. int procvbind(des,lf,v)
  14. design *des;
  15. lfit *lf;
  16. int v;
  17. { double s0, s1, bi;
  18. int i, ii, k;
  19. k = procv_var(des,lf,v);
  20. wdiag(&lf->lfd, &lf->sp, des,des->wd,&lf->dv,0,1,0);
  21. s0 = s1 = 0.0;
  22. for (i=0; i<des->n; i++)
  23. { ii = des->ind[i];
  24. s0+= prwt(&lf->lfd,ii)*des->wd[i]*des->wd[i];
  25. bi = prwt(&lf->lfd,ii)*fabs(des->wd[i]*ipower(dist(des,ii),deg(&lf->sp)+1));
  26. s1+= bi*bi;
  27. }
  28. vr += s0;
  29. tb += s1;
  30. return(k);
  31. }
  32. double bcri(h,c,cri)
  33. double h;
  34. int c, cri;
  35. { double num, den, res[10];
  36. int (*pv)();
  37. if (c==DALP)
  38. blf->sp.nn = h;
  39. else
  40. blf->sp.fixh = h;
  41. if ((cri&63)==BIND)
  42. { pv = procvbind;
  43. vr = tb = 0.0;
  44. }
  45. else pv = procvstd;
  46. if (cri<64) startlf(bdes,blf,pv,0);
  47. switch(cri&63)
  48. { case BGCV:
  49. ressumm(blf,bdes,res);
  50. num = -2*blf->lfd.n*res[0];
  51. den = blf->lfd.n-res[1];
  52. return(num/(den*den));
  53. case BCP:
  54. ressumm(blf,bdes,res);
  55. return(-2*res[0]/sig2-blf->lfd.n+pen*res[1]);
  56. case BIND:
  57. return(vr+pen*pen*tb);
  58. }
  59. LERR(("bcri: unknown criterion"));
  60. return(0.0);
  61. }
  62. void bsel2(h0,g0,ifact,c,cri)
  63. double h0, g0, ifact;
  64. int c, cri;
  65. { int done, inc;
  66. double h1, g1;
  67. h1 = h0; g1 = g0;
  68. done = inc = 0;
  69. while (!done)
  70. { h1 *= 1+ifact;
  71. g0 = g1;
  72. g1 = bcri(h1,c,cri);
  73. if (g1<gmin) { hmin = h1; gmin = g1; }
  74. if (g1>g0) inc++; else inc = 0;
  75. switch(cri)
  76. { case BIND:
  77. done = (inc>=4) & (vr<blf->fp.nv);
  78. break;
  79. default:
  80. done = (inc>=4);
  81. }
  82. }
  83. }
  84. void bsel3(h0,g0,ifact,c,cri)
  85. double h0, g0, ifact;
  86. int c, cri;
  87. { double h1, g1;
  88. int i;
  89. hmin = h0; gmin = g0;
  90. for (i=-1; i<=1; i++) if (i!=0)
  91. { h1 = h0*(1+i*ifact);
  92. g1 = bcri(h1,c,cri);
  93. if (g1<gmin) { hmin = h1; gmin = g1; }
  94. }
  95. return;
  96. }
  97. void bselect(lf,des,c,cri,pn)
  98. lfit *lf;
  99. design *des;
  100. int c, cri;
  101. double pn;
  102. { double h0, g0, ifact;
  103. int i;
  104. pen = pn;
  105. blf = lf;
  106. bdes = des;
  107. if (cri==BIND) pen /= factorial(deg(&lf->sp)+1);
  108. hmin = h0 = (c==DFXH) ? fixh(&lf->sp) : nn(&lf->sp);
  109. if (h0==0) LERR(("bselect: initial bandwidth is 0"));
  110. if (lf_error) return;
  111. sig2 = 1.0;
  112. gmin = g0 = bcri(h0,c,cri);
  113. if (cri==BCP)
  114. { sig2 = rv(&lf->fp);
  115. g0 = gmin = bcri(h0,c,cri+64);
  116. }
  117. ifact = 0.3;
  118. bsel2(h0,g0,ifact,c,cri);
  119. for (i=0; i<5; i++)
  120. { ifact = ifact/2;
  121. bsel3(hmin,gmin,ifact,c,cri);
  122. }
  123. if (c==DFXH)
  124. fixh(&lf->sp) = hmin;
  125. else
  126. nn(&lf->sp) = hmin;
  127. startlf(des,lf,procvstd,0);
  128. ressumm(lf,des,lf->fp.kap);
  129. }
  130. double compsda(x,h,n)
  131. double *x, h;
  132. int n;
  133. /* n/(n-1) * int( fhat''(x)^2 dx ); bandwidth h */
  134. { int i, j;
  135. double ik, sd, z;
  136. ik = wint(1,NULL,0,WGAUS);
  137. sd = 0;
  138. for (i=0; i<n; i++)
  139. for (j=i; j<n; j++)
  140. { z = (x[i]-x[j])/h;
  141. sd += (2-(i==j))*Wconv4(z,WGAUS)/(ik*ik);
  142. }
  143. sd = sd/(n*(n-1)*h*h*h*h*h);
  144. return(sd);
  145. }
  146. double widthsj(x,lambda,n)
  147. double *x, lambda;
  148. int n;
  149. { double ik, a, b, td, sa, z, c, c1, c2, c3;
  150. int i, j;
  151. a = GFACT*0.920*lambda*exp(-log((double)n)/7)/SQRT2;
  152. b = GFACT*0.912*lambda*exp(-log((double)n)/9)/SQRT2;
  153. ik = wint(1,NULL,0,WGAUS);
  154. td = 0;
  155. for (i=0; i<n; i++)
  156. for (j=i; j<n; j++)
  157. { z = (x[i]-x[j])/b;
  158. td += (2-(i==j))*Wconv6(z,WGAUS)/(ik*ik);
  159. }
  160. td = -td/(n*(n-1));
  161. j = 2.0;
  162. c1 = Wconv4(0.0,WGAUS);
  163. c2 = wint(1,&j,1,WGAUS);
  164. c3 = Wconv(0.0,WGAUS); /* (2*c1/(c2*c3))^(1/7)=1.357 */
  165. sa = compsda(x,a,n);
  166. c = b*exp(log(c1*ik/(c2*c3*GFACT*GFACT*GFACT*GFACT)*sa/td)/7)*SQRT2;
  167. return(c);
  168. }
  169. void kdecri(x,h,res,c,k,ker,n)
  170. double *x, h, *res, c;
  171. int k, ker, n;
  172. { int i, j;
  173. double degfree, dfd, pen, s, r0, r1, d0, d1, ik, wij;
  174. if (h<=0) WARN(("kdecri, h = %6.4f",h));
  175. res[0] = res[1] = 0.0;
  176. ik = wint(1,NULL,0,ker);
  177. switch(k)
  178. { case 1: /* aic */
  179. pen = 2.0;
  180. for (i=0; i<n; i++)
  181. { r0 = d0 = 0.0;
  182. for (j=0; j<n; j++)
  183. { s = (x[i]-x[j])/h;
  184. r0 += W(s,ker);
  185. d0 += s*s*Wd(s,ker);
  186. }
  187. d0 = -(d0+r0)/(n*h*h*ik); /* d0 = d/dh fhat(xi) */
  188. r0 /= n*h*ik; /* r0 = fhat(xi) */
  189. res[0] += -2*log(r0)+pen*W(0.0,ker)/(n*h*ik*r0);
  190. res[1] += -2*d0/r0-pen*W(0.0,ker)/(n*h*ik*r0)*(d0/r0+1.0/h);
  191. }
  192. return;
  193. case 2: /* ocv */
  194. for (i=0; i<n; i++)
  195. { r0 = 0.0; d0 = 0.0;
  196. for (j=0; j<n; j++) if (i!=j)
  197. { s = (x[i]-x[j])/h;
  198. r0 += W(s,ker);
  199. d0 += s*s*Wd(s,ker);
  200. }
  201. d0 = -(d0+r0)/((n-1)*h*h);
  202. r0 = r0/((n-1)*h);
  203. res[0] -= log(r0);
  204. res[1] -= d0/r0;
  205. }
  206. return;
  207. case 3: /* lscv */
  208. r0 = r1 = d0 = d1 = degfree = 0.0;
  209. for (i=0; i<n; i++)
  210. { dfd = 0.0;
  211. for (j=0; j<n; j++)
  212. { s = (x[i]-x[j])/h;
  213. wij = W(s,ker);
  214. dfd += wij;
  215. /*
  216. * r0 = \int fhat * fhat = sum_{i,j} W*W( (Xi-Xj)/h ) / n^2 h.
  217. * d0 is it's derivative wrt h.
  218. *
  219. * r1 = 1/n sum( f_{-i}(X_i) ).
  220. * d1 is it's derivative wrt h.
  221. *
  222. * degfree = sum_i ( W_0 / sum_j W( (Xi-Xj)/h ) ) is fitted d.f.
  223. */
  224. r0 += Wconv(s,ker);
  225. d0 += -s*s*Wconv1(s,ker);
  226. if (i != j)
  227. { r1 += wij;
  228. d1 += -s*s*Wd(s,ker);
  229. }
  230. }
  231. degfree += 1.0/dfd;
  232. }
  233. d1 -= r1;
  234. d0 -= r0;
  235. res[0] = r0/(n*n*h*ik*ik) - 2*r1/(n*(n-1)*h*ik);
  236. res[1] = d0/(n*n*h*h*ik*ik) - 2*d1/(n*(n-1)*h*h*ik);
  237. res[2] = degfree;
  238. return;
  239. case 4: /* bcv */
  240. r0 = d0 = 0.0;
  241. for (i=0; i<n; i++)
  242. for (j=i+1; j<n; j++)
  243. { s = (x[i]-x[j])/h;
  244. r0 += 2*Wconv4(s,ker);
  245. d0 += 2*s*Wconv5(s,ker);
  246. }
  247. d0 = (-d0-r0)/(n*n*h*h*ik*ik);
  248. r0 = r0/(n*n*h*ik*ik);
  249. j = 2.0;
  250. d1 = wint(1,&j,1,ker);
  251. r1 = Wconv(0.0,ker);
  252. res[0] = (d1*d1*r0/4+r1/(n*h))/(ik*ik);
  253. res[1] = (d1*d1*d0/4-r1/(n*h*h))/(ik*ik);
  254. return;
  255. case 5: /* sjpi */
  256. s = c*exp(5*log(h)/7)/SQRT2;
  257. d0 = compsda(x,s,n);
  258. res[0] = d0; /* this is S(alpha) in SJ */
  259. res[1] = exp(log(Wikk(WGAUS,0)/(d0*n))/5)-h;
  260. return;
  261. case 6: /* gas-k-k */
  262. s = exp(log(1.0*n)/10)*h;
  263. d0 = compsda(x,s,n);
  264. res[0] = d0;
  265. res[1] = exp(log(Wikk(WGAUS,0)/(d0*n))/5)-h;
  266. return;
  267. }
  268. LERR(("kdecri: what???"));
  269. return;
  270. }
  271. double esolve(x,j,h0,h1,k,c,ker,n)
  272. double *x, h0, h1, c;
  273. int j, k, ker, n;
  274. { double h[7], d[7], r[7], res[4], min, minh, fact;
  275. int i, nc;
  276. min = 1.0e30; minh = 0.0;
  277. fact = 1.00001;
  278. h[6] = h0; kdecri(x,h[6],res,c,j,ker,n);
  279. r[6] = res[0]; d[6] = res[1];
  280. if (lf_error) return(0.0);
  281. nc = 0;
  282. for (i=0; i<k; i++)
  283. { h[5] = h[6]; r[5] = r[6]; d[5] = d[6];
  284. h[6] = h0*exp((i+1)*log(h1/h0)/k);
  285. kdecri(x,h[6],res,c,j,ker,n);
  286. r[6] = res[0]; d[6] = res[1];
  287. if (lf_error) return(0.0);
  288. if (d[5]*d[6]<0)
  289. { h[2] = h[0] = h[5]; d[2] = d[0] = d[5]; r[2] = r[0] = r[5];
  290. h[3] = h[1] = h[6]; d[3] = d[1] = d[6]; r[3] = r[1] = r[6];
  291. while ((h[3]>fact*h[2])|(h[2]>fact*h[3]))
  292. { h[4] = h[3]-d[3]*(h[3]-h[2])/(d[3]-d[2]);
  293. if ((h[4]<h[0]) | (h[4]>h[1])) h[4] = (h[0]+h[1])/2;
  294. kdecri(x,h[4],res,c,j,ker,n);
  295. r[4] = res[0]; d[4] = res[1];
  296. if (lf_error) return(0.0);
  297. h[2] = h[3]; h[3] = h[4];
  298. d[2] = d[3]; d[3] = d[4];
  299. r[2] = r[3]; r[3] = r[4];
  300. if (d[4]*d[0]>0) { h[0] = h[4]; d[0] = d[4]; r[0] = r[4]; }
  301. else { h[1] = h[4]; d[1] = d[4]; r[1] = r[4]; }
  302. }
  303. if (j>=4) return(h[4]); /* first min for BCV etc */
  304. if (r[4]<=min) { min = r[4]; minh = h[4]; }
  305. nc++;
  306. }
  307. }
  308. if (nc==0) minh = (r[5]<r[6]) ? h0 : h1;
  309. return(minh);
  310. }
  311. void kdeselect(band,x,ind,h0,h1,meth,nm,ker,n)
  312. double h0, h1, *band, *x;
  313. int *ind, nm, ker, n, *meth;
  314. { double scale, c;
  315. int i, k;
  316. k = n/4;
  317. for (i=0; i<n; i++) ind[i] = i;
  318. scale = kordstat(x,n+1-k,n,ind) - kordstat(x,k,n,ind);
  319. c = widthsj(x,scale,n);
  320. for (i=0; i<nm; i++)
  321. band[i] = esolve(x,meth[i],h0,h1,10,c,ker,n);
  322. }
  323. /*
  324. * Copyright 1996-2006 Catherine Loader.
  325. */
  326. /*
  327. * The function dens_integrate(lf,des,z) is used to integrate a density
  328. * estimate (z=1) or the density squared (z=2). This is used to renormalize
  329. * the estimate (function dens_renorm) or in the computation of LSCV
  330. * (in modlscv.c). The implementation is presently for d=1.
  331. *
  332. * The computation orders the fit points selected by locfit, and
  333. * integrates analytically over each interval. For the log-link,
  334. * the interpolant used is peicewise quadratic (with one knot in
  335. * the middle of each interval); this differs from the cubic interpolant
  336. * used elsewhere in Locfit.
  337. *
  338. * TODO: allow for xlim. What can be done simply in >=2 dimensions?
  339. */
  340. #include "lfev.h"
  341. /*
  342. * Finds the order of observations in the array x, and
  343. * stores in integer array ind.
  344. * At input, lset l=0 and r=length(x)-1.
  345. * At output, x[ind[0]] <= x[ind[1]] <= ...
  346. */
  347. void lforder(ind,x,l,r)
  348. int *ind, l, r;
  349. double *x;
  350. { double piv;
  351. int i, i0, i1;
  352. piv = (x[ind[l]]+x[ind[r]])/2;
  353. i0 = l; i1 = r;
  354. while (i0<=i1)
  355. { while ((i0<=i1) && (x[ind[i0]]<=piv)) i0++;
  356. while ((i0<=i1) && (x[ind[i1]]>piv)) i1--;
  357. if (i0<i1)
  358. { ISWAP(ind[i0],ind[i1]);
  359. i0++; i1--;
  360. }
  361. }
  362. /* now, x[ind[l..i1]] <= piv < x[ind[i0..r]].
  363. put the ties in the middle */
  364. while ((i1>=l) && (x[ind[i1]]==piv)) i1--;
  365. for (i=l; i<=i1; i++)
  366. if (x[ind[i]]==piv)
  367. { ISWAP(ind[i],ind[i1]);
  368. while (x[ind[i1]]==piv) i1--;
  369. }
  370. if (l<i1) lforder(ind,x,l,i1);
  371. if (i0<r) lforder(ind,x,i0,r);
  372. }
  373. /*
  374. * estdiv integrates the density between fit points x0 and x1.
  375. * f0, f1 are function values, d0, d1 are derivatives.
  376. */
  377. double estdiv(x0,x1,f0,f1,d0,d1,lin)
  378. double x0, x1, f0, f1, d0, d1;
  379. int lin;
  380. { double cf[4], I[2], dlt, e0, e1;
  381. if (x0==x1) return(0.0);
  382. if (lin==LIDENT)
  383. {
  384. /* cf are integrals of hermite polynomials.
  385. * Then adjust for x1-x0.
  386. */
  387. cf[0] = cf[1] = 0.5;
  388. cf[2] = 1.0/12.0; cf[3] = -cf[2];
  389. return( (cf[0]*f0+cf[1]*f1)*(x1-x0)
  390. + (cf[2]*d0+cf[3]*d1)*(x1-x0)*(x1-x0) );
  391. }
  392. /*
  393. * this is for LLOG
  394. */
  395. dlt = (x1-x0)/2;
  396. cf[0] = f0;
  397. cf[1] = d0;
  398. cf[2] = ( 2*(f1-f0) - dlt*(d1+3*d0) )/(4*dlt*dlt);
  399. recurint(0.0,dlt,cf,I,0,WRECT);
  400. e0 = I[0];
  401. cf[0] = f1;
  402. cf[1] = -d1;
  403. cf[2] = ( 2*(f0-f1) + dlt*(d0+3*d1) )/( 4*dlt*dlt );
  404. recurint(0.0,dlt,cf,I,0,WRECT);
  405. e1 = I[0];
  406. return(e0+e1);
  407. }
  408. /*
  409. * Evaluate the integral of the density estimate to the power z.
  410. * This would be severely messed up, if I ever implement parcomp
  411. * for densities.
  412. */
  413. double dens_integrate(lf,des,z)
  414. lfit *lf;
  415. design *des;
  416. int z;
  417. { int has_deriv, i, i0, i1, nv, *ind;
  418. double *xev, *fit, *deriv, sum, term;
  419. double d0, d1, f0, f1;
  420. fitpt *fp;
  421. fp = &lf->fp;
  422. if (fp->d >= 2)
  423. { WARN(("dens_integrate requires d=1"));
  424. return(0.0);
  425. }
  426. has_deriv = (deg(&lf->sp) > 0); /* not right? */
  427. fit = fp->coef;
  428. if (has_deriv)
  429. deriv = &fit[fp->nvm];
  430. xev = evp(fp);
  431. /*
  432. * order the vertices
  433. */
  434. nv = fp->nv;
  435. if (lf->lfd.n<nv) return(0.0);
  436. ind = des->ind;
  437. for (i=0; i<nv; i++) ind[i] = i;
  438. lforder(ind,xev,0,nv-1);
  439. sum = 0.0;
  440. /*
  441. * Estimate the contribution of the boundaries.
  442. * should really check flim here.
  443. */
  444. i0 = ind[0]; i1 = ind[1];
  445. f1 = fit[i0];
  446. d1 = (has_deriv) ? deriv[i0] :
  447. (fit[i1]-fit[i0])/(xev[i1]-xev[i0]);
  448. if (d1 <= 0) WARN(("dens_integrate - ouch!"));
  449. if (z==2)
  450. { if (link(&lf->sp)==LLOG)
  451. { f1 *= 2; d1 *= 2; }
  452. else
  453. { d1 = 2*d1*f1; f1 = f1*f1; }
  454. }
  455. term = (link(&lf->sp)==LIDENT) ? f1*f1/(2*d1) : exp(f1)/d1;
  456. sum += term;
  457. i0 = ind[nv-2]; i1 = ind[nv-1];
  458. f0 = fit[i1];
  459. d0 = (has_deriv) ? deriv[i1] :
  460. (fit[i1]-fit[i0])/(xev[i1]-xev[i0]);
  461. if (d0 >= 0) WARN(("dens_integrate - ouch!"));
  462. if (z==2)
  463. { if (link(&lf->sp)==LLOG)
  464. { f0 *= 2; d0 *= 2; }
  465. else
  466. { d0 = 2*d0*f0; f0 = f0*f0; }
  467. }
  468. term = (link(&lf->sp)==LIDENT) ? -f0*f0/(2*d0) : exp(f0)/d0;
  469. sum += term;
  470. for (i=1; i<nv; i++)
  471. { i0 = ind[i-1]; i1 = ind[i];
  472. f0 = fit[i0]; f1 = fit[i1];
  473. d0 = (has_deriv) ? deriv[i0] :
  474. (f1-f0)/(xev[i1]-xev[i0]);
  475. d1 = (has_deriv) ? deriv[i1] : d0;
  476. if (z==2)
  477. { if (link(&lf->sp)==LLOG)
  478. { f0 *= 2; f1 *= 2; d0 *= 2; d1 *= 2; }
  479. else
  480. { d0 *= 2*f0; d1 *= 2*f1; f0 = f0*f0; f1 = f1*f1; }
  481. }
  482. term = estdiv(xev[i0],xev[i1],f0,f1,d0,d1,link(&lf->sp));
  483. sum += term;
  484. }
  485. return(sum);
  486. }
  487. void dens_renorm(lf,des)
  488. lfit *lf;
  489. design *des;
  490. { int i;
  491. double sum;
  492. sum = dens_integrate(lf,des,1);
  493. if (sum==0.0) return;
  494. sum = log(sum);
  495. for (i=0; i<lf->fp.nv; i++) lf->fp.coef[i] -= sum;
  496. }
  497. /*
  498. * Copyright 1996-2006 Catherine Loader.
  499. */
  500. /*
  501. * This file contains functions for constructing and
  502. * interpolating the adaptive tree structure. This is
  503. * the default evaluation structure used by Locfit.
  504. */
  505. #include "lfev.h"
  506. /*
  507. Guess the number of fitting points.
  508. Needs improving!
  509. */
  510. void atree_guessnv(evs,nvm,ncm,vc,d,alp)
  511. evstruc *evs;
  512. double alp;
  513. int *nvm, *ncm, *vc, d;
  514. { double a0, cu, ifl;
  515. int i, nv, nc;
  516. *ncm = 1<<30; *nvm = 1<<30;
  517. *vc = 1 << d;
  518. if (alp>0)
  519. { a0 = (alp > 1) ? 1 : 1/alp;
  520. if (cut(evs)<0.01)
  521. { WARN(("guessnv: cut too small."));
  522. cut(evs) = 0.01;
  523. }
  524. cu = 1;
  525. for (i=0; i<d; i++) cu *= MIN(1.0,cut(evs));
  526. nv = (int)((5*a0/cu)**vc); /* this allows 10*a0/cu splits */
  527. nc = (int)(10*a0/cu+1); /* and 10*a0/cu cells */
  528. if (nv<*nvm) *nvm = nv;
  529. if (nc<*ncm) *ncm = nc;
  530. }
  531. if (*nvm == 1<<30) /* by default, allow 100 splits */
  532. { *nvm = 102**vc;
  533. *ncm = 201;
  534. }
  535. /* inflation based on mk */
  536. ifl = mk(evs)/100.0;
  537. *nvm = *vc+(int)(ifl**nvm);
  538. *ncm = (int)(ifl**ncm);
  539. }
  540. /*
  541. Determine whether a cell in the tree needs splitting.
  542. If so, return the split variable (0..d-1).
  543. Otherwise, return -1.
  544. */
  545. int atree_split(lf,ce,le,ll,ur)
  546. lfit *lf;
  547. int *ce;
  548. double *le, *ll, *ur;
  549. { int d, vc, i, is;
  550. double h, hmin, score[MXDIM];
  551. d = lf->fp.d; vc = 1<<d;
  552. hmin = 0.0;
  553. for (i=0; i<vc; i++)
  554. { h = lf->fp.h[ce[i]];
  555. if ((h>0) && ((hmin==0)|(h<hmin))) hmin = h;
  556. }
  557. is = 0;
  558. for (i=0; i<d; i++)
  559. { le[i] = (ur[i]-ll[i])/lf->lfd.sca[i];
  560. if ((lf->lfd.sty[i]==STCPAR) || (hmin==0))
  561. score[i] = 2*(ur[i]-ll[i])/(lf->evs.fl[i+d]-lf->evs.fl[i]);
  562. else
  563. score[i] = le[i]/hmin;
  564. if (score[i]>score[is]) is = i;
  565. }
  566. if (cut(&lf->evs)<score[is]) return(is);
  567. return(-1);
  568. }
  569. /*
  570. recursively grow the tree structure, begining with the parent cell.
  571. */
  572. void atree_grow(des,lf,ce,ct,term,ll,ur)
  573. design *des;
  574. lfit *lf;
  575. int *ce, *ct, *term;
  576. double *ll, *ur;
  577. { int nce[1<<MXDIM];
  578. int i, i0, i1, d, vc, pv, tk, ns;
  579. double le[MXDIM], z;
  580. d = lf->fp.d; vc = 1<<d;
  581. /* does this cell need splitting?
  582. If not, wrap up and return.
  583. */
  584. ns = atree_split(lf,ce,le,ll,ur);
  585. if (ns==-1)
  586. { if (ct != NULL) /* reconstructing terminal cells */
  587. { for (i=0; i<vc; i++) term[*ct*vc+i] = ce[i];
  588. (*ct)++;
  589. }
  590. return;
  591. }
  592. /* split the cell at the midpoint on side ns */
  593. tk = 1<<ns;
  594. for (i=0; i<vc; i++)
  595. { if ((i&tk)==0) nce[i] = ce[i];
  596. else
  597. { i0 = ce[i];
  598. i1 = ce[i-tk];
  599. pv = (lf->lfd.sty[i]!=STCPAR) &&
  600. (le[ns] < (cut(&lf->evs)*MIN(lf->fp.h[i0],lf->fp.h[i1])));
  601. nce[i] = newsplit(des,lf,i0,i1,pv);
  602. if (lf_error) return;
  603. }
  604. }
  605. z = ur[ns]; ur[ns] = (z+ll[ns])/2;
  606. atree_grow(des,lf,nce,ct,term,ll,ur);
  607. if (lf_error) return;
  608. ur[ns] = z;
  609. for (i=0; i<vc; i++)
  610. nce[i] = ((i&tk)== 0) ? nce[i+tk] : ce[i];
  611. z = ll[ns]; ll[ns] = (z+ur[ns])/2;
  612. atree_grow(des,lf,nce,ct,term,ll,ur);
  613. if (lf_error) return;
  614. ll[ns] = z;
  615. }
  616. void atree_start(des,lf)
  617. design *des;
  618. lfit *lf;
  619. { int d, i, j, k, vc, ncm, nvm;
  620. double ll[MXDIM], ur[MXDIM];
  621. if (lf_debug>1) mut_printf(" In atree_start\n");
  622. d = lf->fp.d;
  623. atree_guessnv(&lf->evs,&nvm,&ncm,&vc,d,nn(&lf->sp));
  624. if (lf_debug>2) mut_printf(" atree_start: nvm %d ncm %d\n",nvm,ncm);
  625. trchck(lf,nvm,ncm,vc);
  626. /* Set the lower left, upper right limits. */
  627. for (j=0; j<d; j++)
  628. { ll[j] = lf->evs.fl[j];
  629. ur[j] = lf->evs.fl[j+d];
  630. }
  631. /* Set the initial cell; fit at the vertices. */
  632. for (i=0; i<vc; i++)
  633. { j = i;
  634. for (k=0; k<d; ++k)
  635. { evptx(&lf->fp,i,k) = (j%2) ? ur[k] : ll[k];
  636. j >>= 1;
  637. }
  638. lf->evs.ce[i] = i;
  639. PROC_VERTEX(des,lf,i);
  640. if (lf_error) return;
  641. lf->evs.s[i] = 0;
  642. }
  643. lf->fp.nv = vc;
  644. /* build the tree */
  645. atree_grow(des,lf,lf->evs.ce,NULL,NULL,ll,ur);
  646. lf->evs.nce = 1;
  647. }
  648. double atree_int(lf,x,what)
  649. lfit *lf;
  650. double *x;
  651. int what;
  652. { double vv[64][64], *ll, *ur, h, xx[MXDIM];
  653. int lo, tk, ns, nv, nc, d, i, vc;
  654. int ce[64];
  655. fitpt *fp;
  656. evstruc *evs;
  657. fp = &lf->fp;
  658. evs= &lf->evs;
  659. d = fp->d;
  660. vc = 1<<d;
  661. for (i=0; i<vc; i++)
  662. { setzero(vv[i],vc);
  663. nc = exvval(fp,vv[i],i,d,what,1);
  664. ce[i] = evs->ce[i];
  665. }
  666. ns = 0;
  667. while(ns!=-1)
  668. { ll = evpt(fp,ce[0]); ur = evpt(fp,ce[vc-1]);
  669. ns = atree_split(lf,ce,xx,ll,ur);
  670. if (ns!=-1)
  671. { tk = 1<<ns;
  672. h = ur[ns]-ll[ns];
  673. lo = (2*(x[ns]-ll[ns])) < h;
  674. for (i=0; i<vc; i++) if ((tk&i)==0)
  675. { nv = findpt(fp,evs,(int)ce[i],(int)ce[i+tk]);
  676. if (nv==-1) LERR(("Descend tree problem"));
  677. if (lf_error) return(0.0);
  678. if (lo)
  679. { ce[i+tk] = nv;
  680. if (evs->s[nv]) exvvalpv(vv[i+tk],vv[i],vv[i+tk],d,ns,h,nc);
  681. else exvval(fp,vv[i+tk],nv,d,what,1);
  682. }
  683. else
  684. { ce[i] = nv;
  685. if (evs->s[nv]) exvvalpv(vv[i],vv[i],vv[i+tk],d,ns,h,nc);
  686. else exvval(fp,vv[i],nv,d,what,1);
  687. } }
  688. } }
  689. ll = evpt(fp,ce[0]); ur = evpt(fp,ce[vc-1]);
  690. return(rectcell_interp(x,vv,ll,ur,d,nc));
  691. }
  692. /*
  693. * Copyright 1996-2006 Catherine Loader.
  694. */
  695. #include "lfev.h"
  696. double linear_interp(h,d,f0,f1)
  697. double h, d, f0, f1;
  698. { if (d==0) return(f0);
  699. return( ( (d-h)*f0 + h*f1 ) / d );
  700. }
  701. void hermite2(x,z,phi)
  702. double x, z, *phi;
  703. { double h;
  704. if (z==0)
  705. { phi[0] = 1.0; phi[1] = phi[2] = phi[3] = 0.0;
  706. return;
  707. }
  708. h = x/z;
  709. if (h<0)
  710. { phi[0] = 1; phi[1] = 0;
  711. phi[2] = h; phi[3] = 0;
  712. return;
  713. }
  714. if (h>1)
  715. { phi[0] = 0; phi[1] = 1;
  716. phi[2] = 0; phi[3] = h-1;
  717. return;
  718. }
  719. phi[1] = h*h*(3-2*h);
  720. phi[0] = 1-phi[1];
  721. phi[2] = h*(1-h)*(1-h);
  722. phi[3] = h*h*(h - 1);
  723. }
  724. double cubic_interp(h,f0,f1,d0,d1)
  725. double h, f0, f1, d0, d1;
  726. { double phi[4];
  727. hermite2(h,1.0,phi);
  728. return(phi[0]*f0+phi[1]*f1+phi[2]*d0+phi[3]*d1);
  729. }
  730. double cubintd(h,f0,f1,d0,d1)
  731. double h, f0, f1, d0, d1;
  732. { double phi[4];
  733. phi[1] = 6*h*(1-h);
  734. phi[0] = -phi[1];
  735. phi[2] = (1-h)*(1-3*h);
  736. phi[3] = h*(3*h-2);
  737. return(phi[0]*f0+phi[1]*f1+phi[2]*d0+phi[3]*d1);
  738. }
  739. /*
  740. interpolate over a rectangular cell.
  741. x = interpolation point.
  742. vv = array of vertex values.
  743. ll = lower left corner.
  744. ur = upper right corner.
  745. d = dimension.
  746. nc = no of coefficients.
  747. */
  748. double rectcell_interp(x,vv,ll,ur,d,nc)
  749. double *x, vv[64][64], *ll, *ur;
  750. int d, nc;
  751. { double phi[4];
  752. int i, j, k, tk;
  753. tk = 1<<d;
  754. for (i=0; i<tk; i++) if (vv[i][0]==NOSLN) return(NOSLN);
  755. /* no derivatives - use multilinear interpolation */
  756. if (nc==1)
  757. { for (i=d-1; i>=0; i--)
  758. { tk = 1<<i;
  759. for (j=0; j<tk; j++)
  760. vv[j][0] = linear_interp(x[i]-ll[i],ur[i]-ll[i],vv[j][0],vv[j+tk][0]);
  761. }
  762. return(vv[0][0]);
  763. }
  764. /* with derivatives -- use cubic */
  765. if (nc==d+1)
  766. { for (i=d-1; i>=0; i--)
  767. { hermite2(x[i]-ll[i],ur[i]-ll[i],phi);
  768. tk = 1<<i;
  769. phi[2] *= ur[i]-ll[i];
  770. phi[3] *= ur[i]-ll[i];
  771. for (j=0; j<tk; j++)
  772. { vv[j][0] = phi[0]*vv[j][0] + phi[1]*vv[j+tk][0]
  773. + phi[2]*vv[j][i+1] + phi[3]*vv[j+tk][i+1];
  774. for (k=1; k<=i; k++)
  775. vv[j][k] = phi[0]*vv[j][k] + phi[1]*vv[j+tk][k];
  776. }
  777. }
  778. return(vv[0][0]);
  779. }
  780. /* with all coefs -- use multicubic */
  781. for (i=d-1; i>=0; i--)
  782. { hermite2(x[i]-ll[i],ur[i]-ll[i],phi);
  783. tk = 1<<i;
  784. phi[2] *= ur[i]-ll[i];
  785. phi[3] *= ur[i]-ll[i];
  786. for (j=0; j<tk; j++)
  787. for (k=0; k<tk; k++)
  788. vv[j][k] = phi[0]*vv[j][k] + phi[1]*vv[j+tk][k]
  789. + phi[2]*vv[j][k+tk] + phi[3]*vv[j+tk][k+tk];
  790. }
  791. return(vv[0][0]);
  792. }
  793. int exvval(fp,vv,nv,d,what,z)
  794. fitpt *fp;
  795. double *vv;
  796. int nv, d, z, what;
  797. { int i, k;
  798. double *values;
  799. k = (z) ? 1<<d : d+1;
  800. for (i=1; i<k; i++) vv[i] = 0.0;
  801. switch(what)
  802. { case PCOEF:
  803. values = fp->coef;
  804. break;
  805. case PVARI:
  806. case PNLX:
  807. values = fp->nlx;
  808. break;
  809. case PT0:
  810. values = fp->t0;
  811. break;
  812. case PBAND:
  813. vv[0] = fp->h[nv];
  814. return(1);
  815. case PDEGR:
  816. vv[0] = fp->deg[nv];
  817. return(1);
  818. case PLIK:
  819. vv[0] = fp->lik[nv];
  820. return(1);
  821. case PRDF:
  822. vv[0] = fp->lik[2*fp->nvm+nv];
  823. return(1);
  824. default:
  825. LERR(("Invalid what in exvval"));
  826. return(0);
  827. }
  828. vv[0] = values[nv];
  829. if (!fp->hasd) return(1);
  830. if (z)
  831. { for (i=0; i<d; i++) vv[1<<i] = values[(i+1)*fp->nvm+nv];
  832. return(1<<d);
  833. }
  834. else
  835. { for (i=1; i<=d; i++) vv[i] = values[i*fp->nvm+nv];
  836. return(d+1);
  837. }
  838. }
  839. void exvvalpv(vv,vl,vr,d,k,dl,nc)
  840. double *vv, *vl, *vr, dl;
  841. int d, k, nc;
  842. { int i, tk, td;
  843. double f0, f1;
  844. if (nc==1)
  845. { vv[0] = (vl[0]+vr[0])/2;
  846. return;
  847. }
  848. tk = 1<<k;
  849. td = 1<<d;
  850. for (i=0; i<td; i++) if ((i&tk)==0)
  851. { f0 = (vl[i]+vr[i])/2 + dl*(vl[i+tk]-vr[i+tk])/8;
  852. f1 = 1.5*(vr[i]-vl[i])/dl - (vl[i+tk]+vr[i+tk])/4;
  853. vv[i] = f0;
  854. vv[i+tk] = f1;
  855. }
  856. }
  857. double grid_int(fp,evs,x,what)
  858. fitpt *fp;
  859. evstruc *evs;
  860. double *x;
  861. int what;
  862. { int d, i, j, jj, nc, sk, v[MXDIM], vc, z0, nce[1<<MXDIM], *mg;
  863. double *ll, *ur, vv[64][64], z;
  864. d = fp->d;
  865. ll = evpt(fp,0); ur = evpt(fp,fp->nv-1);
  866. mg = mg(evs);
  867. z0 = 0; vc = 1<<d;
  868. for (j=d-1; j>=0; j--)
  869. { v[j] = (int)((mg[j]-1)*(x[j]-ll[j])/(ur[j]-ll[j]));
  870. if (v[j]<0) v[j]=0;
  871. if (v[j]>=mg[j]-1) v[j] = mg[j]-2;
  872. z0 = z0*mg[j]+v[j];
  873. }
  874. nce[0] = z0; nce[1] = z0+1; sk = jj = 1;
  875. for (i=1; i<d; i++)
  876. { sk *= mg[i-1];
  877. jj<<=1;
  878. for (j=0; j<jj; j++)
  879. nce[j+jj] = nce[j]+sk;
  880. }
  881. for (i=0; i<vc; i++)
  882. nc = exvval(fp,vv[i],nce[i],d,what,1);
  883. ll = evpt(fp,nce[0]);
  884. ur = evpt(fp,nce[vc-1]);
  885. z = rectcell_interp(x,vv,ll,ur,d,nc);
  886. return(z);
  887. }
  888. double fitp_int(fp,x,what,i)
  889. fitpt *fp;
  890. double *x;
  891. int what, i;
  892. { double vv[1+MXDIM];
  893. exvval(fp,vv,i,fp->d,what,0);
  894. return(vv[0]);
  895. }
  896. double xbar_int(fp,x,what)
  897. fitpt *fp;
  898. double *x;
  899. int what;
  900. { int i, nc;
  901. double vv[1+MXDIM], f;
  902. nc = exvval(fp,vv,0,fp->d,what,0);
  903. f = vv[0];
  904. if (nc>1)
  905. for (i=0; i<fp->d; i++)
  906. f += vv[i+1]*(x[i]-evptx(fp,0,i));
  907. return(f);
  908. }
  909. double dointpoint(lf,x,what,ev,j)
  910. lfit *lf;
  911. double *x;
  912. int what, ev, j;
  913. { double xf, f, link[LLEN];
  914. int i, rt;
  915. fitpt *fp;
  916. evstruc *evs;
  917. fp = &lf->fp; evs = &lf->evs;
  918. for (i=0; i<fp->d; i++) if (lf->lfd.sty[i]==STANGL)
  919. { xf = floor(x[i]/(2*PI*lf->lfd.sca[i]));
  920. x[i] -= xf*2*PI*lf->lfd.sca[i];
  921. }
  922. if (what > 64)
  923. { rt = what-64;
  924. what = PCOEF;
  925. }
  926. else rt = 0;
  927. switch(ev)
  928. { case EGRID: f = grid_int(fp,evs,x,what); break;
  929. case EKDTR: f = kdtre_int(fp,evs,x,what); break;
  930. case ETREE: f = atree_int(lf,x,what); break;
  931. case EPHULL: f = triang_int(lf,x,what); break;
  932. case EFITP: f = fitp_int(fp,x,what,j); break;
  933. case EXBAR: f = xbar_int(fp,x,what); break;
  934. case ENONE: f = 0; break;
  935. case ESPHR: f = sphere_int(lf,x,what); break;
  936. default: LERR(("dointpoint: cannot interpolate structure %d",ev));
  937. }
  938. if (((what==PT0)|(what==PNLX)) && (f<0)) f = 0.0;
  939. f += addparcomp(lf,x,what);
  940. if (rt>0)
  941. {
  942. stdlinks(link,&lf->lfd,&lf->sp,j,f,rsc(fp));
  943. f = resid(resp(&lf->lfd,j),prwt(&lf->lfd,j),f,fam(&lf->sp),rt,link);
  944. }
  945. return(f);
  946. }
  947. /*
  948. * Copyright 1996-2006 Catherine Loader.
  949. */
  950. /*
  951. * Routines for building and interpolating the kd tree.
  952. * Initially, this started from the loess code.
  953. *
  954. * Todo: EKDCE isn't working.
  955. */
  956. #include "lfev.h"
  957. void newcell();
  958. static int nterm;
  959. void kdtre_guessnv(evs,nvm,ncm,vc,n,d,alp)
  960. evstruc *evs;
  961. double alp;
  962. int *nvm, *ncm, *vc, n, d;
  963. { int k;
  964. if (ev(evs) == EKDTR)
  965. { nterm = (int)(cut(evs)/4 * n * MIN(alp,1.0) );
  966. k = 2*n/nterm;
  967. *vc = 1<<d;
  968. *ncm = 2*k+1;
  969. *nvm = (k+2)**vc/2;
  970. return;
  971. }
  972. if (ev(evs) == EKDCE)
  973. { nterm = (int)(n * alp);
  974. *vc = 1;
  975. *nvm = 1+(int)(2*n/nterm);
  976. *ncm = 2**nvm+1;
  977. return;
  978. }
  979. *nvm = *ncm = *vc = 0;
  980. return;
  981. }
  982. /*
  983. Split x[pi[l..r]] into two equal sized sets.
  984. Let m=(l+r)/2.
  985. At return,
  986. x[pi[l..m]] < x[pi[m+1..r]].
  987. Assuming no ties:
  988. If l+r is odd, the sets have the same size.
  989. If l+r is even, the low set is larger by 1.
  990. If there are ties, all ties go in the low set.
  991. */
  992. int ksmall(l, r, m, x, pi)
  993. int l, r, m, *pi;
  994. double *x;
  995. {
  996. int il, ir, jl, jr;
  997. double t;
  998. while(l<r)
  999. { t = x[pi[m]];
  1000. /*
  1001. permute the observations so that
  1002. x[pi[l..il]] < t <= x[pi[ir..r]].
  1003. */
  1004. ir = l; il = r;
  1005. while (ir<il)
  1006. { while ((ir<=r) && (x[pi[ir]] < t)) ir++;
  1007. while ((il>=l) && (x[pi[il]]>= t)) il--;
  1008. if (ir<il) ISWAP(pi[ir],pi[il]);
  1009. }
  1010. /*
  1011. move = t to the middle:
  1012. x[pi[l..il]] < t
  1013. x[pi[jl..jr]] = t
  1014. x[pi[ir..r]] > t
  1015. */
  1016. jl = ir; jr = r;
  1017. while (ir<jr)
  1018. { while ((ir<=r) && (x[pi[ir]]== t)) ir++;
  1019. while ((jr>=jl) && (x[pi[jr]] > t)) jr--;
  1020. if (ir<jr) ISWAP(pi[ir],pi[jr]);
  1021. }
  1022. /*
  1023. we're done if m is in the middle, jl <= m <= jr.
  1024. */
  1025. if ((jl<=m) & (jr>=m)) return(jr);
  1026. /*
  1027. update l or r.
  1028. */
  1029. if (m>=ir) l = ir;
  1030. if (m<=il) r = il;
  1031. }
  1032. if (l==r) return(l);
  1033. LERR(("ksmall failure"));
  1034. return(0);
  1035. }
  1036. int terminal(lf,p,pi,fc,d,m,split_val)
  1037. lfit *lf;
  1038. int p, d, fc, *m, *pi;
  1039. double *split_val;
  1040. { int i, k, lo, hi, split_var;
  1041. double max, min, score, max_score, t;
  1042. /*
  1043. if there are fewer than fc points in the cell, this cell
  1044. is terminal.
  1045. */
  1046. lo = lf->evs.lo[p]; hi = lf->evs.hi[p];
  1047. if (hi-lo < fc) return(-1);
  1048. /* determine the split variable */
  1049. max_score = 0.0; split_var = 0;
  1050. for (k=0; k<d; k++)
  1051. { max = min = datum(&lf->lfd, k, pi[lo]);
  1052. for (i=lo+1; i<=hi; i++)
  1053. { t = datum(&lf->lfd,k,pi[i]);
  1054. if (t<min) min = t;
  1055. if (t>max) max = t;
  1056. }
  1057. score = (max-min) / lf->lfd.sca[k];
  1058. if (score > max_score)
  1059. { max_score = score;
  1060. split_var = k;
  1061. }
  1062. }
  1063. if (max_score==0) /* all points in the cell are equal */
  1064. return(-1);
  1065. *m = ksmall(lo,hi,(lo+hi)/2, dvari(&lf->lfd,split_var), pi);
  1066. *split_val = datum(&lf->lfd, split_var, pi[*m]);
  1067. if (*m==hi) /* all observations go lo */
  1068. return(-1);
  1069. return(split_var);
  1070. }
  1071. void kdtre_start(des,lf)
  1072. design *des;
  1073. lfit *lf;
  1074. {
  1075. int i, j, vc, d, nc, nv, ncm, nvm, k, m, n, p, *pi;
  1076. double sv;
  1077. d = lf->lfd.d; n = lf->lfd.n; pi = des->ind;
  1078. kdtre_guessnv(&lf->evs,&nvm,&ncm,&vc,n,d,nn(&lf->sp));
  1079. trchck(lf,nvm,ncm,vc);
  1080. nv = 0;
  1081. if (ev(&lf->evs) != EKDCE)
  1082. { for (i=0; i<vc; i++)
  1083. { j = i;
  1084. for (k=0; k<d; ++k)
  1085. { evptx(&lf->fp,i,k) = lf->evs.fl[d*(j%2)+k];
  1086. j >>= 1;
  1087. }
  1088. }
  1089. nv = vc;
  1090. for (j=0; j<vc; j++) lf->evs.ce[j] = j;
  1091. }
  1092. for (i=0; i<n; i++) pi[i] = i;
  1093. p = 0; nc = 1;
  1094. lf->evs.lo[p] = 0; lf->evs.hi[p] = n-1;
  1095. lf->evs.s[p] = -1;
  1096. while (p<nc)
  1097. { k = terminal(lf,p,pi,nterm,d,&m,&sv);
  1098. if (k>=0)
  1099. {
  1100. if ((ncm<nc+2) | (2*nvm<2*nv+vc))
  1101. { WARN(("Insufficient space for full tree"));
  1102. lf->evs.nce = nc; lf->fp.nv = nv;
  1103. return;
  1104. }
  1105. /* new lo cell has obsn's lo[p]..m */
  1106. lf->evs.lo[nc] = lf->evs.lo[p];
  1107. lf->evs.hi[nc] = m;
  1108. lf->evs.s[nc] = -1;
  1109. /* new hi cell has obsn's m+1..hi[p] */
  1110. lf->evs.lo[nc+1] = m+1;
  1111. lf->evs.hi[nc+1] = lf->evs.hi[p];
  1112. lf->evs.s[nc+1] = -1;
  1113. /* cell p is split on variable k, value sv */
  1114. lf->evs.s[p] = k;
  1115. lf->evs.sv[p] = sv;
  1116. lf->evs.lo[p] = nc; lf->evs.hi[p] = nc+1;
  1117. nc=nc+2; i = nv;
  1118. /* now compute the new vertices. */
  1119. if (ev(&lf->evs) != EKDCE)
  1120. newcell(&nv,vc,evp(&lf->fp), d, k, sv,
  1121. &lf->evs.ce[p*vc], &lf->evs.ce[(nc-2)*vc], &lf->evs.ce[(nc-1)*vc]);
  1122. }
  1123. else if (ev(&lf->evs)==EKDCE) /* new vertex at cell center */
  1124. { sv = 0;
  1125. for (i=0; i<d; i++) evptx(&lf->fp,nv,i) = 0;
  1126. for (j=lf->evs.lo[p]; j<=lf->evs.hi[p]; j++)
  1127. { sv += prwt(&lf->lfd,(int)pi[j]);
  1128. for (i=0; i<d; i++)
  1129. evptx(&lf->fp,nv,i) += datum(&lf->lfd,i,pi[j])*prwt(&lf->lfd,(int)pi[j]);
  1130. }
  1131. for (i=0; i<d; i++) evptx(&lf->fp,nv,i) /= sv;
  1132. lf->lfd.n = lf->evs.hi[p] - lf->evs.lo[p] + 1;
  1133. des->ind = &pi[lf->evs.lo[p]]; /* why? */
  1134. PROC_VERTEX(des,lf,nv);
  1135. lf->lfd.n = n; des->ind = pi;
  1136. nv++;
  1137. }
  1138. p++;
  1139. }
  1140. /* We've built the tree. Now do the fitting. */
  1141. if (ev(&lf->evs)==EKDTR)
  1142. for (i=0; i<nv; i++) PROC_VERTEX(des,lf,i);
  1143. lf->evs.nce = nc; lf->fp.nv = nv;
  1144. return;
  1145. }
  1146. void newcell(nv,vc,xev, d, k, split_val, cpar, clef, crig)
  1147. double *xev, split_val;
  1148. int *cpar, *clef, *crig;
  1149. int *nv, vc, d, k;
  1150. { int i, ii, j, j2, tk, match;
  1151. tk = 1<<k;
  1152. for (i=0; i<vc; i++)
  1153. { if ((i&tk) == 0)
  1154. { for (j=0; j<d; j++) xev[*nv*d+j] = xev[d*cpar[i]+j];
  1155. xev[*nv*d+k] = split_val;
  1156. match = 0; j = vc; /* no matches in first vc points */
  1157. while ((j<*nv) && (!match))
  1158. { j2 = 0;
  1159. while ((j2<d) && (xev[*nv*d+j2] == xev[j*d+j2])) j2++;
  1160. match = (j2==d);
  1161. if (!match) j++;
  1162. }
  1163. ii = i+tk;
  1164. clef[i] = cpar[i];
  1165. clef[ii]= crig[i] = j;
  1166. crig[ii]= cpar[ii];
  1167. if (!match) (*nv)++;
  1168. }
  1169. }
  1170. return;
  1171. }
  1172. extern void hermite2();
  1173. double blend(fp,evs,s,x,ll,ur,j,nt,t,what)
  1174. fitpt *fp;
  1175. evstruc *evs;
  1176. double s, *x, *ll, *ur;
  1177. int j, nt, *t, what;
  1178. {
  1179. int *ce, k, k1, m, nc, j0, j1;
  1180. double v0, v1, xibar, g0[3], g1[3], gg[4], gp[4], phi[4];
  1181. ce = evs->ce;
  1182. for (k=0; k<4; k++) /* North South East West */
  1183. { k1 = (k>1);
  1184. v0 = ll[k1]; v1 = ur[k1];
  1185. j0 = ce[j+2*(k==0)+(k==2)];
  1186. j1 = ce[j+3-2*(k==1)-(k==3)];
  1187. xibar = (k%2==0) ? ur[k<2] : ll[k<2];
  1188. m = nt;
  1189. while ((m>=0) && ((evs->s[t[m]] != (k<=1)) | (evs->sv[t[m]] != xibar))) m--;
  1190. if (m >= 0)
  1191. { m = (k%2==1) ? evs->lo[t[m]] : evs->hi[t[m]];
  1192. while (evs->s[m] != -1)
  1193. m = (x[evs->s[m]] < evs->sv[m]) ? evs->lo[m] : evs->hi[m];
  1194. if (v0 < evptx(fp,ce[4*m+2*(k==1)+(k==3)],k1))
  1195. { j0 = ce[4*m+2*(k==1)+(k==3)];
  1196. v0 = evptx(fp,j0,k1);
  1197. }
  1198. if (evptx(fp,ce[4*m+3-2*(k==0)-(k==2)],k1) < v1)
  1199. { j1 = ce[4*m+3-2*(k==0)-(k==2)];
  1200. v1 = evptx(fp,j1,k1);
  1201. }
  1202. }
  1203. nc = exvval(fp,g0,j0,2,what,0);
  1204. nc = exvval(fp,g1,j1,2,what,0);
  1205. if (nc==1)
  1206. gg[k] = linear_interp((x[(k>1)]-v0),v1-v0,g0[0],g1[0]);
  1207. else
  1208. { hermite2(x[(k>1)]-v0,v1-v0,phi);
  1209. gg[k] = phi[0]*g0[0]+phi[1]*g1[0]+(phi[2]*g0[1+k1]+phi[3]*g1[1+k1])*(v1-v0);
  1210. gp[k] = phi[0]*g0[2-k1] + phi[1]*g1[2-k1];
  1211. }
  1212. }
  1213. s = -s;
  1214. if (nc==1)
  1215. for (k=0; k<2; k++)
  1216. s += linear_interp(x[k]-ll[k],ur[k]-ll[k],gg[3-2*k],gg[2-2*k]);
  1217. else
  1218. for (k=0; k<2; k++) /* EW NS */
  1219. { hermite2(x[k]-ll[k],ur[k]-ll[k],phi);
  1220. s += phi[0]*gg[3-2*k] + phi[1]*gg[2-2*k]
  1221. +(phi[2]*gp[3-2*k] + phi[3]*gp[2-2*k]) * (ur[k]-ll[k]);
  1222. }
  1223. return(s);
  1224. }
  1225. double kdtre_int(fp,evs,x,what)
  1226. fitpt *fp;
  1227. evstruc *evs;
  1228. double *x;
  1229. int what;
  1230. {
  1231. int *ce, k, vc, t[20], nt, nc, j, d;
  1232. double *ll, *ur, ff, vv[64][64];
  1233. d = fp->d;
  1234. vc = 1<<d;
  1235. if (d > 6) { LERR(("d too large in kdint")); return(NOSLN); }
  1236. /* descend the tree to find the terminal cell */
  1237. nt = 0; t[nt] = 0; k = 0;
  1238. while (evs->s[k] != -1)
  1239. { nt++;
  1240. if (nt>=20) { LERR(("Too many levels in kdint")); return(NOSLN); }
  1241. k = t[nt] = (x[evs->s[k]] < evs->sv[k]) ? evs->lo[k] : evs->hi[k];
  1242. }
  1243. ce = &evs->ce[k*vc];
  1244. ll = evpt(fp,ce[0]);
  1245. ur = evpt(fp,ce[vc-1]);
  1246. nc = 0;
  1247. for (j=0; j<vc; j++) nc = exvval(fp,vv[j],(int)ce[j],d,what,0);
  1248. ff = rectcell_interp(x,vv,ll,ur,d,nc);
  1249. if (d==2) ff = blend(fp,evs,ff,x,ll,ur,k*vc,nt,t,what);
  1250. return(ff);
  1251. }
  1252. /*
  1253. * Copyright 1996-2006 Catherine Loader.
  1254. */
  1255. #include "lfev.h"
  1256. /*
  1257. * convert eval. structure string to numeric code.
  1258. */
  1259. #define NETYPE 11
  1260. static char *etype[NETYPE]= { "tree", "phull", "data", "grid", "kdtree",
  1261. "kdcenter", "cross", "preset", "xbar", "none",
  1262. "sphere" };
  1263. static int evals[NETYPE]= { ETREE, EPHULL, EDATA, EGRID, EKDTR,
  1264. EKDCE, ECROS, EPRES, EXBAR, ENONE, ESPHR };
  1265. int lfevstr(char *z)
  1266. { return(pmatch(z, etype, evals, NETYPE, -1));
  1267. }
  1268. void evstruc_init(evs)
  1269. evstruc *evs;
  1270. { int i;
  1271. ev(evs) = ETREE;
  1272. mk(evs) = 100;
  1273. cut(evs) = 0.8;
  1274. for (i=0; i<MXDIM; i++)
  1275. { evs->fl[i] = evs->fl[i+MXDIM] = 0.0;
  1276. evs->mg[i] = 10;
  1277. }
  1278. evs->nce = evs->ncm = 0;
  1279. }
  1280. int evstruc_reqi(nvm,ncm,vc)
  1281. int nvm, ncm, vc;
  1282. { return(ncm*vc+3*MAX(ncm,nvm));
  1283. }
  1284. /* al=1: allows dynamic allocation.
  1285. * al=0: inhibit. use with caution.
  1286. */
  1287. void evstruc_alloc(evs,nvm,ncm,vc,al)
  1288. evstruc *evs;
  1289. int nvm, ncm, vc, al;
  1290. { int rw, *k;
  1291. if (al)
  1292. { rw = evstruc_reqi(nvm,ncm,vc);
  1293. if (evs->liw<rw)
  1294. { evs->iwk = (int *)calloc(rw,sizeof(int));
  1295. if ( evs->iwk == NULL ) {
  1296. printf("Problem allocating memory for evs->iwk\n");fflush(stdout);
  1297. }
  1298. evs->liw = rw;
  1299. }
  1300. }
  1301. k = evs->iwk;
  1302. evs->ce = k; k += vc*ncm;
  1303. evs->s = k; k += MAX(ncm,nvm);
  1304. evs->lo = k; k += MAX(ncm,nvm);
  1305. evs->hi = k; k += MAX(ncm,nvm);
  1306. }
  1307. void guessnv(evs,sp,mdl,n,d,lw,nvc)
  1308. evstruc *evs;
  1309. smpar *sp;
  1310. module *mdl;
  1311. int n, d, *lw, *nvc;
  1312. { int i, nvm, ncm, vc;
  1313. npar(sp) = calcp(sp,d);
  1314. switch(ev(evs))
  1315. { case ETREE:
  1316. atree_guessnv(evs,&nvm,&ncm,&vc,d,nn(sp));
  1317. break;
  1318. case EPHULL:
  1319. nvm = ncm = mk(evs)*d;
  1320. vc = d+1;
  1321. break;
  1322. case EDATA:
  1323. case ECROS:
  1324. nvm = n;
  1325. ncm = vc = 0;
  1326. break;
  1327. case EKDTR:
  1328. case EKDCE:
  1329. kdtre_guessnv(evs,&nvm,&ncm,&vc,n,d,nn(sp));
  1330. break;
  1331. case EGRID:
  1332. nvm = 1;
  1333. for (i=0; i<d; i++) nvm *= evs->mg[i];
  1334. ncm = 0;
  1335. vc = 1<<d;
  1336. break;
  1337. case EXBAR:
  1338. case ENONE:
  1339. nvm = 1;
  1340. ncm = vc = 0;
  1341. break;
  1342. case EPRES:
  1343. nvm = evs->mg[0];
  1344. ncm = vc = 0;
  1345. break;
  1346. default:
  1347. LERR(("guessnv: I don't know this evaluation structure."));
  1348. nvm = ncm = vc = 0;
  1349. }
  1350. lw[0] = des_reqd(n,npar(sp));
  1351. lw[1] = lfit_reqd(d,nvm,ncm,mdl);
  1352. lw[2] = evstruc_reqi(nvm,ncm,vc);
  1353. lw[6] = des_reqi(n,npar(sp));
  1354. lw[3] = pc_reqd(d,npar(sp));
  1355. lw[4] = mdl->keepv;
  1356. lw[5] = mdl->keepc;
  1357. if (nvc==NULL) return;
  1358. nvc[0] = nvm;
  1359. nvc[1] = ncm;
  1360. nvc[2] = vc;
  1361. nvc[3] = nvc[4] = 0;
  1362. }
  1363. /*
  1364. * trchck checks the working space on the lfit structure
  1365. * has space for nvm vertices and ncm cells.
  1366. */
  1367. void lfit_alloc(lf)
  1368. lfit *lf;
  1369. { lf->fp.lwk = lf->fp.lev = lf->evs.liw = lf->pc.lwk = 0;
  1370. lf->lf_init_id = LF_INIT_ID;
  1371. }
  1372. int lfit_reqd(d,nvm,ncm,mdl)
  1373. int d, nvm, ncm;
  1374. module *mdl;
  1375. { int z;
  1376. z = mdl->keepv;
  1377. return(nvm*z+ncm);
  1378. }
  1379. void trchck(lf,nvm,ncm,vc)
  1380. lfit *lf;
  1381. int nvm, ncm, vc;
  1382. { int rw, d, *k;
  1383. double *z;
  1384. if (lf->lf_init_id != LF_INIT_ID) lfit_alloc(lf);
  1385. lf->fp.nvm = nvm; lf->evs.ncm = ncm;
  1386. d = lf->lfd.d;
  1387. if (lf->fp.lev < d*nvm)
  1388. { lf->fp.xev = (double *)calloc(d*nvm,sizeof(double));
  1389. if ( lf->fp.xev == NULL ) {
  1390. printf("Problem allocating memory for lf->fp.xev\n");fflush(stdout);
  1391. }
  1392. lf->fp.lev = d*nvm;
  1393. }
  1394. rw = lfit_reqd(d,nvm,ncm,&lf->mdl);
  1395. if (lf->fp.lwk < rw)
  1396. {
  1397. lf->fp.coef = (double *)calloc(rw,sizeof(double));
  1398. if ( lf->fp.coef == NULL ) {
  1399. printf("Problem allocating memory for lf->fp.coef\n");fflush(stdout);
  1400. }
  1401. lf->fp.lwk = rw;
  1402. }
  1403. z = lf->fp.wk = lf->fp.coef;
  1404. lf->fp.h = NULL;
  1405. if (!lf->mdl.isset) mut_printf("module not set.\n");
  1406. else
  1407. { if (lf->mdl.alloc!=NULL) lf->mdl.alloc(lf);
  1408. z += KEEPV(lf) * nvm;
  1409. }
  1410. lf->evs.sv = z; z += ncm;
  1411. evstruc_alloc(&lf->evs,nvm,ncm,vc,1);
  1412. }
  1413. void data_guessnv(nvm,ncm,vc,n)
  1414. int *nvm, *ncm, *vc, n;
  1415. { *nvm = n;
  1416. *ncm = *vc = 0;
  1417. }
  1418. void dataf(des,lf)
  1419. design *des;
  1420. lfit *lf;
  1421. {
  1422. int d, i, j, ncm, nv, vc;
  1423. d = lf->lfd.d;
  1424. data_guessnv(&nv,&ncm,&vc,lf->lfd.n);
  1425. trchck(lf,nv,ncm,vc);
  1426. for (i=0; i<nv; i++)
  1427. for (j=0; j<d; j++) evptx(&lf->fp,i,j) = datum(&lf->lfd,j,i);
  1428. for (i=0; i<nv; i++)
  1429. {
  1430. PROC_VERTEX(des,lf,i);
  1431. lf->evs.s[i] = 0;
  1432. }
  1433. lf->fp.nv = lf->fp.nvm = nv; lf->evs.nce = 0;
  1434. }
  1435. void xbar_guessnv(nvm,ncm,vc)
  1436. int *nvm, *ncm, *vc;
  1437. { *nvm = 1;
  1438. *ncm = *vc = 0;
  1439. return;
  1440. }
  1441. void xbarf(des,lf)
  1442. design *des;
  1443. lfit *lf;
  1444. { int i, d, nvm, ncm, vc;
  1445. d = lf->lfd.d;
  1446. xbar_guessnv(&nvm,&ncm,&vc);
  1447. trchck(lf,1,0,0);
  1448. for (i=0; i<d; i++) evptx(&lf->fp,0,i) = lf->pc.xbar[i];
  1449. PROC_VERTEX(des,lf,0);
  1450. lf->evs.s[0] = 0;
  1451. lf->fp.nv = 1; lf->evs.nce = 0;
  1452. }
  1453. void preset(des,lf)
  1454. design *des;
  1455. lfit *lf;
  1456. { int i, nv;
  1457. nv = lf->fp.nvm;
  1458. trchck(lf,nv,0,0);
  1459. for (i=0; i<nv; i++)
  1460. {
  1461. PROC_VERTEX(des,lf,i);
  1462. lf->evs.s[i] = 0;
  1463. }
  1464. lf->fp.nv = nv; lf->evs.nce = 0;
  1465. }
  1466. void crossf(des,lf)
  1467. design *des;
  1468. lfit *lf;
  1469. { int d, i, j, n, nv, ncm, vc;
  1470. double w;
  1471. n = lf->lfd.n; d = lf->lfd.d;
  1472. data_guessnv(&nv,&ncm,&vc,n);
  1473. trchck(lf,nv,ncm,vc);
  1474. if (lf->lfd.w==NULL) LERR(("crossf() needs prior weights"));
  1475. for (i=0; i<n; i++)
  1476. for (j=0; j<d; j++) evptx(&lf->fp,i,j) = datum(&lf->lfd,j,i);
  1477. for (i=0; i<n; i++)
  1478. { lf->evs.s[i] = 0;
  1479. w = prwt(&lf->lfd,i);
  1480. lf->lfd.w[i] = 0;
  1481. PROC_VERTEX(des,lf,i);
  1482. lf->lfd.w[i] = w;
  1483. }
  1484. lf->fp.nv = n; lf->evs.nce = 0;
  1485. }
  1486. void gridf(des,lf)
  1487. design *des;
  1488. lfit *lf;
  1489. { int d, i, j, nv, u0, u1, z;
  1490. nv = 1; d = lf->lfd.d;
  1491. for (i=0; i<d; i++)
  1492. { if (lf->evs.mg[i]==0)
  1493. lf->evs.mg[i] = 2+(int)((lf->evs.fl[i+d]-lf->evs.fl[i])/(lf->lfd.sca[i]*cut(&lf->evs)));
  1494. nv *= lf->evs.mg[i];
  1495. }
  1496. trchck(lf,nv,0,1<<d);
  1497. for (i=0; i<nv; i++)
  1498. { z = i;
  1499. for (j=0; j<d; j++)
  1500. { u0 = z%lf->evs.mg[j];
  1501. u1 = lf->evs.mg[j]-1-u0;
  1502. evptx(&lf->fp,i,j) = (lf->evs.mg[j]==1) ? lf->evs.fl[j] :
  1503. (u1*lf->evs.fl[j]+u0*lf->evs.fl[j+d])/(lf->evs.mg[j]-1);
  1504. z = z/lf->evs.mg[j];
  1505. }
  1506. lf->evs.s[i] = 0;
  1507. PROC_VERTEX(des,lf,i);
  1508. }
  1509. lf->fp.nv = nv; lf->evs.nce = 0;
  1510. }
  1511. int findpt(fp,evs,i0,i1)
  1512. fitpt *fp;
  1513. evstruc *evs;
  1514. int i0, i1;
  1515. { int i;
  1516. if (i0>i1) ISWAP(i0,i1);
  1517. for (i=i1+1; i<fp->nv; i++)
  1518. if ((evs->lo[i]==i0) && (evs->hi[i]==i1)) return(i);
  1519. return(-1);
  1520. }
  1521. /*
  1522. add a new vertex at the midpoint of (x[i0],x[i1]).
  1523. return the vertex number.
  1524. */
  1525. int newsplit(des,lf,i0,i1,pv)
  1526. design *des;
  1527. lfit *lf;
  1528. int i0, i1, pv;
  1529. { int i, nv;
  1530. i = findpt(&lf->fp,&lf->evs,i0,i1);
  1531. if (i>=0) return(i);
  1532. if (i0>i1) ISWAP(i0,i1);
  1533. nv = lf->fp.nv;
  1534. /* the point is new. Now check we have space for the new point. */
  1535. if (nv>=lf->fp.nvm)
  1536. {
  1537. LERR(("newsplit: out of vertex space"));
  1538. return(-1);
  1539. }
  1540. /* compute the new point, and evaluate the fit */
  1541. lf->evs.lo[nv] = i0;
  1542. lf->evs.hi[nv] = i1;
  1543. for (i=0; i<lf->fp.d; i++)
  1544. evptx(&lf->fp,nv,i) = (evptx(&lf->fp,i0,i)+evptx(&lf->fp,i1,i))/2;
  1545. if (pv) /* pseudo vertex */
  1546. { lf->fp.h[nv] = (lf->fp.h[i0]+lf->fp.h[i1])/2;
  1547. lf->evs.s[nv] = 1; /* pseudo-vertex */
  1548. }
  1549. else /* real vertex */
  1550. {
  1551. PROC_VERTEX(des,lf,nv);
  1552. lf->evs.s[nv] = 0;
  1553. }
  1554. lf->fp.nv++;
  1555. return(nv);
  1556. }
  1557. /*
  1558. * Copyright 1996-2006 Catherine Loader.
  1559. */
  1560. /*
  1561. * Functions for constructing the fit and
  1562. * interpolating on the circle/sphere. d=2 only.
  1563. */
  1564. #include "lfev.h"
  1565. /*
  1566. * Guess the number of fitting points.
  1567. */
  1568. void sphere_guessnv(nvm,ncm,vc,mg)
  1569. int *nvm, *ncm, *vc, *mg;
  1570. { *nvm = mg[1]*(mg[0]+1);
  1571. *ncm = 0;
  1572. *vc = 0;
  1573. }
  1574. void sphere_start(des,lf)
  1575. design *des;
  1576. lfit *lf;
  1577. { int d, i, j, ct, nv, ncm, vc, *mg;
  1578. double rmin, rmax, *orig, r, th, c, s;
  1579. mg = mg(&lf->evs);
  1580. sphere_guessnv(&nv,&ncm,&vc,mg);
  1581. trchck(lf,nv,0,0);
  1582. d = lf->lfd.d;
  1583. rmin = lf->evs.fl[0];
  1584. rmax = lf->evs.fl[1];
  1585. orig = &lf->evs.fl[2];
  1586. rmin = 0; rmax = 1; orig[0] = orig[1] = 0.0;
  1587. ct = 0;
  1588. for (i=0; i<mg[1]; i++)
  1589. { th = 2*PI*i/mg[1];
  1590. c = cos(th);
  1591. s = sin(th);
  1592. for (j=0; j<=mg[0]; j++)
  1593. { r = rmin + (rmax-rmin)*j/mg[0];
  1594. evptx(&lf->fp,ct,0) = orig[0] + r*c;
  1595. evptx(&lf->fp,ct,1) = orig[1] + r*s;
  1596. PROC_VERTEX(des,lf,ct);
  1597. ct++;
  1598. }
  1599. }
  1600. lf->fp.nv = ct;
  1601. lf->evs.nce = 0;
  1602. }
  1603. double sphere_int(lf,x,what)
  1604. lfit *lf;
  1605. double *x;
  1606. int what;
  1607. { double rmin, rmax, *orig, dx, dy, r, th, th0, th1;
  1608. double v[64][64], c0, c1, s0, s1, r0, r1, d0, d1;
  1609. double ll[2], ur[2], xx[2];
  1610. int i0, j0, i1, j1, *mg, nc, ce[4];
  1611. rmin = lf->evs.fl[0];
  1612. rmax = lf->evs.fl[1];
  1613. orig = &lf->evs.fl[2];
  1614. rmin = 0; rmax = 1; orig[0] = orig[1] = 0.0;
  1615. mg = mg(&lf->evs);
  1616. dx = x[0] - orig[0];
  1617. dy = x[1] - orig[1];
  1618. r = sqrt(dx*dx+dy*dy);
  1619. th = atan2(dy,dx); /* between -pi and pi */
  1620. i0 = (int)floor(mg[1]*th/(2*PI)) % mg[1];
  1621. j0 = (int)(mg[0]*(r-rmin)/(rmax-rmin));
  1622. i1 = (i0+1) % mg[1];
  1623. j1 = j0+1; if (j1>mg[0]) { j0 = mg[0]-1; j1 = mg[0]; }
  1624. ce[0] = i0*(mg[0]+1)+j0;
  1625. ce[1] = i0*(mg[0]+1)+j1;
  1626. ce[2] = i1*(mg[0]+1)+j0;
  1627. ce[3] = i1*(mg[0]+1)+j1;
  1628. nc = exvval(&lf->fp,v[0],ce[0],2,what,1);
  1629. nc = exvval(&lf->fp,v[1],ce[1],2,what,1);
  1630. nc = exvval(&lf->fp,v[2],ce[2],2,what,1);
  1631. nc = exvval(&lf->fp,v[3],ce[3],2,what,1);
  1632. th0 = 2*PI*i0/mg[1]; c0 = cos(th0); s0 = sin(th0);
  1633. th1 = 2*PI*i1/mg[1]; c1 = cos(th1); s1 = sin(th1);
  1634. r0 = rmin + j0*(rmax-rmin)/mg[0];
  1635. r1 = rmin + j1*(rmax-rmin)/mg[0];
  1636. d0 = c0*v[0][1] + s0*v[0][2];
  1637. d1 = r0*(c0*v[0][2]-s0*v[0][1]);
  1638. v[0][1] = d0; v[0][2] = d1;
  1639. d0 = c0*v[1][1] + s0*v[1][2];
  1640. d1 = r1*(c0*v[1][2]-s0*v[1][1]);
  1641. v[1][1] = d0; v[1][2] = d1;
  1642. d0 = c1*v[2][1] + s1*v[2][2];
  1643. d1 = r0*(c1*v[2][2]-s1*v[2][1]);
  1644. v[2][1] = d0; v[2][2] = d1;
  1645. d0 = c1*v[3][1] + s1*v[3][2];
  1646. d1 = r1*(c1*v[3][2]-s1*v[3][1]);
  1647. v[3][1] = d0; v[3][2] = d1;
  1648. xx[0] = r; xx[1] = th;
  1649. ll[0] = r0; ll[1] = th0;
  1650. ur[0] = r1; ur[1] = th1;
  1651. return(rectcell_interp(xx,v,ll,ur,2,nc));
  1652. }
  1653. /*
  1654. * Copyright 1996-2006 Catherine Loader.
  1655. */
  1656. #include "lfev.h"
  1657. void solve(A,b,d) /* this is crude! A organized by column. */
  1658. double *A, *b;
  1659. int d;
  1660. { int i, j, k;
  1661. double piv;
  1662. for (i=0; i<d; i++)
  1663. { piv = A[(d+1)*i];
  1664. for (j=i; j<d; j++) A[j*d+i] /= piv;
  1665. b[i] /= piv;
  1666. for (j=0; j<d; j++) if (j != i)
  1667. { piv = A[i*d+j];
  1668. A[i*d+j] = 0;
  1669. for (k=i+1; k<d; k++)
  1670. A[k*d+j] -= piv*A[k*d+i];
  1671. b[j] -= piv*b[i];
  1672. }
  1673. }
  1674. }
  1675. void triang_guessnv(nvm,ncm,vc,d,mk)
  1676. int *nvm, *ncm, *vc, d, mk;
  1677. { *nvm = *ncm = mk*d;
  1678. *vc = d+1;
  1679. return;
  1680. }
  1681. int triang_split(lf,ce,le)
  1682. lfit *lf;
  1683. double *le;
  1684. int *ce;
  1685. { int d, i, j, k, nts, vc;
  1686. double di, dfx[MXDIM];
  1687. nts = 0; d = lf->fp.d; vc = d+1;
  1688. for (i=0; i<d; i++)
  1689. for (j=i+1; j<=d; j++)
  1690. { for (k=0; k<d; k++)
  1691. dfx[k] = evptx(&lf->fp,ce[i],k)-evptx(&lf->fp,ce[j],k);
  1692. di = rho(dfx,lf->lfd.sca,d,KSPH,NULL);
  1693. le[i*vc+j] = le[j*vc+i] = di/MIN(lf->fp.h[ce[i]],lf->fp.h[ce[j]]);
  1694. nts = nts || le[i*vc+j]>cut(&lf->evs);
  1695. }
  1696. return(nts);
  1697. }
  1698. void resort(pv,xev,dig)
  1699. double *xev;
  1700. int *pv, *dig;
  1701. { double d0, d1, d2;
  1702. int i;
  1703. d0 = d1 = d2 = 0;
  1704. for (i=0; i<3; i++)
  1705. { d0 += (xev[3*pv[11]+i]-xev[3*pv[1]+i])*(xev[3*pv[11]+i]-xev[3*pv[1]+i]);
  1706. d1 += (xev[3*pv[ 7]+i]-xev[3*pv[2]+i])*(xev[3*pv[ 7]+i]-xev[3*pv[2]+i]);
  1707. d2 += (xev[3*pv[ 6]+i]-xev[3*pv[3]+i])*(xev[3*pv[ 6]+i]-xev[3*pv[3]+i]);
  1708. }
  1709. if ((d0<=d1) & (d0<=d2))
  1710. { dig[0] = pv[1]; dig[1] = pv[11];
  1711. dig[2] = pv[2]; dig[3] = pv[7];
  1712. dig[4] = pv[3]; dig[5] = pv[6];
  1713. }
  1714. else if (d1<=d2)
  1715. { dig[0] = pv[2]; dig[1] = pv[7];
  1716. dig[2] = pv[1]; dig[3] = pv[11];
  1717. dig[4] = pv[3]; dig[5] = pv[6];
  1718. }
  1719. else
  1720. { dig[0] = pv[3]; dig[1] = pv[6];
  1721. dig[2] = pv[2]; dig[3] = pv[7];
  1722. dig[4] = pv[1]; dig[5] = pv[11];
  1723. }
  1724. }
  1725. void triang_grow(des,lf,ce,ct,term)
  1726. design *des;
  1727. lfit *lf;
  1728. int *ce, *ct, *term;
  1729. { double le[(1+MXDIM)*(1+MXDIM)], ml;
  1730. int d, i, j, im, jm, vc, pv[(1+MXDIM)*(1+MXDIM)], dig[6];
  1731. int nce[1+MXDIM];
  1732. if (lf_error) return;
  1733. d = lf->fp.d; vc = d+1;
  1734. if (!triang_split(lf,ce,le))
  1735. { if (ct != NULL)
  1736. { for (i=0; i<vc; i++) term[*ct*vc+i] = ce[i];
  1737. (*ct)++;
  1738. }
  1739. return;
  1740. }
  1741. if (d>3)
  1742. { ml = 0;
  1743. for (i=0; i<d; i++)
  1744. for (j=i+1; j<vc; j++)
  1745. if (le[i*vc+j]>ml) { ml = le[i*vc+j]; im = i; jm = j; }
  1746. pv[0] = newsplit(des,lf,(int)ce[im],(int)ce[jm],0);
  1747. for (i=0; i<vc; i++) nce[i] = ce[i];
  1748. nce[im] = pv[0]; triang_grow(des,lf,nce,ct,term); nce[im] = ce[im];
  1749. nce[jm] = pv[0]; triang_grow(des,lf,nce,ct,term);
  1750. return;
  1751. }
  1752. for (i=0; i<d; i++)
  1753. for (j=i+1; j<=d; j++)
  1754. pv[i*vc+j] = pv[j*vc+i]
  1755. = newsplit(des,lf,(int)ce[i],(int)ce[j],le[i*vc+j]<=cut(&lf->evs));
  1756. for (i=0; i<=d; i++) /* corners */
  1757. { for (j=0; j<=d; j++) nce[j] = (j==i) ? ce[i] : pv[i*vc+j];
  1758. triang_grow(des,lf,nce,ct,term);
  1759. }
  1760. if (d==2) /* center for d=2 */
  1761. { nce[0] = pv[5]; nce[1] = pv[2]; nce[2] = pv[1];
  1762. triang_grow(des,lf,nce,ct,term);
  1763. }
  1764. if (d==3) /* center for d=3 */
  1765. { resort(pv,evp(&lf->fp),dig);
  1766. nce[0] = dig[0]; nce[1] = dig[1];
  1767. nce[2] = dig[2]; nce[3] = dig[4]; triang_grow(des,lf,nce,ct,term);
  1768. nce[2] = dig[5]; nce[3] = dig[3]; triang_grow(des,lf,nce,ct,term);
  1769. nce[2] = dig[2]; nce[3] = dig[5]; triang_grow(des,lf,nce,ct,term);
  1770. nce[2] = dig[4]; nce[3] = dig[3]; triang_grow(des,lf,nce,ct,term);
  1771. }
  1772. }
  1773. void triang_descend(tr,xa,ce)
  1774. lfit *tr;
  1775. double *xa;
  1776. int *ce;
  1777. { double le[(1+MXDIM)*(1+MXDIM)], ml;
  1778. int d, vc, i, j, im, jm, pv[(1+MXDIM)*(1+MXDIM)];
  1779. design *des;
  1780. des = NULL;
  1781. if (!triang_split(tr,ce,le)) return;
  1782. d = tr->fp.d; vc = d+1;
  1783. if (d>3) /* split longest edge */
  1784. { ml = 0;
  1785. for (i=0; i<d; i++)
  1786. for (j=i+1; j<vc; j++)
  1787. if (le[i*vc+j]>ml) { ml = le[i*vc+j]; im = i; jm = j; }
  1788. pv[0] = newsplit(des,tr,(int)ce[im],(int)ce[jm],0);
  1789. if (xa[im]>xa[jm])
  1790. { xa[im] -= xa[jm]; xa[jm] *= 2; ce[jm] = pv[0]; }
  1791. else
  1792. { xa[jm] -= xa[im]; xa[im] *= 2; ce[im] = pv[0]; }
  1793. triang_descend(tr,xa,ce);
  1794. return;
  1795. }
  1796. for (i=0; i<d; i++)
  1797. for (j=i+1; j<=d; j++)
  1798. pv[i*vc+j] = pv[j*vc+i]
  1799. = newsplit(des,tr,(int)ce[i],(int)ce[j],le[i*d+j]<=cut(&tr->evs));
  1800. for (i=0; i<=d; i++) if (xa[i]>=0.5) /* in corner */
  1801. { for (j=0; j<=d; j++)
  1802. { if (i!=j) ce[j] = pv[i*vc+j];
  1803. xa[j] = 2*xa[j];
  1804. }
  1805. xa[i] -= 1;
  1806. triang_descend(tr,xa,ce);
  1807. return;
  1808. }
  1809. if (d==1) { LERR(("weights sum to < 1")); }
  1810. if (d==2) /* center */
  1811. { ce[0] = pv[5]; xa[0] = 1-2*xa[0];
  1812. ce[1] = pv[2]; xa[1] = 1-2*xa[1];
  1813. ce[2] = pv[1]; xa[2] = 1-2*xa[2];
  1814. triang_descend(tr,xa,ce);
  1815. }
  1816. if (d==3) /* center */
  1817. { double z; int dig[6];
  1818. resort(pv,evp(&tr->fp),dig);
  1819. ce[0] = dig[0]; ce[1] = dig[1];
  1820. xa[0] *= 2; xa[1] *= 2; xa[2] *= 2; xa[3] *= 2;
  1821. if (xa[0]+xa[2]>=1)
  1822. { if (xa[0]+xa[3]>=1)
  1823. { ce[2] = dig[2]; ce[3] = dig[4];
  1824. z = xa[0];
  1825. xa[3] += z-1; xa[2] += z-1; xa[0] = xa[1]; xa[1] = 1-z;
  1826. }
  1827. else
  1828. { ce[2] = dig[2]; ce[3] = dig[5];
  1829. z = xa[3]; xa[3] = xa[1]+xa[2]-1; xa[1] = z;
  1830. z = xa[2]; xa[2] += xa[0]-1; xa[0] = 1-z;
  1831. } }
  1832. else
  1833. { if (xa[1]+xa[2]>=1)
  1834. { ce[2] = dig[5]; ce[3] = dig[3];
  1835. xa[1] = 1-xa[1]; xa[2] -= xa[1]; xa[3] -= xa[1];
  1836. }
  1837. else
  1838. { ce[2] = dig[4]; ce[3] = dig[3];
  1839. z = xa[3]; xa[3] += xa[1]-1; xa[1] = xa[2];
  1840. xa[2] = z+xa[0]-1; xa[0] = 1-z;
  1841. } }
  1842. triang_descend(tr,xa,ce);
  1843. } }
  1844. void covrofdata(lfd,V,mn) /* covar of data; mean in mn */
  1845. lfdata *lfd;
  1846. double *V, *mn;
  1847. { int d, i, j, k;
  1848. double s;
  1849. s = 0; d = lfd->d;
  1850. for (i=0; i<d*d; i++) V[i] = 0;
  1851. for (i=0; i<lfd->n; i++)
  1852. { s += prwt(lfd,i);
  1853. for (j=0; j<d; j++)
  1854. for (k=0; k<d; k++)
  1855. V[j*d+k] += prwt(lfd,i)*(datum(lfd,j,i)-mn[j])*(datum(lfd,k,i)-mn[k]);
  1856. }
  1857. for (i=0; i<d*d; i++) V[i] /= s;
  1858. }
  1859. int intri(x,w,xev,xa,d) /* is x in triangle bounded by xd[0..d-1]? */
  1860. double *x, *xev, *xa;
  1861. int *w, d;
  1862. { int i, j;
  1863. double eps, *r, xd[MXDIM*MXDIM];
  1864. eps = 1.0e-10;
  1865. r = &xev[w[d]*d];
  1866. for (i=0; i<d; i++)
  1867. { xa[i] = x[i]-r[i];
  1868. for (j=0; j<d; j++) xd[i*d+j] = xev[w[i]*d+j]-r[j];
  1869. }
  1870. solve(xd,xa,d);
  1871. xa[d] = 1.0;
  1872. for (i=0; i<d; i++) xa[d] -= xa[i];
  1873. for (i=0; i<=d; i++) if ((xa[i]<-eps) | (xa[i]>1+eps)) return(0);
  1874. return(1);
  1875. }
  1876. void triang_start(des,lf) /* Triangulation with polyhedral start */
  1877. design *des;
  1878. lfit *lf;
  1879. {
  1880. int i, j, k, n, d, nc, nvm, ncm, vc;
  1881. int *ce, ed[1+MXDIM];
  1882. double V[MXDIM*MXDIM], P[MXDIM*MXDIM], sigma, z[MXDIM], xa[1+MXDIM], *xev;
  1883. xev = evp(&lf->fp);
  1884. d = lf->lfd.d; n = lf->lfd.n;
  1885. lf->fp.nv = nc = 0;
  1886. triang_guessnv(&nvm,&ncm,&vc,d,mk(&lf->evs));
  1887. trchck(lf,nvm,ncm,vc);
  1888. ce = lf->evs.ce;
  1889. for (j=0; j<d; j++) xev[j] = lf->pc.xbar[j];
  1890. lf->fp.nv = 1;
  1891. covrofdata(&lf->lfd,V,xev); /* fix this with scaling */
  1892. eig_dec(V,P,d);
  1893. for (i=0; i<d; i++) /* add vertices +- 2sigma*eigenvect */
  1894. { sigma = sqrt(V[i*(d+1)]);
  1895. for (j=0; j<d; j++)
  1896. xev[lf->fp.nv*d+j] = xev[j]-2*sigma*P[j*d+i];
  1897. lf->fp.nv++;
  1898. for (j=0; j<d; j++)
  1899. xev[lf->fp.nv*d+j] = xev[j]+2*sigma*P[j*d+i];
  1900. lf->fp.nv++;
  1901. }
  1902. for (i=0; i<n; i++) /* is point i inside? */
  1903. { ed[0] = 0;
  1904. for (j=0; j<d; j++)
  1905. { z[j] = 0;
  1906. for (k=0; k<d; k++) z[j] += P[k*d+j]*(datum(&lf->lfd,k,i)-xev[k]);
  1907. ed[j+1] = 2*j+1+(z[j]>0);
  1908. for (k=0; k<d; k++) z[j] = datum(&lf->lfd,j,i);
  1909. }
  1910. k = intri(z,ed,xev,xa,d);
  1911. if (xa[0]<0)
  1912. { for (j=1; j<=d; j++)
  1913. for (k=0; k<d; k++)
  1914. xev[ed[j]*d+k] = xa[0]*xev[k]+(1-xa[0])*xev[ed[j]*d+k];
  1915. }
  1916. }
  1917. nc = 1<<d; /* create initial cells */
  1918. for (i=0; i<nc; i++)
  1919. { ce[i*vc] = 0; k = i;
  1920. for (j=0; j<d; j++)
  1921. { ce[i*vc+j+1] = 2*j+(k%2)+1;
  1922. k>>=1;
  1923. }
  1924. }
  1925. for (i=0; i<lf->fp.nv; i++)
  1926. { PROC_VERTEX(des,lf,i);
  1927. if (lf_error) return;
  1928. lf->evs.s[i] = 0;
  1929. }
  1930. for (i=0; i<nc; i++)
  1931. triang_grow(des,lf,&ce[i*vc],NULL,NULL);
  1932. lf->evs.nce = nc;
  1933. }
  1934. double triang_cubicint(v,vv,w,d,nc,xxa)
  1935. double *v, *vv, *xxa;
  1936. int *w, d, nc;
  1937. { double sa, lb, *vert0, *vert1, *vals0, *vals1, deriv0, deriv1;
  1938. int i, j, k;
  1939. if (nc==1) /* linear interpolate */
  1940. { sa = 0;
  1941. for (i=0; i<=d; i++) sa += xxa[i]*vv[i];
  1942. return(sa);
  1943. }
  1944. sa = 1.0;
  1945. for (j=d; j>0; j--) /* eliminate v[w[j]] */
  1946. { lb = xxa[j]/sa;
  1947. for (k=0; k<j; k++) /* Interpolate edge v[w[k]],v[w[j]] */
  1948. { vert0 = &v[w[k]*d];
  1949. vert1 = &v[w[j]*d];
  1950. vals0 = &vv[k*nc];
  1951. vals1 = &vv[j*nc];
  1952. deriv0 = deriv1 = 0;
  1953. for (i=0; i<d; i++)
  1954. { deriv0 += (vert1[i]-vert0[i])*vals0[i+1];
  1955. deriv1 += (vert1[i]-vert0[i])*vals1[i+1];
  1956. }
  1957. vals0[0] = cubic_interp(lb,vals0[0],vals1[0],deriv0,deriv1);
  1958. for (i=1; i<=d; i++)
  1959. vals0[i] = (1-lb)*((1-lb)*vals0[i]+lb*vals1[i]);
  1960. }
  1961. sa -= xxa[j];
  1962. if (sa<=0) j = 0;
  1963. }
  1964. return(vals0[0]);
  1965. }
  1966. double triang_clotoch(xev,vv,ce,p,xxa)
  1967. double *xev, *vv, *xxa;
  1968. int *ce, p;
  1969. { double cfo[3], cfe[3], cg[9], *va, *vb, *vc,
  1970. l0, nm[3], na, nb, nc, *xl, *xr, *xz, d0, d1, lb, dlt, gam;
  1971. int i, w[3], cfl, cfr;
  1972. if (p==1)
  1973. return(xxa[0]*vv[0]+xxa[1]*vv[1]+xxa[2]*vv[2]);
  1974. if (xxa[2]<=MIN(xxa[0],xxa[1]))
  1975. { va = &xev[2*ce[0]]; vb = &xev[2*ce[1]]; vc = &xev[2*ce[2]];
  1976. w[0] = 0; w[1] = 3; w[2] = 6;
  1977. }
  1978. else
  1979. if (xxa[1]<xxa[0])
  1980. { w[0] = 0; w[1] = 6; w[2] = 3;
  1981. va = &xev[2*ce[0]]; vb = &xev[2*ce[2]]; vc = &xev[2*ce[1]];
  1982. lb = xxa[1]; xxa[1] = xxa[2]; xxa[2] = lb;
  1983. }
  1984. else
  1985. { w[0] = 6; w[1] = 3; w[2] = 0;
  1986. va = &xev[2*ce[2]]; vb = &xev[2*ce[1]]; vc = &xev[2*ce[0]];
  1987. lb = xxa[0]; xxa[0] = xxa[2]; xxa[2] = lb;
  1988. }
  1989. /* set cg to values and derivatives on standard triangle */
  1990. for (i=0; i<3; i++)
  1991. { cg[3*i] = vv[w[i]];
  1992. cg[3*i+1] = ((vb[0]-va[0])*vv[w[i]+1]
  1993. +(vb[1]-va[1])*vv[w[i]+2])/2; /* df/dx */
  1994. cg[3*i+2] = ((2*vc[0]-vb[0]-va[0])*vv[w[i]+1]
  1995. +(2*vc[1]-vb[1]-va[1])*vv[w[i]+2])/2.0; /* sqrt{3} df/dy */
  1996. }
  1997. dlt = (vb[0]-va[0])*(vc[1]-va[1])-(vc[0]-va[0])*(vb[1]-va[1]);
  1998. /* Twice area; +ve if abc antic.wise -ve is abc c.wise */
  1999. cfo[0] = (cg[0]+cg[3]+cg[6])/3;
  2000. cfo[1] = (2*cg[0]-cg[3]-cg[6])/4;
  2001. cfo[2] = (2*cg[3]-cg[0]-cg[6])/4;
  2002. na = -cg[1]+cg[2]; /* perp. deriv, rel. length 2 */
  2003. nb = -cg[4]-cg[5];
  2004. nc = 2*cg[7];
  2005. cfo[1] += (nb-nc)/16;
  2006. cfo[2] += (nc-na)/16;
  2007. na = -cg[1]-cg[2]/3.0; /* derivatives back to origin */
  2008. nb = cg[4]-cg[5]/3.0;
  2009. nc = cg[8]/1.5;
  2010. cfo[0] -= (na+nb+nc)*7/54;
  2011. cfo[1] += 13*(nb+nc-2*na)/144;
  2012. cfo[2] += 13*(na+nc-2*nb)/144;
  2013. for (i=0; i<3; i++)
  2014. { /* Outward normals by linear interpolation on original triangle.
  2015. Convert to outward normals on standard triangle.
  2016. Actually, computed to opposite corner */
  2017. switch(i)
  2018. { case 0: xl = vc; xr = vb; xz = va; cfl = w[2]; cfr = w[1];
  2019. break;
  2020. case 1: xl = va; xr = vc; xz = vb; cfl = w[0]; cfr = w[2];
  2021. break;
  2022. case 2: xl = vb; xr = va; xz = vc; cfl = w[1]; cfr = w[0];
  2023. break;
  2024. }
  2025. na = xr[0]-xl[0]; nb = xr[1]-xl[1];
  2026. lb = na*na+nb*nb;
  2027. d0 = 1.5*(vv[cfr]-vv[cfl]) - 0.25*(na*(vv[cfl+1]+vv[cfr+1])
  2028. +nb*(vv[cfl+2]+vv[cfr+2]));
  2029. d1 = 0.5*( na*(vv[cfl+2]+vv[cfr+2])-nb*(vv[cfl+1]+vv[cfr+1]) );
  2030. l0 = (xz[0]-xl[0])*na+(xz[1]-xl[1])*nb-lb/2;
  2031. nm[i] = (d1*dlt-l0*d0)/lb;
  2032. }
  2033. cfo[0] -= (nm[0]+nm[1]+nm[2])*4/81;
  2034. cfo[1] += (2*nm[0]-nm[1]-nm[2])/27;
  2035. cfo[2] += (2*nm[1]-nm[0]-nm[2])/27;
  2036. gam = xxa[0]+xxa[1]-2*xxa[2];
  2037. if (gam==0) return(cfo[0]);
  2038. lb = (xxa[0]-xxa[2])/gam;
  2039. d0 = -2*cg[4]; d1 = -2*cg[1];
  2040. cfe[0] = cubic_interp(lb,cg[3],cg[0],d0,d1);
  2041. cfe[1] = cubintd(lb,cg[3],cg[0],d0,d1);
  2042. cfe[2] = -(1-lb)*(1-2*lb)*cg[5] + 4*lb*(1-lb)*nm[2] - lb*(2*lb-1)*cg[2];
  2043. d0 = 2*(lb*cfo[1]+(1-lb)*cfo[2]);
  2044. d1 = (lb-0.5)*cfe[1]+cfe[2]/3.0;
  2045. return(cubic_interp(gam,cfo[0],cfe[0],d0,d1));
  2046. }
  2047. int triang_getvertexvals(fp,evs,vv,i,what)
  2048. fitpt *fp;
  2049. evstruc *evs;
  2050. double *vv;
  2051. int i, what;
  2052. { double dx, P, le, vl[1+MXDIM], vh[1+MXDIM];
  2053. int d, il, ih, j, nc;
  2054. d = fp->d;
  2055. if (evs->s[i]==0) return(exvval(fp,vv,i,d,what,0));
  2056. il = evs->lo[i]; nc = triang_getvertexvals(fp,evs,vl,il,what);
  2057. ih = evs->hi[i]; nc = triang_getvertexvals(fp,evs,vh,ih,what);
  2058. vv[0] = (vl[0]+vh[0])/2;
  2059. if (nc==1) return(nc);
  2060. P = 1.5*(vh[0]-vl[0]);
  2061. le = 0.0;
  2062. for (j=0; j<d; j++)
  2063. { dx = evptx(fp,ih,j)-evptx(fp,il,j);
  2064. vv[0] += dx*(vl[j+1]-vh[j+1])/8;
  2065. vv[j+1] = (vl[j+1]+vh[j+1])/2;
  2066. P -= 1.5*dx*vv[j+1];
  2067. le += dx*dx;
  2068. }
  2069. for (j=0; j<d; j++)
  2070. vv[j+1] += P*(evptx(fp,ih,j)-evptx(fp,il,j))/le;
  2071. return(nc);
  2072. }
  2073. double triang_int(lf,x,what)
  2074. lfit *lf;
  2075. double *x;
  2076. int what;
  2077. {
  2078. int d, i, j, k, vc, nc;
  2079. int *ce, nce[1+MXDIM];
  2080. double xa[1+MXDIM], vv[(1+MXDIM)*(1+MXDIM)], lb;
  2081. fitpt *fp;
  2082. evstruc *evs;
  2083. fp = &lf->fp;
  2084. evs= &lf->evs;
  2085. d = fp->d; vc = d+1;
  2086. ce = evs->ce;
  2087. i = 0;
  2088. while ((i<evs->nce) && (!intri(x,&ce[i*vc],evp(fp),xa,d))) i++;
  2089. if (i==evs->nce) return(NOSLN);
  2090. i *= vc;
  2091. for (j=0; j<vc; j++) nce[j] = ce[i+j];
  2092. triang_descend(lf,xa,nce);
  2093. /* order the vertices -- needed for asymmetric interptr */
  2094. do
  2095. { k=0;
  2096. for (i=0; i<d; i++)
  2097. if (nce[i]>nce[i+1])
  2098. { j=nce[i]; nce[i]=nce[i+1]; nce[i+1]=j; k=1;
  2099. lb = xa[i]; xa[i] = xa[i+1]; xa[i+1] = lb;
  2100. }
  2101. } while(k);
  2102. nc = 0;
  2103. for (i=0; i<vc; i++)
  2104. nc = triang_getvertexvals(fp,evs,&vv[i*nc],nce[i],what);
  2105. return((d==2) ? triang_clotoch(evp(fp),vv,nce,nc,xa) :
  2106. triang_cubicint(evp(fp),vv,nce,d,nc,xa));
  2107. }
  2108. /*
  2109. * Copyright 1996-2006 Catherine Loader.
  2110. */
  2111. /*
  2112. * Functions for computing residuals and fitted values from
  2113. * the locfit object.
  2114. *
  2115. * fitted(lf,fit,what,cv,ty) computes fitted values from the
  2116. * fit structure in lf.
  2117. * resid(y,c,w,th,mi,ty) converts fitted values to residuals
  2118. */
  2119. #include "lfev.h"
  2120. #define NRT 8
  2121. static char *rtype[NRT] = { "deviance", "d2", "pearson", "raw",
  2122. "ldot", "lddot", "fit", "mean" };
  2123. static int rvals[NRT] = { RDEV, RDEV2, RPEAR, RRAW, RLDOT, RLDDT, RFIT, RMEAN};
  2124. int restyp(z)
  2125. char *z;
  2126. { int val;
  2127. val = pmatch(z, rtype, rvals, NRT, -1);
  2128. if (val==-1) LERR(("Unknown type = %s",z));
  2129. return(val);
  2130. }
  2131. double resid(y,w,th,fam,ty,res)
  2132. int fam, ty;
  2133. double y, w, th, *res;
  2134. { double raw;
  2135. fam = fam & 63;
  2136. if ((fam==TGAUS) | (fam==TROBT) | (fam==TCAUC))
  2137. raw = y-res[ZMEAN];
  2138. else
  2139. raw = y-w*res[ZMEAN];
  2140. switch(ty)
  2141. { case RDEV:
  2142. if (res[ZDLL]>0) return(sqrt(-2*res[ZLIK]));
  2143. else return(-sqrt(-2*res[ZLIK]));
  2144. case RPEAR:
  2145. if (res[ZDDLL]<=0)
  2146. { if (res[ZDLL]==0) return(0);
  2147. return(NOSLN);
  2148. }
  2149. return(res[ZDLL]/sqrt(res[ZDDLL]));
  2150. case RRAW: return(raw);
  2151. case RLDOT: return(res[ZDLL]);
  2152. case RDEV2: return(-2*res[ZLIK]);
  2153. case RLDDT: return(res[ZDDLL]);
  2154. case RFIT: return(th);
  2155. case RMEAN: return(res[ZMEAN]);
  2156. default: LERR(("resid: unknown residual type %d",ty));
  2157. }
  2158. return(0.0);
  2159. }
  2160. double studentize(res,inl,var,ty,link)
  2161. double res, inl, var, *link;
  2162. int ty;
  2163. { double den;
  2164. inl *= link[ZDDLL];
  2165. var = var*var*link[ZDDLL];
  2166. if (inl>1) inl = 1;
  2167. if (var>inl) var = inl;
  2168. den = 1-2*inl+var;
  2169. if (den<0) return(0.0);
  2170. switch(ty)
  2171. { case RDEV:
  2172. case RPEAR:
  2173. case RRAW:
  2174. case RLDOT:
  2175. return(res/sqrt(den));
  2176. case RDEV2:
  2177. return(res/den);
  2178. default: return(res);
  2179. }
  2180. }
  2181. void fitted(lf,fit,what,cv,st,ty)
  2182. lfit *lf;
  2183. double *fit;
  2184. int what, cv, st, ty;
  2185. { int i, j, d, n, evo;
  2186. double xx[MXDIM], th, inl, var, link[LLEN];
  2187. n = lf->lfd.n;
  2188. d = lf->lfd.d;
  2189. evo = ev(&lf->evs);
  2190. cv &= (evo!=ECROS);
  2191. if ((evo==EDATA)|(evo==ECROS)) evo = EFITP;
  2192. setfamily(&lf->sp);
  2193. for (i=0; i<n; i++)
  2194. { for (j=0; j<d; j++) xx[j] = datum(&lf->lfd,j,i);
  2195. th = dointpoint(lf,xx,what,evo,i);
  2196. if ((what==PT0)|(what==PVARI)) th = th*th;
  2197. if (what==PCOEF)
  2198. {
  2199. th += base(&lf->lfd,i);
  2200. stdlinks(link,&lf->lfd,&lf->sp,i,th,rsc(&lf->fp));
  2201. if ((cv)|(st))
  2202. { inl = dointpoint(lf,xx,PT0,evo,i);
  2203. inl = inl*inl;
  2204. if (cv)
  2205. { th -= inl*link[ZDLL];
  2206. stdlinks(link,&lf->lfd,&lf->sp,i,th,rsc(&lf->fp));
  2207. }
  2208. if (st) var = dointpoint(lf,xx,PNLX,evo,i);
  2209. }
  2210. fit[i] = resid(resp(&lf->lfd,i),prwt(&lf->lfd,i),th,fam(&lf->sp),ty,link);
  2211. if (st) fit[i] = studentize(fit[i],inl,var,ty,link);
  2212. } else fit[i] = th;
  2213. if (lf_error) return;
  2214. }
  2215. }
  2216. /*
  2217. * Copyright 1996-2006 Catherine Loader.
  2218. */
  2219. #include "lfev.h"
  2220. extern double robscale;
  2221. /* special version of ressumm to estimate sigma^2, with derivative estimation */
  2222. void ressummd(lf)
  2223. lfit *lf;
  2224. { int i;
  2225. double s0, s1;
  2226. s0 = s1 = 0.0;
  2227. if ((fam(&lf->sp)&64)==0)
  2228. { rv(&lf->fp) = 1.0;
  2229. return;
  2230. }
  2231. for (i=0; i<lf->fp.nv; i++)
  2232. { s0 += lf->fp.lik[2*lf->fp.nvm+i];
  2233. s1 += lf->fp.lik[i];
  2234. }
  2235. if (s0==0.0)
  2236. rv(&lf->fp) = 0.0;
  2237. else
  2238. rv(&lf->fp) = -2*s1/s0;
  2239. }
  2240. /*
  2241. * res[0] = log-likelihood.
  2242. * res[1] = df0 = tr(H)
  2243. * res[2] = df0 = tr(H'H)
  2244. * res[3] = s^2.
  2245. * res[5] = robustscale.
  2246. */
  2247. void ressumm(lf,des,res)
  2248. lfit *lf;
  2249. design *des;
  2250. double *res;
  2251. { int i, j, evo, tg;
  2252. double *oy, pw, r1, r2, rdf, t0, t1, u[MXDIM], link[LLEN];
  2253. fitpt *fp;
  2254. res[0] = res[1] = res[2] = 0.0;
  2255. evo = ev(&lf->evs);
  2256. if ((evo==EKDCE) | (evo==EPRES))
  2257. { res[3] = 1.0;
  2258. return;
  2259. }
  2260. if (lf->dv.nd>0)
  2261. { ressummd(lf);
  2262. return;
  2263. }
  2264. r1 = r2 = 0.0;
  2265. if ((evo==EDATA) | (evo==ECROS)) evo = EFITP;
  2266. for (i=0; i<lf->lfd.n; i++)
  2267. { for (j=0; j<lf->lfd.d; j++) u[j] = datum(&lf->lfd,j,i);
  2268. fitv(des,i) = base(&lf->lfd,i)+dointpoint(lf,u,PCOEF,evo,i);
  2269. des->wd[i] = resp(&(lf->lfd),i) - fitv(des,i);
  2270. wght(des,i) = 1.0;
  2271. des->ind[i] = i;
  2272. }
  2273. tg = fam(&lf->sp);
  2274. res[5] = 1.0;
  2275. if ((tg==TROBT+64) | (tg==TCAUC+64)) /* global robust scale */
  2276. { oy = lf->lfd.y; lf->lfd.y = des->wd;
  2277. des->xev = lf->pc.xbar;
  2278. locfit(&lf->lfd,des,&lf->sp,1,0,0);
  2279. lf->lfd.y = oy;
  2280. res[5] = robscale;
  2281. }
  2282. for (i=0; i<lf->lfd.n; i++)
  2283. { for (j=0; j<lf->lfd.d; j++) u[j] = datum(&lf->lfd,j,i);
  2284. t0 = dointpoint(lf,u,PT0,evo,i);
  2285. t1 = dointpoint(lf,u,PNLX,evo,i);
  2286. stdlinks(link,&lf->lfd,&lf->sp,i,fitv(des,i),res[5]);
  2287. t1 = t1*t1*link[ZDDLL];
  2288. t0 = t0*t0*link[ZDDLL];
  2289. if (t1>1) t1 = 1;
  2290. if (t0>1) t0 = 1; /* no observation gives >1 deg.free */
  2291. res[0] += link[ZLIK];
  2292. res[1] += t0;
  2293. res[2] += t1;
  2294. pw = prwt(&lf->lfd,i);
  2295. if (pw>0)
  2296. { r1 += link[ZDLL]*link[ZDLL]/pw;
  2297. r2 += link[ZDDLL]/pw;
  2298. }
  2299. }
  2300. res[3] = 1.0;
  2301. if ((fam(&lf->sp)&64)==64) /* quasi family */
  2302. { rdf = lf->lfd.n-2*res[1]+res[2];
  2303. if (rdf<1.0)
  2304. { WARN(("Estimated rdf < 1.0; not estimating variance"));
  2305. }
  2306. else
  2307. res[3] = r1/r2 * lf->lfd.n / rdf;
  2308. }
  2309. /* try to ensure consistency for family="circ"! */
  2310. if (((fam(&lf->sp)&63)==TCIRC) & (lf->lfd.d==1))
  2311. { int *ind, nv;
  2312. double dlt, th0, th1;
  2313. ind = des->ind;
  2314. nv = fp->nv;
  2315. for (i=0; i<nv; i++) ind[i] = i;
  2316. lforder(ind,evp(fp),0,nv-1);
  2317. for (i=1; i<nv; i++)
  2318. { dlt = evptx(fp,ind[i],0)-evptx(fp,ind[i-1],0);
  2319. th0 = fp->coef[ind[i]]-dlt*fp->coef[ind[i]+nv]-fp->coef[ind[i-1]];
  2320. th1 = fp->coef[ind[i]]-dlt*fp->coef[ind[i-1]+nv]-fp->coef[ind[i-1]];
  2321. if ((th0>PI)&(th1>PI))
  2322. { for (j=0; j<i; j++)
  2323. fp->coef[ind[j]] += 2*PI;
  2324. i--;
  2325. }
  2326. if ((th0<(-PI))&(th1<(-PI)))
  2327. { for (j=0; j<i; j++)
  2328. fp->coef[ind[j]] -= 2*PI;
  2329. i--;
  2330. }
  2331. }
  2332. }
  2333. }
  2334. double rss(lf,des,df)
  2335. lfit *lf;
  2336. design *des;
  2337. double *df;
  2338. { double ss, res[10];
  2339. ss = 0;
  2340. ressumm(lf,des,res);
  2341. *df = lf->lfd.n - 2*res[1] + res[2];
  2342. return(-2*res[0]);
  2343. }
  2344. /*
  2345. * Copyright 1996-2006 Catherine Loader.
  2346. */
  2347. /*
  2348. *
  2349. * Derivative corrections. The local slopes are not the derivatives
  2350. * of the local likelihood estimate; the function dercor() computes
  2351. * the adjustment to get the correct derivatives under the assumption
  2352. * that h is constant.
  2353. *
  2354. * By differentiating the local likelihood equations, one obtains
  2355. *
  2356. * d ^ ^ T -1 T d . ^
  2357. * -- a = a - (X W V X) X -- W l( Y, X a)
  2358. * dx 0 1 dx
  2359. */
  2360. #include "lfev.h"
  2361. extern double robscale;
  2362. void dercor(lfd,sp,des,coef)
  2363. lfdata *lfd;
  2364. smpar *sp;
  2365. design *des;
  2366. double *coef;
  2367. { double s1, dc[MXDIM], wd, link[LLEN];
  2368. int i, ii, j, m, d, p;
  2369. if (fam(sp)<=THAZ) return;
  2370. if (ker(sp)==WPARM) return;
  2371. d = lfd->d;
  2372. p = des->p; m = des->n;
  2373. if (lf_debug>1) mut_printf(" Correcting derivatives\n");
  2374. fitfun(lfd, sp, des->xev,des->xev,des->f1,NULL);
  2375. jacob_solve(&des->xtwx,des->f1);
  2376. setzero(dc,d);
  2377. /* correction term is e1^T (XTWVX)^{-1} XTW' ldot. */
  2378. for (i=0; i<m; i++)
  2379. { s1 = innerprod(des->f1,d_xi(des,i),p);
  2380. ii = des->ind[i];
  2381. stdlinks(link,lfd,sp,ii,fitv(des,ii),robscale);
  2382. for (j=0; j<d; j++)
  2383. { wd = wght(des,ii)*weightd(datum(lfd,j,ii)-des->xev[j],lfd->sca[j],
  2384. d,ker(sp),kt(sp),des->h,lfd->sty[j],dist(des,ii));
  2385. dc[j] += s1*wd*link[ZDLL];
  2386. }
  2387. }
  2388. for (j=0; j<d; j++) coef[j+1] += dc[j];
  2389. }
  2390. /*
  2391. * Copyright 1996-2006 Catherine Loader.
  2392. */
  2393. #include "lfev.h"
  2394. void allocallcf(lf)
  2395. lfit *lf;
  2396. { lf->fp.coef = VVEC(lf,0);
  2397. lf->fp.h = VVEC(lf,NPAR(lf));
  2398. }
  2399. int procvallcf(des,lf,v)
  2400. design *des;
  2401. lfit *lf;
  2402. int v;
  2403. { int i, p, lf_status;
  2404. lf_status = procv_nov(des,lf,v);
  2405. if (lf_error) return(lf_status);
  2406. p = NPAR(lf);
  2407. for (i=0; i<p; i++) VVAL(lf,v,i) = des->cf[i];
  2408. lf->fp.h[v] = des->h;
  2409. return(0);
  2410. }
  2411. void initallcf(lf)
  2412. lfit *lf;
  2413. { PROCV(lf) = procvallcf;
  2414. ALLOC(lf) = allocallcf;
  2415. PPROC(lf) = NULL;
  2416. KEEPV(lf) = NPAR(lf)+1;
  2417. KEEPC(lf) = 0;
  2418. NOPC(lf) = 1;
  2419. }
  2420. /*
  2421. * Copyright 1996-2006 Catherine Loader.
  2422. */
  2423. #include "lfev.h"
  2424. void pprocgam(lf,des,res)
  2425. lfit *lf;
  2426. design *des;
  2427. double *res;
  2428. { int i, j, n, evo, op;
  2429. double *fv, *vr, df, t0, t1, u[MXDIM], link[LLEN];
  2430. n = lf->lfd.n;
  2431. fv = res;
  2432. vr = &res[n];
  2433. df = 0.0;
  2434. evo = ev(&lf->evs);
  2435. if (evo==EDATA) evo = EFITP;
  2436. for (i=0; i<n; i++)
  2437. { for (j=0; j<lf->lfd.d; j++) u[j] = datum(&lf->lfd,j,i);
  2438. fitv(des,i) = base(&lf->lfd,i)+dointpoint(lf,u,PCOEF,evo,i);
  2439. lf->lfd.y[i] -= fitv(des,i);
  2440. wght(des,i) = 1.0;
  2441. des->ind[i] = i;
  2442. t0 = dointpoint(lf,u,PT0,evo,i);
  2443. t1 = dointpoint(lf,u,PNLX,evo,i);
  2444. stdlinks(link,&lf->lfd,&lf->sp,i,fitv(des,i),1.0);
  2445. t0 = t0*t0*link[ZDDLL];
  2446. t1 = t1*t1*link[ZDDLL];
  2447. if (t0>1) t0 = 1; /* no observation gives >1 deg.free */
  2448. if (t1>1) t1 = 1; /* no observation gives >1 deg.free */
  2449. vr[i] = t1;
  2450. df += t0;
  2451. }
  2452. des->n = n;
  2453. deg(&lf->sp) = 1;
  2454. op = npar(&lf->sp);
  2455. npar(&lf->sp) = des->p = 1+lf->lfd.d;
  2456. des->xev = lf->pc.xbar;
  2457. locfit(&lf->lfd,des,&lf->sp,1,0,0);
  2458. for (i=0; i<n; i++) fv[i] = resp(&lf->lfd,i) - fitv(des,i);
  2459. for (i=0; i<=lf->lfd.d; i++)
  2460. lf->pc.coef[i] += des->cf[i];
  2461. res[2*n] = df-2;
  2462. npar(&lf->sp) = op;
  2463. }
  2464. void initgam(lf)
  2465. lfit *lf;
  2466. { initstd(lf);
  2467. PPROC(lf) = pprocgam;
  2468. KEEPC(lf) = 2*NOBS(lf)+1;
  2469. }
  2470. /*
  2471. * Copyright 1996-2006 Catherine Loader.
  2472. */
  2473. #include "lfev.h"
  2474. int procvhatm(des,lf,v)
  2475. design *des;
  2476. lfit *lf;
  2477. int v;
  2478. { int k;
  2479. double *l;
  2480. l = &lf->fp.coef[v*lf->lfd.n];
  2481. if ((ker(&lf->sp)!=WPARM) | (!haspc(&lf->pc)))
  2482. { k = procv_nov(des,lf,v);
  2483. wdiag(&lf->lfd,&lf->sp,des,l,&lf->dv,0,1,1);
  2484. }
  2485. else
  2486. wdiagp(&lf->lfd,&lf->sp,des,l,&lf->pc,&lf->dv,0,1,1);
  2487. lf->fp.h[v] = des->h;
  2488. return(k);
  2489. }
  2490. void allochatm(lf)
  2491. lfit *lf;
  2492. { lf->fp.coef = VVEC(lf,0);
  2493. lf->fp.h = VVEC(lf,NOBS(lf));
  2494. }
  2495. void pprochatm(lf,des,res)
  2496. lfit *lf;
  2497. design *des;
  2498. double *res;
  2499. { transpose(lf->fp.coef,lf->fp.nvm,lf->lfd.n);
  2500. }
  2501. void inithatm(lf)
  2502. lfit *lf;
  2503. { PROCV(lf) = procvhatm;
  2504. ALLOC(lf) = allochatm;
  2505. PPROC(lf) = pprochatm;
  2506. KEEPV(lf) = NOBS(lf)+1;
  2507. KEEPC(lf) = 1;
  2508. NOPC(lf) = 1; /* could use pc if mi[MKER] == WPARM */
  2509. }
  2510. /*
  2511. * Copyright 1996-2006 Catherine Loader.
  2512. */
  2513. #include "lfev.h"
  2514. static lfit *lf_scb;
  2515. static lfdata *lfd_scb;
  2516. static smpar *scb_sp;
  2517. static design *des_scb;
  2518. int scbfitter(x,l,reqd)
  2519. double *x, *l;
  2520. int reqd;
  2521. {
  2522. int m;
  2523. des_scb->xev = x;
  2524. if ((ker(scb_sp)!=WPARM) | (!haspc(&lf_scb->pc)))
  2525. { locfit(lfd_scb,des_scb,&lf_scb->sp,1,1,0);
  2526. m = wdiag(lfd_scb, scb_sp, des_scb,l,&lf_scb->dv,reqd,2,0);
  2527. }
  2528. else
  2529. m = wdiagp(lfd_scb, scb_sp, des_scb,l,&lf_scb->pc,&lf_scb->dv,reqd,2,0);
  2530. return(m);
  2531. }
  2532. int constants(lf,des,res)
  2533. lfit *lf;
  2534. design *des;
  2535. double *res;
  2536. {
  2537. int d, m, nt;
  2538. double *wk;
  2539. evstruc *evs;
  2540. lf_scb = lf;
  2541. des_scb = des;
  2542. lfd_scb = &lf->lfd;
  2543. scb_sp = &lf->sp;
  2544. evs = &lf->evs;
  2545. d = lfd_scb->d;
  2546. m = lfd_scb->n;
  2547. trchck(lf,0,0,0);
  2548. if (lf_error) return(0);
  2549. if ((ker(scb_sp) != WPARM) && (lf->sp.nn>0))
  2550. WARN(("constants are approximate for varying h"));
  2551. npar(scb_sp) = calcp(scb_sp,lf->lfd.d);
  2552. des_init(des,m,npar(scb_sp));
  2553. set_scales(&lf->lfd);
  2554. set_flim(&lf->lfd,&lf->evs);
  2555. compparcomp(des,&lf->lfd,&lf->sp,&lf->pc,ker(scb_sp)!=WPARM);
  2556. wk = &res[d+1];
  2557. nt = tube_constants(scbfitter,d,m,ev(evs),mg(evs),evs->fl,
  2558. res,wk,(d>3) ? 4 : d+1,0);
  2559. lf->evs.nce = nt; /* cheat way to return it to S. */
  2560. return(nt);
  2561. }
  2562. void initkappa(lf)
  2563. lfit *lf;
  2564. { PROCV(lf) = NULL;
  2565. ALLOC(lf) = NULL;
  2566. PPROC(lf) = (void *)constants;
  2567. KEEPV(lf) = 0;
  2568. KEEPC(lf) = NVAR(lf)+1+k0_reqd(NVAR(lf),NOBS(lf),0);
  2569. NOPC(lf) = 0;
  2570. }
  2571. /*
  2572. * Copyright 1996-2006 Catherine Loader.
  2573. */
  2574. #include "lfev.h"
  2575. /* fix df computation for link=IDENT. */
  2576. void pproclscv(lf,des,res)
  2577. lfit *lf;
  2578. design *des;
  2579. double *res;
  2580. { double df, fh, fh_cv, infl, z0, z1, x[MXDIM];
  2581. int i, n, j, evo;
  2582. z1 = df = 0.0;
  2583. evo = ev(&lf->evs);
  2584. n = lf->lfd.n;
  2585. if ((evo==EDATA) | (evo==ECROS)) evo = EFITP;
  2586. z0 = dens_integrate(lf,des,2);
  2587. for (i=0; i<n; i++)
  2588. { for (j=0; j<lf->lfd.d; j++) x[j] = datum(&lf->lfd,j,i);
  2589. fh = base(&lf->lfd,i)+dointpoint(lf,x,PCOEF,evo,i);
  2590. if (link(&lf->sp)==LLOG) fh = exp(fh);
  2591. infl = dointpoint(lf,x,PT0,evo,i);
  2592. infl = infl * infl;
  2593. if (infl>1) infl = 1;
  2594. fh_cv = (link(&lf->sp) == LIDENT) ?
  2595. (n*fh - infl) / (n-1.0) : fh*(1-infl)*n/(n-1.0);
  2596. z1 += fh_cv;
  2597. df += infl;
  2598. }
  2599. res[0] = z0-2*z1/n;
  2600. res[1] = df;
  2601. }
  2602. void initlscv(lf)
  2603. lfit *lf;
  2604. { initstd(lf);
  2605. KEEPC(lf) = 2;
  2606. PPROC(lf) = pproclscv;
  2607. NOPC(lf) = 1;
  2608. }
  2609. /*
  2610. * Copyright 1996-2006 Catherine Loader.
  2611. */
  2612. #include "lfev.h"
  2613. static double pen, sig2;
  2614. void goldensec(f,des,tr,eps,xm,ym,meth)
  2615. double (*f)(), eps, *xm, *ym;
  2616. int meth;
  2617. design *des;
  2618. lfit *tr;
  2619. { double x[4], y[4], xx[11], yy[11];
  2620. int i, im;
  2621. xx[0] = tr->sp.fixh;
  2622. if (xx[0]<=0)
  2623. { LERR(("regband: initialize h>0"));
  2624. return;
  2625. }
  2626. for (i=0; i<=10; i++)
  2627. { if (i>0) xx[i] = (1+gold_rat)*xx[i-1];
  2628. yy[i] = f(xx[i],des,tr,meth);
  2629. if ((i==0) || (yy[i]<yy[im])) im = i;
  2630. }
  2631. if (im==0) im = 1;
  2632. if (im==10)im = 9;
  2633. x[0] = xx[im-1]; y[0] = yy[im-1];
  2634. x[1] = xx[im]; y[1] = yy[im];
  2635. x[3] = xx[im+1]; y[3] = yy[im+1];
  2636. x[2] = gold_rat*x[3]+(1-gold_rat)*x[0];
  2637. y[2] = f(x[2],des,tr,meth);
  2638. while (x[3]-x[0]>eps)
  2639. { if (y[1]<y[2])
  2640. { x[3] = x[2]; y[3] = y[2];
  2641. x[2] = x[1]; y[2] = y[1];
  2642. x[1] = gold_rat*x[0]+(1-gold_rat)*x[3];
  2643. y[1] = f(x[1],des,tr,meth);
  2644. }
  2645. else
  2646. { x[0] = x[1]; y[0] = y[1];
  2647. x[1] = x[2]; y[1] = y[2];
  2648. x[2] = gold_rat*x[3]+(1-gold_rat)*x[0];
  2649. y[2] = f(x[2],des,tr,meth);
  2650. }
  2651. }
  2652. im = 0;
  2653. for (i=1; i<4; i++) if (y[i]<y[im]) im = i;
  2654. *xm = x[im]; *ym = y[im];
  2655. }
  2656. double dnk(x,k)
  2657. double x;
  2658. int k;
  2659. { double f;
  2660. switch(k)
  2661. { case 0: f = 1; break;
  2662. case 1: f = -x; break;
  2663. case 2: f = x*x-1; break;
  2664. case 3: f = x*(x*x-3); break;
  2665. case 4: f = 3-x*x*(6-x*x); break;
  2666. case 5: f = -x*(15-x*x*(10-x*x)); break;
  2667. case 6: f = -15+x*x*(45-x*x*(15-x*x)); break;
  2668. default: LERR(("dnk: k=%d too large",k)); return(0.0);
  2669. }
  2670. return(f*exp(-x*x/2)/S2PI);
  2671. }
  2672. double locai(h,des,lf)
  2673. double h;
  2674. design *des;
  2675. lfit *lf;
  2676. { double cp, res[10];
  2677. nn(&lf->sp) = h;
  2678. lf->mdl.procv = procvstd;
  2679. nstartlf(des,lf);
  2680. ressumm(lf,des,res);
  2681. cp = -2*res[0] + pen*res[1];
  2682. return(cp);
  2683. }
  2684. static int fc;
  2685. double loccp(h,des,lf,m) /* m=1: cp m=2: gcv */
  2686. double h;
  2687. design *des;
  2688. lfit *lf;
  2689. int m;
  2690. { double cp, res[10];
  2691. int dg, n;
  2692. n = lf->lfd.n;
  2693. nn(&lf->sp) = 0;
  2694. fixh(&lf->sp) = h;
  2695. lf->mdl.procv = procvstd;
  2696. nstartlf(des,lf);
  2697. ressumm(lf,des,res);
  2698. if (m==1)
  2699. { if (fc) sig2 = res[3]; /* first call - set sig2 */
  2700. cp = -2*res[0]/sig2 - n + 2*res[1];
  2701. }
  2702. else
  2703. cp = -2*n*res[0]/((n-res[1])*(n-res[1]));
  2704. fc = 0;
  2705. return(cp);
  2706. }
  2707. double cp(des,lf,meth)
  2708. design *des;
  2709. lfit *lf;
  2710. int meth;
  2711. { double hm, ym;
  2712. fc = 1;
  2713. goldensec(loccp,des,lf,0.001,&hm,&ym,meth);
  2714. return(hm);
  2715. }
  2716. double gkk(des,lf)
  2717. design *des;
  2718. lfit *lf;
  2719. { double h, h5, nf, th;
  2720. int i, j, n, dg0, dg1;
  2721. ev(&lf->evs)= EDATA;
  2722. nn(&lf->sp) = 0;
  2723. n = lf->lfd.n;
  2724. dg0 = deg0(&lf->sp); /* target degree */
  2725. dg1 = dg0+1+(dg0%2==0); /* pilot degree */
  2726. nf = exp(log(1.0*n)/10); /* bandwidth inflation factor */
  2727. h = lf->sp.fixh; /* start bandwidth */
  2728. for (i=0; i<=10; i++)
  2729. { deg(&lf->sp) = dg1;
  2730. lf->sp.fixh = h*nf;
  2731. lf->mdl.procv = procvstd;
  2732. nstartlf(des,lf);
  2733. th = 0;
  2734. for (j=10; j<n-10; j++)
  2735. th += lf->fp.coef[dg1*n+j]*lf->fp.coef[dg1*n+j];
  2736. th *= n/(n-20.0);
  2737. h5 = sig2 * Wikk(ker(&lf->sp),dg0) / th;
  2738. h = exp(log(h5)/(2*dg1+1));
  2739. if (lf_error) return(0.0);
  2740. /* mut_printf("pilot %8.5f sel %8.5f\n",lf->sp.fixh,h); */
  2741. }
  2742. return(h);
  2743. }
  2744. double rsw(des,lf)
  2745. design *des;
  2746. lfit *lf;
  2747. { int i, j, k, nmax, nvm, n, mk, evo, dg0, dg1;
  2748. double rss[6], cp[6], th22, dx, d2, hh;
  2749. nmax = 5;
  2750. evo = ev(&lf->evs); ev(&lf->evs) = EGRID;
  2751. mk = ker(&lf->sp); ker(&lf->sp) = WRECT;
  2752. dg0 = deg0(&lf->sp);
  2753. dg1 = 1 + dg0 + (dg0%2==0);
  2754. deg(&lf->sp) = 4;
  2755. for (k=nmax; k>0; k--)
  2756. { lf->evs.mg[0] = k;
  2757. lf->evs.fl[0] = 1.0/(2*k);
  2758. lf->evs.fl[1] = 1-1.0/(2*k);
  2759. nn(&lf->sp) = 0;
  2760. fixh(&lf->sp) = 1.0/(2*k);
  2761. lf->mdl.procv = procvstd;
  2762. nstartlf(des,lf);
  2763. nvm = lf->fp.nvm;
  2764. rss[k] = 0;
  2765. for (i=0; i<k; i++) rss[k] += -2*lf->fp.lik[i];
  2766. }
  2767. n = lf->lfd.n; k = 1;
  2768. for (i=1; i<=nmax; i++)
  2769. { /* cp[i] = (n-5*nmax)*rss[i]/rss[nmax]-(n-10*i); */
  2770. cp[i] = rss[i]/sig2-(n-10*i);
  2771. if (cp[i]<cp[k]) k = i;
  2772. }
  2773. lf->evs.mg[0] = k;
  2774. lf->evs.fl[0] = 1.0/(2*k);
  2775. lf->evs.fl[1] = 1-1.0/(2*k);
  2776. nn(&lf->sp) = 0;
  2777. fixh(&lf->sp) = 1.0/(2*k);
  2778. lf->mdl.procv = procvstd;
  2779. nstartlf(des,lf);
  2780. ker(&lf->sp) = mk; ev(&lf->evs) = evo;
  2781. nvm = lf->fp.nvm;
  2782. th22 = 0;
  2783. for (i=10; i<n-10; i++)
  2784. { j = floor(k*datum(&lf->lfd,0,i));
  2785. if (j>=k) j = k-1;
  2786. dx = datum(&lf->lfd,0,i)-evptx(&lf->fp,0,j);
  2787. if (dg1==2)
  2788. d2 = lf->fp.coef[2*nvm+j]+dx*lf->fp.coef[3*nvm+j]+dx*dx*lf->fp.coef[4*nvm+j]/2;
  2789. else d2 = lf->fp.coef[4*nvm+j];
  2790. th22 += d2*d2;
  2791. }
  2792. hh = Wikk(mk,dg0)*sig2/th22*(n-20.0)/n;
  2793. return(exp(log(hh)/(2*dg1+1)));
  2794. }
  2795. #define MAXMETH 10
  2796. void rband(lf,des,hhat)
  2797. lfit *lf;
  2798. design *des;
  2799. double *hhat;
  2800. { int i, dg, nmeth, meth[MAXMETH];
  2801. double h0, res[10];
  2802. nmeth = lf->dv.nd;
  2803. for (i=0; i<nmeth; i++) meth[i] = lf->dv.deriv[i];
  2804. lf->dv.nd = 0;
  2805. /* first, estimate sigma^2.
  2806. * this is ridiculously bad. lf->sp.fixh = 0.05????
  2807. */
  2808. /* dg = deg(&lf->sp); deg(&lf->sp) = 2;
  2809. h0 = lf->sp.fixh; lf->sp.fixh = 0.05;
  2810. lf->mdl.procv = procvstd;
  2811. nstartlf(des,lf);
  2812. ressumm(lf,des,res);
  2813. deg(&lf->sp) = dg; lf->sp.fixh = h0;
  2814. sig2 = res[3]; */
  2815. for (i=0; i<nmeth; i++)
  2816. {
  2817. switch(meth[i])
  2818. { case 0: hhat[i] = cp(des,lf,1);
  2819. break;
  2820. case 1: hhat[i] = cp(des,lf,2);
  2821. break;
  2822. case 2: hhat[i] = gkk(des,lf);
  2823. break;
  2824. case 3: hhat[i] = rsw(des,lf);
  2825. break;
  2826. default: hhat[i] = 0;
  2827. mut_printf("Unknown method %d\n",meth[i]);
  2828. }
  2829. if (lf_error) i = nmeth;
  2830. }
  2831. lf->dv.nd = nmeth;
  2832. }
  2833. void initrband(lf)
  2834. lfit *lf;
  2835. {
  2836. initstd(lf);
  2837. KEEPC(lf) = MAXMETH;
  2838. PROCV(lf) = NULL;
  2839. PPROC(lf) = rband;
  2840. }
  2841. /*
  2842. * Copyright 1996-2006 Catherine Loader.
  2843. */
  2844. #include "lfev.h"
  2845. static double scb_crit, *x, c[10], kap[5], kaq[5], max_p2;
  2846. static int side, type;
  2847. design *scb_des;
  2848. double covar_par(lf,des,x1,x2)
  2849. lfit *lf;
  2850. design *des;
  2851. double x1, x2;
  2852. { double *v1, *v2, *wk;
  2853. paramcomp *pc;
  2854. int i, j, p, ispar;
  2855. v1 = des->f1; v2 = des->ss; wk = des->oc;
  2856. ispar = (ker(&lf->sp)==WPARM) && (haspc(&lf->pc));
  2857. p = npar(&lf->sp);
  2858. /* for parametric models, the covariance is
  2859. * A(x1)^T (X^T W V X)^{-1} A(x2)
  2860. * which we can find easily from the parametric component.
  2861. */
  2862. if (ispar)
  2863. { pc = &lf->pc;
  2864. fitfun(&lf->lfd, &lf->sp, &x1,pc->xbar,v1,NULL);
  2865. fitfun(&lf->lfd, &lf->sp, &x2,pc->xbar,v2,NULL);
  2866. jacob_hsolve(&lf->pc.xtwx,v1);
  2867. jacob_hsolve(&lf->pc.xtwx,v2);
  2868. }
  2869. /* for non-parametric models, we must use the cholseky decomposition
  2870. * of M2 = X^T W^2 V X. Courtesy of lf_vcov caclulations, should have
  2871. * des->P = M2^{1/2} M1^{-1}.
  2872. */
  2873. if (!ispar)
  2874. { fitfun(&lf->lfd, &lf->sp, &x1,des->xev,wk,NULL);
  2875. for (i=0; i<p; i++)
  2876. { v1[i] = 0;
  2877. for (j=0; j<p; j++) v1[i] += des->P[i*p+j]*wk[j];
  2878. }
  2879. fitfun(&lf->lfd, &lf->sp, &x2,des->xev,wk,NULL);
  2880. for (i=0; i<p; i++)
  2881. { v2[i] = 0;
  2882. for (j=0; j<p; j++) v2[i] += des->P[i*p+j]*wk[j];
  2883. }
  2884. }
  2885. return(innerprod(v1,v2,p));
  2886. }
  2887. void cumulant(lf,des,sd)
  2888. lfit *lf;
  2889. design *des;
  2890. double sd;
  2891. { double b2i, b3i, b3j, b4i;
  2892. double ss, si, sj, uii, uij, ujj, k1;
  2893. int ii, i, j, jj;
  2894. for (i=1; i<10; i++) c[i] = 0.0;
  2895. k1 = 0;
  2896. /* ss = sd*sd; */
  2897. ss = covar_par(lf,des,des->xev[0],des->xev[0]);
  2898. /*
  2899. * this isn't valid for nonparametric models. At a minimum,
  2900. * the sums would have to include weights. Still have to work
  2901. * out the right way.
  2902. */
  2903. for (i=0; i<lf->lfd.n; i++)
  2904. { ii = des->ind[i];
  2905. b2i = b2(fitv(des,ii),fam(&lf->sp),prwt(&lf->lfd,ii));
  2906. b3i = b3(fitv(des,ii),fam(&lf->sp),prwt(&lf->lfd,ii));
  2907. b4i = b4(fitv(des,ii),fam(&lf->sp),prwt(&lf->lfd,ii));
  2908. si = covar_par(lf,des,des->xev[0],datum(&lf->lfd,0,ii));
  2909. uii= covar_par(lf,des,datum(&lf->lfd,0,ii),datum(&lf->lfd,0,ii));
  2910. if (lf_error) return;
  2911. c[2] += b4i*si*si*uii;
  2912. c[6] += b4i*si*si*si*si;
  2913. c[7] += b3i*si*uii;
  2914. c[8] += b3i*si*si*si;
  2915. /* c[9] += b2i*si*si*si*si;
  2916. c[9] += b2i*b2i*si*si*si*si; */
  2917. k1 += b3i*si*(si*si/ss-uii);
  2918. /* i=j components */
  2919. c[1] += b3i*b3i*si*si*uii*uii;
  2920. c[3] += b3i*b3i*si*si*si*si*uii;
  2921. c[4] += b3i*b3i*si*si*uii*uii;
  2922. for (j=i+1; j<lf->lfd.n; j++)
  2923. { jj = des->ind[j];
  2924. b3j = b3(fitv(des,ii),fam(&lf->sp),prwt(&lf->lfd,jj));
  2925. sj = covar_par(lf,des,des->xev[0],datum(&lf->lfd,0,jj));
  2926. uij= covar_par(lf,des,datum(&lf->lfd,0,ii),datum(&lf->lfd,0,jj));
  2927. ujj= covar_par(lf,des,datum(&lf->lfd,0,jj),datum(&lf->lfd,0,jj));
  2928. c[1] += 2*b3i*b3j*si*sj*uij*uij;
  2929. c[3] += 2*b3i*b3j*si*si*sj*sj*uij;
  2930. c[4] += b3i*b3j*uij*(si*si*ujj+sj*sj*uii);
  2931. if (lf_error) return;
  2932. }
  2933. }
  2934. c[5] = c[1];
  2935. c[7] = c[7]*c[8];
  2936. c[8] = c[8]*c[8];
  2937. c[1] /= ss; c[2] /= ss; c[3] /= ss*ss; c[4] /= ss;
  2938. c[5] /= ss; c[6] /= ss*ss; c[7] /= ss*ss;
  2939. c[8] /= ss*ss*ss; c[9] /= ss*ss;
  2940. /* constants used in p(x,z) computation */
  2941. kap[1] = k1/(2*sqrt(ss));
  2942. kap[2] = 1 + 0.5*(c[1]-c[2]+c[4]-c[7]) - 3*c[3] + c[6] + 1.75*c[8];
  2943. kap[4] = -9*c[3] + 3*c[6] + 6*c[8] + 3*c[9];
  2944. /* constants used in q(x,u) computation */
  2945. kaq[2] = c[3] - 1.5*c[8] - c[5] - c[4] + 0.5*c[7] + c[6] - c[2];
  2946. kaq[4] = -3*c[3] - 6*c[4] - 6*c[5] + 3*c[6] + 3*c[7] - 3*c[8] + 3*c[9];
  2947. }
  2948. /* q2(u) := u+q2(x,u) in paper */
  2949. double q2(u)
  2950. double u;
  2951. { return(u-u*(36.0*kaq[2] + 3*kaq[4]*(u*u-3) + c[8]*((u*u-10)*u*u+15))/72.0);
  2952. }
  2953. /* p2(u) := p2(x,u) in paper */
  2954. double p2(u)
  2955. double u;
  2956. { return( -u*( 36*(kap[2]-1+kap[1]*kap[1])
  2957. + 3*(kap[4]+4*kap[1]*sqrt(kap[3]))*(u*u-3)
  2958. + c[8]*((u*u-10)*u*u+15) ) / 72 );
  2959. }
  2960. extern int likereg();
  2961. double gldn_like(a)
  2962. double a;
  2963. { int err;
  2964. scb_des->fix[0] = 1;
  2965. scb_des->cf[0] = a;
  2966. max_nr(likereg, scb_des->cf, scb_des->oc, scb_des->res, scb_des->f1,
  2967. &scb_des->xtwx, scb_des->p, lf_maxit, 1.0e-6, &err);
  2968. scb_des->fix[0] = 0;
  2969. return(scb_des->llk);
  2970. }
  2971. /* v1/v2 is correct for deg=0 only */
  2972. void get_gldn(fp,des,lo,hi,v)
  2973. fitpt *fp;
  2974. design *des;
  2975. double *lo, *hi;
  2976. int v;
  2977. { double v1, v2, c, tlk;
  2978. int err;
  2979. v1 = fp->nlx[v];
  2980. v2 = fp->t0[v];
  2981. c = scb_crit * v1 / v2;
  2982. tlk = des->llk - c*c/2;
  2983. mut_printf("v %8.5f %8.5f c %8.5f tlk %8.5f llk %8.5f\n",v1,v2,c,tlk,des->llk);
  2984. /* want: { a : l(a) >= l(a-hat) - c*c/2 } */
  2985. lo[v] = fp->coef[v] - scb_crit*v1;
  2986. hi[v] = fp->coef[v] + scb_crit*v1;
  2987. err = 0;
  2988. mut_printf("lo %2d\n",v);
  2989. lo[v] = solve_secant(gldn_like,tlk,lo[v],fp->coef[v],1e-8,BDF_EXPLEFT,&err);
  2990. if (err>0) mut_printf("solve_secant error\n");
  2991. mut_printf("hi %2d\n",v);
  2992. hi[v] = solve_secant(gldn_like,tlk,fp->coef[v],hi[v],1e-8,BDF_EXPRIGHT,&err);
  2993. if (err>0) mut_printf("solve_secant error\n");
  2994. }
  2995. int procvscb2(des,lf,v)
  2996. design *des;
  2997. lfit *lf;
  2998. int v;
  2999. { double thhat, sd, *lo, *hi, u;
  3000. int err, st, tmp;
  3001. x = des->xev = evpt(&lf->fp,v);
  3002. tmp = haspc(&lf->pc);
  3003. /* if ((ker(&lf->sp)==WPARM) && (haspc(&lf->pc)))
  3004. { lf->coef[v] = thhat = addparcomp(lf,des->xev,PCOEF);
  3005. lf->nlx[v] = lf->t0[v] = sd = addparcomp(lf,des->xev,PNLX);
  3006. }
  3007. else */
  3008. { haspc(&lf->pc) = 0;
  3009. st = procvstd(des,lf,v);
  3010. thhat = lf->fp.coef[v];
  3011. sd = lf->fp.nlx[v];
  3012. }
  3013. if ((type==GLM2) | (type==GLM3) | (type==GLM4))
  3014. { if (ker(&lf->sp) != WPARM)
  3015. WARN(("nonparametric fit; correction is invalid"));
  3016. cumulant(lf,des,sd);
  3017. }
  3018. haspc(&lf->pc) = tmp;
  3019. lo = lf->fp.t0;
  3020. hi = &lo[lf->fp.nvm];
  3021. switch(type)
  3022. {
  3023. case GLM1:
  3024. return(st);
  3025. case GLM2: /* centered scr */
  3026. lo[v] = kap[1];
  3027. hi[v] = sqrt(kap[2]);
  3028. return(st);
  3029. case GLM3: /* corrected 2 */
  3030. lo[v] = solve_secant(q2,scb_crit,0.0,2*scb_crit,0.000001,BDF_NONE,&err);
  3031. return(st);
  3032. case GLM4: /* corrected 2' */
  3033. u = fabs(p2(scb_crit));
  3034. max_p2 = MAX(max_p2,u);
  3035. return(st);
  3036. case GLDN:
  3037. get_gldn(&lf->fp,des,lo,hi,v);
  3038. return(st);
  3039. }
  3040. LERR(("procvscb2: invalid type"));
  3041. return(st);
  3042. }
  3043. void scb(lf,des,res)
  3044. lfit *lf;
  3045. design *des;
  3046. double *res;
  3047. { double k1, k2, *lo, *hi, sig, thhat, nlx, rss[10];
  3048. int i, nterms;
  3049. scb_des= des;
  3050. npar(&lf->sp) = calcp(&lf->sp,lf->lfd.d);
  3051. des_init(des,lf->lfd.n,npar(&lf->sp));
  3052. type = geth(&lf->fp);
  3053. if (type >= 80) /* simultaneous */
  3054. {
  3055. nterms = constants(lf,des,res);
  3056. scb_crit = critval(0.05,res,nterms,lf->lfd.d,TWO_SIDED,0.0,GAUSS);
  3057. type -= 10;
  3058. }
  3059. else /* pointwise */
  3060. { res[0] = 1;
  3061. scb_crit = critval(0.05,res,1,lf->lfd.d,TWO_SIDED,0.0,GAUSS);
  3062. }
  3063. max_p2 = 0.0;
  3064. lf->mdl.procv = procvscb2;
  3065. nstartlf(des,lf);
  3066. if ((fam(&lf->sp)&64)==64)
  3067. { i = haspc(&lf->pc); haspc(&lf->pc) = 0;
  3068. ressumm(lf,des,rss);
  3069. haspc(&lf->pc) = i;
  3070. sig = sqrt(rss[3]);
  3071. }
  3072. else sig = 1.0;
  3073. lo = lf->fp.t0;
  3074. hi = &lo[lf->fp.nvm];
  3075. for (i=0; i<lf->fp.nv; i++)
  3076. { thhat = lf->fp.coef[i];
  3077. nlx = lf->fp.nlx[i];
  3078. switch(type)
  3079. {
  3080. case GLM1: /* basic scb */
  3081. lo[i] = thhat - scb_crit * sig * nlx;
  3082. hi[i] = thhat + scb_crit * sig * nlx;
  3083. break;
  3084. case GLM2:
  3085. k1 = lo[i];
  3086. k2 = hi[i];
  3087. lo[i] = thhat - k1*nlx - scb_crit*nlx*k2;
  3088. hi[i] = thhat - k1*nlx + scb_crit*nlx*k2;
  3089. break;
  3090. case GLM3:
  3091. k1 = lo[i];
  3092. lo[i] = thhat - k1*nlx;
  3093. hi[i] = thhat + k1*nlx;
  3094. case GLM4: /* corrected 2' */
  3095. lo[i] = thhat - (scb_crit-max_p2)*lf->fp.nlx[i];
  3096. hi[i] = thhat + (scb_crit-max_p2)*lf->fp.nlx[i];
  3097. break;
  3098. case GLDN:
  3099. break;
  3100. }
  3101. }
  3102. }
  3103. void initscb(lf)
  3104. lfit *lf;
  3105. { initstd(lf);
  3106. PROCV(lf) = NULL;
  3107. KEEPC(lf) = NVAR(lf)+1+k0_reqd(NVAR(lf),NOBS(lf),0);
  3108. PPROC(lf) = scb;
  3109. }
  3110. /*
  3111. * Copyright 1996-2006 Catherine Loader.
  3112. */
  3113. #include "lfev.h"
  3114. int procvsimple(des,lf,v)
  3115. design *des;
  3116. lfit *lf;
  3117. int v;
  3118. { int lf_status;
  3119. lf_status = procv_nov(des,lf,v);
  3120. VVAL(lf,v,0) = des->cf[cfn(des,0)];
  3121. return(lf_status);
  3122. }
  3123. void allocsimple(lf)
  3124. lfit *lf;
  3125. { lf->fp.coef = VVEC(lf,0);
  3126. lf->fp.h = NULL;
  3127. }
  3128. void initsimple(lf)
  3129. lfit *lf;
  3130. {
  3131. PROCV(lf) = procvsimple;
  3132. ALLOC(lf) = allocsimple;
  3133. PPROC(lf) = NULL;
  3134. KEEPV(lf) = 1;
  3135. KEEPC(lf) = 1;
  3136. NOPC(lf) = 1;
  3137. }
  3138. /*
  3139. * Copyright 1996-2006 Catherine Loader.
  3140. */
  3141. #include "lfev.h"
  3142. /* 3d+8 variables to keep:
  3143. * d+1 coef+derivs.
  3144. * d+1 sd's + derivs.
  3145. * d+1 infl + derivs.
  3146. * 3 likelihood and d.f's.
  3147. * 1 bandwidth h
  3148. * 1 degree.
  3149. */
  3150. void allocstd(lf)
  3151. lfit *lf;
  3152. { int d;
  3153. d = NVAR(lf);
  3154. lf->fp.coef = VVEC(lf,0);
  3155. lf->fp.nlx = VVEC(lf,d+1);
  3156. lf->fp.t0 = VVEC(lf,2*d+2);
  3157. lf->fp.lik = VVEC(lf,3*d+3);
  3158. lf->fp.h = VVEC(lf,3*d+6);
  3159. lf->fp.deg = VVEC(lf,3*d+7);
  3160. }
  3161. int procvstd(des,lf,v)
  3162. design *des;
  3163. lfit *lf;
  3164. int v;
  3165. { int d, p, nvm, i, k;
  3166. double t0[1+MXDIM], vari[1+MXDIM];
  3167. k = procv_var(des,lf,v);
  3168. if (lf_error) return(k);
  3169. d = lf->lfd.d;
  3170. p = npar(&lf->sp);
  3171. nvm = lf->fp.nvm;
  3172. if (k != LF_OK) lf_status_msg(k);
  3173. lf->fp.lik[v] = des->llk;
  3174. lf->fp.lik[nvm+v] = des->tr2;
  3175. lf->fp.lik[2*nvm+v] = des->tr0 - des->tr2;
  3176. for (i=0; i<des->ncoef; i++)
  3177. vari[i] = des->V[p*cfn(des,0) + cfn(des,i)];
  3178. vari[0] = sqrt(vari[0]);
  3179. if (vari[0]>0) for (i=1; i<des->ncoef; i++) vari[i] /= vari[0];
  3180. t0[0] = sqrt(des->f1[0]);
  3181. if (t0[0]>0) for (i=1; i<des->ncoef; i++) t0[i] = des->f1[i]/t0[0];
  3182. if (dc(&lf->fp)) dercor(&lf->lfd,&lf->sp,des,des->cf);
  3183. subparcomp(des,lf,des->cf);
  3184. for (i=0; i<des->ncoef; i++)
  3185. lf->fp.coef[i*lf->fp.nvm+v] = des->cf[cfn(des,i)];
  3186. subparcomp2(des,lf,vari,t0);
  3187. for (i=0; i<des->ncoef; i++)
  3188. { lf->fp.nlx[i*nvm+v] = vari[i];
  3189. lf->fp.t0[i*nvm+v] = t0[i];
  3190. }
  3191. lf->fp.deg[v] = deg(&lf->sp);
  3192. return(k);
  3193. }
  3194. void pprocstd(lf,des,res)
  3195. lfit *lf;
  3196. design *des;
  3197. double *res;
  3198. {
  3199. ressumm(lf,des,res);
  3200. }
  3201. void initstd(lf)
  3202. lfit *lf;
  3203. { PROCV(lf) = procvstd;
  3204. ALLOC(lf) = allocstd;
  3205. PPROC(lf) = pprocstd;
  3206. KEEPV(lf) = 3*NVAR(lf) + 8;
  3207. KEEPC(lf) = 6;
  3208. NOPC(lf) = 0;
  3209. }
  3210. /*
  3211. * Copyright 1996-2006 Catherine Loader.
  3212. */
  3213. #include "lfev.h"
  3214. extern void procstd(), allocstd();
  3215. extern double robscale;
  3216. double vocri(lk,t0,t2,pen)
  3217. double lk, t0, t2, pen;
  3218. { if (pen==0) return(-2*t0*lk/((t0-t2)*(t0-t2)));
  3219. return((-2*lk+pen*t2)/t0);
  3220. }
  3221. double intvo(des,lf,c0,c1,a,p,t0,t20,t21)
  3222. design *des;
  3223. lfit *lf;
  3224. double *c0, *c1, a, t0, t20, t21;
  3225. int p;
  3226. { double th, lk, link[LLEN];
  3227. int i, ii;
  3228. lk = 0;
  3229. for (i=0; i<des->n; i++)
  3230. { ii = des->ind[i];
  3231. th = (1-a)*innerprod(c0,d_xi(des,i),p) + a*innerprod(c1,d_xi(des,i),p);
  3232. stdlinks(link,&lf->lfd,&lf->sp,ii,th,robscale);
  3233. lk += wght(des,ii)*link[ZLIK];
  3234. }
  3235. des->llk = lk;
  3236. return(vocri(des->llk,t0,(1-a)*t20+a*t21,pen(&lf->sp)));
  3237. }
  3238. int procvvord(des,lf,v)
  3239. design *des;
  3240. lfit *lf;
  3241. int v;
  3242. { double tr[6], gcv, g0, ap, coef[4][10], t2[4], th, md;
  3243. int i, j, k, d1, i0, p1, ip;
  3244. des->xev = evpt(&lf->fp,v);
  3245. ap = pen(&lf->sp);
  3246. if ((ap==0) & ((fam(&lf->sp)&63)!=TGAUS)) ap = 2.0;
  3247. d1 = deg(&lf->sp); p1 = npar(&lf->sp);
  3248. for (i=0; i<p1; i++) coef[0][i] = coef[1][i] = coef[2][i] = coef[3][i] = 0.0;
  3249. i0 = 0; g0 = 0;
  3250. ip = 1;
  3251. for (i=deg0(&lf->sp); i<=d1; i++)
  3252. { deg(&lf->sp) = i;
  3253. des->p = npar(&lf->sp) = calcp(&lf->sp,lf->lfd.d);
  3254. k = locfit(&lf->lfd,des,&lf->sp,0, i==deg0(&lf->sp),0);
  3255. local_df(&lf->lfd,&lf->sp,des,tr);
  3256. gcv = vocri(des->llk,tr[0],tr[2],ap);
  3257. if ((i==deg0(&lf->sp)) || (gcv<g0)) { i0 = i; g0 = gcv; md = i; }
  3258. for (j=0; j<des->p; j++) coef[i][j] = des->cf[j];
  3259. t2[i] = tr[2];
  3260. #ifdef RESEARCH
  3261. if ((ip) && (i>deg0(&lf->sp)))
  3262. { for (j=1; j<10; j++)
  3263. { gcv = intvo(des,lf,coef[i-1],coef[i],j/10.0,des->p,tr[0],t2[i-1],t2[i]);
  3264. if (gcv<g0) { g0 = gcv; md = i-1+j/10.0; }
  3265. }
  3266. }
  3267. #endif
  3268. }
  3269. lf->fp.h[v] = des->h;
  3270. if (lf->fp.h[v]<=0) WARN(("zero bandwidth in procvvord"));
  3271. if (i0<d1) /* recompute the best fit */
  3272. { deg(&lf->sp) = i0;
  3273. des->p = npar(&lf->sp) = calcp(&lf->sp,lf->lfd.d);
  3274. k = locfit(&lf->lfd,des,&lf->sp,0,0,0);
  3275. for (i=npar(&lf->sp); i<p1; i++) des->cf[i] = 0.0;
  3276. i0 = md; if (i0==d1) i0--;
  3277. th = md-i0;
  3278. for (i=0; i<p1; i++) des->cf[i] = (1-th)*coef[i0][i]+th*coef[i0+1][i];
  3279. deg(&lf->sp) = d1; npar(&lf->sp) = p1;
  3280. }
  3281. for (i=0; i<p1; i++) lf->fp.coef[i*lf->fp.nvm+v] = des->cf[i];
  3282. lf->fp.deg[v] = md;
  3283. return(k);
  3284. }
  3285. void initvord(lf)
  3286. lfit *lf;
  3287. { initstd(lf);
  3288. PROCV(lf) = procvvord;
  3289. ALLOC(lf) = allocstd;
  3290. PPROC(lf) = NULL;
  3291. KEEPC(lf) = 0;
  3292. NOPC(lf) = 1;
  3293. }
  3294. /*
  3295. * Copyright 1996-2006 Catherine Loader.
  3296. */
  3297. /*
  3298. * functions for computing and subtracting, adding the
  3299. * parametric component
  3300. */
  3301. #include "lfev.h"
  3302. int noparcomp(sp)
  3303. smpar *sp;
  3304. { int tg;
  3305. if (ubas(sp)) return(1);
  3306. tg = fam(sp) & 63;
  3307. if (tg<=THAZ) return(1);
  3308. if (tg==TROBT) return(1);
  3309. if (tg==TCAUC) return(1);
  3310. if (tg==TQUANT) return(1);
  3311. return(0);
  3312. }
  3313. int pc_reqd(d,p)
  3314. int d, p;
  3315. { return(d + 2*p + jac_reqd(p));
  3316. }
  3317. void pcchk(pc,d,p,lc)
  3318. paramcomp *pc;
  3319. int d, p, lc;
  3320. { int rw;
  3321. double *z;
  3322. rw = pc_reqd(d,p);
  3323. if (pc->lwk < rw)
  3324. { pc->wk = (double *)calloc(rw,sizeof(double));
  3325. if ( pc->wk == NULL ) {
  3326. printf("Problem allocating memory for pc->wk\n");fflush(stdout);
  3327. }
  3328. pc->lwk= rw;
  3329. }
  3330. z = pc->wk;
  3331. pc->xbar = z; z += d;
  3332. pc->coef = z; z += p;
  3333. pc->f = z; z += p;
  3334. z = jac_alloc(&pc->xtwx,p,z);
  3335. pc->xtwx.p = p;
  3336. }
  3337. void compparcomp(des,lfd,sp,pc,nopc)
  3338. design *des;
  3339. lfdata *lfd;
  3340. smpar *sp;
  3341. paramcomp *pc;
  3342. int nopc;
  3343. { int i, j, k, p;
  3344. double wt, sw;
  3345. if (lf_debug>1) mut_printf(" compparcomp:\n");
  3346. p = des->p;
  3347. pcchk(pc,lfd->d,p,1);
  3348. for (i=0; i<lfd->d; i++) pc->xbar[i] = 0.0;
  3349. sw = 0.0;
  3350. for (i=0; i<lfd->n; i++)
  3351. {
  3352. wt = prwt(lfd,i);
  3353. sw += wt;
  3354. for (j=0; j<lfd->d; j++)
  3355. pc->xbar[j] += datum(lfd,j,i)*wt;
  3356. des->ind[i] = i;
  3357. wght(des,i) = 1.0;
  3358. }
  3359. for (i=0; i<lfd->d; i++) pc->xbar[i] /= sw;
  3360. if ((nopc) || noparcomp(sp))
  3361. { haspc(pc) = 0;
  3362. return;
  3363. }
  3364. haspc(pc) = 1;
  3365. des->xev = pc->xbar;
  3366. k = locfit(lfd,des,sp,0,0,0);
  3367. if (k != LF_OK) lf_status_msg(k);
  3368. if (lf_error) return;
  3369. switch(k)
  3370. { case LF_NOPT: return;
  3371. case LF_INFA: return;
  3372. case LF_NCON: return;
  3373. case LF_OOB: return;
  3374. case LF_NSLN: return;
  3375. case LF_PF:
  3376. WARN(("compparcomp: perfect fit"));
  3377. case LF_OK:
  3378. case LF_DONE:
  3379. for (i=0; i<p; i++)
  3380. { pc->coef[i] = des->cf[i];
  3381. pc->xtwx.dg[i] = des->xtwx.dg[i];
  3382. pc->xtwx.wk[i] = des->xtwx.wk[i];
  3383. }
  3384. for (i=0; i<p*p; i++)
  3385. { pc->xtwx.Z[i] = des->xtwx.Z[i];
  3386. pc->xtwx.Q[i] = des->xtwx.Q[i];
  3387. }
  3388. pc->xtwx.sm = des->xtwx.sm;
  3389. pc->xtwx.st = des->xtwx.st;
  3390. return;
  3391. default:
  3392. LERR(("compparcomp: locfit unknown return status %d",k));
  3393. return;
  3394. }
  3395. }
  3396. void subparcomp(des,lf,coef)
  3397. design *des;
  3398. lfit *lf;
  3399. double *coef;
  3400. { int i, nd;
  3401. deriv *dv;
  3402. paramcomp *pc;
  3403. pc = &lf->pc;
  3404. if (!haspc(pc)) return;
  3405. dv = &lf->dv; nd = dv->nd;
  3406. fitfun(&lf->lfd, &lf->sp, des->xev,pc->xbar,des->f1,dv);
  3407. coef[0] -= innerprod(pc->coef,des->f1,pc->xtwx.p);
  3408. if (des->ncoef == 1) return;
  3409. dv->nd = nd+1;
  3410. for (i=0; i<lf->lfd.d; i++)
  3411. { dv->deriv[nd] = i;
  3412. fitfun(&lf->lfd, &lf->sp, des->xev,pc->xbar,des->f1,dv);
  3413. coef[i+1] -= innerprod(pc->coef,des->f1,pc->xtwx.p);
  3414. }
  3415. dv->nd = nd;
  3416. }
  3417. void subparcomp2(des,lf,vr,il)
  3418. design *des;
  3419. lfit *lf;
  3420. double *vr, *il;
  3421. { double t0, t1;
  3422. int i, nd;
  3423. deriv *dv;
  3424. paramcomp *pc;
  3425. pc = &lf->pc;
  3426. if (!haspc(pc)) return;
  3427. dv = &lf->dv; nd = dv->nd;
  3428. fitfun(&lf->lfd, &lf->sp, des->xev,pc->xbar,des->f1,dv);
  3429. for (i=0; i<npar(&lf->sp); i++) pc->f[i] = des->f1[i];
  3430. jacob_solve(&pc->xtwx,des->f1);
  3431. t0 = sqrt(innerprod(pc->f,des->f1,pc->xtwx.p));
  3432. vr[0] -= t0;
  3433. il[0] -= t0;
  3434. if ((t0==0) | (des->ncoef==1)) return;
  3435. dv->nd = nd+1;
  3436. for (i=0; i<lf->lfd.d; i++)
  3437. { dv->deriv[nd] = i;
  3438. fitfun(&lf->lfd, &lf->sp, des->xev,pc->xbar,pc->f,dv);
  3439. t1 = innerprod(pc->f,des->f1,pc->xtwx.p)/t0;
  3440. vr[i+1] -= t1;
  3441. il[i+1] -= t1;
  3442. }
  3443. dv->nd = nd;
  3444. }
  3445. double addparcomp(lf,x,c)
  3446. lfit *lf;
  3447. double *x;
  3448. int c;
  3449. { double y;
  3450. paramcomp *pc;
  3451. pc = &lf->pc;
  3452. if (!haspc(pc)) return(0.0);
  3453. fitfun(&lf->lfd, &lf->sp, x,pc->xbar,pc->f,&lf->dv);
  3454. if (c==PCOEF) return(innerprod(pc->coef,pc->f,pc->xtwx.p));
  3455. if ((c==PNLX)|(c==PT0)|(c==PVARI))
  3456. { y = sqrt(jacob_qf(&pc->xtwx,pc->f));
  3457. return(y);
  3458. }
  3459. return(0.0);
  3460. }
  3461. /*
  3462. * Copyright 1996-2006 Catherine Loader.
  3463. */
  3464. #include "lfev.h"
  3465. /*
  3466. preplot(): interpolates the fit to a new set of points.
  3467. lf -- the fit structure.
  3468. x -- the points to predict at.
  3469. f -- vector to return the predictions.
  3470. se -- vector to return std errors (NULL if not req'd)
  3471. band-- char for conf band type. ('n'=none, 'g'=global etc.)
  3472. n -- no of predictions (or vector of margin lengths for grid)
  3473. where -- where to predict:
  3474. 1 = points in the array x.
  3475. 2 = grid defined by margins in x.
  3476. 3 = data points from lf (ignore x).
  3477. 4 = fit points from lf (ignore x).
  3478. what -- what to predict.
  3479. (PCOEF etc; see lfcons.h file)
  3480. */
  3481. #define NWH 8
  3482. static char *whtyp[NWH] = { "coef", "nlx", "infl", "band",
  3483. "degr", "like", "rdf", "vari" };
  3484. static int whval[NWH] = { PCOEF, PNLX, PT0, PBAND, PDEGR, PLIK, PRDF, PVARI };
  3485. int ppwhat(z)
  3486. char *z;
  3487. { int val;
  3488. val = pmatch(z, whtyp, whval, NWH, -1);
  3489. if (val==-1) LERR(("Unknown what = %s",z));
  3490. return(val);
  3491. }
  3492. static char cb;
  3493. double *sef, *fit, sigmahat;
  3494. void predptall(lf,x,what,ev,i)
  3495. lfit *lf;
  3496. double *x;
  3497. int what, ev, i;
  3498. { double lik, rdf;
  3499. fit[i] = dointpoint(lf,x,what,ev,i);
  3500. if (cb=='n') return;
  3501. sef[i] = dointpoint(lf,x,PNLX,ev,i);
  3502. if (cb=='g')
  3503. { sef[i] *= sigmahat;
  3504. return;
  3505. }
  3506. if (cb=='l')
  3507. { lik = dointpoint(lf,x,PLIK,ev,i);
  3508. rdf = dointpoint(lf,x,PRDF,ev,i);
  3509. sef[i] *= sqrt(-2*lik/rdf);
  3510. return;
  3511. }
  3512. if (cb=='p')
  3513. { sef[i] = sigmahat*sqrt(1+sef[i]*sef[i]);
  3514. return;
  3515. }
  3516. }
  3517. void predptdir(lf,des,x,what,i)
  3518. lfit *lf;
  3519. design *des;
  3520. double *x;
  3521. int what, i;
  3522. { int needcv;
  3523. des->xev = x;
  3524. needcv = (what==PVARI) | (what==PNLX) | (what==PT0) | (what==PRDF);
  3525. locfit(&lf->lfd,des,&lf->sp,0,1,needcv);
  3526. switch(what)
  3527. { case PCOEF: fit[i] = des->cf[0]; break;
  3528. case PVARI: fit[i] = des->V[0]; break;
  3529. case PNLX: fit[i] = sqrt(des->V[0]); break;
  3530. case PT0: fit[i] = des->f1[0]; break;
  3531. case PBAND: fit[i] = des->h; break;
  3532. case PDEGR: fit[i] = deg(&lf->sp); break;
  3533. case PLIK: fit[i] = des->llk; break;
  3534. case PRDF: fit[i] = des->tr0 - des->tr2; break;
  3535. default: LERR(("unknown what in predptdir"));
  3536. }
  3537. }
  3538. void prepvector(lf,des,x,n,what,dir) /* interpolate a vector */
  3539. lfit *lf;
  3540. design *des;
  3541. double **x;
  3542. int n, what, dir;
  3543. { int i, j;
  3544. double xx[MXDIM];
  3545. for (i=0; i<n; i++)
  3546. { for (j=0; j<lf->fp.d; j++) xx[j] = x[j][i];
  3547. if (dir)
  3548. predptdir(lf,des,xx,what,i);
  3549. else
  3550. predptall(lf,xx,what,ev(&lf->evs),i);
  3551. if (lf_error) return;
  3552. }
  3553. }
  3554. void prepfitp(lf,what)
  3555. lfit *lf;
  3556. int what;
  3557. { int i;
  3558. for (i=0; i<lf->fp.nv; i++)
  3559. { predptall(lf,evpt(&lf->fp,i),what,EFITP,i);
  3560. if (lf_error) return;
  3561. }
  3562. }
  3563. void prepgrid(lf,des,x,mg,n,what,dir) /* interpolate a grid given margins */
  3564. lfit *lf;
  3565. design *des;
  3566. double **x;
  3567. int *mg, dir, n, what;
  3568. { int i, ii, j, d;
  3569. double xv[MXDIM];
  3570. d = lf->fp.d;
  3571. for (i=0; i<n; i++)
  3572. { ii = i;
  3573. for (j=0; j<d; j++)
  3574. { xv[j] = x[j][ii%mg[j]];
  3575. ii /= mg[j];
  3576. }
  3577. if (dir)
  3578. predptdir(lf,des,xv,what,i);
  3579. else
  3580. predptall(lf,xv,what,ev(&lf->evs),i);
  3581. if (lf_error) return;
  3582. }
  3583. }
  3584. void preplot(lf,x,f,se,band,mg,where,what,dir)
  3585. lfit *lf;
  3586. double **x, *f, *se;
  3587. int *mg, where, what, dir;
  3588. char band;
  3589. { int d, i, n;
  3590. double *xx[MXDIM];
  3591. design ppdes;
  3592. d = lf->fp.d;
  3593. fit = f;
  3594. sef = se;
  3595. cb = band;
  3596. if (cb!='n') sigmahat = sqrt(rv(&lf->fp));
  3597. if (dir) des_init(&ppdes,lf->lfd.n,npar(&lf->sp));
  3598. switch(where)
  3599. { case 1: /* vector */
  3600. n = mg[0];
  3601. prepvector(lf,&ppdes,x,n,what,dir);
  3602. break;
  3603. case 2: /* grid */
  3604. n = 1;
  3605. for (i=0; i<d; i++) n *= mg[i];
  3606. prepgrid(lf,&ppdes,x,mg,n,what,dir);
  3607. break;
  3608. case 3: /* data */
  3609. n = lf->lfd.n;
  3610. if ((ev(&lf->evs)==EDATA) | (ev(&lf->evs)==ECROS))
  3611. { prepfitp(lf,what);
  3612. dir = 0;
  3613. }
  3614. else
  3615. { for (i=0; i<d; i++) xx[i] = dvari(&lf->lfd,i);
  3616. prepvector(lf,&ppdes,xx,n,what,dir);
  3617. }
  3618. break;
  3619. case 4: /* fit points */
  3620. n = lf->fp.nv; dir = 0;
  3621. prepfitp(lf,what);
  3622. break;
  3623. default:
  3624. LERR(("unknown where in preplot"));
  3625. }
  3626. if ((!dir) && ((what==PT0)|(what==PVARI)))
  3627. for (i=0; i<n; i++) f[i] = f[i]*f[i];
  3628. }
  3629. /*
  3630. * Copyright 1996-2006 Catherine Loader.
  3631. */
  3632. #include "lfev.h"
  3633. int procv_nov(des,lf,v)
  3634. design *des;
  3635. lfit *lf;
  3636. int v;
  3637. { int lf_status;
  3638. if (lf_debug>1) mut_printf(" procveraw: %d\n",v);
  3639. des->xev = evpt(&lf->fp,v);
  3640. if (acri(&lf->sp)==ANONE)
  3641. lf_status = locfit(&lf->lfd,des,&lf->sp,0,1,0);
  3642. else
  3643. lf_status = alocfit(&lf->lfd,&lf->sp,&lf->dv,des,0);
  3644. if (lf->fp.h != NULL) lf->fp.h[v] = des->h;
  3645. return(lf_status);
  3646. }
  3647. int procv_var(des,lf,v)
  3648. design *des;
  3649. lfit *lf;
  3650. int v;
  3651. { int i, lf_status;
  3652. if (lf_debug>1) mut_printf(" procvraw: %d\n",v);
  3653. des->xev = evpt(&lf->fp,v);
  3654. if (acri(&lf->sp)==ANONE)
  3655. lf_status = locfit(&lf->lfd,des,&lf->sp,0,1,1);
  3656. else
  3657. lf_status = alocfit(&lf->lfd,&lf->sp,&lf->dv,des,1);
  3658. if (lf->fp.h != NULL) lf->fp.h[v] = des->h;
  3659. return(lf_status);
  3660. }
  3661. /*
  3662. * Copyright 1996-2006 Catherine Loader.
  3663. */
  3664. /*
  3665. * startmodule(lf,des,mod,dir) -- the standard entry point.
  3666. * des and lf are pointers to the design and fit structures.
  3667. * mod - module name. Set to NULL if the module is already
  3668. * initialized.
  3669. * dir - for dynamic modules, the directory.
  3670. *
  3671. * initmodule(mdl,mod,dir,lf)
  3672. * direct call for module initialization.
  3673. *
  3674. */
  3675. #include "lfev.h"
  3676. #ifdef WINDOWS
  3677. #define DIRSEP '\\'
  3678. #define PATHSEP ';'
  3679. #else
  3680. #define DIRSEP '/'
  3681. #define PATHSEP ':'
  3682. #endif
  3683. #ifdef ALLOW_MODULES
  3684. #ifdef WINDOWS
  3685. #include <windows.h>
  3686. #define DLEXT "dll"
  3687. #define DLOPEN(x) LoadLibrary(x)
  3688. #define DLSYM GetProcAddress
  3689. #else
  3690. #include <dlfcn.h>
  3691. #define DLEXT "so"
  3692. #define DLOPEN(x) dlopen(x,RTLD_LAZY)
  3693. #define DLSYM dlsym
  3694. #endif
  3695. #endif
  3696. static double fpkap[6];
  3697. void fitpt_init(fp)
  3698. fitpt *fp;
  3699. {
  3700. dc(fp) = 0;
  3701. geth(fp) = GSTD;
  3702. fp->nv = fp->nvm = 0;
  3703. if (fp->kap==NULL) fp->kap = fpkap;
  3704. }
  3705. void lfit_init(lf)
  3706. lfit *lf;
  3707. {
  3708. lfdata_init(&lf->lfd);
  3709. evstruc_init(&lf->evs);
  3710. smpar_init(&lf->sp,&lf->lfd);
  3711. deriv_init(&lf->dv);
  3712. fitpt_init(&lf->fp);
  3713. lf->mdl.np = 0;
  3714. }
  3715. void fitdefault(lf)
  3716. lfit *lf;
  3717. { WARN(("fitdefault deprecated -- use lfit_init()"));
  3718. lfit_init(lf);
  3719. }
  3720. void set_flim(lfd,evs)
  3721. lfdata *lfd;
  3722. evstruc *evs;
  3723. { int i, j, d, n;
  3724. double z, mx, mn, *bx;
  3725. if (ev(evs)==ESPHR) return;
  3726. d = lfd->d; n = lfd->n;
  3727. bx = evs->fl;
  3728. for (i=0; i<d; i++)
  3729. if (bx[i]==bx[i+d])
  3730. { if (lfd->sty[i]==STANGL)
  3731. { bx[i] = 0.0; bx[i+d] = 2*PI*lfd->sca[i];
  3732. }
  3733. else
  3734. { mx = mn = datum(lfd,i,0);
  3735. for (j=1; j<n; j++)
  3736. { mx = MAX(mx,datum(lfd,i,j));
  3737. mn = MIN(mn,datum(lfd,i,j));
  3738. }
  3739. if (lfd->xl[i]<lfd->xl[i+d]) /* user set xlim; maybe use them. */
  3740. { z = mx-mn;
  3741. if (mn-0.2*z < lfd->xl[i]) mn = lfd->xl[i];
  3742. if (mx+0.2*z > lfd->xl[i+d]) mx = lfd->xl[i+d];
  3743. }
  3744. bx[i] = mn;
  3745. bx[i+d] = mx;
  3746. }
  3747. }
  3748. }
  3749. double vvari(v,n)
  3750. double *v;
  3751. int n;
  3752. { int i;
  3753. double xb, s2;
  3754. xb = s2 = 0.0;
  3755. for (i=0; i<n; i++) xb += v[i];
  3756. xb /= n;
  3757. for (i=0; i<n; i++) s2 += SQR(v[i]-xb);
  3758. return(s2/(n-1));
  3759. }
  3760. void set_scales(lfd)
  3761. lfdata *lfd;
  3762. { int i;
  3763. for (i=0; i<lfd->d; i++)
  3764. if (lfd->sca[i]<=0) /* set automatic scales */
  3765. { if (lfd->sty[i]==STANGL)
  3766. lfd->sca[i] = 1.0;
  3767. else lfd->sca[i] = sqrt(vvari(lfd->x[i],lfd->n));
  3768. }
  3769. }
  3770. void nstartlf(des,lf)
  3771. design *des;
  3772. lfit *lf;
  3773. { int i, d, n;
  3774. if (lf_debug>0) mut_printf("nstartlf\n");
  3775. n = lf->lfd.n;
  3776. d = lf->lfd.d;
  3777. npar(&lf->sp) = calcp(&lf->sp,d);
  3778. des_init(des,n,npar(&lf->sp));
  3779. set_scales(&lf->lfd);
  3780. set_flim(&lf->lfd,&lf->evs);
  3781. compparcomp(des,&lf->lfd,&lf->sp,&lf->pc,lf->mdl.nopc);
  3782. if (lf_error) return;
  3783. makecfn(&lf->sp,des,&lf->dv,lf->lfd.d);
  3784. lf->lfd.ord = 0;
  3785. if ((d==1) && (lf->lfd.sty[0]!=STANGL))
  3786. { i = 1;
  3787. while ((i<n) && (datum(&lf->lfd,0,i)>=datum(&lf->lfd,0,i-1))) i++;
  3788. lf->lfd.ord = (i==n);
  3789. }
  3790. for (i=0; i<npar(&lf->sp); i++) des->fix[i] = 0;
  3791. lf->fp.d = lf->lfd.d;
  3792. lf->fp.hasd = (des->ncoef==(1+lf->fp.d));
  3793. lf->fp.nv = lf->evs.nce = 0;
  3794. if (lf_debug>1) mut_printf("call eval structure %d\n",ev(&lf->evs));
  3795. switch(ev(&lf->evs))
  3796. { case EPHULL: triang_start(des,lf); break;
  3797. case EDATA: dataf(des,lf); break;
  3798. case ECROS: crossf(des,lf); break;
  3799. case EGRID: gridf(des,lf); break;
  3800. case ETREE: atree_start(des,lf); break;
  3801. case EKDCE: kt(&lf->sp) = KCE;
  3802. case EKDTR: kdtre_start(des,lf); break;
  3803. case EPRES: preset(des,lf); break;
  3804. case EXBAR: xbarf(des,lf); break;
  3805. case ENONE: return;
  3806. case ESPHR: sphere_start(des,lf); break;
  3807. case ESPEC: lf->evs.espec(des,lf); break;
  3808. default: LERR(("startlf: Invalid evaluation structure %d",ev(&lf->evs)));
  3809. }
  3810. /* renormalize for family=density */
  3811. if ((de_renorm) && (fam(&lf->sp)==TDEN)) dens_renorm(lf,des);
  3812. }
  3813. /*
  3814. * getnextdir() gets the next dir from a string dirpath="dir1:dir2:dir3:..."
  3815. * (;-separated on windows).
  3816. * The directory is returned through dirnext, and the function returns
  3817. * a pointer to the next string.
  3818. * typical usage is recursive, dirpath = getnextdir(dirpath,dirnext).
  3819. * with the above example, this sets dirnext="dir1" and dirpath="dir2:dir3:...".
  3820. * if the input dirpath has no :, then it is copied to dirnext, and return is "".
  3821. * if input dirpath is "", dirnext is set to "", and null pointer returned.
  3822. */
  3823. char *getnextdir(dirpath,dirnext)
  3824. char *dirpath, *dirnext;
  3825. { char *z;
  3826. if (strlen(dirpath)==0)
  3827. { sprintf(dirnext,"");
  3828. return(NULL);
  3829. }
  3830. z = strchr(dirpath,PATHSEP);
  3831. if (z==NULL)
  3832. { sprintf(dirnext,"%s%c",dirpath,DIRSEP);
  3833. return(&dirpath[strlen(dirnext)]);
  3834. }
  3835. *z = '\0';
  3836. sprintf(dirnext,"%s%c",dirpath,DIRSEP);
  3837. return(++z);
  3838. }
  3839. int initmodule(mdl, mod, dir, lf)
  3840. module *mdl;
  3841. lfit *lf;
  3842. char *mod, *dir;
  3843. { int n, d, p;
  3844. #ifdef ALLOW_MODULES
  3845. #ifdef WINDOWS
  3846. HINSTANCE res;
  3847. typedef void (CALLBACK* DLLFN)();
  3848. DLLFN init;
  3849. #else
  3850. void *res;
  3851. void (*init)();
  3852. #endif
  3853. char distname[500];
  3854. #endif
  3855. n = lf->lfd.n;
  3856. d = lf->lfd.d;
  3857. p = npar(&lf->sp) = calcp(&lf->sp,d);
  3858. mdl->isset = 1;
  3859. PPROC(lf) = NULL;
  3860. if (strcmp(mod,"std")==0) { initstd(lf); return(1); }
  3861. if (strcmp(mod,"simple")==0) { initsimple(lf); return(1); }
  3862. if (strcmp(mod,"allcf")==0) { initallcf(lf); return(1); }
  3863. if (strcmp(mod,"hatm")==0) { inithatm(lf); return(1); }
  3864. if (strcmp(mod,"kappa")==0) { initkappa(lf); return(1); }
  3865. if (strcmp(mod,"lscv")==0) { initlscv(lf); return(1); }
  3866. if (strcmp(mod,"gamf")==0) { initgam(lf); return(1); }
  3867. if (strcmp(mod,"gamp")==0) { initgam(lf); return(1); }
  3868. if (strcmp(mod,"rband")==0) { initrband(lf); return(1); }
  3869. if (strcmp(mod,"scb")==0) { initscb(lf); return(1); }
  3870. if (strcmp(mod,"vord")==0) { initvord(lf); return(1); }
  3871. #ifdef ALLOW_MODULES
  3872. while (dir != NULL)
  3873. {
  3874. dir = getnextdir(dir,distname);
  3875. sprintf(&distname[strlen(distname)],"mod%s.%s",mod,DLEXT);
  3876. res = DLOPEN(distname);
  3877. if (res==NULL)
  3878. {
  3879. #ifdef WINDOWS
  3880. mut_printf("LoadLibrary failed: %s, %d\n",distname,GetLastError());
  3881. #else
  3882. mut_printf("dlopen failed: %s\n",dlerror());
  3883. #endif
  3884. }
  3885. else
  3886. {
  3887. #ifdef WINDOWS
  3888. mut_printf("LoadLibrary success: %s\n",distname);
  3889. #else
  3890. mut_printf("dlopen success: %s\n",distname);
  3891. #endif
  3892. sprintf(distname,"init%s",mod);
  3893. init = (void *)DLSYM(res,distname);
  3894. if (init==NULL)
  3895. { mut_printf("I can't find %s() function.\n",distname);
  3896. mdl->isset = 0;
  3897. return(0);
  3898. }
  3899. init(lf);
  3900. return(1);
  3901. }
  3902. }
  3903. #endif
  3904. mdl->isset = 0;
  3905. return(0);
  3906. }
  3907. /*
  3908. * startmodule is the entry point to launch the fit.
  3909. * if mod is provided, will first initialize the module.
  3910. * if mod==NULL, assumes the module has been initialized separately.
  3911. */
  3912. void startmodule(lf,des,mod,dir)
  3913. lfit *lf;
  3914. design *des;
  3915. char *mod, *dir;
  3916. { int z;
  3917. if (mod != NULL)
  3918. { z = initmodule(&lf->mdl,mod,dir,lf);
  3919. if (!z) return;
  3920. }
  3921. lf->fp.nv = lf->evs.nce = 0;
  3922. if (lf_error) return;
  3923. if (PROCV(lf) != NULL) nstartlf(des,lf);
  3924. if (lf_error) return;
  3925. if (PPROC(lf) != NULL) PPROC(lf)(lf,des,lf->fp.kap);
  3926. }
  3927. /* for compatability, more or less. */
  3928. void startlf(des,lf,vfun,nopc)
  3929. design *des;
  3930. lfit *lf;
  3931. int (*vfun)(), nopc;
  3932. { int z;
  3933. z = initmodule(&lf->mdl,"std",NULL,lf);
  3934. if (!z) return;
  3935. lf->mdl.procv = vfun;
  3936. lf->mdl.nopc = nopc;
  3937. nstartlf(des,lf);
  3938. }