void NMfit::autoiter() { realt convergence = F_->get_settings()->nm_convergence; wssr_before_ = compute_wssr(a_orig_, dmdm_); F_->msg("WSSR before starting simplex fit: " + S(wssr_before_)); for (int iter = 0; !termination_criteria(iter, convergence); ++iter) { ++iter_nr_; change_simplex(); find_best_worst(); iteration_plot(best->a, best->wssr); } post_fit (best->a, best->wssr); }
void LMfit::init() { alpha.resize(na_*na_); alpha_.resize(na_*na_); beta.resize(na_); beta_.resize(na_); lambda = F_->get_settings()->lm_lambda_start; a = a_orig_; if (F_->get_verbosity() >= 1) F_->ui()->mesg(print_matrix (a, 1, na_, "Initial A")); //no need to optimise it (and compute chi2 and derivatives together) chi2 = compute_wssr(a, dmdm_); compute_derivatives(a, dmdm_, alpha, beta); }
bool LMfit::do_iteration() //pre: init() callled { if (na_ < 1) throw ExecuteError("No parameters to fit."); iter_nr_++; alpha_ = alpha; for (int j = 0; j < na_; j++) alpha_[na_ * j + j] *= (1.0 + lambda); beta_ = beta; if (F_->get_verbosity() > 1) { // level: debug F_->ui()->mesg(print_matrix (beta_, 1, na_, "beta")); F_->ui()->mesg(print_matrix (alpha_, na_, na_, "alpha'")); } // Matrix solution (Ax=b) alpha_ * da == beta_ Jordan (alpha_, beta_, na_); // da is in beta_ if (F_->get_verbosity() >= 1) { // level: verbose vector<realt> rel(na_); for (int q = 0; q < na_; q++) rel[q] = beta_[q] / a[q] * 100; if (F_->get_verbosity() >= 1) F_->ui()->mesg(print_matrix (rel, 1, na_, "delta(A)/A[%]")); } for (int i = 0; i < na_; i++) beta_[i] = a[i] + beta_[i]; // and now there is new a[] in beta_[] if (F_->get_verbosity() >= 1) output_tried_parameters(beta_); // compute chi2_ chi2_ = compute_wssr(beta_, dmdm_); if (chi2_ < chi2) { // better fitting chi2 = chi2_; a = beta_; compute_derivatives(a, dmdm_, alpha, beta); lambda /= F_->get_settings()->lm_lambda_down_factor; return true; } else { // worse fitting lambda *= F_->get_settings()->lm_lambda_up_factor; return false; } }
double NLfit::calculate(int n, const double* par, double* grad) { assert(n == na_); vector<realt> A(par, par+n); if (F_->get_verbosity() >= 1) output_tried_parameters(A); bool stop = common_termination_criteria(); if (stop) nlopt_force_stop(opt_); double wssr; if (!grad || stop) wssr = compute_wssr(A, fitted_datas_); else wssr = compute_wssr_gradient(A, fitted_datas_, grad); if (F_->get_verbosity() >= 1) F_->ui()->mesg(iteration_info(wssr)); return wssr; }
void NMfit::compute_v(Vertex& v) { assert (!v.a.empty()); v.wssr = compute_wssr(v.a, dmdm_); v.computed = true; }