def covar(self): """ Returns ------- covar : np.ndarray The covariance matrix for the fitting system """ _pvals = np.array(self.varying_parameters()) hess = approx_hess2(_pvals, self.nll) covar = np.linalg.inv(hess) self.setp(_pvals) return covar
def covar(self, target="nll"): """ Estimates a covariance matrix based on numerical differentiation of either the negative log-likelihood or negative log-posterior probabilities. Parameters ---------- target : str, {"nll", "nlpost"} Returns ------- covar : np.ndarray The covariance matrix for the fitting system Notes ----- Estimation of a covariance matrix can be susceptible to numeric instabilities. Critically evaluate the matrix before further use. """ _pvals = np.array(self.varying_parameters()) if target == "nll": fn = self.nll elif target == "nlpost": fn = self.nlpost try: # from statsmodels # the output from this for the test in test_objective.covar # is very similar to numdifftools.Hessian, or a chained version # of approx_derivative hess = approx_hess2(_pvals, fn) covar = np.linalg.inv(hess) except LinAlgError: sz = np.size(_pvals, 0) covar = np.full((sz, sz), np.inf) finally: self.setp(_pvals) return covar