Skip to content

Privacy: how close do synthetic rows come?

Use unseen rows as a yardstick for how close synthetic rows come to real ones, catch exact copies, leave out identifiers, and state what a clean score doesn't show.

Applied ~14 min12 steps

What runs in CodeSplash

You will be able to

  • Explain why privacy is measured against rows the generator never saw
  • Read distance to closest record and exact-copy rates from the scorecard
  • Separate direct identifiers from quasi-identifiers before generating
  • State what these checks don't show

Read first

01

Synthetic isn't automatically private

Read

A generator learns from real people. If it learns too well, it gives them back.

So privacy is something you measure for each table. The scorecard asks a concrete question: are synthetic rows closer to the training rows than you'd expect by chance?

02

Copy the table

Predict

Predict first

A "generator" returns the training table unchanged. How does it score?

Fidelity perfect, privacy goneFidelity poor, privacy goneFidelity perfect, privacy fine
Reveal

Fidelity perfect, privacy gone. Every column, pair and row matches, and every row is someone's real record. That's why a perfect fidelity score should worry you, and why the two have to be read together.

03

The fidelity–privacy frontier

Explore

The first generator here copies each row and adds noise. Press copy the rows, then raise the noise slowly.

DExplorable · Synthetic data: fidelity against privacy

Can synthetic data look like the real thing without copying it?

A ratio near 1 means synthetic rows sit about as far from the training data as new real rows do. What will this generator's ratio be?

1.50
How sure?

How it's generated

0.10

Used by “Copy rows and add noise”.

300
  • Real (training)
  • Synthetic
as far as new real rows
  • Privacy ratio
  • Largest column KS × 4

Commit your prediction above to see the result

Make your prediction first; the figure appears when you commit.

Make your prediction to see the result.

Watch the privacy ratio: the typical distance from a synthetic row to its closest training row, divided by the same for held-out real rows. Around 1, synthetic rows sit about as far away as new real people would. Then try the check: a ratio of at least 1, with a column KS under 0.1.

04

Unseen rows are the yardstick

Read

How close is too close? Real data has near neighbors anyway, so raw distance can't say. The fair comparison is with real rows the generator never saw.

That's the distance to closest recordDistance to closest recordHow far a row sits from its nearest training row; synthetic rows should sit no closer than real rows the generator never saw.. For each row, the app finds its nearest training row by Gower distance, for synthetic rows and for holdout rows. Then:

share closer=#{synthetic rows closer than the 5th percentile of holdout distances}#{synthetic rows}\text{share closer} = \frac{\#\{\text{synthetic rows closer than the 5th percentile of holdout distances}\}}{\#\{\text{synthetic rows}\}}

If synthetic rows are no closer than unseen rows, this comes out near 5%.

05

Read the histogram

Explore

The scorecard draws both sets of distances.

Distance to closest recordAre synthetic rows suspiciously close to (copies of) real training rows?
View data
Gower distancepair
0.7175synthetic → train
0.2174synthetic → train
0.5495synthetic → train
0.8781synthetic → train
0.3242synthetic → train
0.8852synthetic → train
0.7705synthetic → train
0.2525synthetic → train
0.1997synthetic → train
0.4702synthetic → train
0.8462synthetic → train
1.032synthetic → train
0.2576synthetic → train
0.3339synthetic → train
1.013synthetic → train
0.5003synthetic → train
0.4487synthetic → train
0.5033synthetic → train
0.5950synthetic → train
0.7156synthetic → train
0.8409synthetic → train
0.4644synthetic → train
0.7235synthetic → train
0.3089synthetic → train
0.4263synthetic → train
0.3669synthetic → train
0.2826synthetic → train
0.4245synthetic → train
0.3592synthetic → train
0.3586synthetic → train
0.3112synthetic → train
0.2749synthetic → train
0.8823synthetic → train
0.5773synthetic → train
0.3404synthetic → train
0.4394synthetic → train
0.3355synthetic → train
0.2064synthetic → train
1.412synthetic → train
0.2338synthetic → train
0.1487synthetic → train
0.4480synthetic → train
0.4224synthetic → train
0.4819synthetic → train
0.4662synthetic → train
0.3114synthetic → train
0.6560synthetic → train
0.3462synthetic → train
0.4096synthetic → train
0.5465synthetic → train
0.9643synthetic → train
0.5391synthetic → train
0.1392synthetic → train
0.3875synthetic → train
0.7416synthetic → train
0.3740synthetic → train
0.4776synthetic → train
1.417synthetic → train
0.6222synthetic → train
0.7127synthetic → train

First 60 of 3000 rows.

Compare the two medians, the dashed lines, and then the left edges of the two distributions. The left edge is what matters: a pile of synthetic rows near zero with no matching pile of holdout rows suggests memorization.

Medians can look fine while a few rows sit almost on top of real people.

06

Exact copies

Read

Distance has a blunt companion: count the rows that match a training row in every column. Holdout rows set the baseline, since real data has duplicates anyway.

Worse still are copies of rows that are unique in the data, because those can only be one person.

The sequential trees in the last lesson gave 64 exact copies in 300 rows. Those runs set the holdout to zero, so the card couldn't compare them with anything, and it would say Privacy not measured.

07

How the light is set

Read
LightShare closerExact copies above holdoutCopies of unique rows
GoodAt most 8%At most 1%At most 0.5%
CautionAt most 15%At most 5%At most 2%
PoorAnything moreAnything moreAnything more

For Share safely, this light sets the verdict: Safe to share, Share inside the team only, or Do not share.

08

Leave identifiers out first

In the app

A generator can only leak what it was given. The Privacy scan in the studio's setup column flags columns before you generate:

  • Direct identifiersDirect identifierA column that points to a person on its own, such as a name, e-mail address, phone number or account number.: names, e-mails, phones, account numbers, and near-unique keys.
  • Quasi-identifiersQuasi-identifierA column that identifies people only in combination with others, such as age, postcode and occupation together.: age, postcode, occupation and the like, which identify people in combination.

Choose a dataset and read the flags. Use every safe column leaves out the direct identifiers. Keep quasi-identifiers only if the analysis needs them.

The scan reads names, patterns and uniqueness. No identifiers found means the scan found none, so read your columns too.

09

What a clean score doesn't show

Reflect

These checks look for one kind of leak: rows too close to real ones. They give no formal guarantee. In particular:

  • They don't test whether someone could work out that a given person was in the training data.
  • They don't test whether a rare combination gives away a sensitive value.
  • CodeSplash doesn't offer differential privacy, the approach that gives a mathematical guarantee.

Who would receive your table, and what else might they know about these people? If the honest answer worries you, get a privacy review before it goes out.

10

Check your understanding

Checkpoint

Check yourself

"Share closer" is 31%. What does that mean?

Show the answer

Answer: b

Unseen rows set the baseline of about 5%. At 31%, the generator is hugging its training data, so the card would rate privacy poor.

Check yourself

You set the holdout to 0 to train on every row. What happens to the privacy score?

Show the answer

Answer: a

Without unseen rows there's no yardstick. The card says so, and for sharing, it tells you to generate again with a holdout.

Check yourself

Which is a quasi-identifier rather than a direct identifier?

Show the answer

Answer: a

A postcode alone rarely points to one person, but combined with age and occupation it may. The others point to a person on their own.

Check yourself

The light is green and the verdict says Safe to share. Which statement is justified?

Show the answer

Answer: a

The card reports specific measurements, compatible with low risk of this one kind of leak. It's not a legal finding or a guarantee.

11

Compute it yourself

In the app

Here is the core of the check, for numeric columns only. Each distance is Gower's: differences scaled by the training range, then averaged over the columns.

Python
import numpy as np
import pandas as pd
from sklearn.neighbors import NearestNeighbors

cols = ["age", "visits", "tenure_months"]
train, holdout, synth = (pd.read_csv(f)[cols].dropna() for f in ["train.csv", "holdout.csv", "synthetic.csv"])
scale = train.max() - train.min()

nn = NearestNeighbors(n_neighbors=1, metric="manhattan").fit(train / scale)
d_syn = nn.kneighbors(synth / scale)[0].ravel() / len(cols)
d_hold = nn.kneighbors(holdout / scale)[0].ravel() / len(cols)

print("share closer:", round((d_syn < np.quantile(d_hold, 0.05)).mean(), 3))
R
cols <- c("age", "visits", "tenure_months")
read_cols <- function(f) as.matrix(na.omit(read.csv(f)[, cols]))
train <- read_cols("train.csv")
holdout <- read_cols("holdout.csv")
synth <- read_cols("synthetic.csv")
scale <- apply(train, 2, function(x) diff(range(x)))

nearest <- function(from, to) {
  to <- sweep(to, 2, scale, "/")
  apply(sweep(from, 2, scale, "/"), 1, function(r) min(rowMeans(abs(sweep(to, 2, r)))))
}

d_syn <- nearest(synth, train)
d_hold <- nearest(holdout, train)
cat("share closer:", round(mean(d_syn < quantile(d_hold, 0.05)), 3), "\n")
12

Recap

Recap
  • Measure privacy for each table, against real rows the generator never saw.
  • Look at the left edge of the distance histogram, and count exact copies and copies of unique rows.
  • Leave direct identifiers out, and keep quasi-identifiers only if you need them.
  • A clean score fits low risk of one kind of leak. It's not a guarantee.

Concepts in this lesson