예제 #1
0
 def __init__(self,
              bigy,
              bigX,
              iter=False,
              maxiter=5,
              epsilon=0.00001,
              verbose=False):
     # setting up the cross-products
     self.bigy = bigy
     self.bigX = bigX
     self.n_eq = len(bigy.keys())
     self.n = bigy[0].shape[0]
     self.bigK = np.zeros((self.n_eq, 1), dtype=np.int_)
     for r in range(self.n_eq):
         self.bigK[r] = self.bigX[r].shape[1]
     self.bigXX, self.bigXy = sur_crossprod(self.bigX, self.bigy)
     # OLS regression by equation, sets up initial residuals
     self.sur_ols()  # creates self.bOLS and self.olsE
     # SUR estimation using OLS residuals - two step estimation
     self.bSUR, self.varb, self.sig = sur_est(self.bigXX, self.bigXy,
                                              self.olsE, self.bigK)
     resids = sur_resids(self.bigy, self.bigX,
                         self.bSUR)  # matrix of residuals
     # Sigma and log det(Sigma) for null model
     self.sig_ols = self.sig
     sols = np.diag(np.diag(self.sig))
     self.ldetS0 = np.log(np.diag(sols)).sum()
     det0 = self.ldetS0
     # setup for iteration
     det1 = la.slogdet(self.sig)[1]
     self.ldetS1 = det1
     #self.niter = 0
     if iter:  # iterated FGLS aka ML
         n_iter = 0
         while np.abs(det1 - det0) > epsilon and n_iter <= maxiter:
             n_iter += 1
             det0 = det1
             self.bSUR,self.varb,self.sig = sur_est(self.bigXX,self.bigXy,\
                       resids,self.bigK)
             resids = sur_resids(self.bigy, self.bigX, self.bSUR)
             det1 = la.slogdet(self.sig)[1]
             if verbose:
                 print(n_iter, det0, det1)
         self.bigE = sur_resids(self.bigy, self.bigX, self.bSUR)
         self.ldetS1 = det1
         self.niter = n_iter
     else:
         self.niter = 1
         self.bigE = resids
     self.corr = sur_corr(self.sig)
     lik = self.n_eq * (1.0 + np.log(2.0 * np.pi)) + self.ldetS1
     self.llik = -(self.n / 2.0) * lik
예제 #2
0
파일: sur.py 프로젝트: CartoDB/pysal
 def __init__(self,bigy,bigX,iter=False,maxiter=5,epsilon=0.00001,verbose=False):
     # setting up the cross-products
     self.bigy = bigy
     self.bigX = bigX
     self.n_eq = len(bigy.keys())
     self.n = bigy[0].shape[0]
     self.bigK = np.zeros((self.n_eq,1),dtype=np.int_)
     for r in range(self.n_eq):
         self.bigK[r] = self.bigX[r].shape[1]
     self.bigXX,self.bigXy = sur_crossprod(self.bigX,self.bigy)
     # OLS regression by equation, sets up initial residuals
     self.sur_ols() # creates self.bOLS and self.olsE
     # SUR estimation using OLS residuals - two step estimation
     self.bSUR,self.varb,self.sig = sur_est(self.bigXX,self.bigXy,self.olsE,self.bigK)
     resids = sur_resids(self.bigy,self.bigX,self.bSUR)  # matrix of residuals
     # Sigma and log det(Sigma) for null model
     self.sig_ols = self.sig
     sols = np.diag(np.diag(self.sig))
     self.ldetS0 = np.log(np.diag(sols)).sum()
     det0 = self.ldetS0
     # setup for iteration
     det1 = la.slogdet(self.sig)[1]
     self.ldetS1 = det1
     #self.niter = 0
     if iter:    # iterated FGLS aka ML
         n_iter = 0
         while np.abs(det1-det0) > epsilon and n_iter <= maxiter:
             n_iter += 1
             det0 = det1
             self.bSUR,self.varb,self.sig = sur_est(self.bigXX,self.bigXy,\
                       resids,self.bigK)
             resids = sur_resids(self.bigy,self.bigX,self.bSUR)
             det1 = la.slogdet(self.sig)[1]
             if verbose:
                 print (n_iter,det0,det1)
         self.bigE = sur_resids(self.bigy,self.bigX,self.bSUR)
         self.ldetS1 = det1
         self.niter = n_iter
     else:
         self.niter = 1
         self.bigE = resids
     self.corr = sur_corr(self.sig)
     lik = self.n_eq * (1.0 + np.log(2.0*np.pi)) + self.ldetS1
     self.llik = - (self.n / 2.0) * lik
예제 #3
0
    def __init__(self, bigy, bigX, bigyend, bigq):
        # setting up the cross-products
        self.bigy = bigy
        self.n_eq = len(bigy.keys())
        self.n = bigy[0].shape[0]
        # dictionary with exog and endog, Z
        self.bigZ = {}
        for r in range(self.n_eq):
            self.bigZ[r] = np.hstack((bigX[r], bigyend[r]))
        # number of explanatory variables by equation
        self.bigK = np.zeros((self.n_eq, 1), dtype=np.int_)
        for r in range(self.n_eq):
            self.bigK[r] = self.bigZ[r].shape[1]
        # dictionary with instruments, H
        bigH = {}
        for r in range(self.n_eq):
            bigH[r] = np.hstack((bigX[r], bigq[r]))
        # dictionary with instrumental variables, X and yend_predicted, Z-hat
        bigZhat = {}
        for r in range(self.n_eq):
            try:
                HHi = la.inv(np.dot(bigH[r].T, bigH[r]))
            except:
                raise Exception, "ERROR: singular cross product matrix, check instruments"
            Hye = np.dot(bigH[r].T, bigyend[r])
            yp = np.dot(bigH[r], np.dot(HHi, Hye))
            bigZhat[r] = np.hstack((bigX[r], yp))
        self.bigZHZH, self.bigZHy = sur_crossprod(bigZhat, self.bigy)

        # 2SLS regression by equation, sets up initial residuals
        self.sur_2sls()  # creates self.b2SLS and self.tslsE

        self.b3SLS, self.varb, self.sig = sur_est(self.bigZHZH, self.bigZHy,
                                                  self.tslsE, self.bigK)
        self.bigE = sur_resids(self.bigy, self.bigZ,
                               self.b3SLS)  # matrix of residuals

        # inter-equation correlation matrix
        self.corr = sur_corr(self.sig)
예제 #4
0
파일: sur.py 프로젝트: CartoDB/pysal
    def __init__(self,bigy,bigX,bigyend,bigq):
        # setting up the cross-products
        self.bigy = bigy
        self.n_eq = len(bigy.keys())
        self.n = bigy[0].shape[0]
        # dictionary with exog and endog, Z
        self.bigZ = {}
        for r in range(self.n_eq):
            self.bigZ[r] = np.hstack((bigX[r],bigyend[r]))   
        # number of explanatory variables by equation     
        self.bigK = np.zeros((self.n_eq,1),dtype=np.int_)
        for r in range(self.n_eq):
            self.bigK[r] = self.bigZ[r].shape[1]
        # dictionary with instruments, H
        bigH = {}
        for r in range(self.n_eq):
            bigH[r] = np.hstack((bigX[r],bigq[r]))    
        # dictionary with instrumental variables, X and yend_predicted, Z-hat
        bigZhat = {}
        for r in range(self.n_eq):
            try:
                HHi = la.inv(np.dot(bigH[r].T,bigH[r]))
            except:
                raise Exception, "ERROR: singular cross product matrix, check instruments"
            Hye = np.dot(bigH[r].T,bigyend[r])
            yp = np.dot(bigH[r],np.dot(HHi,Hye))
            bigZhat[r] = np.hstack((bigX[r],yp))
        self.bigZHZH,self.bigZHy = sur_crossprod(bigZhat,self.bigy)
         
        # 2SLS regression by equation, sets up initial residuals
        self.sur_2sls() # creates self.b2SLS and self.tslsE

        self.b3SLS,self.varb,self.sig = sur_est(self.bigZHZH,self.bigZHy,self.tslsE,self.bigK)
        self.bigE = sur_resids(self.bigy,self.bigZ,self.b3SLS)  # matrix of residuals
        
        # inter-equation correlation matrix
        self.corr = sur_corr(self.sig)
예제 #5
0
 def __init__(self,bigy,bigX,w,epsilon=0.0000001):
     # setting up constants
     self.n = w.n
     self.n2 = self.n / 2.0
     self.n_eq = len(bigy.keys())
     WS = w.sparse
     I = sp.identity(self.n)
     # variables
     self.bigy = bigy
     self.bigX = bigX
     # number of variables by equation
     self.bigK = np.zeros((self.n_eq,1),dtype=np.int_)
     for r in range(self.n_eq):
         self.bigK[r] = self.bigX[r].shape[1]
     # spatially lagged variables
     self.bigylag = {}
     for r in range(self.n_eq):
         self.bigylag[r] = WS*self.bigy[r]
     # note: unlike WX as instruments, this includes the constant
     self.bigXlag = {}
     for r in range(self.n_eq):
         self.bigXlag[r] = WS*self.bigX[r]
         
     # spatial parameter starting values
     lam = np.zeros((self.n_eq,1))  # initialize as an array
     fun0 = 0.0
     fun1 = 0.0
     for r in range(self.n_eq):
         res = minimize_scalar(err_c_loglik_sp, 0.0, bounds=(-1.0,1.0),
                     args=(self.n, self.bigy[r], self.bigylag[r], 
                     self.bigX[r], self.bigXlag[r], I, WS),
                     method='bounded', options={'xatol':epsilon})
         lam[r]= res.x
         fun1 += res.fun
     self.lamols = lam
     self.clikerr = -fun1  #negative because use in min
     
     # SUR starting values
     reg0 = BaseSUR(self.bigy,self.bigX,iter=True)
     bigE = reg0.bigE
     self.bSUR0 = reg0.bSUR
     self.llik = reg0.llik  # as is, includes constant
     
     # iteration
     lambdabounds = [ (-1.0,+1.0) for i in range(self.n_eq)]
     while abs(fun0 - fun1) > epsilon:
         fun0 = fun1
         sply = filter_dict(lam,self.bigy,self.bigylag)
         splX = filter_dict(lam,self.bigX,self.bigXlag)
         WbigE = WS * bigE
         splbigE = bigE - WbigE * lam.T
         splXX,splXy = sur_crossprod(splX,sply)
         b1,varb1,sig1 = sur_est(splXX,splXy,splbigE,self.bigK)
         bigE = sur_resids(self.bigy,self.bigX,b1)
         res = minimize(clik,lam,args=(self.n,self.n2,self.n_eq,\
                         bigE,I,WS),method='L-BFGS-B',\
                         bounds=lambdabounds)
         lam = res.x
         lam.resize((self.n_eq,1))
         fun1 = res.fun
     self.lamsur = lam
     self.bSUR = b1
     self.varb = varb1
     self.sig = sig1
     self.corr = sur_corr(self.sig)
     self.bigE = bigE
     self.cliksurerr = -fun1  #negative because use in min, no constant