#include "MLEstimation2Dist.h" /*-------------------------------------------------------------------------------------------*/ //Function declarations: void ReadDates(int noDates, char *DatesFile, int *dates, int intervention_date[]); int ReadParam(char *ParamFile, param *GP); double neglogLikelihood(int noDates, int *dates, int intervention_date[], int *pfail, workparam Pi); double PDF(double x, double kappa, double eta); double gammaPDF(double x, double alpha, double beta); double weibullPDF(double x, double kappa, double eta); double MLE(char *InputFile , int noDates, int *dates, int intervention_date[], int ndim, int DoFit, param *GP); void ConstructLHTable(int num, int paramnum, double **SamplePoints); void FindMin(int i, int ndim, int noDates, int *dates, int intervention_date[], param *GP, workparam P[], double L[], workparam Pres[], double Lres[], int itres[]); int amoeba(int noDates, int *dates, int intervention_date[], workparam P[], double L[], double ftol, int ndim, double (*funk)(int, int *, int [], int*, workparam), int *nfunk); double amotry(int noDates, int *dates, int intervention_date[], workparam P[], double L[], workparam psum, int ndim, double (*funk)(int, int *, int [], int *, workparam), int worst, double fac); void OutputRes(char *OutputFile, int ndim, int DoFit, workparam Pres[], double Lres[], int itres[]); int CalcCIs(char *InputFile, int noDates, int *dates, int intervention_date[], int ndim, param *GP, double optL); /*-------------------------------------------------------------------------------------------*/ //Global variables: double **matrix; int DIST_SWITCH; //0 for gamma distribution, 1 for weibull distribution, //2 for negative binomial distribution. //currently, only 1=weibull works. long seed, rcount; /*-------------------------------------------------------------------------------------------*/ /*-------------------------------------------------------------------------------------------*/ int main(int argc, char *argv[]) { int noDates; char DatesFile[255], ParamFile[255]; int *dates; int intervention_date[NUM_INTERVAL-1]; param *GP; int DoFit, ndim; double neg2lnL; int i, loop, fail; if(argc<7) ERR_CRITICAL("Syntax:\n \n"); sscanf(argv[1],"%d", &DIST_SWITCH); sscanf(argv[2],"%d", &noDates); sscanf(argv[3],"%s", DatesFile); sscanf(argv[4],"%s", ParamFile); sscanf(argv[5],"%d", &DoFit); sscanf(argv[6],"%d", &seed); if(seed>0) seed=-seed; if(seed==0) { seed=(long)time(NULL); if(seed>0) seed=-seed; } if(noDates<=0) ERR_CRITICAL("positive number of dates necessary.\n"); if(!(dates=(int *) calloc(noDates+1, sizeof(int)))) ERR_CRITICAL("Unable to allocate memory for list of dates.\n"); if(!(GP=(param *) malloc(sizeof(param)))) ERR_CRITICAL("Unable to allcoate memory for parameters GP.\n"); if(!(matrix=dmatrix(0, noDates, 0, noDates))) ERR_CRITICAL("main: Unable to allocate memory for matrix.\n"); ReadDates(noDates, DatesFile, dates, intervention_date); ndim=ReadParam(ParamFile, GP); neg2lnL=MLE(DatesFile, noDates, dates, intervention_date, ndim, DoFit, GP); fail=CalcCIs(DatesFile, noDates, dates, intervention_date, ndim, GP, neg2lnL); if(DoFit>0) loop=DoFit; else loop=1000; for(i=0;i=0;i++){ eofind=fscanf(indat, "%d", &dates[i]); } if(eofind<0) ERR_CRITICAL("ReadDates: input file does not contain enough dates.\n"); fclose(indat); sprintf(filename, "%s.inter.txt", DatesFile); if(!(indat=fopen(filename, "r"))) ERR_CRITICAL("ReadDates: Unable to open file for input.\n"); for(i=0;i=0;i++) { eofind=fscanf(indat, "%d", &intervention_date[i]); } if(eofind<0) ERR_CRITICAL("ReadDates: intervention input file does not contain enough dates.\n"); fclose(indat); printf("Successfully read list of dates.\n"); } /*-------------------------------------------------------------------------------------------*/ /*-------------------------------------------------------------------------------------------*/ int ReadParam(char *ParamFile, param *GP){ FILE *indat; char filename[255]; int j; int scan=0; sprintf(filename, "%s.txt", ParamFile); if(!(indat=fopen(filename, "r"))) ERR_CRITICAL("ReadParam: Unable to open file for input.\n"); for(j=0;jindex.array[j].Value, &GP->index.array[j].Min, &GP->index.array[j].Max, &GP->index.array[j].Scale, &GP->index.array[j].Scan); if(GP->index.array[j].Scan==1 || GP->index.array[j].Scan==2) scan++; } fclose(indat); printf("Successfully read Params.\n"); return scan; } /*-------------------------------------------------------------------------------------------*/ /*-------------------------------------------------------------------------------------------*/ double neglogLikelihood(int noDates, int *dates, int intervention_date[], int *pfail, workparam Pi) { int i,j,jmin, v; int timediffij; double lnL, Lsum; *pfail=0; //Calculate probability matrix: for(i=0;iintervention_date[v];v++) ; for(j=0;j0 /*&& timediffij<=30*/ && i!=j){ //i can infect j if i was earlier, //but less than 30 days before, //and i cannot infect itself. matrix[i][j] = PDF(timediffij, Pi.index.named.kappa[v], Pi.index.named.eta[v]); } else { matrix[i][j]=0; } } } jmin=1; while(dates[jmin]==dates[jmin-1]) jmin++; lnL=0.0; for(j=jmin;j0) { if(!(SamplePoints=dmatrix(0,DoFit+1,0,ndim+1))) ERR_CRITICAL("MLE: Unable to allocate memory for SamplePoints.\n"); ConstructLHTable(DoFit, ndim, SamplePoints); loop=DoFit; for(i=0;i0)&&(jindex.array[j].Scan==1) { P[0].index.array[k] = GP->index.array[j].Min + SamplePoints[i][k] * (GP->index.array[j].Max-GP->index.array[j].Min); for(l=1;lindex.array[j].Scale; k++; } else if(GP->index.array[j].Scan==2) { if(GP->index.array[j].Max<0.0) { P[0].index.array[k] = -exp(log(-GP->index.array[j].Min) + (log(-GP->index.array[j].Max)-log(GP->index.array[i].Min)) *SamplePoints[i][k]); for(l=1;lindex.array[j].Scale; } else { P[0].index.array[k] = exp(log(GP->index.array[j].Min) + (log(GP->index.array[j].Max)-log(GP->index.array[j].Min)) *SamplePoints[i][k]); for(l=1;lindex.array[j].Scale; } k++; } } FindMin(i, ndim, noDates, dates, intervention_date, GP, P, L, Pres, Lres, itres); } OutputRes(InputFile, ndim, DoFit, Pres, Lres, itres); //write the MLestimates into GP: for(k=0, j=0;jindex.array[k].Scan>0) { GP->index.array[k].Value=Pres[0].index.array[j]; j++; } } bestL=Lres[0]; printf("ML estimation completed.\n"); free_dmatrix(SamplePoints, 0, DoFit+1, 0, ndim+1); free(Pres); free(Lres); free(itres); } else if (DoFit==0) { for(k=0, j=0;jindex.array[j].Scan>0) { P[0].index.array[k] = GP->index.array[j].Value; for(l=1;lindex.array[j].Scale; k++; } } FindMin(0, ndim, noDates, dates, intervention_date, GP, P, L, Pres, Lres, itres); //write the MLestimates into GP: for(k=0, j=0;jindex.array[k].Scan>0) { GP->index.array[k].Value=Pres[0].index.array[j]; j++; } } bestL=Lres[0]; } free(P); free(L); return bestL; } /*-------------------------------------------------------------------------------------------*/ void ConstructLHTable(int num, int paramnum, double **SamplePoints) { int i,j,k,l; double r, f,ns,s; static FILE *dat; ns=(double) num; s=1.0/ns; for(i=0;iindex.array[k-1].Scale; } } amoeba(noDates, dates, intervention_date, P, L, TINY, ndim, &neglogLikelihood, &nfunk); it+=nfunk; } best=0; for(j=1;jL[1]) { sworst=1; worst=0; } else { sworst=0; worst=1; } for(i=0;iL[worst]) { sworst=worst; worst=i; } else if (L[i]>L[sworst] && i!=worst) sworst=i; } rtol=2.0*fabs(L[best]-L[worst])/(fabs(L[worst])+fabs(L[best])+TINY); //Compute the fractional range from worst to best and return if satisfactory. if(rtol=NMAX) { fprintf(stderr, "amoeba: NMAX exceeded\n"); return 1; } *nfunk+=2; //Begin a new iteration. First extrapolate by a factor -1 through the face of the //simplex across from the high point, i.e., reflect the simplex from the hight point. Ltry=amotry(noDates, dates, intervention_date, P, L, psum, ndim, funk, worst, 2.0); if(Ltry <=L[best]) //Gives a result better than the best point, //so try an additional extrapolation by a factor 2. Ltry=amotry(noDates, dates, intervention_date, P, L, psum, ndim, funk, worst, 2.0); else if (Ltry>=L[sworst]) { //The reflecte point is worse than the second worst, //so look for an intermediate better point, i.e., do a one-dimensional contraction. Lsave=L[worst]; Ltry=amotry(noDates, dates, intervention_date, P, L, psum, ndim, funk, worst, 0.5); if(Ltry>=Lsave) { //Can't seem to get rid of that bad point. Better for(i=0;iindex.array[l].Scan>0) { P.index.array[m]=GP->index.array[l].Value; for(i=0;iindex.array[k].Scan) { case 1: j++; xmin=GP->index.array[k].Min; xmax=GP->index.array[k].Max; dx=(xmax-xmin)/(NO_X-1); for(i=0;iindex.array[k].Min; xmax=GP->index.array[k].Max; dx=pow(xmax/xmin, 1.0/(NO_X-1)); for(i=0;iindex.named.kappa[i].Value); for(i=0;iindex.named.eta[i].Value); fprintf(out, "\n"); for(i=0;iindex.array[m].Scan==1 || GP->index.array[m].Scan==2) { GP->index.array[m].Value=Pbest.index.array[l]; l++; } } fail=1; /*sprintf(filename, "%s.paramest.txt", InputFile); if(!(out=fopen(filename, "a"))) ERR_CRITICAL("CalcCIs: Unable to open .paramest file for append.\n"); fprintf(out, "-1\t-1\t%lg", Lbest); for(l=0;l