liblocf.c 115 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940
  1. /*
  2. * Copyright 1996-2006 Catherine Loader.
  3. */
  4. #include "mex.h"
  5. /*
  6. * Copyright 1996-2006 Catherine Loader.
  7. */
  8. /*
  9. * Integration for hazard rate estimation. The functions in this
  10. * file are used to evaluate
  11. * sum int_0^{Ti} W_i(t,x) A()A()' exp( P() ) dt
  12. * for hazard rate models.
  13. *
  14. * These routines assume the weight function is supported on [-1,1].
  15. * hasint_sph multiplies by exp(base(lf,i)), which allows estimating
  16. * the baseline in a proportional hazards model, when the covariate
  17. * effect base(lf,i) is known.
  18. *
  19. * TODO:
  20. * hazint_sph, should be able to reduce mint in some cases with
  21. * small integration range. onedint could be used for beta-family
  22. * (RECT,EPAN,BISQ,TRWT) kernels.
  23. * hazint_prod, restrict terms from the sum based on x values.
  24. * I should count obs >= max, and only do that integration once.
  25. */
  26. #include "locf.h"
  27. static double ilim[2*MXDIM], *ff, tmax;
  28. static lfdata *haz_lfd;
  29. static smpar *haz_sp;
  30. /*
  31. * hrao returns 0 if integration region is empty.
  32. * 1 otherwise.
  33. */
  34. int haz_sph_int(dfx,cf,h,r1)
  35. double *dfx, *cf, h, *r1;
  36. { double s, t0, t1, wt, th;
  37. int j, dim, p;
  38. s = 0; p = npar(haz_sp);
  39. dim = haz_lfd->d;
  40. for (j=1; j<dim; j++) s += SQR(dfx[j]/(h*haz_lfd->sca[j]));
  41. if (s>1) return(0);
  42. setzero(r1,p*p);
  43. t1 = sqrt(1-s)*h*haz_lfd->sca[0];
  44. t0 = -t1;
  45. if (t0<ilim[0]) t0 = ilim[0];
  46. if (t1>ilim[dim]) t1 = ilim[dim];
  47. if (t1>dfx[0]) t1 = dfx[0];
  48. if (t1<t0) return(0);
  49. /* Numerical integration by Simpson's rule.
  50. */
  51. for (j=0; j<=de_mint; j++)
  52. { dfx[0] = t0+(t1-t0)*j/de_mint;
  53. wt = weight(haz_lfd, haz_sp, dfx, NULL, h, 0, 0.0);
  54. fitfun(haz_lfd, haz_sp, dfx,NULL,ff,NULL);
  55. th = innerprod(cf,ff,p);
  56. if (link(haz_sp)==LLOG) th = exp(th);
  57. wt *= 2+2*(j&1)-(j==0)-(j==de_mint);
  58. addouter(r1,ff,ff,p,wt*th);
  59. }
  60. multmatscal(r1,(t1-t0)/(3*de_mint),p*p);
  61. return(1);
  62. }
  63. int hazint_sph(t,resp,r1,cf,h)
  64. double *t, *resp, *r1, *cf, h;
  65. { int i, j, n, p, st;
  66. double dfx[MXDIM], eb, sb;
  67. p = npar(haz_sp);
  68. setzero(resp,p*p);
  69. sb = 0.0;
  70. n = haz_lfd->n;
  71. for (i=0; i<=n; i++)
  72. {
  73. if (i==n)
  74. { dfx[0] = tmax-t[0];
  75. for (j=1; j<haz_lfd->d; j++) dfx[j] = 0.0;
  76. eb = exp(sb/n);
  77. }
  78. else
  79. { eb = exp(base(haz_lfd,i)); sb += base(haz_lfd,i);
  80. for (j=0; j<haz_lfd->d; j++) dfx[j] = datum(haz_lfd,j,i)-t[j];
  81. }
  82. st = haz_sph_int(dfx,cf,h,r1);
  83. if (st)
  84. for (j=0; j<p*p; j++) resp[j] += eb*r1[j];
  85. }
  86. return(LF_OK);
  87. }
  88. int hazint_prod(t,resp,x,cf,h)
  89. double *t, *resp, *x, *cf, h;
  90. { int d, p, i, j, k, st;
  91. double dfx[MXDIM], t_prev,
  92. hj, hs, ncf[MXDEG], ef, il1;
  93. double prod_wk[MXDIM][2*MXDEG+1], eb, sb;
  94. p = npar(haz_sp);
  95. d = haz_lfd->d;
  96. setzero(resp,p*p);
  97. hj = hs = h*haz_lfd->sca[0];
  98. ncf[0] = cf[0];
  99. for (i=1; i<=deg(haz_sp); i++)
  100. { ncf[i] = hj*cf[(i-1)*d+1]; hj *= hs;
  101. }
  102. /* for i=0..n....
  103. * First we compute prod_wk[j], j=0..d.
  104. * For j=0, this is int_0^T_i (u-t)^k W((u-t)/h) exp(b0*(u-t)) du
  105. * For remaining j, (x(i,j)-x(j))^k Wj exp(bj*(x..-x.))
  106. *
  107. * Second, we add to the integration (exp(a) incl. in integral)
  108. * with the right factorial denominators.
  109. */
  110. t_prev = ilim[0]; sb = 0.0;
  111. for (i=0; i<=haz_lfd->n; i++)
  112. { if (i==haz_lfd->n)
  113. { dfx[0] = tmax-t[0];
  114. for (j=1; j<d; j++) dfx[j] = 0.0;
  115. eb = exp(sb/haz_lfd->n);
  116. }
  117. else
  118. { eb = exp(base(haz_lfd,i)); sb += base(haz_lfd,i);
  119. for (j=0; j<d; j++) dfx[j] = datum(haz_lfd,j,i)-t[j];
  120. }
  121. if (dfx[0]>ilim[0]) /* else it doesn't contribute */
  122. {
  123. /* time integral */
  124. il1 = (dfx[0]>ilim[d]) ? ilim[d] : dfx[0];
  125. if (il1 != t_prev) /* don't repeat! */
  126. { st = onedint(haz_sp,ncf,ilim[0]/hs,il1/hs,prod_wk[0]);
  127. if (st>0) return(st);
  128. hj = eb;
  129. for (j=0; j<=2*deg(haz_sp); j++)
  130. { hj *= hs;
  131. prod_wk[0][j] *= hj;
  132. }
  133. t_prev = il1;
  134. }
  135. /* covariate terms */
  136. for (j=1; j<d; j++)
  137. {
  138. ef = 0.0;
  139. for (k=deg(haz_sp); k>0; k--) ef = (ef+dfx[j])*cf[1+(k-1)*d+j];
  140. ef = exp(ef);
  141. prod_wk[j][0] = ef * W(dfx[j]/(h*haz_lfd->sca[j]),ker(haz_sp));
  142. for (k=1; k<=2*deg(haz_sp); k++)
  143. prod_wk[j][k] = prod_wk[j][k-1] * dfx[j];
  144. }
  145. /* add to the integration. */
  146. prodintresp(resp,prod_wk,d,deg(haz_sp),p);
  147. } /* if dfx0 > ilim0 */
  148. } /* n loop */
  149. /* symmetrize */
  150. for (k=0; k<p; k++)
  151. for (j=k; j<p; j++)
  152. resp[j*p+k] = resp[k*p+j];
  153. return(LF_OK);
  154. }
  155. int hazint(t,resp,resp1,cf,h)
  156. double *t, *resp, *resp1, *cf, h;
  157. { if (haz_lfd->d==1) return(hazint_prod(t,resp,resp1,cf,h));
  158. if (kt(haz_sp)==KPROD) return(hazint_prod(t,resp,resp1,cf,h));
  159. return(hazint_sph(t,resp,resp1,cf,h));
  160. }
  161. void haz_init(lfd,des,sp,il)
  162. lfdata *lfd;
  163. design *des;
  164. smpar *sp;
  165. double *il;
  166. { int i;
  167. haz_lfd = lfd;
  168. haz_sp = sp;
  169. tmax = datum(lfd,0,0);
  170. for (i=1; i<lfd->n; i++) tmax = MAX(tmax,datum(lfd,0,i));
  171. ff = des->xtwx.wk;
  172. for (i=0; i<2*lfd->d; i++) ilim[i] = il[i];
  173. }
  174. /*
  175. * Copyright 1996-2006 Catherine Loader.
  176. */
  177. /*
  178. *
  179. * Routines for one-dimensional numerical integration
  180. * in density estimation. The entry point is
  181. *
  182. * onedint(cf,mi,l0,l1,resp)
  183. *
  184. * which evaluates int W(u)u^j exp( P(u) ), j=0..2*deg.
  185. * P(u) = cf[0] + cf[1]u + cf[2]u^2/2 + ... + cf[deg]u^deg/deg!
  186. * l0 and l1 are the integration limits.
  187. * The results are returned through the vector resp.
  188. *
  189. */
  190. #include "locf.h"
  191. static int debug;
  192. int exbctay(b,c,n,z) /* n-term taylor series of e^(bx+cx^2) */
  193. double b, c, *z;
  194. int n;
  195. { double ec[20];
  196. int i, j;
  197. z[0] = 1;
  198. for (i=1; i<=n; i++) z[i] = z[i-1]*b/i;
  199. if (c==0.0) return(n);
  200. if (n>=40)
  201. { WARN(("exbctay limit to n<40"));
  202. n = 39;
  203. }
  204. ec[0] = 1;
  205. for (i=1; 2*i<=n; i++) ec[i] = ec[i-1]*c/i;
  206. for (i=n; i>1; i--)
  207. for (j=1; 2*j<=i; j++)
  208. z[i] += ec[j]*z[i-2*j];
  209. return(n);
  210. }
  211. double explinjtay(l0,l1,j,cf)
  212. /* int_l0^l1 x^j e^(a+bx+cx^2); exbctay aroud l1 */
  213. double l0, l1, *cf;
  214. int j;
  215. { double tc[40], f, s;
  216. int k, n;
  217. if ((l0!=0.0) | (l1!=1.0)) WARN(("explinjtay: invalid l0, l1"));
  218. n = exbctay(cf[1]+2*cf[2]*l1,cf[2],20,tc);
  219. s = tc[0]/(j+1);
  220. f = 1/(j+1);
  221. for (k=1; k<=n; k++)
  222. { f *= -k/(j+k+1.0);
  223. s += tc[k]*f;
  224. }
  225. return(f);
  226. }
  227. void explint1(l0,l1,cf,I,p) /* int x^j exp(a+bx); j=0..p-1 */
  228. double l0, l1, *cf, *I;
  229. int p;
  230. { double y0, y1, f;
  231. int j, k, k1;
  232. y0 = mut_exp(cf[0]+l0*cf[1]);
  233. y1 = mut_exp(cf[0]+l1*cf[1]);
  234. if (p<2*fabs(cf[1])) k = p; else k = (int)fabs(cf[1]);
  235. if (k>0)
  236. { I[0] = (y1-y0)/cf[1];
  237. for (j=1; j<k; j++) /* forward steps for small j */
  238. { y1 *= l1; y0 *= l0;
  239. I[j] = (y1-y0-j*I[j-1])/cf[1];
  240. }
  241. if (k==p) return;
  242. y1 *= l1; y0 *= l0;
  243. }
  244. f = 1; k1 = k;
  245. while ((k<50) && (f>1.0e-8)) /* initially Ik = diff(x^{k+1}e^{a+bx}) */
  246. { y1 *= l1; y0 *= l0;
  247. I[k] = y1-y0;
  248. if (k>=p) f *= fabs(cf[1])/(k+1);
  249. k++;
  250. }
  251. if (k==50) WARN(("explint1: want k>50"));
  252. I[k] = 0.0;
  253. for (j=k-1; j>=k1; j--) /* now do back step recursion */
  254. I[j] = (I[j]-cf[1]*I[j+1])/(j+1);
  255. }
  256. void explintyl(l0,l1,cf,I,p) /* small c, use taylor series and explint1 */
  257. double l0, l1, *cf, *I;
  258. int p;
  259. { int i;
  260. double c;
  261. explint1(l0,l1,cf,I,p+8);
  262. c = cf[2];
  263. for (i=0; i<p; i++)
  264. I[i] = (((I[i+8]*c/4+I[i+6])*c/3+I[i+4])*c/2+I[i+2])*c+I[i];
  265. }
  266. void solvetrid(X,y,m)
  267. double *X, *y;
  268. int m;
  269. { int i;
  270. double s;
  271. for (i=1; i<m; i++)
  272. { s = X[3*i]/X[3*i-2];
  273. X[3*i] = 0; X[3*i+1] -= s*X[3*i-1];
  274. y[i] -= s*y[i-1];
  275. }
  276. for (i=m-2; i>=0; i--)
  277. { s = X[3*i+2]/X[3*i+4];
  278. X[3*i+2] = 0;
  279. y[i] -= s*y[i+1];
  280. }
  281. for (i=0; i<m; i++) y[i] /= X[3*i+1];
  282. }
  283. void initi0i1(I,cf,y0,y1,l0,l1)
  284. double *I, *cf, y0, y1, l0, l1;
  285. { double a0, a1, c, d, bi;
  286. d = -cf[1]/(2*cf[2]); c = sqrt(2*fabs(cf[2]));
  287. a0 = c*(l0-d); a1 = c*(l1-d);
  288. if (cf[2]<0)
  289. { bi = mut_exp(cf[0]+cf[1]*d+cf[2]*d*d)/c;
  290. if (a0>0)
  291. { if (a0>6) I[0] = (y0*ptail(-a0)-y1*ptail(-a1))/c;
  292. else I[0] = S2PI*(mut_pnorm(-a0)-mut_pnorm(-a1))*bi;
  293. }
  294. else
  295. { if (a1< -6) I[0] = (y1*ptail(a1)-y0*ptail(a0))/c;
  296. else I[0] = S2PI*(mut_pnorm(a1)-mut_pnorm(a0))*bi;
  297. }
  298. }
  299. else
  300. I[0] = (y1*mut_daws(a1)-y0*mut_daws(a0))/c;
  301. I[1] = (y1-y0)/(2*cf[2])+d*I[0];
  302. }
  303. void explinsid(l0,l1,cf,I,p) /* large b; don't use fwd recursion */
  304. double l0, l1, *cf, *I;
  305. int p;
  306. { int k, k0, k1, k2;
  307. double y0, y1, Z[150];
  308. if (debug) mut_printf("side: %8.5f %8.5f %8.5f limt %8.5f %8.5f p %2d\n",cf[0],cf[1],cf[2],l0,l1,p);
  309. k0 = 2;
  310. k1 = (int)(fabs(cf[1])+fabs(2*cf[2]));
  311. if (k1<2) k1 = 2;
  312. if (k1>p+20) k1 = p+20;
  313. k2 = p+20;
  314. if (k2>50) { mut_printf("onedint: k2 warning\n"); k2 = 50; }
  315. if (debug) mut_printf("k0 %2d k1 %2d k2 %2d p %2d\n",k0,k1,k2,p);
  316. y0 = mut_exp(cf[0]+l0*(cf[1]+l0*cf[2]));
  317. y1 = mut_exp(cf[0]+l1*(cf[1]+l1*cf[2]));
  318. initi0i1(I,cf,y0,y1,l0,l1);
  319. if (debug) mut_printf("i0 %8.5f i1 %8.5f\n",I[0],I[1]);
  320. y1 *= l1; y0 *= l0; /* should be x^(k1)*exp(..) */
  321. if (k0<k1) /* center steps; initially x^k*exp(...) */
  322. for (k=k0; k<k1; k++)
  323. { y1 *= l1; y0 *= l0;
  324. I[k] = y1-y0;
  325. Z[3*k] = k; Z[3*k+1] = cf[1]; Z[3*k+2] = 2*cf[2];
  326. }
  327. y1 *= l1; y0 *= l0; /* should be x^(k1)*exp(..) */
  328. if (debug) mut_printf("k1 %2d y0 %8.5f y1 %8.5f\n",k1,y0,y1);
  329. for (k=k1; k<k2; k++)
  330. { y1 *= l1; y0 *= l0;
  331. I[k] = y1-y0;
  332. }
  333. I[k2] = I[k2+1] = 0.0;
  334. for (k=k2-1; k>=k1; k--)
  335. I[k] = (I[k]-cf[1]*I[k+1]-2*cf[2]*I[k+2])/(k+1);
  336. if (k0<k1)
  337. { I[k0] -= k0*I[k0-1];
  338. I[k1-1] -= 2*cf[2]*I[k1];
  339. Z[3*k0] = Z[3*k1-1] = 0;
  340. solvetrid(&Z[3*k0],&I[k0],k1-k0);
  341. }
  342. if (debug)
  343. { mut_printf("explinsid:\n");
  344. for (k=0; k<p; k++) mut_printf(" %8.5f\n",I[k]);
  345. }
  346. }
  347. void explinbkr(l0,l1,cf,I,p) /* small b,c; use back recursion */
  348. double l0, l1, *cf, *I;
  349. int p;
  350. { int k, km;
  351. double y0, y1;
  352. y0 = mut_exp(cf[0]+l0*(cf[1]+cf[2]*l0));
  353. y1 = mut_exp(cf[0]+l1*(cf[1]+cf[2]*l1));
  354. km = p+10;
  355. for (k=0; k<=km; k++)
  356. { y1 *= l1; y0 *= l0;
  357. I[k] = y1-y0;
  358. }
  359. I[km+1] = I[km+2] = 0;
  360. for (k=km; k>=0; k--)
  361. I[k] = (I[k]-cf[1]*I[k+1]-2*cf[2]*I[k+2])/(k+1);
  362. }
  363. void explinfbk0(l0,l1,cf,I,p) /* fwd and bac recur; b=0; c<0 */
  364. double l0, l1, *cf, *I;
  365. int p;
  366. { double y0, y1, f1, f2, f, ml2;
  367. int k, ks;
  368. y0 = mut_exp(cf[0]+l0*l0*cf[2]);
  369. y1 = mut_exp(cf[0]+l1*l1*cf[2]);
  370. initi0i1(I,cf,y0,y1,l0,l1);
  371. ml2 = MAX(l0*l0,l1*l1);
  372. ks = 1+(int)(2*fabs(cf[2])*ml2);
  373. if (ks<2) ks = 2;
  374. if (ks>p-3) ks = p;
  375. /* forward recursion for k < ks */
  376. for (k=2; k<ks; k++)
  377. { y1 *= l1; y0 *= l0;
  378. I[k] = (y1-y0-(k-1)*I[k-2])/(2*cf[2]);
  379. }
  380. if (ks==p) return;
  381. y1 *= l1*l1; y0 *= l0*l0;
  382. for (k=ks; k<p; k++) /* set I[k] = x^{k+1}e^(a+cx^2) | {l0,l1} */
  383. { y1 *= l1; y0 *= l0;
  384. I[k] = y1-y0;
  385. }
  386. /* initialize I[p-2] and I[p-1] */
  387. f1 = 1.0/p; f2 = 1.0/(p-1);
  388. I[p-1] *= f1; I[p-2] *= f2;
  389. k = p; f = 1.0;
  390. while (f>1.0e-8)
  391. { y1 *= l1; y0 *= l0;
  392. if ((k-p)%2==0) /* add to I[p-2] */
  393. { f2 *= -2*cf[2]/(k+1);
  394. I[p-2] += (y1-y0)*f2;
  395. }
  396. else /* add to I[p-1] */
  397. { f1 *= -2*cf[2]/(k+1);
  398. I[p-1] += (y1-y0)*f1;
  399. f *= 2*fabs(cf[2])*ml2/(k+1);
  400. }
  401. k++;
  402. }
  403. /* use back recursion for I[ks..(p-3)] */
  404. for (k=p-3; k>=ks; k--)
  405. I[k] = (I[k]-2*cf[2]*I[k+2])/(k+1);
  406. }
  407. void explinfbk(l0,l1,cf,I,p) /* fwd and bac recur; b not too large */
  408. double l0, l1, *cf, *I;
  409. int p;
  410. { double y0, y1;
  411. int k, ks, km;
  412. y0 = mut_exp(cf[0]+l0*(cf[1]+l0*cf[2]));
  413. y1 = mut_exp(cf[0]+l1*(cf[1]+l1*cf[2]));
  414. initi0i1(I,cf,y0,y1,l0,l1);
  415. ks = (int)(3*fabs(cf[2]));
  416. if (ks<3) ks = 3;
  417. if (ks>0.75*p) ks = p; /* stretch the forward recurs as far as poss. */
  418. /* forward recursion for k < ks */
  419. for (k=2; k<ks; k++)
  420. { y1 *= l1; y0 *= l0;
  421. I[k] = (y1-y0-cf[1]*I[k-1]-(k-1)*I[k-2])/(2*cf[2]);
  422. }
  423. if (ks==p) return;
  424. km = p+15;
  425. y1 *= l1*l1; y0 *= l0*l0;
  426. for (k=ks; k<=km; k++)
  427. { y1 *= l1; y0 *= l0;
  428. I[k] = y1-y0;
  429. }
  430. I[km+1] = I[km+2] = 0.0;
  431. for (k=km; k>=ks; k--)
  432. I[k] = (I[k]-cf[1]*I[k+1]-2*cf[2]*I[k+2])/(k+1);
  433. }
  434. void recent(I,resp,wt,p,s,x)
  435. double *I, *resp, *wt, x;
  436. int p, s;
  437. { int i, j;
  438. /* first, use W taylor series I -> resp */
  439. for (i=0; i<=p; i++)
  440. { resp[i] = 0.0;
  441. for (j=0; j<s; j++) resp[i] += wt[j]*I[i+j];
  442. }
  443. /* now, recenter x -> 0 */
  444. if (x==0) return;
  445. for (j=0; j<=p; j++) for (i=p; i>j; i--) resp[i] += x*resp[i-1];
  446. }
  447. void recurint(l0,l2,cf,resp,p,ker)
  448. double l0, l2, *cf, *resp;
  449. int p, ker;
  450. { int i, s;
  451. double l1, d0, d1, d2, dl, z0, z1, z2, wt[20], ncf[3], I[50], r1[5], r2[5];
  452. if (debug) mut_printf("\nrecurint: %8.5f %8.5f %8.5f %8.5f %8.5f\n",cf[0],cf[1],cf[2],l0,l2);
  453. if (cf[2]==0) /* go straight to explint1 */
  454. { s = wtaylor(wt,0.0,ker);
  455. if (debug) mut_printf("case 1\n");
  456. explint1(l0,l2,cf,I,p+s);
  457. recent(I,resp,wt,p,s,0.0);
  458. return;
  459. }
  460. dl = l2-l0;
  461. d0 = cf[1]+2*l0*cf[2];
  462. d2 = cf[1]+2*l2*cf[2];
  463. z0 = cf[0]+l0*(cf[1]+l0*cf[2]);
  464. z2 = cf[0]+l2*(cf[1]+l2*cf[2]);
  465. if ((fabs(cf[1]*dl)<1) && (fabs(cf[2]*dl*dl)<1))
  466. { ncf[0] = z0; ncf[1] = d0; ncf[2] = cf[2];
  467. if (debug) mut_printf("case 2\n");
  468. s = wtaylor(wt,l0,ker);
  469. explinbkr(0.0,dl,ncf,I,p+s);
  470. recent(I,resp,wt,p,s,l0);
  471. return;
  472. }
  473. if (fabs(cf[2]*dl*dl)<0.001) /* small c, use explint1+tay.ser */
  474. { ncf[0] = z0; ncf[1] = d0; ncf[2] = cf[2];
  475. if (debug) mut_printf("case small c\n");
  476. s = wtaylor(wt,l0,ker);
  477. explintyl(0.0,l2-l0,ncf,I,p+s);
  478. recent(I,resp,wt,p,s,l0);
  479. return;
  480. }
  481. if (d0*d2<=0) /* max/min in [l0,l2] */
  482. { l1 = -cf[1]/(2*cf[2]);
  483. z1 = cf[0]+l1*(cf[1]+l1*cf[2]);
  484. d1 = 0.0;
  485. if (cf[2]<0) /* peak, integrate around l1 */
  486. { s = wtaylor(wt,l1,ker);
  487. ncf[0] = z1; ncf[1] = 0.0; ncf[2] = cf[2];
  488. if (debug) mut_printf("case peak p %2d s %2d\n",p,s);
  489. explinfbk0(l0-l1,l2-l1,ncf,I,p+s);
  490. recent(I,resp,wt,p,s,l1);
  491. return;
  492. }
  493. }
  494. if ((d0-2*cf[2]*dl)*(d2+2*cf[2]*dl)<0) /* max/min is close to [l0,l2] */
  495. { l1 = -cf[1]/(2*cf[2]);
  496. z1 = cf[0]+l1*(cf[1]+l1*cf[2]);
  497. if (l1<l0) { l1 = l0; z1 = z0; }
  498. if (l1>l2) { l1 = l2; z1 = z2; }
  499. if ((z1>=z0) & (z1>=z2)) /* peak; integrate around l1 */
  500. { s = wtaylor(wt,l1,ker);
  501. if (debug) mut_printf("case 4\n");
  502. d1 = cf[1]+2*l1*cf[2];
  503. ncf[0] = z1; ncf[1] = d1; ncf[2] = cf[2];
  504. explinfbk(l0-l1,l2-l1,ncf,I,p+s);
  505. recent(I,resp,wt,p,s,l1);
  506. return;
  507. }
  508. /* trough; integrate [l0,l1] and [l1,l2] */
  509. for (i=0; i<=p; i++) r1[i] = r2[i] = 0.0;
  510. if (l0<l1)
  511. { s = wtaylor(wt,l0,ker);
  512. if (debug) mut_printf("case 5\n");
  513. ncf[0] = z0; ncf[1] = d0; ncf[2] = cf[2];
  514. explinfbk(0.0,l1-l0,ncf,I,p+s);
  515. recent(I,r1,wt,p,s,l0);
  516. }
  517. if (l1<l2)
  518. { s = wtaylor(wt,l2,ker);
  519. if (debug) mut_printf("case 6\n");
  520. ncf[0] = z2; ncf[1] = d2; ncf[2] = cf[2];
  521. explinfbk(l1-l2,0.0,ncf,I,p+s);
  522. recent(I,r2,wt,p,s,l2);
  523. }
  524. for (i=0; i<=p; i++) resp[i] = r1[i]+r2[i];
  525. return;
  526. }
  527. /* Now, quadratic is monotone on [l0,l2]; big b; moderate c */
  528. if (z2>z0+3) /* steep increase, expand around l2 */
  529. { s = wtaylor(wt,l2,ker);
  530. if (debug) mut_printf("case 7\n");
  531. ncf[0] = z2; ncf[1] = d2; ncf[2] = cf[2];
  532. explinsid(l0-l2,0.0,ncf,I,p+s);
  533. recent(I,resp,wt,p,s,l2);
  534. if (debug) mut_printf("7 resp: %8.5f %8.5f %8.5f %8.5f\n",resp[0],resp[1],resp[2],resp[3]);
  535. return;
  536. }
  537. /* bias towards expansion around l0, because it's often 0 */
  538. if (debug) mut_printf("case 8\n");
  539. s = wtaylor(wt,l0,ker);
  540. ncf[0] = z0; ncf[1] = d0; ncf[2] = cf[2];
  541. explinsid(0.0,l2-l0,ncf,I,p+s);
  542. recent(I,resp,wt,p,s,l0);
  543. return;
  544. }
  545. int onedexpl(cf,deg,resp)
  546. double *cf, *resp;
  547. int deg;
  548. { int i;
  549. double f0, fr, fl;
  550. if (deg>=2) LERR(("onedexpl only valid for deg=0,1"));
  551. if (fabs(cf[1])>=EFACT) return(LF_BADP);
  552. f0 = exp(cf[0]); fl = fr = 1.0;
  553. for (i=0; i<=2*deg; i++)
  554. { f0 *= i+1;
  555. fl /=-(EFACT+cf[1]);
  556. fr /= EFACT-cf[1];
  557. resp[i] = f0*(fr-fl);
  558. }
  559. return(LF_OK);
  560. }
  561. int onedgaus(cf,deg,resp)
  562. double *cf, *resp;
  563. int deg;
  564. { int i;
  565. double f0, mu, s2;
  566. if (deg==3)
  567. { LERR(("onedgaus only valid for deg=0,1,2"));
  568. return(LF_ERR);
  569. }
  570. if (2*cf[2]>=GFACT*GFACT) return(LF_BADP);
  571. s2 = 1/(GFACT*GFACT-2*cf[2]);
  572. mu = cf[1]*s2;
  573. resp[0] = 1.0;
  574. if (deg>=1)
  575. { resp[1] = mu;
  576. resp[2] = s2+mu*mu;
  577. if (deg==2)
  578. { resp[3] = mu*(3*s2+mu*mu);
  579. resp[4] = 3*s2*s2 + mu*mu*(6*s2+mu*mu);
  580. }
  581. }
  582. f0 = S2PI * exp(cf[0]+mu*mu/(2*s2))*sqrt(s2);
  583. for (i=0; i<=2*deg; i++) resp[i] *= f0;
  584. return(LF_OK);
  585. }
  586. int onedint(sp,cf,l0,l1,resp) /* int W(u)u^j exp(..), j=0..2*deg */
  587. smpar *sp;
  588. double *cf, l0, l1, *resp;
  589. { double u, uj, y, ncf[4], rr[5];
  590. int i, j;
  591. if (debug) mut_printf("onedint: %f %f %f %f %f\n",cf[0],cf[1],cf[2],l0,l1);
  592. if (deg(sp)<=2)
  593. { for (i=0; i<3; i++) ncf[i] = (i>deg(sp)) ? 0.0 : cf[i];
  594. ncf[2] /= 2;
  595. if (ker(sp)==WEXPL) return(onedexpl(ncf,deg(sp),resp));
  596. if (ker(sp)==WGAUS) return(onedgaus(ncf,deg(sp),resp));
  597. if (l1>0)
  598. recurint(MAX(l0,0.0),l1,ncf,resp,2*deg(sp),ker(sp));
  599. else for (i=0; i<=2*deg(sp); i++) resp[i] = 0;
  600. if (l0<0)
  601. { ncf[1] = -ncf[1];
  602. l0 = -l0; l1 = -l1;
  603. recurint(MAX(l1,0.0),l0,ncf,rr,2*deg(sp),ker(sp));
  604. }
  605. else for (i=0; i<=2*deg(sp); i++) rr[i] = 0.0;
  606. for (i=0; i<=2*deg(sp); i++)
  607. resp[i] += (i%2==0) ? rr[i] : -rr[i];
  608. return(LF_OK);
  609. }
  610. /* For degree >= 3, we use Simpson's rule. */
  611. for (j=0; j<=2*deg(sp); j++) resp[j] = 0.0;
  612. for (i=0; i<=de_mint; i++)
  613. { u = l0+(l1-l0)*i/de_mint;
  614. y = cf[0]; uj = 1;
  615. for (j=1; j<=deg(sp); j++)
  616. { uj *= u;
  617. y += cf[j]*uj/fact[j];
  618. }
  619. y = (4-2*(i%2==0)-(i==0)-(i==de_mint)) *
  620. W(fabs(u),ker(sp))*exp(MIN(y,300.0));
  621. for (j=0; j<=2*deg(sp); j++)
  622. { resp[j] += y;
  623. y *= u;
  624. }
  625. }
  626. for (j=0; j<=2*deg(sp); j++) resp[j] = resp[j]*(l1-l0)/(3*de_mint);
  627. return(LF_OK);
  628. }
  629. /*
  630. * Copyright 1996-2006 Catherine Loader.
  631. */
  632. #include "locf.h"
  633. extern int lf_status;
  634. static double u[MXDIM], ilim[2*MXDIM], *ff, hh, *cff;
  635. static lfdata *den_lfd;
  636. static design *den_des;
  637. static smpar *den_sp;
  638. int fact[] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800};
  639. int de_mint = 20;
  640. int de_itype = IDEFA;
  641. int de_renorm= 0;
  642. int multint(), prodint(), gausint(), mlinint();
  643. #define NITYPE 7
  644. static char *itype[NITYPE] = { "default", "multi", "product", "mlinear",
  645. "hazard", "sphere", "monte" };
  646. static int ivals[NITYPE] =
  647. { IDEFA, IMULT, IPROD, IMLIN, IHAZD, ISPHR, IMONT };
  648. int deitype(char *z)
  649. { return(pmatch(z, itype, ivals, NITYPE, IDEFA));
  650. }
  651. void prresp(coef,resp,p)
  652. double *coef, *resp;
  653. int p;
  654. { int i, j;
  655. mut_printf("Coefficients:\n");
  656. for (i=0; i<p; i++) mut_printf("%8.5f ",coef[i]);
  657. mut_printf("\n");
  658. mut_printf("Response matrix:\n");
  659. for (i=0; i<p; i++)
  660. { for (j=0; j<p; j++) mut_printf("%9.6f, ",resp[i+j*p]);
  661. mut_printf("\n");
  662. }
  663. }
  664. int mif(u,d,resp,M)
  665. double *u, *resp, *M;
  666. int d;
  667. { double wt;
  668. int i, j, p;
  669. p = den_des->p;
  670. wt = weight(den_lfd, den_sp, u, NULL, hh, 0, 0.0);
  671. if (wt==0)
  672. { setzero(resp,p*p);
  673. return(p*p);
  674. }
  675. fitfun(den_lfd, den_sp, u,NULL,ff,NULL);
  676. if (link(den_sp)==LLOG)
  677. wt *= mut_exp(innerprod(ff,cff,p));
  678. for (i=0; i<p; i++)
  679. for (j=0; j<p; j++)
  680. resp[i*p+j] = wt*ff[i]*ff[j];
  681. return(p*p);
  682. }
  683. int multint(t,resp1,resp2,cf,h)
  684. double *t, *resp1, *resp2, *cf, h;
  685. { int d, i, mg[MXDIM];
  686. if (ker(den_sp)==WGAUS) return(gausint(t,resp1,resp2,cf,h,den_lfd->sca));
  687. d = den_lfd->d;
  688. for (i=0; i<d; i++) mg[i] = de_mint;
  689. hh = h;
  690. cff= cf;
  691. simpsonm(mif,ilim,&ilim[d],d,resp1,mg,resp2);
  692. return(LF_OK);
  693. }
  694. int mlinint(t,resp1,resp2,cf,h)
  695. double *t, *resp1, *resp2, *cf, h;
  696. {
  697. double hd, nb, wt, wu, g[4], w0, w1, v, *sca;
  698. int d, p, i, j, jmax, k, l, z, jj[2];
  699. d = den_lfd->d; p = den_des->p; sca = den_lfd->sca;
  700. hd = 1;
  701. for (i=0; i<d; i++) hd *= h*sca[i];
  702. if (link(den_sp)==LIDENT)
  703. { setzero(resp1,p*p);
  704. resp1[0] = wint(d,NULL,0,ker(den_sp))*hd;
  705. if (deg(den_sp)==0) return(LF_OK);
  706. jj[0] = 2; w0 = wint(d,jj,1,ker(den_sp))*hd*h*h;
  707. for (i=0; i<d; i++) resp1[(i+1)*p+i+1] = w0*sca[i]*sca[i];
  708. if (deg(den_sp)==1) return(LF_OK);
  709. for (i=0; i<d; i++)
  710. { j = p-(d-i)*(d-i+1)/2;
  711. resp1[j] = resp1[p*j] = w0*sca[i]*sca[i]/2;
  712. }
  713. if (d>1)
  714. { jj[1] = 2;
  715. w0 = wint(d,jj,2,ker(den_sp)) * hd*h*h*h*h;
  716. }
  717. jj[0] = 4;
  718. w1 = wint(d,jj,1,ker(den_sp)) * hd*h*h*h*h/4;
  719. z = d+1;
  720. for (i=0; i<d; i++)
  721. { k = p-(d-i)*(d-i+1)/2;
  722. for (j=i; j<d; j++)
  723. { l = p-(d-j)*(d-j+1)/2;
  724. if (i==j) resp1[z*p+z] = w1*SQR(sca[i])*SQR(sca[i]);
  725. else
  726. { resp1[z*p+z] = w0*SQR(sca[i])*SQR(sca[j]);
  727. resp1[k*p+l] = resp1[k+p*l] = w0/4*SQR(sca[i])*SQR(sca[j]);
  728. }
  729. z++;
  730. } }
  731. return(LF_OK);
  732. }
  733. switch(deg(den_sp))
  734. { case 0:
  735. resp1[0] = mut_exp(cf[0])*wint(d,NULL,0,ker(den_sp))*hd;
  736. return(LF_OK);
  737. case 1:
  738. nb = 0.0;
  739. for (i=1; i<=d; i++)
  740. { v = h*cf[i]*sca[i-1];
  741. nb += v*v;
  742. }
  743. if (ker(den_sp)==WGAUS)
  744. { w0 = 1/(GFACT*GFACT);
  745. g[0] = mut_exp(cf[0]+w0*nb/2+d*log(S2PI/2.5));
  746. g[1] = g[3] = g[0]*w0;
  747. g[2] = g[0]*w0*w0;
  748. }
  749. else
  750. { wt = wu = mut_exp(cf[0]);
  751. w0 = wint(d,NULL,0,ker(den_sp)); g[0] = wt*w0;
  752. g[1] = g[2] = g[3] = 0.0;
  753. j = 0; jmax = (d+2)*de_mint;
  754. while ((j<jmax) && (wt*w0/g[0]>1.0e-8))
  755. { j++;
  756. jj[0] = 2*j; w0 = wint(d,jj,1,ker(den_sp));
  757. if (d==1) g[3] += wt * w0;
  758. else
  759. { jj[0] = 2; jj[1] = 2*j-2; w1 = wint(d,jj,2,ker(den_sp));
  760. g[3] += wt*w1;
  761. g[2] += wu*(w0-w1);
  762. }
  763. wt /= (2*j-1.0); g[1] += wt*w0;
  764. wt *= nb/(2*j); g[0] += wt*w0;
  765. wu /= (2*j-1.0)*(2*j);
  766. if (j>1) wu *= nb;
  767. }
  768. if (j==jmax) WARN(("mlinint: series not converged"));
  769. }
  770. g[0] *= hd; g[1] *= hd;
  771. g[2] *= hd; g[3] *= hd;
  772. resp1[0] = g[0];
  773. for (i=1; i<=d; i++)
  774. { resp1[i] = resp1[(d+1)*i] = cf[i]*SQR(h*sca[i-1])*g[1];
  775. for (j=1; j<=d; j++)
  776. { resp1[(d+1)*i+j] = (i==j) ? g[3]*SQR(h*sca[i-1]) : 0;
  777. resp1[(d+1)*i+j] += g[2]*SQR(h*h*sca[i-1]*sca[j-1])*cf[i]*cf[j];
  778. }
  779. }
  780. return(LF_OK);
  781. }
  782. LERR(("mlinint: deg=0,1 only"));
  783. return(LF_ERR);
  784. }
  785. void prodintresp(resp,prod_wk,dim,deg,p)
  786. double *resp, prod_wk[MXDIM][2*MXDEG+1];
  787. int dim, deg, p;
  788. { double prod;
  789. int i, j, k, j1, k1;
  790. prod = 1.0;
  791. for (i=0; i<dim; i++) prod *= prod_wk[i][0];
  792. resp[0] += prod;
  793. if (deg==0) return;
  794. for (j1=1; j1<=deg; j1++)
  795. { for (j=0; j<dim; j++)
  796. { prod = 1.0;
  797. for (i=0; i<dim; i++) prod *= prod_wk[i][j1*(j==i)];
  798. prod /= fact[j1];
  799. resp[1 + (j1-1)*dim +j] += prod;
  800. }
  801. }
  802. for (k1=1; k1<=deg; k1++)
  803. for (j1=k1; j1<=deg; j1++)
  804. { for (k=0; k<dim; k++)
  805. for (j=0; j<dim; j++)
  806. { prod = 1.0;
  807. for (i=0; i<dim; i++) prod *= prod_wk[i][k1*(k==i) + j1*(j==i)];
  808. prod /= fact[k1]*fact[j1];
  809. resp[ (1+(k1-1)*dim+k)*p + 1+(j1-1)*dim+j] += prod;
  810. }
  811. }
  812. }
  813. int prodint(t,resp,resp2,coef,h)
  814. double *t, *resp, *resp2, *coef, h;
  815. { int dim, p, i, j, k, st;
  816. double cf[MXDEG+1], hj, hs, prod_wk[MXDIM][2*MXDEG+1];
  817. dim = den_lfd->d;
  818. p = den_des->p;
  819. for (i=0; i<p*p; i++) resp[i] = 0.0;
  820. cf[0] = coef[0];
  821. /* compute the one dimensional terms
  822. */
  823. for (i=0; i<dim; i++)
  824. { hj = 1; hs = h*den_lfd->sca[i];
  825. for (j=0; j<deg(den_sp); j++)
  826. { hj *= hs;
  827. cf[j+1] = hj*coef[ j*dim+i+1 ];
  828. }
  829. st = onedint(den_sp,cf,ilim[i]/hs,ilim[i+dim]/hs,prod_wk[i]);
  830. if (st==LF_BADP) return(st);
  831. hj = 1;
  832. for (j=0; j<=2*deg(den_sp); j++)
  833. { hj *= hs;
  834. prod_wk[i][j] *= hj;
  835. }
  836. cf[0] = 0.0; /* so we only include it once, when d>=2 */
  837. }
  838. /* transfer to the resp array
  839. */
  840. prodintresp(resp,prod_wk,dim,deg(den_sp),p);
  841. /* Symmetrize.
  842. */
  843. for (k=0; k<p; k++)
  844. for (j=k; j<p; j++)
  845. resp[j*p+k] = resp[k*p+j];
  846. return(st);
  847. }
  848. int gausint(t,resp,C,cf,h,sca)
  849. double *t, *resp, *C, *cf, h, *sca;
  850. { double nb, det, z, *P;
  851. int d, p, i, j, k, l, m1, m2, f;
  852. d = den_lfd->d; p = den_des->p;
  853. m1 = d+1; nb = 0;
  854. P = &C[d*d];
  855. resp[0] = 1;
  856. for (i=0; i<d; i++)
  857. { C[i*d+i] = SQR(GFACT/(h*sca[i]))-cf[m1++];
  858. for (j=i+1; j<d; j++) C[i*d+j] = C[j*d+i] = -cf[m1++];
  859. }
  860. eig_dec(C,P,d);
  861. det = 1;
  862. for (i=1; i<=d; i++)
  863. { det *= C[(i-1)*(d+1)];
  864. if (det <= 0) return(LF_BADP);
  865. resp[i] = cf[i];
  866. for (j=1; j<=d; j++) resp[j+i*p] = 0;
  867. resp[i+i*p] = 1;
  868. svdsolve(&resp[i*p+1],u,P,C,P,d,0.0);
  869. }
  870. svdsolve(&resp[1],u,P,C,P,d,0.0);
  871. det = sqrt(det);
  872. for (i=1; i<=d; i++)
  873. { nb += cf[i]*resp[i];
  874. resp[i*p] = resp[i];
  875. for (j=1; j<=d; j++)
  876. resp[i+p*j] += resp[i]*resp[j];
  877. }
  878. m1 = d;
  879. for (i=1; i<=d; i++)
  880. for (j=i; j<=d; j++)
  881. { m1++; f = 1+(i==j);
  882. resp[m1] = resp[m1*p] = resp[i*p+j]/f;
  883. m2 = d;
  884. for (k=1; k<=d; k++)
  885. { resp[m1+k*p] = resp[k+m1*p] =
  886. ( resp[i]*resp[j*p+k] + resp[j]*resp[i*p+k]
  887. + resp[k]*resp[i*p+j] - 2*resp[i]*resp[j]*resp[k] )/f;
  888. for (l=k; l<=d; l++)
  889. { m2++; f = (1+(i==j))*(1+(k==l));
  890. resp[m1+m2*p] = resp[m2+m1*p] = ( resp[i+j*p]*resp[k+l*p]
  891. + resp[i+k*p]*resp[j+l*p] + resp[i+l*p]*resp[j+k*p]
  892. - 2*resp[i]*resp[j]*resp[k]*resp[l] )/f;
  893. } } }
  894. z = mut_exp(d*0.918938533+cf[0]+nb/2)/det;
  895. multmatscal(resp,z,p*p);
  896. return(LF_OK);
  897. }
  898. int likeden(coef, lk0, f1, A)
  899. double *coef, *lk0, *f1, *A;
  900. { double lk, r;
  901. int i, j, p, rstat;
  902. lf_status = LF_OK;
  903. p = den_des->p;
  904. if ((link(den_sp)==LIDENT) && (coef[0] != 0.0)) return(NR_BREAK);
  905. lf_status = (den_des->itype)(den_des->xev,A,den_des->xtwx.Q,coef,den_des->h);
  906. if (lf_error) lf_status = LF_ERR;
  907. if (lf_status==LF_BADP)
  908. { *lk0 = -1.0e300;
  909. return(NR_REDUCE);
  910. }
  911. if (lf_status!=LF_OK) return(NR_BREAK);
  912. if (lf_debug>2) prresp(coef,A,p);
  913. den_des->xtwx.p = p;
  914. rstat = NR_OK;
  915. switch(link(den_sp))
  916. { case LLOG:
  917. r = den_des->ss[0]/A[0];
  918. coef[0] += log(r);
  919. multmatscal(A,r,p*p);
  920. A[0] = den_des->ss[0];
  921. lk = -A[0];
  922. if (fabs(coef[0]) > 700)
  923. { lf_status = LF_OOB;
  924. rstat = NR_REDUCE;
  925. }
  926. for (i=0; i<p; i++)
  927. { lk += coef[i]*den_des->ss[i];
  928. f1[i] = den_des->ss[i]-A[i];
  929. }
  930. break;
  931. case LIDENT:
  932. lk = 0.0;
  933. for (i=0; i<p; i++)
  934. { f1[i] = den_des->ss[i];
  935. for (j=0; j<p; j++)
  936. den_des->res[i] -= A[i*p+j]*coef[j];
  937. }
  938. break;
  939. }
  940. *lk0 = den_des->llk = lk;
  941. return(rstat);
  942. }
  943. int inre(x,bound,d)
  944. double *x, *bound;
  945. int d;
  946. { int i, z;
  947. z = 1;
  948. for (i=0; i<d; i++)
  949. if (bound[i]<bound[i+d])
  950. z &= (x[i]>=bound[i]) & (x[i]<=bound[i+d]);
  951. return(z);
  952. }
  953. int setintlimits(lfd, x, h, ang, lset)
  954. lfdata *lfd;
  955. int *ang, *lset;
  956. double *x, h;
  957. { int d, i;
  958. d = lfd->d;
  959. *ang = *lset = 0;
  960. for (i=0; i<d; i++)
  961. { if (lfd->sty[i]==STANGL)
  962. { ilim[i+d] = ((h<2) ? 2*asin(h/2) : PI)*lfd->sca[i];
  963. ilim[i] = -ilim[i+d];
  964. *ang = 1;
  965. }
  966. else
  967. { ilim[i+d] = h*lfd->sca[i];
  968. ilim[i] = -ilim[i+d];
  969. if (lfd->sty[i]==STLEFT) { ilim[i+d] = 0; *lset = 1; }
  970. if (lfd->sty[i]==STRIGH) { ilim[i] = 0; *lset = 1; }
  971. if (lfd->xl[i]<lfd->xl[i+d]) /* user limits for this variable */
  972. { if (lfd->xl[i]-x[i]> ilim[i])
  973. { ilim[i] = lfd->xl[i]-x[i]; *lset=1; }
  974. if (lfd->xl[i+d]-x[i]< ilim[i+d])
  975. { ilim[i+d] = lfd->xl[i+d]-x[i]; *lset=1; }
  976. }
  977. }
  978. if (ilim[i]==ilim[i+d]) return(LF_DEMP); /* empty integration */
  979. }
  980. return(LF_OK);
  981. }
  982. int selectintmeth(itype,lset,ang)
  983. int itype, lset, ang;
  984. {
  985. if (itype==IDEFA) /* select the default method */
  986. { if (fam(den_sp)==THAZ)
  987. { if (ang) return(IDEFA);
  988. return( IHAZD );
  989. }
  990. if (ubas(den_sp)) return(IMULT);
  991. if (ang) return(IMULT);
  992. if (iscompact(ker(den_sp)))
  993. { if (kt(den_sp)==KPROD) return(IPROD);
  994. if (lset)
  995. return( (den_lfd->d==1) ? IPROD : IMULT );
  996. if (deg(den_sp)<=1) return(IMLIN);
  997. if (den_lfd->d==1) return(IPROD);
  998. return(IMULT);
  999. }
  1000. if (ker(den_sp)==WGAUS)
  1001. { if (lset) WARN(("Integration for Gaussian weights ignores limits"));
  1002. if ((den_lfd->d==1)|(kt(den_sp)==KPROD)) return(IPROD);
  1003. if (deg(den_sp)<=1) return(IMLIN);
  1004. if (deg(den_sp)==2) return(IMULT);
  1005. }
  1006. return(IDEFA);
  1007. }
  1008. /* user provided an integration method, check it is valid */
  1009. if (fam(den_sp)==THAZ)
  1010. { if (ang) return(INVLD);
  1011. if (!iscompact(ker(den_sp))) return(INVLD);
  1012. return( ((kt(den_sp)==KPROD) | (kt(den_sp)==KSPH)) ? IHAZD : INVLD );
  1013. }
  1014. if ((ang) && (itype != IMULT)) return(INVLD);
  1015. switch(itype)
  1016. { case IMULT:
  1017. if (ker(den_sp)==WGAUS) return(deg(den_sp)==2);
  1018. return( iscompact(ker(den_sp)) ? IMULT : INVLD );
  1019. case IPROD: return( ((den_lfd->d==1) | (kt(den_sp)==KPROD)) ? IPROD : INVLD );
  1020. case IMLIN: return( ((kt(den_sp)==KSPH) && (!lset) &&
  1021. (deg(den_sp)<=1)) ? IMLIN : INVLD );
  1022. }
  1023. return(INVLD);
  1024. }
  1025. extern double lf_tol;
  1026. int densinit(lfd,des,sp)
  1027. lfdata *lfd;
  1028. design *des;
  1029. smpar *sp;
  1030. { int p, i, ii, j, nnz, rnz, ang, lset, status;
  1031. double w, *cf;
  1032. den_lfd = lfd;
  1033. den_des = des;
  1034. den_sp = sp;
  1035. cf = des->cf;
  1036. lf_tol = (link(sp)==LLOG) ? 1.0e-6 : 0.0;
  1037. p = des->p;
  1038. ff = des->xtwx.wk;
  1039. cf[0] = NOSLN;
  1040. for (i=1; i<p; i++) cf[i] = 0.0;
  1041. if (!inre(des->xev,lfd->xl,lfd->d)) return(LF_XOOR);
  1042. status = setintlimits(lfd,des->xev,des->h,&ang,&lset);
  1043. if (status != LF_OK) return(status);
  1044. switch(selectintmeth(de_itype,lset,ang))
  1045. { case IMULT: des->itype = multint; break;
  1046. case IPROD: des->itype = prodint; break;
  1047. case IMLIN: des->itype = mlinint; break;
  1048. case IHAZD: des->itype = hazint; break;
  1049. case INVLD: LERR(("Invalid integration method %d",de_itype));
  1050. break;
  1051. case IDEFA: LERR(("No integration type available for this model"));
  1052. break;
  1053. default: LERR(("densinit: unknown integral type"));
  1054. }
  1055. switch(deg(den_sp))
  1056. { case 0: rnz = 1; break;
  1057. case 1: rnz = 1; break;
  1058. case 2: rnz = lfd->d+1; break;
  1059. case 3: rnz = lfd->d+2; break;
  1060. default: LERR(("densinit: invalid degree %d",deg(den_sp)));
  1061. }
  1062. if (lf_error) return(LF_ERR);
  1063. setzero(des->ss,p);
  1064. nnz = 0;
  1065. for (i=0; i<des->n; i++)
  1066. { ii = des->ind[i];
  1067. if (!cens(lfd,ii))
  1068. { w = wght(des,ii)*prwt(lfd,ii);
  1069. for (j=0; j<p; j++) des->ss[j] += d_xij(des,ii,j)*w;
  1070. if (wght(des,ii)>0.00001) nnz++;
  1071. } }
  1072. if (fam(den_sp)==THAZ) haz_init(lfd,des,sp,ilim);
  1073. /* this should really only be done once. Not sure how to enforce that,
  1074. * esp. when locfit() has been called directly.
  1075. */
  1076. if (fam(den_sp)==TDEN)
  1077. des->smwt = (lfd->w==NULL) ? lfd->n : vecsum(lfd->w,lfd->n);
  1078. if (lf_debug>2)
  1079. { mut_printf(" LHS: ");
  1080. for (i=0; i<p; i++) mut_printf(" %8.5f",des->ss[i]);
  1081. mut_printf("\n");
  1082. }
  1083. switch(link(den_sp))
  1084. { case LIDENT:
  1085. cf[0] = 0.0;
  1086. return(LF_OK);
  1087. case LLOG:
  1088. if (nnz<rnz) { cf[0] = -1000; return(LF_DNOP); }
  1089. cf[0] = 0.0;
  1090. return(LF_OK);
  1091. default:
  1092. LERR(("unknown link in densinit"));
  1093. return(LF_ERR);
  1094. }
  1095. }
  1096. /*
  1097. * Copyright 1996-2006 Catherine Loader.
  1098. */
  1099. #include "locf.h"
  1100. int bino_vallink(link)
  1101. int link;
  1102. { return((link==LLOGIT) | (link==LIDENT) | (link==LASIN));
  1103. }
  1104. int bino_fam(y,p,th,link,res,cens,w)
  1105. double y, p, th, *res, w;
  1106. int link, cens;
  1107. { double wp;
  1108. if (link==LINIT)
  1109. { if (y<0) y = 0;
  1110. if (y>w) y = w;
  1111. res[ZDLL] = y;
  1112. return(LF_OK);
  1113. }
  1114. wp = w*p;
  1115. if (link==LIDENT)
  1116. { if ((p<=0) && (y>0)) return(LF_BADP);
  1117. if ((p>=1) && (y<w)) return(LF_BADP);
  1118. res[ZLIK] = res[ZDLL] = res[ZDDLL] = 0.0;
  1119. if (y>0)
  1120. { res[ZLIK] += y*log(wp/y);
  1121. res[ZDLL] += y/p;
  1122. res[ZDDLL]+= y/(p*p);
  1123. }
  1124. if (y<w)
  1125. { res[ZLIK] += (w-y)*log((w-wp)/(w-y));
  1126. res[ZDLL] -= (w-y)/(1-p);
  1127. res[ZDDLL]+= (w-y)/SQR(1-p);
  1128. }
  1129. return(LF_OK);
  1130. }
  1131. if (link==LLOGIT)
  1132. { if ((y<0) | (y>w)) /* goon observation; delete it */
  1133. { res[ZLIK] = res[ZDLL] = res[ZDDLL] = 0.0;
  1134. return(LF_OK);
  1135. }
  1136. res[ZLIK] = (th<0) ? th*y-w*log(1+exp(th)) : th*(y-w)-w*log(1+exp(-th));
  1137. if (y>0) res[ZLIK] -= y*log(y/w);
  1138. if (y<w) res[ZLIK] -= (w-y)*log(1-y/w);
  1139. res[ZDLL] = (y-wp);
  1140. res[ZDDLL]= wp*(1-p);
  1141. return(LF_OK);
  1142. }
  1143. if (link==LASIN)
  1144. { if ((p<=0) && (y>0)) return(LF_BADP);
  1145. if ((p>=1) && (y<w)) return(LF_BADP);
  1146. if ((th<0) | (th>PI/2)) return(LF_BADP);
  1147. res[ZDLL] = res[ZDDLL] = res[ZLIK] = 0;
  1148. if (y>0)
  1149. { res[ZDLL] += 2*y*sqrt((1-p)/p);
  1150. res[ZLIK] += y*log(wp/y);
  1151. }
  1152. if (y<w)
  1153. { res[ZDLL] -= 2*(w-y)*sqrt(p/(1-p));
  1154. res[ZLIK] += (w-y)*log((w-wp)/(w-y));
  1155. }
  1156. res[ZDDLL] = 4*w;
  1157. return(LF_OK);
  1158. }
  1159. LERR(("link %d invalid for binomial family",link));
  1160. return(LF_LNK);
  1161. }
  1162. int bino_check(sp,des,lfd)
  1163. smpar *sp;
  1164. design *des;
  1165. lfdata *lfd;
  1166. { int i, ii;
  1167. double t0, t1;
  1168. if (fabs(des->cf[0])>700) return(LF_OOB);
  1169. /* check for separation.
  1170. * this won't detect separation if there's boundary points with
  1171. * both 0 and 1 responses.
  1172. */
  1173. t0 = -1e100; t1 = 1e100;
  1174. for (i=0; i<des->n; i++)
  1175. { ii = des->ind[i];
  1176. if ((resp(lfd,ii)<prwt(lfd,ii)) && (fitv(des,ii) > t0)) t0 = fitv(des,ii);
  1177. if ((resp(lfd,ii)>0) && (fitv(des,ii) < t1)) t1 = fitv(des,ii);
  1178. if (t1 <= t0) return(LF_OK);
  1179. }
  1180. mut_printf("separated %8.5f %8.5f\n",t0,t1);
  1181. return(LF_NSLN);
  1182. }
  1183. void setfbino(fam)
  1184. family *fam;
  1185. { fam->deflink = LLOGIT;
  1186. fam->canlink = LLOGIT;
  1187. fam->vallink = bino_vallink;
  1188. fam->family = bino_fam;
  1189. fam->pcheck = bino_check;
  1190. }
  1191. int rbin_vallink(link)
  1192. int link;
  1193. { return(link==LLOGIT);
  1194. }
  1195. int rbin_fam(y,p,th,link,res,cens,w)
  1196. double y, p, th, *res, w;
  1197. int link, cens;
  1198. { double s2y;
  1199. if (link==LINIT)
  1200. { res[ZDLL] = y;
  1201. return(LF_OK);
  1202. }
  1203. if ((y<0) | (y>w)) /* goon observation; delete it */
  1204. { res[ZLIK] = res[ZDLL] = res[ZDDLL] = 0.0;
  1205. return(LF_OK);
  1206. }
  1207. res[ZLIK] = (th<0) ? th*y-w*log(1+exp(th)) : th*(y-w)-w*log(1+exp(-th));
  1208. if (y>0) res[ZLIK] -= y*log(y/w);
  1209. if (y<w) res[ZLIK] -= (w-y)*log(1-y/w);
  1210. res[ZDLL] = (y-w*p);
  1211. res[ZDDLL]= w*p*(1-p);
  1212. if (-res[ZLIK]>HUBERC*HUBERC/2.0)
  1213. { s2y = sqrt(-2*res[ZLIK]);
  1214. res[ZLIK] = HUBERC*(HUBERC/2.0-s2y);
  1215. res[ZDLL] *= HUBERC/s2y;
  1216. res[ZDDLL] = HUBERC/s2y*(res[ZDDLL]-1/(s2y*s2y)*w*p*(1-p));
  1217. }
  1218. return(LF_OK);
  1219. }
  1220. void setfrbino(fam)
  1221. family *fam;
  1222. { fam->deflink = LLOGIT;
  1223. fam->canlink = LLOGIT;
  1224. fam->vallink = rbin_vallink;
  1225. fam->family = rbin_fam;
  1226. fam->pcheck = bino_check;
  1227. }
  1228. /*
  1229. * Copyright 1996-2006 Catherine Loader.
  1230. */
  1231. #include "locf.h"
  1232. int circ_vallink(link)
  1233. int link;
  1234. { return(link==LIDENT);
  1235. }
  1236. int circ_fam(y,mean,th,link,res,cens,w)
  1237. double y, mean, th, *res, w;
  1238. int link, cens;
  1239. { if (link==LINIT)
  1240. { res[ZDLL] = w*sin(y);
  1241. res[ZLIK] = w*cos(y);
  1242. return(LF_OK);
  1243. }
  1244. res[ZDLL] = w*sin(y-mean);
  1245. res[ZDDLL]= w*cos(y-mean);
  1246. res[ZLIK] = res[ZDDLL]-w;
  1247. return(LF_OK);
  1248. }
  1249. extern double lf_tol;
  1250. int circ_init(lfd,des,sp)
  1251. lfdata *lfd;
  1252. design *des;
  1253. smpar *sp;
  1254. { int i, ii;
  1255. double s0, s1;
  1256. s0 = s1 = 0.0;
  1257. for (i=0; i<des->n; i++)
  1258. { ii = des->ind[i];
  1259. s0 += wght(des,ii)*prwt(lfd,ii)*sin(resp(lfd,ii)-base(lfd,ii));
  1260. s1 += wght(des,ii)*prwt(lfd,ii)*cos(resp(lfd,ii)-base(lfd,ii));
  1261. }
  1262. des->cf[0] = atan2(s0,s1);
  1263. for (i=1; i<des->p; i++) des->cf[i] = 0.0;
  1264. lf_tol = 1.0e-6;
  1265. return(LF_OK);
  1266. }
  1267. void setfcirc(fam)
  1268. family *fam;
  1269. { fam->deflink = LIDENT;
  1270. fam->canlink = LIDENT;
  1271. fam->vallink = circ_vallink;
  1272. fam->family = circ_fam;
  1273. fam->initial = circ_init;
  1274. }
  1275. /*
  1276. * Copyright 1996-2006 Catherine Loader.
  1277. */
  1278. #include "locf.h"
  1279. int dens_vallink(link)
  1280. int link;
  1281. { return((link==LIDENT) | (link==LLOG));
  1282. }
  1283. int dens_fam(y,mean,th,link,res,cens,w)
  1284. double y, mean, th, *res, w;
  1285. int link, cens;
  1286. { if (cens)
  1287. res[ZLIK] = res[ZDLL] = res[ZDDLL] = 0.0;
  1288. else
  1289. { res[ZLIK] = w*th;
  1290. res[ZDLL] = res[ZDDLL] = w;
  1291. }
  1292. return(LF_OK);
  1293. }
  1294. void setfdensity(fam)
  1295. family *fam;
  1296. { fam->deflink = LLOG;
  1297. fam->canlink = LLOG;
  1298. fam->vallink = dens_vallink;
  1299. fam->family = dens_fam;
  1300. fam->initial = densinit;
  1301. fam->like = likeden;
  1302. }
  1303. /*
  1304. * Copyright 1996-2006 Catherine Loader.
  1305. */
  1306. #include "locf.h"
  1307. int gamma_vallink(link)
  1308. int link;
  1309. { return((link==LIDENT) | (link==LLOG) | (link==LINVER));
  1310. }
  1311. int gamma_fam(y,mean,th,link,res,cens,w)
  1312. double y, mean, th, *res, w;
  1313. int link, cens;
  1314. { double lb, pt, dg;
  1315. if (link==LINIT)
  1316. { res[ZDLL] = MAX(y,0.0);
  1317. return(LF_OK);
  1318. }
  1319. res[ZLIK] = res[ZDLL] = res[ZDDLL] = 0.0;
  1320. if (w==0.0) return(LF_OK);
  1321. if ((mean<=0) & (y>0)) return(LF_BADP);
  1322. if (link==LIDENT) lb = 1/th;
  1323. if (link==LINVER) lb = th;
  1324. if (link==LLOG) lb = mut_exp(-th);
  1325. if (cens)
  1326. { if (y<=0) return(LF_OK);
  1327. pt = 1-igamma(lb*y,w);
  1328. dg = dgamma(lb*y,w,1.0,0);
  1329. res[ZLIK] = log(pt);
  1330. res[ZDLL] = -y*dg/pt;
  1331. /*
  1332. * res[ZDLL] = -y*dg/pt * dlb/dth.
  1333. * res[ZDDLL] = y*dg/pt * (d2lb/dth2 + ((w-1)/lb-y)*(dlb/dth)^2)
  1334. * + res[ZDLL]^2.
  1335. */
  1336. if (link==LLOG) /* lambda = exp(-theta) */
  1337. { res[ZDLL] *= -lb;
  1338. res[ZDDLL] = dg*y*lb*(w-lb*y)/pt + SQR(res[ZDLL]);
  1339. return(LF_OK);
  1340. }
  1341. if (link==LINVER) /* lambda = theta */
  1342. { res[ZDLL] *= 1.0;
  1343. res[ZDDLL] = dg*y*((w-1)*mean-y)/pt + SQR(res[ZDLL]);
  1344. return(LF_OK);
  1345. }
  1346. if (link==LIDENT) /* lambda = 1/theta */
  1347. { res[ZDLL] *= -lb*lb;
  1348. res[ZDDLL] = dg*y*lb*lb*lb*(1+w-lb*y)/pt + SQR(res[ZDLL]);
  1349. return(LF_OK);
  1350. }
  1351. }
  1352. else
  1353. { if (y<0) WARN(("Negative Gamma observation"));
  1354. if (link==LLOG)
  1355. { res[ZLIK] = -lb*y+w*(1-th);
  1356. if (y>0) res[ZLIK] += w*log(y/w);
  1357. res[ZDLL] = lb*y-w;
  1358. res[ZDDLL]= lb*y;
  1359. return(LF_OK);
  1360. }
  1361. if (link==LINVER)
  1362. { res[ZLIK] = -lb*y+w-w*log(mean);
  1363. if (y>0) res[ZLIK] += w*log(y/w);
  1364. res[ZDLL] = -y+w*mean;
  1365. res[ZDDLL]= w*mean*mean;
  1366. return(LF_OK);
  1367. }
  1368. if (link==LIDENT)
  1369. { res[ZLIK] = -lb*y+w-w*log(mean);
  1370. if (y>0) res[ZLIK] += w*log(y/w);
  1371. res[ZDLL] = lb*lb*(y-w*mean);
  1372. res[ZDDLL]= lb*lb*lb*(2*y-w*mean);
  1373. return(LF_OK);
  1374. }
  1375. }
  1376. LERR(("link %d invalid for Gamma family",link));
  1377. return(LF_LNK);
  1378. }
  1379. void setfgamma(fam)
  1380. family *fam;
  1381. { fam->deflink = LLOG;
  1382. fam->canlink = LINVER;
  1383. fam->vallink = gamma_vallink;
  1384. fam->family = gamma_fam;
  1385. }
  1386. /*
  1387. * Copyright 1996-2006 Catherine Loader.
  1388. */
  1389. #include "locf.h"
  1390. int gaus_vallink(link)
  1391. int link;
  1392. { return((link==LIDENT) | (link==LLOG) | (link==LLOGIT));
  1393. }
  1394. int gaus_fam(y,mean,th,link,res,cens,w)
  1395. double y, mean, th, *res, w;
  1396. int link, cens;
  1397. { double z, pz, dp;
  1398. if (link==LINIT)
  1399. { res[ZDLL] = w*y;
  1400. return(LF_OK);
  1401. }
  1402. z = y-mean;
  1403. if (cens)
  1404. { if (link!=LIDENT)
  1405. { LERR(("Link invalid for censored Gaussian family"));
  1406. return(LF_LNK);
  1407. }
  1408. pz = mut_pnorm(-z);
  1409. dp = ((z>6) ? ptail(-z) : exp(-z*z/2)/pz)/2.5066283;
  1410. res[ZLIK] = w*log(pz);
  1411. res[ZDLL] = w*dp;
  1412. res[ZDDLL]= w*dp*(dp-z);
  1413. return(LF_OK);
  1414. }
  1415. res[ZLIK] = -w*z*z/2;
  1416. switch(link)
  1417. { case LIDENT:
  1418. res[ZDLL] = w*z;
  1419. res[ZDDLL]= w;
  1420. break;
  1421. case LLOG:
  1422. res[ZDLL] = w*z*mean;
  1423. res[ZDDLL]= w*mean*mean;
  1424. break;
  1425. case LLOGIT:
  1426. res[ZDLL] = w*z*mean*(1-mean);
  1427. res[ZDDLL]= w*mean*mean*(1-mean)*(1-mean);
  1428. break;
  1429. default:
  1430. LERR(("Invalid link for Gaussian family"));
  1431. return(LF_LNK);
  1432. }
  1433. return(LF_OK);
  1434. }
  1435. int gaus_check(sp,des,lfd)
  1436. smpar *sp;
  1437. design *des;
  1438. lfdata *lfd;
  1439. { int i, ii;
  1440. if (fami(sp)->robust) return(LF_OK);
  1441. if (link(sp)==LIDENT)
  1442. { for (i=0; i<des->n; i++)
  1443. { ii = des->ind[i];
  1444. if (cens(lfd,ii)) return(LF_OK);
  1445. }
  1446. return(LF_DONE);
  1447. }
  1448. return(LF_OK);
  1449. }
  1450. void setfgauss(fam)
  1451. family *fam;
  1452. { fam->deflink = LIDENT;
  1453. fam->canlink = LIDENT;
  1454. fam->vallink = gaus_vallink;
  1455. fam->family = gaus_fam;
  1456. fam->pcheck = gaus_check;
  1457. }
  1458. /*
  1459. * Copyright 1996-2006 Catherine Loader.
  1460. */
  1461. #include "locf.h"
  1462. int geom_vallink(link)
  1463. int link;
  1464. { return((link==LIDENT) | (link==LLOG));
  1465. }
  1466. int geom_fam(y,mean,th,link,res,cens,w)
  1467. double y, mean, th, *res, w;
  1468. int link, cens;
  1469. { double p, pt, dp, p1;
  1470. if (link==LINIT)
  1471. { res[ZDLL] = MAX(y,0.0);
  1472. return(LF_OK);
  1473. }
  1474. p = 1/(1+mean);
  1475. if (cens) /* censored observation */
  1476. { if (y<=0)
  1477. { res[ZLIK] = res[ZDLL] = res[ZDDLL] = 0;
  1478. return(LF_OK);
  1479. }
  1480. p1 = (link==LIDENT) ? -p*p : -p*(1-p);
  1481. pt = 1-ibeta(p,w,y);
  1482. dp = dbeta(p,w,y,0)/pt;
  1483. res[ZLIK] = log(pt);
  1484. res[ZDLL] = -dp*p1;
  1485. res[ZDDLL] = dp*dp*p1*p1;
  1486. if (link==LIDENT)
  1487. res[ZDDLL] += dp*p*p*p*(1+w*(1-p)-p*y)/(1-p);
  1488. else
  1489. res[ZDDLL] += dp*p*(1-p)*(w*(1-p)-p*y);
  1490. return(LF_OK);
  1491. }
  1492. else
  1493. { res[ZLIK] = (y+w)*log((y/w+1)/(mean+1));
  1494. if (y>0) res[ZLIK] += y*log(w*mean/y);
  1495. if (link==LLOG)
  1496. { res[ZDLL] = (y-w*mean)*p;
  1497. res[ZDDLL]= (y+w)*p*(1-p);
  1498. return(LF_OK);
  1499. }
  1500. if (link==LIDENT)
  1501. { res[ZDLL] = (y-w*mean)/(mean*(1+mean));
  1502. res[ZDDLL]= w/(mean*(1+mean));
  1503. return(LF_OK);
  1504. }
  1505. }
  1506. LERR(("link %d invalid for geometric family",link));
  1507. return(LF_LNK);
  1508. }
  1509. void setfgeom(fam)
  1510. family *fam;
  1511. { fam->deflink = LLOG;
  1512. fam->canlink = LIDENT; /* this isn't correct. I haven't prog. canon */
  1513. fam->vallink = geom_vallink;
  1514. fam->family = geom_fam;
  1515. }
  1516. /*
  1517. * Copyright 1996-2006 Catherine Loader.
  1518. */
  1519. #include "locf.h"
  1520. #define HUBERC 2.0
  1521. double links_rs;
  1522. int inllmix=0;
  1523. /*
  1524. * lffamily("name") converts family names into a numeric value.
  1525. * typical usage is fam(&lf->sp) = lffamily("gaussian");
  1526. * Note that family can be preceded by q and/or r for quasi, robust.
  1527. *
  1528. * link(&lf->sp) = lflink("log") does the same for the link function.
  1529. */
  1530. #define NFAMILY 18
  1531. static char *famil[NFAMILY] =
  1532. { "density", "ate", "hazard", "gaussian", "binomial",
  1533. "poisson", "gamma", "geometric", "circular", "obust", "huber",
  1534. "weibull", "cauchy","probab", "logistic", "nbinomial",
  1535. "vonmises", "quant" };
  1536. static int fvals[NFAMILY] =
  1537. { TDEN, TRAT, THAZ, TGAUS, TLOGT,
  1538. TPOIS, TGAMM, TGEOM, TCIRC, TROBT, TROBT,
  1539. TWEIB, TCAUC, TPROB, TLOGT, TGEOM, TCIRC, TQUANT };
  1540. int lffamily(z)
  1541. char *z;
  1542. { int quasi, robu, f;
  1543. quasi = robu = 0;
  1544. while ((z[0]=='q') | (z[0]=='r'))
  1545. { quasi |= (z[0]=='q');
  1546. robu |= (z[0]=='r');
  1547. z++;
  1548. }
  1549. z[0] = tolower(z[0]);
  1550. f = pmatch(z,famil,fvals,NFAMILY,-1);
  1551. if ((z[0]=='o') | (z[0]=='a')) robu = 0;
  1552. if (f==-1)
  1553. { WARN(("unknown family %s",z));
  1554. f = TGAUS;
  1555. }
  1556. if (quasi) f += 64;
  1557. if (robu) f += 128;
  1558. return(f);
  1559. }
  1560. #define NLINKS 8
  1561. static char *ltype[NLINKS] = { "default", "canonical", "identity", "log",
  1562. "logi", "inverse", "sqrt", "arcsin" };
  1563. static int lvals[NLINKS] = { LDEFAU, LCANON, LIDENT, LLOG,
  1564. LLOGIT, LINVER, LSQRT, LASIN };
  1565. int lflink(char *z)
  1566. { int f;
  1567. if (z==NULL) return(LDEFAU);
  1568. z[0] = tolower(z[0]);
  1569. f = pmatch(z, ltype, lvals, NLINKS, -1);
  1570. if (f==-1)
  1571. { WARN(("unknown link %s",z));
  1572. f = LDEFAU;
  1573. }
  1574. return(f);
  1575. }
  1576. int defaultlink(link,fam)
  1577. int link;
  1578. family *fam;
  1579. { if (link==LDEFAU) return(fam->deflink);
  1580. if (link==LCANON) return(fam->canlink);
  1581. return(link);
  1582. }
  1583. /*
  1584. void robustify(res,rs)
  1585. double *res, rs;
  1586. { double sc, z;
  1587. sc = rs*HUBERC;
  1588. if (res[ZLIK] > -sc*sc/2) return;
  1589. z = sqrt(-2*res[ZLIK]);
  1590. res[ZDDLL]= -sc*res[ZDLL]*res[ZDLL]/(z*z*z)+sc*res[ZDDLL]/z;
  1591. res[ZDLL]*= sc/z;
  1592. res[ZLIK] = sc*sc/2-sc*z;
  1593. }
  1594. */
  1595. void robustify(res,rs)
  1596. double *res, rs;
  1597. { double sc, z;
  1598. sc = rs*HUBERC;
  1599. if (res[ZLIK] > -sc*sc/2)
  1600. { res[ZLIK] /= sc*sc;
  1601. res[ZDLL] /= sc*sc;
  1602. res[ZDDLL] /= sc*sc;
  1603. return;
  1604. }
  1605. z = sqrt(-2*res[ZLIK]);
  1606. res[ZDDLL]= (-sc*res[ZDLL]*res[ZDLL]/(z*z*z)+sc*res[ZDDLL]/z)/(sc*sc);
  1607. res[ZDLL]*= 1.0/(z*sc);
  1608. res[ZLIK] = 0.5-z/sc;
  1609. }
  1610. double lf_link(y,lin)
  1611. double y;
  1612. int lin;
  1613. { switch(lin)
  1614. { case LIDENT: return(y);
  1615. case LLOG: return(log(y));
  1616. case LLOGIT: return(logit(y));
  1617. case LINVER: return(1/y);
  1618. case LSQRT: return(sqrt(fabs(y)));
  1619. case LASIN: return(asin(sqrt(y)));
  1620. }
  1621. LERR(("link: unknown link %d",lin));
  1622. return(0.0);
  1623. }
  1624. double invlink(th,lin)
  1625. double th;
  1626. int lin;
  1627. { switch(lin)
  1628. { case LIDENT: return(th);
  1629. case LLOG: return(mut_exp(th));
  1630. case LLOGIT: return(expit(th));
  1631. case LINVER: return(1/th);
  1632. case LSQRT: return(th*fabs(th));
  1633. case LASIN: return(sin(th)*sin(th));
  1634. case LINIT: return(0.0);
  1635. }
  1636. LERR(("invlink: unknown link %d",lin));
  1637. return(0.0);
  1638. }
  1639. /* the link and various related functions */
  1640. int links(th,y,fam,link,res,c,w,rs)
  1641. double th, y, *res, w, rs;
  1642. int link, c;
  1643. family *fam;
  1644. { double mean;
  1645. int st;
  1646. mean = res[ZMEAN] = invlink(th,link);
  1647. if (lf_error) return(LF_LNK);
  1648. links_rs = rs;
  1649. /* mut_printf("links: rs %8.5f\n",rs); */
  1650. st = fam->family(y,mean,th,link,res,c,w);
  1651. if (st!=LF_OK) return(st);
  1652. if (link==LINIT) return(st);
  1653. if (isrobust(fam)) robustify(res,rs);
  1654. return(st);
  1655. }
  1656. /*
  1657. stdlinks is a version of links when family, link, response e.t.c
  1658. all come from the standard places.
  1659. */
  1660. int stdlinks(res,lfd,sp,i,th,rs)
  1661. lfdata *lfd;
  1662. smpar *sp;
  1663. double th, rs, *res;
  1664. int i;
  1665. {
  1666. return(links(th,resp(lfd,i),fami(sp),link(sp),res,cens(lfd,i),prwt(lfd,i),rs));
  1667. }
  1668. /*
  1669. * functions used in variance, skewness, kurtosis calculations
  1670. * in scb corrections.
  1671. */
  1672. double b2(th,tg,w)
  1673. double th, w;
  1674. int tg;
  1675. { double y;
  1676. switch(tg&63)
  1677. { case TGAUS: return(w);
  1678. case TPOIS: return(w*mut_exp(th));
  1679. case TLOGT:
  1680. y = expit(th);
  1681. return(w*y*(1-y));
  1682. }
  1683. LERR(("b2: invalid family %d",tg));
  1684. return(0.0);
  1685. }
  1686. double b3(th,tg,w)
  1687. double th, w;
  1688. int tg;
  1689. { double y;
  1690. switch(tg&63)
  1691. { case TGAUS: return(0.0);
  1692. case TPOIS: return(w*mut_exp(th));
  1693. case TLOGT:
  1694. y = expit(th);
  1695. return(w*y*(1-y)*(1-2*y));
  1696. }
  1697. LERR(("b3: invalid family %d",tg));
  1698. return(0.0);
  1699. }
  1700. double b4(th,tg,w)
  1701. double th, w;
  1702. int tg;
  1703. { double y;
  1704. switch(tg&63)
  1705. { case TGAUS: return(0.0);
  1706. case TPOIS: return(w*mut_exp(th));
  1707. case TLOGT:
  1708. y = expit(th); y = y*(1-y);
  1709. return(w*y*(1-6*y));
  1710. }
  1711. LERR(("b4: invalid family %d",tg));
  1712. return(0.0);
  1713. }
  1714. int def_check(sp,des,lfd)
  1715. smpar *sp;
  1716. design *des;
  1717. lfdata *lfd;
  1718. { switch(link(sp))
  1719. { case LLOG: if (des->cf[0]>700) return(LF_OOB);
  1720. break;
  1721. }
  1722. return(LF_OK);
  1723. }
  1724. extern void setfdensity(), setfgauss(), setfbino(), setfpoisson();
  1725. extern void setfgamma(), setfgeom(), setfcirc(), setfweibull();
  1726. extern void setfrbino(), setfrobust(), setfcauchy(), setfquant();
  1727. void setfamily(sp)
  1728. smpar *sp;
  1729. { int tg, lnk;
  1730. family *f;
  1731. tg = fam(sp);
  1732. f = fami(sp);
  1733. f->quasi = tg&64;
  1734. f->robust = tg&128;
  1735. f->initial = reginit;
  1736. f->like = likereg;
  1737. f->pcheck = def_check;
  1738. switch(tg&63)
  1739. { case TDEN:
  1740. case THAZ:
  1741. case TRAT: setfdensity(f); break;
  1742. case TGAUS: setfgauss(f); break;
  1743. case TLOGT: setfbino(f); break;
  1744. case TRBIN: setfrbino(f); break;
  1745. case TPROB:
  1746. case TPOIS: setfpoisson(f); break;
  1747. case TGAMM: setfgamma(f); break;
  1748. case TGEOM: setfgeom(f); break;
  1749. case TWEIB: setfweibull(f);
  1750. case TCIRC: setfcirc(f); break;
  1751. case TROBT: setfrobust(f); break;
  1752. case TCAUC: setfcauchy(f); break;
  1753. case TQUANT: setfquant(f); break;
  1754. default: LERR(("setfamily: unknown family %d",tg&63));
  1755. return;
  1756. }
  1757. lnk = defaultlink(link(sp),f);
  1758. if (!f->vallink(lnk))
  1759. { WARN(("setfamily: invalid link %d - revert to default",link(sp)));
  1760. link(sp) = f->deflink;
  1761. }
  1762. else
  1763. link(sp) = lnk;
  1764. }
  1765. /*
  1766. * Copyright 1996-2006 Catherine Loader.
  1767. */
  1768. #include "locf.h"
  1769. int pois_vallink(link)
  1770. int link;
  1771. { return((link==LLOG) | (link==LIDENT) | (link==LSQRT));
  1772. }
  1773. int pois_fam(y,mean,th,link,res,cens,w)
  1774. double y, mean, th, *res, w;
  1775. int link, cens;
  1776. { double wmu, pt, dp;
  1777. if (link==LINIT)
  1778. { res[ZDLL] = MAX(y,0.0);
  1779. return(LF_OK);
  1780. }
  1781. wmu = w*mean;
  1782. if (inllmix) y = w*y;
  1783. if (cens)
  1784. { if (y<=0)
  1785. { res[ZLIK] = res[ZDLL] = res[ZDDLL] = 0.0;
  1786. return(LF_OK);
  1787. }
  1788. pt = igamma(wmu,y);
  1789. dp = dgamma(wmu,y,1.0,0)/pt;
  1790. res[ZLIK] = log(pt);
  1791. /*
  1792. * res[ZDLL] = dp * w*dmu/dth
  1793. * res[ZDDLL]= -dp*(w*d2mu/dth2 + (y-1)/mu*(dmu/dth)^2) + res[ZDLL]^2
  1794. */
  1795. if (link==LLOG)
  1796. { res[ZDLL] = dp*wmu;
  1797. res[ZDDLL]= -dp*wmu*(y-wmu) + SQR(res[ZDLL]);
  1798. return(LF_OK);
  1799. }
  1800. if (link==LIDENT)
  1801. { res[ZDLL] = dp*w;
  1802. res[ZDDLL]= -dp*(y-1-wmu)*w/mean + SQR(res[ZDLL]);
  1803. return(LF_OK);
  1804. }
  1805. if (link==LSQRT)
  1806. { res[ZDLL] = dp*2*w*th;
  1807. res[ZDDLL]= -dp*w*(4*y-2-4*wmu) + SQR(res[ZDLL]);
  1808. return(LF_OK);
  1809. } }
  1810. if (link==LLOG)
  1811. { if (y<0) /* goon observation - delete it */
  1812. { res[ZLIK] = res[ZDLL] = res[ZDDLL] = 0;
  1813. return(LF_OK);
  1814. }
  1815. res[ZLIK] = res[ZDLL] = y-wmu;
  1816. if (y>0) res[ZLIK] += y*(th-log(y/w));
  1817. res[ZDDLL] = wmu;
  1818. return(LF_OK);
  1819. }
  1820. if (link==LIDENT)
  1821. { if ((mean<=0) && (y>0)) return(LF_BADP);
  1822. res[ZLIK] = y-wmu;
  1823. res[ZDLL] = -w;
  1824. res[ZDDLL] = 0;
  1825. if (y>0)
  1826. { res[ZLIK] += y*log(wmu/y);
  1827. res[ZDLL] += y/mean;
  1828. res[ZDDLL]= y/(mean*mean);
  1829. }
  1830. return(LF_OK);
  1831. }
  1832. if (link==LSQRT)
  1833. { if ((mean<=0) && (y>0)) return(LF_BADP);
  1834. res[ZLIK] = y-wmu;
  1835. res[ZDLL] = -2*w*th;
  1836. res[ZDDLL]= 2*w;
  1837. if (y>0)
  1838. { res[ZLIK] += y*log(wmu/y);
  1839. res[ZDLL] += 2*y/th;
  1840. res[ZDDLL]+= 2*y/mean;
  1841. }
  1842. return(LF_OK);
  1843. }
  1844. LERR(("link %d invalid for Poisson family",link));
  1845. return(LF_LNK);
  1846. }
  1847. void setfpoisson(fam)
  1848. family *fam;
  1849. { fam->deflink = LLOG;
  1850. fam->canlink = LLOG;
  1851. fam->vallink = pois_vallink;
  1852. fam->family = pois_fam;
  1853. }
  1854. /*
  1855. * Copyright 1996-2006 Catherine Loader.
  1856. */
  1857. #include "locf.h"
  1858. #define QTOL 1.0e-10
  1859. extern int lf_status;
  1860. static double q0;
  1861. int quant_vallink(int link) { return(1); }
  1862. int quant_fam(y,mean,th,link,res,cens,w)
  1863. double y, mean, th, *res, w;
  1864. int link, cens;
  1865. { double z, p;
  1866. if (link==LINIT)
  1867. { res[ZDLL] = w*y;
  1868. return(LF_OK);
  1869. }
  1870. p = 0.5; /* should be pen(sp) */
  1871. z = y-mean;
  1872. res[ZLIK] = (z<0) ? (w*z/p) : (-w*z/(1-p));
  1873. res[ZDLL] = (z<0) ? -w/p : w/(1-p);
  1874. res[ZDDLL]= w/(p*(1-p));
  1875. return(LF_OK);
  1876. }
  1877. int quant_check(sp,des,lfd)
  1878. smpar *sp;
  1879. design *des;
  1880. lfdata *lfd;
  1881. { return(LF_DONE);
  1882. }
  1883. void setfquant(fam)
  1884. family *fam;
  1885. { fam->deflink = LIDENT;
  1886. fam->canlink = LIDENT;
  1887. fam->vallink = quant_vallink;
  1888. fam->family = quant_fam;
  1889. fam->pcheck = quant_check;
  1890. }
  1891. /*
  1892. * cycling rule for choosing among ties.
  1893. */
  1894. int tiecycle(ind,i0,i1,oi)
  1895. int *ind, i0, i1, oi;
  1896. { int i, ii, im;
  1897. im = ind[i0];
  1898. for (i=i0+1; i<=i1; i++)
  1899. { ii = ind[i];
  1900. if (im<=oi)
  1901. { if ((ii<im) | (ii>oi)) im = ii;
  1902. }
  1903. else
  1904. { if ((ii<im) & (ii>oi)) im = ii;
  1905. }
  1906. }
  1907. return(im);
  1908. }
  1909. /*
  1910. * move coefficient vector cf, as far as possible, in direction dc.
  1911. */
  1912. int movecoef(lfd,des,p,cf,dc,oi)
  1913. lfdata *lfd;
  1914. design *des;
  1915. double p, *cf, *dc;
  1916. int oi;
  1917. { int i, ii, im, i0, i1, j;
  1918. double *lb, *el, e, sp, sn, sw, sum1, sum2, tol1;
  1919. lb = des->th;
  1920. el = des->res;
  1921. sum1 = sum2 = 0.0;
  1922. sp = sn = sw = 0.0;
  1923. for (i=0; i<des->n; i++)
  1924. { ii = des->ind[i];
  1925. lb[ii] = innerprod(dc,d_xi(des,ii),des->p);
  1926. e = resp(lfd,ii) - innerprod(cf,d_xi(des,ii),des->p);
  1927. el[ii] = (fabs(lb[ii])<QTOL) ? 1e100 : e/lb[ii];
  1928. if (lb[ii]>0)
  1929. sp += prwt(lfd,ii)*wght(des,ii)*lb[ii];
  1930. else
  1931. sn -= prwt(lfd,ii)*wght(des,ii)*lb[ii];
  1932. sw += prwt(lfd,ii)*wght(des,ii);
  1933. }
  1934. printf("sp %8.5f sn %8.5f\n",sn,sp);
  1935. /* if sn, sp are both zero, should return an LF_PF.
  1936. * but within numerical tolerance? what does it mean?
  1937. */
  1938. if (sn+sp <= QTOL*q0) { lf_status = LF_PF; return(0); }
  1939. sum1 = sp/(1-p) + sn/p;
  1940. tol1 = QTOL*(sp+sn);
  1941. mut_order(el,des->ind,0,des->n-1);
  1942. for (i=0; i<des->n; i++)
  1943. { ii = des->ind[i];
  1944. sum2 += prwt(lfd,ii)*wght(des,ii)*((lb[ii]>0) ? lb[ii]/p : -lb[ii]/(1-p) );
  1945. sum1 -= prwt(lfd,ii)*wght(des,ii)*((lb[ii]>0) ? lb[ii]/(1-p) : -lb[ii]/p );
  1946. if (sum1<=sum2+tol1)
  1947. {
  1948. /* determine the range of ties [i0,i1]
  1949. * el[ind[i0..i1]] = el[ind[i]].
  1950. * if sum1==sum2, el[ind[i+1]]..el[ind[i1]]] = el[ind[i1]], else i1 = i.
  1951. */
  1952. i0 = i1 = i;
  1953. while ((i0>0) && (el[des->ind[i0-1]]==el[ii])) i0--;
  1954. while ((i1<des->n-1) && (el[des->ind[i1+1]]==el[ii])) i1++;
  1955. if (sum1>=sum2-tol1)
  1956. while ((i1<des->n-1) && (el[des->ind[i1+1]]==el[des->ind[i+1]])) i1++;
  1957. if (i0<i1) ii = tiecycle(des->ind,i0,i1,oi);
  1958. for (j=0; j<des->p; j++) cf[j] += el[ii]*dc[j];
  1959. return(ii);
  1960. }
  1961. }
  1962. mut_printf("Big finddlt problem.\n");
  1963. ii = des->ind[des->n-1];
  1964. for (j=0; j<des->p; j++) cf[j] += el[ii]*dc[j];
  1965. return(ii);
  1966. }
  1967. /*
  1968. * special version of movecoef for min/max.
  1969. */
  1970. int movemin(lfd,des,f,cf,dc,oi)
  1971. design *des;
  1972. lfdata *lfd;
  1973. double *cf, *dc, f;
  1974. int oi;
  1975. { int i, ii, im, p, s, ssum;
  1976. double *lb, sum, lb0, lb1, z0, z1;
  1977. lb = des->th;
  1978. s = (f<=0.0) ? 1 : -1;
  1979. /* first, determine whether move should be in positive or negative direction */
  1980. p = des->p;
  1981. sum = 0;
  1982. for (i=0; i<des->n; i++)
  1983. { ii = des->ind[i];
  1984. lb[ii] = innerprod(dc,d_xi(des,ii),des->p);
  1985. sum += prwt(lfd,ii)*wght(des,ii)*lb[ii];
  1986. }
  1987. if (fabs(sum) <= QTOL*q0)
  1988. { lf_status = LF_PF;
  1989. return(0);
  1990. }
  1991. ssum = (sum<=0.0) ? -1 : 1;
  1992. if (ssum != s)
  1993. for (i=0; i<p; i++) dc[i] = -dc[i];
  1994. /* now, move positively. How far can we move? */
  1995. lb0 = 1.0e100; im = oi;
  1996. for (i=0; i<des->n; i++)
  1997. { ii = des->ind[i];
  1998. lb[ii] = innerprod(dc,d_xi(des,ii),des->p); /* must recompute - signs! */
  1999. if (s*lb[ii]>QTOL) /* should have scale-free tolerance here */
  2000. { z0 = innerprod(cf,d_xi(des,ii),p);
  2001. lb1 = (resp(lfd,ii) - z0)/lb[ii];
  2002. if (lb1<lb0)
  2003. { if (fabs(lb1-lb0)<QTOL) /* cycle */
  2004. { if (im<=oi)
  2005. { if ((ii>oi) | (ii<im)) im = ii; }
  2006. else
  2007. { if ((ii>oi) & (ii<im)) im = ii; }
  2008. }
  2009. else
  2010. { im = ii; lb0 = lb1; }
  2011. }
  2012. }
  2013. }
  2014. for (i=0; i<p; i++) cf[i] = cf[i]+lb0*dc[i];
  2015. if (im==-1) lf_status = LF_PF;
  2016. return(im);
  2017. }
  2018. double qll(lfd,spr,des,cf)
  2019. lfdata *lfd;
  2020. smpar *spr;
  2021. design *des;
  2022. double *cf;
  2023. { int i, ii;
  2024. double th, sp, sn, p, e;
  2025. p = pen(spr);
  2026. sp = sn = 0.0;
  2027. for (i=0; i<des->n; i++)
  2028. { ii = des->ind[i];
  2029. th = innerprod(d_xi(des,ii),cf,des->p);
  2030. e = resp(lfd,ii)-th;
  2031. if (e<0) sn -= prwt(lfd,ii)*wght(des,ii)*e;
  2032. if (e>0) sp += prwt(lfd,ii)*wght(des,ii)*e;
  2033. }
  2034. if (p<=0.0) return((sn<QTOL) ? -sp : -1e300);
  2035. if (p>=1.0) return((sp<QTOL) ? -sn : -1e300);
  2036. return(-sp/(1-p)-sn/p);
  2037. }
  2038. /*
  2039. * running quantile smoother.
  2040. */
  2041. void lfquantile(lfd,sp,des,maxit)
  2042. lfdata *lfd;
  2043. smpar *sp;
  2044. design *des;
  2045. int maxit;
  2046. { int i, ii, im, j, k, p, *ci, (*mover)();
  2047. double *cf, *db, *dc, *cm, f, q1, q2, l0;
  2048. printf("in lfquantile\n");
  2049. f = pen(sp);
  2050. p = des->p;
  2051. cf = des->cf;
  2052. dc = des->oc;
  2053. db = des->ss;
  2054. setzero(cf,p);
  2055. setzero(dc,p);
  2056. cm = des->V;
  2057. setzero(cm,p*p);
  2058. ci = (int *)des->fix;
  2059. q1 = -qll(lfd,sp,des,cf);
  2060. if (q1==0.0) { lf_status = LF_PF; return; }
  2061. for (i=0; i<p; i++) cm[i*(p+1)] = 1;
  2062. mover = movecoef;
  2063. if ((f<=0.0) | (f>=1.0)) mover = movemin;
  2064. dc[0] = 1.0;
  2065. im = mover(lfd,des,f,cf,dc,-1);
  2066. if (lf_status != LF_OK) return;
  2067. ci[0] = im;
  2068. printf("init const %2d\n",ci[0]);
  2069. q0 = -qll(lfd,sp,des,cf);
  2070. if (q0<QTOL*q1) { lf_status = LF_PF; return; }
  2071. printf("loop 0\n"); fflush(stdout);
  2072. for (i=1; i<p; i++)
  2073. {
  2074. printf("i %2d\n",i);
  2075. memcpy(&cm[(i-1)*p],d_xi(des,im),p*sizeof(double));
  2076. setzero(db,p);
  2077. db[i] = 1.0;
  2078. resproj(db,cm,dc,p,i);
  2079. printf("call mover\n"); fflush(stdout);
  2080. im = mover(lfd,des,f,cf,dc,-1);
  2081. if (lf_status != LF_OK) return;
  2082. printf("mover %2d\n",im); fflush(stdout);
  2083. ci[i] = im;
  2084. }
  2085. printf("call qll\n"); fflush(stdout);
  2086. q1 = qll(lfd,sp,des,cf);
  2087. printf("loop 1 %d %d %d %d\n",ci[0],ci[1],ci[2],ci[3]); fflush(stdout);
  2088. for (k=0; k<maxit; k++)
  2089. { for (i=0; i<p; i++)
  2090. { for (j=0; j<p; j++)
  2091. if (j!=i) memcpy(&cm[(j-(j>i))*p],d_xi(des,ci[j]),p*sizeof(double));
  2092. memcpy(db,d_xi(des,ci[i]),p*sizeof(double));
  2093. resproj(db,cm,dc,p,p-1);
  2094. printf("call mover\n"); fflush(stdout);
  2095. im = mover(lfd,des,f,cf,dc,ci[i]);
  2096. if (lf_status != LF_OK) return;
  2097. printf("mover %2d\n",im); fflush(stdout);
  2098. ci[i] = im;
  2099. }
  2100. q2 = qll(lfd,sp,des,cf);
  2101. /*
  2102. * convergence: require no change -- reasonable, since discrete?
  2103. * remember we're maximizing, and q's are negative.
  2104. */
  2105. if (q2 <= q1) return;
  2106. q1 = q2;
  2107. }
  2108. printf("loop 2\n");
  2109. mut_printf("Warning: lfquantile not converged.\n");
  2110. }
  2111. /*
  2112. * Copyright 1996-2006 Catherine Loader.
  2113. */
  2114. #include "locf.h"
  2115. extern double links_rs;
  2116. int robust_vallink(link)
  2117. int link;
  2118. { return(link==LIDENT);
  2119. }
  2120. int robust_fam(y,mean,th,link,res,cens,w)
  2121. double y, mean, th, *res, w;
  2122. int link, cens;
  2123. { double z, sw;
  2124. if (link==LINIT)
  2125. { res[ZDLL] = w*y;
  2126. return(LF_OK);
  2127. }
  2128. sw = (w==1.0) ? 1.0 : sqrt(w); /* don't want unnecess. sqrt! */
  2129. z = sw*(y-mean)/links_rs;
  2130. res[ZLIK] = (fabs(z)<HUBERC) ? -z*z/2 : HUBERC*(HUBERC/2.0-fabs(z));
  2131. if (z< -HUBERC)
  2132. { res[ZDLL] = -sw*HUBERC/links_rs;
  2133. res[ZDDLL]= 0.0;
  2134. return(LF_OK);
  2135. }
  2136. if (z> HUBERC)
  2137. { res[ZDLL] = sw*HUBERC/links_rs;
  2138. res[ZDDLL]= 0.0;
  2139. return(LF_OK);
  2140. }
  2141. res[ZDLL] = sw*z/links_rs;
  2142. res[ZDDLL] = w/(links_rs*links_rs);
  2143. return(LF_OK);
  2144. }
  2145. int cauchy_fam(y,p,th,link,res,cens,w)
  2146. double y, p, th, *res, w;
  2147. int link, cens;
  2148. { double z;
  2149. if (link!=LIDENT)
  2150. { LERR(("Invalid link in famcauc"));
  2151. return(LF_LNK);
  2152. }
  2153. z = w*(y-th)/links_rs;
  2154. res[ZLIK] = -log(1+z*z);
  2155. res[ZDLL] = 2*w*z/(links_rs*(1+z*z));
  2156. res[ZDDLL] = 2*w*w*(1-z*z)/(links_rs*links_rs*(1+z*z)*(1+z*z));
  2157. return(LF_OK);
  2158. }
  2159. extern double lf_tol;
  2160. int robust_init(lfd,des,sp)
  2161. lfdata *lfd;
  2162. design *des;
  2163. smpar *sp;
  2164. { int i;
  2165. for (i=0; i<des->n; i++)
  2166. des->res[i] = resp(lfd,(int)des->ind[i]) - base(lfd,(int)des->ind[i]);
  2167. des->cf[0] = median(des->res,des->n);
  2168. for (i=1; i<des->p; i++) des->cf[i] = 0.0;
  2169. lf_tol = 1.0e-6;
  2170. return(LF_OK);
  2171. }
  2172. void setfrobust(fam)
  2173. family *fam;
  2174. { fam->deflink = LIDENT;
  2175. fam->canlink = LIDENT;
  2176. fam->vallink = robust_vallink;
  2177. fam->family = robust_fam;
  2178. fam->initial = robust_init;
  2179. fam->robust = 0;
  2180. }
  2181. void setfcauchy(fam)
  2182. family *fam;
  2183. { fam->deflink = LIDENT;
  2184. fam->canlink = LIDENT;
  2185. fam->vallink = robust_vallink;
  2186. fam->family = cauchy_fam;
  2187. fam->initial = robust_init;
  2188. fam->robust = 0;
  2189. }
  2190. /*
  2191. * Copyright 1996-2006 Catherine Loader.
  2192. */
  2193. #include "locf.h"
  2194. int weibull_vallink(link)
  2195. int link;
  2196. { return((link==LIDENT) | (link==LLOG) | (link==LLOGIT));
  2197. }
  2198. int weibull_fam(y,mean,th,link,res,cens,w)
  2199. double y, mean, th, *res, w;
  2200. int link, cens;
  2201. { double yy;
  2202. yy = pow(y,w);
  2203. if (link==LINIT)
  2204. { res[ZDLL] = MAX(yy,0.0);
  2205. return(LF_OK);
  2206. }
  2207. if (cens)
  2208. { res[ZLIK] = -yy/mean;
  2209. res[ZDLL] = res[ZDDLL] = yy/mean;
  2210. return(LF_OK);
  2211. }
  2212. res[ZLIK] = 1-yy/mean-th;
  2213. if (yy>0) res[ZLIK] += log(w*yy);
  2214. res[ZDLL] = -1+yy/mean;
  2215. res[ZDDLL]= yy/mean;
  2216. return(LF_OK);
  2217. }
  2218. void setfweibull(fam)
  2219. family *fam;
  2220. { fam->deflink = LLOG;
  2221. fam->canlink = LLOG;
  2222. fam->vallink = weibull_vallink;
  2223. fam->family = weibull_fam;
  2224. fam->robust = 0;
  2225. }
  2226. /*
  2227. * Copyright 1996-2006 Catherine Loader.
  2228. */
  2229. /*
  2230. Functions implementing the adaptive bandwidth selection.
  2231. Will make the final call to nbhd() to set smoothing weights
  2232. for selected bandwidth, But will **not** make the
  2233. final call to locfit().
  2234. */
  2235. #include "locf.h"
  2236. static double hmin;
  2237. #define NACRI 5
  2238. static char *atype[NACRI] = { "none", "cp", "ici", "mindex", "ok" };
  2239. static int avals[NACRI] = { ANONE, ACP, AKAT, AMDI, AOK };
  2240. int lfacri(char *z)
  2241. { return(pmatch(z, atype, avals, NACRI, ANONE));
  2242. }
  2243. double adcri(lk,t0,t2,pen)
  2244. double lk, t0, t2, pen;
  2245. { double y;
  2246. /* return(-2*lk/(t0*exp(pen*log(1-t2/t0)))); */
  2247. /* return((-2*lk+pen*t2)/t0); */
  2248. y = (MAX(-2*lk,t0-t2)+pen*t2)/t0;
  2249. return(y);
  2250. }
  2251. double mmse(lfd,sp,dv,des)
  2252. lfdata *lfd;
  2253. smpar *sp;
  2254. deriv *dv;
  2255. design *des;
  2256. { int i, ii, j, p, p1;
  2257. double sv, sb, *l, dp;
  2258. l = des->wd;
  2259. wdiag(lfd, sp, des,l,dv,0,1,0);
  2260. sv = sb = 0;
  2261. p = npar(sp);
  2262. for (i=0; i<des->n; i++)
  2263. { sv += l[i]*l[i];
  2264. ii = des->ind[i];
  2265. dp = dist(des,ii);
  2266. for (j=0; j<deg(sp); j++) dp *= dist(des,ii);
  2267. sb += fabs(l[i])*dp;
  2268. }
  2269. p1 = factorial(deg(sp)+1);
  2270. printf("%8.5f sv %8.5f sb %8.5f %8.5f\n",des->h,sv,sb,sv+sb*sb*pen(sp)*pen(sp)/(p1*p1));
  2271. return(sv+sb*sb*pen(sp)*pen(sp)/(p1*p1));
  2272. }
  2273. static double mcp, clo, cup;
  2274. /*
  2275. Initial bandwidth will be (by default)
  2276. k-nearest neighbors for k small, just large enough to
  2277. get defined estimate (unless user provided nonzero nn or fix-h components)
  2278. */
  2279. int ainitband(lfd,sp,dv,des)
  2280. lfdata *lfd;
  2281. smpar *sp;
  2282. deriv *dv;
  2283. design *des;
  2284. { int lf_status, p, z, cri, noit, redo;
  2285. double ho, t[6];
  2286. if (lf_debug >= 2) mut_printf("ainitband:\n");
  2287. p = des->p;
  2288. cri = acri(sp);
  2289. noit = (cri!=AOK);
  2290. z = (int)(lfd->n*nn(sp));
  2291. if ((noit) && (z<p+2)) z = p+2;
  2292. redo = 0; ho = -1;
  2293. do
  2294. {
  2295. nbhd(lfd,des,z,redo,sp);
  2296. if (z<des->n) z = des->n;
  2297. if (des->h>ho) lf_status = locfit(lfd,des,sp,noit,0,0);
  2298. z++;
  2299. redo = 1;
  2300. } while ((z<=lfd->n) && ((des->h==0)||(lf_status!=LF_OK)));
  2301. hmin = des->h;
  2302. switch(cri)
  2303. { case ACP:
  2304. local_df(lfd,sp,des,t);
  2305. mcp = adcri(des->llk,t[0],t[2],pen(sp));
  2306. return(lf_status);
  2307. case AKAT:
  2308. local_df(lfd,sp,des,t);
  2309. clo = des->cf[0]-pen(sp)*t[5];
  2310. cup = des->cf[0]+pen(sp)*t[5];
  2311. return(lf_status);
  2312. case AMDI:
  2313. mcp = mmse(lfd,sp,dv,des);
  2314. return(lf_status);
  2315. case AOK: return(lf_status);
  2316. }
  2317. LERR(("aband1: unknown criterion"));
  2318. return(LF_ERR);
  2319. }
  2320. /*
  2321. aband2 increases the initial bandwidth until lack of fit results,
  2322. or the fit is close to a global fit. Increase h by 1+0.3/d at
  2323. each iteration.
  2324. */
  2325. double aband2(lfd,sp,dv,des,h0)
  2326. lfdata *lfd;
  2327. smpar *sp;
  2328. deriv *dv;
  2329. design *des;
  2330. double h0;
  2331. { double t[6], h1, nu1, cp, ncp, tlo, tup;
  2332. int d, inc, n, p, done;
  2333. if (lf_debug >= 2) mut_printf("aband2:\n");
  2334. d = lfd->d; n = lfd->n; p = npar(sp);
  2335. h1 = des->h = h0;
  2336. done = 0; nu1 = 0.0;
  2337. inc = 0; ncp = 0.0;
  2338. while ((!done) & (nu1<(n-p)*0.95))
  2339. { fixh(sp) = (1+0.3/d)*des->h;
  2340. nbhd(lfd,des,0,1,sp);
  2341. if (locfit(lfd,des,sp,1,0,0) > 0) WARN(("aband2: failed fit"));
  2342. local_df(lfd,sp,des,t);
  2343. nu1 = t[0]-t[2]; /* tr(A) */
  2344. switch(acri(sp))
  2345. { case AKAT:
  2346. tlo = des->cf[0]-pen(sp)*t[5];
  2347. tup = des->cf[0]+pen(sp)*t[5];
  2348. /* mut_printf("h %8.5f tlo %8.5f tup %8.5f\n",des->h,tlo,tup); */
  2349. done = ((tlo>cup) | (tup<clo));
  2350. if (!done)
  2351. { clo = MAX(clo,tlo);
  2352. cup = MIN(cup,tup);
  2353. h1 = des->h;
  2354. }
  2355. break;
  2356. case ACP:
  2357. cp = adcri(des->llk,t[0],t[2],pen(sp));
  2358. /* mut_printf("h %8.5f lk %8.5f t0 %8.5f t2 %8.5f cp %8.5f\n",des->h,des->llk,t[0],t[2],cp); */
  2359. if (cp<mcp) { mcp = cp; h1 = des->h; }
  2360. if (cp>=ncp) inc++; else inc = 0;
  2361. ncp = cp;
  2362. done = (inc>=10) | ((inc>=3) & ((t[0]-t[2])>=10) & (cp>1.5*mcp));
  2363. break;
  2364. case AMDI:
  2365. cp = mmse(lfd,sp,dv,des);
  2366. if (cp<mcp) { mcp = cp; h1 = des->h; }
  2367. if (cp>ncp) inc++; else inc = 0;
  2368. ncp = cp;
  2369. done = (inc>=3);
  2370. break;
  2371. }
  2372. }
  2373. return(h1);
  2374. }
  2375. /*
  2376. aband3 does a finer search around best h so far. Try
  2377. h*(1-0.2/d), h/(1-0.1/d), h*(1+0.1/d), h*(1+0.2/d)
  2378. */
  2379. double aband3(lfd,sp,dv,des,h0)
  2380. lfdata *lfd;
  2381. smpar *sp;
  2382. deriv *dv;
  2383. design *des;
  2384. double h0;
  2385. { double t[6], h1, cp, tlo, tup;
  2386. int i, i0, d, n;
  2387. if (lf_debug >= 2) mut_printf("aband3:\n");
  2388. d = lfd->d; n = lfd->n;
  2389. h1 = h0;
  2390. i0 = (acri(sp)==AKAT) ? 1 : -2;
  2391. if (h0==hmin) i0 = 1;
  2392. for (i=i0; i<=2; i++)
  2393. { if (i==0) i++;
  2394. fixh(sp) = h0*(1+0.1*i/d);
  2395. nbhd(lfd,des,0,1,sp);
  2396. if (locfit(lfd,des,sp,1,0,0) > 0) WARN(("aband3: failed fit"));
  2397. local_df(lfd,sp,des,t);
  2398. switch (acri(sp))
  2399. { case AKAT:
  2400. tlo = des->cf[0]-pen(sp)*t[5];
  2401. tup = des->cf[0]+pen(sp)*t[5];
  2402. if ((tlo>cup) | (tup<clo)) /* done */
  2403. i = 2;
  2404. else
  2405. { h1 = des->h;
  2406. clo = MAX(clo,tlo);
  2407. cup = MIN(cup,tup);
  2408. }
  2409. break;
  2410. case ACP:
  2411. cp = adcri(des->llk,t[0],t[2],pen(sp));
  2412. if (cp<mcp) { mcp = cp; h1 = des->h; }
  2413. else
  2414. { if (i>0) i = 2; }
  2415. break;
  2416. case AMDI:
  2417. cp = mmse(lfd,sp,dv,des);
  2418. if (cp<mcp) { mcp = cp; h1 = des->h; }
  2419. else
  2420. { if (i>0) i = 2; }
  2421. }
  2422. }
  2423. return(h1);
  2424. }
  2425. int alocfit(lfd,sp,dv,des,cv)
  2426. lfdata *lfd;
  2427. smpar *sp;
  2428. deriv *dv;
  2429. design *des;
  2430. int cv;
  2431. { int lf_status;
  2432. double h0;
  2433. lf_status = ainitband(lfd,sp,dv,des);
  2434. if (lf_error) return(lf_status);
  2435. if (acri(sp) == AOK) return(lf_status);
  2436. h0 = fixh(sp);
  2437. fixh(sp) = aband2(lfd,sp,dv,des,des->h);
  2438. fixh(sp) = aband3(lfd,sp,dv,des,fixh(sp));
  2439. nbhd(lfd,des,0,1,sp);
  2440. lf_status = locfit(lfd,des,sp,0,0,cv);
  2441. fixh(sp) = h0;
  2442. return(lf_status);
  2443. }
  2444. /*
  2445. * Copyright 1996-2006 Catherine Loader.
  2446. */
  2447. /*
  2448. *
  2449. * Evaluate the locfit fitting functions.
  2450. * calcp(sp,d)
  2451. * calculates the number of fitting functions.
  2452. * makecfn(sp,des,dv,d)
  2453. * makes the coef.number vector.
  2454. * fitfun(lfd, sp, x,t,f,dv)
  2455. * lfd is the local fit structure.
  2456. * sp smoothing parameter structure.
  2457. * x is the data point.
  2458. * t is the fitting point.
  2459. * f is a vector to return the results.
  2460. * dv derivative structure.
  2461. * designmatrix(lfd, sp, des)
  2462. * is a wrapper for fitfun to build the design matrix.
  2463. *
  2464. */
  2465. #include "locf.h"
  2466. int calcp(sp,d)
  2467. smpar *sp;
  2468. int d;
  2469. { int i, k;
  2470. if (ubas(sp)) return(npar(sp));
  2471. switch (kt(sp))
  2472. { case KSPH:
  2473. case KCE:
  2474. k = 1;
  2475. for (i=1; i<=deg(sp); i++) k = k*(d+i)/i;
  2476. return(k);
  2477. case KPROD: return(d*deg(sp)+1);
  2478. case KLM: return(d);
  2479. case KZEON: return(1);
  2480. }
  2481. LERR(("calcp: invalid kt %d",kt(sp)));
  2482. return(0);
  2483. }
  2484. int coefnumber(dv,kt,d,deg)
  2485. int kt, d, deg;
  2486. deriv *dv;
  2487. { int d0, d1, t;
  2488. if (d==1)
  2489. { if (dv->nd<=deg) return(dv->nd);
  2490. return(-1);
  2491. }
  2492. if (dv->nd==0) return(0);
  2493. if (deg==0) return(-1);
  2494. if (dv->nd==1) return(1+dv->deriv[0]);
  2495. if (deg==1) return(-1);
  2496. if (kt==KPROD) return(-1);
  2497. if (dv->nd==2)
  2498. { d0 = dv->deriv[0]; d1 = dv->deriv[1];
  2499. if (d0<d1) { t = d0; d0 = d1; d1 = t; }
  2500. return((d+1)*(d0+1)-d0*(d0+3)/2+d1);
  2501. }
  2502. if (deg==2) return(-1);
  2503. LERR(("coefnumber not programmed for nd>=3"));
  2504. return(-1);
  2505. }
  2506. void makecfn(sp,des,dv,d)
  2507. smpar *sp;
  2508. design *des;
  2509. deriv *dv;
  2510. int d;
  2511. { int i, nd;
  2512. nd = dv->nd;
  2513. des->cfn[0] = coefnumber(dv,kt(sp),d,deg(sp));
  2514. des->ncoef = 1;
  2515. if (nd >= deg(sp)) return;
  2516. if (kt(sp)==KZEON) return;
  2517. if (d>1)
  2518. { if (nd>=2) return;
  2519. if ((nd>=1) && (kt(sp)==KPROD)) return;
  2520. }
  2521. dv->nd = nd+1;
  2522. for (i=0; i<d; i++)
  2523. { dv->deriv[nd] = i;
  2524. des->cfn[i+1] = coefnumber(dv,kt(sp),d,deg(sp));
  2525. }
  2526. dv->nd = nd;
  2527. des->ncoef = 1+d;
  2528. }
  2529. void fitfunangl(dx,ff,sca,cd,deg)
  2530. double dx, *ff, sca;
  2531. int deg, cd;
  2532. {
  2533. if (deg>=3) WARN(("Can't handle angular model with deg>=3"));
  2534. switch(cd)
  2535. { case 0:
  2536. ff[0] = 1;
  2537. ff[1] = sin(dx/sca)*sca;
  2538. ff[2] = (1-cos(dx/sca))*sca*sca;
  2539. return;
  2540. case 1:
  2541. ff[0] = 0;
  2542. ff[1] = cos(dx/sca);
  2543. ff[2] = sin(dx/sca)*sca;
  2544. return;
  2545. case 2:
  2546. ff[0] = 0;
  2547. ff[1] = -sin(dx/sca)/sca;
  2548. ff[2] = cos(dx/sca);
  2549. return;
  2550. default: WARN(("Can't handle angular model with >2 derivs"));
  2551. }
  2552. }
  2553. void fitfun(lfd,sp,x,t,f,dv)
  2554. lfdata *lfd;
  2555. smpar *sp;
  2556. double *x, *t, *f;
  2557. deriv *dv;
  2558. { int d, deg, nd, m, i, j, k, ct_deriv[MXDIM];
  2559. double ff[MXDIM][1+MXDEG], dx[MXDIM], *xx[MXDIM];
  2560. if (ubas(sp))
  2561. { for (i=0; i<lfd->d; i++) xx[i] = &x[i];
  2562. i = 0;
  2563. sp->vbasis(xx,t,1,lfd->d,1,npar(sp),f);
  2564. return;
  2565. }
  2566. d = lfd->d;
  2567. deg = deg(sp);
  2568. m = 0;
  2569. nd = (dv==NULL) ? 0 : dv->nd;
  2570. if (kt(sp)==KZEON)
  2571. { f[0] = 1.0;
  2572. return;
  2573. }
  2574. if (kt(sp)==KLM)
  2575. { for (i=0; i<d; i++) f[m++] = x[i];
  2576. return;
  2577. }
  2578. f[m++] = (nd==0);
  2579. if (deg==0) return;
  2580. for (i=0; i<d; i++)
  2581. { ct_deriv[i] = 0;
  2582. dx[i] = (t==NULL) ? x[i] : x[i]-t[i];
  2583. }
  2584. for (i=0; i<nd; i++) ct_deriv[dv->deriv[i]]++;
  2585. for (i=0; i<d; i++)
  2586. { switch(lfd->sty[i])
  2587. {
  2588. case STANGL:
  2589. fitfunangl(dx[i],ff[i],lfd->sca[i],ct_deriv[i],deg(sp));
  2590. break;
  2591. default:
  2592. for (j=0; j<ct_deriv[i]; j++) ff[i][j] = 0.0;
  2593. ff[i][ct_deriv[i]] = 1.0;
  2594. for (j=ct_deriv[i]+1; j<=deg; j++)
  2595. ff[i][j] = ff[i][j-1]*dx[i]/(j-ct_deriv[i]);
  2596. }
  2597. }
  2598. /*
  2599. * Product kernels. Note that if ct_deriv[i] != nd, that implies
  2600. * there is differentiation wrt another variable, and all components
  2601. * involving x[i] are 0.
  2602. */
  2603. if ((d==1) || (kt(sp)==KPROD))
  2604. { for (j=1; j<=deg; j++)
  2605. for (i=0; i<d; i++)
  2606. f[m++] = (ct_deriv[i]==nd) ? ff[i][j] : 0.0;
  2607. return;
  2608. }
  2609. /*
  2610. * Spherical kernels with the full polynomial basis.
  2611. * Presently implemented up to deg=3.
  2612. */
  2613. for (i=0; i<d; i++)
  2614. f[m++] = (ct_deriv[i]==nd) ? ff[i][1] : 0.0;
  2615. if (deg==1) return;
  2616. for (i=0; i<d; i++)
  2617. {
  2618. /* xi^2/2 terms. */
  2619. f[m++] = (ct_deriv[i]==nd) ? ff[i][2] : 0.0;
  2620. /* xi xj terms */
  2621. for (j=i+1; j<d; j++)
  2622. f[m++] = (ct_deriv[i]+ct_deriv[j]==nd) ? ff[i][1]*ff[j][1] : 0.0;
  2623. }
  2624. if (deg==2) return;
  2625. for (i=0; i<d; i++)
  2626. {
  2627. /* xi^3/6 terms */
  2628. f[m++] = (ct_deriv[i]==nd) ? ff[i][3] : 0.0;
  2629. /* xi^2/2 xk terms */
  2630. for (k=i+1; k<d; k++)
  2631. f[m++] = (ct_deriv[i]+ct_deriv[k]==nd) ? ff[i][2]*ff[k][1] : 0.0;
  2632. /* xi xj xk terms */
  2633. for (j=i+1; j<d; j++)
  2634. { f[m++] = (ct_deriv[i]+ct_deriv[j]==nd) ? ff[i][1]*ff[j][2] : 0.0;
  2635. for (k=j+1; k<d; k++)
  2636. f[m++] = (ct_deriv[i]+ct_deriv[j]+ct_deriv[k]==nd) ?
  2637. ff[i][1]*ff[j][1]*ff[k][1] : 0.0;
  2638. }
  2639. }
  2640. if (deg==3) return;
  2641. LERR(("fitfun: can't handle deg=%d for spherical kernels",deg));
  2642. }
  2643. /*
  2644. * Build the design matrix. Assumes des->ind contains the indices of
  2645. * the required data points; des->n the number of points; des->xev
  2646. * the fitting point.
  2647. */
  2648. void designmatrix(lfd,sp,des)
  2649. lfdata *lfd;
  2650. smpar *sp;
  2651. design *des;
  2652. { int i, ii, j, p;
  2653. double *X, u[MXDIM];
  2654. X = d_x(des);
  2655. p = des->p;
  2656. if (ubas(sp))
  2657. {
  2658. sp->vbasis(lfd->x,des->xev,lfd->n,lfd->d,des->n,p,X);
  2659. return;
  2660. }
  2661. for (i=0; i<des->n; i++)
  2662. { ii = des->ind[i];
  2663. for (j=0; j<lfd->d; j++) u[j] = datum(lfd,j,ii);
  2664. fitfun(lfd,sp,u,des->xev,&X[ii*p],NULL);
  2665. }
  2666. }
  2667. /*
  2668. * Copyright 1996-2006 Catherine Loader.
  2669. */
  2670. /*
  2671. *
  2672. *
  2673. * Functions for determining bandwidth; smoothing neighborhood
  2674. * and smoothing weights.
  2675. */
  2676. #include "locf.h"
  2677. double rho(x,sc,d,kt,sty) /* ||x|| for appropriate distance metric */
  2678. double *x, *sc;
  2679. int d, kt, *sty;
  2680. { double rhoi[MXDIM], s;
  2681. int i;
  2682. for (i=0; i<d; i++)
  2683. { if (sty!=NULL)
  2684. { switch(sty[i])
  2685. { case STANGL: rhoi[i] = 2*sin(x[i]/(2*sc[i])); break;
  2686. case STCPAR: rhoi[i] = 0; break;
  2687. default: rhoi[i] = x[i]/sc[i];
  2688. } }
  2689. else rhoi[i] = x[i]/sc[i];
  2690. }
  2691. if (d==1) return(fabs(rhoi[0]));
  2692. s = 0;
  2693. if (kt==KPROD)
  2694. { for (i=0; i<d; i++)
  2695. { rhoi[i] = fabs(rhoi[i]);
  2696. if (rhoi[i]>s) s = rhoi[i];
  2697. }
  2698. return(s);
  2699. }
  2700. if (kt==KSPH)
  2701. { for (i=0; i<d; i++)
  2702. s += rhoi[i]*rhoi[i];
  2703. return(sqrt(s));
  2704. }
  2705. LERR(("rho: invalid kt"));
  2706. return(0.0);
  2707. }
  2708. double kordstat(x,k,n,ind)
  2709. double *x;
  2710. int k, n, *ind;
  2711. { int i, i0, i1, l, r;
  2712. double piv;
  2713. if (k<1) return(0.0);
  2714. i0 = 0; i1 = n-1;
  2715. while (1)
  2716. { piv = x[ind[(i0+i1)/2]];
  2717. l = i0; r = i1;
  2718. while (l<=r)
  2719. { while ((l<=i1) && (x[ind[l]]<=piv)) l++;
  2720. while ((r>=i0) && (x[ind[r]]>piv)) r--;
  2721. if (l<=r) ISWAP(ind[l],ind[r]);
  2722. } /* now, x[ind[i0..r]] <= piv < x[ind[l..i1]] */
  2723. if (r<k-1) i0 = l; /* go right */
  2724. else /* put pivots in middle */
  2725. { for (i=i0; i<=r; )
  2726. if (x[ind[i]]==piv) { ISWAP(ind[i],ind[r]); r--; }
  2727. else i++;
  2728. if (r<k-1) return(piv);
  2729. i1 = r;
  2730. }
  2731. }
  2732. }
  2733. /* check if i'th data point is in limits */
  2734. int inlim(lfd,i)
  2735. lfdata *lfd;
  2736. int i;
  2737. { int d, j, k;
  2738. double *xlim;
  2739. xlim = lfd->xl;
  2740. d = lfd->d;
  2741. k = 1;
  2742. for (j=0; j<d; j++)
  2743. { if (xlim[j]<xlim[j+d])
  2744. k &= ((datum(lfd,j,i)>=xlim[j]) & (datum(lfd,j,i)<=xlim[j+d]));
  2745. }
  2746. return(k);
  2747. }
  2748. double compbandwid(di,ind,x,n,d,nn,fxh)
  2749. double *di, *x, fxh;
  2750. int n, d, nn, *ind;
  2751. { int i;
  2752. double nnh;
  2753. if (nn==0) return(fxh);
  2754. if (nn<n)
  2755. nnh = kordstat(di,nn,n,ind);
  2756. else
  2757. { nnh = 0;
  2758. for (i=0; i<n; i++) nnh = MAX(nnh,di[i]);
  2759. nnh = nnh*exp(log(1.0*nn/n)/d);
  2760. }
  2761. return(MAX(fxh,nnh));
  2762. }
  2763. /*
  2764. fast version of nbhd for ordered 1-d data
  2765. */
  2766. void nbhd1(lfd,sp,des,k)
  2767. lfdata *lfd;
  2768. smpar *sp;
  2769. design *des;
  2770. int k;
  2771. { double x, h, *xd, sc;
  2772. int i, l, r, m, n, z;
  2773. n = lfd->n;
  2774. x = des->xev[0];
  2775. xd = dvari(lfd,0);
  2776. sc = lfd->sca[0];
  2777. /* find closest data point to x */
  2778. if (x<=xd[0]) z = 0;
  2779. else
  2780. if (x>=xd[n-1]) z = n-1;
  2781. else
  2782. { l = 0; r = n-1;
  2783. while (r-l>1)
  2784. { z = (r+l)/2;
  2785. if (xd[z]>x) r = z;
  2786. else l = z;
  2787. }
  2788. /* now, xd[0..l] <= x < x[r..n-1] */
  2789. if ((x-xd[l])>(xd[r]-x)) z = r; else z = l;
  2790. }
  2791. /* closest point to x is xd[z] */
  2792. if (nn(sp)<0) /* user bandwidth */
  2793. h = sp->vb(des->xev);
  2794. else
  2795. { if (k>0) /* set h to nearest neighbor bandwidth */
  2796. { l = r = z;
  2797. if (l==0) r = k-1;
  2798. if (r==n-1) l = n-k;
  2799. while (r-l<k-1)
  2800. { if ((x-xd[l-1])<(xd[r+1]-x)) l--; else r++;
  2801. if (l==0) r = k-1;
  2802. if (r==n-1) l = n-k;
  2803. }
  2804. h = x-xd[l];
  2805. if (h<xd[r]-x) h = xd[r]-x;
  2806. }
  2807. else h = 0;
  2808. h /= sc;
  2809. if (h<fixh(sp)) h = fixh(sp);
  2810. }
  2811. m = 0;
  2812. if (xd[z]>x) z--; /* so xd[z]<=x */
  2813. /* look left */
  2814. for (i=z; i>=0; i--) if (inlim(lfd,i))
  2815. { dist(des,i) = (x-xd[i])/sc;
  2816. wght(des,i) = weight(lfd, sp, &xd[i], &x, h, 1, dist(des,i));
  2817. if (wght(des,i)>0)
  2818. { des->ind[m] = i;
  2819. m++;
  2820. } else i = 0;
  2821. }
  2822. /* look right */
  2823. for (i=z+1; i<n; i++) if (inlim(lfd,i))
  2824. { dist(des,i) = (xd[i]-x)/sc;
  2825. wght(des,i) = weight(lfd, sp, &xd[i], &x, h, 1, dist(des,i));
  2826. if (wght(des,i)>0)
  2827. { des->ind[m] = i;
  2828. m++;
  2829. } else i = n;
  2830. }
  2831. des->n = m;
  2832. des->h = h;
  2833. }
  2834. void nbhd_zeon(lfd,des)
  2835. lfdata *lfd;
  2836. design *des;
  2837. { int i, j, m, eq;
  2838. m = 0;
  2839. for (i=0; i<lfd->n; i++)
  2840. { eq = 1;
  2841. for (j=0; j<lfd->d; j++) eq = eq && (des->xev[j] == datum(lfd,j,i));
  2842. if (eq)
  2843. { wght(des,i) = 1;
  2844. des->ind[m] = i;
  2845. m++;
  2846. }
  2847. }
  2848. des->n = m;
  2849. des->h = 1.0;
  2850. }
  2851. void nbhd(lfd,des,nn,redo,sp)
  2852. lfdata *lfd;
  2853. design *des;
  2854. int redo, nn;
  2855. smpar *sp;
  2856. { int d, i, j, m, n;
  2857. double h, u[MXDIM];
  2858. if (lf_debug>1) mut_printf("nbhd: nn %d fixh %8.5f\n",nn,fixh(sp));
  2859. d = lfd->d; n = lfd->n;
  2860. if (ker(sp)==WPARM)
  2861. { for (i=0; i<n; i++)
  2862. { wght(des,i) = 1.0;
  2863. des->ind[i] = i;
  2864. }
  2865. des->n = n;
  2866. return;
  2867. }
  2868. if (kt(sp)==KZEON)
  2869. { nbhd_zeon(lfd,des);
  2870. return;
  2871. }
  2872. if (kt(sp)==KCE)
  2873. { des->h = 0.0;
  2874. return;
  2875. }
  2876. /* ordered 1-dim; use fast searches */
  2877. if ((nn<=n) & (lfd->ord) & (ker(sp)!=WMINM) & (lfd->sty[0]!=STANGL))
  2878. { nbhd1(lfd,sp,des,nn);
  2879. return;
  2880. }
  2881. if (!redo)
  2882. { for (i=0; i<n; i++)
  2883. { for (j=0; j<d; j++) u[j] = datum(lfd,j,i)-des->xev[j];
  2884. dist(des,i) = rho(u,lfd->sca,d,kt(sp),lfd->sty);
  2885. des->ind[i] = i;
  2886. }
  2887. }
  2888. else
  2889. for (i=0; i<n; i++) des->ind[i] = i;
  2890. if (ker(sp)==WMINM)
  2891. { des->h = minmax(lfd,des,sp);
  2892. return;
  2893. }
  2894. if (nn<0)
  2895. h = sp->vb(des->xev);
  2896. else
  2897. h = compbandwid(des->di,des->ind,des->xev,n,lfd->d,nn,fixh(sp));
  2898. m = 0;
  2899. for (i=0; i<n; i++) if (inlim(lfd,i))
  2900. { for (j=0; j<d; j++) u[j] = datum(lfd,j,i);
  2901. wght(des,i) = weight(lfd, sp, u, des->xev, h, 1, dist(des,i));
  2902. if (wght(des,i)>0)
  2903. { des->ind[m] = i;
  2904. m++;
  2905. }
  2906. }
  2907. des->n = m;
  2908. des->h = h;
  2909. }
  2910. /*
  2911. * Copyright 1996-2006 Catherine Loader.
  2912. */
  2913. /*
  2914. *
  2915. * This file includes functions to solve for the scale estimate in
  2916. * local robust regression and likelihood. The main entry point is
  2917. * lf_robust(lfd,sp,des,mxit),
  2918. * called from the locfit() function.
  2919. *
  2920. * The update_rs(x) accepts a residual scale x as the argument (actually,
  2921. * it works on the log-scale). The function computes the local fit
  2922. * assuming this residual scale, and re-estimates the scale from this
  2923. * new fit. The final solution satisfies the fixed point equation
  2924. * update_rs(x)=x. The function lf_robust() automatically calls
  2925. * update_rs() through the fixed point iterations.
  2926. *
  2927. * The estimation of the scale from the fit is based on the sqrt of
  2928. * the median deviance of observations with non-zero weights (in the
  2929. * gaussian case, this is the median absolute residual).
  2930. *
  2931. * TODO:
  2932. * Should use smoothing weights in the median.
  2933. */
  2934. #include "locf.h"
  2935. extern int lf_status;
  2936. double robscale;
  2937. static lfdata *rob_lfd;
  2938. static smpar *rob_sp;
  2939. static design *rob_des;
  2940. static int rob_mxit;
  2941. double median(x,n)
  2942. double *x;
  2943. int n;
  2944. { int i, j, lt, eq, gt;
  2945. double lo, hi, s;
  2946. lo = hi = x[0];
  2947. for (i=0; i<n; i++)
  2948. { lo = MIN(lo,x[i]);
  2949. hi = MAX(hi,x[i]);
  2950. }
  2951. if (lo==hi) return(lo);
  2952. lo -= (hi-lo);
  2953. hi += (hi-lo);
  2954. for (i=0; i<n; i++)
  2955. { if ((x[i]>lo) & (x[i]<hi))
  2956. { s = x[i]; lt = eq = gt = 0;
  2957. for (j=0; j<n; j++)
  2958. { lt += (x[j]<s);
  2959. eq += (x[j]==s);
  2960. gt += (x[j]>s);
  2961. }
  2962. if ((2*(lt+eq)>n) && (2*(gt+eq)>n)) return(s);
  2963. if (2*(lt+eq)<=n) lo = s;
  2964. if (2*(gt+eq)<=n) hi = s;
  2965. }
  2966. }
  2967. return((hi+lo)/2);
  2968. }
  2969. double nrobustscale(lfd,sp,des,rs)
  2970. lfdata *lfd;
  2971. smpar *sp;
  2972. design *des;
  2973. double rs;
  2974. { int i, ii, p;
  2975. double link[LLEN], sc, sd, sw, e;
  2976. p = des->p; sc = sd = sw = 0.0;
  2977. for (i=0; i<des->n; i++)
  2978. { ii = des->ind[i];
  2979. fitv(des,ii) = base(lfd,ii)+innerprod(des->cf,d_xi(des,ii),p);
  2980. e = resp(lfd,ii)-fitv(des,ii);
  2981. stdlinks(link,lfd,sp,ii,fitv(des,ii),rs);
  2982. sc += wght(des,ii)*e*link[ZDLL];
  2983. sd += wght(des,ii)*e*e*link[ZDDLL];
  2984. sw += wght(des,ii);
  2985. }
  2986. /* newton-raphson iteration for log(s)
  2987. -psi(ei/s) - log(s); s = e^{-th}
  2988. */
  2989. rs *= exp((sc-sw)/(sd+sc));
  2990. return(rs);
  2991. }
  2992. double robustscale(lfd,sp,des)
  2993. lfdata *lfd;
  2994. smpar *sp;
  2995. design *des;
  2996. { int i, ii, p, fam, lin, or;
  2997. double rs, link[LLEN];
  2998. p = des->p;
  2999. fam = fam(sp);
  3000. lin = link(sp);
  3001. or = fami(sp)->robust;
  3002. fami(sp)->robust = 0;
  3003. for (i=0; i<des->n; i++)
  3004. { ii = des->ind[i];
  3005. fitv(des,ii) = base(lfd,ii) + innerprod(des->cf,d_xi(des,ii),p);
  3006. links(fitv(des,ii),resp(lfd,ii),fami(sp),lin,link,cens(lfd,ii),prwt(lfd,ii),1.0);
  3007. des->res[i] = -2*link[ZLIK];
  3008. }
  3009. fami(sp)->robust = or;
  3010. rs = sqrt(median(des->res,des->n));
  3011. if (rs==0.0) rs = 1.0;
  3012. return(rs);
  3013. }
  3014. double update_rs(x)
  3015. double x;
  3016. { double nx;
  3017. if (lf_status != LF_OK) return(x);
  3018. robscale = exp(x);
  3019. lfiter(rob_lfd,rob_sp,rob_des,rob_mxit);
  3020. if (lf_status != LF_OK) return(x);
  3021. nx = log(robustscale(rob_lfd,rob_sp,rob_des));
  3022. if (nx<x-0.2) nx = x-0.2;
  3023. return(nx);
  3024. }
  3025. void lf_robust(lfd,sp,des,mxit)
  3026. lfdata *lfd;
  3027. design *des;
  3028. smpar *sp;
  3029. int mxit;
  3030. { double x;
  3031. rob_lfd = lfd;
  3032. rob_des = des;
  3033. rob_sp = sp;
  3034. rob_mxit = mxit;
  3035. lf_status = LF_OK;
  3036. x = log(robustscale(lfd,sp,des));
  3037. solve_fp(update_rs, x, 1.0e-6, mxit);
  3038. }
  3039. /*
  3040. * Copyright 1996-2006 Catherine Loader.
  3041. */
  3042. /*
  3043. * Post-fitting functions to compute the local variance and
  3044. * influence functions. Also the local degrees of freedom
  3045. * calculations for adaptive smoothing.
  3046. */
  3047. #include "locf.h"
  3048. extern double robscale;
  3049. /*
  3050. vmat() computes (after the local fit..) the matrix
  3051. M2 = X^T W^2 V X.
  3052. M12 = (X^T W V X)^{-1} M2
  3053. Also, for convenience, tr[0] = sum(wi) tr[1] = sum(wi^2).
  3054. */
  3055. void vmat(lfd, sp, des, M12, M2)
  3056. lfdata *lfd;
  3057. smpar *sp;
  3058. design *des;
  3059. double *M12, *M2;
  3060. { int i, ii, p, nk, ok;
  3061. double link[LLEN], h, ww, tr0, tr1;
  3062. p = des->p;
  3063. setzero(M2,p*p);
  3064. nk = -1;
  3065. /* for density estimation, use integral rather than
  3066. sum form, if W^2 is programmed...
  3067. */
  3068. if ((fam(sp)<=THAZ) && (link(sp)==LLOG))
  3069. { switch(ker(sp))
  3070. { case WGAUS: nk = WGAUS; h = des->h/SQRT2; break;
  3071. case WRECT: nk = WRECT; h = des->h; break;
  3072. case WEPAN: nk = WBISQ; h = des->h; break;
  3073. case WBISQ: nk = WQUQU; h = des->h; break;
  3074. case WTCUB: nk = W6CUB; h = des->h; break;
  3075. case WEXPL: nk = WEXPL; h = des->h/2; break;
  3076. }
  3077. }
  3078. tr0 = tr1 = 0.0;
  3079. if (nk != -1)
  3080. { ok = ker(sp); ker(sp) = nk;
  3081. /* compute M2 using integration. Use M12 as work matrix. */
  3082. (des->itype)(des->xev, M2, M12, des->cf, h);
  3083. ker(sp) = ok;
  3084. if (fam(sp)==TDEN) multmatscal(M2,des->smwt,p*p);
  3085. tr0 = des->ss[0];
  3086. tr1 = M2[0]; /* n int W e^<a,A> */
  3087. }
  3088. else
  3089. { for (i=0; i<des->n; i++)
  3090. { ii = des->ind[i];
  3091. stdlinks(link,lfd,sp,ii,fitv(des,ii),robscale);
  3092. ww = SQR(wght(des,ii))*link[ZDDLL];
  3093. tr0 += wght(des,ii);
  3094. tr1 += SQR(wght(des,ii));
  3095. addouter(M2,d_xi(des,ii),d_xi(des,ii),p,ww);
  3096. }
  3097. }
  3098. des->tr0 = tr0;
  3099. des->tr1 = tr1;
  3100. memcpy(M12,M2,p*p*sizeof(double));
  3101. for (i=0; i<p; i++)
  3102. jacob_solve(&des->xtwx,&M12[i*p]);
  3103. }
  3104. void lf_vcov(lfd,sp,des)
  3105. lfdata *lfd;
  3106. smpar *sp;
  3107. design *des;
  3108. { int i, j, k, p;
  3109. double *M12, *M2;
  3110. M12 = des->V; M2 = des->P; p = des->p;
  3111. vmat(lfd,sp,des,M12,M2); /* M2 = X^T W^2 V X tr0=sum(W) tr1=sum(W*W) */
  3112. des->tr2 = m_trace(M12,p); /* tr (XTWVX)^{-1}(XTW^2VX) */
  3113. /*
  3114. * Covariance matrix is M1^{-1} * M2 * M1^{-1}
  3115. * We compute this using the cholesky decomposition of
  3116. * M2; premultiplying by M1^{-1} and squaring. This
  3117. * is more stable than direct computation in near-singular cases.
  3118. */
  3119. chol_dec(M2,p,p);
  3120. for (i=0; i<p; i++)
  3121. for (j=0; j<i; j++)
  3122. { M2[j*p+i] = M2[i*p+j];
  3123. M2[i*p+j] = 0.0;
  3124. }
  3125. for (i=0; i<p; i++) jacob_solve(&des->xtwx,&M2[i*p]);
  3126. for (i=0; i<p; i++)
  3127. { for (j=0; j<p; j++)
  3128. { M12[i*p+j] = 0;
  3129. for (k=0; k<p; k++)
  3130. M12[i*p+j] += M2[k*p+i]*M2[k*p+j]; /* ith column of covariance */
  3131. }
  3132. }
  3133. if ((fam(sp)==TDEN) && (link(sp)==LIDENT))
  3134. multmatscal(M12,1/SQR(des->smwt),p*p);
  3135. /* this computes the influence function as des->f1[0]. */
  3136. unitvec(des->f1,0,des->p);
  3137. jacob_solve(&des->xtwx,des->f1);
  3138. }
  3139. /* local_df computes:
  3140. * tr[0] = trace(W)
  3141. * tr[1] = trace(W*W)
  3142. * tr[2] = trace( M1^{-1} M2 )
  3143. * tr[3] = trace( M1^{-1} M3 )
  3144. * tr[4] = trace( (M1^{-1} M2)^2 )
  3145. * tr[5] = var(theta-hat).
  3146. */
  3147. void local_df(lfd,sp,des,tr)
  3148. lfdata *lfd;
  3149. smpar *sp;
  3150. design *des;
  3151. double *tr;
  3152. { int i, ii, j, p;
  3153. double *m2, *V, ww, link[LLEN];
  3154. tr[0] = tr[1] = tr[2] = tr[3] = tr[4] = tr[5] = 0.0;
  3155. m2 = des->V; V = des->P; p = des->p;
  3156. vmat(lfd,sp,des,m2,V); /* M = X^T W^2 V X tr0=sum(W) tr1=sum(W*W) */
  3157. tr[0] = des->tr0;
  3158. tr[1] = des->tr1;
  3159. tr[2] = m_trace(m2,p); /* tr (XTWVX)^{-1}(XTW^2VX) */
  3160. unitvec(des->f1,0,p);
  3161. jacob_solve(&des->xtwx,des->f1);
  3162. for (i=0; i<p; i++)
  3163. for (j=0; j<p; j++)
  3164. { tr[4] += m2[i*p+j]*m2[j*p+i]; /* tr(M^2) */
  3165. tr[5] += des->f1[i]*V[i*p+j]*des->f1[j]; /* var(thetahat) */
  3166. }
  3167. tr[5] = sqrt(tr[5]);
  3168. setzero(m2,p*p);
  3169. for (i=0; i<des->n; i++)
  3170. { ii = des->ind[i];
  3171. stdlinks(link,lfd,sp,ii,fitv(des,ii),robscale);
  3172. ww = wght(des,ii)*wght(des,ii)*wght(des,ii)*link[ZDDLL];
  3173. addouter(m2,d_xi(des,ii),d_xi(des,ii),p,ww);
  3174. }
  3175. for (i=0; i<p; i++)
  3176. { jacob_solve(&des->xtwx,&m2[i*p]);
  3177. tr[3] += m2[i*(p+1)];
  3178. }
  3179. return;
  3180. }
  3181. /*
  3182. * Copyright 1996-2006 Catherine Loader.
  3183. */
  3184. /*
  3185. * Routines for computing weight diagrams.
  3186. * wdiag(lf,des,lx,deg,ty,exp)
  3187. * Must locfit() first, unless ker==WPARM and has par. comp.
  3188. *
  3189. */
  3190. #include "locf.h"
  3191. static double *wd;
  3192. extern double robscale;
  3193. void nnresproj(lfd,sp,des,u,m,p)
  3194. lfdata *lfd;
  3195. smpar *sp;
  3196. design *des;
  3197. double *u;
  3198. int m, p;
  3199. { int i, ii, j;
  3200. double link[LLEN];
  3201. setzero(des->f1,p);
  3202. for (j=0; j<m; j++)
  3203. { ii = des->ind[j];
  3204. stdlinks(link,lfd,sp,ii,fitv(des,ii),robscale);
  3205. for (i=0; i<p; i++) des->f1[i] += link[ZDDLL]*d_xij(des,j,ii)*u[j];
  3206. }
  3207. jacob_solve(&des->xtwx,des->f1);
  3208. for (i=0; i<m; i++)
  3209. { ii = des->ind[i];
  3210. u[i] -= innerprod(des->f1,d_xi(des,ii),p)*wght(des,ii);
  3211. }
  3212. }
  3213. void wdexpand(l,n,ind,m)
  3214. double *l;
  3215. int *ind, n, m;
  3216. { int i, j, t;
  3217. double z;
  3218. for (j=m; j<n; j++) { l[j] = 0.0; ind[j] = -1; }
  3219. j = m-1;
  3220. while (j>=0)
  3221. { if (ind[j]==j) j--;
  3222. else
  3223. { i = ind[j];
  3224. z = l[j]; l[j] = l[i]; l[i] = z;
  3225. t = ind[j]; ind[j] = ind[i]; ind[i] = t;
  3226. if (ind[j]==-1) j--;
  3227. }
  3228. }
  3229. /* for (i=n-1; i>=0; i--)
  3230. { l[i] = ((j>=0) && (ind[j]==i)) ? l[j--] : 0.0; } */
  3231. }
  3232. int wdiagp(lfd,sp,des,lx,pc,dv,deg,ty,exp)
  3233. lfdata *lfd;
  3234. smpar *sp;
  3235. design *des;
  3236. paramcomp *pc;
  3237. deriv *dv;
  3238. double *lx;
  3239. int deg, ty, exp;
  3240. { int i, j, p, nd;
  3241. double *l1;
  3242. p = des->p;
  3243. fitfun(lfd,sp,des->xev,pc->xbar,des->f1,dv);
  3244. if (exp)
  3245. { jacob_solve(&pc->xtwx,des->f1);
  3246. for (i=0; i<lfd->n; i++)
  3247. lx[i] = innerprod(des->f1,d_xi(des,des->ind[i]),p);
  3248. return(lfd->n);
  3249. }
  3250. jacob_hsolve(&pc->xtwx,des->f1);
  3251. for (i=0; i<p; i++) lx[i] = des->f1[i];
  3252. nd = dv->nd;
  3253. dv->nd = nd+1;
  3254. if (deg>=1)
  3255. for (i=0; i<lfd->d; i++)
  3256. { dv->deriv[nd] = i;
  3257. l1 = &lx[(i+1)*p];
  3258. fitfun(lfd,sp,des->xev,pc->xbar,l1,dv);
  3259. jacob_hsolve(&pc->xtwx,l1);
  3260. }
  3261. dv->nd = nd+2;
  3262. if (deg>=2)
  3263. for (i=0; i<lfd->d; i++)
  3264. { dv->deriv[nd] = i;
  3265. for (j=0; j<lfd->d; j++)
  3266. { dv->deriv[nd+1] = j;
  3267. l1 = &lx[(i*lfd->d+j+lfd->d+1)*p];
  3268. fitfun(lfd,sp,des->xev,pc->xbar,l1,dv);
  3269. jacob_hsolve(&pc->xtwx,l1);
  3270. } }
  3271. dv->nd = nd;
  3272. return(p);
  3273. }
  3274. int wdiag(lfd,sp,des,lx,dv,deg,ty,exp)
  3275. lfdata *lfd;
  3276. smpar *sp;
  3277. design *des;
  3278. deriv *dv;
  3279. double *lx;
  3280. int deg, ty, exp;
  3281. /* deg=0: l(x) only.
  3282. deg=1: l(x), l'(x)
  3283. deg=2: l(x), l'(x), l''(x)
  3284. ty = 1: e1 (X^T WVX)^{-1} X^T W -- hat matrix
  3285. ty = 2: e1 (X^T WVX)^{-1} X^T WV^{1/2} -- scb's
  3286. */
  3287. { double w, *X, *lxd, *lxdd, wdd, wdw, *ulx, link[LLEN], h;
  3288. double dfx[MXDIM], hs[MXDIM];
  3289. int i, ii, j, k, l, m, d, p, nd;
  3290. h = des->h;
  3291. nd = dv->nd;
  3292. wd = des->wd;
  3293. d = lfd->d; p = des->p; X = d_x(des);
  3294. ulx = des->res;
  3295. m = des->n;
  3296. for (i=0; i<d; i++) hs[i] = h*lfd->sca[i];
  3297. if (deg>0)
  3298. { lxd = &lx[m];
  3299. setzero(lxd,m*d);
  3300. if (deg>1)
  3301. { lxdd = &lxd[d*m];
  3302. setzero(lxdd,m*d*d);
  3303. } }
  3304. if (nd>0) fitfun(lfd,sp,des->xev,des->xev,des->f1,dv); /* c(0) */
  3305. else unitvec(des->f1,0,p);
  3306. jacob_solve(&des->xtwx,des->f1); /* c(0) (X^TWX)^{-1} */
  3307. for (i=0; i<m; i++)
  3308. { ii = des->ind[i];
  3309. lx[i] = innerprod(des->f1,&X[ii*p],p); /* c(0)(XTWX)^{-1}X^T */
  3310. if (deg>0)
  3311. { wd[i] = Wd(dist(des,ii)/h,ker(sp));
  3312. for (j=0; j<d; j++)
  3313. { dfx[j] = datum(lfd,j,ii)-des->xev[j];
  3314. lxd[j*m+i] = lx[i]*wght(des,ii)*weightd(dfx[j],lfd->sca[j],
  3315. d,ker(sp),kt(sp),h,lfd->sty[j],dist(des,ii));
  3316. /* c(0) (XTWX)^{-1}XTW' */
  3317. }
  3318. if (deg>1)
  3319. { wdd = Wdd(dist(des,ii)/h,ker(sp));
  3320. for (j=0; j<d; j++)
  3321. for (k=0; k<d; k++)
  3322. { w = (dist(des,ii)==0) ? 0 : h/dist(des,ii);
  3323. w = wdd * (des->xev[k]-datum(lfd,k,ii)) * (des->xev[j]-datum(lfd,j,ii))
  3324. * w*w / (hs[k]*hs[k]*hs[j]*hs[j]);
  3325. if (j==k) w += wd[i]/(hs[j]*hs[j]);
  3326. lxdd[(j*d+k)*m+i] = lx[i]*w;
  3327. /* c(0)(XTWX)^{-1}XTW'' */
  3328. }
  3329. }
  3330. }
  3331. lx[i] *= wght(des,ii);
  3332. }
  3333. dv->nd = nd+1;
  3334. if (deg==2)
  3335. { for (i=0; i<d; i++)
  3336. { dv->deriv[nd] = i;
  3337. fitfun(lfd,sp,des->xev,des->xev,des->f1,dv);
  3338. for (k=0; k<m; k++)
  3339. { ii = des->ind[i];
  3340. stdlinks(link,lfd,sp,ii,fitv(des,ii),robscale);
  3341. for (j=0; j<p; j++)
  3342. des->f1[j] -= link[ZDDLL]*lxd[i*m+k]*X[ii*p+j];
  3343. /* c'(x)-c(x)(XTWX)^{-1}XTW'X */
  3344. }
  3345. jacob_solve(&des->xtwx,des->f1); /* (...)(XTWX)^{-1} */
  3346. for (j=0; j<m; j++)
  3347. { ii = des->ind[j];
  3348. ulx[j] = innerprod(des->f1,&X[ii*p],p); /* (...)XT */
  3349. }
  3350. for (j=0; j<d; j++)
  3351. for (k=0; k<m; k++)
  3352. { ii = des->ind[k];
  3353. dfx[j] = datum(lfd,j,ii)-des->xev[j];
  3354. wdw = wght(des,ii)*weightd(dfx[j],lfd->sca[j],d,ker(sp),
  3355. kt(sp),h,lfd->sty[j],dist(des,ii));
  3356. lxdd[(i*d+j)*m+k] += ulx[k]*wdw;
  3357. lxdd[(j*d+i)*m+k] += ulx[k]*wdw;
  3358. } /* + 2(c'-c(XTWX)^{-1}XTW'X)(XTWX)^{-1}XTW' */
  3359. }
  3360. for (j=0; j<d*d; j++) nnresproj(lfd,sp,des,&lxdd[j*m],m,p);
  3361. /* * (I-X(XTWX)^{-1} XTW */
  3362. }
  3363. if (deg>0)
  3364. { for (j=0; j<d; j++) nnresproj(lfd,sp,des,&lxd[j*m],m,p);
  3365. /* c(0)(XTWX)^{-1}XTW'(I-X(XTWX)^{-1}XTW) */
  3366. for (i=0; i<d; i++)
  3367. { dv->deriv[nd]=i;
  3368. fitfun(lfd,sp,des->xev,des->xev,des->f1,dv);
  3369. jacob_solve(&des->xtwx,des->f1);
  3370. for (k=0; k<m; k++)
  3371. { ii = des->ind[k];
  3372. for (l=0; l<p; l++)
  3373. lxd[i*m+k] += des->f1[l]*X[ii*p+l]*wght(des,ii);
  3374. } /* add c'(0)(XTWX)^{-1}XTW */
  3375. }
  3376. }
  3377. dv->nd = nd+2;
  3378. if (deg==2)
  3379. { for (i=0; i<d; i++)
  3380. { dv->deriv[nd]=i;
  3381. for (j=0; j<d; j++)
  3382. { dv->deriv[nd+1]=j;
  3383. fitfun(lfd,sp,des->xev,des->xev,des->f1,dv);
  3384. jacob_solve(&des->xtwx,des->f1);
  3385. for (k=0; k<m; k++)
  3386. { ii = des->ind[k];
  3387. for (l=0; l<p; l++)
  3388. lxdd[(i*d+j)*m+k] += des->f1[l]*X[ii*p+l]*wght(des,ii);
  3389. } /* + c''(x)(XTWX)^{-1}XTW */
  3390. }
  3391. }
  3392. }
  3393. dv->nd = nd;
  3394. k = 1+d*(deg>0)+d*d*(deg==2);
  3395. if (exp) wdexpand(lx,lfd->n,des->ind,m);
  3396. if (ty==1) return(m);
  3397. for (i=0; i<m; i++)
  3398. { ii = des->ind[i];
  3399. stdlinks(link,lfd,sp,ii,fitv(des,ii),robscale);
  3400. link[ZDDLL] = sqrt(fabs(link[ZDDLL]));
  3401. for (j=0; j<k; j++) lx[j*m+i] *= link[ZDDLL];
  3402. }
  3403. return(m);
  3404. }
  3405. /*
  3406. * Copyright 1996-2006 Catherine Loader.
  3407. */
  3408. /*
  3409. * String matching functions. For a given argument string, find
  3410. * the best match from an array of possibilities. Is there a library
  3411. * function somewhere to do something like this?
  3412. *
  3413. * return values of -1 indicate failure/unknown string.
  3414. */
  3415. #include "locf.h"
  3416. int ct_match(z1, z2)
  3417. char *z1, *z2;
  3418. { int ct = 0;
  3419. while (z1[ct]==z2[ct])
  3420. { if (z1[ct]=='\0') return(ct+1);
  3421. ct++;
  3422. }
  3423. return(ct);
  3424. }
  3425. int pmatch(z, strings, vals, n, def)
  3426. char *z, **strings;
  3427. int *vals, n, def;
  3428. { int i, ct, best, best_ct;
  3429. best = -1;
  3430. best_ct = 0;
  3431. for (i=0; i<n; i++)
  3432. { ct = ct_match(z,strings[i]);
  3433. if (ct==strlen(z)+1) return(vals[i]);
  3434. if (ct>best_ct) { best = i; best_ct = ct; }
  3435. }
  3436. if (best==-1) return(def);
  3437. return(vals[best]);
  3438. }
  3439. /*
  3440. * Copyright 1996-2006 Catherine Loader.
  3441. */
  3442. #include "locf.h"
  3443. int lf_maxit = 20;
  3444. int lf_debug = 0;
  3445. int lf_error = 0;
  3446. double s0, s1;
  3447. static lfdata *lf_lfd;
  3448. static design *lf_des;
  3449. static smpar *lf_sp;
  3450. int lf_status;
  3451. int ident=0;
  3452. double lf_tol;
  3453. extern double robscale;
  3454. void lfdata_init(lfd)
  3455. lfdata *lfd;
  3456. { int i;
  3457. for (i=0; i<MXDIM; i++)
  3458. { lfd->sty[i] = 0;
  3459. lfd->sca[i] = 1.0;
  3460. lfd->xl[i] = lfd->xl[i+MXDIM] = 0.0;
  3461. }
  3462. lfd->y = lfd->w = lfd->c = lfd->b = NULL;
  3463. lfd->d = lfd->n = 0;
  3464. }
  3465. void smpar_init(sp,lfd)
  3466. smpar *sp;
  3467. lfdata *lfd;
  3468. { nn(sp) = 0.7;
  3469. fixh(sp)= 0.0;
  3470. pen(sp) = 0.0;
  3471. acri(sp)= ANONE;
  3472. deg(sp) = deg0(sp) = 2;
  3473. ubas(sp) = 0;
  3474. kt(sp) = KSPH;
  3475. ker(sp) = WTCUB;
  3476. fam(sp) = 64+TGAUS;
  3477. link(sp)= LDEFAU;
  3478. npar(sp) = calcp(sp,lfd->d);
  3479. }
  3480. void deriv_init(dv)
  3481. deriv *dv;
  3482. { dv->nd = 0;
  3483. }
  3484. int des_reqd(n,p)
  3485. int n, p;
  3486. {
  3487. return(n*(p+5)+2*p*p+4*p + jac_reqd(p));
  3488. }
  3489. int des_reqi(n,p)
  3490. int n, p;
  3491. { return(n+p);
  3492. }
  3493. void des_init(des,n,p)
  3494. design *des;
  3495. int n, p;
  3496. { double *z;
  3497. int k;
  3498. if (n<=0) WARN(("des_init: n <= 0"));
  3499. if (p<=0) WARN(("des_init: p <= 0"));
  3500. if (des->des_init_id != DES_INIT_ID)
  3501. { des->lwk = des->lind = 0;
  3502. des->des_init_id = DES_INIT_ID;
  3503. }
  3504. k = des_reqd(n,p);
  3505. if (k>des->lwk)
  3506. { des->wk = (double *)calloc(k,sizeof(double));
  3507. if ( des->wk == NULL ) {
  3508. printf("Problem allocating memory for des->wk\n");fflush(stdout);
  3509. }
  3510. des->lwk = k;
  3511. }
  3512. z = des->wk;
  3513. des->X = z; z += n*p;
  3514. des->w = z; z += n;
  3515. des->res=z; z += n;
  3516. des->di =z; z += n;
  3517. des->th =z; z += n;
  3518. des->wd =z; z += n;
  3519. des->V =z; z += p*p;
  3520. des->P =z; z += p*p;
  3521. des->f1 =z; z += p;
  3522. des->ss =z; z += p;
  3523. des->oc =z; z += p;
  3524. des->cf =z; z += p;
  3525. z = jac_alloc(&des->xtwx,p,z);
  3526. k = des_reqi(n,p);
  3527. if (k>des->lind)
  3528. {
  3529. des->ind = (int *)calloc(k,sizeof(int));
  3530. if ( des->ind == NULL ) {
  3531. printf("Problem allocating memory for des->ind\n");fflush(stdout);
  3532. }
  3533. des->lind = k;
  3534. }
  3535. des->fix = &des->ind[n];
  3536. for (k=0; k<p; k++) des->fix[k] = 0;
  3537. des->n = n; des->p = p;
  3538. des->smwt = n;
  3539. des->xtwx.p = p;
  3540. }
  3541. void deschk(des,n,p)
  3542. design *des;
  3543. int n, p;
  3544. { WARN(("deschk deprecated - use des_init()"));
  3545. des_init(des,n,p);
  3546. }
  3547. int likereg(coef, lk0, f1, Z)
  3548. double *coef, *lk0, *f1, *Z;
  3549. { int i, ii, j, p;
  3550. double lk, ww, link[LLEN], *X;
  3551. if (lf_debug>2) mut_printf(" likereg: %8.5f\n",coef[0]);
  3552. lf_status = LF_OK;
  3553. lk = 0.0; p = lf_des->p;
  3554. setzero(Z,p*p);
  3555. setzero(f1,p);
  3556. for (i=0; i<lf_des->n; i++)
  3557. {
  3558. ii = lf_des->ind[i];
  3559. X = d_xi(lf_des,ii);
  3560. fitv(lf_des,ii) = base(lf_lfd,ii)+innerprod(coef,X,p);
  3561. lf_status = stdlinks(link,lf_lfd,lf_sp,ii,fitv(lf_des,ii),robscale);
  3562. if (lf_status == LF_BADP)
  3563. { *lk0 = -1.0e300;
  3564. return(NR_REDUCE);
  3565. }
  3566. if (lf_error) lf_status = LF_ERR;
  3567. if (lf_status != LF_OK) return(NR_BREAK);
  3568. ww = wght(lf_des,ii);
  3569. lk += ww*link[ZLIK];
  3570. for (j=0; j<p; j++)
  3571. f1[j] += X[j]*ww*link[ZDLL];
  3572. addouter(Z, X, X, p, ww*link[ZDDLL]);
  3573. }
  3574. for (i=0; i<p; i++) if (lf_des->fix[i])
  3575. { for (j=0; j<p; j++) Z[i*p+j] = Z[j*p+i] = 0.0;
  3576. Z[i*p+i] = 1.0;
  3577. f1[i] = 0.0;
  3578. }
  3579. if (lf_debug>4) prresp(coef,Z,p);
  3580. if (lf_debug>3) mut_printf(" likelihood: %8.5f\n",lk);
  3581. *lk0 = lf_des->llk = lk;
  3582. lf_status = fami(lf_sp)->pcheck(lf_sp,lf_des,lf_lfd);
  3583. switch(lf_status)
  3584. { case LF_DONE: return(NR_BREAK);
  3585. case LF_OOB: return(NR_REDUCE);
  3586. case LF_PF: return(NR_REDUCE);
  3587. case LF_NSLN: return(NR_BREAK);
  3588. }
  3589. return(NR_OK);
  3590. }
  3591. int reginit(lfd,des,sp)
  3592. lfdata *lfd;
  3593. design *des;
  3594. smpar *sp;
  3595. { int i, ii;
  3596. double sb, link[LLEN];
  3597. s0 = s1 = sb = 0;
  3598. for (i=0; i<des->n; i++)
  3599. { ii = des->ind[i];
  3600. links(base(lfd,ii),resp(lfd,ii),fami(sp),LINIT,link,cens(lfd,ii),prwt(lfd,ii),1.0);
  3601. s1 += wght(des,ii)*link[ZDLL];
  3602. s0 += wght(des,ii)*prwt(lfd,ii);
  3603. sb += wght(des,ii)*prwt(lfd,ii)*base(lfd,ii);
  3604. }
  3605. if (s0==0) return(LF_NOPT); /* no observations with W>0 */
  3606. setzero(des->cf,des->p);
  3607. lf_tol = 1.0e-6*s0;
  3608. switch(link(sp))
  3609. { case LIDENT:
  3610. des->cf[0] = (s1-sb)/s0;
  3611. return(LF_OK);
  3612. case LLOG:
  3613. if (s1<=0.0)
  3614. { des->cf[0] = -1000;
  3615. return(LF_INFA);
  3616. }
  3617. des->cf[0] = log(s1/s0) - sb/s0;
  3618. return(LF_OK);
  3619. case LLOGIT:
  3620. if (s1<=0.0)
  3621. { des->cf[0] = -1000;
  3622. return(LF_INFA);
  3623. }
  3624. if (s1>=s0)
  3625. { des->cf[0] = 1000;
  3626. return(LF_INFA);
  3627. }
  3628. des->cf[0] = logit(s1/s0)-sb/s0;
  3629. return(LF_OK);
  3630. case LINVER:
  3631. if (s1<=0.0)
  3632. { des->cf[0] = 1e100;
  3633. return(LF_INFA);
  3634. }
  3635. des->cf[0] = s0/s1-sb/s0;
  3636. return(LF_OK);
  3637. case LSQRT:
  3638. des->cf[0] = sqrt(s1/s0)-sb/s0;
  3639. return(LF_OK);
  3640. case LASIN:
  3641. des->cf[0] = asin(sqrt(s1/s0))-sb/s0;
  3642. return(LF_OK);
  3643. default:
  3644. LERR(("reginit: invalid link %d",link(sp)));
  3645. return(LF_ERR);
  3646. }
  3647. }
  3648. int lfinit(lfd,sp,des)
  3649. lfdata *lfd;
  3650. smpar *sp;
  3651. design *des;
  3652. { int initstat;
  3653. des->xtwx.sm = (deg0(sp)<deg(sp)) ? JAC_CHOL : JAC_EIGD;
  3654. designmatrix(lfd,sp,des);
  3655. setfamily(sp);
  3656. initstat = fami(sp)->initial(lfd,des,sp);
  3657. return(initstat);
  3658. }
  3659. void lfiter(lfd,sp,des,maxit)
  3660. lfdata *lfd;
  3661. smpar *sp;
  3662. design *des;
  3663. int maxit;
  3664. { int err;
  3665. if (lf_debug>1) mut_printf(" lfiter: %8.5f\n",des->cf[0]);
  3666. lf_des = des;
  3667. lf_lfd = lfd;
  3668. lf_sp = sp;
  3669. max_nr(fami(sp)->like, des->cf, des->oc, des->res, des->f1,
  3670. &des->xtwx, des->p, maxit, lf_tol, &err);
  3671. switch(err)
  3672. { case NR_OK: return;
  3673. case NR_NCON:
  3674. WARN(("max_nr not converged"));
  3675. return;
  3676. case NR_NDIV:
  3677. WARN(("max_nr reduction problem"));
  3678. return;
  3679. }
  3680. WARN(("max_nr return status %d",err));
  3681. }
  3682. int use_robust_scale(int tg)
  3683. { if ((tg&64)==0) return(0); /* not quasi - no scale */
  3684. if (((tg&128)==0) & (((tg&63)!=TROBT) & ((tg&63)!=TCAUC))) return(0);
  3685. return(1);
  3686. }
  3687. /*
  3688. * noit not really needed any more, since
  3689. * gauss->pcheck returns LF_DONE, and likereg NR_BREAK
  3690. * in gaussian case.
  3691. * nb: 0/1: does local neighborhood and weights need computing?
  3692. * cv: 0/1: is variance/covariance matrix needed?
  3693. */
  3694. int locfit(lfd,des,sp,noit,nb,cv)
  3695. lfdata *lfd;
  3696. design *des;
  3697. smpar *sp;
  3698. int noit, nb, cv;
  3699. { int i;
  3700. if (des->xev==NULL)
  3701. { LERR(("locfit: NULL evaluation point?"));
  3702. return(246);
  3703. }
  3704. if (lf_debug>0)
  3705. { mut_printf("locfit: ");
  3706. for (i=0; i<lfd->d; i++) mut_printf(" %10.6f",des->xev[i]);
  3707. mut_printf("\n");
  3708. }
  3709. /* the 1e-12 avoids problems that can occur with roundoff */
  3710. if (nb) nbhd(lfd,des,(int)(lfd->n*nn(sp)+1e-12),0,sp);
  3711. lf_status = lfinit(lfd,sp,des);
  3712. if (lf_status == LF_OK)
  3713. { if (use_robust_scale(fam(sp)))
  3714. lf_robust(lfd,sp,des,lf_maxit);
  3715. else
  3716. { if ((fam(sp)&63)==TQUANT)
  3717. lfquantile(lfd,sp,des,lf_maxit);
  3718. else
  3719. { robscale = 1.0;
  3720. lfiter(lfd,sp,des,lf_maxit);
  3721. }
  3722. }
  3723. }
  3724. if (lf_status == LF_DONE) lf_status = LF_OK;
  3725. if (lf_status == LF_OOB) lf_status = LF_OK;
  3726. if ((fam(sp)&63)==TDEN) /* convert from rate to density */
  3727. { switch(link(sp))
  3728. { case LLOG:
  3729. des->cf[0] -= log(des->smwt);
  3730. break;
  3731. case LIDENT:
  3732. multmatscal(des->cf,1.0/des->smwt,des->p);
  3733. break;
  3734. default: LERR(("Density adjustment; invalid link"));
  3735. }
  3736. }
  3737. /* variance calculations, if requested */
  3738. if (cv)
  3739. { switch(lf_status)
  3740. { case LF_PF: /* for these cases, variance calc. would likely fail. */
  3741. case LF_NOPT:
  3742. case LF_NSLN:
  3743. case LF_INFA:
  3744. case LF_DEMP:
  3745. case LF_XOOR:
  3746. case LF_DNOP:
  3747. case LF_BADP:
  3748. des->llk = des->tr0 = des->tr1 = des->tr2 = 0.0;
  3749. setzero(des->V,des->p*des->p);
  3750. setzero(des->f1,des->p);
  3751. break;
  3752. default: lf_vcov(lfd,sp,des);
  3753. }
  3754. }
  3755. return(lf_status);
  3756. }
  3757. void lf_status_msg(status)
  3758. int status;
  3759. { switch(status)
  3760. { case LF_OK: return;
  3761. case LF_NCON: WARN(("locfit did not converge")); return;
  3762. case LF_OOB: WARN(("parameters out of bounds")); return;
  3763. case LF_PF: WARN(("perfect fit")); return;
  3764. case LF_NOPT: WARN(("no points with non-zero weight")); return;
  3765. case LF_NSLN: WARN(("no solution")); return;
  3766. case LF_INFA: WARN(("initial value problem")); return;
  3767. case LF_DEMP: WARN(("density estimate, empty integration region")); return;
  3768. case LF_XOOR: WARN(("procv: fit point outside xlim region")); return;
  3769. case LF_DNOP: WARN(("density estimation -- insufficient points in smoothing window")); return;
  3770. case LF_BADP: WARN(("bad parameters")); return;
  3771. default: WARN(("procv: unknown return code %d",status)); return;
  3772. } }
  3773. /*
  3774. * Copyright 1996-2006 Catherine Loader.
  3775. */
  3776. /*
  3777. * Compute minimax weights for local regression.
  3778. */
  3779. #include "locf.h"
  3780. #define NR_EMPTY 834
  3781. int mmsm_ct;
  3782. static int debug=0;
  3783. #define CONVTOL 1.0e-8
  3784. #define SINGTOL 1.0e-10
  3785. #define NR_SINGULAR 100
  3786. static lfdata *mm_lfd;
  3787. static design *mm_des;
  3788. static double mm_gam, mmf, lb;
  3789. static int st;
  3790. double ipower(x,n) /* use for n not too large!! */
  3791. double x;
  3792. int n;
  3793. { if (n==0) return(1.0);
  3794. if (n<0) return(1/ipower(x,-n));
  3795. return(x*ipower(x,n-1));
  3796. }
  3797. double setmmwt(des,a,gam)
  3798. design *des;
  3799. double *a, gam;
  3800. { double ip, w0, w1, sw, wt;
  3801. int i;
  3802. sw = 0.0;
  3803. for (i=0; i<mm_lfd->n; i++)
  3804. { ip = innerprod(a,d_xi(des,i),des->p);
  3805. wt = prwt(mm_lfd,i);
  3806. w0 = ip - gam*des->wd[i];
  3807. w1 = ip + gam*des->wd[i];
  3808. wght(des,i) = 0.0;
  3809. if (w0>0) { wght(des,i) = w0; sw += wt*w0*w0; }
  3810. if (w1<0) { wght(des,i) = w1; sw += wt*w1*w1; }
  3811. }
  3812. return(sw/2-a[0]);
  3813. }
  3814. /* compute sum_{w!=0} AA^T; e1-sum wA */
  3815. int mmsums(des,coef,f,z,J)
  3816. design *des;
  3817. double *coef, *f, *z;
  3818. jacobian *J;
  3819. { int ct, i, j, p, sing;
  3820. double *A;
  3821. mmsm_ct++;
  3822. A = J->Z;
  3823. *f = setmmwt(des,coef,mm_gam);
  3824. p = des->p;
  3825. setzero(A,p*p);
  3826. setzero(z,p);
  3827. z[0] = 1.0;
  3828. ct = 0;
  3829. for (i=0; i<mm_lfd->n; i++)
  3830. if (wght(des,i)!=0.0)
  3831. { addouter(A,d_xi(des,i),d_xi(des,i),p,prwt(mm_lfd,i));
  3832. for (j=0; j<p; j++) z[j] -= prwt(mm_lfd,i)*wght(des,i)*d_xij(des,i,j);
  3833. ct++;
  3834. }
  3835. if (ct==0) return(NR_EMPTY);
  3836. J->st = JAC_RAW;
  3837. J->p = p;
  3838. jacob_dec(J,JAC_EIGD);
  3839. sing = 0;
  3840. for (i=0; i<p; i++) sing |= (J->Z[i*p+i]<SINGTOL);
  3841. if ((debug) & (sing)) mut_printf("SINGULAR!!!!\n");
  3842. return((sing) ? NR_SINGULAR : NR_OK);
  3843. }
  3844. int descenddir(des,coef,dlt,f,af)
  3845. design *des;
  3846. double *coef, *dlt, *f;
  3847. int af;
  3848. { int i, p;
  3849. double f0, *oc;
  3850. if (debug) mut_printf("descenddir: %8.5f %8.5f\n",dlt[0],dlt[1]);
  3851. f0 = *f;
  3852. oc = des->oc;
  3853. p = des->p;
  3854. memcpy(oc,coef,p*sizeof(double));
  3855. for (i=0; i<p; i++) coef[i] = oc[i]+lb*dlt[i];
  3856. st = mmsums(des,coef,f,des->f1,&des->xtwx);
  3857. if (*f>f0) /* halve till we drop */
  3858. { while (*f>f0)
  3859. { lb = lb/2.0;
  3860. for (i=0; i<p; i++) coef[i] = oc[i]+lb*dlt[i];
  3861. st = mmsums(des,coef,f,des->f1,&des->xtwx);
  3862. }
  3863. return(st);
  3864. }
  3865. if (!af) return(st);
  3866. /* double */
  3867. while (*f<f0)
  3868. { f0 = *f;
  3869. lb *= 2.0;
  3870. for (i=0; i<p; i++) coef[i] = oc[i]+lb*dlt[i];
  3871. st = mmsums(des,coef,f,des->f1,&des->xtwx);
  3872. }
  3873. lb /= 2.0;
  3874. for (i=0; i<p; i++) coef[i] = oc[i]+lb*dlt[i];
  3875. st = mmsums(des,coef,f,des->f1,&des->xtwx);
  3876. return(st);
  3877. }
  3878. int mm_initial(des)
  3879. design *des;
  3880. { double *dlt;
  3881. dlt = des->ss;
  3882. setzero(des->cf,des->p);
  3883. st = mmsums(des,des->cf,&mmf,des->f1,&des->xtwx);
  3884. setzero(dlt,des->p);
  3885. dlt[0] = 1;
  3886. lb = 1.0;
  3887. st = descenddir(des,des->cf,dlt,&mmf,1);
  3888. return(st);
  3889. }
  3890. void getsingdir(des,dlt)
  3891. design *des;
  3892. double *dlt;
  3893. { double f, sw, c0;
  3894. int i, j, p, sd;
  3895. sd = -1; p = des->p;
  3896. setzero(dlt,p);
  3897. for (i=0; i<p; i++) if (des->xtwx.Z[i*p+i]<SINGTOL) sd = i;
  3898. if (sd==-1)
  3899. { mut_printf("getsingdir: nonsing?\n");
  3900. return;
  3901. }
  3902. if (des->xtwx.dg[sd]>0)
  3903. for (i=0; i<p; i++) dlt[i] = des->xtwx.Q[p*i+sd]*des->xtwx.dg[i];
  3904. else
  3905. { dlt[sd] = 1.0;
  3906. }
  3907. c0 = innerprod(dlt,des->f1,p);
  3908. if (c0<0) for (i=0; i<p; i++) dlt[i] = -dlt[i];
  3909. }
  3910. void mmax(coef, old_coef, delta, J, p, maxit, tol, err)
  3911. double *coef, *old_coef, *delta, tol;
  3912. int p, maxit, *err;
  3913. jacobian *J;
  3914. { double old_f, lambda;
  3915. int i, j;
  3916. *err = NR_OK;
  3917. for (j=0; j<maxit; j++)
  3918. { memcpy(old_coef,coef,p*sizeof(double));
  3919. old_f = mmf;
  3920. if (st == NR_SINGULAR)
  3921. {
  3922. getsingdir(mm_des,delta);
  3923. st = descenddir(mm_des,coef,delta,&mmf,1);
  3924. }
  3925. if (st == NR_EMPTY)
  3926. {
  3927. setzero(delta,p);
  3928. delta[0] = 1.0;
  3929. st = descenddir(mm_des,coef,delta,&mmf,1);
  3930. }
  3931. if (st == NR_OK)
  3932. {
  3933. lb = 1.0;
  3934. jacob_solve(J,mm_des->f1);
  3935. memcpy(delta,mm_des->f1,p*sizeof(double));
  3936. st = descenddir(mm_des,coef,delta,&mmf,0);
  3937. }
  3938. if ((j>0) & (fabs(mmf-old_f)<tol)) return;
  3939. }
  3940. WARN(("findab not converged"));
  3941. *err = NR_NCON;
  3942. return;
  3943. }
  3944. double findab(gam)
  3945. double gam;
  3946. { double sl;
  3947. int i, p, nr_stat;
  3948. if (debug) mut_printf(" findab: gam %8.5f\n",gam);
  3949. mm_gam = gam;
  3950. p = mm_des->p;
  3951. lb = 1.0;
  3952. st = mm_initial(mm_des);
  3953. mmax(mm_des->cf, mm_des->oc, mm_des->ss,
  3954. &mm_des->xtwx, p, lf_maxit, CONVTOL, &nr_stat);
  3955. sl = 0.0;
  3956. for (i=0; i<mm_lfd->n; i++) sl += fabs(wght(mm_des,i))*mm_des->wd[i];
  3957. if (debug) mut_printf(" sl %8.5f gam %8.5f %8.5f %d\n", sl,gam,sl-gam,nr_stat);
  3958. return(sl-gam);
  3959. }
  3960. double weightmm(coef,di,ff,gam)
  3961. double *coef, di, *ff, gam;
  3962. { double y1, y2, ip;
  3963. ip = innerprod(ff,coef,mm_des->p);
  3964. y1 = ip-gam*di; if (y1>0) return(y1/ip);
  3965. y2 = ip+gam*di; if (y2<0) return(y2/ip);
  3966. return(0.0);
  3967. }
  3968. double minmax(lfd,des,sp)
  3969. lfdata *lfd;
  3970. design *des;
  3971. smpar *sp;
  3972. { double h, u[MXDIM], gam;
  3973. int i, j, m, d1, p1, err_flag;
  3974. if (debug) mut_printf("minmax: x %8.5f\n",des->xev[0]);
  3975. mm_lfd = lfd;
  3976. mm_des = des;
  3977. mmsm_ct = 0;
  3978. d1 = deg(sp)+1;
  3979. p1 = factorial(d1);
  3980. for (i=0; i<lfd->n; i++)
  3981. { for (j=0; j<lfd->d; j++) u[j] = datum(lfd,j,i);
  3982. des->wd[i] = sp->nn/p1*ipower(dist(des,i),d1);
  3983. des->ind[i] = i;
  3984. fitfun(lfd, sp, u,des->xev,d_xi(des,i),NULL);
  3985. }
  3986. /* find gamma (i.e. solve eqn 13.17 from book), using the secant method.
  3987. * As a side effect, this finds the other minimax coefficients.
  3988. * Note that 13.17 is rewritten as
  3989. * g2 = sum |l_i(x)| (||xi-x||^(p+1) M/(s*(p+1)!))
  3990. * where g2 = gamma * s * (p+1)! / M. The gam variable below is g2.
  3991. * The smoothing parameter is sp->nn == M/s.
  3992. */
  3993. gam = solve_secant(findab, 0.0, 0.0,1.0, 0.0000001, BDF_EXPRIGHT, &err_flag);
  3994. /*
  3995. * Set the smoothing weights, in preparation for the actual fit.
  3996. */
  3997. h = 0.0; m = 0;
  3998. for (i=0; i<lfd->n; i++)
  3999. { wght(des,i) = weightmm(des->cf, des->wd[i],d_xi(des,i),gam);
  4000. if (wght(des,i)>0)
  4001. { if (dist(des,i)>h) h = dist(des,i);
  4002. des->ind[m] = i;
  4003. m++;
  4004. }
  4005. }
  4006. des->n = m;
  4007. return(h);
  4008. }
  4009. /*
  4010. * Copyright 1996-2006 Catherine Loader.
  4011. */
  4012. /*
  4013. *
  4014. * Defines the weight functions and related quantities used
  4015. * in LOCFIT.
  4016. */
  4017. #include "locf.h"
  4018. /*
  4019. * convert kernel and kernel type strings to numeric codes.
  4020. */
  4021. #define NWFUNS 13
  4022. static char *wfuns[NWFUNS] = {
  4023. "rectangular", "epanechnikov", "bisquare", "tricube",
  4024. "triweight", "gaussian", "triangular", "ququ",
  4025. "6cub", "minimax", "exponential", "maclean", "parametric" };
  4026. static int wvals[NWFUNS] = { WRECT, WEPAN, WBISQ, WTCUB,
  4027. WTRWT, WGAUS, WTRIA, WQUQU, W6CUB, WMINM, WEXPL, WMACL, WPARM };
  4028. int lfkernel(char *z)
  4029. { return(pmatch(z, wfuns, wvals, NWFUNS, WTCUB));
  4030. }
  4031. #define NKTYPE 5
  4032. static char *ktype[NKTYPE] = { "spherical", "product", "center", "lm", "zeon" };
  4033. static int kvals[NKTYPE] = { KSPH, KPROD, KCE, KLM, KZEON };
  4034. int lfketype(char *z)
  4035. { return(pmatch(z, ktype, kvals, NKTYPE, KSPH));
  4036. }
  4037. /* The weight functions themselves. Used everywhere. */
  4038. double W(u,ker)
  4039. double u;
  4040. int ker;
  4041. { u = fabs(u);
  4042. switch(ker)
  4043. { case WRECT: return((u>1) ? 0.0 : 1.0);
  4044. case WEPAN: return((u>1) ? 0.0 : 1-u*u);
  4045. case WBISQ: if (u>1) return(0.0);
  4046. u = 1-u*u; return(u*u);
  4047. case WTCUB: if (u>1) return(0.0);
  4048. u = 1-u*u*u; return(u*u*u);
  4049. case WTRWT: if (u>1) return(0.0);
  4050. u = 1-u*u; return(u*u*u);
  4051. case WQUQU: if (u>1) return(0.0);
  4052. u = 1-u*u; return(u*u*u*u);
  4053. case WTRIA: if (u>1) return(0.0);
  4054. return(1-u);
  4055. case W6CUB: if (u>1) return(0.0);
  4056. u = 1-u*u*u; u = u*u*u; return(u*u);
  4057. case WGAUS: return(exp(-SQR(GFACT*u)/2.0));
  4058. case WEXPL: return(exp(-EFACT*u));
  4059. case WMACL: return(1/((u+1.0e-100)*(u+1.0e-100)));
  4060. case WMINM: LERR(("WMINM in W"));
  4061. return(0.0);
  4062. case WPARM: return(1.0);
  4063. }
  4064. LERR(("W(): Unknown kernel %d\n",ker));
  4065. return(1.0);
  4066. }
  4067. int iscompact(ker)
  4068. int ker;
  4069. { if ((ker==WEXPL) | (ker==WGAUS) | (ker==WMACL) | (ker==WPARM)) return(0);
  4070. return(1);
  4071. }
  4072. double weightprod(lfd,u,h,ker)
  4073. lfdata *lfd;
  4074. double *u, h;
  4075. int ker;
  4076. { int i;
  4077. double sc, w;
  4078. w = 1.0;
  4079. for (i=0; i<lfd->d; i++)
  4080. { sc = lfd->sca[i];
  4081. switch(lfd->sty[i])
  4082. { case STLEFT:
  4083. if (u[i]>0) return(0.0);
  4084. w *= W(-u[i]/(h*sc),ker);
  4085. break;
  4086. case STRIGH:
  4087. if (u[i]<0) return(0.0);
  4088. w *= W(u[i]/(h*sc),ker);
  4089. break;
  4090. case STANGL:
  4091. w *= W(2*fabs(sin(u[i]/(2*sc)))/h,ker);
  4092. break;
  4093. case STCPAR:
  4094. break;
  4095. default:
  4096. w *= W(fabs(u[i])/(h*sc),ker);
  4097. }
  4098. if (w==0.0) return(w);
  4099. }
  4100. return(w);
  4101. }
  4102. double weightsph(lfd,u,h,ker, hasdi,di)
  4103. lfdata *lfd;
  4104. double *u, h, di;
  4105. int ker, hasdi;
  4106. { int i;
  4107. if (!hasdi) di = rho(u,lfd->sca,lfd->d,KSPH,lfd->sty);
  4108. for (i=0; i<lfd->d; i++)
  4109. { if ((lfd->sty[i]==STLEFT) && (u[i]>0.0)) return(0.0);
  4110. if ((lfd->sty[i]==STRIGH) && (u[i]<0.0)) return(0.0);
  4111. }
  4112. if (h==0) return((di==0.0) ? 1.0 : 0.0);
  4113. return(W(di/h,ker));
  4114. }
  4115. double weight(lfd,sp,x,t,h, hasdi,di)
  4116. lfdata *lfd;
  4117. smpar *sp;
  4118. double *x, *t, h, di;
  4119. int hasdi;
  4120. { double u[MXDIM];
  4121. int i;
  4122. for (i=0; i<lfd->d; i++) u[i] = (t==NULL) ? x[i] : x[i]-t[i];
  4123. switch(kt(sp))
  4124. { case KPROD: return(weightprod(lfd,u,h,ker(sp)));
  4125. case KSPH: return(weightsph(lfd,u,h,ker(sp), hasdi,di));
  4126. }
  4127. LERR(("weight: unknown kernel type %d",kt(sp)));
  4128. return(1.0);
  4129. }
  4130. double sgn(x)
  4131. double x;
  4132. { if (x>0) return(1.0);
  4133. if (x<0) return(-1.0);
  4134. return(0.0);
  4135. }
  4136. double WdW(u,ker) /* W'(u)/W(u) */
  4137. double u;
  4138. int ker;
  4139. { double eps=1.0e-10;
  4140. if (ker==WGAUS) return(-GFACT*GFACT*u);
  4141. if (ker==WPARM) return(0.0);
  4142. if (fabs(u)>=1) return(0.0);
  4143. switch(ker)
  4144. { case WRECT: return(0.0);
  4145. case WTRIA: return(-sgn(u)/(1-fabs(u)+eps));
  4146. case WEPAN: return(-2*u/(1-u*u+eps));
  4147. case WBISQ: return(-4*u/(1-u*u+eps));
  4148. case WTRWT: return(-6*u/(1-u*u+eps));
  4149. case WTCUB: return(-9*sgn(u)*u*u/(1-u*u*fabs(u)+eps));
  4150. case WEXPL: return((u>0) ? -EFACT : EFACT);
  4151. }
  4152. LERR(("WdW: invalid kernel"));
  4153. return(0.0);
  4154. }
  4155. /* deriv. weights .. spherical, product etc
  4156. u, sc, sty needed only in relevant direction
  4157. Acutally, returns (d/dx W(||x||/h) ) / W(.)
  4158. */
  4159. double weightd(u,sc,d,ker,kt,h,sty,di)
  4160. double u, sc, h, di;
  4161. int d, ker, kt, sty;
  4162. { if (sty==STANGL)
  4163. { if (kt==KPROD)
  4164. return(-WdW(2*sin(u/(2*sc)),ker)*cos(u/(2*sc))/(h*sc));
  4165. if (di==0.0) return(0.0);
  4166. return(-WdW(di/h,ker)*sin(u/sc)/(h*sc*di));
  4167. }
  4168. if (sty==STCPAR) return(0.0);
  4169. if (kt==KPROD)
  4170. return(-WdW(u/(h*sc),ker)/(h*sc));
  4171. if (di==0.0) return(0.0);
  4172. return(-WdW(di/h,ker)*u/(h*di*sc*sc));
  4173. }
  4174. double weightdd(u,sc,d,ker,kt,h,sty,di,i0,i1)
  4175. double *u, *sc, h, di;
  4176. int d, ker, kt, i0, i1, *sty;
  4177. { double w;
  4178. w = 1;
  4179. if (kt==KPROD)
  4180. {
  4181. w = WdW(u[i0]/(h*sc[i0]),ker)*WdW(u[i1]/(h*sc[i1]),ker)/(h*h*sc[i0]*sc[i1]);
  4182. }
  4183. return(0.0);
  4184. }
  4185. /* Derivatives W'(u)/u.
  4186. Used in simult. conf. band computations,
  4187. and kernel density bandwidth selectors. */
  4188. double Wd(u,ker)
  4189. double u;
  4190. int ker;
  4191. { double v;
  4192. if (ker==WGAUS) return(-SQR(GFACT)*exp(-SQR(GFACT*u)/2));
  4193. if (ker==WPARM) return(0.0);
  4194. if (fabs(u)>1) return(0.0);
  4195. switch(ker)
  4196. { case WEPAN: return(-2.0);
  4197. case WBISQ: return(-4*(1-u*u));
  4198. case WTCUB: v = 1-u*u*u;
  4199. return(-9*v*v*u);
  4200. case WTRWT: v = 1-u*u;
  4201. return(-6*v*v);
  4202. default: LERR(("Invalid kernel %d in Wd",ker));
  4203. }
  4204. return(0.0);
  4205. }
  4206. /* Second derivatives W''(u)-W'(u)/u.
  4207. used in simult. conf. band computations in >1 dimension. */
  4208. double Wdd(u,ker)
  4209. double u;
  4210. int ker;
  4211. { double v;
  4212. if (ker==WGAUS) return(SQR(u*GFACT*GFACT)*exp(-SQR(u*GFACT)/2));
  4213. if (ker==WPARM) return(0.0);
  4214. if (u>1) return(0.0);
  4215. switch(ker)
  4216. { case WBISQ: return(12*u*u);
  4217. case WTCUB: v = 1-u*u*u;
  4218. return(-9*u*v*v+54*u*u*u*u*v);
  4219. case WTRWT: return(24*u*u*(1-u*u));
  4220. default: LERR(("Invalid kernel %d in Wdd",ker));
  4221. }
  4222. return(0.0);
  4223. }
  4224. /* int u1^j1..ud^jd W(u) du.
  4225. Used for local log-linear density estimation.
  4226. Assume all j_i are even.
  4227. Also in some bandwidth selection.
  4228. */
  4229. double wint(d,j,nj,ker)
  4230. int d, *j, nj, ker;
  4231. { double I, z;
  4232. int k, dj;
  4233. dj = d;
  4234. for (k=0; k<nj; k++) dj += j[k];
  4235. switch(ker) /* int_0^1 u^(dj-1) W(u)du */
  4236. { case WRECT: I = 1.0/dj; break;
  4237. case WEPAN: I = 2.0/(dj*(dj+2)); break;
  4238. case WBISQ: I = 8.0/(dj*(dj+2)*(dj+4)); break;
  4239. case WTCUB: I = 162.0/(dj*(dj+3)*(dj+6)*(dj+9)); break;
  4240. case WTRWT: I = 48.0/(dj*(dj+2)*(dj+4)*(dj+6)); break;
  4241. case WTRIA: I = 1.0/(dj*(dj+1)); break;
  4242. case WQUQU: I = 384.0/(dj*(dj+2)*(dj+4)*(dj+6)*(dj+8)); break;
  4243. case W6CUB: I = 524880.0/(dj*(dj+3)*(dj+6)*(dj+9)*(dj+12)*(dj+15)*(dj+18)); break;
  4244. case WGAUS: switch(d)
  4245. { case 1: I = S2PI/GFACT; break;
  4246. case 2: I = 2*PI/(GFACT*GFACT); break;
  4247. default: I = exp(d*log(S2PI/GFACT)); /* for nj=0 */
  4248. }
  4249. for (k=0; k<nj; k++) /* deliberate drop */
  4250. switch(j[k])
  4251. { case 4: I *= 3.0/(GFACT*GFACT);
  4252. case 2: I /= GFACT*GFACT;
  4253. }
  4254. return(I);
  4255. case WEXPL: I = factorial(dj-1)/ipower(EFACT,dj); break;
  4256. default: LERR(("Unknown kernel %d in exacint",ker));
  4257. }
  4258. if ((d==1) && (nj==0)) return(2*I); /* common case quick */
  4259. z = (d-nj)*LOGPI/2-mut_lgammai(dj);
  4260. for (k=0; k<nj; k++) z += mut_lgammai(j[k]+1);
  4261. return(2*I*exp(z));
  4262. }
  4263. /* taylor series expansion of weight function around x.
  4264. 0 and 1 are common arguments, so are worth programming
  4265. as special cases.
  4266. Used in density estimation.
  4267. */
  4268. int wtaylor(f,x,ker)
  4269. double *f, x;
  4270. int ker;
  4271. { double v;
  4272. switch(ker)
  4273. { case WRECT:
  4274. f[0] = 1.0;
  4275. return(1);
  4276. case WEPAN:
  4277. f[0] = 1-x*x; f[1] = -2*x; f[2] = -1;
  4278. return(3);
  4279. case WBISQ:
  4280. v = 1-x*x;
  4281. f[0] = v*v; f[1] = -4*x*v; f[2] = 4-6*v;
  4282. f[3] = 4*x; f[4] = 1;
  4283. return(5);
  4284. case WTCUB:
  4285. if (x==1.0)
  4286. { f[0] = f[1] = f[2] = 0; f[3] = -27; f[4] = -81; f[5] = -108;
  4287. f[6] = -81; f[7] = -36; f[8] = -9; f[9] = -1; return(10); }
  4288. if (x==0.0)
  4289. { f[1] = f[2] = f[4] = f[5] = f[7] = f[8] = 0;
  4290. f[0] = 1; f[3] = -3; f[6] = 3; f[9] = -1; return(10); }
  4291. v = 1-x*x*x;
  4292. f[0] = v*v*v; f[1] = -9*v*v*x*x; f[2] = x*v*(27-36*v);
  4293. f[3] = -27+v*(108-84*v); f[4] = -3*x*x*(27-42*v);
  4294. f[5] = x*(-108+126*v); f[6] = -81+84*v;
  4295. f[7] = -36*x*x; f[8] = -9*x; f[9] = -1;
  4296. return(10);
  4297. case WTRWT:
  4298. v = 1-x*x;
  4299. f[0] = v*v*v; f[1] = -6*x*v*v; f[2] = v*(12-15*v);
  4300. f[3] = x*(20*v-8); f[4] = 15*v-12; f[5] = -6; f[6] = -1;
  4301. return(7);
  4302. case WTRIA:
  4303. f[0] = 1-x; f[1] = -1;
  4304. return(2);
  4305. case WQUQU:
  4306. v = 1-x*x;
  4307. f[0] = v*v*v*v; f[1] = -8*x*v*v*v; f[2] = v*v*(24-28*v);
  4308. f[3] = v*x*(56*v-32); f[4] = (70*v-80)*v+16; f[5] = x*(32-56*v);
  4309. f[6] = 24-28*v; f[7] = 8*x; f[8] = 1;
  4310. return(9);
  4311. case W6CUB:
  4312. v = 1-x*x*x;
  4313. f[0] = v*v*v*v*v*v;
  4314. f[1] = -18*x*x*v*v*v*v*v;
  4315. f[2] = x*v*v*v*v*(135-153*v);
  4316. f[3] = v*v*v*(-540+v*(1350-816*v));
  4317. f[4] = x*x*v*v*(1215-v*(4050-v*3060));
  4318. f[5] = x*v*(-1458+v*(9234+v*(-16254+v*8568)));
  4319. f[6] = 729-v*(10206-v*(35154-v*(44226-v*18564)));
  4320. f[7] = x*x*(4374-v*(30132-v*(56862-v*31824)));
  4321. f[8] = x*(12393-v*(61479-v*(92664-v*43758)));
  4322. f[9] = 21870-v*(89100-v*(115830-v*48620));
  4323. f[10]= x*x*(26730-v*(69498-v*43758));
  4324. f[11]= x*(23814-v*(55458-v*31824));
  4325. f[12]= 15849-v*(34398-v*18564);
  4326. f[13]= x*x*(7938-8568*v);
  4327. f[14]= x*(2970-3060*v);
  4328. f[15]= 810-816*v;
  4329. f[16]= 153*x*x;
  4330. f[17]= 18*x;
  4331. f[18]= 1;
  4332. return(19);
  4333. }
  4334. LERR(("Invalid kernel %d in wtaylor",ker));
  4335. return(0);
  4336. }
  4337. /* convolution int W(x)W(x+v)dx.
  4338. used in kde bandwidth selection.
  4339. */
  4340. double Wconv(v,ker)
  4341. double v;
  4342. int ker;
  4343. { double v2;
  4344. switch(ker)
  4345. { case WGAUS: return(SQRPI/GFACT*exp(-SQR(GFACT*v)/4));
  4346. case WRECT:
  4347. v = fabs(v);
  4348. if (v>2) return(0.0);
  4349. return(2-v);
  4350. case WEPAN:
  4351. v = fabs(v);
  4352. if (v>2) return(0.0);
  4353. return((2-v)*(16+v*(8-v*(16-v*(2+v))))/30);
  4354. case WBISQ:
  4355. v = fabs(v);
  4356. if (v>2) return(0.0);
  4357. v2 = 2-v;
  4358. return(v2*v2*v2*v2*v2*(16+v*(40+v*(36+v*(10+v))))/630);
  4359. }
  4360. LERR(("Wconv not implemented for kernel %d",ker));
  4361. return(0.0);
  4362. }
  4363. /* derivative of Wconv.
  4364. 1/v d/dv int W(x)W(x+v)dx
  4365. used in kde bandwidth selection.
  4366. */
  4367. double Wconv1(v,ker)
  4368. double v;
  4369. int ker;
  4370. { double v2;
  4371. v = fabs(v);
  4372. switch(ker)
  4373. { case WGAUS: return(-0.5*SQRPI*GFACT*exp(-SQR(GFACT*v)/4));
  4374. case WRECT:
  4375. if (v>2) return(0.0);
  4376. return(1.0);
  4377. case WEPAN:
  4378. if (v>2) return(0.0);
  4379. return((-16+v*(12-v*v))/6);
  4380. case WBISQ:
  4381. if (v>2) return(0.0);
  4382. v2 = 2-v;
  4383. return(-v2*v2*v2*v2*(32+v*(64+v*(24+v*3)))/210);
  4384. }
  4385. LERR(("Wconv1 not implemented for kernel %d",ker));
  4386. return(0.0);
  4387. }
  4388. /* 4th derivative of Wconv.
  4389. used in kde bandwidth selection (BCV, SJPI, GKK)
  4390. */
  4391. double Wconv4(v,ker)
  4392. double v;
  4393. int ker;
  4394. { double gv;
  4395. switch(ker)
  4396. { case WGAUS:
  4397. gv = GFACT*v;
  4398. return(exp(-SQR(gv)/4)*GFACT*GFACT*GFACT*(12-gv*gv*(12-gv*gv))*SQRPI/16);
  4399. }
  4400. LERR(("Wconv4 not implemented for kernel %d",ker));
  4401. return(0.0);
  4402. }
  4403. /* 5th derivative of Wconv.
  4404. used in kde bandwidth selection (BCV method only)
  4405. */
  4406. double Wconv5(v,ker) /* (d/dv)^5 int W(x)W(x+v)dx */
  4407. double v;
  4408. int ker;
  4409. { double gv;
  4410. switch(ker)
  4411. { case WGAUS:
  4412. gv = GFACT*v;
  4413. return(-exp(-SQR(gv)/4)*GFACT*GFACT*GFACT*GFACT*gv*(60-gv*gv*(20-gv*gv))*SQRPI/32);
  4414. }
  4415. LERR(("Wconv5 not implemented for kernel %d",ker));
  4416. return(0.0);
  4417. }
  4418. /* 6th derivative of Wconv.
  4419. used in kde bandwidth selection (SJPI)
  4420. */
  4421. double Wconv6(v,ker)
  4422. double v;
  4423. int ker;
  4424. { double gv, z;
  4425. switch(ker)
  4426. { case WGAUS:
  4427. gv = GFACT*v;
  4428. gv = gv*gv;
  4429. z = exp(-gv/4)*(-120+gv*(180-gv*(30-gv)))*0.02769459142;
  4430. gv = GFACT*GFACT;
  4431. return(z*gv*gv*GFACT);
  4432. }
  4433. LERR(("Wconv6 not implemented for kernel %d",ker));
  4434. return(0.0);
  4435. }
  4436. /* int W(v)^2 dv / (int v^2 W(v) dv)^2
  4437. used in some bandwidth selectors
  4438. */
  4439. double Wikk(ker,deg)
  4440. int ker, deg;
  4441. { switch(deg)
  4442. { case 0:
  4443. case 1: /* int W(v)^2 dv / (int v^2 W(v) dv)^2 */
  4444. switch(ker)
  4445. { case WRECT: return(4.5);
  4446. case WEPAN: return(15.0);
  4447. case WBISQ: return(35.0);
  4448. case WGAUS: return(0.2820947918*GFACT*GFACT*GFACT*GFACT*GFACT);
  4449. case WTCUB: return(34.152111046847892); /* 59049 / 1729 */
  4450. case WTRWT: return(66.083916083916080); /* 9450/143 */
  4451. }
  4452. case 2:
  4453. case 3: /* 4!^2/8*int(W1^2)/int(v^4W1)^2
  4454. W1=W*(n4-v^2n2)/(n0n4-n2n2) */
  4455. switch(ker)
  4456. { case WRECT: return(11025.0);
  4457. case WEPAN: return(39690.0);
  4458. case WBISQ: return(110346.9231);
  4459. case WGAUS: return(14527.43412);
  4460. case WTCUB: return(126500.5904);
  4461. case WTRWT: return(254371.7647);
  4462. }
  4463. }
  4464. LERR(("Wikk not implemented for kernel %d, deg %d",ker,deg));
  4465. return(0.0);
  4466. }