def bse(self): # allow user to specify? if self.model.method == "cmle": # uses different scale/sigma definition resid = self.resid ssr = np.dot(resid,resid) ols_scale = ssr/(self.nobs - self.k_ar - self.k_trend) return np.sqrt(np.diag(self.cov_params(scale=ols_scale))) else: hess = approx_hess(self.params, self.model.loglike) return np.sqrt(np.diag(-np.linalg.inv(hess[0])))
def bse(self): # allow user to specify? if self.model.method == "cmle": # uses different scale/sigma definition resid = self.resid ssr = np.dot(resid, resid) ols_scale = ssr / (self.nobs - self.k_ar - self.k_trend) return np.sqrt(np.diag(self.cov_params(scale=ols_scale))) else: hess = approx_hess(self.params, self.model.loglike) return np.sqrt(np.diag(-np.linalg.inv(hess[0])))
def hessian(self, params): """ Compute the Hessian at params, Notes ----- This is a numerical approximation. """ loglike = self.loglike #if self.transparams: # params = self._invtransparams(params) if not fast_kalman or self.method == "css": return approx_hess_cs(params, loglike, epsilon=1e-5) else: return approx_hess(params, self.loglike, epsilon=1e-3)[0]
def hessian(self, params): """ Returns numerical hessian for now. """ loglike = self.loglike return approx_hess(params, loglike)[0]
data_exog = sm.add_constant(rvs) xbeta = 0.9 + 0.1*rvs.sum(1) data_endog = xbeta + 0.1*np.random.standard_t(5, size=nobs) #print data_endog modp = MyT(data_endog, data_exog) modp.start_value = np.ones(data_exog.shape[1]+2) modp.start_value[-2] = 10 modp.start_params = modp.start_value resp = modp.fit(start_params = modp.start_value) print resp.params print resp.bse from statsmodels.sandbox.regression.numdiff import approx_fprime1, approx_hess hb=-approx_hess(modp.start_value, modp.loglike, epsilon=-1e-4)[0] tmp = modp.loglike(modp.start_value) print tmp.shape ''' >>> tmp = modp.loglike(modp.start_value) 8 >>> tmp.shape (100,) >>> tmp.sum(0) -24220.877108016182 >>> tmp = modp.nloglikeobs(modp.start_value) 8 >>> tmp.shape (100, 100)
modp.fixed_paramsmask = None resp = modp.fit(start_params = modp.start_params, disp=1, method='nm')#'newton') #resp = modp.fit(start_params = modp.start_params, disp=1, method='newton') print '\nestimation results t-dist' print resp.params print resp.bse resp2 = modp.fit(start_params = resp.params, method='Newton') print 'using Newton' print resp2.params print resp2.bse from statsmodels.sandbox.regression.numdiff import approx_fprime1, approx_hess hb=-approx_hess(modp.start_params, modp.loglike, epsilon=-1e-4)[0] tmp = modp.loglike(modp.start_params) print tmp.shape #np.linalg.eigh(np.linalg.inv(hb))[0] pp=np.array(store_params) print pp.min(0) print pp.max(0) ##################### Example: Pareto # estimating scale doesn't work yet, a bug somewhere ? # fit_ks works well, but no bse or other result statistics yet
return approx_fprime1(params, self.nloglike) File "c:\josef\eclipsegworkspace\statsmodels-josef-experimental-gsoc\scikits\s tatsmodels\sandbox\regression\numdiff.py", line 81, in approx_fprime1 nobs = np.size(f0) #len(f0) TypeError: object of type 'numpy.float64' has no len() ''' res_bfgs = mod_norm2.fit(start_params=start_params, method="bfgs", fprime=None, maxiter=500, retall=0) from statsmodels.sandbox.regression.numdiff import approx_fprime1, approx_hess hb = -approx_hess(res_norm3.params, mod_norm2.loglike, epsilon=-1e-4)[0] hf = -approx_hess(res_norm3.params, mod_norm2.loglike, epsilon=1e-4)[0] hh = (hf + hb) / 2. print np.linalg.eigh(hh) grad = -approx_fprime1(res_norm3.params, mod_norm2.loglike, epsilon=-1e-4) print grad gradb = -approx_fprime1(res_norm3.params, mod_norm2.loglike, epsilon=-1e-4) gradf = -approx_fprime1(res_norm3.params, mod_norm2.loglike, epsilon=1e-4) print(gradb + gradf) / 2. print res_norm3.model.score(res_norm3.params) print res_norm3.model.score(start_params) mod_norm2.loglike(start_params / 2.) print np.linalg.inv(-1 * mod_norm2.hessian(res_norm3.params)) print np.sqrt(np.diag(res_bfgs.cov_params()))
print '\nResults with TLinearModel' print '-------------------------' resp = modp.fit(start_params=modp.start_params, disp=1, method='nm', maxfun=10000, maxiter=5000) #'newton') #resp = modp.fit(start_params = modp.start_params, disp=1, method='newton') print 'using Nelder-Mead' print resp.params print resp.bse resp2 = modp.fit(start_params=resp.params, method='Newton') print 'using Newton' print resp2.params print resp2.bse from statsmodels.sandbox.regression.numdiff import approx_fprime1, approx_hess hb = -approx_hess(modp.start_params, modp.loglike, epsilon=-1e-4)[0] tmp = modp.loglike(modp.start_params) print tmp.shape print 'eigenvalues of numerical Hessian' print np.linalg.eigh(np.linalg.inv(hb))[0] #store_params is only available in original test script ##pp=np.array(store_params) ##print pp.min(0) ##print pp.max(0)
def hessian(self, AB_mask): """ Returns numerical hessian. """ loglike = self.loglike return approx_hess(AB_mask, loglike)[0]
score = lambda params: -self.score(params) File "c:\josef\eclipsegworkspace\statsmodels-josef-experimental-gsoc\scikits\s tatsmodels\model.py", line 480, in score return approx_fprime1(params, self.nloglike) File "c:\josef\eclipsegworkspace\statsmodels-josef-experimental-gsoc\scikits\s tatsmodels\sandbox\regression\numdiff.py", line 81, in approx_fprime1 nobs = np.size(f0) #len(f0) TypeError: object of type 'numpy.float64' has no len() ''' res_bfgs = mod_norm2.fit(start_params=start_params, method="bfgs", fprime=None, maxiter = 500, retall=0) from statsmodels.sandbox.regression.numdiff import approx_fprime1, approx_hess hb=-approx_hess(res_norm3.params, mod_norm2.loglike, epsilon=-1e-4)[0] hf=-approx_hess(res_norm3.params, mod_norm2.loglike, epsilon=1e-4)[0] hh = (hf+hb)/2. print np.linalg.eigh(hh) grad = -approx_fprime1(res_norm3.params, mod_norm2.loglike, epsilon=-1e-4) print grad gradb = -approx_fprime1(res_norm3.params, mod_norm2.loglike, epsilon=-1e-4) gradf = -approx_fprime1(res_norm3.params, mod_norm2.loglike, epsilon=1e-4) print (gradb+gradf)/2. print res_norm3.model.score(res_norm3.params) print res_norm3.model.score(start_params) mod_norm2.loglike(start_params/2.) print np.linalg.inv(-1*mod_norm2.hessian(res_norm3.params)) print np.sqrt(np.diag(res_bfgs.cov_params()))