Exemplo n.º 1
0
 def __init__(
     self,
     y,
     w,
     transform="R",
     permutations=PERMUTATIONS,
     star=False,
     keep_simulations=True,
 ):
     y = np.asarray(y).flatten()
     self.n = len(y)
     self.y = y
     self.w = w
     self.w_original = w.transform
     self.w.transform = self.w_transform = transform.lower()
     self.permutations = permutations
     self.star = star
     self.calc()
     self.p_norm = np.array([1 - stats.norm.cdf(np.abs(i)) for i in self.Zs])
     if permutations:
         self.__crand(keep_simulations)
         if keep_simulations:
             self.sim = sim = self.rGs.T
             self.EG_sim = sim.mean(axis=0)
             self.seG_sim = sim.std(axis=0)
             self.VG_sim = self.seG_sim * self.seG_sim
             self.z_sim = (self.Gs - self.EG_sim) / self.seG_sim
             self.p_z_sim = 1 - stats.norm.cdf(np.abs(self.z_sim))
Exemplo n.º 2
0
    def __init__(self, y, w, permutations=PERMUTATIONS):
        y = np.asarray(y).flatten()
        self.n = len(y)
        self.y = y
        w.transform = "B"
        self.w = w
        self.permutations = permutations
        self.__moments()
        self.y2 = y * y
        y = y.reshape(
            len(y), 1
        )  # Ensure that y is an n by 1 vector, otherwise y*y.T == y*y
        self.den_sum = (y * y.T).sum() - (y * y).sum()
        self.G = self.__calc(self.y)
        self.z_norm = (self.G - self.EG) / np.sqrt(self.VG)
        self.p_norm = 1.0 - stats.norm.cdf(np.abs(self.z_norm))

        if permutations:
            sim = [
                self.__calc(np.random.permutation(self.y)) for i in range(permutations)
            ]
            self.sim = sim = np.array(sim)
            above = sim >= self.G
            larger = sum(above)
            if (self.permutations - larger) < larger:
                larger = self.permutations - larger
            self.p_sim = (larger + 1.0) / (permutations + 1.0)
            self.EG_sim = sum(sim) / permutations
            self.seG_sim = sim.std()
            self.VG_sim = self.seG_sim ** 2
            self.z_sim = (self.G - self.EG_sim) / self.seG_sim
            self.p_z_sim = 1.0 - stats.norm.cdf(np.abs(self.z_sim))
Exemplo n.º 3
0
    def calc(self):
        w = self.w
        W = w.sparse

        self.y_sum = self.y.sum()

        y = self.y
        remove_self = not self.star
        N = self.w.n - remove_self

        statistic = (W @ y) / (y.sum() - y * remove_self)

        # ----------------------------------------------------#
        # compute moments necessary for analytical inference  #
        # ----------------------------------------------------#

        empirical_mean = (y.sum() - y * remove_self) / N
        # variance looks complex, yes, but it obtains from E[x^2] - E[x]^2.
        # So, break it down to allow subtraction of the self-neighbor.
        mean_of_squares = ((y**2).sum() - (y**2) * remove_self) / N
        empirical_variance = mean_of_squares - empirical_mean**2

        # Since we have corrected the diagonal, this should work
        cardinality = np.asarray(W.sum(axis=1)).squeeze()
        expected_value = cardinality / N
        expected_variance = (cardinality * (N - cardinality) / (N - 1) *
                             (1 / N**2) * (empirical_variance /
                                           (empirical_mean**2)))
        z_scores = (statistic - expected_value) / np.sqrt(expected_variance)

        self.Gs = statistic
        self.EGs = expected_value
        self.VGs = expected_variance
        self.Zs = z_scores
Exemplo n.º 4
0
 def __init__(self,
              y,
              w,
              transform='R',
              permutations=PERMUTATIONS,
              star=False):
     y = np.asarray(y).flatten()
     self.n = len(y)
     self.y = y
     self.w = w
     self.w_original = w.transform
     self.w.transform = self.w_transform = transform.lower()
     self.permutations = permutations
     self.star = star
     self.calc()
     self.p_norm = np.array(
         [1 - stats.norm.cdf(np.abs(i)) for i in self.Zs])
     if permutations:
         self.__crand()
         sim = np.transpose(self.rGs)
         above = sim >= self.Gs
         larger = sum(above)
         low_extreme = (self.permutations - larger) < larger
         larger[low_extreme] = self.permutations - larger[low_extreme]
         self.p_sim = (larger + 1.0) / (permutations + 1)
         self.sim = sim
         self.EG_sim = sim.mean()
         self.seG_sim = sim.std()
         self.VG_sim = self.seG_sim * self.seG_sim
         self.z_sim = (self.Gs - self.EG_sim) / self.seG_sim
         self.p_z_sim = 1 - stats.norm.cdf(np.abs(self.z_sim))
Exemplo n.º 5
0
def _infer_star_and_structure_w(weights, star, transform):
    assert transform.lower() in ("r", "b"), (
        f'Transforms must be binary "b" or row-standardized "r".'
        f"Recieved: {transform}")
    adj_matrix = weights.sparse
    diagonal = adj_matrix.diagonal()
    zero_diagonal = (diagonal == 0).all()

    # Gi has a zero diagonal, Gi* has a nonzero diagonal
    star = (not zero_diagonal) if star is None else star

    # Want zero diagonal but do not have it
    if (not zero_diagonal) & (star is False):
        weights = fill_diagonal(weights, 0)
    # Want nonzero diagonal and have it
    elif (not zero_diagonal) & (star is True):
        weights = weights
    # Want zero diagonal and have it
    elif zero_diagonal & (star is False):
        weights = weights
    # Want nonzero diagonal and do not have it
    elif zero_diagonal & (star is True):
        # if the input is binary or requested transform is binary,
        # set the diagonal to 1.
        if transform.lower() == "b" or weights.transform.lower() == "b":
            weights = fill_diagonal(weights, 1)
        # if we know the target is row-standardized, use the row max
        # this works successfully for effectively binary but "O"-transformed input
        elif transform.lower() == "r":
            # This warning is presented in the documentation as well
            warnings.warn(
                "Gi* requested, but (a) weights are already row-standardized,"
                " (b) no weights are on the diagonal, and"
                " (c) no default value supplied to star. Assuming that the"
                " self-weight is equivalent to the maximum weight in the"
                " row. To use a different default (like, .5), set `star=.5`,"
                " or use libpysal.weights.fill_diagonal() to set the diagonal"
                " values of your weights matrix and use `star=None` in Gi_Local."
            )
            weights = fill_diagonal(
                weights,
                np.asarray(adj_matrix.max(axis=1).todense()).flatten())
    else:  # star was something else, so try to fill the weights with it
        try:
            weights = fill_diagonal(weights, star)
        except:
            raise TypeError(
                f"Type of star ({type(star)}) not understood."
                f" Must be an integer, boolean, float, or numpy.ndarray.")
    star = (weights.sparse.diagonal() > 0).any()
    weights.transform = transform

    return weights, star
Exemplo n.º 6
0
 def __init__(
     self,
     y,
     w,
     transform="R",
     permutations=PERMUTATIONS,
     star=False,
     keep_simulations=True,
     n_jobs=-1,
     seed=None,
     island_weight=0,
 ):
     y = np.asarray(y).flatten()
     self.n = len(y)
     self.y = y
     w, star = _infer_star_and_structure_w(w, star, transform)
     w.transform = transform
     self.w_transform = transform
     self.w = w
     self.permutations = permutations
     self.star = star
     self.calc()
     self.p_norm = 1 - stats.norm.cdf(np.abs(self.Zs))
     if permutations:
         self.p_sim, self.rGs = _crand_plus(
             y,
             w,
             self.Gs,
             permutations,
             keep_simulations,
             n_jobs=n_jobs,
             stat_func=_g_local_star_crand if star else _g_local_crand,
             scaling=y.sum(),
             seed=seed,
             island_weight=island_weight,
         )
         if keep_simulations:
             self.sim = sim = self.rGs.T
             self.EG_sim = sim.mean(axis=0)
             self.seG_sim = sim.std(axis=0)
             self.VG_sim = self.seG_sim * self.seG_sim
             self.z_sim = (self.Gs - self.EG_sim) / self.seG_sim
             self.p_z_sim = 1 - stats.norm.cdf(np.abs(self.z_sim))