src/HOL/Probability/ex/Koepf_Duermuth_Countermeasure.thy
changeset 41981 cdf7693bbe08
parent 41689 3e39b0e730d6
child 42256 461624ffd382
equal deleted inserted replaced
41980:28b51effc5ed 41981:cdf7693bbe08
     1 (* Author: Johannes Hölzl, TU München *)
     1 (* Author: Johannes Hölzl, TU München *)
     2 
     2 
     3 header {* Formalization of a Countermeasure by Koepf \& Duermuth 2009 *}
     3 header {* Formalization of a Countermeasure by Koepf \& Duermuth 2009 *}
     4 
     4 
     5 theory Koepf_Duermuth_Countermeasure
     5 theory Koepf_Duermuth_Countermeasure
     6   imports Information "~~/src/HOL/Library/Permutation"
     6   imports "~~/src/HOL/Probability/Information" "~~/src/HOL/Library/Permutation"
     7 begin
     7 begin
     8 
     8 
     9 lemma
     9 lemma
    10   fixes p u :: "'a \<Rightarrow> real"
    10   fixes p u :: "'a \<Rightarrow> real"
    11   assumes "1 < b"
    11   assumes "1 < b"
   199   and b_gt_1[simp, intro]: "1 < b"
   199   and b_gt_1[simp, intro]: "1 < b"
   200 
   200 
   201 lemma (in finite_information) positive_p_sum[simp]: "0 \<le> setsum p X"
   201 lemma (in finite_information) positive_p_sum[simp]: "0 \<le> setsum p X"
   202    by (auto intro!: setsum_nonneg)
   202    by (auto intro!: setsum_nonneg)
   203 
   203 
   204 sublocale finite_information \<subseteq> finite_measure_space "\<lparr> space = \<Omega>, sets = Pow \<Omega>, measure = \<lambda>S. Real (setsum p S)\<rparr>"
   204 sublocale finite_information \<subseteq> finite_measure_space "\<lparr> space = \<Omega>, sets = Pow \<Omega>, measure = \<lambda>S. extreal (setsum p S)\<rparr>"
   205   by (rule finite_measure_spaceI) (simp_all add: setsum_Un_disjoint finite_subset)
   205   by (rule finite_measure_spaceI) (simp_all add: setsum_Un_disjoint finite_subset)
   206 
   206 
   207 sublocale finite_information \<subseteq> finite_prob_space "\<lparr> space = \<Omega>, sets = Pow \<Omega>, measure = \<lambda>S. Real (setsum p S)\<rparr>"
   207 sublocale finite_information \<subseteq> finite_prob_space "\<lparr> space = \<Omega>, sets = Pow \<Omega>, measure = \<lambda>S. extreal (setsum p S)\<rparr>"
       
   208   by default (simp add: one_extreal_def)
       
   209 
       
   210 sublocale finite_information \<subseteq> information_space "\<lparr> space = \<Omega>, sets = Pow \<Omega>, measure = \<lambda>S. extreal (setsum p S)\<rparr>" b
   208   by default simp
   211   by default simp
   209 
   212 
   210 sublocale finite_information \<subseteq> information_space "\<lparr> space = \<Omega>, sets = Pow \<Omega>, measure = \<lambda>S. Real (setsum p S)\<rparr>" b
   213 lemma (in finite_information) \<mu>'_eq: "A \<subseteq> \<Omega> \<Longrightarrow> \<mu>' A = setsum p A"
   211   by default simp
   214   unfolding \<mu>'_def by auto
   212 
   215 
   213 locale koepf_duermuth = K: finite_information keys K b + M: finite_information messages M b
   216 locale koepf_duermuth = K: finite_information keys K b + M: finite_information messages M b
   214     for b :: real
   217     for b :: real
   215     and keys :: "'key set" and K :: "'key \<Rightarrow> real"
   218     and keys :: "'key set" and K :: "'key \<Rightarrow> real"
   216     and messages :: "'message set" and M :: "'message \<Rightarrow> real" +
   219     and messages :: "'message set" and M :: "'message \<Rightarrow> real" +
   256 qed auto
   259 qed auto
   257 
   260 
   258 lemma SIGMA_image_vimage:
   261 lemma SIGMA_image_vimage:
   259   "snd ` (SIGMA x:f`X. f -` {x} \<inter> X) = X"
   262   "snd ` (SIGMA x:f`X. f -` {x} \<inter> X) = X"
   260   by (auto simp: image_iff)
   263   by (auto simp: image_iff)
   261 
       
   262 lemma zero_le_eq_True: "0 \<le> (x::pextreal) \<longleftrightarrow> True" by simp
       
   263 
       
   264 lemma Real_setprod:
       
   265   assumes"\<And>i. i\<in>X \<Longrightarrow> 0 \<le> f i"
       
   266   shows "(\<Prod>i\<in>X. Real (f i)) = Real (\<Prod>i\<in>X. f i)"
       
   267 proof cases
       
   268   assume "finite X"
       
   269   from this assms show ?thesis by induct (auto simp: mult_le_0_iff)
       
   270 qed simp
       
   271 
   264 
   272 lemma inj_Cons[simp]: "\<And>X. inj_on (\<lambda>(xs, x). x#xs) X" by (auto intro!: inj_onI)
   265 lemma inj_Cons[simp]: "\<And>X. inj_on (\<lambda>(xs, x). x#xs) X" by (auto intro!: inj_onI)
   273 
   266 
   274 lemma setprod_setsum_distrib_lists:
   267 lemma setprod_setsum_distrib_lists:
   275   fixes P and S :: "'a set" and f :: "'a \<Rightarrow> _::semiring_0" assumes "finite S"
   268   fixes P and S :: "'a set" and f :: "'a \<Rightarrow> _::semiring_0" assumes "finite S"
   321 
   314 
   322 abbreviation
   315 abbreviation
   323  "p A \<equiv> setsum P A"
   316  "p A \<equiv> setsum P A"
   324 
   317 
   325 abbreviation probability ("\<P>'(_') _") where
   318 abbreviation probability ("\<P>'(_') _") where
   326  "\<P>(X) x \<equiv> real (distribution X x)"
   319  "\<P>(X) x \<equiv> distribution X x"
   327 
   320 
   328 abbreviation joint_probability ("\<P>'(_, _') _") where
   321 abbreviation joint_probability ("\<P>'(_, _') _") where
   329  "\<P>(X, Y) x \<equiv> real (joint_distribution X Y x)"
   322  "\<P>(X, Y) x \<equiv> joint_distribution X Y x"
   330 
   323 
   331 abbreviation conditional_probability ("\<P>'(_|_') _") where
   324 abbreviation conditional_probability ("\<P>'(_|_') _") where
   332  "\<P>(X|Y) x \<equiv> \<P>(X, Y) x / \<P>(Y) (snd`x)"
   325  "\<P>(X|Y) x \<equiv> \<P>(X, Y) x / \<P>(Y) (snd`x)"
   333 
   326 
   334 notation
   327 notation
   353 lemma \<P>_k: assumes "k \<in> keys" shows "\<P>(fst) {k} = K k"
   346 lemma \<P>_k: assumes "k \<in> keys" shows "\<P>(fst) {k} = K k"
   354 proof -
   347 proof -
   355   from assms have *:
   348   from assms have *:
   356       "fst -` {k} \<inter> msgs = {k}\<times>{ms. length ms = n \<and> (\<forall>M\<in>set ms. M \<in> messages)}"
   349       "fst -` {k} \<inter> msgs = {k}\<times>{ms. length ms = n \<and> (\<forall>M\<in>set ms. M \<in> messages)}"
   357     unfolding msgs_def by auto
   350     unfolding msgs_def by auto
   358   show "\<P>(fst) {k} = K k" unfolding distribution_def
   351   show "\<P>(fst) {k} = K k"
   359     apply (simp add: *) unfolding P_def
   352     apply (simp add: \<mu>'_eq distribution_def)
       
   353     apply (simp add: * P_def)
   360     apply (simp add: setsum_cartesian_product')
   354     apply (simp add: setsum_cartesian_product')
   361     using setprod_setsum_distrib_lists[OF M.finite_space, of M n "\<lambda>x x. True"]
   355     using setprod_setsum_distrib_lists[OF M.finite_space, of M n "\<lambda>x x. True"] `k \<in> keys`
   362     by (simp add: setsum_right_distrib[symmetric] subset_eq setprod_1)
   356     by (auto simp add: setsum_right_distrib[symmetric] subset_eq setprod_1)
   363 qed
   357 qed
   364 
   358 
   365 lemma fst_image_msgs[simp]: "fst`msgs = keys"
   359 lemma fst_image_msgs[simp]: "fst`msgs = keys"
   366 proof -
   360 proof -
   367   from M.not_empty obtain m where "m \<in> messages" by auto
   361   from M.not_empty obtain m where "m \<in> messages" by auto
   388       then have **: "\<And>ms. length ms = n \<Longrightarrow> OB (k, ms) = obs \<longleftrightarrow> (\<forall>i<n. observe k (ms!i) = obs ! i)"
   382       then have **: "\<And>ms. length ms = n \<Longrightarrow> OB (k, ms) = obs \<longleftrightarrow> (\<forall>i<n. observe k (ms!i) = obs ! i)"
   389         unfolding OB_def msgs_def by (simp add: image_iff list_eq_iff_nth_eq)
   383         unfolding OB_def msgs_def by (simp add: image_iff list_eq_iff_nth_eq)
   390 
   384 
   391       have "\<P>(OB, fst) {(obs, k)} / K k =
   385       have "\<P>(OB, fst) {(obs, k)} / K k =
   392           p ({k}\<times>{ms. (k,ms) \<in> msgs \<and> OB (k,ms) = obs}) / K k"
   386           p ({k}\<times>{ms. (k,ms) \<in> msgs \<and> OB (k,ms) = obs}) / K k"
   393         unfolding distribution_def by (auto intro!: arg_cong[where f=p])
   387         apply (simp add: distribution_def \<mu>'_eq) by (auto intro!: arg_cong[where f=p])
   394       also have "\<dots> =
   388       also have "\<dots> =
   395           (\<Prod>i<n. \<Sum>m\<in>{m\<in>messages. observe k m = obs ! i}. M m)"
   389           (\<Prod>i<n. \<Sum>m\<in>{m\<in>messages. observe k m = obs ! i}. M m)"
   396         unfolding P_def using `K k \<noteq> 0` `k \<in> keys`
   390         unfolding P_def using `K k \<noteq> 0` `k \<in> keys`
   397         apply (simp add: setsum_cartesian_product' setsum_divide_distrib msgs_def ** cong: conj_cong)
   391         apply (simp add: setsum_cartesian_product' setsum_divide_distrib msgs_def ** cong: conj_cong)
   398         apply (subst setprod_setsum_distrib_lists[OF M.finite_space, unfolded subset_eq]) ..
   392         apply (subst setprod_setsum_distrib_lists[OF M.finite_space, unfolded subset_eq]) ..
   413       (\<Union>obs'\<in>?S obs. ((\<lambda>x. (OB x, fst x)) -` {(obs', k)} \<inter> msgs))" by auto
   407       (\<Union>obs'\<in>?S obs. ((\<lambda>x. (OB x, fst x)) -` {(obs', k)} \<inter> msgs))" by auto
   414     have df: "disjoint_family_on (\<lambda>obs'. (\<lambda>x. (OB x, fst x)) -` {(obs', k)} \<inter> msgs) (?S obs)"
   408     have df: "disjoint_family_on (\<lambda>obs'. (\<lambda>x. (OB x, fst x)) -` {(obs', k)} \<inter> msgs) (?S obs)"
   415       unfolding disjoint_family_on_def by auto
   409       unfolding disjoint_family_on_def by auto
   416     have "\<P>(t\<circ>OB, fst) {(t obs, k)} = (\<Sum>obs'\<in>?S obs. \<P>(OB, fst) {(obs', k)})"
   410     have "\<P>(t\<circ>OB, fst) {(t obs, k)} = (\<Sum>obs'\<in>?S obs. \<P>(OB, fst) {(obs', k)})"
   417       unfolding distribution_def comp_def
   411       unfolding distribution_def comp_def
   418       using real_finite_measure_finite_Union[OF _ df]
   412       using finite_measure_finite_Union[OF _ _ df]
   419       by (force simp add: * intro!: setsum_nonneg)
   413       by (force simp add: * intro!: setsum_nonneg)
   420     also have "(\<Sum>obs'\<in>?S obs. \<P>(OB, fst) {(obs', k)}) = real (card (?S obs)) * \<P>(OB, fst) {(obs, k)}"
   414     also have "(\<Sum>obs'\<in>?S obs. \<P>(OB, fst) {(obs', k)}) = real (card (?S obs)) * \<P>(OB, fst) {(obs, k)}"
   421       by (simp add: t_eq_imp[OF `k \<in> keys` `K k \<noteq> 0` obs] real_eq_of_nat)
   415       by (simp add: t_eq_imp[OF `k \<in> keys` `K k \<noteq> 0` obs] real_eq_of_nat)
   422     finally have "\<P>(t\<circ>OB, fst) {(t obs, k)} = real (card (?S obs)) * \<P>(OB, fst) {(obs, k)}" .}
   416     finally have "\<P>(t\<circ>OB, fst) {(t obs, k)} = real (card (?S obs)) * \<P>(OB, fst) {(obs, k)}" .}
   423   note P_t_eq_P_OB = this
   417   note P_t_eq_P_OB = this
   431   { fix k obs assume "k \<in> keys" and obs: "obs \<in> OB`msgs"
   425   { fix k obs assume "k \<in> keys" and obs: "obs \<in> OB`msgs"
   432     then have "t -`{t obs} \<inter> OB`msgs \<noteq> {}" (is "?S \<noteq> {}") by auto
   426     then have "t -`{t obs} \<inter> OB`msgs \<noteq> {}" (is "?S \<noteq> {}") by auto
   433     then have "real (card ?S) \<noteq> 0" by auto
   427     then have "real (card ?S) \<noteq> 0" by auto
   434 
   428 
   435     have "\<P>(fst | t\<circ>OB) {(k, t obs)} = \<P>(t\<circ>OB | fst) {(t obs, k)} * \<P>(fst) {k} / \<P>(t\<circ>OB) {t obs}"
   429     have "\<P>(fst | t\<circ>OB) {(k, t obs)} = \<P>(t\<circ>OB | fst) {(t obs, k)} * \<P>(fst) {k} / \<P>(t\<circ>OB) {t obs}"
   436       using real_distribution_order'[of fst k "t\<circ>OB" "t obs"]
   430       using distribution_order(7,8)[where X=fst and x=k and Y="t\<circ>OB" and y="t obs"]
   437       by (subst joint_distribution_commute) auto
   431       by (subst joint_distribution_commute) auto
   438     also have "\<P>(t\<circ>OB) {t obs} = (\<Sum>k'\<in>keys. \<P>(t\<circ>OB|fst) {(t obs, k')} * \<P>(fst) {k'})"
   432     also have "\<P>(t\<circ>OB) {t obs} = (\<Sum>k'\<in>keys. \<P>(t\<circ>OB|fst) {(t obs, k')} * \<P>(fst) {k'})"
   439       using setsum_real_distribution(2)[of "t\<circ>OB" fst "t obs", symmetric]
   433       using setsum_distribution(2)[of "t\<circ>OB" fst "t obs", symmetric]
   440       using real_distribution_order'[of fst _ "t\<circ>OB" "t obs"] by (auto intro!: setsum_cong)
   434       by (auto intro!: setsum_cong distribution_order(8))
   441     also have "\<P>(t\<circ>OB | fst) {(t obs, k)} * \<P>(fst) {k} / (\<Sum>k'\<in>keys. \<P>(t\<circ>OB|fst) {(t obs, k')} * \<P>(fst) {k'}) =
   435     also have "\<P>(t\<circ>OB | fst) {(t obs, k)} * \<P>(fst) {k} / (\<Sum>k'\<in>keys. \<P>(t\<circ>OB|fst) {(t obs, k')} * \<P>(fst) {k'}) =
   442       \<P>(OB | fst) {(obs, k)} * \<P>(fst) {k} / (\<Sum>k'\<in>keys. \<P>(OB|fst) {(obs, k')} * \<P>(fst) {k'})"
   436       \<P>(OB | fst) {(obs, k)} * \<P>(fst) {k} / (\<Sum>k'\<in>keys. \<P>(OB|fst) {(obs, k')} * \<P>(fst) {k'})"
   443       using CP_t_K[OF `k\<in>keys` obs] CP_t_K[OF _ obs] `real (card ?S) \<noteq> 0`
   437       using CP_t_K[OF `k\<in>keys` obs] CP_t_K[OF _ obs] `real (card ?S) \<noteq> 0`
   444       by (simp only: setsum_right_distrib[symmetric] ac_simps
   438       by (simp only: setsum_right_distrib[symmetric] ac_simps
   445           mult_divide_mult_cancel_left[OF `real (card ?S) \<noteq> 0`]
   439           mult_divide_mult_cancel_left[OF `real (card ?S) \<noteq> 0`]
   446         cong: setsum_cong)
   440         cong: setsum_cong)
   447     also have "(\<Sum>k'\<in>keys. \<P>(OB|fst) {(obs, k')} * \<P>(fst) {k'}) = \<P>(OB) {obs}"
   441     also have "(\<Sum>k'\<in>keys. \<P>(OB|fst) {(obs, k')} * \<P>(fst) {k'}) = \<P>(OB) {obs}"
   448       using setsum_real_distribution(2)[of OB fst obs, symmetric]
   442       using setsum_distribution(2)[of OB fst obs, symmetric]
   449       using real_distribution_order'[of fst _ OB obs] by (auto intro!: setsum_cong)
   443       by (auto intro!: setsum_cong distribution_order(8))
   450     also have "\<P>(OB | fst) {(obs, k)} * \<P>(fst) {k} / \<P>(OB) {obs} = \<P>(fst | OB) {(k, obs)}"
   444     also have "\<P>(OB | fst) {(obs, k)} * \<P>(fst) {k} / \<P>(OB) {obs} = \<P>(fst | OB) {(k, obs)}"
   451       using real_distribution_order'[of fst k OB obs]
   445       by (subst joint_distribution_commute) (auto intro!: distribution_order(8))
   452       by (subst joint_distribution_commute) auto
       
   453     finally have "\<P>(fst | t\<circ>OB) {(k, t obs)} = \<P>(fst | OB) {(k, obs)}" . }
   446     finally have "\<P>(fst | t\<circ>OB) {(k, t obs)} = \<P>(fst | OB) {(k, obs)}" . }
   454   note CP_T_eq_CP_O = this
   447   note CP_T_eq_CP_O = this
   455 
   448 
   456   let "?H obs" = "(\<Sum>k\<in>keys. \<P>(fst|OB) {(k, obs)} * log b (\<P>(fst|OB) {(k, obs)})) :: real"
   449   let "?H obs" = "(\<Sum>k\<in>keys. \<P>(fst|OB) {(k, obs)} * log b (\<P>(fst|OB) {(k, obs)})) :: real"
   457   let "?Ht obs" = "(\<Sum>k\<in>keys. \<P>(fst|t\<circ>OB) {(k, obs)} * log b (\<P>(fst|t\<circ>OB) {(k, obs)})) :: real"
   450   let "?Ht obs" = "(\<Sum>k\<in>keys. \<P>(fst|t\<circ>OB) {(k, obs)} * log b (\<P>(fst|t\<circ>OB) {(k, obs)})) :: real"
   470       (\<Union>obs\<in>?S (OB x). OB -` {obs} \<inter> msgs)" by auto
   463       (\<Union>obs\<in>?S (OB x). OB -` {obs} \<inter> msgs)" by auto
   471     have df: "disjoint_family_on (\<lambda>obs. OB -` {obs} \<inter> msgs) (?S (OB x))"
   464     have df: "disjoint_family_on (\<lambda>obs. OB -` {obs} \<inter> msgs) (?S (OB x))"
   472       unfolding disjoint_family_on_def by auto
   465       unfolding disjoint_family_on_def by auto
   473     have "\<P>(t\<circ>OB) {t (OB x)} = (\<Sum>obs\<in>?S (OB x). \<P>(OB) {obs})"
   466     have "\<P>(t\<circ>OB) {t (OB x)} = (\<Sum>obs\<in>?S (OB x). \<P>(OB) {obs})"
   474       unfolding distribution_def comp_def
   467       unfolding distribution_def comp_def
   475       using real_finite_measure_finite_Union[OF _ df]
   468       using finite_measure_finite_Union[OF _ _ df]
   476       by (force simp add: * intro!: setsum_nonneg) }
   469       by (force simp add: * intro!: setsum_nonneg) }
   477   note P_t_sum_P_O = this
   470   note P_t_sum_P_O = this
   478 
   471 
   479   txt {* Lemma 3 *}
   472   txt {* Lemma 3 *}
   480   have "\<H>(fst | OB) = -(\<Sum>obs\<in>OB`msgs. \<P>(OB) {obs} * ?Ht (t obs))"
   473   have "\<H>(fst | OB) = -(\<Sum>obs\<in>OB`msgs. \<P>(OB) {obs} * ?Ht (t obs))"