Definitioncore
l-diversity
Also called l-diverse table.
Requires each group of records with the same quasi-identifiers to hold at least l well-represented values of the sensitive attribute.
l-diversity is a condition on a released table that protects the sensitive values, not only the identities. k-anonymity makes each record look like at least others on the quasi-identifier, so a linkage attack can only find a group of records. But if all records of that group have the same diagnosis, the attacker learns the diagnosis anyway. Machanavajjhala, Gehrke, Kifer and Venkitasubramaniam (ICDE 2006) named this the homogeneity attack, and they asked that each group hold at least "well-represented" sensitive values. They gave three exact meanings of "well-represented": distinct, entropy and recursive . The value of , and of , is a policy choice of the publisher, not a result. l-diversity is a syntactic condition on one release; it gives no bound of the kind that differential privacy gives, and t-closeness shows two attacks that it does not stop.
Definition
A table has a set of quasi-identifier attributes and one sensitive attribute . The rows of that have the same values on form a block (the paper says a -block; other papers say an equivalence class). For a block and a sensitive value , let be the number of rows of with value , and let . Sort the counts of the values that occur in as , so is the number of distinct sensitive values in and .
The principle. A block is -diverse if it holds at least well-represented values of . A table is -diverse if every block is -diverse. The three instances below make "well-represented" exact. Each one is a property of single blocks, so a table has it if and only if every block has it.
- Distinct -diversity. : the block holds at least distinct sensitive values.
- Entropy -diversity. The Shannon entropy of the block is at least :
The base of the logarithm does not matter, as long as both sides use the same base. Multiply by and take exponentials: the condition is the same as the integer test , which needs no floating point. 3. Recursive -diversity. For a constant ,
The most frequent value must not be too frequent compared with the tail that remains after the most frequent values. If the tail is empty, the right side is 0, and the block fails.
Some facts follow at once, and each is a theorem:
- Entropy and recursive imply distinct. , with equality for the uniform distribution, so forces . A recursive -diverse block has a non-empty tail, so .
- A bound on the whole table. The distribution of in is the mixture of the block distributions, with weights . Entropy is concave, so . If each block has , then . A table whose sensitive column has low entropy can be entropy -diverse only for a small , whatever the anonymization does.
- Monotonicity. If two blocks are -diverse in one of the three senses, then the block made by merging them is -diverse in the same sense. For entropy this is the concavity again. For the recursive form it holds because and the sum of the largest counts are both subadditive under merging. So a coarser generalization of an -diverse table is -diverse, and the lattice searches that prune by monotonicity, such as Incognito, work for l-diversity as they do for k-anonymity.
- Background knowledge in the recursive form. Suppose a block is recursive -diverse and the attacker can rule out one sensitive value for the target, for example because the target is male and the value is a pregnancy. Remove that value from the block. The rest is recursive -diverse. If the removed value had rank 1, the new largest count is and the new tail is the old tail. If it had rank , the largest count is still , and the new tail is the old tail, or the old tail with replaced by . So an attacker who can rule out values still faces a -diverse block, in which the most frequent value has fewer than times the rows of all the others.
proofs/correctness.md proves the monotonicity and the test below. The test reads each row once, groups the rows by their quasi-identifier values, and checks each block.
L-DIVERSE(T, Q, S, l, variant, c)- 1
B ← the blocks of T: the groups of rows with equal values on Q - 2
for each block E in B do - 3
r_1 ≥ r_2 ≥ ... ≥ r_m ← the counts of the distinct values of S in E - 4
if variant = distinct and m < l then return false - 5
if variant = entropy and |E|^|E| < l^|E| · r_1^r_1 ⋯ r_m^r_m then return false ▷ the same as H(E) < log l - 6
if variant = recursive and r_1 ≥ c · (r_l + ... + r_m) then return false - 7
return true
With a hash table for the blocks, line L1 takes expected time for rows with a fixed number of quasi-identifiers, in the word-RAM model. Line L3 sorts the counts, per block and in total. Line L5 multiplies integers with up to bits, which is cheap for the small blocks that releases use; a floating point version with a tolerance is the usual choice for large blocks.
Examples and non-examples
The table below is synthetic. It is already generalized: Age and ZIP code are the quasi-identifiers, and the disease is sensitive. It has three blocks of 4 rows, so it is 4-anonymous.
| block | Age | ZIP | diseases | counts |
|---|---|---|---|---|
| < 30 | 130** | heart, heart, heart, heart | 4 | |
| ≥ 40 | 1485* | flu, heart, cancer, flu | 2, 1, 1 | |
| 3* | 130** | flu, cancer, viral, heart | 1, 1, 1, 1 |
- is the homogeneity attack. An attacker who knows that a neighbour is under 30, lives in 130**, and is in the table, learns that the neighbour has heart disease, although the table is 4-anonymous. has , so the table is not even distinct 2-diverse.
- . Distinct: . Entropy: bits, and , so is entropy -diverse for but not for . The integer test for reads . Recursive: for the condition is , true for every ; for it is , so is -diverse but not -diverse.
- . All four values differ, bits, and is -diverse in every sense for each (recursive with any ).
- A repair by merging. Generalize Age in and to "any" and ZIP to 1****. The merged block has counts 5 (heart), 2 (flu), 1 (cancer). Its entropy is bits, and , so it is entropy 2-diverse. Monotonicity does not predict this, because was not 2-diverse; the merge works because brings enough other values. The merged block is recursive -diverse for (the test is ) and recursive -diverse only for .
- l = 1. Every non-empty block is 1-diverse in all three senses, so 1-diversity adds nothing to k-anonymity.
- The skewness attack (a non-example of protection). Li, Li and Venkatasubramanian take a test result with 99 percent negative and 1 percent positive records. A block with 50 percent positive records is distinct, entropy and recursive 2-diverse, yet it raises the chance that a member is positive from 1 percent to 50 percent. A block with 49 positive and 1 negative records is distinct 2-diverse and has the same entropy as one with 1 positive and 49 negative. Their Example 2 also notes that a distinct 2-diverse version of a table of 10000 such records can have at most blocks, because each block needs a positive record, so the information loss is large.
- The similarity attack (a non-example of protection). A block with the diseases gastric ulcer, gastritis and stomach cancer is distinct 3-diverse, but each value is a stomach disease, so the attacker learns that. The salaries 3k, 4k and 5k in one block are distinct, but all low. l-diversity treats the values as labels with no distance between them.
Related definitions
- k-anonymity: the condition that l-diversity extends. Distinct -diversity of a block with values implies that the block has at least rows, so a distinct -diverse table is -anonymous.
- Quasi-identifier and linkage attack: the attributes that define the blocks, and the attack that k-anonymity stops and l-diversity extends to sensitive values.
- t-closeness: asks that the distribution of in each block be close to its distribution in the whole table, under the earth mover's distance. It answers the skewness and similarity attacks above.
- Anatomy: releases the quasi-identifiers and the sensitive values in two tables joined by a group id, with -diverse groups, and gives a breach probability of at most without generalization.
- m-invariance: l-diversity for a table that is released again after insertions and deletions; each single release can be -diverse and the sequence can still leak.
- Generalization and suppression, Incognito and Mondrian: the methods that produce the blocks; each can test l-diversity in place of k-anonymity.
- Differential privacy: a condition on the release mechanism, not on the released table, that holds against every background knowledge.
Proof: The l-diversity test is exact, and the three instances survive the merging of blocksfull
We use the notation of the page. A block has the sorted positive counts of its sensitive values, with . For a count vector over the sensitive values (zeros allowed), write , , and for the sum of the largest entries of . Then recursive -diversity of a block with vector reads . If has fewer than positive entries, then , the right side is 0, and the block fails, which agrees with the page.
Claim 1 (the test). L-DIVERSE returns true if and only if every block of satisfies the chosen instance.
Proof. Line L1 puts two rows in the same group exactly when they agree on , so the groups are the blocks. Line L3 computes the sorted counts of the block of line L2. The test returns false only at lines L4 to L6, and only for a block that breaks the instance, provided that each line tests the instance exactly. Line L4 is the definition of distinct -diversity. Line L6 is the definition of the recursive form, because . For line L5, use :
Since and the logarithm and the exponential are increasing, holds if and only if , and line L5 returns false exactly when this fails. If no block fails, the loop ends and line L7 returns true. Each block is visited once, so the loop ends.
Claim 2 (monotonicity). Let blocks and have count vectors and , and let be the merged block, with vector . If and are both -diverse in one of the three senses, then is -diverse in the same sense.
Proof.
- Distinct. Each value that occurs in occurs in , so has at least as many distinct values as , which is at least .
- Entropy. Let . The distribution of is . The function is concave on distributions, because is concave on . So .
- Recursive. The condition is . is additive. is subadditive: for each value , . is subadditive: let be the set of values with the largest entries of ; then . With ,
By induction on the number of merges, a generalization whose blocks are unions of the blocks of an -diverse table is -diverse in the same sense.
Claim 3 (ruling out one value). Let . If a block is recursive -diverse, then for each value in it, the block without the rows of value is recursive -diverse.
Proof. Let have rank , so its count is , and let be the sorted counts after the removal. The old condition is .
- If , then , and the entries of from rank on are . So .
- If , then , and the entries from rank on are again . The same chain holds.
- If , then , and the entries from rank on are and the values without . Their sum is , because . The chain holds again.
Cost. Line L1 takes expected time with a hash table, line L3 takes over all blocks, and lines L4 and L6 take per block. Line L5 multiplies integers of bits.
Code
Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/l-diversity/. The authors are listed in prov/HGM0PD.jsonl.
from collections import Counter
from collections.abc import Hashable, Sequence
from fractions import Fraction
Row = Sequence[Hashable]
def blocks(table: Sequence[Row], qi: Sequence[int]) -> dict[tuple[Hashable, ...], list[Row]]:
"""Group the rows of the table by their values on the quasi-identifier columns qi.
Cost: O(n) expected time for n rows and a fixed number of columns.
"""
out: dict[tuple[Hashable, ...], list[Row]] = {}
for row in table:
out.setdefault(tuple(row[j] for j in qi), []).append(row)
return out
def block_is_diverse(
counts: Sequence[int], l: int, variant: str = "distinct", c: Fraction | int = 1
) -> bool:
"""Test one block, given the counts of its distinct sensitive values.
Precondition: every count is positive, l >= 1, c > 0, and variant is
"distinct", "entropy" or "recursive". The entropy test is exact: it
compares N^N with l^N * prod(r^r), where N is the size of the block.
Cost: O(m log m) for m values, plus the big integer products.
"""
r = sorted(counts, reverse=True) # @line L3
m = len(r)
size = sum(r)
if variant == "distinct":
return m >= l # @line L4
if variant == "entropy":
rhs = l**size
for x in r:
rhs *= x**x
return size**size >= rhs # @line L5
if variant == "recursive":
return Fraction(r[0]) < Fraction(c) * sum(r[l - 1 :]) if m > 0 else False # @line L6
raise ValueError("unknown variant " + repr(variant))
def is_l_diverse(
table: Sequence[Row],
qi: Sequence[int],
s: int,
l: int,
variant: str = "distinct",
c: Fraction | int = 1,
) -> bool:
"""Return True if every block of the table is l-diverse in the given sense.
table is a list of rows, qi the column indices of the quasi-identifier and
s the column index of the sensitive attribute. An empty table is l-diverse.
Precondition: l >= 1 and c > 0. Cost: O(n log n) for n rows in the word-RAM
model, plus the integer products of the entropy test.
"""
groups = blocks(table, qi) # @line L1
for rows in groups.values(): # @line L2
counts = list(Counter(row[s] for row in rows).values())
if not block_is_diverse(counts, l, variant, c):
return False
return True # @line L7
test_l_diversity.py 139 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/l-diversity/. The authors are listed in prov/HGM0PD.jsonl.
import itertools
import math
import random
from collections import Counter
from fractions import Fraction
from l_diversity import block_is_diverse, is_l_diverse
TABLE = [
("<30", "130**", "heart"),
("<30", "130**", "heart"),
("<30", "130**", "heart"),
("<30", "130**", "heart"),
(">=40", "1485*", "flu"),
(">=40", "1485*", "heart"),
(">=40", "1485*", "cancer"),
(">=40", "1485*", "flu"),
("3*", "130**", "flu"),
("3*", "130**", "cancer"),
("3*", "130**", "viral"),
("3*", "130**", "heart"),
]
def test_example_blocks() -> None:
assert not is_l_diverse(TABLE, [0, 1], 2, 2, "distinct")
e2 = [2, 1, 1]
assert block_is_diverse(e2, 3, "distinct")
assert block_is_diverse(e2, 2, "entropy")
assert not block_is_diverse(e2, 3, "entropy")
assert block_is_diverse(e2, 2, "recursive", Fraction(101, 100))
assert not block_is_diverse(e2, 2, "recursive", 1)
assert block_is_diverse(e2, 3, "recursive", 3)
assert not block_is_diverse(e2, 3, "recursive", 2)
e3 = [1, 1, 1, 1]
assert block_is_diverse(e3, 4, "entropy")
assert block_is_diverse(e3, 4, "recursive", 2)
def test_example_merge() -> None:
merged = [(a if a == "3*" else "any", z if a == "3*" else "1****", d) for a, z, d in TABLE]
assert is_l_diverse(merged, [0, 1], 2, 2, "entropy")
assert not is_l_diverse(merged, [0, 1], 2, 3, "entropy")
assert is_l_diverse(merged, [0, 1], 2, 2, "recursive", Fraction(17, 10))
assert not is_l_diverse(merged, [0, 1], 2, 2, "recursive", Fraction(5, 3))
assert is_l_diverse(merged, [0, 1], 2, 3, "recursive", 6)
assert not is_l_diverse(merged, [0, 1], 2, 3, "recursive", 5)
def test_edges() -> None:
for v in ("distinct", "entropy", "recursive"):
assert is_l_diverse([], [0], 1, 3, v, 2)
assert is_l_diverse([("a", "x")], [0], 1, 1, v, 2)
assert not is_l_diverse([("a", "x")], [0], 1, 2, v, 2)
# uniform block with exactly l values: entropy equals log l, which passes
assert block_is_diverse([3, 3, 3], 3, "entropy")
assert not block_is_diverse([3, 3, 2], 3, "entropy")
# skewness: 49 against 1 is distinct 2-diverse
assert block_is_diverse([49, 1], 2, "distinct")
assert not block_is_diverse([49, 1], 2, "entropy")
def oracle(counts: list[int], l: int, variant: str, c: Fraction) -> bool:
size = sum(counts)
if variant == "distinct":
return len(counts) >= l
if variant == "entropy":
h = -sum(x / size * math.log(x / size) for x in counts)
return h >= math.log(l) or math.isclose(h, math.log(l), rel_tol=1e-12, abs_tol=1e-12)
# recursive: the tail after the l - 1 largest counts is the smallest total
# that remains after removing any l - 1 of the values
if len(counts) < l:
return False
tail = min(size - sum(x) for x in itertools.combinations(counts, l - 1))
return max(counts) < c * tail
def random_counts(rng: random.Random) -> list[int]:
return [rng.randint(1, 6) for _ in range(rng.randint(1, 6))]
def test_against_oracle() -> None:
rng = random.Random(7)
for _ in range(1500):
counts = random_counts(rng)
l = rng.randint(1, 5)
c = Fraction(rng.randint(1, 12), rng.randint(1, 4))
for v in ("distinct", "entropy", "recursive"):
assert block_is_diverse(counts, l, v, c) == oracle(counts, l, v, c)
def test_table_against_oracle() -> None:
rng = random.Random(11)
for _ in range(500):
n = rng.randint(0, 20)
table = [(rng.randint(0, 2), rng.randint(0, 1), rng.choice("abcde")) for _ in range(n)]
l = rng.randint(1, 4)
for v in ("distinct", "entropy", "recursive"):
want = all(
oracle(list(Counter(r[2] for r in table if (r[0], r[1]) == key).values()), l, v, Fraction(2))
for key in {(r[0], r[1]) for r in table}
)
assert is_l_diverse(table, [0, 1], 2, l, v, 2) == want
def merge(a: list[tuple[str, int]], b: list[tuple[str, int]]) -> list[int]:
total: Counter[str] = Counter()
for s, x in a + b:
total[s] += x
return list(total.values())
def test_monotone_under_merge() -> None:
rng = random.Random(3)
for _ in range(1000):
a = [(s, rng.randint(1, 5)) for s in rng.sample("abcdefg", rng.randint(1, 5))]
b = [(s, rng.randint(1, 5)) for s in rng.sample("abcdefg", rng.randint(1, 5))]
l = rng.randint(1, 4)
c = Fraction(rng.randint(1, 8), rng.randint(1, 3))
for v in ("distinct", "entropy", "recursive"):
if block_is_diverse([x for _, x in a], l, v, c) and block_is_diverse([x for _, x in b], l, v, c):
assert block_is_diverse(merge(a, b), l, v, c)
def test_ruling_out_one_value() -> None:
rng = random.Random(5)
for _ in range(1000):
counts = random_counts(rng)
l = rng.randint(2, 5)
c = Fraction(rng.randint(1, 8), rng.randint(1, 3))
if block_is_diverse(counts, l, "recursive", c):
for j in range(len(counts)):
rest = counts[:j] + counts[j + 1 :]
assert block_is_diverse(rest, l - 1, "recursive", c)
if block_is_diverse(counts, l, "entropy"):
assert block_is_diverse(counts, l, "distinct")
References
- Ashwin Machanavajjhala, Johannes Gehrke, Daniel Kifer and Muthuramakrishnan Venkitasubramaniam, l-diversity: privacy beyond k-anonymity, 22nd International Conference on Data Engineering (ICDE 2006), page 24, the homogeneity and background knowledge attacks on k-anonymity, the l-diversity principle, and its entropy and recursive (c, l) instances
- Ashwin Machanavajjhala, Daniel Kifer, Johannes Gehrke and Muthuramakrishnan Venkitasubramaniam, l-diversity: privacy beyond k-anonymity, ACM Transactions on Knowledge Discovery from Data 1(1), article 3, 2007, the journal version, with Bayes-optimal privacy as the motivation, the monotonicity of the instances, and the use of l-diversity inside Incognito
- Ninghui Li, Tiancheng Li and Suresh Venkatasubramanian, t-closeness: privacy beyond k-anonymity and l-diversity, ICDE 2007, pages 106-115, Section 3 (the limits of l-diversity, with the skewness attack and the similarity attack, and Example 2 on a skewed test result)
- Benjamin C. M. Fung, Ke Wang, Rui Chen and Philip S. Yu, Privacy-preserving data publishing: a survey of recent developments, ACM Computing Surveys 42(4), article 14, 2010, the privacy models against attribute linkage, which compare l-diversity with its variants and with t-closeness
Where this leads
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H2 full proof |
|
| Lean | L0 no Lean part |
|
| Code (python) | I0 not checked yet |
|
| Visual | V0 none |
|
| Writing | W4 complete |
|
Tag HGM0PD Edit this page History Report an error Markdown