diff-priv 0.1.0

k-anonymity, (c,l)-diversity and ε-differential privacy framework
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
use crate::data_manipulation::aggregation::truncate_to_domain;
use num::abs;
use rand::distributions::{Distribution, Uniform};
use rand::thread_rng;
use rand_distr::Normal;
use serde::Serialize;
use std::time::SystemTime;
use uuid::Uuid;

#[derive(Hash, Eq, PartialEq)]
pub enum SensitiveAttribute {
    String(String),
    Integer(i32),
}

/// value, min_value, max_value, weight of attribute
pub type IntervalType = (
    QuasiIdentifierType,
    QuasiIdentifierType,
    QuasiIdentifierType,
    usize,
);

/// rank, max_rank, weight of attribute
pub type OrdinalType = (i32, i32, usize);

/// value, max value, weight of attribute
pub type NominalType = (i32, i32, usize);

#[derive(Debug, Copy, Clone)]
pub enum QuasiIdentifierType {
    Float(f64),
    Integer(i32),
}

/// Possible quasi identifier data category types
#[derive(Debug)]
pub enum QuasiIdentifierTypes {
    /// value, min_value, max_value, weight of attribute
    Interval(IntervalType),
    /// rank, max_rank, weight of attribute
    Ordinal(OrdinalType),
    /// value, weight of attribute
    Nominal(NominalType),
}

impl QuasiIdentifierTypes {
    /// consume itself and extract the value of the quasi identifier
    pub fn extract_value(self) -> QuasiIdentifierType {
        match self {
            QuasiIdentifierTypes::Interval((value, _, _, _)) => value,
            QuasiIdentifierTypes::Ordinal((value, _, _)) => QuasiIdentifierType::Integer(value),
            QuasiIdentifierTypes::Nominal((value, _, _)) => QuasiIdentifierType::Integer(value),
        }
    }

    pub fn randomize(self) -> QuasiIdentifierTypes {
        let mut rng = thread_rng();
        match self {
            QuasiIdentifierTypes::Interval((value, min, max, weight)) => match (value, min, max) {
                (
                    QuasiIdentifierType::Float(val_fl),
                    QuasiIdentifierType::Float(min_val),
                    QuasiIdentifierType::Float(max_val),
                ) => {
                    let normal: Normal<f64> = Normal::new(val_fl, 1.0).unwrap();
                    let e = normal.sample(&mut rng);
                    QuasiIdentifierTypes::Interval((
                        QuasiIdentifierType::Float(truncate_to_domain(e, min_val, max_val)),
                        QuasiIdentifierType::Float(min_val),
                        QuasiIdentifierType::Float(max_val),
                        weight,
                    ))
                }
                (
                    QuasiIdentifierType::Integer(val_int),
                    QuasiIdentifierType::Integer(min_val),
                    QuasiIdentifierType::Integer(max_val),
                ) => {
                    let normal: Normal<f64> = Normal::new(val_int as f64, 1.0).unwrap();
                    let e = normal.sample(&mut rng);
                    QuasiIdentifierTypes::Interval((
                        QuasiIdentifierType::Integer(truncate_to_domain(
                            e as i32, min_val, max_val,
                        )),
                        QuasiIdentifierType::Integer(min_val),
                        QuasiIdentifierType::Integer(max_val),
                        weight,
                    ))
                }
                _ => panic!("Wrong combination of type found in randomization of interval"),
            },
            QuasiIdentifierTypes::Ordinal((_, max_rank, weight)) => {
                let between = Uniform::<i32>::from(0..max_rank + 1);
                let random_ordinal_qi = between.sample(&mut rng);
                QuasiIdentifierTypes::Ordinal((random_ordinal_qi as i32, max_rank, weight))
            }
            QuasiIdentifierTypes::Nominal((_, max_value, weight)) => {
                let between = Uniform::<i32>::from(0..max_value + 1);
                let random_nominal_qi = between.sample(&mut rng);
                QuasiIdentifierTypes::Nominal((random_nominal_qi, max_value, weight))
            }
        }
    }
}

/// The role of this trait is to create a generic way of making sure that the struct can be anonymized
/// using the Anonymizer
pub trait Anonymizable: Default + Clone + Serialize + Sync {
    /// compare 2 data points and return the euclidean difference between them
    fn calculate_difference(&self, other: &Self) -> f64 {
        let mut sum_weight: usize = 0;
        let diff: f64 = self
            .quasi_identifiers()
            .into_iter()
            .zip(other.quasi_identifiers().into_iter())
            .map(|(x, y)| match (x, y) {
                (
                    QuasiIdentifierTypes::Interval(interval_x),
                    QuasiIdentifierTypes::Interval(interval_y),
                ) => {
                    let (_, _, _, weight) = interval_x;
                    sum_weight += weight;
                    Self::calculate_interval_distance(interval_x, interval_y)
                }
                (
                    QuasiIdentifierTypes::Ordinal(ordinal_x),
                    QuasiIdentifierTypes::Ordinal(ordinal_y),
                ) => {
                    let (_, _, weight) = ordinal_x;
                    sum_weight += weight;
                    Self::calculate_ordinal_distance(ordinal_x, ordinal_y)
                }
                (
                    QuasiIdentifierTypes::Nominal(nominal_x),
                    QuasiIdentifierTypes::Nominal(nominal_y),
                ) => {
                    let (_, _, weight) = nominal_x;
                    sum_weight += weight;
                    Self::calculate_nominal_distance(nominal_x, nominal_y)
                }
                _ => {
                    panic!("wrong types provided")
                }
            })
            .sum();

        diff / sum_weight as f64
    }

    /// calculate the info loss between 2 different Anonymizable
    /// structs
    fn calculate_info_loss(&self, other: &Self) -> f64 {
        let mut distance = 0.0;
        let self_qi = self.quasi_identifiers();
        let other_qi = other.quasi_identifiers();

        self_qi
            .into_iter()
            .zip(other_qi.into_iter())
            .for_each(|(x, y)| match (x.extract_value(), y.extract_value()) {
                (QuasiIdentifierType::Integer(value1), QuasiIdentifierType::Integer(value2)) => {
                    distance += (value1 as f64 - value2 as f64).powi(2)
                }
                (QuasiIdentifierType::Float(value1), QuasiIdentifierType::Float(value2)) => {
                    distance += (value1 - value2).powi(2)
                }
                _ => {
                    panic!("Incompatible values have been found")
                }
            });

        distance.sqrt()
    }

    /// return the values of the quasi identifiers in the data struct
    fn quasi_identifiers(&self) -> Vec<QuasiIdentifierTypes>;

    /// return a copy of the Anonymizable struct and replace its
    /// quasi identifier attributes with given QI's
    /// we return a copy because we want to keep the original intact for new aggregation
    fn update_quasi_identifiers(&self, qi: Vec<QuasiIdentifierTypes>) -> Self;

    /// return a copy of the sensitive attribute of the struct
    fn sensitive_value(&self) -> SensitiveAttribute;

    /// extract all the values in string format to be used for creating CSV
    fn extract_string_values(&self, uuid: Uuid, dr: f64) -> Vec<String>;

    // get the timestamp that the tuple has entered the algorithm
    fn get_timestamp(&self) -> SystemTime;

    /// suppress the qi's based on a buffer of Anonymizables
    fn suppress(&self) -> Self {
        let suppressed_qi = self
            .quasi_identifiers()
            .into_iter()
            .map(|x| x.randomize())
            .collect();

        self.update_quasi_identifiers(suppressed_qi)
    }

    /// calculate the euclidean distance between 2 ordinal data category types
    /// TODO: clarify that ranking starts at 1
    fn calculate_ordinal_distance(ordinal_x: OrdinalType, ordinal_y: OrdinalType) -> f64 {
        let (rank1, max_rank, weight) = ordinal_x;
        let (rank2, _, _) = ordinal_y;

        let x = (rank1 as f64 - 1.0) / (max_rank as f64 - 1.0);
        let y = (rank2 as f64 - 1.0) / (max_rank as f64 - 1.0);

        (weight as f64)
            * Self::calculate_interval_distance(
                (
                    QuasiIdentifierType::Float(x),
                    QuasiIdentifierType::Float(1.0),
                    QuasiIdentifierType::Float(max_rank as f64),
                    weight,
                ),
                (
                    QuasiIdentifierType::Float(y),
                    QuasiIdentifierType::Float(1.0),
                    QuasiIdentifierType::Float(max_rank as f64),
                    weight,
                ),
            )
    }

    /// calculate the euclidean distance between 2 interval data types
    fn calculate_interval_distance(interval_x: IntervalType, interval_y: IntervalType) -> f64 {
        let (num1, min, max, weight) = interval_x;
        let (num2, _, _, _) = interval_y;

        match (num1, min, max, num2) {
            (
                QuasiIdentifierType::Float(x),
                QuasiIdentifierType::Float(min),
                QuasiIdentifierType::Float(max),
                QuasiIdentifierType::Float(y),
            ) => weight as f64 * abs(x - y) / (max - min),
            (
                QuasiIdentifierType::Integer(x),
                QuasiIdentifierType::Integer(min),
                QuasiIdentifierType::Integer(max),
                QuasiIdentifierType::Integer(y),
            ) => weight as f64 * abs(x as f64 - y as f64) / (max as f64 - min as f64),
            _ => {
                panic!("wrong type conversion")
            }
        }
    }

    /// calculate the euclidean distance between 2 nominal data types
    fn calculate_nominal_distance(nominal_x: NominalType, nominal_y: NominalType) -> f64 {
        let (x, _, weight) = nominal_x;
        let (y, _, _) = nominal_y;

        match x == y {
            true => 0.0,
            false => weight as f64,
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::data_manipulation::aggregation::AggregateType;
    use crate::data_manipulation::anonymizable::Anonymizable;
    use crate::data_manipulation::anonymizable::QuasiIdentifierType::{Float, Integer};
    use crate::data_manipulation::anonymizable::QuasiIdentifierTypes::{
        Interval, Nominal, Ordinal,
    };
    use crate::data_manipulation::mueller::MuellerStream;

    #[test]
    fn get_quasi_identifiers() {
        let mueller = MuellerStream {
            age: Some(32),
            gender: Some("male".to_string()),
            ..MuellerStream::default()
        };

        let mut quasi_identifiers = mueller.quasi_identifiers();

        match quasi_identifiers.remove(0) {
            Interval((Integer(32), Integer(33), Integer(85), 1)) => {}
            _ => {
                panic!()
            }
        }

        match quasi_identifiers.remove(0) {
            Nominal((0, 1, 1)) => {}
            _ => {
                panic!()
            }
        }
    }

    #[test]
    fn update_quasi_identifiers() {
        let mueller = MuellerStream {
            age: Some(32),
            gender: Some("male".to_string()),
            ..MuellerStream::default()
        };

        let centroid = MuellerStream {
            age: Some(50),
            gender: Some("female".to_string()),
            ..MuellerStream::default()
        };

        let anonymized = mueller.update_quasi_identifiers(centroid.quasi_identifiers());

        assert_eq!(anonymized.age, Some(50));
        assert_eq!(anonymized.gender, Some("female".to_string()))
    }

    #[test]
    fn calculate_difference() {
        let mueller = MuellerStream {
            age: Some(37),
            gender: Some("male".to_string()),
            ..MuellerStream::default()
        };

        let centroid = MuellerStream {
            age: Some(50),
            gender: Some("female".to_string()),
            ..MuellerStream::default()
        };

        let difference = mueller.calculate_difference(&centroid);

        assert_eq!(difference, 0.625)
    }

    #[test]
    fn calculate_difference_zero() {
        let mueller = MuellerStream {
            age: Some(37),
            gender: Some("male".to_string()),
            ..MuellerStream::default()
        };

        let centroid = MuellerStream {
            age: Some(37),
            gender: Some("male".to_string()),
            ..MuellerStream::default()
        };

        let difference = mueller.calculate_difference(&centroid);

        assert_eq!(difference, 0.0)
    }

    #[test]
    fn calculate_difference_one() {
        let mueller = MuellerStream {
            age: Some(33),
            gender: Some("male".to_string()),
            ..MuellerStream::default()
        };

        let centroid = MuellerStream {
            age: Some(85),
            gender: Some("female".to_string()),
            ..MuellerStream::default()
        };

        let difference = mueller.calculate_difference(&centroid);

        assert_eq!(difference, 1.0)
    }

    #[test]
    fn calculate_info_loss() {
        let mueller = MuellerStream {
            age: Some(33),
            gender: Some("male".to_string()),
            ..MuellerStream::default()
        };

        let centroid = MuellerStream {
            age: Some(50),
            gender: Some("female".to_string()),
            ..MuellerStream::default()
        };

        let info_loss = mueller.calculate_info_loss(&centroid);
        assert!((info_loss - 17.29) <= f64::EPSILON)
    }

    #[test]
    fn aggregation_interval_integer() {
        let agg1 = Interval((Integer(1), Integer(0), Integer(10), 1));
        let agg2 = Interval((Integer(4), Integer(0), Integer(10), 1));
        let agg3 = Interval((Integer(6), Integer(0), Integer(10), 1));
        let agg4 = Interval((Integer(10), Integer(0), Integer(10), 1));

        let aggregation = AggregateType::Mean(vec![agg1, agg2, agg3, agg4]).aggregate();

        if let Integer(value) = aggregation.extract_value() {
            assert_eq!(value, 5)
        } else {
            panic!()
        }
    }

    #[test]
    fn aggregation_interval_float() {
        let agg1 = Interval((Float(1.0), Float(0.0), Float(10.0), 1));
        let agg2 = Interval((Float(4.0), Float(0.0), Float(10.0), 1));
        let agg3 = Interval((Float(6.0), Float(0.0), Float(10.0), 1));
        let agg4 = Interval((Float(10.0), Float(0.0), Float(10.0), 1));

        let aggregation = AggregateType::Mean(vec![agg1, agg2, agg3, agg4]).aggregate();

        if let Float(value) = aggregation.extract_value() {
            assert_eq!(value, 5.25)
        } else {
            panic!()
        }
    }

    #[test]
    fn aggregation_ordinal() {
        let agg1 = Ordinal((1, 10, 1));
        let agg2 = Ordinal((1, 10, 1));
        let agg3 = Ordinal((2, 10, 1));
        let agg4 = Ordinal((4, 10, 1));

        let aggregation = AggregateType::Mode(vec![agg1, agg2, agg3, agg4]).aggregate();

        if let Integer(value) = aggregation.extract_value() {
            assert_eq!(value, 1)
        } else {
            panic!()
        }
    }

    #[test]
    fn aggregation_nominal() {
        let agg1 = Nominal((1, 4, 10));
        let agg2 = Nominal((1, 4, 10));
        let agg3 = Nominal((2, 4, 10));
        let agg4 = Nominal((4, 4, 10));

        let aggregation = AggregateType::Mode(vec![agg1, agg2, agg3, agg4]).aggregate();

        if let Integer(value) = aggregation.extract_value() {
            assert_eq!(value, 1)
        } else {
            panic!()
        }
    }
}