Top Banner
Entropy & coding Ben Langmead Department of Computer Science Please sign guestbook (www.langmead-lab.org/teaching-materials) to tell me briey how you are using the slides. For original Keynote les, email me ([email protected]).
51

210 entropy coding pub · H wc(U) = log 2 |U| How many bits are required to encode items from universe U? If codes can have various lengths, longest code must be ≥ log 2 (|U|) If

Jul 25, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 210 entropy coding pub · H wc(U) = log 2 |U| How many bits are required to encode items from universe U? If codes can have various lengths, longest code must be ≥ log 2 (|U|) If

Entropy & codingBen Langmead

Department of Computer Science

Please sign guestbook (www.langmead-lab.org/teaching-materials) to tell me briefly how you are using the slides. For original Keynote files, email me ([email protected]).

Page 2: 210 entropy coding pub · H wc(U) = log 2 |U| How many bits are required to encode items from universe U? If codes can have various lengths, longest code must be ≥ log 2 (|U|) If

Say, code = rank + (13 * suit)

Where Ace = 0, Jack = 10, ... = 0, = 1, ...♠ ♥

Let's identify items with codes, made of bits

A 02 13 104 11

♠♠♠♠

10JQK

♣♣♣♣

...

110011110010110001110000

Entropy & coding

Page 3: 210 entropy coding pub · H wc(U) = log 2 |U| How many bits are required to encode items from universe U? If codes can have various lengths, longest code must be ≥ log 2 (|U|) If

Hwc(U) = log2 |U |

How many bits are required to encode items from universe ?U

If codes can have various lengths, longest code must be ≥ log2( |U | )

If codes must have same length, length must be , best choice is ≥ log2( |U | ) ⌈log2( |U | )⌉

Entropy & coding

Page 4: 210 entropy coding pub · H wc(U) = log 2 |U| How many bits are required to encode items from universe U? If codes can have various lengths, longest code must be ≥ log 2 (|U|) If

Entropy

How many bits required to identify an item from this set?

1 bit 3 bits 6 bits

(37 or 38 slots)

https://commons.wikimedia.org/wiki/File:13-02-27-spielbank-wiesbaden-by-RalfR-094.jpg

Page 5: 210 entropy coding pub · H wc(U) = log 2 |U| How many bits are required to encode items from universe U? If codes can have various lengths, longest code must be ≥ log 2 (|U|) If

Entropy

This is worst-case entropy

Hwc(U) = log2 |U |

If , then |U | = 2n Hwc(U) =

If ,U = {length-n strings from Σ = {1, . . . , σ}}then Hwc(U) = log2 σn = n log2 σ

n

Page 6: 210 entropy coding pub · H wc(U) = log 2 |U| How many bits are required to encode items from universe U? If codes can have various lengths, longest code must be ≥ log 2 (|U|) If

Entropy

ℓ̄ = ∑u∈U

Pr(u) ⋅ ℓ(u)

If codes can vary in length, we can use shorter codes for more frequent events

Seeking to minimize average (or expected) code length ℓ̄

= length of code for ℓ(u) u

https://commons.wikimedia.org/wiki/File:International_Morse_Code.svg

Page 7: 210 entropy coding pub · H wc(U) = log 2 |U| How many bits are required to encode items from universe U? If codes can have various lengths, longest code must be ≥ log 2 (|U|) If

Entropy

Instead of items , let's think of a discrete r.v. and its sample space & probability function

u ∈ UX Ω Pr

H(X) = ∑s ∈ Ω

Pr(s) ⋅ log21

Pr(s)

= − ∑s ∈ Ω

Pr(s) ⋅ log2 Pr(s)

This is Shannon entropy

Page 8: 210 entropy coding pub · H wc(U) = log 2 |U| How many bits are required to encode items from universe U? If codes can have various lengths, longest code must be ≥ log 2 (|U|) If

Entropy

H(X) = 0.5 ⋅ log21

0.5+ 0.5 ⋅ log2

10.5

= 0.5 ⋅ 1 + 0.5 ⋅ 1

X ={ : 0.5, : 0.5

{= 1

Page 9: 210 entropy coding pub · H wc(U) = log 2 |U| How many bits are required to encode items from universe U? If codes can have various lengths, longest code must be ≥ log 2 (|U|) If

Entropy

H(X) = 0.9 ⋅ log21

0.9+ 0.1 ⋅ log2

10.1

= 0.9 ⋅ 0.15 + 0.1 ⋅ 3.32

X ={ : 0.9, : 0.1

{= 0.47

Page 10: 210 entropy coding pub · H wc(U) = log 2 |U| How many bits are required to encode items from universe U? If codes can have various lengths, longest code must be ≥ log 2 (|U|) If

Entropy

●●

●●

●●

●●

●●

●●●●●●●●●●●●●●●●●●

●●

●●

●●

●●

0.0 0.2 0.4 0.6 0.8 1.0

0.0

0.2

0.4

0.6

0.8

1.0

p

H(X)

X = : p, : 1 − p {{

p = 0.1

p = 0.5

Page 11: 210 entropy coding pub · H wc(U) = log 2 |U| How many bits are required to encode items from universe U? If codes can have various lengths, longest code must be ≥ log 2 (|U|) If

Entropy

H(X) =6

∑i = 1

16

log2 6

= log2 6 = 2.58

X ={ :16 each

{

Page 12: 210 entropy coding pub · H wc(U) = log 2 |U| How many bits are required to encode items from universe U? If codes can have various lengths, longest code must be ≥ log 2 (|U|) If

Entropy

0

1

2

3

16

,16

,16

,16

,16

,16 1

4,

14

,14

,112

,112

,1121

2,

12

,0,0,0,0

1,0,0,0,0,0

12

,110

,110

,110

,110

,110

Page 13: 210 entropy coding pub · H wc(U) = log 2 |U| How many bits are required to encode items from universe U? If codes can have various lengths, longest code must be ≥ log 2 (|U|) If

Entropy

0

1

2

3 16

,16

,16

,16

,16

,16

12

,110

,110

,110

,110

,110

14

,14

,14

,112

,112

,112

12

,12

,0,0,0,0

1,0,0,0,0,0

Page 14: 210 entropy coding pub · H wc(U) = log 2 |U| How many bits are required to encode items from universe U? If codes can have various lengths, longest code must be ≥ log 2 (|U|) If

Entropy

H(X) = ∑s ∈ ΩX

Pr(s) ⋅ log21

Pr(s)

= ∑s ∈ ΩX

1|ΩX |

⋅ log2 |ΩX |

When outcomes are equally probable:

Matching the definition of worst-case entropy

= log2 |ΩX |

Page 15: 210 entropy coding pub · H wc(U) = log 2 |U| How many bits are required to encode items from universe U? If codes can have various lengths, longest code must be ≥ log 2 (|U|) If

Entropy

Shannon entropy is a function of a random variable

H(X)

The r.v. models a data source; e.g. a person speaking, or letters of a DNA string

Assumes a memoryless source; each item is an i.i.d. draw

Page 16: 210 entropy coding pub · H wc(U) = log 2 |U| How many bits are required to encode items from universe U? If codes can have various lengths, longest code must be ≥ log 2 (|U|) If

Entropy

So far we've seen

Worst-case entropy is a function of a setHwc(U)

Shannon entropy , a function of a random variable

H(X)

When outcomes are equiprobable, = H(X) Hwc(ΩX)

Page 17: 210 entropy coding pub · H wc(U) = log 2 |U| How many bits are required to encode items from universe U? If codes can have various lengths, longest code must be ≥ log 2 (|U|) If

Entropy

Say we have a memoryless binary source and an example string it emittedB

We can count B's 0s & 1s to "train" a model

H0(B) = H (X ∼ Bern( mn ))

=mn

log2nm

+n − m

nlog2

nn − m

m =n = |B |

# 1s in B

is the empirical zero order entropyH0

Page 18: 210 entropy coding pub · H wc(U) = log 2 |U| How many bits are required to encode items from universe U? If codes can have various lengths, longest code must be ≥ log 2 (|U|) If

Entropy

So:

Empirical zero order entropy of a sequence is the Shannon entropy of a memoryless source "trained" to

H0(B)B

B

Worst-case entropy is a function of a setHwc(U)

Shannon entropy , a function of a random variable

H(X)

Page 19: 210 entropy coding pub · H wc(U) = log 2 |U| How many bits are required to encode items from universe U? If codes can have various lengths, longest code must be ≥ log 2 (|U|) If

Codes

(Note: letters are separated by a pause of duration equal to three dots; words separated by 7-dot pause.)

A good code will:

Give unambiguous mappings for encoding & decoding

Allow efficient encoding & decoding

Minimize average code length (approach )H0

https://commons.wikimedia.org/wiki/File:International_Morse_Code.svg

Page 20: 210 entropy coding pub · H wc(U) = log 2 |U| How many bits are required to encode items from universe U? If codes can have various lengths, longest code must be ≥ log 2 (|U|) If

Codes

H(X) = ∑s ∈ Ω

Pr(s) ⋅ log21

Pr(s)

Shannon entropy equation hints at codes of

length log21

Pr(s)

Page 21: 210 entropy coding pub · H wc(U) = log 2 |U| How many bits are required to encode items from universe U? If codes can have various lengths, longest code must be ≥ log 2 (|U|) If

Codes

Say we have a source emitting symbols from alphabet Σ = {𝚊, 𝚌, 𝚐, 𝚝}

Source is memoryless, modeled by r.v.:

X = { 𝚊 :12

, 𝚌 :14

, 𝚐 :18

, 𝚝 :18

}

is a function mapping symbols to binary code sequences. C

C : Σ → {0,1} *

What kind of do we want?C

Page 22: 210 entropy coding pub · H wc(U) = log 2 |U| How many bits are required to encode items from universe U? If codes can have various lengths, longest code must be ≥ log 2 (|U|) If

Codes

Proposal 1

Example courtesy of Mathematicalmonk videos on information theory https://youtu.be/9MCxXJn7TPU

a a g c

0 0 1 1 0 1 0

C(𝚊) = 𝟶C(𝚌) = 𝟷𝟶C(𝚐) = 𝟷𝟷𝟶C(𝚝) = 𝟷𝟷𝟷

Each codeword is unique; i.e. is injectiveC

X = { 𝚊 :12

, 𝚌 :14

, 𝚐 :18

, 𝚝 :18

}

Page 23: 210 entropy coding pub · H wc(U) = log 2 |U| How many bits are required to encode items from universe U? If codes can have various lengths, longest code must be ≥ log 2 (|U|) If

Codes

Proposal 1

C(𝚊) = 𝟶C(𝚌) = 𝟷𝟶C(𝚐) = 𝟷𝟷𝟶C(𝚝) = 𝟷𝟷𝟷 1 1 1 0 0 1 0

Can we go recover original string from code?

?

Example courtesy of Mathematicalmonk videos on information theory https://youtu.be/9MCxXJn7TPU

Page 24: 210 entropy coding pub · H wc(U) = log 2 |U| How many bits are required to encode items from universe U? If codes can have various lengths, longest code must be ≥ log 2 (|U|) If

Codes

Proposal 1

C(𝚊) = 𝟶C(𝚌) = 𝟷𝟶C(𝚐) = 𝟷𝟷𝟶C(𝚝) = 𝟷𝟷𝟷 1 1 1 0 0 1 0

Can we go recover original string from code?

yes

t a a c

Example courtesy of Mathematicalmonk videos on information theory https://youtu.be/9MCxXJn7TPU

Page 25: 210 entropy coding pub · H wc(U) = log 2 |U| How many bits are required to encode items from universe U? If codes can have various lengths, longest code must be ≥ log 2 (|U|) If

Codes

Proposal 2

C(𝚊) = 𝟶C(𝚌) = 𝟷C(𝚐) = 𝟶𝟷C(𝚝) = 𝟷𝟶

a a g c

0 0 0 1 1

Example courtesy of Mathematicalmonk videos on information theory https://youtu.be/9MCxXJn7TPU

Again, is injectiveC

X = { 𝚊 :12

, 𝚌 :14

, 𝚐 :18

, 𝚝 :18

}

Page 26: 210 entropy coding pub · H wc(U) = log 2 |U| How many bits are required to encode items from universe U? If codes can have various lengths, longest code must be ≥ log 2 (|U|) If

Codes

Proposal 2

Can we go recover original string from code?

?

C(𝚊) = 𝟶C(𝚌) = 𝟷C(𝚐) = 𝟶𝟷C(𝚝) = 𝟷𝟶 0 0 0 1 1

Example courtesy of Mathematicalmonk videos on information theory https://youtu.be/9MCxXJn7TPU

Page 27: 210 entropy coding pub · H wc(U) = log 2 |U| How many bits are required to encode items from universe U? If codes can have various lengths, longest code must be ≥ log 2 (|U|) If

Codes

Proposal 2

Can we go recover original string from code?

?

C(𝚊) = 𝟶C(𝚌) = 𝟷C(𝚐) = 𝟶𝟷C(𝚝) = 𝟷𝟶 0 0 0 1 1

no

a a a c ? a a g ?

Example courtesy of Mathematicalmonk videos on information theory https://youtu.be/9MCxXJn7TPU

Page 28: 210 entropy coding pub · H wc(U) = log 2 |U| How many bits are required to encode items from universe U? If codes can have various lengths, longest code must be ≥ log 2 (|U|) If

Codes

Let be the code extended to sequences C′

C′ : Σ * → {0,1} *

C(𝚊) = 𝟶C(𝚌) = 𝟷𝟶C(𝚐) = 𝟷𝟷𝟶C(𝚝) = 𝟷𝟷𝟷

C′ (𝚊) = 𝟶C′ (𝚊𝚐) = 𝟶𝟷𝟷𝟶C′ (𝚝𝚝) = 𝟷𝟷𝟷𝟷𝟷𝟷C′ (𝚊𝚊𝚊𝚊𝚌) = 𝟶𝟶𝟶𝟶𝟷𝟶

Goal is for to be injective ( being injective is not enough)

C′

C

Page 29: 210 entropy coding pub · H wc(U) = log 2 |U| How many bits are required to encode items from universe U? If codes can have various lengths, longest code must be ≥ log 2 (|U|) If

Codes

Consider two codes, both unambiguous

C(𝚊) = 𝟷C(𝚌) = 𝟷𝟶C(𝚐) = 𝟶𝟶

C(𝚊) = 𝟷C(𝚌) = 𝟶𝟷C(𝚐) = 𝟶𝟶

A B

a a g c

1 1 0 0 1 0

a a g c

1 1 0 0 0 1

Page 30: 210 entropy coding pub · H wc(U) = log 2 |U| How many bits are required to encode items from universe U? If codes can have various lengths, longest code must be ≥ log 2 (|U|) If

Codes

Now we decode:

C(𝚊) = 𝟷C(𝚌) = 𝟷𝟶C(𝚐) = 𝟶𝟶

A 1 1 0 0 1 0Considering first 1, can't yet tell if it's an a or part of a c

Page 31: 210 entropy coding pub · H wc(U) = log 2 |U| How many bits are required to encode items from universe U? If codes can have various lengths, longest code must be ≥ log 2 (|U|) If

Codes

Now we decode:

C(𝚊) = 𝟷C(𝚌) = 𝟷𝟶C(𝚐) = 𝟶𝟶

A 1 1 0 0 1 0Now sure that first 1 is a. Not sure about second 1.

Page 32: 210 entropy coding pub · H wc(U) = log 2 |U| How many bits are required to encode items from universe U? If codes can have various lengths, longest code must be ≥ log 2 (|U|) If

Codes

Now we decode:

C(𝚊) = 𝟷C(𝚌) = 𝟷𝟶C(𝚐) = 𝟶𝟶

A 1 1 0 0 1 0

Either we have

ac...aag...

Page 33: 210 entropy coding pub · H wc(U) = log 2 |U| How many bits are required to encode items from universe U? If codes can have various lengths, longest code must be ≥ log 2 (|U|) If

Codes

Now we decode:

C(𝚊) = 𝟷C(𝚌) = 𝟷𝟶C(𝚐) = 𝟶𝟶

A 1 1 0 0 1 0

Either we have

acg...aag...

Page 34: 210 entropy coding pub · H wc(U) = log 2 |U| How many bits are required to encode items from universe U? If codes can have various lengths, longest code must be ≥ log 2 (|U|) If

Codes

Now we decode:

C(𝚊) = 𝟷C(𝚌) = 𝟷𝟶C(𝚐) = 𝟶𝟶

A 1 1 0 0 1 0

Now we're sure we have:

aag...

But could still be aaga... or aagc...

Page 35: 210 entropy coding pub · H wc(U) = log 2 |U| How many bits are required to encode items from universe U? If codes can have various lengths, longest code must be ≥ log 2 (|U|) If

Codes

Now we decode:

C(𝚊) = 𝟷C(𝚌) = 𝟷𝟶C(𝚐) = 𝟶𝟶

A 1 1 0 0 1 0

Now we're sure we have:

aagc

Page 36: 210 entropy coding pub · H wc(U) = log 2 |U| How many bits are required to encode items from universe U? If codes can have various lengths, longest code must be ≥ log 2 (|U|) If

Codes

Consider an example with a longer run of 0s:

C(𝚊) = 𝟷C(𝚌) = 𝟷𝟶C(𝚐) = 𝟶𝟶

A

1 1 0 0 0 0 0 0 0 1 0

Can't distinguish a from c until we see whether run of 0s is odd or even

a?c?

Since it's odd, must be a c: acgggc

Page 37: 210 entropy coding pub · H wc(U) = log 2 |U| How many bits are required to encode items from universe U? If codes can have various lengths, longest code must be ≥ log 2 (|U|) If

Codes

Now we decode:

1 1 0 0 0 1Considering first 1, we're immediately sure it's an a

C(𝚊) = 𝟷C(𝚌) = 𝟶𝟷C(𝚐) = 𝟶𝟶

B

Page 38: 210 entropy coding pub · H wc(U) = log 2 |U| How many bits are required to encode items from universe U? If codes can have various lengths, longest code must be ≥ log 2 (|U|) If

Codes

Now we decode:

1 1 0 0 0 1

Definitely aaC(𝚊) = 𝟷C(𝚌) = 𝟶𝟷C(𝚐) = 𝟶𝟶

B

Page 39: 210 entropy coding pub · H wc(U) = log 2 |U| How many bits are required to encode items from universe U? If codes can have various lengths, longest code must be ≥ log 2 (|U|) If

Codes

Now we decode:

1 1 0 0 0 1

Could be aac or aagC(𝚊) = 𝟷C(𝚌) = 𝟶𝟷C(𝚐) = 𝟶𝟶

B

Page 40: 210 entropy coding pub · H wc(U) = log 2 |U| How many bits are required to encode items from universe U? If codes can have various lengths, longest code must be ≥ log 2 (|U|) If

Codes

Now we decode:

1 1 0 0 0 1

Definitely aagC(𝚊) = 𝟷C(𝚌) = 𝟶𝟷C(𝚐) = 𝟶𝟶

B

Page 41: 210 entropy coding pub · H wc(U) = log 2 |U| How many bits are required to encode items from universe U? If codes can have various lengths, longest code must be ≥ log 2 (|U|) If

Codes

Now we decode:

1 1 0 0 0 1C(𝚊) = 𝟷C(𝚌) = 𝟶𝟷C(𝚐) = 𝟶𝟶

B

Could be aagc or aagg

Page 42: 210 entropy coding pub · H wc(U) = log 2 |U| How many bits are required to encode items from universe U? If codes can have various lengths, longest code must be ≥ log 2 (|U|) If

Codes

Now we decode:

1 1 0 0 0 1C(𝚊) = 𝟷C(𝚌) = 𝟶𝟷C(𝚐) = 𝟶𝟶

B

Definitely aagc

Page 43: 210 entropy coding pub · H wc(U) = log 2 |U| How many bits are required to encode items from universe U? If codes can have various lengths, longest code must be ≥ log 2 (|U|) If

Codes

C(𝚊) = 𝟷C(𝚌) = 𝟶𝟷C(𝚐) = 𝟶𝟶

B

1 0 1 0 0 0 0 0 0 1 0c

No problems with decoding efficiency here.

Code is prefix-free; no code is a prefix of another. Also called a prefix code for short.

ggg

AKA instantaneous

Page 44: 210 entropy coding pub · H wc(U) = log 2 |U| How many bits are required to encode items from universe U? If codes can have various lengths, longest code must be ≥ log 2 (|U|) If

Huffman

Say we start with a string: abracadabra

Or equivalently, a r.v.:

X = { 𝚊 :511

, 𝚋 :211

, 𝚌 :11

, 𝚍 :111

, 𝚛 :211

}

{ 𝚊 : 5, 𝚋 : 2, 𝚌 : 1, 𝚍 : 1 𝚛 : 2}

Can compile symbols and their frequencies:

Page 45: 210 entropy coding pub · H wc(U) = log 2 |U| How many bits are required to encode items from universe U? If codes can have various lengths, longest code must be ≥ log 2 (|U|) If

Huffman

{ 𝚊 : 5, 𝚋 : 2, 𝚌 : 1, 𝚍 : 1 𝚛 : 2}

a d r5 2 1 1 2

In each round, join the 2 subtrees with lowest total weight

b c

Page 46: 210 entropy coding pub · H wc(U) = log 2 |U| How many bits are required to encode items from universe U? If codes can have various lengths, longest code must be ≥ log 2 (|U|) If

Huffman

{ 𝚊 : 5, 𝚋 : 2, 𝚌 : 1, 𝚍 : 1 𝚛 : 2}

a d r5 2 1 1 2

In each round, join the 2 subtrees with lowest total weight

2

b c

Weight of new subtree is sum of children

Page 47: 210 entropy coding pub · H wc(U) = log 2 |U| How many bits are required to encode items from universe U? If codes can have various lengths, longest code must be ≥ log 2 (|U|) If

Huffman

a d r5 2 1 1 2

2

4

b c

Page 48: 210 entropy coding pub · H wc(U) = log 2 |U| How many bits are required to encode items from universe U? If codes can have various lengths, longest code must be ≥ log 2 (|U|) If

Huffman

a d5 2 1 1 2

2

b c

4

r

6

Page 49: 210 entropy coding pub · H wc(U) = log 2 |U| How many bits are required to encode items from universe U? If codes can have various lengths, longest code must be ≥ log 2 (|U|) If

Huffman

a d5 2 1 1 2

2

b c

4

r

6

11

This is the tree but what is the code?

Page 50: 210 entropy coding pub · H wc(U) = log 2 |U| How many bits are required to encode items from universe U? If codes can have various lengths, longest code must be ≥ log 2 (|U|) If

Huffman

a d5 2 1 1 2

2

b c

4

r

6

11

0

1

0 1

01

0 1

C(𝚊) = 𝟶C(𝚋) = 𝟷𝟶𝟶C(𝚌) = 𝟷𝟶𝟷𝟶C(𝚍) = 𝟷𝟶𝟷𝟷C(𝚛) = 𝟷𝟷

Label edges with 0/1 according to left/right child of parent

Codes equal root-to-leaf concatenations of 0/1's

Page 51: 210 entropy coding pub · H wc(U) = log 2 |U| How many bits are required to encode items from universe U? If codes can have various lengths, longest code must be ≥ log 2 (|U|) If

Huffman

In other words, if is the number of bits in the Huffman code for an input string of length

cS n

Huffman codes are "optimal," wasting at most 1 bit per symbol

c ≤ n(H0(S) + 1) bits