12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940 |
- /*
- * Copyright 1996-2006 Catherine Loader.
- */
- #include "mex.h"
- /*
- * Copyright 1996-2006 Catherine Loader.
- */
- /*
- * Integration for hazard rate estimation. The functions in this
- * file are used to evaluate
- * sum int_0^{Ti} W_i(t,x) A()A()' exp( P() ) dt
- * for hazard rate models.
- *
- * These routines assume the weight function is supported on [-1,1].
- * hasint_sph multiplies by exp(base(lf,i)), which allows estimating
- * the baseline in a proportional hazards model, when the covariate
- * effect base(lf,i) is known.
- *
- * TODO:
- * hazint_sph, should be able to reduce mint in some cases with
- * small integration range. onedint could be used for beta-family
- * (RECT,EPAN,BISQ,TRWT) kernels.
- * hazint_prod, restrict terms from the sum based on x values.
- * I should count obs >= max, and only do that integration once.
- */
- #include "locf.h"
- static double ilim[2*MXDIM], *ff, tmax;
- static lfdata *haz_lfd;
- static smpar *haz_sp;
- /*
- * hrao returns 0 if integration region is empty.
- * 1 otherwise.
- */
- int haz_sph_int(dfx,cf,h,r1)
- double *dfx, *cf, h, *r1;
- { double s, t0, t1, wt, th;
- int j, dim, p;
- s = 0; p = npar(haz_sp);
- dim = haz_lfd->d;
- for (j=1; j<dim; j++) s += SQR(dfx[j]/(h*haz_lfd->sca[j]));
- if (s>1) return(0);
- setzero(r1,p*p);
- t1 = sqrt(1-s)*h*haz_lfd->sca[0];
- t0 = -t1;
- if (t0<ilim[0]) t0 = ilim[0];
- if (t1>ilim[dim]) t1 = ilim[dim];
- if (t1>dfx[0]) t1 = dfx[0];
- if (t1<t0) return(0);
- /* Numerical integration by Simpson's rule.
- */
- for (j=0; j<=de_mint; j++)
- { dfx[0] = t0+(t1-t0)*j/de_mint;
- wt = weight(haz_lfd, haz_sp, dfx, NULL, h, 0, 0.0);
- fitfun(haz_lfd, haz_sp, dfx,NULL,ff,NULL);
- th = innerprod(cf,ff,p);
- if (link(haz_sp)==LLOG) th = exp(th);
- wt *= 2+2*(j&1)-(j==0)-(j==de_mint);
- addouter(r1,ff,ff,p,wt*th);
- }
- multmatscal(r1,(t1-t0)/(3*de_mint),p*p);
- return(1);
- }
- int hazint_sph(t,resp,r1,cf,h)
- double *t, *resp, *r1, *cf, h;
- { int i, j, n, p, st;
- double dfx[MXDIM], eb, sb;
- p = npar(haz_sp);
- setzero(resp,p*p);
- sb = 0.0;
- n = haz_lfd->n;
- for (i=0; i<=n; i++)
- {
- if (i==n)
- { dfx[0] = tmax-t[0];
- for (j=1; j<haz_lfd->d; j++) dfx[j] = 0.0;
- eb = exp(sb/n);
- }
- else
- { eb = exp(base(haz_lfd,i)); sb += base(haz_lfd,i);
- for (j=0; j<haz_lfd->d; j++) dfx[j] = datum(haz_lfd,j,i)-t[j];
- }
- st = haz_sph_int(dfx,cf,h,r1);
- if (st)
- for (j=0; j<p*p; j++) resp[j] += eb*r1[j];
- }
- return(LF_OK);
- }
- int hazint_prod(t,resp,x,cf,h)
- double *t, *resp, *x, *cf, h;
- { int d, p, i, j, k, st;
- double dfx[MXDIM], t_prev,
- hj, hs, ncf[MXDEG], ef, il1;
- double prod_wk[MXDIM][2*MXDEG+1], eb, sb;
- p = npar(haz_sp);
- d = haz_lfd->d;
- setzero(resp,p*p);
- hj = hs = h*haz_lfd->sca[0];
- ncf[0] = cf[0];
- for (i=1; i<=deg(haz_sp); i++)
- { ncf[i] = hj*cf[(i-1)*d+1]; hj *= hs;
- }
- /* for i=0..n....
- * First we compute prod_wk[j], j=0..d.
- * For j=0, this is int_0^T_i (u-t)^k W((u-t)/h) exp(b0*(u-t)) du
- * For remaining j, (x(i,j)-x(j))^k Wj exp(bj*(x..-x.))
- *
- * Second, we add to the integration (exp(a) incl. in integral)
- * with the right factorial denominators.
- */
- t_prev = ilim[0]; sb = 0.0;
- for (i=0; i<=haz_lfd->n; i++)
- { if (i==haz_lfd->n)
- { dfx[0] = tmax-t[0];
- for (j=1; j<d; j++) dfx[j] = 0.0;
- eb = exp(sb/haz_lfd->n);
- }
- else
- { eb = exp(base(haz_lfd,i)); sb += base(haz_lfd,i);
- for (j=0; j<d; j++) dfx[j] = datum(haz_lfd,j,i)-t[j];
- }
- if (dfx[0]>ilim[0]) /* else it doesn't contribute */
- {
- /* time integral */
- il1 = (dfx[0]>ilim[d]) ? ilim[d] : dfx[0];
- if (il1 != t_prev) /* don't repeat! */
- { st = onedint(haz_sp,ncf,ilim[0]/hs,il1/hs,prod_wk[0]);
- if (st>0) return(st);
- hj = eb;
- for (j=0; j<=2*deg(haz_sp); j++)
- { hj *= hs;
- prod_wk[0][j] *= hj;
- }
- t_prev = il1;
- }
- /* covariate terms */
- for (j=1; j<d; j++)
- {
- ef = 0.0;
- for (k=deg(haz_sp); k>0; k--) ef = (ef+dfx[j])*cf[1+(k-1)*d+j];
- ef = exp(ef);
- prod_wk[j][0] = ef * W(dfx[j]/(h*haz_lfd->sca[j]),ker(haz_sp));
- for (k=1; k<=2*deg(haz_sp); k++)
- prod_wk[j][k] = prod_wk[j][k-1] * dfx[j];
- }
- /* add to the integration. */
- prodintresp(resp,prod_wk,d,deg(haz_sp),p);
- } /* if dfx0 > ilim0 */
- } /* n loop */
- /* symmetrize */
- for (k=0; k<p; k++)
- for (j=k; j<p; j++)
- resp[j*p+k] = resp[k*p+j];
- return(LF_OK);
- }
- int hazint(t,resp,resp1,cf,h)
- double *t, *resp, *resp1, *cf, h;
- { if (haz_lfd->d==1) return(hazint_prod(t,resp,resp1,cf,h));
- if (kt(haz_sp)==KPROD) return(hazint_prod(t,resp,resp1,cf,h));
- return(hazint_sph(t,resp,resp1,cf,h));
- }
- void haz_init(lfd,des,sp,il)
- lfdata *lfd;
- design *des;
- smpar *sp;
- double *il;
- { int i;
-
- haz_lfd = lfd;
- haz_sp = sp;
- tmax = datum(lfd,0,0);
- for (i=1; i<lfd->n; i++) tmax = MAX(tmax,datum(lfd,0,i));
- ff = des->xtwx.wk;
- for (i=0; i<2*lfd->d; i++) ilim[i] = il[i];
- }
- /*
- * Copyright 1996-2006 Catherine Loader.
- */
- /*
- *
- * Routines for one-dimensional numerical integration
- * in density estimation. The entry point is
- *
- * onedint(cf,mi,l0,l1,resp)
- *
- * which evaluates int W(u)u^j exp( P(u) ), j=0..2*deg.
- * P(u) = cf[0] + cf[1]u + cf[2]u^2/2 + ... + cf[deg]u^deg/deg!
- * l0 and l1 are the integration limits.
- * The results are returned through the vector resp.
- *
- */
- #include "locf.h"
- static int debug;
- int exbctay(b,c,n,z) /* n-term taylor series of e^(bx+cx^2) */
- double b, c, *z;
- int n;
- { double ec[20];
- int i, j;
- z[0] = 1;
- for (i=1; i<=n; i++) z[i] = z[i-1]*b/i;
- if (c==0.0) return(n);
- if (n>=40)
- { WARN(("exbctay limit to n<40"));
- n = 39;
- }
- ec[0] = 1;
- for (i=1; 2*i<=n; i++) ec[i] = ec[i-1]*c/i;
- for (i=n; i>1; i--)
- for (j=1; 2*j<=i; j++)
- z[i] += ec[j]*z[i-2*j];
- return(n);
- }
- double explinjtay(l0,l1,j,cf)
- /* int_l0^l1 x^j e^(a+bx+cx^2); exbctay aroud l1 */
- double l0, l1, *cf;
- int j;
- { double tc[40], f, s;
- int k, n;
- if ((l0!=0.0) | (l1!=1.0)) WARN(("explinjtay: invalid l0, l1"));
- n = exbctay(cf[1]+2*cf[2]*l1,cf[2],20,tc);
- s = tc[0]/(j+1);
- f = 1/(j+1);
- for (k=1; k<=n; k++)
- { f *= -k/(j+k+1.0);
- s += tc[k]*f;
- }
- return(f);
- }
- void explint1(l0,l1,cf,I,p) /* int x^j exp(a+bx); j=0..p-1 */
- double l0, l1, *cf, *I;
- int p;
- { double y0, y1, f;
- int j, k, k1;
- y0 = mut_exp(cf[0]+l0*cf[1]);
- y1 = mut_exp(cf[0]+l1*cf[1]);
- if (p<2*fabs(cf[1])) k = p; else k = (int)fabs(cf[1]);
- if (k>0)
- { I[0] = (y1-y0)/cf[1];
- for (j=1; j<k; j++) /* forward steps for small j */
- { y1 *= l1; y0 *= l0;
- I[j] = (y1-y0-j*I[j-1])/cf[1];
- }
- if (k==p) return;
- y1 *= l1; y0 *= l0;
- }
- f = 1; k1 = k;
- while ((k<50) && (f>1.0e-8)) /* initially Ik = diff(x^{k+1}e^{a+bx}) */
- { y1 *= l1; y0 *= l0;
- I[k] = y1-y0;
- if (k>=p) f *= fabs(cf[1])/(k+1);
- k++;
- }
- if (k==50) WARN(("explint1: want k>50"));
- I[k] = 0.0;
- for (j=k-1; j>=k1; j--) /* now do back step recursion */
- I[j] = (I[j]-cf[1]*I[j+1])/(j+1);
- }
- void explintyl(l0,l1,cf,I,p) /* small c, use taylor series and explint1 */
- double l0, l1, *cf, *I;
- int p;
- { int i;
- double c;
- explint1(l0,l1,cf,I,p+8);
- c = cf[2];
- for (i=0; i<p; i++)
- I[i] = (((I[i+8]*c/4+I[i+6])*c/3+I[i+4])*c/2+I[i+2])*c+I[i];
- }
- void solvetrid(X,y,m)
- double *X, *y;
- int m;
- { int i;
- double s;
- for (i=1; i<m; i++)
- { s = X[3*i]/X[3*i-2];
- X[3*i] = 0; X[3*i+1] -= s*X[3*i-1];
- y[i] -= s*y[i-1];
- }
- for (i=m-2; i>=0; i--)
- { s = X[3*i+2]/X[3*i+4];
- X[3*i+2] = 0;
- y[i] -= s*y[i+1];
- }
- for (i=0; i<m; i++) y[i] /= X[3*i+1];
- }
- void initi0i1(I,cf,y0,y1,l0,l1)
- double *I, *cf, y0, y1, l0, l1;
- { double a0, a1, c, d, bi;
- d = -cf[1]/(2*cf[2]); c = sqrt(2*fabs(cf[2]));
- a0 = c*(l0-d); a1 = c*(l1-d);
- if (cf[2]<0)
- { bi = mut_exp(cf[0]+cf[1]*d+cf[2]*d*d)/c;
- if (a0>0)
- { if (a0>6) I[0] = (y0*ptail(-a0)-y1*ptail(-a1))/c;
- else I[0] = S2PI*(mut_pnorm(-a0)-mut_pnorm(-a1))*bi;
- }
- else
- { if (a1< -6) I[0] = (y1*ptail(a1)-y0*ptail(a0))/c;
- else I[0] = S2PI*(mut_pnorm(a1)-mut_pnorm(a0))*bi;
- }
- }
- else
- I[0] = (y1*mut_daws(a1)-y0*mut_daws(a0))/c;
- I[1] = (y1-y0)/(2*cf[2])+d*I[0];
- }
- void explinsid(l0,l1,cf,I,p) /* large b; don't use fwd recursion */
- double l0, l1, *cf, *I;
- int p;
- { int k, k0, k1, k2;
- double y0, y1, Z[150];
- 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);
-
- k0 = 2;
- k1 = (int)(fabs(cf[1])+fabs(2*cf[2]));
- if (k1<2) k1 = 2;
- if (k1>p+20) k1 = p+20;
- k2 = p+20;
- if (k2>50) { mut_printf("onedint: k2 warning\n"); k2 = 50; }
- if (debug) mut_printf("k0 %2d k1 %2d k2 %2d p %2d\n",k0,k1,k2,p);
- y0 = mut_exp(cf[0]+l0*(cf[1]+l0*cf[2]));
- y1 = mut_exp(cf[0]+l1*(cf[1]+l1*cf[2]));
- initi0i1(I,cf,y0,y1,l0,l1);
- if (debug) mut_printf("i0 %8.5f i1 %8.5f\n",I[0],I[1]);
- y1 *= l1; y0 *= l0; /* should be x^(k1)*exp(..) */
- if (k0<k1) /* center steps; initially x^k*exp(...) */
- for (k=k0; k<k1; k++)
- { y1 *= l1; y0 *= l0;
- I[k] = y1-y0;
- Z[3*k] = k; Z[3*k+1] = cf[1]; Z[3*k+2] = 2*cf[2];
- }
-
- y1 *= l1; y0 *= l0; /* should be x^(k1)*exp(..) */
- if (debug) mut_printf("k1 %2d y0 %8.5f y1 %8.5f\n",k1,y0,y1);
- for (k=k1; k<k2; k++)
- { y1 *= l1; y0 *= l0;
- I[k] = y1-y0;
- }
- I[k2] = I[k2+1] = 0.0;
- for (k=k2-1; k>=k1; k--)
- I[k] = (I[k]-cf[1]*I[k+1]-2*cf[2]*I[k+2])/(k+1);
- if (k0<k1)
- { I[k0] -= k0*I[k0-1];
- I[k1-1] -= 2*cf[2]*I[k1];
- Z[3*k0] = Z[3*k1-1] = 0;
- solvetrid(&Z[3*k0],&I[k0],k1-k0);
- }
- if (debug)
- { mut_printf("explinsid:\n");
- for (k=0; k<p; k++) mut_printf(" %8.5f\n",I[k]);
- }
- }
- void explinbkr(l0,l1,cf,I,p) /* small b,c; use back recursion */
- double l0, l1, *cf, *I;
- int p;
- { int k, km;
- double y0, y1;
- y0 = mut_exp(cf[0]+l0*(cf[1]+cf[2]*l0));
- y1 = mut_exp(cf[0]+l1*(cf[1]+cf[2]*l1));
- km = p+10;
- for (k=0; k<=km; k++)
- { y1 *= l1; y0 *= l0;
- I[k] = y1-y0;
- }
- I[km+1] = I[km+2] = 0;
- for (k=km; k>=0; k--)
- I[k] = (I[k]-cf[1]*I[k+1]-2*cf[2]*I[k+2])/(k+1);
- }
- void explinfbk0(l0,l1,cf,I,p) /* fwd and bac recur; b=0; c<0 */
- double l0, l1, *cf, *I;
- int p;
- { double y0, y1, f1, f2, f, ml2;
- int k, ks;
- y0 = mut_exp(cf[0]+l0*l0*cf[2]);
- y1 = mut_exp(cf[0]+l1*l1*cf[2]);
- initi0i1(I,cf,y0,y1,l0,l1);
- ml2 = MAX(l0*l0,l1*l1);
- ks = 1+(int)(2*fabs(cf[2])*ml2);
- if (ks<2) ks = 2;
- if (ks>p-3) ks = p;
- /* forward recursion for k < ks */
- for (k=2; k<ks; k++)
- { y1 *= l1; y0 *= l0;
- I[k] = (y1-y0-(k-1)*I[k-2])/(2*cf[2]);
- }
- if (ks==p) return;
- y1 *= l1*l1; y0 *= l0*l0;
- for (k=ks; k<p; k++) /* set I[k] = x^{k+1}e^(a+cx^2) | {l0,l1} */
- { y1 *= l1; y0 *= l0;
- I[k] = y1-y0;
- }
- /* initialize I[p-2] and I[p-1] */
- f1 = 1.0/p; f2 = 1.0/(p-1);
- I[p-1] *= f1; I[p-2] *= f2;
- k = p; f = 1.0;
- while (f>1.0e-8)
- { y1 *= l1; y0 *= l0;
- if ((k-p)%2==0) /* add to I[p-2] */
- { f2 *= -2*cf[2]/(k+1);
- I[p-2] += (y1-y0)*f2;
- }
- else /* add to I[p-1] */
- { f1 *= -2*cf[2]/(k+1);
- I[p-1] += (y1-y0)*f1;
- f *= 2*fabs(cf[2])*ml2/(k+1);
- }
- k++;
- }
-
- /* use back recursion for I[ks..(p-3)] */
- for (k=p-3; k>=ks; k--)
- I[k] = (I[k]-2*cf[2]*I[k+2])/(k+1);
- }
- void explinfbk(l0,l1,cf,I,p) /* fwd and bac recur; b not too large */
- double l0, l1, *cf, *I;
- int p;
- { double y0, y1;
- int k, ks, km;
- y0 = mut_exp(cf[0]+l0*(cf[1]+l0*cf[2]));
- y1 = mut_exp(cf[0]+l1*(cf[1]+l1*cf[2]));
- initi0i1(I,cf,y0,y1,l0,l1);
- ks = (int)(3*fabs(cf[2]));
- if (ks<3) ks = 3;
- if (ks>0.75*p) ks = p; /* stretch the forward recurs as far as poss. */
- /* forward recursion for k < ks */
- for (k=2; k<ks; k++)
- { y1 *= l1; y0 *= l0;
- I[k] = (y1-y0-cf[1]*I[k-1]-(k-1)*I[k-2])/(2*cf[2]);
- }
- if (ks==p) return;
- km = p+15;
- y1 *= l1*l1; y0 *= l0*l0;
- for (k=ks; k<=km; k++)
- { y1 *= l1; y0 *= l0;
- I[k] = y1-y0;
- }
- I[km+1] = I[km+2] = 0.0;
- for (k=km; k>=ks; k--)
- I[k] = (I[k]-cf[1]*I[k+1]-2*cf[2]*I[k+2])/(k+1);
- }
- void recent(I,resp,wt,p,s,x)
- double *I, *resp, *wt, x;
- int p, s;
- { int i, j;
- /* first, use W taylor series I -> resp */
- for (i=0; i<=p; i++)
- { resp[i] = 0.0;
- for (j=0; j<s; j++) resp[i] += wt[j]*I[i+j];
- }
- /* now, recenter x -> 0 */
- if (x==0) return;
- for (j=0; j<=p; j++) for (i=p; i>j; i--) resp[i] += x*resp[i-1];
- }
- void recurint(l0,l2,cf,resp,p,ker)
- double l0, l2, *cf, *resp;
- int p, ker;
- { int i, s;
- double l1, d0, d1, d2, dl, z0, z1, z2, wt[20], ncf[3], I[50], r1[5], r2[5];
- if (debug) mut_printf("\nrecurint: %8.5f %8.5f %8.5f %8.5f %8.5f\n",cf[0],cf[1],cf[2],l0,l2);
- if (cf[2]==0) /* go straight to explint1 */
- { s = wtaylor(wt,0.0,ker);
- if (debug) mut_printf("case 1\n");
- explint1(l0,l2,cf,I,p+s);
- recent(I,resp,wt,p,s,0.0);
- return;
- }
- dl = l2-l0;
- d0 = cf[1]+2*l0*cf[2];
- d2 = cf[1]+2*l2*cf[2];
- z0 = cf[0]+l0*(cf[1]+l0*cf[2]);
- z2 = cf[0]+l2*(cf[1]+l2*cf[2]);
- if ((fabs(cf[1]*dl)<1) && (fabs(cf[2]*dl*dl)<1))
- { ncf[0] = z0; ncf[1] = d0; ncf[2] = cf[2];
- if (debug) mut_printf("case 2\n");
- s = wtaylor(wt,l0,ker);
- explinbkr(0.0,dl,ncf,I,p+s);
- recent(I,resp,wt,p,s,l0);
- return;
- }
- if (fabs(cf[2]*dl*dl)<0.001) /* small c, use explint1+tay.ser */
- { ncf[0] = z0; ncf[1] = d0; ncf[2] = cf[2];
- if (debug) mut_printf("case small c\n");
- s = wtaylor(wt,l0,ker);
- explintyl(0.0,l2-l0,ncf,I,p+s);
- recent(I,resp,wt,p,s,l0);
- return;
- }
- if (d0*d2<=0) /* max/min in [l0,l2] */
- { l1 = -cf[1]/(2*cf[2]);
- z1 = cf[0]+l1*(cf[1]+l1*cf[2]);
- d1 = 0.0;
- if (cf[2]<0) /* peak, integrate around l1 */
- { s = wtaylor(wt,l1,ker);
- ncf[0] = z1; ncf[1] = 0.0; ncf[2] = cf[2];
- if (debug) mut_printf("case peak p %2d s %2d\n",p,s);
- explinfbk0(l0-l1,l2-l1,ncf,I,p+s);
- recent(I,resp,wt,p,s,l1);
- return;
- }
- }
- if ((d0-2*cf[2]*dl)*(d2+2*cf[2]*dl)<0) /* max/min is close to [l0,l2] */
- { l1 = -cf[1]/(2*cf[2]);
- z1 = cf[0]+l1*(cf[1]+l1*cf[2]);
- if (l1<l0) { l1 = l0; z1 = z0; }
- if (l1>l2) { l1 = l2; z1 = z2; }
- if ((z1>=z0) & (z1>=z2)) /* peak; integrate around l1 */
- { s = wtaylor(wt,l1,ker);
- if (debug) mut_printf("case 4\n");
- d1 = cf[1]+2*l1*cf[2];
- ncf[0] = z1; ncf[1] = d1; ncf[2] = cf[2];
- explinfbk(l0-l1,l2-l1,ncf,I,p+s);
- recent(I,resp,wt,p,s,l1);
- return;
- }
- /* trough; integrate [l0,l1] and [l1,l2] */
- for (i=0; i<=p; i++) r1[i] = r2[i] = 0.0;
- if (l0<l1)
- { s = wtaylor(wt,l0,ker);
- if (debug) mut_printf("case 5\n");
- ncf[0] = z0; ncf[1] = d0; ncf[2] = cf[2];
- explinfbk(0.0,l1-l0,ncf,I,p+s);
- recent(I,r1,wt,p,s,l0);
- }
- if (l1<l2)
- { s = wtaylor(wt,l2,ker);
- if (debug) mut_printf("case 6\n");
- ncf[0] = z2; ncf[1] = d2; ncf[2] = cf[2];
- explinfbk(l1-l2,0.0,ncf,I,p+s);
- recent(I,r2,wt,p,s,l2);
- }
- for (i=0; i<=p; i++) resp[i] = r1[i]+r2[i];
- return;
- }
- /* Now, quadratic is monotone on [l0,l2]; big b; moderate c */
- if (z2>z0+3) /* steep increase, expand around l2 */
- { s = wtaylor(wt,l2,ker);
- if (debug) mut_printf("case 7\n");
- ncf[0] = z2; ncf[1] = d2; ncf[2] = cf[2];
- explinsid(l0-l2,0.0,ncf,I,p+s);
- recent(I,resp,wt,p,s,l2);
- if (debug) mut_printf("7 resp: %8.5f %8.5f %8.5f %8.5f\n",resp[0],resp[1],resp[2],resp[3]);
- return;
- }
- /* bias towards expansion around l0, because it's often 0 */
- if (debug) mut_printf("case 8\n");
- s = wtaylor(wt,l0,ker);
- ncf[0] = z0; ncf[1] = d0; ncf[2] = cf[2];
- explinsid(0.0,l2-l0,ncf,I,p+s);
- recent(I,resp,wt,p,s,l0);
- return;
- }
- int onedexpl(cf,deg,resp)
- double *cf, *resp;
- int deg;
- { int i;
- double f0, fr, fl;
- if (deg>=2) LERR(("onedexpl only valid for deg=0,1"));
- if (fabs(cf[1])>=EFACT) return(LF_BADP);
- f0 = exp(cf[0]); fl = fr = 1.0;
- for (i=0; i<=2*deg; i++)
- { f0 *= i+1;
- fl /=-(EFACT+cf[1]);
- fr /= EFACT-cf[1];
- resp[i] = f0*(fr-fl);
- }
- return(LF_OK);
- }
- int onedgaus(cf,deg,resp)
- double *cf, *resp;
- int deg;
- { int i;
- double f0, mu, s2;
- if (deg==3)
- { LERR(("onedgaus only valid for deg=0,1,2"));
- return(LF_ERR);
- }
- if (2*cf[2]>=GFACT*GFACT) return(LF_BADP);
- s2 = 1/(GFACT*GFACT-2*cf[2]);
- mu = cf[1]*s2;
- resp[0] = 1.0;
- if (deg>=1)
- { resp[1] = mu;
- resp[2] = s2+mu*mu;
- if (deg==2)
- { resp[3] = mu*(3*s2+mu*mu);
- resp[4] = 3*s2*s2 + mu*mu*(6*s2+mu*mu);
- }
- }
- f0 = S2PI * exp(cf[0]+mu*mu/(2*s2))*sqrt(s2);
- for (i=0; i<=2*deg; i++) resp[i] *= f0;
- return(LF_OK);
- }
- int onedint(sp,cf,l0,l1,resp) /* int W(u)u^j exp(..), j=0..2*deg */
- smpar *sp;
- double *cf, l0, l1, *resp;
- { double u, uj, y, ncf[4], rr[5];
- int i, j;
- if (debug) mut_printf("onedint: %f %f %f %f %f\n",cf[0],cf[1],cf[2],l0,l1);
- if (deg(sp)<=2)
- { for (i=0; i<3; i++) ncf[i] = (i>deg(sp)) ? 0.0 : cf[i];
- ncf[2] /= 2;
- if (ker(sp)==WEXPL) return(onedexpl(ncf,deg(sp),resp));
- if (ker(sp)==WGAUS) return(onedgaus(ncf,deg(sp),resp));
- if (l1>0)
- recurint(MAX(l0,0.0),l1,ncf,resp,2*deg(sp),ker(sp));
- else for (i=0; i<=2*deg(sp); i++) resp[i] = 0;
- if (l0<0)
- { ncf[1] = -ncf[1];
- l0 = -l0; l1 = -l1;
- recurint(MAX(l1,0.0),l0,ncf,rr,2*deg(sp),ker(sp));
- }
- else for (i=0; i<=2*deg(sp); i++) rr[i] = 0.0;
- for (i=0; i<=2*deg(sp); i++)
- resp[i] += (i%2==0) ? rr[i] : -rr[i];
- return(LF_OK);
- }
- /* For degree >= 3, we use Simpson's rule. */
- for (j=0; j<=2*deg(sp); j++) resp[j] = 0.0;
- for (i=0; i<=de_mint; i++)
- { u = l0+(l1-l0)*i/de_mint;
- y = cf[0]; uj = 1;
- for (j=1; j<=deg(sp); j++)
- { uj *= u;
- y += cf[j]*uj/fact[j];
- }
- y = (4-2*(i%2==0)-(i==0)-(i==de_mint)) *
- W(fabs(u),ker(sp))*exp(MIN(y,300.0));
- for (j=0; j<=2*deg(sp); j++)
- { resp[j] += y;
- y *= u;
- }
- }
- for (j=0; j<=2*deg(sp); j++) resp[j] = resp[j]*(l1-l0)/(3*de_mint);
- return(LF_OK);
- }
- /*
- * Copyright 1996-2006 Catherine Loader.
- */
- #include "locf.h"
- extern int lf_status;
- static double u[MXDIM], ilim[2*MXDIM], *ff, hh, *cff;
- static lfdata *den_lfd;
- static design *den_des;
- static smpar *den_sp;
- int fact[] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800};
- int de_mint = 20;
- int de_itype = IDEFA;
- int de_renorm= 0;
- int multint(), prodint(), gausint(), mlinint();
- #define NITYPE 7
- static char *itype[NITYPE] = { "default", "multi", "product", "mlinear",
- "hazard", "sphere", "monte" };
- static int ivals[NITYPE] =
- { IDEFA, IMULT, IPROD, IMLIN, IHAZD, ISPHR, IMONT };
- int deitype(char *z)
- { return(pmatch(z, itype, ivals, NITYPE, IDEFA));
- }
- void prresp(coef,resp,p)
- double *coef, *resp;
- int p;
- { int i, j;
- mut_printf("Coefficients:\n");
- for (i=0; i<p; i++) mut_printf("%8.5f ",coef[i]);
- mut_printf("\n");
- mut_printf("Response matrix:\n");
- for (i=0; i<p; i++)
- { for (j=0; j<p; j++) mut_printf("%9.6f, ",resp[i+j*p]);
- mut_printf("\n");
- }
- }
- int mif(u,d,resp,M)
- double *u, *resp, *M;
- int d;
- { double wt;
- int i, j, p;
- p = den_des->p;
- wt = weight(den_lfd, den_sp, u, NULL, hh, 0, 0.0);
- if (wt==0)
- { setzero(resp,p*p);
- return(p*p);
- }
- fitfun(den_lfd, den_sp, u,NULL,ff,NULL);
- if (link(den_sp)==LLOG)
- wt *= mut_exp(innerprod(ff,cff,p));
- for (i=0; i<p; i++)
- for (j=0; j<p; j++)
- resp[i*p+j] = wt*ff[i]*ff[j];
- return(p*p);
- }
- int multint(t,resp1,resp2,cf,h)
- double *t, *resp1, *resp2, *cf, h;
- { int d, i, mg[MXDIM];
- if (ker(den_sp)==WGAUS) return(gausint(t,resp1,resp2,cf,h,den_lfd->sca));
- d = den_lfd->d;
- for (i=0; i<d; i++) mg[i] = de_mint;
- hh = h;
- cff= cf;
- simpsonm(mif,ilim,&ilim[d],d,resp1,mg,resp2);
- return(LF_OK);
- }
- int mlinint(t,resp1,resp2,cf,h)
- double *t, *resp1, *resp2, *cf, h;
- {
- double hd, nb, wt, wu, g[4], w0, w1, v, *sca;
- int d, p, i, j, jmax, k, l, z, jj[2];
- d = den_lfd->d; p = den_des->p; sca = den_lfd->sca;
- hd = 1;
- for (i=0; i<d; i++) hd *= h*sca[i];
- if (link(den_sp)==LIDENT)
- { setzero(resp1,p*p);
- resp1[0] = wint(d,NULL,0,ker(den_sp))*hd;
- if (deg(den_sp)==0) return(LF_OK);
- jj[0] = 2; w0 = wint(d,jj,1,ker(den_sp))*hd*h*h;
- for (i=0; i<d; i++) resp1[(i+1)*p+i+1] = w0*sca[i]*sca[i];
- if (deg(den_sp)==1) return(LF_OK);
- for (i=0; i<d; i++)
- { j = p-(d-i)*(d-i+1)/2;
- resp1[j] = resp1[p*j] = w0*sca[i]*sca[i]/2;
- }
- if (d>1)
- { jj[1] = 2;
- w0 = wint(d,jj,2,ker(den_sp)) * hd*h*h*h*h;
- }
- jj[0] = 4;
- w1 = wint(d,jj,1,ker(den_sp)) * hd*h*h*h*h/4;
- z = d+1;
- for (i=0; i<d; i++)
- { k = p-(d-i)*(d-i+1)/2;
- for (j=i; j<d; j++)
- { l = p-(d-j)*(d-j+1)/2;
- if (i==j) resp1[z*p+z] = w1*SQR(sca[i])*SQR(sca[i]);
- else
- { resp1[z*p+z] = w0*SQR(sca[i])*SQR(sca[j]);
- resp1[k*p+l] = resp1[k+p*l] = w0/4*SQR(sca[i])*SQR(sca[j]);
- }
- z++;
- } }
- return(LF_OK);
- }
- switch(deg(den_sp))
- { case 0:
- resp1[0] = mut_exp(cf[0])*wint(d,NULL,0,ker(den_sp))*hd;
- return(LF_OK);
- case 1:
- nb = 0.0;
- for (i=1; i<=d; i++)
- { v = h*cf[i]*sca[i-1];
- nb += v*v;
- }
- if (ker(den_sp)==WGAUS)
- { w0 = 1/(GFACT*GFACT);
- g[0] = mut_exp(cf[0]+w0*nb/2+d*log(S2PI/2.5));
- g[1] = g[3] = g[0]*w0;
- g[2] = g[0]*w0*w0;
- }
- else
- { wt = wu = mut_exp(cf[0]);
- w0 = wint(d,NULL,0,ker(den_sp)); g[0] = wt*w0;
- g[1] = g[2] = g[3] = 0.0;
- j = 0; jmax = (d+2)*de_mint;
- while ((j<jmax) && (wt*w0/g[0]>1.0e-8))
- { j++;
- jj[0] = 2*j; w0 = wint(d,jj,1,ker(den_sp));
- if (d==1) g[3] += wt * w0;
- else
- { jj[0] = 2; jj[1] = 2*j-2; w1 = wint(d,jj,2,ker(den_sp));
- g[3] += wt*w1;
- g[2] += wu*(w0-w1);
- }
- wt /= (2*j-1.0); g[1] += wt*w0;
- wt *= nb/(2*j); g[0] += wt*w0;
- wu /= (2*j-1.0)*(2*j);
- if (j>1) wu *= nb;
- }
- if (j==jmax) WARN(("mlinint: series not converged"));
- }
- g[0] *= hd; g[1] *= hd;
- g[2] *= hd; g[3] *= hd;
- resp1[0] = g[0];
- for (i=1; i<=d; i++)
- { resp1[i] = resp1[(d+1)*i] = cf[i]*SQR(h*sca[i-1])*g[1];
- for (j=1; j<=d; j++)
- { resp1[(d+1)*i+j] = (i==j) ? g[3]*SQR(h*sca[i-1]) : 0;
- resp1[(d+1)*i+j] += g[2]*SQR(h*h*sca[i-1]*sca[j-1])*cf[i]*cf[j];
- }
- }
- return(LF_OK);
- }
- LERR(("mlinint: deg=0,1 only"));
- return(LF_ERR);
- }
- void prodintresp(resp,prod_wk,dim,deg,p)
- double *resp, prod_wk[MXDIM][2*MXDEG+1];
- int dim, deg, p;
- { double prod;
- int i, j, k, j1, k1;
- prod = 1.0;
- for (i=0; i<dim; i++) prod *= prod_wk[i][0];
- resp[0] += prod;
- if (deg==0) return;
- for (j1=1; j1<=deg; j1++)
- { for (j=0; j<dim; j++)
- { prod = 1.0;
- for (i=0; i<dim; i++) prod *= prod_wk[i][j1*(j==i)];
- prod /= fact[j1];
- resp[1 + (j1-1)*dim +j] += prod;
- }
- }
- for (k1=1; k1<=deg; k1++)
- for (j1=k1; j1<=deg; j1++)
- { for (k=0; k<dim; k++)
- for (j=0; j<dim; j++)
- { prod = 1.0;
- for (i=0; i<dim; i++) prod *= prod_wk[i][k1*(k==i) + j1*(j==i)];
- prod /= fact[k1]*fact[j1];
- resp[ (1+(k1-1)*dim+k)*p + 1+(j1-1)*dim+j] += prod;
- }
- }
- }
- int prodint(t,resp,resp2,coef,h)
- double *t, *resp, *resp2, *coef, h;
- { int dim, p, i, j, k, st;
- double cf[MXDEG+1], hj, hs, prod_wk[MXDIM][2*MXDEG+1];
- dim = den_lfd->d;
- p = den_des->p;
- for (i=0; i<p*p; i++) resp[i] = 0.0;
- cf[0] = coef[0];
- /* compute the one dimensional terms
- */
- for (i=0; i<dim; i++)
- { hj = 1; hs = h*den_lfd->sca[i];
- for (j=0; j<deg(den_sp); j++)
- { hj *= hs;
- cf[j+1] = hj*coef[ j*dim+i+1 ];
- }
- st = onedint(den_sp,cf,ilim[i]/hs,ilim[i+dim]/hs,prod_wk[i]);
- if (st==LF_BADP) return(st);
- hj = 1;
- for (j=0; j<=2*deg(den_sp); j++)
- { hj *= hs;
- prod_wk[i][j] *= hj;
- }
- cf[0] = 0.0; /* so we only include it once, when d>=2 */
- }
- /* transfer to the resp array
- */
- prodintresp(resp,prod_wk,dim,deg(den_sp),p);
- /* Symmetrize.
- */
- for (k=0; k<p; k++)
- for (j=k; j<p; j++)
- resp[j*p+k] = resp[k*p+j];
- return(st);
- }
- int gausint(t,resp,C,cf,h,sca)
- double *t, *resp, *C, *cf, h, *sca;
- { double nb, det, z, *P;
- int d, p, i, j, k, l, m1, m2, f;
- d = den_lfd->d; p = den_des->p;
- m1 = d+1; nb = 0;
- P = &C[d*d];
- resp[0] = 1;
- for (i=0; i<d; i++)
- { C[i*d+i] = SQR(GFACT/(h*sca[i]))-cf[m1++];
- for (j=i+1; j<d; j++) C[i*d+j] = C[j*d+i] = -cf[m1++];
- }
- eig_dec(C,P,d);
- det = 1;
- for (i=1; i<=d; i++)
- { det *= C[(i-1)*(d+1)];
- if (det <= 0) return(LF_BADP);
- resp[i] = cf[i];
- for (j=1; j<=d; j++) resp[j+i*p] = 0;
- resp[i+i*p] = 1;
- svdsolve(&resp[i*p+1],u,P,C,P,d,0.0);
- }
- svdsolve(&resp[1],u,P,C,P,d,0.0);
- det = sqrt(det);
- for (i=1; i<=d; i++)
- { nb += cf[i]*resp[i];
- resp[i*p] = resp[i];
- for (j=1; j<=d; j++)
- resp[i+p*j] += resp[i]*resp[j];
- }
- m1 = d;
- for (i=1; i<=d; i++)
- for (j=i; j<=d; j++)
- { m1++; f = 1+(i==j);
- resp[m1] = resp[m1*p] = resp[i*p+j]/f;
- m2 = d;
- for (k=1; k<=d; k++)
- { resp[m1+k*p] = resp[k+m1*p] =
- ( resp[i]*resp[j*p+k] + resp[j]*resp[i*p+k]
- + resp[k]*resp[i*p+j] - 2*resp[i]*resp[j]*resp[k] )/f;
- for (l=k; l<=d; l++)
- { m2++; f = (1+(i==j))*(1+(k==l));
- resp[m1+m2*p] = resp[m2+m1*p] = ( resp[i+j*p]*resp[k+l*p]
- + resp[i+k*p]*resp[j+l*p] + resp[i+l*p]*resp[j+k*p]
- - 2*resp[i]*resp[j]*resp[k]*resp[l] )/f;
- } } }
- z = mut_exp(d*0.918938533+cf[0]+nb/2)/det;
- multmatscal(resp,z,p*p);
- return(LF_OK);
- }
- int likeden(coef, lk0, f1, A)
- double *coef, *lk0, *f1, *A;
- { double lk, r;
- int i, j, p, rstat;
- lf_status = LF_OK;
- p = den_des->p;
- if ((link(den_sp)==LIDENT) && (coef[0] != 0.0)) return(NR_BREAK);
- lf_status = (den_des->itype)(den_des->xev,A,den_des->xtwx.Q,coef,den_des->h);
- if (lf_error) lf_status = LF_ERR;
- if (lf_status==LF_BADP)
- { *lk0 = -1.0e300;
- return(NR_REDUCE);
- }
- if (lf_status!=LF_OK) return(NR_BREAK);
- if (lf_debug>2) prresp(coef,A,p);
- den_des->xtwx.p = p;
- rstat = NR_OK;
- switch(link(den_sp))
- { case LLOG:
- r = den_des->ss[0]/A[0];
- coef[0] += log(r);
- multmatscal(A,r,p*p);
- A[0] = den_des->ss[0];
- lk = -A[0];
- if (fabs(coef[0]) > 700)
- { lf_status = LF_OOB;
- rstat = NR_REDUCE;
- }
- for (i=0; i<p; i++)
- { lk += coef[i]*den_des->ss[i];
- f1[i] = den_des->ss[i]-A[i];
- }
- break;
- case LIDENT:
- lk = 0.0;
- for (i=0; i<p; i++)
- { f1[i] = den_des->ss[i];
- for (j=0; j<p; j++)
- den_des->res[i] -= A[i*p+j]*coef[j];
- }
- break;
- }
- *lk0 = den_des->llk = lk;
- return(rstat);
- }
- int inre(x,bound,d)
- double *x, *bound;
- int d;
- { int i, z;
- z = 1;
- for (i=0; i<d; i++)
- if (bound[i]<bound[i+d])
- z &= (x[i]>=bound[i]) & (x[i]<=bound[i+d]);
- return(z);
- }
- int setintlimits(lfd, x, h, ang, lset)
- lfdata *lfd;
- int *ang, *lset;
- double *x, h;
- { int d, i;
- d = lfd->d;
- *ang = *lset = 0;
- for (i=0; i<d; i++)
- { if (lfd->sty[i]==STANGL)
- { ilim[i+d] = ((h<2) ? 2*asin(h/2) : PI)*lfd->sca[i];
- ilim[i] = -ilim[i+d];
- *ang = 1;
- }
- else
- { ilim[i+d] = h*lfd->sca[i];
- ilim[i] = -ilim[i+d];
- if (lfd->sty[i]==STLEFT) { ilim[i+d] = 0; *lset = 1; }
- if (lfd->sty[i]==STRIGH) { ilim[i] = 0; *lset = 1; }
- if (lfd->xl[i]<lfd->xl[i+d]) /* user limits for this variable */
- { if (lfd->xl[i]-x[i]> ilim[i])
- { ilim[i] = lfd->xl[i]-x[i]; *lset=1; }
- if (lfd->xl[i+d]-x[i]< ilim[i+d])
- { ilim[i+d] = lfd->xl[i+d]-x[i]; *lset=1; }
- }
- }
- if (ilim[i]==ilim[i+d]) return(LF_DEMP); /* empty integration */
- }
- return(LF_OK);
- }
- int selectintmeth(itype,lset,ang)
- int itype, lset, ang;
- {
- if (itype==IDEFA) /* select the default method */
- { if (fam(den_sp)==THAZ)
- { if (ang) return(IDEFA);
- return( IHAZD );
- }
- if (ubas(den_sp)) return(IMULT);
- if (ang) return(IMULT);
- if (iscompact(ker(den_sp)))
- { if (kt(den_sp)==KPROD) return(IPROD);
- if (lset)
- return( (den_lfd->d==1) ? IPROD : IMULT );
- if (deg(den_sp)<=1) return(IMLIN);
- if (den_lfd->d==1) return(IPROD);
- return(IMULT);
- }
- if (ker(den_sp)==WGAUS)
- { if (lset) WARN(("Integration for Gaussian weights ignores limits"));
- if ((den_lfd->d==1)|(kt(den_sp)==KPROD)) return(IPROD);
- if (deg(den_sp)<=1) return(IMLIN);
- if (deg(den_sp)==2) return(IMULT);
- }
- return(IDEFA);
- }
- /* user provided an integration method, check it is valid */
- if (fam(den_sp)==THAZ)
- { if (ang) return(INVLD);
- if (!iscompact(ker(den_sp))) return(INVLD);
- return( ((kt(den_sp)==KPROD) | (kt(den_sp)==KSPH)) ? IHAZD : INVLD );
- }
- if ((ang) && (itype != IMULT)) return(INVLD);
- switch(itype)
- { case IMULT:
- if (ker(den_sp)==WGAUS) return(deg(den_sp)==2);
- return( iscompact(ker(den_sp)) ? IMULT : INVLD );
- case IPROD: return( ((den_lfd->d==1) | (kt(den_sp)==KPROD)) ? IPROD : INVLD );
- case IMLIN: return( ((kt(den_sp)==KSPH) && (!lset) &&
- (deg(den_sp)<=1)) ? IMLIN : INVLD );
- }
- return(INVLD);
- }
- extern double lf_tol;
- int densinit(lfd,des,sp)
- lfdata *lfd;
- design *des;
- smpar *sp;
- { int p, i, ii, j, nnz, rnz, ang, lset, status;
- double w, *cf;
- den_lfd = lfd;
- den_des = des;
- den_sp = sp;
- cf = des->cf;
- lf_tol = (link(sp)==LLOG) ? 1.0e-6 : 0.0;
- p = des->p;
- ff = des->xtwx.wk;
- cf[0] = NOSLN;
- for (i=1; i<p; i++) cf[i] = 0.0;
- if (!inre(des->xev,lfd->xl,lfd->d)) return(LF_XOOR);
- status = setintlimits(lfd,des->xev,des->h,&ang,&lset);
- if (status != LF_OK) return(status);
- switch(selectintmeth(de_itype,lset,ang))
- { case IMULT: des->itype = multint; break;
- case IPROD: des->itype = prodint; break;
- case IMLIN: des->itype = mlinint; break;
- case IHAZD: des->itype = hazint; break;
- case INVLD: LERR(("Invalid integration method %d",de_itype));
- break;
- case IDEFA: LERR(("No integration type available for this model"));
- break;
- default: LERR(("densinit: unknown integral type"));
- }
- switch(deg(den_sp))
- { case 0: rnz = 1; break;
- case 1: rnz = 1; break;
- case 2: rnz = lfd->d+1; break;
- case 3: rnz = lfd->d+2; break;
- default: LERR(("densinit: invalid degree %d",deg(den_sp)));
- }
- if (lf_error) return(LF_ERR);
- setzero(des->ss,p);
- nnz = 0;
- for (i=0; i<des->n; i++)
- { ii = des->ind[i];
- if (!cens(lfd,ii))
- { w = wght(des,ii)*prwt(lfd,ii);
- for (j=0; j<p; j++) des->ss[j] += d_xij(des,ii,j)*w;
- if (wght(des,ii)>0.00001) nnz++;
- } }
- if (fam(den_sp)==THAZ) haz_init(lfd,des,sp,ilim);
- /* this should really only be done once. Not sure how to enforce that,
- * esp. when locfit() has been called directly.
- */
- if (fam(den_sp)==TDEN)
- des->smwt = (lfd->w==NULL) ? lfd->n : vecsum(lfd->w,lfd->n);
- if (lf_debug>2)
- { mut_printf(" LHS: ");
- for (i=0; i<p; i++) mut_printf(" %8.5f",des->ss[i]);
- mut_printf("\n");
- }
- switch(link(den_sp))
- { case LIDENT:
- cf[0] = 0.0;
- return(LF_OK);
- case LLOG:
- if (nnz<rnz) { cf[0] = -1000; return(LF_DNOP); }
- cf[0] = 0.0;
- return(LF_OK);
- default:
- LERR(("unknown link in densinit"));
- return(LF_ERR);
- }
- }
- /*
- * Copyright 1996-2006 Catherine Loader.
- */
- #include "locf.h"
- int bino_vallink(link)
- int link;
- { return((link==LLOGIT) | (link==LIDENT) | (link==LASIN));
- }
- int bino_fam(y,p,th,link,res,cens,w)
- double y, p, th, *res, w;
- int link, cens;
- { double wp;
- if (link==LINIT)
- { if (y<0) y = 0;
- if (y>w) y = w;
- res[ZDLL] = y;
- return(LF_OK);
- }
- wp = w*p;
- if (link==LIDENT)
- { if ((p<=0) && (y>0)) return(LF_BADP);
- if ((p>=1) && (y<w)) return(LF_BADP);
- res[ZLIK] = res[ZDLL] = res[ZDDLL] = 0.0;
- if (y>0)
- { res[ZLIK] += y*log(wp/y);
- res[ZDLL] += y/p;
- res[ZDDLL]+= y/(p*p);
- }
- if (y<w)
- { res[ZLIK] += (w-y)*log((w-wp)/(w-y));
- res[ZDLL] -= (w-y)/(1-p);
- res[ZDDLL]+= (w-y)/SQR(1-p);
- }
- return(LF_OK);
- }
- if (link==LLOGIT)
- { if ((y<0) | (y>w)) /* goon observation; delete it */
- { res[ZLIK] = res[ZDLL] = res[ZDDLL] = 0.0;
- return(LF_OK);
- }
- res[ZLIK] = (th<0) ? th*y-w*log(1+exp(th)) : th*(y-w)-w*log(1+exp(-th));
- if (y>0) res[ZLIK] -= y*log(y/w);
- if (y<w) res[ZLIK] -= (w-y)*log(1-y/w);
- res[ZDLL] = (y-wp);
- res[ZDDLL]= wp*(1-p);
- return(LF_OK);
- }
- if (link==LASIN)
- { if ((p<=0) && (y>0)) return(LF_BADP);
- if ((p>=1) && (y<w)) return(LF_BADP);
- if ((th<0) | (th>PI/2)) return(LF_BADP);
- res[ZDLL] = res[ZDDLL] = res[ZLIK] = 0;
- if (y>0)
- { res[ZDLL] += 2*y*sqrt((1-p)/p);
- res[ZLIK] += y*log(wp/y);
- }
- if (y<w)
- { res[ZDLL] -= 2*(w-y)*sqrt(p/(1-p));
- res[ZLIK] += (w-y)*log((w-wp)/(w-y));
- }
- res[ZDDLL] = 4*w;
- return(LF_OK);
- }
- LERR(("link %d invalid for binomial family",link));
- return(LF_LNK);
- }
- int bino_check(sp,des,lfd)
- smpar *sp;
- design *des;
- lfdata *lfd;
- { int i, ii;
- double t0, t1;
- if (fabs(des->cf[0])>700) return(LF_OOB);
- /* check for separation.
- * this won't detect separation if there's boundary points with
- * both 0 and 1 responses.
- */
- t0 = -1e100; t1 = 1e100;
- for (i=0; i<des->n; i++)
- { ii = des->ind[i];
- if ((resp(lfd,ii)<prwt(lfd,ii)) && (fitv(des,ii) > t0)) t0 = fitv(des,ii);
- if ((resp(lfd,ii)>0) && (fitv(des,ii) < t1)) t1 = fitv(des,ii);
- if (t1 <= t0) return(LF_OK);
- }
- mut_printf("separated %8.5f %8.5f\n",t0,t1);
- return(LF_NSLN);
- }
- void setfbino(fam)
- family *fam;
- { fam->deflink = LLOGIT;
- fam->canlink = LLOGIT;
- fam->vallink = bino_vallink;
- fam->family = bino_fam;
- fam->pcheck = bino_check;
- }
- int rbin_vallink(link)
- int link;
- { return(link==LLOGIT);
- }
- int rbin_fam(y,p,th,link,res,cens,w)
- double y, p, th, *res, w;
- int link, cens;
- { double s2y;
- if (link==LINIT)
- { res[ZDLL] = y;
- return(LF_OK);
- }
- if ((y<0) | (y>w)) /* goon observation; delete it */
- { res[ZLIK] = res[ZDLL] = res[ZDDLL] = 0.0;
- return(LF_OK);
- }
- res[ZLIK] = (th<0) ? th*y-w*log(1+exp(th)) : th*(y-w)-w*log(1+exp(-th));
- if (y>0) res[ZLIK] -= y*log(y/w);
- if (y<w) res[ZLIK] -= (w-y)*log(1-y/w);
- res[ZDLL] = (y-w*p);
- res[ZDDLL]= w*p*(1-p);
- if (-res[ZLIK]>HUBERC*HUBERC/2.0)
- { s2y = sqrt(-2*res[ZLIK]);
- res[ZLIK] = HUBERC*(HUBERC/2.0-s2y);
- res[ZDLL] *= HUBERC/s2y;
- res[ZDDLL] = HUBERC/s2y*(res[ZDDLL]-1/(s2y*s2y)*w*p*(1-p));
- }
- return(LF_OK);
- }
- void setfrbino(fam)
- family *fam;
- { fam->deflink = LLOGIT;
- fam->canlink = LLOGIT;
- fam->vallink = rbin_vallink;
- fam->family = rbin_fam;
- fam->pcheck = bino_check;
- }
- /*
- * Copyright 1996-2006 Catherine Loader.
- */
- #include "locf.h"
- int circ_vallink(link)
- int link;
- { return(link==LIDENT);
- }
- int circ_fam(y,mean,th,link,res,cens,w)
- double y, mean, th, *res, w;
- int link, cens;
- { if (link==LINIT)
- { res[ZDLL] = w*sin(y);
- res[ZLIK] = w*cos(y);
- return(LF_OK);
- }
- res[ZDLL] = w*sin(y-mean);
- res[ZDDLL]= w*cos(y-mean);
- res[ZLIK] = res[ZDDLL]-w;
- return(LF_OK);
- }
- extern double lf_tol;
- int circ_init(lfd,des,sp)
- lfdata *lfd;
- design *des;
- smpar *sp;
- { int i, ii;
- double s0, s1;
- s0 = s1 = 0.0;
- for (i=0; i<des->n; i++)
- { ii = des->ind[i];
- s0 += wght(des,ii)*prwt(lfd,ii)*sin(resp(lfd,ii)-base(lfd,ii));
- s1 += wght(des,ii)*prwt(lfd,ii)*cos(resp(lfd,ii)-base(lfd,ii));
- }
- des->cf[0] = atan2(s0,s1);
- for (i=1; i<des->p; i++) des->cf[i] = 0.0;
- lf_tol = 1.0e-6;
- return(LF_OK);
- }
- void setfcirc(fam)
- family *fam;
- { fam->deflink = LIDENT;
- fam->canlink = LIDENT;
- fam->vallink = circ_vallink;
- fam->family = circ_fam;
- fam->initial = circ_init;
- }
- /*
- * Copyright 1996-2006 Catherine Loader.
- */
- #include "locf.h"
- int dens_vallink(link)
- int link;
- { return((link==LIDENT) | (link==LLOG));
- }
- int dens_fam(y,mean,th,link,res,cens,w)
- double y, mean, th, *res, w;
- int link, cens;
- { if (cens)
- res[ZLIK] = res[ZDLL] = res[ZDDLL] = 0.0;
- else
- { res[ZLIK] = w*th;
- res[ZDLL] = res[ZDDLL] = w;
- }
- return(LF_OK);
- }
- void setfdensity(fam)
- family *fam;
- { fam->deflink = LLOG;
- fam->canlink = LLOG;
- fam->vallink = dens_vallink;
- fam->family = dens_fam;
- fam->initial = densinit;
- fam->like = likeden;
- }
- /*
- * Copyright 1996-2006 Catherine Loader.
- */
- #include "locf.h"
- int gamma_vallink(link)
- int link;
- { return((link==LIDENT) | (link==LLOG) | (link==LINVER));
- }
- int gamma_fam(y,mean,th,link,res,cens,w)
- double y, mean, th, *res, w;
- int link, cens;
- { double lb, pt, dg;
- if (link==LINIT)
- { res[ZDLL] = MAX(y,0.0);
- return(LF_OK);
- }
- res[ZLIK] = res[ZDLL] = res[ZDDLL] = 0.0;
- if (w==0.0) return(LF_OK);
- if ((mean<=0) & (y>0)) return(LF_BADP);
- if (link==LIDENT) lb = 1/th;
- if (link==LINVER) lb = th;
- if (link==LLOG) lb = mut_exp(-th);
- if (cens)
- { if (y<=0) return(LF_OK);
- pt = 1-igamma(lb*y,w);
- dg = dgamma(lb*y,w,1.0,0);
- res[ZLIK] = log(pt);
- res[ZDLL] = -y*dg/pt;
- /*
- * res[ZDLL] = -y*dg/pt * dlb/dth.
- * res[ZDDLL] = y*dg/pt * (d2lb/dth2 + ((w-1)/lb-y)*(dlb/dth)^2)
- * + res[ZDLL]^2.
- */
- if (link==LLOG) /* lambda = exp(-theta) */
- { res[ZDLL] *= -lb;
- res[ZDDLL] = dg*y*lb*(w-lb*y)/pt + SQR(res[ZDLL]);
- return(LF_OK);
- }
- if (link==LINVER) /* lambda = theta */
- { res[ZDLL] *= 1.0;
- res[ZDDLL] = dg*y*((w-1)*mean-y)/pt + SQR(res[ZDLL]);
- return(LF_OK);
- }
- if (link==LIDENT) /* lambda = 1/theta */
- { res[ZDLL] *= -lb*lb;
- res[ZDDLL] = dg*y*lb*lb*lb*(1+w-lb*y)/pt + SQR(res[ZDLL]);
- return(LF_OK);
- }
- }
- else
- { if (y<0) WARN(("Negative Gamma observation"));
- if (link==LLOG)
- { res[ZLIK] = -lb*y+w*(1-th);
- if (y>0) res[ZLIK] += w*log(y/w);
- res[ZDLL] = lb*y-w;
- res[ZDDLL]= lb*y;
- return(LF_OK);
- }
- if (link==LINVER)
- { res[ZLIK] = -lb*y+w-w*log(mean);
- if (y>0) res[ZLIK] += w*log(y/w);
- res[ZDLL] = -y+w*mean;
- res[ZDDLL]= w*mean*mean;
- return(LF_OK);
- }
- if (link==LIDENT)
- { res[ZLIK] = -lb*y+w-w*log(mean);
- if (y>0) res[ZLIK] += w*log(y/w);
- res[ZDLL] = lb*lb*(y-w*mean);
- res[ZDDLL]= lb*lb*lb*(2*y-w*mean);
- return(LF_OK);
- }
- }
- LERR(("link %d invalid for Gamma family",link));
- return(LF_LNK);
- }
- void setfgamma(fam)
- family *fam;
- { fam->deflink = LLOG;
- fam->canlink = LINVER;
- fam->vallink = gamma_vallink;
- fam->family = gamma_fam;
- }
- /*
- * Copyright 1996-2006 Catherine Loader.
- */
- #include "locf.h"
- int gaus_vallink(link)
- int link;
- { return((link==LIDENT) | (link==LLOG) | (link==LLOGIT));
- }
- int gaus_fam(y,mean,th,link,res,cens,w)
- double y, mean, th, *res, w;
- int link, cens;
- { double z, pz, dp;
- if (link==LINIT)
- { res[ZDLL] = w*y;
- return(LF_OK);
- }
- z = y-mean;
- if (cens)
- { if (link!=LIDENT)
- { LERR(("Link invalid for censored Gaussian family"));
- return(LF_LNK);
- }
- pz = mut_pnorm(-z);
- dp = ((z>6) ? ptail(-z) : exp(-z*z/2)/pz)/2.5066283;
- res[ZLIK] = w*log(pz);
- res[ZDLL] = w*dp;
- res[ZDDLL]= w*dp*(dp-z);
- return(LF_OK);
- }
- res[ZLIK] = -w*z*z/2;
- switch(link)
- { case LIDENT:
- res[ZDLL] = w*z;
- res[ZDDLL]= w;
- break;
- case LLOG:
- res[ZDLL] = w*z*mean;
- res[ZDDLL]= w*mean*mean;
- break;
- case LLOGIT:
- res[ZDLL] = w*z*mean*(1-mean);
- res[ZDDLL]= w*mean*mean*(1-mean)*(1-mean);
- break;
- default:
- LERR(("Invalid link for Gaussian family"));
- return(LF_LNK);
- }
- return(LF_OK);
- }
- int gaus_check(sp,des,lfd)
- smpar *sp;
- design *des;
- lfdata *lfd;
- { int i, ii;
- if (fami(sp)->robust) return(LF_OK);
- if (link(sp)==LIDENT)
- { for (i=0; i<des->n; i++)
- { ii = des->ind[i];
- if (cens(lfd,ii)) return(LF_OK);
- }
- return(LF_DONE);
- }
- return(LF_OK);
- }
- void setfgauss(fam)
- family *fam;
- { fam->deflink = LIDENT;
- fam->canlink = LIDENT;
- fam->vallink = gaus_vallink;
- fam->family = gaus_fam;
- fam->pcheck = gaus_check;
- }
- /*
- * Copyright 1996-2006 Catherine Loader.
- */
- #include "locf.h"
- int geom_vallink(link)
- int link;
- { return((link==LIDENT) | (link==LLOG));
- }
- int geom_fam(y,mean,th,link,res,cens,w)
- double y, mean, th, *res, w;
- int link, cens;
- { double p, pt, dp, p1;
- if (link==LINIT)
- { res[ZDLL] = MAX(y,0.0);
- return(LF_OK);
- }
- p = 1/(1+mean);
- if (cens) /* censored observation */
- { if (y<=0)
- { res[ZLIK] = res[ZDLL] = res[ZDDLL] = 0;
- return(LF_OK);
- }
- p1 = (link==LIDENT) ? -p*p : -p*(1-p);
- pt = 1-ibeta(p,w,y);
- dp = dbeta(p,w,y,0)/pt;
- res[ZLIK] = log(pt);
- res[ZDLL] = -dp*p1;
- res[ZDDLL] = dp*dp*p1*p1;
- if (link==LIDENT)
- res[ZDDLL] += dp*p*p*p*(1+w*(1-p)-p*y)/(1-p);
- else
- res[ZDDLL] += dp*p*(1-p)*(w*(1-p)-p*y);
- return(LF_OK);
- }
- else
- { res[ZLIK] = (y+w)*log((y/w+1)/(mean+1));
- if (y>0) res[ZLIK] += y*log(w*mean/y);
- if (link==LLOG)
- { res[ZDLL] = (y-w*mean)*p;
- res[ZDDLL]= (y+w)*p*(1-p);
- return(LF_OK);
- }
- if (link==LIDENT)
- { res[ZDLL] = (y-w*mean)/(mean*(1+mean));
- res[ZDDLL]= w/(mean*(1+mean));
- return(LF_OK);
- }
- }
- LERR(("link %d invalid for geometric family",link));
- return(LF_LNK);
- }
- void setfgeom(fam)
- family *fam;
- { fam->deflink = LLOG;
- fam->canlink = LIDENT; /* this isn't correct. I haven't prog. canon */
- fam->vallink = geom_vallink;
- fam->family = geom_fam;
- }
- /*
- * Copyright 1996-2006 Catherine Loader.
- */
- #include "locf.h"
- #define HUBERC 2.0
- double links_rs;
- int inllmix=0;
- /*
- * lffamily("name") converts family names into a numeric value.
- * typical usage is fam(&lf->sp) = lffamily("gaussian");
- * Note that family can be preceded by q and/or r for quasi, robust.
- *
- * link(&lf->sp) = lflink("log") does the same for the link function.
- */
- #define NFAMILY 18
- static char *famil[NFAMILY] =
- { "density", "ate", "hazard", "gaussian", "binomial",
- "poisson", "gamma", "geometric", "circular", "obust", "huber",
- "weibull", "cauchy","probab", "logistic", "nbinomial",
- "vonmises", "quant" };
- static int fvals[NFAMILY] =
- { TDEN, TRAT, THAZ, TGAUS, TLOGT,
- TPOIS, TGAMM, TGEOM, TCIRC, TROBT, TROBT,
- TWEIB, TCAUC, TPROB, TLOGT, TGEOM, TCIRC, TQUANT };
- int lffamily(z)
- char *z;
- { int quasi, robu, f;
- quasi = robu = 0;
- while ((z[0]=='q') | (z[0]=='r'))
- { quasi |= (z[0]=='q');
- robu |= (z[0]=='r');
- z++;
- }
- z[0] = tolower(z[0]);
- f = pmatch(z,famil,fvals,NFAMILY,-1);
- if ((z[0]=='o') | (z[0]=='a')) robu = 0;
- if (f==-1)
- { WARN(("unknown family %s",z));
- f = TGAUS;
- }
- if (quasi) f += 64;
- if (robu) f += 128;
- return(f);
- }
- #define NLINKS 8
- static char *ltype[NLINKS] = { "default", "canonical", "identity", "log",
- "logi", "inverse", "sqrt", "arcsin" };
- static int lvals[NLINKS] = { LDEFAU, LCANON, LIDENT, LLOG,
- LLOGIT, LINVER, LSQRT, LASIN };
- int lflink(char *z)
- { int f;
- if (z==NULL) return(LDEFAU);
- z[0] = tolower(z[0]);
- f = pmatch(z, ltype, lvals, NLINKS, -1);
- if (f==-1)
- { WARN(("unknown link %s",z));
- f = LDEFAU;
- }
- return(f);
- }
- int defaultlink(link,fam)
- int link;
- family *fam;
- { if (link==LDEFAU) return(fam->deflink);
- if (link==LCANON) return(fam->canlink);
- return(link);
- }
- /*
- void robustify(res,rs)
- double *res, rs;
- { double sc, z;
- sc = rs*HUBERC;
- if (res[ZLIK] > -sc*sc/2) return;
- z = sqrt(-2*res[ZLIK]);
- res[ZDDLL]= -sc*res[ZDLL]*res[ZDLL]/(z*z*z)+sc*res[ZDDLL]/z;
- res[ZDLL]*= sc/z;
- res[ZLIK] = sc*sc/2-sc*z;
- }
- */
- void robustify(res,rs)
- double *res, rs;
- { double sc, z;
- sc = rs*HUBERC;
- if (res[ZLIK] > -sc*sc/2)
- { res[ZLIK] /= sc*sc;
- res[ZDLL] /= sc*sc;
- res[ZDDLL] /= sc*sc;
- return;
- }
- z = sqrt(-2*res[ZLIK]);
- res[ZDDLL]= (-sc*res[ZDLL]*res[ZDLL]/(z*z*z)+sc*res[ZDDLL]/z)/(sc*sc);
- res[ZDLL]*= 1.0/(z*sc);
- res[ZLIK] = 0.5-z/sc;
- }
- double lf_link(y,lin)
- double y;
- int lin;
- { switch(lin)
- { case LIDENT: return(y);
- case LLOG: return(log(y));
- case LLOGIT: return(logit(y));
- case LINVER: return(1/y);
- case LSQRT: return(sqrt(fabs(y)));
- case LASIN: return(asin(sqrt(y)));
- }
- LERR(("link: unknown link %d",lin));
- return(0.0);
- }
- double invlink(th,lin)
- double th;
- int lin;
- { switch(lin)
- { case LIDENT: return(th);
- case LLOG: return(mut_exp(th));
- case LLOGIT: return(expit(th));
- case LINVER: return(1/th);
- case LSQRT: return(th*fabs(th));
- case LASIN: return(sin(th)*sin(th));
- case LINIT: return(0.0);
- }
- LERR(("invlink: unknown link %d",lin));
- return(0.0);
- }
- /* the link and various related functions */
- int links(th,y,fam,link,res,c,w,rs)
- double th, y, *res, w, rs;
- int link, c;
- family *fam;
- { double mean;
- int st;
- mean = res[ZMEAN] = invlink(th,link);
- if (lf_error) return(LF_LNK);
- links_rs = rs;
- /* mut_printf("links: rs %8.5f\n",rs); */
- st = fam->family(y,mean,th,link,res,c,w);
- if (st!=LF_OK) return(st);
- if (link==LINIT) return(st);
- if (isrobust(fam)) robustify(res,rs);
- return(st);
- }
- /*
- stdlinks is a version of links when family, link, response e.t.c
- all come from the standard places.
- */
- int stdlinks(res,lfd,sp,i,th,rs)
- lfdata *lfd;
- smpar *sp;
- double th, rs, *res;
- int i;
- {
- return(links(th,resp(lfd,i),fami(sp),link(sp),res,cens(lfd,i),prwt(lfd,i),rs));
- }
- /*
- * functions used in variance, skewness, kurtosis calculations
- * in scb corrections.
- */
- double b2(th,tg,w)
- double th, w;
- int tg;
- { double y;
- switch(tg&63)
- { case TGAUS: return(w);
- case TPOIS: return(w*mut_exp(th));
- case TLOGT:
- y = expit(th);
- return(w*y*(1-y));
- }
- LERR(("b2: invalid family %d",tg));
- return(0.0);
- }
- double b3(th,tg,w)
- double th, w;
- int tg;
- { double y;
- switch(tg&63)
- { case TGAUS: return(0.0);
- case TPOIS: return(w*mut_exp(th));
- case TLOGT:
- y = expit(th);
- return(w*y*(1-y)*(1-2*y));
- }
- LERR(("b3: invalid family %d",tg));
- return(0.0);
- }
- double b4(th,tg,w)
- double th, w;
- int tg;
- { double y;
- switch(tg&63)
- { case TGAUS: return(0.0);
- case TPOIS: return(w*mut_exp(th));
- case TLOGT:
- y = expit(th); y = y*(1-y);
- return(w*y*(1-6*y));
- }
- LERR(("b4: invalid family %d",tg));
- return(0.0);
- }
- int def_check(sp,des,lfd)
- smpar *sp;
- design *des;
- lfdata *lfd;
- { switch(link(sp))
- { case LLOG: if (des->cf[0]>700) return(LF_OOB);
- break;
- }
- return(LF_OK);
- }
- extern void setfdensity(), setfgauss(), setfbino(), setfpoisson();
- extern void setfgamma(), setfgeom(), setfcirc(), setfweibull();
- extern void setfrbino(), setfrobust(), setfcauchy(), setfquant();
- void setfamily(sp)
- smpar *sp;
- { int tg, lnk;
- family *f;
- tg = fam(sp);
- f = fami(sp);
- f->quasi = tg&64;
- f->robust = tg&128;
- f->initial = reginit;
- f->like = likereg;
- f->pcheck = def_check;
- switch(tg&63)
- { case TDEN:
- case THAZ:
- case TRAT: setfdensity(f); break;
- case TGAUS: setfgauss(f); break;
- case TLOGT: setfbino(f); break;
- case TRBIN: setfrbino(f); break;
- case TPROB:
- case TPOIS: setfpoisson(f); break;
- case TGAMM: setfgamma(f); break;
- case TGEOM: setfgeom(f); break;
- case TWEIB: setfweibull(f);
- case TCIRC: setfcirc(f); break;
- case TROBT: setfrobust(f); break;
- case TCAUC: setfcauchy(f); break;
- case TQUANT: setfquant(f); break;
- default: LERR(("setfamily: unknown family %d",tg&63));
- return;
- }
-
- lnk = defaultlink(link(sp),f);
- if (!f->vallink(lnk))
- { WARN(("setfamily: invalid link %d - revert to default",link(sp)));
- link(sp) = f->deflink;
- }
- else
- link(sp) = lnk;
- }
- /*
- * Copyright 1996-2006 Catherine Loader.
- */
- #include "locf.h"
- int pois_vallink(link)
- int link;
- { return((link==LLOG) | (link==LIDENT) | (link==LSQRT));
- }
- int pois_fam(y,mean,th,link,res,cens,w)
- double y, mean, th, *res, w;
- int link, cens;
- { double wmu, pt, dp;
- if (link==LINIT)
- { res[ZDLL] = MAX(y,0.0);
- return(LF_OK);
- }
- wmu = w*mean;
- if (inllmix) y = w*y;
- if (cens)
- { if (y<=0)
- { res[ZLIK] = res[ZDLL] = res[ZDDLL] = 0.0;
- return(LF_OK);
- }
- pt = igamma(wmu,y);
- dp = dgamma(wmu,y,1.0,0)/pt;
- res[ZLIK] = log(pt);
- /*
- * res[ZDLL] = dp * w*dmu/dth
- * res[ZDDLL]= -dp*(w*d2mu/dth2 + (y-1)/mu*(dmu/dth)^2) + res[ZDLL]^2
- */
- if (link==LLOG)
- { res[ZDLL] = dp*wmu;
- res[ZDDLL]= -dp*wmu*(y-wmu) + SQR(res[ZDLL]);
- return(LF_OK);
- }
- if (link==LIDENT)
- { res[ZDLL] = dp*w;
- res[ZDDLL]= -dp*(y-1-wmu)*w/mean + SQR(res[ZDLL]);
- return(LF_OK);
- }
- if (link==LSQRT)
- { res[ZDLL] = dp*2*w*th;
- res[ZDDLL]= -dp*w*(4*y-2-4*wmu) + SQR(res[ZDLL]);
- return(LF_OK);
- } }
- if (link==LLOG)
- { if (y<0) /* goon observation - delete it */
- { res[ZLIK] = res[ZDLL] = res[ZDDLL] = 0;
- return(LF_OK);
- }
- res[ZLIK] = res[ZDLL] = y-wmu;
- if (y>0) res[ZLIK] += y*(th-log(y/w));
- res[ZDDLL] = wmu;
- return(LF_OK);
- }
- if (link==LIDENT)
- { if ((mean<=0) && (y>0)) return(LF_BADP);
- res[ZLIK] = y-wmu;
- res[ZDLL] = -w;
- res[ZDDLL] = 0;
- if (y>0)
- { res[ZLIK] += y*log(wmu/y);
- res[ZDLL] += y/mean;
- res[ZDDLL]= y/(mean*mean);
- }
- return(LF_OK);
- }
- if (link==LSQRT)
- { if ((mean<=0) && (y>0)) return(LF_BADP);
- res[ZLIK] = y-wmu;
- res[ZDLL] = -2*w*th;
- res[ZDDLL]= 2*w;
- if (y>0)
- { res[ZLIK] += y*log(wmu/y);
- res[ZDLL] += 2*y/th;
- res[ZDDLL]+= 2*y/mean;
- }
- return(LF_OK);
- }
- LERR(("link %d invalid for Poisson family",link));
- return(LF_LNK);
- }
- void setfpoisson(fam)
- family *fam;
- { fam->deflink = LLOG;
- fam->canlink = LLOG;
- fam->vallink = pois_vallink;
- fam->family = pois_fam;
- }
- /*
- * Copyright 1996-2006 Catherine Loader.
- */
- #include "locf.h"
- #define QTOL 1.0e-10
- extern int lf_status;
- static double q0;
- int quant_vallink(int link) { return(1); }
- int quant_fam(y,mean,th,link,res,cens,w)
- double y, mean, th, *res, w;
- int link, cens;
- { double z, p;
- if (link==LINIT)
- { res[ZDLL] = w*y;
- return(LF_OK);
- }
- p = 0.5; /* should be pen(sp) */
- z = y-mean;
- res[ZLIK] = (z<0) ? (w*z/p) : (-w*z/(1-p));
- res[ZDLL] = (z<0) ? -w/p : w/(1-p);
- res[ZDDLL]= w/(p*(1-p));
- return(LF_OK);
- }
- int quant_check(sp,des,lfd)
- smpar *sp;
- design *des;
- lfdata *lfd;
- { return(LF_DONE);
- }
- void setfquant(fam)
- family *fam;
- { fam->deflink = LIDENT;
- fam->canlink = LIDENT;
- fam->vallink = quant_vallink;
- fam->family = quant_fam;
- fam->pcheck = quant_check;
- }
- /*
- * cycling rule for choosing among ties.
- */
- int tiecycle(ind,i0,i1,oi)
- int *ind, i0, i1, oi;
- { int i, ii, im;
- im = ind[i0];
- for (i=i0+1; i<=i1; i++)
- { ii = ind[i];
- if (im<=oi)
- { if ((ii<im) | (ii>oi)) im = ii;
- }
- else
- { if ((ii<im) & (ii>oi)) im = ii;
- }
- }
- return(im);
- }
- /*
- * move coefficient vector cf, as far as possible, in direction dc.
- */
- int movecoef(lfd,des,p,cf,dc,oi)
- lfdata *lfd;
- design *des;
- double p, *cf, *dc;
- int oi;
- { int i, ii, im, i0, i1, j;
- double *lb, *el, e, sp, sn, sw, sum1, sum2, tol1;
- lb = des->th;
- el = des->res;
- sum1 = sum2 = 0.0;
- sp = sn = sw = 0.0;
- for (i=0; i<des->n; i++)
- { ii = des->ind[i];
- lb[ii] = innerprod(dc,d_xi(des,ii),des->p);
- e = resp(lfd,ii) - innerprod(cf,d_xi(des,ii),des->p);
- el[ii] = (fabs(lb[ii])<QTOL) ? 1e100 : e/lb[ii];
- if (lb[ii]>0)
- sp += prwt(lfd,ii)*wght(des,ii)*lb[ii];
- else
- sn -= prwt(lfd,ii)*wght(des,ii)*lb[ii];
- sw += prwt(lfd,ii)*wght(des,ii);
- }
- printf("sp %8.5f sn %8.5f\n",sn,sp);
- /* if sn, sp are both zero, should return an LF_PF.
- * but within numerical tolerance? what does it mean?
- */
- if (sn+sp <= QTOL*q0) { lf_status = LF_PF; return(0); }
- sum1 = sp/(1-p) + sn/p;
- tol1 = QTOL*(sp+sn);
- mut_order(el,des->ind,0,des->n-1);
- for (i=0; i<des->n; i++)
- { ii = des->ind[i];
- sum2 += prwt(lfd,ii)*wght(des,ii)*((lb[ii]>0) ? lb[ii]/p : -lb[ii]/(1-p) );
- sum1 -= prwt(lfd,ii)*wght(des,ii)*((lb[ii]>0) ? lb[ii]/(1-p) : -lb[ii]/p );
- if (sum1<=sum2+tol1)
- {
- /* determine the range of ties [i0,i1]
- * el[ind[i0..i1]] = el[ind[i]].
- * if sum1==sum2, el[ind[i+1]]..el[ind[i1]]] = el[ind[i1]], else i1 = i.
- */
- i0 = i1 = i;
- while ((i0>0) && (el[des->ind[i0-1]]==el[ii])) i0--;
- while ((i1<des->n-1) && (el[des->ind[i1+1]]==el[ii])) i1++;
- if (sum1>=sum2-tol1)
- while ((i1<des->n-1) && (el[des->ind[i1+1]]==el[des->ind[i+1]])) i1++;
- if (i0<i1) ii = tiecycle(des->ind,i0,i1,oi);
- for (j=0; j<des->p; j++) cf[j] += el[ii]*dc[j];
- return(ii);
- }
- }
- mut_printf("Big finddlt problem.\n");
- ii = des->ind[des->n-1];
- for (j=0; j<des->p; j++) cf[j] += el[ii]*dc[j];
- return(ii);
- }
- /*
- * special version of movecoef for min/max.
- */
- int movemin(lfd,des,f,cf,dc,oi)
- design *des;
- lfdata *lfd;
- double *cf, *dc, f;
- int oi;
- { int i, ii, im, p, s, ssum;
- double *lb, sum, lb0, lb1, z0, z1;
- lb = des->th;
- s = (f<=0.0) ? 1 : -1;
- /* first, determine whether move should be in positive or negative direction */
- p = des->p;
- sum = 0;
- for (i=0; i<des->n; i++)
- { ii = des->ind[i];
- lb[ii] = innerprod(dc,d_xi(des,ii),des->p);
- sum += prwt(lfd,ii)*wght(des,ii)*lb[ii];
- }
- if (fabs(sum) <= QTOL*q0)
- { lf_status = LF_PF;
- return(0);
- }
- ssum = (sum<=0.0) ? -1 : 1;
- if (ssum != s)
- for (i=0; i<p; i++) dc[i] = -dc[i];
- /* now, move positively. How far can we move? */
- lb0 = 1.0e100; im = oi;
- for (i=0; i<des->n; i++)
- { ii = des->ind[i];
- lb[ii] = innerprod(dc,d_xi(des,ii),des->p); /* must recompute - signs! */
- if (s*lb[ii]>QTOL) /* should have scale-free tolerance here */
- { z0 = innerprod(cf,d_xi(des,ii),p);
- lb1 = (resp(lfd,ii) - z0)/lb[ii];
- if (lb1<lb0)
- { if (fabs(lb1-lb0)<QTOL) /* cycle */
- { if (im<=oi)
- { if ((ii>oi) | (ii<im)) im = ii; }
- else
- { if ((ii>oi) & (ii<im)) im = ii; }
- }
- else
- { im = ii; lb0 = lb1; }
- }
- }
- }
- for (i=0; i<p; i++) cf[i] = cf[i]+lb0*dc[i];
- if (im==-1) lf_status = LF_PF;
- return(im);
- }
- double qll(lfd,spr,des,cf)
- lfdata *lfd;
- smpar *spr;
- design *des;
- double *cf;
- { int i, ii;
- double th, sp, sn, p, e;
- p = pen(spr);
- sp = sn = 0.0;
- for (i=0; i<des->n; i++)
- { ii = des->ind[i];
- th = innerprod(d_xi(des,ii),cf,des->p);
- e = resp(lfd,ii)-th;
- if (e<0) sn -= prwt(lfd,ii)*wght(des,ii)*e;
- if (e>0) sp += prwt(lfd,ii)*wght(des,ii)*e;
- }
- if (p<=0.0) return((sn<QTOL) ? -sp : -1e300);
- if (p>=1.0) return((sp<QTOL) ? -sn : -1e300);
- return(-sp/(1-p)-sn/p);
- }
- /*
- * running quantile smoother.
- */
- void lfquantile(lfd,sp,des,maxit)
- lfdata *lfd;
- smpar *sp;
- design *des;
- int maxit;
- { int i, ii, im, j, k, p, *ci, (*mover)();
- double *cf, *db, *dc, *cm, f, q1, q2, l0;
- printf("in lfquantile\n");
- f = pen(sp);
- p = des->p;
- cf = des->cf;
- dc = des->oc;
- db = des->ss;
- setzero(cf,p);
- setzero(dc,p);
- cm = des->V;
- setzero(cm,p*p);
- ci = (int *)des->fix;
- q1 = -qll(lfd,sp,des,cf);
- if (q1==0.0) { lf_status = LF_PF; return; }
- for (i=0; i<p; i++) cm[i*(p+1)] = 1;
- mover = movecoef;
- if ((f<=0.0) | (f>=1.0)) mover = movemin;
- dc[0] = 1.0;
- im = mover(lfd,des,f,cf,dc,-1);
- if (lf_status != LF_OK) return;
- ci[0] = im;
- printf("init const %2d\n",ci[0]);
- q0 = -qll(lfd,sp,des,cf);
- if (q0<QTOL*q1) { lf_status = LF_PF; return; }
- printf("loop 0\n"); fflush(stdout);
- for (i=1; i<p; i++)
- {
- printf("i %2d\n",i);
- memcpy(&cm[(i-1)*p],d_xi(des,im),p*sizeof(double));
- setzero(db,p);
- db[i] = 1.0;
- resproj(db,cm,dc,p,i);
- printf("call mover\n"); fflush(stdout);
- im = mover(lfd,des,f,cf,dc,-1);
- if (lf_status != LF_OK) return;
- printf("mover %2d\n",im); fflush(stdout);
- ci[i] = im;
- }
- printf("call qll\n"); fflush(stdout);
- q1 = qll(lfd,sp,des,cf);
- printf("loop 1 %d %d %d %d\n",ci[0],ci[1],ci[2],ci[3]); fflush(stdout);
- for (k=0; k<maxit; k++)
- { for (i=0; i<p; i++)
- { for (j=0; j<p; j++)
- if (j!=i) memcpy(&cm[(j-(j>i))*p],d_xi(des,ci[j]),p*sizeof(double));
- memcpy(db,d_xi(des,ci[i]),p*sizeof(double));
- resproj(db,cm,dc,p,p-1);
- printf("call mover\n"); fflush(stdout);
- im = mover(lfd,des,f,cf,dc,ci[i]);
- if (lf_status != LF_OK) return;
- printf("mover %2d\n",im); fflush(stdout);
- ci[i] = im;
- }
- q2 = qll(lfd,sp,des,cf);
- /*
- * convergence: require no change -- reasonable, since discrete?
- * remember we're maximizing, and q's are negative.
- */
- if (q2 <= q1) return;
- q1 = q2;
- }
- printf("loop 2\n");
- mut_printf("Warning: lfquantile not converged.\n");
- }
- /*
- * Copyright 1996-2006 Catherine Loader.
- */
- #include "locf.h"
- extern double links_rs;
- int robust_vallink(link)
- int link;
- { return(link==LIDENT);
- }
- int robust_fam(y,mean,th,link,res,cens,w)
- double y, mean, th, *res, w;
- int link, cens;
- { double z, sw;
- if (link==LINIT)
- { res[ZDLL] = w*y;
- return(LF_OK);
- }
- sw = (w==1.0) ? 1.0 : sqrt(w); /* don't want unnecess. sqrt! */
- z = sw*(y-mean)/links_rs;
- res[ZLIK] = (fabs(z)<HUBERC) ? -z*z/2 : HUBERC*(HUBERC/2.0-fabs(z));
- if (z< -HUBERC)
- { res[ZDLL] = -sw*HUBERC/links_rs;
- res[ZDDLL]= 0.0;
- return(LF_OK);
- }
- if (z> HUBERC)
- { res[ZDLL] = sw*HUBERC/links_rs;
- res[ZDDLL]= 0.0;
- return(LF_OK);
- }
- res[ZDLL] = sw*z/links_rs;
- res[ZDDLL] = w/(links_rs*links_rs);
- return(LF_OK);
- }
- int cauchy_fam(y,p,th,link,res,cens,w)
- double y, p, th, *res, w;
- int link, cens;
- { double z;
- if (link!=LIDENT)
- { LERR(("Invalid link in famcauc"));
- return(LF_LNK);
- }
- z = w*(y-th)/links_rs;
- res[ZLIK] = -log(1+z*z);
- res[ZDLL] = 2*w*z/(links_rs*(1+z*z));
- res[ZDDLL] = 2*w*w*(1-z*z)/(links_rs*links_rs*(1+z*z)*(1+z*z));
- return(LF_OK);
- }
- extern double lf_tol;
- int robust_init(lfd,des,sp)
- lfdata *lfd;
- design *des;
- smpar *sp;
- { int i;
- for (i=0; i<des->n; i++)
- des->res[i] = resp(lfd,(int)des->ind[i]) - base(lfd,(int)des->ind[i]);
- des->cf[0] = median(des->res,des->n);
- for (i=1; i<des->p; i++) des->cf[i] = 0.0;
- lf_tol = 1.0e-6;
- return(LF_OK);
- }
- void setfrobust(fam)
- family *fam;
- { fam->deflink = LIDENT;
- fam->canlink = LIDENT;
- fam->vallink = robust_vallink;
- fam->family = robust_fam;
- fam->initial = robust_init;
- fam->robust = 0;
- }
- void setfcauchy(fam)
- family *fam;
- { fam->deflink = LIDENT;
- fam->canlink = LIDENT;
- fam->vallink = robust_vallink;
- fam->family = cauchy_fam;
- fam->initial = robust_init;
- fam->robust = 0;
- }
- /*
- * Copyright 1996-2006 Catherine Loader.
- */
- #include "locf.h"
- int weibull_vallink(link)
- int link;
- { return((link==LIDENT) | (link==LLOG) | (link==LLOGIT));
- }
- int weibull_fam(y,mean,th,link,res,cens,w)
- double y, mean, th, *res, w;
- int link, cens;
- { double yy;
- yy = pow(y,w);
- if (link==LINIT)
- { res[ZDLL] = MAX(yy,0.0);
- return(LF_OK);
- }
- if (cens)
- { res[ZLIK] = -yy/mean;
- res[ZDLL] = res[ZDDLL] = yy/mean;
- return(LF_OK);
- }
- res[ZLIK] = 1-yy/mean-th;
- if (yy>0) res[ZLIK] += log(w*yy);
- res[ZDLL] = -1+yy/mean;
- res[ZDDLL]= yy/mean;
- return(LF_OK);
- }
- void setfweibull(fam)
- family *fam;
- { fam->deflink = LLOG;
- fam->canlink = LLOG;
- fam->vallink = weibull_vallink;
- fam->family = weibull_fam;
- fam->robust = 0;
- }
- /*
- * Copyright 1996-2006 Catherine Loader.
- */
- /*
- Functions implementing the adaptive bandwidth selection.
- Will make the final call to nbhd() to set smoothing weights
- for selected bandwidth, But will **not** make the
- final call to locfit().
- */
- #include "locf.h"
- static double hmin;
- #define NACRI 5
- static char *atype[NACRI] = { "none", "cp", "ici", "mindex", "ok" };
- static int avals[NACRI] = { ANONE, ACP, AKAT, AMDI, AOK };
- int lfacri(char *z)
- { return(pmatch(z, atype, avals, NACRI, ANONE));
- }
- double adcri(lk,t0,t2,pen)
- double lk, t0, t2, pen;
- { double y;
- /* return(-2*lk/(t0*exp(pen*log(1-t2/t0)))); */
- /* return((-2*lk+pen*t2)/t0); */
- y = (MAX(-2*lk,t0-t2)+pen*t2)/t0;
- return(y);
- }
- double mmse(lfd,sp,dv,des)
- lfdata *lfd;
- smpar *sp;
- deriv *dv;
- design *des;
- { int i, ii, j, p, p1;
- double sv, sb, *l, dp;
- l = des->wd;
- wdiag(lfd, sp, des,l,dv,0,1,0);
- sv = sb = 0;
- p = npar(sp);
- for (i=0; i<des->n; i++)
- { sv += l[i]*l[i];
- ii = des->ind[i];
- dp = dist(des,ii);
- for (j=0; j<deg(sp); j++) dp *= dist(des,ii);
- sb += fabs(l[i])*dp;
- }
- p1 = factorial(deg(sp)+1);
- 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));
- return(sv+sb*sb*pen(sp)*pen(sp)/(p1*p1));
- }
- static double mcp, clo, cup;
- /*
- Initial bandwidth will be (by default)
- k-nearest neighbors for k small, just large enough to
- get defined estimate (unless user provided nonzero nn or fix-h components)
- */
- int ainitband(lfd,sp,dv,des)
- lfdata *lfd;
- smpar *sp;
- deriv *dv;
- design *des;
- { int lf_status, p, z, cri, noit, redo;
- double ho, t[6];
- if (lf_debug >= 2) mut_printf("ainitband:\n");
- p = des->p;
- cri = acri(sp);
- noit = (cri!=AOK);
- z = (int)(lfd->n*nn(sp));
- if ((noit) && (z<p+2)) z = p+2;
- redo = 0; ho = -1;
- do
- {
- nbhd(lfd,des,z,redo,sp);
- if (z<des->n) z = des->n;
- if (des->h>ho) lf_status = locfit(lfd,des,sp,noit,0,0);
- z++;
- redo = 1;
- } while ((z<=lfd->n) && ((des->h==0)||(lf_status!=LF_OK)));
- hmin = des->h;
- switch(cri)
- { case ACP:
- local_df(lfd,sp,des,t);
- mcp = adcri(des->llk,t[0],t[2],pen(sp));
- return(lf_status);
- case AKAT:
- local_df(lfd,sp,des,t);
- clo = des->cf[0]-pen(sp)*t[5];
- cup = des->cf[0]+pen(sp)*t[5];
- return(lf_status);
- case AMDI:
- mcp = mmse(lfd,sp,dv,des);
- return(lf_status);
- case AOK: return(lf_status);
- }
- LERR(("aband1: unknown criterion"));
- return(LF_ERR);
- }
- /*
- aband2 increases the initial bandwidth until lack of fit results,
- or the fit is close to a global fit. Increase h by 1+0.3/d at
- each iteration.
- */
- double aband2(lfd,sp,dv,des,h0)
- lfdata *lfd;
- smpar *sp;
- deriv *dv;
- design *des;
- double h0;
- { double t[6], h1, nu1, cp, ncp, tlo, tup;
- int d, inc, n, p, done;
- if (lf_debug >= 2) mut_printf("aband2:\n");
- d = lfd->d; n = lfd->n; p = npar(sp);
- h1 = des->h = h0;
- done = 0; nu1 = 0.0;
- inc = 0; ncp = 0.0;
- while ((!done) & (nu1<(n-p)*0.95))
- { fixh(sp) = (1+0.3/d)*des->h;
- nbhd(lfd,des,0,1,sp);
- if (locfit(lfd,des,sp,1,0,0) > 0) WARN(("aband2: failed fit"));
- local_df(lfd,sp,des,t);
- nu1 = t[0]-t[2]; /* tr(A) */
- switch(acri(sp))
- { case AKAT:
- tlo = des->cf[0]-pen(sp)*t[5];
- tup = des->cf[0]+pen(sp)*t[5];
- /* mut_printf("h %8.5f tlo %8.5f tup %8.5f\n",des->h,tlo,tup); */
- done = ((tlo>cup) | (tup<clo));
- if (!done)
- { clo = MAX(clo,tlo);
- cup = MIN(cup,tup);
- h1 = des->h;
- }
- break;
- case ACP:
- cp = adcri(des->llk,t[0],t[2],pen(sp));
- /* 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); */
- if (cp<mcp) { mcp = cp; h1 = des->h; }
- if (cp>=ncp) inc++; else inc = 0;
- ncp = cp;
- done = (inc>=10) | ((inc>=3) & ((t[0]-t[2])>=10) & (cp>1.5*mcp));
- break;
- case AMDI:
- cp = mmse(lfd,sp,dv,des);
- if (cp<mcp) { mcp = cp; h1 = des->h; }
- if (cp>ncp) inc++; else inc = 0;
- ncp = cp;
- done = (inc>=3);
- break;
- }
- }
- return(h1);
- }
- /*
- aband3 does a finer search around best h so far. Try
- h*(1-0.2/d), h/(1-0.1/d), h*(1+0.1/d), h*(1+0.2/d)
- */
- double aband3(lfd,sp,dv,des,h0)
- lfdata *lfd;
- smpar *sp;
- deriv *dv;
- design *des;
- double h0;
- { double t[6], h1, cp, tlo, tup;
- int i, i0, d, n;
- if (lf_debug >= 2) mut_printf("aband3:\n");
- d = lfd->d; n = lfd->n;
- h1 = h0;
- i0 = (acri(sp)==AKAT) ? 1 : -2;
- if (h0==hmin) i0 = 1;
- for (i=i0; i<=2; i++)
- { if (i==0) i++;
- fixh(sp) = h0*(1+0.1*i/d);
- nbhd(lfd,des,0,1,sp);
- if (locfit(lfd,des,sp,1,0,0) > 0) WARN(("aband3: failed fit"));
- local_df(lfd,sp,des,t);
- switch (acri(sp))
- { case AKAT:
- tlo = des->cf[0]-pen(sp)*t[5];
- tup = des->cf[0]+pen(sp)*t[5];
- if ((tlo>cup) | (tup<clo)) /* done */
- i = 2;
- else
- { h1 = des->h;
- clo = MAX(clo,tlo);
- cup = MIN(cup,tup);
- }
- break;
- case ACP:
- cp = adcri(des->llk,t[0],t[2],pen(sp));
- if (cp<mcp) { mcp = cp; h1 = des->h; }
- else
- { if (i>0) i = 2; }
- break;
- case AMDI:
- cp = mmse(lfd,sp,dv,des);
- if (cp<mcp) { mcp = cp; h1 = des->h; }
- else
- { if (i>0) i = 2; }
- }
- }
- return(h1);
- }
- int alocfit(lfd,sp,dv,des,cv)
- lfdata *lfd;
- smpar *sp;
- deriv *dv;
- design *des;
- int cv;
- { int lf_status;
- double h0;
- lf_status = ainitband(lfd,sp,dv,des);
- if (lf_error) return(lf_status);
- if (acri(sp) == AOK) return(lf_status);
- h0 = fixh(sp);
- fixh(sp) = aband2(lfd,sp,dv,des,des->h);
- fixh(sp) = aband3(lfd,sp,dv,des,fixh(sp));
- nbhd(lfd,des,0,1,sp);
- lf_status = locfit(lfd,des,sp,0,0,cv);
- fixh(sp) = h0;
- return(lf_status);
- }
- /*
- * Copyright 1996-2006 Catherine Loader.
- */
- /*
- *
- * Evaluate the locfit fitting functions.
- * calcp(sp,d)
- * calculates the number of fitting functions.
- * makecfn(sp,des,dv,d)
- * makes the coef.number vector.
- * fitfun(lfd, sp, x,t,f,dv)
- * lfd is the local fit structure.
- * sp smoothing parameter structure.
- * x is the data point.
- * t is the fitting point.
- * f is a vector to return the results.
- * dv derivative structure.
- * designmatrix(lfd, sp, des)
- * is a wrapper for fitfun to build the design matrix.
- *
- */
- #include "locf.h"
- int calcp(sp,d)
- smpar *sp;
- int d;
- { int i, k;
- if (ubas(sp)) return(npar(sp));
- switch (kt(sp))
- { case KSPH:
- case KCE:
- k = 1;
- for (i=1; i<=deg(sp); i++) k = k*(d+i)/i;
- return(k);
- case KPROD: return(d*deg(sp)+1);
- case KLM: return(d);
- case KZEON: return(1);
- }
- LERR(("calcp: invalid kt %d",kt(sp)));
- return(0);
- }
- int coefnumber(dv,kt,d,deg)
- int kt, d, deg;
- deriv *dv;
- { int d0, d1, t;
- if (d==1)
- { if (dv->nd<=deg) return(dv->nd);
- return(-1);
- }
- if (dv->nd==0) return(0);
- if (deg==0) return(-1);
- if (dv->nd==1) return(1+dv->deriv[0]);
- if (deg==1) return(-1);
- if (kt==KPROD) return(-1);
- if (dv->nd==2)
- { d0 = dv->deriv[0]; d1 = dv->deriv[1];
- if (d0<d1) { t = d0; d0 = d1; d1 = t; }
- return((d+1)*(d0+1)-d0*(d0+3)/2+d1);
- }
- if (deg==2) return(-1);
- LERR(("coefnumber not programmed for nd>=3"));
- return(-1);
- }
- void makecfn(sp,des,dv,d)
- smpar *sp;
- design *des;
- deriv *dv;
- int d;
- { int i, nd;
-
- nd = dv->nd;
- des->cfn[0] = coefnumber(dv,kt(sp),d,deg(sp));
- des->ncoef = 1;
- if (nd >= deg(sp)) return;
- if (kt(sp)==KZEON) return;
- if (d>1)
- { if (nd>=2) return;
- if ((nd>=1) && (kt(sp)==KPROD)) return;
- }
- dv->nd = nd+1;
- for (i=0; i<d; i++)
- { dv->deriv[nd] = i;
- des->cfn[i+1] = coefnumber(dv,kt(sp),d,deg(sp));
- }
- dv->nd = nd;
- des->ncoef = 1+d;
- }
- void fitfunangl(dx,ff,sca,cd,deg)
- double dx, *ff, sca;
- int deg, cd;
- {
- if (deg>=3) WARN(("Can't handle angular model with deg>=3"));
- switch(cd)
- { case 0:
- ff[0] = 1;
- ff[1] = sin(dx/sca)*sca;
- ff[2] = (1-cos(dx/sca))*sca*sca;
- return;
- case 1:
- ff[0] = 0;
- ff[1] = cos(dx/sca);
- ff[2] = sin(dx/sca)*sca;
- return;
- case 2:
- ff[0] = 0;
- ff[1] = -sin(dx/sca)/sca;
- ff[2] = cos(dx/sca);
- return;
- default: WARN(("Can't handle angular model with >2 derivs"));
- }
- }
- void fitfun(lfd,sp,x,t,f,dv)
- lfdata *lfd;
- smpar *sp;
- double *x, *t, *f;
- deriv *dv;
- { int d, deg, nd, m, i, j, k, ct_deriv[MXDIM];
- double ff[MXDIM][1+MXDEG], dx[MXDIM], *xx[MXDIM];
- if (ubas(sp))
- { for (i=0; i<lfd->d; i++) xx[i] = &x[i];
- i = 0;
- sp->vbasis(xx,t,1,lfd->d,1,npar(sp),f);
- return;
- }
- d = lfd->d;
- deg = deg(sp);
- m = 0;
- nd = (dv==NULL) ? 0 : dv->nd;
- if (kt(sp)==KZEON)
- { f[0] = 1.0;
- return;
- }
- if (kt(sp)==KLM)
- { for (i=0; i<d; i++) f[m++] = x[i];
- return;
- }
- f[m++] = (nd==0);
- if (deg==0) return;
- for (i=0; i<d; i++)
- { ct_deriv[i] = 0;
- dx[i] = (t==NULL) ? x[i] : x[i]-t[i];
- }
- for (i=0; i<nd; i++) ct_deriv[dv->deriv[i]]++;
- for (i=0; i<d; i++)
- { switch(lfd->sty[i])
- {
- case STANGL:
- fitfunangl(dx[i],ff[i],lfd->sca[i],ct_deriv[i],deg(sp));
- break;
- default:
- for (j=0; j<ct_deriv[i]; j++) ff[i][j] = 0.0;
- ff[i][ct_deriv[i]] = 1.0;
- for (j=ct_deriv[i]+1; j<=deg; j++)
- ff[i][j] = ff[i][j-1]*dx[i]/(j-ct_deriv[i]);
- }
- }
- /*
- * Product kernels. Note that if ct_deriv[i] != nd, that implies
- * there is differentiation wrt another variable, and all components
- * involving x[i] are 0.
- */
- if ((d==1) || (kt(sp)==KPROD))
- { for (j=1; j<=deg; j++)
- for (i=0; i<d; i++)
- f[m++] = (ct_deriv[i]==nd) ? ff[i][j] : 0.0;
- return;
- }
- /*
- * Spherical kernels with the full polynomial basis.
- * Presently implemented up to deg=3.
- */
- for (i=0; i<d; i++)
- f[m++] = (ct_deriv[i]==nd) ? ff[i][1] : 0.0;
- if (deg==1) return;
- for (i=0; i<d; i++)
- {
- /* xi^2/2 terms. */
- f[m++] = (ct_deriv[i]==nd) ? ff[i][2] : 0.0;
- /* xi xj terms */
- for (j=i+1; j<d; j++)
- f[m++] = (ct_deriv[i]+ct_deriv[j]==nd) ? ff[i][1]*ff[j][1] : 0.0;
- }
- if (deg==2) return;
- for (i=0; i<d; i++)
- {
- /* xi^3/6 terms */
- f[m++] = (ct_deriv[i]==nd) ? ff[i][3] : 0.0;
- /* xi^2/2 xk terms */
- for (k=i+1; k<d; k++)
- f[m++] = (ct_deriv[i]+ct_deriv[k]==nd) ? ff[i][2]*ff[k][1] : 0.0;
- /* xi xj xk terms */
- for (j=i+1; j<d; j++)
- { f[m++] = (ct_deriv[i]+ct_deriv[j]==nd) ? ff[i][1]*ff[j][2] : 0.0;
- for (k=j+1; k<d; k++)
- f[m++] = (ct_deriv[i]+ct_deriv[j]+ct_deriv[k]==nd) ?
- ff[i][1]*ff[j][1]*ff[k][1] : 0.0;
- }
- }
- if (deg==3) return;
- LERR(("fitfun: can't handle deg=%d for spherical kernels",deg));
- }
- /*
- * Build the design matrix. Assumes des->ind contains the indices of
- * the required data points; des->n the number of points; des->xev
- * the fitting point.
- */
- void designmatrix(lfd,sp,des)
- lfdata *lfd;
- smpar *sp;
- design *des;
- { int i, ii, j, p;
- double *X, u[MXDIM];
- X = d_x(des);
- p = des->p;
- if (ubas(sp))
- {
- sp->vbasis(lfd->x,des->xev,lfd->n,lfd->d,des->n,p,X);
- return;
- }
- for (i=0; i<des->n; i++)
- { ii = des->ind[i];
- for (j=0; j<lfd->d; j++) u[j] = datum(lfd,j,ii);
- fitfun(lfd,sp,u,des->xev,&X[ii*p],NULL);
- }
- }
- /*
- * Copyright 1996-2006 Catherine Loader.
- */
- /*
- *
- *
- * Functions for determining bandwidth; smoothing neighborhood
- * and smoothing weights.
- */
- #include "locf.h"
- double rho(x,sc,d,kt,sty) /* ||x|| for appropriate distance metric */
- double *x, *sc;
- int d, kt, *sty;
- { double rhoi[MXDIM], s;
- int i;
- for (i=0; i<d; i++)
- { if (sty!=NULL)
- { switch(sty[i])
- { case STANGL: rhoi[i] = 2*sin(x[i]/(2*sc[i])); break;
- case STCPAR: rhoi[i] = 0; break;
- default: rhoi[i] = x[i]/sc[i];
- } }
- else rhoi[i] = x[i]/sc[i];
- }
- if (d==1) return(fabs(rhoi[0]));
- s = 0;
- if (kt==KPROD)
- { for (i=0; i<d; i++)
- { rhoi[i] = fabs(rhoi[i]);
- if (rhoi[i]>s) s = rhoi[i];
- }
- return(s);
- }
- if (kt==KSPH)
- { for (i=0; i<d; i++)
- s += rhoi[i]*rhoi[i];
- return(sqrt(s));
- }
- LERR(("rho: invalid kt"));
- return(0.0);
- }
- double kordstat(x,k,n,ind)
- double *x;
- int k, n, *ind;
- { int i, i0, i1, l, r;
- double piv;
- if (k<1) return(0.0);
- i0 = 0; i1 = n-1;
- while (1)
- { piv = x[ind[(i0+i1)/2]];
- l = i0; r = i1;
- while (l<=r)
- { while ((l<=i1) && (x[ind[l]]<=piv)) l++;
- while ((r>=i0) && (x[ind[r]]>piv)) r--;
- if (l<=r) ISWAP(ind[l],ind[r]);
- } /* now, x[ind[i0..r]] <= piv < x[ind[l..i1]] */
- if (r<k-1) i0 = l; /* go right */
- else /* put pivots in middle */
- { for (i=i0; i<=r; )
- if (x[ind[i]]==piv) { ISWAP(ind[i],ind[r]); r--; }
- else i++;
- if (r<k-1) return(piv);
- i1 = r;
- }
- }
- }
- /* check if i'th data point is in limits */
- int inlim(lfd,i)
- lfdata *lfd;
- int i;
- { int d, j, k;
- double *xlim;
- xlim = lfd->xl;
- d = lfd->d;
- k = 1;
- for (j=0; j<d; j++)
- { if (xlim[j]<xlim[j+d])
- k &= ((datum(lfd,j,i)>=xlim[j]) & (datum(lfd,j,i)<=xlim[j+d]));
- }
- return(k);
- }
- double compbandwid(di,ind,x,n,d,nn,fxh)
- double *di, *x, fxh;
- int n, d, nn, *ind;
- { int i;
- double nnh;
- if (nn==0) return(fxh);
- if (nn<n)
- nnh = kordstat(di,nn,n,ind);
- else
- { nnh = 0;
- for (i=0; i<n; i++) nnh = MAX(nnh,di[i]);
- nnh = nnh*exp(log(1.0*nn/n)/d);
- }
- return(MAX(fxh,nnh));
- }
- /*
- fast version of nbhd for ordered 1-d data
- */
- void nbhd1(lfd,sp,des,k)
- lfdata *lfd;
- smpar *sp;
- design *des;
- int k;
- { double x, h, *xd, sc;
- int i, l, r, m, n, z;
- n = lfd->n;
- x = des->xev[0];
- xd = dvari(lfd,0);
- sc = lfd->sca[0];
- /* find closest data point to x */
- if (x<=xd[0]) z = 0;
- else
- if (x>=xd[n-1]) z = n-1;
- else
- { l = 0; r = n-1;
- while (r-l>1)
- { z = (r+l)/2;
- if (xd[z]>x) r = z;
- else l = z;
- }
- /* now, xd[0..l] <= x < x[r..n-1] */
- if ((x-xd[l])>(xd[r]-x)) z = r; else z = l;
- }
- /* closest point to x is xd[z] */
- if (nn(sp)<0) /* user bandwidth */
- h = sp->vb(des->xev);
- else
- { if (k>0) /* set h to nearest neighbor bandwidth */
- { l = r = z;
- if (l==0) r = k-1;
- if (r==n-1) l = n-k;
- while (r-l<k-1)
- { if ((x-xd[l-1])<(xd[r+1]-x)) l--; else r++;
- if (l==0) r = k-1;
- if (r==n-1) l = n-k;
- }
- h = x-xd[l];
- if (h<xd[r]-x) h = xd[r]-x;
- }
- else h = 0;
- h /= sc;
- if (h<fixh(sp)) h = fixh(sp);
- }
- m = 0;
- if (xd[z]>x) z--; /* so xd[z]<=x */
- /* look left */
- for (i=z; i>=0; i--) if (inlim(lfd,i))
- { dist(des,i) = (x-xd[i])/sc;
- wght(des,i) = weight(lfd, sp, &xd[i], &x, h, 1, dist(des,i));
- if (wght(des,i)>0)
- { des->ind[m] = i;
- m++;
- } else i = 0;
- }
- /* look right */
- for (i=z+1; i<n; i++) if (inlim(lfd,i))
- { dist(des,i) = (xd[i]-x)/sc;
- wght(des,i) = weight(lfd, sp, &xd[i], &x, h, 1, dist(des,i));
- if (wght(des,i)>0)
- { des->ind[m] = i;
- m++;
- } else i = n;
- }
- des->n = m;
- des->h = h;
- }
- void nbhd_zeon(lfd,des)
- lfdata *lfd;
- design *des;
- { int i, j, m, eq;
- m = 0;
- for (i=0; i<lfd->n; i++)
- { eq = 1;
- for (j=0; j<lfd->d; j++) eq = eq && (des->xev[j] == datum(lfd,j,i));
- if (eq)
- { wght(des,i) = 1;
- des->ind[m] = i;
- m++;
- }
- }
- des->n = m;
- des->h = 1.0;
- }
- void nbhd(lfd,des,nn,redo,sp)
- lfdata *lfd;
- design *des;
- int redo, nn;
- smpar *sp;
- { int d, i, j, m, n;
- double h, u[MXDIM];
- if (lf_debug>1) mut_printf("nbhd: nn %d fixh %8.5f\n",nn,fixh(sp));
-
- d = lfd->d; n = lfd->n;
- if (ker(sp)==WPARM)
- { for (i=0; i<n; i++)
- { wght(des,i) = 1.0;
- des->ind[i] = i;
- }
- des->n = n;
- return;
- }
- if (kt(sp)==KZEON)
- { nbhd_zeon(lfd,des);
- return;
- }
- if (kt(sp)==KCE)
- { des->h = 0.0;
- return;
- }
- /* ordered 1-dim; use fast searches */
- if ((nn<=n) & (lfd->ord) & (ker(sp)!=WMINM) & (lfd->sty[0]!=STANGL))
- { nbhd1(lfd,sp,des,nn);
- return;
- }
- if (!redo)
- { for (i=0; i<n; i++)
- { for (j=0; j<d; j++) u[j] = datum(lfd,j,i)-des->xev[j];
- dist(des,i) = rho(u,lfd->sca,d,kt(sp),lfd->sty);
- des->ind[i] = i;
- }
- }
- else
- for (i=0; i<n; i++) des->ind[i] = i;
- if (ker(sp)==WMINM)
- { des->h = minmax(lfd,des,sp);
- return;
- }
- if (nn<0)
- h = sp->vb(des->xev);
- else
- h = compbandwid(des->di,des->ind,des->xev,n,lfd->d,nn,fixh(sp));
- m = 0;
- for (i=0; i<n; i++) if (inlim(lfd,i))
- { for (j=0; j<d; j++) u[j] = datum(lfd,j,i);
- wght(des,i) = weight(lfd, sp, u, des->xev, h, 1, dist(des,i));
- if (wght(des,i)>0)
- { des->ind[m] = i;
- m++;
- }
- }
- des->n = m;
- des->h = h;
- }
- /*
- * Copyright 1996-2006 Catherine Loader.
- */
- /*
- *
- * This file includes functions to solve for the scale estimate in
- * local robust regression and likelihood. The main entry point is
- * lf_robust(lfd,sp,des,mxit),
- * called from the locfit() function.
- *
- * The update_rs(x) accepts a residual scale x as the argument (actually,
- * it works on the log-scale). The function computes the local fit
- * assuming this residual scale, and re-estimates the scale from this
- * new fit. The final solution satisfies the fixed point equation
- * update_rs(x)=x. The function lf_robust() automatically calls
- * update_rs() through the fixed point iterations.
- *
- * The estimation of the scale from the fit is based on the sqrt of
- * the median deviance of observations with non-zero weights (in the
- * gaussian case, this is the median absolute residual).
- *
- * TODO:
- * Should use smoothing weights in the median.
- */
- #include "locf.h"
- extern int lf_status;
- double robscale;
- static lfdata *rob_lfd;
- static smpar *rob_sp;
- static design *rob_des;
- static int rob_mxit;
- double median(x,n)
- double *x;
- int n;
- { int i, j, lt, eq, gt;
- double lo, hi, s;
- lo = hi = x[0];
- for (i=0; i<n; i++)
- { lo = MIN(lo,x[i]);
- hi = MAX(hi,x[i]);
- }
- if (lo==hi) return(lo);
- lo -= (hi-lo);
- hi += (hi-lo);
- for (i=0; i<n; i++)
- { if ((x[i]>lo) & (x[i]<hi))
- { s = x[i]; lt = eq = gt = 0;
- for (j=0; j<n; j++)
- { lt += (x[j]<s);
- eq += (x[j]==s);
- gt += (x[j]>s);
- }
- if ((2*(lt+eq)>n) && (2*(gt+eq)>n)) return(s);
- if (2*(lt+eq)<=n) lo = s;
- if (2*(gt+eq)<=n) hi = s;
- }
- }
- return((hi+lo)/2);
- }
- double nrobustscale(lfd,sp,des,rs)
- lfdata *lfd;
- smpar *sp;
- design *des;
- double rs;
- { int i, ii, p;
- double link[LLEN], sc, sd, sw, e;
- p = des->p; sc = sd = sw = 0.0;
- for (i=0; i<des->n; i++)
- { ii = des->ind[i];
- fitv(des,ii) = base(lfd,ii)+innerprod(des->cf,d_xi(des,ii),p);
- e = resp(lfd,ii)-fitv(des,ii);
- stdlinks(link,lfd,sp,ii,fitv(des,ii),rs);
- sc += wght(des,ii)*e*link[ZDLL];
- sd += wght(des,ii)*e*e*link[ZDDLL];
- sw += wght(des,ii);
- }
- /* newton-raphson iteration for log(s)
- -psi(ei/s) - log(s); s = e^{-th}
- */
- rs *= exp((sc-sw)/(sd+sc));
- return(rs);
- }
- double robustscale(lfd,sp,des)
- lfdata *lfd;
- smpar *sp;
- design *des;
- { int i, ii, p, fam, lin, or;
- double rs, link[LLEN];
- p = des->p;
- fam = fam(sp);
- lin = link(sp);
- or = fami(sp)->robust;
- fami(sp)->robust = 0;
- for (i=0; i<des->n; i++)
- { ii = des->ind[i];
- fitv(des,ii) = base(lfd,ii) + innerprod(des->cf,d_xi(des,ii),p);
- links(fitv(des,ii),resp(lfd,ii),fami(sp),lin,link,cens(lfd,ii),prwt(lfd,ii),1.0);
- des->res[i] = -2*link[ZLIK];
- }
- fami(sp)->robust = or;
- rs = sqrt(median(des->res,des->n));
- if (rs==0.0) rs = 1.0;
- return(rs);
- }
- double update_rs(x)
- double x;
- { double nx;
- if (lf_status != LF_OK) return(x);
- robscale = exp(x);
- lfiter(rob_lfd,rob_sp,rob_des,rob_mxit);
- if (lf_status != LF_OK) return(x);
- nx = log(robustscale(rob_lfd,rob_sp,rob_des));
- if (nx<x-0.2) nx = x-0.2;
- return(nx);
- }
- void lf_robust(lfd,sp,des,mxit)
- lfdata *lfd;
- design *des;
- smpar *sp;
- int mxit;
- { double x;
- rob_lfd = lfd;
- rob_des = des;
- rob_sp = sp;
- rob_mxit = mxit;
- lf_status = LF_OK;
- x = log(robustscale(lfd,sp,des));
- solve_fp(update_rs, x, 1.0e-6, mxit);
- }
- /*
- * Copyright 1996-2006 Catherine Loader.
- */
- /*
- * Post-fitting functions to compute the local variance and
- * influence functions. Also the local degrees of freedom
- * calculations for adaptive smoothing.
- */
- #include "locf.h"
- extern double robscale;
- /*
- vmat() computes (after the local fit..) the matrix
- M2 = X^T W^2 V X.
- M12 = (X^T W V X)^{-1} M2
- Also, for convenience, tr[0] = sum(wi) tr[1] = sum(wi^2).
- */
- void vmat(lfd, sp, des, M12, M2)
- lfdata *lfd;
- smpar *sp;
- design *des;
- double *M12, *M2;
- { int i, ii, p, nk, ok;
- double link[LLEN], h, ww, tr0, tr1;
- p = des->p;
- setzero(M2,p*p);
- nk = -1;
- /* for density estimation, use integral rather than
- sum form, if W^2 is programmed...
- */
- if ((fam(sp)<=THAZ) && (link(sp)==LLOG))
- { switch(ker(sp))
- { case WGAUS: nk = WGAUS; h = des->h/SQRT2; break;
- case WRECT: nk = WRECT; h = des->h; break;
- case WEPAN: nk = WBISQ; h = des->h; break;
- case WBISQ: nk = WQUQU; h = des->h; break;
- case WTCUB: nk = W6CUB; h = des->h; break;
- case WEXPL: nk = WEXPL; h = des->h/2; break;
- }
- }
- tr0 = tr1 = 0.0;
- if (nk != -1)
- { ok = ker(sp); ker(sp) = nk;
- /* compute M2 using integration. Use M12 as work matrix. */
- (des->itype)(des->xev, M2, M12, des->cf, h);
- ker(sp) = ok;
- if (fam(sp)==TDEN) multmatscal(M2,des->smwt,p*p);
- tr0 = des->ss[0];
- tr1 = M2[0]; /* n int W e^<a,A> */
- }
- else
- { for (i=0; i<des->n; i++)
- { ii = des->ind[i];
- stdlinks(link,lfd,sp,ii,fitv(des,ii),robscale);
- ww = SQR(wght(des,ii))*link[ZDDLL];
- tr0 += wght(des,ii);
- tr1 += SQR(wght(des,ii));
- addouter(M2,d_xi(des,ii),d_xi(des,ii),p,ww);
- }
- }
- des->tr0 = tr0;
- des->tr1 = tr1;
- memcpy(M12,M2,p*p*sizeof(double));
- for (i=0; i<p; i++)
- jacob_solve(&des->xtwx,&M12[i*p]);
- }
- void lf_vcov(lfd,sp,des)
- lfdata *lfd;
- smpar *sp;
- design *des;
- { int i, j, k, p;
- double *M12, *M2;
- M12 = des->V; M2 = des->P; p = des->p;
- vmat(lfd,sp,des,M12,M2); /* M2 = X^T W^2 V X tr0=sum(W) tr1=sum(W*W) */
- des->tr2 = m_trace(M12,p); /* tr (XTWVX)^{-1}(XTW^2VX) */
- /*
- * Covariance matrix is M1^{-1} * M2 * M1^{-1}
- * We compute this using the cholesky decomposition of
- * M2; premultiplying by M1^{-1} and squaring. This
- * is more stable than direct computation in near-singular cases.
- */
- chol_dec(M2,p,p);
- for (i=0; i<p; i++)
- for (j=0; j<i; j++)
- { M2[j*p+i] = M2[i*p+j];
- M2[i*p+j] = 0.0;
- }
- for (i=0; i<p; i++) jacob_solve(&des->xtwx,&M2[i*p]);
- for (i=0; i<p; i++)
- { for (j=0; j<p; j++)
- { M12[i*p+j] = 0;
- for (k=0; k<p; k++)
- M12[i*p+j] += M2[k*p+i]*M2[k*p+j]; /* ith column of covariance */
- }
- }
- if ((fam(sp)==TDEN) && (link(sp)==LIDENT))
- multmatscal(M12,1/SQR(des->smwt),p*p);
- /* this computes the influence function as des->f1[0]. */
- unitvec(des->f1,0,des->p);
- jacob_solve(&des->xtwx,des->f1);
- }
- /* local_df computes:
- * tr[0] = trace(W)
- * tr[1] = trace(W*W)
- * tr[2] = trace( M1^{-1} M2 )
- * tr[3] = trace( M1^{-1} M3 )
- * tr[4] = trace( (M1^{-1} M2)^2 )
- * tr[5] = var(theta-hat).
- */
- void local_df(lfd,sp,des,tr)
- lfdata *lfd;
- smpar *sp;
- design *des;
- double *tr;
- { int i, ii, j, p;
- double *m2, *V, ww, link[LLEN];
- tr[0] = tr[1] = tr[2] = tr[3] = tr[4] = tr[5] = 0.0;
- m2 = des->V; V = des->P; p = des->p;
- vmat(lfd,sp,des,m2,V); /* M = X^T W^2 V X tr0=sum(W) tr1=sum(W*W) */
- tr[0] = des->tr0;
- tr[1] = des->tr1;
- tr[2] = m_trace(m2,p); /* tr (XTWVX)^{-1}(XTW^2VX) */
- unitvec(des->f1,0,p);
- jacob_solve(&des->xtwx,des->f1);
- for (i=0; i<p; i++)
- for (j=0; j<p; j++)
- { tr[4] += m2[i*p+j]*m2[j*p+i]; /* tr(M^2) */
- tr[5] += des->f1[i]*V[i*p+j]*des->f1[j]; /* var(thetahat) */
- }
- tr[5] = sqrt(tr[5]);
- setzero(m2,p*p);
- for (i=0; i<des->n; i++)
- { ii = des->ind[i];
- stdlinks(link,lfd,sp,ii,fitv(des,ii),robscale);
- ww = wght(des,ii)*wght(des,ii)*wght(des,ii)*link[ZDDLL];
- addouter(m2,d_xi(des,ii),d_xi(des,ii),p,ww);
- }
- for (i=0; i<p; i++)
- { jacob_solve(&des->xtwx,&m2[i*p]);
- tr[3] += m2[i*(p+1)];
- }
- return;
- }
- /*
- * Copyright 1996-2006 Catherine Loader.
- */
- /*
- * Routines for computing weight diagrams.
- * wdiag(lf,des,lx,deg,ty,exp)
- * Must locfit() first, unless ker==WPARM and has par. comp.
- *
- */
- #include "locf.h"
- static double *wd;
- extern double robscale;
- void nnresproj(lfd,sp,des,u,m,p)
- lfdata *lfd;
- smpar *sp;
- design *des;
- double *u;
- int m, p;
- { int i, ii, j;
- double link[LLEN];
- setzero(des->f1,p);
- for (j=0; j<m; j++)
- { ii = des->ind[j];
- stdlinks(link,lfd,sp,ii,fitv(des,ii),robscale);
- for (i=0; i<p; i++) des->f1[i] += link[ZDDLL]*d_xij(des,j,ii)*u[j];
- }
- jacob_solve(&des->xtwx,des->f1);
- for (i=0; i<m; i++)
- { ii = des->ind[i];
- u[i] -= innerprod(des->f1,d_xi(des,ii),p)*wght(des,ii);
- }
- }
- void wdexpand(l,n,ind,m)
- double *l;
- int *ind, n, m;
- { int i, j, t;
- double z;
- for (j=m; j<n; j++) { l[j] = 0.0; ind[j] = -1; }
- j = m-1;
- while (j>=0)
- { if (ind[j]==j) j--;
- else
- { i = ind[j];
- z = l[j]; l[j] = l[i]; l[i] = z;
- t = ind[j]; ind[j] = ind[i]; ind[i] = t;
- if (ind[j]==-1) j--;
- }
- }
- /* for (i=n-1; i>=0; i--)
- { l[i] = ((j>=0) && (ind[j]==i)) ? l[j--] : 0.0; } */
- }
- int wdiagp(lfd,sp,des,lx,pc,dv,deg,ty,exp)
- lfdata *lfd;
- smpar *sp;
- design *des;
- paramcomp *pc;
- deriv *dv;
- double *lx;
- int deg, ty, exp;
- { int i, j, p, nd;
- double *l1;
- p = des->p;
- fitfun(lfd,sp,des->xev,pc->xbar,des->f1,dv);
- if (exp)
- { jacob_solve(&pc->xtwx,des->f1);
- for (i=0; i<lfd->n; i++)
- lx[i] = innerprod(des->f1,d_xi(des,des->ind[i]),p);
- return(lfd->n);
- }
- jacob_hsolve(&pc->xtwx,des->f1);
- for (i=0; i<p; i++) lx[i] = des->f1[i];
- nd = dv->nd;
- dv->nd = nd+1;
- if (deg>=1)
- for (i=0; i<lfd->d; i++)
- { dv->deriv[nd] = i;
- l1 = &lx[(i+1)*p];
- fitfun(lfd,sp,des->xev,pc->xbar,l1,dv);
- jacob_hsolve(&pc->xtwx,l1);
- }
- dv->nd = nd+2;
- if (deg>=2)
- for (i=0; i<lfd->d; i++)
- { dv->deriv[nd] = i;
- for (j=0; j<lfd->d; j++)
- { dv->deriv[nd+1] = j;
- l1 = &lx[(i*lfd->d+j+lfd->d+1)*p];
- fitfun(lfd,sp,des->xev,pc->xbar,l1,dv);
- jacob_hsolve(&pc->xtwx,l1);
- } }
- dv->nd = nd;
- return(p);
- }
- int wdiag(lfd,sp,des,lx,dv,deg,ty,exp)
- lfdata *lfd;
- smpar *sp;
- design *des;
- deriv *dv;
- double *lx;
- int deg, ty, exp;
- /* deg=0: l(x) only.
- deg=1: l(x), l'(x)
- deg=2: l(x), l'(x), l''(x)
- ty = 1: e1 (X^T WVX)^{-1} X^T W -- hat matrix
- ty = 2: e1 (X^T WVX)^{-1} X^T WV^{1/2} -- scb's
- */
- { double w, *X, *lxd, *lxdd, wdd, wdw, *ulx, link[LLEN], h;
- double dfx[MXDIM], hs[MXDIM];
- int i, ii, j, k, l, m, d, p, nd;
- h = des->h;
- nd = dv->nd;
- wd = des->wd;
- d = lfd->d; p = des->p; X = d_x(des);
- ulx = des->res;
- m = des->n;
- for (i=0; i<d; i++) hs[i] = h*lfd->sca[i];
- if (deg>0)
- { lxd = &lx[m];
- setzero(lxd,m*d);
- if (deg>1)
- { lxdd = &lxd[d*m];
- setzero(lxdd,m*d*d);
- } }
- if (nd>0) fitfun(lfd,sp,des->xev,des->xev,des->f1,dv); /* c(0) */
- else unitvec(des->f1,0,p);
- jacob_solve(&des->xtwx,des->f1); /* c(0) (X^TWX)^{-1} */
- for (i=0; i<m; i++)
- { ii = des->ind[i];
- lx[i] = innerprod(des->f1,&X[ii*p],p); /* c(0)(XTWX)^{-1}X^T */
- if (deg>0)
- { wd[i] = Wd(dist(des,ii)/h,ker(sp));
- for (j=0; j<d; j++)
- { dfx[j] = datum(lfd,j,ii)-des->xev[j];
- lxd[j*m+i] = lx[i]*wght(des,ii)*weightd(dfx[j],lfd->sca[j],
- d,ker(sp),kt(sp),h,lfd->sty[j],dist(des,ii));
- /* c(0) (XTWX)^{-1}XTW' */
- }
- if (deg>1)
- { wdd = Wdd(dist(des,ii)/h,ker(sp));
- for (j=0; j<d; j++)
- for (k=0; k<d; k++)
- { w = (dist(des,ii)==0) ? 0 : h/dist(des,ii);
- w = wdd * (des->xev[k]-datum(lfd,k,ii)) * (des->xev[j]-datum(lfd,j,ii))
- * w*w / (hs[k]*hs[k]*hs[j]*hs[j]);
- if (j==k) w += wd[i]/(hs[j]*hs[j]);
- lxdd[(j*d+k)*m+i] = lx[i]*w;
- /* c(0)(XTWX)^{-1}XTW'' */
- }
- }
- }
- lx[i] *= wght(des,ii);
- }
- dv->nd = nd+1;
- if (deg==2)
- { for (i=0; i<d; i++)
- { dv->deriv[nd] = i;
- fitfun(lfd,sp,des->xev,des->xev,des->f1,dv);
- for (k=0; k<m; k++)
- { ii = des->ind[i];
- stdlinks(link,lfd,sp,ii,fitv(des,ii),robscale);
- for (j=0; j<p; j++)
- des->f1[j] -= link[ZDDLL]*lxd[i*m+k]*X[ii*p+j];
- /* c'(x)-c(x)(XTWX)^{-1}XTW'X */
- }
- jacob_solve(&des->xtwx,des->f1); /* (...)(XTWX)^{-1} */
- for (j=0; j<m; j++)
- { ii = des->ind[j];
- ulx[j] = innerprod(des->f1,&X[ii*p],p); /* (...)XT */
- }
- for (j=0; j<d; j++)
- for (k=0; k<m; k++)
- { ii = des->ind[k];
- dfx[j] = datum(lfd,j,ii)-des->xev[j];
- wdw = wght(des,ii)*weightd(dfx[j],lfd->sca[j],d,ker(sp),
- kt(sp),h,lfd->sty[j],dist(des,ii));
- lxdd[(i*d+j)*m+k] += ulx[k]*wdw;
- lxdd[(j*d+i)*m+k] += ulx[k]*wdw;
- } /* + 2(c'-c(XTWX)^{-1}XTW'X)(XTWX)^{-1}XTW' */
- }
- for (j=0; j<d*d; j++) nnresproj(lfd,sp,des,&lxdd[j*m],m,p);
- /* * (I-X(XTWX)^{-1} XTW */
- }
- if (deg>0)
- { for (j=0; j<d; j++) nnresproj(lfd,sp,des,&lxd[j*m],m,p);
- /* c(0)(XTWX)^{-1}XTW'(I-X(XTWX)^{-1}XTW) */
- for (i=0; i<d; i++)
- { dv->deriv[nd]=i;
- fitfun(lfd,sp,des->xev,des->xev,des->f1,dv);
- jacob_solve(&des->xtwx,des->f1);
- for (k=0; k<m; k++)
- { ii = des->ind[k];
- for (l=0; l<p; l++)
- lxd[i*m+k] += des->f1[l]*X[ii*p+l]*wght(des,ii);
- } /* add c'(0)(XTWX)^{-1}XTW */
- }
- }
- dv->nd = nd+2;
- if (deg==2)
- { for (i=0; i<d; i++)
- { dv->deriv[nd]=i;
- for (j=0; j<d; j++)
- { dv->deriv[nd+1]=j;
- fitfun(lfd,sp,des->xev,des->xev,des->f1,dv);
- jacob_solve(&des->xtwx,des->f1);
- for (k=0; k<m; k++)
- { ii = des->ind[k];
- for (l=0; l<p; l++)
- lxdd[(i*d+j)*m+k] += des->f1[l]*X[ii*p+l]*wght(des,ii);
- } /* + c''(x)(XTWX)^{-1}XTW */
- }
- }
- }
- dv->nd = nd;
- k = 1+d*(deg>0)+d*d*(deg==2);
- if (exp) wdexpand(lx,lfd->n,des->ind,m);
-
- if (ty==1) return(m);
- for (i=0; i<m; i++)
- { ii = des->ind[i];
- stdlinks(link,lfd,sp,ii,fitv(des,ii),robscale);
- link[ZDDLL] = sqrt(fabs(link[ZDDLL]));
- for (j=0; j<k; j++) lx[j*m+i] *= link[ZDDLL];
- }
- return(m);
- }
- /*
- * Copyright 1996-2006 Catherine Loader.
- */
- /*
- * String matching functions. For a given argument string, find
- * the best match from an array of possibilities. Is there a library
- * function somewhere to do something like this?
- *
- * return values of -1 indicate failure/unknown string.
- */
- #include "locf.h"
- int ct_match(z1, z2)
- char *z1, *z2;
- { int ct = 0;
- while (z1[ct]==z2[ct])
- { if (z1[ct]=='\0') return(ct+1);
- ct++;
- }
- return(ct);
- }
- int pmatch(z, strings, vals, n, def)
- char *z, **strings;
- int *vals, n, def;
- { int i, ct, best, best_ct;
- best = -1;
- best_ct = 0;
- for (i=0; i<n; i++)
- { ct = ct_match(z,strings[i]);
- if (ct==strlen(z)+1) return(vals[i]);
- if (ct>best_ct) { best = i; best_ct = ct; }
- }
- if (best==-1) return(def);
- return(vals[best]);
- }
- /*
- * Copyright 1996-2006 Catherine Loader.
- */
- #include "locf.h"
- int lf_maxit = 20;
- int lf_debug = 0;
- int lf_error = 0;
- double s0, s1;
- static lfdata *lf_lfd;
- static design *lf_des;
- static smpar *lf_sp;
- int lf_status;
- int ident=0;
- double lf_tol;
- extern double robscale;
- void lfdata_init(lfd)
- lfdata *lfd;
- { int i;
- for (i=0; i<MXDIM; i++)
- { lfd->sty[i] = 0;
- lfd->sca[i] = 1.0;
- lfd->xl[i] = lfd->xl[i+MXDIM] = 0.0;
- }
- lfd->y = lfd->w = lfd->c = lfd->b = NULL;
- lfd->d = lfd->n = 0;
- }
- void smpar_init(sp,lfd)
- smpar *sp;
- lfdata *lfd;
- { nn(sp) = 0.7;
- fixh(sp)= 0.0;
- pen(sp) = 0.0;
- acri(sp)= ANONE;
- deg(sp) = deg0(sp) = 2;
- ubas(sp) = 0;
- kt(sp) = KSPH;
- ker(sp) = WTCUB;
- fam(sp) = 64+TGAUS;
- link(sp)= LDEFAU;
- npar(sp) = calcp(sp,lfd->d);
- }
- void deriv_init(dv)
- deriv *dv;
- { dv->nd = 0;
- }
- int des_reqd(n,p)
- int n, p;
- {
- return(n*(p+5)+2*p*p+4*p + jac_reqd(p));
- }
- int des_reqi(n,p)
- int n, p;
- { return(n+p);
- }
-
- void des_init(des,n,p)
- design *des;
- int n, p;
- { double *z;
- int k;
- if (n<=0) WARN(("des_init: n <= 0"));
- if (p<=0) WARN(("des_init: p <= 0"));
- if (des->des_init_id != DES_INIT_ID)
- { des->lwk = des->lind = 0;
- des->des_init_id = DES_INIT_ID;
- }
- k = des_reqd(n,p);
- if (k>des->lwk)
- { des->wk = (double *)calloc(k,sizeof(double));
- if ( des->wk == NULL ) {
- printf("Problem allocating memory for des->wk\n");fflush(stdout);
- }
- des->lwk = k;
- }
- z = des->wk;
- des->X = z; z += n*p;
- des->w = z; z += n;
- des->res=z; z += n;
- des->di =z; z += n;
- des->th =z; z += n;
- des->wd =z; z += n;
- des->V =z; z += p*p;
- des->P =z; z += p*p;
- des->f1 =z; z += p;
- des->ss =z; z += p;
- des->oc =z; z += p;
- des->cf =z; z += p;
-
- z = jac_alloc(&des->xtwx,p,z);
-
- k = des_reqi(n,p);
- if (k>des->lind)
- {
- des->ind = (int *)calloc(k,sizeof(int));
- if ( des->ind == NULL ) {
- printf("Problem allocating memory for des->ind\n");fflush(stdout);
- }
- des->lind = k;
- }
- des->fix = &des->ind[n];
- for (k=0; k<p; k++) des->fix[k] = 0;
- des->n = n; des->p = p;
- des->smwt = n;
- des->xtwx.p = p;
- }
- void deschk(des,n,p)
- design *des;
- int n, p;
- { WARN(("deschk deprecated - use des_init()"));
- des_init(des,n,p);
- }
- int likereg(coef, lk0, f1, Z)
- double *coef, *lk0, *f1, *Z;
- { int i, ii, j, p;
- double lk, ww, link[LLEN], *X;
- if (lf_debug>2) mut_printf(" likereg: %8.5f\n",coef[0]);
- lf_status = LF_OK;
- lk = 0.0; p = lf_des->p;
- setzero(Z,p*p);
- setzero(f1,p);
- for (i=0; i<lf_des->n; i++)
- {
- ii = lf_des->ind[i];
- X = d_xi(lf_des,ii);
- fitv(lf_des,ii) = base(lf_lfd,ii)+innerprod(coef,X,p);
- lf_status = stdlinks(link,lf_lfd,lf_sp,ii,fitv(lf_des,ii),robscale);
- if (lf_status == LF_BADP)
- { *lk0 = -1.0e300;
- return(NR_REDUCE);
- }
- if (lf_error) lf_status = LF_ERR;
- if (lf_status != LF_OK) return(NR_BREAK);
- ww = wght(lf_des,ii);
- lk += ww*link[ZLIK];
- for (j=0; j<p; j++)
- f1[j] += X[j]*ww*link[ZDLL];
- addouter(Z, X, X, p, ww*link[ZDDLL]);
- }
- for (i=0; i<p; i++) if (lf_des->fix[i])
- { for (j=0; j<p; j++) Z[i*p+j] = Z[j*p+i] = 0.0;
- Z[i*p+i] = 1.0;
- f1[i] = 0.0;
- }
- if (lf_debug>4) prresp(coef,Z,p);
- if (lf_debug>3) mut_printf(" likelihood: %8.5f\n",lk);
- *lk0 = lf_des->llk = lk;
- lf_status = fami(lf_sp)->pcheck(lf_sp,lf_des,lf_lfd);
- switch(lf_status)
- { case LF_DONE: return(NR_BREAK);
- case LF_OOB: return(NR_REDUCE);
- case LF_PF: return(NR_REDUCE);
- case LF_NSLN: return(NR_BREAK);
- }
- return(NR_OK);
- }
- int reginit(lfd,des,sp)
- lfdata *lfd;
- design *des;
- smpar *sp;
- { int i, ii;
- double sb, link[LLEN];
- s0 = s1 = sb = 0;
- for (i=0; i<des->n; i++)
- { ii = des->ind[i];
- links(base(lfd,ii),resp(lfd,ii),fami(sp),LINIT,link,cens(lfd,ii),prwt(lfd,ii),1.0);
- s1 += wght(des,ii)*link[ZDLL];
- s0 += wght(des,ii)*prwt(lfd,ii);
- sb += wght(des,ii)*prwt(lfd,ii)*base(lfd,ii);
- }
- if (s0==0) return(LF_NOPT); /* no observations with W>0 */
- setzero(des->cf,des->p);
- lf_tol = 1.0e-6*s0;
- switch(link(sp))
- { case LIDENT:
- des->cf[0] = (s1-sb)/s0;
- return(LF_OK);
- case LLOG:
- if (s1<=0.0)
- { des->cf[0] = -1000;
- return(LF_INFA);
- }
- des->cf[0] = log(s1/s0) - sb/s0;
- return(LF_OK);
- case LLOGIT:
- if (s1<=0.0)
- { des->cf[0] = -1000;
- return(LF_INFA);
- }
- if (s1>=s0)
- { des->cf[0] = 1000;
- return(LF_INFA);
- }
- des->cf[0] = logit(s1/s0)-sb/s0;
- return(LF_OK);
- case LINVER:
- if (s1<=0.0)
- { des->cf[0] = 1e100;
- return(LF_INFA);
- }
- des->cf[0] = s0/s1-sb/s0;
- return(LF_OK);
- case LSQRT:
- des->cf[0] = sqrt(s1/s0)-sb/s0;
- return(LF_OK);
- case LASIN:
- des->cf[0] = asin(sqrt(s1/s0))-sb/s0;
- return(LF_OK);
- default:
- LERR(("reginit: invalid link %d",link(sp)));
- return(LF_ERR);
- }
- }
- int lfinit(lfd,sp,des)
- lfdata *lfd;
- smpar *sp;
- design *des;
- { int initstat;
- des->xtwx.sm = (deg0(sp)<deg(sp)) ? JAC_CHOL : JAC_EIGD;
- designmatrix(lfd,sp,des);
- setfamily(sp);
- initstat = fami(sp)->initial(lfd,des,sp);
- return(initstat);
- }
- void lfiter(lfd,sp,des,maxit)
- lfdata *lfd;
- smpar *sp;
- design *des;
- int maxit;
- { int err;
- if (lf_debug>1) mut_printf(" lfiter: %8.5f\n",des->cf[0]);
- lf_des = des;
- lf_lfd = lfd;
- lf_sp = sp;
- max_nr(fami(sp)->like, des->cf, des->oc, des->res, des->f1,
- &des->xtwx, des->p, maxit, lf_tol, &err);
- switch(err)
- { case NR_OK: return;
- case NR_NCON:
- WARN(("max_nr not converged"));
- return;
- case NR_NDIV:
- WARN(("max_nr reduction problem"));
- return;
- }
- WARN(("max_nr return status %d",err));
- }
- int use_robust_scale(int tg)
- { if ((tg&64)==0) return(0); /* not quasi - no scale */
- if (((tg&128)==0) & (((tg&63)!=TROBT) & ((tg&63)!=TCAUC))) return(0);
- return(1);
- }
- /*
- * noit not really needed any more, since
- * gauss->pcheck returns LF_DONE, and likereg NR_BREAK
- * in gaussian case.
- * nb: 0/1: does local neighborhood and weights need computing?
- * cv: 0/1: is variance/covariance matrix needed?
- */
- int locfit(lfd,des,sp,noit,nb,cv)
- lfdata *lfd;
- design *des;
- smpar *sp;
- int noit, nb, cv;
- { int i;
- if (des->xev==NULL)
- { LERR(("locfit: NULL evaluation point?"));
- return(246);
- }
- if (lf_debug>0)
- { mut_printf("locfit: ");
- for (i=0; i<lfd->d; i++) mut_printf(" %10.6f",des->xev[i]);
- mut_printf("\n");
- }
- /* the 1e-12 avoids problems that can occur with roundoff */
- if (nb) nbhd(lfd,des,(int)(lfd->n*nn(sp)+1e-12),0,sp);
- lf_status = lfinit(lfd,sp,des);
- if (lf_status == LF_OK)
- { if (use_robust_scale(fam(sp)))
- lf_robust(lfd,sp,des,lf_maxit);
- else
- { if ((fam(sp)&63)==TQUANT)
- lfquantile(lfd,sp,des,lf_maxit);
- else
- { robscale = 1.0;
- lfiter(lfd,sp,des,lf_maxit);
- }
- }
- }
- if (lf_status == LF_DONE) lf_status = LF_OK;
- if (lf_status == LF_OOB) lf_status = LF_OK;
- if ((fam(sp)&63)==TDEN) /* convert from rate to density */
- { switch(link(sp))
- { case LLOG:
- des->cf[0] -= log(des->smwt);
- break;
- case LIDENT:
- multmatscal(des->cf,1.0/des->smwt,des->p);
- break;
- default: LERR(("Density adjustment; invalid link"));
- }
- }
- /* variance calculations, if requested */
- if (cv)
- { switch(lf_status)
- { case LF_PF: /* for these cases, variance calc. would likely fail. */
- case LF_NOPT:
- case LF_NSLN:
- case LF_INFA:
- case LF_DEMP:
- case LF_XOOR:
- case LF_DNOP:
- case LF_BADP:
- des->llk = des->tr0 = des->tr1 = des->tr2 = 0.0;
- setzero(des->V,des->p*des->p);
- setzero(des->f1,des->p);
- break;
- default: lf_vcov(lfd,sp,des);
- }
- }
- return(lf_status);
- }
- void lf_status_msg(status)
- int status;
- { switch(status)
- { case LF_OK: return;
- case LF_NCON: WARN(("locfit did not converge")); return;
- case LF_OOB: WARN(("parameters out of bounds")); return;
- case LF_PF: WARN(("perfect fit")); return;
- case LF_NOPT: WARN(("no points with non-zero weight")); return;
- case LF_NSLN: WARN(("no solution")); return;
- case LF_INFA: WARN(("initial value problem")); return;
- case LF_DEMP: WARN(("density estimate, empty integration region")); return;
- case LF_XOOR: WARN(("procv: fit point outside xlim region")); return;
- case LF_DNOP: WARN(("density estimation -- insufficient points in smoothing window")); return;
- case LF_BADP: WARN(("bad parameters")); return;
- default: WARN(("procv: unknown return code %d",status)); return;
- } }
- /*
- * Copyright 1996-2006 Catherine Loader.
- */
- /*
- * Compute minimax weights for local regression.
- */
- #include "locf.h"
- #define NR_EMPTY 834
- int mmsm_ct;
- static int debug=0;
- #define CONVTOL 1.0e-8
- #define SINGTOL 1.0e-10
- #define NR_SINGULAR 100
- static lfdata *mm_lfd;
- static design *mm_des;
- static double mm_gam, mmf, lb;
- static int st;
- double ipower(x,n) /* use for n not too large!! */
- double x;
- int n;
- { if (n==0) return(1.0);
- if (n<0) return(1/ipower(x,-n));
- return(x*ipower(x,n-1));
- }
- double setmmwt(des,a,gam)
- design *des;
- double *a, gam;
- { double ip, w0, w1, sw, wt;
- int i;
- sw = 0.0;
- for (i=0; i<mm_lfd->n; i++)
- { ip = innerprod(a,d_xi(des,i),des->p);
- wt = prwt(mm_lfd,i);
- w0 = ip - gam*des->wd[i];
- w1 = ip + gam*des->wd[i];
- wght(des,i) = 0.0;
- if (w0>0) { wght(des,i) = w0; sw += wt*w0*w0; }
- if (w1<0) { wght(des,i) = w1; sw += wt*w1*w1; }
- }
- return(sw/2-a[0]);
- }
- /* compute sum_{w!=0} AA^T; e1-sum wA */
- int mmsums(des,coef,f,z,J)
- design *des;
- double *coef, *f, *z;
- jacobian *J;
- { int ct, i, j, p, sing;
- double *A;
- mmsm_ct++;
- A = J->Z;
- *f = setmmwt(des,coef,mm_gam);
- p = des->p;
- setzero(A,p*p);
- setzero(z,p);
- z[0] = 1.0;
- ct = 0;
- for (i=0; i<mm_lfd->n; i++)
- if (wght(des,i)!=0.0)
- { addouter(A,d_xi(des,i),d_xi(des,i),p,prwt(mm_lfd,i));
- for (j=0; j<p; j++) z[j] -= prwt(mm_lfd,i)*wght(des,i)*d_xij(des,i,j);
- ct++;
- }
- if (ct==0) return(NR_EMPTY);
- J->st = JAC_RAW;
- J->p = p;
- jacob_dec(J,JAC_EIGD);
- sing = 0;
- for (i=0; i<p; i++) sing |= (J->Z[i*p+i]<SINGTOL);
- if ((debug) & (sing)) mut_printf("SINGULAR!!!!\n");
- return((sing) ? NR_SINGULAR : NR_OK);
- }
- int descenddir(des,coef,dlt,f,af)
- design *des;
- double *coef, *dlt, *f;
- int af;
- { int i, p;
- double f0, *oc;
- if (debug) mut_printf("descenddir: %8.5f %8.5f\n",dlt[0],dlt[1]);
- f0 = *f;
- oc = des->oc;
- p = des->p;
- memcpy(oc,coef,p*sizeof(double));
- for (i=0; i<p; i++) coef[i] = oc[i]+lb*dlt[i];
- st = mmsums(des,coef,f,des->f1,&des->xtwx);
- if (*f>f0) /* halve till we drop */
- { while (*f>f0)
- { lb = lb/2.0;
- for (i=0; i<p; i++) coef[i] = oc[i]+lb*dlt[i];
- st = mmsums(des,coef,f,des->f1,&des->xtwx);
- }
- return(st);
- }
- if (!af) return(st);
- /* double */
- while (*f<f0)
- { f0 = *f;
- lb *= 2.0;
- for (i=0; i<p; i++) coef[i] = oc[i]+lb*dlt[i];
- st = mmsums(des,coef,f,des->f1,&des->xtwx);
- }
- lb /= 2.0;
- for (i=0; i<p; i++) coef[i] = oc[i]+lb*dlt[i];
- st = mmsums(des,coef,f,des->f1,&des->xtwx);
- return(st);
- }
- int mm_initial(des)
- design *des;
- { double *dlt;
- dlt = des->ss;
- setzero(des->cf,des->p);
- st = mmsums(des,des->cf,&mmf,des->f1,&des->xtwx);
- setzero(dlt,des->p);
- dlt[0] = 1;
- lb = 1.0;
- st = descenddir(des,des->cf,dlt,&mmf,1);
- return(st);
- }
- void getsingdir(des,dlt)
- design *des;
- double *dlt;
- { double f, sw, c0;
- int i, j, p, sd;
- sd = -1; p = des->p;
- setzero(dlt,p);
- for (i=0; i<p; i++) if (des->xtwx.Z[i*p+i]<SINGTOL) sd = i;
- if (sd==-1)
- { mut_printf("getsingdir: nonsing?\n");
- return;
- }
- if (des->xtwx.dg[sd]>0)
- for (i=0; i<p; i++) dlt[i] = des->xtwx.Q[p*i+sd]*des->xtwx.dg[i];
- else
- { dlt[sd] = 1.0;
- }
- c0 = innerprod(dlt,des->f1,p);
- if (c0<0) for (i=0; i<p; i++) dlt[i] = -dlt[i];
- }
- void mmax(coef, old_coef, delta, J, p, maxit, tol, err)
- double *coef, *old_coef, *delta, tol;
- int p, maxit, *err;
- jacobian *J;
- { double old_f, lambda;
- int i, j;
- *err = NR_OK;
-
- for (j=0; j<maxit; j++)
- { memcpy(old_coef,coef,p*sizeof(double));
- old_f = mmf;
- if (st == NR_SINGULAR)
- {
- getsingdir(mm_des,delta);
- st = descenddir(mm_des,coef,delta,&mmf,1);
- }
- if (st == NR_EMPTY)
- {
- setzero(delta,p);
- delta[0] = 1.0;
- st = descenddir(mm_des,coef,delta,&mmf,1);
- }
- if (st == NR_OK)
- {
- lb = 1.0;
- jacob_solve(J,mm_des->f1);
- memcpy(delta,mm_des->f1,p*sizeof(double));
- st = descenddir(mm_des,coef,delta,&mmf,0);
- }
- if ((j>0) & (fabs(mmf-old_f)<tol)) return;
- }
- WARN(("findab not converged"));
- *err = NR_NCON;
- return;
- }
- double findab(gam)
- double gam;
- { double sl;
- int i, p, nr_stat;
- if (debug) mut_printf(" findab: gam %8.5f\n",gam);
- mm_gam = gam;
- p = mm_des->p;
- lb = 1.0;
- st = mm_initial(mm_des);
- mmax(mm_des->cf, mm_des->oc, mm_des->ss,
- &mm_des->xtwx, p, lf_maxit, CONVTOL, &nr_stat);
- sl = 0.0;
- for (i=0; i<mm_lfd->n; i++) sl += fabs(wght(mm_des,i))*mm_des->wd[i];
- if (debug) mut_printf(" sl %8.5f gam %8.5f %8.5f %d\n", sl,gam,sl-gam,nr_stat);
- return(sl-gam);
- }
- double weightmm(coef,di,ff,gam)
- double *coef, di, *ff, gam;
- { double y1, y2, ip;
- ip = innerprod(ff,coef,mm_des->p);
- y1 = ip-gam*di; if (y1>0) return(y1/ip);
- y2 = ip+gam*di; if (y2<0) return(y2/ip);
- return(0.0);
- }
- double minmax(lfd,des,sp)
- lfdata *lfd;
- design *des;
- smpar *sp;
- { double h, u[MXDIM], gam;
- int i, j, m, d1, p1, err_flag;
- if (debug) mut_printf("minmax: x %8.5f\n",des->xev[0]);
- mm_lfd = lfd;
- mm_des = des;
- mmsm_ct = 0;
- d1 = deg(sp)+1;
- p1 = factorial(d1);
- for (i=0; i<lfd->n; i++)
- { for (j=0; j<lfd->d; j++) u[j] = datum(lfd,j,i);
- des->wd[i] = sp->nn/p1*ipower(dist(des,i),d1);
- des->ind[i] = i;
- fitfun(lfd, sp, u,des->xev,d_xi(des,i),NULL);
- }
- /* find gamma (i.e. solve eqn 13.17 from book), using the secant method.
- * As a side effect, this finds the other minimax coefficients.
- * Note that 13.17 is rewritten as
- * g2 = sum |l_i(x)| (||xi-x||^(p+1) M/(s*(p+1)!))
- * where g2 = gamma * s * (p+1)! / M. The gam variable below is g2.
- * The smoothing parameter is sp->nn == M/s.
- */
- gam = solve_secant(findab, 0.0, 0.0,1.0, 0.0000001, BDF_EXPRIGHT, &err_flag);
- /*
- * Set the smoothing weights, in preparation for the actual fit.
- */
- h = 0.0; m = 0;
- for (i=0; i<lfd->n; i++)
- { wght(des,i) = weightmm(des->cf, des->wd[i],d_xi(des,i),gam);
- if (wght(des,i)>0)
- { if (dist(des,i)>h) h = dist(des,i);
- des->ind[m] = i;
- m++;
- }
- }
- des->n = m;
- return(h);
- }
- /*
- * Copyright 1996-2006 Catherine Loader.
- */
- /*
- *
- * Defines the weight functions and related quantities used
- * in LOCFIT.
- */
- #include "locf.h"
- /*
- * convert kernel and kernel type strings to numeric codes.
- */
- #define NWFUNS 13
- static char *wfuns[NWFUNS] = {
- "rectangular", "epanechnikov", "bisquare", "tricube",
- "triweight", "gaussian", "triangular", "ququ",
- "6cub", "minimax", "exponential", "maclean", "parametric" };
- static int wvals[NWFUNS] = { WRECT, WEPAN, WBISQ, WTCUB,
- WTRWT, WGAUS, WTRIA, WQUQU, W6CUB, WMINM, WEXPL, WMACL, WPARM };
- int lfkernel(char *z)
- { return(pmatch(z, wfuns, wvals, NWFUNS, WTCUB));
- }
- #define NKTYPE 5
- static char *ktype[NKTYPE] = { "spherical", "product", "center", "lm", "zeon" };
- static int kvals[NKTYPE] = { KSPH, KPROD, KCE, KLM, KZEON };
- int lfketype(char *z)
- { return(pmatch(z, ktype, kvals, NKTYPE, KSPH));
- }
- /* The weight functions themselves. Used everywhere. */
- double W(u,ker)
- double u;
- int ker;
- { u = fabs(u);
- switch(ker)
- { case WRECT: return((u>1) ? 0.0 : 1.0);
- case WEPAN: return((u>1) ? 0.0 : 1-u*u);
- case WBISQ: if (u>1) return(0.0);
- u = 1-u*u; return(u*u);
- case WTCUB: if (u>1) return(0.0);
- u = 1-u*u*u; return(u*u*u);
- case WTRWT: if (u>1) return(0.0);
- u = 1-u*u; return(u*u*u);
- case WQUQU: if (u>1) return(0.0);
- u = 1-u*u; return(u*u*u*u);
- case WTRIA: if (u>1) return(0.0);
- return(1-u);
- case W6CUB: if (u>1) return(0.0);
- u = 1-u*u*u; u = u*u*u; return(u*u);
- case WGAUS: return(exp(-SQR(GFACT*u)/2.0));
- case WEXPL: return(exp(-EFACT*u));
- case WMACL: return(1/((u+1.0e-100)*(u+1.0e-100)));
- case WMINM: LERR(("WMINM in W"));
- return(0.0);
- case WPARM: return(1.0);
- }
- LERR(("W(): Unknown kernel %d\n",ker));
- return(1.0);
- }
- int iscompact(ker)
- int ker;
- { if ((ker==WEXPL) | (ker==WGAUS) | (ker==WMACL) | (ker==WPARM)) return(0);
- return(1);
- }
- double weightprod(lfd,u,h,ker)
- lfdata *lfd;
- double *u, h;
- int ker;
- { int i;
- double sc, w;
- w = 1.0;
- for (i=0; i<lfd->d; i++)
- { sc = lfd->sca[i];
- switch(lfd->sty[i])
- { case STLEFT:
- if (u[i]>0) return(0.0);
- w *= W(-u[i]/(h*sc),ker);
- break;
- case STRIGH:
- if (u[i]<0) return(0.0);
- w *= W(u[i]/(h*sc),ker);
- break;
- case STANGL:
- w *= W(2*fabs(sin(u[i]/(2*sc)))/h,ker);
- break;
- case STCPAR:
- break;
- default:
- w *= W(fabs(u[i])/(h*sc),ker);
- }
- if (w==0.0) return(w);
- }
- return(w);
- }
- double weightsph(lfd,u,h,ker, hasdi,di)
- lfdata *lfd;
- double *u, h, di;
- int ker, hasdi;
- { int i;
- if (!hasdi) di = rho(u,lfd->sca,lfd->d,KSPH,lfd->sty);
- for (i=0; i<lfd->d; i++)
- { if ((lfd->sty[i]==STLEFT) && (u[i]>0.0)) return(0.0);
- if ((lfd->sty[i]==STRIGH) && (u[i]<0.0)) return(0.0);
- }
- if (h==0) return((di==0.0) ? 1.0 : 0.0);
- return(W(di/h,ker));
- }
- double weight(lfd,sp,x,t,h, hasdi,di)
- lfdata *lfd;
- smpar *sp;
- double *x, *t, h, di;
- int hasdi;
- { double u[MXDIM];
- int i;
- for (i=0; i<lfd->d; i++) u[i] = (t==NULL) ? x[i] : x[i]-t[i];
- switch(kt(sp))
- { case KPROD: return(weightprod(lfd,u,h,ker(sp)));
- case KSPH: return(weightsph(lfd,u,h,ker(sp), hasdi,di));
- }
- LERR(("weight: unknown kernel type %d",kt(sp)));
- return(1.0);
- }
- double sgn(x)
- double x;
- { if (x>0) return(1.0);
- if (x<0) return(-1.0);
- return(0.0);
- }
- double WdW(u,ker) /* W'(u)/W(u) */
- double u;
- int ker;
- { double eps=1.0e-10;
- if (ker==WGAUS) return(-GFACT*GFACT*u);
- if (ker==WPARM) return(0.0);
- if (fabs(u)>=1) return(0.0);
- switch(ker)
- { case WRECT: return(0.0);
- case WTRIA: return(-sgn(u)/(1-fabs(u)+eps));
- case WEPAN: return(-2*u/(1-u*u+eps));
- case WBISQ: return(-4*u/(1-u*u+eps));
- case WTRWT: return(-6*u/(1-u*u+eps));
- case WTCUB: return(-9*sgn(u)*u*u/(1-u*u*fabs(u)+eps));
- case WEXPL: return((u>0) ? -EFACT : EFACT);
- }
- LERR(("WdW: invalid kernel"));
- return(0.0);
- }
- /* deriv. weights .. spherical, product etc
- u, sc, sty needed only in relevant direction
- Acutally, returns (d/dx W(||x||/h) ) / W(.)
- */
- double weightd(u,sc,d,ker,kt,h,sty,di)
- double u, sc, h, di;
- int d, ker, kt, sty;
- { if (sty==STANGL)
- { if (kt==KPROD)
- return(-WdW(2*sin(u/(2*sc)),ker)*cos(u/(2*sc))/(h*sc));
- if (di==0.0) return(0.0);
- return(-WdW(di/h,ker)*sin(u/sc)/(h*sc*di));
- }
- if (sty==STCPAR) return(0.0);
- if (kt==KPROD)
- return(-WdW(u/(h*sc),ker)/(h*sc));
- if (di==0.0) return(0.0);
- return(-WdW(di/h,ker)*u/(h*di*sc*sc));
- }
- double weightdd(u,sc,d,ker,kt,h,sty,di,i0,i1)
- double *u, *sc, h, di;
- int d, ker, kt, i0, i1, *sty;
- { double w;
- w = 1;
- if (kt==KPROD)
- {
- w = WdW(u[i0]/(h*sc[i0]),ker)*WdW(u[i1]/(h*sc[i1]),ker)/(h*h*sc[i0]*sc[i1]);
- }
- return(0.0);
- }
- /* Derivatives W'(u)/u.
- Used in simult. conf. band computations,
- and kernel density bandwidth selectors. */
- double Wd(u,ker)
- double u;
- int ker;
- { double v;
- if (ker==WGAUS) return(-SQR(GFACT)*exp(-SQR(GFACT*u)/2));
- if (ker==WPARM) return(0.0);
- if (fabs(u)>1) return(0.0);
- switch(ker)
- { case WEPAN: return(-2.0);
- case WBISQ: return(-4*(1-u*u));
- case WTCUB: v = 1-u*u*u;
- return(-9*v*v*u);
- case WTRWT: v = 1-u*u;
- return(-6*v*v);
- default: LERR(("Invalid kernel %d in Wd",ker));
- }
- return(0.0);
- }
- /* Second derivatives W''(u)-W'(u)/u.
- used in simult. conf. band computations in >1 dimension. */
- double Wdd(u,ker)
- double u;
- int ker;
- { double v;
- if (ker==WGAUS) return(SQR(u*GFACT*GFACT)*exp(-SQR(u*GFACT)/2));
- if (ker==WPARM) return(0.0);
- if (u>1) return(0.0);
- switch(ker)
- { case WBISQ: return(12*u*u);
- case WTCUB: v = 1-u*u*u;
- return(-9*u*v*v+54*u*u*u*u*v);
- case WTRWT: return(24*u*u*(1-u*u));
- default: LERR(("Invalid kernel %d in Wdd",ker));
- }
- return(0.0);
- }
- /* int u1^j1..ud^jd W(u) du.
- Used for local log-linear density estimation.
- Assume all j_i are even.
- Also in some bandwidth selection.
- */
- double wint(d,j,nj,ker)
- int d, *j, nj, ker;
- { double I, z;
- int k, dj;
- dj = d;
- for (k=0; k<nj; k++) dj += j[k];
- switch(ker) /* int_0^1 u^(dj-1) W(u)du */
- { case WRECT: I = 1.0/dj; break;
- case WEPAN: I = 2.0/(dj*(dj+2)); break;
- case WBISQ: I = 8.0/(dj*(dj+2)*(dj+4)); break;
- case WTCUB: I = 162.0/(dj*(dj+3)*(dj+6)*(dj+9)); break;
- case WTRWT: I = 48.0/(dj*(dj+2)*(dj+4)*(dj+6)); break;
- case WTRIA: I = 1.0/(dj*(dj+1)); break;
- case WQUQU: I = 384.0/(dj*(dj+2)*(dj+4)*(dj+6)*(dj+8)); break;
- case W6CUB: I = 524880.0/(dj*(dj+3)*(dj+6)*(dj+9)*(dj+12)*(dj+15)*(dj+18)); break;
- case WGAUS: switch(d)
- { case 1: I = S2PI/GFACT; break;
- case 2: I = 2*PI/(GFACT*GFACT); break;
- default: I = exp(d*log(S2PI/GFACT)); /* for nj=0 */
- }
- for (k=0; k<nj; k++) /* deliberate drop */
- switch(j[k])
- { case 4: I *= 3.0/(GFACT*GFACT);
- case 2: I /= GFACT*GFACT;
- }
- return(I);
- case WEXPL: I = factorial(dj-1)/ipower(EFACT,dj); break;
- default: LERR(("Unknown kernel %d in exacint",ker));
- }
- if ((d==1) && (nj==0)) return(2*I); /* common case quick */
- z = (d-nj)*LOGPI/2-mut_lgammai(dj);
- for (k=0; k<nj; k++) z += mut_lgammai(j[k]+1);
- return(2*I*exp(z));
- }
- /* taylor series expansion of weight function around x.
- 0 and 1 are common arguments, so are worth programming
- as special cases.
- Used in density estimation.
- */
- int wtaylor(f,x,ker)
- double *f, x;
- int ker;
- { double v;
- switch(ker)
- { case WRECT:
- f[0] = 1.0;
- return(1);
- case WEPAN:
- f[0] = 1-x*x; f[1] = -2*x; f[2] = -1;
- return(3);
- case WBISQ:
- v = 1-x*x;
- f[0] = v*v; f[1] = -4*x*v; f[2] = 4-6*v;
- f[3] = 4*x; f[4] = 1;
- return(5);
- case WTCUB:
- if (x==1.0)
- { f[0] = f[1] = f[2] = 0; f[3] = -27; f[4] = -81; f[5] = -108;
- f[6] = -81; f[7] = -36; f[8] = -9; f[9] = -1; return(10); }
- if (x==0.0)
- { f[1] = f[2] = f[4] = f[5] = f[7] = f[8] = 0;
- f[0] = 1; f[3] = -3; f[6] = 3; f[9] = -1; return(10); }
- v = 1-x*x*x;
- f[0] = v*v*v; f[1] = -9*v*v*x*x; f[2] = x*v*(27-36*v);
- f[3] = -27+v*(108-84*v); f[4] = -3*x*x*(27-42*v);
- f[5] = x*(-108+126*v); f[6] = -81+84*v;
- f[7] = -36*x*x; f[8] = -9*x; f[9] = -1;
- return(10);
- case WTRWT:
- v = 1-x*x;
- f[0] = v*v*v; f[1] = -6*x*v*v; f[2] = v*(12-15*v);
- f[3] = x*(20*v-8); f[4] = 15*v-12; f[5] = -6; f[6] = -1;
- return(7);
- case WTRIA:
- f[0] = 1-x; f[1] = -1;
- return(2);
- case WQUQU:
- v = 1-x*x;
- f[0] = v*v*v*v; f[1] = -8*x*v*v*v; f[2] = v*v*(24-28*v);
- f[3] = v*x*(56*v-32); f[4] = (70*v-80)*v+16; f[5] = x*(32-56*v);
- f[6] = 24-28*v; f[7] = 8*x; f[8] = 1;
- return(9);
- case W6CUB:
- v = 1-x*x*x;
- f[0] = v*v*v*v*v*v;
- f[1] = -18*x*x*v*v*v*v*v;
- f[2] = x*v*v*v*v*(135-153*v);
- f[3] = v*v*v*(-540+v*(1350-816*v));
- f[4] = x*x*v*v*(1215-v*(4050-v*3060));
- f[5] = x*v*(-1458+v*(9234+v*(-16254+v*8568)));
- f[6] = 729-v*(10206-v*(35154-v*(44226-v*18564)));
- f[7] = x*x*(4374-v*(30132-v*(56862-v*31824)));
- f[8] = x*(12393-v*(61479-v*(92664-v*43758)));
- f[9] = 21870-v*(89100-v*(115830-v*48620));
- f[10]= x*x*(26730-v*(69498-v*43758));
- f[11]= x*(23814-v*(55458-v*31824));
- f[12]= 15849-v*(34398-v*18564);
- f[13]= x*x*(7938-8568*v);
- f[14]= x*(2970-3060*v);
- f[15]= 810-816*v;
- f[16]= 153*x*x;
- f[17]= 18*x;
- f[18]= 1;
- return(19);
- }
- LERR(("Invalid kernel %d in wtaylor",ker));
- return(0);
- }
- /* convolution int W(x)W(x+v)dx.
- used in kde bandwidth selection.
- */
- double Wconv(v,ker)
- double v;
- int ker;
- { double v2;
- switch(ker)
- { case WGAUS: return(SQRPI/GFACT*exp(-SQR(GFACT*v)/4));
- case WRECT:
- v = fabs(v);
- if (v>2) return(0.0);
- return(2-v);
- case WEPAN:
- v = fabs(v);
- if (v>2) return(0.0);
- return((2-v)*(16+v*(8-v*(16-v*(2+v))))/30);
- case WBISQ:
- v = fabs(v);
- if (v>2) return(0.0);
- v2 = 2-v;
- return(v2*v2*v2*v2*v2*(16+v*(40+v*(36+v*(10+v))))/630);
- }
- LERR(("Wconv not implemented for kernel %d",ker));
- return(0.0);
- }
- /* derivative of Wconv.
- 1/v d/dv int W(x)W(x+v)dx
- used in kde bandwidth selection.
- */
- double Wconv1(v,ker)
- double v;
- int ker;
- { double v2;
- v = fabs(v);
- switch(ker)
- { case WGAUS: return(-0.5*SQRPI*GFACT*exp(-SQR(GFACT*v)/4));
- case WRECT:
- if (v>2) return(0.0);
- return(1.0);
- case WEPAN:
- if (v>2) return(0.0);
- return((-16+v*(12-v*v))/6);
- case WBISQ:
- if (v>2) return(0.0);
- v2 = 2-v;
- return(-v2*v2*v2*v2*(32+v*(64+v*(24+v*3)))/210);
- }
- LERR(("Wconv1 not implemented for kernel %d",ker));
- return(0.0);
- }
- /* 4th derivative of Wconv.
- used in kde bandwidth selection (BCV, SJPI, GKK)
- */
- double Wconv4(v,ker)
- double v;
- int ker;
- { double gv;
- switch(ker)
- { case WGAUS:
- gv = GFACT*v;
- return(exp(-SQR(gv)/4)*GFACT*GFACT*GFACT*(12-gv*gv*(12-gv*gv))*SQRPI/16);
- }
- LERR(("Wconv4 not implemented for kernel %d",ker));
- return(0.0);
- }
- /* 5th derivative of Wconv.
- used in kde bandwidth selection (BCV method only)
- */
- double Wconv5(v,ker) /* (d/dv)^5 int W(x)W(x+v)dx */
- double v;
- int ker;
- { double gv;
- switch(ker)
- { case WGAUS:
- gv = GFACT*v;
- return(-exp(-SQR(gv)/4)*GFACT*GFACT*GFACT*GFACT*gv*(60-gv*gv*(20-gv*gv))*SQRPI/32);
- }
- LERR(("Wconv5 not implemented for kernel %d",ker));
- return(0.0);
- }
- /* 6th derivative of Wconv.
- used in kde bandwidth selection (SJPI)
- */
- double Wconv6(v,ker)
- double v;
- int ker;
- { double gv, z;
- switch(ker)
- { case WGAUS:
- gv = GFACT*v;
- gv = gv*gv;
- z = exp(-gv/4)*(-120+gv*(180-gv*(30-gv)))*0.02769459142;
- gv = GFACT*GFACT;
- return(z*gv*gv*GFACT);
- }
- LERR(("Wconv6 not implemented for kernel %d",ker));
- return(0.0);
- }
- /* int W(v)^2 dv / (int v^2 W(v) dv)^2
- used in some bandwidth selectors
- */
- double Wikk(ker,deg)
- int ker, deg;
- { switch(deg)
- { case 0:
- case 1: /* int W(v)^2 dv / (int v^2 W(v) dv)^2 */
- switch(ker)
- { case WRECT: return(4.5);
- case WEPAN: return(15.0);
- case WBISQ: return(35.0);
- case WGAUS: return(0.2820947918*GFACT*GFACT*GFACT*GFACT*GFACT);
- case WTCUB: return(34.152111046847892); /* 59049 / 1729 */
- case WTRWT: return(66.083916083916080); /* 9450/143 */
- }
- case 2:
- case 3: /* 4!^2/8*int(W1^2)/int(v^4W1)^2
- W1=W*(n4-v^2n2)/(n0n4-n2n2) */
- switch(ker)
- { case WRECT: return(11025.0);
- case WEPAN: return(39690.0);
- case WBISQ: return(110346.9231);
- case WGAUS: return(14527.43412);
- case WTCUB: return(126500.5904);
- case WTRWT: return(254371.7647);
- }
- }
- LERR(("Wikk not implemented for kernel %d, deg %d",ker,deg));
- return(0.0);
- }
|