def go(self, guess): """ Run leastsq and set the result """ guess = np.array(guess, dtype='f8', copy=False) nobj = guess.size // self.npars_per nleft = guess.size % self.npars_per if nobj != self.nobj or nleft != 0: raise ValueError("bad guess size: %d" % guess.size) bounds = self._get_bounds(nobj) result = run_leastsq(self._calc_fdiff, guess, self.n_prior_pars, k_space=True, bounds=bounds, **self.lm_pars) result['model'] = self.model if result['flags'] == 0: stat_dict = self.get_fit_stats(result['pars']) result.update(stat_dict) self._result = result
def fit_exp_binned(x, y, yerr, guess): # assume quadratic def loss(pars): model = exp_func(pars, x) return (model - y) / yerr return run_leastsq( loss, np.array(guess), 0, )
def fit_exp(x, y, guess): def loss(pars): model = exp_func(pars, x) return (model - y) return run_leastsq( loss, np.array(guess), 0, maxfev=4000, xtol=1.0e-5, ftol=1.0e-5, )
def fit_erf(x, y, guess, type='falling'): def func(pars): return erf_func(pars, x, type) def loss(pars): model = func(pars) return (model - y) return run_leastsq( loss, np.array(guess), 0, )
def go(self, ntry=100): ngauss = self.ngauss # ysum = self.y.sum() npars_per = 3 for i in range(ntry): self.guess = np.zeros(npars_per * ngauss) for ip, prior in enumerate(self.priors): self.guess[ip] = prior.sample(sigma_factor=1) # self.guess[ip] = prior.sample(sigma_factor=0.1) print_pars(self.guess, front='guess: ') self.result = run_leastsq( self._errfunc, self.guess, self.n_prior_pars, bounds=self.bounds, maxfev=4000, ) if self.result['flags'] == 0: break