Algorithm Search⌘K

Algorithmadvanced

Anatomy

Also called bucketization.

Releases quasi-identifiers and sensitive values in two tables linked only by group id, with l-diverse groups and no generalization.

Time
O(n log m) worst
Space
O(n) extra
Where
l = the diversity parameter; m = the number of distinct sensitive values; n = the number of tuples
Properties
deterministicnot randomized
Origin
Xiaokui Xiao, Yufei Tao, 2006

Anatomy publishes a table with a sensitive attribute as two tables. The quasi-identifier table (QIT) holds the exact quasi-identifier values of each tuple and a group id. The sensitive table (ST) holds, for each group, the sensitive values and their counts. Nothing joins a tuple to its own sensitive value except the group id, and each group holds at least ll tuples with distinct sensitive values. Xiao and Tao (VLDB 2006) proved that an attacker who knows a person's quasi-identifier values and knows that the person is in the table then guesses the person's sensitive value with probability at most 1/l1/l, the same as for an l-diverse generalized table. But no value is coarsened, so aggregate queries that mix quasi-identifiers and the sensitive attribute are much more accurate. Their algorithm Anatomize builds the groups in O(nlog⁡m)O(n \log m) time in the word-RAM model, and its reconstruction error is within a factor 1+1/n1 + 1/n of the lowest possible. The value of ll is a policy choice.

In plain words

A hospital wants to publish patient ages and ZIP codes, and also the diseases, without letting anyone link a patient to a disease. Generalization would blur the ages into ranges. Anatomy keeps them exact.

It puts the patients into small groups, and each group must have different diseases. It then publishes two lists. The first list gives each patient's age and ZIP code and the number of the group. The second list gives, for each group number, the diseases in the group, but not who has which one.

If a group has three patients with three different diseases, a neighbour who finds a patient in the first list only learns that the patient has one of three diseases. The groups are formed by taking one patient from each of the most common diseases at a time, so that no disease is left over at the end.

The problem

The input is a table TT of nn tuples with quasi-identifier attributes A1,…,AdA_1, \ldots, A_d and one categorical sensitive attribute AsA^s, and an integer l≥1l \ge 1. A partition of TT into groups G1,…,GgG_1, \ldots, G_g is l-diverse if in each group GjG_j, the most frequent sensitive value vv has cj(v)/∣Gj∣≤1/lc_j(v) / |G_j| \le 1/l (Definition 2 of the paper). The output is the QIT, with one row (t[A1],…,t[Ad],j)(t[A_1], \ldots, t[A_d], j) for each tuple tt in group jj, and the ST, with one row (j,v,cj(v))(j, v, c_j(v)) for each group jj and each value vv in it (Definition 3).

An l-diverse partition exists if and only if at most n/ln/l tuples share a sensitive value; the paper calls this the eligibility condition. It is necessary because each group can hold at most ∣Gj∣/l|G_j|/l tuples of a value, and these numbers add up to at most n/ln/l. The algorithm below shows that it is also sufficient.

The quality measure of the paper is the reconstruction error. For a tuple tt with value vhv_h in group GG, Errt=(1−c(vh)/∣G∣)2+∑h′≠hc(vh′)2/∣G∣2Err_t = (1 - c(v_h)/|G|)^2 + \sum_{h' \ne h} c(v_{h'})^2 / |G|^2, the squared distance between the true point of tt and the distribution that the release gives for it, and RCE=∑tErrtRCE = \sum_t Err_t. Theorem 2 of the paper proves RCE≥n(1−1/l)RCE \ge n (1 - 1/l) for every l-diverse partition.

The algorithm

  1. ANATOMIZE(T, l) ▷ at most n/l tuples of T share a sensitive value
  2. 1QIT ← ∅, ST ← ∅, g ← 0
  3. 2hash the tuples of T into buckets by their sensitive value
  4. 3while at least l buckets are non-empty do
  5. 4g ← g + 1, G_g ← ∅
  6. 5S ← the l buckets that hold the most tuples
  7. 6for each bucket B in S do
  8. 7remove one tuple from B and add it to G_g
  9. 8for each non-empty bucket B do ▷ B holds exactly one tuple
  10. 9t ← the tuple of B
  11. 10S' ← the groups that hold no tuple with the sensitive value of t
  12. 11add t to one group of S'
  13. 12for j ← 1 to g do
  14. 13for each tuple t of G_j, insert (t[A_1], ..., t[A_d], j) into QIT
  15. 14for each distinct value v in G_j, insert (j, v, c_j(v)) into ST
  16. 15return QIT, ST

Lines L3 to L7 are the group creation phase. Each iteration takes one tuple from each of the ll largest buckets, so each new group has exactly ll tuples with ll distinct values. The phase stops when fewer than ll buckets are non-empty. Lines L8 to L11 are the residue phase. The eligibility condition guarantees two facts that the paper proves as Properties 1 and 2: at the end of the first phase each non-empty bucket holds exactly one tuple, and each such residue tuple has a group without its value. Lines L12 to L14 write the two tables.

The paper removes "an arbitrary tuple" at line L7 and picks "a random group" at line L11; both choices are free. The code takes the oldest tuple of each bucket, breaks ties between buckets of the same size by the first appearance of their value, and puts a residue tuple into the first group of S′S'. Within each group, it writes the QIT rows and the ST rows in sorted order, so that the order of the rows says nothing about which tuple has which value (see Pitfalls).

Example

Take seven synthetic tuples with l=2l = 2. Age and ZIP code are the quasi-identifiers and the disease is sensitive. Flu occurs 3 times, and 3≤7/23 \le 7/2, so the table is eligible.

tuple Age ZIP disease
t1t_1 23 11001 flu
t2t_2 27 11002 gastritis
t3t_3 35 11005 flu
t4t_4 59 12001 dyspepsia
t5t_5 61 12003 flu
t6t_6 65 12004 gastritis
t7t_7 70 12007 pneumonia

Line L2 makes four buckets. Ties of size go to the value that appears first in the table.

step buckets before the step (flu, gastritis, dyspepsia, pneumonia) the ll largest new group
iteration 1 {t1,t3,t5}\{t_1, t_3, t_5\}, {t2,t6}\{t_2, t_6\}, {t4}\{t_4\}, {t7}\{t_7\} flu, gastritis G1={t1,t2}G_1 = \{t_1, t_2\}
iteration 2 {t3,t5}\{t_3, t_5\}, {t6}\{t_6\}, {t4}\{t_4\}, {t7}\{t_7\} flu, gastritis G2={t3,t6}G_2 = \{t_3, t_6\}
iteration 3 {t5}\{t_5\}, ∅\emptyset, {t4}\{t_4\}, {t7}\{t_7\} flu, dyspepsia G3={t5,t4}G_3 = \{t_5, t_4\}
residue ∅\emptyset, ∅\emptyset, ∅\emptyset, {t7}\{t_7\} one bucket left, 1<l1 < l t7t_7 goes to G1G_1, which has no pneumonia

The release is the two tables below.

Age ZIP group
23 11001 1
27 11002 1
70 12007 1
35 11005 2
65 12004 2
59 12001 3
61 12003 3
group disease count
1 flu 1
1 gastritis 1
1 pneumonia 1
2 flu 1
2 gastritis 1
3 dyspepsia 1
3 flu 1

An attacker who knows that a person aged 23 in ZIP 11001 is in the table finds group 1, and learns only that the person has flu, gastritis or pneumonia, each with probability 1/3≤1/21/3 \le 1/2. A researcher still sees the exact ages. Each tuple is alone with its value in its group, so Errt=1−1/∣G∣Err_t = 1 - 1/|G|, and RCE=3⋅23+4⋅12=4RCE = 3 \cdot \tfrac23 + 4 \cdot \tfrac12 = 4. The lower bound is 7(1−12)=3.57 (1 - \tfrac12) = 3.5, and 4/3.5=8/7=1+1/n4 / 3.5 = 8/7 = 1 + 1/n, the worst case of Theorem 4.

Correctness

Let RR be the number of tuples left in the buckets, c1c_1 the size of the largest bucket and jj the number of buckets of that size. The group creation phase keeps the invariant: either c1≤1c_1 \le 1, or R−j≥l(c1−1)R - j \ge l (c_1 - 1). At the start, it follows from the eligibility condition lc1≤nl c_1 \le n. When the phase stops, N<lN < l buckets are non-empty, and R≤jc1+(N−j)(c1−1)R \le j c_1 + (N - j)(c_1 - 1) gives R−j≤N(c1−1)R - j \le N (c_1 - 1). With c1≥2c_1 \ge 2 this is less than l(c1−1)l (c_1 - 1), against the invariant. So each bucket holds at most one tuple (Property 1). The first phase makes g=⌊n/l⌋g = \lfloor n/l \rfloor groups. A residue value vv occurs at most ⌊n/l⌋\lfloor n/l \rfloor times in TT, and one of these occurrences is the residue tuple, so at most g−1g - 1 groups hold vv and S′S' is not empty (Property 2). Each group then has at least ll tuples, all with distinct values (Property 3), so cj(v)/∣Gj∣≤1/∣Gj∣≤1/lc_j(v)/|G_j| \le 1/|G_j| \le 1/l. The full proof is in proofs/correctness.md.

The privacy claim is a theorem of the paper (Lemma 1, Corollary 1 and Theorem 1): if the attacker knows the quasi-identifier values of a person and that the person is in TT, and treats each assignment of the values of a group to its tuples as equally likely, then the probability that the person has value vv is cj(v)/∣Gj∣≤1/lc_j(v) / |G_j| \le 1/l.

Proof: Anatomize returns an l-diverse partition of every eligible tablefull

Let TT have n≥1n \ge 1 tuples and let l≥1l \ge 1. The table is eligible if at most n/ln/l tuples share a sensitive value. A partition is l-diverse if in each group GG every value vv has cG(v)≤∣G∣/lc_G(v) \le |G| / l. During the group creation phase (lines L3 to L7), let RR be the number of tuples left in the buckets, NN the number of non-empty buckets, c1c_1 the size of the largest bucket and jj the number of buckets of size c1c_1.

Claim 0 (eligibility is necessary). If an l-diverse partition exists, the table is eligible.

Proof. For a value vv, cT(v)=∑GcG(v)≤∑G∣G∣/l=n/lc_T(v) = \sum_G c_G(v) \le \sum_G |G| / l = n / l. □\square

Claim 1 (invariant). Before each test of line L3, either c1≤1c_1 \le 1 or R−j≥l(c1−1)R - j \ge l (c_1 - 1).

Proof. At the start, R=nR = n and lc1≤nl c_1 \le n. If j≤lj \le l, then R−j≥lc1−lR - j \ge l c_1 - l. If j>lj > l, then R≥jc1R \ge j c_1, so R−j≥j(c1−1)≥l(c1−1)R - j \ge j (c_1 - 1) \ge l (c_1 - 1).

Now take an iteration with c1≥2c_1 \ge 2 (if c1≤1c_1 \le 1, every bucket still has at most one tuple afterwards, so the claim holds). Line L5 takes the ll largest buckets and line L7 removes one tuple from each, so R′=R−lR' = R - l.

  • If j>lj > l, all ll chosen buckets have size c1c_1. Then c1′=c1c_1' = c_1 and j′=j−lj' = j - l, and R′−j′=R−j≥l(c1′−1)R' - j' = R - j \ge l (c_1' - 1).
  • If j≤lj \le l, all jj buckets of size c1c_1 are chosen and drop to c1−1c_1 - 1, so c1′=c1−1c_1' = c_1 - 1. Let uu be the number of buckets of size c1−1c_1 - 1 that are not chosen; then j′=j+uj' = j + u. If u=0u = 0, R′−j′=R−l−j≥l(c1−1)−l=l(c1′−1)R' - j' = R - l - j \ge l (c_1 - 1) - l = l (c_1' - 1). If u>0u > 0, a bucket of size c1−1c_1 - 1 was passed over, so all ll chosen buckets have size at least c1−1c_1 - 1, and R≥jc1+(l−j)(c1−1)+u(c1−1)=l(c1−1)+j+u(c1−1)R \ge j c_1 + (l - j)(c_1 - 1) + u (c_1 - 1) = l (c_1 - 1) + j + u (c_1 - 1). Hence R′−j′=R−l−j−u≥l(c1−2)+u(c1−2)≥l(c1′−1)R' - j' = R - l - j - u \ge l (c_1 - 2) + u (c_1 - 2) \ge l (c_1' - 1). □\square

Claim 2 (Property 1). When line L3 stops the loop, each bucket holds at most one tuple, and g=⌊n/l⌋g = \lfloor n / l \rfloor groups exist.

Proof. The loop stops with N<lN < l. Suppose c1≥2c_1 \ge 2. The jj largest buckets hold jc1j c_1 tuples and the other N−jN - j hold at most c1−1c_1 - 1 each, so R−j≤N(c1−1)<l(c1−1)R - j \le N (c_1 - 1) < l (c_1 - 1), which contradicts Claim 1. So c1≤1c_1 \le 1 and R=N<lR = N < l. Each iteration removed ll tuples, so gl=n−Rg l = n - R with 0≤R<l0 \le R < l, which gives g=⌊n/l⌋g = \lfloor n/l \rfloor. □\square

Claim 3 (Property 2). For each residue tuple tt, the set S′S' of line L10 is not empty.

Proof. Let vv be the value of tt. Since cT(v)≤n/lc_T(v) \le n / l and cT(v)c_T(v) is an integer, cT(v)≤gc_T(v) \le g. Each group made by lines L4 to L7 takes at most one tuple from each bucket, so it holds vv at most once, and tt itself is in no group yet. The residue tuples have distinct values by Claim 2, so no earlier residue tuple added vv. Hence at most cT(v)−1≤g−1c_T(v) - 1 \le g - 1 groups hold vv. Since n≥ln \ge l for an eligible non-empty table (otherwise cT(v)≥1>n/lc_T(v) \ge 1 > n / l), g≥1g \ge 1 and at least one group lacks vv. □\square

Claim 4 (Property 3 and correctness). If the table is eligible, the groups form a partition of TT into groups of at least ll tuples with pairwise distinct values, so the partition is l-diverse.

Proof. Every tuple leaves its bucket exactly once, at line L7 or at line L9, and joins exactly one group, so the groups partition TT. Each group starts with ll tuples from ll different buckets, so with ll distinct values, and grows only at line L11, by a tuple whose value it does not hold (Claim 3). So in each group every value occurs once, and cG(v)=1≤∣G∣/lc_G(v) = 1 \le |G| / l. Lines L12 to L14 write exactly the rows of Definition 3 of Xiao and Tao for this partition. With Claim 0, an l-diverse partition exists if and only if the table is eligible, and the code raises an error exactly when it is not. ■\blacksquare

The error. In a group of size GG where all counts are 1, each tuple has error (1−1/G)2+(G−1)/G2=(G−1)/G(1 - 1/G)^2 + (G - 1)/G^2 = (G - 1)/G, so the group contributes G−1G - 1 and the release has RCE=n−g=n−⌊n/l⌋RCE = n - g = n - \lfloor n/l \rfloor. Since ⌊n/l⌋≥(n−l+1)/l\lfloor n / l \rfloor \ge (n - l + 1)/l, this is at most n(1−1/l)+(l−1)/ln (1 - 1/l) + (l - 1)/l, which is 1+1/n1 + 1/n times the lower bound n(1−1/l)n (1 - 1/l) of Theorem 2 of the paper.

Cost. Line L2 is O(n)O(n) with hashing. The heap holds at most mm entries, and each of the gg iterations does ll pops and at most ll pushes, so the first phase is O(nlog⁡m)O(n \log m). The residue phase handles fewer than ll tuples and scans at most gg groups for each, with a set of values per group, which is O(n)O(n). Lines L12 to L14 are O(nlog⁡n)O(n \log n) with the sorting inside groups. The extra space is O(n)O(n).

Complexity

The model is the word-RAM, with the sensitive values hashed in O(1)O(1) expected time.

  • Line L2 takes O(n)O(n) expected time.
  • The code keeps the non-empty buckets in a binary heap keyed by size. Each of the ⌊n/l⌋\lfloor n/l \rfloor iterations pops ll buckets and pushes back at most ll, at O(log⁡m)O(\log m) each, so the first phase takes O(nlog⁡m)O(n \log m) time.
  • At most l−1l - 1 residue tuples exist, and each scans at most g=⌊n/l⌋g = \lfloor n/l \rfloor groups with a set of values per group, so the residue phase takes O(n)O(n) time.
  • Lines L12 to L14 take O(n)O(n) time, or O(nlog⁡n)O(n \log n) with the sorting of rows inside each group.
  • The extra space is O(n)O(n) for the groups.

In external memory, Theorem 3 of the paper shows O(n/b)O(n/b) I/Os and O(m)O(m) memory for a page size bb: the bucket sizes fit in memory, and the groups are written out in the order they are made. Theorem 4 bounds the quality: if ll divides nn, then RCE=n(1−1/l)RCE = n(1 - 1/l), the lower bound; otherwise RCERCE is at most 1+1/n1 + 1/n times the lower bound. With all counts 1, RCE=∑j(∣Gj∣−1)=n−⌊n/l⌋RCE = \sum_j (|G_j| - 1) = n - \lfloor n/l \rfloor, whatever group each residue tuple joins.

Properties

  • No generalization. The quasi-identifier values are published exactly. The privacy comes from cutting the link between the QIT row and the ST row.
  • Groups of size ll or slightly more. Each group has ll tuples, and at most l−1l - 1 groups receive one or more residue tuples.
  • Deterministic as coded. The paper allows any choice at lines L7 and L11; the guarantee holds for every choice.
  • Membership is not hidden. The QIT shows the exact quasi-identifier values of everyone in the table, so an attacker learns who is in it. A generalized table can hide this when the attacker does not know who is in the table, which the paper discusses as its assumption A2.

Variants and improvements

  • Other diversity rules per group. Lines L3 to L7 make each group distinct ll-diverse with one tuple per value, which also meets the entropy and recursive forms of l-diversity. A group rule based on t-closeness needs a different grouping step.
  • Republication. When the table changes and is released again, each release can be l-diverse and the sequence can still leak. m-invariance is the fix of Xiao and Tao for that case; it uses the same group and signature idea.
  • More than two tables. Later work cuts the attributes into more than two column groups, each released with a group id, so that correlated quasi-identifiers stay together.
  • The same format with a generalized QIT. Some systems combine a coarse generalization of the quasi-identifiers with a separate sensitive table, to hide membership as well.

When to use it

Use anatomy when analysts need the exact quasi-identifier values, for example to run range queries or to fit a model on age and ZIP code together with the sensitive attribute, and when publishing who is in the table is acceptable. If membership itself is sensitive, use a generalization such as Mondrian, or a differentially private release. If the attacker may learn the correlation between quasi-identifiers and the sensitive value from the release itself, the 1/l1/l bound does not hold, as the next section explains.

Pitfalls

  • Taking any ll buckets at line L5. If line L5 takes ll non-empty buckets that are not the largest, values can be stranded. With the values a,a,b,ca, a, b, c and l=2l = 2 (eligible, since 2≤4/22 \le 4/2), the group {b,c}\{b, c\} leaves the bucket {a,a}\{a, a\}, and the second aa has no group without aa. Line L5 must take the ll largest buckets.
  • Placing a residue tuple anywhere at line L11. A residue tuple put into a group that already has its value makes that value occur twice in a group of l+1l + 1 tuples, and 2/(l+1)>1/l2/(l + 1) > 1/l for l>1l > 1. Line L10 must restrict the choice to groups without the value.
  • Leaking through row order at lines L13 and L14. If the QIT rows of a group are written in the same order as the ST rows, for example both in the order in which line L7 picked the tuples, then the ii-th row of one table matches the ii-th row of the other, and the release links every person to a value. Write each group's rows in an order that does not depend on the link, such as sorted by value.
  • Skipping the eligibility test. If more than n/ln/l tuples share a value, no l-diverse partition exists. Without the test, the loop of line L3 ends with a bucket of two or more tuples, and line L11 fails. Test first, and suppress tuples of the frequent value or lower ll.
  • Trusting 1/l1/l against a learning attacker. The bound assumes that each assignment inside a group is equally likely. Kifer (SIGMOD 2009) showed that an attacker can learn from the whole anatomized release how quasi-identifiers and sensitive values correlate, and then guess single values with probability well above 1/l1/l. Anatomy limits what one row reveals, not what the release reveals about the population.

Implementations

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.

anatomy_bucketization.pyI0RawGitHub
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/anatomy-bucketization/. The authors are listed in prov/Y4WDK2.jsonl.

import heapq
from collections import Counter, deque
from collections.abc import Hashable, Sequence
from fractions import Fraction

Row = Sequence[Hashable]


def anatomize_groups(values: Sequence[Hashable], l: int) -> list[list[int]]:
    """Partition the positions 0..n-1 into l-diverse groups by their sensitive values.

    Precondition: l >= 1 and at most n / l positions share a value (the
    eligibility condition); otherwise ValueError is raised. Each group gets at
    least l positions, all with distinct values. Ties between buckets of the
    same size go to the value that appears first, and each bucket gives up its
    oldest position first. Cost: O(n log m) time and O(n) extra space for n
    values with m distinct ones.
    """
    if l < 1:
        raise ValueError("l must be at least 1")
    n = len(values)
    groups: list[list[int]] = []  # @line L1
    index: dict[Hashable, int] = {}
    queues: list[deque[int]] = []
    for i, v in enumerate(values):  # @line L2
        if v not in index:
            index[v] = len(queues)
            queues.append(deque())
        queues[index[v]].append(i)
    if queues and l * max(len(q) for q in queues) > n:
        raise ValueError("not eligible: more than n / l tuples share a sensitive value")
    heap = [(-len(q), k) for k, q in enumerate(queues)]
    heapq.heapify(heap)
    while len(heap) >= l:  # @line L3
        group: list[int] = []  # @line L4
        chosen = [heapq.heappop(heap) for _ in range(l)]  # @line L5
        for neg_size, k in chosen:  # @line L6
            group.append(queues[k].popleft())  # @line L7
            if neg_size + 1 < 0:
                heapq.heappush(heap, (neg_size + 1, k))
        groups.append(group)
    seen = [{values[i] for i in g} for g in groups]
    for _, k in sorted(heap, key=lambda e: e[1]):  # @line L8
        i = queues[k].popleft()  # @line L9
        v = values[i]
        target = next(j for j, vs in enumerate(seen) if v not in vs)  # @line L10
        groups[target].append(i)  # @line L11
        seen[target].add(v)
    return groups


def anatomize(
    table: Sequence[Row], qi: Sequence[int], s: int, l: int
) -> tuple[list[tuple[Hashable, ...]], list[tuple[int, Hashable, int]]]:
    """Return the quasi-identifier table and the sensitive table of an anatomized release.

    A QIT row is the quasi-identifier values of a tuple followed by its group
    id (from 1). An ST row is (group id, value, count). Inside each group the
    rows are sorted, so the row order does not link a QIT row to an ST row.
    Precondition: as for anatomize_groups, and the values of each column are
    mutually comparable. Cost: O(n log n) time, O(n) extra space.
    """
    groups = anatomize_groups([row[s] for row in table], l)
    qit: list[tuple[Hashable, ...]] = []
    st: list[tuple[int, Hashable, int]] = []
    for j, g in enumerate(groups, start=1):  # @line L12
        qit.extend((*key, j) for key in sorted(tuple(table[i][a] for a in qi) for i in g))  # @line L13
        counts = Counter(table[i][s] for i in g)
        st.extend((j, v, c) for v, c in sorted(counts.items()))  # @line L14
    return qit, st  # @line L15


def reconstruction_error(groups: Sequence[Sequence[Hashable]]) -> Fraction:
    """Return the RCE of Xiao and Tao for groups given as lists of sensitive values.

    For a tuple with value v in group G, the error is (1 - c(v)/|G|)^2 plus the
    sum of c(u)^2 / |G|^2 over the other values u of G. Cost: O(n) for n tuples
    when each group has O(1) distinct values, O(sum of |G| m_G) in general.
    """
    total = Fraction(0)
    for g in groups:
        size = len(g)
        counts = Counter(g)
        for v in g:
            err = (1 - Fraction(counts[v], size)) ** 2
            err += sum((Fraction(c * c, size * size) for u, c in counts.items() if u != v), Fraction(0))
            total += err
    return total
test_anatomy_bucketization.py 149 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/anatomy-bucketization/. The authors are listed in prov/Y4WDK2.jsonl.

import random
from collections import Counter
from fractions import Fraction

import pytest

from anatomy_bucketization import anatomize, anatomize_groups, reconstruction_error

TABLE = [
    (23, 11001, "flu"),
    (27, 11002, "gastritis"),
    (35, 11005, "flu"),
    (59, 12001, "dyspepsia"),
    (61, 12003, "flu"),
    (65, 12004, "gastritis"),
    (70, 12007, "pneumonia"),
]


def test_example() -> None:
    values = [r[2] for r in TABLE]
    groups = anatomize_groups(values, 2)
    assert groups == [[0, 1, 6], [2, 5], [4, 3]]
    qit, st = anatomize(TABLE, [0, 1], 2, 2)
    assert qit == [
        (23, 11001, 1),
        (27, 11002, 1),
        (70, 12007, 1),
        (35, 11005, 2),
        (65, 12004, 2),
        (59, 12001, 3),
        (61, 12003, 3),
    ]
    assert st == [
        (1, "flu", 1),
        (1, "gastritis", 1),
        (1, "pneumonia", 1),
        (2, "flu", 1),
        (2, "gastritis", 1),
        (3, "dyspepsia", 1),
        (3, "flu", 1),
    ]
    rce = reconstruction_error([[values[i] for i in g] for g in groups])
    assert rce == 4
    assert rce / (7 * (1 - Fraction(1, 2))) == Fraction(8, 7)


def test_edges() -> None:
    assert anatomize([], [0], 1, 3) == ([], [])
    assert anatomize_groups(["a", "b", "a"], 1) == [[0], [2], [1]]
    with pytest.raises(ValueError):
        anatomize_groups(["a", "a", "b"], 2)
    with pytest.raises(ValueError):
        anatomize_groups(["a"], 0)
    with pytest.raises(ValueError):
        anatomize_groups(["a", "b"], 3)
    # the largest buckets must be taken first, or the second "a" is stranded
    groups = anatomize_groups(["a", "a", "b", "c"], 2)
    assert sorted(sorted(g) for g in groups) == [[0, 2], [1, 3]]


def set_partitions(n: int) -> list[list[int]]:
    """Every set partition of range(n) as a restricted growth string."""
    out: list[list[int]] = [[]]
    for _ in range(n):
        out = [s + [b] for s in out for b in range((max(s) + 2) if s else 1)]
    return out


PARTITIONS = {n: set_partitions(n) for n in range(1, 7)}


def blocks_of(labels: list[int], values: list[str]) -> list[list[str]]:
    out: dict[int, list[str]] = {}
    for b, v in zip(labels, values):
        out.setdefault(b, []).append(v)
    return list(out.values())


def diverse(block: list[str], l: int) -> bool:
    return l * max(Counter(block).values()) <= len(block)


def rce_closed_form(blocks: list[list[str]]) -> Fraction:
    # summing the per-tuple error over a block of size g gives g - (sum of c^2) / g
    return sum(
        (Fraction(len(b) ** 2 - sum(c * c for c in Counter(b).values()), len(b)) for b in blocks),
        Fraction(0),
    )


def check_groups(groups: list[list[int]], values: list[str], l: int) -> None:
    n = len(values)
    assert sorted(i for g in groups for i in g) == list(range(n))
    assert len(groups) == n // l
    for g in groups:
        assert len(g) >= l
        assert len({values[i] for i in g}) == len(g)


def test_against_brute_force() -> None:
    rng = random.Random(7)
    for _ in range(500):
        n = rng.randint(1, 6)
        l = rng.randint(1, 3)
        values = [rng.choice("abcd"[: rng.randint(1, 4)]) for _ in range(n)]
        best = None
        for labels in PARTITIONS[n]:
            blocks = blocks_of(labels, values)
            if all(diverse(b, l) for b in blocks):
                rce = rce_closed_form(blocks)
                best = rce if best is None or rce < best else best
        eligible = l * max(Counter(values).values()) <= n
        assert eligible == (best is not None)
        if not eligible:
            with pytest.raises(ValueError):
                anatomize_groups(values, l)
            continue
        groups = anatomize_groups(values, l)
        check_groups(groups, values, l)
        got = reconstruction_error([[values[i] for i in g] for g in groups])
        assert got == rce_closed_form([[values[i] for i in g] for g in groups])
        assert best >= n * (1 - Fraction(1, l))
        assert best <= got <= (1 + Fraction(1, n)) * best


def test_large_random() -> None:
    rng = random.Random(8)
    for _ in range(500):
        n = rng.randint(0, 30)
        l = rng.randint(1, 5)
        m = rng.randint(1, 8)
        values = [rng.choice("abcdefgh"[:m]) for _ in range(n)]
        if n and l * max(Counter(values).values()) > n:
            with pytest.raises(ValueError):
                anatomize_groups(values, l)
            continue
        groups = anatomize_groups(values, l)
        check_groups(groups, values, l)
        got = reconstruction_error([[values[i] for i in g] for g in groups])
        assert got == n - n // l
        table = [(rng.randint(0, 9), v) for v in values]
        qit, st = anatomize(table, [0], 1, l)
        assert len(qit) == n
        assert sum(c for _, _, c in st) == n
        assert Counter(v for _, v, _ in st) == Counter(v for g in groups for v in {values[i] for i in g})

History

Xiao and Tao presented anatomy at VLDB 2006, with the l-diverse partition, the two-table release, the lower bound on the reconstruction error and the linear I/O algorithm. Their running example compared anatomy with a generalized table of the same data and showed much smaller errors on aggregate queries. The same release format, a table of group ids next to a table of sensitive values, is often called bucketization in later work. Kifer's 2009 deFinetti attack showed the limits of the 1/l1/l bound when the attacker learns from the release, and the same authors' m-invariance of 2007 extended the group and signature idea to tables that change over time.

References

  1. Xiaokui Xiao and Yufei Tao, Anatomy: simple and effective privacy preservation, 32nd International Conference on Very Large Data Bases (VLDB 2006), pages 139-150, Section 3 (Definitions 2 and 3, Lemma 1, Corollary 1 and Theorem 1 on the breach probability 1/l, and the assumptions A1 and A2), Section 4 (the reconstruction error), Section 5.1 (Theorem 2), Section 5.2 (Figure 3 and Properties 1 to 3) and Section 5.3 (Theorems 3 and 4)
  2. Daniel Kifer, Attacks on privacy and deFinetti's theorem, ACM SIGMOD 2009, pages 127-138, the attack that learns the correlation between quasi-identifiers and sensitive values from an anatomized release and uses it against single records
  3. Ashwin Machanavajjhala, Johannes Gehrke, Daniel Kifer and Muthuramakrishnan Venkitasubramaniam, l-diversity: privacy beyond k-anonymity, ICDE 2006, page 24, the l-diversity principle that anatomy enforces on each group
  4. 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 anonymization operations, where anatomy is compared with generalization, suppression and perturbation

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.

LadderLevelWhat the next level needs
Human proofH1 sketch
  • a full proof of time.worst
  • a full proof of space.extra
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
Code (python)I0 not checked yet
  • the tests did not run in this build
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW2 basic
  • a scene preset for the example