src/HOL/Probability/Information.thy
author wenzelm
Tue Feb 28 21:53:36 2012 +0100 (2012-02-28)
changeset 46731 5302e932d1e5
parent 45777 c36637603821
child 46905 6b1c0a80a57a
permissions -rw-r--r--
avoid undeclared variables in let bindings;
tuned proofs;
hoelzl@42067
     1
(*  Title:      HOL/Probability/Information.thy
hoelzl@42067
     2
    Author:     Johannes Hölzl, TU München
hoelzl@42067
     3
    Author:     Armin Heller, TU München
hoelzl@42067
     4
*)
hoelzl@42067
     5
hoelzl@42067
     6
header {*Information theory*}
hoelzl@42067
     7
hoelzl@36080
     8
theory Information
wenzelm@41413
     9
imports
hoelzl@43340
    10
  Independent_Family
hoelzl@43556
    11
  Radon_Nikodym
wenzelm@41413
    12
  "~~/src/HOL/Library/Convex"
hoelzl@36080
    13
begin
hoelzl@36080
    14
hoelzl@39097
    15
lemma log_le: "1 < a \<Longrightarrow> 0 < x \<Longrightarrow> x \<le> y \<Longrightarrow> log a x \<le> log a y"
hoelzl@39097
    16
  by (subst log_le_cancel_iff) auto
hoelzl@39097
    17
hoelzl@39097
    18
lemma log_less: "1 < a \<Longrightarrow> 0 < x \<Longrightarrow> x < y \<Longrightarrow> log a x < log a y"
hoelzl@39097
    19
  by (subst log_less_cancel_iff) auto
hoelzl@39097
    20
hoelzl@39097
    21
lemma setsum_cartesian_product':
hoelzl@39097
    22
  "(\<Sum>x\<in>A \<times> B. f x) = (\<Sum>x\<in>A. setsum (\<lambda>y. f (x, y)) B)"
hoelzl@39097
    23
  unfolding setsum_cartesian_product by simp
hoelzl@39097
    24
hoelzl@36624
    25
section "Convex theory"
hoelzl@36080
    26
hoelzl@36624
    27
lemma log_setsum:
hoelzl@36624
    28
  assumes "finite s" "s \<noteq> {}"
hoelzl@36624
    29
  assumes "b > 1"
hoelzl@36624
    30
  assumes "(\<Sum> i \<in> s. a i) = 1"
hoelzl@36624
    31
  assumes "\<And> i. i \<in> s \<Longrightarrow> a i \<ge> 0"
hoelzl@36624
    32
  assumes "\<And> i. i \<in> s \<Longrightarrow> y i \<in> {0 <..}"
hoelzl@36624
    33
  shows "log b (\<Sum> i \<in> s. a i * y i) \<ge> (\<Sum> i \<in> s. a i * log b (y i))"
hoelzl@36624
    34
proof -
hoelzl@36624
    35
  have "convex_on {0 <..} (\<lambda> x. - log b x)"
hoelzl@36624
    36
    by (rule minus_log_convex[OF `b > 1`])
hoelzl@36624
    37
  hence "- log b (\<Sum> i \<in> s. a i * y i) \<le> (\<Sum> i \<in> s. a i * - log b (y i))"
nipkow@44890
    38
    using convex_on_setsum[of _ _ "\<lambda> x. - log b x"] assms pos_is_convex by fastforce
hoelzl@36624
    39
  thus ?thesis by (auto simp add:setsum_negf le_imp_neg_le)
hoelzl@36624
    40
qed
hoelzl@36080
    41
hoelzl@36624
    42
lemma log_setsum':
hoelzl@36624
    43
  assumes "finite s" "s \<noteq> {}"
hoelzl@36624
    44
  assumes "b > 1"
hoelzl@36624
    45
  assumes "(\<Sum> i \<in> s. a i) = 1"
hoelzl@36624
    46
  assumes pos: "\<And> i. i \<in> s \<Longrightarrow> 0 \<le> a i"
hoelzl@36624
    47
          "\<And> i. \<lbrakk> i \<in> s ; 0 < a i \<rbrakk> \<Longrightarrow> 0 < y i"
hoelzl@36624
    48
  shows "log b (\<Sum> i \<in> s. a i * y i) \<ge> (\<Sum> i \<in> s. a i * log b (y i))"
hoelzl@36080
    49
proof -
hoelzl@36624
    50
  have "\<And>y. (\<Sum> i \<in> s - {i. a i = 0}. a i * y i) = (\<Sum> i \<in> s. a i * y i)"
hoelzl@36624
    51
    using assms by (auto intro!: setsum_mono_zero_cong_left)
hoelzl@36624
    52
  moreover have "log b (\<Sum> i \<in> s - {i. a i = 0}. a i * y i) \<ge> (\<Sum> i \<in> s - {i. a i = 0}. a i * log b (y i))"
hoelzl@36624
    53
  proof (rule log_setsum)
hoelzl@36624
    54
    have "setsum a (s - {i. a i = 0}) = setsum a s"
hoelzl@36624
    55
      using assms(1) by (rule setsum_mono_zero_cong_left) auto
hoelzl@36624
    56
    thus sum_1: "setsum a (s - {i. a i = 0}) = 1"
hoelzl@36624
    57
      "finite (s - {i. a i = 0})" using assms by simp_all
hoelzl@36624
    58
hoelzl@36624
    59
    show "s - {i. a i = 0} \<noteq> {}"
hoelzl@36624
    60
    proof
hoelzl@36624
    61
      assume *: "s - {i. a i = 0} = {}"
hoelzl@36624
    62
      hence "setsum a (s - {i. a i = 0}) = 0" by (simp add: * setsum_empty)
hoelzl@36624
    63
      with sum_1 show False by simp
hoelzl@38656
    64
    qed
hoelzl@36624
    65
hoelzl@36624
    66
    fix i assume "i \<in> s - {i. a i = 0}"
hoelzl@36624
    67
    hence "i \<in> s" "a i \<noteq> 0" by simp_all
hoelzl@36624
    68
    thus "0 \<le> a i" "y i \<in> {0<..}" using pos[of i] by auto
hoelzl@36624
    69
  qed fact+
hoelzl@36624
    70
  ultimately show ?thesis by simp
hoelzl@36080
    71
qed
hoelzl@36080
    72
hoelzl@36624
    73
lemma log_setsum_divide:
hoelzl@36624
    74
  assumes "finite S" and "S \<noteq> {}" and "1 < b"
hoelzl@36624
    75
  assumes "(\<Sum>x\<in>S. g x) = 1"
hoelzl@36624
    76
  assumes pos: "\<And>x. x \<in> S \<Longrightarrow> g x \<ge> 0" "\<And>x. x \<in> S \<Longrightarrow> f x \<ge> 0"
hoelzl@36624
    77
  assumes g_pos: "\<And>x. \<lbrakk> x \<in> S ; 0 < g x \<rbrakk> \<Longrightarrow> 0 < f x"
hoelzl@36624
    78
  shows "- (\<Sum>x\<in>S. g x * log b (g x / f x)) \<le> log b (\<Sum>x\<in>S. f x)"
hoelzl@36624
    79
proof -
hoelzl@36624
    80
  have log_mono: "\<And>x y. 0 < x \<Longrightarrow> x \<le> y \<Longrightarrow> log b x \<le> log b y"
hoelzl@36624
    81
    using `1 < b` by (subst log_le_cancel_iff) auto
hoelzl@36080
    82
hoelzl@36624
    83
  have "- (\<Sum>x\<in>S. g x * log b (g x / f x)) = (\<Sum>x\<in>S. g x * log b (f x / g x))"
hoelzl@36624
    84
  proof (unfold setsum_negf[symmetric], rule setsum_cong)
hoelzl@36624
    85
    fix x assume x: "x \<in> S"
hoelzl@36624
    86
    show "- (g x * log b (g x / f x)) = g x * log b (f x / g x)"
hoelzl@36624
    87
    proof (cases "g x = 0")
hoelzl@36624
    88
      case False
hoelzl@36624
    89
      with pos[OF x] g_pos[OF x] have "0 < f x" "0 < g x" by simp_all
hoelzl@36624
    90
      thus ?thesis using `1 < b` by (simp add: log_divide field_simps)
hoelzl@36624
    91
    qed simp
hoelzl@36624
    92
  qed rule
hoelzl@36624
    93
  also have "... \<le> log b (\<Sum>x\<in>S. g x * (f x / g x))"
hoelzl@36624
    94
  proof (rule log_setsum')
hoelzl@36624
    95
    fix x assume x: "x \<in> S" "0 < g x"
hoelzl@36624
    96
    with g_pos[OF x] show "0 < f x / g x" by (safe intro!: divide_pos_pos)
hoelzl@36624
    97
  qed fact+
hoelzl@36624
    98
  also have "... = log b (\<Sum>x\<in>S - {x. g x = 0}. f x)" using `finite S`
hoelzl@36624
    99
    by (auto intro!: setsum_mono_zero_cong_right arg_cong[where f="log b"]
hoelzl@36624
   100
        split: split_if_asm)
hoelzl@36624
   101
  also have "... \<le> log b (\<Sum>x\<in>S. f x)"
hoelzl@36624
   102
  proof (rule log_mono)
hoelzl@36624
   103
    have "0 = (\<Sum>x\<in>S - {x. g x = 0}. 0)" by simp
hoelzl@36624
   104
    also have "... < (\<Sum>x\<in>S - {x. g x = 0}. f x)" (is "_ < ?sum")
hoelzl@36624
   105
    proof (rule setsum_strict_mono)
hoelzl@36624
   106
      show "finite (S - {x. g x = 0})" using `finite S` by simp
hoelzl@36624
   107
      show "S - {x. g x = 0} \<noteq> {}"
hoelzl@36624
   108
      proof
hoelzl@36624
   109
        assume "S - {x. g x = 0} = {}"
hoelzl@36624
   110
        hence "(\<Sum>x\<in>S. g x) = 0" by (subst setsum_0') auto
hoelzl@36624
   111
        with `(\<Sum>x\<in>S. g x) = 1` show False by simp
hoelzl@36624
   112
      qed
hoelzl@36624
   113
      fix x assume "x \<in> S - {x. g x = 0}"
hoelzl@36624
   114
      thus "0 < f x" using g_pos[of x] pos(1)[of x] by auto
hoelzl@36624
   115
    qed
hoelzl@36624
   116
    finally show "0 < ?sum" .
hoelzl@36624
   117
    show "(\<Sum>x\<in>S - {x. g x = 0}. f x) \<le> (\<Sum>x\<in>S. f x)"
hoelzl@36624
   118
      using `finite S` pos by (auto intro!: setsum_mono2)
hoelzl@36080
   119
  qed
hoelzl@36624
   120
  finally show ?thesis .
hoelzl@36080
   121
qed
hoelzl@36080
   122
hoelzl@39097
   123
lemma split_pairs:
hoelzl@40859
   124
  "((A, B) = X) \<longleftrightarrow> (fst X = A \<and> snd X = B)" and
hoelzl@40859
   125
  "(X = (A, B)) \<longleftrightarrow> (fst X = A \<and> snd X = B)" by auto
hoelzl@38656
   126
hoelzl@38656
   127
section "Information theory"
hoelzl@38656
   128
hoelzl@40859
   129
locale information_space = prob_space +
hoelzl@38656
   130
  fixes b :: real assumes b_gt_1: "1 < b"
hoelzl@38656
   131
hoelzl@40859
   132
context information_space
hoelzl@38656
   133
begin
hoelzl@38656
   134
hoelzl@40859
   135
text {* Introduce some simplification rules for logarithm of base @{term b}. *}
hoelzl@40859
   136
hoelzl@40859
   137
lemma log_neg_const:
hoelzl@40859
   138
  assumes "x \<le> 0"
hoelzl@40859
   139
  shows "log b x = log b 0"
hoelzl@36624
   140
proof -
hoelzl@40859
   141
  { fix u :: real
hoelzl@40859
   142
    have "x \<le> 0" by fact
hoelzl@40859
   143
    also have "0 < exp u"
hoelzl@40859
   144
      using exp_gt_zero .
hoelzl@40859
   145
    finally have "exp u \<noteq> x"
hoelzl@40859
   146
      by auto }
hoelzl@40859
   147
  then show "log b x = log b 0"
hoelzl@40859
   148
    by (simp add: log_def ln_def)
hoelzl@38656
   149
qed
hoelzl@38656
   150
hoelzl@40859
   151
lemma log_mult_eq:
hoelzl@40859
   152
  "log b (A * B) = (if 0 < A * B then log b \<bar>A\<bar> + log b \<bar>B\<bar> else log b 0)"
hoelzl@40859
   153
  using log_mult[of b "\<bar>A\<bar>" "\<bar>B\<bar>"] b_gt_1 log_neg_const[of "A * B"]
hoelzl@40859
   154
  by (auto simp: zero_less_mult_iff mult_le_0_iff)
hoelzl@38656
   155
hoelzl@40859
   156
lemma log_inverse_eq:
hoelzl@40859
   157
  "log b (inverse B) = (if 0 < B then - log b B else log b 0)"
hoelzl@40859
   158
  using log_inverse[of b B] log_neg_const[of "inverse B"] b_gt_1 by simp
hoelzl@36080
   159
hoelzl@40859
   160
lemma log_divide_eq:
hoelzl@40859
   161
  "log b (A / B) = (if 0 < A * B then log b \<bar>A\<bar> - log b \<bar>B\<bar> else log b 0)"
hoelzl@40859
   162
  unfolding divide_inverse log_mult_eq log_inverse_eq abs_inverse
hoelzl@40859
   163
  by (auto simp: zero_less_mult_iff mult_le_0_iff)
hoelzl@38656
   164
hoelzl@40859
   165
lemmas log_simps = log_mult_eq log_inverse_eq log_divide_eq
hoelzl@38656
   166
hoelzl@38656
   167
end
hoelzl@38656
   168
hoelzl@39097
   169
subsection "Kullback$-$Leibler divergence"
hoelzl@36080
   170
hoelzl@39097
   171
text {* The Kullback$-$Leibler divergence is also known as relative entropy or
hoelzl@39097
   172
Kullback$-$Leibler distance. *}
hoelzl@39097
   173
hoelzl@39097
   174
definition
hoelzl@43340
   175
  "entropy_density b M \<nu> = log b \<circ> real \<circ> RN_deriv M \<nu>"
hoelzl@43340
   176
hoelzl@43340
   177
definition
hoelzl@43340
   178
  "KL_divergence b M \<nu> = integral\<^isup>L (M\<lparr>measure := \<nu>\<rparr>) (entropy_density b M \<nu>)"
hoelzl@43340
   179
hoelzl@43340
   180
lemma (in information_space) measurable_entropy_density:
hoelzl@43340
   181
  assumes ps: "prob_space (M\<lparr>measure := \<nu>\<rparr>)"
hoelzl@43340
   182
  assumes ac: "absolutely_continuous \<nu>"
hoelzl@43340
   183
  shows "entropy_density b M \<nu> \<in> borel_measurable M"
hoelzl@43340
   184
proof -
hoelzl@43340
   185
  interpret \<nu>: prob_space "M\<lparr>measure := \<nu>\<rparr>" by fact
hoelzl@43340
   186
  have "measure_space (M\<lparr>measure := \<nu>\<rparr>)" by fact
hoelzl@43340
   187
  from RN_deriv[OF this ac] b_gt_1 show ?thesis
hoelzl@43340
   188
    unfolding entropy_density_def
hoelzl@43340
   189
    by (intro measurable_comp) auto
hoelzl@43340
   190
qed
hoelzl@43340
   191
hoelzl@43340
   192
lemma (in information_space) KL_gt_0:
hoelzl@43340
   193
  assumes ps: "prob_space (M\<lparr>measure := \<nu>\<rparr>)"
hoelzl@43340
   194
  assumes ac: "absolutely_continuous \<nu>"
hoelzl@43340
   195
  assumes int: "integrable (M\<lparr> measure := \<nu> \<rparr>) (entropy_density b M \<nu>)"
hoelzl@43340
   196
  assumes A: "A \<in> sets M" "\<nu> A \<noteq> \<mu> A"
hoelzl@43340
   197
  shows "0 < KL_divergence b M \<nu>"
hoelzl@43340
   198
proof -
hoelzl@43340
   199
  interpret \<nu>: prob_space "M\<lparr>measure := \<nu>\<rparr>" by fact
hoelzl@43340
   200
  have ms: "measure_space (M\<lparr>measure := \<nu>\<rparr>)" by default
hoelzl@45777
   201
  have fms: "finite_measure (M\<lparr>measure := \<nu>\<rparr>)" by unfold_locales
hoelzl@43340
   202
  note RN = RN_deriv[OF ms ac]
hoelzl@43340
   203
hoelzl@43340
   204
  from real_RN_deriv[OF fms ac] guess D . note D = this
hoelzl@43340
   205
  with absolutely_continuous_AE[OF ms] ac
hoelzl@43920
   206
  have D\<nu>: "AE x in M\<lparr>measure := \<nu>\<rparr>. RN_deriv M \<nu> x = ereal (D x)"
hoelzl@43340
   207
    by auto
hoelzl@43340
   208
hoelzl@43340
   209
  def f \<equiv> "\<lambda>x. if D x = 0 then 1 else 1 / D x"
hoelzl@43340
   210
  with D have f_borel: "f \<in> borel_measurable M"
hoelzl@43340
   211
    by (auto intro!: measurable_If)
hoelzl@43340
   212
hoelzl@43340
   213
  have "KL_divergence b M \<nu> = 1 / ln b * (\<integral> x. ln b * entropy_density b M \<nu> x \<partial>M\<lparr>measure := \<nu>\<rparr>)"
hoelzl@43340
   214
    unfolding KL_divergence_def using int b_gt_1
hoelzl@43340
   215
    by (simp add: integral_cmult)
hoelzl@43340
   216
hoelzl@43340
   217
  { fix A assume "A \<in> sets M"
hoelzl@43920
   218
    with RN D have "\<nu>.\<mu> A = (\<integral>\<^isup>+ x. ereal (D x) * indicator A x \<partial>M)"
hoelzl@43340
   219
      by (auto intro!: positive_integral_cong_AE) }
hoelzl@43340
   220
  note D_density = this
hoelzl@43340
   221
hoelzl@43340
   222
  have ln_entropy: "(\<lambda>x. ln b * entropy_density b M \<nu> x) \<in> borel_measurable M"
hoelzl@43340
   223
    using measurable_entropy_density[OF ps ac] by auto
hoelzl@43340
   224
hoelzl@43340
   225
  have "integrable (M\<lparr>measure := \<nu>\<rparr>) (\<lambda>x. ln b * entropy_density b M \<nu> x)"
hoelzl@43340
   226
    using int by auto
hoelzl@43340
   227
  moreover have "integrable (M\<lparr>measure := \<nu>\<rparr>) (\<lambda>x. ln b * entropy_density b M \<nu> x) \<longleftrightarrow>
hoelzl@43340
   228
      integrable M (\<lambda>x. D x * (ln b * entropy_density b M \<nu> x))"
hoelzl@43340
   229
    using D D_density ln_entropy
hoelzl@43340
   230
    by (intro integral_translated_density) auto
hoelzl@43340
   231
  ultimately have M_int: "integrable M (\<lambda>x. D x * (ln b * entropy_density b M \<nu> x))"
hoelzl@43340
   232
    by simp
hoelzl@43340
   233
hoelzl@43920
   234
  have D_neg: "(\<integral>\<^isup>+ x. ereal (- D x) \<partial>M) = 0"
hoelzl@43340
   235
    using D by (subst positive_integral_0_iff_AE) auto
hoelzl@43340
   236
hoelzl@43920
   237
  have "(\<integral>\<^isup>+ x. ereal (D x) \<partial>M) = \<nu> (space M)"
hoelzl@43340
   238
    using RN D by (auto intro!: positive_integral_cong_AE)
hoelzl@43920
   239
  then have D_pos: "(\<integral>\<^isup>+ x. ereal (D x) \<partial>M) = 1"
hoelzl@43340
   240
    using \<nu>.measure_space_1 by simp
hoelzl@43340
   241
hoelzl@43340
   242
  have "integrable M D"
hoelzl@43340
   243
    using D_pos D_neg D by (auto simp: integrable_def)
hoelzl@43340
   244
hoelzl@43340
   245
  have "integral\<^isup>L M D = 1"
hoelzl@43340
   246
    using D_pos D_neg by (auto simp: lebesgue_integral_def)
hoelzl@43340
   247
hoelzl@43340
   248
  let ?D_set = "{x\<in>space M. D x \<noteq> 0}"
hoelzl@43340
   249
  have [simp, intro]: "?D_set \<in> sets M"
hoelzl@43340
   250
    using D by (auto intro: sets_Collect)
hoelzl@43340
   251
hoelzl@43340
   252
  have "0 \<le> 1 - \<mu>' ?D_set"
hoelzl@43340
   253
    using prob_le_1 by (auto simp: field_simps)
hoelzl@43340
   254
  also have "\<dots> = (\<integral> x. D x - indicator ?D_set x \<partial>M)"
hoelzl@43340
   255
    using `integrable M D` `integral\<^isup>L M D = 1`
hoelzl@43340
   256
    by (simp add: \<mu>'_def)
hoelzl@43340
   257
  also have "\<dots> < (\<integral> x. D x * (ln b * entropy_density b M \<nu> x) \<partial>M)"
hoelzl@43340
   258
  proof (rule integral_less_AE)
hoelzl@43340
   259
    show "integrable M (\<lambda>x. D x - indicator ?D_set x)"
hoelzl@43340
   260
      using `integrable M D`
hoelzl@43340
   261
      by (intro integral_diff integral_indicator) auto
hoelzl@43340
   262
  next
hoelzl@43340
   263
    show "integrable M (\<lambda>x. D x * (ln b * entropy_density b M \<nu> x))"
hoelzl@43340
   264
      by fact
hoelzl@43340
   265
  next
hoelzl@43340
   266
    show "\<mu> {x\<in>space M. D x \<noteq> 1 \<and> D x \<noteq> 0} \<noteq> 0"
hoelzl@43340
   267
    proof
hoelzl@43340
   268
      assume eq_0: "\<mu> {x\<in>space M. D x \<noteq> 1 \<and> D x \<noteq> 0} = 0"
hoelzl@43340
   269
      then have disj: "AE x. D x = 1 \<or> D x = 0"
hoelzl@43340
   270
        using D(1) by (auto intro!: AE_I[OF subset_refl] sets_Collect)
hoelzl@43340
   271
hoelzl@43340
   272
      have "\<mu> {x\<in>space M. D x = 1} = (\<integral>\<^isup>+ x. indicator {x\<in>space M. D x = 1} x \<partial>M)"
hoelzl@43340
   273
        using D(1) by auto
hoelzl@43920
   274
      also have "\<dots> = (\<integral>\<^isup>+ x. ereal (D x) * indicator {x\<in>space M. D x \<noteq> 0} x \<partial>M)"
hoelzl@43920
   275
        using disj by (auto intro!: positive_integral_cong_AE simp: indicator_def one_ereal_def)
hoelzl@43340
   276
      also have "\<dots> = \<nu> {x\<in>space M. D x \<noteq> 0}"
hoelzl@43340
   277
        using D(1) D_density by auto
hoelzl@43340
   278
      also have "\<dots> = \<nu> (space M)"
hoelzl@43340
   279
        using D_density D(1) by (auto intro!: positive_integral_cong simp: indicator_def)
hoelzl@43340
   280
      finally have "AE x. D x = 1"
hoelzl@43340
   281
        using D(1) \<nu>.measure_space_1 by (intro AE_I_eq_1) auto
hoelzl@43920
   282
      then have "(\<integral>\<^isup>+x. indicator A x\<partial>M) = (\<integral>\<^isup>+x. ereal (D x) * indicator A x\<partial>M)"
hoelzl@43920
   283
        by (intro positive_integral_cong_AE) (auto simp: one_ereal_def[symmetric])
hoelzl@43340
   284
      also have "\<dots> = \<nu> A"
hoelzl@43340
   285
        using `A \<in> sets M` D_density by simp
hoelzl@43340
   286
      finally show False using `A \<in> sets M` `\<nu> A \<noteq> \<mu> A` by simp
hoelzl@43340
   287
    qed
hoelzl@43340
   288
    show "{x\<in>space M. D x \<noteq> 1 \<and> D x \<noteq> 0} \<in> sets M"
hoelzl@43340
   289
      using D(1) by (auto intro: sets_Collect)
hoelzl@43340
   290
hoelzl@43340
   291
    show "AE t. t \<in> {x\<in>space M. D x \<noteq> 1 \<and> D x \<noteq> 0} \<longrightarrow>
hoelzl@43340
   292
      D t - indicator ?D_set t \<noteq> D t * (ln b * entropy_density b M \<nu> t)"
hoelzl@43340
   293
      using D(2)
hoelzl@43340
   294
    proof (elim AE_mp, safe intro!: AE_I2)
hoelzl@43340
   295
      fix t assume Dt: "t \<in> space M" "D t \<noteq> 1" "D t \<noteq> 0"
hoelzl@43920
   296
        and RN: "RN_deriv M \<nu> t = ereal (D t)"
hoelzl@43340
   297
        and eq: "D t - indicator ?D_set t = D t * (ln b * entropy_density b M \<nu> t)"
hoelzl@43340
   298
hoelzl@43340
   299
      have "D t - 1 = D t - indicator ?D_set t"
hoelzl@43340
   300
        using Dt by simp
hoelzl@43340
   301
      also note eq
hoelzl@43340
   302
      also have "D t * (ln b * entropy_density b M \<nu> t) = - D t * ln (1 / D t)"
hoelzl@43340
   303
        using RN b_gt_1 `D t \<noteq> 0` `0 \<le> D t`
hoelzl@43340
   304
        by (simp add: entropy_density_def log_def ln_div less_le)
hoelzl@43340
   305
      finally have "ln (1 / D t) = 1 / D t - 1"
hoelzl@43340
   306
        using `D t \<noteq> 0` by (auto simp: field_simps)
hoelzl@43340
   307
      from ln_eq_minus_one[OF _ this] `D t \<noteq> 0` `0 \<le> D t` `D t \<noteq> 1`
hoelzl@43340
   308
      show False by auto
hoelzl@43340
   309
    qed
hoelzl@43340
   310
hoelzl@43340
   311
    show "AE t. D t - indicator ?D_set t \<le> D t * (ln b * entropy_density b M \<nu> t)"
hoelzl@43340
   312
      using D(2)
hoelzl@43340
   313
    proof (elim AE_mp, intro AE_I2 impI)
hoelzl@43920
   314
      fix t assume "t \<in> space M" and RN: "RN_deriv M \<nu> t = ereal (D t)"
hoelzl@43340
   315
      show "D t - indicator ?D_set t \<le> D t * (ln b * entropy_density b M \<nu> t)"
hoelzl@43340
   316
      proof cases
hoelzl@43340
   317
        assume asm: "D t \<noteq> 0"
hoelzl@43340
   318
        then have "0 < D t" using `0 \<le> D t` by auto
hoelzl@43340
   319
        then have "0 < 1 / D t" by auto
hoelzl@43340
   320
        have "D t - indicator ?D_set t \<le> - D t * (1 / D t - 1)"
hoelzl@43340
   321
          using asm `t \<in> space M` by (simp add: field_simps)
hoelzl@43340
   322
        also have "- D t * (1 / D t - 1) \<le> - D t * ln (1 / D t)"
hoelzl@43340
   323
          using ln_le_minus_one `0 < 1 / D t` by (intro mult_left_mono_neg) auto
hoelzl@43340
   324
        also have "\<dots> = D t * (ln b * entropy_density b M \<nu> t)"
hoelzl@43340
   325
          using `0 < D t` RN b_gt_1
hoelzl@43340
   326
          by (simp_all add: log_def ln_div entropy_density_def)
hoelzl@43340
   327
        finally show ?thesis by simp
hoelzl@43340
   328
      qed simp
hoelzl@43340
   329
    qed
hoelzl@43340
   330
  qed
hoelzl@43340
   331
  also have "\<dots> = (\<integral> x. ln b * entropy_density b M \<nu> x \<partial>M\<lparr>measure := \<nu>\<rparr>)"
hoelzl@43340
   332
    using D D_density ln_entropy
hoelzl@43340
   333
    by (intro integral_translated_density[symmetric]) auto
hoelzl@43340
   334
  also have "\<dots> = ln b * (\<integral> x. entropy_density b M \<nu> x \<partial>M\<lparr>measure := \<nu>\<rparr>)"
hoelzl@43340
   335
    using int by (rule \<nu>.integral_cmult)
hoelzl@43340
   336
  finally show "0 < KL_divergence b M \<nu>"
hoelzl@43340
   337
    using b_gt_1 by (auto simp: KL_divergence_def zero_less_mult_iff)
hoelzl@43340
   338
qed
hoelzl@43340
   339
hoelzl@43340
   340
lemma (in sigma_finite_measure) KL_eq_0:
hoelzl@43340
   341
  assumes eq: "\<forall>A\<in>sets M. \<nu> A = measure M A"
hoelzl@43340
   342
  shows "KL_divergence b M \<nu> = 0"
hoelzl@43340
   343
proof -
hoelzl@43340
   344
  have "AE x. 1 = RN_deriv M \<nu> x"
hoelzl@43340
   345
  proof (rule RN_deriv_unique)
hoelzl@43340
   346
    show "measure_space (M\<lparr>measure := \<nu>\<rparr>)"
hoelzl@43340
   347
      using eq by (intro measure_space_cong) auto
hoelzl@43340
   348
    show "absolutely_continuous \<nu>"
hoelzl@43340
   349
      unfolding absolutely_continuous_def using eq by auto
hoelzl@43920
   350
    show "(\<lambda>x. 1) \<in> borel_measurable M" "AE x. 0 \<le> (1 :: ereal)" by auto
hoelzl@43340
   351
    fix A assume "A \<in> sets M"
hoelzl@43340
   352
    with eq show "\<nu> A = \<integral>\<^isup>+ x. 1 * indicator A x \<partial>M" by simp
hoelzl@43340
   353
  qed
hoelzl@43340
   354
  then have "AE x. log b (real (RN_deriv M \<nu> x)) = 0"
hoelzl@43340
   355
    by (elim AE_mp) simp
hoelzl@43340
   356
  from integral_cong_AE[OF this]
hoelzl@43340
   357
  have "integral\<^isup>L M (entropy_density b M \<nu>) = 0"
hoelzl@43340
   358
    by (simp add: entropy_density_def comp_def)
hoelzl@43340
   359
  with eq show "KL_divergence b M \<nu> = 0"
hoelzl@43340
   360
    unfolding KL_divergence_def
hoelzl@43340
   361
    by (subst integral_cong_measure) auto
hoelzl@43340
   362
qed
hoelzl@43340
   363
hoelzl@43340
   364
lemma (in information_space) KL_eq_0_imp:
hoelzl@43340
   365
  assumes ps: "prob_space (M\<lparr>measure := \<nu>\<rparr>)"
hoelzl@43340
   366
  assumes ac: "absolutely_continuous \<nu>"
hoelzl@43340
   367
  assumes int: "integrable (M\<lparr> measure := \<nu> \<rparr>) (entropy_density b M \<nu>)"
hoelzl@43340
   368
  assumes KL: "KL_divergence b M \<nu> = 0"
hoelzl@43340
   369
  shows "\<forall>A\<in>sets M. \<nu> A = \<mu> A"
hoelzl@43340
   370
  by (metis less_imp_neq KL_gt_0 assms)
hoelzl@43340
   371
hoelzl@43340
   372
lemma (in information_space) KL_ge_0:
hoelzl@43340
   373
  assumes ps: "prob_space (M\<lparr>measure := \<nu>\<rparr>)"
hoelzl@43340
   374
  assumes ac: "absolutely_continuous \<nu>"
hoelzl@43340
   375
  assumes int: "integrable (M\<lparr> measure := \<nu> \<rparr>) (entropy_density b M \<nu>)"
hoelzl@43340
   376
  shows "0 \<le> KL_divergence b M \<nu>"
hoelzl@43340
   377
  using KL_eq_0 KL_gt_0[OF ps ac int]
hoelzl@43340
   378
  by (cases "\<forall>A\<in>sets M. \<nu> A = measure M A") (auto simp: le_less)
hoelzl@43340
   379
hoelzl@38656
   380
hoelzl@41833
   381
lemma (in sigma_finite_measure) KL_divergence_vimage:
hoelzl@41833
   382
  assumes T: "T \<in> measure_preserving M M'"
hoelzl@41833
   383
    and T': "T' \<in> measure_preserving (M'\<lparr> measure := \<nu>' \<rparr>) (M\<lparr> measure := \<nu> \<rparr>)"
hoelzl@41833
   384
    and inv: "\<And>x. x \<in> space M \<Longrightarrow> T' (T x) = x"
hoelzl@41833
   385
    and inv': "\<And>x. x \<in> space M' \<Longrightarrow> T (T' x) = x"
hoelzl@41833
   386
  and \<nu>': "measure_space (M'\<lparr>measure := \<nu>'\<rparr>)" "measure_space.absolutely_continuous M' \<nu>'"
hoelzl@41833
   387
  and "1 < b"
hoelzl@41833
   388
  shows "KL_divergence b M' \<nu>' = KL_divergence b M \<nu>"
hoelzl@41833
   389
proof -
hoelzl@41833
   390
  interpret \<nu>': measure_space "M'\<lparr>measure := \<nu>'\<rparr>" by fact
hoelzl@41833
   391
  have M: "measure_space (M\<lparr> measure := \<nu>\<rparr>)"
hoelzl@41833
   392
    by (rule \<nu>'.measure_space_vimage[OF _ T'], simp) default
hoelzl@41833
   393
  have "sigma_algebra (M'\<lparr> measure := \<nu>'\<rparr>)" by default
hoelzl@41833
   394
  then have saM': "sigma_algebra M'" by simp
hoelzl@41833
   395
  then interpret M': measure_space M' by (rule measure_space_vimage) fact
hoelzl@41833
   396
  have ac: "absolutely_continuous \<nu>" unfolding absolutely_continuous_def
hoelzl@41833
   397
  proof safe
hoelzl@41833
   398
    fix N assume N: "N \<in> sets M" and N_0: "\<mu> N = 0"
hoelzl@41833
   399
    then have N': "T' -` N \<inter> space M' \<in> sets M'"
hoelzl@41833
   400
      using T' by (auto simp: measurable_def measure_preserving_def)
hoelzl@41833
   401
    have "T -` (T' -` N \<inter> space M') \<inter> space M = N"
hoelzl@41833
   402
      using inv T N sets_into_space[OF N] by (auto simp: measurable_def measure_preserving_def)
hoelzl@41833
   403
    then have "measure M' (T' -` N \<inter> space M') = 0"
hoelzl@41833
   404
      using measure_preservingD[OF T N'] N_0 by auto
hoelzl@41833
   405
    with \<nu>'(2) N' show "\<nu> N = 0" using measure_preservingD[OF T', of N] N
hoelzl@41833
   406
      unfolding M'.absolutely_continuous_def measurable_def by auto
hoelzl@41833
   407
  qed
hoelzl@41833
   408
hoelzl@41833
   409
  have sa: "sigma_algebra (M\<lparr>measure := \<nu>\<rparr>)" by simp default
hoelzl@41833
   410
  have AE: "AE x. RN_deriv M' \<nu>' (T x) = RN_deriv M \<nu> x"
hoelzl@41833
   411
    by (rule RN_deriv_vimage[OF T T' inv \<nu>'])
hoelzl@41833
   412
  show ?thesis
hoelzl@43340
   413
    unfolding KL_divergence_def entropy_density_def comp_def
hoelzl@41833
   414
  proof (subst \<nu>'.integral_vimage[OF sa T'])
hoelzl@41833
   415
    show "(\<lambda>x. log b (real (RN_deriv M \<nu> x))) \<in> borel_measurable (M\<lparr>measure := \<nu>\<rparr>)"
hoelzl@41833
   416
      by (auto intro!: RN_deriv[OF M ac] borel_measurable_log[OF _ `1 < b`])
hoelzl@41833
   417
    have "(\<integral> x. log b (real (RN_deriv M' \<nu>' x)) \<partial>M'\<lparr>measure := \<nu>'\<rparr>) =
hoelzl@41833
   418
      (\<integral> x. log b (real (RN_deriv M' \<nu>' (T (T' x)))) \<partial>M'\<lparr>measure := \<nu>'\<rparr>)" (is "?l = _")
hoelzl@41833
   419
      using inv' by (auto intro!: \<nu>'.integral_cong)
hoelzl@41833
   420
    also have "\<dots> = (\<integral> x. log b (real (RN_deriv M \<nu> (T' x))) \<partial>M'\<lparr>measure := \<nu>'\<rparr>)" (is "_ = ?r")
hoelzl@41833
   421
      using M ac AE
hoelzl@41833
   422
      by (intro \<nu>'.integral_cong_AE \<nu>'.almost_everywhere_vimage[OF sa T'] absolutely_continuous_AE[OF M])
hoelzl@41833
   423
         (auto elim!: AE_mp)
hoelzl@41833
   424
    finally show "?l = ?r" .
hoelzl@41833
   425
  qed
hoelzl@41833
   426
qed
hoelzl@41833
   427
hoelzl@40859
   428
lemma (in sigma_finite_measure) KL_divergence_cong:
hoelzl@41689
   429
  assumes "measure_space (M\<lparr>measure := \<nu>\<rparr>)" (is "measure_space ?\<nu>")
hoelzl@41689
   430
  assumes [simp]: "sets N = sets M" "space N = space M"
hoelzl@41689
   431
    "\<And>A. A \<in> sets M \<Longrightarrow> measure N A = \<mu> A"
hoelzl@41689
   432
    "\<And>A. A \<in> sets M \<Longrightarrow> \<nu> A = \<nu>' A"
hoelzl@41689
   433
  shows "KL_divergence b M \<nu> = KL_divergence b N \<nu>'"
hoelzl@40859
   434
proof -
hoelzl@41689
   435
  interpret \<nu>: measure_space ?\<nu> by fact
hoelzl@41689
   436
  have "KL_divergence b M \<nu> = \<integral>x. log b (real (RN_deriv N \<nu>' x)) \<partial>?\<nu>"
hoelzl@43340
   437
    by (simp cong: RN_deriv_cong \<nu>.integral_cong add: KL_divergence_def entropy_density_def)
hoelzl@41689
   438
  also have "\<dots> = KL_divergence b N \<nu>'"
hoelzl@43340
   439
    by (auto intro!: \<nu>.integral_cong_measure[symmetric] simp: KL_divergence_def entropy_density_def comp_def)
hoelzl@41689
   440
  finally show ?thesis .
hoelzl@40859
   441
qed
hoelzl@40859
   442
hoelzl@38656
   443
lemma (in finite_measure_space) KL_divergence_eq_finite:
hoelzl@41689
   444
  assumes v: "finite_measure_space (M\<lparr>measure := \<nu>\<rparr>)"
hoelzl@40859
   445
  assumes ac: "absolutely_continuous \<nu>"
hoelzl@41689
   446
  shows "KL_divergence b M \<nu> = (\<Sum>x\<in>space M. real (\<nu> {x}) * log b (real (\<nu> {x}) / real (\<mu> {x})))" (is "_ = ?sum")
hoelzl@43340
   447
proof (simp add: KL_divergence_def finite_measure_space.integral_finite_singleton[OF v] entropy_density_def)
hoelzl@41689
   448
  interpret v: finite_measure_space "M\<lparr>measure := \<nu>\<rparr>" by fact
hoelzl@41689
   449
  have ms: "measure_space (M\<lparr>measure := \<nu>\<rparr>)" by default
hoelzl@41689
   450
  show "(\<Sum>x \<in> space M. log b (real (RN_deriv M \<nu> x)) * real (\<nu> {x})) = ?sum"
hoelzl@38656
   451
    using RN_deriv_finite_measure[OF ms ac]
hoelzl@41981
   452
    by (auto intro!: setsum_cong simp: field_simps)
hoelzl@38656
   453
qed
hoelzl@36080
   454
hoelzl@38656
   455
lemma (in finite_prob_space) KL_divergence_positive_finite:
hoelzl@41689
   456
  assumes v: "finite_prob_space (M\<lparr>measure := \<nu>\<rparr>)"
hoelzl@40859
   457
  assumes ac: "absolutely_continuous \<nu>"
hoelzl@38656
   458
  and "1 < b"
hoelzl@41689
   459
  shows "0 \<le> KL_divergence b M \<nu>"
hoelzl@38656
   460
proof -
hoelzl@43340
   461
  interpret information_space M by default fact
hoelzl@41689
   462
  interpret v: finite_prob_space "M\<lparr>measure := \<nu>\<rparr>" by fact
hoelzl@45777
   463
  have ps: "prob_space (M\<lparr>measure := \<nu>\<rparr>)" by unfold_locales
hoelzl@43340
   464
  from KL_ge_0[OF this ac v.integral_finite_singleton(1)] show ?thesis .
hoelzl@36080
   465
qed
hoelzl@36080
   466
hoelzl@39097
   467
subsection {* Mutual Information *}
hoelzl@39097
   468
hoelzl@36080
   469
definition (in prob_space)
hoelzl@38656
   470
  "mutual_information b S T X Y =
hoelzl@43920
   471
    KL_divergence b (S\<lparr>measure := ereal\<circ>distribution X\<rparr> \<Otimes>\<^isub>M T\<lparr>measure := ereal\<circ>distribution Y\<rparr>)
hoelzl@43920
   472
      (ereal\<circ>joint_distribution X Y)"
hoelzl@36080
   473
hoelzl@43340
   474
lemma (in information_space)
hoelzl@43340
   475
  fixes S T X Y
hoelzl@43920
   476
  defines "P \<equiv> S\<lparr>measure := ereal\<circ>distribution X\<rparr> \<Otimes>\<^isub>M T\<lparr>measure := ereal\<circ>distribution Y\<rparr>"
hoelzl@43340
   477
  shows "indep_var S X T Y \<longleftrightarrow>
hoelzl@43340
   478
    (random_variable S X \<and> random_variable T Y \<and>
hoelzl@43920
   479
      measure_space.absolutely_continuous P (ereal\<circ>joint_distribution X Y) \<and>
hoelzl@43920
   480
      integrable (P\<lparr>measure := (ereal\<circ>joint_distribution X Y)\<rparr>)
hoelzl@43920
   481
        (entropy_density b P (ereal\<circ>joint_distribution X Y)) \<and>
hoelzl@43340
   482
     mutual_information b S T X Y = 0)"
hoelzl@43340
   483
proof safe
hoelzl@43340
   484
  assume indep: "indep_var S X T Y"
hoelzl@43340
   485
  then have "random_variable S X" "random_variable T Y"
hoelzl@43340
   486
    by (blast dest: indep_var_rv1 indep_var_rv2)+
hoelzl@43340
   487
  then show "sigma_algebra S" "X \<in> measurable M S" "sigma_algebra T" "Y \<in> measurable M T"
hoelzl@43340
   488
    by blast+
hoelzl@43340
   489
hoelzl@43920
   490
  interpret X: prob_space "S\<lparr>measure := ereal\<circ>distribution X\<rparr>"
hoelzl@43340
   491
    by (rule distribution_prob_space) fact
hoelzl@43920
   492
  interpret Y: prob_space "T\<lparr>measure := ereal\<circ>distribution Y\<rparr>"
hoelzl@43340
   493
    by (rule distribution_prob_space) fact
hoelzl@43920
   494
  interpret XY: pair_prob_space "S\<lparr>measure := ereal\<circ>distribution X\<rparr>" "T\<lparr>measure := ereal\<circ>distribution Y\<rparr>" by default
hoelzl@43340
   495
  interpret XY: information_space XY.P b by default (rule b_gt_1)
hoelzl@43340
   496
hoelzl@43920
   497
  let ?J = "XY.P\<lparr> measure := (ereal\<circ>joint_distribution X Y) \<rparr>"
hoelzl@43340
   498
  { fix A assume "A \<in> sets XY.P"
hoelzl@43920
   499
    then have "ereal (joint_distribution X Y A) = XY.\<mu> A"
hoelzl@43340
   500
      using indep_var_distributionD[OF indep]
hoelzl@43340
   501
      by (simp add: XY.P.finite_measure_eq) }
hoelzl@43340
   502
  note j_eq = this
hoelzl@43340
   503
hoelzl@43340
   504
  interpret J: prob_space ?J
hoelzl@43340
   505
    using j_eq by (intro XY.prob_space_cong) auto
hoelzl@43340
   506
hoelzl@43920
   507
  have ac: "XY.absolutely_continuous (ereal\<circ>joint_distribution X Y)"
hoelzl@43340
   508
    by (simp add: XY.absolutely_continuous_def j_eq)
hoelzl@43920
   509
  then show "measure_space.absolutely_continuous P (ereal\<circ>joint_distribution X Y)"
hoelzl@43340
   510
    unfolding P_def .
hoelzl@43340
   511
hoelzl@43920
   512
  have ed: "entropy_density b XY.P (ereal\<circ>joint_distribution X Y) \<in> borel_measurable XY.P"
hoelzl@43340
   513
    by (rule XY.measurable_entropy_density) (default | fact)+
hoelzl@43340
   514
hoelzl@43920
   515
  have "AE x in XY.P. 1 = RN_deriv XY.P (ereal\<circ>joint_distribution X Y) x"
hoelzl@43340
   516
  proof (rule XY.RN_deriv_unique[OF _ ac])
hoelzl@43340
   517
    show "measure_space ?J" by default
hoelzl@43340
   518
    fix A assume "A \<in> sets XY.P"
hoelzl@43920
   519
    then show "(ereal\<circ>joint_distribution X Y) A = (\<integral>\<^isup>+ x. 1 * indicator A x \<partial>XY.P)"
hoelzl@43340
   520
      by (simp add: j_eq)
hoelzl@43340
   521
  qed (insert XY.measurable_const[of 1 borel], auto)
hoelzl@43920
   522
  then have ae_XY: "AE x in XY.P. entropy_density b XY.P (ereal\<circ>joint_distribution X Y) x = 0"
hoelzl@43340
   523
    by (elim XY.AE_mp) (simp add: entropy_density_def)
hoelzl@43920
   524
  have ae_J: "AE x in ?J. entropy_density b XY.P (ereal\<circ>joint_distribution X Y) x = 0"
hoelzl@43340
   525
  proof (rule XY.absolutely_continuous_AE)
hoelzl@43340
   526
    show "measure_space ?J" by default
hoelzl@43340
   527
    show "XY.absolutely_continuous (measure ?J)"
hoelzl@43340
   528
      using ac by simp
hoelzl@43340
   529
  qed (insert ae_XY, simp_all)
hoelzl@43920
   530
  then show "integrable (P\<lparr>measure := (ereal\<circ>joint_distribution X Y)\<rparr>)
hoelzl@43920
   531
        (entropy_density b P (ereal\<circ>joint_distribution X Y))"
hoelzl@43340
   532
    unfolding P_def
hoelzl@43340
   533
    using ed XY.measurable_const[of 0 borel]
hoelzl@43340
   534
    by (subst J.integrable_cong_AE) auto
hoelzl@43340
   535
hoelzl@43340
   536
  show "mutual_information b S T X Y = 0"
hoelzl@43340
   537
    unfolding mutual_information_def KL_divergence_def P_def
hoelzl@43340
   538
    by (subst J.integral_cong_AE[OF ae_J]) simp
hoelzl@43340
   539
next
hoelzl@43340
   540
  assume "sigma_algebra S" "X \<in> measurable M S" "sigma_algebra T" "Y \<in> measurable M T"
hoelzl@43340
   541
  then have rvs: "random_variable S X" "random_variable T Y" by blast+
hoelzl@43340
   542
hoelzl@43920
   543
  interpret X: prob_space "S\<lparr>measure := ereal\<circ>distribution X\<rparr>"
hoelzl@43340
   544
    by (rule distribution_prob_space) fact
hoelzl@43920
   545
  interpret Y: prob_space "T\<lparr>measure := ereal\<circ>distribution Y\<rparr>"
hoelzl@43340
   546
    by (rule distribution_prob_space) fact
hoelzl@43920
   547
  interpret XY: pair_prob_space "S\<lparr>measure := ereal\<circ>distribution X\<rparr>" "T\<lparr>measure := ereal\<circ>distribution Y\<rparr>" by default
hoelzl@43340
   548
  interpret XY: information_space XY.P b by default (rule b_gt_1)
hoelzl@43340
   549
hoelzl@43920
   550
  let ?J = "XY.P\<lparr> measure := (ereal\<circ>joint_distribution X Y) \<rparr>"
hoelzl@43340
   551
  interpret J: prob_space ?J
hoelzl@43340
   552
    using rvs by (intro joint_distribution_prob_space) auto
hoelzl@43340
   553
hoelzl@43920
   554
  assume ac: "measure_space.absolutely_continuous P (ereal\<circ>joint_distribution X Y)"
hoelzl@43920
   555
  assume int: "integrable (P\<lparr>measure := (ereal\<circ>joint_distribution X Y)\<rparr>)
hoelzl@43920
   556
        (entropy_density b P (ereal\<circ>joint_distribution X Y))"
hoelzl@43340
   557
  assume I_eq_0: "mutual_information b S T X Y = 0"
hoelzl@43340
   558
hoelzl@43920
   559
  have eq: "\<forall>A\<in>sets XY.P. (ereal \<circ> joint_distribution X Y) A = XY.\<mu> A"
hoelzl@43340
   560
  proof (rule XY.KL_eq_0_imp)
hoelzl@45777
   561
    show "prob_space ?J" by unfold_locales
hoelzl@43920
   562
    show "XY.absolutely_continuous (ereal\<circ>joint_distribution X Y)"
hoelzl@43340
   563
      using ac by (simp add: P_def)
hoelzl@43920
   564
    show "integrable ?J (entropy_density b XY.P (ereal\<circ>joint_distribution X Y))"
hoelzl@43340
   565
      using int by (simp add: P_def)
hoelzl@43920
   566
    show "KL_divergence b XY.P (ereal\<circ>joint_distribution X Y) = 0"
hoelzl@43340
   567
      using I_eq_0 unfolding mutual_information_def by (simp add: P_def)
hoelzl@43340
   568
  qed
hoelzl@43340
   569
hoelzl@43340
   570
  { fix S X assume "sigma_algebra S"
hoelzl@43340
   571
    interpret S: sigma_algebra S by fact
hoelzl@43340
   572
    have "Int_stable \<lparr>space = space M, sets = {X -` A \<inter> space M |A. A \<in> sets S}\<rparr>"
hoelzl@43340
   573
    proof (safe intro!: Int_stableI)
hoelzl@43340
   574
      fix A B assume "A \<in> sets S" "B \<in> sets S"
hoelzl@43340
   575
      then show "\<exists>C. (X -` A \<inter> space M) \<inter> (X -` B \<inter> space M) = (X -` C \<inter> space M) \<and> C \<in> sets S"
hoelzl@43340
   576
        by (intro exI[of _ "A \<inter> B"]) auto
hoelzl@43340
   577
    qed }
hoelzl@43340
   578
  note Int_stable = this
hoelzl@43340
   579
hoelzl@43340
   580
  show "indep_var S X T Y" unfolding indep_var_eq
hoelzl@43340
   581
  proof (intro conjI indep_set_sigma_sets Int_stable)
hoelzl@43340
   582
    show "indep_set {X -` A \<inter> space M |A. A \<in> sets S} {Y -` A \<inter> space M |A. A \<in> sets T}"
hoelzl@43340
   583
    proof (safe intro!: indep_setI)
hoelzl@43340
   584
      { fix A assume "A \<in> sets S" then show "X -` A \<inter> space M \<in> sets M"
hoelzl@43340
   585
        using `X \<in> measurable M S` by (auto intro: measurable_sets) }
hoelzl@43340
   586
      { fix A assume "A \<in> sets T" then show "Y -` A \<inter> space M \<in> sets M"
hoelzl@43340
   587
        using `Y \<in> measurable M T` by (auto intro: measurable_sets) }
hoelzl@43340
   588
    next
hoelzl@43340
   589
      fix A B assume ab: "A \<in> sets S" "B \<in> sets T"
hoelzl@43920
   590
      have "ereal (prob ((X -` A \<inter> space M) \<inter> (Y -` B \<inter> space M))) =
hoelzl@43920
   591
        ereal (joint_distribution X Y (A \<times> B))"
hoelzl@43340
   592
        unfolding distribution_def
hoelzl@43920
   593
        by (intro arg_cong[where f="\<lambda>C. ereal (prob C)"]) auto
hoelzl@43340
   594
      also have "\<dots> = XY.\<mu> (A \<times> B)"
hoelzl@43340
   595
        using ab eq by (auto simp: XY.finite_measure_eq)
hoelzl@43920
   596
      also have "\<dots> = ereal (distribution X A) * ereal (distribution Y B)"
hoelzl@43340
   597
        using ab by (simp add: XY.pair_measure_times)
hoelzl@43340
   598
      finally show "prob ((X -` A \<inter> space M) \<inter> (Y -` B \<inter> space M)) =
hoelzl@43340
   599
        prob (X -` A \<inter> space M) * prob (Y -` B \<inter> space M)"
hoelzl@43340
   600
        unfolding distribution_def by simp
hoelzl@43340
   601
    qed
hoelzl@43340
   602
  qed fact+
hoelzl@43340
   603
qed
hoelzl@43340
   604
hoelzl@43340
   605
lemma (in information_space) mutual_information_commute_generic:
hoelzl@43340
   606
  assumes X: "random_variable S X" and Y: "random_variable T Y"
hoelzl@43340
   607
  assumes ac: "measure_space.absolutely_continuous
hoelzl@43920
   608
    (S\<lparr>measure := ereal\<circ>distribution X\<rparr> \<Otimes>\<^isub>M T\<lparr>measure := ereal\<circ>distribution Y\<rparr>) (ereal\<circ>joint_distribution X Y)"
hoelzl@43340
   609
  shows "mutual_information b S T X Y = mutual_information b T S Y X"
hoelzl@43340
   610
proof -
hoelzl@43920
   611
  let ?S = "S\<lparr>measure := ereal\<circ>distribution X\<rparr>" and ?T = "T\<lparr>measure := ereal\<circ>distribution Y\<rparr>"
hoelzl@43340
   612
  interpret S: prob_space ?S using X by (rule distribution_prob_space)
hoelzl@43340
   613
  interpret T: prob_space ?T using Y by (rule distribution_prob_space)
hoelzl@43340
   614
  interpret P: pair_prob_space ?S ?T ..
hoelzl@43340
   615
  interpret Q: pair_prob_space ?T ?S ..
hoelzl@43340
   616
  show ?thesis
hoelzl@43340
   617
    unfolding mutual_information_def
hoelzl@43340
   618
  proof (intro Q.KL_divergence_vimage[OF Q.measure_preserving_swap _ _ _ _ ac b_gt_1])
hoelzl@43340
   619
    show "(\<lambda>(x,y). (y,x)) \<in> measure_preserving
hoelzl@43920
   620
      (P.P \<lparr> measure := ereal\<circ>joint_distribution X Y\<rparr>) (Q.P \<lparr> measure := ereal\<circ>joint_distribution Y X\<rparr>)"
hoelzl@43340
   621
      using X Y unfolding measurable_def
hoelzl@43340
   622
      unfolding measure_preserving_def using P.pair_sigma_algebra_swap_measurable
hoelzl@43340
   623
      by (auto simp add: space_pair_measure distribution_def intro!: arg_cong[where f=\<mu>'])
hoelzl@43920
   624
    have "prob_space (P.P\<lparr> measure := ereal\<circ>joint_distribution X Y\<rparr>)"
hoelzl@43340
   625
      using X Y by (auto intro!: distribution_prob_space random_variable_pairI)
hoelzl@43920
   626
    then show "measure_space (P.P\<lparr> measure := ereal\<circ>joint_distribution X Y\<rparr>)"
hoelzl@45777
   627
      unfolding prob_space_def finite_measure_def sigma_finite_measure_def by simp
hoelzl@43340
   628
  qed auto
hoelzl@43340
   629
qed
hoelzl@43340
   630
hoelzl@40859
   631
definition (in prob_space)
hoelzl@40859
   632
  "entropy b s X = mutual_information b s s X X"
hoelzl@40859
   633
hoelzl@40859
   634
abbreviation (in information_space)
hoelzl@40859
   635
  mutual_information_Pow ("\<I>'(_ ; _')") where
hoelzl@36624
   636
  "\<I>(X ; Y) \<equiv> mutual_information b
hoelzl@43920
   637
    \<lparr> space = X`space M, sets = Pow (X`space M), measure = ereal\<circ>distribution X \<rparr>
hoelzl@43920
   638
    \<lparr> space = Y`space M, sets = Pow (Y`space M), measure = ereal\<circ>distribution Y \<rparr> X Y"
hoelzl@41689
   639
hoelzl@40859
   640
lemma (in prob_space) finite_variables_absolutely_continuous:
hoelzl@40859
   641
  assumes X: "finite_random_variable S X" and Y: "finite_random_variable T Y"
hoelzl@41689
   642
  shows "measure_space.absolutely_continuous
hoelzl@43920
   643
    (S\<lparr>measure := ereal\<circ>distribution X\<rparr> \<Otimes>\<^isub>M T\<lparr>measure := ereal\<circ>distribution Y\<rparr>)
hoelzl@43920
   644
    (ereal\<circ>joint_distribution X Y)"
hoelzl@40859
   645
proof -
hoelzl@43920
   646
  interpret X: finite_prob_space "S\<lparr>measure := ereal\<circ>distribution X\<rparr>"
hoelzl@41689
   647
    using X by (rule distribution_finite_prob_space)
hoelzl@43920
   648
  interpret Y: finite_prob_space "T\<lparr>measure := ereal\<circ>distribution Y\<rparr>"
hoelzl@41689
   649
    using Y by (rule distribution_finite_prob_space)
hoelzl@41689
   650
  interpret XY: pair_finite_prob_space
hoelzl@43920
   651
    "S\<lparr>measure := ereal\<circ>distribution X\<rparr>" "T\<lparr> measure := ereal\<circ>distribution Y\<rparr>" by default
hoelzl@43920
   652
  interpret P: finite_prob_space "XY.P\<lparr> measure := ereal\<circ>joint_distribution X Y\<rparr>"
hoelzl@41689
   653
    using assms by (auto intro!: joint_distribution_finite_prob_space)
hoelzl@41689
   654
  note rv = assms[THEN finite_random_variableD]
hoelzl@43920
   655
  show "XY.absolutely_continuous (ereal\<circ>joint_distribution X Y)"
hoelzl@40859
   656
  proof (rule XY.absolutely_continuousI)
hoelzl@45777
   657
    show "finite_measure_space (XY.P\<lparr> measure := ereal\<circ>joint_distribution X Y\<rparr>)" by unfold_locales
hoelzl@41689
   658
    fix x assume "x \<in> space XY.P" and "XY.\<mu> {x} = 0"
hoelzl@41981
   659
    then obtain a b where "x = (a, b)"
hoelzl@41981
   660
      and "distribution X {a} = 0 \<or> distribution Y {b} = 0"
hoelzl@41689
   661
      by (cases x) (auto simp: space_pair_measure)
hoelzl@41981
   662
    with finite_distribution_order(5,6)[OF X Y]
hoelzl@43920
   663
    show "(ereal \<circ> joint_distribution X Y) {x} = 0" by auto
hoelzl@40859
   664
  qed
hoelzl@40859
   665
qed
hoelzl@40859
   666
hoelzl@40859
   667
lemma (in information_space)
hoelzl@40859
   668
  assumes MX: "finite_random_variable MX X"
hoelzl@40859
   669
  assumes MY: "finite_random_variable MY Y"
hoelzl@40859
   670
  shows mutual_information_generic_eq:
hoelzl@36624
   671
    "mutual_information b MX MY X Y = (\<Sum> (x,y) \<in> space MX \<times> space MY.
hoelzl@41981
   672
      joint_distribution X Y {(x,y)} *
hoelzl@41981
   673
      log b (joint_distribution X Y {(x,y)} /
hoelzl@41981
   674
      (distribution X {x} * distribution Y {y})))"
hoelzl@40859
   675
    (is ?sum)
hoelzl@36624
   676
  and mutual_information_positive_generic:
hoelzl@40859
   677
     "0 \<le> mutual_information b MX MY X Y" (is ?positive)
hoelzl@36624
   678
proof -
hoelzl@43920
   679
  interpret X: finite_prob_space "MX\<lparr>measure := ereal\<circ>distribution X\<rparr>"
hoelzl@41689
   680
    using MX by (rule distribution_finite_prob_space)
hoelzl@43920
   681
  interpret Y: finite_prob_space "MY\<lparr>measure := ereal\<circ>distribution Y\<rparr>"
hoelzl@41689
   682
    using MY by (rule distribution_finite_prob_space)
hoelzl@43920
   683
  interpret XY: pair_finite_prob_space "MX\<lparr>measure := ereal\<circ>distribution X\<rparr>" "MY\<lparr>measure := ereal\<circ>distribution Y\<rparr>" by default
hoelzl@43920
   684
  interpret P: finite_prob_space "XY.P\<lparr>measure := ereal\<circ>joint_distribution X Y\<rparr>"
hoelzl@41689
   685
    using assms by (auto intro!: joint_distribution_finite_prob_space)
hoelzl@36080
   686
hoelzl@45777
   687
  have P_ms: "finite_measure_space (XY.P\<lparr>measure := ereal\<circ>joint_distribution X Y\<rparr>)" by unfold_locales
hoelzl@45777
   688
  have P_ps: "finite_prob_space (XY.P\<lparr>measure := ereal\<circ>joint_distribution X Y\<rparr>)" by unfold_locales
hoelzl@36624
   689
hoelzl@40859
   690
  show ?sum
hoelzl@38656
   691
    unfolding Let_def mutual_information_def
hoelzl@40859
   692
    by (subst XY.KL_divergence_eq_finite[OF P_ms finite_variables_absolutely_continuous[OF MX MY]])
hoelzl@41981
   693
       (auto simp add: space_pair_measure setsum_cartesian_product')
hoelzl@36080
   694
hoelzl@36624
   695
  show ?positive
hoelzl@40859
   696
    using XY.KL_divergence_positive_finite[OF P_ps finite_variables_absolutely_continuous[OF MX MY] b_gt_1]
hoelzl@40859
   697
    unfolding mutual_information_def .
hoelzl@36080
   698
qed
hoelzl@36080
   699
hoelzl@41661
   700
lemma (in information_space) mutual_information_commute:
hoelzl@41661
   701
  assumes X: "finite_random_variable S X" and Y: "finite_random_variable T Y"
hoelzl@41661
   702
  shows "mutual_information b S T X Y = mutual_information b T S Y X"
hoelzl@41661
   703
  unfolding mutual_information_generic_eq[OF X Y] mutual_information_generic_eq[OF Y X]
hoelzl@41661
   704
  unfolding joint_distribution_commute_singleton[of X Y]
hoelzl@41661
   705
  by (auto simp add: ac_simps intro!: setsum_reindex_cong[OF swap_inj_on])
hoelzl@41661
   706
hoelzl@41661
   707
lemma (in information_space) mutual_information_commute_simple:
hoelzl@41689
   708
  assumes X: "simple_function M X" and Y: "simple_function M Y"
hoelzl@41661
   709
  shows "\<I>(X;Y) = \<I>(Y;X)"
hoelzl@41833
   710
  by (intro mutual_information_commute X Y simple_function_imp_finite_random_variable)
hoelzl@41661
   711
hoelzl@40859
   712
lemma (in information_space) mutual_information_eq:
hoelzl@41689
   713
  assumes "simple_function M X" "simple_function M Y"
hoelzl@40859
   714
  shows "\<I>(X;Y) = (\<Sum> (x,y) \<in> X ` space M \<times> Y ` space M.
hoelzl@41981
   715
    distribution (\<lambda>x. (X x, Y x)) {(x,y)} * log b (distribution (\<lambda>x. (X x, Y x)) {(x,y)} /
hoelzl@41981
   716
                                                   (distribution X {x} * distribution Y {y})))"
hoelzl@40859
   717
  using assms by (simp add: mutual_information_generic_eq)
hoelzl@36080
   718
hoelzl@40859
   719
lemma (in information_space) mutual_information_generic_cong:
hoelzl@39097
   720
  assumes X: "\<And>x. x \<in> space M \<Longrightarrow> X x = X' x"
hoelzl@39097
   721
  assumes Y: "\<And>x. x \<in> space M \<Longrightarrow> Y x = Y' x"
hoelzl@40859
   722
  shows "mutual_information b MX MY X Y = mutual_information b MX MY X' Y'"
hoelzl@40859
   723
  unfolding mutual_information_def using X Y
hoelzl@40859
   724
  by (simp cong: distribution_cong)
hoelzl@39097
   725
hoelzl@40859
   726
lemma (in information_space) mutual_information_cong:
hoelzl@40859
   727
  assumes X: "\<And>x. x \<in> space M \<Longrightarrow> X x = X' x"
hoelzl@40859
   728
  assumes Y: "\<And>x. x \<in> space M \<Longrightarrow> Y x = Y' x"
hoelzl@40859
   729
  shows "\<I>(X; Y) = \<I>(X'; Y')"
hoelzl@40859
   730
  unfolding mutual_information_def using X Y
hoelzl@40859
   731
  by (simp cong: distribution_cong image_cong)
hoelzl@40859
   732
hoelzl@40859
   733
lemma (in information_space) mutual_information_positive:
hoelzl@41689
   734
  assumes "simple_function M X" "simple_function M Y"
hoelzl@40859
   735
  shows "0 \<le> \<I>(X;Y)"
hoelzl@40859
   736
  using assms by (simp add: mutual_information_positive_generic)
hoelzl@36080
   737
hoelzl@39097
   738
subsection {* Entropy *}
hoelzl@39097
   739
hoelzl@40859
   740
abbreviation (in information_space)
hoelzl@40859
   741
  entropy_Pow ("\<H>'(_')") where
hoelzl@43920
   742
  "\<H>(X) \<equiv> entropy b \<lparr> space = X`space M, sets = Pow (X`space M), measure = ereal\<circ>distribution X \<rparr> X"
hoelzl@36080
   743
hoelzl@40859
   744
lemma (in information_space) entropy_generic_eq:
hoelzl@41981
   745
  fixes X :: "'a \<Rightarrow> 'c"
hoelzl@40859
   746
  assumes MX: "finite_random_variable MX X"
hoelzl@41981
   747
  shows "entropy b MX X = -(\<Sum> x \<in> space MX. distribution X {x} * log b (distribution X {x}))"
hoelzl@39097
   748
proof -
hoelzl@43920
   749
  interpret MX: finite_prob_space "MX\<lparr>measure := ereal\<circ>distribution X\<rparr>"
hoelzl@41689
   750
    using MX by (rule distribution_finite_prob_space)
wenzelm@46731
   751
  let ?X = "\<lambda>x. distribution X {x}"
wenzelm@46731
   752
  let ?XX = "\<lambda>x y. joint_distribution X X {(x, y)}"
hoelzl@41981
   753
hoelzl@41981
   754
  { fix x y :: 'c
hoelzl@41981
   755
    { assume "x \<noteq> y"
hoelzl@41981
   756
      then have "(\<lambda>x. (X x, X x)) -` {(x,y)} \<inter> space M = {}" by auto
hoelzl@41981
   757
      then have "joint_distribution X X {(x, y)} = 0" by (simp add: distribution_def) }
hoelzl@39097
   758
    then have "?XX x y * log b (?XX x y / (?X x * ?X y)) =
hoelzl@39097
   759
        (if x = y then - ?X y * log b (?X y) else 0)"
hoelzl@41981
   760
      by (auto simp: log_simps zero_less_mult_iff) }
hoelzl@39097
   761
  note remove_XX = this
hoelzl@41981
   762
hoelzl@39097
   763
  show ?thesis
hoelzl@39097
   764
    unfolding entropy_def mutual_information_generic_eq[OF MX MX]
hoelzl@39097
   765
    unfolding setsum_cartesian_product[symmetric] setsum_negf[symmetric] remove_XX
hoelzl@41689
   766
    using MX.finite_space by (auto simp: setsum_cases)
hoelzl@39097
   767
qed
hoelzl@36624
   768
hoelzl@40859
   769
lemma (in information_space) entropy_eq:
hoelzl@41689
   770
  assumes "simple_function M X"
hoelzl@41981
   771
  shows "\<H>(X) = -(\<Sum> x \<in> X ` space M. distribution X {x} * log b (distribution X {x}))"
hoelzl@40859
   772
  using assms by (simp add: entropy_generic_eq)
hoelzl@36080
   773
hoelzl@40859
   774
lemma (in information_space) entropy_positive:
hoelzl@41689
   775
  "simple_function M X \<Longrightarrow> 0 \<le> \<H>(X)"
hoelzl@40859
   776
  unfolding entropy_def by (simp add: mutual_information_positive)
hoelzl@36080
   777
hoelzl@40859
   778
lemma (in information_space) entropy_certainty_eq_0:
hoelzl@41981
   779
  assumes X: "simple_function M X" and "x \<in> X ` space M" and "distribution X {x} = 1"
hoelzl@39097
   780
  shows "\<H>(X) = 0"
hoelzl@39097
   781
proof -
hoelzl@43920
   782
  let ?X = "\<lparr> space = X ` space M, sets = Pow (X ` space M), measure = ereal\<circ>distribution X\<rparr>"
hoelzl@41689
   783
  note simple_function_imp_finite_random_variable[OF `simple_function M X`]
hoelzl@43920
   784
  from distribution_finite_prob_space[OF this, of "\<lparr> measure = ereal\<circ>distribution X \<rparr>"]
hoelzl@41689
   785
  interpret X: finite_prob_space ?X by simp
hoelzl@39097
   786
  have "distribution X (X ` space M - {x}) = distribution X (X ` space M) - distribution X {x}"
hoelzl@39097
   787
    using X.measure_compl[of "{x}"] assms by auto
hoelzl@39097
   788
  also have "\<dots> = 0" using X.prob_space assms by auto
hoelzl@39097
   789
  finally have X0: "distribution X (X ` space M - {x}) = 0" by auto
hoelzl@41981
   790
  { fix y assume *: "y \<in> X ` space M"
hoelzl@41981
   791
    { assume asm: "y \<noteq> x"
hoelzl@41981
   792
      with * have "{y} \<subseteq> X ` space M - {x}" by auto
hoelzl@41981
   793
      from X.measure_mono[OF this] X0 asm *
hoelzl@41981
   794
      have "distribution X {y} = 0"  by (auto intro: antisym) }
hoelzl@41981
   795
    then have "distribution X {y} = (if x = y then 1 else 0)"
hoelzl@41981
   796
      using assms by auto }
hoelzl@41981
   797
  note fi = this
hoelzl@39097
   798
  have y: "\<And>y. (if x = y then 1 else 0) * log b (if x = y then 1 else 0) = 0" by simp
hoelzl@41689
   799
  show ?thesis unfolding entropy_eq[OF `simple_function M X`] by (auto simp: y fi)
hoelzl@39097
   800
qed
hoelzl@39097
   801
hoelzl@40859
   802
lemma (in information_space) entropy_le_card_not_0:
hoelzl@41981
   803
  assumes X: "simple_function M X"
hoelzl@41981
   804
  shows "\<H>(X) \<le> log b (card (X ` space M \<inter> {x. distribution X {x} \<noteq> 0}))"
hoelzl@39097
   805
proof -
wenzelm@46731
   806
  let ?p = "\<lambda>x. distribution X {x}"
hoelzl@39097
   807
  have "\<H>(X) = (\<Sum>x\<in>X`space M. ?p x * log b (1 / ?p x))"
hoelzl@41981
   808
    unfolding entropy_eq[OF X] setsum_negf[symmetric]
hoelzl@41981
   809
    by (auto intro!: setsum_cong simp: log_simps)
hoelzl@39097
   810
  also have "\<dots> \<le> log b (\<Sum>x\<in>X`space M. ?p x * (1 / ?p x))"
hoelzl@41981
   811
    using not_empty b_gt_1 `simple_function M X` sum_over_space_real_distribution[OF X]
hoelzl@41981
   812
    by (intro log_setsum') (auto simp: simple_function_def)
hoelzl@41981
   813
  also have "\<dots> = log b (\<Sum>x\<in>X`space M. if ?p x \<noteq> 0 then 1 else 0)"
hoelzl@41981
   814
    by (intro arg_cong[where f="\<lambda>X. log b X"] setsum_cong) auto
hoelzl@39097
   815
  finally show ?thesis
hoelzl@41689
   816
    using `simple_function M X` by (auto simp: setsum_cases real_eq_of_nat simple_function_def)
hoelzl@39097
   817
qed
hoelzl@39097
   818
hoelzl@41981
   819
lemma (in prob_space) measure'_translate:
hoelzl@41981
   820
  assumes X: "random_variable S X" and A: "A \<in> sets S"
hoelzl@43920
   821
  shows "finite_measure.\<mu>' (S\<lparr> measure := ereal\<circ>distribution X \<rparr>) A = distribution X A"
hoelzl@41981
   822
proof -
hoelzl@43920
   823
  interpret S: prob_space "S\<lparr> measure := ereal\<circ>distribution X \<rparr>"
hoelzl@41981
   824
    using distribution_prob_space[OF X] .
hoelzl@41981
   825
  from A show "S.\<mu>' A = distribution X A"
hoelzl@41981
   826
    unfolding S.\<mu>'_def by (simp add: distribution_def_raw \<mu>'_def)
hoelzl@41981
   827
qed
hoelzl@41981
   828
hoelzl@40859
   829
lemma (in information_space) entropy_uniform_max:
hoelzl@41981
   830
  assumes X: "simple_function M X"
hoelzl@39097
   831
  assumes "\<And>x y. \<lbrakk> x \<in> X ` space M ; y \<in> X ` space M \<rbrakk> \<Longrightarrow> distribution X {x} = distribution X {y}"
hoelzl@39097
   832
  shows "\<H>(X) = log b (real (card (X ` space M)))"
hoelzl@39097
   833
proof -
hoelzl@43920
   834
  let ?X = "\<lparr> space = X ` space M, sets = Pow (X ` space M), measure = undefined\<rparr>\<lparr> measure := ereal\<circ>distribution X\<rparr>"
hoelzl@41981
   835
  note frv = simple_function_imp_finite_random_variable[OF X]
hoelzl@43920
   836
  from distribution_finite_prob_space[OF this, of "\<lparr> measure = ereal\<circ>distribution X \<rparr>"]
hoelzl@41689
   837
  interpret X: finite_prob_space ?X by simp
hoelzl@41981
   838
  note rv = finite_random_variableD[OF frv]
hoelzl@39097
   839
  have card_gt0: "0 < card (X ` space M)" unfolding card_gt_0_iff
hoelzl@41689
   840
    using `simple_function M X` not_empty by (auto simp: simple_function_def)
hoelzl@41981
   841
  { fix x assume "x \<in> space ?X"
hoelzl@41981
   842
    moreover then have "X.\<mu>' {x} = 1 / card (space ?X)"
hoelzl@41981
   843
    proof (rule X.uniform_prob)
hoelzl@41981
   844
      fix x y assume "x \<in> space ?X" "y \<in> space ?X"
hoelzl@41981
   845
      with assms(2)[of x y] show "X.\<mu>' {x} = X.\<mu>' {y}"
hoelzl@41981
   846
        by (subst (1 2) measure'_translate[OF rv]) auto
hoelzl@41981
   847
    qed
hoelzl@41981
   848
    ultimately have "distribution X {x} = 1 / card (space ?X)"
hoelzl@41981
   849
      by (subst (asm) measure'_translate[OF rv]) auto }
hoelzl@39097
   850
  thus ?thesis
hoelzl@40859
   851
    using not_empty X.finite_space b_gt_1 card_gt0
hoelzl@41689
   852
    by (simp add: entropy_eq[OF `simple_function M X`] real_eq_of_nat[symmetric] log_simps)
hoelzl@39097
   853
qed
hoelzl@39097
   854
hoelzl@40859
   855
lemma (in information_space) entropy_le_card:
hoelzl@41689
   856
  assumes "simple_function M X"
hoelzl@40859
   857
  shows "\<H>(X) \<le> log b (real (card (X ` space M)))"
hoelzl@39097
   858
proof cases
hoelzl@39097
   859
  assume "X ` space M \<inter> {x. distribution X {x} \<noteq> 0} = {}"
hoelzl@39097
   860
  then have "\<And>x. x\<in>X`space M \<Longrightarrow> distribution X {x} = 0" by auto
hoelzl@39097
   861
  moreover
hoelzl@39097
   862
  have "0 < card (X`space M)"
hoelzl@41689
   863
    using `simple_function M X` not_empty
hoelzl@40859
   864
    by (auto simp: card_gt_0_iff simple_function_def)
hoelzl@39097
   865
  then have "log b 1 \<le> log b (real (card (X`space M)))"
hoelzl@39097
   866
    using b_gt_1 by (intro log_le) auto
hoelzl@40859
   867
  ultimately show ?thesis using assms by (simp add: entropy_eq)
hoelzl@39097
   868
next
hoelzl@39097
   869
  assume False: "X ` space M \<inter> {x. distribution X {x} \<noteq> 0} \<noteq> {}"
hoelzl@39097
   870
  have "card (X ` space M \<inter> {x. distribution X {x} \<noteq> 0}) \<le> card (X ` space M)"
hoelzl@40859
   871
    (is "?A \<le> ?B") using assms not_empty by (auto intro!: card_mono simp: simple_function_def)
hoelzl@40859
   872
  note entropy_le_card_not_0[OF assms]
hoelzl@39097
   873
  also have "log b (real ?A) \<le> log b (real ?B)"
hoelzl@40859
   874
    using b_gt_1 False not_empty `?A \<le> ?B` assms
hoelzl@40859
   875
    by (auto intro!: log_le simp: card_gt_0_iff simp: simple_function_def)
hoelzl@39097
   876
  finally show ?thesis .
hoelzl@39097
   877
qed
hoelzl@39097
   878
hoelzl@40859
   879
lemma (in information_space) entropy_commute:
hoelzl@41689
   880
  assumes "simple_function M X" "simple_function M Y"
hoelzl@40859
   881
  shows "\<H>(\<lambda>x. (X x, Y x)) = \<H>(\<lambda>x. (Y x, X x))"
hoelzl@39097
   882
proof -
hoelzl@41689
   883
  have sf: "simple_function M (\<lambda>x. (X x, Y x))" "simple_function M (\<lambda>x. (Y x, X x))"
hoelzl@40859
   884
    using assms by (auto intro: simple_function_Pair)
hoelzl@39097
   885
  have *: "(\<lambda>x. (Y x, X x))`space M = (\<lambda>(a,b). (b,a))`(\<lambda>x. (X x, Y x))`space M"
hoelzl@39097
   886
    by auto
hoelzl@39097
   887
  have inj: "\<And>X. inj_on (\<lambda>(a,b). (b,a)) X"
hoelzl@39097
   888
    by (auto intro!: inj_onI)
hoelzl@39097
   889
  show ?thesis
hoelzl@40859
   890
    unfolding sf[THEN entropy_eq] unfolding * setsum_reindex[OF inj]
hoelzl@39097
   891
    by (simp add: joint_distribution_commute[of Y X] split_beta)
hoelzl@39097
   892
qed
hoelzl@39097
   893
hoelzl@40859
   894
lemma (in information_space) entropy_eq_cartesian_product:
hoelzl@41689
   895
  assumes "simple_function M X" "simple_function M Y"
hoelzl@40859
   896
  shows "\<H>(\<lambda>x. (X x, Y x)) = -(\<Sum>x\<in>X`space M. \<Sum>y\<in>Y`space M.
hoelzl@41981
   897
    joint_distribution X Y {(x,y)} * log b (joint_distribution X Y {(x,y)}))"
hoelzl@39097
   898
proof -
hoelzl@41689
   899
  have sf: "simple_function M (\<lambda>x. (X x, Y x))"
hoelzl@40859
   900
    using assms by (auto intro: simple_function_Pair)
hoelzl@39097
   901
  { fix x assume "x\<notin>(\<lambda>x. (X x, Y x))`space M"
hoelzl@39097
   902
    then have "(\<lambda>x. (X x, Y x)) -` {x} \<inter> space M = {}" by auto
hoelzl@39097
   903
    then have "joint_distribution X Y {x} = 0"
hoelzl@39097
   904
      unfolding distribution_def by auto }
hoelzl@40859
   905
  then show ?thesis using sf assms
hoelzl@40859
   906
    unfolding entropy_eq[OF sf] neg_equal_iff_equal setsum_cartesian_product
hoelzl@40859
   907
    by (auto intro!: setsum_mono_zero_cong_left simp: simple_function_def)
hoelzl@39097
   908
qed
hoelzl@39097
   909
hoelzl@39097
   910
subsection {* Conditional Mutual Information *}
hoelzl@39097
   911
hoelzl@36080
   912
definition (in prob_space)
hoelzl@41689
   913
  "conditional_mutual_information b MX MY MZ X Y Z \<equiv>
hoelzl@41689
   914
    mutual_information b MX (MY \<Otimes>\<^isub>M MZ) X (\<lambda>x. (Y x, Z x)) -
hoelzl@41689
   915
    mutual_information b MX MZ X Z"
hoelzl@36080
   916
hoelzl@40859
   917
abbreviation (in information_space)
hoelzl@40859
   918
  conditional_mutual_information_Pow ("\<I>'( _ ; _ | _ ')") where
hoelzl@36624
   919
  "\<I>(X ; Y | Z) \<equiv> conditional_mutual_information b
hoelzl@43920
   920
    \<lparr> space = X`space M, sets = Pow (X`space M), measure = ereal\<circ>distribution X \<rparr>
hoelzl@43920
   921
    \<lparr> space = Y`space M, sets = Pow (Y`space M), measure = ereal\<circ>distribution Y \<rparr>
hoelzl@43920
   922
    \<lparr> space = Z`space M, sets = Pow (Z`space M), measure = ereal\<circ>distribution Z \<rparr>
hoelzl@36080
   923
    X Y Z"
hoelzl@36080
   924
hoelzl@40859
   925
lemma (in information_space) conditional_mutual_information_generic_eq:
hoelzl@40859
   926
  assumes MX: "finite_random_variable MX X"
hoelzl@40859
   927
    and MY: "finite_random_variable MY Y"
hoelzl@40859
   928
    and MZ: "finite_random_variable MZ Z"
hoelzl@40859
   929
  shows "conditional_mutual_information b MX MY MZ X Y Z = (\<Sum>(x, y, z) \<in> space MX \<times> space MY \<times> space MZ.
hoelzl@41981
   930
             distribution (\<lambda>x. (X x, Y x, Z x)) {(x, y, z)} *
hoelzl@41981
   931
             log b (distribution (\<lambda>x. (X x, Y x, Z x)) {(x, y, z)} /
hoelzl@41981
   932
    (joint_distribution X Z {(x, z)} * (joint_distribution Y Z {(y,z)} / distribution Z {z}))))"
hoelzl@41981
   933
  (is "_ = (\<Sum>(x, y, z)\<in>?S. ?XYZ x y z * log b (?XYZ x y z / (?XZ x z * (?YZ y z / ?Z z))))")
hoelzl@40859
   934
proof -
hoelzl@41981
   935
  let ?X = "\<lambda>x. distribution X {x}"
hoelzl@40859
   936
  note finite_var = MX MY MZ
hoelzl@40859
   937
  note YZ = finite_random_variable_pairI[OF finite_var(2,3)]
hoelzl@41981
   938
  note XYZ = finite_random_variable_pairI[OF MX YZ]
hoelzl@40859
   939
  note XZ = finite_random_variable_pairI[OF finite_var(1,3)]
hoelzl@40859
   940
  note ZX = finite_random_variable_pairI[OF finite_var(3,1)]
hoelzl@40859
   941
  note YZX = finite_random_variable_pairI[OF finite_var(2) ZX]
hoelzl@40859
   942
  note order1 =
hoelzl@41981
   943
    finite_distribution_order(5,6)[OF finite_var(1) YZ]
hoelzl@41981
   944
    finite_distribution_order(5,6)[OF finite_var(1,3)]
hoelzl@40859
   945
hoelzl@41981
   946
  note random_var = finite_var[THEN finite_random_variableD]
hoelzl@40859
   947
  note finite = finite_var(1) YZ finite_var(3) XZ YZX
hoelzl@40859
   948
hoelzl@40859
   949
  have order2: "\<And>x y z. \<lbrakk>x \<in> space MX; y \<in> space MY; z \<in> space MZ; joint_distribution X Z {(x, z)} = 0\<rbrakk>
hoelzl@40859
   950
          \<Longrightarrow> joint_distribution X (\<lambda>x. (Y x, Z x)) {(x, y, z)} = 0"
hoelzl@40859
   951
    unfolding joint_distribution_commute_singleton[of X]
hoelzl@40859
   952
    unfolding joint_distribution_assoc_singleton[symmetric]
hoelzl@40859
   953
    using finite_distribution_order(6)[OF finite_var(2) ZX]
hoelzl@41981
   954
    by auto
hoelzl@36624
   955
hoelzl@41981
   956
  have "(\<Sum>(x, y, z)\<in>?S. ?XYZ x y z * log b (?XYZ x y z / (?XZ x z * (?YZ y z / ?Z z)))) =
hoelzl@40859
   957
    (\<Sum>(x, y, z)\<in>?S. ?XYZ x y z * (log b (?XYZ x y z / (?X x * ?YZ y z)) - log b (?XZ x z / (?X x * ?Z z))))"
hoelzl@40859
   958
    (is "(\<Sum>(x, y, z)\<in>?S. ?L x y z) = (\<Sum>(x, y, z)\<in>?S. ?R x y z)")
hoelzl@40859
   959
  proof (safe intro!: setsum_cong)
hoelzl@40859
   960
    fix x y z assume space: "x \<in> space MX" "y \<in> space MY" "z \<in> space MZ"
hoelzl@40859
   961
    show "?L x y z = ?R x y z"
hoelzl@40859
   962
    proof cases
hoelzl@40859
   963
      assume "?XYZ x y z \<noteq> 0"
hoelzl@41981
   964
      with space have "0 < ?X x" "0 < ?Z z" "0 < ?XZ x z" "0 < ?YZ y z" "0 < ?XYZ x y z"
hoelzl@41981
   965
        using order1 order2 by (auto simp: less_le)
hoelzl@41981
   966
      with b_gt_1 show ?thesis
hoelzl@41981
   967
        by (simp add: log_mult log_divide zero_less_mult_iff zero_less_divide_iff)
hoelzl@40859
   968
    qed simp
hoelzl@40859
   969
  qed
hoelzl@40859
   970
  also have "\<dots> = (\<Sum>(x, y, z)\<in>?S. ?XYZ x y z * log b (?XYZ x y z / (?X x * ?YZ y z))) -
hoelzl@40859
   971
                  (\<Sum>(x, y, z)\<in>?S. ?XYZ x y z * log b (?XZ x z / (?X x * ?Z z)))"
hoelzl@40859
   972
    by (auto simp add: setsum_subtractf[symmetric] field_simps intro!: setsum_cong)
hoelzl@40859
   973
  also have "(\<Sum>(x, y, z)\<in>?S. ?XYZ x y z * log b (?XZ x z / (?X x * ?Z z))) =
hoelzl@40859
   974
             (\<Sum>(x, z)\<in>space MX \<times> space MZ. ?XZ x z * log b (?XZ x z / (?X x * ?Z z)))"
hoelzl@40859
   975
    unfolding setsum_cartesian_product[symmetric] setsum_commute[of _ _ "space MY"]
hoelzl@40859
   976
              setsum_left_distrib[symmetric]
hoelzl@40859
   977
    unfolding joint_distribution_commute_singleton[of X]
hoelzl@40859
   978
    unfolding joint_distribution_assoc_singleton[symmetric]
hoelzl@41981
   979
    using setsum_joint_distribution_singleton[OF finite_var(2) ZX]
hoelzl@41981
   980
    by (intro setsum_cong refl) (simp add: space_pair_measure)
hoelzl@40859
   981
  also have "(\<Sum>(x, y, z)\<in>?S. ?XYZ x y z * log b (?XYZ x y z / (?X x * ?YZ y z))) -
hoelzl@40859
   982
             (\<Sum>(x, z)\<in>space MX \<times> space MZ. ?XZ x z * log b (?XZ x z / (?X x * ?Z z))) =
hoelzl@40859
   983
             conditional_mutual_information b MX MY MZ X Y Z"
hoelzl@40859
   984
    unfolding conditional_mutual_information_def
hoelzl@40859
   985
    unfolding mutual_information_generic_eq[OF finite_var(1,3)]
hoelzl@40859
   986
    unfolding mutual_information_generic_eq[OF finite_var(1) YZ]
hoelzl@41689
   987
    by (simp add: space_sigma space_pair_measure setsum_cartesian_product')
hoelzl@40859
   988
  finally show ?thesis by simp
hoelzl@40859
   989
qed
hoelzl@40859
   990
hoelzl@40859
   991
lemma (in information_space) conditional_mutual_information_eq:
hoelzl@41689
   992
  assumes "simple_function M X" "simple_function M Y" "simple_function M Z"
hoelzl@40859
   993
  shows "\<I>(X;Y|Z) = (\<Sum>(x, y, z) \<in> X`space M \<times> Y`space M \<times> Z`space M.
hoelzl@41981
   994
             distribution (\<lambda>x. (X x, Y x, Z x)) {(x, y, z)} *
hoelzl@41981
   995
             log b (distribution (\<lambda>x. (X x, Y x, Z x)) {(x, y, z)} /
hoelzl@41981
   996
    (joint_distribution X Z {(x, z)} * joint_distribution Y Z {(y,z)} / distribution Z {z})))"
hoelzl@41981
   997
  by (subst conditional_mutual_information_generic_eq[OF assms[THEN simple_function_imp_finite_random_variable]])
hoelzl@41981
   998
     simp
hoelzl@40859
   999
hoelzl@40859
  1000
lemma (in information_space) conditional_mutual_information_eq_mutual_information:
hoelzl@41689
  1001
  assumes X: "simple_function M X" and Y: "simple_function M Y"
hoelzl@40859
  1002
  shows "\<I>(X ; Y) = \<I>(X ; Y | (\<lambda>x. ()))"
hoelzl@36624
  1003
proof -
hoelzl@36624
  1004
  have [simp]: "(\<lambda>x. ()) ` space M = {()}" using not_empty by auto
hoelzl@41689
  1005
  have C: "simple_function M (\<lambda>x. ())" by auto
hoelzl@36624
  1006
  show ?thesis
hoelzl@40859
  1007
    unfolding conditional_mutual_information_eq[OF X Y C]
hoelzl@40859
  1008
    unfolding mutual_information_eq[OF X Y]
hoelzl@36624
  1009
    by (simp add: setsum_cartesian_product' distribution_remove_const)
hoelzl@36624
  1010
qed
hoelzl@36624
  1011
hoelzl@40859
  1012
lemma (in information_space) conditional_mutual_information_generic_positive:
hoelzl@41981
  1013
  assumes X: "finite_random_variable MX X" and Y: "finite_random_variable MY Y" and Z: "finite_random_variable MZ Z"
hoelzl@40859
  1014
  shows "0 \<le> conditional_mutual_information b MX MY MZ X Y Z"
hoelzl@40859
  1015
proof (cases "space MX \<times> space MY \<times> space MZ = {}")
hoelzl@40859
  1016
  case True show ?thesis
hoelzl@40859
  1017
    unfolding conditional_mutual_information_generic_eq[OF assms] True
hoelzl@40859
  1018
    by simp
hoelzl@40859
  1019
next
hoelzl@40859
  1020
  case False
hoelzl@41981
  1021
  let ?dXYZ = "distribution (\<lambda>x. (X x, Y x, Z x))"
hoelzl@41981
  1022
  let ?dXZ = "joint_distribution X Z"
hoelzl@41981
  1023
  let ?dYZ = "joint_distribution Y Z"
hoelzl@41981
  1024
  let ?dX = "distribution X"
hoelzl@41981
  1025
  let ?dZ = "distribution Z"
hoelzl@40859
  1026
  let ?M = "space MX \<times> space MY \<times> space MZ"
hoelzl@36624
  1027
hoelzl@41981
  1028
  note YZ = finite_random_variable_pairI[OF Y Z]
hoelzl@41981
  1029
  note XZ = finite_random_variable_pairI[OF X Z]
hoelzl@41981
  1030
  note ZX = finite_random_variable_pairI[OF Z X]
hoelzl@41981
  1031
  note YZ = finite_random_variable_pairI[OF Y Z]
hoelzl@41981
  1032
  note XYZ = finite_random_variable_pairI[OF X YZ]
hoelzl@41981
  1033
  note finite = Z YZ XZ XYZ
hoelzl@40859
  1034
  have order: "\<And>x y z. \<lbrakk>x \<in> space MX; y \<in> space MY; z \<in> space MZ; joint_distribution X Z {(x, z)} = 0\<rbrakk>
hoelzl@40859
  1035
          \<Longrightarrow> joint_distribution X (\<lambda>x. (Y x, Z x)) {(x, y, z)} = 0"
hoelzl@40859
  1036
    unfolding joint_distribution_commute_singleton[of X]
hoelzl@40859
  1037
    unfolding joint_distribution_assoc_singleton[symmetric]
hoelzl@41981
  1038
    using finite_distribution_order(6)[OF Y ZX]
hoelzl@41981
  1039
    by auto
hoelzl@40859
  1040
hoelzl@40859
  1041
  note order = order
hoelzl@41981
  1042
    finite_distribution_order(5,6)[OF X YZ]
hoelzl@41981
  1043
    finite_distribution_order(5,6)[OF Y Z]
hoelzl@40859
  1044
hoelzl@40859
  1045
  have "- conditional_mutual_information b MX MY MZ X Y Z = - (\<Sum>(x, y, z) \<in> ?M. ?dXYZ {(x, y, z)} *
hoelzl@40859
  1046
    log b (?dXYZ {(x, y, z)} / (?dXZ {(x, z)} * ?dYZ {(y,z)} / ?dZ {z})))"
hoelzl@41981
  1047
    unfolding conditional_mutual_information_generic_eq[OF assms] neg_equal_iff_equal by auto
hoelzl@40859
  1048
  also have "\<dots> \<le> log b (\<Sum>(x, y, z) \<in> ?M. ?dXZ {(x, z)} * ?dYZ {(y,z)} / ?dZ {z})"
hoelzl@41981
  1049
    unfolding split_beta'
hoelzl@36624
  1050
  proof (rule log_setsum_divide)
hoelzl@40859
  1051
    show "?M \<noteq> {}" using False by simp
hoelzl@36624
  1052
    show "1 < b" using b_gt_1 .
hoelzl@36080
  1053
hoelzl@40859
  1054
    show "finite ?M" using assms
hoelzl@40859
  1055
      unfolding finite_sigma_algebra_def finite_sigma_algebra_axioms_def by auto
hoelzl@40859
  1056
hoelzl@40859
  1057
    show "(\<Sum>x\<in>?M. ?dXYZ {(fst x, fst (snd x), snd (snd x))}) = 1"
hoelzl@40859
  1058
      unfolding setsum_cartesian_product'
hoelzl@40859
  1059
      unfolding setsum_commute[of _ "space MY"]
hoelzl@40859
  1060
      unfolding setsum_commute[of _ "space MZ"]
hoelzl@41689
  1061
      by (simp_all add: space_pair_measure
hoelzl@41981
  1062
                        setsum_joint_distribution_singleton[OF X YZ]
hoelzl@41981
  1063
                        setsum_joint_distribution_singleton[OF Y Z]
hoelzl@41981
  1064
                        setsum_distribution[OF Z])
hoelzl@40859
  1065
hoelzl@36624
  1066
    fix x assume "x \<in> ?M"
hoelzl@38656
  1067
    let ?x = "(fst x, fst (snd x), snd (snd x))"
hoelzl@38656
  1068
hoelzl@41981
  1069
    show "0 \<le> ?dXYZ {?x}"
hoelzl@41981
  1070
      "0 \<le> ?dXZ {(fst x, snd (snd x))} * ?dYZ {(fst (snd x), snd (snd x))} / ?dZ {snd (snd x)}"
hoelzl@41981
  1071
     by (simp_all add: mult_nonneg_nonneg divide_nonneg_nonneg)
hoelzl@36080
  1072
hoelzl@38656
  1073
    assume *: "0 < ?dXYZ {?x}"
hoelzl@41981
  1074
    with `x \<in> ?M` finite order show "0 < ?dXZ {(fst x, snd (snd x))} * ?dYZ {(fst (snd x), snd (snd x))} / ?dZ {snd (snd x)}"
hoelzl@41981
  1075
      by (cases x) (auto simp add: zero_le_mult_iff zero_le_divide_iff less_le)
hoelzl@40859
  1076
  qed
hoelzl@40859
  1077
  also have "(\<Sum>(x, y, z) \<in> ?M. ?dXZ {(x, z)} * ?dYZ {(y,z)} / ?dZ {z}) = (\<Sum>z\<in>space MZ. ?dZ {z})"
hoelzl@36624
  1078
    apply (simp add: setsum_cartesian_product')
hoelzl@36624
  1079
    apply (subst setsum_commute)
hoelzl@36624
  1080
    apply (subst (2) setsum_commute)
hoelzl@40859
  1081
    by (auto simp: setsum_divide_distrib[symmetric] setsum_product[symmetric]
hoelzl@41981
  1082
                   setsum_joint_distribution_singleton[OF X Z]
hoelzl@41981
  1083
                   setsum_joint_distribution_singleton[OF Y Z]
hoelzl@36624
  1084
          intro!: setsum_cong)
hoelzl@40859
  1085
  also have "log b (\<Sum>z\<in>space MZ. ?dZ {z}) = 0"
hoelzl@45710
  1086
    unfolding setsum_distribution[OF Z] by simp
hoelzl@40859
  1087
  finally show ?thesis by simp
hoelzl@36080
  1088
qed
hoelzl@36080
  1089
hoelzl@40859
  1090
lemma (in information_space) conditional_mutual_information_positive:
hoelzl@41689
  1091
  assumes "simple_function M X" and "simple_function M Y" and "simple_function M Z"
hoelzl@40859
  1092
  shows "0 \<le> \<I>(X;Y|Z)"
hoelzl@41689
  1093
  by (rule conditional_mutual_information_generic_positive[OF assms[THEN simple_function_imp_finite_random_variable]])
hoelzl@40859
  1094
hoelzl@39097
  1095
subsection {* Conditional Entropy *}
hoelzl@39097
  1096
hoelzl@36080
  1097
definition (in prob_space)
hoelzl@36080
  1098
  "conditional_entropy b S T X Y = conditional_mutual_information b S S T X X Y"
hoelzl@36080
  1099
hoelzl@40859
  1100
abbreviation (in information_space)
hoelzl@40859
  1101
  conditional_entropy_Pow ("\<H>'(_ | _')") where
hoelzl@36624
  1102
  "\<H>(X | Y) \<equiv> conditional_entropy b
hoelzl@43920
  1103
    \<lparr> space = X`space M, sets = Pow (X`space M), measure = ereal\<circ>distribution X \<rparr>
hoelzl@43920
  1104
    \<lparr> space = Y`space M, sets = Pow (Y`space M), measure = ereal\<circ>distribution Y \<rparr> X Y"
hoelzl@36080
  1105
hoelzl@40859
  1106
lemma (in information_space) conditional_entropy_positive:
hoelzl@41689
  1107
  "simple_function M X \<Longrightarrow> simple_function M Y \<Longrightarrow> 0 \<le> \<H>(X | Y)"
hoelzl@40859
  1108
  unfolding conditional_entropy_def by (auto intro!: conditional_mutual_information_positive)
hoelzl@36080
  1109
hoelzl@40859
  1110
lemma (in information_space) conditional_entropy_generic_eq:
hoelzl@41689
  1111
  fixes MX :: "('c, 'd) measure_space_scheme" and MY :: "('e, 'f) measure_space_scheme"
hoelzl@40859
  1112
  assumes MX: "finite_random_variable MX X"
hoelzl@40859
  1113
  assumes MZ: "finite_random_variable MZ Z"
hoelzl@39097
  1114
  shows "conditional_entropy b MX MZ X Z =
hoelzl@39097
  1115
     - (\<Sum>(x, z)\<in>space MX \<times> space MZ.
hoelzl@41981
  1116
         joint_distribution X Z {(x, z)} * log b (joint_distribution X Z {(x, z)} / distribution Z {z}))"
hoelzl@40859
  1117
proof -
hoelzl@40859
  1118
  interpret MX: finite_sigma_algebra MX using MX by simp
hoelzl@40859
  1119
  interpret MZ: finite_sigma_algebra MZ using MZ by simp
wenzelm@46731
  1120
  let ?XXZ = "\<lambda>x y z. joint_distribution X (\<lambda>x. (X x, Z x)) {(x, y, z)}"
wenzelm@46731
  1121
  let ?XZ = "\<lambda>x z. joint_distribution X Z {(x, z)}"
wenzelm@46731
  1122
  let ?Z = "\<lambda>z. distribution Z {z}"
wenzelm@46731
  1123
  let ?f = "\<lambda>x y z. log b (?XXZ x y z * ?Z z / (?XZ x z * ?XZ y z))"
hoelzl@40859
  1124
  { fix x z have "?XXZ x x z = ?XZ x z"
hoelzl@41981
  1125
      unfolding distribution_def by (auto intro!: arg_cong[where f=\<mu>']) }
hoelzl@40859
  1126
  note this[simp]
hoelzl@41689
  1127
  { fix x x' :: 'c and z assume "x' \<noteq> x"
hoelzl@40859
  1128
    then have "?XXZ x x' z = 0"
hoelzl@41981
  1129
      by (auto simp: distribution_def empty_measure'[symmetric]
hoelzl@41981
  1130
               simp del: empty_measure' intro!: arg_cong[where f=\<mu>']) }
hoelzl@40859
  1131
  note this[simp]
hoelzl@40859
  1132
  { fix x x' z assume *: "x \<in> space MX" "z \<in> space MZ"
hoelzl@41981
  1133
    then have "(\<Sum>x'\<in>space MX. ?XXZ x x' z * ?f x x' z)
hoelzl@41981
  1134
      = (\<Sum>x'\<in>space MX. if x = x' then ?XZ x z * ?f x x z else 0)"
hoelzl@40859
  1135
      by (auto intro!: setsum_cong)
hoelzl@41981
  1136
    also have "\<dots> = ?XZ x z * ?f x x z"
hoelzl@40859
  1137
      using `x \<in> space MX` by (simp add: setsum_cases[OF MX.finite_space])
hoelzl@41981
  1138
    also have "\<dots> = ?XZ x z * log b (?Z z / ?XZ x z)" by auto
hoelzl@41981
  1139
    also have "\<dots> = - ?XZ x z * log b (?XZ x z / ?Z z)"
hoelzl@40859
  1140
      using finite_distribution_order(6)[OF MX MZ]
hoelzl@41981
  1141
      by (auto simp: log_simps field_simps zero_less_mult_iff)
hoelzl@41981
  1142
    finally have "(\<Sum>x'\<in>space MX. ?XXZ x x' z * ?f x x' z) = - ?XZ x z * log b (?XZ x z / ?Z z)" . }
hoelzl@40859
  1143
  note * = this
hoelzl@40859
  1144
  show ?thesis
hoelzl@40859
  1145
    unfolding conditional_entropy_def
hoelzl@40859
  1146
    unfolding conditional_mutual_information_generic_eq[OF MX MX MZ]
hoelzl@40859
  1147
    by (auto simp: setsum_cartesian_product' setsum_negf[symmetric]
hoelzl@41981
  1148
                   setsum_commute[of _ "space MZ"] *
hoelzl@40859
  1149
             intro!: setsum_cong)
hoelzl@39097
  1150
qed
hoelzl@39097
  1151
hoelzl@40859
  1152
lemma (in information_space) conditional_entropy_eq:
hoelzl@41689
  1153
  assumes "simple_function M X" "simple_function M Z"
hoelzl@40859
  1154
  shows "\<H>(X | Z) =
hoelzl@36080
  1155
     - (\<Sum>(x, z)\<in>X ` space M \<times> Z ` space M.
hoelzl@41981
  1156
         joint_distribution X Z {(x, z)} *
hoelzl@41981
  1157
         log b (joint_distribution X Z {(x, z)} / distribution Z {z}))"
hoelzl@41981
  1158
  by (subst conditional_entropy_generic_eq[OF assms[THEN simple_function_imp_finite_random_variable]])
hoelzl@41981
  1159
     simp
hoelzl@39097
  1160
hoelzl@40859
  1161
lemma (in information_space) conditional_entropy_eq_ce_with_hypothesis:
hoelzl@41689
  1162
  assumes X: "simple_function M X" and Y: "simple_function M Y"
hoelzl@40859
  1163
  shows "\<H>(X | Y) =
hoelzl@41981
  1164
    -(\<Sum>y\<in>Y`space M. distribution Y {y} *
hoelzl@41981
  1165
      (\<Sum>x\<in>X`space M. joint_distribution X Y {(x,y)} / distribution Y {(y)} *
hoelzl@41981
  1166
              log b (joint_distribution X Y {(x,y)} / distribution Y {(y)})))"
hoelzl@40859
  1167
  unfolding conditional_entropy_eq[OF assms]
hoelzl@40859
  1168
  using finite_distribution_order(5,6)[OF assms[THEN simple_function_imp_finite_random_variable]]
hoelzl@41981
  1169
  by (auto simp: setsum_cartesian_product'  setsum_commute[of _ "Y`space M"] setsum_right_distrib
hoelzl@40859
  1170
           intro!: setsum_cong)
hoelzl@39097
  1171
hoelzl@40859
  1172
lemma (in information_space) conditional_entropy_eq_cartesian_product:
hoelzl@41689
  1173
  assumes "simple_function M X" "simple_function M Y"
hoelzl@40859
  1174
  shows "\<H>(X | Y) = -(\<Sum>x\<in>X`space M. \<Sum>y\<in>Y`space M.
hoelzl@41981
  1175
    joint_distribution X Y {(x,y)} *
hoelzl@41981
  1176
    log b (joint_distribution X Y {(x,y)} / distribution Y {y}))"
hoelzl@40859
  1177
  unfolding conditional_entropy_eq[OF assms]
hoelzl@40859
  1178
  by (auto intro!: setsum_cong simp: setsum_cartesian_product')
hoelzl@36080
  1179
hoelzl@39097
  1180
subsection {* Equalities *}
hoelzl@39097
  1181
hoelzl@40859
  1182
lemma (in information_space) mutual_information_eq_entropy_conditional_entropy:
hoelzl@41689
  1183
  assumes X: "simple_function M X" and Z: "simple_function M Z"
hoelzl@40859
  1184
  shows  "\<I>(X ; Z) = \<H>(X) - \<H>(X | Z)"
hoelzl@40859
  1185
proof -
wenzelm@46731
  1186
  let ?XZ = "\<lambda>x z. joint_distribution X Z {(x, z)}"
wenzelm@46731
  1187
  let ?Z = "\<lambda>z. distribution Z {z}"
wenzelm@46731
  1188
  let ?X = "\<lambda>x. distribution X {x}"
hoelzl@40859
  1189
  note fX = X[THEN simple_function_imp_finite_random_variable]
hoelzl@40859
  1190
  note fZ = Z[THEN simple_function_imp_finite_random_variable]
hoelzl@40859
  1191
  note finite_distribution_order[OF fX fZ, simp]
hoelzl@40859
  1192
  { fix x z assume "x \<in> X`space M" "z \<in> Z`space M"
hoelzl@40859
  1193
    have "?XZ x z * log b (?XZ x z / (?X x * ?Z z)) =
hoelzl@40859
  1194
          ?XZ x z * log b (?XZ x z / ?Z z) - ?XZ x z * log b (?X x)"
hoelzl@41981
  1195
      by (auto simp: log_simps zero_le_mult_iff field_simps less_le) }
hoelzl@40859
  1196
  note * = this
hoelzl@40859
  1197
  show ?thesis
hoelzl@40859
  1198
    unfolding entropy_eq[OF X] conditional_entropy_eq[OF X Z] mutual_information_eq[OF X Z]
hoelzl@41981
  1199
    using setsum_joint_distribution_singleton[OF fZ fX, unfolded joint_distribution_commute_singleton[of Z X]]
hoelzl@40859
  1200
    by (simp add: * setsum_cartesian_product' setsum_subtractf setsum_left_distrib[symmetric]
hoelzl@41981
  1201
                     setsum_distribution)
hoelzl@40859
  1202
qed
hoelzl@36080
  1203
hoelzl@40859
  1204
lemma (in information_space) conditional_entropy_less_eq_entropy:
hoelzl@41689
  1205
  assumes X: "simple_function M X" and Z: "simple_function M Z"
hoelzl@40859
  1206
  shows "\<H>(X | Z) \<le> \<H>(X)"
hoelzl@36624
  1207
proof -
hoelzl@40859
  1208
  have "\<I>(X ; Z) = \<H>(X) - \<H>(X | Z)" using mutual_information_eq_entropy_conditional_entropy[OF assms] .
hoelzl@40859
  1209
  with mutual_information_positive[OF X Z] entropy_positive[OF X]
hoelzl@36624
  1210
  show ?thesis by auto
hoelzl@36080
  1211
qed
hoelzl@36080
  1212
hoelzl@40859
  1213
lemma (in information_space) entropy_chain_rule:
hoelzl@41689
  1214
  assumes X: "simple_function M X" and Y: "simple_function M Y"
hoelzl@40859
  1215
  shows  "\<H>(\<lambda>x. (X x, Y x)) = \<H>(X) + \<H>(Y|X)"
hoelzl@40859
  1216
proof -
wenzelm@46731
  1217
  let ?XY = "\<lambda>x y. joint_distribution X Y {(x, y)}"
wenzelm@46731
  1218
  let ?Y = "\<lambda>y. distribution Y {y}"
wenzelm@46731
  1219
  let ?X = "\<lambda>x. distribution X {x}"
hoelzl@40859
  1220
  note fX = X[THEN simple_function_imp_finite_random_variable]
hoelzl@40859
  1221
  note fY = Y[THEN simple_function_imp_finite_random_variable]
hoelzl@40859
  1222
  note finite_distribution_order[OF fX fY, simp]
hoelzl@40859
  1223
  { fix x y assume "x \<in> X`space M" "y \<in> Y`space M"
hoelzl@40859
  1224
    have "?XY x y * log b (?XY x y / ?X x) =
hoelzl@40859
  1225
          ?XY x y * log b (?XY x y) - ?XY x y * log b (?X x)"
hoelzl@41981
  1226
      by (auto simp: log_simps zero_le_mult_iff field_simps less_le) }
hoelzl@40859
  1227
  note * = this
hoelzl@40859
  1228
  show ?thesis
hoelzl@41981
  1229
    using setsum_joint_distribution_singleton[OF fY fX]
hoelzl@40859
  1230
    unfolding entropy_eq[OF X] conditional_entropy_eq_cartesian_product[OF Y X] entropy_eq_cartesian_product[OF X Y]
hoelzl@40859
  1231
    unfolding joint_distribution_commute_singleton[of Y X] setsum_commute[of _ "X`space M"]
hoelzl@40859
  1232
    by (simp add: * setsum_subtractf setsum_left_distrib[symmetric])
hoelzl@40859
  1233
qed
hoelzl@38656
  1234
hoelzl@39097
  1235
section {* Partitioning *}
hoelzl@36080
  1236
hoelzl@36624
  1237
definition "subvimage A f g \<longleftrightarrow> (\<forall>x \<in> A. f -` {f x} \<inter> A \<subseteq> g -` {g x} \<inter> A)"
hoelzl@36624
  1238
hoelzl@36624
  1239
lemma subvimageI:
hoelzl@36624
  1240
  assumes "\<And>x y. \<lbrakk> x \<in> A ; y \<in> A ; f x = f y \<rbrakk> \<Longrightarrow> g x = g y"
hoelzl@36624
  1241
  shows "subvimage A f g"
hoelzl@36624
  1242
  using assms unfolding subvimage_def by blast
hoelzl@36624
  1243
hoelzl@36624
  1244
lemma subvimageE[consumes 1]:
hoelzl@36624
  1245
  assumes "subvimage A f g"
hoelzl@36624
  1246
  obtains "\<And>x y. \<lbrakk> x \<in> A ; y \<in> A ; f x = f y \<rbrakk> \<Longrightarrow> g x = g y"
hoelzl@36624
  1247
  using assms unfolding subvimage_def by blast
hoelzl@36624
  1248
hoelzl@36624
  1249
lemma subvimageD:
hoelzl@36624
  1250
  "\<lbrakk> subvimage A f g ; x \<in> A ; y \<in> A ; f x = f y \<rbrakk> \<Longrightarrow> g x = g y"
hoelzl@36624
  1251
  using assms unfolding subvimage_def by blast
hoelzl@36624
  1252
hoelzl@36624
  1253
lemma subvimage_subset:
hoelzl@36624
  1254
  "\<lbrakk> subvimage B f g ; A \<subseteq> B \<rbrakk> \<Longrightarrow> subvimage A f g"
hoelzl@36624
  1255
  unfolding subvimage_def by auto
hoelzl@36624
  1256
hoelzl@36624
  1257
lemma subvimage_idem[intro]: "subvimage A g g"
hoelzl@36624
  1258
  by (safe intro!: subvimageI)
hoelzl@36624
  1259
hoelzl@36624
  1260
lemma subvimage_comp_finer[intro]:
hoelzl@36624
  1261
  assumes svi: "subvimage A g h"
hoelzl@36624
  1262
  shows "subvimage A g (f \<circ> h)"
hoelzl@36624
  1263
proof (rule subvimageI, simp)
hoelzl@36624
  1264
  fix x y assume "x \<in> A" "y \<in> A" "g x = g y"
hoelzl@36624
  1265
  from svi[THEN subvimageD, OF this]
hoelzl@36624
  1266
  show "f (h x) = f (h y)" by simp
hoelzl@36624
  1267
qed
hoelzl@36624
  1268
hoelzl@36624
  1269
lemma subvimage_comp_gran:
hoelzl@36624
  1270
  assumes svi: "subvimage A g h"
hoelzl@36624
  1271
  assumes inj: "inj_on f (g ` A)"
hoelzl@36624
  1272
  shows "subvimage A (f \<circ> g) h"
hoelzl@36624
  1273
  by (rule subvimageI) (auto intro!: subvimageD[OF svi] simp: inj_on_iff[OF inj])
hoelzl@36624
  1274
hoelzl@36624
  1275
lemma subvimage_comp:
hoelzl@36624
  1276
  assumes svi: "subvimage (f ` A) g h"
hoelzl@36624
  1277
  shows "subvimage A (g \<circ> f) (h \<circ> f)"
hoelzl@36624
  1278
  by (rule subvimageI) (auto intro!: svi[THEN subvimageD])
hoelzl@36624
  1279
hoelzl@36624
  1280
lemma subvimage_trans:
hoelzl@36624
  1281
  assumes fg: "subvimage A f g"
hoelzl@36624
  1282
  assumes gh: "subvimage A g h"
hoelzl@36624
  1283
  shows "subvimage A f h"
hoelzl@36624
  1284
  by (rule subvimageI) (auto intro!: fg[THEN subvimageD] gh[THEN subvimageD])
hoelzl@36624
  1285
hoelzl@36624
  1286
lemma subvimage_translator:
hoelzl@36624
  1287
  assumes svi: "subvimage A f g"
hoelzl@36624
  1288
  shows "\<exists>h. \<forall>x \<in> A. h (f x)  = g x"
hoelzl@36624
  1289
proof (safe intro!: exI[of _ "\<lambda>x. (THE z. z \<in> (g ` (f -` {x} \<inter> A)))"])
hoelzl@36624
  1290
  fix x assume "x \<in> A"
hoelzl@36624
  1291
  show "(THE x'. x' \<in> (g ` (f -` {f x} \<inter> A))) = g x"
hoelzl@36624
  1292
    by (rule theI2[of _ "g x"])
hoelzl@36624
  1293
      (insert `x \<in> A`, auto intro!: svi[THEN subvimageD])
hoelzl@36624
  1294
qed
hoelzl@36624
  1295
hoelzl@36624
  1296
lemma subvimage_translator_image:
hoelzl@36624
  1297
  assumes svi: "subvimage A f g"
hoelzl@36624
  1298
  shows "\<exists>h. h ` f ` A = g ` A"
hoelzl@36624
  1299
proof -
hoelzl@36624
  1300
  from subvimage_translator[OF svi]
hoelzl@36624
  1301
  obtain h where "\<And>x. x \<in> A \<Longrightarrow> h (f x) = g x" by auto
hoelzl@36624
  1302
  thus ?thesis
hoelzl@36624
  1303
    by (auto intro!: exI[of _ h]
hoelzl@36624
  1304
      simp: image_compose[symmetric] comp_def cong: image_cong)
hoelzl@36624
  1305
qed
hoelzl@36624
  1306
hoelzl@36624
  1307
lemma subvimage_finite:
hoelzl@36624
  1308
  assumes svi: "subvimage A f g" and fin: "finite (f`A)"
hoelzl@36624
  1309
  shows "finite (g`A)"
hoelzl@36624
  1310
proof -
hoelzl@36624
  1311
  from subvimage_translator_image[OF svi]
nipkow@44890
  1312
  obtain h where "g`A = h`f`A" by fastforce
hoelzl@36624
  1313
  with fin show "finite (g`A)" by simp
hoelzl@36624
  1314
qed
hoelzl@36624
  1315
hoelzl@36624
  1316
lemma subvimage_disj:
hoelzl@36624
  1317
  assumes svi: "subvimage A f g"
hoelzl@36624
  1318
  shows "f -` {x} \<inter> A \<subseteq> g -` {y} \<inter> A \<or>
hoelzl@36624
  1319
      f -` {x} \<inter> g -` {y} \<inter> A = {}" (is "?sub \<or> ?dist")
hoelzl@36624
  1320
proof (rule disjCI)
hoelzl@36624
  1321
  assume "\<not> ?dist"
hoelzl@36624
  1322
  then obtain z where "z \<in> A" and "x = f z" and "y = g z" by auto
hoelzl@36624
  1323
  thus "?sub" using svi unfolding subvimage_def by auto
hoelzl@36624
  1324
qed
hoelzl@36624
  1325
hoelzl@36624
  1326
lemma setsum_image_split:
hoelzl@36624
  1327
  assumes svi: "subvimage A f g" and fin: "finite (f ` A)"
hoelzl@36624
  1328
  shows "(\<Sum>x\<in>f`A. h x) = (\<Sum>y\<in>g`A. \<Sum>x\<in>f`(g -` {y} \<inter> A). h x)"
hoelzl@36624
  1329
    (is "?lhs = ?rhs")
hoelzl@36624
  1330
proof -
hoelzl@36624
  1331
  have "f ` A =
hoelzl@36624
  1332
      snd ` (SIGMA x : g ` A. f ` (g -` {x} \<inter> A))"
hoelzl@36624
  1333
      (is "_ = snd ` ?SIGMA")
hoelzl@36624
  1334
    unfolding image_split_eq_Sigma[symmetric]
hoelzl@36624
  1335
    by (simp add: image_compose[symmetric] comp_def)
hoelzl@36624
  1336
  moreover
hoelzl@36624
  1337
  have snd_inj: "inj_on snd ?SIGMA"
hoelzl@36624
  1338
    unfolding image_split_eq_Sigma[symmetric]
hoelzl@36624
  1339
    by (auto intro!: inj_onI subvimageD[OF svi])
hoelzl@36624
  1340
  ultimately
hoelzl@36624
  1341
  have "(\<Sum>x\<in>f`A. h x) = (\<Sum>(x,y)\<in>?SIGMA. h y)"
hoelzl@36624
  1342
    by (auto simp: setsum_reindex intro: setsum_cong)
hoelzl@36624
  1343
  also have "... = ?rhs"
hoelzl@36624
  1344
    using subvimage_finite[OF svi fin] fin
hoelzl@36624
  1345
    apply (subst setsum_Sigma[symmetric])
hoelzl@36624
  1346
    by (auto intro!: finite_subset[of _ "f`A"])
hoelzl@36624
  1347
  finally show ?thesis .
hoelzl@36624
  1348
qed
hoelzl@36624
  1349
hoelzl@40859
  1350
lemma (in information_space) entropy_partition:
hoelzl@41689
  1351
  assumes sf: "simple_function M X" "simple_function M P"
hoelzl@36624
  1352
  assumes svi: "subvimage (space M) X P"
hoelzl@36624
  1353
  shows "\<H>(X) = \<H>(P) + \<H>(X|P)"
hoelzl@36624
  1354
proof -
wenzelm@46731
  1355
  let ?XP = "\<lambda>x p. joint_distribution X P {(x, p)}"
wenzelm@46731
  1356
  let ?X = "\<lambda>x. distribution X {x}"
wenzelm@46731
  1357
  let ?P = "\<lambda>p. distribution P {p}"
hoelzl@40859
  1358
  note fX = sf(1)[THEN simple_function_imp_finite_random_variable]
hoelzl@40859
  1359
  note fP = sf(2)[THEN simple_function_imp_finite_random_variable]
hoelzl@40859
  1360
  note finite_distribution_order[OF fX fP, simp]
hoelzl@41981
  1361
  have "(\<Sum>x\<in>X ` space M. ?X x * log b (?X x)) =
hoelzl@41981
  1362
    (\<Sum>y\<in>P `space M. \<Sum>x\<in>X ` space M. ?XP x y * log b (?XP x y))"
hoelzl@36624
  1363
  proof (subst setsum_image_split[OF svi],
hoelzl@40859
  1364
      safe intro!: setsum_mono_zero_cong_left imageI)
hoelzl@40859
  1365
    show "finite (X ` space M)" "finite (X ` space M)" "finite (P ` space M)"
hoelzl@40859
  1366
      using sf unfolding simple_function_def by auto
hoelzl@40859
  1367
  next
hoelzl@36624
  1368
    fix p x assume in_space: "p \<in> space M" "x \<in> space M"
hoelzl@41981
  1369
    assume "?XP (X x) (P p) * log b (?XP (X x) (P p)) \<noteq> 0"
hoelzl@36624
  1370
    hence "(\<lambda>x. (X x, P x)) -` {(X x, P p)} \<inter> space M \<noteq> {}" by (auto simp: distribution_def)
hoelzl@36624
  1371
    with svi[unfolded subvimage_def, rule_format, OF `x \<in> space M`]
hoelzl@36624
  1372
    show "x \<in> P -` {P p}" by auto
hoelzl@36624
  1373
  next
hoelzl@36624
  1374
    fix p x assume in_space: "p \<in> space M" "x \<in> space M"
hoelzl@36624
  1375
    assume "P x = P p"
hoelzl@36624
  1376
    from this[symmetric] svi[unfolded subvimage_def, rule_format, OF `x \<in> space M`]
hoelzl@36624
  1377
    have "X -` {X x} \<inter> space M \<subseteq> P -` {P p} \<inter> space M"
hoelzl@36624
  1378
      by auto
hoelzl@36624
  1379
    hence "(\<lambda>x. (X x, P x)) -` {(X x, P p)} \<inter> space M = X -` {X x} \<inter> space M"
hoelzl@36624
  1380
      by auto
hoelzl@41981
  1381
    thus "?X (X x) * log b (?X (X x)) = ?XP (X x) (P p) * log b (?XP (X x) (P p))"
hoelzl@36624
  1382
      by (auto simp: distribution_def)
hoelzl@36624
  1383
  qed
hoelzl@41981
  1384
  moreover have "\<And>x y. ?XP x y * log b (?XP x y / ?P y) =
hoelzl@41981
  1385
      ?XP x y * log b (?XP x y) - ?XP x y * log b (?P y)"
hoelzl@40859
  1386
    by (auto simp add: log_simps zero_less_mult_iff field_simps)
hoelzl@40859
  1387
  ultimately show ?thesis
hoelzl@40859
  1388
    unfolding sf[THEN entropy_eq] conditional_entropy_eq[OF sf]
hoelzl@41981
  1389
    using setsum_joint_distribution_singleton[OF fX fP]
hoelzl@41981
  1390
    by (simp add: setsum_cartesian_product' setsum_subtractf setsum_distribution
hoelzl@36624
  1391
      setsum_left_distrib[symmetric] setsum_commute[where B="P`space M"])
hoelzl@36624
  1392
qed
hoelzl@36624
  1393
hoelzl@40859
  1394
corollary (in information_space) entropy_data_processing:
hoelzl@41689
  1395
  assumes X: "simple_function M X" shows "\<H>(f \<circ> X) \<le> \<H>(X)"
hoelzl@40859
  1396
proof -
hoelzl@40859
  1397
  note X
hoelzl@41689
  1398
  moreover have fX: "simple_function M (f \<circ> X)" using X by auto
hoelzl@40859
  1399
  moreover have "subvimage (space M) X (f \<circ> X)" by auto
hoelzl@40859
  1400
  ultimately have "\<H>(X) = \<H>(f\<circ>X) + \<H>(X|f\<circ>X)" by (rule entropy_partition)
hoelzl@40859
  1401
  then show "\<H>(f \<circ> X) \<le> \<H>(X)"
hoelzl@40859
  1402
    by (auto intro: conditional_entropy_positive[OF X fX])
hoelzl@40859
  1403
qed
hoelzl@36624
  1404
hoelzl@40859
  1405
corollary (in information_space) entropy_of_inj:
hoelzl@41689
  1406
  assumes X: "simple_function M X" and inj: "inj_on f (X`space M)"
hoelzl@36624
  1407
  shows "\<H>(f \<circ> X) = \<H>(X)"
hoelzl@36624
  1408
proof (rule antisym)
hoelzl@40859
  1409
  show "\<H>(f \<circ> X) \<le> \<H>(X)" using entropy_data_processing[OF X] .
hoelzl@36624
  1410
next
hoelzl@41689
  1411
  have sf: "simple_function M (f \<circ> X)"
hoelzl@40859
  1412
    using X by auto
hoelzl@36624
  1413
  have "\<H>(X) = \<H>(the_inv_into (X`space M) f \<circ> (f \<circ> X))"
hoelzl@40859
  1414
    by (auto intro!: mutual_information_cong simp: entropy_def the_inv_into_f_f[OF inj])
hoelzl@36624
  1415
  also have "... \<le> \<H>(f \<circ> X)"
hoelzl@40859
  1416
    using entropy_data_processing[OF sf] .
hoelzl@36624
  1417
  finally show "\<H>(X) \<le> \<H>(f \<circ> X)" .
hoelzl@36624
  1418
qed
hoelzl@36624
  1419
hoelzl@36080
  1420
end