src/HOL/Probability/Information.thy
changeset 43340 60e181c4eae4
parent 42148 d596e7bb251f
child 43556 0d78c8d31d0d
     1.1 --- a/src/HOL/Probability/Information.thy	Thu Jun 09 13:55:11 2011 +0200
     1.2 +++ b/src/HOL/Probability/Information.thy	Thu Jun 09 14:04:34 2011 +0200
     1.3 @@ -7,14 +7,10 @@
     1.4  
     1.5  theory Information
     1.6  imports
     1.7 -  Probability_Measure
     1.8 +  Independent_Family
     1.9    "~~/src/HOL/Library/Convex"
    1.10  begin
    1.11  
    1.12 -lemma (in prob_space) not_zero_less_distribution[simp]:
    1.13 -  "(\<not> 0 < distribution X A) \<longleftrightarrow> distribution X A = 0"
    1.14 -  using distribution_positive[of X A] by arith
    1.15 -
    1.16  lemma log_le: "1 < a \<Longrightarrow> 0 < x \<Longrightarrow> x \<le> y \<Longrightarrow> log a x \<le> log a y"
    1.17    by (subst log_le_cancel_iff) auto
    1.18  
    1.19 @@ -175,7 +171,211 @@
    1.20  Kullback$-$Leibler distance. *}
    1.21  
    1.22  definition
    1.23 -  "KL_divergence b M \<nu> = \<integral>x. log b (real (RN_deriv M \<nu> x)) \<partial>M\<lparr>measure := \<nu>\<rparr>"
    1.24 +  "entropy_density b M \<nu> = log b \<circ> real \<circ> RN_deriv M \<nu>"
    1.25 +
    1.26 +definition
    1.27 +  "KL_divergence b M \<nu> = integral\<^isup>L (M\<lparr>measure := \<nu>\<rparr>) (entropy_density b M \<nu>)"
    1.28 +
    1.29 +lemma (in information_space) measurable_entropy_density:
    1.30 +  assumes ps: "prob_space (M\<lparr>measure := \<nu>\<rparr>)"
    1.31 +  assumes ac: "absolutely_continuous \<nu>"
    1.32 +  shows "entropy_density b M \<nu> \<in> borel_measurable M"
    1.33 +proof -
    1.34 +  interpret \<nu>: prob_space "M\<lparr>measure := \<nu>\<rparr>" by fact
    1.35 +  have "measure_space (M\<lparr>measure := \<nu>\<rparr>)" by fact
    1.36 +  from RN_deriv[OF this ac] b_gt_1 show ?thesis
    1.37 +    unfolding entropy_density_def
    1.38 +    by (intro measurable_comp) auto
    1.39 +qed
    1.40 +
    1.41 +lemma (in information_space) KL_gt_0:
    1.42 +  assumes ps: "prob_space (M\<lparr>measure := \<nu>\<rparr>)"
    1.43 +  assumes ac: "absolutely_continuous \<nu>"
    1.44 +  assumes int: "integrable (M\<lparr> measure := \<nu> \<rparr>) (entropy_density b M \<nu>)"
    1.45 +  assumes A: "A \<in> sets M" "\<nu> A \<noteq> \<mu> A"
    1.46 +  shows "0 < KL_divergence b M \<nu>"
    1.47 +proof -
    1.48 +  interpret \<nu>: prob_space "M\<lparr>measure := \<nu>\<rparr>" by fact
    1.49 +  have ms: "measure_space (M\<lparr>measure := \<nu>\<rparr>)" by default
    1.50 +  have fms: "finite_measure (M\<lparr>measure := \<nu>\<rparr>)" by default
    1.51 +  note RN = RN_deriv[OF ms ac]
    1.52 +
    1.53 +  from real_RN_deriv[OF fms ac] guess D . note D = this
    1.54 +  with absolutely_continuous_AE[OF ms] ac
    1.55 +  have D\<nu>: "AE x in M\<lparr>measure := \<nu>\<rparr>. RN_deriv M \<nu> x = extreal (D x)"
    1.56 +    by auto
    1.57 +
    1.58 +  def f \<equiv> "\<lambda>x. if D x = 0 then 1 else 1 / D x"
    1.59 +  with D have f_borel: "f \<in> borel_measurable M"
    1.60 +    by (auto intro!: measurable_If)
    1.61 +
    1.62 +  have "KL_divergence b M \<nu> = 1 / ln b * (\<integral> x. ln b * entropy_density b M \<nu> x \<partial>M\<lparr>measure := \<nu>\<rparr>)"
    1.63 +    unfolding KL_divergence_def using int b_gt_1
    1.64 +    by (simp add: integral_cmult)
    1.65 +
    1.66 +  { fix A assume "A \<in> sets M"
    1.67 +    with RN D have "\<nu>.\<mu> A = (\<integral>\<^isup>+ x. extreal (D x) * indicator A x \<partial>M)"
    1.68 +      by (auto intro!: positive_integral_cong_AE) }
    1.69 +  note D_density = this
    1.70 +
    1.71 +  have ln_entropy: "(\<lambda>x. ln b * entropy_density b M \<nu> x) \<in> borel_measurable M"
    1.72 +    using measurable_entropy_density[OF ps ac] by auto
    1.73 +
    1.74 +  have "integrable (M\<lparr>measure := \<nu>\<rparr>) (\<lambda>x. ln b * entropy_density b M \<nu> x)"
    1.75 +    using int by auto
    1.76 +  moreover have "integrable (M\<lparr>measure := \<nu>\<rparr>) (\<lambda>x. ln b * entropy_density b M \<nu> x) \<longleftrightarrow>
    1.77 +      integrable M (\<lambda>x. D x * (ln b * entropy_density b M \<nu> x))"
    1.78 +    using D D_density ln_entropy
    1.79 +    by (intro integral_translated_density) auto
    1.80 +  ultimately have M_int: "integrable M (\<lambda>x. D x * (ln b * entropy_density b M \<nu> x))"
    1.81 +    by simp
    1.82 +
    1.83 +  have D_neg: "(\<integral>\<^isup>+ x. extreal (- D x) \<partial>M) = 0"
    1.84 +    using D by (subst positive_integral_0_iff_AE) auto
    1.85 +
    1.86 +  have "(\<integral>\<^isup>+ x. extreal (D x) \<partial>M) = \<nu> (space M)"
    1.87 +    using RN D by (auto intro!: positive_integral_cong_AE)
    1.88 +  then have D_pos: "(\<integral>\<^isup>+ x. extreal (D x) \<partial>M) = 1"
    1.89 +    using \<nu>.measure_space_1 by simp
    1.90 +
    1.91 +  have "integrable M D"
    1.92 +    using D_pos D_neg D by (auto simp: integrable_def)
    1.93 +
    1.94 +  have "integral\<^isup>L M D = 1"
    1.95 +    using D_pos D_neg by (auto simp: lebesgue_integral_def)
    1.96 +
    1.97 +  let ?D_set = "{x\<in>space M. D x \<noteq> 0}"
    1.98 +  have [simp, intro]: "?D_set \<in> sets M"
    1.99 +    using D by (auto intro: sets_Collect)
   1.100 +
   1.101 +  have "0 \<le> 1 - \<mu>' ?D_set"
   1.102 +    using prob_le_1 by (auto simp: field_simps)
   1.103 +  also have "\<dots> = (\<integral> x. D x - indicator ?D_set x \<partial>M)"
   1.104 +    using `integrable M D` `integral\<^isup>L M D = 1`
   1.105 +    by (simp add: \<mu>'_def)
   1.106 +  also have "\<dots> < (\<integral> x. D x * (ln b * entropy_density b M \<nu> x) \<partial>M)"
   1.107 +  proof (rule integral_less_AE)
   1.108 +    show "integrable M (\<lambda>x. D x - indicator ?D_set x)"
   1.109 +      using `integrable M D`
   1.110 +      by (intro integral_diff integral_indicator) auto
   1.111 +  next
   1.112 +    show "integrable M (\<lambda>x. D x * (ln b * entropy_density b M \<nu> x))"
   1.113 +      by fact
   1.114 +  next
   1.115 +    show "\<mu> {x\<in>space M. D x \<noteq> 1 \<and> D x \<noteq> 0} \<noteq> 0"
   1.116 +    proof
   1.117 +      assume eq_0: "\<mu> {x\<in>space M. D x \<noteq> 1 \<and> D x \<noteq> 0} = 0"
   1.118 +      then have disj: "AE x. D x = 1 \<or> D x = 0"
   1.119 +        using D(1) by (auto intro!: AE_I[OF subset_refl] sets_Collect)
   1.120 +
   1.121 +      have "\<mu> {x\<in>space M. D x = 1} = (\<integral>\<^isup>+ x. indicator {x\<in>space M. D x = 1} x \<partial>M)"
   1.122 +        using D(1) by auto
   1.123 +      also have "\<dots> = (\<integral>\<^isup>+ x. extreal (D x) * indicator {x\<in>space M. D x \<noteq> 0} x \<partial>M)"
   1.124 +        using disj by (auto intro!: positive_integral_cong_AE simp: indicator_def one_extreal_def)
   1.125 +      also have "\<dots> = \<nu> {x\<in>space M. D x \<noteq> 0}"
   1.126 +        using D(1) D_density by auto
   1.127 +      also have "\<dots> = \<nu> (space M)"
   1.128 +        using D_density D(1) by (auto intro!: positive_integral_cong simp: indicator_def)
   1.129 +      finally have "AE x. D x = 1"
   1.130 +        using D(1) \<nu>.measure_space_1 by (intro AE_I_eq_1) auto
   1.131 +      then have "(\<integral>\<^isup>+x. indicator A x\<partial>M) = (\<integral>\<^isup>+x. extreal (D x) * indicator A x\<partial>M)"
   1.132 +        by (intro positive_integral_cong_AE) (auto simp: one_extreal_def[symmetric])
   1.133 +      also have "\<dots> = \<nu> A"
   1.134 +        using `A \<in> sets M` D_density by simp
   1.135 +      finally show False using `A \<in> sets M` `\<nu> A \<noteq> \<mu> A` by simp
   1.136 +    qed
   1.137 +    show "{x\<in>space M. D x \<noteq> 1 \<and> D x \<noteq> 0} \<in> sets M"
   1.138 +      using D(1) by (auto intro: sets_Collect)
   1.139 +
   1.140 +    show "AE t. t \<in> {x\<in>space M. D x \<noteq> 1 \<and> D x \<noteq> 0} \<longrightarrow>
   1.141 +      D t - indicator ?D_set t \<noteq> D t * (ln b * entropy_density b M \<nu> t)"
   1.142 +      using D(2)
   1.143 +    proof (elim AE_mp, safe intro!: AE_I2)
   1.144 +      fix t assume Dt: "t \<in> space M" "D t \<noteq> 1" "D t \<noteq> 0"
   1.145 +        and RN: "RN_deriv M \<nu> t = extreal (D t)"
   1.146 +        and eq: "D t - indicator ?D_set t = D t * (ln b * entropy_density b M \<nu> t)"
   1.147 +
   1.148 +      have "D t - 1 = D t - indicator ?D_set t"
   1.149 +        using Dt by simp
   1.150 +      also note eq
   1.151 +      also have "D t * (ln b * entropy_density b M \<nu> t) = - D t * ln (1 / D t)"
   1.152 +        using RN b_gt_1 `D t \<noteq> 0` `0 \<le> D t`
   1.153 +        by (simp add: entropy_density_def log_def ln_div less_le)
   1.154 +      finally have "ln (1 / D t) = 1 / D t - 1"
   1.155 +        using `D t \<noteq> 0` by (auto simp: field_simps)
   1.156 +      from ln_eq_minus_one[OF _ this] `D t \<noteq> 0` `0 \<le> D t` `D t \<noteq> 1`
   1.157 +      show False by auto
   1.158 +    qed
   1.159 +
   1.160 +    show "AE t. D t - indicator ?D_set t \<le> D t * (ln b * entropy_density b M \<nu> t)"
   1.161 +      using D(2)
   1.162 +    proof (elim AE_mp, intro AE_I2 impI)
   1.163 +      fix t assume "t \<in> space M" and RN: "RN_deriv M \<nu> t = extreal (D t)"
   1.164 +      show "D t - indicator ?D_set t \<le> D t * (ln b * entropy_density b M \<nu> t)"
   1.165 +      proof cases
   1.166 +        assume asm: "D t \<noteq> 0"
   1.167 +        then have "0 < D t" using `0 \<le> D t` by auto
   1.168 +        then have "0 < 1 / D t" by auto
   1.169 +        have "D t - indicator ?D_set t \<le> - D t * (1 / D t - 1)"
   1.170 +          using asm `t \<in> space M` by (simp add: field_simps)
   1.171 +        also have "- D t * (1 / D t - 1) \<le> - D t * ln (1 / D t)"
   1.172 +          using ln_le_minus_one `0 < 1 / D t` by (intro mult_left_mono_neg) auto
   1.173 +        also have "\<dots> = D t * (ln b * entropy_density b M \<nu> t)"
   1.174 +          using `0 < D t` RN b_gt_1
   1.175 +          by (simp_all add: log_def ln_div entropy_density_def)
   1.176 +        finally show ?thesis by simp
   1.177 +      qed simp
   1.178 +    qed
   1.179 +  qed
   1.180 +  also have "\<dots> = (\<integral> x. ln b * entropy_density b M \<nu> x \<partial>M\<lparr>measure := \<nu>\<rparr>)"
   1.181 +    using D D_density ln_entropy
   1.182 +    by (intro integral_translated_density[symmetric]) auto
   1.183 +  also have "\<dots> = ln b * (\<integral> x. entropy_density b M \<nu> x \<partial>M\<lparr>measure := \<nu>\<rparr>)"
   1.184 +    using int by (rule \<nu>.integral_cmult)
   1.185 +  finally show "0 < KL_divergence b M \<nu>"
   1.186 +    using b_gt_1 by (auto simp: KL_divergence_def zero_less_mult_iff)
   1.187 +qed
   1.188 +
   1.189 +lemma (in sigma_finite_measure) KL_eq_0:
   1.190 +  assumes eq: "\<forall>A\<in>sets M. \<nu> A = measure M A"
   1.191 +  shows "KL_divergence b M \<nu> = 0"
   1.192 +proof -
   1.193 +  have "AE x. 1 = RN_deriv M \<nu> x"
   1.194 +  proof (rule RN_deriv_unique)
   1.195 +    show "measure_space (M\<lparr>measure := \<nu>\<rparr>)"
   1.196 +      using eq by (intro measure_space_cong) auto
   1.197 +    show "absolutely_continuous \<nu>"
   1.198 +      unfolding absolutely_continuous_def using eq by auto
   1.199 +    show "(\<lambda>x. 1) \<in> borel_measurable M" "AE x. 0 \<le> (1 :: extreal)" by auto
   1.200 +    fix A assume "A \<in> sets M"
   1.201 +    with eq show "\<nu> A = \<integral>\<^isup>+ x. 1 * indicator A x \<partial>M" by simp
   1.202 +  qed
   1.203 +  then have "AE x. log b (real (RN_deriv M \<nu> x)) = 0"
   1.204 +    by (elim AE_mp) simp
   1.205 +  from integral_cong_AE[OF this]
   1.206 +  have "integral\<^isup>L M (entropy_density b M \<nu>) = 0"
   1.207 +    by (simp add: entropy_density_def comp_def)
   1.208 +  with eq show "KL_divergence b M \<nu> = 0"
   1.209 +    unfolding KL_divergence_def
   1.210 +    by (subst integral_cong_measure) auto
   1.211 +qed
   1.212 +
   1.213 +lemma (in information_space) KL_eq_0_imp:
   1.214 +  assumes ps: "prob_space (M\<lparr>measure := \<nu>\<rparr>)"
   1.215 +  assumes ac: "absolutely_continuous \<nu>"
   1.216 +  assumes int: "integrable (M\<lparr> measure := \<nu> \<rparr>) (entropy_density b M \<nu>)"
   1.217 +  assumes KL: "KL_divergence b M \<nu> = 0"
   1.218 +  shows "\<forall>A\<in>sets M. \<nu> A = \<mu> A"
   1.219 +  by (metis less_imp_neq KL_gt_0 assms)
   1.220 +
   1.221 +lemma (in information_space) KL_ge_0:
   1.222 +  assumes ps: "prob_space (M\<lparr>measure := \<nu>\<rparr>)"
   1.223 +  assumes ac: "absolutely_continuous \<nu>"
   1.224 +  assumes int: "integrable (M\<lparr> measure := \<nu> \<rparr>) (entropy_density b M \<nu>)"
   1.225 +  shows "0 \<le> KL_divergence b M \<nu>"
   1.226 +  using KL_eq_0 KL_gt_0[OF ps ac int]
   1.227 +  by (cases "\<forall>A\<in>sets M. \<nu> A = measure M A") (auto simp: le_less)
   1.228 +
   1.229  
   1.230  lemma (in sigma_finite_measure) KL_divergence_vimage:
   1.231    assumes T: "T \<in> measure_preserving M M'"
   1.232 @@ -209,7 +409,7 @@
   1.233    have AE: "AE x. RN_deriv M' \<nu>' (T x) = RN_deriv M \<nu> x"
   1.234      by (rule RN_deriv_vimage[OF T T' inv \<nu>'])
   1.235    show ?thesis
   1.236 -    unfolding KL_divergence_def
   1.237 +    unfolding KL_divergence_def entropy_density_def comp_def
   1.238    proof (subst \<nu>'.integral_vimage[OF sa T'])
   1.239      show "(\<lambda>x. log b (real (RN_deriv M \<nu> x))) \<in> borel_measurable (M\<lparr>measure := \<nu>\<rparr>)"
   1.240        by (auto intro!: RN_deriv[OF M ac] borel_measurable_log[OF _ `1 < b`])
   1.241 @@ -233,9 +433,9 @@
   1.242  proof -
   1.243    interpret \<nu>: measure_space ?\<nu> by fact
   1.244    have "KL_divergence b M \<nu> = \<integral>x. log b (real (RN_deriv N \<nu>' x)) \<partial>?\<nu>"
   1.245 -    by (simp cong: RN_deriv_cong \<nu>.integral_cong add: KL_divergence_def)
   1.246 +    by (simp cong: RN_deriv_cong \<nu>.integral_cong add: KL_divergence_def entropy_density_def)
   1.247    also have "\<dots> = KL_divergence b N \<nu>'"
   1.248 -    by (auto intro!: \<nu>.integral_cong_measure[symmetric] simp: KL_divergence_def)
   1.249 +    by (auto intro!: \<nu>.integral_cong_measure[symmetric] simp: KL_divergence_def entropy_density_def comp_def)
   1.250    finally show ?thesis .
   1.251  qed
   1.252  
   1.253 @@ -243,7 +443,7 @@
   1.254    assumes v: "finite_measure_space (M\<lparr>measure := \<nu>\<rparr>)"
   1.255    assumes ac: "absolutely_continuous \<nu>"
   1.256    shows "KL_divergence b M \<nu> = (\<Sum>x\<in>space M. real (\<nu> {x}) * log b (real (\<nu> {x}) / real (\<mu> {x})))" (is "_ = ?sum")
   1.257 -proof (simp add: KL_divergence_def finite_measure_space.integral_finite_singleton[OF v])
   1.258 +proof (simp add: KL_divergence_def finite_measure_space.integral_finite_singleton[OF v] entropy_density_def)
   1.259    interpret v: finite_measure_space "M\<lparr>measure := \<nu>\<rparr>" by fact
   1.260    have ms: "measure_space (M\<lparr>measure := \<nu>\<rparr>)" by default
   1.261    show "(\<Sum>x \<in> space M. log b (real (RN_deriv M \<nu> x)) * real (\<nu> {x})) = ?sum"
   1.262 @@ -257,27 +457,10 @@
   1.263    and "1 < b"
   1.264    shows "0 \<le> KL_divergence b M \<nu>"
   1.265  proof -
   1.266 +  interpret information_space M by default fact
   1.267    interpret v: finite_prob_space "M\<lparr>measure := \<nu>\<rparr>" by fact
   1.268 -  have ms: "finite_measure_space (M\<lparr>measure := \<nu>\<rparr>)" by default
   1.269 -
   1.270 -  have "- (KL_divergence b M \<nu>) \<le> log b (\<Sum>x\<in>space M. real (\<mu> {x}))"
   1.271 -  proof (subst KL_divergence_eq_finite[OF ms ac], safe intro!: log_setsum_divide not_empty)
   1.272 -    show "finite (space M)" using finite_space by simp
   1.273 -    show "1 < b" by fact
   1.274 -    show "(\<Sum>x\<in>space M. real (\<nu> {x})) = 1"
   1.275 -      using v.finite_sum_over_space_eq_1 by (simp add: v.\<mu>'_def)
   1.276 -
   1.277 -    fix x assume "x \<in> space M"
   1.278 -    then have x: "{x} \<in> sets M" unfolding sets_eq_Pow by auto
   1.279 -    { assume "0 < real (\<nu> {x})"
   1.280 -      then have "\<nu> {x} \<noteq> 0" by auto
   1.281 -      then have "\<mu> {x} \<noteq> 0"
   1.282 -        using ac[unfolded absolutely_continuous_def, THEN bspec, of "{x}"] x by auto
   1.283 -      thus "0 < real (\<mu> {x})" using real_measure[OF x] by auto }
   1.284 -    show "0 \<le> real (\<mu> {x})" "0 \<le> real (\<nu> {x})"
   1.285 -      using real_measure[OF x] v.real_measure[of "{x}"] x by auto
   1.286 -  qed
   1.287 -  thus "0 \<le> KL_divergence b M \<nu>" using finite_sum_over_space_eq_1 by (simp add: \<mu>'_def)
   1.288 +  have ps: "prob_space (M\<lparr>measure := \<nu>\<rparr>)" by default
   1.289 +  from KL_ge_0[OF this ac v.integral_finite_singleton(1)] show ?thesis .
   1.290  qed
   1.291  
   1.292  subsection {* Mutual Information *}
   1.293 @@ -287,6 +470,163 @@
   1.294      KL_divergence b (S\<lparr>measure := extreal\<circ>distribution X\<rparr> \<Otimes>\<^isub>M T\<lparr>measure := extreal\<circ>distribution Y\<rparr>)
   1.295        (extreal\<circ>joint_distribution X Y)"
   1.296  
   1.297 +lemma (in information_space)
   1.298 +  fixes S T X Y
   1.299 +  defines "P \<equiv> S\<lparr>measure := extreal\<circ>distribution X\<rparr> \<Otimes>\<^isub>M T\<lparr>measure := extreal\<circ>distribution Y\<rparr>"
   1.300 +  shows "indep_var S X T Y \<longleftrightarrow>
   1.301 +    (random_variable S X \<and> random_variable T Y \<and>
   1.302 +      measure_space.absolutely_continuous P (extreal\<circ>joint_distribution X Y) \<and>
   1.303 +      integrable (P\<lparr>measure := (extreal\<circ>joint_distribution X Y)\<rparr>)
   1.304 +        (entropy_density b P (extreal\<circ>joint_distribution X Y)) \<and>
   1.305 +     mutual_information b S T X Y = 0)"
   1.306 +proof safe
   1.307 +  assume indep: "indep_var S X T Y"
   1.308 +  then have "random_variable S X" "random_variable T Y"
   1.309 +    by (blast dest: indep_var_rv1 indep_var_rv2)+
   1.310 +  then show "sigma_algebra S" "X \<in> measurable M S" "sigma_algebra T" "Y \<in> measurable M T"
   1.311 +    by blast+
   1.312 +
   1.313 +  interpret X: prob_space "S\<lparr>measure := extreal\<circ>distribution X\<rparr>"
   1.314 +    by (rule distribution_prob_space) fact
   1.315 +  interpret Y: prob_space "T\<lparr>measure := extreal\<circ>distribution Y\<rparr>"
   1.316 +    by (rule distribution_prob_space) fact
   1.317 +  interpret XY: pair_prob_space "S\<lparr>measure := extreal\<circ>distribution X\<rparr>" "T\<lparr>measure := extreal\<circ>distribution Y\<rparr>" by default
   1.318 +  interpret XY: information_space XY.P b by default (rule b_gt_1)
   1.319 +
   1.320 +  let ?J = "XY.P\<lparr> measure := (extreal\<circ>joint_distribution X Y) \<rparr>"
   1.321 +  { fix A assume "A \<in> sets XY.P"
   1.322 +    then have "extreal (joint_distribution X Y A) = XY.\<mu> A"
   1.323 +      using indep_var_distributionD[OF indep]
   1.324 +      by (simp add: XY.P.finite_measure_eq) }
   1.325 +  note j_eq = this
   1.326 +
   1.327 +  interpret J: prob_space ?J
   1.328 +    using j_eq by (intro XY.prob_space_cong) auto
   1.329 +
   1.330 +  have ac: "XY.absolutely_continuous (extreal\<circ>joint_distribution X Y)"
   1.331 +    by (simp add: XY.absolutely_continuous_def j_eq)
   1.332 +  then show "measure_space.absolutely_continuous P (extreal\<circ>joint_distribution X Y)"
   1.333 +    unfolding P_def .
   1.334 +
   1.335 +  have ed: "entropy_density b XY.P (extreal\<circ>joint_distribution X Y) \<in> borel_measurable XY.P"
   1.336 +    by (rule XY.measurable_entropy_density) (default | fact)+
   1.337 +
   1.338 +  have "AE x in XY.P. 1 = RN_deriv XY.P (extreal\<circ>joint_distribution X Y) x"
   1.339 +  proof (rule XY.RN_deriv_unique[OF _ ac])
   1.340 +    show "measure_space ?J" by default
   1.341 +    fix A assume "A \<in> sets XY.P"
   1.342 +    then show "(extreal\<circ>joint_distribution X Y) A = (\<integral>\<^isup>+ x. 1 * indicator A x \<partial>XY.P)"
   1.343 +      by (simp add: j_eq)
   1.344 +  qed (insert XY.measurable_const[of 1 borel], auto)
   1.345 +  then have ae_XY: "AE x in XY.P. entropy_density b XY.P (extreal\<circ>joint_distribution X Y) x = 0"
   1.346 +    by (elim XY.AE_mp) (simp add: entropy_density_def)
   1.347 +  have ae_J: "AE x in ?J. entropy_density b XY.P (extreal\<circ>joint_distribution X Y) x = 0"
   1.348 +  proof (rule XY.absolutely_continuous_AE)
   1.349 +    show "measure_space ?J" by default
   1.350 +    show "XY.absolutely_continuous (measure ?J)"
   1.351 +      using ac by simp
   1.352 +  qed (insert ae_XY, simp_all)
   1.353 +  then show "integrable (P\<lparr>measure := (extreal\<circ>joint_distribution X Y)\<rparr>)
   1.354 +        (entropy_density b P (extreal\<circ>joint_distribution X Y))"
   1.355 +    unfolding P_def
   1.356 +    using ed XY.measurable_const[of 0 borel]
   1.357 +    by (subst J.integrable_cong_AE) auto
   1.358 +
   1.359 +  show "mutual_information b S T X Y = 0"
   1.360 +    unfolding mutual_information_def KL_divergence_def P_def
   1.361 +    by (subst J.integral_cong_AE[OF ae_J]) simp
   1.362 +next
   1.363 +  assume "sigma_algebra S" "X \<in> measurable M S" "sigma_algebra T" "Y \<in> measurable M T"
   1.364 +  then have rvs: "random_variable S X" "random_variable T Y" by blast+
   1.365 +
   1.366 +  interpret X: prob_space "S\<lparr>measure := extreal\<circ>distribution X\<rparr>"
   1.367 +    by (rule distribution_prob_space) fact
   1.368 +  interpret Y: prob_space "T\<lparr>measure := extreal\<circ>distribution Y\<rparr>"
   1.369 +    by (rule distribution_prob_space) fact
   1.370 +  interpret XY: pair_prob_space "S\<lparr>measure := extreal\<circ>distribution X\<rparr>" "T\<lparr>measure := extreal\<circ>distribution Y\<rparr>" by default
   1.371 +  interpret XY: information_space XY.P b by default (rule b_gt_1)
   1.372 +
   1.373 +  let ?J = "XY.P\<lparr> measure := (extreal\<circ>joint_distribution X Y) \<rparr>"
   1.374 +  interpret J: prob_space ?J
   1.375 +    using rvs by (intro joint_distribution_prob_space) auto
   1.376 +
   1.377 +  assume ac: "measure_space.absolutely_continuous P (extreal\<circ>joint_distribution X Y)"
   1.378 +  assume int: "integrable (P\<lparr>measure := (extreal\<circ>joint_distribution X Y)\<rparr>)
   1.379 +        (entropy_density b P (extreal\<circ>joint_distribution X Y))"
   1.380 +  assume I_eq_0: "mutual_information b S T X Y = 0"
   1.381 +
   1.382 +  have eq: "\<forall>A\<in>sets XY.P. (extreal \<circ> joint_distribution X Y) A = XY.\<mu> A"
   1.383 +  proof (rule XY.KL_eq_0_imp)
   1.384 +    show "prob_space ?J" by default
   1.385 +    show "XY.absolutely_continuous (extreal\<circ>joint_distribution X Y)"
   1.386 +      using ac by (simp add: P_def)
   1.387 +    show "integrable ?J (entropy_density b XY.P (extreal\<circ>joint_distribution X Y))"
   1.388 +      using int by (simp add: P_def)
   1.389 +    show "KL_divergence b XY.P (extreal\<circ>joint_distribution X Y) = 0"
   1.390 +      using I_eq_0 unfolding mutual_information_def by (simp add: P_def)
   1.391 +  qed
   1.392 +
   1.393 +  { fix S X assume "sigma_algebra S"
   1.394 +    interpret S: sigma_algebra S by fact
   1.395 +    have "Int_stable \<lparr>space = space M, sets = {X -` A \<inter> space M |A. A \<in> sets S}\<rparr>"
   1.396 +    proof (safe intro!: Int_stableI)
   1.397 +      fix A B assume "A \<in> sets S" "B \<in> sets S"
   1.398 +      then show "\<exists>C. (X -` A \<inter> space M) \<inter> (X -` B \<inter> space M) = (X -` C \<inter> space M) \<and> C \<in> sets S"
   1.399 +        by (intro exI[of _ "A \<inter> B"]) auto
   1.400 +    qed }
   1.401 +  note Int_stable = this
   1.402 +
   1.403 +  show "indep_var S X T Y" unfolding indep_var_eq
   1.404 +  proof (intro conjI indep_set_sigma_sets Int_stable)
   1.405 +    show "indep_set {X -` A \<inter> space M |A. A \<in> sets S} {Y -` A \<inter> space M |A. A \<in> sets T}"
   1.406 +    proof (safe intro!: indep_setI)
   1.407 +      { fix A assume "A \<in> sets S" then show "X -` A \<inter> space M \<in> sets M"
   1.408 +        using `X \<in> measurable M S` by (auto intro: measurable_sets) }
   1.409 +      { fix A assume "A \<in> sets T" then show "Y -` A \<inter> space M \<in> sets M"
   1.410 +        using `Y \<in> measurable M T` by (auto intro: measurable_sets) }
   1.411 +    next
   1.412 +      fix A B assume ab: "A \<in> sets S" "B \<in> sets T"
   1.413 +      have "extreal (prob ((X -` A \<inter> space M) \<inter> (Y -` B \<inter> space M))) =
   1.414 +        extreal (joint_distribution X Y (A \<times> B))"
   1.415 +        unfolding distribution_def
   1.416 +        by (intro arg_cong[where f="\<lambda>C. extreal (prob C)"]) auto
   1.417 +      also have "\<dots> = XY.\<mu> (A \<times> B)"
   1.418 +        using ab eq by (auto simp: XY.finite_measure_eq)
   1.419 +      also have "\<dots> = extreal (distribution X A) * extreal (distribution Y B)"
   1.420 +        using ab by (simp add: XY.pair_measure_times)
   1.421 +      finally show "prob ((X -` A \<inter> space M) \<inter> (Y -` B \<inter> space M)) =
   1.422 +        prob (X -` A \<inter> space M) * prob (Y -` B \<inter> space M)"
   1.423 +        unfolding distribution_def by simp
   1.424 +    qed
   1.425 +  qed fact+
   1.426 +qed
   1.427 +
   1.428 +lemma (in information_space) mutual_information_commute_generic:
   1.429 +  assumes X: "random_variable S X" and Y: "random_variable T Y"
   1.430 +  assumes ac: "measure_space.absolutely_continuous
   1.431 +    (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)"
   1.432 +  shows "mutual_information b S T X Y = mutual_information b T S Y X"
   1.433 +proof -
   1.434 +  let ?S = "S\<lparr>measure := extreal\<circ>distribution X\<rparr>" and ?T = "T\<lparr>measure := extreal\<circ>distribution Y\<rparr>"
   1.435 +  interpret S: prob_space ?S using X by (rule distribution_prob_space)
   1.436 +  interpret T: prob_space ?T using Y by (rule distribution_prob_space)
   1.437 +  interpret P: pair_prob_space ?S ?T ..
   1.438 +  interpret Q: pair_prob_space ?T ?S ..
   1.439 +  show ?thesis
   1.440 +    unfolding mutual_information_def
   1.441 +  proof (intro Q.KL_divergence_vimage[OF Q.measure_preserving_swap _ _ _ _ ac b_gt_1])
   1.442 +    show "(\<lambda>(x,y). (y,x)) \<in> measure_preserving
   1.443 +      (P.P \<lparr> measure := extreal\<circ>joint_distribution X Y\<rparr>) (Q.P \<lparr> measure := extreal\<circ>joint_distribution Y X\<rparr>)"
   1.444 +      using X Y unfolding measurable_def
   1.445 +      unfolding measure_preserving_def using P.pair_sigma_algebra_swap_measurable
   1.446 +      by (auto simp add: space_pair_measure distribution_def intro!: arg_cong[where f=\<mu>'])
   1.447 +    have "prob_space (P.P\<lparr> measure := extreal\<circ>joint_distribution X Y\<rparr>)"
   1.448 +      using X Y by (auto intro!: distribution_prob_space random_variable_pairI)
   1.449 +    then show "measure_space (P.P\<lparr> measure := extreal\<circ>joint_distribution X Y\<rparr>)"
   1.450 +      unfolding prob_space_def by simp
   1.451 +  qed auto
   1.452 +qed
   1.453 +
   1.454  definition (in prob_space)
   1.455    "entropy b s X = mutual_information b s s X X"
   1.456  
   1.457 @@ -356,32 +696,6 @@
   1.458      unfolding mutual_information_def .
   1.459  qed
   1.460  
   1.461 -lemma (in information_space) mutual_information_commute_generic:
   1.462 -  assumes X: "random_variable S X" and Y: "random_variable T Y"
   1.463 -  assumes ac: "measure_space.absolutely_continuous
   1.464 -    (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)"
   1.465 -  shows "mutual_information b S T X Y = mutual_information b T S Y X"
   1.466 -proof -
   1.467 -  let ?S = "S\<lparr>measure := extreal\<circ>distribution X\<rparr>" and ?T = "T\<lparr>measure := extreal\<circ>distribution Y\<rparr>"
   1.468 -  interpret S: prob_space ?S using X by (rule distribution_prob_space)
   1.469 -  interpret T: prob_space ?T using Y by (rule distribution_prob_space)
   1.470 -  interpret P: pair_prob_space ?S ?T ..
   1.471 -  interpret Q: pair_prob_space ?T ?S ..
   1.472 -  show ?thesis
   1.473 -    unfolding mutual_information_def
   1.474 -  proof (intro Q.KL_divergence_vimage[OF Q.measure_preserving_swap _ _ _ _ ac b_gt_1])
   1.475 -    show "(\<lambda>(x,y). (y,x)) \<in> measure_preserving
   1.476 -      (P.P \<lparr> measure := extreal\<circ>joint_distribution X Y\<rparr>) (Q.P \<lparr> measure := extreal\<circ>joint_distribution Y X\<rparr>)"
   1.477 -      using X Y unfolding measurable_def
   1.478 -      unfolding measure_preserving_def using P.pair_sigma_algebra_swap_measurable
   1.479 -      by (auto simp add: space_pair_measure distribution_def intro!: arg_cong[where f=\<mu>'])
   1.480 -    have "prob_space (P.P\<lparr> measure := extreal\<circ>joint_distribution X Y\<rparr>)"
   1.481 -      using X Y by (auto intro!: distribution_prob_space random_variable_pairI)
   1.482 -    then show "measure_space (P.P\<lparr> measure := extreal\<circ>joint_distribution X Y\<rparr>)"
   1.483 -      unfolding prob_space_def by simp
   1.484 -  qed auto
   1.485 -qed
   1.486 -
   1.487  lemma (in information_space) mutual_information_commute:
   1.488    assumes X: "finite_random_variable S X" and Y: "finite_random_variable T Y"
   1.489    shows "mutual_information b S T X Y = mutual_information b T S Y X"