def main():
    records = readdata()
    K = [5, 10, 50, 100]
    KAnony = KAnonymity(records)
    for k in K:
        print('############# k-anonymity for k={} #############: \n'.format(k))
        KAnony.anonymize(k=k)
        print('\n')
Esempio n. 2
0
def evaluate_laplace_mechanism(eps=[0.5, 1]):
    """
    Evaluate for Laplace Mechanism
    """
    recordsv0 = readdata()
    recordsv1, recordsv2, recordsv3 = generate_data_for_laplace_mechanism(
        recordsv0)

    res1000 = {e: [] for e in eps}
    # res4000 = {e: [] for e in eps}
    rmse = {e: 0 for e in eps}
    """
    evaluate for epsilon = 0.5 and 1 for 1000 queries
    """
    printsent = [
        'original data', 'data removed a record with the oldest age',
        'data removed any record with age 26',
        'data removed any record with the youghest age'
    ]
    i = 0
    for records in (recordsv0, recordsv1, recordsv2, recordsv3):
        print('############ Processing for {} ############'.format(
            printsent[i]))
        i += 1
        LampMec = laplace_mechanism.LaplaceMechanism(records)
        for e in eps:
            print('query 1000 results with epsilon = {}'.format(e))
            res1000[e].append(LampMec.query_with_dp(e, querynum=1000))
            # res4000[e].append(LampMec.query_with_dp(e, querynum=4000))
            rmse[e] = LampMec.calc_distortion(
                LampMec.query_with_dp(e, querynum=4000))

    print('\n')
    for e in eps:
        print('############ Prove the {}-indistinguishable'.format(e))
        for i in range(1, 4):
            tmpresij, tmpresji = laplace_mechanism.prove_indistinguishable(
                res1000[e][0], res1000[e][i])
            print('** {} ** OVER ** {} **:'.format(printsent[0], printsent[i]))
            print(tmpresij)
            print('** {} ** OVER ** {} **:'.format(printsent[i], printsent[0]))
            print(tmpresji)
            print('exp^e = {}'.format(math.exp(e)))
            print('\n')

    print('############ Measure the distortion (RMSE) ############')
    for e in eps:
        print('RMSE for e = {}: {}'.format(e, rmse[e]))
    print('Distortion of e=1 is smaller than e=0.5 ?: ',
          True if rmse[1] <= rmse[0.5] else False)
    del recordsv0
    del recordsv1
    del recordsv2
    del recordsv3
Esempio n. 3
0
def evaluate_exponential_mechanism(eps=[0.5, 1]):
    """
    Evaulate for Exponential Mechanism
    """

    recordsv0 = readdata()
    recordsv1, recordsv2, recordsv3 = generate_data_for_exponential_mechanism(
        recordsv0)

    res1000 = {e: [] for e in eps}
    # res4000 = {e: [] for e in eps}
    dist = {e: 0 for e in eps}
    """
    evaluate for epsilon = 0.5 and 1 for 1000 queries
    """
    printsent = [
        'original data', 'data removed a record with most frequent education',
        'data removed a record with second most frequent education',
        'data removed any record with the least frequent education'
    ]
    i = 0
    for records in (recordsv0, recordsv1, recordsv2, recordsv3):
        print('############ Processing for {} ############'.format(
            printsent[i]))
        i += 1
        ExpMe = exponential_mechanism.ExponentialMechanism(records)
        for e in eps:
            print('query 1000 results with epsilon = {}'.format(e))
            res1000[e].append(ExpMe.query_with_dp(e, querynum=1000))
            # res4000[e].append(LampMec.query_with_dp(e, querynum=4000))
            dist[e] = ExpMe.calc_distortion(
                ExpMe.query_with_dp(e, querynum=4000))

    print('\n')
    for e in eps:
        print('############ Prove the {}-indistinguishable'.format(e))
        for i in range(1, 4):
            tmpresij, tmpresji = exponential_mechanism.prove_indistinguishable(
                res1000[e][0], res1000[e][i])
            print('** {} ** OVER ** {} **:'.format(printsent[0], printsent[i]))
            print(tmpresij)
            print('** {} ** OVER ** {} **:'.format(printsent[i], printsent[0]))
            print(tmpresji)
            print('exp^e = {}'.format(math.exp(e)))
            print('\n')

    print('############ Measure the distortion (1-precision) ############')
    for e in eps:
        print('distortion for e = {}: {}'.format(e, dist[e]))
    print('Distortion of e=1 is smaller than e=0.5 ?: ',
          True if dist[1] <= dist[0.5] else False)
    for val1, val2 in zip(queryres1, queryres2):
        count1[math.floor((val1 - minval + 1) /
                          ((maxval - minval + 1) / bucketnum)) - 1] += 1
        count2[math.floor((val2 - minval + 1) //
                          ((maxval - minval + 1) / bucketnum)) - 1] += 1
    prob1 = list(map(lambda x: x / len(queryres1), count1))
    prob2 = list(map(lambda x: x / len(queryres2), count2))

    res1overres2 = sum(p1 / p2
                       for p1, p2 in zip(prob1, prob2) if p2 != 0) / bucketnum
    res2overres1 = sum(p2 / p1
                       for p1, p2 in zip(prob1, prob2) if p1 != 0) / bucketnum
    return res1overres2, res2overres1


if __name__ == "__main__":
    records = readdata()
    v1, v2, v3 = generate_data_for_laplace_mechanism(records)
    LapMe = LaplaceMechanism(records)
    res1 = LapMe.query_with_dp(0.5, 1000)
    print(res1)
    print(LapMe.calc_groundtruth())
    print(LapMe.calc_distortion(LapMe.query_with_dp(1, 1000)))
    LapMe2 = LaplaceMechanism(v1)
    res2 = LapMe2.query_with_dp(0.5, 1000)
    print(LapMe.calc_distortion(res1))
    print(LapMe2.calc_distortion(res2))
    print(prove_indistinguishable(res1, res2))
    print(prove_indistinguishable(res2, res1))
    print(math.exp(0.5))