def _set_results(self, phi, value, error): fit_parameters = np.zeros(self.model.n_pars) fit_errors = np.zeros(self.model.n_pars) for ipar in range(self.model.n_pars): fit_parameters[ipar] = np.average(self.samples[self.burn_in:,ipar]) fit_errors[ipar] = np.std(self.samples[self.burn_in:,ipar]) loss_value = loss.chi2(value, self.model.update_and_evaluate(phi, fit_parameters), error) pred = self.model.update_and_evaluate(phi, fit_parameters) ndf = len(phi) quality = 1-chi2pdf.cdf(loss.chi2(value, pred, error), ndf) return FitResult(fit_parameters=fit_parameters, fit_errors=fit_errors, loss=loss_value, quality=quality, ndf=ndf, samples=self.samples[self.burn_in:,:])
def fit(self, phi, value, error, x0=None): func = lambda p: self.loss_function(value, self.model.update_and_evaluate(phi, p), error) + self.penalty * np.sum(p**2) if x0 is not None: self.model.pars = x0 else: if self.bounds is None: self.model.initialize_parameters() else: self.model.pars = utils.random_search(func, self.bounds, 1000) bad_fit = True while bad_fit: if self.bounds is not None: result = minimize(func, x0=self.model.pars, bounds=self.bounds) identity = np.identity(self.model.n_pars) err = np.sqrt(np.abs(np.array(np.matrix(result.hess_inv * identity).diagonal()))) err = err[0] else: result = minimize(func, x0=self.model.pars) err = np.sqrt(result.hess_inv.diagonal()) bad_fit = not result.success fit_parameters = result.x fit_errors = err loss_value = self.loss_function(value, self.model.update_and_evaluate(phi, fit_parameters), error) pred = self.model.update_and_evaluate(phi, fit_parameters) ndf = len(phi) quality = 1-chi2pdf.cdf(loss.chi2(value, pred, error), ndf) return FitResult(fit_parameters=fit_parameters, fit_errors=fit_errors, loss=loss_value, quality=quality, ndf=ndf)
def _set_result(self, result, phi, value, error): z = result[0].mean quality = result.Q fit_parameters = np.array([result[1].mean/z, result[2].mean/z, result[3].mean/z]) fit_errors = np.array([result[4].mean/z - fit_parameters[0]**2, result[5].mean/z - fit_parameters[1]**2, result[6].mean/z - fit_parameters[2]**2]) fit_errors = np.sqrt(fit_errors) loss_value = loss.chi2(value, self.model.update_and_evaluate(phi, fit_parameters), error) return FitResult(fit_parameters=fit_parameters, fit_errors=fit_errors, loss=loss_value, quality=quality, z=z)
def _set_results(self, phi, value, error): self.fit_container = np.array(self.fit_container, dtype=np.float32) fit_parameters = np.zeros(self.model.n_pars) fit_errors = np.zeros(self.model.n_pars) for ipar in range(self.model.n_pars): fit_parameters[ipar] = np.average(self.fit_container[:,ipar]) fit_errors[ipar] = np.std(self.fit_container[:,ipar]) loss_value = self.loss_function(value, self.model.update_and_evaluate(phi, fit_parameters), error) pred = self.model.update_and_evaluate(phi, fit_parameters) ndf = len(phi) quality = 1-chi2pdf.cdf(loss.chi2(value, pred, error), ndf) return FitResult(fit_parameters=fit_parameters, fit_errors=fit_errors, loss=loss_value, quality=quality, ndf=ndf, replicas=self.fit_container)