本文整理汇总了Python中utils.spdot函数的典型用法代码示例。如果您正苦于以下问题:Python spdot函数的具体用法?Python spdot怎么用?Python spdot使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了spdot函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: gradient
def gradient(self, par):
beta = np.reshape(np.array(par), (self.k, 1))
q = 2 * self.y - 1
qxb = q * spdot(self.x, beta)
lamb = q * norm.pdf(qxb) / norm.cdf(qxb)
gradient = spdot(lamb.T, self.x)[0]
return gradient
开发者ID:ljwolf,项目名称:pysal,代码行数:7,代码来源:probit.py
示例2: par_est
def par_est(self):
start = np.dot(la.inv(spdot(self.x.T, self.x)),
spdot(self.x.T, self.y))
flogl = lambda par: -self.ll(par)
if self.optim == 'newton':
fgrad = lambda par: self.gradient(par)
fhess = lambda par: self.hessian(par)
par_hat = newton(flogl, start, fgrad, fhess, self.maxiter)
warn = par_hat[2]
else:
fgrad = lambda par: -self.gradient(par)
if self.optim == 'bfgs':
par_hat = op.fmin_bfgs(
flogl, start, fgrad, full_output=1, disp=0)
warn = par_hat[6]
if self.optim == 'ncg':
fhess = lambda par: -self.hessian(par)
par_hat = op.fmin_ncg(
flogl, start, fgrad, fhess=fhess, full_output=1, disp=0)
warn = par_hat[5]
if warn > 0:
warn = True
else:
warn = False
return par_hat, warn
开发者ID:ljwolf,项目名称:pysal,代码行数:25,代码来源:probit.py
示例3: __init__
def __init__(self, y, x, yend, q=None, h=None,
robust=None, gwk=None, sig2n_k=False):
if issubclass(type(q), np.ndarray) and issubclass(type(h), np.ndarray):
raise Exception, "Please do not provide 'q' and 'h' together"
if q == None and h == None:
raise Exception, "Please provide either 'q' or 'h'"
self.y = y
self.n = y.shape[0]
self.x = x
self.kstar = yend.shape[1]
# including exogenous and endogenous variables
z = sphstack(self.x, yend)
if type(h).__name__ not in ['ndarray', 'csr_matrix']:
# including exogenous variables and instrument
h = sphstack(self.x, q)
self.z = z
self.h = h
self.q = q
self.yend = yend
# k = number of exogenous variables and endogenous variables
self.k = z.shape[1]
hth = spdot(h.T, h)
hthi = la.inv(hth)
zth = spdot(z.T, h)
hty = spdot(h.T, y)
factor_1 = np.dot(zth, hthi)
factor_2 = np.dot(factor_1, zth.T)
# this one needs to be in cache to be used in AK
varb = la.inv(factor_2)
factor_3 = np.dot(varb, factor_1)
betas = np.dot(factor_3, hty)
self.betas = betas
self.varb = varb
self.zthhthi = factor_1
# predicted values
self.predy = spdot(z, betas)
# residuals
u = y - self.predy
self.u = u
# attributes used in property
self.hth = hth # Required for condition index
self.hthi = hthi # Used in error models
self.htz = zth.T
if robust:
self.vm = ROBUST.robust_vm(reg=self, gwk=gwk, sig2n_k=sig2n_k)
self._cache = {}
if sig2n_k:
self.sig2 = self.sig2n_k
else:
self.sig2 = self.sig2n
开发者ID:sukhjitsehra,项目名称:processing_pysal,代码行数:59,代码来源:twosls.py
示例4: hessian
def hessian(self, par):
beta = np.reshape(np.array(par), (self.k, 1))
q = 2 * self.y - 1
xb = spdot(self.x, beta)
qxb = q * xb
lamb = q * norm.pdf(qxb) / norm.cdf(qxb)
hessian = spdot(self.x.T, spbroadcast(self.x,-lamb * (lamb + xb)))
return hessian
开发者ID:ljwolf,项目名称:pysal,代码行数:8,代码来源:probit.py
示例5: xb
def xb(self):
try:
return self._cache['xb']
except AttributeError:
self._cache = {}
self._cache['xb'] = spdot(self.x, self.betas)
except KeyError:
self._cache['xb'] = spdot(self.x, self.betas)
return self._cache['xb']
开发者ID:ljwolf,项目名称:pysal,代码行数:9,代码来源:probit.py
示例6: slopes_vm
def slopes_vm(self):
if 'slopes_vm' not in self._cache:
x = self.xmean
b = self.betas
dfdb = np.eye(self.k) - spdot(b.T, x) * spdot(b, x.T)
slopes_vm = (self.scale ** 2) * \
np.dot(np.dot(dfdb, self.vm), dfdb.T)
self._cache['slopes_vm'] = slopes_vm[1:, 1:]
return self._cache['slopes_vm']
开发者ID:GeoDaCenter,项目名称:GeoDaSpace,代码行数:9,代码来源:probit.py
示例7: _get_spat_diag_props
def _get_spat_diag_props(self,y, x, w, yend, q, w_lags, lag_q):
self._cache = {}
yend, q = set_endog(y, x, w, yend, q, w_lags, lag_q)
x = USER.check_constant(x)
x = REGI.regimeX_setup(x, self.regimes, [True] * x.shape[1], self.regimes_set)
self.z = sphstack(x,REGI.regimeX_setup(yend, self.regimes, [True] * (yend.shape[1]-1)+[False], self.regimes_set))
self.h = sphstack(x,REGI.regimeX_setup(q, self.regimes, [True] * q.shape[1], self.regimes_set))
hthi = np.linalg.inv(spdot(self.h.T,self.h))
zth = spdot(self.z.T,self.h)
self.varb = np.linalg.inv(spdot(spdot(zth,hthi),zth.T))
开发者ID:surfcao,项目名称:pysal,代码行数:10,代码来源:twosls_sp_regimes.py
示例8: _get_spat_diag_props
def _get_spat_diag_props(self, results, regi_ids, x, yend, q):
self._cache = {}
x = USER.check_constant(x)
x = REGI.regimeX_setup(
x, self.regimes, [True] * x.shape[1], self.regimes_set)
self.z = sphstack(x, REGI.regimeX_setup(
yend, self.regimes, [True] * yend.shape[1], self.regimes_set))
self.h = sphstack(
x, REGI.regimeX_setup(q, self.regimes, [True] * q.shape[1], self.regimes_set))
hthi = np.linalg.inv(spdot(self.h.T, self.h))
zth = spdot(self.z.T, self.h)
self.varb = np.linalg.inv(spdot(spdot(zth, hthi), zth.T))
开发者ID:elkingtonx,项目名称:pysal,代码行数:12,代码来源:twosls_regimes.py
示例9: AB
def AB(self):
"""
Computes A and B matrices as in Cliff-Ord 1981, p. 203
"""
if 'AB' not in self._cache:
U = (self.w.sparse + self.w.sparse.T) / 2.
z = spdot(U, self.reg.x, array_out=False)
c1 = spdot(self.reg.x.T, z, array_out=False)
c2 = spdot(z.T, z, array_out=False)
G = self.reg.xtxi
A = spdot(G, c1)
B = spdot(G, c2)
self._cache['AB'] = [A, B]
return self._cache['AB']
开发者ID:DrizzleRisk,项目名称:Security-Project,代码行数:14,代码来源:diagnostics_sp.py
示例10: __init__
def __init__(self, y, x, w):
#1a. OLS --> \tilde{betas}
ols = OLS.BaseOLS(y=y, x=x)
self.n, self.k = ols.x.shape
self.x = ols.x
self.y = ols.y
#1b. GMM --> \tilde{\lambda1}
moments = _momentsGM_Error(w, ols.u)
lambda1 = optim_moments(moments)
#2a. OLS -->\hat{betas}
xs = get_spFilter(w, lambda1, self.x)
ys = get_spFilter(w, lambda1, self.y)
ols2 = OLS.BaseOLS(y=ys, x=xs)
#Output
self.predy = spdot(self.x, ols2.betas)
self.u = y - self.predy
self.betas = np.vstack((ols2.betas, np.array([[lambda1]])))
self.sig2 = ols2.sig2n
self.e_filtered = self.u - lambda1*w*self.u
self.vm = self.sig2 * ols2.xtxi
se_betas = np.sqrt(self.vm.diagonal())
self._cache = {}
开发者ID:surfcao,项目名称:pysal,代码行数:27,代码来源:error_sp.py
示例11: _get_spat_diag_props
def _get_spat_diag_props(self, x, sig2n_k):
self.k = self.kr
self._cache = {}
x = np.hstack((np.ones((x.shape[0], 1)), x))
self.x = REGI.regimeX_setup(x, self.regimes, [True] * x.shape[1], self.regimes_set)
self.xtx = spdot(self.x.T, self.x)
self.xtxi = np.linalg.inv(self.xtx)
开发者ID:surfcao,项目名称:pysal,代码行数:7,代码来源:ols_regimes.py
示例12: _optimal_weight
def _optimal_weight(reg, sig2n_k, warn=True):
try:
Hu = reg.h.toarray() * reg.u ** 2
except:
Hu = reg.h * reg.u ** 2
if sig2n_k:
S = spdot(reg.h.T, Hu, array_out=True) / (reg.n - reg.k)
else:
S = spdot(reg.h.T, Hu, array_out=True) / reg.n
Si = np.linalg.inv(S)
ZtH = spdot(reg.z.T, reg.h)
ZtHSi = spdot(ZtH, Si)
fac2 = np.linalg.inv(spdot(ZtHSi, ZtH.T, array_out=True))
fac3 = spdot(ZtHSi, spdot(reg.h.T, reg.y), array_out=True)
betas = np.dot(fac2, fac3)
if sig2n_k:
vm = fac2 * (reg.n - reg.k)
else:
vm = fac2 * reg.n
RegressionProps_basic(reg, betas=betas, vm=vm, sig2=False)
reg.title += " (Optimal-Weighted GMM)"
if warn:
set_warn(
reg, "Residuals treated as homoskedastic for the purpose of diagnostics.")
return
开发者ID:elkingtonx,项目名称:pysal,代码行数:25,代码来源:twosls_regimes.py
示例13: _optimal_weight
def _optimal_weight(self,sig2n_k):
''' Computes optimal weights GMM. '''
H = spdot(spdot(self.h,self.hthi),self.htz)
fac2, ZtHSi = self._get_fac2_het(H,self.u,sig2n_k)
fac3 = spdot(ZtHSi,spdot(H.T,self.y),array_out=True)
betas = np.dot(fac2,fac3)
"""
# Updating S_hat to S_tilde
predy = spdot(self.z, betas)
u = self.y - predy
fac2, ZtHSi = self._get_fac2_het(u,sig2n_k)
"""
if sig2n_k:
vm = fac2*(self.n-self.k)
else:
vm = fac2*self.n
#return betas, predy, u, vm #Use this line instead of next if updating S_hat to S_tilde.
return betas, vm
开发者ID:ShuyaoHong,项目名称:pysal,代码行数:18,代码来源:twosls_regimes.py
示例14: slopes_vm
def slopes_vm(self):
try:
return self._cache['slopes_vm']
except AttributeError:
self._cache = {}
x = self.xmean
b = self.betas
dfdb = np.eye(self.k) - spdot(b.T, x) * spdot(b, x.T)
slopes_vm = (self.scale ** 2) * \
np.dot(np.dot(dfdb, self.vm), dfdb.T)
self._cache['slopes_vm'] = slopes_vm[1:, 1:]
except KeyError:
x = self.xmean
b = self.betas
dfdb = np.eye(self.k) - spdot(b.T, x) * spdot(b, x.T)
slopes_vm = (self.scale ** 2) * \
np.dot(np.dot(dfdb, self.vm), dfdb.T)
self._cache['slopes_vm'] = slopes_vm[1:, 1:]
return self._cache['slopes_vm']
开发者ID:ljwolf,项目名称:pysal,代码行数:19,代码来源:probit.py
示例15: j
def j(self):
if 'j' not in self._cache:
wxb = self.w.sparse * self.reg.predy
wxb2 = np.dot(wxb.T, wxb)
xwxb = spdot(self.reg.x.T, wxb)
num1 = wxb2 - np.dot(xwxb.T, np.dot(self.reg.xtxi, xwxb))
num = num1 + (self.t * self.reg.sig2n)
den = self.reg.n * self.reg.sig2n
self._cache['j'] = num / den
return self._cache['j']
开发者ID:DrizzleRisk,项目名称:Security-Project,代码行数:10,代码来源:diagnostics_sp.py
示例16: akTest
def akTest(iv, w, spDcache):
"""
Computes AK-test for the general case (end. reg. + sp. lag)
...
Parameters
----------
iv : STSLS_dev
Instance from spatial 2SLS regression
w : W
Spatial weights instance
spDcache : spDcache
Instance of spDcache class
Attributes
----------
mi : float
Moran's I statistic for IV residuals
ak : float
Square of corrected Moran's I for residuals::
.. math::
ak = \dfrac{N \times I^*}{\phi^2}
p : float
P-value of the test
ToDo:
* Code in as Nancy
* Compare both
"""
mi = get_mI(iv, w, spDcache)
# Phi2
etwz = spdot(iv.u.T, spdot(w.sparse, iv.z))
a = np.dot(etwz,np.dot(iv.varb,etwz.T))
s12 = (w.s0 / w.n)**2
phi2 = ( spDcache.t + (4.0 / iv.sig2n) * a ) / (s12 * w.n)
ak = w.n * mi**2 / phi2
pval = chisqprob(ak, 1)
return (mi, ak[0][0], pval[0][0])
开发者ID:Alwnikrotikz,项目名称:pysal,代码行数:42,代码来源:diagnostics_sp.py
示例17: __init__
def __init__(self, y, x, robust=None, gwk=None, sig2n_k=True):
self.x = x
self.xtx = spdot(self.x.T, self.x)
xty = spdot(self.x.T, y)
self.xtxi = la.inv(self.xtx)
self.betas = np.dot(self.xtxi, xty)
predy = spdot(self.x, self.betas)
u = y - predy
self.u = u
self.predy = predy
self.y = y
self.n, self.k = self.x.shape
if sig2n_k:
self.sig2 = self.sig2n_k
else:
self.sig2 = self.sig2n
if robust is not None:
self.vm = ROBUST.robust_vm(reg=self, gwk=gwk, sig2n_k=sig2n_k)
开发者ID:CartoDB,项目名称:pysal,代码行数:22,代码来源:ols.py
示例18: hac_multi
def hac_multi(reg, gwk, constant=False):
"""
HAC robust estimation of the variance-covariance matrix for multi-regression object
Parameters
----------
reg : Regression object (OLS or TSLS)
output instance from a regression model
gwk : PySAL weights object
Spatial weights based on kernel functions
Returns
--------
psi : kxk array
Robust estimation of the variance-covariance
"""
if not constant:
reg.hac_var = check_constant(reg.hac_var)
xu = spbroadcast(reg.hac_var, reg.u)
gwkxu = lag_spatial(gwk, xu)
psi0 = spdot(xu.T, gwkxu)
counter = 0
for m in reg.multi:
reg.multi[m].robust = 'hac'
reg.multi[m].name_gwk = reg.name_gwk
try:
psi1 = spdot(reg.multi[m].varb, reg.multi[m].zthhthi)
reg.multi[m].vm = spdot(psi1, np.dot(psi0, psi1.T))
except:
reg.multi[m].vm = spdot(
reg.multi[m].xtxi, np.dot(psi0, reg.multi[m].xtxi))
reg.vm[(counter * reg.kr):((counter + 1) * reg.kr),
(counter * reg.kr):((counter + 1) * reg.kr)] = reg.multi[m].vm
counter += 1
开发者ID:CartoDB,项目名称:pysal,代码行数:38,代码来源:robust.py
示例19: _get_fac2_het
def _get_fac2_het(self,H,u,sig2n_k):
D = SP.lil_matrix((self.n, self.n))
D.setdiag(u**2)
if sig2n_k:
S = spdot(spdot(self.z.T,D),self.z,array_out=True)/(self.n-self.k)
else:
S = spdot(spdot(self.z.T,D),self.z,array_out=True)/self.n
Si = np.linalg.inv(S)
ZtH = spdot(self.z.T,H)
ZtHSi = spdot(ZtH,Si)
fac2 = np.linalg.inv(spdot(ZtHSi,ZtH.T,array_out=True))
return fac2, ZtHSi
开发者ID:ShuyaoHong,项目名称:pysal,代码行数:12,代码来源:twosls_regimes.py
示例20: trA
def trA(self):
if 'trA' not in self._cache:
xtwx = spdot(self.reg.x.T, spdot(self.w.sparse, self.reg.x))
mw = np.dot(self.reg.xtxi, xtwx)
self._cache['trA'] = np.sum(mw.diagonal())
return self._cache['trA']
开发者ID:DrizzleRisk,项目名称:Security-Project,代码行数:6,代码来源:diagnostics_sp.py
注:本文中的utils.spdot函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论