def smooth_sensitivity_study(prior, sample_size, epsilon, delta, percentage): Bayesian_Model = BayesInferwithDirPrior(prior, sample_size, epsilon, delta) observation = [sample_size * i for i in percentage] Bayesian_Model._set_observation([sample_size * i for i in percentage]) Bayesian_Model._set_candidate_scores() Bayesian_Model._set_LS_Candidates() xstick = [ str((r._minus(Bayesian_Model._prior))._alphas) for r in Bayesian_Model._candidates ] beta = 0.00000001 y = [ Bayesian_Model._LS_Candidates[r] * math.exp(-beta * Hamming_Distance(observation, r._alphas)) for r in Bayesian_Model._candidates ] scatter_plot( y, xstick, 'Smooth Sensitivity Study w.r.t. x :' + str([sample_size * i for i in percentage]) + r'; $\epsilon = $' + str(epsilon) + r'; $\delta:$' + str(delta), r"$\Delta_l(H(BI(x'),-))e^{- \gamma *d(x,x')}$")
def probability_values(datasize,epsilon,delta,prior,observation, mech): Bayesian_Model = BayesInferwithDirPrior(prior, datasize, epsilon, delta) Bayesian_Model._set_observation(observation) Bayesian_Model._set_candidate_scores() Bayesian_Model._set_local_sensitivities() if(mech == r'Alg 3 - $\mathsf{EHD}$'): Bayesian_Model._set_up_exp_mech_with_GS() elif(mech == r'Alg 4 - $\mathsf{EHDL}$'): Bayesian_Model._set_up_exp_mech_with_LS() elif(mech == r'Alg 5 - $\mathsf{EHDS}$'): Bayesian_Model._set_up_exp_mech_with_gamma_SS() probabilities_exp = {} for i in range(len(Bayesian_Model._candidates)): z = Bayesian_Model._candidates[i] if(mech == r'Alg 3 - $\mathsf{EHD}$'): probabilities_exp[str(z._alphas)] = Bayesian_Model._GS_probabilities[i] elif(mech == r'Alg 4 - $\mathsf{EHDL}$'): probabilities_exp[str(z._alphas)] = Bayesian_Model._LS_probabilities[i] elif(mech == r'Alg 5 - $\mathsf{EHDS}$'): probabilities_exp[str(z._alphas)] = Bayesian_Model._gamma_SS_probabilities[i] return probabilities_exp
def ss_exponentiate_component_study(prior, sample_size, epsilon, delta, percentage): Bayesian_Model = BayesInferwithDirPrior(prior, sample_size, epsilon, delta) observation = [sample_size * i for i in percentage] Bayesian_Model._set_observation(observation) Bayesian_Model._set_candidate_scores() xstick = [ str((c._minus(Bayesian_Model._prior))._alphas) for c in Bayesian_Model._candidates ] # print [c._alphas for c in Bayesian_Model._candidates] beta = math.log(1 - epsilon / (2.0 * math.log(delta / (2.0 * (sample_size))))) # y=[Hamming_Distance(Bayesian_Model._observation_counts, r._alphas) for r in Bayesian_Model._candidates] # print y y = [ math.exp(-beta * Hamming_Distance(observation, r._alphas)) for r in Bayesian_Model._candidates ] scatter_plot( y, xstick, 'Exponentiate Component of Smooth Sensitivity w.r.t. x :' + str([sample_size * i for i in percentage]) + r'; $\epsilon:$' + str(epsilon) + r'; $\delta:$' + str(delta), r"$e^{- \gamma *d(x,x')}$") return
def exp_distribution_over_candidates(dataobs, prior, epsilon, mech): n = sum(dataobs) Bayesian_Model = BayesInferwithDirPrior(prior, n, epsilon) Bayesian_Model._set_observation(dataobs) Bayesian_Model._set_candidate_scores() Bayesian_Model._set_local_sensitivities() if mech == "exp": Bayesian_Model._set_up_exp_mech_with_GS() elif mech == "gamma": Bayesian_Model._set_up_exp_mech_with_gamma_SS() exp_prob = {} for i in range(len(Bayesian_Model._candidates)): z = Bayesian_Model._candidates[i]._pointwise_sub(prior) if mech == "exp": exp_prob[list2key(z._alphas)] = Bayesian_Model._GS_probabilities[i] elif mech == "gamma": exp_prob[list2key( z._alphas)] = Bayesian_Model._gamma_SS_probabilities[i] # print exp_prob return exp_prob
def privacy_loss_of_size_n(prior, datasize, epsilon, delta, mech): Bayesian_Model = BayesInferwithDirPrior(prior, datasize, epsilon, delta) Bayesian_Model._set_candidate_scores() candidates = Bayesian_Model._candidates ######################################################################################################################### #ALL POSSIBLE DATA SETS UNDER SIZE N for i in range(len(candidates)): candidates[i]._minus(prior) candidates = [c._alphas for c in candidates] privacyloss_of_n = 0.0 pair_of_n = [] candidates = [[datasize - 3,3], [datasize - 2, 2],[datasize - 1, 1],[datasize, 0],[0, datasize], [1, datasize - 1], [2, datasize - 2], [3, datasize - 3]] for C in candidates: ######################################################################################################################### #GET THE ADJACENT DATA SET OF DATA SET C C_adj = get_adjacent_set(C) ######################################################################################################################### #GET THE PRIVACY LOSS UNDER TWO ADJACENT DATA SET C AND loss = privacy_loss_one_pair(datasize,epsilon,delta,prior,C,C_adj, mech) if privacyloss_of_n < loss[-1][1]: privacyloss_of_n = loss[-1][1] pair_of_n = (C,C_adj) return privacyloss_of_n, pair_of_n
def get_steps_opt(dataobs, prior, epsilon): n = sum(dataobs) Bayesian_Model = BayesInferwithDirPrior(prior, n, epsilon) Bayesian_Model._set_observation(dataobs) Bayesian_Model._set_candidate_scores() cpara = [] for c in Bayesian_Model._candidates: cpara.append(list2key(c._minus(prior)._alphas)) return cpara
def ls_scaled_by_eps(n, prior, epsilon): Bayesian_Model = BayesInferwithDirPrior(prior, n, epsilon) Bayesian_Model._set_observation(gen_dataset_rand(len(prior._alphas), n)) Bayesian_Model._set_candidate_scores() Bayesian_Model._set_local_sensitivities() ls = [] xstick = [] for c in Bayesian_Model._candidates: ls.append(Bayesian_Model._LS_Candidates[c] / epsilon) xstick.append(c._alphas) return ls, xstick
def expect_errors_Lap(n, prior, epsilon): Bayesian_Model = BayesInferwithDirPrior(prior, n, epsilon) Bayesian_Model._set_observation(gen_dataset_rand(len(prior._alphas), n)) Bayesian_Model._set_candidate_scores() candidates = Bayesian_Model._candidates expecterror = [] xstick = [] for c in candidates: expecterror.append(expect_error_Lap(c, prior, epsilon)) xstick.append(str(c._alphas)) return expecterror, xstick
def decomposed_probability_values(datasize,epsilon,delta,prior,observation): Bayesian_Model = BayesInferwithDirPrior(prior, datasize, epsilon, delta) Bayesian_Model._set_observation(observation) Bayesian_Model._set_candidate_scores() Bayesian_Model._set_local_sensitivities() nomalizer = Bayesian_Model._set_up_exp_mech_with_gamma_SS() probabilities_exp = {} for i in range(len(Bayesian_Model._candidates)): z = Bayesian_Model._candidates[i] probabilities_exp[str(z._alphas)] = nomalizer * Bayesian_Model._gamma_SS_probabilities[i] return nomalizer, probabilities_exp
def sensitivities(datasets, prior, beta, delta, label): sensitivities = [] for dataset in datasets: Bayesian_Model = BayesInferwithDirPrior(prior, sum(dataset), epsilon, delta) Bayesian_Model._set_observation(dataset) Bayesian_Model._set_candidate_scores() Bayesian_Model._set_local_sensitivities() candidates = Bayesian_Model._candidates for c in candidates: ######################################################################################################################### #GET THE ADJACENT DATA SET OF DATA SET C if (label == r"Local Sensitivity ($LS(x')$)"): sensitivities.append(Bayesian_Model._LS_Candidates[c]) elif (label == r"Our Sensitivity ($\frac{1}{\frac{1}{LS(x')} +\gamma \cdot d(x,x')}$)"): sensitivities.append((1.0 / (1.0/Bayesian_Model._LS_Candidates[c] + 1.0 * Hamming_Distance(Bayesian_Model._observation_counts, [c._alphas[i] - Bayesian_Model._prior._alphas[i] for i in range(Bayesian_Model._prior._size)])))) return sensitivities, [list(numpy.array(c._alphas) - 1) for c in candidates]
def exp_smooth_theoryProb(dataobs, prior, epsilon, gamma): n = sum(dataobs) Bayesian_Model = BayesInferwithDirPrior(prior, n, epsilon, 0.1, gamma) Bayesian_Model._set_observation(dataobs) Bayesian_Model._set_candidate_scores() Bayesian_Model._set_local_sensitivities() Bayesian_Model._gamma = gamma Bayesian_Model._set_up_exp_mech_with_gamma_SS() exp_prob = {} for i in range(len(Bayesian_Model._candidates)): z = Bayesian_Model._candidates[i]._pointwise_sub(prior) exp_prob[list2key( z._alphas)] = Bayesian_Model._gamma_SS_probabilities[i] return exp_prob
def smooth_sensitivity_study2(prior, sample_size, epsilon, delta, percentage): Bayesian_Model = BayesInferwithDirPrior(prior, sample_size, epsilon, delta) observation = [sample_size * i for i in percentage] Bayesian_Model._set_observation([sample_size * i for i in percentage]) Bayesian_Model._set_candidate_scores() Bayesian_Model._set_LS_Candidates() x = [(c._minus(Bayesian_Model._prior)) for c in Bayesian_Model._candidates] beta = 0.001 # math.log(1 - epsilon / (2.0 * math.log(delta / (2.0 * (sample_size))))) max_value_list = [] xstick = [ str((r._minus(Bayesian_Model._prior))._alphas) for r in Bayesian_Model._candidates ] for t in x: Bayesian_Model._set_observation(t._alphas) # Bayesian_Model._set_candidate_scores() # Bayesian_Model._set_LS_Candidates() max_value = 0.0 max_y = '' # max_y_list = [] for r in Bayesian_Model._candidates: temp = Bayesian_Model._LS_Candidates[r] * math.exp( -beta * Hamming_Distance(t._alphas, r._alphas)) if max_value < temp: max_y = r._alphas max_value = temp # for r in Bayesian_Model._candidates: # temp = Bayesian_Model._LS_Candidates[r] * math.exp(- beta * Hamming_Distance(observation, r._alphas)) # if max_value == temp: # max_y_list.append(str(r._alphas)) max_value_list.append(max_value) print "when data set x = " + str( t._alphas) + ", smooth sensitivity: S(" + str( t._alphas) + ") = " + str(max_value) scatter_plot( y, xstick, 'Smooth Sensitivity Given Size' + str(sample_size), )
def sensitivities_2(datasizes, prior, beta, delta, label, gamma=1.0): sensitivities = [] candidates = [] for datasize in datasizes: Bayesian_Model = BayesInferwithDirPrior(prior, datasize, epsilon, delta, gamma) Bayesian_Model._set_observation(gen_dataset([0.5,0.5],datasize)) Bayesian_Model._set_candidate_scores() Bayesian_Model._set_local_sensitivities() candidates = [list(numpy.array(c._alphas) - 1) for c in Bayesian_Model._candidates] if(label == r"Local Sensitivity ($LS(x)$)"): for c in Bayesian_Model._candidates: sensitivities.append(Bayesian_Model._LS_Candidates[c]) elif(label == r"Our Sensitivity ($\max_{x'}(\frac{1}{\frac{1}{LS(x')} +\gamma \cdot d(x,x')})$)"): for c in candidates: Bayesian_Model._set_observation(c) Bayesian_Model._set_up_exp_mech_with_LS() Bayesian_Model._set_up_exp_mech_with_gamma_SS() sensitivities.append(Bayesian_Model._gamma_SS) return sensitivities, candidates
def ss_ls_component_study(prior, sample_size, epsilon, delta, percentage): Bayesian_Model = BayesInferwithDirPrior(prior, sample_size, epsilon, delta) observation = [sample_size * i for i in percentage] Bayesian_Model._set_observation([sample_size * i for i in percentage]) Bayesian_Model._set_candidate_scores() Bayesian_Model._set_LS_Candidates() xstick = [str(c._alphas) for c in Bayesian_Model._candidates] beta = math.log(1 - epsilon / (2.0 * math.log(delta / (2.0 * (sample_size))))) y = [ 1.0 / ((1 - Bayesian_Model._LS_Candidates[r]**2)) for r in Bayesian_Model._candidates ] print abs(y[2] - y[1]) scatter_plot(y, xstick, "", r"$\frac{1}{LS((\alpha, 100 - \alpha))}$") return
def accuracy_VS_mean(sample_size, epsilon, delta, prior): data = [] xstick = [] temp = BayesInferwithDirPrior(prior, sample_size, epsilon, delta) temp._set_candidate_scores() observations = temp._candidates for i in range(len(observations)): observations[i]._minus(prior) for observation in observations: Bayesian_Model = BayesInferwithDirPrior(prior, sample_size, epsilon, delta) Bayesian_Model._set_observation(observation._alphas) Bayesian_Model._experiments(500) data.append(Bayesian_Model._accuracy[Bayesian_Model._keys[3]]) data.append(Bayesian_Model._accuracy[Bayesian_Model._keys[0]]) xstick.append(str(observation._alphas) + "/ExpMech") xstick.append(str(observation._alphas) + "/Laplace") plot_error_box(data, "Different Prior Distributions", xstick, "Accuracy VS. Prior Distribution") return
def mean_error_fix_n(n, prior, epsilon, times, mech): Bayesian_Model = BayesInferwithDirPrior(prior, n, epsilon) Bayesian_Model._set_observation(gen_dataset_rand(len(prior._alphas), n)) Bayesian_Model._set_candidate_scores() candidates = Bayesian_Model._candidates meanerror = [] xstick = [] for c in candidates: accuracy = [] Bayesian_Model._set_observation(c._alphas) for i in range(times): if (mech == "lap"): Bayesian_Model._laplace_mechanism_no_post(sensitivity=1.0) elif (mech == "lappost"): Bayesian_Model._laplace_mechanism_symetric(sensitivity=1.0) accuracy.append(Bayesian_Model._posterior - Bayesian_Model._laplaced_posterior) meanerror.append(numpy.mean(accuracy)) xstick.append(str(list(numpy.array(c._alphas) - 1))) return meanerror, xstick
def row_discrete_probabilities(sample_size, epsilon, delta, prior, observation): Bayesian_Model = BayesInferwithDirPrior(prior, sample_size, epsilon, delta) Bayesian_Model._set_observation(observation) print Bayesian_Model._observation_counts Bayesian_Model._set_candidate_scores() Bayesian_Model._set_local_sensitivities() Bayesian_Model._set_up_exp_mech_with_gamma_SS() Bayesian_Model._set_up_exp_mech_with_SS() Bayesian_Model._set_up_exp_mech_with_GS() Bayesian_Model._set_up_exp_mech_with_LS() ############################################################################# #SPLIT THE BINS ############################################################################# Candidate_bins_by_step = {} probability_distance_pairs_in_exp = [] nomalizer = 0.0 sorted_scores = sorted(Bayesian_Model._candidate_scores.items(), key=operator.itemgetter(1)) counter = 0 while counter < len(sorted_scores): flage = counter key = str(sorted_scores[flage][1]) Candidate_bins_by_step[key] = [] # parameters_of_bin = [] while counter < len(sorted_scores) and sorted_scores[flage][ 1] == sorted_scores[counter][1]: Candidate_bins_by_step[key].append(sorted_scores[counter][0]) # parameters_of_bin.append(sorted_scores[counter][0]._alphas) counter += 1 # print parameters_of_bin print key ############################################################################# #SPLIT THE BINS ############################################################################# # Candidate_bins_by_step = {} # for r in Bayesian_Model._candidates: # if str(sorted(r._alphas)) not in Candidate_bins_by_step.keys(): # Candidate_bins_by_step[str(sorted(r._alphas))] = [] # for c in Bayesian_Model._candidates: # if set(c._alphas) == set(r._alphas): # Candidate_bins_by_step[str(sorted(r._alphas))].append(c) ############################################################################# #SUM UP the prob within the same bin ############################################################################# # exp = calculate_prob_exp(Candidate_bins_by_step, Bayesian_Model, mechanism_parameter = 4, # sensitivity = Bayesian_Model._SS, savename = "_exp.txt") ############################################################################# #SUM UP the prob within the same bin ############################################################################# exp_gamma = calculate_prob_exp(Candidate_bins_by_step, Bayesian_Model, "gamma") ############################################################################# #SUM UP the prob within the same bin ############################################################################# exp_LS = calculate_prob_exp(Candidate_bins_by_step, Bayesian_Model, "local") ############################################################################# #SUM UP the prob within the same bin ############################################################################# exp_GS = calculate_prob_exp(Candidate_bins_by_step, Bayesian_Model, "exp") ############################################################################# #SETTING THE SENSITIVITY ############################################################################# if (len(prior._alphas) == 2): sensitivity1 = 1.0 sensitivity2 = 2.0 else: sensitivity1 = 2.0 sensitivity2 = len(prior._alphas) * 1.0 ############################################################################# #CALCULATE the Laplace prob within the same bin ############################################################################# step, lap_1 = calculate_prob_lap(Candidate_bins_by_step, Bayesian_Model, sensitivity=sensitivity1, savename="_lap_1.txt") step, lap_2 = calculate_prob_lap(Candidate_bins_by_step, Bayesian_Model, sensitivity=sensitivity2, savename="_lap_2.txt") print step, exp_gamma, lap_1, lap_2 # return ############################################################################# #PLOT the prob within the same bin ############################################################################# ############################################################################# #LABELS SETTING ############################################################################# labels = [ r'Alg 5 - $\mathsf{EHDS}$ ', r"Alg 4 - $\mathsf{EHDL}$", r"Alg 3 - $\mathsf{EHD}$", r'Alg 1 - $\mathsf{LSDim}$ (sensitivity = ' + str(sensitivity2) + ')', r'Alg 2 - $\mathsf{LSHist}$ (sensitivity = ' + str(sensitivity1) + ')' ] ############################################################################# #PLOTTING ############################################################################# plt.figure() plt.plot(step, lap_2, '-', color='lightblue', label=(labels[3])) plt.plot(step, lap_1, '-', color='navy', label=(labels[4])) plt.plot(step, exp_GS, '-', label=(labels[2])) plt.plot(step, exp_LS, '-', label=(labels[1])) plt.plot(step, exp_gamma, '-', label=(labels[0])) # plt.plot(step, exp, label=(labels[0])) # plt.plot(step, exp_new, label=(labels[1])) # plt.plot(step, exp_LS, label=(labels[2])) # plt.plot(step, exp_GS, label=(labels[3])) # plt.plot(step, lap_1, color = 'navy', label=(labels[4])) # plt.plot(step, lap_2, color = 'lightblue', label=(labels[5])) ############################################################################# #PLOT FEATURE SETTING ############################################################################# plt.xlabel("c / Hellinger distance from true posterior") plt.ylabel("Pr[H(BI(x),r) = c]") plt.title("Accuracy with Data size " + str(sample_size), fontsize=15) plt.legend() plt.grid() plt.show()