int main(void) { int i,iflag,j,nmin=0; float ax,bx,cx,fa,fb,fc,xmin,dbr,amin[21]; printf("\nMinima of the function bessj0\n"); printf("%10s %8s %16s %12s %11s\n", "min. #","x","bessj0(x)","bessj1(x)","DBRENT"); for (i=1;i<=100;i++) { ax=i; bx=i+1.0; mnbrak(&ax,&bx,&cx,&fa,&fb,&fc,func); dbr=dbrent(ax,bx,cx,func,dfunc,TOL,&xmin); if (nmin == 0) { amin[1]=xmin; nmin=1; printf("%7d %15.6f %12.6f %12.6f %12.6f\n", nmin,xmin,func(xmin),dfunc(xmin),dbr); } else { iflag=0; for (j=1;j<=nmin;j++) if (fabs(xmin-amin[j]) <= EQL*xmin) iflag=1; if (iflag == 0) { amin[++nmin]=xmin; printf("%7d %15.6f %12.6f %12.6f %12.6f\n", nmin,xmin,func(xmin),dfunc(xmin),dbr); } } } return 0; }
void dlinmin(float p[], float xi[], int n, float *fret, float (*func)(float []), void (*dfunc)(float [], float [])) { float dbrent(float ax, float bx, float cx, float (*f)(float), float (*df)(float), float tol, float *xmin); float f1dim(float x); float df1dim(float x); void mnbrak(float *ax, float *bx, float *cx, float *fa, float *fb, float *fc, float (*func)(float)); int j; float xx,xmin,fx,fb,fa,bx,ax; ncom=n; pcom=vector(1,n); xicom=vector(1,n); nrfunc=func; nrdfun=dfunc; for (j=1;j<=n;j++) { pcom[j]=p[j]; xicom[j]=xi[j]; } ax=0.0; xx=1.0; mnbrak(&ax,&xx,&bx,&fa,&fx,&fb,f1dim); *fret=dbrent(ax,xx,bx,f1dim,df1dim,TOL,&xmin); for (j=1;j<=n;j++) { xi[j] *= xmin; p[j] += xi[j]; } free_vector(xicom,1,n); free_vector(pcom,1,n); }
void linmin(double p[], double xi[], int n, double *fret, double (*func)(double [])) { double brent(double ax, double bx, double cx, double (*f)(double), double tol, double *xmin); double f1dim(double x); void mnbrak(double *ax, double *bx, double *cx, double *fa, double *fb, double *fc, double (*func)(double)); int j; double xx,xmin,fx,fb,fa,bx,ax; ncom=n; pcom=dvector(1,n); xicom=dvector(1,n); nrfunc=func; for (j=1;j<=n;j++) { pcom[j]=p[j]; xicom[j]=xi[j]; } ax=0.0; xx=1.0; mnbrak(&ax,&xx,&bx,&fa,&fx,&fb,f1dim); *fret=brent(ax,xx,bx,f1dim,TOL,&xmin); for (j=1;j<=n;j++) { xi[j] *= xmin; p[j] += xi[j]; } free_dvector(xicom,1,n); free_dvector(pcom,1,n); }
void ML_multi_Powell::linmin(v_ratep_type& p, v_ratep_type& xi, double& fret, ptr_eval_func func ) { int j; const double TOL=1.0e-8; double xx, xmin, fx, fb, fa, bx, ax; int n = p.size(); ncom = n; pcom.resize(n); xicom.resize(n); nrfunc = func; for(j = 0; j < n; ++j) { pcom[j] = p[j]; xicom[j] = xi[j]; } ax = 0.0; xx = 1.0; mnbrak(ax, xx, bx, fa, fx, fb, &ML_multi_Powell::f1dim); fret = brent(ax, xx, bx, &ML_multi_Powell::f1dim, TOL, xmin); for (j = 0; j < n; ++j) { xi[j] *= xmin; p[j] += xi[j]; } }
void linmin( vectorN &p, vectorN &xi, int n, m_real &fret, m_real (*func) (vectorN const&)) { m_real xx, xmin, fx, fb, fa, bx, ax; ncom = n; pcom.setSize(n); xicom.setSize(n); nrfunc = func; for (int j=0; j<n; j++) { pcom[j] = p[j]; xicom[j] = xi[j]; } ax = 0.0; xx = 1.0; mnbrak(ax, xx, bx, fa, fx, fb, f1dim); fret = brent(ax, xx, bx, f1dim, TOL, xmin); for (int j=0; j<n; j++) { xi[j] *= xmin; p[j] += xi[j]; } }
void linmin(float p[], float xi[], int n, float *fret, float (*func)(float [])) { float brent(float ax, float bx, float cx, float (*f)(float), float tol, float *xmin); float f1dim(float x); void mnbrak(float *ax, float *bx, float *cx, float *fa, float *fb, float *fc, float (*func)(float)); int j; float xx,xmin,fx,fb,fa,bx,ax; if (DEBUGGING_NR) { fprintf(stderr,"Inside linmin with n=%d\n",n); } ncom=n; if (DEBUGGING_NR) { fprintf(stderr,"Calling vector(1,%d)\n",n); } pcom=vector(1,n); if (DEBUGGING_NR) { fprintf(stderr,"done vector(1,%d)\n",n); } xicom=vector(1,n); if (DEBUGGING_NR && 0) { fprintf(stderr,"done vector(1,%d)\n",n); } nrfunc=func; for (j=1;j<=n;j++) { pcom[j]=p[j]; xicom[j]=xi[j]; } ax=0.0; xx=1.0; if (DEBUGGING_NR && 0) { fprintf(stderr,"calling mnbrak\n"); } mnbrak(&ax,&xx,&bx,&fa,&fx,&fb,f1dim); if (DEBUGGING_NR && 0) { fprintf(stderr,"returned from mnbrak\n"); } *fret=brent(ax,xx,bx,f1dim,TOL,&xmin); if (DEBUGGING_NR) { fprintf(stderr,"returned from brent\n"); } for (j=1;j<=n;j++) { xi[j] *= xmin; p[j] += xi[j]; } free_vector(xicom,1,n); free_vector(pcom,1,n); }
void linmin(pT& p,pT& xi,rT& fret,func_obj<rT,pT>& func) { // assert(p.size()==10); //assert(xi.size()==10); func_adaptor<rT,pT> fadpt(p,xi,func); int j=0; const rT TOL=std::sqrt(std::numeric_limits<rT>::epsilon()); rT xx=0,xmin=0,fx=0,fb=0,fa=0,bx=0,ax=0; int n=(int)get_size(p); ax=0.; xx=1.; mnbrak(ax,xx,bx,fa,fx,fb,fadpt); //cout<<xx<<endl; fret=brent(ax,xx,bx,fadpt,TOL,xmin); //cout<<xmin<<endl; #ifdef _OPENMP #pragma omp parallel for #endif for(j=0;j<n;++j) { //get_element(xi,j)*=xmin; set_element(xi,j, get_element(xi,j)*xmin); //get_element(p,j)+=get_element(xi,j); set_element(p,j, get_element(p,j)+get_element(xi,j)); } // delete xicom_p; //delete pcom_p; }
main ( int argc, char *argv[] ){ int i, j, k, l, iter, internal_index; double fret; double *p, *del, *p_old, abs_dev, Z_old, Z_diff; char *in_data, *in_model, *out_model; FILE *fp_in_data, *fp_in_model, *fp_out_model, *fp_evolution; char evolution_file_name[100] ; double *mu_vec, *grad_mu; double mu_x, mu_y; double FTOL; double mu_scalar; double ax, bx, xx, fa, fx, fb, xmin ; /* output version number and compilation parameters */ printf("VERA_OC version: %s\n", __DATE__); #ifdef USERHOD printf("rho_delta may be non-zero, "); #else printf("rho_delta forced to zero, "); #endif #ifdef MUNONNEG printf("non-negative mus only.\n "); #else printf("mus may be negative.\n "); #endif /* initialize command line variables */ verbose = FALSE; disp_iter = FALSE; start_mod = FALSE; evolution = FALSE; stop_crit = FALSE; in_data = NULL; in_model = NULL; out_model = NULL; start_time(); /* start timer */ /* parse command line */ for (i=1; i<argc; i++) { if (strcmp(argv[i], "-verbose") == 0) verbose = TRUE ; else if (strcmp(argv[i], "-iter") == 0) disp_iter = TRUE; else if (strcmp(argv[i], "-init") == 0) start_mod = TRUE; else if (strcmp(argv[i], "-evol") == 0) evolution = TRUE; else if (in_model == NULL && start_mod == TRUE ) in_model = argv[i]; else if (strcmp(argv[i], "-crit") == 0) stop_crit = TRUE; else if (stop_crit == TRUE ){ EPS_BETA = atof(argv[i]); stop_crit = FALSE; } else if (in_data == NULL) in_data = argv[i]; else if (out_model == NULL) out_model = argv[i]; else die (1) ; } if (in_data == NULL || (in_model == NULL && start_mod == TRUE) || out_model == NULL ) die(1); fp_in_data = fopen(in_data, "r"); if (fp_in_data == NULL) die (2); if ( start_mod == TRUE ){ fp_in_model = fopen(in_model, "r"); if (fp_in_model == NULL) die (2); } fp_out_model = fopen(out_model, "w"); if (fp_out_model == NULL) die (2); if ( evolution == TRUE ){ strcpy ( evolution_file_name, in_data ); strcat ( evolution_file_name, ".evolution" ); fp_evolution = fopen( evolution_file_name, "w" ); fprintf(fp_evolution, "iter sigma_eps_x sigma_del_x objective function\n"); fflush( fp_evolution ); } /* read in the data */ printf ("Reading expression data (merge) file %s\n", in_data ); read_data_OC( fp_in_data ); /* allocate memory for mu variables and set them to zero */ muX = (float *) calloc(N, sizeof(float)); for (i=0 ; i<N ; i++ ){ muX[i] = 0.; } /* read in the model */ cov = (double *) calloc(NDIM+1, sizeof(double)); if ( start_mod == TRUE ) { printf ("Reading input model (initial guess) file %s\n", in_model ); read_mod( fp_in_model ); } else { /* if model is not read in use starting guess */ cov[1] = 0.3 ; cov[2] = 20 ; /* cov[2] = 10 ; */ } /* If mu values were not read in by read_mod, assign mu to means */ if( muX[0] == 0. && muX[N-1] == 0. ){ /* this criterion could be improved e.g. use sum over all entries */ for(i=0 ; i<N ; i++ ){ for (j=0; j<m[i] ; j++ ){ muX[i] += X[i][j] ; } muX[i] /= (float)(m[i]); } } /****************************************************************************/ /********* Memory allocation, Initialization ******/ /****************************************************************************/ /* beta vector and gradient indexed 1,..,6 for NumRec routines*/ p = (double *) calloc(NDIM+1, sizeof(double)); del = (double *) calloc(NDIM+1 ,sizeof(double)); p_old = (double *) calloc(NDIM+1, sizeof(double)); /* parameters of covariance matrix and vectors for single gene objective function */ grad_mu = (double *) calloc(NDIM_MU+1, sizeof(double)); for( i=1 ; i<= NDIM ; i++ ){ p[i] = cov[i]; } if ( evolution == TRUE ){ /* print initial parameter evolution file */ fprintf ( fp_evolution, "%4d ", 0); for ( j=1 ; j<= NDIM; j++ ){ fprintf ( fp_evolution," %9.4f ", p[j]); fflush( fp_evolution ); } } /*****************************************************************************/ /** Master loop begins here ******/ /*****************************************************************************/ if ( disp_iter == FALSE ) printf("Iterations in progress: "); fflush(stdout); for (k=0 ; k<MAX_MASTER ; k++){ /* master loop */ /* display . for iteration */ if ( disp_iter == FALSE ){ if ( (k+1) % 5 ){ printf("."); fflush(stdout); } else { printf("%d" , k+1 ); fflush(stdout); } } if ( disp_iter == TRUE ) printf("\n\n ********** Starting master loop iteration %d ************** \n", k); /****************************************************************************/ /********* Optimize beta ******/ /****************************************************************************/ for( i=0 ; i<N ; i++ ){ for (j=0; j<m[i] ; j++ ){ X[i][j] -= muX[i] ; } } /* save current values of beta to later assess progress of iteration */ for( i=1 ; i<= NDIM ; i++ ){ p_old[i] = p[i]; } Z_old = Z_OC(p); if ( disp_iter == TRUE ) printf("Objective function at beginning of master it. no. %d is %14f\n", k, Z_old); /* log10(tolerance) drops with each master iteration */ LINMIN_TOL = pow(10., log10(LINMIN_TOL_HIGH) + k* (log10(LINMIN_TOL_LOW)-log10(LINMIN_TOL_HIGH)) / (double) MAX_MASTER ) ; FTOL = pow(10., log10(FTOL_HIGH) + k* (log10(FTOL_LOW)-log10(FTOL_HIGH)) / (double) MAX_MASTER ); if ( disp_iter == TRUE ) printf("\nlog10(LINMIN_TOL): %f, log10(FTOL): %f\n\n", log10(LINMIN_TOL), log10(FTOL)); /* shift intensities by mus for this evaluation below */ /* for( i=0 ; i<N ; i++ ){ for (j=0; j<m[i] ; j++ ){ X[i][j] -= muX[i] ; } } */ if ( disp_iter == TRUE ) printf("Objective function at beginning of master it. no. %d is %14f\n", k, Z_OC(p)); if ( evolution == TRUE ) fprintf( fp_evolution, "%18.3f\n", Z_OC(p) ); if ( disp_iter == TRUE ) printf("\n+++Starting epsilon+delta optimization++++++\n"); LINMIN_TOL = LINMIN_TOL_LOW; frprmn(p, NDIM, FTOL_LOW, &iter, &fret, Z_OC, gradZ_OC); if (verbose == TRUE){ printf("Iterations: %3d\n",iter); printf("frprmn solution vector: ("); for (j=1 ;j<=NDIM ; j++) printf("%6.4f,", p[j]); printf(")\n"); printf("Func. value at solution %14f\n",fret); } /* print result of beta optimization */ if ( disp_iter == TRUE ){ printf( " Beta: "); for (j=1 ;j<=NDIM ; j++) printf("%6.4f,", p[j]); printf("\n"); printf( " Value of objective function: %14f\n", fret); } /* copy covariance into global location */ for (i=1 ; i<=NDIM ; i++ ) cov[i] = p[i]; /****************************************************************************/ /********* Optimize mus ******/ /****************************************************************************/ if ( disp_iter == TRUE ) printf("\n+++Starting mu optimization++++++\n"); /* Need unshifted intensities below */ for( i=0 ; i<N ; i++ ){ for (j=0; j<m[i] ; j++ ){ X[i][j] += muX[i] ; } } for(i=0 ; i<N; i++ ){ /* loop over genes */ if ( disp_iter == TRUE ) printf("mu optimization for gene %d begins \n", internal2full[i]); mu_x = (double) muX[i] ; gene_index = i; /* seed unconstrained with result from above */ mu_scalar = mu_x; /********* Will have to change to single variable minimization ****/ if ( disp_iter == TRUE ) printf("mu_scalar before minimization %f\n", mu_scalar ); /*frprmn(mu_scalar, NDIM_MU, FTOL, &iter, &fret, Z_gene_OC, derivativeZ_gene_OC); */ ax = mu_x; xx = mu_x+ 5.; /* who knows? */ mnbrak(&ax,&xx,&bx,&fa,&fx,&fb,Z_gene_OC); brent(ax,xx,bx,Z_gene_OC,LINMIN_TOL,&xmin); mu_scalar=xmin; if ( disp_iter == TRUE ){ printf("mu_scalar after minimization %f\n", mu_scalar ); printf("mu derivative: %f\n", derivativeZ_gene_OC(mu_scalar) ); } #ifdef MUNONNEG mu_scalar = fabs( mu_scalar ); #endif /* replace revised muX */ muX[i] = mu_scalar; } /* mu optimization ends here. X is not mu subtracted at this point*/ /* correct for instances of finding negative sigma */ if ( p[1] < 0 ){ p[1] = -p[1]; } if ( p[2] < 0 ){ p[2] = -p[2]; } /* summarize result of beta optimization */ if ( disp_iter == TRUE ){ printf( " Beta after master loop iteration %d : ",k); for (j=1 ;j<=NDIM ; j++) printf(" %6.4f ", p[j]); printf( "\n"); } if ( evolution == TRUE ){ /* print results of master loop iteration in parameter evolution file */ fprintf ( fp_evolution, "%4d ", k+1); for ( j=1 ; j<= NDIM; j++ ){ fprintf ( fp_evolution," %9.4f ", p[j]); fflush( fp_evolution ); } } /* exit master loop if maximum absolute increment is below threshold */ abs_dev = 0.; for ( j=1 ; j<=NDIM ; j++ ){ if ( fabs(p[j]-p_old[j]) > abs_dev ) abs_dev = fabs(p[j]-p_old[j]); } /* shift for evaluation */ for( i=0 ; i<N ; i++ ){ for (j=0; j<m[i] ; j++ ){ X[i][j] -= muX[i] ; } } Z_diff = fabs(Z_OC(p)-Z_old); if ( disp_iter == TRUE ) printf("Objective function at end of master it. no. %d is %14f\n", k, Z_OC(p)); /* unshift */ for( i=0 ; i<N ; i++ ){ for (j=0; j<m[i] ; j++ ){ X[i][j] += muX[i] ; } } if ( Z_diff > abs_dev ) abs_dev = Z_diff ; if ( abs_dev < EPS_BETA && k > MIN_MASTER ) break; } /* close master loop */ /* shift intensities by mus for this evaluation below */ for( i=0 ; i<N ; i++ ){ for (j=0; j<m[i] ; j++ ){ X[i][j] -= muX[i] ; } } printf("\nOptimization completed in %d iterations.\n", k+1); if( k == MAX_MASTER ) printf("Warning: This is the maximum of number of allowed iterations\n"); printf( "Final objective value %f\n\n", Z_OC(cov)); if ( evolution == TRUE ) fprintf( fp_evolution, "%18.3f\n", Z_OC(cov)); /****************************************************************************/ /********* Print out final model ******/ /****************************************************************************/ fprintf(fp_out_model,"sig_eps_x sig_del_x\n"); fprintf(fp_out_model," %8.5f %8.5f", p[1],p[2]); fprintf(fp_out_model,"\n"); fclose(fp_in_data); if ( start_mod == TRUE ){ fclose(fp_in_model); } if ( evolution == TRUE ) fclose( fp_evolution ); stdout_prn_time(); /* display elapsed time */ return (0); }
void fitexy(float x[], float y[], int ndat, float sigx[], float sigy[], float *a, float *b, float *siga, float *sigb, float *chi2, float *q) { void avevar(float data[], unsigned long n, float *ave, float *var); float brent(float ax, float bx, float cx, float (*f)(float), float tol, float *xmin); float chixy(float bang); void fit(float x[], float y[], int ndata, float sig[], int mwt, float *a, float *b, float *siga, float *sigb, float *chi2, float *q); float gammq(float a, float x); void mnbrak(float *ax, float *bx, float *cx, float *fa, float *fb, float *fc, float (*func)(float)); float zbrent(float (*func)(float), float x1, float x2, float tol); int j; float swap,amx,amn,varx,vary,ang[7],ch[7],scale,bmn,bmx,d1,d2,r2, dum1,dum2,dum3,dum4,dum5; xx=vector(1,ndat); yy=vector(1,ndat); sx=vector(1,ndat); sy=vector(1,ndat); ww=vector(1,ndat); avevar(x,ndat,&dum1,&varx); avevar(y,ndat,&dum1,&vary); scale=sqrt(varx/vary); nn=ndat; for (j=1;j<=ndat;j++) { xx[j]=x[j]; yy[j]=y[j]*scale; sx[j]=sigx[j]; sy[j]=sigy[j]*scale; ww[j]=sqrt(SQR(sx[j])+SQR(sy[j])); } fit(xx,yy,nn,ww,1,&dum1,b,&dum2,&dum3,&dum4,&dum5); offs=ang[1]=0.0; ang[2]=atan(*b); ang[4]=0.0; ang[5]=ang[2]; ang[6]=POTN; for (j=4;j<=6;j++) ch[j]=chixy(ang[j]); mnbrak(&ang[1],&ang[2],&ang[3],&ch[1],&ch[2],&ch[3],chixy); *chi2=brent(ang[1],ang[2],ang[3],chixy,ACC,b); *chi2=chixy(*b); *a=aa; *q=gammq(0.5*(nn-2),*chi2*0.5); for (r2=0.0,j=1;j<=nn;j++) r2 += ww[j]; r2=1.0/r2; bmx=BIG; bmn=BIG; offs=(*chi2)+1.0; for (j=1;j<=6;j++) { if (ch[j] > offs) { d1=fabs(ang[j]-(*b)); while (d1 >= PI) d1 -= PI; d2=PI-d1; if (ang[j] < *b) { swap=d1; d1=d2; d2=swap; } if (d1 < bmx) bmx=d1; if (d2 < bmn) bmn=d2; } } if (bmx < BIG) { bmx=zbrent(chixy,*b,*b+bmx,ACC)-(*b); amx=aa-(*a); bmn=zbrent(chixy,*b,*b-bmn,ACC)-(*b); amn=aa-(*a); *sigb=sqrt(0.5*(bmx*bmx+bmn*bmn))/(scale*SQR(cos(*b))); *siga=sqrt(0.5*(amx*amx+amn*amn)+r2)/scale; } else (*sigb)=(*siga)=BIG; *a /= scale; *b=tan(*b)/scale; free_vector(ww,1,ndat); free_vector(sy,1,ndat); free_vector(sx,1,ndat); free_vector(yy,1,ndat); free_vector(xx,1,ndat); }