Ejemplo n.º 1
0
    def Run(self, W, x, eps, seed):
        x = x.flatten()
        prng = np.random.RandomState(seed)

        if self.workload_based:
            mapping = mapper.WorkloadBased(W).mapping()
            reducer = transformation.ReduceByPartition(mapping)
            x = reducer.transform(x)
            # Reduce workload
            # W = support.reduce_queries(mapping, W)
            W = W * support.expansion_matrix(mapping)

        # Orange AHPparition(PA) operator in paper can be expressed
        # as the following sequence of simpler opeartors
        M = selection.Identity(x.shape).select()
        y = measurement.Laplace(M, self.ratio * eps).measure(x, prng)
        xest = inference.AHPThresholding(self.eta, self.ratio).infer(M, y, eps)
        mapping = mapper.AHPCluster(xest, (1 - self.ratio) * eps).mapping()

        # TR
        reducer = transformation.ReduceByPartition(mapping)

        x_bar = reducer.transform(x)
        # SI LM LS
        M_bar = selection.Identity(x_bar.shape).select()
        y_bar = measurement.Laplace(M_bar, eps * (1 - self.ratio)).measure(
            x_bar, prng)
        x_bar_hat = inference.LeastSquares().infer(M_bar, y_bar)
        x_hat = support.expansion_matrix(mapping) * x_bar_hat

        return x_hat
Ejemplo n.º 2
0
    def Run(self, W, x, eps, seed):
        assert len(x.shape) == 2, "Adaptive Grid only works for 2D domain"
        shape_2d = x.shape
        x = x.flatten()
        prng = np.random.RandomState(seed)
        Ms = []
        ys = []

        M = selection.UniformGrid(shape_2d, 
                                  self.data_scale, 
                                  eps, 
                                  ag_flag=True, 
                                  c=self.c).select()


        y  = measurement.Laplace(M, self.alpha*eps).measure(x, prng)
        x_hat = inference.LeastSquares().infer(M, y)

        Ms.append(M)
        ys.append(y)

        # Prepare parition object for later SplitByParition.
        # This Partition selection operator is missing from Figure 2, plan 12 in the paper.
        uniform_mapping = mapper.UGridPartition(shape_2d, 
                                                self.data_scale, 
                                                eps, 
                                                ag_flag=True, 
                                                c=self.c).mapping()
        x_sub_list =  meta.SplitByPartition(uniform_mapping).transform(x)
        sub_domains = support.get_subdomain_grid(uniform_mapping, shape_2d)

        ll, hi =[], []

        for i in sorted(set(uniform_mapping)):

            x_i = x_sub_list[i]
            P_i = support.projection_matrix(uniform_mapping, i) 
            x_hat_i =  P_i * x_hat 

            sub_domain_shape = sub_domains[i]
            M_i = selection.AdaptiveGrid(sub_domain_shape, 
                                         x_hat_i, 
                                         (1-self.alpha)*eps, 
                                         c2=self.c2).select()


            y_i = measurement.Laplace(M_i, (1-self.alpha)*eps).measure(x_i, prng)

            offset = np.unravel_index(P_i.matrix.nonzero()[1][0], shape_2d)
            ll.extend(M_i._lower + np.array(offset))
            hi.extend(M_i._higher + np.array(offset))

            ys.append(y_i)

        Ms.append(workload.RangeQueries(shape_2d, np.array(ll), np.array(hi)))
        x_hat = inference.LeastSquares().infer(Ms, ys)

        return x_hat
Ejemplo n.º 3
0
    def Run(self, W, x, eps, seed):
        assert len(x.shape) == 2, "Adaptive Grid only works for 2D domain"

        shape_2d = x.shape
        x = x.flatten()
        prng = np.random.RandomState(seed)
        Ms = []
        ys = []

        M = selection.UniformGrid(shape_2d, 
								  self.data_scale, 
								  eps, 
								  ag_flag=True, 
								  c=self.c).select()
        if not isinstance(M, np.ndarray):
            M = M.toarray()

        y  = measurement.Laplace(M, self.alpha*eps).measure(x, prng)
        x_hat = inference.LeastSquares().infer(M, y)

        Ms.append(M)
        ys.append(y)

        # Prepare parition object for later SplitByParition.
        # This Partition selection operator is missing from Figure 2, plan 12 in the paper.
        uniform_mapping = mapper.UGridPartition(shape_2d, 
												self.data_scale, 
												eps, 
												ag_flag=True, 
												c=self.c).mapping()
        x_sub_list =  meta.SplitByPartition(uniform_mapping).transform(x)
        sub_domains = support.get_subdomain_grid(uniform_mapping, shape_2d)

        for i in sorted(set(uniform_mapping)):
            x_i = x_sub_list[i]

            P_i = support.projection_matrix(uniform_mapping, i) 
            x_hat_i =  P_i * x_hat 

            sub_domain_shape = sub_domains[i]

            M_i = selection.AdaptiveGrid(sub_domain_shape, 
										 x_hat_i, 
										 (1-self.alpha)*eps, 
										 c2=self.c2).select()
            if not isinstance(M, np.ndarray):
                M_i = M_i.toarray()

            y_i = measurement.Laplace(M_i, (1-self.alpha)*eps).measure(x_i, prng)

            M_i_o = M_i * P_i

            Ms.append(M_i_o)
            ys.append(y_i)

        x_hat = inference.LeastSquares().infer(Ms, ys, [1.0]*len(ys))

        return x_hat
Ejemplo n.º 4
0
    def Run(self, W, x, eps, seed):
        domain_dimension = len(self.domain_shape)
        eps_share = util.old_div(float(eps), domain_dimension)

        x = x.flatten()
        prng = np.random.RandomState(seed)
        
        Ms = []
        ys = []
        scale_factors = []
        for i in range(domain_dimension):
            # Reducde domain to get marginals
            marginal_mapping = mapper.MarginalPartition(
                domain_shape=self.domain_shape, proj_dim=i).mapping()
            reducer = transformation.ReduceByPartition(marginal_mapping)
            x_i = reducer.transform(x)

            if self.domain_shape[i] < 50:
                # run identity subplan
                M_i = selection.Identity(x_i.shape).select()
                y_i = measurement.Laplace(M_i, eps_share).measure(x_i, prng)
                noise_scale_factor = laplace_scale_factor(
                    M_i, eps_share)
                
            else:
                # run dawa subplan
                W = get_matrix(W)

                W_i = W * support.expansion_matrix(marginal_mapping)

                dawa = pmapper.Dawa(eps_share, self.ratio, self.approx)
                mapping = dawa.mapping(x_i, prng)

                reducer = transformation.ReduceByPartition(mapping)
                x_bar = reducer.transform(x_i)
                W_bar = W_i * support.expansion_matrix(mapping)

                M_bar = selection.GreedyH(x_bar.shape, W_bar).select()
                y_i = measurement.Laplace(
                    M_bar, eps_share * (1 - self.ratio)).measure(x_bar, prng)

                noise_scale_factor = laplace_scale_factor(
                    M_bar, eps_share * (1 - self.ratio))

                # expand the dawa reduction
                M_i = M_bar * support.reduction_matrix(mapping)

            MM = M_i * support.reduction_matrix(marginal_mapping)
            Ms.append(MM)
            ys.append(y_i)
            scale_factors.append(noise_scale_factor)

        x_hat = inference.LeastSquares(method='lsmr').infer(Ms, ys, scale_factors)

        return x_hat  
Ejemplo n.º 5
0
    def Run(self, W, x, eps, seed):
        x = x.flatten()
        prng = np.random.RandomState(seed)

        striped_mapping = mapper.Striped(self.domain,
                                         self.stripe_dim).mapping()
        x_sub_list = meta.SplitByPartition(striped_mapping).transform(x)

        Ms = []
        ys = []
        scale_factors = []
        group_idx = sorted(set(striped_mapping))
        for i in group_idx:

            x_i = x_sub_list[group_idx.index(i)]
            P_i = support.projection_matrix(striped_mapping, i)

            M_bar = selection.HB(x_i.shape).select()
            y_i = measurement.Laplace(M_bar, eps).measure(x_i, prng)

            noise_scale_factor = laplace_scale_factor(M_bar, eps)

            M_i = M_bar * P_i

            Ms.append(M_i)
            ys.append(y_i)
            scale_factors.append(noise_scale_factor)

        x_hat = inference.LeastSquares().infer(Ms, ys, scale_factors)

        return x_hat
Ejemplo n.º 6
0
 def Run(self, W, x, eps, seed):
     x = x.flatten()
     prng = np.random.RandomState(seed)
     M = selection.HD_IHB(self.domain, self.impl, self.stripe_dim).select()
     y = measurement.Laplace(M, eps).measure(x, prng)
     x_hat = inference.LeastSquares().infer(M, y)
     return x_hat
Ejemplo n.º 7
0
    def Run(self, W, x, eps, seed):
        x = x.flatten()
        prng = np.random.RandomState(seed)

        if len(self.domain_shape) == 2:
            # apply hilbert transform to convert 2d domain into 1d
            hilbert_mapping = mapper.HilbertTransform(
                self.domain_shape).mapping()
            domain_reducer = transformation.ReduceByPartition(hilbert_mapping)

            x = domain_reducer.transform(x)
            W = W.get_matrix() * support.expansion_matrix(hilbert_mapping)

            dawa = pmapper.Dawa(eps, self.ratio, self.approx)
            mapping = dawa.mapping(x, prng)
        elif len(self.domain_shape) == 1:
            W = W.get_matrix()
            dawa = pmapper.Dawa(eps, self.ratio, self.approx)
            mapping = dawa.mapping(x, prng)

        reducer = transformation.ReduceByPartition(mapping)
        x_bar = reducer.transform(x)
        W_bar = W * support.expansion_matrix(mapping)

        M_bar = selection.GreedyH(x_bar.shape, W_bar).select()
        y = measurement.Laplace(M_bar,
                                eps * (1 - self.ratio)).measure(x_bar, prng)
        x_bar_hat = inference.LeastSquares().infer(M_bar, y)

        x_bar_hat_exp = support.expansion_matrix(mapping) * x_bar_hat

        if len(self.domain_shape) == 1:
            return x_bar_hat_exp
        elif len(self.domain_shape) == 2:
            return support.expansion_matrix(hilbert_mapping) * x_bar_hat_exp
Ejemplo n.º 8
0
    def Run(self, W, x, eps, seed):
        prng = np.random.RandomState(seed)
        M = selection.GreedyH(x.shape, W).select()
        y = measurement.Laplace(M, eps).measure(x, prng)
        x_hat = inference.LeastSquares().infer(M, y)

        return x_hat
Ejemplo n.º 9
0
    def Run(self, W, x, eps, seed):
        x = x.flatten()
        prng = np.random.RandomState(seed)
        M = selection.Wavelet(self.domain_shape).select()
        y = measurement.Laplace(M, eps).measure(x, prng)
        x_hat = inference.LeastSquares().infer(M, y)

        return x_hat
Ejemplo n.º 10
0
    def Run(self, W, x, eps, seed):
        x = x.flatten()            
        prng = np.random.RandomState(seed)

        striped_vectors = mapper.Striped(self.domain, self.stripe_dim).partitions()
        hd_vector = support.combine_all(striped_vectors)
        striped_mapping = hd_vector.flatten()

        x_sub_list = meta.SplitByPartition(striped_mapping).transform(x)

        Ms = []
        ys = []
        scale_factors = []
        group_idx = sorted(set(striped_mapping))

        # Given a group id on the full vector, recover the group id for each partition
        # put back in loop to save memory
        self.subgroups = {}
        for i in group_idx:
            selected_idx = np.where(hd_vector == i)
            ans = [p[i[0]] for p, i in zip(striped_vectors, selected_idx)]
            self.subgroups[i] = ans

        for i in group_idx: 
            x_i = x_sub_list[group_idx.index(i)]
            
            # overwriting standard projection for efficiency
            W_i = self.project_workload(W, striped_vectors, hd_vector, i)

            dawa = pmapper.Dawa(eps, self.ratio, self.approx)
            mapping = dawa.mapping(x_i, prng)
            reducer = transformation.ReduceByPartition(mapping)
            x_bar = reducer.transform(x_i)
            W_bar = W_i * support.expansion_matrix(mapping)

            M_bar = selection.GreedyH(x_bar.shape, W_bar).select()
            if not isinstance(M_bar, np.ndarray):
                M_bar = M_bar.toarray()

            y_i = measurement.Laplace(
                M_bar, eps * (1 - self.ratio)).measure(x_bar, prng)

            noise_scale_factor = laplace_scale_factor(
                M_bar, eps * (1 - self.ratio))

            # convert the measurement back to the original domain for inference
            P_i = support.projection_matrix(striped_mapping, i)
            M_i = (M_bar * support.reduction_matrix(mapping)) * P_i

            Ms.append(M_i)
            ys.append(y_i)
            scale_factors.append(noise_scale_factor)

        x_hat = inference.LeastSquares().infer(Ms, ys, scale_factors)

        return x_hat
Ejemplo n.º 11
0
    def Run(self, W, x, eps, seed):
        x = x.flatten()
        prng = np.random.RandomState(seed)
        shape_2d = (x.shape[0]//2,2)
        
        M = selection.QuadTree(shape_2d).select()
        y  = measurement.Laplace(M, eps).measure(x, prng)
        x_hat = inference.LeastSquares().infer(M, y)

        return x_hat
Ejemplo n.º 12
0
    def Run(self, W, relation, eps, seed):
        prng = np.random.RandomState(seed)
        M = pselection.PrivBayesSelect(self.theta, self.domain,
                                       eps).select(relation, prng)
        x = transformation.Vectorize(
            '', reduced_domain=self.domain).transform(relation)
        y = measurement.Laplace(M, eps).measure(x, prng)
        x_hat = inference.LeastSquares().infer(M, y)

        return x_hat
Ejemplo n.º 13
0
    def Run(self, W, x, eps, seed):
        prng = np.random.RandomState(seed)
        W = get_matrix(W)
        M = selection.GreedyH(x.shape, W).select()
        if not isinstance(M, np.ndarray):
            M = M.toarray()

        y  = measurement.Laplace(M, eps).measure(x, prng)
        x_hat = inference.LeastSquares().infer(M, y)

        return x_hat
Ejemplo n.º 14
0
    def Run(self, W, x, eps, seed):
        domain_shape = x.shape
        x = x.flatten()
        prng = np.random.RandomState(seed)

        M = selection.HDMarginal(domain_shape).select()

        y = measurement.Laplace(M, eps).measure(x, prng)
        x_hat = inference.LeastSquares(method='lsmr').infer(M, y)

        return x_hat
Ejemplo n.º 15
0
    def Run(self, W, x, eps, seed):
        x = x.flatten()            
        prng = np.random.RandomState(seed)
        M = selection.hd_IHB(self.domain, self.stripe_dim).select()

        if not isinstance(M, np.ndarray):
                M = M.toarray()
        y  = measurement.Laplace(M, eps).measure(x, prng)
        x_hat = inference.LeastSquares().infer(M, y)

        return x_hat
Ejemplo n.º 16
0
    def Run(self, W, x, eps, seed):
        assert len(x.shape) == 2, "QuadTree only works for 2D domain"
        prng = np.random.RandomState(seed)
        shape_2d = x.shape
        x = x.flatten()

        M = selection.QuadTree(shape_2d).select()

        y = measurement.Laplace(M, eps).measure(x, prng)
        x_hat = inference.LeastSquares().infer(M, y)

        return x_hat
Ejemplo n.º 17
0
    def Run(self, W, x, eps, seed):
        assert len(x.shape) == 2, "Uniform Grid only works for 2D domain"

        shape_2d = x.shape
        x = x.flatten()
        prng = np.random.RandomState(seed)

        M = selection.UniformGrid(shape_2d, self.data_scale, eps).select()
        y = measurement.Laplace(M, eps).measure(x, prng)
        x_hat = inference.LeastSquares().infer(M, y)

        return x_hat
Ejemplo n.º 18
0
    def Run(self, W, x, eps, seed):
        x = x.flatten()
        prng = np.random.RandomState(seed)

        if self.workload_based:
            W = get_matrix(W)
            mapping = mapper.WorkloadBased(W).mapping() 
            reducer = transformation.ReduceByPartition(mapping)
            x = reducer.transform(x)
            # Reduce workload
            # W = support.reduce_queries(mapping, W)
            W = W * support.expansion_matrix(mapping)
            self.domain_shape = x.shape


        if len(self.domain_shape) == 2:
            # apply hilbert transform to convert 2d domain into 1d
            hilbert_mapping = mapper.HilbertTransform(self.domain_shape).mapping()
            domain_reducer = transformation.ReduceByPartition(hilbert_mapping)

            x = domain_reducer.transform(x)

            W = get_matrix(W)
            W = W * support.expansion_matrix(hilbert_mapping)

            dawa = pmapper.Dawa(eps, self.ratio, self.approx)
            mapping = dawa.mapping(x, prng)

        elif len(self.domain_shape) == 1:

            W = get_matrix(W)
            dawa = pmapper.Dawa(eps, self.ratio, self.approx)
            mapping = dawa.mapping(x, prng)


        reducer = transformation.ReduceByPartition(mapping)
        x_bar = reducer.transform(x)
        W_bar = W * support.expansion_matrix(mapping)

        M_bar = selection.GreedyH(x_bar.shape, W_bar).select()
        if not isinstance(M_bar, np.ndarray):
            M_bar = M_bar.toarray()
        y = measurement.Laplace(M_bar, eps*(1-self.ratio)).measure(x_bar, prng)
        x_bar_hat = inference.LeastSquares().infer(M_bar, y)

        x_bar_hat_exp = support.expansion_matrix(mapping) * x_bar_hat


        if len(self.domain_shape) == 1:
            return x_bar_hat_exp
        elif len(self.domain_shape) == 2:
            return support.expansion_matrix(hilbert_mapping) * x_bar_hat_exp
Ejemplo n.º 19
0
def expected_error_empirical(W, A, eps=np.sqrt(2), trials=25):
    prng = np.random.RandomState(9999)
    x = np.ones(W.shape[1])     # makeup of data vector doesn't matter
    true_ans = W @ x

    errors = np.zeros(trials)
    for i in range(trials):
        y = measurement.Laplace(A, eps).measure(x, prng)
        x_hat = inference.LeastSquares().infer(A, y)
        ans = W @ x_hat
        total_sqerr_workload = np.sum((ans-true_ans)**2)
        errors[i] = total_sqerr_workload
    return np.mean(errors)
Ejemplo n.º 20
0
    def Run(self, W, x, eps, seed):
        x = x.flatten()
        prng = np.random.RandomState(seed)

        domain_size = np.prod(self.domain_shape)
        # Start with a unifrom estimation of x
        x_hat = np.array([self.data_scale / float(domain_size)] * domain_size)
        
        # non-zero regs to avoid super long convergence time.
        nnls = inference.NonNegativeLeastSquares(l1_reg=1e-6, l2_reg=1e-6)

        measuredQueries = []

        M_history = []
        y_history = []
        noise_scales = []

        if self.total_noise_scale != 0:
                M_history.append(workload.Total(domain_size))
                y_history.append(np.array([self.data_scale]))
                noise_scales.append(self.total_noise_scale)


        for i in range(1, self.rounds+1):
            eps_round = eps / float(self.rounds)

            # SW + SH2
            worst_approx = pselection.WorstApprox(W,
                                                  measuredQueries,
                                                  x_hat, 
                                                  eps_round * self.ratio)

            W_next = worst_approx.select(x, prng)
            measuredQueries.append(W_next.mwem_index)
            M = selection.AddEquiWidthIntervals(W_next, i).select()

            laplace = measurement.Laplace(M, eps_round * (1-self.ratio))

            y = laplace.measure(x, prng)

            # default use history
            M_history.append(M) 
            y_history.append(y)
            noise_scales.append(laplace_scale_factor(M, eps_round * (1-self.ratio)))
            

            x_hat = nnls.infer(M_history, y_history, noise_scales)

        return x_hat
Ejemplo n.º 21
0
    def Run(self, W, x, eps, seed):
        x = x.flatten()
        prng = np.random.RandomState(seed)

        domain_size = np.prod(self.domain_shape)
        # Start with a unifrom estimation of x
        x_hat = np.array([self.data_scale / float(domain_size)] * domain_size)
        
        W = get_matrix(W)
        if not isinstance(W, np.ndarray):
            W = W.toarray()
        measuredQueries = []

        nnls = inference.NonNegativeLeastSquares(method='new')
        
        M_history = np.empty((0, domain_size))
        y_history = []
        for i in range(1, self.rounds+1):
            eps_round = eps / float(self.rounds)

            # SW + SH2
            worst_approx = pselection.WorstApprox(W,
                                                  measuredQueries,
                                                  x_hat, 
                                                  eps_round * self.ratio)

            W_next = worst_approx.select(x, prng)
            measuredQueries.append(W_next.mwem_index)
            M = selection.AddEquiWidthIntervals(W_next, i).select()

            if not isinstance(M, np.ndarray):
                M = M.toarray()
                
            laplace = measurement.Laplace(M, eps_round * (1-self.ratio))
            y = laplace.measure(x, prng)

            # default use history
            M_history = np.vstack([M_history, M])
            y_history.extend(y)
            
            if self.total_noise_scale != 0:
                total_query = sparse.csr_matrix([1]*domain_size)
                noise_scale = laplace_scale_factor(M, eps_round * (1-self.ratio))
                x_hat = nnls.infer([total_query, M_history], [[self.data_scale], y_history], [self.total_noise_scale, noise_scale])
            else:
                x_hat = nnls.infer(M, y)

        return x_hat
Ejemplo n.º 22
0
    def Run(self, W, x, eps, seed):
        x = x.flatten()
        prng = np.random.RandomState(seed)
        if self.workload_based:
            mapping = mapper.WorkloadBased(W).mapping()
            reducer = transformation.ReduceByPartition(mapping)
            x = reducer.transform(x)
            # Reduce workload
            # W = support.reduce_queries(mapping, W)
            W = W * support.expansion_matrix(mapping)

        M = selection.Identity(x.shape).select()
        y = measurement.Laplace(M, eps).measure(x, prng)
        x_hat = inference.LeastSquares().infer(M, y)

        return x_hat
Ejemplo n.º 23
0
    def Run(self, W, x, eps, seed):
        x = x.flatten()
        prng = np.random.RandomState(seed)
        domain_size = np.prod(self.domain_shape)

        # Start with a uniform estimation of x
        x_hat = np.array([self.data_scale / float(domain_size)] * domain_size)

        W = get_matrix(W)
        if not isinstance(W, np.ndarray):
            W = W.toarray()

        measuredQueries = []
        mult_weight = inference.MultiplicativeWeights(updateRounds = self.update_rounds)

        M_history = np.empty((0, domain_size))
        y_history = []
        for i in range(1, self.rounds+1):
            eps_round = eps / float(self.rounds)
            # SW

            worst_approx = pselection.WorstApprox(W,
                                                  measuredQueries,
                                                  x_hat,
                                                  eps_round * self.ratio,
                                                  'EXPONENTIAL')
            M = worst_approx.select(x, prng)
            measuredQueries.append(M.mwem_index)
            if not isinstance(M, np.ndarray):
                M = M.toarray()

            # LM 
            laplace = measurement.Laplace(M, eps_round * (1-self.ratio))
            y = laplace.measure(x, prng)

            M_history = np.vstack([M_history, M])
            y_history.extend(y)

            # MW
            if self.use_history:
                x_hat = mult_weight.infer(M_history, y_history, x_hat)
            else:
                x_hat = mult_weight.infer(M, y, x_hat)

        return x_hat
Ejemplo n.º 24
0
    def Run(self, W, x, eps, seed):
        x = x.flatten()            
        prng = np.random.RandomState(seed)

        striped_mapping = mapper.Striped(self.domain, self.stripe_dim).mapping()
        x_sub_list = meta.SplitByPartition(striped_mapping).transform(x)

        Ms = []
        ys = []
        scale_factors = []
        group_idx = sorted(set(striped_mapping))

        W = get_matrix(W)

        for i in group_idx: 
            x_i = x_sub_list[group_idx.index(i)]
            P_i = support.projection_matrix(striped_mapping, i)
            W_i = W * P_i.T

            dawa = pmapper.Dawa(eps, self.ratio, self.approx)
            mapping = dawa.mapping(x_i, prng)
            reducer = transformation.ReduceByPartition(mapping)
            x_bar = reducer.transform(x_i)
            W_bar = W_i * support.expansion_matrix(mapping)

            M_bar = selection.GreedyH(x_bar.shape, W_bar).select()

            if not isinstance(M_bar, np.ndarray):
                M_bar = M_bar.toarray()

            y_i = measurement.Laplace(
                M_bar, eps * (1 - self.ratio)).measure(x_bar, prng)

            noise_scale_factor = laplace_scale_factor(
                M_bar, eps * (1 - self.ratio))

            M_i = (M_bar * support.reduction_matrix(mapping)) * P_i

            Ms.append(M_i)
            ys.append(y_i)
            scale_factors.append(noise_scale_factor)

        x_hat = inference.LeastSquares().infer(Ms, ys, scale_factors)

        return x_hat
Ejemplo n.º 25
0
    def Run(self, W, x, eps, seed):
        x = x.flatten()
        prng = np.random.RandomState(seed)

        domain_size = np.prod(self.domain_shape)
        # Start with a unifrom estimation of x
        x_hat = np.array([self.data_scale / float(domain_size)] * domain_size)
        
        measuredQueries = []
        mult_weight = inference.MultiplicativeWeights(updateRounds = self.update_rounds)

        M_history = []
        y_history = []

        for i in range(1, self.rounds+1):
            eps_round = eps / float(self.rounds)
            # SW + SH2


            worst_approx = pselection.WorstApprox(W,
                                                  measuredQueries, 
                                                  x_hat, 
                                                  eps_round * self.ratio)

            W_next = worst_approx.select(x, prng)
            measuredQueries.append(W_next.mwem_index)
            M = selection.AddEquiWidthIntervals(W_next, i).select()

            # LM 
            laplace = measurement.Laplace(M, eps_round * (1-self.ratio))
            y = laplace.measure(x, prng)

            M_history.append(M) 
            y_history.append(y)

            # MW
            if self.use_history:
                x_hat = mult_weight.infer(M_history, y_history, x_hat)
            else:
                x_hat = mult_weight.infer(M, y, x_hat)

        return x_hat
Ejemplo n.º 26
0
    def Run(self, W, x, eps, seed):
        x = x.flatten()
        prng = np.random.RandomState(seed)
        domain_size = np.prod(self.domain_shape)

        # Start with a unifrom estimation of x
        x_hat = np.array([self.data_scale / float(domain_size)] * domain_size)

        W = get_matrix(W)

        W_partial = sparse.csr_matrix(W.shape)
        mult_weight = inference.MultiplicativeWeights(updateRounds = self.update_rounds)

        M_history = np.empty((0, domain_size))
        y_history = []
        for i in range(1, self.rounds+1):
            eps_round = eps / float(self.rounds)
            # SW

            worst_approx = pselection.WorstApprox(sparse.csr_matrix(W),
                                                  W_partial,
                                                  x_hat,
                                                  eps_round * self.ratio,
                                                  'EXPONENTIAL')
            W_next = worst_approx.select(x, prng)
            M = support.extract_M(W_next)
            W_partial += W_next

            # LM 
            laplace = measurement.Laplace(M, eps_round * (1-self.ratio))
            y = laplace.measure(x, prng)

            M_history = sparse.vstack([M_history, M])
            y_history.extend(y)

            # MW
            if self.use_history:
                x_hat = mult_weight.infer(M_history, y_history, x_hat)
            else:
                x_hat = mult_weight.infer(M, y, x_hat)

        return x_hat
Ejemplo n.º 27
0
    def Run(self, W, x, eps, seed):
        prng = np.random.RandomState(seed)
        domain_size = np.prod(self.domain_shape)
        # Start with a unifrom estimation of x
        x_hat = np.array([self.data_scale / float(domain_size)] * domain_size)
        
        W = get_matrix(W)

        W_partial = sparse.csr_matrix(W.shape)
        nnls = inference.NonNegativeLeastSquares()

        M_history = np.empty((0, domain_size))
        y_history = []
        for i in range(1, self.rounds+1):
            eps_round = eps / float(self.rounds)

            # SW + SH2
            worst_approx = pselection.WorstApprox(sparse.csr_matrix(W),
                                                  W_partial, 
                                                  x_hat, 
                                                  eps_round * self.ratio)

            W_next = worst_approx.select(x, prng)
            M = selection.AddEquiWidthIntervals(W_next, i).select()

            W_partial += W_next

            laplace = measurement.Laplace(M, eps_round * (1-self.ratio))
            y = laplace.measure(x, prng)

            # default use history
            M_history = sparse.vstack([M_history, M])
            y_history.extend(y)
            
            if self.total_noise_scale != 0:
                total_query = sparse.csr_matrix([1]*domain_size)
                noise_scale = laplace_scale_factor(M, eps_round * (1-self.ratio))
                x_hat = nnls.infer([total_query, M_history], [[self.data_scale], y_history], [self.total_noise_scale, noise_scale])
            else:
                x_hat = nnls.infer(M, y)

        return x_hat
Ejemplo n.º 28
0
    def Run(self, W, x, eps, seed):
        prng = np.random.RandomState(seed)
        x_hat = prng.rand(*x.shape)
        W_partial = sparse.csr_matrix(W.get_matrix().shape)
        nnls = inference.NonNegativeLeastSquares()

        measured_queries = []
        for i in range(1, self.rounds + 1):
            eps_round = eps / float(self.rounds)

            worst_approx = pselection.WorstApprox(
                sparse.csr_matrix(W.get_matrix()), W_partial, x_hat,
                eps_round * self.ratio)
            W_next = worst_approx.select(x, prng)
            M = support.extract_M(W_next)
            W_partial += W_next
            laplace = measurement.Laplace(M, eps * (1 - self.ratio))
            y = laplace.measure(x, prng)
            x_hat = nnls.infer(M, y)

        return x_hat
Ejemplo n.º 29
0
    def Run(self, W, x, eps, seed):
        x = x.flatten()
        prng = np.random.RandomState(seed)
        if self.workload_based:
            W = get_matrix(W)
            mapping = mapper.WorkloadBased(W).mapping() 
            reducer = transformation.ReduceByPartition(mapping)
            x = reducer.transform(x)
            # Reduce workload
            # W = support.reduce_queries(mapping, W)
            W = W * support.expansion_matrix(mapping)
            self.domain_shape = x.shape

        M = selection.HB(self.domain_shape).select()

        if not isinstance(M, np.ndarray):
            M = M.toarray()

        y  = measurement.Laplace(M, eps).measure(x, prng)
        x_hat = inference.LeastSquares().infer(M, y)

        return x_hat
Ejemplo n.º 30
0
    def Run(self, W, x, eps, seed):
        prng = np.random.RandomState(seed)
        x_hat = prng.rand(*x.shape)
        W_partial = sparse.csr_matrix(W.get_matrix().shape)
        mult_weight = inference.MultiplicativeWeights()

        measured_queries = []
        for i in range(1, self.rounds + 1):
            eps_round = eps / float(self.rounds)

            # SW + SH2
            worst_approx = pselection.WorstApprox(
                sparse.csr_matrix(W.get_matrix()), W_partial, x_hat,
                eps_round * self.ratio)

            W_next = worst_approx.select(x, prng)
            M = selection.AddEquiWidthIntervals(W_next, i).select()

            W_partial += W_next
            laplace = measurement.Laplace(M, eps * (1 - self.ratio))
            y = laplace.measure(x, prng)
            x_hat = mult_weight.infer(M, y, x_hat)

        return x_hat