Algorithms · Counting & data

Differential privacy

How Apple learns your favourite emoji without knowing it’s yours. Every phone fibs a little on purpose. Across millions of phones, the fibs cancel out and the truth appears.

The problem

Apple wants the emoji keyboard to suggest the right emoji at the right moment. To do that, it needs to know which emojis people actually use. Apple's Differential Privacy Team has described using the same approach to find popular health data types and resource-heavy websites in Safari.

Your emoji history is personal, though. Apple's goal, described in the December 2017 paper "Learning with Privacy at Scale", is to learn what's popular across everyone without learning what you in particular use.

The obvious way

The obvious plan is for every phone to upload its real answer, and for Apple to promise to keep the database safe. It works perfectly for counting. But the truth now sits on a server, where a breach, a leak, a subpoena or a curious employee could expose it. Everyone has to trust Apple and everything that ever touches that database, forever.

The clever idea

Imagine a survey that asks "do you do something embarrassing?" Before answering, you secretly flip a coin. Heads, you tell the truth. Tails, you flip again and say yes or no at random. Nobody can tell whether your "yes" is real or a coin. But the surveyor knows the coin rules, so across thousands of people they can subtract the coin's effect and recover the true share.

Apple calls its version local differential privacy: each device adds carefully calibrated noise before sending, so Apple doesn't have to trust itself with anyone's true data. Apple's real algorithms, such as the Private Count Mean Sketch, are more sophisticated. The code below uses the simplest version of the idea.

How the code does it

Everything private happens in device_report. Half the time the phone tells the truth, and otherwise it answers with a fresh coin flip. The server only ever sees what this function returns. Then estimate does the reverse: because yes answers are half truth plus a quarter pure luck, doubling the yes-share and subtracting 0.5 recovers the real share.

The chart from this line should show the raw noisy share sitting far from the truth, while the estimate lands close to it. The table at these lines puts both approaches side by side: exact but exposed, versus slightly off but deniable. The loop shows the error tending to shrink as the crowd grows, though a single run with one seed can wobble. Finally, the last lines check a single "yes": a non-fan says yes 1 time in 4, so a report is never proof.

Trade-offs

The price is accuracy: the answer is an estimate, and you need a big crowd for the noise to wash out. Rare things are hard to spot, and small groups give shaky results. Don't use this when you need exact numbers about individuals, like billing, medical records or anything where one person's true value matters. It's built for questions about what's popular across many people.

Where else you'll see it

Beyond emoji, Apple's described uses include popular health data types and resource-heavy websites in Safari. The same pattern of noise on the device, with patterns recovered in aggregate, appears anywhere a company wants crowd-level insight without holding raw personal data.

One phone reports "yes, I use 😂 most". What can Apple safely conclude about that person?

  • They definitely use 😂 the most
  • Nothing certain: non-fans say yes 1 time in 4
  • They definitely don't use 😂
Show the answer

Nothing certain: non-fans say yes 1 time in 4. A non-fan reports yes if the first flip is tails and the second says yes, which is 1 in 4. So a single yes is deniable, and only the crowd's average is meaningful.

Change it and see
  • Change 0.30 to 0.60
  • Change random.seed(7) to another number
  • Make the truth probability 0.9 in device_report
  • Add 1000000 to the crowd sizes

The code

Python, with what each line means beside it. It compares the clever way with collecting everyone’s real answer and promising to keep it safe.

import randomBring in the random tools
random.seed(7)Fix the dice so results repeat
def device_report(truly_uses):Each phone decides what to report
if random.random() < 0.5:Flip a coin: heads
return truly_usesHeads: tell the truth
return random.random() < 0.5Tails: answer yes or no randomly
def estimate(reports):Apple turns reports into an estimate
yes_share = sum(reports) / len(reports)Count the share of yes answers
return 2 * yes_share - 0.5Undo the coin's effect with math
def simulate(n):Build a pretend crowd of n users
truth = [random.random() < 0.30 for _ in range(n)]Each truly likes 😂 with 30% chance
return truth, [device_report(t) for t in truth]Every phone sends its noisy report
truth, noisy = simulate(10000)Simulate 10,000 users
real = sum(truth) / 10000Compute the real 😂 share
show([real, sum(noisy) / 10000, estimate(noisy)], "true | raw noisy | estimated")Chart truth, raw noise, and estimate
show([Compare the two approaches side by side
{"way": "obvious", "server learns": "user 0 uses 😂: " + str(truth[0]), "off by": 0},Obvious way: server sees the true answer
{"way": "clever", "server learns": "report: " + str(noisy[0]) + " (maybe noise)", "off by": round(abs(estimate(noisy) - real), 3)},Clever way: server sees only a noisy report
], "obvious vs clever")Close the table
errors = [] Start a list of errors
for n in [100, 1000, 10000, 100000]:Try four crowd sizes
t, r = simulate(n)Simulate a crowd of that size
errors.append(round(abs(estimate(r) - sum(t) / n), 4))Record how far the estimate is off
show(errors, "error at 100, 1k, 10k, 100k users")Chart how the error changes
fans_who_said_yes = sum(1 for t, r in zip(truth, noisy) if t and r)Find fans whose noisy report said yes
print("Share of 'yes' reports from real fans:", round(fans_who_said_yes / sum(noisy), 2))Print how many yes reports are real
print("A non-fan still says yes 1 time in 4, so one report proves nothing.")Print why one report proves nothing

Read more

Run it. Then change it.

The code runs right in the page and draws as it goes. Free, nothing to install.

More on counting & data