Example #1
0
    def draw_mix_uniforms(pbs, pmix, n, seed):
        """
        Draw n samples from a mixture of uniform distributions whose bounds 
        are specified in pbs, and mixing proportion is specified in pmix.

        Return a one-dimensional numpy array of n samples.
        """
        assert pbs.shape[0] == len(pmix)
        c = len(pmix)
        sam_list = []
        scale = pbs[:, 1] - pbs[:, 0]
        with util.NumpySeedContext(seed=seed):
            # counts for each mixture component 
            counts = np.random.multinomial(n, pmix, size=1)
            # counts is a 2d array
            counts = counts[0]
            # For each component, draw from its corresponding uniform
            # distribution.
            for i, nc in enumerate(counts):
                #print i
                sam_i = stats.uniform.rvs(loc=pbs[i, 0], scale=scale[i], size=nc)
                #print 'pbs[{0},0]: {1}'.format(i, pbs[i, 0])
                #print 'sam_i: {0}'.format(sam_i)
                sam_list.append(sam_i)
            sample = np.hstack(sam_list)
            np.random.shuffle(sample)
        return sample
Example #2
0
 def sample(self, n, seed):
     with util.NumpySeedContext(seed=seed):
         X = SSMixUnif1D.draw_mix_uniforms(self.pbs, self.pmix, n, seed)
         X = X[:, np.newaxis]
         Y = SSMixUnif1D.draw_mix_uniforms(self.qbs, self.qmix, n, seed+2)
         Y = Y[:, np.newaxis]
     return TSTData(X, Y, label='mix_unif1d')
Example #3
0
def gauss_mech(quantity, sensitivity, epsilon=1.0, delta=1e-4, return_sigma=False, seed=23):
	with util.NumpySeedContext(seed=seed+1000):
		shape = quantity.shape
		sigma = sensitivity * sqrt(2.0 * log(1.25 / delta)) / epsilon
		print('Normal')
		print('sigma:{}'.format(sigma))
		private_quantity = quantity + np.random.normal(loc=0.0, scale=sigma, size=shape)
	if return_sigma:
		return private_quantity, sigma
	else:
		return private_quantity
Example #4
0
def improve_gauss_mech(quantity, sensitivity, epsilon=1.0, 
                       delta=1e-4, return_sigma=False, seed=23):
	with util.NumpySeedContext(seed=seed+1000):
		shape = quantity.shape
		sigma = calibrateAnalyticGaussianMechanism(epsilon, delta, sensitivity)
		print('Improve')
		print('sigma:{}'.format(sigma))
		private_quantity = quantity + np.random.normal(loc=0.0, scale=sigma, size=shape)
	if return_sigma:
		return private_quantity, sigma
	else:
		return private_quantity
Example #5
0
def analyse_gauss_mech(quantity, sensitivity, epsilon=0.5, 
                       delta=1e-4, gauss_noise='Normal', seed=23):
	with util.NumpySeedContext(seed=seed+3000):
		shape = quantity.shape
		if shape[0] != shape[1]:
			raiseValueError('Shape of input must be square')
		J = shape[0]
		if gauss_noise == 'Normal':
			sigma = sensitivity * sqrt(2.0 * log(1.25 / delta)) / epsilon
		elif gauss_noise == 'Improved':
			sigma = calibrateAnalyticGaussianMechanism(epsilon, delta, sensitivity)
		# construct symmetric matrix
		print('analyse_gauss_mech sigma {}'.format(sigma))
		eta = np.random.normal( loc=0.0, scale=sigma, size=(J,J))
		diag_upper = np.triu(eta)
		noise = diag_upper + np.tril(diag_upper.T, k=-1)
		private_quantity = quantity + noise
		# make it psd 
		private_PSD = util.PSD(private_quantity, reg=1e-4)
		return private_PSD