src/HOL/Probability/Information.thy
author hoelzl
Mon May 03 14:35:10 2010 +0200 (2010-05-03)
changeset 36624 25153c08655e
parent 36623 d26348b667f2
child 36649 bfd8c550faa6
permissions -rw-r--r--
Cleanup information theory
hoelzl@36080
     1
theory Information
hoelzl@36623
     2
imports Probability_Space Product_Measure "../Multivariate_Analysis/Convex"
hoelzl@36080
     3
begin
hoelzl@36080
     4
hoelzl@36624
     5
section "Convex theory"
hoelzl@36080
     6
hoelzl@36624
     7
lemma log_setsum:
hoelzl@36624
     8
  assumes "finite s" "s \<noteq> {}"
hoelzl@36624
     9
  assumes "b > 1"
hoelzl@36624
    10
  assumes "(\<Sum> i \<in> s. a i) = 1"
hoelzl@36624
    11
  assumes "\<And> i. i \<in> s \<Longrightarrow> a i \<ge> 0"
hoelzl@36624
    12
  assumes "\<And> i. i \<in> s \<Longrightarrow> y i \<in> {0 <..}"
hoelzl@36624
    13
  shows "log b (\<Sum> i \<in> s. a i * y i) \<ge> (\<Sum> i \<in> s. a i * log b (y i))"
hoelzl@36624
    14
proof -
hoelzl@36624
    15
  have "convex_on {0 <..} (\<lambda> x. - log b x)"
hoelzl@36624
    16
    by (rule minus_log_convex[OF `b > 1`])
hoelzl@36624
    17
  hence "- log b (\<Sum> i \<in> s. a i * y i) \<le> (\<Sum> i \<in> s. a i * - log b (y i))"
hoelzl@36624
    18
    using convex_on_setsum[of _ _ "\<lambda> x. - log b x"] assms pos_is_convex by fastsimp
hoelzl@36624
    19
  thus ?thesis by (auto simp add:setsum_negf le_imp_neg_le)
hoelzl@36624
    20
qed
hoelzl@36080
    21
hoelzl@36624
    22
lemma log_setsum':
hoelzl@36624
    23
  assumes "finite s" "s \<noteq> {}"
hoelzl@36624
    24
  assumes "b > 1"
hoelzl@36624
    25
  assumes "(\<Sum> i \<in> s. a i) = 1"
hoelzl@36624
    26
  assumes pos: "\<And> i. i \<in> s \<Longrightarrow> 0 \<le> a i"
hoelzl@36624
    27
          "\<And> i. \<lbrakk> i \<in> s ; 0 < a i \<rbrakk> \<Longrightarrow> 0 < y i"
hoelzl@36624
    28
  shows "log b (\<Sum> i \<in> s. a i * y i) \<ge> (\<Sum> i \<in> s. a i * log b (y i))"
hoelzl@36080
    29
proof -
hoelzl@36624
    30
  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
    31
    using assms by (auto intro!: setsum_mono_zero_cong_left)
hoelzl@36624
    32
  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
    33
  proof (rule log_setsum)
hoelzl@36624
    34
    have "setsum a (s - {i. a i = 0}) = setsum a s"
hoelzl@36624
    35
      using assms(1) by (rule setsum_mono_zero_cong_left) auto
hoelzl@36624
    36
    thus sum_1: "setsum a (s - {i. a i = 0}) = 1"
hoelzl@36624
    37
      "finite (s - {i. a i = 0})" using assms by simp_all
hoelzl@36624
    38
hoelzl@36624
    39
    show "s - {i. a i = 0} \<noteq> {}"
hoelzl@36624
    40
    proof
hoelzl@36624
    41
      assume *: "s - {i. a i = 0} = {}"
hoelzl@36624
    42
      hence "setsum a (s - {i. a i = 0}) = 0" by (simp add: * setsum_empty)
hoelzl@36624
    43
      with sum_1 show False by simp
hoelzl@36624
    44
qed
hoelzl@36624
    45
hoelzl@36624
    46
    fix i assume "i \<in> s - {i. a i = 0}"
hoelzl@36624
    47
    hence "i \<in> s" "a i \<noteq> 0" by simp_all
hoelzl@36624
    48
    thus "0 \<le> a i" "y i \<in> {0<..}" using pos[of i] by auto
hoelzl@36624
    49
  qed fact+
hoelzl@36624
    50
  ultimately show ?thesis by simp
hoelzl@36080
    51
qed
hoelzl@36080
    52
hoelzl@36624
    53
section "Information theory"
hoelzl@36624
    54
hoelzl@36624
    55
lemma (in finite_prob_space) sum_over_space_distrib:
hoelzl@36624
    56
  "(\<Sum>x\<in>X`space M. distribution X {x}) = 1"
hoelzl@36624
    57
  unfolding distribution_def prob_space[symmetric] using finite_space
hoelzl@36624
    58
  by (subst measure_finitely_additive'')
hoelzl@36624
    59
     (auto simp add: disjoint_family_on_def sets_eq_Pow intro!: arg_cong[where f=prob])
hoelzl@36624
    60
hoelzl@36624
    61
locale finite_information_space = finite_prob_space +
hoelzl@36624
    62
  fixes b :: real assumes b_gt_1: "1 < b"
hoelzl@36624
    63
hoelzl@36624
    64
definition
hoelzl@36624
    65
  "KL_divergence b M X Y =
hoelzl@36624
    66
    measure_space.integral (M\<lparr>measure := X\<rparr>)
hoelzl@36624
    67
                           (\<lambda>x. log b ((measure_space.RN_deriv (M \<lparr>measure := Y\<rparr> ) X) x))"
hoelzl@36624
    68
hoelzl@36624
    69
lemma (in finite_prob_space) distribution_mono:
hoelzl@36624
    70
  assumes "\<And>t. \<lbrakk> t \<in> space M ; X t \<in> x \<rbrakk> \<Longrightarrow> Y t \<in> y"
hoelzl@36624
    71
  shows "distribution X x \<le> distribution Y y"
hoelzl@36624
    72
  unfolding distribution_def
hoelzl@36624
    73
  using assms by (auto simp: sets_eq_Pow intro!: measure_mono)
hoelzl@36624
    74
hoelzl@36624
    75
lemma (in prob_space) distribution_remove_const:
hoelzl@36624
    76
  shows "joint_distribution X (\<lambda>x. ()) {(x, ())} = distribution X {x}"
hoelzl@36624
    77
  and "joint_distribution (\<lambda>x. ()) X {((), x)} = distribution X {x}"
hoelzl@36624
    78
  and "joint_distribution X (\<lambda>x. (Y x, ())) {(x, y, ())} = joint_distribution X Y {(x, y)}"
hoelzl@36624
    79
  and "joint_distribution X (\<lambda>x. ((), Y x)) {(x, (), y)} = joint_distribution X Y {(x, y)}"
hoelzl@36624
    80
  and "distribution (\<lambda>x. ()) {()} = 1"
hoelzl@36624
    81
  unfolding prob_space[symmetric]
hoelzl@36624
    82
  by (auto intro!: arg_cong[where f=prob] simp: distribution_def)
hoelzl@36624
    83
hoelzl@36624
    84
hoelzl@36624
    85
context finite_information_space
hoelzl@36624
    86
begin
hoelzl@36624
    87
hoelzl@36624
    88
lemma distribution_mono_gt_0:
hoelzl@36624
    89
  assumes gt_0: "0 < distribution X x"
hoelzl@36624
    90
  assumes *: "\<And>t. \<lbrakk> t \<in> space M ; X t \<in> x \<rbrakk> \<Longrightarrow> Y t \<in> y"
hoelzl@36624
    91
  shows "0 < distribution Y y"
hoelzl@36624
    92
  by (rule less_le_trans[OF gt_0 distribution_mono]) (rule *)
hoelzl@36624
    93
hoelzl@36624
    94
lemma
hoelzl@36624
    95
  assumes "0 \<le> A" and pos: "0 < A \<Longrightarrow> 0 < B" "0 < A \<Longrightarrow> 0 < C"
hoelzl@36624
    96
  shows mult_log_mult: "A * log b (B * C) = A * log b B + A * log b C" (is "?mult")
hoelzl@36624
    97
  and mult_log_divide: "A * log b (B / C) = A * log b B - A * log b C" (is "?div")
hoelzl@36080
    98
proof -
hoelzl@36624
    99
  have "?mult \<and> ?div"
hoelzl@36624
   100
proof (cases "A = 0")
hoelzl@36624
   101
  case False
hoelzl@36624
   102
  hence "0 < A" using `0 \<le> A` by auto
hoelzl@36624
   103
    with pos[OF this] show "?mult \<and> ?div" using b_gt_1
hoelzl@36624
   104
      by (auto simp: log_divide log_mult field_simps)
hoelzl@36624
   105
qed simp
hoelzl@36624
   106
  thus ?mult and ?div by auto
hoelzl@36080
   107
qed
hoelzl@36080
   108
hoelzl@36624
   109
lemma split_pairs:
hoelzl@36624
   110
  shows
hoelzl@36624
   111
    "((A, B) = X) \<longleftrightarrow> (fst X = A \<and> snd X = B)" and
hoelzl@36624
   112
    "(X = (A, B)) \<longleftrightarrow> (fst X = A \<and> snd X = B)" by auto
hoelzl@36624
   113
hoelzl@36624
   114
ML {*
hoelzl@36624
   115
hoelzl@36624
   116
  (* tactic to solve equations of the form @{term "W * log b (X / (Y * Z)) = W * log b X - W * log b (Y * Z)"}
hoelzl@36624
   117
     where @{term W} is a joint distribution of @{term X}, @{term Y}, and @{term Z}. *)
hoelzl@36624
   118
hoelzl@36624
   119
  val mult_log_intros = [@{thm mult_log_divide}, @{thm mult_log_mult}]
hoelzl@36624
   120
  val intros = [@{thm divide_pos_pos}, @{thm mult_pos_pos}, @{thm positive_distribution}]
hoelzl@36624
   121
hoelzl@36624
   122
  val distribution_gt_0_tac = (rtac @{thm distribution_mono_gt_0}
hoelzl@36624
   123
    THEN' assume_tac
hoelzl@36624
   124
    THEN' clarsimp_tac (clasimpset_of @{context} addsimps2 @{thms split_pairs}))
hoelzl@36624
   125
hoelzl@36624
   126
  val distr_mult_log_eq_tac = REPEAT_ALL_NEW (CHANGED o TRY o
hoelzl@36624
   127
    (resolve_tac (mult_log_intros @ intros)
hoelzl@36624
   128
      ORELSE' distribution_gt_0_tac
hoelzl@36624
   129
      ORELSE' clarsimp_tac (clasimpset_of @{context})))
hoelzl@36624
   130
hoelzl@36624
   131
  fun instanciate_term thy redex intro =
hoelzl@36624
   132
    let
hoelzl@36624
   133
      val intro_concl = Thm.concl_of intro
hoelzl@36624
   134
hoelzl@36624
   135
      val lhs = intro_concl |> HOLogic.dest_Trueprop |> HOLogic.dest_eq |> fst
hoelzl@36624
   136
hoelzl@36624
   137
      val m = SOME (Pattern.match thy (lhs, redex) (Vartab.empty, Vartab.empty))
hoelzl@36624
   138
        handle Pattern.MATCH => NONE
hoelzl@36624
   139
hoelzl@36624
   140
    in
hoelzl@36624
   141
      Option.map (fn m => Envir.subst_term m intro_concl) m
hoelzl@36624
   142
    end
hoelzl@36624
   143
hoelzl@36624
   144
  fun mult_log_simproc simpset redex =
hoelzl@36624
   145
  let
hoelzl@36624
   146
    val ctxt = Simplifier.the_context simpset
hoelzl@36624
   147
    val thy = ProofContext.theory_of ctxt
hoelzl@36624
   148
    fun prove (SOME thm) = (SOME
hoelzl@36624
   149
          (Goal.prove ctxt [] [] thm (K (distr_mult_log_eq_tac 1))
hoelzl@36624
   150
           |> mk_meta_eq)
hoelzl@36624
   151
            handle THM _ => NONE)
hoelzl@36624
   152
      | prove NONE = NONE
hoelzl@36624
   153
  in
hoelzl@36624
   154
    get_first (instanciate_term thy (term_of redex) #> prove) mult_log_intros
hoelzl@36624
   155
  end
hoelzl@36624
   156
*}
hoelzl@36624
   157
hoelzl@36624
   158
simproc_setup mult_log ("distribution X x * log b (A * B)" |
hoelzl@36624
   159
                        "distribution X x * log b (A / B)") = {* K mult_log_simproc *}
hoelzl@36624
   160
hoelzl@36624
   161
end
hoelzl@36624
   162
hoelzl@36624
   163
lemma KL_divergence_eq_finite:
hoelzl@36624
   164
  assumes u: "finite_measure_space (M\<lparr>measure := u\<rparr>)"
hoelzl@36624
   165
  assumes v: "finite_measure_space (M\<lparr>measure := v\<rparr>)"
hoelzl@36624
   166
  assumes u_0: "\<And>x. \<lbrakk> x \<in> space M ; v {x} = 0 \<rbrakk> \<Longrightarrow> u {x} = 0"
hoelzl@36624
   167
  shows "KL_divergence b M u v = (\<Sum>x\<in>space M. u {x} * log b (u {x} / v {x}))" (is "_ = ?sum")
hoelzl@36624
   168
proof (simp add: KL_divergence_def, subst finite_measure_space.integral_finite_singleton, simp_all add: u)
hoelzl@36624
   169
  have ms_u: "measure_space (M\<lparr>measure := u\<rparr>)"
hoelzl@36624
   170
    using u unfolding finite_measure_space_def by simp
hoelzl@36624
   171
hoelzl@36624
   172
  show "(\<Sum>x \<in> space M. log b (measure_space.RN_deriv (M\<lparr>measure := v\<rparr>) u x) * u {x}) = ?sum"
hoelzl@36624
   173
    apply (rule setsum_cong[OF refl])
hoelzl@36624
   174
    apply simp
hoelzl@36624
   175
    apply (safe intro!: arg_cong[where f="log b"] )
hoelzl@36624
   176
    apply (subst finite_measure_space.RN_deriv_finite_singleton)
hoelzl@36624
   177
    using assms ms_u by auto
hoelzl@36080
   178
qed
hoelzl@36080
   179
hoelzl@36624
   180
lemma log_setsum_divide:
hoelzl@36624
   181
  assumes "finite S" and "S \<noteq> {}" and "1 < b"
hoelzl@36624
   182
  assumes "(\<Sum>x\<in>S. g x) = 1"
hoelzl@36624
   183
  assumes pos: "\<And>x. x \<in> S \<Longrightarrow> g x \<ge> 0" "\<And>x. x \<in> S \<Longrightarrow> f x \<ge> 0"
hoelzl@36624
   184
  assumes g_pos: "\<And>x. \<lbrakk> x \<in> S ; 0 < g x \<rbrakk> \<Longrightarrow> 0 < f x"
hoelzl@36624
   185
  shows "- (\<Sum>x\<in>S. g x * log b (g x / f x)) \<le> log b (\<Sum>x\<in>S. f x)"
hoelzl@36624
   186
proof -
hoelzl@36624
   187
  have log_mono: "\<And>x y. 0 < x \<Longrightarrow> x \<le> y \<Longrightarrow> log b x \<le> log b y"
hoelzl@36624
   188
    using `1 < b` by (subst log_le_cancel_iff) auto
hoelzl@36080
   189
hoelzl@36624
   190
  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
   191
  proof (unfold setsum_negf[symmetric], rule setsum_cong)
hoelzl@36624
   192
    fix x assume x: "x \<in> S"
hoelzl@36624
   193
    show "- (g x * log b (g x / f x)) = g x * log b (f x / g x)"
hoelzl@36624
   194
    proof (cases "g x = 0")
hoelzl@36624
   195
      case False
hoelzl@36624
   196
      with pos[OF x] g_pos[OF x] have "0 < f x" "0 < g x" by simp_all
hoelzl@36624
   197
      thus ?thesis using `1 < b` by (simp add: log_divide field_simps)
hoelzl@36624
   198
    qed simp
hoelzl@36624
   199
  qed rule
hoelzl@36624
   200
  also have "... \<le> log b (\<Sum>x\<in>S. g x * (f x / g x))"
hoelzl@36624
   201
  proof (rule log_setsum')
hoelzl@36624
   202
    fix x assume x: "x \<in> S" "0 < g x"
hoelzl@36624
   203
    with g_pos[OF x] show "0 < f x / g x" by (safe intro!: divide_pos_pos)
hoelzl@36624
   204
  qed fact+
hoelzl@36624
   205
  also have "... = log b (\<Sum>x\<in>S - {x. g x = 0}. f x)" using `finite S`
hoelzl@36624
   206
    by (auto intro!: setsum_mono_zero_cong_right arg_cong[where f="log b"]
hoelzl@36624
   207
        split: split_if_asm)
hoelzl@36624
   208
  also have "... \<le> log b (\<Sum>x\<in>S. f x)"
hoelzl@36624
   209
  proof (rule log_mono)
hoelzl@36624
   210
    have "0 = (\<Sum>x\<in>S - {x. g x = 0}. 0)" by simp
hoelzl@36624
   211
    also have "... < (\<Sum>x\<in>S - {x. g x = 0}. f x)" (is "_ < ?sum")
hoelzl@36624
   212
    proof (rule setsum_strict_mono)
hoelzl@36624
   213
      show "finite (S - {x. g x = 0})" using `finite S` by simp
hoelzl@36624
   214
      show "S - {x. g x = 0} \<noteq> {}"
hoelzl@36624
   215
      proof
hoelzl@36624
   216
        assume "S - {x. g x = 0} = {}"
hoelzl@36624
   217
        hence "(\<Sum>x\<in>S. g x) = 0" by (subst setsum_0') auto
hoelzl@36624
   218
        with `(\<Sum>x\<in>S. g x) = 1` show False by simp
hoelzl@36624
   219
      qed
hoelzl@36624
   220
      fix x assume "x \<in> S - {x. g x = 0}"
hoelzl@36624
   221
      thus "0 < f x" using g_pos[of x] pos(1)[of x] by auto
hoelzl@36624
   222
    qed
hoelzl@36624
   223
    finally show "0 < ?sum" .
hoelzl@36624
   224
    show "(\<Sum>x\<in>S - {x. g x = 0}. f x) \<le> (\<Sum>x\<in>S. f x)"
hoelzl@36624
   225
      using `finite S` pos by (auto intro!: setsum_mono2)
hoelzl@36080
   226
  qed
hoelzl@36624
   227
  finally show ?thesis .
hoelzl@36080
   228
qed
hoelzl@36080
   229
hoelzl@36624
   230
lemma KL_divergence_positive_finite:
hoelzl@36624
   231
  assumes u: "finite_prob_space (M\<lparr>measure := u\<rparr>)"
hoelzl@36624
   232
  assumes v: "finite_prob_space (M\<lparr>measure := v\<rparr>)"
hoelzl@36624
   233
  assumes u_0: "\<And>x. \<lbrakk> x \<in> space M ; v {x} = 0 \<rbrakk> \<Longrightarrow> u {x} = 0"
hoelzl@36624
   234
  and "1 < b"
hoelzl@36624
   235
  shows "0 \<le> KL_divergence b M u v"
hoelzl@36624
   236
proof -
hoelzl@36624
   237
  interpret u: finite_prob_space "M\<lparr>measure := u\<rparr>" using u .
hoelzl@36624
   238
  interpret v: finite_prob_space "M\<lparr>measure := v\<rparr>" using v .
hoelzl@36080
   239
hoelzl@36624
   240
  have *: "space M \<noteq> {}" using u.not_empty by simp
hoelzl@36080
   241
hoelzl@36624
   242
  have "- (KL_divergence b M u v) \<le> log b (\<Sum>x\<in>space M. v {x})"
hoelzl@36624
   243
  proof (subst KL_divergence_eq_finite, safe intro!: log_setsum_divide *)
hoelzl@36624
   244
    show "finite_measure_space (M\<lparr>measure := u\<rparr>)"
hoelzl@36624
   245
      "finite_measure_space (M\<lparr>measure := v\<rparr>)"
hoelzl@36624
   246
       using u v unfolding finite_prob_space_eq by simp_all
hoelzl@36080
   247
hoelzl@36624
   248
     show "finite (space M)" using u.finite_space by simp
hoelzl@36624
   249
     show "1 < b" by fact
hoelzl@36624
   250
     show "(\<Sum>x\<in>space M. u {x}) = 1" using u.sum_over_space_eq_1 by simp
hoelzl@36080
   251
hoelzl@36624
   252
     fix x assume x: "x \<in> space M"
hoelzl@36624
   253
     thus pos: "0 \<le> u {x}" "0 \<le> v {x}"
hoelzl@36624
   254
       using u.positive u.sets_eq_Pow v.positive v.sets_eq_Pow by simp_all
hoelzl@36080
   255
hoelzl@36624
   256
     { assume "v {x} = 0" from u_0[OF x this] show "u {x} = 0" . }
hoelzl@36624
   257
     { assume "0 < u {x}"
hoelzl@36624
   258
       hence "v {x} \<noteq> 0" using u_0[OF x] by auto
hoelzl@36624
   259
       with pos show "0 < v {x}" by simp }
hoelzl@36080
   260
  qed
hoelzl@36624
   261
  thus "0 \<le> KL_divergence b M u v" using v.sum_over_space_eq_1 by simp
hoelzl@36080
   262
qed
hoelzl@36080
   263
hoelzl@36080
   264
definition (in prob_space)
hoelzl@36080
   265
  "mutual_information b s1 s2 X Y \<equiv>
hoelzl@36080
   266
    let prod_space =
hoelzl@36080
   267
      prod_measure_space (\<lparr>space = space s1, sets = sets s1, measure = distribution X\<rparr>)
hoelzl@36080
   268
                         (\<lparr>space = space s2, sets = sets s2, measure = distribution Y\<rparr>)
hoelzl@36080
   269
    in
hoelzl@36080
   270
      KL_divergence b prod_space (joint_distribution X Y) (measure prod_space)"
hoelzl@36080
   271
hoelzl@36624
   272
abbreviation (in finite_information_space)
hoelzl@36624
   273
  finite_mutual_information ("\<I>'(_ ; _')") where
hoelzl@36624
   274
  "\<I>(X ; Y) \<equiv> mutual_information b
hoelzl@36080
   275
    \<lparr> space = X`space M, sets = Pow (X`space M) \<rparr>
hoelzl@36080
   276
    \<lparr> space = Y`space M, sets = Pow (Y`space M) \<rparr> X Y"
hoelzl@36080
   277
hoelzl@36624
   278
lemma (in finite_measure_space) measure_spaceI: "measure_space M"
hoelzl@36624
   279
  by unfold_locales
hoelzl@36080
   280
hoelzl@36624
   281
lemma prod_measure_times_finite:
hoelzl@36624
   282
  assumes fms: "finite_measure_space M" "finite_measure_space M'" and a: "a \<in> space M \<times> space M'"
hoelzl@36624
   283
  shows "prod_measure M M' {a} = measure M {fst a} * measure M' {snd a}"
hoelzl@36624
   284
proof (cases a)
hoelzl@36624
   285
  case (Pair b c)
hoelzl@36624
   286
  hence a_eq: "{a} = {b} \<times> {c}" by simp
hoelzl@36080
   287
hoelzl@36624
   288
  with fms[THEN finite_measure_space.measure_spaceI]
hoelzl@36624
   289
    fms[THEN finite_measure_space.sets_eq_Pow] a Pair
hoelzl@36624
   290
  show ?thesis unfolding a_eq
hoelzl@36624
   291
    by (subst prod_measure_times) simp_all
hoelzl@36624
   292
qed
hoelzl@36080
   293
hoelzl@36624
   294
lemma setsum_cartesian_product':
hoelzl@36624
   295
  "(\<Sum>x\<in>A \<times> B. f x) = (\<Sum>x\<in>A. setsum (\<lambda>y. f (x, y)) B)"
hoelzl@36624
   296
  unfolding setsum_cartesian_product by simp
hoelzl@36080
   297
hoelzl@36624
   298
lemma (in finite_information_space)
hoelzl@36624
   299
  assumes MX: "finite_prob_space \<lparr> space = space MX, sets = sets MX, measure = distribution X\<rparr>"
hoelzl@36624
   300
    (is "finite_prob_space ?MX")
hoelzl@36624
   301
  assumes MY: "finite_prob_space \<lparr> space = space MY, sets = sets MY, measure = distribution Y\<rparr>"
hoelzl@36624
   302
    (is "finite_prob_space ?MY")
hoelzl@36624
   303
  and X_space: "X ` space M \<subseteq> space MX" and Y_space: "Y ` space M \<subseteq> space MY"
hoelzl@36624
   304
  shows mutual_information_eq_generic:
hoelzl@36624
   305
    "mutual_information b MX MY X Y = (\<Sum> (x,y) \<in> space MX \<times> space MY.
hoelzl@36624
   306
      joint_distribution X Y {(x,y)} *
hoelzl@36624
   307
      log b (joint_distribution X Y {(x,y)} /
hoelzl@36624
   308
      (distribution X {x} * distribution Y {y})))"
hoelzl@36624
   309
    (is "?equality")
hoelzl@36624
   310
  and mutual_information_positive_generic:
hoelzl@36624
   311
    "0 \<le> mutual_information b MX MY X Y" (is "?positive")
hoelzl@36624
   312
proof -
hoelzl@36624
   313
  let ?P = "prod_measure_space ?MX ?MY"
hoelzl@36624
   314
  let ?measure = "joint_distribution X Y"
hoelzl@36624
   315
  let ?P' = "measure_update (\<lambda>_. ?measure) ?P"
hoelzl@36080
   316
hoelzl@36624
   317
  interpret X: finite_prob_space "?MX" using MX .
hoelzl@36624
   318
  moreover interpret Y: finite_prob_space "?MY" using MY .
hoelzl@36624
   319
  ultimately have ms_X: "measure_space ?MX"
hoelzl@36624
   320
    and ms_Y: "measure_space ?MY" by unfold_locales
hoelzl@36080
   321
hoelzl@36624
   322
  have fms_P: "finite_measure_space ?P"
hoelzl@36624
   323
      by (rule finite_measure_space_finite_prod_measure) fact+
hoelzl@36624
   324
hoelzl@36624
   325
  have fms_P': "finite_measure_space ?P'"
hoelzl@36624
   326
      using finite_product_measure_space[of "space MX" "space MY"]
hoelzl@36624
   327
        X.finite_space Y.finite_space sigma_prod_sets_finite[OF X.finite_space Y.finite_space]
hoelzl@36624
   328
        X.sets_eq_Pow Y.sets_eq_Pow
hoelzl@36624
   329
      by (simp add: prod_measure_space_def)
hoelzl@36080
   330
hoelzl@36624
   331
  { fix x assume "x \<in> space ?P"
hoelzl@36624
   332
    hence x_in_MX: "{fst x} \<in> sets MX" using X.sets_eq_Pow
hoelzl@36624
   333
      by (auto simp: prod_measure_space_def)
hoelzl@36624
   334
hoelzl@36624
   335
    assume "measure ?P {x} = 0"
hoelzl@36624
   336
    with prod_measure_times[OF ms_X ms_Y, of "{fst x}" "{snd x}"] x_in_MX
hoelzl@36624
   337
    have "distribution X {fst x} = 0 \<or> distribution Y {snd x} = 0"
hoelzl@36624
   338
      by (simp add: prod_measure_space_def)
hoelzl@36624
   339
hoelzl@36624
   340
    hence "joint_distribution X Y {x} = 0"
hoelzl@36624
   341
      by (cases x) (auto simp: distribution_order) }
hoelzl@36624
   342
  note measure_0 = this
hoelzl@36080
   343
hoelzl@36624
   344
  show ?equality
hoelzl@36624
   345
    unfolding Let_def mutual_information_def using fms_P fms_P' measure_0 MX MY
hoelzl@36624
   346
    by (subst KL_divergence_eq_finite)
hoelzl@36624
   347
       (simp_all add: prod_measure_space_def prod_measure_times_finite
hoelzl@36624
   348
         finite_prob_space_eq setsum_cartesian_product')
hoelzl@36080
   349
hoelzl@36624
   350
  show ?positive
hoelzl@36624
   351
    unfolding Let_def mutual_information_def using measure_0 b_gt_1
hoelzl@36624
   352
  proof (safe intro!: KL_divergence_positive_finite, simp_all)
hoelzl@36624
   353
    from ms_X ms_Y X.top Y.top X.prob_space Y.prob_space
hoelzl@36624
   354
    have "measure ?P (space ?P) = 1"
hoelzl@36624
   355
      by (simp add: prod_measure_space_def, subst prod_measure_times, simp_all)
hoelzl@36624
   356
    with fms_P show "finite_prob_space ?P"
hoelzl@36624
   357
      by (simp add: finite_prob_space_eq)
hoelzl@36624
   358
hoelzl@36624
   359
    from ms_X ms_Y X.top Y.top X.prob_space Y.prob_space Y.not_empty X_space Y_space
hoelzl@36624
   360
    have "measure ?P' (space ?P') = 1" unfolding prob_space[symmetric]
hoelzl@36624
   361
      by (auto simp add: prod_measure_space_def distribution_def vimage_Times comp_def
hoelzl@36624
   362
        intro!: arg_cong[where f=prob])
hoelzl@36624
   363
    with fms_P' show "finite_prob_space ?P'"
hoelzl@36624
   364
      by (simp add: finite_prob_space_eq)
hoelzl@36080
   365
  qed
hoelzl@36080
   366
qed
hoelzl@36080
   367
hoelzl@36624
   368
lemma (in finite_information_space) mutual_information_eq:
hoelzl@36624
   369
  "\<I>(X;Y) = (\<Sum> (x,y) \<in> X ` space M \<times> Y ` space M.
hoelzl@36624
   370
    distribution (\<lambda>x. (X x, Y x)) {(x,y)} * log b (distribution (\<lambda>x. (X x, Y x)) {(x,y)} /
hoelzl@36624
   371
                                                   (distribution X {x} * distribution Y {y})))"
hoelzl@36624
   372
  by (subst mutual_information_eq_generic) (simp_all add: finite_prob_space_of_images)
hoelzl@36080
   373
hoelzl@36624
   374
lemma (in finite_information_space) mutual_information_positive: "0 \<le> \<I>(X;Y)"
hoelzl@36624
   375
  by (subst mutual_information_positive_generic) (simp_all add: finite_prob_space_of_images)
hoelzl@36080
   376
hoelzl@36080
   377
definition (in prob_space)
hoelzl@36080
   378
  "entropy b s X = mutual_information b s s X X"
hoelzl@36080
   379
hoelzl@36624
   380
abbreviation (in finite_information_space)
hoelzl@36624
   381
  finite_entropy ("\<H>'(_')") where
hoelzl@36624
   382
  "\<H>(X) \<equiv> entropy b \<lparr> space = X`space M, sets = Pow (X`space M) \<rparr> X"
hoelzl@36080
   383
hoelzl@36624
   384
lemma (in finite_information_space) joint_distribution_remove[simp]:
hoelzl@36624
   385
    "joint_distribution X X {(x, x)} = distribution X {x}"
hoelzl@36624
   386
  unfolding distribution_def by (auto intro!: arg_cong[where f=prob])
hoelzl@36624
   387
hoelzl@36624
   388
lemma (in finite_information_space) entropy_eq:
hoelzl@36624
   389
  "\<H>(X) = -(\<Sum> x \<in> X ` space M. distribution X {x} * log b (distribution X {x}))"
hoelzl@36080
   390
proof -
hoelzl@36624
   391
  { fix f
hoelzl@36080
   392
  { fix x y
hoelzl@36080
   393
    have "(\<lambda>x. (X x, X x)) -` {(x, y)} = (if x = y then X -` {x} else {})" by auto
hoelzl@36624
   394
      hence "distribution (\<lambda>x. (X x, X x))  {(x,y)} * f x y = (if x = y then distribution X {x} * f x y else 0)"
hoelzl@36080
   395
      unfolding distribution_def by auto }
hoelzl@36624
   396
    hence "(\<Sum>(x, y) \<in> X ` space M \<times> X ` space M. joint_distribution X X {(x, y)} * f x y) =
hoelzl@36624
   397
      (\<Sum>x \<in> X ` space M. distribution X {x} * f x x)"
hoelzl@36624
   398
      unfolding setsum_cartesian_product' by (simp add: setsum_cases finite_space) }
hoelzl@36624
   399
  note remove_cartesian_product = this
hoelzl@36624
   400
hoelzl@36624
   401
  show ?thesis
hoelzl@36624
   402
    unfolding entropy_def mutual_information_eq setsum_negf[symmetric] remove_cartesian_product
hoelzl@36624
   403
    by (auto intro!: setsum_cong)
hoelzl@36080
   404
qed
hoelzl@36080
   405
hoelzl@36624
   406
lemma (in finite_information_space) entropy_positive: "0 \<le> \<H>(X)"
hoelzl@36624
   407
  unfolding entropy_def using mutual_information_positive .
hoelzl@36080
   408
hoelzl@36080
   409
definition (in prob_space)
hoelzl@36080
   410
  "conditional_mutual_information b s1 s2 s3 X Y Z \<equiv>
hoelzl@36080
   411
    let prod_space =
hoelzl@36080
   412
      prod_measure_space \<lparr>space = space s2, sets = sets s2, measure = distribution Y\<rparr>
hoelzl@36080
   413
                         \<lparr>space = space s3, sets = sets s3, measure = distribution Z\<rparr>
hoelzl@36080
   414
    in
hoelzl@36080
   415
      mutual_information b s1 prod_space X (\<lambda>x. (Y x, Z x)) -
hoelzl@36080
   416
      mutual_information b s1 s3 X Z"
hoelzl@36080
   417
hoelzl@36624
   418
abbreviation (in finite_information_space)
hoelzl@36624
   419
  finite_conditional_mutual_information ("\<I>'( _ ; _ | _ ')") where
hoelzl@36624
   420
  "\<I>(X ; Y | Z) \<equiv> conditional_mutual_information b
hoelzl@36080
   421
    \<lparr> space = X`space M, sets = Pow (X`space M) \<rparr>
hoelzl@36080
   422
    \<lparr> space = Y`space M, sets = Pow (Y`space M) \<rparr>
hoelzl@36080
   423
    \<lparr> space = Z`space M, sets = Pow (Z`space M) \<rparr>
hoelzl@36080
   424
    X Y Z"
hoelzl@36080
   425
hoelzl@36624
   426
lemma (in finite_information_space) setsum_distribution_gen:
hoelzl@36624
   427
  assumes "Z -` {c} \<inter> space M = (\<Union>x \<in> X`space M. Y -` {f x}) \<inter> space M"
hoelzl@36624
   428
  and "inj_on f (X`space M)"
hoelzl@36624
   429
  shows "(\<Sum>x \<in> X`space M. distribution Y {f x}) = distribution Z {c}"
hoelzl@36624
   430
  unfolding distribution_def assms
hoelzl@36624
   431
  using finite_space assms
hoelzl@36624
   432
  by (subst measure_finitely_additive'')
hoelzl@36624
   433
     (auto simp add: disjoint_family_on_def sets_eq_Pow inj_on_def
hoelzl@36624
   434
      intro!: arg_cong[where f=prob])
hoelzl@36624
   435
hoelzl@36624
   436
lemma (in finite_information_space) setsum_distribution:
hoelzl@36624
   437
  "(\<Sum>x \<in> X`space M. joint_distribution X Y {(x, y)}) = distribution Y {y}"
hoelzl@36624
   438
  "(\<Sum>y \<in> Y`space M. joint_distribution X Y {(x, y)}) = distribution X {x}"
hoelzl@36624
   439
  "(\<Sum>x \<in> X`space M. joint_distribution X (\<lambda>x. (Y x, Z x)) {(x, y, z)}) = joint_distribution Y Z {(y, z)}"
hoelzl@36624
   440
  "(\<Sum>y \<in> Y`space M. joint_distribution X (\<lambda>x. (Y x, Z x)) {(x, y, z)}) = joint_distribution X Z {(x, z)}"
hoelzl@36624
   441
  "(\<Sum>z \<in> Z`space M. joint_distribution X (\<lambda>x. (Y x, Z x)) {(x, y, z)}) = joint_distribution X Y {(x, y)}"
hoelzl@36624
   442
  by (auto intro!: inj_onI setsum_distribution_gen)
hoelzl@36080
   443
hoelzl@36624
   444
lemma (in finite_information_space) conditional_mutual_information_eq_sum:
hoelzl@36624
   445
   "\<I>(X ; Y | Z) =
hoelzl@36624
   446
     (\<Sum>(x, y, z)\<in>X ` space M \<times> (\<lambda>x. (Y x, Z x)) ` space M.
hoelzl@36624
   447
             distribution (\<lambda>x. (X x, Y x, Z x)) {(x, y, z)} *
hoelzl@36624
   448
             log b (distribution (\<lambda>x. (X x, Y x, Z x)) {(x, y, z)}/
hoelzl@36624
   449
        distribution (\<lambda>x. (Y x, Z x)) {(y, z)})) -
hoelzl@36624
   450
     (\<Sum>(x, z)\<in>X ` space M \<times> Z ` space M.
hoelzl@36624
   451
        distribution (\<lambda>x. (X x, Z x)) {(x,z)} * log b (distribution (\<lambda>x. (X x, Z x)) {(x,z)} / distribution Z {z}))"
hoelzl@36624
   452
  (is "_ = ?rhs")
hoelzl@36624
   453
proof -
hoelzl@36624
   454
  have setsum_product:
hoelzl@36624
   455
    "\<And>f x. (\<Sum>v\<in>(\<lambda>x. (Y x, Z x)) ` space M. joint_distribution X (\<lambda>x. (Y x, Z x)) {(x,v)} * f v)
hoelzl@36624
   456
      = (\<Sum>v\<in>Y ` space M \<times> Z ` space M. joint_distribution X (\<lambda>x. (Y x, Z x)) {(x,v)} * f v)"
hoelzl@36624
   457
  proof (safe intro!: setsum_mono_zero_cong_left imageI)
hoelzl@36624
   458
    fix x y z f
hoelzl@36624
   459
    assume *: "(Y y, Z z) \<notin> (\<lambda>x. (Y x, Z x)) ` space M" and "y \<in> space M" "z \<in> space M"
hoelzl@36624
   460
    hence "(\<lambda>x. (X x, Y x, Z x)) -` {(x, Y y, Z z)} \<inter> space M = {}"
hoelzl@36624
   461
    proof safe
hoelzl@36624
   462
      fix x' assume x': "x' \<in> space M" and eq: "Y x' = Y y" "Z x' = Z z"
hoelzl@36624
   463
      have "(Y y, Z z) \<in> (\<lambda>x. (Y x, Z x)) ` space M" using eq[symmetric] x' by auto
hoelzl@36624
   464
      thus "x' \<in> {}" using * by auto
hoelzl@36624
   465
    qed
hoelzl@36624
   466
    thus "joint_distribution X (\<lambda>x. (Y x, Z x)) {(x, Y y, Z z)} * f (Y y) (Z z) = 0"
hoelzl@36624
   467
      unfolding distribution_def by simp
hoelzl@36624
   468
  qed (simp add: finite_space)
hoelzl@36624
   469
hoelzl@36624
   470
  thus ?thesis
hoelzl@36624
   471
    unfolding conditional_mutual_information_def Let_def mutual_information_eq
hoelzl@36624
   472
    apply (subst mutual_information_eq_generic)
hoelzl@36624
   473
    by (auto simp add: prod_measure_space_def sigma_prod_sets_finite finite_space
hoelzl@36624
   474
        finite_prob_space_of_images finite_product_prob_space_of_images
hoelzl@36624
   475
        setsum_cartesian_product' setsum_product setsum_subtractf setsum_addf
hoelzl@36624
   476
        setsum_left_distrib[symmetric] setsum_distribution
hoelzl@36624
   477
      cong: setsum_cong)
hoelzl@36080
   478
qed
hoelzl@36080
   479
hoelzl@36624
   480
lemma (in finite_information_space) conditional_mutual_information_eq:
hoelzl@36624
   481
  "\<I>(X ; Y | Z) = (\<Sum>(x, y, z) \<in> X ` space M \<times> Y ` space M \<times> Z ` space M.
hoelzl@36080
   482
             distribution (\<lambda>x. (X x, Y x, Z x)) {(x, y, z)} *
hoelzl@36080
   483
             log b (distribution (\<lambda>x. (X x, Y x, Z x)) {(x, y, z)}/
hoelzl@36624
   484
    (joint_distribution X Z {(x, z)} * joint_distribution Y Z {(y,z)} / distribution Z {z})))"
hoelzl@36624
   485
  unfolding conditional_mutual_information_def Let_def mutual_information_eq
hoelzl@36624
   486
    apply (subst mutual_information_eq_generic)
hoelzl@36624
   487
  by (auto simp add: prod_measure_space_def sigma_prod_sets_finite finite_space
hoelzl@36624
   488
      finite_prob_space_of_images finite_product_prob_space_of_images
hoelzl@36624
   489
      setsum_cartesian_product' setsum_product setsum_subtractf setsum_addf
hoelzl@36624
   490
      setsum_left_distrib[symmetric] setsum_distribution setsum_commute[where A="Y`space M"]
hoelzl@36624
   491
    cong: setsum_cong)
hoelzl@36624
   492
hoelzl@36624
   493
lemma (in finite_information_space) conditional_mutual_information_eq_mutual_information:
hoelzl@36624
   494
  "\<I>(X ; Y) = \<I>(X ; Y | (\<lambda>x. ()))"
hoelzl@36624
   495
proof -
hoelzl@36624
   496
  have [simp]: "(\<lambda>x. ()) ` space M = {()}" using not_empty by auto
hoelzl@36624
   497
hoelzl@36624
   498
  show ?thesis
hoelzl@36624
   499
    unfolding conditional_mutual_information_eq mutual_information_eq
hoelzl@36624
   500
    by (simp add: setsum_cartesian_product' distribution_remove_const)
hoelzl@36624
   501
qed
hoelzl@36624
   502
hoelzl@36624
   503
lemma (in finite_information_space) conditional_mutual_information_positive:
hoelzl@36624
   504
  "0 \<le> \<I>(X ; Y | Z)"
hoelzl@36080
   505
proof -
hoelzl@36080
   506
  let ?dXYZ = "distribution (\<lambda>x. (X x, Y x, Z x))"
hoelzl@36624
   507
  let ?dXZ = "joint_distribution X Z"
hoelzl@36624
   508
  let ?dYZ = "joint_distribution Y Z"
hoelzl@36080
   509
  let ?dX = "distribution X"
hoelzl@36080
   510
  let ?dZ = "distribution Z"
hoelzl@36624
   511
  let ?M = "X ` space M \<times> Y ` space M \<times> Z ` space M"
hoelzl@36624
   512
hoelzl@36624
   513
  have split_beta: "\<And>f. split f = (\<lambda>x. f (fst x) (snd x))" by (simp add: expand_fun_eq)
hoelzl@36080
   514
hoelzl@36624
   515
  have "- (\<Sum>(x, y, z) \<in> ?M. ?dXYZ {(x, y, z)} *
hoelzl@36624
   516
    log b (?dXYZ {(x, y, z)} / (?dXZ {(x, z)} * ?dYZ {(y,z)} / ?dZ {z})))
hoelzl@36624
   517
    \<le> log b (\<Sum>(x, y, z) \<in> ?M. ?dXZ {(x, z)} * ?dYZ {(y,z)} / ?dZ {z})"
hoelzl@36624
   518
    unfolding split_beta
hoelzl@36624
   519
  proof (rule log_setsum_divide)
hoelzl@36624
   520
    show "?M \<noteq> {}" using not_empty by simp
hoelzl@36624
   521
    show "1 < b" using b_gt_1 .
hoelzl@36080
   522
hoelzl@36624
   523
    fix x assume "x \<in> ?M"
hoelzl@36624
   524
    show "0 \<le> ?dXYZ {(fst x, fst (snd x), snd (snd x))}" using positive_distribution .
hoelzl@36624
   525
    show "0 \<le> ?dXZ {(fst x, snd (snd x))} * ?dYZ {(fst (snd x), snd (snd x))} / ?dZ {snd (snd x)}"
hoelzl@36624
   526
      by (auto intro!: mult_nonneg_nonneg positive_distribution simp: zero_le_divide_iff)
hoelzl@36080
   527
hoelzl@36624
   528
    assume *: "0 < ?dXYZ {(fst x, fst (snd x), snd (snd x))}"
hoelzl@36624
   529
    thus "0 < ?dXZ {(fst x, snd (snd x))} * ?dYZ {(fst (snd x), snd (snd x))} / ?dZ {snd (snd x)}"
hoelzl@36624
   530
      by (auto intro!: divide_pos_pos mult_pos_pos
hoelzl@36624
   531
           intro: distribution_order(6) distribution_mono_gt_0)
hoelzl@36624
   532
  qed (simp_all add: setsum_cartesian_product' sum_over_space_distrib setsum_distribution finite_space)
hoelzl@36624
   533
  also have "(\<Sum>(x, y, z) \<in> ?M. ?dXZ {(x, z)} * ?dYZ {(y,z)} / ?dZ {z}) = (\<Sum>z\<in>Z`space M. ?dZ {z})"
hoelzl@36624
   534
    apply (simp add: setsum_cartesian_product')
hoelzl@36624
   535
    apply (subst setsum_commute)
hoelzl@36624
   536
    apply (subst (2) setsum_commute)
hoelzl@36624
   537
    by (auto simp: setsum_divide_distrib[symmetric] setsum_product[symmetric] setsum_distribution
hoelzl@36624
   538
          intro!: setsum_cong)
hoelzl@36624
   539
  finally show ?thesis
hoelzl@36624
   540
    unfolding conditional_mutual_information_eq sum_over_space_distrib by simp
hoelzl@36080
   541
qed
hoelzl@36080
   542
hoelzl@36624
   543
hoelzl@36080
   544
definition (in prob_space)
hoelzl@36080
   545
  "conditional_entropy b S T X Y = conditional_mutual_information b S S T X X Y"
hoelzl@36080
   546
hoelzl@36624
   547
abbreviation (in finite_information_space)
hoelzl@36624
   548
  finite_conditional_entropy ("\<H>'(_ | _')") where
hoelzl@36624
   549
  "\<H>(X | Y) \<equiv> conditional_entropy b
hoelzl@36080
   550
    \<lparr> space = X`space M, sets = Pow (X`space M) \<rparr>
hoelzl@36080
   551
    \<lparr> space = Y`space M, sets = Pow (Y`space M) \<rparr> X Y"
hoelzl@36080
   552
hoelzl@36624
   553
lemma (in finite_information_space) conditional_entropy_positive:
hoelzl@36624
   554
  "0 \<le> \<H>(X | Y)" unfolding conditional_entropy_def using conditional_mutual_information_positive .
hoelzl@36080
   555
hoelzl@36624
   556
lemma (in finite_information_space) conditional_entropy_eq:
hoelzl@36624
   557
  "\<H>(X | Z) =
hoelzl@36080
   558
     - (\<Sum>(x, z)\<in>X ` space M \<times> Z ` space M.
hoelzl@36080
   559
         joint_distribution X Z {(x, z)} *
hoelzl@36080
   560
         log b (joint_distribution X Z {(x, z)} / distribution Z {z}))"
hoelzl@36080
   561
proof -
hoelzl@36080
   562
  have *: "\<And>x y z. (\<lambda>x. (X x, X x, Z x)) -` {(x, y, z)} = (if x = y then (\<lambda>x. (X x, Z x)) -` {(x, z)} else {})" by auto
hoelzl@36080
   563
  show ?thesis
hoelzl@36624
   564
    unfolding conditional_mutual_information_eq_sum
hoelzl@36624
   565
      conditional_entropy_def distribution_def *
hoelzl@36080
   566
    by (auto intro!: setsum_0')
hoelzl@36080
   567
qed
hoelzl@36080
   568
hoelzl@36624
   569
lemma (in finite_information_space) mutual_information_eq_entropy_conditional_entropy:
hoelzl@36624
   570
  "\<I>(X ; Z) = \<H>(X) - \<H>(X | Z)"
hoelzl@36624
   571
  unfolding mutual_information_eq entropy_eq conditional_entropy_eq
hoelzl@36080
   572
  using finite_space
hoelzl@36624
   573
  by (auto simp add: setsum_addf setsum_subtractf setsum_cartesian_product'
hoelzl@36624
   574
      setsum_left_distrib[symmetric] setsum_addf setsum_distribution)
hoelzl@36080
   575
hoelzl@36624
   576
lemma (in finite_information_space) conditional_entropy_less_eq_entropy:
hoelzl@36624
   577
  "\<H>(X | Z) \<le> \<H>(X)"
hoelzl@36624
   578
proof -
hoelzl@36624
   579
  have "\<I>(X ; Z) = \<H>(X) - \<H>(X | Z)" using mutual_information_eq_entropy_conditional_entropy .
hoelzl@36624
   580
  with mutual_information_positive[of X Z] entropy_positive[of X]
hoelzl@36624
   581
  show ?thesis by auto
hoelzl@36080
   582
qed
hoelzl@36080
   583
hoelzl@36080
   584
(* -------------Entropy of a RV with a certain event is zero---------------- *)
hoelzl@36080
   585
hoelzl@36624
   586
lemma (in finite_information_space) finite_entropy_certainty_eq_0:
hoelzl@36624
   587
  assumes "x \<in> X ` space M" and "distribution X {x} = 1"
hoelzl@36624
   588
  shows "\<H>(X) = 0"
hoelzl@36080
   589
proof -
hoelzl@36080
   590
  interpret X: finite_prob_space "\<lparr> space = X ` space M,
hoelzl@36080
   591
    sets = Pow (X ` space M),
hoelzl@36624
   592
    measure = distribution X\<rparr>" by (rule finite_prob_space_of_images)
hoelzl@36080
   593
hoelzl@36080
   594
  have "distribution X (X ` space M - {x}) = distribution X (X ` space M) - distribution X {x}"
hoelzl@36080
   595
    using X.measure_compl[of "{x}"] assms by auto
hoelzl@36080
   596
  also have "\<dots> = 0" using X.prob_space assms by auto
hoelzl@36080
   597
  finally have X0: "distribution X (X ` space M - {x}) = 0" by auto
hoelzl@36080
   598
hoelzl@36080
   599
  { fix y assume asm: "y \<noteq> x" "y \<in> X ` space M"
hoelzl@36080
   600
    hence "{y} \<subseteq> X ` space M - {x}" by auto
hoelzl@36080
   601
    from X.measure_mono[OF this] X0 X.positive[of "{y}"] asm
hoelzl@36080
   602
    have "distribution X {y} = 0" by auto }
hoelzl@36080
   603
hoelzl@36080
   604
  hence fi: "\<And> y. y \<in> X ` space M \<Longrightarrow> distribution X {y} = (if x = y then 1 else 0)"
hoelzl@36080
   605
    using assms by auto
hoelzl@36080
   606
hoelzl@36080
   607
  have y: "\<And>y. (if x = y then 1 else 0) * log b (if x = y then 1 else 0) = 0" by simp
hoelzl@36080
   608
hoelzl@36624
   609
  show ?thesis unfolding entropy_eq by (auto simp: y fi)
hoelzl@36080
   610
qed
hoelzl@36080
   611
(* --------------- upper bound on entropy for a rv ------------------------- *)
hoelzl@36080
   612
hoelzl@36624
   613
lemma (in finite_information_space) finite_entropy_le_card:
hoelzl@36624
   614
  "\<H>(X) \<le> log b (real (card (X ` space M \<inter> {x . distribution X {x} \<noteq> 0})))"
hoelzl@36080
   615
proof -
hoelzl@36080
   616
  interpret X: finite_prob_space "\<lparr>space = X ` space M,
hoelzl@36080
   617
                                    sets = Pow (X ` space M),
hoelzl@36080
   618
                                 measure = distribution X\<rparr>"
hoelzl@36624
   619
    using finite_prob_space_of_images by auto
hoelzl@36624
   620
hoelzl@36080
   621
  have triv: "\<And> x. (if distribution X {x} \<noteq> 0 then distribution X {x} else 0) = distribution X {x}"
hoelzl@36080
   622
    by auto
hoelzl@36080
   623
  hence sum1: "(\<Sum> x \<in> X ` space M \<inter> {y. distribution X {y} \<noteq> 0}. distribution X {x}) = 1"
hoelzl@36080
   624
    using X.measure_finitely_additive''[of "X ` space M" "\<lambda> x. {x}", simplified]
hoelzl@36080
   625
      sets_eq_Pow inj_singleton[unfolded inj_on_def, rule_format]
hoelzl@36080
   626
    unfolding disjoint_family_on_def  X.prob_space[symmetric]
hoelzl@36080
   627
    using finite_imageI[OF finite_space, of X] by (auto simp add:triv setsum_restrict_set)
hoelzl@36080
   628
  have pos: "\<And> x. x \<in> X ` space M \<inter> {y. distribution X {y} \<noteq> 0} \<Longrightarrow> inverse (distribution X {x}) > 0"
hoelzl@36080
   629
    using X.positive sets_eq_Pow unfolding inverse_positive_iff_positive less_le by auto
hoelzl@36080
   630
  { assume asm: "X ` space M \<inter> {y. distribution X {y} \<noteq> 0} = {}" 
hoelzl@36080
   631
    { fix x assume "x \<in> X ` space M"
hoelzl@36080
   632
      hence "distribution X {x} = 0" using asm by blast }
hoelzl@36080
   633
    hence A: "(\<Sum> x \<in> X ` space M. distribution X {x}) = 0" by auto
hoelzl@36080
   634
    have B: "(\<Sum> x \<in> X ` space M. distribution X {x})
hoelzl@36080
   635
      \<ge> (\<Sum> x \<in> X ` space M \<inter> {y. distribution X {y} \<noteq> 0}. distribution X {x})"
hoelzl@36080
   636
      using finite_imageI[OF finite_space, of X]
hoelzl@36080
   637
      by (subst setsum_mono2) auto
hoelzl@36080
   638
    from A B have "False" using sum1 by auto } note not_empty = this
hoelzl@36080
   639
  { fix x assume asm: "x \<in> X ` space M"
hoelzl@36080
   640
    have "- distribution X {x} * log b (distribution X {x})
hoelzl@36080
   641
       = - (if distribution X {x} \<noteq> 0 
hoelzl@36080
   642
            then distribution X {x} * log b (distribution X {x})
hoelzl@36080
   643
            else 0)"
hoelzl@36080
   644
      by auto
hoelzl@36080
   645
    also have "\<dots> = (if distribution X {x} \<noteq> 0 
hoelzl@36080
   646
          then distribution X {x} * - log b (distribution X {x})
hoelzl@36080
   647
          else 0)"
hoelzl@36080
   648
      by auto
hoelzl@36080
   649
    also have "\<dots> = (if distribution X {x} \<noteq> 0
hoelzl@36080
   650
                    then distribution X {x} * log b (inverse (distribution X {x}))
hoelzl@36080
   651
                    else 0)"
hoelzl@36624
   652
      using log_inverse b_gt_1 X.positive[of "{x}"] asm by auto
hoelzl@36080
   653
    finally have "- distribution X {x} * log b (distribution X {x})
hoelzl@36080
   654
                 = (if distribution X {x} \<noteq> 0 
hoelzl@36080
   655
                    then distribution X {x} * log b (inverse (distribution X {x}))
hoelzl@36080
   656
                    else 0)"
hoelzl@36080
   657
      by auto } note log_inv = this
hoelzl@36080
   658
  have "- (\<Sum> x \<in> X ` space M. distribution X {x} * log b (distribution X {x}))
hoelzl@36080
   659
       = (\<Sum> x \<in> X ` space M. (if distribution X {x} \<noteq> 0 
hoelzl@36080
   660
          then distribution X {x} * log b (inverse (distribution X {x}))
hoelzl@36080
   661
          else 0))"
hoelzl@36080
   662
    unfolding setsum_negf[symmetric] using log_inv by auto
hoelzl@36080
   663
  also have "\<dots> = (\<Sum> x \<in> X ` space M \<inter> {y. distribution X {y} \<noteq> 0}.
hoelzl@36080
   664
                          distribution X {x} * log b (inverse (distribution X {x})))"
hoelzl@36080
   665
    unfolding setsum_restrict_set[OF finite_imageI[OF finite_space, of X]] by auto
hoelzl@36080
   666
  also have "\<dots> \<le> log b (\<Sum> x \<in> X ` space M \<inter> {y. distribution X {y} \<noteq> 0}.
hoelzl@36080
   667
                          distribution X {x} * (inverse (distribution X {x})))"
hoelzl@36624
   668
    apply (subst log_setsum[OF _ _ b_gt_1 sum1, 
hoelzl@36080
   669
     unfolded greaterThan_iff, OF _ _ _]) using pos sets_eq_Pow
hoelzl@36080
   670
      X.finite_space assms X.positive not_empty by auto
hoelzl@36080
   671
  also have "\<dots> = log b (\<Sum> x \<in> X ` space M \<inter> {y. distribution X {y} \<noteq> 0}. 1)"
hoelzl@36080
   672
    by auto
hoelzl@36080
   673
  also have "\<dots> \<le> log b (real_of_nat (card (X ` space M \<inter> {y. distribution X {y} \<noteq> 0})))"
hoelzl@36080
   674
    by auto
hoelzl@36080
   675
  finally have "- (\<Sum>x\<in>X ` space M. distribution X {x} * log b (distribution X {x}))
hoelzl@36080
   676
               \<le> log b (real_of_nat (card (X ` space M \<inter> {y. distribution X {y} \<noteq> 0})))" by simp
hoelzl@36624
   677
  thus ?thesis unfolding entropy_eq real_eq_of_nat by auto
hoelzl@36080
   678
qed
hoelzl@36080
   679
hoelzl@36080
   680
(* --------------- entropy is maximal for a uniform rv --------------------- *)
hoelzl@36080
   681
hoelzl@36080
   682
lemma (in finite_prob_space) uniform_prob:
hoelzl@36080
   683
  assumes "x \<in> space M"
hoelzl@36080
   684
  assumes "\<And> x y. \<lbrakk>x \<in> space M ; y \<in> space M\<rbrakk> \<Longrightarrow> prob {x} = prob {y}"
hoelzl@36080
   685
  shows "prob {x} = 1 / real (card (space M))"
hoelzl@36080
   686
proof -
hoelzl@36080
   687
  have prob_x: "\<And> y. y \<in> space M \<Longrightarrow> prob {y} = prob {x}"
hoelzl@36080
   688
    using assms(2)[OF _ `x \<in> space M`] by blast
hoelzl@36080
   689
  have "1 = prob (space M)"
hoelzl@36080
   690
    using prob_space by auto
hoelzl@36080
   691
  also have "\<dots> = (\<Sum> x \<in> space M. prob {x})"
hoelzl@36080
   692
    using measure_finitely_additive''[of "space M" "\<lambda> x. {x}", simplified]
hoelzl@36080
   693
      sets_eq_Pow inj_singleton[unfolded inj_on_def, rule_format]
hoelzl@36080
   694
      finite_space unfolding disjoint_family_on_def  prob_space[symmetric]
hoelzl@36080
   695
    by (auto simp add:setsum_restrict_set)
hoelzl@36080
   696
  also have "\<dots> = (\<Sum> y \<in> space M. prob {x})"
hoelzl@36080
   697
    using prob_x by auto
hoelzl@36080
   698
  also have "\<dots> = real_of_nat (card (space M)) * prob {x}" by simp
hoelzl@36080
   699
  finally have one: "1 = real (card (space M)) * prob {x}"
hoelzl@36080
   700
    using real_eq_of_nat by auto
hoelzl@36080
   701
  hence two: "real (card (space M)) \<noteq> 0" by fastsimp 
hoelzl@36080
   702
  from one have three: "prob {x} \<noteq> 0" by fastsimp
hoelzl@36080
   703
  thus ?thesis using one two three divide_cancel_right
hoelzl@36080
   704
    by (auto simp:field_simps)
hoelzl@36080
   705
qed
hoelzl@36080
   706
hoelzl@36624
   707
lemma (in finite_information_space) finite_entropy_uniform_max:
hoelzl@36080
   708
  assumes "\<And>x y. \<lbrakk> x \<in> X ` space M ; y \<in> X ` space M \<rbrakk> \<Longrightarrow> distribution X {x} = distribution X {y}"
hoelzl@36624
   709
  shows "\<H>(X) = log b (real (card (X ` space M)))"
hoelzl@36080
   710
proof -
hoelzl@36080
   711
  interpret X: finite_prob_space "\<lparr>space = X ` space M,
hoelzl@36080
   712
                                    sets = Pow (X ` space M),
hoelzl@36080
   713
                                 measure = distribution X\<rparr>"
hoelzl@36624
   714
    using finite_prob_space_of_images by auto
hoelzl@36624
   715
hoelzl@36080
   716
  { fix x assume xasm: "x \<in> X ` space M"
hoelzl@36080
   717
    hence card_gt0: "real (card (X ` space M)) > 0"
hoelzl@36080
   718
      using card_gt_0_iff X.finite_space by auto
hoelzl@36080
   719
    from xasm have "\<And> y. y \<in> X ` space M \<Longrightarrow> distribution X {y} = distribution X {x}"
hoelzl@36080
   720
      using assms by blast
hoelzl@36080
   721
    hence "- (\<Sum>x\<in>X ` space M. distribution X {x} * log b (distribution X {x}))
hoelzl@36624
   722
         = - real (card (X ` space M)) * distribution X {x} * log b (distribution X {x})"
hoelzl@36624
   723
      unfolding real_eq_of_nat by auto
hoelzl@36080
   724
    also have "\<dots> = - real (card (X ` space M)) * (1 / real (card (X ` space M))) * log b (1 / real (card (X ` space M)))"
hoelzl@36624
   725
      by (auto simp: X.uniform_prob[simplified, OF xasm assms])
hoelzl@36080
   726
    also have "\<dots> = log b (real (card (X ` space M)))"
hoelzl@36080
   727
      unfolding inverse_eq_divide[symmetric]
hoelzl@36624
   728
      using card_gt0 log_inverse b_gt_1
hoelzl@36080
   729
      by (auto simp add:field_simps card_gt0)
hoelzl@36080
   730
    finally have ?thesis
hoelzl@36624
   731
      unfolding entropy_eq by auto }
hoelzl@36080
   732
  moreover
hoelzl@36080
   733
  { assume "X ` space M = {}"
hoelzl@36080
   734
    hence "distribution X (X ` space M) = 0"
hoelzl@36080
   735
      using X.empty_measure by simp
hoelzl@36080
   736
    hence "False" using X.prob_space by auto }
hoelzl@36080
   737
  ultimately show ?thesis by auto
hoelzl@36080
   738
qed
hoelzl@36080
   739
hoelzl@36624
   740
definition "subvimage A f g \<longleftrightarrow> (\<forall>x \<in> A. f -` {f x} \<inter> A \<subseteq> g -` {g x} \<inter> A)"
hoelzl@36624
   741
hoelzl@36624
   742
lemma subvimageI:
hoelzl@36624
   743
  assumes "\<And>x y. \<lbrakk> x \<in> A ; y \<in> A ; f x = f y \<rbrakk> \<Longrightarrow> g x = g y"
hoelzl@36624
   744
  shows "subvimage A f g"
hoelzl@36624
   745
  using assms unfolding subvimage_def by blast
hoelzl@36624
   746
hoelzl@36624
   747
lemma subvimageE[consumes 1]:
hoelzl@36624
   748
  assumes "subvimage A f g"
hoelzl@36624
   749
  obtains "\<And>x y. \<lbrakk> x \<in> A ; y \<in> A ; f x = f y \<rbrakk> \<Longrightarrow> g x = g y"
hoelzl@36624
   750
  using assms unfolding subvimage_def by blast
hoelzl@36624
   751
hoelzl@36624
   752
lemma subvimageD:
hoelzl@36624
   753
  "\<lbrakk> subvimage A f g ; x \<in> A ; y \<in> A ; f x = f y \<rbrakk> \<Longrightarrow> g x = g y"
hoelzl@36624
   754
  using assms unfolding subvimage_def by blast
hoelzl@36624
   755
hoelzl@36624
   756
lemma subvimage_subset:
hoelzl@36624
   757
  "\<lbrakk> subvimage B f g ; A \<subseteq> B \<rbrakk> \<Longrightarrow> subvimage A f g"
hoelzl@36624
   758
  unfolding subvimage_def by auto
hoelzl@36624
   759
hoelzl@36624
   760
lemma subvimage_idem[intro]: "subvimage A g g"
hoelzl@36624
   761
  by (safe intro!: subvimageI)
hoelzl@36624
   762
hoelzl@36624
   763
lemma subvimage_comp_finer[intro]:
hoelzl@36624
   764
  assumes svi: "subvimage A g h"
hoelzl@36624
   765
  shows "subvimage A g (f \<circ> h)"
hoelzl@36624
   766
proof (rule subvimageI, simp)
hoelzl@36624
   767
  fix x y assume "x \<in> A" "y \<in> A" "g x = g y"
hoelzl@36624
   768
  from svi[THEN subvimageD, OF this]
hoelzl@36624
   769
  show "f (h x) = f (h y)" by simp
hoelzl@36624
   770
qed
hoelzl@36624
   771
hoelzl@36624
   772
lemma subvimage_comp_gran:
hoelzl@36624
   773
  assumes svi: "subvimage A g h"
hoelzl@36624
   774
  assumes inj: "inj_on f (g ` A)"
hoelzl@36624
   775
  shows "subvimage A (f \<circ> g) h"
hoelzl@36624
   776
  by (rule subvimageI) (auto intro!: subvimageD[OF svi] simp: inj_on_iff[OF inj])
hoelzl@36624
   777
hoelzl@36624
   778
lemma subvimage_comp:
hoelzl@36624
   779
  assumes svi: "subvimage (f ` A) g h"
hoelzl@36624
   780
  shows "subvimage A (g \<circ> f) (h \<circ> f)"
hoelzl@36624
   781
  by (rule subvimageI) (auto intro!: svi[THEN subvimageD])
hoelzl@36624
   782
hoelzl@36624
   783
lemma subvimage_trans:
hoelzl@36624
   784
  assumes fg: "subvimage A f g"
hoelzl@36624
   785
  assumes gh: "subvimage A g h"
hoelzl@36624
   786
  shows "subvimage A f h"
hoelzl@36624
   787
  by (rule subvimageI) (auto intro!: fg[THEN subvimageD] gh[THEN subvimageD])
hoelzl@36624
   788
hoelzl@36624
   789
lemma subvimage_translator:
hoelzl@36624
   790
  assumes svi: "subvimage A f g"
hoelzl@36624
   791
  shows "\<exists>h. \<forall>x \<in> A. h (f x)  = g x"
hoelzl@36624
   792
proof (safe intro!: exI[of _ "\<lambda>x. (THE z. z \<in> (g ` (f -` {x} \<inter> A)))"])
hoelzl@36624
   793
  fix x assume "x \<in> A"
hoelzl@36624
   794
  show "(THE x'. x' \<in> (g ` (f -` {f x} \<inter> A))) = g x"
hoelzl@36624
   795
    by (rule theI2[of _ "g x"])
hoelzl@36624
   796
      (insert `x \<in> A`, auto intro!: svi[THEN subvimageD])
hoelzl@36624
   797
qed
hoelzl@36624
   798
hoelzl@36624
   799
lemma subvimage_translator_image:
hoelzl@36624
   800
  assumes svi: "subvimage A f g"
hoelzl@36624
   801
  shows "\<exists>h. h ` f ` A = g ` A"
hoelzl@36624
   802
proof -
hoelzl@36624
   803
  from subvimage_translator[OF svi]
hoelzl@36624
   804
  obtain h where "\<And>x. x \<in> A \<Longrightarrow> h (f x) = g x" by auto
hoelzl@36624
   805
  thus ?thesis
hoelzl@36624
   806
    by (auto intro!: exI[of _ h]
hoelzl@36624
   807
      simp: image_compose[symmetric] comp_def cong: image_cong)
hoelzl@36624
   808
qed
hoelzl@36624
   809
hoelzl@36624
   810
lemma subvimage_finite:
hoelzl@36624
   811
  assumes svi: "subvimage A f g" and fin: "finite (f`A)"
hoelzl@36624
   812
  shows "finite (g`A)"
hoelzl@36624
   813
proof -
hoelzl@36624
   814
  from subvimage_translator_image[OF svi]
hoelzl@36624
   815
  obtain h where "g`A = h`f`A" by fastsimp
hoelzl@36624
   816
  with fin show "finite (g`A)" by simp
hoelzl@36624
   817
qed
hoelzl@36624
   818
hoelzl@36624
   819
lemma subvimage_disj:
hoelzl@36624
   820
  assumes svi: "subvimage A f g"
hoelzl@36624
   821
  shows "f -` {x} \<inter> A \<subseteq> g -` {y} \<inter> A \<or>
hoelzl@36624
   822
      f -` {x} \<inter> g -` {y} \<inter> A = {}" (is "?sub \<or> ?dist")
hoelzl@36624
   823
proof (rule disjCI)
hoelzl@36624
   824
  assume "\<not> ?dist"
hoelzl@36624
   825
  then obtain z where "z \<in> A" and "x = f z" and "y = g z" by auto
hoelzl@36624
   826
  thus "?sub" using svi unfolding subvimage_def by auto
hoelzl@36624
   827
qed
hoelzl@36624
   828
hoelzl@36624
   829
lemma setsum_image_split:
hoelzl@36624
   830
  assumes svi: "subvimage A f g" and fin: "finite (f ` A)"
hoelzl@36624
   831
  shows "(\<Sum>x\<in>f`A. h x) = (\<Sum>y\<in>g`A. \<Sum>x\<in>f`(g -` {y} \<inter> A). h x)"
hoelzl@36624
   832
    (is "?lhs = ?rhs")
hoelzl@36624
   833
proof -
hoelzl@36624
   834
  have "f ` A =
hoelzl@36624
   835
      snd ` (SIGMA x : g ` A. f ` (g -` {x} \<inter> A))"
hoelzl@36624
   836
      (is "_ = snd ` ?SIGMA")
hoelzl@36624
   837
    unfolding image_split_eq_Sigma[symmetric]
hoelzl@36624
   838
    by (simp add: image_compose[symmetric] comp_def)
hoelzl@36624
   839
  moreover
hoelzl@36624
   840
  have snd_inj: "inj_on snd ?SIGMA"
hoelzl@36624
   841
    unfolding image_split_eq_Sigma[symmetric]
hoelzl@36624
   842
    by (auto intro!: inj_onI subvimageD[OF svi])
hoelzl@36624
   843
  ultimately
hoelzl@36624
   844
  have "(\<Sum>x\<in>f`A. h x) = (\<Sum>(x,y)\<in>?SIGMA. h y)"
hoelzl@36624
   845
    by (auto simp: setsum_reindex intro: setsum_cong)
hoelzl@36624
   846
  also have "... = ?rhs"
hoelzl@36624
   847
    using subvimage_finite[OF svi fin] fin
hoelzl@36624
   848
    apply (subst setsum_Sigma[symmetric])
hoelzl@36624
   849
    by (auto intro!: finite_subset[of _ "f`A"])
hoelzl@36624
   850
  finally show ?thesis .
hoelzl@36624
   851
qed
hoelzl@36624
   852
hoelzl@36624
   853
lemma (in finite_information_space) entropy_partition:
hoelzl@36624
   854
  assumes svi: "subvimage (space M) X P"
hoelzl@36624
   855
  shows "\<H>(X) = \<H>(P) + \<H>(X|P)"
hoelzl@36624
   856
proof -
hoelzl@36624
   857
  have "(\<Sum>x\<in>X ` space M. distribution X {x} * log b (distribution X {x})) =
hoelzl@36624
   858
    (\<Sum>y\<in>P `space M. \<Sum>x\<in>X ` space M.
hoelzl@36624
   859
    joint_distribution X P {(x, y)} * log b (joint_distribution X P {(x, y)}))"
hoelzl@36624
   860
  proof (subst setsum_image_split[OF svi],
hoelzl@36624
   861
      safe intro!: finite_imageI finite_space setsum_mono_zero_cong_left imageI)
hoelzl@36624
   862
    fix p x assume in_space: "p \<in> space M" "x \<in> space M"
hoelzl@36624
   863
    assume "joint_distribution X P {(X x, P p)} * log b (joint_distribution X P {(X x, P p)}) \<noteq> 0"
hoelzl@36624
   864
    hence "(\<lambda>x. (X x, P x)) -` {(X x, P p)} \<inter> space M \<noteq> {}" by (auto simp: distribution_def)
hoelzl@36624
   865
    with svi[unfolded subvimage_def, rule_format, OF `x \<in> space M`]
hoelzl@36624
   866
    show "x \<in> P -` {P p}" by auto
hoelzl@36624
   867
  next
hoelzl@36624
   868
    fix p x assume in_space: "p \<in> space M" "x \<in> space M"
hoelzl@36624
   869
    assume "P x = P p"
hoelzl@36624
   870
    from this[symmetric] svi[unfolded subvimage_def, rule_format, OF `x \<in> space M`]
hoelzl@36624
   871
    have "X -` {X x} \<inter> space M \<subseteq> P -` {P p} \<inter> space M"
hoelzl@36624
   872
      by auto
hoelzl@36624
   873
    hence "(\<lambda>x. (X x, P x)) -` {(X x, P p)} \<inter> space M = X -` {X x} \<inter> space M"
hoelzl@36624
   874
      by auto
hoelzl@36624
   875
    thus "distribution X {X x} * log b (distribution X {X x}) =
hoelzl@36624
   876
          joint_distribution X P {(X x, P p)} *
hoelzl@36624
   877
          log b (joint_distribution X P {(X x, P p)})"
hoelzl@36624
   878
      by (auto simp: distribution_def)
hoelzl@36624
   879
  qed
hoelzl@36624
   880
  thus ?thesis
hoelzl@36624
   881
  unfolding entropy_eq conditional_entropy_eq
hoelzl@36624
   882
    by (simp add: setsum_cartesian_product' setsum_subtractf setsum_distribution
hoelzl@36624
   883
      setsum_left_distrib[symmetric] setsum_commute[where B="P`space M"])
hoelzl@36624
   884
qed
hoelzl@36624
   885
hoelzl@36624
   886
corollary (in finite_information_space) entropy_data_processing:
hoelzl@36624
   887
  "\<H>(f \<circ> X) \<le> \<H>(X)"
hoelzl@36624
   888
  by (subst (2) entropy_partition[of _ "f \<circ> X"]) (auto intro: conditional_entropy_positive)
hoelzl@36624
   889
hoelzl@36624
   890
lemma (in prob_space) distribution_cong:
hoelzl@36624
   891
  assumes "\<And>x. x \<in> space M \<Longrightarrow> X x = Y x"
hoelzl@36624
   892
  shows "distribution X = distribution Y"
hoelzl@36624
   893
  unfolding distribution_def expand_fun_eq
hoelzl@36624
   894
  using assms by (auto intro!: arg_cong[where f=prob])
hoelzl@36624
   895
hoelzl@36624
   896
lemma (in prob_space) joint_distribution_cong:
hoelzl@36624
   897
  assumes "\<And>x. x \<in> space M \<Longrightarrow> X x = X' x"
hoelzl@36624
   898
  assumes "\<And>x. x \<in> space M \<Longrightarrow> Y x = Y' x"
hoelzl@36624
   899
  shows "joint_distribution X Y = joint_distribution X' Y'"
hoelzl@36624
   900
  unfolding distribution_def expand_fun_eq
hoelzl@36624
   901
  using assms by (auto intro!: arg_cong[where f=prob])
hoelzl@36624
   902
hoelzl@36624
   903
lemma image_cong:
hoelzl@36624
   904
  "\<lbrakk> \<And>x. x \<in> S \<Longrightarrow> X x = X' x \<rbrakk> \<Longrightarrow> X ` S = X' ` S"
hoelzl@36624
   905
  by (auto intro!: image_eqI)
hoelzl@36624
   906
hoelzl@36624
   907
lemma (in finite_information_space) mutual_information_cong:
hoelzl@36624
   908
  assumes X: "\<And>x. x \<in> space M \<Longrightarrow> X x = X' x"
hoelzl@36624
   909
  assumes Y: "\<And>x. x \<in> space M \<Longrightarrow> Y x = Y' x"
hoelzl@36624
   910
  shows "\<I>(X ; Y) = \<I>(X' ; Y')"
hoelzl@36624
   911
proof -
hoelzl@36624
   912
  have "X ` space M = X' ` space M" using X by (rule image_cong)
hoelzl@36624
   913
  moreover have "Y ` space M = Y' ` space M" using Y by (rule image_cong)
hoelzl@36624
   914
  ultimately show ?thesis
hoelzl@36624
   915
  unfolding mutual_information_eq
hoelzl@36624
   916
    using
hoelzl@36624
   917
      assms[THEN distribution_cong]
hoelzl@36624
   918
      joint_distribution_cong[OF assms]
hoelzl@36624
   919
    by (auto intro!: setsum_cong)
hoelzl@36624
   920
qed
hoelzl@36624
   921
hoelzl@36624
   922
corollary (in finite_information_space) entropy_of_inj:
hoelzl@36624
   923
  assumes "inj_on f (X`space M)"
hoelzl@36624
   924
  shows "\<H>(f \<circ> X) = \<H>(X)"
hoelzl@36624
   925
proof (rule antisym)
hoelzl@36624
   926
  show "\<H>(f \<circ> X) \<le> \<H>(X)" using entropy_data_processing .
hoelzl@36624
   927
next
hoelzl@36624
   928
  have "\<H>(X) = \<H>(the_inv_into (X`space M) f \<circ> (f \<circ> X))"
hoelzl@36624
   929
    by (auto intro!: mutual_information_cong simp: entropy_def the_inv_into_f_f[OF assms])
hoelzl@36624
   930
  also have "... \<le> \<H>(f \<circ> X)"
hoelzl@36624
   931
    using entropy_data_processing .
hoelzl@36624
   932
  finally show "\<H>(X) \<le> \<H>(f \<circ> X)" .
hoelzl@36624
   933
qed
hoelzl@36624
   934
hoelzl@36080
   935
end