ポアソン回帰モデル
プログラム
#include <oxstd.h> #import <maximize> // mx: independent variable, vy: dependent variable // We need to use them in the following subroutine. So set them static. static decl mx,vm,vy; fLK(const dX, const adFunc, const avScore, const amHess){ // fLK will calculate the value of log posterior density as adFunc[0] decl vb;vb=dX; adFunc[0]=-vb'*vb/2000+sumc((vy.*mx))*vb-sumc(vm.*exp(mx*vb)); // use the following instead for the improper prior // adFunc[0]=sumc((vy.*mx))*vb-sumc(vm.*exp(mx*vb)); // 1st derivative vector if(avScore){ avScore[0]=-vb/1000+sumc(vy.*mx)'-sumc(vm.*exp(mx*vb).*mx)';} // use the following for the improper prior // avScore[0]=sumc(vy.*mx)'-sumc(vm.*exp(mx*vb).*mx)';} // 2nd derivative matrix, Hessian matrix if(amHess){ amHess[0]=-1/1000*unit(rows(vb))-mx'*(vm.*exp(mx*vb).*mx);} // use the following for the improper prior //amHess[0]=-mx'*(vm.*exp(mx*vb).*mx);} // return 1; } // main(){ decl cburn,cm,cn,cnobs,crepeat; decl car_accept,car_count,cmh_accept,cmh_count; decl dh_n,dh_o,dlhat,dl_n,dl_o,dfrac; decl vb,vb1,vb_o,vb_n,veig,vbhat,vl1hat; decl file,mdata,mbeta_s,mB1,mB1root,ml2hat; // Read Data File // X1-X8, m_i, y cnobs=34;file = fopen("ship.txt");fscan(file,"%#m",cnobs,10,&mdata);fclose(file); // dependent varible: yesvm vy=mdata[][9]; // y //independent variables: Constant X1-X8 mx=ones(cnobs,1)~mdata[][0:7];cm=columns(mx); vm=mdata[][8]; // m // Suppose that the prior for beta is set as follows: vb ~ N(0,1000I). // To use improper prior, edit the subroutine fLK as mentioned above. vbhat=zeros(cm,1);MaxBFGS(fLK,&vbhat,&dlhat,0,1); fLK(vbhat,&dlhat,&vl1hat,&ml2hat); // If improper prior is used, print the following lines to get // maximum likelihood estimates // println("Maximum Likelihood Estimate=",vbhat'); // println("Standard Deviation of M.L.E.=",sqrt(diagonal(invert(-ml2hat)))); // vbhat : posterior mode // dlhat : log posterior evaluated at mode (constant term ignored) // vl1hat: 1st derivative vector of log posterior evaluated at mode // ml2hat: 2nd derivative matrix of log posterior evaluated at mode // Hessian matrix mB1=-invert(ml2hat); // check whether mB1 is positive definite (just in case) // veig: eigenvalues of mB1. should be > 0 for positive definite matrix eigen(mB1,&veig); if(min(veig)<=0) {println("mB1 is not positive definite! Something may be wrong with the program.");} mB1root=choleski(mB1);vb1=mB1*(vl1hat+invert(mB1)*vbhat); // // Burn-in period: crepeat, Number of samples: cburn // Since convergence seems slow, we take crepeat=20,000. crepeat=20000;cburn=2000; mbeta_s=zeros(crepeat,cm); car_accept=car_count=cmh_accept=cmh_count=0; vb=vbhat; for(cn=-cburn;cn<crepeat;++cn){ // A-R Step vb_o=vb; fLK(vb_o,&dl_o,0,0); dh_o=dlhat+vl1hat'*(vb_o-vbhat)+0.5*(vb_o-vbhat)'*ml2hat*(vb_o-vbhat); // generate new beta do{ vb_n=vb1+mB1root*rann(cm,1); fLK(vb_n,&dl_n,0,0); dh_n=dlhat+vl1hat'*(vb_n-vbhat)+0.5*(vb_n-vbhat)'*ml2hat*(vb_n-vbhat); car_count+=1; }while(ranu(1,1)>=exp(dl_n-dh_n)); car_accept+=1; // M-H Step dfrac=exp(dl_n+min(dl_o|dh_o)-dl_o-min(dl_n|dh_n)); if(ranu(1,1)<=dfrac){vb=vb_n;cmh_accept+=1;} cmh_count+=1; if(cn>=0){mbeta_s[cn][]=vb';} } format(200); file = fopen("beta.txt","w");fprint(file,"%15.10f",mbeta_s);fclose(file); // check whether the argorithm was efficient enough. println("AR acceptance=",car_accept/car_count); println("MH acceptance=",cmh_accept/cmh_count); }