//sfs input is here the untransformed, e.g length=dim void bfgs(double *sfs,Matrix<double> *GL1){ double p[dim-1]; for(int i=1;i<dim;i++) p[i-1] = sfs[i]/sfs[0]; //initialize the stuff needed for the bfgs double para[dim-1]; /* parameters */ double min[dim-1]; /* lower bound */ double max[dim-1]; /* upper bound */ int nbd[dim-1]; /* boundary constraint option; nbd[i] = 0 or 1 or 2 or 3; see bfgs.h for details */ int noisy=-1; /* bigger value produces more detailed output */ for (int i=0; i<dim-1; i++){ min[i] = 0.0000001; max[i] = 10.0; nbd[i] = 2; } double fnmax ; if(noGrad==0) fnmax= findmax_bfgs( dim-1, p,NULL, lik1_bfgs, lik1_grad_bfgs_master, min, max, nbd, noisy ); else fnmax= findmax_bfgs( dim-1, p,NULL, lik1_bfgs,NULL, min, max, nbd, noisy ); //tranform back double ts=1; for(int i=0;i<dim-1;i++) ts += p[i]; sfs[0]=1.0/ts; for(int i=0;i<dim-1;i++) sfs[i+1]=p[i]/ts; }
double frequency::likeNoFixedMinor_bfgs(double *loglikes,int numInds,int major){ bfgs_vars bv; bv.loglikes = loglikes; bv.numInds = numInds; bv.major=major; bv.indF=indF; double start[1] = {0.01}; double lbound[1] = {0}; double ubound[1] = {1}; int lims[1] = {2}; double res=findmax_bfgs(1,start,(void*) &bv,likeNoFixedMinor_wrapper,NULL,lbound,ubound,lims,-1); //fprintf(stderr,"%f\n",res); //fprintf(stderr,"%s\t like %f isoptim is: %f\n",__FUNCTION__,res,start[0]); return start[0]; }
double run_optim_full_calcA2(const genome &g,const std::vector<perChr>&pc,para&p){ toOptim2 *to = new toOptim2(g,pc); double tsk[4];//tmparray used of saving stackpointers to->tsk = tsk; double pars[2]; pars[0]=myRand(0,1);pars[1]=myRand(0,1-pars[0]); //fix last pars double lbd[2]={0.000001,0.000001}; double ubd[2]={0.999999,0.999999}; int nbd[2]={2,2}; double opt= -findmax_bfgs(2,pars,(void *)to, bfgs_call_full_calcA2,NULL,lbd, ubd,nbd, -1); p.k0=pars[0]; p.k1=pars[1]; p.k2=1-pars[0]-pars[1]; p.a=calculateA(p.k0,p.k1,p.k2,PHI); return opt; }
double run_optim_k2zero_calcA2(const genome &g,const std::vector<perChr>&pc,para&p){ toOptim2 *to = new toOptim2(g,pc); double tsk[4];//tmparray used of saving stackpointers to->tsk = tsk; double pars[1]={myRand(0,1)}; // fprintf(stderr,"pars=%f\n",pars[0]); double lbd[1]={0.000001}; double ubd[1]={0.999999}; int nbd[1]={2}; double opt= -findmax_bfgs(1,pars,(void *)to, bfgs_call_k2zero_calcA2,NULL,lbd, ubd,nbd, -1); p.k0=pars[0]; p.k1=1-p.k0; p.k2=0; p.a=calculateA(p.k0,p.k1,p.k2,PHI); return opt; }
double frequency::likeFixedMinor_bfgs(double *loglikes,int numInds){ bfgs_vars *bv = new bfgs_vars; bv->loglikes = loglikes; bv->numInds = numInds; bv->indF = indF; double start[1] = {0.01}; double lbound[1] = {0}; double ubound[1] = {1}; int lims[1] = {2}; double res=findmax_bfgs(1,start,bv,&likeFixedMinor_wrapper,NULL,lbound,ubound,lims,-1); //fprintf(stderr,"%s\t like %f isoptim is: %f\n",__FUNCTION__,res,start[0]); delete bv; return start[0]; }
double run_optim_full2(const genome &g,const std::vector<perChr>&pc,para&p){ toOptim2 *to = new toOptim2(g,pc); double tsk[4];//tmparray used of saving stackpointers to->tsk = tsk; double pars[3];pars[0]=myRand(alim[0],alim[1]); pars[1]=myRand(0,1);pars[2]=myRand(0,1-pars[1]); //double pars[3];pars[0]=myRand(alim[0],alim[1]); pars[1]=myRand(0,1);pars[2]=1-pars[1];pars[2]=0; // double pars[3]={0.045854, 0.823746,0.176254}; //fix last pars double lbd[3]={alim[0],0.000001,0.000001}; double ubd[3]={alim[1],0.999999,0.999999}; int nbd[3]={2,2,2}; double opt= -findmax_bfgs(3,pars,(void *)to, bfgs_call_full2,NULL,lbd, ubd,nbd, -1); p.a=pars[0]; p.k0=pars[1]; p.k1=pars[2]; p.k2=1-pars[1]-pars[2]; return opt; }