Exemple #1
0
    def __init__(self, x, y, regime, permutations=0):

        x = np.asarray(x)
        y = np.asarray(y)
        res = Tau_Local(x, y)
        self.n = res.n
        self.S = res.S

        reg = np.array(regime).flatten()
        ur = np.unique(reg).tolist()
        k = len(ur)
        P = np.zeros((k, self.n))
        for i, r in enumerate(reg):
            P[ur.index(r), i] = 1  # construct P matrix

        w = weights.block_weights(regime)
        w.transform = 'b'
        W = w.full()[0]
        WH = np.ones((self.n, self.n)) - np.eye(self.n) - W

        # inter and intraregional decomposition of Tau for the observed value

        self.tau_reg = self._calc(W, WH, P, self.S)

        if permutations > 0:
            tau_reg_sim = np.zeros((permutations, k, k))
            larger = np.zeros((k, k))
            smaller = np.zeros((k, k))
            rids = np.arange(len(x))
            for i in range(permutations):
                np.random.shuffle(rids)
                res = Tau_Local(x[rids], y[rids])
                tau_reg_sim[i] = self._calc(W, WH, P, res.S)
                larger += np.greater_equal(tau_reg_sim[i], self.tau_reg)
                smaller += np.less_equal(tau_reg_sim[i], self.tau_reg)

            m = np.less(smaller, larger)
            pvalues = (1 + m * smaller +
                       (1 - m) * larger) / (1. + permutations)
            self.tau_reg_sim = tau_reg_sim
            self.tau_reg_pvalues = pvalues
Exemple #2
0
def kendallTau(data_df, quantilePolygon):
    caseWeekly_unstack = data_df['CasesWeekly'].unstack(level=0)

    zip_codes = quantilePolygon
    data_df_zipGPD = zip_codes.merge(caseWeekly_unstack,
                                     left_on='zip',
                                     right_on=caseWeekly_unstack.index)
    # print(data_df_zipGPD)
    W = ps.lib.weights.Queen(data_df_zipGPD.geometry)
    W.transform = 'R'
    weeks = idx_weekNumber = data_df.index.get_level_values('Week Number')
    weeks = np.unique(weeks)
    valArray = data_df_zipGPD[weeks].to_numpy()
    valArray_fillNan = bfill(valArray).T
    valArray_fillNan[np.isnan(valArray_fillNan)] = 0

    data_df_zipGPD_bfill = data_df_zipGPD.fillna(method='bfill', axis=1)
    data_df_zipGPD_bfill[weeks] = data_df_zipGPD_bfill[weeks].fillna(0)
    print(data_df_zipGPD_bfill.isna().sum())
    data_df_zipGPD_bfill["idxPlus"] = data_df_zipGPD_bfill["idx"].apply(
        lambda x: str(x[0]) + "-" + str(x[1]))

    plt.figure()
    plt.rcParams.update({'font.size': 30})
    ax = data_df_zipGPD_bfill.plot(column="idxPlus", figsize=(20, 20))
    plt.title("pop_quantile-5")
    # ax=showVector(zs_zip,'quantile')
    data_df_zipGPD_bfill.apply(lambda x: ax.annotate(
        s=x["idxPlus"], xy=x.geometry.centroid.coords[0], ha='center'),
                               axis=1)

    #Classic Kendall’s tau
    tau = giddy.rank.Tau(data_df_zipGPD_bfill[10], data_df_zipGPD_bfill[19])
    print("_" * 50, "Classic Kendall’s tau")
    #−1≤τ≤1 . Smaller τ indicates higher exchange mobility
    print("tau.concordant:", tau.concordant)
    print("tau.discordant:", tau.discordant)
    print("tau.tau:", tau.tau)
    print("tau.tau_p:", tau.tau_p)

    #Spatial Kendall’s tau
    w_quantile = block_weights(data_df_zipGPD_bfill["idxPlus"])
    np.random.seed(12345)
    tau_w = giddy.rank.SpatialTau(data_df_zipGPD_bfill[10],
                                  data_df_zipGPD_bfill[19], w_quantile, 999)
    print("_" * 50, "Spatial Kendall’s tau")
    print("tau_w.concordant:", tau_w.concordant)
    print("tau_w.concordant_spatial:", tau_w.concordant_spatial)
    print("tau_w.discordant:", tau_w.discordant)
    print("tau_w.discordant_spatial:", tau_w.discordant_spatial)
    print("tau_w.tau_spatial:", tau_w.tau_spatial)
    print("tau_w.tau_spatial_psim:", tau_w.tau_spatial_psim)

    #Inter- and Intra-regional decomposition of Kendall’s tau
    np.random.seed(12345)
    tau_w = giddy.rank.Tau_Regional(data_df_zipGPD_bfill[10],
                                    data_df_zipGPD_bfill[19],
                                    data_df_zipGPD_bfill["idxPlus"], 999)
    print("_" * 50, "Inter- and Intra-regional decomposition of Kendall’s tau")
    print("tau_w.tau_reg:", tau_w.tau_reg)
    print("tau_w.tau_reg_pvalues:", tau_w.tau_reg_pvalues)
    print("tau_w.tau_reg * (tau_w.tau_reg_pvalues<0.05):",
          tau_w.tau_reg * (tau_w.tau_reg_pvalues < 0.05))

    #Local Kendall’s tau
    tau_r = giddy.rank.Tau_Local(data_df_zipGPD_bfill[10],
                                 data_df_zipGPD_bfill[19])
    LocalKendallTau = pd.DataFrame({
        "STATE_NAME":
        data_df_zipGPD_bfill['zip'].tolist(),
        "$\\tau_r$":
        tau_r.tau_local
    }).head()
    print("Local Kendall’s tau:", LocalKendallTau)

    #Local indicator of mobility association-LIMA
    #1-Neighbor set LIMA
    data_df_zipGPD_bfill_dropZero = data_df_zipGPD_bfill.loc[~(
        data_df_zipGPD_bfill[weeks] == 0).all(axis=1)]
    # w_quantile_dropZero=block_weights(data_df_zipGPD_bfill_dropZero["idxPlus"]) #prompt mistake divide by zero
    W_ = ps.lib.weights.Queen(data_df_zipGPD_bfill_dropZero.geometry)
    W_.transform = 'R'
    print(data_df_zipGPD_bfill.shape, data_df_zipGPD_bfill_dropZero.shape)
    tau_wr = giddy.rank.Tau_Local_Neighbor(data_df_zipGPD_bfill_dropZero[10],
                                           data_df_zipGPD_bfill_dropZero[19],
                                           W_, 999)
    # print("tau_wr.tau_ln:",tau_wr.tau_ln)

    plt.figure()
    data_df_zipGPD_bfill_dropZero["tau_ln"] = tau_wr.tau_ln
    fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(20, 20))
    ln_map = data_df_zipGPD_bfill_dropZero.plot(ax=ax,
                                                column="tau_ln",
                                                cmap='coolwarm',
                                                scheme='equal_interval',
                                                legend=True)
    leg = ax.get_legend()
    leg.set_bbox_to_anchor((0.8, 0.15, 0.16, 0.2))
    ln_map.set_title("Neighbor set LIMA for Covid-19",
                     fontdict={"fontsize": 20})
    ax.set_axis_off()

    print("tau_wr.tau_ln_pvalues:", tau_wr.tau_ln_pvalues)
    sig_wr = tau_wr.tau_ln * (tau_wr.tau_ln_pvalues < 0.05)
    print("sig_wr :", sig_wr)

    plt.figure()
    data_df_zipGPD_bfill_dropZero["sig_wr"] = sig_wr
    fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(20, 20))
    data_df_zipGPD_bfill_dropZero[data_df_zipGPD_bfill_dropZero["sig_wr"] ==
                                  0].plot(ax=ax,
                                          color='white',
                                          edgecolor='black')
    sig_ln_map = data_df_zipGPD_bfill_dropZero[
        data_df_zipGPD_bfill_dropZero["sig_wr"] != 0].plot(
            ax=ax,
            column="sig_wr",
            cmap='coolwarm',
            scheme='equal_interval',
            legend=True)
    leg = ax.get_legend()
    leg.set_bbox_to_anchor((0.8, 0.15, 0.16, 0.2))
    sig_ln_map.set_title("Significant Neighbor set LIMA for Covid=-19",
                         fontdict={"fontsize": 20})
    ax.set_axis_off()

    #Neighborhood set LIMA
    tau_wwr = giddy.rank.Tau_Local_Neighborhood(
        data_df_zipGPD_bfill_dropZero[10], data_df_zipGPD_bfill_dropZero[19],
        W_, 999)
    print("tau_wwr.tau_lnhood:", tau_wwr.tau_lnhood)

    plt.figure()
    data_df_zipGPD_bfill_dropZero["tau_lnhood"] = tau_wwr.tau_lnhood
    fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(20, 20))
    ln_map = data_df_zipGPD_bfill_dropZero.plot(ax=ax,
                                                column="tau_lnhood",
                                                cmap='coolwarm',
                                                scheme='equal_interval',
                                                legend=True)
    leg = ax.get_legend()
    leg.set_bbox_to_anchor((0.8, 0.15, 0.16, 0.2))
    ln_map.set_title("Neighborhood set LIMA for Covid-19",
                     fontdict={"fontsize": 20})
    ax.set_axis_off()

    print("tau_wwr.tau_lnhood_pvalues:", tau_wwr.tau_lnhood_pvalues)
    sig_lnhood = tau_wwr.tau_lnhood * (tau_wwr.tau_lnhood_pvalues < 0.05)

    plt.figure()
    data_df_zipGPD_bfill_dropZero["sig_lnhood"] = sig_lnhood
    fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(20, 20))
    data_df_zipGPD_bfill_dropZero[data_df_zipGPD_bfill_dropZero["sig_lnhood"]
                                  == 0].plot(ax=ax,
                                             color='white',
                                             edgecolor='black')
    sig_ln_map = data_df_zipGPD_bfill_dropZero[
        data_df_zipGPD_bfill_dropZero["sig_lnhood"] != 0].plot(
            ax=ax, column="sig_lnhood", categorical=True, legend=True)
    leg = ax.get_legend()
    leg.set_bbox_to_anchor((0.8, 0.15, 0.16, 0.2))
    sig_ln_map.set_title(
        "Significant Neighborhood set LIMA for U.S. states 1929-2009",
        fontdict={"fontsize": 20})
    ax.set_axis_off()