src/HOL/Probability/ex/Koepf_Duermuth_Countermeasure.thy
author paulson <lp15@cam.ac.uk>
Fri Nov 13 12:27:13 2015 +0000 (2015-11-13)
changeset 61649 268d88ec9087
parent 61609 77b453bd616f
child 61808 fc1556774cfe
permissions -rw-r--r--
Tweaks for "real": Removal of [iff] status for some lemmas, adding [simp] for others. Plus fixes.
hoelzl@41689
     1
(* Author: Johannes Hölzl, TU München *)
hoelzl@40859
     2
wenzelm@58876
     3
section {* Formalization of a Countermeasure by Koepf \& Duermuth 2009 *}
hoelzl@40859
     4
hoelzl@40859
     5
theory Koepf_Duermuth_Countermeasure
hoelzl@41981
     6
  imports "~~/src/HOL/Probability/Information" "~~/src/HOL/Library/Permutation"
hoelzl@40859
     7
begin
hoelzl@40859
     8
hoelzl@45715
     9
lemma SIGMA_image_vimage:
hoelzl@45715
    10
  "snd ` (SIGMA x:f`X. f -` {x} \<inter> X) = X"
hoelzl@45715
    11
  by (auto simp: image_iff)
hoelzl@40859
    12
hoelzl@45715
    13
declare inj_split_Cons[simp]
hoelzl@40859
    14
hoelzl@42256
    15
definition extensionalD :: "'b \<Rightarrow> 'a set \<Rightarrow> ('a \<Rightarrow> 'b) set" where
hoelzl@42256
    16
  "extensionalD d A = {f. \<forall>x. x \<notin> A \<longrightarrow> f x = d}"
hoelzl@40859
    17
hoelzl@42256
    18
lemma extensionalD_empty[simp]: "extensionalD d {} = {\<lambda>x. d}"
hoelzl@42256
    19
  unfolding extensionalD_def by (simp add: set_eq_iff fun_eq_iff)
hoelzl@40859
    20
hoelzl@40859
    21
lemma funset_eq_UN_fun_upd_I:
hoelzl@40859
    22
  assumes *: "\<And>f. f \<in> F (insert a A) \<Longrightarrow> f(a := d) \<in> F A"
hoelzl@40859
    23
  and **: "\<And>f. f \<in> F (insert a A) \<Longrightarrow> f a \<in> G (f(a:=d))"
hoelzl@40859
    24
  and ***: "\<And>f x. \<lbrakk> f \<in> F A ; x \<in> G f \<rbrakk> \<Longrightarrow> f(a:=x) \<in> F (insert a A)"
hoelzl@40859
    25
  shows "F (insert a A) = (\<Union>f\<in>F A. fun_upd f a ` (G f))"
hoelzl@40859
    26
proof safe
hoelzl@40859
    27
  fix f assume f: "f \<in> F (insert a A)"
hoelzl@40859
    28
  show "f \<in> (\<Union>f\<in>F A. fun_upd f a ` G f)"
hoelzl@40859
    29
  proof (rule UN_I[of "f(a := d)"])
hoelzl@40859
    30
    show "f(a := d) \<in> F A" using *[OF f] .
hoelzl@40859
    31
    show "f \<in> fun_upd (f(a:=d)) a ` G (f(a:=d))"
hoelzl@40859
    32
    proof (rule image_eqI[of _ _ "f a"])
hoelzl@40859
    33
      show "f a \<in> G (f(a := d))" using **[OF f] .
hoelzl@40859
    34
    qed simp
hoelzl@40859
    35
  qed
hoelzl@40859
    36
next
hoelzl@40859
    37
  fix f x assume "f \<in> F A" "x \<in> G f"
hoelzl@40859
    38
  from ***[OF this] show "f(a := x) \<in> F (insert a A)" .
hoelzl@40859
    39
qed
hoelzl@40859
    40
hoelzl@42256
    41
lemma extensionalD_insert[simp]:
hoelzl@40859
    42
  assumes "a \<notin> A"
hoelzl@42256
    43
  shows "extensionalD d (insert a A) \<inter> (insert a A \<rightarrow> B) = (\<Union>f \<in> extensionalD d A \<inter> (A \<rightarrow> B). (\<lambda>b. f(a := b)) ` B)"
hoelzl@40859
    44
  apply (rule funset_eq_UN_fun_upd_I)
hoelzl@40859
    45
  using assms
hoelzl@42256
    46
  by (auto intro!: inj_onI dest: inj_onD split: split_if_asm simp: extensionalD_def)
hoelzl@40859
    47
hoelzl@42256
    48
lemma finite_extensionalD_funcset[simp, intro]:
hoelzl@40859
    49
  assumes "finite A" "finite B"
hoelzl@42256
    50
  shows "finite (extensionalD d A \<inter> (A \<rightarrow> B))"
hoelzl@40859
    51
  using assms by induct auto
hoelzl@40859
    52
hoelzl@40859
    53
lemma fun_upd_eq_iff: "f(a := b) = g(a := b') \<longleftrightarrow> b = b' \<and> f(a := d) = g(a := d)"
hoelzl@41689
    54
  by (auto simp: fun_eq_iff)
hoelzl@40859
    55
hoelzl@40859
    56
lemma card_funcset:
hoelzl@40859
    57
  assumes "finite A" "finite B"
hoelzl@42256
    58
  shows "card (extensionalD d A \<inter> (A \<rightarrow> B)) = card B ^ card A"
hoelzl@40859
    59
using `finite A` proof induct
hoelzl@42256
    60
  case (insert a A) thus ?case unfolding extensionalD_insert[OF `a \<notin> A`]
hoelzl@40859
    61
  proof (subst card_UN_disjoint, safe, simp_all)
hoelzl@42256
    62
    show "finite (extensionalD d A \<inter> (A \<rightarrow> B))" "\<And>f. finite (fun_upd f a ` B)"
hoelzl@40859
    63
      using `finite B` `finite A` by simp_all
hoelzl@40859
    64
  next
hoelzl@40859
    65
    fix f g b b' assume "f \<noteq> g" and eq: "f(a := b) = g(a := b')" and
hoelzl@42256
    66
      ext: "f \<in> extensionalD d A" "g \<in> extensionalD d A"
hoelzl@40859
    67
    have "f a = d" "g a = d"
hoelzl@42256
    68
      using ext `a \<notin> A` by (auto simp add: extensionalD_def)
hoelzl@40859
    69
    with `f \<noteq> g` eq show False unfolding fun_upd_eq_iff[of _ _ b _ _ d]
hoelzl@40859
    70
      by (auto simp: fun_upd_idem fun_upd_eq_iff)
hoelzl@40859
    71
  next
hoelzl@42256
    72
    { fix f assume "f \<in> extensionalD d A \<inter> (A \<rightarrow> B)"
hoelzl@40859
    73
      have "card (fun_upd f a ` B) = card B"
hoelzl@40859
    74
      proof (auto intro!: card_image inj_onI)
hoelzl@40859
    75
        fix b b' assume "f(a := b) = f(a := b')"
hoelzl@40859
    76
        from fun_upd_eq_iff[THEN iffD1, OF this] show "b = b'" by simp
hoelzl@40859
    77
      qed }
hoelzl@42256
    78
    then show "(\<Sum>i\<in>extensionalD d A \<inter> (A \<rightarrow> B). card (fun_upd i a ` B)) = card B * card B ^ card A"
hoelzl@40859
    79
      using insert by simp
hoelzl@40859
    80
  qed
hoelzl@40859
    81
qed simp
hoelzl@40859
    82
hoelzl@45715
    83
lemma zero_notin_Suc_image[simp]: "0 \<notin> Suc ` A"
hoelzl@45715
    84
  by auto
hoelzl@40859
    85
hoelzl@45715
    86
lemma setprod_setsum_distrib_lists:
hoelzl@45715
    87
  fixes P and S :: "'a set" and f :: "'a \<Rightarrow> _::semiring_0" assumes "finite S"
hoelzl@45715
    88
  shows "(\<Sum>ms\<in>{ms. set ms \<subseteq> S \<and> length ms = n \<and> (\<forall>i<n. P i (ms!i))}. \<Prod>x<n. f (ms ! x)) =
hoelzl@45715
    89
         (\<Prod>i<n. \<Sum>m\<in>{m\<in>S. P i m}. f m)"
hoelzl@45715
    90
proof (induct n arbitrary: P)
hoelzl@45715
    91
  case 0 then show ?case by (simp cong: conj_cong)
hoelzl@45715
    92
next
hoelzl@45715
    93
  case (Suc n)
hoelzl@45715
    94
  have *: "{ms. set ms \<subseteq> S \<and> length ms = Suc n \<and> (\<forall>i<Suc n. P i (ms ! i))} =
hoelzl@45715
    95
    (\<lambda>(xs, x). x#xs) ` ({ms. set ms \<subseteq> S \<and> length ms = n \<and> (\<forall>i<n. P (Suc i) (ms ! i))} \<times> {m\<in>S. P 0 m})"
hoelzl@45715
    96
    apply (auto simp: image_iff length_Suc_conv)
hoelzl@45715
    97
    apply force+
hoelzl@45715
    98
    apply (case_tac i)
hoelzl@45715
    99
    by force+
hoelzl@45715
   100
  show ?case unfolding *
hoelzl@45715
   101
    using Suc[of "\<lambda>i. P (Suc i)"]
haftmann@57418
   102
    by (simp add: setsum.reindex split_conv setsum_cartesian_product'
haftmann@57418
   103
      lessThan_Suc_eq_insert_0 setprod.reindex setsum_left_distrib[symmetric] setsum_right_distrib[symmetric])
hoelzl@45715
   104
qed
hoelzl@45715
   105
hoelzl@47694
   106
declare space_point_measure[simp]
hoelzl@45715
   107
hoelzl@47694
   108
declare sets_point_measure[simp]
hoelzl@45715
   109
hoelzl@47694
   110
lemma measure_point_measure:
hoelzl@47694
   111
  "finite \<Omega> \<Longrightarrow> A \<subseteq> \<Omega> \<Longrightarrow> (\<And>x. x \<in> \<Omega> \<Longrightarrow> 0 \<le> p x) \<Longrightarrow>
hoelzl@47694
   112
    measure (point_measure \<Omega> (\<lambda>x. ereal (p x))) A = (\<Sum>i\<in>A. p i)"
hoelzl@47694
   113
  unfolding measure_def by (subst emeasure_point_measure_finite) auto
hoelzl@45715
   114
hoelzl@40859
   115
locale finite_information =
hoelzl@40859
   116
  fixes \<Omega> :: "'a set"
hoelzl@40859
   117
    and p :: "'a \<Rightarrow> real"
hoelzl@40859
   118
    and b :: real
hoelzl@40859
   119
  assumes finite_space[simp, intro]: "finite \<Omega>"
hoelzl@40859
   120
  and p_sums_1[simp]: "(\<Sum>\<omega>\<in>\<Omega>. p \<omega>) = 1"
hoelzl@40859
   121
  and positive_p[simp, intro]: "\<And>x. 0 \<le> p x"
hoelzl@40859
   122
  and b_gt_1[simp, intro]: "1 < b"
hoelzl@40859
   123
hoelzl@40859
   124
lemma (in finite_information) positive_p_sum[simp]: "0 \<le> setsum p X"
hoelzl@40859
   125
   by (auto intro!: setsum_nonneg)
hoelzl@40859
   126
hoelzl@47694
   127
sublocale finite_information \<subseteq> prob_space "point_measure \<Omega> p"
wenzelm@61169
   128
  by standard (simp add: one_ereal_def emeasure_point_measure_finite)
hoelzl@40859
   129
hoelzl@47694
   130
sublocale finite_information \<subseteq> information_space "point_measure \<Omega> p" b
wenzelm@61169
   131
  by standard simp
hoelzl@40859
   132
hoelzl@47694
   133
lemma (in finite_information) \<mu>'_eq: "A \<subseteq> \<Omega> \<Longrightarrow> prob A = setsum p A"
hoelzl@47694
   134
  by (auto simp: measure_point_measure)
hoelzl@40859
   135
hoelzl@40859
   136
locale koepf_duermuth = K: finite_information keys K b + M: finite_information messages M b
hoelzl@40859
   137
    for b :: real
hoelzl@40859
   138
    and keys :: "'key set" and K :: "'key \<Rightarrow> real"
hoelzl@40859
   139
    and messages :: "'message set" and M :: "'message \<Rightarrow> real" +
hoelzl@40859
   140
  fixes observe :: "'key \<Rightarrow> 'message \<Rightarrow> 'observation"
hoelzl@40859
   141
    and n :: nat
hoelzl@40859
   142
begin
hoelzl@40859
   143
hoelzl@40859
   144
definition msgs :: "('key \<times> 'message list) set" where
hoelzl@45715
   145
  "msgs = keys \<times> {ms. set ms \<subseteq> messages \<and> length ms = n}"
hoelzl@40859
   146
hoelzl@40859
   147
definition P :: "('key \<times> 'message list) \<Rightarrow> real" where
hoelzl@49793
   148
  "P = (\<lambda>(k, ms). K k * (\<Prod>i<n. M (ms ! i)))"
hoelzl@40859
   149
hoelzl@40859
   150
end
hoelzl@40859
   151
hoelzl@40859
   152
sublocale koepf_duermuth \<subseteq> finite_information msgs P b
wenzelm@61169
   153
proof
hoelzl@40859
   154
  show "finite msgs" unfolding msgs_def
hoelzl@45715
   155
    using finite_lists_length_eq[OF M.finite_space, of n]
hoelzl@40859
   156
    by auto
hoelzl@40859
   157
hoelzl@40859
   158
  have [simp]: "\<And>A. inj_on (\<lambda>(xs, n). n # xs) A" by (force intro!: inj_onI)
hoelzl@40859
   159
hoelzl@40859
   160
  note setsum_right_distrib[symmetric, simp]
hoelzl@40859
   161
  note setsum_left_distrib[symmetric, simp]
hoelzl@40859
   162
  note setsum_cartesian_product'[simp]
hoelzl@40859
   163
hoelzl@45715
   164
  have "(\<Sum>ms | set ms \<subseteq> messages \<and> length ms = n. \<Prod>x<length ms. M (ms ! x)) = 1"
hoelzl@40859
   165
  proof (induct n)
hoelzl@40859
   166
    case 0 then show ?case by (simp cong: conj_cong)
hoelzl@40859
   167
  next
hoelzl@40859
   168
    case (Suc n)
hoelzl@40859
   169
    then show ?case
hoelzl@45715
   170
      by (simp add: lists_length_Suc_eq lessThan_Suc_eq_insert_0
haftmann@57418
   171
                    setsum.reindex setprod.reindex)
hoelzl@40859
   172
  qed
hoelzl@40859
   173
  then show "setsum P msgs = 1"
hoelzl@40859
   174
    unfolding msgs_def P_def by simp
hoelzl@40859
   175
  fix x
hoelzl@40859
   176
  have "\<And> A f. 0 \<le> (\<Prod>x\<in>A. M (f x))" by (auto simp: setprod_nonneg)
hoelzl@40859
   177
  then show "0 \<le> P x"
hoelzl@40859
   178
    unfolding P_def by (auto split: prod.split simp: zero_le_mult_iff)
hoelzl@40859
   179
qed auto
hoelzl@40859
   180
hoelzl@40859
   181
context koepf_duermuth
hoelzl@40859
   182
begin
hoelzl@40859
   183
hoelzl@40859
   184
definition observations :: "'observation set" where
hoelzl@40859
   185
  "observations = (\<Union>f\<in>observe ` keys. f ` messages)"
hoelzl@40859
   186
hoelzl@40859
   187
lemma finite_observations[simp, intro]: "finite observations"
hoelzl@40859
   188
  unfolding observations_def by auto
hoelzl@40859
   189
hoelzl@40859
   190
definition OB :: "'key \<times> 'message list \<Rightarrow> 'observation list" where
hoelzl@40859
   191
  "OB = (\<lambda>(k, ms). map (observe k) ms)"
hoelzl@40859
   192
hoelzl@40859
   193
definition t :: "'observation list \<Rightarrow> 'observation \<Rightarrow> nat" where
hoelzl@49793
   194
  t_def2: "t seq obs = card { i. i < length seq \<and> seq ! i = obs}"
hoelzl@49793
   195
hoelzl@49793
   196
lemma t_def: "t seq obs = length (filter (op = obs) seq)"
hoelzl@49793
   197
  unfolding t_def2 length_filter_conv_card by (subst eq_commute) simp
hoelzl@40859
   198
hoelzl@40859
   199
lemma card_T_bound: "card ((t\<circ>OB)`msgs) \<le> (n+1)^card observations"
hoelzl@40859
   200
proof -
hoelzl@42256
   201
  have "(t\<circ>OB)`msgs \<subseteq> extensionalD 0 observations \<inter> (observations \<rightarrow> {.. n})"
hoelzl@42256
   202
    unfolding observations_def extensionalD_def OB_def msgs_def
hoelzl@45715
   203
    by (auto simp add: t_def comp_def image_iff subset_eq)
hoelzl@42256
   204
  with finite_extensionalD_funcset
hoelzl@42256
   205
  have "card ((t\<circ>OB)`msgs) \<le> card (extensionalD 0 observations \<inter> (observations \<rightarrow> {.. n}))"
hoelzl@40859
   206
    by (rule card_mono) auto
hoelzl@40859
   207
  also have "\<dots> = (n + 1) ^ card observations"
hoelzl@40859
   208
    by (subst card_funcset) auto
hoelzl@40859
   209
  finally show ?thesis .
hoelzl@40859
   210
qed
hoelzl@40859
   211
hoelzl@40859
   212
abbreviation
hoelzl@40859
   213
 "p A \<equiv> setsum P A"
hoelzl@40859
   214
hoelzl@47694
   215
abbreviation
hoelzl@47694
   216
  "\<mu> \<equiv> point_measure msgs P"
hoelzl@47694
   217
hoelzl@40859
   218
abbreviation probability ("\<P>'(_') _") where
hoelzl@47694
   219
  "\<P>(X) x \<equiv> measure \<mu> (X -` x \<inter> msgs)"
hoelzl@40859
   220
hoelzl@47694
   221
abbreviation joint_probability ("\<P>'(_ ; _') _") where
hoelzl@47694
   222
  "\<P>(X ; Y) x \<equiv> \<P>(\<lambda>x. (X x, Y x)) x"
hoelzl@40859
   223
hoelzl@47694
   224
no_notation disj (infixr "|" 30)
hoelzl@47694
   225
hoelzl@47694
   226
abbreviation conditional_probability ("\<P>'(_ | _') _") where
hoelzl@47694
   227
  "\<P>(X | Y) x \<equiv> (\<P>(X ; Y) x) / \<P>(Y) (snd`x)"
hoelzl@40859
   228
hoelzl@40859
   229
notation
hoelzl@41689
   230
  entropy_Pow ("\<H>'( _ ')")
hoelzl@40859
   231
hoelzl@40859
   232
notation
hoelzl@41689
   233
  conditional_entropy_Pow ("\<H>'( _ | _ ')")
hoelzl@40859
   234
hoelzl@40859
   235
notation
hoelzl@41689
   236
  mutual_information_Pow ("\<I>'( _ ; _ ')")
hoelzl@40859
   237
hoelzl@40859
   238
lemma t_eq_imp_bij_func:
hoelzl@40859
   239
  assumes "t xs = t ys"
hoelzl@40859
   240
  shows "\<exists>f. bij_betw f {..<length xs} {..<length ys} \<and> (\<forall>i<length xs. xs ! i = ys ! (f i))"
hoelzl@40859
   241
proof -
nipkow@60515
   242
  have "count (mset xs) = count (mset ys)"
nipkow@60515
   243
    using assms by (simp add: fun_eq_iff count_mset t_def)
nipkow@60515
   244
  then have "xs <~~> ys" unfolding mset_eq_perm count_inject .
hoelzl@41689
   245
  then show ?thesis by (rule permutation_Ex_bij)
hoelzl@40859
   246
qed
hoelzl@40859
   247
hoelzl@40859
   248
lemma \<P>_k: assumes "k \<in> keys" shows "\<P>(fst) {k} = K k"
hoelzl@40859
   249
proof -
hoelzl@40859
   250
  from assms have *:
hoelzl@45715
   251
      "fst -` {k} \<inter> msgs = {k}\<times>{ms. set ms \<subseteq> messages \<and> length ms = n}"
hoelzl@40859
   252
    unfolding msgs_def by auto
hoelzl@47694
   253
  show "(\<P>(fst) {k}) = K k"
hoelzl@47694
   254
    apply (simp add: \<mu>'_eq)
hoelzl@41981
   255
    apply (simp add: * P_def)
hoelzl@40859
   256
    apply (simp add: setsum_cartesian_product')
hoelzl@41981
   257
    using setprod_setsum_distrib_lists[OF M.finite_space, of M n "\<lambda>x x. True"] `k \<in> keys`
haftmann@57418
   258
    by (auto simp add: setsum_right_distrib[symmetric] subset_eq setprod.neutral_const)
hoelzl@40859
   259
qed
hoelzl@40859
   260
hoelzl@40859
   261
lemma fst_image_msgs[simp]: "fst`msgs = keys"
hoelzl@40859
   262
proof -
hoelzl@40859
   263
  from M.not_empty obtain m where "m \<in> messages" by auto
hoelzl@45715
   264
  then have *: "{m. set m \<subseteq> messages \<and> length m = n} \<noteq> {}"
hoelzl@40859
   265
    by (auto intro!: exI[of _ "replicate n m"])
hoelzl@40859
   266
  then show ?thesis
hoelzl@40859
   267
    unfolding msgs_def fst_image_times if_not_P[OF *] by simp
hoelzl@40859
   268
qed
hoelzl@40859
   269
hoelzl@47694
   270
lemma setsum_distribution_cut:
hoelzl@47694
   271
  "\<P>(X) {x} = (\<Sum>y \<in> Y`space \<mu>. \<P>(X ; Y) {(x, y)})"
hoelzl@47694
   272
  by (subst finite_measure_finite_Union[symmetric])
hoelzl@47694
   273
     (auto simp add: disjoint_family_on_def inj_on_def
hoelzl@47694
   274
           intro!: arg_cong[where f=prob])
hoelzl@47694
   275
hoelzl@47694
   276
lemma prob_conj_imp1:
hoelzl@47694
   277
  "prob ({x. Q x} \<inter> msgs) = 0 \<Longrightarrow> prob ({x. Pr x \<and> Q x} \<inter> msgs) = 0"
hoelzl@47694
   278
  using finite_measure_mono[of "{x. Pr x \<and> Q x} \<inter> msgs" "{x. Q x} \<inter> msgs"]
hoelzl@47694
   279
  using measure_nonneg[of \<mu> "{x. Pr x \<and> Q x} \<inter> msgs"]
hoelzl@47694
   280
  by (simp add: subset_eq)
hoelzl@47694
   281
hoelzl@47694
   282
lemma prob_conj_imp2:
hoelzl@47694
   283
  "prob ({x. Pr x} \<inter> msgs) = 0 \<Longrightarrow> prob ({x. Pr x \<and> Q x} \<inter> msgs) = 0"
hoelzl@47694
   284
  using finite_measure_mono[of "{x. Pr x \<and> Q x} \<inter> msgs" "{x. Pr x} \<inter> msgs"]
hoelzl@47694
   285
  using measure_nonneg[of \<mu> "{x. Pr x \<and> Q x} \<inter> msgs"]
hoelzl@47694
   286
  by (simp add: subset_eq)
hoelzl@47694
   287
hoelzl@47694
   288
lemma simple_function_finite: "simple_function \<mu> f"
hoelzl@47694
   289
  by (simp add: simple_function_def)
hoelzl@47694
   290
hoelzl@47694
   291
lemma entropy_commute: "\<H>(\<lambda>x. (X x, Y x)) = \<H>(\<lambda>x. (Y x, X x))"
hoelzl@47694
   292
  apply (subst (1 2) entropy_simple_distributed[OF simple_distributedI[OF simple_function_finite refl]])
hoelzl@47694
   293
  unfolding space_point_measure
hoelzl@47694
   294
proof -
hoelzl@47694
   295
  have eq: "(\<lambda>x. (X x, Y x)) ` msgs = (\<lambda>(x, y). (y, x)) ` (\<lambda>x. (Y x, X x)) ` msgs"
hoelzl@47694
   296
    by auto
hoelzl@47694
   297
  show "- (\<Sum>x\<in>(\<lambda>x. (X x, Y x)) ` msgs. (\<P>(X ; Y) {x}) * log b (\<P>(X ; Y) {x})) =
hoelzl@47694
   298
    - (\<Sum>x\<in>(\<lambda>x. (Y x, X x)) ` msgs. (\<P>(Y ; X) {x}) * log b (\<P>(Y ; X) {x}))"
hoelzl@47694
   299
    unfolding eq
haftmann@57418
   300
    apply (subst setsum.reindex)
haftmann@57418
   301
    apply (auto simp: vimage_def inj_on_def intro!: setsum.cong arg_cong[where f="\<lambda>x. prob x * log b (prob x)"])
hoelzl@47694
   302
    done
hoelzl@47694
   303
qed
hoelzl@47694
   304
hoelzl@47694
   305
lemma (in -) measure_eq_0I: "A = {} \<Longrightarrow> measure M A = 0" by simp
hoelzl@47694
   306
hoelzl@47694
   307
lemma conditional_entropy_eq_ce_with_hypothesis:
hoelzl@47694
   308
  "\<H>(X | Y) = -(\<Sum>y\<in>Y`msgs. (\<P>(Y) {y}) * (\<Sum>x\<in>X`msgs. (\<P>(X ; Y) {(x,y)}) / (\<P>(Y) {y}) *
hoelzl@47694
   309
     log b ((\<P>(X ; Y) {(x,y)}) / (\<P>(Y) {y}))))"
hoelzl@47694
   310
  apply (subst conditional_entropy_eq[OF
hoelzl@47694
   311
    simple_distributedI[OF simple_function_finite refl]
hoelzl@47694
   312
    simple_distributedI[OF simple_function_finite refl]])
hoelzl@47694
   313
  unfolding space_point_measure
hoelzl@47694
   314
proof -
hoelzl@47694
   315
  have "- (\<Sum>(x, y)\<in>(\<lambda>x. (X x, Y x)) ` msgs. (\<P>(X ; Y) {(x, y)}) * log b ((\<P>(X ; Y) {(x, y)}) / (\<P>(Y) {y}))) =
hoelzl@47694
   316
    - (\<Sum>x\<in>X`msgs. (\<Sum>y\<in>Y`msgs. (\<P>(X ; Y) {(x, y)}) * log b ((\<P>(X ; Y) {(x, y)}) / (\<P>(Y) {y}))))"
haftmann@57418
   317
    unfolding setsum.cartesian_product
haftmann@57418
   318
    apply (intro arg_cong[where f=uminus] setsum.mono_neutral_left)
hoelzl@47694
   319
    apply (auto simp: vimage_def image_iff intro!: measure_eq_0I)
hoelzl@47694
   320
    apply metis
hoelzl@47694
   321
    done
hoelzl@47694
   322
  also have "\<dots> = - (\<Sum>y\<in>Y`msgs. (\<Sum>x\<in>X`msgs. (\<P>(X ; Y) {(x, y)}) * log b ((\<P>(X ; Y) {(x, y)}) / (\<P>(Y) {y}))))"
haftmann@57418
   323
    by (subst setsum.commute) rule
hoelzl@47694
   324
  also have "\<dots> = -(\<Sum>y\<in>Y`msgs. (\<P>(Y) {y}) * (\<Sum>x\<in>X`msgs. (\<P>(X ; Y) {(x,y)}) / (\<P>(Y) {y}) * log b ((\<P>(X ; Y) {(x,y)}) / (\<P>(Y) {y}))))"
haftmann@57418
   325
    by (auto simp add: setsum_right_distrib vimage_def intro!: setsum.cong prob_conj_imp1)
hoelzl@47694
   326
  finally show "- (\<Sum>(x, y)\<in>(\<lambda>x. (X x, Y x)) ` msgs. (\<P>(X ; Y) {(x, y)}) * log b ((\<P>(X ; Y) {(x, y)}) / (\<P>(Y) {y}))) =
hoelzl@47694
   327
    -(\<Sum>y\<in>Y`msgs. (\<P>(Y) {y}) * (\<Sum>x\<in>X`msgs. (\<P>(X ; Y) {(x,y)}) / (\<P>(Y) {y}) * log b ((\<P>(X ; Y) {(x,y)}) / (\<P>(Y) {y}))))" .
hoelzl@47694
   328
qed
hoelzl@47694
   329
hoelzl@49793
   330
lemma ce_OB_eq_ce_t: "\<I>(fst ; OB) = \<I>(fst ; t\<circ>OB)"
hoelzl@40859
   331
proof -
hoelzl@40859
   332
  txt {* Lemma 2 *}
hoelzl@40859
   333
hoelzl@40859
   334
  { fix k obs obs'
hoelzl@40859
   335
    assume "k \<in> keys" "K k \<noteq> 0" and obs': "obs' \<in> OB ` msgs" and obs: "obs \<in> OB ` msgs"
hoelzl@40859
   336
    assume "t obs = t obs'"
hoelzl@40859
   337
    from t_eq_imp_bij_func[OF this]
hoelzl@40859
   338
    obtain t_f where "bij_betw t_f {..<n} {..<n}" and
hoelzl@40859
   339
      obs_t_f: "\<And>i. i<n \<Longrightarrow> obs!i = obs' ! t_f i"
hoelzl@40859
   340
      using obs obs' unfolding OB_def msgs_def by auto
hoelzl@40859
   341
    then have t_f: "inj_on t_f {..<n}" "{..<n} = t_f`{..<n}" unfolding bij_betw_def by auto
hoelzl@40859
   342
hoelzl@40859
   343
    { fix obs assume "obs \<in> OB`msgs"
hoelzl@40859
   344
      then have **: "\<And>ms. length ms = n \<Longrightarrow> OB (k, ms) = obs \<longleftrightarrow> (\<forall>i<n. observe k (ms!i) = obs ! i)"
hoelzl@40859
   345
        unfolding OB_def msgs_def by (simp add: image_iff list_eq_iff_nth_eq)
hoelzl@40859
   346
hoelzl@47694
   347
      have "(\<P>(OB ; fst) {(obs, k)}) / K k =
hoelzl@40859
   348
          p ({k}\<times>{ms. (k,ms) \<in> msgs \<and> OB (k,ms) = obs}) / K k"
hoelzl@47694
   349
        apply (simp add: \<mu>'_eq) by (auto intro!: arg_cong[where f=p])
hoelzl@40859
   350
      also have "\<dots> =
hoelzl@40859
   351
          (\<Prod>i<n. \<Sum>m\<in>{m\<in>messages. observe k m = obs ! i}. M m)"
hoelzl@40859
   352
        unfolding P_def using `K k \<noteq> 0` `k \<in> keys`
hoelzl@40859
   353
        apply (simp add: setsum_cartesian_product' setsum_divide_distrib msgs_def ** cong: conj_cong)
hoelzl@45715
   354
        apply (subst setprod_setsum_distrib_lists[OF M.finite_space]) ..
hoelzl@47694
   355
      finally have "(\<P>(OB ; fst) {(obs, k)}) / K k =
hoelzl@40859
   356
            (\<Prod>i<n. \<Sum>m\<in>{m\<in>messages. observe k m = obs ! i}. M m)" . }
hoelzl@40859
   357
    note * = this
hoelzl@40859
   358
hoelzl@47694
   359
    have "(\<P>(OB ; fst) {(obs, k)}) / K k = (\<P>(OB ; fst) {(obs', k)}) / K k"
hoelzl@40859
   360
      unfolding *[OF obs] *[OF obs']
haftmann@57418
   361
      using t_f(1) obs_t_f by (subst (2) t_f(2)) (simp add: setprod.reindex)
hoelzl@47694
   362
    then have "(\<P>(OB ; fst) {(obs, k)}) = (\<P>(OB ; fst) {(obs', k)})"
hoelzl@40859
   363
      using `K k \<noteq> 0` by auto }
hoelzl@40859
   364
  note t_eq_imp = this
hoelzl@40859
   365
wenzelm@46731
   366
  let ?S = "\<lambda>obs. t -`{t obs} \<inter> OB`msgs"
hoelzl@40859
   367
  { fix k obs assume "k \<in> keys" "K k \<noteq> 0" and obs: "obs \<in> OB`msgs"
hoelzl@40859
   368
    have *: "((\<lambda>x. (t (OB x), fst x)) -` {(t obs, k)} \<inter> msgs) =
hoelzl@40859
   369
      (\<Union>obs'\<in>?S obs. ((\<lambda>x. (OB x, fst x)) -` {(obs', k)} \<inter> msgs))" by auto
hoelzl@40859
   370
    have df: "disjoint_family_on (\<lambda>obs'. (\<lambda>x. (OB x, fst x)) -` {(obs', k)} \<inter> msgs) (?S obs)"
hoelzl@40859
   371
      unfolding disjoint_family_on_def by auto
hoelzl@47694
   372
    have "\<P>(t\<circ>OB ; fst) {(t obs, k)} = (\<Sum>obs'\<in>?S obs. \<P>(OB ; fst) {(obs', k)})"
hoelzl@47694
   373
      unfolding comp_def
hoelzl@47694
   374
      using finite_measure_finite_Union[OF _ df]
hoelzl@47694
   375
      by (auto simp add: * intro!: setsum_nonneg)
hoelzl@47694
   376
    also have "(\<Sum>obs'\<in>?S obs. \<P>(OB ; fst) {(obs', k)}) = real (card (?S obs)) * \<P>(OB ; fst) {(obs, k)}"
lp15@61609
   377
      by (simp add: t_eq_imp[OF `k \<in> keys` `K k \<noteq> 0` obs])
hoelzl@47694
   378
    finally have "\<P>(t\<circ>OB ; fst) {(t obs, k)} = real (card (?S obs)) * \<P>(OB ; fst) {(obs, k)}" .}
hoelzl@40859
   379
  note P_t_eq_P_OB = this
hoelzl@40859
   380
hoelzl@40859
   381
  { fix k obs assume "k \<in> keys" and obs: "obs \<in> OB`msgs"
hoelzl@40859
   382
    have "\<P>(t\<circ>OB | fst) {(t obs, k)} =
hoelzl@40859
   383
      real (card (t -` {t obs} \<inter> OB ` msgs)) * \<P>(OB | fst) {(obs, k)}"
hoelzl@40859
   384
      using \<P>_k[OF `k \<in> keys`] P_t_eq_P_OB[OF `k \<in> keys` _ obs] by auto }
hoelzl@40859
   385
  note CP_t_K = this
hoelzl@40859
   386
hoelzl@40859
   387
  { fix k obs assume "k \<in> keys" and obs: "obs \<in> OB`msgs"
hoelzl@40859
   388
    then have "t -`{t obs} \<inter> OB`msgs \<noteq> {}" (is "?S \<noteq> {}") by auto
hoelzl@40859
   389
    then have "real (card ?S) \<noteq> 0" by auto
hoelzl@40859
   390
hoelzl@40859
   391
    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}"
hoelzl@47694
   392
      using finite_measure_mono[of "{x. fst x = k \<and> t (OB x) = t obs} \<inter> msgs" "{x. fst x = k} \<inter> msgs"]
hoelzl@47694
   393
      using measure_nonneg[of \<mu> "{x. fst x = k \<and> t (OB x) = t obs} \<inter> msgs"]
hoelzl@47694
   394
      by (auto simp add: vimage_def conj_commute subset_eq)
hoelzl@47694
   395
    also have "(\<P>(t\<circ>OB) {t obs}) = (\<Sum>k'\<in>keys. (\<P>(t\<circ>OB|fst) {(t obs, k')}) * (\<P>(fst) {k'}))"
hoelzl@47694
   396
      using finite_measure_mono[of "{x. t (OB x) = t obs \<and> fst x = k} \<inter> msgs" "{x. fst x = k} \<inter> msgs"]
hoelzl@47694
   397
      using measure_nonneg[of \<mu> "{x. fst x = k \<and> t (OB x) = t obs} \<inter> msgs"]
hoelzl@47694
   398
      apply (simp add: setsum_distribution_cut[of "t\<circ>OB" "t obs" fst])
haftmann@57418
   399
      apply (auto intro!: setsum.cong simp: subset_eq vimage_def prob_conj_imp1)
hoelzl@47694
   400
      done
hoelzl@40859
   401
    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'}) =
hoelzl@40859
   402
      \<P>(OB | fst) {(obs, k)} * \<P>(fst) {k} / (\<Sum>k'\<in>keys. \<P>(OB|fst) {(obs, k')} * \<P>(fst) {k'})"
hoelzl@40859
   403
      using CP_t_K[OF `k\<in>keys` obs] CP_t_K[OF _ obs] `real (card ?S) \<noteq> 0`
hoelzl@40859
   404
      by (simp only: setsum_right_distrib[symmetric] ac_simps
hoelzl@40859
   405
          mult_divide_mult_cancel_left[OF `real (card ?S) \<noteq> 0`]
haftmann@57418
   406
        cong: setsum.cong)
hoelzl@40859
   407
    also have "(\<Sum>k'\<in>keys. \<P>(OB|fst) {(obs, k')} * \<P>(fst) {k'}) = \<P>(OB) {obs}"
hoelzl@47694
   408
      using setsum_distribution_cut[of OB obs fst]
haftmann@57418
   409
      by (auto intro!: setsum.cong simp: prob_conj_imp1 vimage_def)
hoelzl@40859
   410
    also have "\<P>(OB | fst) {(obs, k)} * \<P>(fst) {k} / \<P>(OB) {obs} = \<P>(fst | OB) {(k, obs)}"
hoelzl@47694
   411
      by (auto simp: vimage_def conj_commute prob_conj_imp2)
hoelzl@40859
   412
    finally have "\<P>(fst | t\<circ>OB) {(k, t obs)} = \<P>(fst | OB) {(k, obs)}" . }
hoelzl@40859
   413
  note CP_T_eq_CP_O = this
hoelzl@40859
   414
wenzelm@46731
   415
  let ?H = "\<lambda>obs. (\<Sum>k\<in>keys. \<P>(fst|OB) {(k, obs)} * log b (\<P>(fst|OB) {(k, obs)})) :: real"
wenzelm@46731
   416
  let ?Ht = "\<lambda>obs. (\<Sum>k\<in>keys. \<P>(fst|t\<circ>OB) {(k, obs)} * log b (\<P>(fst|t\<circ>OB) {(k, obs)})) :: real"
hoelzl@40859
   417
hoelzl@40859
   418
  { fix obs assume obs: "obs \<in> OB`msgs"
hoelzl@40859
   419
    have "?H obs = (\<Sum>k\<in>keys. \<P>(fst|t\<circ>OB) {(k, t obs)} * log b (\<P>(fst|t\<circ>OB) {(k, t obs)}))"
hoelzl@40859
   420
      using CP_T_eq_CP_O[OF _ obs]
hoelzl@40859
   421
      by simp
hoelzl@40859
   422
    then have "?H obs = ?Ht (t obs)" . }
hoelzl@40859
   423
  note * = this
hoelzl@40859
   424
hoelzl@40859
   425
  have **: "\<And>x f A. (\<Sum>y\<in>t-`{x}\<inter>A. f y (t y)) = (\<Sum>y\<in>t-`{x}\<inter>A. f y x)" by auto
hoelzl@40859
   426
hoelzl@40859
   427
  { fix x
hoelzl@40859
   428
    have *: "(\<lambda>x. t (OB x)) -` {t (OB x)} \<inter> msgs =
hoelzl@40859
   429
      (\<Union>obs\<in>?S (OB x). OB -` {obs} \<inter> msgs)" by auto
hoelzl@40859
   430
    have df: "disjoint_family_on (\<lambda>obs. OB -` {obs} \<inter> msgs) (?S (OB x))"
hoelzl@40859
   431
      unfolding disjoint_family_on_def by auto
hoelzl@40859
   432
    have "\<P>(t\<circ>OB) {t (OB x)} = (\<Sum>obs\<in>?S (OB x). \<P>(OB) {obs})"
hoelzl@47694
   433
      unfolding comp_def
hoelzl@47694
   434
      using finite_measure_finite_Union[OF _ df]
hoelzl@40859
   435
      by (force simp add: * intro!: setsum_nonneg) }
hoelzl@40859
   436
  note P_t_sum_P_O = this
hoelzl@40859
   437
hoelzl@40859
   438
  txt {* Lemma 3 *}
hoelzl@40859
   439
  have "\<H>(fst | OB) = -(\<Sum>obs\<in>OB`msgs. \<P>(OB) {obs} * ?Ht (t obs))"
hoelzl@47694
   440
    unfolding conditional_entropy_eq_ce_with_hypothesis using * by simp
hoelzl@40859
   441
  also have "\<dots> = -(\<Sum>obs\<in>t`OB`msgs. \<P>(t\<circ>OB) {obs} * ?Ht obs)"
hoelzl@40859
   442
    apply (subst SIGMA_image_vimage[symmetric, of "OB`msgs" t])
haftmann@57418
   443
    apply (subst setsum.reindex)
nipkow@44890
   444
    apply (fastforce intro!: inj_onI)
hoelzl@40859
   445
    apply simp
haftmann@57418
   446
    apply (subst setsum.Sigma[symmetric, unfolded split_def])
nipkow@44890
   447
    using finite_space apply fastforce
nipkow@44890
   448
    using finite_space apply fastforce
haftmann@57418
   449
    apply (safe intro!: setsum.cong)
hoelzl@40859
   450
    using P_t_sum_P_O
hoelzl@40859
   451
    by (simp add: setsum_divide_distrib[symmetric] field_simps **
hoelzl@40859
   452
                  setsum_right_distrib[symmetric] setsum_left_distrib[symmetric])
hoelzl@40859
   453
  also have "\<dots> = \<H>(fst | t\<circ>OB)"
hoelzl@47694
   454
    unfolding conditional_entropy_eq_ce_with_hypothesis
hoelzl@40859
   455
    by (simp add: comp_def image_image[symmetric])
hoelzl@49793
   456
  finally show ?thesis
hoelzl@49793
   457
    by (subst (1 2) mutual_information_eq_entropy_conditional_entropy) simp_all
hoelzl@40859
   458
qed
hoelzl@40859
   459
hoelzl@40859
   460
theorem "\<I>(fst ; OB) \<le> real (card observations) * log b (real n + 1)"
hoelzl@40859
   461
proof -
hoelzl@49793
   462
  have "\<I>(fst ; OB) = \<H>(fst) - \<H>(fst | t\<circ>OB)"
hoelzl@49793
   463
    unfolding ce_OB_eq_ce_t
hoelzl@49793
   464
    by (rule mutual_information_eq_entropy_conditional_entropy simple_function_finite)+
hoelzl@40859
   465
  also have "\<dots> = \<H>(t\<circ>OB) - \<H>(t\<circ>OB | fst)"
hoelzl@41689
   466
    unfolding entropy_chain_rule[symmetric, OF simple_function_finite simple_function_finite] sign_simps
hoelzl@47694
   467
    by (subst entropy_commute) simp
hoelzl@40859
   468
  also have "\<dots> \<le> \<H>(t\<circ>OB)"
hoelzl@47694
   469
    using conditional_entropy_nonneg[of "t\<circ>OB" fst] by simp
hoelzl@40859
   470
  also have "\<dots> \<le> log b (real (card ((t\<circ>OB)`msgs)))"
hoelzl@47694
   471
    using entropy_le_card[of "t\<circ>OB", OF simple_distributedI[OF simple_function_finite refl]] by simp
hoelzl@40859
   472
  also have "\<dots> \<le> log b (real (n + 1)^card observations)"
hoelzl@40859
   473
    using card_T_bound not_empty
lp15@61649
   474
    by (auto intro!: log_le simp: card_gt_0_iff of_nat_power [symmetric] simp del: of_nat_power of_nat_Suc)
hoelzl@40859
   475
  also have "\<dots> = real (card observations) * log b (real n + 1)"
lp15@61609
   476
    by (simp add: log_nat_power add.commute)
hoelzl@40859
   477
  finally show ?thesis  .
hoelzl@40859
   478
qed
hoelzl@40859
   479
hoelzl@40859
   480
end
hoelzl@40859
   481
hoelzl@41689
   482
end