Definitioncore
t-closeness
Also called t-close table.
Requires the sensitive attribute distribution in each group to be within earth mover's distance t of its distribution in the whole table.
t-closeness is a condition on a released table that limits what the table tells about the sensitive value of one person beyond what it tells about the whole population. Li, Li and Venkatasubramanian (ICDE 2007) showed that l-diversity can fail in two ways: a block can be diverse but far more likely to hold a rare value than the table as a whole (the skewness attack), and a block can hold distinct values that all mean nearly the same thing (the similarity attack). t-closeness asks that, in each block, the distribution of the sensitive attribute be within distance of its distribution in the whole table. The distance is the earth mover's distance (also called the Wasserstein distance, a transportation cost), so values that are close in meaning count as close. The threshold is a policy choice. Like l-diversity, t-closeness is a property of one released table, not of the release mechanism.
Definition
A table has quasi-identifier attributes and a sensitive attribute with domain . A block (equivalence class) is a maximal set of rows with the same values on . Let be the distribution of in the whole table, and its distribution in a block .
Definition (Li, Li and Venkatasubramanian, Definition 2). A block has t-closeness if . A table has t-closeness if all its blocks have it. Here is the earth mover's distance for a ground distance between values with :
A flow moves mass from value of to value of , and is the least total work. With , (Fact 1 of the paper), so lies in .
The paper uses three ground distances, each with a closed formula (Section 5):
- Ordered distance, for a numeric or ordered attribute with : . Then, with ,
Each term is the mass that must cross the gap between and .
- Equal distance, for a categorical attribute with no structure: for . Then , which is the total variation distance.
- Hierarchical distance, for a categorical attribute with a generalization tree of height : the distance between two leaves is the height of their lowest common ancestor divided by . The paper gives a formula that sums the excess mass of each internal node.
Facts, each a theorem:
- Merging keeps t-closeness (Fact 2 of the paper). If and are the distributions of two blocks and , the distribution of the merged block is with , and . The reason: mixing the two best flows with weights and gives a flow for . So a coarser generalization of a t-close table is t-close, and the lattice searches that need monotonicity, such as Incognito, work.
- The extreme cases. forces every block to have the distribution , which is the same as publishing and no link between the quasi-identifiers and . allows every table. Putting all rows in one block always gives .
- t-closeness does not imply l-diversity. If one value has , a block made only of has equal distance to , so it is -close and holds one value. Such a block tells an attacker little beyond the population, because nearly everyone has ; whether that is acceptable is again a policy choice.
proofs/correctness.md proves the ordered formula and the merging fact, and that the test below is exact. The test computes the running sum of in one pass per block.
T-CLOSE(T, Q, S, t) ▷ S takes the ordered values v_1 < ... < v_m, with m ≥ 2- 1
W ← the distribution of S in the whole table T - 2
for each block E of T under Q do - 3
P ← the distribution of S in E - 4
run ← 0, d ← 0 - 5
for i ← 1 to m − 1 do - 6
run ← run + P[i] − W[i] - 7
d ← d + |run| - 8
if d / (m − 1) > t then return false - 9
return true
For rows and blocks, the test takes time in the word-RAM model with exact rational arithmetic, after the blocks are found by hashing. The code also has the equal distance, which replaces lines L4 to L8 by half the sum of . A general ground distance needs a transportation solver, for example the min-cost flow of the paper's Section 5.
Examples and non-examples
The table is Table 4 of Li, Li and Venkatasubramanian: nine synthetic records in three blocks, with Salary as the sensitive attribute. The salaries are , once each, so is uniform with and . The values below were computed exactly with fractions.
| block | salaries | ordered | equal |
|---|---|---|---|
| 3k, 4k, 5k | |||
| 6k, 8k, 11k | |||
| 7k, 9k, 10k |
- The similarity attack, measured. The table is distinct 3-diverse, but tells an attacker that its members have low salaries. The ordered distance sees this: is the farthest block from . The running sums for are , whose sum is , and . So the table is -close and not -close.
- The equal distance is blind here. Each block has three of the nine values with mass each, so the total variation to is for all three blocks. A distance that treats 3k and 11k as just two labels cannot tell from . The paper also notes that the Kullback-Leibler divergence has the same problem.
- The skewness attack, measured. A test result has 99 percent negative records. A block with 50 percent positive records has equal distance to , so it is not -close, although it is 2-diverse in all three senses of l-diversity.
- Merging. has distance at most by the merging fact. All three blocks together give the distribution and distance 0.
- Non-example: a numeric attribute with the equal distance. Using the equal distance for salaries makes t-closeness blind to the similarity attack, as the table shows. Use the ordered distance, or a hierarchy, when the values have meaning.
Related definitions
- l-diversity: the condition t-closeness refines. l-diversity asks for many values in a block; t-closeness asks for a block that looks like the table.
- k-anonymity: t-closeness is used together with it, because t-closeness alone does not bound the size of a block from below.
- Total variation distance: the earth mover's distance under the equal ground distance. The Kullback-Leibler divergence is another distance the paper considers and rejects, because it ignores the ground distance.
- Anatomy and m-invariance: other models for the sensitive values, for one release without generalization and for a sequence of releases.
- Differential privacy: a condition on the mechanism that holds against every background knowledge. t-closeness bounds only the gap between the block and the table, and it assumes that the attacker's knowledge about the population is the table distribution .
Proof: The ordered earth mover's distance has a closed form, merging keeps t-closeness, and the test is exactfull
Let and be distributions over the ordered values , with , and let the ground distance be . A flow is a matrix with row sums and column sums . Its work is , and is the least work. Write for , so .
Claim 1 (closed form). .
Proof of the lower bound. Fix a flow and a gap between and , . Since is the number of gaps with ,
The mass of on is , and the mass of there is . Subtract: . Both sums are non-negative, so their sum is at least . Hence the work of is at least .
Proof of the upper bound. Build a flow on the path that sends units across gap , to the right if and to the left if . At node the net amount sent away is , so node starts with and ends with . Split this flow on the path into paths from a node with to a node with , by the standard decomposition of a flow without cycles; on a path graph, a path from to crosses exactly gaps. The matrix of these paths, with the mass that stays at each node on the diagonal, is a flow whose work is . With the lower bound, this is the minimum.
Claim 2 (merging). For any ground distance, for .
Proof. Let and be optimal flows for and . The matrix is non-negative, has row sums and column sums . So it is a flow for the mixture, and its work is . The minimum is at most this.
If blocks and are merged, the distribution of the result is the mixture with . If both blocks are within of , so is the merged block. By induction, every table whose blocks are unions of the blocks of a t-close table is t-close.
Claim 3 (the test). T-CLOSE returns true if and only if every block has under the ordered distance.
Proof. Line L1 computes from all rows. The blocks of line L2 are the groups of rows with equal values on , and line L3 computes the distribution of a block. The loop of lines L5 to L7 keeps the invariant: before the iteration for , and . It holds at the start by line L4, and line L6 adds to make , then line L7 adds . After the loop, , so by Claim 1 line L8 compares with and returns false exactly for a block that is not within . If no block fails, line L9 returns true. With exact fractions, no rounding enters.
Cost. Lines L1 to L3 read each row a constant number of times with a hash table, and the loop costs per block, so the time is for rows and blocks.
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/t-closeness/. The authors are listed in prov/4AARD3.jsonl.
from collections import Counter
from collections.abc import Hashable, Sequence
from fractions import Fraction
Row = Sequence[Hashable]
def distribution(values: Sequence[Hashable], domain: Sequence[Hashable]) -> list[Fraction]:
"""Return the distribution of the values over the domain, as exact fractions.
Precondition: values is not empty and each value is in the domain. Cost: O(len(values) + len(domain)).
"""
count = Counter(values)
if set(count) - set(domain):
raise ValueError("a value is not in the domain")
return [Fraction(count[v], len(values)) for v in domain]
def emd_ordered(p: Sequence[Fraction], w: Sequence[Fraction]) -> Fraction:
"""Earth mover's distance under the ordered distance |i - j| / (m - 1).
Precondition: p and w are distributions over the same m ordered values. Cost: O(m).
"""
m = len(p)
if m < 2:
return Fraction(0)
run = Fraction(0) # @line L4
d = Fraction(0)
for i in range(m - 1): # @line L5
run += p[i] - w[i] # @line L6
d += abs(run) # @line L7
return d / (m - 1)
def emd_equal(p: Sequence[Fraction], w: Sequence[Fraction]) -> Fraction:
"""Earth mover's distance under the equal distance; this is the total variation distance. Cost: O(m)."""
return sum((abs(a - b) for a, b in zip(p, w)), Fraction(0)) / 2
def is_t_close(
table: Sequence[Row],
qi: Sequence[int],
s: int,
t: Fraction | int,
domain: Sequence[Hashable],
ground: str = "ordered",
) -> bool:
"""Return True if every block of the table is within distance t of the whole table.
domain lists the values of column s, in increasing order for the ordered
distance. ground is "ordered" or "equal". An empty table is t-close.
Cost: O(n + g m) for n rows, g blocks and m values, with exact fractions.
"""
if not table:
return True
dist = emd_ordered if ground == "ordered" else emd_equal
w = distribution([row[s] for row in table], domain) # @line L1
groups: dict[tuple[Hashable, ...], list[Hashable]] = {}
for row in table:
groups.setdefault(tuple(row[j] for j in qi), []).append(row[s])
for values in groups.values(): # @line L2
p = distribution(values, domain) # @line L3
if dist(p, w) > t: # @line L8
return False
return True # @line L9
test_t_closeness.py 164 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/t-closeness/. The authors are listed in prov/4AARD3.jsonl.
import itertools
import random
from fractions import Fraction
from t_closeness import distribution, emd_equal, emd_ordered, is_t_close
SALARIES = [3, 4, 5, 6, 7, 8, 9, 10, 11]
TABLE = [
("476**", "2*", 3),
("476**", "2*", 4),
("476**", "2*", 5),
("4790*", ">=40", 6),
("4790*", ">=40", 11),
("4790*", ">=40", 8),
("476**", "3*", 7),
("476**", "3*", 9),
("476**", "3*", 10),
]
def test_example() -> None:
w = distribution([r[2] for r in TABLE], SALARIES)
p1 = distribution([3, 4, 5], SALARIES)
p2 = distribution([6, 8, 11], SALARIES)
p3 = distribution([7, 9, 10], SALARIES)
assert emd_ordered(p1, w) == Fraction(3, 8)
assert emd_ordered(p2, w) == Fraction(1, 6)
assert emd_ordered(p3, w) == Fraction(17, 72)
for p in (p1, p2, p3):
assert emd_equal(p, w) == Fraction(2, 3)
assert is_t_close(TABLE, [0, 1], 2, Fraction(3, 8), SALARIES)
assert not is_t_close(TABLE, [0, 1], 2, Fraction(3, 10), SALARIES)
assert not is_t_close(TABLE, [0, 1], 2, Fraction(1, 2), SALARIES, "equal")
def test_edges() -> None:
assert is_t_close([], [0], 1, 0, ["a"])
one_block = [("x", v) for v in "abcab"]
assert is_t_close(one_block, [0], 1, 0, "abc")
assert emd_ordered([Fraction(1)], [Fraction(1)]) == 0
# all mass at the two ends: distance 1
assert emd_ordered([Fraction(1), 0, 0], [0, 0, Fraction(1)]) == 1
# skewness: 50 percent positive against 1 percent
p = [Fraction(1, 2), Fraction(1, 2)]
w = [Fraction(1, 100), Fraction(99, 100)]
assert emd_equal(p, w) == Fraction(49, 100)
def coupling(p: list[Fraction], w: list[Fraction]) -> Fraction:
"""Monotone transport on the line: move mass in sorted order from p to w."""
m = len(p)
i = j = 0
a, b = p[0], w[0]
cost = Fraction(0)
while True:
move = min(a, b)
cost += move * abs(i - j)
a -= move
b -= move
if a == 0:
i += 1
if i == m:
break
a = p[i]
if b == 0:
j += 1
if j == m:
break
b = w[j]
return cost / (m - 1)
def random_dist(rng: random.Random, m: int, denom: int) -> list[Fraction]:
cuts = sorted(rng.randint(0, denom) for _ in range(m - 1))
edges = [0, *cuts, denom]
return [Fraction(edges[i + 1] - edges[i], denom) for i in range(m)]
def test_ordered_against_coupling() -> None:
rng = random.Random(1)
for _ in range(800):
m = rng.randint(2, 7)
p = random_dist(rng, m, rng.randint(1, 12))
w = random_dist(rng, m, rng.randint(1, 12))
assert emd_ordered(p, w) == coupling(p, w)
def lp_brute(a: list[int], b: list[int], scale: int) -> Fraction:
"""Exact transport cost for m = 3 by trying every integer flow (optimal flows are integral)."""
best = None
for f11 in range(a[0] + 1):
for f12 in range(a[0] - f11 + 1):
f13 = a[0] - f11 - f12
for f21 in range(a[1] + 1):
for f22 in range(a[1] - f21 + 1):
f23 = a[1] - f21 - f22
f31, f32, f33 = b[0] - f11 - f21, b[1] - f12 - f22, b[2] - f13 - f23
if min(f31, f32, f33) < 0:
continue
cost = 2 * (f13 + f31) + f12 + f21 + f23 + f32
best = cost if best is None or cost < best else best
assert best is not None
return Fraction(best, 2 * scale)
def test_ordered_against_lp() -> None:
rng = random.Random(2)
for _ in range(120):
scale = rng.randint(1, 6)
p = random_dist(rng, 3, scale)
w = random_dist(rng, 3, scale)
a = [int(x * scale) for x in p]
b = [int(x * scale) for x in w]
assert emd_ordered(p, w) == lp_brute(a, b, scale)
def test_equal_against_subsets() -> None:
rng = random.Random(3)
for _ in range(600):
m = rng.randint(1, 6)
p = random_dist(rng, m, rng.randint(1, 10))
w = random_dist(rng, m, rng.randint(1, 10))
tv = max(
sum((p[i] - w[i] for i in sub), Fraction(0))
for r in range(m + 1)
for sub in itertools.combinations(range(m), r)
)
assert emd_equal(p, w) == tv
def test_table_against_oracle() -> None:
rng = random.Random(4)
domain = [0, 1, 2, 3, 4]
for _ in range(500):
n = rng.randint(1, 15)
table = [(rng.randint(0, 2), rng.choice(domain)) for _ in range(n)]
t = Fraction(rng.randint(0, 10), 10)
w = distribution([r[1] for r in table], domain)
want = all(
coupling(distribution([r[1] for r in table if r[0] == key], domain), w) <= t
for key in {r[0] for r in table}
)
assert is_t_close(table, [0], 1, t, domain) == want
def test_merge_is_convex() -> None:
rng = random.Random(5)
for _ in range(500):
m = rng.randint(2, 6)
w = random_dist(rng, m, 12)
c1 = [rng.randint(0, 3) for _ in range(m)]
c2 = [rng.randint(0, 3) for _ in range(m)]
if sum(c1) == 0 or sum(c2) == 0:
continue
p1 = [Fraction(x, sum(c1)) for x in c1]
p2 = [Fraction(x, sum(c2)) for x in c2]
lam = Fraction(sum(c1), sum(c1) + sum(c2))
p = [Fraction(x + y, sum(c1) + sum(c2)) for x, y in zip(c1, c2)]
for dist in (emd_ordered, emd_equal):
assert dist(p, w) <= lam * dist(p1, w) + (1 - lam) * dist(p2, w)
assert sum(p) == 1
References
- Ninghui Li, Tiancheng Li and Suresh Venkatasubramanian, t-closeness: privacy beyond k-anonymity and l-diversity, 23rd International Conference on Data Engineering (ICDE 2007), pages 106-115, Section 3 (the skewness and similarity attacks on l-diversity), Section 4 (Definition 2, the earth mover's distance, Facts 1 and 2, the generalization and subset properties) and Section 5 (the ordered, equal and hierarchical distances)
- Yossi Rubner, Carlo Tomasi and Leonidas J. Guibas, The earth mover's distance as a metric for image retrieval, International Journal of Computer Vision 40(2), 2000, pages 99-121, the earth mover's distance as a transportation problem between two distributions over a space with a ground distance
- Ashwin Machanavajjhala, Johannes Gehrke, Daniel Kifer and Muthuramakrishnan Venkitasubramaniam, l-diversity: privacy beyond k-anonymity, ICDE 2006, page 24, the l-diversity principle that t-closeness refines
- 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, including t-closeness and its criticism
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 4AARD3 Edit this page History Report an error Markdown