Mental Models204 cards · 12 fields

Mental Models/Computing & Information

Compression

数据压缩

If you can say something shorter without losing anything, you've found its pattern.

In a Chinese work group chat, once a task has been handed out, everyone replies with just "1" to mean "got it." A sentence you say every day gets squeezed down to a single character; a sentence you only say now and then, nobody bothers to shorten.

In 1951 David Huffman, a graduate student at MIT, ran into the arithmetic version of this. He was taking Robert Fano's course on information theory, and Fano gave the students a choice: sit the final exam, or solve a problem. The problem went like this. A computer records each letter as a string of 0s and 1s, giving common letters short strings and rare letters long ones. How should the strings be assigned so that a piece of text uses up the fewest 0s and 1s?

Huffman chose the problem. For months he got nowhere, and he was about to give up and go back to studying for the exam. Then it occurred to him that he could start from the rarest letters. First join the two rarest letters into a group and treat it as one letter; then join that with whatever is now rarest, and keep joining, layer by layer, up to the most common letter. Each join adds one digit to the string of every letter in the group, so the rarest letters end up with the longest strings and the most common letter with the shortest. He published the method in 1952, and formats like ZIP and JPEG later came to use it. How much it saves depends entirely on how uneven the letters' frequencies are: the more lopsided, the more it saves.

When you tell your roommate "the usual place," you mean the bubble tea shop by the complex's east gate, which saves six words each time. Say it twenty times a month and you save 120 words. You can only save like this because the two of you go to that one shop over and over. If you went to a different shop every time, no place would be worth a code name. The cost is that both of you have to remember where "the usual place" is, and that agreement also counts toward the total length.

That is what data compression is: find what keeps coming up, give it a short name, write that down together with the agreement, and you can still get back the original word for word. The shorter you can make it, the better you have worked out the pattern in the data.

The commoner A is, the more short codes save
Average per letter1.75 bits
Saved per 100 letters25 bits
01240%50%60%70%80%90%A's share of all lettersAverage bits per letterTwo bits eachCoded by frequency
01240%60%80%A's share of all lettersAverage bits per letterTwo bits eachCoded by frequency

Drag to change A's share. B takes half of what's left, C and D split the rest. The codes stay 0, 10, 110, 111; see how far they pull away from two bits each.

Short codes only pay off when frequencies are uneven

If four letters A, B, C and D each make up a quarter of the text, writing every letter as two binary digits is already as short as it gets, and it won't compress. Now say A makes up half, B a quarter, and C and D an eighth each. Code A as 0, B as 10, C as 110 and D as 111. Multiply each letter's number of digits by how often it appears and add them up, and you get the average code length:

Half the letters take just 1 digit, and only a quarter take 3, so each letter averages 1.75 digits. A hundred letters take 25 digits fewer than writing two digits each. Write the common ones short and the rare ones long, and the total length comes down.

This code can be built with Huffman's method. First join C and D, the rarest, into a group. That group makes up a quarter, so join it with B, which is also a quarter, and finally join that with A. Every time two groups are joined, one more 0 or 1 is needed to tell them apart, so every letter in the group gets one more digit in its code. C and D, joined first, are the longest; A, joined last, has just one digit. The extra digits all land on the rarest letters.

Count the rule and the exceptions together

A thousand characters are all A, except the 500th, which is B. Writing "A repeated a thousand times, with the 500th changed to B" takes only a dozen or so words. If three hundred Bs are scattered through the thousand characters, you have to list all three hundred positions one by one. That list of exceptions is longer than the original, so the rule was a waste of space. To judge whether you've found a pattern, check whether the rule plus the exceptions is shorter than just copying out the original.

Being able to compress shows you understand

Copying down the sunrise time for every day of a year takes 365 lines. If you understand how the Earth turns, a few numbers let you work out any day. Both records are equally complete. The difference is that the short one isn't copied, it's worked out: it can also work out any day next year, while the copied table can't work out a single line. Being able to compress shows you've found the rule that produces the data, and that rule can also produce data you haven't seen yet.

Data with lots of repetition, a lopsided spread, or values that depend on what came before compresses best. A record of coin tosses won't compress, because there was never a pattern in it. Nor can a lossless method make every input shorter. There are so many possible strings of a thousand digits that there aren't enough shorter strings to go around, so two inputs must end up as the same short string, and when you decompress you can't tell them apart. Lossy compression means deciding first what to throw away: delete a rare fault from an incident report, and the report gets shorter, but whoever investigates loses the clue.

Explain
Compressing a ZIP file a second time barely shrinks it, because the first pass already used up the repetition there was to use.
Design
When designing a daily stock report, write only the rows that differ from yesterday instead of sending the whole table again; on days with few changes, the report is short.
Communicate
When reporting thirty pages of meeting notes to your boss, start with the two rules that ran through the whole meeting, then report separately the three things that didn't fit those rules.
  1. In these records, what keeps coming up, or can be worked out from what came before?
  2. Adding up the rule, when it applies and all the exceptions, is that shorter than writing each item out?
  3. Would the details I'm deleting change the decision I have to make next?

Technology

Not one pixel lost

In 1996 the World Wide Web Consortium published the first recommended specification for the PNG image format. It rewrites each pixel as its difference from the neighboring pixel, then compresses the data that repeats. In a picture with large areas of solid color, most of the differences are zero, so that's where it saves the most space. When the file is opened, the decoder runs the steps backward, and every pixel comes back exactly as it was.

Investing

Fewer surprises in the earnings report

You're reading the financial reports of a coffee shop brand. You first estimate next quarter from past sales at its established stores, and list new stores ramping up and store closures separately. When the new quarter's books arrive, most of the sales can be explained this way, with only a small amount left over. That means you've grasped how the company works. If every store needs a reason of its own, it's time to rewrite the explanation.

Personal life

Turn what you forget into rules

You travel a lot for work. Every time, you make a new packing list, and every time you forget the same few things. So you merge all your old lists into one standing list, plus two extra lines: "Overnight: add toiletry bag" and "Rain: add umbrella." From then on, each trip you only note the unusual items. What's common is written once, the conditions are spelled out, and the exceptions are kept on their own.

A warehouse's goods fall into four classes. Their shares of shipments are: class A 60%, class B 20%, classes C and D 10% each. The warehouse manager wants to give each class a binary label, and no label may be the start of another label, so the scanner can read them one after another without a break.

Which assignment uses the fewest digits in total for the labels on a thousand items?

The same compression program handles two sets of data, each ten thousand lines long. One is the hourly count of people entering a subway station, and it shrinks a lot. The other is a sequence of numbers from a random number generator, and it barely changes.

What is the most direct explanation for why the second set hardly compresses?

A food factory's monthly quality report runs to twenty pages. The manager wants to squeeze it onto one page with only the month's average defect rate. Every year there are one or two batches of raw material that go wrong, and afterward the factory has to trace which batches and which production line were involved.

How should the page be changed so it is short and still lets them trace the problem?