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(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))
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))
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.) 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. - stats.norm.cdf(np.abs(self.z_sim))
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))
def __crand(self): y = self.y rGs = np.zeros((self.n, self.permutations)) n_1 = self.n - 1 rid = list(range(n_1)) prange = list(range(self.permutations)) k = self.w.max_neighbors + 1 rids = np.array([np.random.permutation(rid)[0:k] for i in prange]) ids = np.arange(self.w.n) ido = self.w.id_order wc = self.__getCardinalities() if self.w_transform == "r": den = np.array(wc) + self.star else: den = np.ones(self.w.n) for i in range(self.w.n): idsi = ids[ids != i] np.random.shuffle(idsi) yi_star = y[i] * self.star wci = wc[i] rGs[i] = (y[idsi[rids[:, 0:wci]]]).sum(1) + yi_star rGs[i] = (np.array(rGs[i]) / den[i]) / (self.y_sum - (1 - self.star) * y[i]) self.rGs = rGs
def __crand(self): y = self.y rGs = np.zeros((self.n, self.permutations)) n_1 = self.n - 1 rid = list(range(n_1)) prange = list(range(self.permutations)) k = self.w.max_neighbors + 1 rids = np.array([np.random.permutation(rid)[0:k] for i in prange]) ids = np.arange(self.w.n) ido = self.w.id_order wc = self.__getCardinalities() if self.w_transform == 'r': den = np.array(wc) + self.star else: den = np.ones(self.w.n) for i in range(self.w.n): idsi = ids[ids != i] np.random.shuffle(idsi) yi_star = y[i] * self.star wci = wc[i] rGs[i] = (y[idsi[rids[:, 0:wci]]]).sum(1) + yi_star rGs[i] = (np.array(rGs[i]) / den[i]) / ( self.y_sum - (1 - self.star) * y[i]) self.rGs = rGs
def __getCardinalities(self): ido = self.w.id_order self.wc = np.array( [self.w.cardinalities[ido[i]] for i in range(self.n)]) return self.wc