NatUnit.lean

Lean 4 source, lean/DataMiningAsObservation/NatUnit.lean at commit bccca4f. Appendix C, Machine-checked statements, says what each file proves. All Lean files.

/-
NatUnit.lean. Chapter 0 section 0.7 and chapter 4 section 4.2, the nat.

A nat is the unit of information measured with the natural logarithm, as a bit is with the
logarithm to base two. The two conversions undo each other, the conversion is monotone,
and one nat is between 1.4426 and 1.4427 bits.
-/
import Mathlib

namespace DMO.NatUnit

/-- Bits from nats. -/
noncomputable def bitsOfNats (x : ℝ) : ℝ := x / Real.log 2

/-- Nats from bits. -/
noncomputable def natsOfBits (b : ℝ) : ℝ := b * Real.log 2

theorem log_two_pos : 0 < Real.log 2 := Real.log_pos (by norm_num)

/-- **The conversions undo each other.** -/
theorem nats_bits (x : ℝ) : natsOfBits (bitsOfNats x) = x := by
  unfold natsOfBits bitsOfNats
  rw [div_mul_cancel₀ _ log_two_pos.ne']

theorem bits_nats (b : ℝ) : bitsOfNats (natsOfBits b) = b := by
  unfold natsOfBits bitsOfNats
  rw [mul_div_cancel_right₀ _ log_two_pos.ne']

/-- **The conversion is monotone.** -/
theorem bitsOfNats_mono {x y : ℝ} (h : x ≤ y) : bitsOfNats x ≤ bitsOfNats y := by
  unfold bitsOfNats
  exact div_le_div_of_nonneg_right h log_two_pos.le

/-- **One nat is between 1.4426 and 1.4427 bits.** -/
theorem one_nat_in_bits : 1.4426 < bitsOfNats 1 ∧ bitsOfNats 1 < 1.4427 := by
  unfold bitsOfNats
  constructor
  · rw [lt_div_iff₀ log_two_pos]
    linarith [Real.log_two_lt_d9]
  · rw [div_lt_iff₀ log_two_pos]
    linarith [Real.log_two_gt_d9]

end DMO.NatUnit