本文整理汇总了Python中statsmodels.tools.numdiff.approx_hess函数的典型用法代码示例。如果您正苦于以下问题:Python approx_hess函数的具体用法?Python approx_hess怎么用?Python approx_hess使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了approx_hess函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: hessian
def hessian(self, params):
"""
Conditional logit Hessian matrix of the log-likelihood
Parameters
-----------
params : array-like
The parameters of the model
Returns
-------
hess : ndarray, (K, K)
The Hessian
Notes
-----
It is the second derivative with respect to the flattened parameters
of the loglikelihood function of the conditional logit model
evaluated at `params`.
.. math:: \\frac{\\partial^{2}\\ln L}{\\partial\\beta_{j}\\partial\\beta_{l}}=-\\sum_{i=1}^{n}\\frac{\\exp\\left(\\beta_{j}^{\\prime}x_{i}\\right)}{\\sum_{k=0}^{J}\\exp\\left(\\beta_{k}^{\\prime}x_{i}\\right)}\\left[\\boldsymbol{1}\\left(j=l\\right)-\\frac{\\exp\\left(\\beta_{l}^{\\prime}x_{i}\\right)}{\\sum_{k=0}^{J}\\exp\\left(\\beta_{k}^{\\prime}x_{i}\\right)}\\right]x_{i}x_{l}^{\\prime}
where
:math:`\\boldsymbol{1}\\left(j=l\\right)` equals 1 if `j` = `l` and 0
otherwise.
"""
# TODO: analytical derivatives
from statsmodels.tools.numdiff import approx_hess
# need options for hess (epsilon)
return approx_hess(params, self.loglike)
示例2: hessian
def hessian(self, params):
"""
Generic Zero Inflated model Hessian matrix of the loglikelihood
Parameters
----------
params : array-like
The parameters of the model
Returns
-------
hess : ndarray, (k_vars, k_vars)
The Hessian, second derivative of loglikelihood function,
evaluated at `params`
Notes
-----
"""
hess_arr_main = self._hessian_main(params)
hess_arr_infl = self._hessian_inflate(params)
if hess_arr_main is None or hess_arr_infl is None:
return approx_hess(params, self.loglike)
dim = self.k_exog + self.k_inflate
hess_arr = np.zeros((dim, dim))
hess_arr[:self.k_inflate,:] = hess_arr_infl
hess_arr[self.k_inflate:,self.k_inflate:] = hess_arr_main
tri_idx = np.triu_indices(self.k_exog + self.k_inflate, k=1)
hess_arr[tri_idx] = hess_arr.T[tri_idx]
return hess_arr
示例3: compute_param_cov
def compute_param_cov(self, params, backcast=None, robust=True):
"""
Computes parameter covariances using numerical derivatives.
Parameters
----------
params : 1-d array
Model parameters
robust : bool, optional
Flag indicating whether to use robust standard errors (True) or
classic MLE (False)
"""
resids = self.resids(self.starting_values())
var_bounds = self.volatility.variance_bounds(resids)
nobs = resids.shape[0]
if backcast is None and self._backcast is None:
backcast = self.volatility.backcast(resids)
self._backcast = backcast
elif backcast is None:
backcast = self._backcast
kwargs = {"sigma2": np.zeros_like(resids), "backcast": backcast, "var_bounds": var_bounds, "individual": False}
hess = approx_hess(params, self._loglikelihood, kwargs=kwargs)
hess /= nobs
inv_hess = np.linalg.inv(hess)
if robust:
kwargs["individual"] = True
scores = approx_fprime(params, self._loglikelihood, kwargs=kwargs)
score_cov = np.cov(scores.T)
return inv_hess.dot(score_cov).dot(inv_hess) / nobs
else:
return inv_hess / nobs
示例4: bse
def bse(self): # allow user to specify?
if self.model.method == "cmle": # uses different scale/sigma def.
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)))
示例5: hessian_numdiff
def hessian_numdiff(self, params, pen_weight=None, **kwds):
"""hessian based on finite difference derivative
"""
if pen_weight is None:
pen_weight = self.pen_weight
loglike = lambda p: self.loglike(p, pen_weight=pen_weight, **kwds)
from statsmodels.tools.numdiff import approx_hess
return approx_hess(params, loglike)
示例6: test_derivatives
def test_derivatives(self):
pen = self.pen
x = self.params
ps = np.array([pen.deriv(np.atleast_1d(xi)) for xi in x])
psn = np.array([approx_fprime(np.atleast_1d(xi), pen.func) for xi in x])
assert_allclose(ps, psn, rtol=1e-7, atol=1e-8)
ph = np.array([pen.deriv2(np.atleast_1d(xi)) for xi in x])
phn = np.array([approx_hess(np.atleast_1d(xi), pen.func) for xi in x])
if ph.ndim == 2:
# SmoothedSCAD returns only diagonal if hessian if independent
# TODO should ww allow this also in [email protected]?
ph = np.array([np.diag(phi) for phi in ph])
assert_allclose(ph, phn, rtol=1e-7, atol=1e-8)
示例7: test_compute_covariance_without_hess_inverse
def test_compute_covariance_without_hess_inverse(self):
fitmethod = "powell"
opt = scipy.optimize.minimize(self.lpost, self.t0,
method=fitmethod,
args=self.neg, tol=1.e-10)
optres = OptimizationResultsSubclassDummy(self.lpost, opt,
neg=True)
optres._compute_covariance(self.lpost, opt)
if comp_hessian:
phess = approx_hess(opt.x, self.lpost)
hess_inv = np.linalg.inv(phess)
assert np.all(optres.cov == hess_inv)
assert np.all(optres.err == np.sqrt(np.diag(np.abs(hess_inv))))
else:
assert optres.cov is None
assert optres.err is None
示例8: _compute_covariance
def _compute_covariance(self, lpost, res):
if hasattr(res, "hess_inv"):
if not isinstance(res.hess_inv, np.ndarray):
self.cov = np.asarray(res.hess_inv.todense())
else:
self.cov = res.hess_inv
self.err = np.sqrt(np.diag(self.cov))
else:
if comp_hessian:
# calculate Hessian approximating with finite differences
logging.info("Approximating Hessian with finite differences ...")
phess = approx_hess(self.p_opt, lpost)
self.cov = np.linalg.inv(phess)
self.err = np.sqrt(np.diag(np.abs(self.cov)))
else:
self.cov = None
self.err = None
示例9: print
modp.fixed_params = None
modp.fixed_paramsmask = None
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.tools.numdiff import approx_hess
hb=-approx_hess(modp.start_params, modp.loglike, epsilon=-1e-4)
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)
示例10: hessian
def hessian(self, params):
"""
Returns numerical hessian for now.
"""
loglike = self.loglike
return approx_hess(params, loglike)
示例11: print
xk = np.array([1,2,3])
xk = np.array([1.,1.,1.])
#xk = np.zeros(3)
beta = xk
y = np.dot(x, beta) + 0.1*np.random.randn(nobs)
xkols = np.dot(np.linalg.pinv(x),y)
print(approx_fprime((1,2,3),fun,epsilon,x))
gradtrue = x.sum(0)
print(x.sum(0))
gradcs = approx_fprime_cs((1,2,3), fun, (x,), h=1.0e-20)
print(gradcs, maxabs(gradcs, gradtrue))
print(approx_hess_cs((1,2,3), fun, (x,), h=1.0e-20)) #this is correctly zero
print(approx_hess_cs((1,2,3), fun2, (y,x), h=1.0e-20)-2*np.dot(x.T, x))
print(numdiff.approx_hess(xk,fun2,1e-3, (y,x))[0] - 2*np.dot(x.T, x))
gt = (-x*2*(y-np.dot(x, [1,2,3]))[:,None])
g = approx_fprime_cs((1,2,3), fun1, (y,x), h=1.0e-20)#.T #this shouldn't be transposed
gd = numdiff.approx_fprime((1,2,3),fun1,epsilon,(y,x))
print(maxabs(g, gt))
print(maxabs(gd, gt))
import statsmodels.api as sm
data = sm.datasets.spector.load(as_pandas=False)
data.exog = sm.add_constant(data.exog, prepend=False)
#mod = sm.Probit(data.endog, data.exog)
mod = sm.Logit(data.endog, data.exog)
#res = mod.fit(method="newton")
示例12: test_compare_numdiff
def test_compare_numdiff(self):
n_grp = 200
grpsize = 5
k_fe = 3
k_re = 2
for use_sqrt in False, True:
for reml in False, True:
for profile_fe in False, True:
np.random.seed(3558)
exog_fe = np.random.normal(size=(n_grp * grpsize, k_fe))
exog_re = np.random.normal(size=(n_grp * grpsize, k_re))
exog_re[:, 0] = 1
exog_vc = np.random.normal(size=(n_grp * grpsize, 3))
slopes = np.random.normal(size=(n_grp, k_re))
slopes[:, -1] *= 2
slopes = np.kron(slopes, np.ones((grpsize, 1)))
slopes_vc = np.random.normal(size=(n_grp, 3))
slopes_vc = np.kron(slopes_vc, np.ones((grpsize, 1)))
slopes_vc[:, -1] *= 2
re_values = (slopes * exog_re).sum(1)
vc_values = (slopes_vc * exog_vc).sum(1)
err = np.random.normal(size=n_grp * grpsize)
endog = exog_fe.sum(1) + re_values + vc_values + err
groups = np.kron(range(n_grp), np.ones(grpsize))
vc = {"a": {}, "b": {}}
for i in range(n_grp):
ix = np.flatnonzero(groups == i)
vc["a"][i] = exog_vc[ix, 0:2]
vc["b"][i] = exog_vc[ix, 2:3]
model = MixedLM(endog, exog_fe, groups, exog_re, exog_vc=vc, use_sqrt=use_sqrt)
rslt = model.fit(reml=reml)
loglike = loglike_function(model, profile_fe=profile_fe, has_fe=not profile_fe)
# Test the score at several points.
for kr in range(5):
fe_params = np.random.normal(size=k_fe)
cov_re = np.random.normal(size=(k_re, k_re))
cov_re = np.dot(cov_re.T, cov_re)
vcomp = np.random.normal(size=2) ** 2
params = MixedLMParams.from_components(fe_params, cov_re=cov_re, vcomp=vcomp)
params_vec = params.get_packed(has_fe=not profile_fe, use_sqrt=use_sqrt)
# Check scores
gr = -model.score(params, profile_fe=profile_fe)
ngr = nd.approx_fprime(params_vec, loglike)
assert_allclose(gr, ngr, rtol=1e-3)
# Check Hessian matrices at the MLE (we don't have
# the profile Hessian matrix and we don't care
# about the Hessian for the square root
# transformed parameter).
if (profile_fe is False) and (use_sqrt is False):
hess = -model.hessian(rslt.params_object)
params_vec = rslt.params_object.get_packed(use_sqrt=False, has_fe=True)
loglike_h = loglike_function(model, profile_fe=False, has_fe=True)
nhess = nd.approx_hess(params_vec, loglike_h)
assert_allclose(hess, nhess, rtol=1e-3)
示例13: hessian
def hessian(self, AB_mask):
"""
Returns numerical hessian.
"""
loglike = self.loglike
return approx_hess(AB_mask, loglike)
示例14: test_compare_numdiff
def test_compare_numdiff(self):
import statsmodels.tools.numdiff as nd
n_grp = 200
grpsize = 5
k_fe = 3
k_re = 2
for jl in 0,1:
for reml in False,True:
for cov_pen_wt in 0,10:
cov_pen = penalties.PSD(cov_pen_wt)
np.random.seed(3558)
exog_fe = np.random.normal(size=(n_grp*grpsize, k_fe))
exog_re = np.random.normal(size=(n_grp*grpsize, k_re))
exog_re[:, 0] = 1
slopes = np.random.normal(size=(n_grp, k_re))
slopes = np.kron(slopes, np.ones((grpsize,1)))
re_values = (slopes * exog_re).sum(1)
err = np.random.normal(size=n_grp*grpsize)
endog = exog_fe.sum(1) + re_values + err
groups = np.kron(range(n_grp), np.ones(grpsize))
if jl == 0:
md = MixedLM(endog, exog_fe, groups, exog_re)
score = lambda x: -md.score_sqrt(x)
hessian = lambda x : -md.hessian_sqrt(x)
else:
md = MixedLM(endog, exog_fe, groups, exog_re, use_sqrt=False)
score = lambda x: -md.score_full(x)
hessian = lambda x: -md.hessian_full(x)
md.reml = reml
md.cov_pen = cov_pen
loglike = lambda x: -md.loglike(x)
rslt = md.fit()
# Test the score at several points.
for kr in range(5):
fe_params = np.random.normal(size=k_fe)
cov_re = np.random.normal(size=(k_re, k_re))
cov_re = np.dot(cov_re.T, cov_re)
params = MixedLMParams.from_components(fe_params, cov_re)
if jl == 0:
params_vec = params.get_packed()
else:
params_vec = params.get_packed(use_sqrt=False)
# Check scores
gr = score(params)
ngr = nd.approx_fprime(params_vec, loglike)
assert_allclose(gr, ngr, rtol=1e-2)
# Hessian matrices don't agree well away from
# the MLE.
#if cov_pen_wt == 0:
# hess = hessian(params)
# nhess = nd.approx_hess(params_vec, loglike)
# assert_allclose(hess, nhess, rtol=1e-2)
# Check Hessian matrices at the MLE.
if cov_pen_wt == 0:
hess = hessian(rslt.params_object)
params_vec = rslt.params_object.get_packed()
nhess = nd.approx_hess(params_vec, loglike)
assert_allclose(hess, nhess, rtol=1e-2)
示例15: test_compare_numdiff
def test_compare_numdiff(self, use_sqrt, reml, profile_fe):
n_grp = 200
grpsize = 5
k_fe = 3
k_re = 2
np.random.seed(3558)
exog_fe = np.random.normal(size=(n_grp * grpsize, k_fe))
exog_re = np.random.normal(size=(n_grp * grpsize, k_re))
exog_re[:, 0] = 1
exog_vc = np.random.normal(size=(n_grp * grpsize, 3))
slopes = np.random.normal(size=(n_grp, k_re))
slopes[:, -1] *= 2
slopes = np.kron(slopes, np.ones((grpsize, 1)))
slopes_vc = np.random.normal(size=(n_grp, 3))
slopes_vc = np.kron(slopes_vc, np.ones((grpsize, 1)))
slopes_vc[:, -1] *= 2
re_values = (slopes * exog_re).sum(1)
vc_values = (slopes_vc * exog_vc).sum(1)
err = np.random.normal(size=n_grp * grpsize)
endog = exog_fe.sum(1) + re_values + vc_values + err
groups = np.kron(range(n_grp), np.ones(grpsize))
vc = {"a": {}, "b": {}}
for i in range(n_grp):
ix = np.flatnonzero(groups == i)
vc["a"][i] = exog_vc[ix, 0:2]
vc["b"][i] = exog_vc[ix, 2:3]
model = MixedLM(
endog,
exog_fe,
groups,
exog_re,
exog_vc=vc,
use_sqrt=use_sqrt)
rslt = model.fit(reml=reml)
loglike = loglike_function(
model, profile_fe=profile_fe, has_fe=not profile_fe)
try:
# Test the score at several points.
for kr in range(5):
fe_params = np.random.normal(size=k_fe)
cov_re = np.random.normal(size=(k_re, k_re))
cov_re = np.dot(cov_re.T, cov_re)
vcomp = np.random.normal(size=2)**2
params = MixedLMParams.from_components(
fe_params, cov_re=cov_re, vcomp=vcomp)
params_vec = params.get_packed(
has_fe=not profile_fe, use_sqrt=use_sqrt)
# Check scores
gr = -model.score(params, profile_fe=profile_fe)
ngr = nd.approx_fprime(params_vec, loglike)
assert_allclose(gr, ngr, rtol=1e-3)
# Check Hessian matrices at the MLE (we don't have
# the profile Hessian matrix and we don't care
# about the Hessian for the square root
# transformed parameter).
if (profile_fe is False) and (use_sqrt is False):
hess = -model.hessian(rslt.params_object)
params_vec = rslt.params_object.get_packed(
use_sqrt=False, has_fe=True)
loglike_h = loglike_function(
model, profile_fe=False, has_fe=True)
nhess = nd.approx_hess(params_vec, loglike_h)
assert_allclose(hess, nhess, rtol=1e-3)
except AssertionError:
# See GH#5628; because this test fails unpredictably but only on
# OSX, we only xfail it there.
if PLATFORM_OSX:
pytest.xfail("fails on OSX due to unresolved "
"numerical differences")
else:
raise