Package: bitinformation
Bit-Information-Content ToolThe method calculates how much information content each bit in a number has.
In essence, it is a statistical analysis of bit sequences.
For example, according to this approach, random sequences of binary values and or a sequences of ones or zeros contain no information.
Once a sequence has a structure, the information content is non-zero.[0101010101010101] # low information content
[1111111111111111] # zero information content
[0000000000000000] # zero information content
[0000111100001111] # high information contentAlgorithmThe following example explains the algorithm step by step without using formulas, when possible.In the first step, assume there is a sequenceSof 4-bit numbers.
The sequenceSis split into two arraysAandB.Ais created by removing the last element fromSand B, by removing the first element.
The example below uses Python notatation to illustrate that.S = [0, 1, 2, 3, 4, 5, 6, 7]

A = S[:-1] = [0, 1, 2, 3, 4, 5, 6]
B = S[1: ] = [1, 2, 3, 4, 5, 6, 7]The next step is presented as a spreadsheet.
In our example we work with 4-bit numbers, so we can identify each bit with the indexi = [0, 1, 2, 3].
For illustration, we exapand our table withiandA, andiandB.A'andB'are the binary representations of the columnsAandB, respectively.
The columnsA'[i]andB'[i]are the bits at the positioni.iABA'=bin(A)B'=bin(B)A'[i]B'[i]seq = A'[idx]B'[idx]001000000010101012000100101010023001000110101034001101001010045010001010101056010101101010067011001110101101000000010000112000100100101123001000111111134001101001010145010001010000156010101100101167011001111111201000000010000212000100100000223001000110000234001101000101245010001011111256010101101111267011001111111301000000010000312000100100000323001000110000334001101000000345010001010000356010101100000367011001110000The next stpe is groupping the table by(i, seq)columns and count the occurences.pis the probability with wich a sequence at bit positionioccurs.iseqcountp = count/700000.00000140.57101030.42901100.00010020.28610120.28611010.14311120.28620030.42920110.14321000.00021130.42930071.00030100.00031000.00031100.000In the last step we compute the mutual information.
To do that we take the columnsiandpfrom the table and reshape them so that we have the probabilities for each sequence, i.e.,p00,p01,p10,p11, in separate columns.
This allows us to continue our example as a spreadsheet.Formula below computes mutual information.
It says how much information a bit contains.M' = p00 * log(p00 / (p00 + p01) / (p00 + p10)) +
     p01 * log(p01 / (p00 + p01) / (p01 + p11)) +
     p10 * log(p10 / (p10 + p11) / (p00 + p10)) +
     p11 * log(p11 / (p10 + p11) / (p01 + p11))

M = M' / log(2)ip00p01p10p11M00.0000.5710.4290.0000.69910.2860.2860.1430.2862.06120.4290.1430.0000.4290.23531.0000.0000.0000.0000.000Installpython3 -m pip install bitinformationUsageCompute bit informationimport numpy as np
import bitinformation.bitinformation as bit
data = np.random.rand(10000)  
bi = bit.BitInformation()
bi.bitinformation(data)Compare dataimport numpy as np
import bitinformation.bitinformation as bit
data1 = np.random.rand(10000)  
data2 = np.random.rand(10000)  
res = bit.compare_data(data1, data2)Compare GRIB filesimport numpy as np
import bitinformation.bitinformation as bit
fn1 = "grib.grib"
res = bit.compare_data(data1, data2)This tool is based on work by Klöwer et. al:https://github.com/milankl/BitInformation.jl