(sin asunto)
Nan loz
nan_lo2001 en yahoo.com
Vie Jul 28 00:46:04 CEST 2006
Hola a todos! Necesito ayuda con un codigo que escribi
para estimar regresiones. La funcion que me interesa
es la ultima (hacvar). El problema es que es muy
lento, usando solo 49 observaciones se demora como 10
minutos asi que para poderlo implementar realmente
necesito que sea mas rapido. Les agradezco cualquier
sugerencia. Muchas gracias,
Nancy Lozano
class getdata:
"""This class gets data and defines y, x and inst
to be used in regressions
also asks to define coordinates"""
def
__init__(self,filename,y=[],x=[],inst=[],xcoord=[],ycoord=[]):
self.db=dbfreader113005.dbf(filename)
self.ynames=y
self.xnames=x
self.instnames=inst
self.Y=self.db.arrayfields(self.ynames)[1]
self.X=self.db.arrayfields(self.xnames)[1]
self.H=self.db.arrayfields(self.instnames)[1]#if
inst is empty then this gives a matrix of all numeric
vaiables
self.xcoord=self.db.listfields(xcoord)
self.ycoord=self.db.listfields(ycoord)
#estimating OLS
class estimators:
def __init__(self,Y,X,H,xcoord,ycoord):
self.var = X
self.Y = Y
self.H = H
self.xcoord=xcoord
self.ycoord=ycoord
""" The class estimator must receive as input a
getdata object
It can also receive dbf objects
X,Y,H: must be in array format
X: array of exogenous variables
Y: array of dependent variable
H: array of instruments to be used in IV
regressions
xcoord: x coordinate values, must be as a list
ycoord: y coordinate values, must come as a
list"""
def ols(self,constant=1):
"""The function ols gives as result a
dictionary with keys:values
olsb: array of coefficients from ols
resid: array of residuals
varcov: variance-covariance matrix as array
yhat: estimated value of dependent variable
seb: standard errors for ols coefficients"""
n=len(self.Y[0])
if constant==1:
c=nm.ones((1,n),type='Float64')
self.X=nm.concatenate((c,self.var))
else:
self.X=self.var
self.k=len(self.X)
XpX=nm.innerproduct(self.X,self.X)
iXpX=la.inverse(XpX)
XY=nm.innerproduct(self.X,self.Y)
olsb=mm(iXpX,XY)
Xt=nm.transpose(self.X)
yhat=mm(Xt,olsb)
resid=self.Y[0,:]-yhat[:,0]
residt=nm.transpose(resid)
sigmasqd=(mm(residt,resid))*(1.0/(float(n)-float(self.k)))
#print sigmasqd
varcov=sigmasqd*iXpX
res2=mm(residt,resid)
sumy=sum(self.Y[0,:])/float(n)
R2 = 1.0-(res2/sum([(i-sumy)**2.0 for i in
self.Y[0,:]]))
R2_adj=1.0-((float(n)-1.0)*((1.0-R2)/(float(n)-float(self.k))))
self.olsresults = {}
self.olsresults['olsb']=olsb.tolist()
self.olsresults['resid']=resid.tolist()
self.olsresults['varcov']=varcov
self.olsresults['yhat']=yhat.tolist()
self.olsresults['seb']=[math.sqrt(i) for i in
varcov.diagonal()]
self.olsresults['t-values']=[i[0]/j for (i,j) in
zip(olsb.tolist(),self.olsresults['seb'])]
self.olsresults['sigmasqd']=sigmasqd
self.olsresults['R2']=R2
self.olsresults['R2_adj']=R2_adj
return self.olsresults
def hacvar(self,dm,kmethod):
"""This function gives the HAC estimate for
the variance-covariance matrix
from Kelejian and Prucha (2005).
OLS must be run before this function is used
so that the residuals from the OLS
regression can be used to estimate the
variance covariance matrix
dm: this is the cutoff used to determine the
neighbors, it must be in the same units
as the x,y coordinates kmethod: defines the
kernel that must be used, ex. Parzen"""
self.hacvar=[]
v=self.X.tolist()
h=len(v)
n=float(len(v[0]))
for k in range(h):
row=[]
for l in range(h):
sumij=0.0
testij=0.0
for i, p in enumerate(v[l]):
tempkern=Kernels.kernel(self.xcoord[0][i],self.ycoord[0][i],self.xcoord[0],self.ycoord[0],dm)
tempk=getattr(tempkern,kmethod)()
ri=self.ols()['resid'][i]
temp1=[a*ri*self.ols()['resid'][j]*v[k][i]*tempk[j]
for j, a in enumerate(v[l])]
sumj=sum(temp1)
sumij+=sumj
if tempkern.nneigh>=n**(1.0/3.0):
t=1.0
else:
t=0.0
testij+=t
rowkl=sumij/n
row.append(rowkl)
self.hacvar.append(row)
if testij>0:
print "The number of neighbors is more than n^(1/3)
for %s observations" % testij
det=la.determinant(self.hacvar)
if det<0.0:
print "The determinant of the estimated HAC var-cov
matrix is not p.s.d"
else:
print "The determinant of the estimated HAC var-cov
matrix is %s " % det
return self.hacvar
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
Más información sobre la lista de distribución Python-es