Exemple #1
0
    def __init__(self, y, x, yend=None, q=None,\
                 w=None, w_lags=1, lag_q=True,\
                 robust=None, gwk=None, sig2n_k=False,\
                 spat_diag=False,\
                 vm=False, name_y=None, name_x=None,\
                 name_yend=None, name_q=None,\
                 name_w=None, name_gwk=None, name_ds=None):

        n = USER.check_arrays(x, yend, q)
        USER.check_y(y, n)
        USER.check_weights(w, y, w_required=True)
        USER.check_robust(robust, gwk)
        yend2, q2 = set_endog(y, x, w, yend, q, w_lags, lag_q)
        x_constant = USER.check_constant(x)
        BaseGM_Lag.__init__(self, y=y, x=x_constant, w=w.sparse, yend=yend2, q=q2,\
                            w_lags=w_lags, robust=robust, gwk=gwk,\
                            lag_q=lag_q, sig2n_k=sig2n_k)
        self.predy_e, self.e_pred, warn = sp_att(w,self.y,self.predy,\
                      yend2[:,-1].reshape(self.n,1),self.betas[-1])
        set_warn(self,warn)
        self.title = "SPATIAL TWO STAGE LEAST SQUARES"        
        self.name_ds = USER.set_name_ds(name_ds)
        self.name_y = USER.set_name_y(name_y)
        self.name_x = USER.set_name_x(name_x, x)
        self.name_yend = USER.set_name_yend(name_yend, yend)
        self.name_yend.append(USER.set_name_yend_sp(self.name_y))
        self.name_z = self.name_x + self.name_yend
        self.name_q = USER.set_name_q(name_q, q)
        self.name_q.extend(USER.set_name_q_sp(self.name_x, w_lags, self.name_q, lag_q))
        self.name_h = USER.set_name_h(self.name_x, self.name_q)
        self.robust = USER.set_robust(robust)
        self.name_w = USER.set_name_w(name_w, w)
        self.name_gwk = USER.set_name_w(name_gwk, gwk)
        SUMMARY.GM_Lag(reg=self, w=w, vm=vm, spat_diag=spat_diag)
Exemple #2
0
    def __init__(self, y, x, yend=None, q=None,\
                 w=None, w_lags=1, lag_q=True,\
                 vm=False, name_y=None, name_x=None,\
                 name_yend=None, name_q=None,\
                 name_w=None, name_ds=None):

        n = USER.check_arrays(y, x, yend, q)
        USER.check_y(y, n)
        USER.check_weights(w, y, w_required=True)
        yend2, q2 = set_endog(y, x, w, yend, q, w_lags, lag_q)
        x_constant = USER.check_constant(x)
        BaseGM_Combo.__init__(self, y=y, x=x_constant, w=w.sparse, yend=yend2, q=q2,\
                                w_lags=w_lags, lag_q=lag_q)
        self.rho = self.betas[-2]
        self.predy_e, self.e_pred, warn = sp_att(w,self.y,\
                   self.predy,yend2[:,-1].reshape(self.n,1),self.rho)
        set_warn(self, warn)
        self.title = "SPATIALLY WEIGHTED TWO STAGE LEAST SQUARES"        
        self.name_ds = USER.set_name_ds(name_ds)
        self.name_y = USER.set_name_y(name_y)
        self.name_x = USER.set_name_x(name_x, x)
        self.name_yend = USER.set_name_yend(name_yend, yend)
        self.name_yend.append(USER.set_name_yend_sp(self.name_y))
        self.name_z = self.name_x + self.name_yend
        self.name_z.append('lambda')
        self.name_q = USER.set_name_q(name_q, q)
        self.name_q.extend(USER.set_name_q_sp(self.name_x, w_lags, self.name_q, lag_q))
        self.name_h = USER.set_name_h(self.name_x, self.name_q)
        self.name_w = USER.set_name_w(name_w, w)
        SUMMARY.GM_Combo(reg=self, w=w, vm=vm)   
Exemple #3
0
    def __init__(self, y, x, yend=None, q=None,\
                 w=None, w_lags=1, lag_q=True,\
                 robust=None, gwk=None, sig2n_k=False,\
                 spat_diag=False,\
                 vm=False, name_y=None, name_x=None,\
                 name_yend=None, name_q=None,\
                 name_w=None, name_gwk=None, name_ds=None):

        n = USER.check_arrays(x, yend, q)
        USER.check_y(y, n)
        USER.check_weights(w, y, w_required=True)
        USER.check_robust(robust, gwk)
        yend2, q2 = set_endog(y, x, w, yend, q, w_lags, lag_q)
        x_constant = USER.check_constant(x)
        BaseGM_Lag.__init__(self, y=y, x=x_constant, w=w.sparse, yend=yend2, q=q2,\
                            w_lags=w_lags, robust=robust, gwk=gwk,\
                            lag_q=lag_q, sig2n_k=sig2n_k)
        self.predy_e, self.e_pred, warn = sp_att(w,self.y,self.predy,\
                      yend2[:,-1].reshape(self.n,1),self.betas[-1])
        set_warn(self, warn)
        self.title = "SPATIAL TWO STAGE LEAST SQUARES"
        self.name_ds = USER.set_name_ds(name_ds)
        self.name_y = USER.set_name_y(name_y)
        self.name_x = USER.set_name_x(name_x, x)
        self.name_yend = USER.set_name_yend(name_yend, yend)
        self.name_yend.append(USER.set_name_yend_sp(self.name_y))
        self.name_z = self.name_x + self.name_yend
        self.name_q = USER.set_name_q(name_q, q)
        self.name_q.extend(
            USER.set_name_q_sp(self.name_x, w_lags, self.name_q, lag_q))
        self.name_h = USER.set_name_h(self.name_x, self.name_q)
        self.robust = USER.set_robust(robust)
        self.name_w = USER.set_name_w(name_w, w)
        self.name_gwk = USER.set_name_w(name_gwk, gwk)
        SUMMARY.GM_Lag(reg=self, w=w, vm=vm, spat_diag=spat_diag)
Exemple #4
0
    def __init__(self, y, x, equationID, yend=None, q=None, w=None,\
                 cores=None, sig2n_k=False, name_y=None, name_x=None, name_yend=None,\
                 name_q=None, name_equationID=None, name_ds=None, name_w=None, vm=False,\
                 w_lags=1, lag_q=True):

        ThreeSLS.__init__(self, y=y, x=x, equationID=equationID, yend=yend, q=q, w=w, cores=cores,\
                 sig2n_k=sig2n_k, name_y=name_y, name_x=name_x, name_yend=name_yend, name_q=name_q,\
                 name_equationID=name_equationID, name_ds=name_ds, name_w=name_w, vm=vm,\
                 w_lags=w_lags, lag_q=lag_q)

        for r in self.eq_set:
            self.multi[r].predy_e, self.multi[r].e_pred, warn = sp_att(w,self.multi[r].y,self.multi[r].predy,\
                          self.multi[r].yend[:,-1].reshape(self.n,1),self.multi[r].betas[-1])
            set_warn(self.multi[r], warn)

        title = "SPATIAL THREE-STAGE LEAST SQUARES - EQUATION "
        self.multi = USER.set_name_multi(self.multi,
                                         self.eq_set,
                                         name_equationID,
                                         y,
                                         x,
                                         name_y,
                                         name_x,
                                         name_ds,
                                         title,
                                         name_w,
                                         robust=None,
                                         endog=(yend, q, name_yend, name_q),
                                         sp_lag=(w_lags, lag_q))
        SUMMARY.GM_Lag_multi(reg=self,
                             multireg=self.multi,
                             vm=vm,
                             spat_diag=False,
                             sur=True)
Exemple #5
0
    def __init__(self, y, x, yend=None, q=None,\
                 w=None, w_lags=1, lag_q=True,\
                 vm=False, name_y=None, name_x=None,\
                 name_yend=None, name_q=None,\
                 name_w=None, name_ds=None):

        n = USER.check_arrays(y, x, yend, q)
        USER.check_y(y, n)
        USER.check_weights(w, y, w_required=True)
        yend2, q2 = set_endog(y, x, w, yend, q, w_lags, lag_q)
        x_constant = USER.check_constant(x)
        BaseGM_Combo.__init__(self, y=y, x=x_constant, w=w.sparse, yend=yend2, q=q2,\
                                w_lags=w_lags, lag_q=lag_q)
        self.predy_e, self.e_pred, warn = sp_att(w,self.y,\
                   self.predy,yend2[:,-1].reshape(self.n,1),self.betas[-2])
        set_warn(self, warn)
        self.title = "SPATIALLY WEIGHTED TWO STAGE LEAST SQUARES"
        self.name_ds = USER.set_name_ds(name_ds)
        self.name_y = USER.set_name_y(name_y)
        self.name_x = USER.set_name_x(name_x, x)
        self.name_yend = USER.set_name_yend(name_yend, yend)
        self.name_yend.append(USER.set_name_yend_sp(self.name_y))
        self.name_z = self.name_x + self.name_yend
        self.name_z.append('lambda')
        self.name_q = USER.set_name_q(name_q, q)
        self.name_q.extend(
            USER.set_name_q_sp(self.name_x, w_lags, self.name_q, lag_q))
        self.name_h = USER.set_name_h(self.name_x, self.name_q)
        self.name_w = USER.set_name_w(name_w, w)
        SUMMARY.GM_Combo(reg=self, w=w, vm=vm)
Exemple #6
0
 def GM_Lag_Regimes_Multi(self, y, x, w_i, regi_ids, cores=None,\
              yend=None, q=None, w_lags=1, lag_q=True,\
              robust=None, gwk=None, sig2n_k=False,cols2regi='all',\
              spat_diag=False, vm=False, name_y=None, name_x=None,\
              name_yend=None, name_q=None, name_regimes=None,\
              name_w=None, name_gwk=None, name_ds=None):
     pool = mp.Pool(cores)
     self.name_ds = USER.set_name_ds(name_ds)
     name_x = USER.set_name_x(name_x, x)
     name_yend.append(USER.set_name_yend_sp(name_y))
     self.name_w = USER.set_name_w(name_w, w_i)
     self.name_gwk = USER.set_name_w(name_gwk, gwk)
     results_p = {}
     for r in self.regimes_set:
         w_r = w_i[r].sparse
         results_p[r] = pool.apply_async(_work,args=(y,x,regi_ids,r,yend,q,w_r,w_lags,lag_q,robust,sig2n_k,self.name_ds,name_y,name_x,name_yend,name_q,self.name_w,name_regimes, ))
     self.kryd = 0
     self.kr = len(cols2regi) + 1
     self.kf = 0
     self.nr = len(self.regimes_set)
     self.name_x_r = name_x + name_yend
     self.name_regimes = name_regimes
     self.vm = np.zeros((self.nr*self.kr,self.nr*self.kr),float)
     self.betas = np.zeros((self.nr*self.kr,1),float)
     self.u = np.zeros((self.n,1),float)
     self.predy = np.zeros((self.n,1),float)
     self.predy_e = np.zeros((self.n,1),float)
     self.e_pred = np.zeros((self.n,1),float)
     pool.close()
     pool.join()
     results = {}
     self.name_y, self.name_x, self.name_yend, self.name_q, self.name_z, self.name_h = [],[],[],[],[],[]
     counter = 0
     for r in self.regimes_set:
         results[r] = results_p[r].get()
         results[r].predy_e, results[r].e_pred = sp_att(w_i[r],results[r].y,results[r].predy, results[r].yend[:,-1].reshape(results[r].n,1),results[r].betas[-1])
         results[r].w = w_i[r]
         self.vm[(counter*self.kr):((counter+1)*self.kr),(counter*self.kr):((counter+1)*self.kr)] = results[r].vm
         self.betas[(counter*self.kr):((counter+1)*self.kr),] = results[r].betas
         self.u[regi_ids[r],]=results[r].u
         self.predy[regi_ids[r],]=results[r].predy
         self.predy_e[regi_ids[r],]=results[r].predy_e
         self.e_pred[regi_ids[r],]=results[r].e_pred
         self.name_y += results[r].name_y
         self.name_x += results[r].name_x
         self.name_yend += results[r].name_yend
         self.name_q += results[r].name_q
         self.name_z += results[r].name_z
         self.name_h += results[r].name_h
         if r == self.regimes_set[0]:
             self.hac_var = np.zeros((self.n,results[r].h.shape[1]),float)
         self.hac_var[regi_ids[r],] = results[r].h                
         counter += 1
     self.multi = results
     if robust == 'hac':
         hac_multi(self,gwk,constant=True)
     self.chow = REGI.Chow(self)
     SUMMARY.GM_Lag_multi(reg=self, multireg=self.multi, vm=vm, spat_diag=spat_diag, regimes=True)
Exemple #7
0
 def sp_att_reg(self, w_i, regi_ids, wy):
     predy_e_r,e_pred_r = {},{}
     self.predy_e = np.zeros((self.n,1),float) 
     self.e_pred = np.zeros((self.n,1),float)
     counter = 1
     for r in self.regimes_set:
         lambd = self.betas[(self.kr-self.kryd)*self.nr+counter*self.kryd-1]
         self.predy_e[regi_ids[r],], self.e_pred[regi_ids[r],] = sp_att(w_i[r],\
                       self.y[regi_ids[r]],self.predy[regi_ids[r]],\
                       wy[regi_ids[r]],lambd)
         counter += 1
Exemple #8
0
 def sp_att_reg(self, w_i, regi_ids, wy):
     predy_e_r,e_pred_r = {},{}
     self.predy_e = np.zeros((self.n,1),float) 
     self.e_pred = np.zeros((self.n,1),float)
     counter = 1
     for r in self.regimes_set:
         self.rho = self.betas[(self.kr-self.kryd)*self.nr+self.kf-(self.yend.shape[1]-self.nr*self.kryd)+self.kryd*counter-1]
         self.predy_e[regi_ids[r],], self.e_pred[regi_ids[r],], warn = sp_att(w_i[r],\
                       self.y[regi_ids[r]],self.predy[regi_ids[r]],\
                       wy[regi_ids[r]],self.rho)
         counter += 1
Exemple #9
0
 def sp_att_reg(self, w_i, regi_ids, wy):
     predy_e_r, e_pred_r = {}, {}
     self.predy_e = np.zeros((self.n, 1), float)
     self.e_pred = np.zeros((self.n, 1), float)
     counter = 1
     for r in self.regimes_set:
         self.rho = self.betas[(self.kr - self.kryd) * self.nr + self.kf - (
             self.yend.shape[1] - self.nr * self.kryd) + self.kryd * counter - 1]
         self.predy_e[regi_ids[r], ], self.e_pred[regi_ids[r], ], warn = sp_att(w_i[r],
                                                                                self.y[regi_ids[r]], self.predy[
                                                                                    regi_ids[r]],
                                                                                wy[regi_ids[r]], self.rho)
         counter += 1
Exemple #10
0
    def __init__(self, y, x, equationID, yend=None, q=None, w=None,\
                 cores=None, sig2n_k=False, name_y=None, name_x=None, name_yend=None,\
                 name_q=None, name_equationID=None, name_ds=None, name_w=None, vm=False,\
                 w_lags=1, lag_q=True):

        ThreeSLS.__init__(self, y=y, x=x, equationID=equationID, yend=yend, q=q, w=w, cores=cores,\
                 sig2n_k=sig2n_k, name_y=name_y, name_x=name_x, name_yend=name_yend, name_q=name_q,\
                 name_equationID=name_equationID, name_ds=name_ds, name_w=name_w, vm=vm,\
                 w_lags=w_lags, lag_q=lag_q)

        for r in self.eq_set:
            self.multi[r].predy_e, self.multi[r].e_pred, warn = sp_att(w,self.multi[r].y,self.multi[r].predy,\
                          self.multi[r].yend[:,-1].reshape(self.n,1),self.multi[r].betas[-1])
            set_warn(self.multi[r], warn)
        
        title = "SPATIAL THREE-STAGE LEAST SQUARES - EQUATION "
        self.multi = USER.set_name_multi(self.multi,self.eq_set,name_equationID,y,x,name_y,name_x,name_ds,title,name_w,robust=None,endog=(yend,q,name_yend,name_q),sp_lag=(w_lags, lag_q))
        SUMMARY.GM_Lag_multi(reg=self, multireg=self.multi, vm=vm, spat_diag=False, sur=True)
Exemple #11
0
    def __init__(self, y, x, regimes, yend=None, q=None,\
                 w=None, w_lags=1, lag_q=True,\
                 robust=None, gwk=None, sig2n_k=False,\
                 spat_diag=False, constant_regi='many',\
                 cols2regi='all', regime_lag_sep=True, regime_err_sep=True,\
                 cores=None, vm=False, name_y=None, name_x=None,\
                 name_yend=None, name_q=None, name_regimes=None,\
                 name_w=None, name_gwk=None, name_ds=None):

        n = USER.check_arrays(y, x)
        USER.check_y(y, n)
        USER.check_weights(w, y)
        USER.check_robust(robust, gwk)
        USER.check_spat_diag(spat_diag, w)
        name_x = USER.set_name_x(name_x, x,constant=True)
        name_y = USER.set_name_y(name_y)
        name_yend = USER.set_name_yend(name_yend, yend)
        name_q = USER.set_name_q(name_q, q)
        name_q.extend(USER.set_name_q_sp(name_x, w_lags, name_q, lag_q, force_all=True))
        self.name_regimes = USER.set_name_ds(name_regimes)
        self.constant_regi=constant_regi
        self.n = n
        if cols2regi == 'all':
            if yend!=None:
                cols2regi = [True] * (x.shape[1]+yend.shape[1])
            else:
                cols2regi = [True] * (x.shape[1])     
        if regime_lag_sep == True:
            cols2regi += [True]
            self.regimes_set = list(set(regimes))
            self.regimes_set.sort()
            w_i,regi_ids,warn = REGI.w_regimes(w, regimes, self.regimes_set, transform=True, get_ids=True, min_n=len(cols2regi)+1)
            set_warn(self,warn)
            if not regime_err_sep:
                w = REGI.w_regimes_union(w, w_i, self.regimes_set)
        else:
            cols2regi += [False]
            if regime_err_sep == True:
                raise Exception, "All coefficients must vary accross regimes if regime_err_sep = True."            
        self.cols2regi = cols2regi
        if regime_lag_sep == True and regime_err_sep == True:
            if set(cols2regi) == set([True]):
                self.GM_Lag_Regimes_Multi(y, x, w_i, regi_ids,\
                 yend=yend, q=q, w_lags=w_lags, lag_q=lag_q, cores=cores,\
                 robust=robust, gwk=gwk, sig2n_k=sig2n_k, cols2regi=cols2regi,\
                 spat_diag=spat_diag, vm=vm, name_y=name_y, name_x=name_x,\
                 name_yend=name_yend, name_q=name_q, name_regimes=self.name_regimes,\
                 name_w=name_w, name_gwk=name_gwk, name_ds=name_ds)
            else:
                raise Exception, "All coefficients must vary accross regimes if regime_err_sep = True."
        else:
            yend2, q2 = set_endog(y, x, w, yend, q, w_lags, lag_q)
            name_yend.append(USER.set_name_yend_sp(name_y))
            TSLS_Regimes.__init__(self, y=y, x=x, yend=yend2, q=q2,\
                 regimes=regimes, w=w, robust=robust, gwk=gwk,\
                 sig2n_k=sig2n_k, spat_diag=spat_diag, vm=vm,\
                 constant_regi=constant_regi, cols2regi=cols2regi, name_y=name_y,\
                 name_x=name_x, name_yend=name_yend, name_q=name_q,\
                 name_regimes=name_regimes, name_w=name_w, name_gwk=name_gwk,\
                 name_ds=name_ds,summ=False)
            if regime_lag_sep:
                self.sp_att_reg(w_i, regi_ids, yend2[:,-1].reshape(self.n,1))
            else:
                self.predy_e, self.e_pred = sp_att(w,self.y,self.predy,\
                          yend2[:,-1].reshape(self.n,1),self.betas[-1])
            self.regime_lag_sep=regime_lag_sep
            self.title = "SPATIAL TWO STAGE LEAST SQUARES - REGIMES"
            SUMMARY.GM_Lag(reg=self, w=w, vm=vm, spat_diag=spat_diag, regimes=True)
Exemple #12
0
    def GM_Lag_Regimes_Multi(self,
                             y,
                             x,
                             w_i,
                             w,
                             regi_ids,
                             cores=False,
                             yend=None,
                             q=None,
                             w_lags=1,
                             lag_q=True,
                             robust=None,
                             gwk=None,
                             sig2n_k=False,
                             cols2regi='all',
                             spat_diag=False,
                             vm=False,
                             name_y=None,
                             name_x=None,
                             name_yend=None,
                             name_q=None,
                             name_regimes=None,
                             name_w=None,
                             name_gwk=None,
                             name_ds=None):
        #        pool = mp.Pool(cores)
        self.name_ds = USER.set_name_ds(name_ds)
        name_x = USER.set_name_x(name_x, x)
        name_yend.append(USER.set_name_yend_sp(name_y))
        self.name_w = USER.set_name_w(name_w, w_i)
        self.name_gwk = USER.set_name_w(name_gwk, gwk)
        results_p = {}
        """
        for r in self.regimes_set:
            w_r = w_i[r].sparse
            if system() == 'Windows':
                is_win = True
                results_p[r] = _work(*(y,x,regi_ids,r,yend,q,w_r,w_lags,lag_q,robust,sig2n_k,self.name_ds,name_y,name_x,name_yend,name_q,self.name_w,name_regimes))
            else:                
                results_p[r] = pool.apply_async(_work,args=(y,x,regi_ids,r,yend,q,w_r,w_lags,lag_q,robust,sig2n_k,self.name_ds,name_y,name_x,name_yend,name_q,self.name_w,name_regimes, ))
                is_win = False
        """
        for r in self.regimes_set:
            w_r = w_i[r].sparse
            if cores:
                pool = mp.Pool(None)
                results_p[r] = pool.apply_async(_work,
                                                args=(
                                                    y,
                                                    x,
                                                    regi_ids,
                                                    r,
                                                    yend,
                                                    q,
                                                    w_r,
                                                    w_lags,
                                                    lag_q,
                                                    robust,
                                                    sig2n_k,
                                                    self.name_ds,
                                                    name_y,
                                                    name_x,
                                                    name_yend,
                                                    name_q,
                                                    self.name_w,
                                                    name_regimes,
                                                ))
            else:
                results_p[r] = _work(*(y, x, regi_ids, r, yend, q, w_r, w_lags,
                                       lag_q, robust, sig2n_k, self.name_ds,
                                       name_y, name_x, name_yend, name_q,
                                       self.name_w, name_regimes))

        self.kryd = 0
        self.kr = len(cols2regi) + 1
        self.kf = 0
        self.nr = len(self.regimes_set)
        self.name_x_r = name_x + name_yend
        self.name_regimes = name_regimes
        self.vm = np.zeros((self.nr * self.kr, self.nr * self.kr), float)
        self.betas = np.zeros((self.nr * self.kr, 1), float)
        self.u = np.zeros((self.n, 1), float)
        self.predy = np.zeros((self.n, 1), float)
        self.predy_e = np.zeros((self.n, 1), float)
        self.e_pred = np.zeros((self.n, 1), float)
        """
        if not is_win:
            pool.close()
            pool.join()
        """
        if cores:
            pool.close()
            pool.join()
        results = {}
        self.name_y, self.name_x, self.name_yend, self.name_q, self.name_z, self.name_h = [
        ], [], [], [], [], []
        counter = 0
        for r in self.regimes_set:
            """
            if is_win:
                results[r] = results_p[r]
            else:
                results[r] = results_p[r].get()
            """
            if not cores:
                results[r] = results_p[r]
            else:
                results[r] = results_p[r].get()
            results[r].predy_e, results[r].e_pred, warn = sp_att(
                w_i[r], results[r].y, results[r].predy,
                results[r].yend[:, -1].reshape(results[r].n,
                                               1), results[r].rho)
            set_warn(results[r], warn)
            results[r].w = w_i[r]
            self.vm[(counter * self.kr):((counter + 1) * self.kr),
                    (counter * self.kr):((counter + 1) *
                                         self.kr)] = results[r].vm
            self.betas[(counter * self.kr):((counter + 1) *
                                            self.kr), ] = results[r].betas
            self.u[regi_ids[r], ] = results[r].u
            self.predy[regi_ids[r], ] = results[r].predy
            self.predy_e[regi_ids[r], ] = results[r].predy_e
            self.e_pred[regi_ids[r], ] = results[r].e_pred
            self.name_y += results[r].name_y
            self.name_x += results[r].name_x
            self.name_yend += results[r].name_yend
            self.name_q += results[r].name_q
            self.name_z += results[r].name_z
            self.name_h += results[r].name_h
            if r == self.regimes_set[0]:
                self.hac_var = np.zeros((self.n, results[r].h.shape[1]), float)
            self.hac_var[regi_ids[r], ] = results[r].h
            counter += 1
        self.multi = results
        if robust == 'hac':
            hac_multi(self, gwk, constant=True)
        if robust == 'ogmm':
            set_warn(
                self,
                "Residuals treated as homoskedastic for the purpose of diagnostics."
            )
        self.chow = REGI.Chow(self)
        if spat_diag:
            pass
            #self._get_spat_diag_props(y, x, w, yend, q, w_lags, lag_q)
        SUMMARY.GM_Lag_multi(reg=self,
                             multireg=self.multi,
                             vm=vm,
                             spat_diag=spat_diag,
                             regimes=True,
                             w=w)
Exemple #13
0
    def __init__(self,
                 y,
                 x,
                 regimes,
                 yend=None,
                 q=None,
                 w=None,
                 w_lags=1,
                 lag_q=True,
                 robust=None,
                 gwk=None,
                 sig2n_k=False,
                 spat_diag=False,
                 constant_regi='many',
                 cols2regi='all',
                 regime_lag_sep=False,
                 regime_err_sep=True,
                 cores=False,
                 vm=False,
                 name_y=None,
                 name_x=None,
                 name_yend=None,
                 name_q=None,
                 name_regimes=None,
                 name_w=None,
                 name_gwk=None,
                 name_ds=None):

        n = USER.check_arrays(y, x)
        USER.check_y(y, n)
        USER.check_weights(w, y, w_required=True)
        USER.check_robust(robust, gwk)
        USER.check_spat_diag(spat_diag, w)
        name_x = USER.set_name_x(name_x, x, constant=True)
        name_y = USER.set_name_y(name_y)
        name_yend = USER.set_name_yend(name_yend, yend)
        name_q = USER.set_name_q(name_q, q)
        name_q.extend(
            USER.set_name_q_sp(name_x, w_lags, name_q, lag_q, force_all=True))
        self.name_regimes = USER.set_name_ds(name_regimes)
        self.constant_regi = constant_regi
        self.n = n
        cols2regi = REGI.check_cols2regi(constant_regi,
                                         cols2regi,
                                         x,
                                         yend=yend,
                                         add_cons=False)
        self.cols2regi = cols2regi
        self.regimes_set = REGI._get_regimes_set(regimes)
        self.regimes = regimes
        USER.check_regimes(self.regimes_set, self.n, x.shape[1])
        if regime_err_sep == True and robust == 'hac':
            set_warn(
                self,
                "Error by regimes is incompatible with HAC estimation for Spatial Lag models. Hence, error and lag by regimes have been disabled for this model."
            )
            regime_err_sep = False
            regime_lag_sep = False
        self.regime_err_sep = regime_err_sep
        self.regime_lag_sep = regime_lag_sep
        if regime_lag_sep == True:
            if not regime_err_sep:
                raise Exception, "regime_err_sep must be True when regime_lag_sep=True."
            cols2regi += [True]
            w_i, regi_ids, warn = REGI.w_regimes(w,
                                                 regimes,
                                                 self.regimes_set,
                                                 transform=True,
                                                 get_ids=True,
                                                 min_n=len(cols2regi) + 1)
            set_warn(self, warn)

        else:
            cols2regi += [False]

        if regime_err_sep == True and set(cols2regi) == set(
            [True]) and constant_regi == 'many':
            self.y = y
            self.GM_Lag_Regimes_Multi(y,
                                      x,
                                      w_i,
                                      w,
                                      regi_ids,
                                      yend=yend,
                                      q=q,
                                      w_lags=w_lags,
                                      lag_q=lag_q,
                                      cores=cores,
                                      robust=robust,
                                      gwk=gwk,
                                      sig2n_k=sig2n_k,
                                      cols2regi=cols2regi,
                                      spat_diag=spat_diag,
                                      vm=vm,
                                      name_y=name_y,
                                      name_x=name_x,
                                      name_yend=name_yend,
                                      name_q=name_q,
                                      name_regimes=self.name_regimes,
                                      name_w=name_w,
                                      name_gwk=name_gwk,
                                      name_ds=name_ds)
        else:
            if regime_lag_sep == True:
                w = REGI.w_regimes_union(w, w_i, self.regimes_set)
            yend2, q2 = set_endog(y, x, w, yend, q, w_lags, lag_q)
            name_yend.append(USER.set_name_yend_sp(name_y))
            TSLS_Regimes.__init__(self,
                                  y=y,
                                  x=x,
                                  yend=yend2,
                                  q=q2,
                                  regimes=regimes,
                                  w=w,
                                  robust=robust,
                                  gwk=gwk,
                                  sig2n_k=sig2n_k,
                                  spat_diag=spat_diag,
                                  vm=vm,
                                  constant_regi=constant_regi,
                                  cols2regi=cols2regi,
                                  regime_err_sep=regime_err_sep,
                                  name_y=name_y,
                                  name_x=name_x,
                                  name_yend=name_yend,
                                  name_q=name_q,
                                  name_regimes=name_regimes,
                                  name_w=name_w,
                                  name_gwk=name_gwk,
                                  name_ds=name_ds,
                                  summ=False)
            if regime_lag_sep:
                self.sp_att_reg(w_i, regi_ids, yend2[:, -1].reshape(self.n, 1))
            else:
                self.rho = self.betas[-1]
                self.predy_e, self.e_pred, warn = sp_att(
                    w, self.y, self.predy, yend2[:, -1].reshape(self.n, 1),
                    self.rho)
                set_warn(self, warn)
            self.regime_lag_sep = regime_lag_sep
            self.title = "SPATIAL " + self.title
            SUMMARY.GM_Lag(reg=self,
                           w=w,
                           vm=vm,
                           spat_diag=spat_diag,
                           regimes=True)
Exemple #14
0
    def __init__(self, y, x, regimes, yend=None, q=None,\
                 w=None, w_lags=1, lag_q=True,\
                 robust=None, gwk=None, sig2n_k=False,\
                 spat_diag=False, constant_regi='many',\
                 cols2regi='all', regime_lag_sep=False, regime_err_sep=True,\
                 cores=None, vm=False, name_y=None, name_x=None,\
                 name_yend=None, name_q=None, name_regimes=None,\
                 name_w=None, name_gwk=None, name_ds=None):

        n = USER.check_arrays(y, x)
        USER.check_y(y, n)
        USER.check_weights(w, y, w_required=True)
        USER.check_robust(robust, gwk)
        USER.check_spat_diag(spat_diag, w)
        name_x = USER.set_name_x(name_x, x,constant=True)
        name_y = USER.set_name_y(name_y)
        name_yend = USER.set_name_yend(name_yend, yend)
        name_q = USER.set_name_q(name_q, q)
        name_q.extend(USER.set_name_q_sp(name_x, w_lags, name_q, lag_q, force_all=True))
        self.name_regimes = USER.set_name_ds(name_regimes)
        self.constant_regi=constant_regi
        self.n = n
        cols2regi = REGI.check_cols2regi(constant_regi, cols2regi, x, yend=yend, add_cons=False)    
        self.cols2regi = cols2regi
        self.regimes_set = REGI._get_regimes_set(regimes)
        self.regimes = regimes
        USER.check_regimes(self.regimes_set)
        self.regime_err_sep = regime_err_sep        
        self.regime_lag_sep = regime_lag_sep        
        if regime_lag_sep == True:
            if not regime_err_sep:
                raise Exception, "regime_err_sep must be True when regime_lag_sep=True."
            cols2regi += [True]
            w_i,regi_ids,warn = REGI.w_regimes(w, regimes, self.regimes_set, transform=True, get_ids=True, min_n=len(cols2regi)+1)
            set_warn(self,warn)

        else:
            cols2regi += [False]            

        if regime_err_sep == True and set(cols2regi) == set([True]) and constant_regi == 'many':
            self.y = y
            self.GM_Lag_Regimes_Multi(y, x, w_i, regi_ids,\
                 yend=yend, q=q, w_lags=w_lags, lag_q=lag_q, cores=cores,\
                 robust=robust, gwk=gwk, sig2n_k=sig2n_k, cols2regi=cols2regi,\
                 spat_diag=spat_diag, vm=vm, name_y=name_y, name_x=name_x,\
                 name_yend=name_yend, name_q=name_q, name_regimes=self.name_regimes,\
                 name_w=name_w, name_gwk=name_gwk, name_ds=name_ds)
        else:
            if regime_lag_sep == True:
                w = REGI.w_regimes_union(w, w_i, self.regimes_set)
            yend2, q2 = set_endog(y, x, w, yend, q, w_lags, lag_q)
            name_yend.append(USER.set_name_yend_sp(name_y))
            TSLS_Regimes.__init__(self, y=y, x=x, yend=yend2, q=q2,\
                 regimes=regimes, w=w, robust=robust, gwk=gwk,\
                 sig2n_k=sig2n_k, spat_diag=spat_diag, vm=vm,\
                 constant_regi=constant_regi, cols2regi=cols2regi, regime_err_sep=regime_err_sep,\
                 name_y=name_y, name_x=name_x, name_yend=name_yend, name_q=name_q,\
                 name_regimes=name_regimes, name_w=name_w, name_gwk=name_gwk,\
                 name_ds=name_ds,summ=False)
            if regime_lag_sep:
                self.sp_att_reg(w_i, regi_ids, yend2[:,-1].reshape(self.n,1))
            else:
                self.predy_e, self.e_pred, warn = sp_att(w,self.y,self.predy,\
                          yend2[:,-1].reshape(self.n,1),self.betas[-1])
                set_warn(self,warn)
            self.regime_lag_sep=regime_lag_sep
            if regime_err_sep == True:
                self.title = "SPATIAL TWO STAGE LEAST SQUARES - REGIMES (Group-wise heteroskedasticity)"
            else:
                self.title = "SPATIAL TWO STAGE LEAST SQUARES - REGIMES"
            SUMMARY.GM_Lag(reg=self, w=w, vm=vm, spat_diag=spat_diag, regimes=True)
Exemple #15
0
    def __init__(
        self,
        y,
        x,
        regimes,
        yend=None,
        q=None,
        w=None,
        w_lags=1,
        lag_q=True,
        robust=None,
        gwk=None,
        sig2n_k=False,
        spat_diag=False,
        constant_regi="many",
        cols2regi="all",
        regime_lag_sep=False,
        regime_err_sep=True,
        cores=False,
        vm=False,
        name_y=None,
        name_x=None,
        name_yend=None,
        name_q=None,
        name_regimes=None,
        name_w=None,
        name_gwk=None,
        name_ds=None,
    ):

        n = USER.check_arrays(y, x)
        USER.check_y(y, n)
        USER.check_weights(w, y, w_required=True)
        USER.check_robust(robust, gwk)
        USER.check_spat_diag(spat_diag, w)
        name_x = USER.set_name_x(name_x, x, constant=True)
        name_y = USER.set_name_y(name_y)
        name_yend = USER.set_name_yend(name_yend, yend)
        name_q = USER.set_name_q(name_q, q)
        name_q.extend(USER.set_name_q_sp(name_x, w_lags, name_q, lag_q, force_all=True))
        self.name_regimes = USER.set_name_ds(name_regimes)
        self.constant_regi = constant_regi
        self.n = n
        cols2regi = REGI.check_cols2regi(constant_regi, cols2regi, x, yend=yend, add_cons=False)
        self.cols2regi = cols2regi
        self.regimes_set = REGI._get_regimes_set(regimes)
        self.regimes = regimes
        USER.check_regimes(self.regimes_set, self.n, x.shape[1])
        if regime_err_sep == True and robust == "hac":
            set_warn(
                self,
                "Error by regimes is incompatible with HAC estimation for Spatial Lag models. Hence, error and lag by regimes have been disabled for this model.",
            )
            regime_err_sep = False
            regime_lag_sep = False
        self.regime_err_sep = regime_err_sep
        self.regime_lag_sep = regime_lag_sep
        if regime_lag_sep == True:
            if not regime_err_sep:
                raise Exception, "regime_err_sep must be True when regime_lag_sep=True."
            cols2regi += [True]
            w_i, regi_ids, warn = REGI.w_regimes(
                w, regimes, self.regimes_set, transform=True, get_ids=True, min_n=len(cols2regi) + 1
            )
            set_warn(self, warn)

        else:
            cols2regi += [False]

        if regime_err_sep == True and set(cols2regi) == set([True]) and constant_regi == "many":
            self.y = y
            self.GM_Lag_Regimes_Multi(
                y,
                x,
                w_i,
                w,
                regi_ids,
                yend=yend,
                q=q,
                w_lags=w_lags,
                lag_q=lag_q,
                cores=cores,
                robust=robust,
                gwk=gwk,
                sig2n_k=sig2n_k,
                cols2regi=cols2regi,
                spat_diag=spat_diag,
                vm=vm,
                name_y=name_y,
                name_x=name_x,
                name_yend=name_yend,
                name_q=name_q,
                name_regimes=self.name_regimes,
                name_w=name_w,
                name_gwk=name_gwk,
                name_ds=name_ds,
            )
        else:
            if regime_lag_sep == True:
                w = REGI.w_regimes_union(w, w_i, self.regimes_set)
            yend2, q2 = set_endog(y, x, w, yend, q, w_lags, lag_q)
            name_yend.append(USER.set_name_yend_sp(name_y))
            TSLS_Regimes.__init__(
                self,
                y=y,
                x=x,
                yend=yend2,
                q=q2,
                regimes=regimes,
                w=w,
                robust=robust,
                gwk=gwk,
                sig2n_k=sig2n_k,
                spat_diag=spat_diag,
                vm=vm,
                constant_regi=constant_regi,
                cols2regi=cols2regi,
                regime_err_sep=regime_err_sep,
                name_y=name_y,
                name_x=name_x,
                name_yend=name_yend,
                name_q=name_q,
                name_regimes=name_regimes,
                name_w=name_w,
                name_gwk=name_gwk,
                name_ds=name_ds,
                summ=False,
            )
            if regime_lag_sep:
                self.sp_att_reg(w_i, regi_ids, yend2[:, -1].reshape(self.n, 1))
            else:
                self.rho = self.betas[-1]
                self.predy_e, self.e_pred, warn = sp_att(
                    w, self.y, self.predy, yend2[:, -1].reshape(self.n, 1), self.rho
                )
                set_warn(self, warn)
            self.regime_lag_sep = regime_lag_sep
            self.title = "SPATIAL " + self.title
            SUMMARY.GM_Lag(reg=self, w=w, vm=vm, spat_diag=spat_diag, regimes=True)
Exemple #16
0
    def GM_Lag_Regimes_Multi(
        self,
        y,
        x,
        w_i,
        w,
        regi_ids,
        cores=False,
        yend=None,
        q=None,
        w_lags=1,
        lag_q=True,
        robust=None,
        gwk=None,
        sig2n_k=False,
        cols2regi="all",
        spat_diag=False,
        vm=False,
        name_y=None,
        name_x=None,
        name_yend=None,
        name_q=None,
        name_regimes=None,
        name_w=None,
        name_gwk=None,
        name_ds=None,
    ):
        #        pool = mp.Pool(cores)
        self.name_ds = USER.set_name_ds(name_ds)
        name_x = USER.set_name_x(name_x, x)
        name_yend.append(USER.set_name_yend_sp(name_y))
        self.name_w = USER.set_name_w(name_w, w_i)
        self.name_gwk = USER.set_name_w(name_gwk, gwk)
        results_p = {}
        """
        for r in self.regimes_set:
            w_r = w_i[r].sparse
            if system() == 'Windows':
                is_win = True
                results_p[r] = _work(*(y,x,regi_ids,r,yend,q,w_r,w_lags,lag_q,robust,sig2n_k,self.name_ds,name_y,name_x,name_yend,name_q,self.name_w,name_regimes))
            else:                
                results_p[r] = pool.apply_async(_work,args=(y,x,regi_ids,r,yend,q,w_r,w_lags,lag_q,robust,sig2n_k,self.name_ds,name_y,name_x,name_yend,name_q,self.name_w,name_regimes, ))
                is_win = False
        """
        for r in self.regimes_set:
            w_r = w_i[r].sparse
            if cores:
                pool = mp.Pool(None)
                results_p[r] = pool.apply_async(
                    _work,
                    args=(
                        y,
                        x,
                        regi_ids,
                        r,
                        yend,
                        q,
                        w_r,
                        w_lags,
                        lag_q,
                        robust,
                        sig2n_k,
                        self.name_ds,
                        name_y,
                        name_x,
                        name_yend,
                        name_q,
                        self.name_w,
                        name_regimes,
                    ),
                )
            else:
                results_p[r] = _work(
                    *(
                        y,
                        x,
                        regi_ids,
                        r,
                        yend,
                        q,
                        w_r,
                        w_lags,
                        lag_q,
                        robust,
                        sig2n_k,
                        self.name_ds,
                        name_y,
                        name_x,
                        name_yend,
                        name_q,
                        self.name_w,
                        name_regimes,
                    )
                )

        self.kryd = 0
        self.kr = len(cols2regi) + 1
        self.kf = 0
        self.nr = len(self.regimes_set)
        self.name_x_r = name_x + name_yend
        self.name_regimes = name_regimes
        self.vm = np.zeros((self.nr * self.kr, self.nr * self.kr), float)
        self.betas = np.zeros((self.nr * self.kr, 1), float)
        self.u = np.zeros((self.n, 1), float)
        self.predy = np.zeros((self.n, 1), float)
        self.predy_e = np.zeros((self.n, 1), float)
        self.e_pred = np.zeros((self.n, 1), float)
        """
        if not is_win:
            pool.close()
            pool.join()
        """
        if cores:
            pool.close()
            pool.join()
        results = {}
        self.name_y, self.name_x, self.name_yend, self.name_q, self.name_z, self.name_h = [], [], [], [], [], []
        counter = 0
        for r in self.regimes_set:
            """
            if is_win:
                results[r] = results_p[r]
            else:
                results[r] = results_p[r].get()
            """
            if not cores:
                results[r] = results_p[r]
            else:
                results[r] = results_p[r].get()
            results[r].predy_e, results[r].e_pred, warn = sp_att(
                w_i[r], results[r].y, results[r].predy, results[r].yend[:, -1].reshape(results[r].n, 1), results[r].rho
            )
            set_warn(results[r], warn)
            results[r].w = w_i[r]
            self.vm[
                (counter * self.kr) : ((counter + 1) * self.kr), (counter * self.kr) : ((counter + 1) * self.kr)
            ] = results[r].vm
            self.betas[(counter * self.kr) : ((counter + 1) * self.kr),] = results[r].betas
            self.u[regi_ids[r],] = results[r].u
            self.predy[regi_ids[r],] = results[r].predy
            self.predy_e[regi_ids[r],] = results[r].predy_e
            self.e_pred[regi_ids[r],] = results[r].e_pred
            self.name_y += results[r].name_y
            self.name_x += results[r].name_x
            self.name_yend += results[r].name_yend
            self.name_q += results[r].name_q
            self.name_z += results[r].name_z
            self.name_h += results[r].name_h
            if r == self.regimes_set[0]:
                self.hac_var = np.zeros((self.n, results[r].h.shape[1]), float)
            self.hac_var[regi_ids[r],] = results[r].h
            counter += 1
        self.multi = results
        if robust == "hac":
            hac_multi(self, gwk, constant=True)
        if robust == "ogmm":
            set_warn(self, "Residuals treated as homoskedastic for the purpose of diagnostics.")
        self.chow = REGI.Chow(self)
        if spat_diag:
            pass
            # self._get_spat_diag_props(y, x, w, yend, q, w_lags, lag_q)
        SUMMARY.GM_Lag_multi(reg=self, multireg=self.multi, vm=vm, spat_diag=spat_diag, regimes=True, w=w)
Exemple #17
0
    def __init__(self, y, x, regimes, yend=None, q=None,\
                 w=None, w_lags=1, lag_q=True,\
                 robust=None, gwk=None, sig2n_k=False,\
                 spat_diag=False, constant_regi='many',\
                 cols2regi='all', regime_lag_sep=False, regime_err_sep=True,\
                 cores=None, vm=False, name_y=None, name_x=None,\
                 name_yend=None, name_q=None, name_regimes=None,\
                 name_w=None, name_gwk=None, name_ds=None):

        n = USER.check_arrays(y, x)
        USER.check_y(y, n)
        USER.check_weights(w, y, w_required=True)
        USER.check_robust(robust, gwk)
        USER.check_spat_diag(spat_diag, w)
        name_x = USER.set_name_x(name_x, x, constant=True)
        name_y = USER.set_name_y(name_y)
        name_yend = USER.set_name_yend(name_yend, yend)
        name_q = USER.set_name_q(name_q, q)
        name_q.extend(
            USER.set_name_q_sp(name_x, w_lags, name_q, lag_q, force_all=True))
        self.name_regimes = USER.set_name_ds(name_regimes)
        self.constant_regi = constant_regi
        self.n = n
        cols2regi = REGI.check_cols2regi(constant_regi,
                                         cols2regi,
                                         x,
                                         yend=yend,
                                         add_cons=False)
        self.cols2regi = cols2regi
        self.regimes_set = REGI._get_regimes_set(regimes)
        self.regimes = regimes
        USER.check_regimes(self.regimes_set)
        self.regime_err_sep = regime_err_sep
        self.regime_lag_sep = regime_lag_sep
        if regime_lag_sep == True:
            if not regime_err_sep:
                raise Exception, "regime_err_sep must be True when regime_lag_sep=True."
            cols2regi += [True]
            w_i, regi_ids, warn = REGI.w_regimes(w,
                                                 regimes,
                                                 self.regimes_set,
                                                 transform=True,
                                                 get_ids=True,
                                                 min_n=len(cols2regi) + 1)
            set_warn(self, warn)

        else:
            cols2regi += [False]

        if regime_err_sep == True and set(cols2regi) == set(
            [True]) and constant_regi == 'many':
            self.y = y
            self.GM_Lag_Regimes_Multi(y, x, w_i, regi_ids,\
                 yend=yend, q=q, w_lags=w_lags, lag_q=lag_q, cores=cores,\
                 robust=robust, gwk=gwk, sig2n_k=sig2n_k, cols2regi=cols2regi,\
                 spat_diag=spat_diag, vm=vm, name_y=name_y, name_x=name_x,\
                 name_yend=name_yend, name_q=name_q, name_regimes=self.name_regimes,\
                 name_w=name_w, name_gwk=name_gwk, name_ds=name_ds)
        else:
            if regime_lag_sep == True:
                w = REGI.w_regimes_union(w, w_i, self.regimes_set)
            yend2, q2 = set_endog(y, x, w, yend, q, w_lags, lag_q)
            name_yend.append(USER.set_name_yend_sp(name_y))
            TSLS_Regimes.__init__(self, y=y, x=x, yend=yend2, q=q2,\
                 regimes=regimes, w=w, robust=robust, gwk=gwk,\
                 sig2n_k=sig2n_k, spat_diag=spat_diag, vm=vm,\
                 constant_regi=constant_regi, cols2regi=cols2regi, regime_err_sep=regime_err_sep,\
                 name_y=name_y, name_x=name_x, name_yend=name_yend, name_q=name_q,\
                 name_regimes=name_regimes, name_w=name_w, name_gwk=name_gwk,\
                 name_ds=name_ds,summ=False)
            if regime_lag_sep:
                self.sp_att_reg(w_i, regi_ids, yend2[:, -1].reshape(self.n, 1))
            else:
                self.predy_e, self.e_pred, warn = sp_att(w,self.y,self.predy,\
                          yend2[:,-1].reshape(self.n,1),self.betas[-1])
                set_warn(self, warn)
            self.regime_lag_sep = regime_lag_sep
            if regime_err_sep == True:
                self.title = "SPATIAL TWO STAGE LEAST SQUARES - REGIMES (Group-wise heteroskedasticity)"
            else:
                self.title = "SPATIAL TWO STAGE LEAST SQUARES - REGIMES"
            SUMMARY.GM_Lag(reg=self,
                           w=w,
                           vm=vm,
                           spat_diag=spat_diag,
                           regimes=True)