Skip to content
Français

Privacy and matching

  • Lesson 29
  • 40 min
  • Module 8: Run matching in production

Where we are

Our matcher reads names, emails and dates of birth. These are personal data. In this lesson, we use fewer fields, try matching on hashed and encoded values, see their weaknesses, and handle a deletion request on a merged record.

What you will be able to do

See how much personal data the matcher reads, and what each field is worth. Match on hashed values and on Bloom filter encodings, and measure what each one keeps and loses. Then see why encoding is not the same as privacy, and handle a deletion request for one record of a merged customer.

NotebookHash, encode and attack in a notebookColab · Kaggle · GitHub

The notebook hashes the emails and runs the email rule on the hashes. It builds Bloom filters for names and compares them with the Dice coefficient. Then you run a frequency attack on the encoded surnames, without the key. Its setup cells download the data, so it needs no local setup.

Colab and Kaggle open your own copy. They need a free Google or Kaggle account. Stuck? See the solution notebook.

Before you startWhat to know and to have readyAbout 40 minutes

What you need to know

What you need on your computer

  • The project after the week of Human review, with its virtual environment active. Or use the notebook only.

This lesson is about methods and their measured limits. It is not legal advice. Privacy law differs between countries, and a real project needs its legal and privacy team.

Two clinics in one town want to know which patients they share. Then they can stop double appointments. But neither clinic may send its patient list to the other.

So they agree on a trick. Each clinic writes each patient’s name in a secret code that both clinics know. They compare only the codes. When two codes are equal, the patient is in both lists, and nobody saw a name.

It sounds safe. But one clinic notices that one code appears 40 times in the other list. In this town, the most common surname is Tremblay. So that code is probably “Tremblay”. The secret code hid the name. It did not hide how often the name occurs.

TermLarkfieldA made-up Canadian online shop for home and garden products. It is not a real company. All Nextia courses use its help desk and data as the example.Learn more Meet Larkfield has the same choice. Its matcher reads names, emails, phones, addresses and dates of birth. A partner, or a second team, may want to match without seeing them.

What the matcher reads, and what each field is worth

Section titled “What the matcher reads, and what each field is worth”

Every field in the comparison vector is personal data: information about a person who can be identified. The first rule of privacy is to use fewer fields. We call it data minimisation. But each field also helps the matcher. How much?

We trained LightGBM again with one group of fields left out at a time. The labels, the candidate pairs and the threshold method stayed the same:

LightGBM on the 3,000 labelled pairs with one group of fields left out, measured on the test split of the synthetic records. The axis starts at 0.7.
Text description of the chart

A column chart of F1, on an axis from 0.7 to 0.92. All fields: 0.902. Without date of birth: 0.864. Without phone: 0.87. Without email: 0.818. Without name: 0.74. Without address: 0.752.

Every group helps, and some help far more than others:

  • The name and the address carry most of the matching. Without them, F1 falls to 0.74 and 0.752.
  • The date of birth costs 0.038 of F1. It is one of the most sensitive fields. Is that gain worth it? That is a business and privacy decision, and now it has a number.

Use only the fields that change the result enough, and write down why you keep each one. The AI security course shows how to decide what to keep and for how long, in Minimize and retain deliberately.

A hash turns a value into a short code. The same value always gives the same code. A different value, even with one letter changed, gives a completely different code. You can’t turn the code back into the value.

Plain hashes have one weakness: anyone can hash a list of common names and compare. So the project uses a keyed hash (HMAC): a hash with a secret key mixed in. Without the key, nobody can make the same codes. The key in the course project is a practice key, written in privacy.py. A real key is a secret that only the data owners share.

The project’s run hashed-rules runs the four rules of Module 2 on hashed values only. The result is exactly the same as on raw values: precision 0.723, recall 0.87, F1 0.79. Equal values give equal hashes, and the rules only ask “equal or not”.

But the classifier needs more than “equal”. It needs “how similar”. And a hash can’t say that.

The surnames theriault and theriaul three ways. Raw values: one letter differs, and Jaro-Winkler sees almost the same name; fuzzy matching yes, private no. Hashed with a key: d722583656fc88c4 and 90f9e003d393865f, completely different codes, only equal or not equal; fuzzy matching no, exact matching yes. Bloom filter of 1000 bits: bits from the letter pairs; the Dice of the two filters is 0.844; fuzzy matching yes, and the raw value is not shared.
The same two surnames as raw values, as keyed hashes and as Bloom filters. The codes and the Dice value come from the project's practice key.
Text description of the diagram

Three columns. The first, raw values: theriault and theriaul. One letter differs, and Jaro-Winkler sees almost the same name. Fuzzy matching works, but nothing is private. The second, hashed with a key: d722583656fc88c4 and 90f9e003d393865f. The codes are completely different, and they only say “equal” or “not equal”. Exact matching works; fuzzy matching does not. The third, a Bloom filter of 1000 bits: bits set from the letter pairs _t, th, he, er and so on. The Dice coefficient of the two filters is 0.844. Fuzzy matching works, and the raw value is not shared.

Bloom filters: bits for each pair of letters

Section titled “Bloom filters: bits for each pair of letters”

A Bloom filter is a row of bits, all 0 at the start. To encode a value, we cut it into pairs of letters, called bigrams. “ana” becomes _a, an, na and a_, with _ marking the start and the end. Each bigram is hashed with the key a few times. Each hash names one position in the row, and that bit becomes 1.

Similar values share most of their bigrams, so they set most of the same bits. To compare two filters, we use the Dice coefficient: 2 × the shared bits ÷ (the bits of the first + the bits of the second). Equal filters give 1, and filters with nothing in common give 0.

The project uses 1,000 bits and 20 hashes per bigram. To see every bit, here is a made-up size: 24 bits and 2 hashes. The bit positions are real: they come from the project’s encoding with its practice key.

A tiny Bloom filter with 24 bits and 2 hashes per letter pair. anna has the bigrams _a, a_, an, na, nn. Its bits are 0, 5, 13, 14, 15, 16, 17, 18, 21. ana has the bigrams _a, a_, an, na, and the bits 0, 5, 15, 16, 17, 18, 21. They share 7 bits. Dice = 2 × 7 ÷ (9 + 7) = 0.875.
Two names in a tiny Bloom filter of 24 bits. Shared bits are green.
Text description of the diagram

Two rows of 24 boxes, numbered from 0, one row for “anna” and one for “ana”. For “anna”, the bigrams and their bits: _a sets 0 and 17; a_ sets 0 and 21; an sets 15 and 18; na sets 5 and 16; nn sets 13 and 14. So the bits 0, 5, 13, 14, 15, 16, 17, 18, 21 are 1. For “ana”: _a sets 0 and 17; a_ sets 0 and 21; an sets 15 and 18; na sets 5 and 16. So the bits 0, 5, 15, 16, 17, 18, 21 are 1. Bits 1 in both rows are green: 7 of them. The box below: Dice = 2 × 7 ÷ (9 + 7) = 0.875.

“anna” has one bigram more, nn, so it has two more bits. Everything else is shared, and Dice is 0.875. Two bigrams can also hit the same bit, as _a and a_ do at bit 0. With more bits, that happens less often.

With the project’s real size, the Dice coefficient behaves like a string similarity:

Two valuesHashes equal?Dice of the Bloom filters
theriault / theriaulno0.844
theriault / terriaultno0.825
theriault / smithno0.279
marie claude / marieno0.652

The project’s run bloom-lgbm encodes every record. The first name, the surname and the street become Bloom filters. The email, phone, postcode and date of birth become keyed hashes. It keeps no raw value. Then LightGBM learns from the Dice values and the hash equalities of the 3,000 labelled pairs.

Terminal window
match privacy
'theriault' / 'theriault': hashes d722583656fc88c4 / d722583656fc88c4 (equal)
Bloom filters: 180 and 180 bits set of 1000; Dice 1.000
rules precision 0.723, recall 0.870, F1 0.790
hashed-rules precision 0.723, recall 0.870, F1 0.790
lgbm precision 0.918, recall 0.886, F1 0.902
bloom-lgbm precision 0.913, recall 0.812, F1 0.859
Frequency attack on the encoded surnames of the shop and garden-centre records:
1. 1665 records -> guess 'smith' (right)
2. 1429 records -> guess 'tremblay' (right)
3. 1122 records -> guess 'brown' (right)
4. 998 records -> guess 'gagnon' (right)
5. 899 records -> guess 'taylor' (wrong)
6. 877 records -> guess 'wilson' (wrong)
7. 855 records -> guess 'johnson' (right)
8. 817 records -> guess 'martin' (wrong)
9. 785 records -> guess 'roy' (wrong)
10. 751 records -> guess 'williams' (wrong)
5 of 10 guessed right, without the key

The first line uses the command’s default example, Thériault and Theriault. Cleaning makes them the same value, so their codes are equal. Now try two spellings that differ by one letter. The command prints the same table again after these lines:

Terminal window
match privacy --example Thériault Theriaul
'theriault' / 'theriaul': hashes d722583656fc88c4 / 90f9e003d393865f (different)
Bloom filters: 180 and 159 bits set of 1000; Dice 0.844
Matching on raw, hashed and encoded values, measured on the test split of the synthetic records. The axis starts at 0.7.
Text description of the chart

A column chart of F1, on an axis from 0.7 to 0.92. rules (raw values): 0.79. hashed-rules: 0.79, the same. lgbm (raw values): 0.902. bloom-lgbm (encoded values only): 0.859.

On encoded values only, LightGBM reaches F1 0.859, against 0.902 on raw values. Precision stays almost the same (0.913 against 0.918). Recall falls from 0.886 to 0.812. The encoded vector has 7 fields instead of 16: no sound codes, no town, no swapped dates. It still beats the rules (0.79) and Splink (0.797) on raw values.

The weakness: equal values give equal rows

Section titled “The weakness: equal values give equal rows”

Look again at the end of the output. The attacker has the encoded surnames of the shop and garden-centre records, and no key. He counts the rows. The most frequent row appears 1,665 times. He guesses that it is the most common surname in a public list. Here, the course uses the loyalty members’ surnames as that list.

This is a frequency attack. It needs nothing but counting. On Larkfield’s synthetic records, it guessed 4 of the 5 most common surnames right, and 5 of the top 10. The guesses that fail are names of almost the same frequency, such as Taylor and Wilson. With a better public list, the attacker would do better.

Known ways to make the attack harder exist. We name them here; we did not measure them:

  • Put several fields into one filter per record, so that a row is not one surname.
  • Add a random value per pair of data owners, and change the key often.
  • Let a third party, who sees neither raw list, do the comparison.

None of them makes encoded records anonymous. Treat Bloom filters as a way to share less, not as a way to make the data non-personal.

Common mistake

"Hashing makes it private"

Symptom: TermTomásLarkfield’s AI engineer. He often suggests a quick shortcut, and the lesson shows why it fails. sends a partner the hashed emails and Bloom-encoded names of all Larkfield customers. “They can’t read anything,” he says. The partner counts the encoded surnames and guesses 4 of the 5 most common ones without any key. With a guessed surname, the partner can test other guesses: first names that are common with it, towns, years of birth.

Cause: hashing and Bloom encoding keep equal values equal. That is why matching still works. It is also why frequencies, and any value that is rare or easy to guess, leak through.

Fix: share as few fields as possible. Keep the key away from the party that compares. Prefer one filter per record over one filter per field. Write a privacy review that names the attack and the remaining risk. Treat the encoded data as personal data.

Consequence in AI work: the same mistake appears with embeddings and with “anonymised” logs. A vector or a code is not anonymous when equal inputs give equal outputs, or when similar inputs stay close.

Valérie Tremblay asks Larkfield to delete her shop account, S-009232. Her loyalty and garden-centre records stay. But S-009232 is part of the entity LK-G-000001, and its golden record takes the name from it.

The outputs below come from a run without the practice undo of Undo a merge. If you did that undo, S-009232 is already alone in LK-S-009232. Then match forget says “Removed S-009232 from LK-S-009232; 0 record(s) left in it”. The golden record of LK-G-000001 is the same as below.

Terminal window
match forget S-009232
Removed S-009232 from LK-G-000001; 2 record(s) left in it; its golden record now uses only them.
Kept: the record ID in outputs/forgotten.csv and the event in the history, with no personal data.
Not done here: deleting the record from the course data files and from backups (a real system must).
Terminal window
match golden --show LK-G-000001
LK-G-000001: 2 records from garden,loyalty
name valerie tremblay from L-010072
email tremblay.valerie88@example.net from L-010072
phone 5145558296 from L-010072
address 6760 rue des herons, st eloi du lac QC from G-000001
postcode H6P3H7 from G-000001
dob 1975-01-31 from L-010072
G-000001 TREMBLAY, VAL | tremblay.valerie88@example.net | 5145558296 | 6760, r. des Hérons, Saint-Éloi-du-Lac QC H6P3H7
L-010072 Valérie Tremblay | tremblay.valerie88@example.net | 5145558296 | H6P 3H7

Before the request, the golden record took its name from S-009232. Now it takes it from L-010072. The entity keeps its ID, so other systems still find her. The history keeps only the record ID and the event.

The next nightly run must not bring the record back. match golden reads outputs/forgotten.csv and leaves those records out:

Terminal window
match golden
38,908 entities from 60,107 records (16,513 with more than one record); 0 with two dates of birth
Left out 1 record(s) of outputs/forgotten.csv
Saved outputs/entities.csv and outputs/golden.csv

60,107 records: the course records without S-009232. match golden --show LK-G-000001 still shows two records. Like every run of match golden, it builds the entities from the model’s links on the course records only. So the week’s daily records are not in it, as New records every day says.

A deletion on a merged entity has more steps than one row:

  • Every copy. The source record, the prepared records, the candidate pairs, the review labels, the model’s training data, the backups. In this project, match forget changes the entities and keeps the ID in outputs/forgotten.csv, so later runs leave the record out. The course data files still have S-009232. A real system deletes the source too, and checks that later runs do not restore it.
  • The golden record. It must be built again from the records that remain, as here.
  • The other people. If a deleted record was the only link between two others, their entity may need a split.

The AI security course works through finding and removing personal data in a real system in Find and remove personal data before it reaches a model. The RAG course handles deletions in a search index in Updates and deletions.

When to use encoded matching, and what it costs

Section titled “When to use encoded matching, and what it costs”
  • Use hashed values when two parties only need exact matches on fields that are hard to guess, such as an email. On Larkfield’s rules, nothing was lost.
  • Use Bloom filters when you need fuzzy matching without sharing raw values. Here, the price was 0.043 of F1, mostly recall.
  • Never call it anonymous. A frequency attack guessed 4 of the 5 most common surnames by counting.
  • Use fewer fields first. It is the cheapest protection. Measure what each field adds, as above.
  • Plan deletions before the first merge. Keep the history with IDs only, and know every place a record goes.

Check your understanding

The hashed rules reach exactly the same F1 as the raw rules (0.790). Why does hashing not help LightGBM the same way?

Show the answer

The rules only ask whether two values are equal, and equal values give equal hashes. LightGBM uses similarities: Jaro-Winkler of the names, sound codes, token overlap. A hash destroys similarity: “theriault” and “theriaul” get completely different codes. So a model on hashes only sees “equal” or “not”. Bloom filters keep a similarity (Dice), and LightGBM on them reached F1 0.859.

Check your understanding

Two Bloom filters have 18 and 20 bits set, and 16 bits in common. What is their Dice coefficient?

Show the answer

Dice = 2 × 16 ÷ (18 + 20) = 32 ÷ 38 ≈ 0.842. That is close to the Dice of “theriault” and “theriaul” with the project’s filters (0.844): very similar values.

Check your understanding

A customer asks Larkfield to delete one of her three records. Name three places, other than the entity list, where that record must also go away.

Show the answer

For example: the source file it came from (here, the course data, so that match golden does not bring it back). The prepared records and saved candidate pairs in outputs/. The review labels, and any training set that contains it. The backups. Also check the golden record: it must be built again from the other records. If the record was the only link between two others, the entity may need a split.

Recap

  • Every field the matcher reads is personal data. Use fewer fields: without the date of birth, F1 fell only from 0.902 to 0.864.
  • Keyed hashes keep exact matching: the hashed rules equal the raw rules (F1 0.79). They destroy similarity.
  • Bloom filters keep a fuzzy similarity (Dice). LightGBM on encoded values only reached F1 0.859, against 0.902 on raw values.
  • Equal values give equal codes. A frequency attack guessed 4 of the 5 most common surnames without the key. Encoded data is still personal data.
  • A deletion request on a merged entity reaches every copy of the record, rebuilds the golden record, and keeps only IDs in the history.

Module 8 is done, and with it the teaching modules. The case studies of Module 9 apply the whole course to real open data. Pick one: at least one is needed for the certificate.

Next: Match products between two shops