src/HOL/Probability/Information.thy
author hoelzl
Wed Oct 10 12:12:25 2012 +0200 (2012-10-10)
changeset 49786 f33d5f009627
parent 49785 0a8adca22974
child 49787 d8de705b48d4
permissions -rw-r--r--
continuous version of entropy_le
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@47694
   175
  "entropy_density b M N = log b \<circ> real \<circ> RN_deriv M N"
hoelzl@43340
   176
hoelzl@43340
   177
definition
hoelzl@47694
   178
  "KL_divergence b M N = integral\<^isup>L N (entropy_density b M N)"
hoelzl@43340
   179
hoelzl@43340
   180
lemma (in information_space) measurable_entropy_density:
hoelzl@47694
   181
  assumes ac: "absolutely_continuous M N" "sets N = events"
hoelzl@47694
   182
  shows "entropy_density b M N \<in> borel_measurable M"
hoelzl@43340
   183
proof -
hoelzl@47694
   184
  from borel_measurable_RN_deriv[OF ac] b_gt_1 show ?thesis
hoelzl@43340
   185
    unfolding entropy_density_def
hoelzl@43340
   186
    by (intro measurable_comp) auto
hoelzl@43340
   187
qed
hoelzl@43340
   188
hoelzl@47694
   189
lemma (in sigma_finite_measure) KL_density:
hoelzl@47694
   190
  fixes f :: "'a \<Rightarrow> real"
hoelzl@47694
   191
  assumes "1 < b"
hoelzl@47694
   192
  assumes f: "f \<in> borel_measurable M" "AE x in M. 0 \<le> f x"
hoelzl@47694
   193
  shows "KL_divergence b M (density M f) = (\<integral>x. f x * log b (f x) \<partial>M)"
hoelzl@47694
   194
  unfolding KL_divergence_def
hoelzl@47694
   195
proof (subst integral_density)
hoelzl@47694
   196
  show "entropy_density b M (density M (\<lambda>x. ereal (f x))) \<in> borel_measurable M"
hoelzl@49776
   197
    using f
hoelzl@47694
   198
    by (auto simp: comp_def entropy_density_def intro!: borel_measurable_log borel_measurable_RN_deriv_density)
hoelzl@47694
   199
  have "density M (RN_deriv M (density M f)) = density M f"
hoelzl@47694
   200
    using f by (intro density_RN_deriv_density) auto
hoelzl@47694
   201
  then have eq: "AE x in M. RN_deriv M (density M f) x = f x"
hoelzl@47694
   202
    using f
hoelzl@47694
   203
    by (intro density_unique)
hoelzl@47694
   204
       (auto intro!: borel_measurable_log borel_measurable_RN_deriv_density simp: RN_deriv_density_nonneg)
hoelzl@47694
   205
  show "(\<integral>x. f x * entropy_density b M (density M (\<lambda>x. ereal (f x))) x \<partial>M) = (\<integral>x. f x * log b (f x) \<partial>M)"
hoelzl@47694
   206
    apply (intro integral_cong_AE)
hoelzl@47694
   207
    using eq
hoelzl@47694
   208
    apply eventually_elim
hoelzl@47694
   209
    apply (auto simp: entropy_density_def)
hoelzl@47694
   210
    done
hoelzl@47694
   211
qed fact+
hoelzl@43340
   212
hoelzl@47694
   213
lemma (in sigma_finite_measure) KL_density_density:
hoelzl@47694
   214
  fixes f g :: "'a \<Rightarrow> real"
hoelzl@47694
   215
  assumes "1 < b"
hoelzl@47694
   216
  assumes f: "f \<in> borel_measurable M" "AE x in M. 0 \<le> f x"
hoelzl@47694
   217
  assumes g: "g \<in> borel_measurable M" "AE x in M. 0 \<le> g x"
hoelzl@47694
   218
  assumes ac: "AE x in M. f x = 0 \<longrightarrow> g x = 0"
hoelzl@47694
   219
  shows "KL_divergence b (density M f) (density M g) = (\<integral>x. g x * log b (g x / f x) \<partial>M)"
hoelzl@47694
   220
proof -
hoelzl@47694
   221
  interpret Mf: sigma_finite_measure "density M f"
hoelzl@47694
   222
    using f by (subst sigma_finite_iff_density_finite) auto
hoelzl@47694
   223
  have "KL_divergence b (density M f) (density M g) =
hoelzl@47694
   224
    KL_divergence b (density M f) (density (density M f) (\<lambda>x. g x / f x))"
hoelzl@47694
   225
    using f g ac by (subst density_density_divide) simp_all
hoelzl@47694
   226
  also have "\<dots> = (\<integral>x. (g x / f x) * log b (g x / f x) \<partial>density M f)"
hoelzl@47694
   227
    using f g `1 < b` by (intro Mf.KL_density) (auto simp: AE_density divide_nonneg_nonneg)
hoelzl@47694
   228
  also have "\<dots> = (\<integral>x. g x * log b (g x / f x) \<partial>M)"
hoelzl@47694
   229
    using ac f g `1 < b` by (subst integral_density) (auto intro!: integral_cong_AE)
hoelzl@47694
   230
  finally show ?thesis .
hoelzl@47694
   231
qed
hoelzl@43340
   232
hoelzl@47694
   233
lemma (in information_space) KL_gt_0:
hoelzl@47694
   234
  fixes D :: "'a \<Rightarrow> real"
hoelzl@47694
   235
  assumes "prob_space (density M D)"
hoelzl@47694
   236
  assumes D: "D \<in> borel_measurable M" "AE x in M. 0 \<le> D x"
hoelzl@47694
   237
  assumes int: "integrable M (\<lambda>x. D x * log b (D x))"
hoelzl@47694
   238
  assumes A: "density M D \<noteq> M"
hoelzl@47694
   239
  shows "0 < KL_divergence b M (density M D)"
hoelzl@47694
   240
proof -
hoelzl@47694
   241
  interpret N: prob_space "density M D" by fact
hoelzl@43340
   242
hoelzl@47694
   243
  obtain A where "A \<in> sets M" "emeasure (density M D) A \<noteq> emeasure M A"
hoelzl@47694
   244
    using measure_eqI[of "density M D" M] `density M D \<noteq> M` by auto
hoelzl@47694
   245
hoelzl@47694
   246
  let ?D_set = "{x\<in>space M. D x \<noteq> 0}"
hoelzl@47694
   247
  have [simp, intro]: "?D_set \<in> sets M"
hoelzl@47694
   248
    using D by auto
hoelzl@43340
   249
hoelzl@43920
   250
  have D_neg: "(\<integral>\<^isup>+ x. ereal (- D x) \<partial>M) = 0"
hoelzl@43340
   251
    using D by (subst positive_integral_0_iff_AE) auto
hoelzl@43340
   252
hoelzl@47694
   253
  have "(\<integral>\<^isup>+ x. ereal (D x) \<partial>M) = emeasure (density M D) (space M)"
hoelzl@47694
   254
    using D by (simp add: emeasure_density cong: positive_integral_cong)
hoelzl@43920
   255
  then have D_pos: "(\<integral>\<^isup>+ x. ereal (D x) \<partial>M) = 1"
hoelzl@47694
   256
    using N.emeasure_space_1 by simp
hoelzl@43340
   257
hoelzl@47694
   258
  have "integrable M D" "integral\<^isup>L M D = 1"
hoelzl@47694
   259
    using D D_pos D_neg unfolding integrable_def lebesgue_integral_def by simp_all
hoelzl@43340
   260
hoelzl@47694
   261
  have "0 \<le> 1 - measure M ?D_set"
hoelzl@43340
   262
    using prob_le_1 by (auto simp: field_simps)
hoelzl@43340
   263
  also have "\<dots> = (\<integral> x. D x - indicator ?D_set x \<partial>M)"
hoelzl@43340
   264
    using `integrable M D` `integral\<^isup>L M D = 1`
hoelzl@47694
   265
    by (simp add: emeasure_eq_measure)
hoelzl@47694
   266
  also have "\<dots> < (\<integral> x. D x * (ln b * log b (D x)) \<partial>M)"
hoelzl@43340
   267
  proof (rule integral_less_AE)
hoelzl@43340
   268
    show "integrable M (\<lambda>x. D x - indicator ?D_set x)"
hoelzl@43340
   269
      using `integrable M D`
hoelzl@43340
   270
      by (intro integral_diff integral_indicator) auto
hoelzl@43340
   271
  next
hoelzl@47694
   272
    from integral_cmult(1)[OF int, of "ln b"]
hoelzl@47694
   273
    show "integrable M (\<lambda>x. D x * (ln b * log b (D x)))" 
hoelzl@47694
   274
      by (simp add: ac_simps)
hoelzl@43340
   275
  next
hoelzl@47694
   276
    show "emeasure M {x\<in>space M. D x \<noteq> 1 \<and> D x \<noteq> 0} \<noteq> 0"
hoelzl@43340
   277
    proof
hoelzl@47694
   278
      assume eq_0: "emeasure M {x\<in>space M. D x \<noteq> 1 \<and> D x \<noteq> 0} = 0"
hoelzl@47694
   279
      then have disj: "AE x in M. D x = 1 \<or> D x = 0"
hoelzl@43340
   280
        using D(1) by (auto intro!: AE_I[OF subset_refl] sets_Collect)
hoelzl@43340
   281
hoelzl@47694
   282
      have "emeasure M {x\<in>space M. D x = 1} = (\<integral>\<^isup>+ x. indicator {x\<in>space M. D x = 1} x \<partial>M)"
hoelzl@43340
   283
        using D(1) by auto
hoelzl@47694
   284
      also have "\<dots> = (\<integral>\<^isup>+ x. ereal (D x) \<partial>M)"
hoelzl@43920
   285
        using disj by (auto intro!: positive_integral_cong_AE simp: indicator_def one_ereal_def)
hoelzl@47694
   286
      finally have "AE x in M. D x = 1"
hoelzl@47694
   287
        using D D_pos by (intro AE_I_eq_1) auto
hoelzl@43920
   288
      then have "(\<integral>\<^isup>+x. indicator A x\<partial>M) = (\<integral>\<^isup>+x. ereal (D x) * indicator A x\<partial>M)"
hoelzl@43920
   289
        by (intro positive_integral_cong_AE) (auto simp: one_ereal_def[symmetric])
hoelzl@47694
   290
      also have "\<dots> = density M D A"
hoelzl@47694
   291
        using `A \<in> sets M` D by (simp add: emeasure_density)
hoelzl@47694
   292
      finally show False using `A \<in> sets M` `emeasure (density M D) A \<noteq> emeasure M A` by simp
hoelzl@43340
   293
    qed
hoelzl@43340
   294
    show "{x\<in>space M. D x \<noteq> 1 \<and> D x \<noteq> 0} \<in> sets M"
hoelzl@47694
   295
      using D(1) by (auto intro: sets_Collect_conj)
hoelzl@43340
   296
hoelzl@47694
   297
    show "AE t in M. t \<in> {x\<in>space M. D x \<noteq> 1 \<and> D x \<noteq> 0} \<longrightarrow>
hoelzl@47694
   298
      D t - indicator ?D_set t \<noteq> D t * (ln b * log b (D t))"
hoelzl@43340
   299
      using D(2)
hoelzl@47694
   300
    proof (eventually_elim, safe)
hoelzl@47694
   301
      fix t assume Dt: "t \<in> space M" "D t \<noteq> 1" "D t \<noteq> 0" "0 \<le> D t"
hoelzl@47694
   302
        and eq: "D t - indicator ?D_set t = D t * (ln b * log b (D t))"
hoelzl@43340
   303
hoelzl@43340
   304
      have "D t - 1 = D t - indicator ?D_set t"
hoelzl@43340
   305
        using Dt by simp
hoelzl@43340
   306
      also note eq
hoelzl@47694
   307
      also have "D t * (ln b * log b (D t)) = - D t * ln (1 / D t)"
hoelzl@47694
   308
        using b_gt_1 `D t \<noteq> 0` `0 \<le> D t`
hoelzl@47694
   309
        by (simp add: log_def ln_div less_le)
hoelzl@43340
   310
      finally have "ln (1 / D t) = 1 / D t - 1"
hoelzl@43340
   311
        using `D t \<noteq> 0` by (auto simp: field_simps)
hoelzl@43340
   312
      from ln_eq_minus_one[OF _ this] `D t \<noteq> 0` `0 \<le> D t` `D t \<noteq> 1`
hoelzl@43340
   313
      show False by auto
hoelzl@43340
   314
    qed
hoelzl@43340
   315
hoelzl@47694
   316
    show "AE t in M. D t - indicator ?D_set t \<le> D t * (ln b * log b (D t))"
hoelzl@47694
   317
      using D(2) AE_space
hoelzl@47694
   318
    proof eventually_elim
hoelzl@47694
   319
      fix t assume "t \<in> space M" "0 \<le> D t"
hoelzl@47694
   320
      show "D t - indicator ?D_set t \<le> D t * (ln b * log b (D t))"
hoelzl@43340
   321
      proof cases
hoelzl@43340
   322
        assume asm: "D t \<noteq> 0"
hoelzl@43340
   323
        then have "0 < D t" using `0 \<le> D t` by auto
hoelzl@43340
   324
        then have "0 < 1 / D t" by auto
hoelzl@43340
   325
        have "D t - indicator ?D_set t \<le> - D t * (1 / D t - 1)"
hoelzl@43340
   326
          using asm `t \<in> space M` by (simp add: field_simps)
hoelzl@43340
   327
        also have "- D t * (1 / D t - 1) \<le> - D t * ln (1 / D t)"
hoelzl@43340
   328
          using ln_le_minus_one `0 < 1 / D t` by (intro mult_left_mono_neg) auto
hoelzl@47694
   329
        also have "\<dots> = D t * (ln b * log b (D t))"
hoelzl@47694
   330
          using `0 < D t` b_gt_1
hoelzl@47694
   331
          by (simp_all add: log_def ln_div)
hoelzl@43340
   332
        finally show ?thesis by simp
hoelzl@43340
   333
      qed simp
hoelzl@43340
   334
    qed
hoelzl@43340
   335
  qed
hoelzl@47694
   336
  also have "\<dots> = (\<integral> x. ln b * (D x * log b (D x)) \<partial>M)"
hoelzl@47694
   337
    by (simp add: ac_simps)
hoelzl@47694
   338
  also have "\<dots> = ln b * (\<integral> x. D x * log b (D x) \<partial>M)"
hoelzl@47694
   339
    using int by (rule integral_cmult)
hoelzl@47694
   340
  finally show ?thesis
hoelzl@47694
   341
    using b_gt_1 D by (subst KL_density) (auto simp: zero_less_mult_iff)
hoelzl@43340
   342
qed
hoelzl@43340
   343
hoelzl@47694
   344
lemma (in sigma_finite_measure) KL_same_eq_0: "KL_divergence b M M = 0"
hoelzl@43340
   345
proof -
hoelzl@47694
   346
  have "AE x in M. 1 = RN_deriv M M x"
hoelzl@43340
   347
  proof (rule RN_deriv_unique)
hoelzl@47694
   348
    show "(\<lambda>x. 1) \<in> borel_measurable M" "AE x in M. 0 \<le> (1 :: ereal)" by auto
hoelzl@47694
   349
    show "density M (\<lambda>x. 1) = M"
hoelzl@47694
   350
      apply (auto intro!: measure_eqI emeasure_density)
hoelzl@47694
   351
      apply (subst emeasure_density)
hoelzl@47694
   352
      apply auto
hoelzl@47694
   353
      done
hoelzl@43340
   354
  qed
hoelzl@47694
   355
  then have "AE x in M. log b (real (RN_deriv M M x)) = 0"
hoelzl@43340
   356
    by (elim AE_mp) simp
hoelzl@43340
   357
  from integral_cong_AE[OF this]
hoelzl@47694
   358
  have "integral\<^isup>L M (entropy_density b M M) = 0"
hoelzl@43340
   359
    by (simp add: entropy_density_def comp_def)
hoelzl@47694
   360
  then show "KL_divergence b M M = 0"
hoelzl@43340
   361
    unfolding KL_divergence_def
hoelzl@47694
   362
    by auto
hoelzl@43340
   363
qed
hoelzl@43340
   364
hoelzl@47694
   365
lemma (in information_space) KL_eq_0_iff_eq:
hoelzl@47694
   366
  fixes D :: "'a \<Rightarrow> real"
hoelzl@47694
   367
  assumes "prob_space (density M D)"
hoelzl@47694
   368
  assumes D: "D \<in> borel_measurable M" "AE x in M. 0 \<le> D x"
hoelzl@47694
   369
  assumes int: "integrable M (\<lambda>x. D x * log b (D x))"
hoelzl@47694
   370
  shows "KL_divergence b M (density M D) = 0 \<longleftrightarrow> density M D = M"
hoelzl@47694
   371
  using KL_same_eq_0[of b] KL_gt_0[OF assms]
hoelzl@47694
   372
  by (auto simp: less_le)
hoelzl@43340
   373
hoelzl@47694
   374
lemma (in information_space) KL_eq_0_iff_eq_ac:
hoelzl@47694
   375
  fixes D :: "'a \<Rightarrow> real"
hoelzl@47694
   376
  assumes "prob_space N"
hoelzl@47694
   377
  assumes ac: "absolutely_continuous M N" "sets N = sets M"
hoelzl@47694
   378
  assumes int: "integrable N (entropy_density b M N)"
hoelzl@47694
   379
  shows "KL_divergence b M N = 0 \<longleftrightarrow> N = M"
hoelzl@41833
   380
proof -
hoelzl@47694
   381
  interpret N: prob_space N by fact
hoelzl@47694
   382
  have "finite_measure N" by unfold_locales
hoelzl@47694
   383
  from real_RN_deriv[OF this ac] guess D . note D = this
hoelzl@47694
   384
  
hoelzl@47694
   385
  have "N = density M (RN_deriv M N)"
hoelzl@47694
   386
    using ac by (rule density_RN_deriv[symmetric])
hoelzl@47694
   387
  also have "\<dots> = density M D"
hoelzl@47694
   388
    using borel_measurable_RN_deriv[OF ac] D by (auto intro!: density_cong)
hoelzl@47694
   389
  finally have N: "N = density M D" .
hoelzl@41833
   390
hoelzl@47694
   391
  from absolutely_continuous_AE[OF ac(2,1) D(2)] D b_gt_1 ac measurable_entropy_density
hoelzl@47694
   392
  have "integrable N (\<lambda>x. log b (D x))"
hoelzl@47694
   393
    by (intro integrable_cong_AE[THEN iffD2, OF _ _ _ int])
hoelzl@47694
   394
       (auto simp: N entropy_density_def)
hoelzl@47694
   395
  with D b_gt_1 have "integrable M (\<lambda>x. D x * log b (D x))"
hoelzl@47694
   396
    by (subst integral_density(2)[symmetric]) (auto simp: N[symmetric] comp_def)
hoelzl@47694
   397
  with `prob_space N` D show ?thesis
hoelzl@47694
   398
    unfolding N
hoelzl@47694
   399
    by (intro KL_eq_0_iff_eq) auto
hoelzl@41833
   400
qed
hoelzl@41833
   401
hoelzl@47694
   402
lemma (in information_space) KL_nonneg:
hoelzl@47694
   403
  assumes "prob_space (density M D)"
hoelzl@47694
   404
  assumes D: "D \<in> borel_measurable M" "AE x in M. 0 \<le> D x"
hoelzl@47694
   405
  assumes int: "integrable M (\<lambda>x. D x * log b (D x))"
hoelzl@47694
   406
  shows "0 \<le> KL_divergence b M (density M D)"
hoelzl@47694
   407
  using KL_gt_0[OF assms] by (cases "density M D = M") (auto simp: KL_same_eq_0)
hoelzl@40859
   408
hoelzl@47694
   409
lemma (in sigma_finite_measure) KL_density_density_nonneg:
hoelzl@47694
   410
  fixes f g :: "'a \<Rightarrow> real"
hoelzl@47694
   411
  assumes "1 < b"
hoelzl@47694
   412
  assumes f: "f \<in> borel_measurable M" "AE x in M. 0 \<le> f x" "prob_space (density M f)"
hoelzl@47694
   413
  assumes g: "g \<in> borel_measurable M" "AE x in M. 0 \<le> g x" "prob_space (density M g)"
hoelzl@47694
   414
  assumes ac: "AE x in M. f x = 0 \<longrightarrow> g x = 0"
hoelzl@47694
   415
  assumes int: "integrable M (\<lambda>x. g x * log b (g x / f x))"
hoelzl@47694
   416
  shows "0 \<le> KL_divergence b (density M f) (density M g)"
hoelzl@47694
   417
proof -
hoelzl@47694
   418
  interpret Mf: prob_space "density M f" by fact
hoelzl@47694
   419
  interpret Mf: information_space "density M f" b by default fact
hoelzl@47694
   420
  have eq: "density (density M f) (\<lambda>x. g x / f x) = density M g" (is "?DD = _")
hoelzl@47694
   421
    using f g ac by (subst density_density_divide) simp_all
hoelzl@36080
   422
hoelzl@47694
   423
  have "0 \<le> KL_divergence b (density M f) (density (density M f) (\<lambda>x. g x / f x))"
hoelzl@47694
   424
  proof (rule Mf.KL_nonneg)
hoelzl@47694
   425
    show "prob_space ?DD" unfolding eq by fact
hoelzl@47694
   426
    from f g show "(\<lambda>x. g x / f x) \<in> borel_measurable (density M f)"
hoelzl@47694
   427
      by auto
hoelzl@47694
   428
    show "AE x in density M f. 0 \<le> g x / f x"
hoelzl@47694
   429
      using f g by (auto simp: AE_density divide_nonneg_nonneg)
hoelzl@47694
   430
    show "integrable (density M f) (\<lambda>x. g x / f x * log b (g x / f x))"
hoelzl@47694
   431
      using `1 < b` f g ac
hoelzl@47694
   432
      by (subst integral_density)
hoelzl@47694
   433
         (auto intro!: integrable_cong_AE[THEN iffD2, OF _ _ _ int] measurable_If)
hoelzl@47694
   434
  qed
hoelzl@47694
   435
  also have "\<dots> = KL_divergence b (density M f) (density M g)"
hoelzl@47694
   436
    using f g ac by (subst density_density_divide) simp_all
hoelzl@47694
   437
  finally show ?thesis .
hoelzl@36080
   438
qed
hoelzl@36080
   439
hoelzl@39097
   440
subsection {* Mutual Information *}
hoelzl@39097
   441
hoelzl@36080
   442
definition (in prob_space)
hoelzl@38656
   443
  "mutual_information b S T X Y =
hoelzl@47694
   444
    KL_divergence b (distr M S X \<Otimes>\<^isub>M distr M T Y) (distr M (S \<Otimes>\<^isub>M T) (\<lambda>x. (X x, Y x)))"
hoelzl@36080
   445
hoelzl@47694
   446
lemma (in information_space) mutual_information_indep_vars:
hoelzl@43340
   447
  fixes S T X Y
hoelzl@47694
   448
  defines "P \<equiv> distr M S X \<Otimes>\<^isub>M distr M T Y"
hoelzl@47694
   449
  defines "Q \<equiv> distr M (S \<Otimes>\<^isub>M T) (\<lambda>x. (X x, Y x))"
hoelzl@43340
   450
  shows "indep_var S X T Y \<longleftrightarrow>
hoelzl@43340
   451
    (random_variable S X \<and> random_variable T Y \<and>
hoelzl@47694
   452
      absolutely_continuous P Q \<and> integrable Q (entropy_density b P Q) \<and>
hoelzl@47694
   453
      mutual_information b S T X Y = 0)"
hoelzl@47694
   454
  unfolding indep_var_distribution_eq
hoelzl@43340
   455
proof safe
hoelzl@47694
   456
  assume rv: "random_variable S X" "random_variable T Y"
hoelzl@43340
   457
hoelzl@47694
   458
  interpret X: prob_space "distr M S X"
hoelzl@47694
   459
    by (rule prob_space_distr) fact
hoelzl@47694
   460
  interpret Y: prob_space "distr M T Y"
hoelzl@47694
   461
    by (rule prob_space_distr) fact
hoelzl@47694
   462
  interpret XY: pair_prob_space "distr M S X" "distr M T Y" by default
hoelzl@47694
   463
  interpret P: information_space P b unfolding P_def by default (rule b_gt_1)
hoelzl@43340
   464
hoelzl@47694
   465
  interpret Q: prob_space Q unfolding Q_def
hoelzl@47694
   466
    by (rule prob_space_distr) (simp add: comp_def measurable_pair_iff rv)
hoelzl@43340
   467
hoelzl@47694
   468
  { assume "distr M S X \<Otimes>\<^isub>M distr M T Y = distr M (S \<Otimes>\<^isub>M T) (\<lambda>x. (X x, Y x))"
hoelzl@47694
   469
    then have [simp]: "Q = P"  unfolding Q_def P_def by simp
hoelzl@43340
   470
hoelzl@47694
   471
    show ac: "absolutely_continuous P Q" by (simp add: absolutely_continuous_def)
hoelzl@47694
   472
    then have ed: "entropy_density b P Q \<in> borel_measurable P"
hoelzl@47694
   473
      by (rule P.measurable_entropy_density) simp
hoelzl@43340
   474
hoelzl@47694
   475
    have "AE x in P. 1 = RN_deriv P Q x"
hoelzl@47694
   476
    proof (rule P.RN_deriv_unique)
hoelzl@47694
   477
      show "density P (\<lambda>x. 1) = Q"
hoelzl@47694
   478
        unfolding `Q = P` by (intro measure_eqI) (auto simp: emeasure_density)
hoelzl@47694
   479
    qed auto
hoelzl@47694
   480
    then have ae_0: "AE x in P. entropy_density b P Q x = 0"
hoelzl@47694
   481
      by eventually_elim (auto simp: entropy_density_def)
hoelzl@47694
   482
    then have "integrable P (entropy_density b P Q) \<longleftrightarrow> integrable Q (\<lambda>x. 0)"
hoelzl@47694
   483
      using ed unfolding `Q = P` by (intro integrable_cong_AE) auto
hoelzl@47694
   484
    then show "integrable Q (entropy_density b P Q)" by simp
hoelzl@43340
   485
hoelzl@47694
   486
    show "mutual_information b S T X Y = 0"
hoelzl@47694
   487
      unfolding mutual_information_def KL_divergence_def P_def[symmetric] Q_def[symmetric] `Q = P`
hoelzl@47694
   488
      using ae_0 by (simp cong: integral_cong_AE) }
hoelzl@43340
   489
hoelzl@47694
   490
  { assume ac: "absolutely_continuous P Q"
hoelzl@47694
   491
    assume int: "integrable Q (entropy_density b P Q)"
hoelzl@47694
   492
    assume I_eq_0: "mutual_information b S T X Y = 0"
hoelzl@43340
   493
hoelzl@47694
   494
    have eq: "Q = P"
hoelzl@47694
   495
    proof (rule P.KL_eq_0_iff_eq_ac[THEN iffD1])
hoelzl@47694
   496
      show "prob_space Q" by unfold_locales
hoelzl@47694
   497
      show "absolutely_continuous P Q" by fact
hoelzl@47694
   498
      show "integrable Q (entropy_density b P Q)" by fact
hoelzl@47694
   499
      show "sets Q = sets P" by (simp add: P_def Q_def sets_pair_measure)
hoelzl@47694
   500
      show "KL_divergence b P Q = 0"
hoelzl@47694
   501
        using I_eq_0 unfolding mutual_information_def by (simp add: P_def Q_def)
hoelzl@47694
   502
    qed
hoelzl@47694
   503
    then show "distr M S X \<Otimes>\<^isub>M distr M T Y = distr M (S \<Otimes>\<^isub>M T) (\<lambda>x. (X x, Y x))"
hoelzl@47694
   504
      unfolding P_def Q_def .. }
hoelzl@43340
   505
qed
hoelzl@43340
   506
hoelzl@40859
   507
abbreviation (in information_space)
hoelzl@40859
   508
  mutual_information_Pow ("\<I>'(_ ; _')") where
hoelzl@47694
   509
  "\<I>(X ; Y) \<equiv> mutual_information b (count_space (X`space M)) (count_space (Y`space M)) X Y"
hoelzl@41689
   510
hoelzl@47694
   511
lemma (in information_space)
hoelzl@47694
   512
  fixes Pxy :: "'b \<times> 'c \<Rightarrow> real" and Px :: "'b \<Rightarrow> real" and Py :: "'c \<Rightarrow> real"
hoelzl@47694
   513
  assumes "sigma_finite_measure S" "sigma_finite_measure T"
hoelzl@47694
   514
  assumes Px: "distributed M S X Px" and Py: "distributed M T Y Py"
hoelzl@47694
   515
  assumes Pxy: "distributed M (S \<Otimes>\<^isub>M T) (\<lambda>x. (X x, Y x)) Pxy"
hoelzl@47694
   516
  defines "f \<equiv> \<lambda>x. Pxy x * log b (Pxy x / (Px (fst x) * Py (snd x)))"
hoelzl@47694
   517
  shows mutual_information_distr: "mutual_information b S T X Y = integral\<^isup>L (S \<Otimes>\<^isub>M T) f" (is "?M = ?R")
hoelzl@47694
   518
    and mutual_information_nonneg: "integrable (S \<Otimes>\<^isub>M T) f \<Longrightarrow> 0 \<le> mutual_information b S T X Y"
hoelzl@40859
   519
proof -
hoelzl@47694
   520
  have X: "random_variable S X"
hoelzl@47694
   521
    using Px by (auto simp: distributed_def)
hoelzl@47694
   522
  have Y: "random_variable T Y"
hoelzl@47694
   523
    using Py by (auto simp: distributed_def)
hoelzl@47694
   524
  interpret S: sigma_finite_measure S by fact
hoelzl@47694
   525
  interpret T: sigma_finite_measure T by fact
hoelzl@47694
   526
  interpret ST: pair_sigma_finite S T ..
hoelzl@47694
   527
  interpret X: prob_space "distr M S X" using X by (rule prob_space_distr)
hoelzl@47694
   528
  interpret Y: prob_space "distr M T Y" using Y by (rule prob_space_distr)
hoelzl@47694
   529
  interpret XY: pair_prob_space "distr M S X" "distr M T Y" ..
hoelzl@47694
   530
  let ?P = "S \<Otimes>\<^isub>M T"
hoelzl@47694
   531
  let ?D = "distr M ?P (\<lambda>x. (X x, Y x))"
hoelzl@47694
   532
hoelzl@47694
   533
  { fix A assume "A \<in> sets S"
hoelzl@47694
   534
    with X Y have "emeasure (distr M S X) A = emeasure ?D (A \<times> space T)"
hoelzl@47694
   535
      by (auto simp: emeasure_distr measurable_Pair measurable_space
hoelzl@47694
   536
               intro!: arg_cong[where f="emeasure M"]) }
hoelzl@47694
   537
  note marginal_eq1 = this
hoelzl@47694
   538
  { fix A assume "A \<in> sets T"
hoelzl@47694
   539
    with X Y have "emeasure (distr M T Y) A = emeasure ?D (space S \<times> A)"
hoelzl@47694
   540
      by (auto simp: emeasure_distr measurable_Pair measurable_space
hoelzl@47694
   541
               intro!: arg_cong[where f="emeasure M"]) }
hoelzl@47694
   542
  note marginal_eq2 = this
hoelzl@47694
   543
hoelzl@47694
   544
  have eq: "(\<lambda>x. ereal (Px (fst x) * Py (snd x))) = (\<lambda>(x, y). ereal (Px x) * ereal (Py y))"
hoelzl@47694
   545
    by auto
hoelzl@47694
   546
hoelzl@47694
   547
  have distr_eq: "distr M S X \<Otimes>\<^isub>M distr M T Y = density ?P (\<lambda>x. ereal (Px (fst x) * Py (snd x)))"
hoelzl@47694
   548
    unfolding Px(1)[THEN distributed_distr_eq_density] Py(1)[THEN distributed_distr_eq_density] eq
hoelzl@47694
   549
  proof (subst pair_measure_density)
hoelzl@47694
   550
    show "(\<lambda>x. ereal (Px x)) \<in> borel_measurable S" "(\<lambda>y. ereal (Py y)) \<in> borel_measurable T"
hoelzl@47694
   551
      "AE x in S. 0 \<le> ereal (Px x)" "AE y in T. 0 \<le> ereal (Py y)"
hoelzl@47694
   552
      using Px Py by (auto simp: distributed_def)
hoelzl@47694
   553
    show "sigma_finite_measure (density S Px)" unfolding Px(1)[THEN distributed_distr_eq_density, symmetric] ..
hoelzl@47694
   554
    show "sigma_finite_measure (density T Py)" unfolding Py(1)[THEN distributed_distr_eq_density, symmetric] ..
hoelzl@47694
   555
  qed (fact | simp)+
hoelzl@47694
   556
  
hoelzl@47694
   557
  have M: "?M = KL_divergence b (density ?P (\<lambda>x. ereal (Px (fst x) * Py (snd x)))) (density ?P (\<lambda>x. ereal (Pxy x)))"
hoelzl@47694
   558
    unfolding mutual_information_def distr_eq Pxy(1)[THEN distributed_distr_eq_density] ..
hoelzl@47694
   559
hoelzl@47694
   560
  from Px Py have f: "(\<lambda>x. Px (fst x) * Py (snd x)) \<in> borel_measurable ?P"
hoelzl@47694
   561
    by (intro borel_measurable_times) (auto intro: distributed_real_measurable measurable_fst'' measurable_snd'')
hoelzl@47694
   562
  have PxPy_nonneg: "AE x in ?P. 0 \<le> Px (fst x) * Py (snd x)"
hoelzl@47694
   563
  proof (rule ST.AE_pair_measure)
hoelzl@47694
   564
    show "{x \<in> space ?P. 0 \<le> Px (fst x) * Py (snd x)} \<in> sets ?P"
hoelzl@47694
   565
      using f by auto
hoelzl@47694
   566
    show "AE x in S. AE y in T. 0 \<le> Px (fst (x, y)) * Py (snd (x, y))"
hoelzl@47694
   567
      using Px Py by (auto simp: zero_le_mult_iff dest!: distributed_real_AE)
hoelzl@47694
   568
  qed
hoelzl@47694
   569
hoelzl@47694
   570
  have "(AE x in ?P. Px (fst x) = 0 \<longrightarrow> Pxy x = 0)"
hoelzl@47694
   571
    by (rule subdensity_real[OF measurable_fst Pxy Px]) auto
hoelzl@47694
   572
  moreover
hoelzl@47694
   573
  have "(AE x in ?P. Py (snd x) = 0 \<longrightarrow> Pxy x = 0)"
hoelzl@47694
   574
    by (rule subdensity_real[OF measurable_snd Pxy Py]) auto
hoelzl@47694
   575
  ultimately have ac: "AE x in ?P. Px (fst x) * Py (snd x) = 0 \<longrightarrow> Pxy x = 0"
hoelzl@47694
   576
    by eventually_elim auto
hoelzl@47694
   577
hoelzl@47694
   578
  show "?M = ?R"
hoelzl@47694
   579
    unfolding M f_def
hoelzl@47694
   580
    using b_gt_1 f PxPy_nonneg Pxy[THEN distributed_real_measurable] Pxy[THEN distributed_real_AE] ac
hoelzl@47694
   581
    by (rule ST.KL_density_density)
hoelzl@47694
   582
hoelzl@47694
   583
  assume int: "integrable (S \<Otimes>\<^isub>M T) f"
hoelzl@47694
   584
  show "0 \<le> ?M" unfolding M
hoelzl@47694
   585
  proof (rule ST.KL_density_density_nonneg
hoelzl@47694
   586
    [OF b_gt_1 f PxPy_nonneg _ Pxy[THEN distributed_real_measurable] Pxy[THEN distributed_real_AE] _ ac int[unfolded f_def]])
hoelzl@47694
   587
    show "prob_space (density (S \<Otimes>\<^isub>M T) (\<lambda>x. ereal (Pxy x))) "
hoelzl@47694
   588
      unfolding distributed_distr_eq_density[OF Pxy, symmetric]
hoelzl@47694
   589
      using distributed_measurable[OF Pxy] by (rule prob_space_distr)
hoelzl@47694
   590
    show "prob_space (density (S \<Otimes>\<^isub>M T) (\<lambda>x. ereal (Px (fst x) * Py (snd x))))"
hoelzl@47694
   591
      unfolding distr_eq[symmetric] by unfold_locales
hoelzl@40859
   592
  qed
hoelzl@40859
   593
qed
hoelzl@40859
   594
hoelzl@40859
   595
lemma (in information_space)
hoelzl@47694
   596
  fixes Pxy :: "'b \<times> 'c \<Rightarrow> real" and Px :: "'b \<Rightarrow> real" and Py :: "'c \<Rightarrow> real"
hoelzl@47694
   597
  assumes "sigma_finite_measure S" "sigma_finite_measure T"
hoelzl@47694
   598
  assumes Px: "distributed M S X Px" and Py: "distributed M T Y Py"
hoelzl@47694
   599
  assumes Pxy: "distributed M (S \<Otimes>\<^isub>M T) (\<lambda>x. (X x, Y x)) Pxy"
hoelzl@47694
   600
  assumes ae: "AE x in S. AE y in T. Pxy (x, y) = Px x * Py y"
hoelzl@47694
   601
  shows mutual_information_eq_0: "mutual_information b S T X Y = 0"
hoelzl@36624
   602
proof -
hoelzl@47694
   603
  interpret S: sigma_finite_measure S by fact
hoelzl@47694
   604
  interpret T: sigma_finite_measure T by fact
hoelzl@47694
   605
  interpret ST: pair_sigma_finite S T ..
hoelzl@36080
   606
hoelzl@47694
   607
  have "AE x in S \<Otimes>\<^isub>M T. Px (fst x) = 0 \<longrightarrow> Pxy x = 0"
hoelzl@47694
   608
    by (rule subdensity_real[OF measurable_fst Pxy Px]) auto
hoelzl@47694
   609
  moreover
hoelzl@47694
   610
  have "AE x in S \<Otimes>\<^isub>M T. Py (snd x) = 0 \<longrightarrow> Pxy x = 0"
hoelzl@47694
   611
    by (rule subdensity_real[OF measurable_snd Pxy Py]) auto
hoelzl@47694
   612
  moreover 
hoelzl@47694
   613
  have "AE x in S \<Otimes>\<^isub>M T. Pxy x = Px (fst x) * Py (snd x)"
hoelzl@47694
   614
    using distributed_real_measurable[OF Px] distributed_real_measurable[OF Py] distributed_real_measurable[OF Pxy]
hoelzl@47694
   615
    by (intro ST.AE_pair_measure) (auto simp: ae intro!: measurable_snd'' measurable_fst'')
hoelzl@47694
   616
  ultimately have "AE x in S \<Otimes>\<^isub>M T. Pxy x * log b (Pxy x / (Px (fst x) * Py (snd x))) = 0"
hoelzl@47694
   617
    by eventually_elim simp
hoelzl@47694
   618
  then have "(\<integral>x. Pxy x * log b (Pxy x / (Px (fst x) * Py (snd x))) \<partial>(S \<Otimes>\<^isub>M T)) = (\<integral>x. 0 \<partial>(S \<Otimes>\<^isub>M T))"
hoelzl@47694
   619
    by (rule integral_cong_AE)
hoelzl@47694
   620
  then show ?thesis
hoelzl@47694
   621
    by (subst mutual_information_distr[OF assms(1-5)]) simp
hoelzl@36080
   622
qed
hoelzl@36080
   623
hoelzl@47694
   624
lemma (in information_space) mutual_information_simple_distributed:
hoelzl@47694
   625
  assumes X: "simple_distributed M X Px" and Y: "simple_distributed M Y Py"
hoelzl@47694
   626
  assumes XY: "simple_distributed M (\<lambda>x. (X x, Y x)) Pxy"
hoelzl@47694
   627
  shows "\<I>(X ; Y) = (\<Sum>(x, y)\<in>(\<lambda>x. (X x, Y x))`space M. Pxy (x, y) * log b (Pxy (x, y) / (Px x * Py y)))"
hoelzl@47694
   628
proof (subst mutual_information_distr[OF _ _ simple_distributed[OF X] simple_distributed[OF Y] simple_distributed_joint[OF XY]])
hoelzl@47694
   629
  note fin = simple_distributed_joint_finite[OF XY, simp]
hoelzl@47694
   630
  show "sigma_finite_measure (count_space (X ` space M))"
hoelzl@47694
   631
    by (simp add: sigma_finite_measure_count_space_finite)
hoelzl@47694
   632
  show "sigma_finite_measure (count_space (Y ` space M))"
hoelzl@47694
   633
    by (simp add: sigma_finite_measure_count_space_finite)
hoelzl@47694
   634
  let ?Pxy = "\<lambda>x. (if x \<in> (\<lambda>x. (X x, Y x)) ` space M then Pxy x else 0)"
hoelzl@47694
   635
  let ?f = "\<lambda>x. ?Pxy x * log b (?Pxy x / (Px (fst x) * Py (snd x)))"
hoelzl@47694
   636
  have "\<And>x. ?f x = (if x \<in> (\<lambda>x. (X x, Y x)) ` space M then Pxy x * log b (Pxy x / (Px (fst x) * Py (snd x))) else 0)"
hoelzl@47694
   637
    by auto
hoelzl@47694
   638
  with fin show "(\<integral> x. ?f x \<partial>(count_space (X ` space M) \<Otimes>\<^isub>M count_space (Y ` space M))) =
hoelzl@47694
   639
    (\<Sum>(x, y)\<in>(\<lambda>x. (X x, Y x)) ` space M. Pxy (x, y) * log b (Pxy (x, y) / (Px x * Py y)))"
hoelzl@47694
   640
    by (auto simp add: pair_measure_count_space lebesgue_integral_count_space_finite setsum_cases split_beta'
hoelzl@47694
   641
             intro!: setsum_cong)
hoelzl@47694
   642
qed
hoelzl@36080
   643
hoelzl@47694
   644
lemma (in information_space)
hoelzl@47694
   645
  fixes Pxy :: "'b \<times> 'c \<Rightarrow> real" and Px :: "'b \<Rightarrow> real" and Py :: "'c \<Rightarrow> real"
hoelzl@47694
   646
  assumes Px: "simple_distributed M X Px" and Py: "simple_distributed M Y Py"
hoelzl@47694
   647
  assumes Pxy: "simple_distributed M (\<lambda>x. (X x, Y x)) Pxy"
hoelzl@47694
   648
  assumes ae: "\<forall>x\<in>space M. Pxy (X x, Y x) = Px (X x) * Py (Y x)"
hoelzl@47694
   649
  shows mutual_information_eq_0_simple: "\<I>(X ; Y) = 0"
hoelzl@47694
   650
proof (subst mutual_information_simple_distributed[OF Px Py Pxy])
hoelzl@47694
   651
  have "(\<Sum>(x, y)\<in>(\<lambda>x. (X x, Y x)) ` space M. Pxy (x, y) * log b (Pxy (x, y) / (Px x * Py y))) =
hoelzl@47694
   652
    (\<Sum>(x, y)\<in>(\<lambda>x. (X x, Y x)) ` space M. 0)"
hoelzl@47694
   653
    by (intro setsum_cong) (auto simp: ae)
hoelzl@47694
   654
  then show "(\<Sum>(x, y)\<in>(\<lambda>x. (X x, Y x)) ` space M.
hoelzl@47694
   655
    Pxy (x, y) * log b (Pxy (x, y) / (Px x * Py y))) = 0" by simp
hoelzl@47694
   656
qed
hoelzl@36080
   657
hoelzl@39097
   658
subsection {* Entropy *}
hoelzl@39097
   659
hoelzl@47694
   660
definition (in prob_space) entropy :: "real \<Rightarrow> 'b measure \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> real" where
hoelzl@47694
   661
  "entropy b S X = - KL_divergence b S (distr M S X)"
hoelzl@47694
   662
hoelzl@40859
   663
abbreviation (in information_space)
hoelzl@40859
   664
  entropy_Pow ("\<H>'(_')") where
hoelzl@47694
   665
  "\<H>(X) \<equiv> entropy b (count_space (X`space M)) X"
hoelzl@41981
   666
hoelzl@47694
   667
lemma (in information_space) entropy_distr:
hoelzl@47694
   668
  fixes X :: "'a \<Rightarrow> 'b"
hoelzl@49785
   669
  assumes X: "distributed M MX X f"
hoelzl@47694
   670
  shows "entropy b MX X = - (\<integral>x. f x * log b (f x) \<partial>MX)"
hoelzl@49785
   671
  unfolding entropy_def KL_divergence_def entropy_density_def comp_def
hoelzl@49785
   672
proof (subst integral_cong_AE)
hoelzl@49785
   673
  note D = distributed_measurable[OF X] distributed_borel_measurable[OF X] distributed_AE[OF X]
hoelzl@49785
   674
  interpret X: prob_space "distr M MX X"
hoelzl@49785
   675
    using D(1) by (rule prob_space_distr)
hoelzl@49785
   676
hoelzl@49785
   677
  have sf: "sigma_finite_measure (distr M MX X)" by default
hoelzl@49785
   678
  have ae: "AE x in MX. f x = RN_deriv MX (density MX f) x"
hoelzl@49785
   679
    using D
hoelzl@49785
   680
    by (intro RN_deriv_unique_sigma_finite)
hoelzl@49785
   681
       (auto intro: divide_nonneg_nonneg measure_nonneg
hoelzl@49785
   682
             simp: distributed_distr_eq_density[symmetric, OF X] sf)
hoelzl@49785
   683
  show "AE x in distr M MX X. log b (real (RN_deriv MX (distr M MX X) x)) =
hoelzl@49785
   684
    log b (f x)"
hoelzl@49785
   685
    unfolding distributed_distr_eq_density[OF X]
hoelzl@49785
   686
    apply (subst AE_density)
hoelzl@49785
   687
    using D apply simp
hoelzl@49785
   688
    using ae apply eventually_elim
hoelzl@49785
   689
    apply (subst (asm) eq_commute)
hoelzl@49785
   690
    apply auto
hoelzl@49785
   691
    done
hoelzl@49785
   692
  show "- (\<integral> x. log b (f x) \<partial>distr M MX X) = - (\<integral> x. f x * log b (f x) \<partial>MX)"
hoelzl@49785
   693
    unfolding distributed_distr_eq_density[OF X]
hoelzl@49785
   694
    using D
hoelzl@49785
   695
    by (subst integral_density)
hoelzl@49785
   696
       (auto simp: borel_measurable_ereal_iff)
hoelzl@49785
   697
qed 
hoelzl@49785
   698
hoelzl@49786
   699
lemma (in prob_space) distributed_imp_emeasure_nonzero:
hoelzl@49786
   700
  assumes X: "distributed M MX X Px"
hoelzl@49786
   701
  shows "emeasure MX {x \<in> space MX. Px x \<noteq> 0} \<noteq> 0"
hoelzl@49786
   702
proof
hoelzl@49786
   703
  note Px = distributed_borel_measurable[OF X] distributed_AE[OF X]
hoelzl@49786
   704
  interpret X: prob_space "distr M MX X"
hoelzl@49786
   705
    using distributed_measurable[OF X] by (rule prob_space_distr)
hoelzl@49786
   706
hoelzl@49786
   707
  assume "emeasure MX {x \<in> space MX. Px x \<noteq> 0} = 0"
hoelzl@49786
   708
  with Px have "AE x in MX. Px x = 0"
hoelzl@49786
   709
    by (intro AE_I[OF subset_refl]) (auto simp: borel_measurable_ereal_iff)
hoelzl@49786
   710
  moreover
hoelzl@49786
   711
  from X.emeasure_space_1 have "(\<integral>\<^isup>+x. Px x \<partial>MX) = 1"
hoelzl@49786
   712
    unfolding distributed_distr_eq_density[OF X] using Px
hoelzl@49786
   713
    by (subst (asm) emeasure_density)
hoelzl@49786
   714
       (auto simp: borel_measurable_ereal_iff intro!: integral_cong cong: positive_integral_cong)
hoelzl@49786
   715
  ultimately show False
hoelzl@49786
   716
    by (simp add: positive_integral_cong_AE)
hoelzl@49786
   717
qed
hoelzl@49786
   718
hoelzl@49786
   719
lemma (in information_space) entropy_le:
hoelzl@49786
   720
  fixes Px :: "'b \<Rightarrow> real" and MX :: "'b measure"
hoelzl@49786
   721
  assumes X: "distributed M MX X Px"
hoelzl@49786
   722
  and fin: "emeasure MX {x \<in> space MX. Px x \<noteq> 0} \<noteq> \<infinity>"
hoelzl@49786
   723
  and int: "integrable MX (\<lambda>x. - Px x * log b (Px x))"
hoelzl@49786
   724
  shows "entropy b MX X \<le> log b (measure MX {x \<in> space MX. Px x \<noteq> 0})"
hoelzl@49786
   725
proof -
hoelzl@49786
   726
  note Px = distributed_borel_measurable[OF X] distributed_AE[OF X]
hoelzl@49786
   727
  interpret X: prob_space "distr M MX X"
hoelzl@49786
   728
    using distributed_measurable[OF X] by (rule prob_space_distr)
hoelzl@49786
   729
hoelzl@49786
   730
  have " - log b (measure MX {x \<in> space MX. Px x \<noteq> 0}) = 
hoelzl@49786
   731
    - log b (\<integral> x. indicator {x \<in> space MX. Px x \<noteq> 0} x \<partial>MX)"
hoelzl@49786
   732
    using Px fin
hoelzl@49786
   733
    by (subst integral_indicator) (auto simp: measure_def borel_measurable_ereal_iff)
hoelzl@49786
   734
  also have "- log b (\<integral> x. indicator {x \<in> space MX. Px x \<noteq> 0} x \<partial>MX) = - log b (\<integral> x. 1 / Px x \<partial>distr M MX X)"
hoelzl@49786
   735
    unfolding distributed_distr_eq_density[OF X] using Px
hoelzl@49786
   736
    apply (intro arg_cong[where f="log b"] arg_cong[where f=uminus])
hoelzl@49786
   737
    by (subst integral_density) (auto simp: borel_measurable_ereal_iff intro!: integral_cong)
hoelzl@49786
   738
  also have "\<dots> \<le> (\<integral> x. - log b (1 / Px x) \<partial>distr M MX X)"
hoelzl@49786
   739
  proof (rule X.jensens_inequality[of "\<lambda>x. 1 / Px x" "{0<..}" 0 1 "\<lambda>x. - log b x"])
hoelzl@49786
   740
    show "AE x in distr M MX X. 1 / Px x \<in> {0<..}"
hoelzl@49786
   741
      unfolding distributed_distr_eq_density[OF X]
hoelzl@49786
   742
      using Px by (auto simp: AE_density)
hoelzl@49786
   743
    have [simp]: "\<And>x. x \<in> space MX \<Longrightarrow> ereal (if Px x = 0 then 0 else 1) = indicator {x \<in> space MX. Px x \<noteq> 0} x"
hoelzl@49786
   744
      by (auto simp: one_ereal_def)
hoelzl@49786
   745
    have "(\<integral>\<^isup>+ x. max 0 (ereal (- (if Px x = 0 then 0 else 1))) \<partial>MX) = (\<integral>\<^isup>+ x. 0 \<partial>MX)"
hoelzl@49786
   746
      by (intro positive_integral_cong) (auto split: split_max)
hoelzl@49786
   747
    then show "integrable (distr M MX X) (\<lambda>x. 1 / Px x)"
hoelzl@49786
   748
      unfolding distributed_distr_eq_density[OF X] using Px
hoelzl@49786
   749
      by (auto simp: positive_integral_density integrable_def borel_measurable_ereal_iff fin positive_integral_max_0
hoelzl@49786
   750
              cong: positive_integral_cong)
hoelzl@49786
   751
    have "integrable MX (\<lambda>x. Px x * log b (1 / Px x)) =
hoelzl@49786
   752
      integrable MX (\<lambda>x. - Px x * log b (Px x))"
hoelzl@49786
   753
      using Px
hoelzl@49786
   754
      by (intro integrable_cong_AE)
hoelzl@49786
   755
         (auto simp: borel_measurable_ereal_iff log_divide_eq
hoelzl@49786
   756
                  intro!: measurable_If)
hoelzl@49786
   757
    then show "integrable (distr M MX X) (\<lambda>x. - log b (1 / Px x))"
hoelzl@49786
   758
      unfolding distributed_distr_eq_density[OF X]
hoelzl@49786
   759
      using Px int
hoelzl@49786
   760
      by (subst integral_density) (auto simp: borel_measurable_ereal_iff)
hoelzl@49786
   761
  qed (auto simp: minus_log_convex[OF b_gt_1])
hoelzl@49786
   762
  also have "\<dots> = (\<integral> x. log b (Px x) \<partial>distr M MX X)"
hoelzl@49786
   763
    unfolding distributed_distr_eq_density[OF X] using Px
hoelzl@49786
   764
    by (intro integral_cong_AE) (auto simp: AE_density log_divide_eq)
hoelzl@49786
   765
  also have "\<dots> = - entropy b MX X"
hoelzl@49786
   766
    unfolding distributed_distr_eq_density[OF X] using Px
hoelzl@49786
   767
    by (subst entropy_distr[OF X]) (auto simp: borel_measurable_ereal_iff integral_density)
hoelzl@49786
   768
  finally show ?thesis
hoelzl@49786
   769
    by simp
hoelzl@49786
   770
qed
hoelzl@49786
   771
hoelzl@49786
   772
lemma (in information_space) entropy_le_space:
hoelzl@49786
   773
  fixes Px :: "'b \<Rightarrow> real" and MX :: "'b measure"
hoelzl@49786
   774
  assumes X: "distributed M MX X Px"
hoelzl@49786
   775
  and fin: "finite_measure MX"
hoelzl@49786
   776
  and int: "integrable MX (\<lambda>x. - Px x * log b (Px x))"
hoelzl@49786
   777
  shows "entropy b MX X \<le> log b (measure MX (space MX))"
hoelzl@49786
   778
proof -
hoelzl@49786
   779
  note Px = distributed_borel_measurable[OF X] distributed_AE[OF X]
hoelzl@49786
   780
  interpret finite_measure MX by fact
hoelzl@49786
   781
  have "entropy b MX X \<le> log b (measure MX {x \<in> space MX. Px x \<noteq> 0})"
hoelzl@49786
   782
    using int X by (intro entropy_le) auto
hoelzl@49786
   783
  also have "\<dots> \<le> log b (measure MX (space MX))"
hoelzl@49786
   784
    using Px distributed_imp_emeasure_nonzero[OF X]
hoelzl@49786
   785
    by (intro log_le)
hoelzl@49786
   786
       (auto intro!: borel_measurable_ereal_iff finite_measure_mono b_gt_1
hoelzl@49786
   787
                     less_le[THEN iffD2] measure_nonneg simp: emeasure_eq_measure)
hoelzl@49786
   788
  finally show ?thesis .
hoelzl@49786
   789
qed
hoelzl@49786
   790
hoelzl@49785
   791
lemma (in prob_space) uniform_distributed_params:
hoelzl@49785
   792
  assumes X: "distributed M MX X (\<lambda>x. indicator A x / measure MX A)"
hoelzl@49785
   793
  shows "A \<in> sets MX" "measure MX A \<noteq> 0"
hoelzl@47694
   794
proof -
hoelzl@49785
   795
  interpret X: prob_space "distr M MX X"
hoelzl@49785
   796
    using distributed_measurable[OF X] by (rule prob_space_distr)
hoelzl@49785
   797
hoelzl@49785
   798
  show "measure MX A \<noteq> 0"
hoelzl@49785
   799
  proof
hoelzl@49785
   800
    assume "measure MX A = 0"
hoelzl@49785
   801
    with X.emeasure_space_1 X.prob_space distributed_distr_eq_density[OF X]
hoelzl@49785
   802
    show False
hoelzl@49785
   803
      by (simp add: emeasure_density zero_ereal_def[symmetric])
hoelzl@49785
   804
  qed
hoelzl@49785
   805
  with measure_notin_sets[of A MX] show "A \<in> sets MX"
hoelzl@49785
   806
    by blast
hoelzl@39097
   807
qed
hoelzl@36624
   808
hoelzl@47694
   809
lemma (in information_space) entropy_uniform:
hoelzl@49785
   810
  assumes X: "distributed M MX X (\<lambda>x. indicator A x / measure MX A)" (is "distributed _ _ _ ?f")
hoelzl@47694
   811
  shows "entropy b MX X = log b (measure MX A)"
hoelzl@49785
   812
proof (subst entropy_distr[OF X])
hoelzl@49785
   813
  have [simp]: "emeasure MX A \<noteq> \<infinity>"
hoelzl@49785
   814
    using uniform_distributed_params[OF X] by (auto simp add: measure_def)
hoelzl@49785
   815
  have eq: "(\<integral> x. indicator A x / measure MX A * log b (indicator A x / measure MX A) \<partial>MX) =
hoelzl@49785
   816
    (\<integral> x. (- log b (measure MX A) / measure MX A) * indicator A x \<partial>MX)"
hoelzl@49785
   817
    using measure_nonneg[of MX A] uniform_distributed_params[OF X]
hoelzl@49785
   818
    by (auto intro!: integral_cong split: split_indicator simp: log_divide_eq)
hoelzl@49785
   819
  show "- (\<integral> x. indicator A x / measure MX A * log b (indicator A x / measure MX A) \<partial>MX) =
hoelzl@49785
   820
    log b (measure MX A)"
hoelzl@49785
   821
    unfolding eq using uniform_distributed_params[OF X]
hoelzl@49785
   822
    by (subst lebesgue_integral_cmult) (auto simp: measure_def)
hoelzl@49785
   823
qed
hoelzl@36080
   824
hoelzl@47694
   825
lemma (in information_space) entropy_simple_distributed:
hoelzl@49786
   826
  "simple_distributed M X f \<Longrightarrow> \<H>(X) = - (\<Sum>x\<in>X`space M. f x * log b (f x))"
hoelzl@49786
   827
  by (subst entropy_distr[OF simple_distributed])
hoelzl@49786
   828
     (auto simp add: lebesgue_integral_count_space_finite)
hoelzl@39097
   829
hoelzl@40859
   830
lemma (in information_space) entropy_le_card_not_0:
hoelzl@47694
   831
  assumes X: "simple_distributed M X f"
hoelzl@47694
   832
  shows "\<H>(X) \<le> log b (card (X ` space M \<inter> {x. f x \<noteq> 0}))"
hoelzl@39097
   833
proof -
hoelzl@47694
   834
  have "\<H>(X) = (\<Sum>x\<in>X`space M. f x * log b (1 / f x))"
hoelzl@47694
   835
    unfolding entropy_simple_distributed[OF X] setsum_negf[symmetric]
hoelzl@47694
   836
    using X by (auto dest: simple_distributed_nonneg intro!: setsum_cong simp: log_simps less_le)
hoelzl@47694
   837
  also have "\<dots> \<le> log b (\<Sum>x\<in>X`space M. f x * (1 / f x))"
hoelzl@47694
   838
    using not_empty b_gt_1 `simple_distributed M X f`
hoelzl@47694
   839
    by (intro log_setsum') (auto simp: simple_distributed_nonneg simple_distributed_setsum_space)
hoelzl@47694
   840
  also have "\<dots> = log b (\<Sum>x\<in>X`space M. if f x \<noteq> 0 then 1 else 0)"
hoelzl@41981
   841
    by (intro arg_cong[where f="\<lambda>X. log b X"] setsum_cong) auto
hoelzl@39097
   842
  finally show ?thesis
hoelzl@47694
   843
    using `simple_distributed M X f` by (auto simp: setsum_cases real_eq_of_nat)
hoelzl@39097
   844
qed
hoelzl@39097
   845
hoelzl@40859
   846
lemma (in information_space) entropy_le_card:
hoelzl@47694
   847
  assumes "simple_distributed M X f"
hoelzl@40859
   848
  shows "\<H>(X) \<le> log b (real (card (X ` space M)))"
hoelzl@39097
   849
proof cases
hoelzl@47694
   850
  assume "X ` space M \<inter> {x. f x \<noteq> 0} = {}"
hoelzl@47694
   851
  then have "\<And>x. x\<in>X`space M \<Longrightarrow> f x = 0" by auto
hoelzl@39097
   852
  moreover
hoelzl@39097
   853
  have "0 < card (X`space M)"
hoelzl@47694
   854
    using `simple_distributed M X f` not_empty by (auto simp: card_gt_0_iff)
hoelzl@39097
   855
  then have "log b 1 \<le> log b (real (card (X`space M)))"
hoelzl@39097
   856
    using b_gt_1 by (intro log_le) auto
hoelzl@47694
   857
  ultimately show ?thesis using assms by (simp add: entropy_simple_distributed)
hoelzl@39097
   858
next
hoelzl@47694
   859
  assume False: "X ` space M \<inter> {x. f x \<noteq> 0} \<noteq> {}"
hoelzl@47694
   860
  have "card (X ` space M \<inter> {x. f x \<noteq> 0}) \<le> card (X ` space M)"
hoelzl@47694
   861
    (is "?A \<le> ?B") using assms not_empty
hoelzl@47694
   862
    by (auto intro!: card_mono simp: simple_function_def simple_distributed_def)
hoelzl@40859
   863
  note entropy_le_card_not_0[OF assms]
hoelzl@39097
   864
  also have "log b (real ?A) \<le> log b (real ?B)"
hoelzl@40859
   865
    using b_gt_1 False not_empty `?A \<le> ?B` assms
hoelzl@47694
   866
    by (auto intro!: log_le simp: card_gt_0_iff simp: simple_distributed_def)
hoelzl@39097
   867
  finally show ?thesis .
hoelzl@39097
   868
qed
hoelzl@39097
   869
hoelzl@39097
   870
subsection {* Conditional Mutual Information *}
hoelzl@39097
   871
hoelzl@36080
   872
definition (in prob_space)
hoelzl@41689
   873
  "conditional_mutual_information b MX MY MZ X Y Z \<equiv>
hoelzl@41689
   874
    mutual_information b MX (MY \<Otimes>\<^isub>M MZ) X (\<lambda>x. (Y x, Z x)) -
hoelzl@41689
   875
    mutual_information b MX MZ X Z"
hoelzl@36080
   876
hoelzl@40859
   877
abbreviation (in information_space)
hoelzl@40859
   878
  conditional_mutual_information_Pow ("\<I>'( _ ; _ | _ ')") where
hoelzl@36624
   879
  "\<I>(X ; Y | Z) \<equiv> conditional_mutual_information b
hoelzl@47694
   880
    (count_space (X ` space M)) (count_space (Y ` space M)) (count_space (Z ` space M)) X Y Z"
hoelzl@36080
   881
hoelzl@40859
   882
lemma (in information_space) conditional_mutual_information_generic_eq:
hoelzl@47694
   883
  assumes S: "sigma_finite_measure S" and T: "sigma_finite_measure T" and P: "sigma_finite_measure P"
hoelzl@47694
   884
  assumes Px: "distributed M S X Px"
hoelzl@47694
   885
  assumes Pz: "distributed M P Z Pz"
hoelzl@47694
   886
  assumes Pyz: "distributed M (T \<Otimes>\<^isub>M P) (\<lambda>x. (Y x, Z x)) Pyz"
hoelzl@47694
   887
  assumes Pxz: "distributed M (S \<Otimes>\<^isub>M P) (\<lambda>x. (X x, Z x)) Pxz"
hoelzl@47694
   888
  assumes Pxyz: "distributed M (S \<Otimes>\<^isub>M T \<Otimes>\<^isub>M P) (\<lambda>x. (X x, Y x, Z x)) Pxyz"
hoelzl@47694
   889
  assumes I1: "integrable (S \<Otimes>\<^isub>M T \<Otimes>\<^isub>M P) (\<lambda>(x, y, z). Pxyz (x, y, z) * log b (Pxyz (x, y, z) / (Px x * Pyz (y, z))))"
hoelzl@47694
   890
  assumes I2: "integrable (S \<Otimes>\<^isub>M T \<Otimes>\<^isub>M P) (\<lambda>(x, y, z). Pxyz (x, y, z) * log b (Pxz (x, z) / (Px x * Pz z)))"
hoelzl@47694
   891
  shows "conditional_mutual_information b S T P X Y Z
hoelzl@47694
   892
    = (\<integral>(x, y, z). Pxyz (x, y, z) * log b (Pxyz (x, y, z) / (Pxz (x, z) * (Pyz (y,z) / Pz z))) \<partial>(S \<Otimes>\<^isub>M T \<Otimes>\<^isub>M P))"
hoelzl@40859
   893
proof -
hoelzl@47694
   894
  interpret S: sigma_finite_measure S by fact
hoelzl@47694
   895
  interpret T: sigma_finite_measure T by fact
hoelzl@47694
   896
  interpret P: sigma_finite_measure P by fact
hoelzl@47694
   897
  interpret TP: pair_sigma_finite T P ..
hoelzl@47694
   898
  interpret SP: pair_sigma_finite S P ..
hoelzl@47694
   899
  interpret SPT: pair_sigma_finite "S \<Otimes>\<^isub>M P" T ..
hoelzl@47694
   900
  interpret STP: pair_sigma_finite S "T \<Otimes>\<^isub>M P" ..
hoelzl@47694
   901
  have TP: "sigma_finite_measure (T \<Otimes>\<^isub>M P)" ..
hoelzl@47694
   902
  have SP: "sigma_finite_measure (S \<Otimes>\<^isub>M P)" ..
hoelzl@47694
   903
  have YZ: "random_variable (T \<Otimes>\<^isub>M P) (\<lambda>x. (Y x, Z x))"
hoelzl@47694
   904
    using Pyz by (simp add: distributed_measurable)
hoelzl@47694
   905
hoelzl@47694
   906
  have Pxyz_f: "\<And>M f. f \<in> measurable M (S \<Otimes>\<^isub>M T \<Otimes>\<^isub>M P) \<Longrightarrow> (\<lambda>x. Pxyz (f x)) \<in> borel_measurable M"
hoelzl@47694
   907
    using measurable_comp[OF _ Pxyz[THEN distributed_real_measurable]] by (auto simp: comp_def)
hoelzl@47694
   908
hoelzl@47694
   909
  { fix f g h M
hoelzl@47694
   910
    assume f: "f \<in> measurable M S" and g: "g \<in> measurable M P" and h: "h \<in> measurable M (S \<Otimes>\<^isub>M P)"
hoelzl@47694
   911
    from measurable_comp[OF h Pxz[THEN distributed_real_measurable]]
hoelzl@47694
   912
         measurable_comp[OF f Px[THEN distributed_real_measurable]]
hoelzl@47694
   913
         measurable_comp[OF g Pz[THEN distributed_real_measurable]]
hoelzl@47694
   914
    have "(\<lambda>x. log b (Pxz (h x) / (Px (f x) * Pz (g x)))) \<in> borel_measurable M"
hoelzl@47694
   915
      by (simp add: comp_def b_gt_1) }
hoelzl@47694
   916
  note borel_log = this
hoelzl@47694
   917
hoelzl@47694
   918
  have measurable_cut: "(\<lambda>(x, y, z). (x, z)) \<in> measurable (S \<Otimes>\<^isub>M T \<Otimes>\<^isub>M P) (S \<Otimes>\<^isub>M P)"
hoelzl@47694
   919
    by (auto simp add: split_beta' comp_def intro!: measurable_Pair measurable_snd')
hoelzl@47694
   920
  
hoelzl@47694
   921
  from Pxz Pxyz have distr_eq: "distr M (S \<Otimes>\<^isub>M P) (\<lambda>x. (X x, Z x)) =
hoelzl@47694
   922
    distr (distr M (S \<Otimes>\<^isub>M T \<Otimes>\<^isub>M P) (\<lambda>x. (X x, Y x, Z x))) (S \<Otimes>\<^isub>M P) (\<lambda>(x, y, z). (x, z))"
hoelzl@47694
   923
    by (subst distr_distr[OF measurable_cut]) (auto dest: distributed_measurable simp: comp_def)
hoelzl@40859
   924
hoelzl@47694
   925
  have "mutual_information b S P X Z =
hoelzl@47694
   926
    (\<integral>x. Pxz x * log b (Pxz x / (Px (fst x) * Pz (snd x))) \<partial>(S \<Otimes>\<^isub>M P))"
hoelzl@47694
   927
    by (rule mutual_information_distr[OF S P Px Pz Pxz])
hoelzl@47694
   928
  also have "\<dots> = (\<integral>(x,y,z). Pxyz (x,y,z) * log b (Pxz (x,z) / (Px x * Pz z)) \<partial>(S \<Otimes>\<^isub>M T \<Otimes>\<^isub>M P))"
hoelzl@47694
   929
    using b_gt_1 Pxz Px Pz
hoelzl@47694
   930
    by (subst distributed_transform_integral[OF Pxyz Pxz, where T="\<lambda>(x, y, z). (x, z)"])
hoelzl@47694
   931
       (auto simp: split_beta' intro!: measurable_Pair measurable_snd' measurable_snd'' measurable_fst'' borel_measurable_times
hoelzl@47694
   932
             dest!: distributed_real_measurable)
hoelzl@47694
   933
  finally have mi_eq:
hoelzl@47694
   934
    "mutual_information b S P X Z = (\<integral>(x,y,z). Pxyz (x,y,z) * log b (Pxz (x,z) / (Px x * Pz z)) \<partial>(S \<Otimes>\<^isub>M T \<Otimes>\<^isub>M P))" .
hoelzl@47694
   935
  
hoelzl@47694
   936
  have "AE x in S \<Otimes>\<^isub>M T \<Otimes>\<^isub>M P. Px (fst x) = 0 \<longrightarrow> Pxyz x = 0"
hoelzl@47694
   937
    by (intro subdensity_real[of fst, OF _ Pxyz Px]) auto
hoelzl@47694
   938
  moreover have "AE x in S \<Otimes>\<^isub>M T \<Otimes>\<^isub>M P. Pz (snd (snd x)) = 0 \<longrightarrow> Pxyz x = 0"
hoelzl@47694
   939
    by (intro subdensity_real[of "\<lambda>x. snd (snd x)", OF _ Pxyz Pz]) (auto intro: measurable_snd')
hoelzl@47694
   940
  moreover have "AE x in S \<Otimes>\<^isub>M T \<Otimes>\<^isub>M P. Pxz (fst x, snd (snd x)) = 0 \<longrightarrow> Pxyz x = 0"
hoelzl@47694
   941
    by (intro subdensity_real[of "\<lambda>x. (fst x, snd (snd x))", OF _ Pxyz Pxz]) (auto intro: measurable_Pair measurable_snd')
hoelzl@47694
   942
  moreover have "AE x in S \<Otimes>\<^isub>M T \<Otimes>\<^isub>M P. Pyz (snd x) = 0 \<longrightarrow> Pxyz x = 0"
hoelzl@47694
   943
    by (intro subdensity_real[of snd, OF _ Pxyz Pyz]) (auto intro: measurable_Pair)
hoelzl@47694
   944
  moreover have "AE x in S \<Otimes>\<^isub>M T \<Otimes>\<^isub>M P. 0 \<le> Px (fst x)"
hoelzl@47694
   945
    using Px by (intro STP.AE_pair_measure) (auto simp: comp_def intro!: measurable_fst'' dest: distributed_real_AE distributed_real_measurable)
hoelzl@47694
   946
  moreover have "AE x in S \<Otimes>\<^isub>M T \<Otimes>\<^isub>M P. 0 \<le> Pyz (snd x)"
hoelzl@47694
   947
    using Pyz by (intro STP.AE_pair_measure) (auto simp: comp_def intro!: measurable_snd'' dest: distributed_real_AE distributed_real_measurable)
hoelzl@47694
   948
  moreover have "AE x in S \<Otimes>\<^isub>M T \<Otimes>\<^isub>M P. 0 \<le> Pz (snd (snd x))"
hoelzl@47694
   949
    using Pz Pz[THEN distributed_real_measurable] by (auto intro!: measurable_snd'' TP.AE_pair_measure STP.AE_pair_measure AE_I2[of S] dest: distributed_real_AE)
hoelzl@47694
   950
  moreover have "AE x in S \<Otimes>\<^isub>M T \<Otimes>\<^isub>M P. 0 \<le> Pxz (fst x, snd (snd x))"
hoelzl@47694
   951
    using Pxz[THEN distributed_real_AE, THEN SP.AE_pair]
hoelzl@47694
   952
    using measurable_comp[OF measurable_Pair[OF measurable_fst measurable_comp[OF measurable_snd measurable_snd]] Pxz[THEN distributed_real_measurable], of T]
hoelzl@47694
   953
    using measurable_comp[OF measurable_snd measurable_Pair2[OF Pxz[THEN distributed_real_measurable]], of _ T]
hoelzl@47694
   954
    by (auto intro!: TP.AE_pair_measure STP.AE_pair_measure simp: comp_def)
hoelzl@47694
   955
  moreover note Pxyz[THEN distributed_real_AE]
hoelzl@47694
   956
  ultimately have "AE x in S \<Otimes>\<^isub>M T \<Otimes>\<^isub>M P.
hoelzl@47694
   957
    Pxyz x * log b (Pxyz x / (Px (fst x) * Pyz (snd x))) -
hoelzl@47694
   958
    Pxyz x * log b (Pxz (fst x, snd (snd x)) / (Px (fst x) * Pz (snd (snd x)))) =
hoelzl@47694
   959
    Pxyz x * log b (Pxyz x * Pz (snd (snd x)) / (Pxz (fst x, snd (snd x)) * Pyz (snd x))) "
hoelzl@47694
   960
  proof eventually_elim
hoelzl@47694
   961
    case (goal1 x)
hoelzl@47694
   962
    show ?case
hoelzl@40859
   963
    proof cases
hoelzl@47694
   964
      assume "Pxyz x \<noteq> 0"
hoelzl@47694
   965
      with goal1 have "0 < Px (fst x)" "0 < Pz (snd (snd x))" "0 < Pxz (fst x, snd (snd x))" "0 < Pyz (snd x)" "0 < Pxyz x"
hoelzl@47694
   966
        by auto
hoelzl@47694
   967
      then show ?thesis
hoelzl@47694
   968
        using b_gt_1 by (simp add: log_simps mult_pos_pos less_imp_le field_simps)
hoelzl@40859
   969
    qed simp
hoelzl@40859
   970
  qed
hoelzl@47694
   971
  with I1 I2 show ?thesis
hoelzl@40859
   972
    unfolding conditional_mutual_information_def
hoelzl@47694
   973
    apply (subst mi_eq)
hoelzl@47694
   974
    apply (subst mutual_information_distr[OF S TP Px Pyz Pxyz])
hoelzl@47694
   975
    apply (subst integral_diff(2)[symmetric])
hoelzl@47694
   976
    apply (auto intro!: integral_cong_AE simp: split_beta' simp del: integral_diff)
hoelzl@47694
   977
    done
hoelzl@40859
   978
qed
hoelzl@40859
   979
hoelzl@40859
   980
lemma (in information_space) conditional_mutual_information_eq:
hoelzl@47694
   981
  assumes Pz: "simple_distributed M Z Pz"
hoelzl@47694
   982
  assumes Pyz: "simple_distributed M (\<lambda>x. (Y x, Z x)) Pyz"
hoelzl@47694
   983
  assumes Pxz: "simple_distributed M (\<lambda>x. (X x, Z x)) Pxz"
hoelzl@47694
   984
  assumes Pxyz: "simple_distributed M (\<lambda>x. (X x, Y x, Z x)) Pxyz"
hoelzl@47694
   985
  shows "\<I>(X ; Y | Z) =
hoelzl@47694
   986
   (\<Sum>(x, y, z)\<in>(\<lambda>x. (X x, Y x, Z x))`space M. Pxyz (x, y, z) * log b (Pxyz (x, y, z) / (Pxz (x, z) * (Pyz (y,z) / Pz z))))"
hoelzl@47694
   987
proof (subst conditional_mutual_information_generic_eq[OF _ _ _ _
hoelzl@47694
   988
    simple_distributed[OF Pz] simple_distributed_joint[OF Pyz] simple_distributed_joint[OF Pxz]
hoelzl@47694
   989
    simple_distributed_joint2[OF Pxyz]])
hoelzl@47694
   990
  note simple_distributed_joint2_finite[OF Pxyz, simp]
hoelzl@47694
   991
  show "sigma_finite_measure (count_space (X ` space M))"
hoelzl@47694
   992
    by (simp add: sigma_finite_measure_count_space_finite)
hoelzl@47694
   993
  show "sigma_finite_measure (count_space (Y ` space M))"
hoelzl@47694
   994
    by (simp add: sigma_finite_measure_count_space_finite)
hoelzl@47694
   995
  show "sigma_finite_measure (count_space (Z ` space M))"
hoelzl@47694
   996
    by (simp add: sigma_finite_measure_count_space_finite)
hoelzl@47694
   997
  have "count_space (X ` space M) \<Otimes>\<^isub>M count_space (Y ` space M) \<Otimes>\<^isub>M count_space (Z ` space M) =
hoelzl@47694
   998
      count_space (X`space M \<times> Y`space M \<times> Z`space M)"
hoelzl@47694
   999
    (is "?P = ?C")
hoelzl@47694
  1000
    by (simp add: pair_measure_count_space)
hoelzl@40859
  1001
hoelzl@47694
  1002
  let ?Px = "\<lambda>x. measure M (X -` {x} \<inter> space M)"
hoelzl@47694
  1003
  have "(\<lambda>x. (X x, Z x)) \<in> measurable M (count_space (X ` space M) \<Otimes>\<^isub>M count_space (Z ` space M))"
hoelzl@47694
  1004
    using simple_distributed_joint[OF Pxz] by (rule distributed_measurable)
hoelzl@47694
  1005
  from measurable_comp[OF this measurable_fst]
hoelzl@47694
  1006
  have "random_variable (count_space (X ` space M)) X"
hoelzl@47694
  1007
    by (simp add: comp_def)
hoelzl@47694
  1008
  then have "simple_function M X"    
hoelzl@47694
  1009
    unfolding simple_function_def by auto
hoelzl@47694
  1010
  then have "simple_distributed M X ?Px"
hoelzl@47694
  1011
    by (rule simple_distributedI) auto
hoelzl@47694
  1012
  then show "distributed M (count_space (X ` space M)) X ?Px"
hoelzl@47694
  1013
    by (rule simple_distributed)
hoelzl@47694
  1014
hoelzl@47694
  1015
  let ?f = "(\<lambda>x. if x \<in> (\<lambda>x. (X x, Y x, Z x)) ` space M then Pxyz x else 0)"
hoelzl@47694
  1016
  let ?g = "(\<lambda>x. if x \<in> (\<lambda>x. (Y x, Z x)) ` space M then Pyz x else 0)"
hoelzl@47694
  1017
  let ?h = "(\<lambda>x. if x \<in> (\<lambda>x. (X x, Z x)) ` space M then Pxz x else 0)"
hoelzl@47694
  1018
  show
hoelzl@47694
  1019
      "integrable ?P (\<lambda>(x, y, z). ?f (x, y, z) * log b (?f (x, y, z) / (?Px x * ?g (y, z))))"
hoelzl@47694
  1020
      "integrable ?P (\<lambda>(x, y, z). ?f (x, y, z) * log b (?h (x, z) / (?Px x * Pz z)))"
hoelzl@47694
  1021
    by (auto intro!: integrable_count_space simp: pair_measure_count_space)
hoelzl@47694
  1022
  let ?i = "\<lambda>x y z. ?f (x, y, z) * log b (?f (x, y, z) / (?h (x, z) * (?g (y, z) / Pz z)))"
hoelzl@47694
  1023
  let ?j = "\<lambda>x y z. Pxyz (x, y, z) * log b (Pxyz (x, y, z) / (Pxz (x, z) * (Pyz (y,z) / Pz z)))"
hoelzl@47694
  1024
  have "(\<lambda>(x, y, z). ?i x y z) = (\<lambda>x. if x \<in> (\<lambda>x. (X x, Y x, Z x)) ` space M then ?j (fst x) (fst (snd x)) (snd (snd x)) else 0)"
hoelzl@47694
  1025
    by (auto intro!: ext)
hoelzl@47694
  1026
  then show "(\<integral> (x, y, z). ?i x y z \<partial>?P) = (\<Sum>(x, y, z)\<in>(\<lambda>x. (X x, Y x, Z x)) ` space M. ?j x y z)"
hoelzl@47694
  1027
    by (auto intro!: setsum_cong simp add: `?P = ?C` lebesgue_integral_count_space_finite simple_distributed_finite setsum_cases split_beta')
hoelzl@36624
  1028
qed
hoelzl@36624
  1029
hoelzl@47694
  1030
lemma (in information_space) conditional_mutual_information_nonneg:
hoelzl@47694
  1031
  assumes X: "simple_function M X" and Y: "simple_function M Y" and Z: "simple_function M Z"
hoelzl@47694
  1032
  shows "0 \<le> \<I>(X ; Y | Z)"
hoelzl@47694
  1033
proof -
hoelzl@47694
  1034
  def Pz \<equiv> "\<lambda>x. if x \<in> Z`space M then measure M (Z -` {x} \<inter> space M) else 0"
hoelzl@47694
  1035
  def Pxz \<equiv> "\<lambda>x. if x \<in> (\<lambda>x. (X x, Z x))`space M then measure M ((\<lambda>x. (X x, Z x)) -` {x} \<inter> space M) else 0"
hoelzl@47694
  1036
  def Pyz \<equiv> "\<lambda>x. if x \<in> (\<lambda>x. (Y x, Z x))`space M then measure M ((\<lambda>x. (Y x, Z x)) -` {x} \<inter> space M) else 0"
hoelzl@47694
  1037
  def Pxyz \<equiv> "\<lambda>x. if x \<in> (\<lambda>x. (X x, Y x, Z x))`space M then measure M ((\<lambda>x. (X x, Y x, Z x)) -` {x} \<inter> space M) else 0"
hoelzl@47694
  1038
  let ?M = "X`space M \<times> Y`space M \<times> Z`space M"
hoelzl@36624
  1039
hoelzl@47694
  1040
  note XZ = simple_function_Pair[OF X Z]
hoelzl@47694
  1041
  note YZ = simple_function_Pair[OF Y Z]
hoelzl@47694
  1042
  note XYZ = simple_function_Pair[OF X simple_function_Pair[OF Y Z]]
hoelzl@47694
  1043
  have Pz: "simple_distributed M Z Pz"
hoelzl@47694
  1044
    using Z by (rule simple_distributedI) (auto simp: Pz_def)
hoelzl@47694
  1045
  have Pxz: "simple_distributed M (\<lambda>x. (X x, Z x)) Pxz"
hoelzl@47694
  1046
    using XZ by (rule simple_distributedI) (auto simp: Pxz_def)
hoelzl@47694
  1047
  have Pyz: "simple_distributed M (\<lambda>x. (Y x, Z x)) Pyz"
hoelzl@47694
  1048
    using YZ by (rule simple_distributedI) (auto simp: Pyz_def)
hoelzl@47694
  1049
  have Pxyz: "simple_distributed M (\<lambda>x. (X x, Y x, Z x)) Pxyz"
hoelzl@47694
  1050
    using XYZ by (rule simple_distributedI) (auto simp: Pxyz_def)
hoelzl@40859
  1051
hoelzl@47694
  1052
  { fix z assume z: "z \<in> Z ` space M" then have "(\<Sum>x\<in>X ` space M. Pxz (x, z)) = Pz z"
hoelzl@47694
  1053
      using distributed_marginal_eq_joint_simple[OF X Pz Pxz z]
hoelzl@47694
  1054
      by (auto intro!: setsum_cong simp: Pxz_def) }
hoelzl@47694
  1055
  note marginal1 = this
hoelzl@40859
  1056
hoelzl@47694
  1057
  { fix z assume z: "z \<in> Z ` space M" then have "(\<Sum>y\<in>Y ` space M. Pyz (y, z)) = Pz z"
hoelzl@47694
  1058
      using distributed_marginal_eq_joint_simple[OF Y Pz Pyz z]
hoelzl@47694
  1059
      by (auto intro!: setsum_cong simp: Pyz_def) }
hoelzl@47694
  1060
  note marginal2 = this
hoelzl@47694
  1061
hoelzl@47694
  1062
  have "- \<I>(X ; Y | Z) = - (\<Sum>(x, y, z) \<in> ?M. Pxyz (x, y, z) * log b (Pxyz (x, y, z) / (Pxz (x, z) * (Pyz (y,z) / Pz z))))"
hoelzl@47694
  1063
    unfolding conditional_mutual_information_eq[OF Pz Pyz Pxz Pxyz]
hoelzl@47694
  1064
    using X Y Z by (auto intro!: setsum_mono_zero_left simp: Pxyz_def simple_functionD)
hoelzl@47694
  1065
  also have "\<dots> \<le> log b (\<Sum>(x, y, z) \<in> ?M. Pxz (x, z) * (Pyz (y,z) / Pz z))"
hoelzl@41981
  1066
    unfolding split_beta'
hoelzl@36624
  1067
  proof (rule log_setsum_divide)
hoelzl@47694
  1068
    show "?M \<noteq> {}" using not_empty by simp
hoelzl@36624
  1069
    show "1 < b" using b_gt_1 .
hoelzl@36080
  1070
hoelzl@47694
  1071
    show "finite ?M" using X Y Z by (auto simp: simple_functionD)
hoelzl@40859
  1072
hoelzl@47694
  1073
    then show "(\<Sum>x\<in>?M. Pxyz (fst x, fst (snd x), snd (snd x))) = 1"
hoelzl@47694
  1074
      apply (subst Pxyz[THEN simple_distributed_setsum_space, symmetric])
hoelzl@47694
  1075
      apply simp
hoelzl@47694
  1076
      apply (intro setsum_mono_zero_right)
hoelzl@47694
  1077
      apply (auto simp: Pxyz_def)
hoelzl@47694
  1078
      done
hoelzl@47694
  1079
    let ?N = "(\<lambda>x. (X x, Y x, Z x)) ` space M"
hoelzl@47694
  1080
    fix x assume x: "x \<in> ?M"
hoelzl@47694
  1081
    let ?Q = "Pxyz (fst x, fst (snd x), snd (snd x))"
hoelzl@47694
  1082
    let ?P = "Pxz (fst x, snd (snd x)) * (Pyz (fst (snd x), snd (snd x)) / Pz (snd (snd x)))"
hoelzl@47694
  1083
    from x show "0 \<le> ?Q" "0 \<le> ?P"
hoelzl@47694
  1084
      using Pxyz[THEN simple_distributed, THEN distributed_real_AE]
hoelzl@47694
  1085
      using Pxz[THEN simple_distributed, THEN distributed_real_AE]
hoelzl@47694
  1086
      using Pyz[THEN simple_distributed, THEN distributed_real_AE]
hoelzl@47694
  1087
      using Pz[THEN simple_distributed, THEN distributed_real_AE]
hoelzl@47694
  1088
      by (auto intro!: mult_nonneg_nonneg divide_nonneg_nonneg simp: AE_count_space Pxyz_def Pxz_def Pyz_def Pz_def)
hoelzl@47694
  1089
    moreover assume "0 < ?Q"
hoelzl@47694
  1090
    moreover have "AE x in count_space ?N. Pz (snd (snd x)) = 0 \<longrightarrow> Pxyz x = 0"
hoelzl@47694
  1091
      by (intro subdensity_real[of "\<lambda>x. snd (snd x)", OF _ Pxyz[THEN simple_distributed] Pz[THEN simple_distributed]]) (auto intro: measurable_snd')
hoelzl@47694
  1092
    then have "\<And>x. Pz (snd (snd x)) = 0 \<longrightarrow> Pxyz x = 0"
hoelzl@47694
  1093
      by (auto simp: Pz_def Pxyz_def AE_count_space)
hoelzl@47694
  1094
    moreover have "AE x in count_space ?N. Pxz (fst x, snd (snd x)) = 0 \<longrightarrow> Pxyz x = 0"
hoelzl@47694
  1095
      by (intro subdensity_real[of "\<lambda>x. (fst x, snd (snd x))", OF _ Pxyz[THEN simple_distributed] Pxz[THEN simple_distributed]]) (auto intro: measurable_Pair measurable_snd')
hoelzl@47694
  1096
    then have "\<And>x. Pxz (fst x, snd (snd x)) = 0 \<longrightarrow> Pxyz x = 0"
hoelzl@47694
  1097
      by (auto simp: Pz_def Pxyz_def AE_count_space)
hoelzl@47694
  1098
    moreover have "AE x in count_space ?N. Pyz (snd x) = 0 \<longrightarrow> Pxyz x = 0"
hoelzl@47694
  1099
      by (intro subdensity_real[of snd, OF _ Pxyz[THEN simple_distributed] Pyz[THEN simple_distributed]]) (auto intro: measurable_Pair)
hoelzl@47694
  1100
    then have "\<And>x. Pyz (snd x) = 0 \<longrightarrow> Pxyz x = 0"
hoelzl@47694
  1101
      by (auto simp: Pz_def Pxyz_def AE_count_space)
hoelzl@47694
  1102
    ultimately show "0 < ?P" using x by (auto intro!: divide_pos_pos mult_pos_pos simp: less_le)
hoelzl@40859
  1103
  qed
hoelzl@47694
  1104
  also have "(\<Sum>(x, y, z) \<in> ?M. Pxz (x, z) * (Pyz (y,z) / Pz z)) = (\<Sum>z\<in>Z`space M. Pz z)"
hoelzl@36624
  1105
    apply (simp add: setsum_cartesian_product')
hoelzl@36624
  1106
    apply (subst setsum_commute)
hoelzl@36624
  1107
    apply (subst (2) setsum_commute)
hoelzl@47694
  1108
    apply (auto simp: setsum_divide_distrib[symmetric] setsum_product[symmetric] marginal1 marginal2
hoelzl@36624
  1109
          intro!: setsum_cong)
hoelzl@47694
  1110
    done
hoelzl@47694
  1111
  also have "log b (\<Sum>z\<in>Z`space M. Pz z) = 0"
hoelzl@47694
  1112
    using Pz[THEN simple_distributed_setsum_space] by simp
hoelzl@40859
  1113
  finally show ?thesis by simp
hoelzl@36080
  1114
qed
hoelzl@36080
  1115
hoelzl@39097
  1116
subsection {* Conditional Entropy *}
hoelzl@39097
  1117
hoelzl@36080
  1118
definition (in prob_space)
hoelzl@47694
  1119
  "conditional_entropy b S T X Y = entropy b (S \<Otimes>\<^isub>M T) (\<lambda>x. (X x, Y x)) - entropy b T Y"
hoelzl@36080
  1120
hoelzl@40859
  1121
abbreviation (in information_space)
hoelzl@40859
  1122
  conditional_entropy_Pow ("\<H>'(_ | _')") where
hoelzl@47694
  1123
  "\<H>(X | Y) \<equiv> conditional_entropy b (count_space (X`space M)) (count_space (Y`space M)) X Y"
hoelzl@36080
  1124
hoelzl@40859
  1125
lemma (in information_space) conditional_entropy_generic_eq:
hoelzl@47694
  1126
  fixes Px :: "'b \<Rightarrow> real" and Py :: "'c \<Rightarrow> real"
hoelzl@47694
  1127
  assumes S: "sigma_finite_measure S" and T: "sigma_finite_measure T"
hoelzl@47694
  1128
  assumes Px: "distributed M S X Px"
hoelzl@47694
  1129
  assumes Py: "distributed M T Y Py"
hoelzl@47694
  1130
  assumes Pxy: "distributed M (S \<Otimes>\<^isub>M T) (\<lambda>x. (X x, Y x)) Pxy"
hoelzl@47694
  1131
  assumes I1: "integrable (S \<Otimes>\<^isub>M T) (\<lambda>x. Pxy x * log b (Pxy x))"
hoelzl@47694
  1132
  assumes I2: "integrable (S \<Otimes>\<^isub>M T) (\<lambda>x. Pxy x * log b (Py (snd x)))"
hoelzl@47694
  1133
  shows "conditional_entropy b S T X Y = - (\<integral>(x, y). Pxy (x, y) * log b (Pxy (x, y) / Py y) \<partial>(S \<Otimes>\<^isub>M T))"
hoelzl@40859
  1134
proof -
hoelzl@47694
  1135
  interpret S: sigma_finite_measure S by fact
hoelzl@47694
  1136
  interpret T: sigma_finite_measure T by fact
hoelzl@47694
  1137
  interpret ST: pair_sigma_finite S T ..
hoelzl@47694
  1138
  have ST: "sigma_finite_measure (S \<Otimes>\<^isub>M T)" ..
hoelzl@47694
  1139
hoelzl@47694
  1140
  interpret Pxy: prob_space "density (S \<Otimes>\<^isub>M T) Pxy"
hoelzl@47694
  1141
    unfolding Pxy[THEN distributed_distr_eq_density, symmetric]
hoelzl@47694
  1142
    using Pxy[THEN distributed_measurable] by (rule prob_space_distr)
hoelzl@47694
  1143
hoelzl@47694
  1144
  from Py Pxy have distr_eq: "distr M T Y =
hoelzl@47694
  1145
    distr (distr M (S \<Otimes>\<^isub>M T) (\<lambda>x. (X x, Y x))) T snd"
hoelzl@47694
  1146
    by (subst distr_distr[OF measurable_snd]) (auto dest: distributed_measurable simp: comp_def)
hoelzl@47694
  1147
hoelzl@47694
  1148
  have "entropy b T Y = - (\<integral>y. Py y * log b (Py y) \<partial>T)"
hoelzl@49786
  1149
    by (rule entropy_distr[OF Py])
hoelzl@47694
  1150
  also have "\<dots> = - (\<integral>(x,y). Pxy (x,y) * log b (Py y) \<partial>(S \<Otimes>\<^isub>M T))"
hoelzl@47694
  1151
    using b_gt_1 Py[THEN distributed_real_measurable]
hoelzl@47694
  1152
    by (subst distributed_transform_integral[OF Pxy Py, where T=snd]) (auto intro!: integral_cong)
hoelzl@47694
  1153
  finally have e_eq: "entropy b T Y = - (\<integral>(x,y). Pxy (x,y) * log b (Py y) \<partial>(S \<Otimes>\<^isub>M T))" .
hoelzl@47694
  1154
  
hoelzl@47694
  1155
  have "AE x in S \<Otimes>\<^isub>M T. Px (fst x) = 0 \<longrightarrow> Pxy x = 0"
hoelzl@47694
  1156
    by (intro subdensity_real[of fst, OF _ Pxy Px]) (auto intro: measurable_Pair)
hoelzl@47694
  1157
  moreover have "AE x in S \<Otimes>\<^isub>M T. Py (snd x) = 0 \<longrightarrow> Pxy x = 0"
hoelzl@47694
  1158
    by (intro subdensity_real[of snd, OF _ Pxy Py]) (auto intro: measurable_Pair)
hoelzl@47694
  1159
  moreover have "AE x in S \<Otimes>\<^isub>M T. 0 \<le> Px (fst x)"
hoelzl@47694
  1160
    using Px by (intro ST.AE_pair_measure) (auto simp: comp_def intro!: measurable_fst'' dest: distributed_real_AE distributed_real_measurable)
hoelzl@47694
  1161
  moreover have "AE x in S \<Otimes>\<^isub>M T. 0 \<le> Py (snd x)"
hoelzl@47694
  1162
    using Py by (intro ST.AE_pair_measure) (auto simp: comp_def intro!: measurable_snd'' dest: distributed_real_AE distributed_real_measurable)
hoelzl@47694
  1163
  moreover note Pxy[THEN distributed_real_AE]
hoelzl@47694
  1164
  ultimately have pos: "AE x in S \<Otimes>\<^isub>M T. 0 \<le> Pxy x \<and> 0 \<le> Px (fst x) \<and> 0 \<le> Py (snd x) \<and>
hoelzl@47694
  1165
    (Pxy x = 0 \<or> (Pxy x \<noteq> 0 \<longrightarrow> 0 < Pxy x \<and> 0 < Px (fst x) \<and> 0 < Py (snd x)))"
hoelzl@47694
  1166
    by eventually_elim auto
hoelzl@47694
  1167
hoelzl@47694
  1168
  from pos have "AE x in S \<Otimes>\<^isub>M T.
hoelzl@47694
  1169
     Pxy x * log b (Pxy x) - Pxy x * log b (Py (snd x)) = Pxy x * log b (Pxy x / Py (snd x))"
hoelzl@47694
  1170
    by eventually_elim (auto simp: log_simps mult_pos_pos field_simps b_gt_1)
hoelzl@47694
  1171
  with I1 I2 show ?thesis
hoelzl@40859
  1172
    unfolding conditional_entropy_def
hoelzl@47694
  1173
    apply (subst e_eq)
hoelzl@49786
  1174
    apply (subst entropy_distr[OF Pxy])
hoelzl@47694
  1175
    unfolding minus_diff_minus
hoelzl@47694
  1176
    apply (subst integral_diff(2)[symmetric])
hoelzl@47694
  1177
    apply (auto intro!: integral_cong_AE simp: split_beta' simp del: integral_diff)
hoelzl@47694
  1178
    done
hoelzl@39097
  1179
qed
hoelzl@39097
  1180
hoelzl@40859
  1181
lemma (in information_space) conditional_entropy_eq:
hoelzl@47694
  1182
  assumes Y: "simple_distributed M Y Py" and X: "simple_function M X"
hoelzl@47694
  1183
  assumes XY: "simple_distributed M (\<lambda>x. (X x, Y x)) Pxy"
hoelzl@47694
  1184
    shows "\<H>(X | Y) = - (\<Sum>(x, y)\<in>(\<lambda>x. (X x, Y x)) ` space M. Pxy (x, y) * log b (Pxy (x, y) / Py y))"
hoelzl@47694
  1185
proof (subst conditional_entropy_generic_eq[OF _ _
hoelzl@47694
  1186
  simple_distributed[OF simple_distributedI[OF X refl]] simple_distributed[OF Y] simple_distributed_joint[OF XY]])
hoelzl@47694
  1187
  have [simp]: "finite (X`space M)" using X by (simp add: simple_function_def)
hoelzl@47694
  1188
  note Y[THEN simple_distributed_finite, simp]
hoelzl@47694
  1189
  show "sigma_finite_measure (count_space (X ` space M))"
hoelzl@47694
  1190
    by (simp add: sigma_finite_measure_count_space_finite)
hoelzl@47694
  1191
  show "sigma_finite_measure (count_space (Y ` space M))"
hoelzl@47694
  1192
    by (simp add: sigma_finite_measure_count_space_finite)
hoelzl@47694
  1193
  let ?f = "(\<lambda>x. if x \<in> (\<lambda>x. (X x, Y x)) ` space M then Pxy x else 0)"
hoelzl@47694
  1194
  have "count_space (X ` space M) \<Otimes>\<^isub>M count_space (Y ` space M) = count_space (X`space M \<times> Y`space M)"
hoelzl@47694
  1195
    (is "?P = ?C")
hoelzl@47694
  1196
    using X Y by (simp add: simple_distributed_finite pair_measure_count_space)
hoelzl@47694
  1197
  with X Y show
hoelzl@47694
  1198
      "integrable ?P (\<lambda>x. ?f x * log b (?f x))"
hoelzl@47694
  1199
      "integrable ?P (\<lambda>x. ?f x * log b (Py (snd x)))"
hoelzl@47694
  1200
    by (auto intro!: integrable_count_space simp: simple_distributed_finite)
hoelzl@47694
  1201
  have eq: "(\<lambda>(x, y). ?f (x, y) * log b (?f (x, y) / Py y)) =
hoelzl@47694
  1202
    (\<lambda>x. if x \<in> (\<lambda>x. (X x, Y x)) ` space M then Pxy x * log b (Pxy x / Py (snd x)) else 0)"
hoelzl@47694
  1203
    by auto
hoelzl@47694
  1204
  from X Y show "- (\<integral> (x, y). ?f (x, y) * log b (?f (x, y) / Py y) \<partial>?P) =
hoelzl@47694
  1205
    - (\<Sum>(x, y)\<in>(\<lambda>x. (X x, Y x)) ` space M. Pxy (x, y) * log b (Pxy (x, y) / Py y))"
hoelzl@47694
  1206
    by (auto intro!: setsum_cong simp add: `?P = ?C` lebesgue_integral_count_space_finite simple_distributed_finite eq setsum_cases split_beta')
hoelzl@47694
  1207
qed
hoelzl@39097
  1208
hoelzl@47694
  1209
lemma (in information_space) conditional_mutual_information_eq_conditional_entropy:
hoelzl@41689
  1210
  assumes X: "simple_function M X" and Y: "simple_function M Y"
hoelzl@47694
  1211
  shows "\<I>(X ; X | Y) = \<H>(X | Y)"
hoelzl@47694
  1212
proof -
hoelzl@47694
  1213
  def Py \<equiv> "\<lambda>x. if x \<in> Y`space M then measure M (Y -` {x} \<inter> space M) else 0"
hoelzl@47694
  1214
  def Pxy \<equiv> "\<lambda>x. if x \<in> (\<lambda>x. (X x, Y x))`space M then measure M ((\<lambda>x. (X x, Y x)) -` {x} \<inter> space M) else 0"
hoelzl@47694
  1215
  def Pxxy \<equiv> "\<lambda>x. if x \<in> (\<lambda>x. (X x, X x, Y x))`space M then measure M ((\<lambda>x. (X x, X x, Y x)) -` {x} \<inter> space M) else 0"
hoelzl@47694
  1216
  let ?M = "X`space M \<times> X`space M \<times> Y`space M"
hoelzl@39097
  1217
hoelzl@47694
  1218
  note XY = simple_function_Pair[OF X Y]
hoelzl@47694
  1219
  note XXY = simple_function_Pair[OF X XY]
hoelzl@47694
  1220
  have Py: "simple_distributed M Y Py"
hoelzl@47694
  1221
    using Y by (rule simple_distributedI) (auto simp: Py_def)
hoelzl@47694
  1222
  have Pxy: "simple_distributed M (\<lambda>x. (X x, Y x)) Pxy"
hoelzl@47694
  1223
    using XY by (rule simple_distributedI) (auto simp: Pxy_def)
hoelzl@47694
  1224
  have Pxxy: "simple_distributed M (\<lambda>x. (X x, X x, Y x)) Pxxy"
hoelzl@47694
  1225
    using XXY by (rule simple_distributedI) (auto simp: Pxxy_def)
hoelzl@47694
  1226
  have eq: "(\<lambda>x. (X x, X x, Y x)) ` space M = (\<lambda>(x, y). (x, x, y)) ` (\<lambda>x. (X x, Y x)) ` space M"
hoelzl@47694
  1227
    by auto
hoelzl@47694
  1228
  have inj: "\<And>A. inj_on (\<lambda>(x, y). (x, x, y)) A"
hoelzl@47694
  1229
    by (auto simp: inj_on_def)
hoelzl@47694
  1230
  have Pxxy_eq: "\<And>x y. Pxxy (x, x, y) = Pxy (x, y)"
hoelzl@47694
  1231
    by (auto simp: Pxxy_def Pxy_def intro!: arg_cong[where f=prob])
hoelzl@47694
  1232
  have "AE x in count_space ((\<lambda>x. (X x, Y x))`space M). Py (snd x) = 0 \<longrightarrow> Pxy x = 0"
hoelzl@47694
  1233
    by (intro subdensity_real[of snd, OF _ Pxy[THEN simple_distributed] Py[THEN simple_distributed]]) (auto intro: measurable_Pair)
hoelzl@47694
  1234
  then show ?thesis
hoelzl@47694
  1235
    apply (subst conditional_mutual_information_eq[OF Py Pxy Pxy Pxxy])
hoelzl@47694
  1236
    apply (subst conditional_entropy_eq[OF Py X Pxy])
hoelzl@47694
  1237
    apply (auto intro!: setsum_cong simp: Pxxy_eq setsum_negf[symmetric] eq setsum_reindex[OF inj]
hoelzl@47694
  1238
                log_simps zero_less_mult_iff zero_le_mult_iff field_simps mult_less_0_iff AE_count_space)
hoelzl@47694
  1239
    using Py[THEN simple_distributed, THEN distributed_real_AE] Pxy[THEN simple_distributed, THEN distributed_real_AE]
hoelzl@47694
  1240
    apply (auto simp add: not_le[symmetric] AE_count_space)
hoelzl@47694
  1241
    done
hoelzl@47694
  1242
qed
hoelzl@47694
  1243
hoelzl@47694
  1244
lemma (in information_space) conditional_entropy_nonneg:
hoelzl@47694
  1245
  assumes X: "simple_function M X" and Y: "simple_function M Y" shows "0 \<le> \<H>(X | Y)"
hoelzl@47694
  1246
  using conditional_mutual_information_eq_conditional_entropy[OF X Y] conditional_mutual_information_nonneg[OF X X Y]
hoelzl@47694
  1247
  by simp
hoelzl@36080
  1248
hoelzl@39097
  1249
subsection {* Equalities *}
hoelzl@39097
  1250
hoelzl@47694
  1251
lemma (in information_space) mutual_information_eq_entropy_conditional_entropy_distr:
hoelzl@47694
  1252
  fixes Px :: "'b \<Rightarrow> real" and Py :: "'c \<Rightarrow> real" and Pxy :: "('b \<times> 'c) \<Rightarrow> real"
hoelzl@47694
  1253
  assumes S: "sigma_finite_measure S" and T: "sigma_finite_measure T"
hoelzl@47694
  1254
  assumes Px: "distributed M S X Px" and Py: "distributed M T Y Py"
hoelzl@47694
  1255
  assumes Pxy: "distributed M (S \<Otimes>\<^isub>M T) (\<lambda>x. (X x, Y x)) Pxy"
hoelzl@47694
  1256
  assumes Ix: "integrable(S \<Otimes>\<^isub>M T) (\<lambda>x. Pxy x * log b (Px (fst x)))"
hoelzl@47694
  1257
  assumes Iy: "integrable(S \<Otimes>\<^isub>M T) (\<lambda>x. Pxy x * log b (Py (snd x)))"
hoelzl@47694
  1258
  assumes Ixy: "integrable(S \<Otimes>\<^isub>M T) (\<lambda>x. Pxy x * log b (Pxy x))"
hoelzl@47694
  1259
  shows  "mutual_information b S T X Y = entropy b S X + entropy b T Y - entropy b (S \<Otimes>\<^isub>M T) (\<lambda>x. (X x, Y x))"
hoelzl@40859
  1260
proof -
hoelzl@47694
  1261
  have X: "entropy b S X = - (\<integral>x. Pxy x * log b (Px (fst x)) \<partial>(S \<Otimes>\<^isub>M T))"
hoelzl@47694
  1262
    using b_gt_1 Px[THEN distributed_real_measurable]
hoelzl@49786
  1263
    apply (subst entropy_distr[OF Px])
hoelzl@47694
  1264
    apply (subst distributed_transform_integral[OF Pxy Px, where T=fst])
hoelzl@47694
  1265
    apply (auto intro!: integral_cong)
hoelzl@47694
  1266
    done
hoelzl@47694
  1267
hoelzl@47694
  1268
  have Y: "entropy b T Y = - (\<integral>x. Pxy x * log b (Py (snd x)) \<partial>(S \<Otimes>\<^isub>M T))"
hoelzl@47694
  1269
    using b_gt_1 Py[THEN distributed_real_measurable]
hoelzl@49786
  1270
    apply (subst entropy_distr[OF Py])
hoelzl@47694
  1271
    apply (subst distributed_transform_integral[OF Pxy Py, where T=snd])
hoelzl@47694
  1272
    apply (auto intro!: integral_cong)
hoelzl@47694
  1273
    done
hoelzl@47694
  1274
hoelzl@47694
  1275
  interpret S: sigma_finite_measure S by fact
hoelzl@47694
  1276
  interpret T: sigma_finite_measure T by fact
hoelzl@47694
  1277
  interpret ST: pair_sigma_finite S T ..
hoelzl@47694
  1278
  have ST: "sigma_finite_measure (S \<Otimes>\<^isub>M T)" ..
hoelzl@47694
  1279
hoelzl@47694
  1280
  have XY: "entropy b (S \<Otimes>\<^isub>M T) (\<lambda>x. (X x, Y x)) = - (\<integral>x. Pxy x * log b (Pxy x) \<partial>(S \<Otimes>\<^isub>M T))"
hoelzl@49786
  1281
    by (subst entropy_distr[OF Pxy]) (auto intro!: integral_cong)
hoelzl@47694
  1282
  
hoelzl@47694
  1283
  have "AE x in S \<Otimes>\<^isub>M T. Px (fst x) = 0 \<longrightarrow> Pxy x = 0"
hoelzl@47694
  1284
    by (intro subdensity_real[of fst, OF _ Pxy Px]) (auto intro: measurable_Pair)
hoelzl@47694
  1285
  moreover have "AE x in S \<Otimes>\<^isub>M T. Py (snd x) = 0 \<longrightarrow> Pxy x = 0"
hoelzl@47694
  1286
    by (intro subdensity_real[of snd, OF _ Pxy Py]) (auto intro: measurable_Pair)
hoelzl@47694
  1287
  moreover have "AE x in S \<Otimes>\<^isub>M T. 0 \<le> Px (fst x)"
hoelzl@47694
  1288
    using Px by (intro ST.AE_pair_measure) (auto simp: comp_def intro!: measurable_fst'' dest: distributed_real_AE distributed_real_measurable)
hoelzl@47694
  1289
  moreover have "AE x in S \<Otimes>\<^isub>M T. 0 \<le> Py (snd x)"
hoelzl@47694
  1290
    using Py by (intro ST.AE_pair_measure) (auto simp: comp_def intro!: measurable_snd'' dest: distributed_real_AE distributed_real_measurable)
hoelzl@47694
  1291
  moreover note Pxy[THEN distributed_real_AE]
hoelzl@47694
  1292
  ultimately have "AE x in S \<Otimes>\<^isub>M T. Pxy x * log b (Pxy x) - Pxy x * log b (Px (fst x)) - Pxy x * log b (Py (snd x)) = 
hoelzl@47694
  1293
    Pxy x * log b (Pxy x / (Px (fst x) * Py (snd x)))"
hoelzl@47694
  1294
    (is "AE x in _. ?f x = ?g x")
hoelzl@47694
  1295
  proof eventually_elim
hoelzl@47694
  1296
    case (goal1 x)
hoelzl@47694
  1297
    show ?case
hoelzl@47694
  1298
    proof cases
hoelzl@47694
  1299
      assume "Pxy x \<noteq> 0"
hoelzl@47694
  1300
      with goal1 have "0 < Px (fst x)" "0 < Py (snd x)" "0 < Pxy x"
hoelzl@47694
  1301
        by auto
hoelzl@47694
  1302
      then show ?thesis
hoelzl@47694
  1303
        using b_gt_1 by (simp add: log_simps mult_pos_pos less_imp_le field_simps)
hoelzl@47694
  1304
    qed simp
hoelzl@47694
  1305
  qed
hoelzl@47694
  1306
hoelzl@47694
  1307
  have "entropy b S X + entropy b T Y - entropy b (S \<Otimes>\<^isub>M T) (\<lambda>x. (X x, Y x)) = integral\<^isup>L (S \<Otimes>\<^isub>M T) ?f"
hoelzl@47694
  1308
    unfolding X Y XY
hoelzl@47694
  1309
    apply (subst integral_diff)
hoelzl@47694
  1310
    apply (intro integral_diff Ixy Ix Iy)+
hoelzl@47694
  1311
    apply (subst integral_diff)
hoelzl@47694
  1312
    apply (intro integral_diff Ixy Ix Iy)+
hoelzl@47694
  1313
    apply (simp add: field_simps)
hoelzl@47694
  1314
    done
hoelzl@47694
  1315
  also have "\<dots> = integral\<^isup>L (S \<Otimes>\<^isub>M T) ?g"
hoelzl@47694
  1316
    using `AE x in _. ?f x = ?g x` by (rule integral_cong_AE)
hoelzl@47694
  1317
  also have "\<dots> = mutual_information b S T X Y"
hoelzl@47694
  1318
    by (rule mutual_information_distr[OF S T Px Py Pxy, symmetric])
hoelzl@47694
  1319
  finally show ?thesis ..
hoelzl@47694
  1320
qed
hoelzl@47694
  1321
hoelzl@47694
  1322
lemma (in information_space) mutual_information_eq_entropy_conditional_entropy:
hoelzl@47694
  1323
  assumes sf_X: "simple_function M X" and sf_Y: "simple_function M Y"
hoelzl@47694
  1324
  shows  "\<I>(X ; Y) = \<H>(X) - \<H>(X | Y)"
hoelzl@47694
  1325
proof -
hoelzl@47694
  1326
  have X: "simple_distributed M X (\<lambda>x. measure M (X -` {x} \<inter> space M))"
hoelzl@47694
  1327
    using sf_X by (rule simple_distributedI) auto
hoelzl@47694
  1328
  have Y: "simple_distributed M Y (\<lambda>x. measure M (Y -` {x} \<inter> space M))"
hoelzl@47694
  1329
    using sf_Y by (rule simple_distributedI) auto
hoelzl@47694
  1330
  have sf_XY: "simple_function M (\<lambda>x. (X x, Y x))"
hoelzl@47694
  1331
    using sf_X sf_Y by (rule simple_function_Pair)
hoelzl@47694
  1332
  then have XY: "simple_distributed M (\<lambda>x. (X x, Y x)) (\<lambda>x. measure M ((\<lambda>x. (X x, Y x)) -` {x} \<inter> space M))"
hoelzl@47694
  1333
    by (rule simple_distributedI) auto
hoelzl@47694
  1334
  from simple_distributed_joint_finite[OF this, simp]
hoelzl@47694
  1335
  have eq: "count_space (X ` space M) \<Otimes>\<^isub>M count_space (Y ` space M) = count_space (X ` space M \<times> Y ` space M)"
hoelzl@47694
  1336
    by (simp add: pair_measure_count_space)
hoelzl@47694
  1337
hoelzl@47694
  1338
  have "\<I>(X ; Y) = \<H>(X) + \<H>(Y) - entropy b (count_space (X`space M) \<Otimes>\<^isub>M count_space (Y`space M)) (\<lambda>x. (X x, Y x))"
hoelzl@47694
  1339
    using sigma_finite_measure_count_space_finite sigma_finite_measure_count_space_finite simple_distributed[OF X] simple_distributed[OF Y] simple_distributed_joint[OF XY]
hoelzl@47694
  1340
    by (rule mutual_information_eq_entropy_conditional_entropy_distr) (auto simp: eq integrable_count_space)
hoelzl@47694
  1341
  then show ?thesis
hoelzl@47694
  1342
    unfolding conditional_entropy_def by simp
hoelzl@47694
  1343
qed
hoelzl@47694
  1344
hoelzl@47694
  1345
lemma (in information_space) mutual_information_nonneg_simple:
hoelzl@47694
  1346
  assumes sf_X: "simple_function M X" and sf_Y: "simple_function M Y"
hoelzl@47694
  1347
  shows  "0 \<le> \<I>(X ; Y)"
hoelzl@47694
  1348
proof -
hoelzl@47694
  1349
  have X: "simple_distributed M X (\<lambda>x. measure M (X -` {x} \<inter> space M))"
hoelzl@47694
  1350
    using sf_X by (rule simple_distributedI) auto
hoelzl@47694
  1351
  have Y: "simple_distributed M Y (\<lambda>x. measure M (Y -` {x} \<inter> space M))"
hoelzl@47694
  1352
    using sf_Y by (rule simple_distributedI) auto
hoelzl@47694
  1353
hoelzl@47694
  1354
  have sf_XY: "simple_function M (\<lambda>x. (X x, Y x))"
hoelzl@47694
  1355
    using sf_X sf_Y by (rule simple_function_Pair)
hoelzl@47694
  1356
  then have XY: "simple_distributed M (\<lambda>x. (X x, Y x)) (\<lambda>x. measure M ((\<lambda>x. (X x, Y x)) -` {x} \<inter> space M))"
hoelzl@47694
  1357
    by (rule simple_distributedI) auto
hoelzl@47694
  1358
hoelzl@47694
  1359
  from simple_distributed_joint_finite[OF this, simp]
hoelzl@47694
  1360
  have eq: "count_space (X ` space M) \<Otimes>\<^isub>M count_space (Y ` space M) = count_space (X ` space M \<times> Y ` space M)"
hoelzl@47694
  1361
    by (simp add: pair_measure_count_space)
hoelzl@47694
  1362
hoelzl@40859
  1363
  show ?thesis
hoelzl@47694
  1364
    by (rule mutual_information_nonneg[OF _ _ simple_distributed[OF X] simple_distributed[OF Y] simple_distributed_joint[OF XY]])
hoelzl@47694
  1365
       (simp_all add: eq integrable_count_space sigma_finite_measure_count_space_finite)
hoelzl@40859
  1366
qed
hoelzl@36080
  1367
hoelzl@40859
  1368
lemma (in information_space) conditional_entropy_less_eq_entropy:
hoelzl@41689
  1369
  assumes X: "simple_function M X" and Z: "simple_function M Z"
hoelzl@40859
  1370
  shows "\<H>(X | Z) \<le> \<H>(X)"
hoelzl@36624
  1371
proof -
hoelzl@47694
  1372
  have "0 \<le> \<I>(X ; Z)" using X Z by (rule mutual_information_nonneg_simple)
hoelzl@47694
  1373
  also have "\<I>(X ; Z) = \<H>(X) - \<H>(X | Z)" using mutual_information_eq_entropy_conditional_entropy[OF assms] .
hoelzl@47694
  1374
  finally show ?thesis by auto
hoelzl@36080
  1375
qed
hoelzl@36080
  1376
hoelzl@40859
  1377
lemma (in information_space) entropy_chain_rule:
hoelzl@41689
  1378
  assumes X: "simple_function M X" and Y: "simple_function M Y"
hoelzl@40859
  1379
  shows  "\<H>(\<lambda>x. (X x, Y x)) = \<H>(X) + \<H>(Y|X)"
hoelzl@40859
  1380
proof -
hoelzl@47694
  1381
  note XY = simple_distributedI[OF simple_function_Pair[OF X Y] refl]
hoelzl@47694
  1382
  note YX = simple_distributedI[OF simple_function_Pair[OF Y X] refl]
hoelzl@47694
  1383
  note simple_distributed_joint_finite[OF this, simp]
hoelzl@47694
  1384
  let ?f = "\<lambda>x. prob ((\<lambda>x. (X x, Y x)) -` {x} \<inter> space M)"
hoelzl@47694
  1385
  let ?g = "\<lambda>x. prob ((\<lambda>x. (Y x, X x)) -` {x} \<inter> space M)"
hoelzl@47694
  1386
  let ?h = "\<lambda>x. if x \<in> (\<lambda>x. (Y x, X x)) ` space M then prob ((\<lambda>x. (Y x, X x)) -` {x} \<inter> space M) else 0"
hoelzl@47694
  1387
  have "\<H>(\<lambda>x. (X x, Y x)) = - (\<Sum>x\<in>(\<lambda>x. (X x, Y x)) ` space M. ?f x * log b (?f x))"
hoelzl@47694
  1388
    using XY by (rule entropy_simple_distributed)
hoelzl@47694
  1389
  also have "\<dots> = - (\<Sum>x\<in>(\<lambda>(x, y). (y, x)) ` (\<lambda>x. (X x, Y x)) ` space M. ?g x * log b (?g x))"
hoelzl@47694
  1390
    by (subst (2) setsum_reindex) (auto simp: inj_on_def intro!: setsum_cong arg_cong[where f="\<lambda>A. prob A * log b (prob A)"])
hoelzl@47694
  1391
  also have "\<dots> = - (\<Sum>x\<in>(\<lambda>x. (Y x, X x)) ` space M. ?h x * log b (?h x))"
hoelzl@47694
  1392
    by (auto intro!: setsum_cong)
hoelzl@47694
  1393
  also have "\<dots> = entropy b (count_space (Y ` space M) \<Otimes>\<^isub>M count_space (X ` space M)) (\<lambda>x. (Y x, X x))"
hoelzl@49786
  1394
    by (subst entropy_distr[OF simple_distributed_joint[OF YX]])
hoelzl@47694
  1395
       (auto simp: pair_measure_count_space sigma_finite_measure_count_space_finite lebesgue_integral_count_space_finite
hoelzl@47694
  1396
             cong del: setsum_cong  intro!: setsum_mono_zero_left)
hoelzl@47694
  1397
  finally have "\<H>(\<lambda>x. (X x, Y x)) = entropy b (count_space (Y ` space M) \<Otimes>\<^isub>M count_space (X ` space M)) (\<lambda>x. (Y x, X x))" .
hoelzl@47694
  1398
  then show ?thesis
hoelzl@47694
  1399
    unfolding conditional_entropy_def by simp
hoelzl@36624
  1400
qed
hoelzl@36624
  1401
hoelzl@40859
  1402
lemma (in information_space) entropy_partition:
hoelzl@47694
  1403
  assumes X: "simple_function M X"
hoelzl@47694
  1404
  shows "\<H>(X) = \<H>(f \<circ> X) + \<H>(X|f \<circ> X)"
hoelzl@36624
  1405
proof -
hoelzl@47694
  1406
  note fX = simple_function_compose[OF X, of f]  
hoelzl@47694
  1407
  have eq: "(\<lambda>x. ((f \<circ> X) x, X x)) ` space M = (\<lambda>x. (f x, x)) ` X ` space M" by auto
hoelzl@47694
  1408
  have inj: "\<And>A. inj_on (\<lambda>x. (f x, x)) A"
hoelzl@47694
  1409
    by (auto simp: inj_on_def)
hoelzl@47694
  1410
  show ?thesis
hoelzl@47694
  1411
    apply (subst entropy_chain_rule[symmetric, OF fX X])
hoelzl@47694
  1412
    apply (subst entropy_simple_distributed[OF simple_distributedI[OF simple_function_Pair[OF fX X] refl]])
hoelzl@47694
  1413
    apply (subst entropy_simple_distributed[OF simple_distributedI[OF X refl]])
hoelzl@47694
  1414
    unfolding eq
hoelzl@47694
  1415
    apply (subst setsum_reindex[OF inj])
hoelzl@47694
  1416
    apply (auto intro!: setsum_cong arg_cong[where f="\<lambda>A. prob A * log b (prob A)"])
hoelzl@47694
  1417
    done
hoelzl@36624
  1418
qed
hoelzl@36624
  1419
hoelzl@40859
  1420
corollary (in information_space) entropy_data_processing:
hoelzl@41689
  1421
  assumes X: "simple_function M X" shows "\<H>(f \<circ> X) \<le> \<H>(X)"
hoelzl@40859
  1422
proof -
hoelzl@47694
  1423
  note fX = simple_function_compose[OF X, of f]
hoelzl@47694
  1424
  from X have "\<H>(X) = \<H>(f\<circ>X) + \<H>(X|f\<circ>X)" by (rule entropy_partition)
hoelzl@40859
  1425
  then show "\<H>(f \<circ> X) \<le> \<H>(X)"
hoelzl@47694
  1426
    by (auto intro: conditional_entropy_nonneg[OF X fX])
hoelzl@40859
  1427
qed
hoelzl@36624
  1428
hoelzl@40859
  1429
corollary (in information_space) entropy_of_inj:
hoelzl@41689
  1430
  assumes X: "simple_function M X" and inj: "inj_on f (X`space M)"
hoelzl@36624
  1431
  shows "\<H>(f \<circ> X) = \<H>(X)"
hoelzl@36624
  1432
proof (rule antisym)
hoelzl@40859
  1433
  show "\<H>(f \<circ> X) \<le> \<H>(X)" using entropy_data_processing[OF X] .
hoelzl@36624
  1434
next
hoelzl@41689
  1435
  have sf: "simple_function M (f \<circ> X)"
hoelzl@40859
  1436
    using X by auto
hoelzl@36624
  1437
  have "\<H>(X) = \<H>(the_inv_into (X`space M) f \<circ> (f \<circ> X))"
hoelzl@47694
  1438
    unfolding o_assoc
hoelzl@47694
  1439
    apply (subst entropy_simple_distributed[OF simple_distributedI[OF X refl]])
hoelzl@47694
  1440
    apply (subst entropy_simple_distributed[OF simple_distributedI[OF simple_function_compose[OF X]], where f="\<lambda>x. prob (X -` {x} \<inter> space M)"])
hoelzl@47694
  1441
    apply (auto intro!: setsum_cong arg_cong[where f=prob] image_eqI simp: the_inv_into_f_f[OF inj] comp_def)
hoelzl@47694
  1442
    done
hoelzl@36624
  1443
  also have "... \<le> \<H>(f \<circ> X)"
hoelzl@40859
  1444
    using entropy_data_processing[OF sf] .
hoelzl@36624
  1445
  finally show "\<H>(X) \<le> \<H>(f \<circ> X)" .
hoelzl@36624
  1446
qed
hoelzl@36624
  1447
hoelzl@36080
  1448
end