InformationTheory

InformationTheory.Shannon.ArithmeticCoding

source

Arithmetic Coding / Shannon-Fano-Elias (Cover-Thomas) #

For a finite-alphabet probability distribution P on α, the arithmetic code assigns each symbol a : α a binary codeword of length ℓ(a) = ⌈-log₂ P(a)⌉ + 1, achieving the expected-length sandwich H₂(P) ≤ E[L] ≤ H₂(P) + 2 and prefix-freeness.

Main definitions #

  • sfeLength — Shannon-Fano-Elias codeword length ⌈-log₂ P(a)⌉ + 1.

Main statements #

Implementation notes #

The textbook construction truncates the binary expansion of the cumulative midpoint F̄(a) := F(a) - P(a)/2; the real-valued binary expansion is a Mathlib gap and is avoided entirely. The expected-length bounds use a length-only linear lift of the Shannon-code machinery (entropyD_le_expectedLength_of_kraft / expectedLength_shannon_lt_entropyD_add_one), and the prefix-free code is obtained from exists_prefix_code_of_kraft (integer-slot Kraft-reverse) lifted to List Bool via finTwoEquiv. The textbook midpoint-expansion equivalence is out of scope — it gives the same code and is not needed for the bounds.

References #

  • T. M. Cover and J. A. Thomas, Elements of Information Theory (2nd ed.), Wiley, 2006. (Shannon-Fano-Elias coding); (unique decodability).
noncomputable def

InformationTheory.Shannon.ArithmeticCoding.sfeLength

source
{α : Type u_1} [MeasurableSpace α] (P : MeasureTheory.Measure α) (a : α) :

The Shannon-Fano-Elias codeword length ℓ(a) = ⌈-log₂ P(a)⌉ + 1.

Equations
Instances For
    Used by
      theorem

      InformationTheory.Shannon.ArithmeticCoding.sfeLength_pos

      source
      {α : Type u_1} [Fintype α] [Nonempty α] [MeasurableSpace α] [MeasurableSingletonClass α] (P : MeasureTheory.Measure α) (a : α) :
      0 < sfeLength P a
      Used by
        theorem

        InformationTheory.Shannon.ArithmeticCoding.sfeLength_kraft_le_one

        source

        sfeLength satisfies the Kraft inequality Σ 2^(-ℓ(a)) ≤ 1.

        Each term halves the Shannon-length term: 2^(-(l+1)) = 2^(-l)/2, so the whole sum is (1/2) · kraftSum 2 (shannonLength 2 P) ≤ 1/2 ≤ 1.

        Used by
          theorem

          InformationTheory.Shannon.ArithmeticCoding.arithmeticCode_expected_length_bounds

          source

          Shannon-Fano-Elias coding: the expected-length sandwich H₂(P) ≤ E[L] ≤ H₂(P) + 2.

          Used by
            theorem

            InformationTheory.Shannon.ArithmeticCoding.arithmeticCode_prefix_free

            source
            {α : Type u_1} [Fintype α] [Nonempty α] [MeasurableSpace α] [MeasurableSingletonClass α] (P : MeasureTheory.Measure α) [MeasureTheory.IsProbabilityMeasure P] (hP : ∀ (a : α), 0 < P.real {a}) :
            ∃ (c : αList Bool), (∀ (a : α), (c a).length = sfeLength P a) Function.Injective c ∀ (a b : α), a b¬c a <+: c b

            The prefix-free construction: there is a binary code of length sfeLength P that is injective and prefix-free.

            Used by
              theorem

              InformationTheory.Shannon.ArithmeticCoding.arithmeticCode_unique_decodable

              source
              {α : Type u_1} [Fintype α] [Nonempty α] [MeasurableSpace α] [MeasurableSingletonClass α] (c : αList Bool) (h_pf : ∀ (a b : α), a b¬c a <+: c b) (h_ne : ∀ (a : α), c a []) (s₁ s₂ : List α) :
              (List.map c s₁).flatten = (List.map c s₂).flattens₁ = s₂

              Unique decodability: for a prefix-free code with nonempty codewords, the map s ↦ (s.map c).flatten is injective.

              Used by