def calc_stat_info(self): dep, staterror, syserror = self.data.to_fit(self.stat.calc_staterror) model = self.data.eval_model_to_fit(self.model) stat, fvec = self.stat.calc_stat(dep, model, staterror, syserror) qval = None rstat = None numpoints = len(model) dof = numpoints - len(self.model.thawedpars) if (isinstance(self.stat, (CStat,Chi2)) and not isinstance(self.stat, LeastSq)): if stat >= 0.0: qval = igamc(dof/2., stat/2.) rstat = stat/dof name = self.stat.name if isinstance(self.stat, Chi2) and not isinstance(self.stat, LeastSq): isSimulFit = isinstance(self.data, DataSimulFit) if isSimulFit: is_error_set = [d.staterror is not None for d in self.data.datasets] if all(is_error_set): name = 'chi2' elif self.data.staterror is not None: name = 'chi2' return StatInfoResults(name, stat, numpoints, model, dof, qval, rstat)
def goodness_of_fit(self, statval, dof): """Return the reduced statistic and q value. The reduced statisitc is conceptually simple, as it is just statistic / degrees-of-freedom, but it is not meaningful for all statistics, and it is only valid if there are any degrees of freedom. Parameters ---------- statval : float The statistic value. It is assumed to be finite. dof : int The number of degrees of freedom, which may be 0 or negative. Returns ------- rstat, qval : float or NaN or None, float or NaN or None The reduced statistic and q value. If the statistic does not support a goodness of fit then the return values are None. If it does then NaN is returned if either the number of degrees of freedom is 0 (or less), or the statistic value is less than 0. """ if not self._can_calculate_rstat: return None, None if dof > 0 and statval >= 0.0: qval = igamc(dof / 2.0, statval / 2.0) rstat = statval / dof return rstat, qval return numpy.nan, numpy.nan
def __init__(self, fit, results, init_stat, param_warnings): _vals = fit.data.eval_model_to_fit(fit.model) _dof = len(_vals) - len(tuple(results[1])) _qval = None _rstat = None _covarerr = results[4].get('covarerr') if (isinstance(fit.stat, (CStat,Chi2)) and not isinstance(fit.stat, LeastSq)): if _dof > 0 and results[2] >= 0.0: _qval = igamc(_dof/2., results[2]/2.) _rstat = results[2]/_dof else: _rstat = nan self.succeeded = results[0] self.parnames = tuple(p.fullname for p in fit.model.pars if not p.frozen) self.parvals = tuple(results[1]) self.istatval = init_stat self.statval = results[2] self.dstatval = abs(results[2]-init_stat) self.numpoints = len(_vals) self.dof = _dof self.qval = _qval self.rstat = _rstat self.message = results[3] if _covarerr is not None: self.covarerr = tuple(_covarerr) else: self.covarerr = None self.nfev = results[4].get('nfev') self.extra_output = results[4] self.modelvals = _vals self.methodname = type(fit.method).__name__.lower() self.itermethodname = fit._iterfit.itermethod_opts['name'] statname = type(fit.stat).__name__.lower() if isinstance(fit.stat, Chi2) and not isinstance(fit.stat, LeastSq): isSimulFit = isinstance(fit.data, DataSimulFit) if isSimulFit: is_error_set = [d.staterror is not None for d in fit.data.datasets] if all(is_error_set): statname = 'chi2' elif fit.data.staterror is not None: statname = 'chi2' self.statname = statname self.datasets = None # To be filled by calling function self.param_warnings = param_warnings NoNewAttributesAfterInit.__init__(self)