Ejemplo n.º 1
0
 def signal_subsamp(self, k, mu, sigma, p=.5):
     init_point = random.choice(range(self.n))
     a = [1] * k + [0] * (self.n1 - k)
     b = [a[(init_point + j) % self.n1] for j in range(self.n1)]
     beta = np.kron(b, b)
     coords = np.where(
         (np.random.uniform(0, 1, self.n) < p) * (beta > 0))[0]
     return graphs.fixed_signal(self.n, coords, mu / p**.5, sigma)
Ejemplo n.º 2
0
 def signal(self, rho, mu, sigma):
     """
     Cut size of a k-cluster signal is k*(n-k). If we want rho = k*(n-k) then
     we pick k to be the solution to that quadratic equation.
     """
     if self.n**2 - 4 * rho > 0:
         k = int(
             np.floor(float(self.n) / 2 - np.sqrt(self.n**2 - 4 * rho) / 2))
     else:
         k = self.n / 2
     coords = random.sample(range(self.n), k)
     return graphs.fixed_signal(self.n, coords, mu, sigma)
Ejemplo n.º 3
0
 def signal(self, height, mu, sigma):
     beta = [1]
     de = self.l - height
     for i in range(self.l):
         if i > de:
             beta = np.kron([1, 0, 0, 0, 0, 0], beta)
         if i == de:
             beta = np.kron([1, 1, 1, 0, 0, 0], beta)
         if i < de:
             beta = np.kron([1] * 6, beta)
     coords = np.where(np.array(beta) > 0)[0]
     return graphs.fixed_signal(self.n, coords, mu, sigma)
Ejemplo n.º 4
0
 def signal(self, rho, mu, sigma):
     """
     We're going to active rho subtrees in total. The depth of the subtrees
     depends on the rho parameter, it is np.ceil(np.log2(rho)). rho better be
     smaller than n/2.
     """
     level = int(np.ceil(np.log2(rho)))
     subtrees = random.sample(range(2**level), rho)
     coords = []
     for s in subtrees:
         coords.extend(self.subtree_coords(level, s))
     return graphs.fixed_signal(self.n, coords, mu, sigma)
Ejemplo n.º 5
0
 def signal_subsamp(self, height, mu, sigma, p=.5):
     beta = [1]
     de = self.l - height
     for i in range(self.l):
         if i > de:
             beta = np.kron([1, 0, 0, 0, 0, 0], beta)
         if i == de:
             beta = np.kron([1, 1, 1, 0, 0, 0], beta)
         if i < de:
             beta = np.kron([1] * 6, beta)
     coords = np.where(
         (np.random.uniform(0, 1, self.n) < p) * (np.array(beta) > 0))[0]
     return graphs.fixed_signal(self.n, coords, mu / p**.5, sigma)
Ejemplo n.º 6
0
def ROC(alg, S, n=400, d=2, k=3, rho=1., sigma=1.0, iters=1000, mu=5):
    positives = []
    negatives = []
    alg.preprocess(S.graph())
    for i in range(iters):
        print i
        positives.append(alg.detect(S.signal(k, mu, sigma), k=k, rho=rho))
        negatives.append(
            alg.detect(graphs.fixed_signal(S.graph().numVertices(), [], 0,
                                           sigma),
                       k=k,
                       rho=rho))
    positives.sort()
    negatives.sort()
    return positives, negatives
Ejemplo n.º 7
0
def ROC(alg, S,
                  n=400,
                  d=2,
                  k=3,
                  rho = 1.,
                  sigma=1.0,
                  iters=1000,
                  mu=5):
    positives = []
    negatives = []
    alg.preprocess(S.graph())
    positives.extend([alg.detect(x, k**2, rho) for x in [S.signal(k, mu, sigma) for i in range(iters)]])
    negatives.extend([alg.detect(x, k**2, rho) for x in [graphs.fixed_signal(S.graph().numVertices(), [], 0, sigma) for i in range(iters)]])
    positives.sort()
    negatives.sort()
    return positives, negatives
Ejemplo n.º 8
0
def ROC(alg, S, n=400, d=2, k=3, rho=1., sigma=1.0, iters=1000, mu=5):
    positives = []
    negatives = []
    alg.preprocess(S.graph())
    for i in range(10):
        print "iters range: ", i
        positives.extend([
            alg.detect(x, k**2, rho)
            for x in [S.signal(k, mu, sigma) for i in range(iters / 10)]
        ])
        negatives.extend([
            alg.detect(x, k**2, rho) for x in [
                graphs.fixed_signal(S.graph().numVertices(), [], 0, sigma)
                for i in range(iters / 10)
            ]
        ])
    positives.sort()
    negatives.sort()
    tmppoints = []
    for i in range(len(negatives)):
        tmppoints.append(
            (float(iters - i) / iters,
             float(len([x for x in positives if x > negatives[i]])) / iters))
    return tmppoints
Ejemplo n.º 9
0
 def signal(self, rho, mu, sigma):
     """
     Grow a ball until it can't be grown anymore without exceeding the cut-size criteria.
     """
     assert rho > self.k, "Rho < self.k = n^{2/3} so no candidate patterns"
     start = random.choice(range(self.n))
     coords = set([start])
     cutsize = np.sum(
         [len(set(self.G.neighbors(x)) - coords) for x in coords])
     while cutsize < rho:
         neighbors = reduce(lambda x, y: x | y,
                            [set(self.G.neighbors(x)) for x in coords])
         neighbors -= coords
         toadd = random.choice(list(neighbors))
         newset = coords | set([toadd])
         newcutsize = np.sum(
             [len(set(self.G.neighbors(x)) - newset) for x in newset])
         if newcutsize > rho:
             break
         if newcutsize < cutsize:
             break
         cutsize = newcutsize
         coords = newset
     return graphs.fixed_signal(self.n, list(coords), mu, sigma)
Ejemplo n.º 10
0
 def signal(self, k, mu, sigma):
     return graphs.fixed_signal(self.n1, range(self.csize), mu, sigma)
Ejemplo n.º 11
0
 def signal_subsamp(self, depth, mu, sigma, p=.5):
     beta_seed = 2**(depth + 1)
     EO, beta = make_BBT(self.l, beta_seed)
     coords = np.where(
         (beta > 0) * (np.random.uniform(0, 1, self.n) < p))[0]
     return graphs.fixed_signal(self.n, coords, mu / p**.5, sigma)
Ejemplo n.º 12
0
 def signal(self, depth, mu, sigma):
     beta_seed = 2**(depth + 1)
     EO, beta = make_BBT(self.l, beta_seed)
     coords = np.where(beta > 0)[0]
     return graphs.fixed_signal(self.n, coords, mu, sigma)
Ejemplo n.º 13
0
 def signal(self, k, mu, sigma):
     start = random.choice(range(self.n))
     coords = self.neighs[start][0:k]
     return graphs.fixed_signal(self.n, list(coords), mu, sigma)