Using a spreadsheet I calculated the cost of a unique encoding and the numbers suggest that random data can be theoretically compressed. I say theoretically because reality doesn’t always do things by the numbers. Reading data using an arithmetic encoder with probabilities other than a power two usually reads slightly less bits on average than the calculated number of bits and you can’t actually read or write part of a bit. The probability of compressing random data is extremely low. Only a little less than 3E-13 percent of the data sets would save anything.

While the encoding is not practical because of this, it is interesting to me firstly because it suggests it is possible to compress random data. It also suggests that when compressing an endless data stream it will only reduce it in size and never enlarge it.. Like any other compression algorithm it would enlarge some files of a given length.

The algorithm is based on variable length data sets of one bit symbols. A data set consists of symbols until a 1 is encountered then ends when a 0 is encountered. There can be any number of 0 symbols before the first 1 symbol and any number of 1 symbols before the final 0. The smallest data set is simply a 1 followed by a 0.

These sets can also be encoded by encoding the count of extra symbols in the form of a binary selection of whether there are more extra symbols or the data set is ending. An index into the extra symbol count is needed to determine how many are 0 symbols and how may are 1 symbols.

This is the encoding which is read then written as 0 symbols until a 1 symbol is encountered then 1 symbols until a 0 symbol ends the dataset. The probability of the symbol to end the data set is calculated to break even until the index into the extra characters becomes large enough to allow the end of set symbol to match the size of the output symbol and start saving bits. A limit needs to be placed on the length of a data set because theoretically reading a set could go on forever with nothing ever being output.

To illustrate how data is read, the first symbol read has a probability of 25 percent to end the data set for 2 bits or 75 percent for at least one extra symbol costing 0.415037 bits. If the data set ends there are no extra symbols so no index is needed and a 1 and 0 are written for 2 bits breaking even. If the end of data set symbol is not read, additional symbols are read until an end of data set symbol is encountered

The second symbol read has a probability of one third to end the data set for 1.584963 bits or two thirds for another extra symbol at 0.584963 bits. If the data set ends an index into the extra symbol count of one so far is read. There is always extra symbol count plus one possibilities for the index which is two in this case for 1 bit. That makes a total of 3 bits read for the output of 010 or 110 or 1 bit for the first two extra symbols.

It takes quite some time before the end of data set can be read at 1 bit and start showing a profit. It isn’t until the data set reaches 55 symbols that it starts showing a profit. That’s like flipping a coin and getting heads 53 times in a row. It’s possible but probably not in my lifetime. It’s not quite that bad because the coin is weighted more for heads at the start but still.

While I think the algorithm could compress some data, it just isn’t practical. It is nice to think that something otherwise thought impossible could be done.

Spreadsheet Data

Set Size Extra Symbol Cost Extra Symbol Index Cost Total Extra Symbol Cost End Data Set Cost Another Symbol Cost Bits Read - Bits Written
2 0 0 0 2 0.415037 0
3 0.415037 1 1.415037 1.584963 0.584963 0
4 1 1.584963 2.584963 1.415037 0.678072 0
5 1.678072 2 3.678072 1.321928 0.736966 0
6 2.415037 2.321928 4.736966 1.263034 0.777608 0
7 3.192645 2.584963 5.777608 1.222392 0.807355 0
8 4 2.807355 6.807355 1.192645 0.830075 0
9 4.830075 3 7.830075 1.169925 0.847997 0
10 5.678072 3.169925 8.847997 1.152003 0.862496 0
11 6.540568 3.321928 9.862496 1.137504 0.874469 0
12 7.415037 3.459432 10.87447 1.125531 0.884523 0
13 8.29956 3.584963 11.88452 1.115477 0.893085 0
14 9.192645 3.70044 12.89308 1.106915 0.900464 0
15 10.09311 3.807355 13.90046 1.099536 0.906891 0
16 11 3.906891 14.90689 1.093109 0.912537 0
17 11.91254 4 15.91254 1.087463 0.917538 0
18 12.83007 4.087463 16.91754 1.082462 0.921997 0
19 13.75207 4.169925 17.922 1.078003 0.925999 0
20 14.67807 4.247928 18.926 1.074001 0.929611 0
21 15.60768 4.321928 19.92961 1.070389 0.932886 0
22 16.54057 4.392317 20.93289 1.067114 0.93587 0
23 17.47644 4.459432 21.93587 1.06413 0.938599 0
24 18.41504 4.523562 22.9386 1.061401 0.941106 0
25 19.35614 4.584963 23.94111 1.058894 0.943416 0
26 20.29956 4.643856 24.94342 1.056584 0.945552 0
27 21.24511 4.70044 25.94555 1.054448 0.947533 0
28 22.19265 4.754888 26.94753 1.052467 0.949374 0
29 23.14202 4.807355 27.94937 1.050626 0.95109 0
30 24.09311 4.857981 28.95109 1.04891 0.952694 0
31 25.0458 4.906891 29.95269 1.047306 0.954196 0
32 26 4.954196 30.9542 1.045804 0.955606 0
33 26.95561 5 31.95561 1.044394 0.956931 0
34 27.91254 5.044394 32.95693 1.043069 0.95818 0
35 28.87072 5.087463 33.95818 1.04182 0.959358 0
36 29.83008 5.129283 34.95936 1.040642 0.960472 0
37 30.79055 5.169925 35.96047 1.039528 0.961526 0
38 31.75207 5.209453 36.96153 1.038474 0.962526 0
39 32.7146 5.247928 37.96253 1.037474 0.963475 0
40 33.67807 5.285402 38.96348 1.036525 0.964377 0
41 34.64245 5.321928 39.96438 1.035622 0.965237 0
42 35.60769 5.357552 40.96524 1.034761 0.966057 0
43 36.57374 5.392317 41.96606 1.033938 0.966842 0
44 37.54059 5.426265 42.96685 1.033149 0.967595 0
45 38.50818 5.459432 43.96761 1.032387 0.968324 0
46 39.47651 5.491853 44.96836 1.031641 0.969038 0
47 40.44554 5.523562 45.96911 1.030894 0.969754 0
48 41.4153 5.554589 46.96989 1.030113 0.970502 0
49 42.3858 5.584963 47.97076 1.029237 0.971344 0
50 43.35714 5.61471 48.97185 1.028146 0.972392 0
51 44.32954 5.643856 49.97339 1.026608 0.973874 0
52 45.30341 5.672425 50.97584 1.024165 0.976233 0
53 46.27964 5.70044 51.98008 1.019917 0.980354 0
54 47.26 5.72792 52.98792 1.012081 0.988019 0
55 48.24802 5.754888 54.0029 1 1 0.002904
56 49.24802 5.78136 55.02938 1 1 0.029377
57 50.24802 5.807355 56.05537 1 1 0.055372
58 51.24802 5.83289 57.08091 1 1 0.080907
59 52.24802 5.857981 58.106 1 1 0.105998
60 53.24802 5.882643 59.13066 1 1 0.13066
61 54.24802 5.906891 60.15491 1 1 0.154908
62 55.24802 5.930737 61.17875 1 1 0.178754
63 56.24802 5.954196 62.20221 1 1 0.202213
64 57.24802 5.97728 63.2253 1 1 0.225297
65 58.24802 6 64.24802 1 1 0.248017
66 59.24802 6.022368 65.27038 1 1 0.270385