double MLFitterGSL::iteration(){ //do one itteration //lock the model so user can not add or remove components during itteration modelptr->setlocked(true); //if the model has changed (a new or removed component eg.) create a new solver if (modelptr->has_changed()){ createmodelinfo(); //this automaticaly calls initfitter() via created_modelinfo() } //do an itteration step try{ const int status=gsl_multifit_fdfsolver_iterate(solver); #ifdef FITTER_DEBUG std::cout <<"solver status: "<<gsl_strerror(status)<<"\n"; std::cout <<"chi square/dof: "<<gsl_blas_dnrm2(solver->f)<<"\n"; #endif this->setstatus(gsl_strerror(status)); //update status info of fitter convertxtoparam(solver->x); } catch(...){ Saysomething mysay(0,"Error","Problem with call to gsl_multifit_fdfsolver_iterate, exit MLFitterGSL",true); delete(this); } //unlock the model so user can add or remove components modelptr->setlocked(false); //put the goodness in the goodnessqueue to check for convergence const double newgoodness= goodness_of_fit(); addgoodness(newgoodness); return newgoodness; }
std::string WLSQFitter::goodness_of_fit_string()const{ //returns a string which says how good the fit is char s[256]; double chisq=goodness_of_fit(); sprintf(s,"Normalized Weighted Chi square: %e",chisq/(modelptr->getnpoints())); std::string f=s; return f; }
void fit_halo_profile(struct halo *HALO) { double c=0, r=0, rho0, rho0_halo, rs, chi, gof, per; double *x, *y, *e, *R, *y_th, *x_bin, *y_bin, *e_bin, rMin, rMax; int bins, skip, N, j=0; r = HALO->Rvir; c = HALO->c; bins = HALO->n_bins; skip = HALO->neg_r_bins; N = bins - skip; rho0 = HALO->rho0; rs = HALO->r2; x = (double*) calloc(N, sizeof(double)); y = (double*) calloc(N, sizeof(double)); e = (double*) calloc(N, sizeof(double)); R = (double*) calloc(N, sizeof(double)); for(j=0; j<N; j++) { x[j] = HALO->radius[j+skip]; y[j] = HALO->rho[j+skip]; e[j] = HALO->err[j+skip]; R[j] = HALO->radius[j+skip]/r; } best_fit_nfw(rho0, rs, N, x, y, e); HALO->fit_nfw.rho0 = rho0; HALO->fit_nfw.rs = rs; HALO->fit_nfw.c = HALO->Rvir/rs; y_th = (double*) calloc(bins-skip,sizeof(double)); for(j=skip; j<bins; j++) { y_th[j-skip] = nfw(HALO->radius[j], HALO->fit_nfw.rs, HALO->fit_nfw.rho0); //fprintf(stderr, "%d) R=%e, %e %e %e\n", j, R[j-skip], rho0, y[j-skip], y_th[j-skip]); } // Various estimators for the goodness of fit chi = chi_square(y_th, y, e, N); gof = goodness_of_fit(y_th, y, N); per = percentage_error(y_th, y, N); chi /= (double) (bins-skip); HALO->fit_nfw.chi = chi; HALO->fit_nfw.gof = gof; HALO->fit_nfw.per = per; x_bin = (double*) calloc(BIN_PROFILE+1, sizeof(double)); y_bin = (double*) calloc(BIN_PROFILE, sizeof(double)); e_bin = (double*) calloc(BIN_PROFILE, sizeof(double)); rMin = 2 * Rvir_frac_min; rMax = F_MAX * 1.01; //HALO->radius[bins-1]/r; x_bin = log_stepper(rMin, rMax, BIN_PROFILE+1); average_bin(R, y, x_bin, y_bin, e_bin, BIN_PROFILE+1, N); for(j=0; j<BIN_PROFILE; j++) { HALO->nfw.x[j] = 0.5 * (x_bin[j] + x_bin[j+1]); HALO->nfw.y[j] = y_bin[j]; //fprintf(stderr, "%d %e %e\n", j, x_bin[j+1], y_bin[j]); } free(x); free(y); free(R); free(y_th); free(e); // fprintf(stderr, "ThisTask=%d, skip=%d, bins=%d, rho=%f, rs=%f, ChiSquare=%lf, Red=%lf\n", // ThisTask, skip, bins, rho0, rs, chi_sq, chi_sq/(bins-skip)); }