src/HOL/Probability/Information.thy
author hoelzl
Tue Mar 22 18:53:05 2011 +0100 (2011-03-22)
changeset 42066 6db76c88907a
parent 41981 cdf7693bbe08
child 42067 66c8281349ec
permissions -rw-r--r--
generalized Caratheodory from algebra to ring_of_sets
     1 theory Information
     2 imports
     3   Probability_Space
     4   "~~/src/HOL/Library/Convex"
     5 begin
     6 
     7 lemma (in prob_space) not_zero_less_distribution[simp]:
     8   "(\<not> 0 < distribution X A) \<longleftrightarrow> distribution X A = 0"
     9   using distribution_positive[of X A] by arith
    10 
    11 lemma log_le: "1 < a \<Longrightarrow> 0 < x \<Longrightarrow> x \<le> y \<Longrightarrow> log a x \<le> log a y"
    12   by (subst log_le_cancel_iff) auto
    13 
    14 lemma log_less: "1 < a \<Longrightarrow> 0 < x \<Longrightarrow> x < y \<Longrightarrow> log a x < log a y"
    15   by (subst log_less_cancel_iff) auto
    16 
    17 lemma setsum_cartesian_product':
    18   "(\<Sum>x\<in>A \<times> B. f x) = (\<Sum>x\<in>A. setsum (\<lambda>y. f (x, y)) B)"
    19   unfolding setsum_cartesian_product by simp
    20 
    21 section "Convex theory"
    22 
    23 lemma log_setsum:
    24   assumes "finite s" "s \<noteq> {}"
    25   assumes "b > 1"
    26   assumes "(\<Sum> i \<in> s. a i) = 1"
    27   assumes "\<And> i. i \<in> s \<Longrightarrow> a i \<ge> 0"
    28   assumes "\<And> i. i \<in> s \<Longrightarrow> y i \<in> {0 <..}"
    29   shows "log b (\<Sum> i \<in> s. a i * y i) \<ge> (\<Sum> i \<in> s. a i * log b (y i))"
    30 proof -
    31   have "convex_on {0 <..} (\<lambda> x. - log b x)"
    32     by (rule minus_log_convex[OF `b > 1`])
    33   hence "- log b (\<Sum> i \<in> s. a i * y i) \<le> (\<Sum> i \<in> s. a i * - log b (y i))"
    34     using convex_on_setsum[of _ _ "\<lambda> x. - log b x"] assms pos_is_convex by fastsimp
    35   thus ?thesis by (auto simp add:setsum_negf le_imp_neg_le)
    36 qed
    37 
    38 lemma log_setsum':
    39   assumes "finite s" "s \<noteq> {}"
    40   assumes "b > 1"
    41   assumes "(\<Sum> i \<in> s. a i) = 1"
    42   assumes pos: "\<And> i. i \<in> s \<Longrightarrow> 0 \<le> a i"
    43           "\<And> i. \<lbrakk> i \<in> s ; 0 < a i \<rbrakk> \<Longrightarrow> 0 < y i"
    44   shows "log b (\<Sum> i \<in> s. a i * y i) \<ge> (\<Sum> i \<in> s. a i * log b (y i))"
    45 proof -
    46   have "\<And>y. (\<Sum> i \<in> s - {i. a i = 0}. a i * y i) = (\<Sum> i \<in> s. a i * y i)"
    47     using assms by (auto intro!: setsum_mono_zero_cong_left)
    48   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))"
    49   proof (rule log_setsum)
    50     have "setsum a (s - {i. a i = 0}) = setsum a s"
    51       using assms(1) by (rule setsum_mono_zero_cong_left) auto
    52     thus sum_1: "setsum a (s - {i. a i = 0}) = 1"
    53       "finite (s - {i. a i = 0})" using assms by simp_all
    54 
    55     show "s - {i. a i = 0} \<noteq> {}"
    56     proof
    57       assume *: "s - {i. a i = 0} = {}"
    58       hence "setsum a (s - {i. a i = 0}) = 0" by (simp add: * setsum_empty)
    59       with sum_1 show False by simp
    60     qed
    61 
    62     fix i assume "i \<in> s - {i. a i = 0}"
    63     hence "i \<in> s" "a i \<noteq> 0" by simp_all
    64     thus "0 \<le> a i" "y i \<in> {0<..}" using pos[of i] by auto
    65   qed fact+
    66   ultimately show ?thesis by simp
    67 qed
    68 
    69 lemma log_setsum_divide:
    70   assumes "finite S" and "S \<noteq> {}" and "1 < b"
    71   assumes "(\<Sum>x\<in>S. g x) = 1"
    72   assumes pos: "\<And>x. x \<in> S \<Longrightarrow> g x \<ge> 0" "\<And>x. x \<in> S \<Longrightarrow> f x \<ge> 0"
    73   assumes g_pos: "\<And>x. \<lbrakk> x \<in> S ; 0 < g x \<rbrakk> \<Longrightarrow> 0 < f x"
    74   shows "- (\<Sum>x\<in>S. g x * log b (g x / f x)) \<le> log b (\<Sum>x\<in>S. f x)"
    75 proof -
    76   have log_mono: "\<And>x y. 0 < x \<Longrightarrow> x \<le> y \<Longrightarrow> log b x \<le> log b y"
    77     using `1 < b` by (subst log_le_cancel_iff) auto
    78 
    79   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))"
    80   proof (unfold setsum_negf[symmetric], rule setsum_cong)
    81     fix x assume x: "x \<in> S"
    82     show "- (g x * log b (g x / f x)) = g x * log b (f x / g x)"
    83     proof (cases "g x = 0")
    84       case False
    85       with pos[OF x] g_pos[OF x] have "0 < f x" "0 < g x" by simp_all
    86       thus ?thesis using `1 < b` by (simp add: log_divide field_simps)
    87     qed simp
    88   qed rule
    89   also have "... \<le> log b (\<Sum>x\<in>S. g x * (f x / g x))"
    90   proof (rule log_setsum')
    91     fix x assume x: "x \<in> S" "0 < g x"
    92     with g_pos[OF x] show "0 < f x / g x" by (safe intro!: divide_pos_pos)
    93   qed fact+
    94   also have "... = log b (\<Sum>x\<in>S - {x. g x = 0}. f x)" using `finite S`
    95     by (auto intro!: setsum_mono_zero_cong_right arg_cong[where f="log b"]
    96         split: split_if_asm)
    97   also have "... \<le> log b (\<Sum>x\<in>S. f x)"
    98   proof (rule log_mono)
    99     have "0 = (\<Sum>x\<in>S - {x. g x = 0}. 0)" by simp
   100     also have "... < (\<Sum>x\<in>S - {x. g x = 0}. f x)" (is "_ < ?sum")
   101     proof (rule setsum_strict_mono)
   102       show "finite (S - {x. g x = 0})" using `finite S` by simp
   103       show "S - {x. g x = 0} \<noteq> {}"
   104       proof
   105         assume "S - {x. g x = 0} = {}"
   106         hence "(\<Sum>x\<in>S. g x) = 0" by (subst setsum_0') auto
   107         with `(\<Sum>x\<in>S. g x) = 1` show False by simp
   108       qed
   109       fix x assume "x \<in> S - {x. g x = 0}"
   110       thus "0 < f x" using g_pos[of x] pos(1)[of x] by auto
   111     qed
   112     finally show "0 < ?sum" .
   113     show "(\<Sum>x\<in>S - {x. g x = 0}. f x) \<le> (\<Sum>x\<in>S. f x)"
   114       using `finite S` pos by (auto intro!: setsum_mono2)
   115   qed
   116   finally show ?thesis .
   117 qed
   118 
   119 lemma split_pairs:
   120   "((A, B) = X) \<longleftrightarrow> (fst X = A \<and> snd X = B)" and
   121   "(X = (A, B)) \<longleftrightarrow> (fst X = A \<and> snd X = B)" by auto
   122 
   123 section "Information theory"
   124 
   125 locale information_space = prob_space +
   126   fixes b :: real assumes b_gt_1: "1 < b"
   127 
   128 context information_space
   129 begin
   130 
   131 text {* Introduce some simplification rules for logarithm of base @{term b}. *}
   132 
   133 lemma log_neg_const:
   134   assumes "x \<le> 0"
   135   shows "log b x = log b 0"
   136 proof -
   137   { fix u :: real
   138     have "x \<le> 0" by fact
   139     also have "0 < exp u"
   140       using exp_gt_zero .
   141     finally have "exp u \<noteq> x"
   142       by auto }
   143   then show "log b x = log b 0"
   144     by (simp add: log_def ln_def)
   145 qed
   146 
   147 lemma log_mult_eq:
   148   "log b (A * B) = (if 0 < A * B then log b \<bar>A\<bar> + log b \<bar>B\<bar> else log b 0)"
   149   using log_mult[of b "\<bar>A\<bar>" "\<bar>B\<bar>"] b_gt_1 log_neg_const[of "A * B"]
   150   by (auto simp: zero_less_mult_iff mult_le_0_iff)
   151 
   152 lemma log_inverse_eq:
   153   "log b (inverse B) = (if 0 < B then - log b B else log b 0)"
   154   using log_inverse[of b B] log_neg_const[of "inverse B"] b_gt_1 by simp
   155 
   156 lemma log_divide_eq:
   157   "log b (A / B) = (if 0 < A * B then log b \<bar>A\<bar> - log b \<bar>B\<bar> else log b 0)"
   158   unfolding divide_inverse log_mult_eq log_inverse_eq abs_inverse
   159   by (auto simp: zero_less_mult_iff mult_le_0_iff)
   160 
   161 lemmas log_simps = log_mult_eq log_inverse_eq log_divide_eq
   162 
   163 end
   164 
   165 subsection "Kullback$-$Leibler divergence"
   166 
   167 text {* The Kullback$-$Leibler divergence is also known as relative entropy or
   168 Kullback$-$Leibler distance. *}
   169 
   170 definition
   171   "KL_divergence b M \<nu> = \<integral>x. log b (real (RN_deriv M \<nu> x)) \<partial>M\<lparr>measure := \<nu>\<rparr>"
   172 
   173 lemma (in sigma_finite_measure) KL_divergence_vimage:
   174   assumes T: "T \<in> measure_preserving M M'"
   175     and T': "T' \<in> measure_preserving (M'\<lparr> measure := \<nu>' \<rparr>) (M\<lparr> measure := \<nu> \<rparr>)"
   176     and inv: "\<And>x. x \<in> space M \<Longrightarrow> T' (T x) = x"
   177     and inv': "\<And>x. x \<in> space M' \<Longrightarrow> T (T' x) = x"
   178   and \<nu>': "measure_space (M'\<lparr>measure := \<nu>'\<rparr>)" "measure_space.absolutely_continuous M' \<nu>'"
   179   and "1 < b"
   180   shows "KL_divergence b M' \<nu>' = KL_divergence b M \<nu>"
   181 proof -
   182   interpret \<nu>': measure_space "M'\<lparr>measure := \<nu>'\<rparr>" by fact
   183   have M: "measure_space (M\<lparr> measure := \<nu>\<rparr>)"
   184     by (rule \<nu>'.measure_space_vimage[OF _ T'], simp) default
   185   have "sigma_algebra (M'\<lparr> measure := \<nu>'\<rparr>)" by default
   186   then have saM': "sigma_algebra M'" by simp
   187   then interpret M': measure_space M' by (rule measure_space_vimage) fact
   188   have ac: "absolutely_continuous \<nu>" unfolding absolutely_continuous_def
   189   proof safe
   190     fix N assume N: "N \<in> sets M" and N_0: "\<mu> N = 0"
   191     then have N': "T' -` N \<inter> space M' \<in> sets M'"
   192       using T' by (auto simp: measurable_def measure_preserving_def)
   193     have "T -` (T' -` N \<inter> space M') \<inter> space M = N"
   194       using inv T N sets_into_space[OF N] by (auto simp: measurable_def measure_preserving_def)
   195     then have "measure M' (T' -` N \<inter> space M') = 0"
   196       using measure_preservingD[OF T N'] N_0 by auto
   197     with \<nu>'(2) N' show "\<nu> N = 0" using measure_preservingD[OF T', of N] N
   198       unfolding M'.absolutely_continuous_def measurable_def by auto
   199   qed
   200 
   201   have sa: "sigma_algebra (M\<lparr>measure := \<nu>\<rparr>)" by simp default
   202   have AE: "AE x. RN_deriv M' \<nu>' (T x) = RN_deriv M \<nu> x"
   203     by (rule RN_deriv_vimage[OF T T' inv \<nu>'])
   204   show ?thesis
   205     unfolding KL_divergence_def
   206   proof (subst \<nu>'.integral_vimage[OF sa T'])
   207     show "(\<lambda>x. log b (real (RN_deriv M \<nu> x))) \<in> borel_measurable (M\<lparr>measure := \<nu>\<rparr>)"
   208       by (auto intro!: RN_deriv[OF M ac] borel_measurable_log[OF _ `1 < b`])
   209     have "(\<integral> x. log b (real (RN_deriv M' \<nu>' x)) \<partial>M'\<lparr>measure := \<nu>'\<rparr>) =
   210       (\<integral> x. log b (real (RN_deriv M' \<nu>' (T (T' x)))) \<partial>M'\<lparr>measure := \<nu>'\<rparr>)" (is "?l = _")
   211       using inv' by (auto intro!: \<nu>'.integral_cong)
   212     also have "\<dots> = (\<integral> x. log b (real (RN_deriv M \<nu> (T' x))) \<partial>M'\<lparr>measure := \<nu>'\<rparr>)" (is "_ = ?r")
   213       using M ac AE
   214       by (intro \<nu>'.integral_cong_AE \<nu>'.almost_everywhere_vimage[OF sa T'] absolutely_continuous_AE[OF M])
   215          (auto elim!: AE_mp)
   216     finally show "?l = ?r" .
   217   qed
   218 qed
   219 
   220 lemma (in sigma_finite_measure) KL_divergence_cong:
   221   assumes "measure_space (M\<lparr>measure := \<nu>\<rparr>)" (is "measure_space ?\<nu>")
   222   assumes [simp]: "sets N = sets M" "space N = space M"
   223     "\<And>A. A \<in> sets M \<Longrightarrow> measure N A = \<mu> A"
   224     "\<And>A. A \<in> sets M \<Longrightarrow> \<nu> A = \<nu>' A"
   225   shows "KL_divergence b M \<nu> = KL_divergence b N \<nu>'"
   226 proof -
   227   interpret \<nu>: measure_space ?\<nu> by fact
   228   have "KL_divergence b M \<nu> = \<integral>x. log b (real (RN_deriv N \<nu>' x)) \<partial>?\<nu>"
   229     by (simp cong: RN_deriv_cong \<nu>.integral_cong add: KL_divergence_def)
   230   also have "\<dots> = KL_divergence b N \<nu>'"
   231     by (auto intro!: \<nu>.integral_cong_measure[symmetric] simp: KL_divergence_def)
   232   finally show ?thesis .
   233 qed
   234 
   235 lemma (in finite_measure_space) KL_divergence_eq_finite:
   236   assumes v: "finite_measure_space (M\<lparr>measure := \<nu>\<rparr>)"
   237   assumes ac: "absolutely_continuous \<nu>"
   238   shows "KL_divergence b M \<nu> = (\<Sum>x\<in>space M. real (\<nu> {x}) * log b (real (\<nu> {x}) / real (\<mu> {x})))" (is "_ = ?sum")
   239 proof (simp add: KL_divergence_def finite_measure_space.integral_finite_singleton[OF v])
   240   interpret v: finite_measure_space "M\<lparr>measure := \<nu>\<rparr>" by fact
   241   have ms: "measure_space (M\<lparr>measure := \<nu>\<rparr>)" by default
   242   show "(\<Sum>x \<in> space M. log b (real (RN_deriv M \<nu> x)) * real (\<nu> {x})) = ?sum"
   243     using RN_deriv_finite_measure[OF ms ac]
   244     by (auto intro!: setsum_cong simp: field_simps)
   245 qed
   246 
   247 lemma (in finite_prob_space) KL_divergence_positive_finite:
   248   assumes v: "finite_prob_space (M\<lparr>measure := \<nu>\<rparr>)"
   249   assumes ac: "absolutely_continuous \<nu>"
   250   and "1 < b"
   251   shows "0 \<le> KL_divergence b M \<nu>"
   252 proof -
   253   interpret v: finite_prob_space "M\<lparr>measure := \<nu>\<rparr>" by fact
   254   have ms: "finite_measure_space (M\<lparr>measure := \<nu>\<rparr>)" by default
   255 
   256   have "- (KL_divergence b M \<nu>) \<le> log b (\<Sum>x\<in>space M. real (\<mu> {x}))"
   257   proof (subst KL_divergence_eq_finite[OF ms ac], safe intro!: log_setsum_divide not_empty)
   258     show "finite (space M)" using finite_space by simp
   259     show "1 < b" by fact
   260     show "(\<Sum>x\<in>space M. real (\<nu> {x})) = 1"
   261       using v.finite_sum_over_space_eq_1 by (simp add: v.\<mu>'_def)
   262 
   263     fix x assume "x \<in> space M"
   264     then have x: "{x} \<in> sets M" unfolding sets_eq_Pow by auto
   265     { assume "0 < real (\<nu> {x})"
   266       then have "\<nu> {x} \<noteq> 0" by auto
   267       then have "\<mu> {x} \<noteq> 0"
   268         using ac[unfolded absolutely_continuous_def, THEN bspec, of "{x}"] x by auto
   269       thus "0 < real (\<mu> {x})" using real_measure[OF x] by auto }
   270     show "0 \<le> real (\<mu> {x})" "0 \<le> real (\<nu> {x})"
   271       using real_measure[OF x] v.real_measure[of "{x}"] x by auto
   272   qed
   273   thus "0 \<le> KL_divergence b M \<nu>" using finite_sum_over_space_eq_1 by (simp add: \<mu>'_def)
   274 qed
   275 
   276 subsection {* Mutual Information *}
   277 
   278 definition (in prob_space)
   279   "mutual_information b S T X Y =
   280     KL_divergence b (S\<lparr>measure := extreal\<circ>distribution X\<rparr> \<Otimes>\<^isub>M T\<lparr>measure := extreal\<circ>distribution Y\<rparr>)
   281       (extreal\<circ>joint_distribution X Y)"
   282 
   283 definition (in prob_space)
   284   "entropy b s X = mutual_information b s s X X"
   285 
   286 abbreviation (in information_space)
   287   mutual_information_Pow ("\<I>'(_ ; _')") where
   288   "\<I>(X ; Y) \<equiv> mutual_information b
   289     \<lparr> space = X`space M, sets = Pow (X`space M), measure = extreal\<circ>distribution X \<rparr>
   290     \<lparr> space = Y`space M, sets = Pow (Y`space M), measure = extreal\<circ>distribution Y \<rparr> X Y"
   291 
   292 lemma (in prob_space) finite_variables_absolutely_continuous:
   293   assumes X: "finite_random_variable S X" and Y: "finite_random_variable T Y"
   294   shows "measure_space.absolutely_continuous
   295     (S\<lparr>measure := extreal\<circ>distribution X\<rparr> \<Otimes>\<^isub>M T\<lparr>measure := extreal\<circ>distribution Y\<rparr>)
   296     (extreal\<circ>joint_distribution X Y)"
   297 proof -
   298   interpret X: finite_prob_space "S\<lparr>measure := extreal\<circ>distribution X\<rparr>"
   299     using X by (rule distribution_finite_prob_space)
   300   interpret Y: finite_prob_space "T\<lparr>measure := extreal\<circ>distribution Y\<rparr>"
   301     using Y by (rule distribution_finite_prob_space)
   302   interpret XY: pair_finite_prob_space
   303     "S\<lparr>measure := extreal\<circ>distribution X\<rparr>" "T\<lparr> measure := extreal\<circ>distribution Y\<rparr>" by default
   304   interpret P: finite_prob_space "XY.P\<lparr> measure := extreal\<circ>joint_distribution X Y\<rparr>"
   305     using assms by (auto intro!: joint_distribution_finite_prob_space)
   306   note rv = assms[THEN finite_random_variableD]
   307   show "XY.absolutely_continuous (extreal\<circ>joint_distribution X Y)"
   308   proof (rule XY.absolutely_continuousI)
   309     show "finite_measure_space (XY.P\<lparr> measure := extreal\<circ>joint_distribution X Y\<rparr>)" by default
   310     fix x assume "x \<in> space XY.P" and "XY.\<mu> {x} = 0"
   311     then obtain a b where "x = (a, b)"
   312       and "distribution X {a} = 0 \<or> distribution Y {b} = 0"
   313       by (cases x) (auto simp: space_pair_measure)
   314     with finite_distribution_order(5,6)[OF X Y]
   315     show "(extreal \<circ> joint_distribution X Y) {x} = 0" by auto
   316   qed
   317 qed
   318 
   319 lemma (in information_space)
   320   assumes MX: "finite_random_variable MX X"
   321   assumes MY: "finite_random_variable MY Y"
   322   shows mutual_information_generic_eq:
   323     "mutual_information b MX MY X Y = (\<Sum> (x,y) \<in> space MX \<times> space MY.
   324       joint_distribution X Y {(x,y)} *
   325       log b (joint_distribution X Y {(x,y)} /
   326       (distribution X {x} * distribution Y {y})))"
   327     (is ?sum)
   328   and mutual_information_positive_generic:
   329      "0 \<le> mutual_information b MX MY X Y" (is ?positive)
   330 proof -
   331   interpret X: finite_prob_space "MX\<lparr>measure := extreal\<circ>distribution X\<rparr>"
   332     using MX by (rule distribution_finite_prob_space)
   333   interpret Y: finite_prob_space "MY\<lparr>measure := extreal\<circ>distribution Y\<rparr>"
   334     using MY by (rule distribution_finite_prob_space)
   335   interpret XY: pair_finite_prob_space "MX\<lparr>measure := extreal\<circ>distribution X\<rparr>" "MY\<lparr>measure := extreal\<circ>distribution Y\<rparr>" by default
   336   interpret P: finite_prob_space "XY.P\<lparr>measure := extreal\<circ>joint_distribution X Y\<rparr>"
   337     using assms by (auto intro!: joint_distribution_finite_prob_space)
   338 
   339   have P_ms: "finite_measure_space (XY.P\<lparr>measure := extreal\<circ>joint_distribution X Y\<rparr>)" by default
   340   have P_ps: "finite_prob_space (XY.P\<lparr>measure := extreal\<circ>joint_distribution X Y\<rparr>)" by default
   341 
   342   show ?sum
   343     unfolding Let_def mutual_information_def
   344     by (subst XY.KL_divergence_eq_finite[OF P_ms finite_variables_absolutely_continuous[OF MX MY]])
   345        (auto simp add: space_pair_measure setsum_cartesian_product')
   346 
   347   show ?positive
   348     using XY.KL_divergence_positive_finite[OF P_ps finite_variables_absolutely_continuous[OF MX MY] b_gt_1]
   349     unfolding mutual_information_def .
   350 qed
   351 
   352 lemma (in information_space) mutual_information_commute_generic:
   353   assumes X: "random_variable S X" and Y: "random_variable T Y"
   354   assumes ac: "measure_space.absolutely_continuous
   355     (S\<lparr>measure := extreal\<circ>distribution X\<rparr> \<Otimes>\<^isub>M T\<lparr>measure := extreal\<circ>distribution Y\<rparr>) (extreal\<circ>joint_distribution X Y)"
   356   shows "mutual_information b S T X Y = mutual_information b T S Y X"
   357 proof -
   358   let ?S = "S\<lparr>measure := extreal\<circ>distribution X\<rparr>" and ?T = "T\<lparr>measure := extreal\<circ>distribution Y\<rparr>"
   359   interpret S: prob_space ?S using X by (rule distribution_prob_space)
   360   interpret T: prob_space ?T using Y by (rule distribution_prob_space)
   361   interpret P: pair_prob_space ?S ?T ..
   362   interpret Q: pair_prob_space ?T ?S ..
   363   show ?thesis
   364     unfolding mutual_information_def
   365   proof (intro Q.KL_divergence_vimage[OF Q.measure_preserving_swap _ _ _ _ ac b_gt_1])
   366     show "(\<lambda>(x,y). (y,x)) \<in> measure_preserving
   367       (P.P \<lparr> measure := extreal\<circ>joint_distribution X Y\<rparr>) (Q.P \<lparr> measure := extreal\<circ>joint_distribution Y X\<rparr>)"
   368       using X Y unfolding measurable_def
   369       unfolding measure_preserving_def using P.pair_sigma_algebra_swap_measurable
   370       by (auto simp add: space_pair_measure distribution_def intro!: arg_cong[where f=\<mu>'])
   371     have "prob_space (P.P\<lparr> measure := extreal\<circ>joint_distribution X Y\<rparr>)"
   372       using X Y by (auto intro!: distribution_prob_space random_variable_pairI)
   373     then show "measure_space (P.P\<lparr> measure := extreal\<circ>joint_distribution X Y\<rparr>)"
   374       unfolding prob_space_def by simp
   375   qed auto
   376 qed
   377 
   378 lemma (in information_space) mutual_information_commute:
   379   assumes X: "finite_random_variable S X" and Y: "finite_random_variable T Y"
   380   shows "mutual_information b S T X Y = mutual_information b T S Y X"
   381   unfolding mutual_information_generic_eq[OF X Y] mutual_information_generic_eq[OF Y X]
   382   unfolding joint_distribution_commute_singleton[of X Y]
   383   by (auto simp add: ac_simps intro!: setsum_reindex_cong[OF swap_inj_on])
   384 
   385 lemma (in information_space) mutual_information_commute_simple:
   386   assumes X: "simple_function M X" and Y: "simple_function M Y"
   387   shows "\<I>(X;Y) = \<I>(Y;X)"
   388   by (intro mutual_information_commute X Y simple_function_imp_finite_random_variable)
   389 
   390 lemma (in information_space) mutual_information_eq:
   391   assumes "simple_function M X" "simple_function M Y"
   392   shows "\<I>(X;Y) = (\<Sum> (x,y) \<in> X ` space M \<times> Y ` space M.
   393     distribution (\<lambda>x. (X x, Y x)) {(x,y)} * log b (distribution (\<lambda>x. (X x, Y x)) {(x,y)} /
   394                                                    (distribution X {x} * distribution Y {y})))"
   395   using assms by (simp add: mutual_information_generic_eq)
   396 
   397 lemma (in information_space) mutual_information_generic_cong:
   398   assumes X: "\<And>x. x \<in> space M \<Longrightarrow> X x = X' x"
   399   assumes Y: "\<And>x. x \<in> space M \<Longrightarrow> Y x = Y' x"
   400   shows "mutual_information b MX MY X Y = mutual_information b MX MY X' Y'"
   401   unfolding mutual_information_def using X Y
   402   by (simp cong: distribution_cong)
   403 
   404 lemma (in information_space) mutual_information_cong:
   405   assumes X: "\<And>x. x \<in> space M \<Longrightarrow> X x = X' x"
   406   assumes Y: "\<And>x. x \<in> space M \<Longrightarrow> Y x = Y' x"
   407   shows "\<I>(X; Y) = \<I>(X'; Y')"
   408   unfolding mutual_information_def using X Y
   409   by (simp cong: distribution_cong image_cong)
   410 
   411 lemma (in information_space) mutual_information_positive:
   412   assumes "simple_function M X" "simple_function M Y"
   413   shows "0 \<le> \<I>(X;Y)"
   414   using assms by (simp add: mutual_information_positive_generic)
   415 
   416 subsection {* Entropy *}
   417 
   418 abbreviation (in information_space)
   419   entropy_Pow ("\<H>'(_')") where
   420   "\<H>(X) \<equiv> entropy b \<lparr> space = X`space M, sets = Pow (X`space M), measure = extreal\<circ>distribution X \<rparr> X"
   421 
   422 lemma (in information_space) entropy_generic_eq:
   423   fixes X :: "'a \<Rightarrow> 'c"
   424   assumes MX: "finite_random_variable MX X"
   425   shows "entropy b MX X = -(\<Sum> x \<in> space MX. distribution X {x} * log b (distribution X {x}))"
   426 proof -
   427   interpret MX: finite_prob_space "MX\<lparr>measure := extreal\<circ>distribution X\<rparr>"
   428     using MX by (rule distribution_finite_prob_space)
   429   let "?X x" = "distribution X {x}"
   430   let "?XX x y" = "joint_distribution X X {(x, y)}"
   431 
   432   { fix x y :: 'c
   433     { assume "x \<noteq> y"
   434       then have "(\<lambda>x. (X x, X x)) -` {(x,y)} \<inter> space M = {}" by auto
   435       then have "joint_distribution X X {(x, y)} = 0" by (simp add: distribution_def) }
   436     then have "?XX x y * log b (?XX x y / (?X x * ?X y)) =
   437         (if x = y then - ?X y * log b (?X y) else 0)"
   438       by (auto simp: log_simps zero_less_mult_iff) }
   439   note remove_XX = this
   440 
   441   show ?thesis
   442     unfolding entropy_def mutual_information_generic_eq[OF MX MX]
   443     unfolding setsum_cartesian_product[symmetric] setsum_negf[symmetric] remove_XX
   444     using MX.finite_space by (auto simp: setsum_cases)
   445 qed
   446 
   447 lemma (in information_space) entropy_eq:
   448   assumes "simple_function M X"
   449   shows "\<H>(X) = -(\<Sum> x \<in> X ` space M. distribution X {x} * log b (distribution X {x}))"
   450   using assms by (simp add: entropy_generic_eq)
   451 
   452 lemma (in information_space) entropy_positive:
   453   "simple_function M X \<Longrightarrow> 0 \<le> \<H>(X)"
   454   unfolding entropy_def by (simp add: mutual_information_positive)
   455 
   456 lemma (in information_space) entropy_certainty_eq_0:
   457   assumes X: "simple_function M X" and "x \<in> X ` space M" and "distribution X {x} = 1"
   458   shows "\<H>(X) = 0"
   459 proof -
   460   let ?X = "\<lparr> space = X ` space M, sets = Pow (X ` space M), measure = extreal\<circ>distribution X\<rparr>"
   461   note simple_function_imp_finite_random_variable[OF `simple_function M X`]
   462   from distribution_finite_prob_space[OF this, of "\<lparr> measure = extreal\<circ>distribution X \<rparr>"]
   463   interpret X: finite_prob_space ?X by simp
   464   have "distribution X (X ` space M - {x}) = distribution X (X ` space M) - distribution X {x}"
   465     using X.measure_compl[of "{x}"] assms by auto
   466   also have "\<dots> = 0" using X.prob_space assms by auto
   467   finally have X0: "distribution X (X ` space M - {x}) = 0" by auto
   468   { fix y assume *: "y \<in> X ` space M"
   469     { assume asm: "y \<noteq> x"
   470       with * have "{y} \<subseteq> X ` space M - {x}" by auto
   471       from X.measure_mono[OF this] X0 asm *
   472       have "distribution X {y} = 0"  by (auto intro: antisym) }
   473     then have "distribution X {y} = (if x = y then 1 else 0)"
   474       using assms by auto }
   475   note fi = this
   476   have y: "\<And>y. (if x = y then 1 else 0) * log b (if x = y then 1 else 0) = 0" by simp
   477   show ?thesis unfolding entropy_eq[OF `simple_function M X`] by (auto simp: y fi)
   478 qed
   479 
   480 lemma (in information_space) entropy_le_card_not_0:
   481   assumes X: "simple_function M X"
   482   shows "\<H>(X) \<le> log b (card (X ` space M \<inter> {x. distribution X {x} \<noteq> 0}))"
   483 proof -
   484   let "?p x" = "distribution X {x}"
   485   have "\<H>(X) = (\<Sum>x\<in>X`space M. ?p x * log b (1 / ?p x))"
   486     unfolding entropy_eq[OF X] setsum_negf[symmetric]
   487     by (auto intro!: setsum_cong simp: log_simps)
   488   also have "\<dots> \<le> log b (\<Sum>x\<in>X`space M. ?p x * (1 / ?p x))"
   489     using not_empty b_gt_1 `simple_function M X` sum_over_space_real_distribution[OF X]
   490     by (intro log_setsum') (auto simp: simple_function_def)
   491   also have "\<dots> = log b (\<Sum>x\<in>X`space M. if ?p x \<noteq> 0 then 1 else 0)"
   492     by (intro arg_cong[where f="\<lambda>X. log b X"] setsum_cong) auto
   493   finally show ?thesis
   494     using `simple_function M X` by (auto simp: setsum_cases real_eq_of_nat simple_function_def)
   495 qed
   496 
   497 lemma (in prob_space) measure'_translate:
   498   assumes X: "random_variable S X" and A: "A \<in> sets S"
   499   shows "finite_measure.\<mu>' (S\<lparr> measure := extreal\<circ>distribution X \<rparr>) A = distribution X A"
   500 proof -
   501   interpret S: prob_space "S\<lparr> measure := extreal\<circ>distribution X \<rparr>"
   502     using distribution_prob_space[OF X] .
   503   from A show "S.\<mu>' A = distribution X A"
   504     unfolding S.\<mu>'_def by (simp add: distribution_def_raw \<mu>'_def)
   505 qed
   506 
   507 lemma (in information_space) entropy_uniform_max:
   508   assumes X: "simple_function M X"
   509   assumes "\<And>x y. \<lbrakk> x \<in> X ` space M ; y \<in> X ` space M \<rbrakk> \<Longrightarrow> distribution X {x} = distribution X {y}"
   510   shows "\<H>(X) = log b (real (card (X ` space M)))"
   511 proof -
   512   let ?X = "\<lparr> space = X ` space M, sets = Pow (X ` space M), measure = undefined\<rparr>\<lparr> measure := extreal\<circ>distribution X\<rparr>"
   513   note frv = simple_function_imp_finite_random_variable[OF X]
   514   from distribution_finite_prob_space[OF this, of "\<lparr> measure = extreal\<circ>distribution X \<rparr>"]
   515   interpret X: finite_prob_space ?X by simp
   516   note rv = finite_random_variableD[OF frv]
   517   have card_gt0: "0 < card (X ` space M)" unfolding card_gt_0_iff
   518     using `simple_function M X` not_empty by (auto simp: simple_function_def)
   519   { fix x assume "x \<in> space ?X"
   520     moreover then have "X.\<mu>' {x} = 1 / card (space ?X)"
   521     proof (rule X.uniform_prob)
   522       fix x y assume "x \<in> space ?X" "y \<in> space ?X"
   523       with assms(2)[of x y] show "X.\<mu>' {x} = X.\<mu>' {y}"
   524         by (subst (1 2) measure'_translate[OF rv]) auto
   525     qed
   526     ultimately have "distribution X {x} = 1 / card (space ?X)"
   527       by (subst (asm) measure'_translate[OF rv]) auto }
   528   thus ?thesis
   529     using not_empty X.finite_space b_gt_1 card_gt0
   530     by (simp add: entropy_eq[OF `simple_function M X`] real_eq_of_nat[symmetric] log_simps)
   531 qed
   532 
   533 lemma (in information_space) entropy_le_card:
   534   assumes "simple_function M X"
   535   shows "\<H>(X) \<le> log b (real (card (X ` space M)))"
   536 proof cases
   537   assume "X ` space M \<inter> {x. distribution X {x} \<noteq> 0} = {}"
   538   then have "\<And>x. x\<in>X`space M \<Longrightarrow> distribution X {x} = 0" by auto
   539   moreover
   540   have "0 < card (X`space M)"
   541     using `simple_function M X` not_empty
   542     by (auto simp: card_gt_0_iff simple_function_def)
   543   then have "log b 1 \<le> log b (real (card (X`space M)))"
   544     using b_gt_1 by (intro log_le) auto
   545   ultimately show ?thesis using assms by (simp add: entropy_eq)
   546 next
   547   assume False: "X ` space M \<inter> {x. distribution X {x} \<noteq> 0} \<noteq> {}"
   548   have "card (X ` space M \<inter> {x. distribution X {x} \<noteq> 0}) \<le> card (X ` space M)"
   549     (is "?A \<le> ?B") using assms not_empty by (auto intro!: card_mono simp: simple_function_def)
   550   note entropy_le_card_not_0[OF assms]
   551   also have "log b (real ?A) \<le> log b (real ?B)"
   552     using b_gt_1 False not_empty `?A \<le> ?B` assms
   553     by (auto intro!: log_le simp: card_gt_0_iff simp: simple_function_def)
   554   finally show ?thesis .
   555 qed
   556 
   557 lemma (in information_space) entropy_commute:
   558   assumes "simple_function M X" "simple_function M Y"
   559   shows "\<H>(\<lambda>x. (X x, Y x)) = \<H>(\<lambda>x. (Y x, X x))"
   560 proof -
   561   have sf: "simple_function M (\<lambda>x. (X x, Y x))" "simple_function M (\<lambda>x. (Y x, X x))"
   562     using assms by (auto intro: simple_function_Pair)
   563   have *: "(\<lambda>x. (Y x, X x))`space M = (\<lambda>(a,b). (b,a))`(\<lambda>x. (X x, Y x))`space M"
   564     by auto
   565   have inj: "\<And>X. inj_on (\<lambda>(a,b). (b,a)) X"
   566     by (auto intro!: inj_onI)
   567   show ?thesis
   568     unfolding sf[THEN entropy_eq] unfolding * setsum_reindex[OF inj]
   569     by (simp add: joint_distribution_commute[of Y X] split_beta)
   570 qed
   571 
   572 lemma (in information_space) entropy_eq_cartesian_product:
   573   assumes "simple_function M X" "simple_function M Y"
   574   shows "\<H>(\<lambda>x. (X x, Y x)) = -(\<Sum>x\<in>X`space M. \<Sum>y\<in>Y`space M.
   575     joint_distribution X Y {(x,y)} * log b (joint_distribution X Y {(x,y)}))"
   576 proof -
   577   have sf: "simple_function M (\<lambda>x. (X x, Y x))"
   578     using assms by (auto intro: simple_function_Pair)
   579   { fix x assume "x\<notin>(\<lambda>x. (X x, Y x))`space M"
   580     then have "(\<lambda>x. (X x, Y x)) -` {x} \<inter> space M = {}" by auto
   581     then have "joint_distribution X Y {x} = 0"
   582       unfolding distribution_def by auto }
   583   then show ?thesis using sf assms
   584     unfolding entropy_eq[OF sf] neg_equal_iff_equal setsum_cartesian_product
   585     by (auto intro!: setsum_mono_zero_cong_left simp: simple_function_def)
   586 qed
   587 
   588 subsection {* Conditional Mutual Information *}
   589 
   590 definition (in prob_space)
   591   "conditional_mutual_information b MX MY MZ X Y Z \<equiv>
   592     mutual_information b MX (MY \<Otimes>\<^isub>M MZ) X (\<lambda>x. (Y x, Z x)) -
   593     mutual_information b MX MZ X Z"
   594 
   595 abbreviation (in information_space)
   596   conditional_mutual_information_Pow ("\<I>'( _ ; _ | _ ')") where
   597   "\<I>(X ; Y | Z) \<equiv> conditional_mutual_information b
   598     \<lparr> space = X`space M, sets = Pow (X`space M), measure = extreal\<circ>distribution X \<rparr>
   599     \<lparr> space = Y`space M, sets = Pow (Y`space M), measure = extreal\<circ>distribution Y \<rparr>
   600     \<lparr> space = Z`space M, sets = Pow (Z`space M), measure = extreal\<circ>distribution Z \<rparr>
   601     X Y Z"
   602 
   603 lemma (in information_space) conditional_mutual_information_generic_eq:
   604   assumes MX: "finite_random_variable MX X"
   605     and MY: "finite_random_variable MY Y"
   606     and MZ: "finite_random_variable MZ Z"
   607   shows "conditional_mutual_information b MX MY MZ X Y Z = (\<Sum>(x, y, z) \<in> space MX \<times> space MY \<times> space MZ.
   608              distribution (\<lambda>x. (X x, Y x, Z x)) {(x, y, z)} *
   609              log b (distribution (\<lambda>x. (X x, Y x, Z x)) {(x, y, z)} /
   610     (joint_distribution X Z {(x, z)} * (joint_distribution Y Z {(y,z)} / distribution Z {z}))))"
   611   (is "_ = (\<Sum>(x, y, z)\<in>?S. ?XYZ x y z * log b (?XYZ x y z / (?XZ x z * (?YZ y z / ?Z z))))")
   612 proof -
   613   let ?X = "\<lambda>x. distribution X {x}"
   614   note finite_var = MX MY MZ
   615   note YZ = finite_random_variable_pairI[OF finite_var(2,3)]
   616   note XYZ = finite_random_variable_pairI[OF MX YZ]
   617   note XZ = finite_random_variable_pairI[OF finite_var(1,3)]
   618   note ZX = finite_random_variable_pairI[OF finite_var(3,1)]
   619   note YZX = finite_random_variable_pairI[OF finite_var(2) ZX]
   620   note order1 =
   621     finite_distribution_order(5,6)[OF finite_var(1) YZ]
   622     finite_distribution_order(5,6)[OF finite_var(1,3)]
   623 
   624   note random_var = finite_var[THEN finite_random_variableD]
   625   note finite = finite_var(1) YZ finite_var(3) XZ YZX
   626 
   627   have order2: "\<And>x y z. \<lbrakk>x \<in> space MX; y \<in> space MY; z \<in> space MZ; joint_distribution X Z {(x, z)} = 0\<rbrakk>
   628           \<Longrightarrow> joint_distribution X (\<lambda>x. (Y x, Z x)) {(x, y, z)} = 0"
   629     unfolding joint_distribution_commute_singleton[of X]
   630     unfolding joint_distribution_assoc_singleton[symmetric]
   631     using finite_distribution_order(6)[OF finite_var(2) ZX]
   632     by auto
   633 
   634   have "(\<Sum>(x, y, z)\<in>?S. ?XYZ x y z * log b (?XYZ x y z / (?XZ x z * (?YZ y z / ?Z z)))) =
   635     (\<Sum>(x, y, z)\<in>?S. ?XYZ x y z * (log b (?XYZ x y z / (?X x * ?YZ y z)) - log b (?XZ x z / (?X x * ?Z z))))"
   636     (is "(\<Sum>(x, y, z)\<in>?S. ?L x y z) = (\<Sum>(x, y, z)\<in>?S. ?R x y z)")
   637   proof (safe intro!: setsum_cong)
   638     fix x y z assume space: "x \<in> space MX" "y \<in> space MY" "z \<in> space MZ"
   639     show "?L x y z = ?R x y z"
   640     proof cases
   641       assume "?XYZ x y z \<noteq> 0"
   642       with space have "0 < ?X x" "0 < ?Z z" "0 < ?XZ x z" "0 < ?YZ y z" "0 < ?XYZ x y z"
   643         using order1 order2 by (auto simp: less_le)
   644       with b_gt_1 show ?thesis
   645         by (simp add: log_mult log_divide zero_less_mult_iff zero_less_divide_iff)
   646     qed simp
   647   qed
   648   also have "\<dots> = (\<Sum>(x, y, z)\<in>?S. ?XYZ x y z * log b (?XYZ x y z / (?X x * ?YZ y z))) -
   649                   (\<Sum>(x, y, z)\<in>?S. ?XYZ x y z * log b (?XZ x z / (?X x * ?Z z)))"
   650     by (auto simp add: setsum_subtractf[symmetric] field_simps intro!: setsum_cong)
   651   also have "(\<Sum>(x, y, z)\<in>?S. ?XYZ x y z * log b (?XZ x z / (?X x * ?Z z))) =
   652              (\<Sum>(x, z)\<in>space MX \<times> space MZ. ?XZ x z * log b (?XZ x z / (?X x * ?Z z)))"
   653     unfolding setsum_cartesian_product[symmetric] setsum_commute[of _ _ "space MY"]
   654               setsum_left_distrib[symmetric]
   655     unfolding joint_distribution_commute_singleton[of X]
   656     unfolding joint_distribution_assoc_singleton[symmetric]
   657     using setsum_joint_distribution_singleton[OF finite_var(2) ZX]
   658     by (intro setsum_cong refl) (simp add: space_pair_measure)
   659   also have "(\<Sum>(x, y, z)\<in>?S. ?XYZ x y z * log b (?XYZ x y z / (?X x * ?YZ y z))) -
   660              (\<Sum>(x, z)\<in>space MX \<times> space MZ. ?XZ x z * log b (?XZ x z / (?X x * ?Z z))) =
   661              conditional_mutual_information b MX MY MZ X Y Z"
   662     unfolding conditional_mutual_information_def
   663     unfolding mutual_information_generic_eq[OF finite_var(1,3)]
   664     unfolding mutual_information_generic_eq[OF finite_var(1) YZ]
   665     by (simp add: space_sigma space_pair_measure setsum_cartesian_product')
   666   finally show ?thesis by simp
   667 qed
   668 
   669 lemma (in information_space) conditional_mutual_information_eq:
   670   assumes "simple_function M X" "simple_function M Y" "simple_function M Z"
   671   shows "\<I>(X;Y|Z) = (\<Sum>(x, y, z) \<in> X`space M \<times> Y`space M \<times> Z`space M.
   672              distribution (\<lambda>x. (X x, Y x, Z x)) {(x, y, z)} *
   673              log b (distribution (\<lambda>x. (X x, Y x, Z x)) {(x, y, z)} /
   674     (joint_distribution X Z {(x, z)} * joint_distribution Y Z {(y,z)} / distribution Z {z})))"
   675   by (subst conditional_mutual_information_generic_eq[OF assms[THEN simple_function_imp_finite_random_variable]])
   676      simp
   677 
   678 lemma (in information_space) conditional_mutual_information_eq_mutual_information:
   679   assumes X: "simple_function M X" and Y: "simple_function M Y"
   680   shows "\<I>(X ; Y) = \<I>(X ; Y | (\<lambda>x. ()))"
   681 proof -
   682   have [simp]: "(\<lambda>x. ()) ` space M = {()}" using not_empty by auto
   683   have C: "simple_function M (\<lambda>x. ())" by auto
   684   show ?thesis
   685     unfolding conditional_mutual_information_eq[OF X Y C]
   686     unfolding mutual_information_eq[OF X Y]
   687     by (simp add: setsum_cartesian_product' distribution_remove_const)
   688 qed
   689 
   690 lemma (in prob_space) distribution_unit[simp]: "distribution (\<lambda>x. ()) {()} = 1"
   691   unfolding distribution_def using prob_space by auto
   692 
   693 lemma (in prob_space) joint_distribution_unit[simp]: "distribution (\<lambda>x. (X x, ())) {(a, ())} = distribution X {a}"
   694   unfolding distribution_def by (auto intro!: arg_cong[where f=\<mu>'])
   695 
   696 lemma (in prob_space) setsum_distribution:
   697   assumes X: "finite_random_variable MX X" shows "(\<Sum>a\<in>space MX. distribution X {a}) = 1"
   698   using setsum_joint_distribution[OF assms, of "\<lparr> space = UNIV, sets = Pow UNIV \<rparr>" "\<lambda>x. ()" "{()}"]
   699   using sigma_algebra_Pow[of "UNIV::unit set" "()"] by simp
   700 
   701 lemma (in prob_space) setsum_real_distribution:
   702   fixes MX :: "('c, 'd) measure_space_scheme"
   703   assumes X: "finite_random_variable MX X" shows "(\<Sum>a\<in>space MX. distribution X {a}) = 1"
   704   using setsum_joint_distribution[OF assms, of "\<lparr> space = UNIV, sets = Pow UNIV, measure = undefined \<rparr>" "\<lambda>x. ()" "{()}"]
   705   using sigma_algebra_Pow[of "UNIV::unit set" "\<lparr> measure = undefined \<rparr>"]
   706   by auto
   707 
   708 lemma (in information_space) conditional_mutual_information_generic_positive:
   709   assumes X: "finite_random_variable MX X" and Y: "finite_random_variable MY Y" and Z: "finite_random_variable MZ Z"
   710   shows "0 \<le> conditional_mutual_information b MX MY MZ X Y Z"
   711 proof (cases "space MX \<times> space MY \<times> space MZ = {}")
   712   case True show ?thesis
   713     unfolding conditional_mutual_information_generic_eq[OF assms] True
   714     by simp
   715 next
   716   case False
   717   let ?dXYZ = "distribution (\<lambda>x. (X x, Y x, Z x))"
   718   let ?dXZ = "joint_distribution X Z"
   719   let ?dYZ = "joint_distribution Y Z"
   720   let ?dX = "distribution X"
   721   let ?dZ = "distribution Z"
   722   let ?M = "space MX \<times> space MY \<times> space MZ"
   723 
   724   note YZ = finite_random_variable_pairI[OF Y Z]
   725   note XZ = finite_random_variable_pairI[OF X Z]
   726   note ZX = finite_random_variable_pairI[OF Z X]
   727   note YZ = finite_random_variable_pairI[OF Y Z]
   728   note XYZ = finite_random_variable_pairI[OF X YZ]
   729   note finite = Z YZ XZ XYZ
   730   have order: "\<And>x y z. \<lbrakk>x \<in> space MX; y \<in> space MY; z \<in> space MZ; joint_distribution X Z {(x, z)} = 0\<rbrakk>
   731           \<Longrightarrow> joint_distribution X (\<lambda>x. (Y x, Z x)) {(x, y, z)} = 0"
   732     unfolding joint_distribution_commute_singleton[of X]
   733     unfolding joint_distribution_assoc_singleton[symmetric]
   734     using finite_distribution_order(6)[OF Y ZX]
   735     by auto
   736 
   737   note order = order
   738     finite_distribution_order(5,6)[OF X YZ]
   739     finite_distribution_order(5,6)[OF Y Z]
   740 
   741   have "- conditional_mutual_information b MX MY MZ X Y Z = - (\<Sum>(x, y, z) \<in> ?M. ?dXYZ {(x, y, z)} *
   742     log b (?dXYZ {(x, y, z)} / (?dXZ {(x, z)} * ?dYZ {(y,z)} / ?dZ {z})))"
   743     unfolding conditional_mutual_information_generic_eq[OF assms] neg_equal_iff_equal by auto
   744   also have "\<dots> \<le> log b (\<Sum>(x, y, z) \<in> ?M. ?dXZ {(x, z)} * ?dYZ {(y,z)} / ?dZ {z})"
   745     unfolding split_beta'
   746   proof (rule log_setsum_divide)
   747     show "?M \<noteq> {}" using False by simp
   748     show "1 < b" using b_gt_1 .
   749 
   750     show "finite ?M" using assms
   751       unfolding finite_sigma_algebra_def finite_sigma_algebra_axioms_def by auto
   752 
   753     show "(\<Sum>x\<in>?M. ?dXYZ {(fst x, fst (snd x), snd (snd x))}) = 1"
   754       unfolding setsum_cartesian_product'
   755       unfolding setsum_commute[of _ "space MY"]
   756       unfolding setsum_commute[of _ "space MZ"]
   757       by (simp_all add: space_pair_measure
   758                         setsum_joint_distribution_singleton[OF X YZ]
   759                         setsum_joint_distribution_singleton[OF Y Z]
   760                         setsum_distribution[OF Z])
   761 
   762     fix x assume "x \<in> ?M"
   763     let ?x = "(fst x, fst (snd x), snd (snd x))"
   764 
   765     show "0 \<le> ?dXYZ {?x}"
   766       "0 \<le> ?dXZ {(fst x, snd (snd x))} * ?dYZ {(fst (snd x), snd (snd x))} / ?dZ {snd (snd x)}"
   767      by (simp_all add: mult_nonneg_nonneg divide_nonneg_nonneg)
   768 
   769     assume *: "0 < ?dXYZ {?x}"
   770     with `x \<in> ?M` finite order show "0 < ?dXZ {(fst x, snd (snd x))} * ?dYZ {(fst (snd x), snd (snd x))} / ?dZ {snd (snd x)}"
   771       by (cases x) (auto simp add: zero_le_mult_iff zero_le_divide_iff less_le)
   772   qed
   773   also have "(\<Sum>(x, y, z) \<in> ?M. ?dXZ {(x, z)} * ?dYZ {(y,z)} / ?dZ {z}) = (\<Sum>z\<in>space MZ. ?dZ {z})"
   774     apply (simp add: setsum_cartesian_product')
   775     apply (subst setsum_commute)
   776     apply (subst (2) setsum_commute)
   777     by (auto simp: setsum_divide_distrib[symmetric] setsum_product[symmetric]
   778                    setsum_joint_distribution_singleton[OF X Z]
   779                    setsum_joint_distribution_singleton[OF Y Z]
   780           intro!: setsum_cong)
   781   also have "log b (\<Sum>z\<in>space MZ. ?dZ {z}) = 0"
   782     unfolding setsum_real_distribution[OF Z] by simp
   783   finally show ?thesis by simp
   784 qed
   785 
   786 lemma (in information_space) conditional_mutual_information_positive:
   787   assumes "simple_function M X" and "simple_function M Y" and "simple_function M Z"
   788   shows "0 \<le> \<I>(X;Y|Z)"
   789   by (rule conditional_mutual_information_generic_positive[OF assms[THEN simple_function_imp_finite_random_variable]])
   790 
   791 subsection {* Conditional Entropy *}
   792 
   793 definition (in prob_space)
   794   "conditional_entropy b S T X Y = conditional_mutual_information b S S T X X Y"
   795 
   796 abbreviation (in information_space)
   797   conditional_entropy_Pow ("\<H>'(_ | _')") where
   798   "\<H>(X | Y) \<equiv> conditional_entropy b
   799     \<lparr> space = X`space M, sets = Pow (X`space M), measure = extreal\<circ>distribution X \<rparr>
   800     \<lparr> space = Y`space M, sets = Pow (Y`space M), measure = extreal\<circ>distribution Y \<rparr> X Y"
   801 
   802 lemma (in information_space) conditional_entropy_positive:
   803   "simple_function M X \<Longrightarrow> simple_function M Y \<Longrightarrow> 0 \<le> \<H>(X | Y)"
   804   unfolding conditional_entropy_def by (auto intro!: conditional_mutual_information_positive)
   805 
   806 lemma (in information_space) conditional_entropy_generic_eq:
   807   fixes MX :: "('c, 'd) measure_space_scheme" and MY :: "('e, 'f) measure_space_scheme"
   808   assumes MX: "finite_random_variable MX X"
   809   assumes MZ: "finite_random_variable MZ Z"
   810   shows "conditional_entropy b MX MZ X Z =
   811      - (\<Sum>(x, z)\<in>space MX \<times> space MZ.
   812          joint_distribution X Z {(x, z)} * log b (joint_distribution X Z {(x, z)} / distribution Z {z}))"
   813 proof -
   814   interpret MX: finite_sigma_algebra MX using MX by simp
   815   interpret MZ: finite_sigma_algebra MZ using MZ by simp
   816   let "?XXZ x y z" = "joint_distribution X (\<lambda>x. (X x, Z x)) {(x, y, z)}"
   817   let "?XZ x z" = "joint_distribution X Z {(x, z)}"
   818   let "?Z z" = "distribution Z {z}"
   819   let "?f x y z" = "log b (?XXZ x y z * ?Z z / (?XZ x z * ?XZ y z))"
   820   { fix x z have "?XXZ x x z = ?XZ x z"
   821       unfolding distribution_def by (auto intro!: arg_cong[where f=\<mu>']) }
   822   note this[simp]
   823   { fix x x' :: 'c and z assume "x' \<noteq> x"
   824     then have "?XXZ x x' z = 0"
   825       by (auto simp: distribution_def empty_measure'[symmetric]
   826                simp del: empty_measure' intro!: arg_cong[where f=\<mu>']) }
   827   note this[simp]
   828   { fix x x' z assume *: "x \<in> space MX" "z \<in> space MZ"
   829     then have "(\<Sum>x'\<in>space MX. ?XXZ x x' z * ?f x x' z)
   830       = (\<Sum>x'\<in>space MX. if x = x' then ?XZ x z * ?f x x z else 0)"
   831       by (auto intro!: setsum_cong)
   832     also have "\<dots> = ?XZ x z * ?f x x z"
   833       using `x \<in> space MX` by (simp add: setsum_cases[OF MX.finite_space])
   834     also have "\<dots> = ?XZ x z * log b (?Z z / ?XZ x z)" by auto
   835     also have "\<dots> = - ?XZ x z * log b (?XZ x z / ?Z z)"
   836       using finite_distribution_order(6)[OF MX MZ]
   837       by (auto simp: log_simps field_simps zero_less_mult_iff)
   838     finally have "(\<Sum>x'\<in>space MX. ?XXZ x x' z * ?f x x' z) = - ?XZ x z * log b (?XZ x z / ?Z z)" . }
   839   note * = this
   840   show ?thesis
   841     unfolding conditional_entropy_def
   842     unfolding conditional_mutual_information_generic_eq[OF MX MX MZ]
   843     by (auto simp: setsum_cartesian_product' setsum_negf[symmetric]
   844                    setsum_commute[of _ "space MZ"] *
   845              intro!: setsum_cong)
   846 qed
   847 
   848 lemma (in information_space) conditional_entropy_eq:
   849   assumes "simple_function M X" "simple_function M Z"
   850   shows "\<H>(X | Z) =
   851      - (\<Sum>(x, z)\<in>X ` space M \<times> Z ` space M.
   852          joint_distribution X Z {(x, z)} *
   853          log b (joint_distribution X Z {(x, z)} / distribution Z {z}))"
   854   by (subst conditional_entropy_generic_eq[OF assms[THEN simple_function_imp_finite_random_variable]])
   855      simp
   856 
   857 lemma (in information_space) conditional_entropy_eq_ce_with_hypothesis:
   858   assumes X: "simple_function M X" and Y: "simple_function M Y"
   859   shows "\<H>(X | Y) =
   860     -(\<Sum>y\<in>Y`space M. distribution Y {y} *
   861       (\<Sum>x\<in>X`space M. joint_distribution X Y {(x,y)} / distribution Y {(y)} *
   862               log b (joint_distribution X Y {(x,y)} / distribution Y {(y)})))"
   863   unfolding conditional_entropy_eq[OF assms]
   864   using finite_distribution_order(5,6)[OF assms[THEN simple_function_imp_finite_random_variable]]
   865   by (auto simp: setsum_cartesian_product'  setsum_commute[of _ "Y`space M"] setsum_right_distrib
   866            intro!: setsum_cong)
   867 
   868 lemma (in information_space) conditional_entropy_eq_cartesian_product:
   869   assumes "simple_function M X" "simple_function M Y"
   870   shows "\<H>(X | Y) = -(\<Sum>x\<in>X`space M. \<Sum>y\<in>Y`space M.
   871     joint_distribution X Y {(x,y)} *
   872     log b (joint_distribution X Y {(x,y)} / distribution Y {y}))"
   873   unfolding conditional_entropy_eq[OF assms]
   874   by (auto intro!: setsum_cong simp: setsum_cartesian_product')
   875 
   876 subsection {* Equalities *}
   877 
   878 lemma (in information_space) mutual_information_eq_entropy_conditional_entropy:
   879   assumes X: "simple_function M X" and Z: "simple_function M Z"
   880   shows  "\<I>(X ; Z) = \<H>(X) - \<H>(X | Z)"
   881 proof -
   882   let "?XZ x z" = "joint_distribution X Z {(x, z)}"
   883   let "?Z z" = "distribution Z {z}"
   884   let "?X x" = "distribution X {x}"
   885   note fX = X[THEN simple_function_imp_finite_random_variable]
   886   note fZ = Z[THEN simple_function_imp_finite_random_variable]
   887   note finite_distribution_order[OF fX fZ, simp]
   888   { fix x z assume "x \<in> X`space M" "z \<in> Z`space M"
   889     have "?XZ x z * log b (?XZ x z / (?X x * ?Z z)) =
   890           ?XZ x z * log b (?XZ x z / ?Z z) - ?XZ x z * log b (?X x)"
   891       by (auto simp: log_simps zero_le_mult_iff field_simps less_le) }
   892   note * = this
   893   show ?thesis
   894     unfolding entropy_eq[OF X] conditional_entropy_eq[OF X Z] mutual_information_eq[OF X Z]
   895     using setsum_joint_distribution_singleton[OF fZ fX, unfolded joint_distribution_commute_singleton[of Z X]]
   896     by (simp add: * setsum_cartesian_product' setsum_subtractf setsum_left_distrib[symmetric]
   897                      setsum_distribution)
   898 qed
   899 
   900 lemma (in information_space) conditional_entropy_less_eq_entropy:
   901   assumes X: "simple_function M X" and Z: "simple_function M Z"
   902   shows "\<H>(X | Z) \<le> \<H>(X)"
   903 proof -
   904   have "\<I>(X ; Z) = \<H>(X) - \<H>(X | Z)" using mutual_information_eq_entropy_conditional_entropy[OF assms] .
   905   with mutual_information_positive[OF X Z] entropy_positive[OF X]
   906   show ?thesis by auto
   907 qed
   908 
   909 lemma (in information_space) entropy_chain_rule:
   910   assumes X: "simple_function M X" and Y: "simple_function M Y"
   911   shows  "\<H>(\<lambda>x. (X x, Y x)) = \<H>(X) + \<H>(Y|X)"
   912 proof -
   913   let "?XY x y" = "joint_distribution X Y {(x, y)}"
   914   let "?Y y" = "distribution Y {y}"
   915   let "?X x" = "distribution X {x}"
   916   note fX = X[THEN simple_function_imp_finite_random_variable]
   917   note fY = Y[THEN simple_function_imp_finite_random_variable]
   918   note finite_distribution_order[OF fX fY, simp]
   919   { fix x y assume "x \<in> X`space M" "y \<in> Y`space M"
   920     have "?XY x y * log b (?XY x y / ?X x) =
   921           ?XY x y * log b (?XY x y) - ?XY x y * log b (?X x)"
   922       by (auto simp: log_simps zero_le_mult_iff field_simps less_le) }
   923   note * = this
   924   show ?thesis
   925     using setsum_joint_distribution_singleton[OF fY fX]
   926     unfolding entropy_eq[OF X] conditional_entropy_eq_cartesian_product[OF Y X] entropy_eq_cartesian_product[OF X Y]
   927     unfolding joint_distribution_commute_singleton[of Y X] setsum_commute[of _ "X`space M"]
   928     by (simp add: * setsum_subtractf setsum_left_distrib[symmetric])
   929 qed
   930 
   931 section {* Partitioning *}
   932 
   933 definition "subvimage A f g \<longleftrightarrow> (\<forall>x \<in> A. f -` {f x} \<inter> A \<subseteq> g -` {g x} \<inter> A)"
   934 
   935 lemma subvimageI:
   936   assumes "\<And>x y. \<lbrakk> x \<in> A ; y \<in> A ; f x = f y \<rbrakk> \<Longrightarrow> g x = g y"
   937   shows "subvimage A f g"
   938   using assms unfolding subvimage_def by blast
   939 
   940 lemma subvimageE[consumes 1]:
   941   assumes "subvimage A f g"
   942   obtains "\<And>x y. \<lbrakk> x \<in> A ; y \<in> A ; f x = f y \<rbrakk> \<Longrightarrow> g x = g y"
   943   using assms unfolding subvimage_def by blast
   944 
   945 lemma subvimageD:
   946   "\<lbrakk> subvimage A f g ; x \<in> A ; y \<in> A ; f x = f y \<rbrakk> \<Longrightarrow> g x = g y"
   947   using assms unfolding subvimage_def by blast
   948 
   949 lemma subvimage_subset:
   950   "\<lbrakk> subvimage B f g ; A \<subseteq> B \<rbrakk> \<Longrightarrow> subvimage A f g"
   951   unfolding subvimage_def by auto
   952 
   953 lemma subvimage_idem[intro]: "subvimage A g g"
   954   by (safe intro!: subvimageI)
   955 
   956 lemma subvimage_comp_finer[intro]:
   957   assumes svi: "subvimage A g h"
   958   shows "subvimage A g (f \<circ> h)"
   959 proof (rule subvimageI, simp)
   960   fix x y assume "x \<in> A" "y \<in> A" "g x = g y"
   961   from svi[THEN subvimageD, OF this]
   962   show "f (h x) = f (h y)" by simp
   963 qed
   964 
   965 lemma subvimage_comp_gran:
   966   assumes svi: "subvimage A g h"
   967   assumes inj: "inj_on f (g ` A)"
   968   shows "subvimage A (f \<circ> g) h"
   969   by (rule subvimageI) (auto intro!: subvimageD[OF svi] simp: inj_on_iff[OF inj])
   970 
   971 lemma subvimage_comp:
   972   assumes svi: "subvimage (f ` A) g h"
   973   shows "subvimage A (g \<circ> f) (h \<circ> f)"
   974   by (rule subvimageI) (auto intro!: svi[THEN subvimageD])
   975 
   976 lemma subvimage_trans:
   977   assumes fg: "subvimage A f g"
   978   assumes gh: "subvimage A g h"
   979   shows "subvimage A f h"
   980   by (rule subvimageI) (auto intro!: fg[THEN subvimageD] gh[THEN subvimageD])
   981 
   982 lemma subvimage_translator:
   983   assumes svi: "subvimage A f g"
   984   shows "\<exists>h. \<forall>x \<in> A. h (f x)  = g x"
   985 proof (safe intro!: exI[of _ "\<lambda>x. (THE z. z \<in> (g ` (f -` {x} \<inter> A)))"])
   986   fix x assume "x \<in> A"
   987   show "(THE x'. x' \<in> (g ` (f -` {f x} \<inter> A))) = g x"
   988     by (rule theI2[of _ "g x"])
   989       (insert `x \<in> A`, auto intro!: svi[THEN subvimageD])
   990 qed
   991 
   992 lemma subvimage_translator_image:
   993   assumes svi: "subvimage A f g"
   994   shows "\<exists>h. h ` f ` A = g ` A"
   995 proof -
   996   from subvimage_translator[OF svi]
   997   obtain h where "\<And>x. x \<in> A \<Longrightarrow> h (f x) = g x" by auto
   998   thus ?thesis
   999     by (auto intro!: exI[of _ h]
  1000       simp: image_compose[symmetric] comp_def cong: image_cong)
  1001 qed
  1002 
  1003 lemma subvimage_finite:
  1004   assumes svi: "subvimage A f g" and fin: "finite (f`A)"
  1005   shows "finite (g`A)"
  1006 proof -
  1007   from subvimage_translator_image[OF svi]
  1008   obtain h where "g`A = h`f`A" by fastsimp
  1009   with fin show "finite (g`A)" by simp
  1010 qed
  1011 
  1012 lemma subvimage_disj:
  1013   assumes svi: "subvimage A f g"
  1014   shows "f -` {x} \<inter> A \<subseteq> g -` {y} \<inter> A \<or>
  1015       f -` {x} \<inter> g -` {y} \<inter> A = {}" (is "?sub \<or> ?dist")
  1016 proof (rule disjCI)
  1017   assume "\<not> ?dist"
  1018   then obtain z where "z \<in> A" and "x = f z" and "y = g z" by auto
  1019   thus "?sub" using svi unfolding subvimage_def by auto
  1020 qed
  1021 
  1022 lemma setsum_image_split:
  1023   assumes svi: "subvimage A f g" and fin: "finite (f ` A)"
  1024   shows "(\<Sum>x\<in>f`A. h x) = (\<Sum>y\<in>g`A. \<Sum>x\<in>f`(g -` {y} \<inter> A). h x)"
  1025     (is "?lhs = ?rhs")
  1026 proof -
  1027   have "f ` A =
  1028       snd ` (SIGMA x : g ` A. f ` (g -` {x} \<inter> A))"
  1029       (is "_ = snd ` ?SIGMA")
  1030     unfolding image_split_eq_Sigma[symmetric]
  1031     by (simp add: image_compose[symmetric] comp_def)
  1032   moreover
  1033   have snd_inj: "inj_on snd ?SIGMA"
  1034     unfolding image_split_eq_Sigma[symmetric]
  1035     by (auto intro!: inj_onI subvimageD[OF svi])
  1036   ultimately
  1037   have "(\<Sum>x\<in>f`A. h x) = (\<Sum>(x,y)\<in>?SIGMA. h y)"
  1038     by (auto simp: setsum_reindex intro: setsum_cong)
  1039   also have "... = ?rhs"
  1040     using subvimage_finite[OF svi fin] fin
  1041     apply (subst setsum_Sigma[symmetric])
  1042     by (auto intro!: finite_subset[of _ "f`A"])
  1043   finally show ?thesis .
  1044 qed
  1045 
  1046 lemma (in information_space) entropy_partition:
  1047   assumes sf: "simple_function M X" "simple_function M P"
  1048   assumes svi: "subvimage (space M) X P"
  1049   shows "\<H>(X) = \<H>(P) + \<H>(X|P)"
  1050 proof -
  1051   let "?XP x p" = "joint_distribution X P {(x, p)}"
  1052   let "?X x" = "distribution X {x}"
  1053   let "?P p" = "distribution P {p}"
  1054   note fX = sf(1)[THEN simple_function_imp_finite_random_variable]
  1055   note fP = sf(2)[THEN simple_function_imp_finite_random_variable]
  1056   note finite_distribution_order[OF fX fP, simp]
  1057   have "(\<Sum>x\<in>X ` space M. ?X x * log b (?X x)) =
  1058     (\<Sum>y\<in>P `space M. \<Sum>x\<in>X ` space M. ?XP x y * log b (?XP x y))"
  1059   proof (subst setsum_image_split[OF svi],
  1060       safe intro!: setsum_mono_zero_cong_left imageI)
  1061     show "finite (X ` space M)" "finite (X ` space M)" "finite (P ` space M)"
  1062       using sf unfolding simple_function_def by auto
  1063   next
  1064     fix p x assume in_space: "p \<in> space M" "x \<in> space M"
  1065     assume "?XP (X x) (P p) * log b (?XP (X x) (P p)) \<noteq> 0"
  1066     hence "(\<lambda>x. (X x, P x)) -` {(X x, P p)} \<inter> space M \<noteq> {}" by (auto simp: distribution_def)
  1067     with svi[unfolded subvimage_def, rule_format, OF `x \<in> space M`]
  1068     show "x \<in> P -` {P p}" by auto
  1069   next
  1070     fix p x assume in_space: "p \<in> space M" "x \<in> space M"
  1071     assume "P x = P p"
  1072     from this[symmetric] svi[unfolded subvimage_def, rule_format, OF `x \<in> space M`]
  1073     have "X -` {X x} \<inter> space M \<subseteq> P -` {P p} \<inter> space M"
  1074       by auto
  1075     hence "(\<lambda>x. (X x, P x)) -` {(X x, P p)} \<inter> space M = X -` {X x} \<inter> space M"
  1076       by auto
  1077     thus "?X (X x) * log b (?X (X x)) = ?XP (X x) (P p) * log b (?XP (X x) (P p))"
  1078       by (auto simp: distribution_def)
  1079   qed
  1080   moreover have "\<And>x y. ?XP x y * log b (?XP x y / ?P y) =
  1081       ?XP x y * log b (?XP x y) - ?XP x y * log b (?P y)"
  1082     by (auto simp add: log_simps zero_less_mult_iff field_simps)
  1083   ultimately show ?thesis
  1084     unfolding sf[THEN entropy_eq] conditional_entropy_eq[OF sf]
  1085     using setsum_joint_distribution_singleton[OF fX fP]
  1086     by (simp add: setsum_cartesian_product' setsum_subtractf setsum_distribution
  1087       setsum_left_distrib[symmetric] setsum_commute[where B="P`space M"])
  1088 qed
  1089 
  1090 corollary (in information_space) entropy_data_processing:
  1091   assumes X: "simple_function M X" shows "\<H>(f \<circ> X) \<le> \<H>(X)"
  1092 proof -
  1093   note X
  1094   moreover have fX: "simple_function M (f \<circ> X)" using X by auto
  1095   moreover have "subvimage (space M) X (f \<circ> X)" by auto
  1096   ultimately have "\<H>(X) = \<H>(f\<circ>X) + \<H>(X|f\<circ>X)" by (rule entropy_partition)
  1097   then show "\<H>(f \<circ> X) \<le> \<H>(X)"
  1098     by (auto intro: conditional_entropy_positive[OF X fX])
  1099 qed
  1100 
  1101 corollary (in information_space) entropy_of_inj:
  1102   assumes X: "simple_function M X" and inj: "inj_on f (X`space M)"
  1103   shows "\<H>(f \<circ> X) = \<H>(X)"
  1104 proof (rule antisym)
  1105   show "\<H>(f \<circ> X) \<le> \<H>(X)" using entropy_data_processing[OF X] .
  1106 next
  1107   have sf: "simple_function M (f \<circ> X)"
  1108     using X by auto
  1109   have "\<H>(X) = \<H>(the_inv_into (X`space M) f \<circ> (f \<circ> X))"
  1110     by (auto intro!: mutual_information_cong simp: entropy_def the_inv_into_f_f[OF inj])
  1111   also have "... \<le> \<H>(f \<circ> X)"
  1112     using entropy_data_processing[OF sf] .
  1113   finally show "\<H>(X) \<le> \<H>(f \<circ> X)" .
  1114 qed
  1115 
  1116 end