예제 #1
0
 def __init__(self, start_joint):
     self.start = start_joint
     self.start.bones_out.append(self)
     self.end = Joint(self)
     self.end.bone_in = self
     self.rotation = 0
     self.length = 1
     self.transform = matrix.Identity()
     self.image = None
예제 #2
0
    def shrink_domain(self, epsilon, delta=2.2820610e-12, bound=0):
        # measure all one-way marginals and shrink domain according to noisy measurements
        data = self.data
        self.round1 = list(self.column_order)
        self.delta = delta
        sigma = moments_calibration(1.0, 1.0, epsilon, delta)
        self.sigma = sigma
        print('NOISE LEVEL:', sigma)
        supports = {}
        for i, col in enumerate(self.round1):
            if self.domain[col] <= bound:
                supports[col] = np.asarray(
                    [True for j in range(self.domain[col])])
                del (self.round1[i])
        # round1 measurements will be weighted to have L2 sensitivity 1
        weights = np.ones(len(self.round1))
        weights /= np.linalg.norm(weights)  # now has L2 norm = 1
        for col, wgt in zip(self.round1, weights):
            ##########################
            ### Noise-addition step ##
            ##########################
            proj = (col, )
            hist = np.asarray(data[col].value_counts())
            print(hist)
            noise = sigma * np.random.randn(hist.size)
            y = wgt * hist + noise
            #####################
            ## Post-processing ##
            #####################
            sup = y >= 3 * sigma
            #If the column has fewer possible values than the bound we just leave it be.
            supports[col] = sup
            if sup.sum() == y.size:
                y2 = y
                I2 = matrix.Identity(y.size)
            else:
                y2 = np.append(y[sup], y[~sup].sum())
                I2 = np.ones(y2.size)
                I2[-1] = 1.0 / np.sqrt(y.size - y2.size + 1.0)
                y2[-1] /= np.sqrt(y.size - y2.size + 1.0)
                I2 = sparse.diags(I2)
            self.measurements.append((I2, y2 / wgt, 1.0 / wgt, proj))

        self.supports = supports
        data, new_domain = transform_data(data, self.domain, supports)
        self.domain = new_domain
        self.data = data
        return data, new_domain
예제 #3
0
 def measure(self, round2, from_r=False):
     # measure selected queries, which are round2 queries
     print("selected queries:", round2)
     if from_r:
         round2 = r_to_python(round2, list(self.column_order))
     self.round2 = round2  #round2 is a query list[]
     # round2 measurements will be weighted to have L2 sensitivity 1
     # perform round 2 measurments over compressed domain
     weights = np.ones(len(self.round2))
     weights /= np.linalg.norm(weights)  # now has L2 norm = 1
     for proj, wgt in zip(self.round2, weights):
         #########################
         ## Noise-addition step ##
         #########################
         indices = itemgetter(*proj)(self.domain)
         hist = datavector(self.data[list(proj)], indices)
         Q = matrix.Identity(hist.size)
         noise = self.sigma * np.random.randn(Q.shape[0])
         y = wgt * Q.dot(hist) + noise
         self.measurements.append((Q, y / wgt, 1.0 / wgt, proj))
예제 #4
0
    def test_Identity(self):
        N = 10
        M = matrix.Identity(N)

        self.assertEqual(M[0][0], 1)
        self.assertEqual(M[0][1], 0)
예제 #5
0
 def __init__(self, bone_in):
     self.bone_in = bone_in
     self.bones_out = []
     self.transform = matrix.Identity()
예제 #6
0
    def measure(self):
        data = self.load_data()
        # round1 and round2 measurements will be weighted to have L2 sensitivity 1
        sigma = moments_calibration(1.0, 1.0, self.epsilon, self.delta)
        print('NOISE LEVEL:', sigma)

        weights = np.ones(len(self.round1))
        weights[self.round1.index('INCWAGE_A')] *= 2.0
        weights /= np.linalg.norm(weights)  # now has L2 norm = 1

        supports = {}

        self.measurements = []
        for col, wgt in zip(self.round1, weights):
            ##########################
            ### Noise-addition step ##
            ##########################
            proj = (col, )
            hist = data.project(proj).datavector()
            noise = sigma * np.random.randn(hist.size)
            y = wgt * hist + noise

            #####################
            ## Post-processing ##
            #####################

            if col in [
                    'INCWAGE_A', 'SEA', 'METAREA', 'COUNTY', 'CITY', 'METAREAD'
            ]:
                sup = np.ones(y.size, dtype=bool)
            else:
                sup = y >= 3 * sigma

            supports[col] = sup
            print(col, self.domain.size(col), sup.sum())

            if sup.sum() == y.size:
                y2 = y
                I2 = matrix.Identity(y.size)
            else:
                y2 = np.append(y[sup], y[~sup].sum())
                I2 = np.ones(y2.size)
                I2[-1] = 1.0 / np.sqrt(y.size - y2.size + 1.0)
                y2[-1] /= np.sqrt(y.size - y2.size + 1.0)
                I2 = sparse.diags(I2)

            self.measurements.append((I2, y2 / wgt, 1.0 / wgt, proj))

        self.supports = supports
        # perform round 2 measurments over compressed domain
        data = transform_data(data, supports)
        self.domain = data.domain

        self.round2 = [cl for cl in self.round2 if self.domain.size(cl) < 1e6]
        weights = np.ones(len(self.round2))
        weights[self.round2.index(
            ('SEX', 'CITY', 'INCWAGE_A'))] *= self.weight3
        weights[self.round2.index(('SEX', 'CITY'))] *= 2.0
        weights[self.round2.index(('SEX', 'INCWAGE_A'))] *= 2.0
        weights[self.round2.index(('CITY', 'INCWAGE_A'))] *= 2.0
        weights /= np.linalg.norm(weights)  # now has L2 norm = 1

        for proj, wgt in zip(self.round2, weights):
            #########################
            ## Noise-addition step ##
            #########################
            hist = data.project(proj).datavector()
            if proj == ('SEX', 'CITY', 'INCWAGE_A'):
                dom = self.domain.project(proj).shape
                I = sparse.eye(dom[0] * dom[1])
                Q = sparse.kron(I, self.Q_INCWAGE).tocsr()
            elif proj == ('CITY', 'INCWAGE_A'):
                I = sparse.eye(self.domain.size('CITY'))
                Q = sparse.kron(I, self.Q_INCWAGE).tocsr()
            else:
                Q = matrix.Identity(hist.size)

            noise = sigma * np.random.randn(Q.shape[0])
            y = wgt * Q.dot(hist) + noise
            self.measurements.append((Q, y / wgt, 1.0 / wgt, proj))