Data privacy: masking and anonymisation
Practical and exam-focused notes on data privacy techniques: masking and anonymisation including k-anonymity, l-diversity, t-closeness, and differential privacy with definitions, formulas, proofs and examples.
Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.
Why it matters
Data that a database stores can identify people. Engineers use privacy techniques when sharing data for research, analytics, or third-party services to reduce the risk of exposing individuals. This topic shows practical methods to mask or anonymise data so the data remain useful for analysis while protecting identities and sensitive attributes.
Key ideas
Start with terms you will see often.
- Dataset / microdata: the concrete table of records about individuals. I will call this the original dataset.
- Identifier (explicit identifier): a column that directly identifies a person (for example, Name or Aadhaar Number).
- Quasi-identifier: a column or set of columns (for example, Age, Gender, ZIP code) that do not directly identify a person by themselves but can when combined with other data. I will bold quasi-identifier the first time I use it. (Defined here: a combination of attributes whose values can be linked with external data to re-identify records.)
- Sensitive attribute: a column whose value must be protected (for example, Disease, Salary).
- Anonymisation (or anonymization) : the process that transforms a dataset so individuals cannot be re-identified from the released data. I use anonymisation to mean transformations intended to prevent re-identification even by an attacker with external data.
- Masking: simpler transformations used to hide values (for example, replacing a name by XXXXXX). Masking may not guarantee anonymisation.
- Re-identification / linkage attack: using external data to match quasi-identifiers to identify an individual in the released data.
Formal privacy models commonly used in database releases:
- k-anonymity
- Definition (exact): A released table R satisfies k-anonymity (for integer k ≥ 1) with respect to a set of quasi-identifiers Q if for every combination of values of Q that appears in R, that combination appears in at least k records. Equivalently, each record is indistinguishable from at least k−1 other records on Q.
- Assumptions: the attacker uses only Q and external tables to perform record linkage. No guarantee against attribute disclosure if all records in an equivalence class share the same sensitive value.
- Typical operations to achieve k-anonymity: generalization (replace specific values with coarser ones, e.g., age 27 → 20–29) and suppression (remove or replace by *). Draw a table before and after. Note: generalization can be global (same mapping applied to an entire attribute) or local (different records may be generalized to different levels).
- l-diversity
- Definition (exact): An equivalence class (set of records sharing the same Q values) is l-diverse if it contains at least l "well-represented" distinct values for the sensitive attribute. A table satisfies l-diversity if every equivalence class is l-diverse. "Well-represented" has variants: distinct l-diversity (≥ l distinct values), entropy l-diversity (entropy threshold), and recursive (c)-diversity. In distinct l-diversity, each equivalence class E must contain at least l distinct sensitive values; in entropy l-diversity, letting p_E(v) be the class distribution over sensitive values, −∑_v p_E(v) log p_E(v) ≥ log l.
- Assumptions: attacker may have background knowledge; l-diversity reduces risk of attribute disclosure by ensuring diversity of sensitive values inside each equivalence class.
- t-closeness
- Definition (exact): An equivalence class satisfies t-closeness if the distance between the distribution of the sensitive attribute in the class and the distribution of the attribute in the whole table is no more than t, where distance is measured by an appropriate metric (commonly Earth Mover's Distance, EMD). A table satisfies t-closeness if every equivalence class does.
- Assumptions: knowledge of global sensitive distribution; t controls how close local distributions can be to global one.
- Differential privacy
- Definition (exact): A randomized mechanism M gives ε-differential privacy (ε ≥ 0) if for any two datasets D and D' that differ in exactly one record, and for any measurable subset S of outputs, we have: Pr[M(D) ∈ S] ≤ exp(ε) · Pr[M(D') ∈ S]. The probability is over the randomness of M. ε is called the privacy budget and is dimensionless. Smaller ε → stronger privacy.
- Intuition: output distributions don't change much if any single record is changed or removed, so the presence/absence of a single person's data has a limited effect.
- Important properties (exact statements):
- Composition: If M1 is ε1-DP and M2 is ε2-DP and both are applied (possibly adaptively), the combined mechanism is (ε1 + ε2)-DP (basic composition).
- Post-processing invariance: If M is ε-DP and f is any function, then f(M(D)) is also ε-DP. (Randomised outputs cannot be made less private by further processing.)
- Parallel composition: If M1 and M2 operate on disjoint subsets of individuals (no person's data is included in both), overall privacy is max(ε1, ε2) rather than ε1 + ε2.
- Mechanisms: Add carefully calibrated noise to query results. For numerical queries, the Laplace mechanism is standard; for counts, sensitivity is often 1.
Proofs and derivations commonly asked in exams:
- Show Laplace mechanism gives ε-DP when noise scale b = Δf / ε, where Δf is global sensitivity (maximum change in query result when one record changes). A step-by-step algebraic derivation is below in Worked examples.
Diagrams to draw for exams:
- For k-anonymity: draw two small tables (before and after). Label columns Name (suppressed), Age (generalized), ZIP (prefix), Disease. Show equivalence classes (boxes around rows sharing Q values) and label sizes.
- For differential privacy: draw two datasets D and D' differing by one record, arrow to mechanism M which outputs a noisy value; label probabilities Pr[M(D)=y] and Pr[M(D')=y] and show ratio ≤ e^ε.
Formulas
Pr[M(D) ∈ S] ≤ exp(ε) · Pr[M(D') ∈ S] — ε-differential privacy definition. ε is dimensionless. Applies to all D, D' differing by one record and any output set S.
Δf = max_{D,D' differing by one record} |f(D) − f(D')| — global sensitivity. f is the query function (unit depends on f; e.g., counts → dimensionless integer). Examples: for simple counts, Δf = 1; for a sum over values in [a,b], Δf = b − a; for a mean over a fixed-size dataset of n records with values in [a,b], Δf = (b − a)/n.
Laplace pdf(x; μ,b) = (1/(2b)) · exp(−|x − μ|/b) — Laplace probability density function. μ is location (noisy answer centred at f(D)), b is scale parameter (same units as f's output).
b = Δf / ε — noise scale for Laplace mechanism. Δf same units as f; ε dimensionless. Applies when using Laplace mechanism to achieve ε-DP.
M(D) = f(D) + Lap(0,b) — Laplace mechanism: add Laplace noise with scale b to true answer f(D).
Composition: ε_total = ε1 + ε2 + ... — for sequential composition of mechanisms. ε_i dimensionless.
Worked examples
Example 1 — Achieve 2-anonymity by generalization/suppression (step-by-step) Original table (5 records):
- Name=Ravi, Age=28, ZIP=400088, Disease=Flu
- Name=Anita, Age=27, ZIP=400089, Disease=Flu
- Name=Sunil, Age=35, ZIP=400088, Disease=Cancer
- Name=Meena, Age=40, ZIP=400090, Disease=Cancer
- Name=Ramesh, Age=28, ZIP=400089, Disease=Flu
Identify Q = {Age, ZIP}. Goal: k = 2. Step 1: remove explicit identifier Name (suppression of Name column). Step 2: generalize Age: replace exact ages by decade ranges: 27,28 → 20–29; 35→30–39; 40→40–49. Step 3: generalize ZIP by keeping first 5 digits as prefix 40008x → 40008*, 40009x → 40009* (here 400088 and 400089 → 40008*; 400090 → 40009*). Resulting table:
- Age=20–29, ZIP=40008*, Disease=Flu (rows from original 1,2,5) → equivalence class size = 3 ≥ 2.
- Age=30–39, ZIP=40008*, Disease=Cancer (row 3) → size = 1 < 2: not OK.
- Age=40–49, ZIP=40009*, Disease=Cancer (row 4) → size = 1 < 2: not OK. Step 4: To fix remaining singletons, suppress ZIP for rows 3 and 4 and also generalize Age to 30–49 so they form an equivalence class of size 2, or further generalize both quasi-identifiers so that rows 3 and 4 share the same Q-values. Final choice: generalize Age for rows 3 and 4 to 30–49, ZIP to 4000* (coarser), producing a 2-anonymous table. This demonstrates how to reach k-anonymity.
Example 2 — Laplace mechanism proof sketch and numeric example Query f(D) = count of records with Disease=Flu. Sensitivity Δf = 1 (adding/removing one person changes count by at most 1). Choose ε = 0.5 (dimensionless). Then b = Δf/ε = 1 / 0.5 = 2. Mechanism M(D) = f(D) + Lap(0,2). Proof sketch (algebra): For any output y, Laplace pdf ratio for datasets D and D' differs only by change in f by at most Δf: Pr[M(D)=y] / Pr[M(D')=y] = exp(|y − f(D')|/b − |y − f(D)|/b) ≤ exp(Δf / b) = exp(ε). Thus M is ε-DP when b = Δf/ε. (Show the inequality using triangle inequality: |y − f(D')| − |y − f(D)| ≤ |f(D) − f(D')| ≤ Δf.) Numeric sample: if f(D) = 100, mechanism returns y = 100 + noise. The noise distribution scale is 2; noise typical magnitude O(2). This hides whether true count was 100 or 101 within factor exp(0.5) ≈ 1.65 in likelihood.
Common mistakes
- Confusing pseudonymization (replace identifiers with tokens; reversible with mapping) with full anonymisation (irreversible). Pseudonymized data may still be personal data under law.
- Using differential privacy with very large ε and claiming strong privacy. ε must be small and chosen carefully; very large ε gives little privacy.
- Forgetting composition: multiple DP queries add up privacy loss; do not ignore budget tracking.
For your exam
Question types you will see
- Definitions: state k-anonymity, l-diversity, t-closeness, differential privacy exactly, and list assumptions.
- Short notes: describe pros/cons of masking vs anonymisation, describe attacks (linkage, homogeneity, background knowledge).
- Derivations/proofs: show Laplace mechanism achieves ε-DP with b = Δf/ε; prove post-processing invariance or composition (short form).
- Numericals: given a small table, produce k-anonymous release using generalization/suppression and explain choices; compute noise scale b for DP and show a noisy answer.
- Diagrams: draw before/after tables for k-anonymity and D/D'/M for differential privacy.
What a full-marks answer includes
- Precise definitions with bold/unambiguous terms (quasi-identifier, sensitive attribute).
- Clear assumptions (attacker model: what external data is available, whether attacker knows dataset size, etc.).
- For constructive questions: show step-by-step transformations and final table with equivalence class sizes.
- For DP proofs: show algebraic steps using triangle inequality and state the global sensitivity explicitly.
Quick check
- What is a quasi-identifier?
- State the definition of ε-differential privacy in one line.
- For a count query, what is the global sensitivity Δf?
Answers: A quasi-identifier is a set of attributes that can be combined with external data to re-identify records; Pr[M(D) ∈ S] ≤ exp(ε) · Pr[M(D') ∈ S] for all neighbour D,D' differing by one record and all S; Δf = 1 for a simple count query.
Finished this topic? Mark it so your progress, study plan and readiness keep up.
Stuck on something here?