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