예제 #1
0
    def __crand(self):
        """
        conditional randomization

        for observation i with ni neighbors,  the candidate set cannot include
        i (we don't want i being a neighbor of i). we have to sample without
        replacement from a set of ids that doesn't include i. numpy doesn't
        directly support sampling wo replacement and it is expensive to
        implement this. instead we omit i from the original ids,  permutate the
        ids and take the first ni elements of the permuted ids as the
        neighbors to i in each randomization.

        """
        z = np.array(self.z)
        lisas = np.zeros((self.n, self.permutations))
        n_1 = self.n - 1
        k = self.w.max_neighbors + 1
        nn = self.n - 1
        rids = np.tile(np.arange(nn), (self.permutations, 1))
        shuffle1d.shuffle2(rids, int(time.time() * 10e6))

        ids = np.arange(self.w.n)
        ido = self.w.id_order
        w = [self.w.weights[ido[i]] for i in ids]
        wc = np.array([self.w.cardinalities[ido[i]] for i in ids])

        lisas = shuffle1d.for_i_in_wn(z, ids, rids[:, -k:], wc, lisas, w)
        self.rlisas = (n_1 / self.den) * lisas
예제 #2
0
    def __crand(self):
        """
        conditional randomization

        for observation i with ni neighbors,  the candidate set cannot include
        i (we don't want i being a neighbor of i). we have to sample without
        replacement from a set of ids that doesn't include i. numpy doesn't
        directly support sampling wo replacement and it is expensive to
        implement this. instead we omit i from the original ids,  permutate the
        ids and take the first ni elements of the permuted ids as the
        neighbors to i in each randomization.

        """
        z = np.array(self.z)
        lisas = np.zeros((self.n, self.permutations))
        n_1 = self.n - 1
        k = self.w.max_neighbors + 1
        nn = self.n - 1
        rids = np.tile(np.arange(nn), (self.permutations, 1))
        shuffle1d.shuffle2(rids, int(time.time() * 10e6))

        ids = np.arange(self.w.n)
        ido = self.w.id_order
        w = [self.w.weights[ido[i]] for i in ids]
        wc = np.array([self.w.cardinalities[ido[i]] for i in ids])

        lisas = shuffle1d.for_i_in_wn(z, ids, rids[:,-k:], wc, lisas, w)
        self.rlisas = (n_1 / self.den) * lisas
예제 #3
0
def mpcrand(lm, conditional=True, cores=None):
    z = np.array(lm.z)
    #Has to be allocated as a C style array, not a numpy wrapped C style array.
    lisas_c = mp.RawArray(ctypes.c_double, lm.n * lm.permutations)
    lisas = np.frombuffer(lisas_c,
                          dtype=np.float).reshape(lm.n, lm.permutations)
    n_1 = lm.n - 1
    k = lm.w.max_neighbors + 1
    nn = lm.n - 1
    rids = np.tile(np.arange(nn), (lm.permutations, 1))
    shuffle1d.shuffle2(rids, int(time.time() * 10e6))

    ids = np.arange(lm.w.n)
    ido = lm.w.id_order
    wc = np.array([lm.w.cardinalities[ido[i]] for i in ids])

    #Segment the jobs over available cores
    if cores is None:
        ncores = mp.cpu_count()
    else:
        ncores = cores
    pool = mp.Pool(ncores)

    #Compute the offsets - this is a decomposition by areal unit.
    step = lm.n / ncores
    starts = range(0, lm.n, step)
    stops = starts[1:]
    stops[-1] = stops[-1] + 1
    offsets = zip(starts[:-1], stops)
    #Split the weights into ncores lists
    w = [lm.w.weights[ido[i]] for i in ids]

    if conditional == True:
        jobs = [
            mp.Process(target=shuffle1d.mpfori,
                       args=(lisas, z, ids, rids, wc, w, offsets[i][0],
                             offsets[i][1], time.time() * 10e6))
            for i in range(ncores)
        ]
    else:
        jobs = [
            mp.Process(target=shuffle1d.mpfori_nonconditional,
                       args=(lisas, z, ids, rids, wc, w, offsets[i][0],
                             offsets[i][1], time.time() * 10e6))
            for i in range(ncores)
        ]
    for j in jobs:
        j.start()
    for j in jobs:
        j.join()
    return (n_1 / lm.den) * lisas
예제 #4
0
def mpcrand(lm, conditional=True, cores=None):
    z = np.array(lm.z)
    #Has to be allocated as a C style array, not a numpy wrapped C style array.
    lisas_c = mp.RawArray(ctypes.c_double, lm.n * lm.permutations)
    lisas = np.frombuffer(lisas_c, dtype = np.float).reshape(lm.n, lm.permutations)
    n_1 = lm.n - 1
    k = lm.w.max_neighbors + 1
    nn = lm.n - 1
    rids = np.tile(np.arange(nn), (lm.permutations, 1))
    shuffle1d.shuffle2(rids, int(time.time() * 10e6))

    ids = np.arange(lm.w.n)
    ido = lm.w.id_order
    wc = np.array([lm.w.cardinalities[ido[i]] for i in ids])

    #Segment the jobs over available cores
    if cores is None:
       ncores = mp.cpu_count()
    else:
        ncores = cores
    pool = mp.Pool(ncores)
    
    #Compute the offsets - this is a decomposition by areal unit.
    step = lm.n / ncores
    starts = range(0, lm.n, step)
    stops = starts[1:]
    stops[-1] = stops[-1] + 1
    offsets = zip(starts[:-1], stops)
    #Split the weights into ncores lists 
    w = [lm.w.weights[ido[i]] for i in ids]

    if conditional == True:
        jobs =[mp.Process(target=shuffle1d.mpfori, args=(lisas, z, ids, rids, wc, w, offsets[i][0], offsets[i][1], time.time() * 10e6)) for i in range(ncores)]
    else:
        jobs = [mp.Process(target=shuffle1d.mpfori_nonconditional, args=(lisas, z, ids, rids, wc, w, offsets[i][0], offsets[i][1], time.time() * 10e6)) for i in range(ncores)]
    for j in jobs:
        j.start()
    for j in jobs: 
        j.join()
    return (n_1 / lm.den) * lisas