예제 #1
0
파일: sur.py 프로젝트: CartoDB/pysal
    def __init__(self,bigy,bigX,bigyend,bigq,nonspat_diag=True,\
        name_bigy=None,name_bigX=None,name_bigyend=None,name_bigq=None,\
        name_ds=None):
        
        #need checks on match between bigy, bigX dimensions
        BaseThreeSLS.__init__(self,bigy=bigy,bigX=bigX,bigyend=bigyend,\
            bigq=bigq)

        self.name_ds = USER.set_name_ds(name_ds)
        #initialize names - should be generated by sur_stack
        if name_bigy:
            self.name_bigy = name_bigy
        else: # need to construct y names
            self.name_bigy = {}
            for r in range(self.n_eq):
                yn = 'dep_var_' + str(r+1)
                self.name_bigy[r] = yn               
        if name_bigX:
            self.name_bigX = name_bigX
        else: # need to construct x names
            self.name_bigX = {}
            for r in range(self.n_eq):
                k = bigX[r].shape[1] - 1
                name_x = ['var_' + str(i + 1) + "_" + str(r+1) for i in range(k)]
                ct = 'Constant_' + str(r+1)  # NOTE: constant always included in X
                name_x.insert(0, ct)
                self.name_bigX[r] = name_x
        if name_bigyend:
            self.name_bigyend = name_bigyend
        else: # need to construct names
            self.name_bigyend = {}
            for r in range(self.n_eq):
                ky = bigyend[r].shape[1]
                name_ye = ['end_' + str(i + 1) + "_" + str(r+1) for i in range(ky)]
                self.name_bigyend[r] = name_ye
        if name_bigq:
            self.name_bigq = name_bigq
        else: # need to construct names
            self.name_bigq = {}
            for r in range(self.n_eq):
                ki = bigq[r].shape[1]
                name_i = ['inst_' + str(i + 1) + "_" + str(r+1) for i in range(ki)]
                self.name_bigq[r] = name_i                
               
        #inference
        self.tsls_inf = sur_setp(self.b3SLS,self.varb)
        
        # test on constancy of coefficients across equations
        if check_k(self.bigK):   # only for equal number of variables
            self.surchow = sur_chow(self.n_eq,self.bigK,self.b3SLS,self.varb)
        else:
            self.surchow = None                
        
        #Listing of the results
        self.title = "THREE STAGE LEAST SQUARES (3SLS)"                
        SUMMARY.SUR(reg=self, tsls=True, nonspat_diag=nonspat_diag)
예제 #2
0
파일: sur.py 프로젝트: youngpong/pysal
    def __init__(self,bigy,bigX,bigyend,bigq,nonspat_diag=True,\
        name_bigy=None,name_bigX=None,name_bigyend=None,name_bigq=None,\
        name_ds=None):

        #need checks on match between bigy, bigX dimensions
        BaseThreeSLS.__init__(self,bigy=bigy,bigX=bigX,bigyend=bigyend,\
            bigq=bigq)

        self.name_ds = USER.set_name_ds(name_ds)
        #initialize names - should be generated by sur_stack
        if name_bigy:
            self.name_bigy = name_bigy
        else: # need to construct y names
            self.name_bigy = {}
            for r in range(self.n_eq):
                yn = 'dep_var_' + str(r+1)
                self.name_bigy[r] = yn
        if name_bigX:
            self.name_bigX = name_bigX
        else: # need to construct x names
            self.name_bigX = {}
            for r in range(self.n_eq):
                k = bigX[r].shape[1] - 1
                name_x = ['var_' + str(i + 1) + "_" + str(r+1) for i in range(k)]
                ct = 'Constant_' + str(r+1)  # NOTE: constant always included in X
                name_x.insert(0, ct)
                self.name_bigX[r] = name_x
        if name_bigyend:
            self.name_bigyend = name_bigyend
        else: # need to construct names
            self.name_bigyend = {}
            for r in range(self.n_eq):
                ky = bigyend[r].shape[1]
                name_ye = ['end_' + str(i + 1) + "_" + str(r+1) for i in range(ky)]
                self.name_bigyend[r] = name_ye
        if name_bigq:
            self.name_bigq = name_bigq
        else: # need to construct names
            self.name_bigq = {}
            for r in range(self.n_eq):
                ki = bigq[r].shape[1]
                name_i = ['inst_' + str(i + 1) + "_" + str(r+1) for i in range(ki)]
                self.name_bigq[r] = name_i

        #inference
        self.tsls_inf = sur_setp(self.b3SLS,self.varb)

        # test on constancy of coefficients across equations
        if check_k(self.bigK):   # only for equal number of variables
            self.surchow = sur_chow(self.n_eq,self.bigK,self.b3SLS,self.varb)
        else:
            self.surchow = None

        #Listing of the results
        self.title = "THREE STAGE LEAST SQUARES (3SLS)"
        SUMMARY.SUR(reg=self, tsls=True, nonspat_diag=nonspat_diag)
예제 #3
0
    def __init__(self,bigy,bigX,w=None,nonspat_diag=True,spat_diag=False,vm=False,\
        iter=False,maxiter=5,epsilon=0.00001,verbose=False,\
        name_bigy=None,name_bigX=None,name_ds=None,name_w=None):

        #need checks on match between bigy, bigX dimensions
        # init moved here before name check
        BaseSUR.__init__(self,bigy=bigy,bigX=bigX,iter=iter,\
            maxiter=maxiter,epsilon=epsilon,verbose=verbose)

        self.name_ds = USER.set_name_ds(name_ds)
        self.name_w = USER.set_name_w(name_w, w)
        #initialize names - should be generated by sur_stack
        if name_bigy:
            self.name_bigy = name_bigy
        else:  # need to construct y names
            self.name_bigy = {}
            for r in range(self.n_eq):
                yn = 'dep_var_' + str(r)
                self.name_bigy[r] = yn
        if name_bigX:
            self.name_bigX = name_bigX
        else:  # need to construct x names
            self.name_bigX = {}
            for r in range(self.n_eq):
                k = self.bigX[r].shape[1] - 1
                name_x = ['var_' + str(i + 1) + "_" + str(r) for i in range(k)]
                ct = 'Constant_' + str(
                    r)  # NOTE: constant always included in X
                name_x.insert(0, ct)
                self.name_bigX[r] = name_x

        #inference
        self.sur_inf = sur_setp(self.bSUR, self.varb)

        if nonspat_diag:
            #LR test on off-diagonal elements of Sigma
            self.lrtest = sur_lrtest(self.n, self.n_eq, self.ldetS0,
                                     self.ldetS1)

            #LM test on off-diagonal elements of Sigma
            self.lmtest = sur_lmtest(self.n, self.n_eq, self.sig_ols)
        else:
            self.lrtest = None
            self.lmtest = None

        #LM test on spatial error autocorrelation
        if spat_diag:
            if not w:
                raise Exception, "Error: spatial weights needed"
            WS = w.sparse
            self.lmEtest = surLMe(self.n_eq, WS, self.bigE, self.sig)
        else:
            self.lmEtest = None

        #LM test on spatial lag autocorrelation

        # test on constancy of coefficients across equations
        if check_k(self.bigK):  # only for equal number of variables
            self.surchow = sur_chow(self.n_eq, self.bigK, self.bSUR, self.varb)
        else:
            self.surchow = None

        #Listing of the results
        self.title = "SEEMINGLY UNRELATED REGRESSIONS (SUR)"
        SUMMARY.SUR(reg=self,
                    nonspat_diag=nonspat_diag,
                    spat_diag=spat_diag,
                    surlm=True)
예제 #4
0
파일: sur.py 프로젝트: CartoDB/pysal
    def __init__(self,bigy,bigX,w=None,nonspat_diag=True,spat_diag=False,vm=False,\
        iter=False,maxiter=5,epsilon=0.00001,verbose=False,\
        name_bigy=None,name_bigX=None,name_ds=None,name_w=None):
        
        #need checks on match between bigy, bigX dimensions
        # init moved here before name check
        BaseSUR.__init__(self,bigy=bigy,bigX=bigX,iter=iter,\
            maxiter=maxiter,epsilon=epsilon,verbose=verbose)

        self.name_ds = USER.set_name_ds(name_ds)
        self.name_w = USER.set_name_w(name_w, w)
        #initialize names - should be generated by sur_stack
        if name_bigy:
            self.name_bigy = name_bigy
        else: # need to construct y names
            self.name_bigy = {}
            for r in range(self.n_eq):
                yn = 'dep_var_' + str(r)
                self.name_bigy[r] = yn               
        if name_bigX:
            self.name_bigX = name_bigX
        else: # need to construct x names
            self.name_bigX = {}
            for r in range(self.n_eq):
                k = self.bigX[r].shape[1] - 1
                name_x = ['var_' + str(i + 1) + "_" + str(r) for i in range(k)]
                ct = 'Constant_' + str(r)  # NOTE: constant always included in X
                name_x.insert(0, ct)
                self.name_bigX[r] = name_x
        
        #inference
        self.sur_inf = sur_setp(self.bSUR,self.varb)
        
        if nonspat_diag:
            #LR test on off-diagonal elements of Sigma
            self.lrtest = sur_lrtest(self.n,self.n_eq,self.ldetS0,self.ldetS1)
        
            #LM test on off-diagonal elements of Sigma
            self.lmtest = sur_lmtest(self.n,self.n_eq,self.sig_ols)
        else:
            self.lrtest = None
            self.lmtest = None
        
        #LM test on spatial error autocorrelation
        if spat_diag:
            if not w:
                 raise Exception, "Error: spatial weights needed"
            WS = w.sparse
            self.lmEtest = surLMe(self.n_eq,WS,self.bigE,self.sig)
        else:
            self.lmEtest = None
        
        #LM test on spatial lag autocorrelation
        
        # test on constancy of coefficients across equations
        if check_k(self.bigK):   # only for equal number of variables
            self.surchow = sur_chow(self.n_eq,self.bigK,self.bSUR,self.varb)
        else:
            self.surchow = None
        
        #Listing of the results
        self.title = "SEEMINGLY UNRELATED REGRESSIONS (SUR)"                
        SUMMARY.SUR(reg=self, nonspat_diag=nonspat_diag, spat_diag=spat_diag, surlm=True)
예제 #5
0
    def __init__(self,bigy,bigX,w,nonspat_diag=True,spat_diag=False,vm=False,\
        epsilon=0.0000001,\
        name_bigy=None,name_bigX=None,name_ds=None,name_w=None):
        
        #need checks on match between bigy, bigX dimensions

        # moved init here
        BaseSURerrorML.__init__(self,bigy=bigy,bigX=bigX,w=w,epsilon=epsilon)

        # check on variable names for listing results
        self.name_ds = USER.set_name_ds(name_ds)
        self.name_w = USER.set_name_w(name_w, w)
        #initialize names - should be generated by sur_stack
        if name_bigy:
            self.name_bigy = name_bigy
        else: # need to construct y names
            self.name_bigy = {}
            for r in range(self.n_eq):
                yn = 'dep_var_' + str(r)
                self.name_bigy[r] = yn               
        if name_bigX:
            self.name_bigX = name_bigX
        else: # need to construct x names
            self.name_bigX = {}
            for r in range(self.n_eq):
                k = self.bigX[r].shape[1] - 1
                name_x = ['var_' + str(i + 1) + "_" + str(r+1) for i in range(k)]
                ct = 'Constant_' + str(r+1)  # NOTE: constant always included in X
                name_x.insert(0, ct)
                self.name_bigX[r] = name_x

        

                   
        #inference
        self.sur_inf = sur_setp(self.bSUR,self.varb)
        
        # adjust concentrated log lik for constant
        const = -self.n2 * (self.n_eq * (1.0 + np.log(2.0*np.pi)))
        self.errllik = const + self.clikerr
        self.surerrllik = const + self.cliksurerr
                
        # LR test on off-diagonal sigma
        if nonspat_diag:
            M = self.n_eq * (self.n_eq - 1)/2.0
            likrodiag = 2.0 * (self.surerrllik - self.errllik)
            plik1 = stats.chisqprob(likrodiag, M)
            self.lrtest = (likrodiag,int(M),plik1)
        else:
            self.lrtest = None
        
        # LR test on spatial autoregressive coefficients
        if spat_diag:
            liklambda = 2.0 * (self.surerrllik - self.llik)
            plik2 = stats.chisqprob(liklambda, self.n_eq)
            self.likrlambda = (liklambda,self.n_eq,plik2)
        else:
            self.likrlambda = None
        
        # asymptotic variance for spatial coefficient
        if vm:
            self.vm = surerrvm(self.n,self.n_eq,w,self.lamsur,self.sig)
            vlam = self.vm[:self.n_eq,:self.n_eq]
            self.lamsetp = lam_setp(self.lamsur,vlam)
            # test on constancy of lambdas
            R = buildR(kr=1,kf=0,nr=self.n_eq)
            w,p = wald_test(self.lamsur,R,np.zeros((R.shape[0],1)),vlam)
            self.lamtest = (w,R.shape[0],p)
            if spat_diag:  # test on joint significance of lambdas
                Rj = np.identity(self.n_eq)
                wj,pj = wald_test(self.lamsur,Rj,np.zeros((Rj.shape[0],1)),vlam)
                self.joinlam = (wj,Rj.shape[0],pj)
            else:
                self.joinlam = None
        else:
            self.vm = None
            self.lamsetp = None
            self.lamtest = None
            self.joinlam = None

        # test on constancy of regression coefficients across equations
        if check_k(self.bigK):   # only for equal number of variables
            self.surchow = sur_chow(self.n_eq,self.bigK,self.bSUR,self.varb)
        else:
            self.surchow = None          
                    
        # listing of results
        self.title = "SEEMINGLY UNRELATED REGRESSIONS (SUR) - SPATIAL ERROR MODEL"                
        SUMMARY.SUR(reg=self, nonspat_diag=nonspat_diag, spat_diag=spat_diag, lambd=True)
예제 #6
0
파일: sur_lag.py 프로젝트: neophyte7/pysal
    def __init__(self,bigy,bigX,bigyend=None,bigq=None,w=None,vm=False,\
                 w_lags=1, lag_q=True, nonspat_diag=True, spat_diag=False,\
                 name_bigy=None,name_bigX=None,name_bigyend=None,\
                 name_bigq=None,name_ds=None,name_w=None):
        if w == None:
            raise Exception, "Spatial weights required for SUR-Lag"
        self.w = w
        WS = w.sparse
        self.name_ds = USER.set_name_ds(name_ds)
        self.name_w = USER.set_name_w(name_w, w)
        if bigyend and not(bigq):
            raise Exception, "Instruments needed when endogenous variables"
        #initialize
        self.bigy = bigy
        self.n_eq = len(self.bigy.keys())
        if name_bigy:
            self.name_bigy = name_bigy
        else: # need to construct y names
            self.name_bigy = {}
            for r in range(self.n_eq):
                yn = 'dep_var_' + str(r+1)
                self.name_bigy[r] = yn               
        self.bigX = bigX
        if name_bigX:
            self.name_bigX = name_bigX
        else: # need to construct x names
            self.name_bigX = {}
            for r in range(self.n_eq):
                k = self.bigX[r].shape[1] - 1
                name_x = ['var_' + str(i + 1) + "_" + str(r+1) for i in range(k)]
                ct = 'Constant_' + str(r+1)  # NOTE: constant always included in X
                name_x.insert(0, ct)
                self.name_bigX[r] = name_x
        if bigyend: # check on other endogenous
            self.bigyend = bigyend
            if name_bigyend:
                self.name_bigyend = name_bigyend
            else: # need to construct names
                self.name_bigyend = {}
                for r in range(self.n_eq):
                    ky = self.bigyend[r].shape[1]
                    name_ye = ['end_' + str(i + 1) + "_" + str(r+1) for i in range(ky)]
                    self.name_bigyend[r] = name_ye
        if bigq:  # check on instruments
            self.bigq = bigq
            if name_bigq:
                self.name_bigq = name_bigq
            else: # need to construct names
                self.name_bigq = {}
                for r in range(self.n_eq):
                    ki = self.bigq[r].shape[1]
                    name_i = ['inst_' + str(i + 1) + "_" + str(r+1) for i in range(ki)]
                    self.name_bigq[r] = name_i                
                
        #spatial lag dependent variable
        bigylag = {}
        for r in range(self.n_eq):
            bigylag[r] = WS*self.bigy[r]
        if bigyend:
            self.bigyend=bigyend
            for r in range(self.n_eq):
                self.bigyend[r] = np.hstack((self.bigyend[r],bigylag[r]))
            # adjust variable names
            for r in range(self.n_eq):
                wyname = "W_" + self.name_bigy[r]
                self.name_bigyend[r].append(wyname)            
        else: # no other endogenous variables
            self.bigyend={}
            for r in range(self.n_eq):
                self.bigyend[r] = bigylag[r]
            # variable names
            self.name_bigyend = {}
            for r in range(self.n_eq):
                wyname = ["W_" + self.name_bigy[r]]
                self.name_bigyend[r] = wyname
                    
        #spatially lagged exogenous variables
        bigwx = {}
        wxnames = {}
        if w_lags == 1:
            for r in range(self.n_eq):
                bigwx[r] = WS* self.bigX[r][:,1:]
                wxnames[r] = [ "W_" + i for i in self.name_bigX[r][1:]]
            if bigq: # other instruments
                if lag_q:  # also lags for instruments
                    bigwq = {}
                    for r in range(self.n_eq):
                        bigwq = WS* self.bigq[r]
                        self.bigq[r] = np.hstack((self.bigq[r],bigwx[r],bigwq))
                        wqnames = [ "W_" + i for i in self.name_bigq[r]]
                        wxnames[r] = wxnames[r] + wqnames
                        self.name_bigq[r] = self.name_bigq[r] + wxnames[r]
                else:  # no lags for other instruments
                    for r in range(self.n_eq):
                        self.bigq[r] = np.hstack((self.bigq[r],bigwx[r]))
                        self.name_bigq[r] = self.name_bigq[r] + wxnames[r]
            else: #no other instruments only wx
                self.bigq = {}
                self.name_bigq = {}
                for r in range(self.n_eq):
                    self.bigq[r]=bigwx[r]
                    self.name_bigq[r] = wxnames[r]
        elif w_lags > 1:  # higher order lags for WX
            for r in range(self.n_eq):
                bigwxwork = WS* self.bigX[r][:,1:]
                bigwx[r] = bigwxwork
                nameswork = [ "W_" + i for i in self.name_bigX[r][1:]]
                wxnames[r] = nameswork
                for i in range(1,w_lags):
                    bigwxwork = WS*bigwxwork
                    bigwx[r] = np.hstack((bigwx[r],bigwxwork))
                    nameswork = [ "W" + i for i in nameswork ]
                    wxnames[r] = wxnames[r] + nameswork
            if bigq: # other instruments
                if lag_q: # lags for other instruments
                    wq = {}
                    wqnames = {}
                    for r in range(self.n_eq):
                        bigwq = WS* self.bigq[r]
                        wqnameswork = [ "W_" + i for i in self.name_bigq[r]]
                        wqnames[r] = wqnameswork
                        wq[r] = bigwq                        
                        for i in range(1,w_lags):
                            bigwq = WS* bigwq
                            wq[r] = np.hstack((wq[r],bigwq))
                            wqnameswork = [ "W" + i for i in wqnameswork ]
                            wqnames[r] = wqnames[r] + wqnameswork
                        self.bigq[r] = np.hstack((self.bigq[r],bigwx[r],wq[r]))
                        self.name_bigq[r] = self.name_bigq[r] + wxnames[r] + wqnames[r]
                            
                else:  # no lags for other instruments
                    for r in range(self.n_eq):
                        self.bigq[r] = np.hstack((self.bigq[r],bigwx[r]))
                        self.name_bigq[r] = self.name_bigq[r] + wxnames[r]
            else: # no other instruments only wx
                self.bigq = {}
                self.name_bigq = {}
                for r in range(self.n_eq):
                    self.bigq[r] = bigwx[r]
                    self.name_bigq[r] = wxnames[r]

        else:
            raise Exception, "Lag order must be 1 or higher"
            
        BaseThreeSLS.__init__(self,bigy=self.bigy,bigX=self.bigX,bigyend=self.bigyend,\
            bigq=self.bigq)
        
        #inference
        self.tsls_inf = sur_setp(self.b3SLS,self.varb)
        
        # test on joint significance of spatial coefficients
        if spat_diag:   
            self.joinrho = sur_joinrho(self.n_eq,self.bigK,self.b3SLS,self.varb)
        else:
            self.joinrho = None
        
        # test on constancy of coefficients across equations
        if check_k(self.bigK):   # only for equal number of variables
            self.surchow = sur_chow(self.n_eq,self.bigK,self.b3SLS,self.varb)
        else:
            self.surchow = None   
        
        #list results
        self.title = "SEEMINGLY UNRELATED REGRESSIONS (SUR) - SPATIAL LAG MODEL"                
        SUMMARY.SUR(reg=self, tsls=True, spat_diag=spat_diag, nonspat_diag=nonspat_diag)