src/HOL/Probability/Independent_Family.thy
author immler
Thu Nov 15 10:49:58 2012 +0100 (2012-11-15)
changeset 50087 635d73673b5e
parent 50021 d96a3f468203
child 50104 de19856feb54
permissions -rw-r--r--
regularity of measures, therefore:
characterization of closure with infimum distance;
characterize of compact sets as totally bounded;
added Diagonal_Subsequence to Library;
introduced (enumerable) topological basis;
rational boxes as basis of ordered euclidean space;
moved some lemmas upwards
     1 (*  Title:      HOL/Probability/Independent_Family.thy
     2     Author:     Johannes Hölzl, TU München
     3 *)
     4 
     5 header {* Independent families of events, event sets, and random variables *}
     6 
     7 theory Independent_Family
     8   imports Probability_Measure Infinite_Product_Measure
     9 begin
    10 
    11 definition (in prob_space)
    12   "indep_sets F I \<longleftrightarrow> (\<forall>i\<in>I. F i \<subseteq> events) \<and>
    13     (\<forall>J\<subseteq>I. J \<noteq> {} \<longrightarrow> finite J \<longrightarrow> (\<forall>A\<in>Pi J F. prob (\<Inter>j\<in>J. A j) = (\<Prod>j\<in>J. prob (A j))))"
    14 
    15 definition (in prob_space)
    16   "indep_set A B \<longleftrightarrow> indep_sets (bool_case A B) UNIV"
    17 
    18 definition (in prob_space)
    19   indep_events_def_alt: "indep_events A I \<longleftrightarrow> indep_sets (\<lambda>i. {A i}) I"
    20 
    21 lemma image_subset_iff_funcset: "F ` A \<subseteq> B \<longleftrightarrow> F \<in> A \<rightarrow> B"
    22   by auto
    23 
    24 lemma (in prob_space) indep_events_def:
    25   "indep_events A I \<longleftrightarrow> (A`I \<subseteq> events) \<and>
    26     (\<forall>J\<subseteq>I. J \<noteq> {} \<longrightarrow> finite J \<longrightarrow> prob (\<Inter>j\<in>J. A j) = (\<Prod>j\<in>J. prob (A j)))"
    27   unfolding indep_events_def_alt indep_sets_def
    28   apply (simp add: Ball_def Pi_iff image_subset_iff_funcset)
    29   apply (intro conj_cong refl arg_cong[where f=All] ext imp_cong)
    30   apply auto
    31   done
    32 
    33 definition (in prob_space)
    34   "indep_event A B \<longleftrightarrow> indep_events (bool_case A B) UNIV"
    35 
    36 lemma (in prob_space) indep_sets_cong:
    37   "I = J \<Longrightarrow> (\<And>i. i \<in> I \<Longrightarrow> F i = G i) \<Longrightarrow> indep_sets F I \<longleftrightarrow> indep_sets G J"
    38   by (simp add: indep_sets_def, intro conj_cong all_cong imp_cong ball_cong) blast+
    39 
    40 lemma (in prob_space) indep_events_finite_index_events:
    41   "indep_events F I \<longleftrightarrow> (\<forall>J\<subseteq>I. J \<noteq> {} \<longrightarrow> finite J \<longrightarrow> indep_events F J)"
    42   by (auto simp: indep_events_def)
    43 
    44 lemma (in prob_space) indep_sets_finite_index_sets:
    45   "indep_sets F I \<longleftrightarrow> (\<forall>J\<subseteq>I. J \<noteq> {} \<longrightarrow> finite J \<longrightarrow> indep_sets F J)"
    46 proof (intro iffI allI impI)
    47   assume *: "\<forall>J\<subseteq>I. J \<noteq> {} \<longrightarrow> finite J \<longrightarrow> indep_sets F J"
    48   show "indep_sets F I" unfolding indep_sets_def
    49   proof (intro conjI ballI allI impI)
    50     fix i assume "i \<in> I"
    51     with *[THEN spec, of "{i}"] show "F i \<subseteq> events"
    52       by (auto simp: indep_sets_def)
    53   qed (insert *, auto simp: indep_sets_def)
    54 qed (auto simp: indep_sets_def)
    55 
    56 lemma (in prob_space) indep_sets_mono_index:
    57   "J \<subseteq> I \<Longrightarrow> indep_sets F I \<Longrightarrow> indep_sets F J"
    58   unfolding indep_sets_def by auto
    59 
    60 lemma (in prob_space) indep_sets_mono_sets:
    61   assumes indep: "indep_sets F I"
    62   assumes mono: "\<And>i. i\<in>I \<Longrightarrow> G i \<subseteq> F i"
    63   shows "indep_sets G I"
    64 proof -
    65   have "(\<forall>i\<in>I. F i \<subseteq> events) \<Longrightarrow> (\<forall>i\<in>I. G i \<subseteq> events)"
    66     using mono by auto
    67   moreover have "\<And>A J. J \<subseteq> I \<Longrightarrow> A \<in> (\<Pi> j\<in>J. G j) \<Longrightarrow> A \<in> (\<Pi> j\<in>J. F j)"
    68     using mono by (auto simp: Pi_iff)
    69   ultimately show ?thesis
    70     using indep by (auto simp: indep_sets_def)
    71 qed
    72 
    73 lemma (in prob_space) indep_sets_mono:
    74   assumes indep: "indep_sets F I"
    75   assumes mono: "J \<subseteq> I" "\<And>i. i\<in>J \<Longrightarrow> G i \<subseteq> F i"
    76   shows "indep_sets G J"
    77   apply (rule indep_sets_mono_sets)
    78   apply (rule indep_sets_mono_index)
    79   apply (fact +)
    80   done
    81 
    82 lemma (in prob_space) indep_setsI:
    83   assumes "\<And>i. i \<in> I \<Longrightarrow> F i \<subseteq> events"
    84     and "\<And>A J. J \<noteq> {} \<Longrightarrow> J \<subseteq> I \<Longrightarrow> finite J \<Longrightarrow> (\<forall>j\<in>J. A j \<in> F j) \<Longrightarrow> prob (\<Inter>j\<in>J. A j) = (\<Prod>j\<in>J. prob (A j))"
    85   shows "indep_sets F I"
    86   using assms unfolding indep_sets_def by (auto simp: Pi_iff)
    87 
    88 lemma (in prob_space) indep_setsD:
    89   assumes "indep_sets F I" and "J \<subseteq> I" "J \<noteq> {}" "finite J" "\<forall>j\<in>J. A j \<in> F j"
    90   shows "prob (\<Inter>j\<in>J. A j) = (\<Prod>j\<in>J. prob (A j))"
    91   using assms unfolding indep_sets_def by auto
    92 
    93 lemma (in prob_space) indep_setI:
    94   assumes ev: "A \<subseteq> events" "B \<subseteq> events"
    95     and indep: "\<And>a b. a \<in> A \<Longrightarrow> b \<in> B \<Longrightarrow> prob (a \<inter> b) = prob a * prob b"
    96   shows "indep_set A B"
    97   unfolding indep_set_def
    98 proof (rule indep_setsI)
    99   fix F J assume "J \<noteq> {}" "J \<subseteq> UNIV"
   100     and F: "\<forall>j\<in>J. F j \<in> (case j of True \<Rightarrow> A | False \<Rightarrow> B)"
   101   have "J \<in> Pow UNIV" by auto
   102   with F `J \<noteq> {}` indep[of "F True" "F False"]
   103   show "prob (\<Inter>j\<in>J. F j) = (\<Prod>j\<in>J. prob (F j))"
   104     unfolding UNIV_bool Pow_insert by (auto simp: ac_simps)
   105 qed (auto split: bool.split simp: ev)
   106 
   107 lemma (in prob_space) indep_setD:
   108   assumes indep: "indep_set A B" and ev: "a \<in> A" "b \<in> B"
   109   shows "prob (a \<inter> b) = prob a * prob b"
   110   using indep[unfolded indep_set_def, THEN indep_setsD, of UNIV "bool_case a b"] ev
   111   by (simp add: ac_simps UNIV_bool)
   112 
   113 lemma (in prob_space)
   114   assumes indep: "indep_set A B"
   115   shows indep_setD_ev1: "A \<subseteq> events"
   116     and indep_setD_ev2: "B \<subseteq> events"
   117   using indep unfolding indep_set_def indep_sets_def UNIV_bool by auto
   118 
   119 lemma (in prob_space) indep_sets_dynkin:
   120   assumes indep: "indep_sets F I"
   121   shows "indep_sets (\<lambda>i. dynkin (space M) (F i)) I"
   122     (is "indep_sets ?F I")
   123 proof (subst indep_sets_finite_index_sets, intro allI impI ballI)
   124   fix J assume "finite J" "J \<subseteq> I" "J \<noteq> {}"
   125   with indep have "indep_sets F J"
   126     by (subst (asm) indep_sets_finite_index_sets) auto
   127   { fix J K assume "indep_sets F K"
   128     let ?G = "\<lambda>S i. if i \<in> S then ?F i else F i"
   129     assume "finite J" "J \<subseteq> K"
   130     then have "indep_sets (?G J) K"
   131     proof induct
   132       case (insert j J)
   133       moreover def G \<equiv> "?G J"
   134       ultimately have G: "indep_sets G K" "\<And>i. i \<in> K \<Longrightarrow> G i \<subseteq> events" and "j \<in> K"
   135         by (auto simp: indep_sets_def)
   136       let ?D = "{E\<in>events. indep_sets (G(j := {E})) K }"
   137       { fix X assume X: "X \<in> events"
   138         assume indep: "\<And>J A. J \<noteq> {} \<Longrightarrow> J \<subseteq> K \<Longrightarrow> finite J \<Longrightarrow> j \<notin> J \<Longrightarrow> (\<forall>i\<in>J. A i \<in> G i)
   139           \<Longrightarrow> prob ((\<Inter>i\<in>J. A i) \<inter> X) = prob X * (\<Prod>i\<in>J. prob (A i))"
   140         have "indep_sets (G(j := {X})) K"
   141         proof (rule indep_setsI)
   142           fix i assume "i \<in> K" then show "(G(j:={X})) i \<subseteq> events"
   143             using G X by auto
   144         next
   145           fix A J assume J: "J \<noteq> {}" "J \<subseteq> K" "finite J" "\<forall>i\<in>J. A i \<in> (G(j := {X})) i"
   146           show "prob (\<Inter>j\<in>J. A j) = (\<Prod>j\<in>J. prob (A j))"
   147           proof cases
   148             assume "j \<in> J"
   149             with J have "A j = X" by auto
   150             show ?thesis
   151             proof cases
   152               assume "J = {j}" then show ?thesis by simp
   153             next
   154               assume "J \<noteq> {j}"
   155               have "prob (\<Inter>i\<in>J. A i) = prob ((\<Inter>i\<in>J-{j}. A i) \<inter> X)"
   156                 using `j \<in> J` `A j = X` by (auto intro!: arg_cong[where f=prob] split: split_if_asm)
   157               also have "\<dots> = prob X * (\<Prod>i\<in>J-{j}. prob (A i))"
   158               proof (rule indep)
   159                 show "J - {j} \<noteq> {}" "J - {j} \<subseteq> K" "finite (J - {j})" "j \<notin> J - {j}"
   160                   using J `J \<noteq> {j}` `j \<in> J` by auto
   161                 show "\<forall>i\<in>J - {j}. A i \<in> G i"
   162                   using J by auto
   163               qed
   164               also have "\<dots> = prob (A j) * (\<Prod>i\<in>J-{j}. prob (A i))"
   165                 using `A j = X` by simp
   166               also have "\<dots> = (\<Prod>i\<in>J. prob (A i))"
   167                 unfolding setprod.insert_remove[OF `finite J`, symmetric, of "\<lambda>i. prob  (A i)"]
   168                 using `j \<in> J` by (simp add: insert_absorb)
   169               finally show ?thesis .
   170             qed
   171           next
   172             assume "j \<notin> J"
   173             with J have "\<forall>i\<in>J. A i \<in> G i" by (auto split: split_if_asm)
   174             with J show ?thesis
   175               by (intro indep_setsD[OF G(1)]) auto
   176           qed
   177         qed }
   178       note indep_sets_insert = this
   179       have "dynkin_system (space M) ?D"
   180       proof (rule dynkin_systemI', simp_all cong del: indep_sets_cong, safe)
   181         show "indep_sets (G(j := {{}})) K"
   182           by (rule indep_sets_insert) auto
   183       next
   184         fix X assume X: "X \<in> events" and G': "indep_sets (G(j := {X})) K"
   185         show "indep_sets (G(j := {space M - X})) K"
   186         proof (rule indep_sets_insert)
   187           fix J A assume J: "J \<noteq> {}" "J \<subseteq> K" "finite J" "j \<notin> J" and A: "\<forall>i\<in>J. A i \<in> G i"
   188           then have A_sets: "\<And>i. i\<in>J \<Longrightarrow> A i \<in> events"
   189             using G by auto
   190           have "prob ((\<Inter>j\<in>J. A j) \<inter> (space M - X)) =
   191               prob ((\<Inter>j\<in>J. A j) - (\<Inter>i\<in>insert j J. (A(j := X)) i))"
   192             using A_sets sets_into_space[of _ M] X `J \<noteq> {}`
   193             by (auto intro!: arg_cong[where f=prob] split: split_if_asm)
   194           also have "\<dots> = prob (\<Inter>j\<in>J. A j) - prob (\<Inter>i\<in>insert j J. (A(j := X)) i)"
   195             using J `J \<noteq> {}` `j \<notin> J` A_sets X sets_into_space
   196             by (auto intro!: finite_measure_Diff finite_INT split: split_if_asm)
   197           finally have "prob ((\<Inter>j\<in>J. A j) \<inter> (space M - X)) =
   198               prob (\<Inter>j\<in>J. A j) - prob (\<Inter>i\<in>insert j J. (A(j := X)) i)" .
   199           moreover {
   200             have "prob (\<Inter>j\<in>J. A j) = (\<Prod>j\<in>J. prob (A j))"
   201               using J A `finite J` by (intro indep_setsD[OF G(1)]) auto
   202             then have "prob (\<Inter>j\<in>J. A j) = prob (space M) * (\<Prod>i\<in>J. prob (A i))"
   203               using prob_space by simp }
   204           moreover {
   205             have "prob (\<Inter>i\<in>insert j J. (A(j := X)) i) = (\<Prod>i\<in>insert j J. prob ((A(j := X)) i))"
   206               using J A `j \<in> K` by (intro indep_setsD[OF G']) auto
   207             then have "prob (\<Inter>i\<in>insert j J. (A(j := X)) i) = prob X * (\<Prod>i\<in>J. prob (A i))"
   208               using `finite J` `j \<notin> J` by (auto intro!: setprod_cong) }
   209           ultimately have "prob ((\<Inter>j\<in>J. A j) \<inter> (space M - X)) = (prob (space M) - prob X) * (\<Prod>i\<in>J. prob (A i))"
   210             by (simp add: field_simps)
   211           also have "\<dots> = prob (space M - X) * (\<Prod>i\<in>J. prob (A i))"
   212             using X A by (simp add: finite_measure_compl)
   213           finally show "prob ((\<Inter>j\<in>J. A j) \<inter> (space M - X)) = prob (space M - X) * (\<Prod>i\<in>J. prob (A i))" .
   214         qed (insert X, auto)
   215       next
   216         fix F :: "nat \<Rightarrow> 'a set" assume disj: "disjoint_family F" and "range F \<subseteq> ?D"
   217         then have F: "\<And>i. F i \<in> events" "\<And>i. indep_sets (G(j:={F i})) K" by auto
   218         show "indep_sets (G(j := {\<Union>k. F k})) K"
   219         proof (rule indep_sets_insert)
   220           fix J A assume J: "j \<notin> J" "J \<noteq> {}" "J \<subseteq> K" "finite J" and A: "\<forall>i\<in>J. A i \<in> G i"
   221           then have A_sets: "\<And>i. i\<in>J \<Longrightarrow> A i \<in> events"
   222             using G by auto
   223           have "prob ((\<Inter>j\<in>J. A j) \<inter> (\<Union>k. F k)) = prob (\<Union>k. (\<Inter>i\<in>insert j J. (A(j := F k)) i))"
   224             using `J \<noteq> {}` `j \<notin> J` `j \<in> K` by (auto intro!: arg_cong[where f=prob] split: split_if_asm)
   225           moreover have "(\<lambda>k. prob (\<Inter>i\<in>insert j J. (A(j := F k)) i)) sums prob (\<Union>k. (\<Inter>i\<in>insert j J. (A(j := F k)) i))"
   226           proof (rule finite_measure_UNION)
   227             show "disjoint_family (\<lambda>k. \<Inter>i\<in>insert j J. (A(j := F k)) i)"
   228               using disj by (rule disjoint_family_on_bisimulation) auto
   229             show "range (\<lambda>k. \<Inter>i\<in>insert j J. (A(j := F k)) i) \<subseteq> events"
   230               using A_sets F `finite J` `J \<noteq> {}` `j \<notin> J` by (auto intro!: Int)
   231           qed
   232           moreover { fix k
   233             from J A `j \<in> K` have "prob (\<Inter>i\<in>insert j J. (A(j := F k)) i) = prob (F k) * (\<Prod>i\<in>J. prob (A i))"
   234               by (subst indep_setsD[OF F(2)]) (auto intro!: setprod_cong split: split_if_asm)
   235             also have "\<dots> = prob (F k) * prob (\<Inter>i\<in>J. A i)"
   236               using J A `j \<in> K` by (subst indep_setsD[OF G(1)]) auto
   237             finally have "prob (\<Inter>i\<in>insert j J. (A(j := F k)) i) = prob (F k) * prob (\<Inter>i\<in>J. A i)" . }
   238           ultimately have "(\<lambda>k. prob (F k) * prob (\<Inter>i\<in>J. A i)) sums (prob ((\<Inter>j\<in>J. A j) \<inter> (\<Union>k. F k)))"
   239             by simp
   240           moreover
   241           have "(\<lambda>k. prob (F k) * prob (\<Inter>i\<in>J. A i)) sums (prob (\<Union>k. F k) * prob (\<Inter>i\<in>J. A i))"
   242             using disj F(1) by (intro finite_measure_UNION sums_mult2) auto
   243           then have "(\<lambda>k. prob (F k) * prob (\<Inter>i\<in>J. A i)) sums (prob (\<Union>k. F k) * (\<Prod>i\<in>J. prob (A i)))"
   244             using J A `j \<in> K` by (subst indep_setsD[OF G(1), symmetric]) auto
   245           ultimately
   246           show "prob ((\<Inter>j\<in>J. A j) \<inter> (\<Union>k. F k)) = prob (\<Union>k. F k) * (\<Prod>j\<in>J. prob (A j))"
   247             by (auto dest!: sums_unique)
   248         qed (insert F, auto)
   249       qed (insert sets_into_space, auto)
   250       then have mono: "dynkin (space M) (G j) \<subseteq> {E \<in> events. indep_sets (G(j := {E})) K}"
   251       proof (rule dynkin_system.dynkin_subset, safe)
   252         fix X assume "X \<in> G j"
   253         then show "X \<in> events" using G `j \<in> K` by auto
   254         from `indep_sets G K`
   255         show "indep_sets (G(j := {X})) K"
   256           by (rule indep_sets_mono_sets) (insert `X \<in> G j`, auto)
   257       qed
   258       have "indep_sets (G(j:=?D)) K"
   259       proof (rule indep_setsI)
   260         fix i assume "i \<in> K" then show "(G(j := ?D)) i \<subseteq> events"
   261           using G(2) by auto
   262       next
   263         fix A J assume J: "J\<noteq>{}" "J \<subseteq> K" "finite J" and A: "\<forall>i\<in>J. A i \<in> (G(j := ?D)) i"
   264         show "prob (\<Inter>j\<in>J. A j) = (\<Prod>j\<in>J. prob (A j))"
   265         proof cases
   266           assume "j \<in> J"
   267           with A have indep: "indep_sets (G(j := {A j})) K" by auto
   268           from J A show ?thesis
   269             by (intro indep_setsD[OF indep]) auto
   270         next
   271           assume "j \<notin> J"
   272           with J A have "\<forall>i\<in>J. A i \<in> G i" by (auto split: split_if_asm)
   273           with J show ?thesis
   274             by (intro indep_setsD[OF G(1)]) auto
   275         qed
   276       qed
   277       then have "indep_sets (G(j := dynkin (space M) (G j))) K"
   278         by (rule indep_sets_mono_sets) (insert mono, auto)
   279       then show ?case
   280         by (rule indep_sets_mono_sets) (insert `j \<in> K` `j \<notin> J`, auto simp: G_def)
   281     qed (insert `indep_sets F K`, simp) }
   282   from this[OF `indep_sets F J` `finite J` subset_refl]
   283   show "indep_sets ?F J"
   284     by (rule indep_sets_mono_sets) auto
   285 qed
   286 
   287 lemma (in prob_space) indep_sets_sigma:
   288   assumes indep: "indep_sets F I"
   289   assumes stable: "\<And>i. i \<in> I \<Longrightarrow> Int_stable (F i)"
   290   shows "indep_sets (\<lambda>i. sigma_sets (space M) (F i)) I"
   291 proof -
   292   from indep_sets_dynkin[OF indep]
   293   show ?thesis
   294   proof (rule indep_sets_mono_sets, subst sigma_eq_dynkin, simp_all add: stable)
   295     fix i assume "i \<in> I"
   296     with indep have "F i \<subseteq> events" by (auto simp: indep_sets_def)
   297     with sets_into_space show "F i \<subseteq> Pow (space M)" by auto
   298   qed
   299 qed
   300 
   301 lemma (in prob_space) indep_sets_sigma_sets_iff:
   302   assumes "\<And>i. i \<in> I \<Longrightarrow> Int_stable (F i)"
   303   shows "indep_sets (\<lambda>i. sigma_sets (space M) (F i)) I \<longleftrightarrow> indep_sets F I"
   304 proof
   305   assume "indep_sets F I" then show "indep_sets (\<lambda>i. sigma_sets (space M) (F i)) I"
   306     by (rule indep_sets_sigma) fact
   307 next
   308   assume "indep_sets (\<lambda>i. sigma_sets (space M) (F i)) I" then show "indep_sets F I"
   309     by (rule indep_sets_mono_sets) (intro subsetI sigma_sets.Basic)
   310 qed
   311 
   312 definition (in prob_space)
   313   indep_vars_def2: "indep_vars M' X I \<longleftrightarrow>
   314     (\<forall>i\<in>I. random_variable (M' i) (X i)) \<and>
   315     indep_sets (\<lambda>i. { X i -` A \<inter> space M | A. A \<in> sets (M' i)}) I"
   316 
   317 definition (in prob_space)
   318   "indep_var Ma A Mb B \<longleftrightarrow> indep_vars (bool_case Ma Mb) (bool_case A B) UNIV"
   319 
   320 lemma (in prob_space) indep_vars_def:
   321   "indep_vars M' X I \<longleftrightarrow>
   322     (\<forall>i\<in>I. random_variable (M' i) (X i)) \<and>
   323     indep_sets (\<lambda>i. sigma_sets (space M) { X i -` A \<inter> space M | A. A \<in> sets (M' i)}) I"
   324   unfolding indep_vars_def2
   325   apply (rule conj_cong[OF refl])
   326   apply (rule indep_sets_sigma_sets_iff[symmetric])
   327   apply (auto simp: Int_stable_def)
   328   apply (rule_tac x="A \<inter> Aa" in exI)
   329   apply auto
   330   done
   331 
   332 lemma (in prob_space) indep_var_eq:
   333   "indep_var S X T Y \<longleftrightarrow>
   334     (random_variable S X \<and> random_variable T Y) \<and>
   335     indep_set
   336       (sigma_sets (space M) { X -` A \<inter> space M | A. A \<in> sets S})
   337       (sigma_sets (space M) { Y -` A \<inter> space M | A. A \<in> sets T})"
   338   unfolding indep_var_def indep_vars_def indep_set_def UNIV_bool
   339   by (intro arg_cong2[where f="op \<and>"] arg_cong2[where f=indep_sets] ext)
   340      (auto split: bool.split)
   341 
   342 lemma (in prob_space) indep_sets2_eq:
   343   "indep_set A B \<longleftrightarrow> A \<subseteq> events \<and> B \<subseteq> events \<and> (\<forall>a\<in>A. \<forall>b\<in>B. prob (a \<inter> b) = prob a * prob b)"
   344   unfolding indep_set_def
   345 proof (intro iffI ballI conjI)
   346   assume indep: "indep_sets (bool_case A B) UNIV"
   347   { fix a b assume "a \<in> A" "b \<in> B"
   348     with indep_setsD[OF indep, of UNIV "bool_case a b"]
   349     show "prob (a \<inter> b) = prob a * prob b"
   350       unfolding UNIV_bool by (simp add: ac_simps) }
   351   from indep show "A \<subseteq> events" "B \<subseteq> events"
   352     unfolding indep_sets_def UNIV_bool by auto
   353 next
   354   assume *: "A \<subseteq> events \<and> B \<subseteq> events \<and> (\<forall>a\<in>A. \<forall>b\<in>B. prob (a \<inter> b) = prob a * prob b)"
   355   show "indep_sets (bool_case A B) UNIV"
   356   proof (rule indep_setsI)
   357     fix i show "(case i of True \<Rightarrow> A | False \<Rightarrow> B) \<subseteq> events"
   358       using * by (auto split: bool.split)
   359   next
   360     fix J X assume "J \<noteq> {}" "J \<subseteq> UNIV" and X: "\<forall>j\<in>J. X j \<in> (case j of True \<Rightarrow> A | False \<Rightarrow> B)"
   361     then have "J = {True} \<or> J = {False} \<or> J = {True,False}"
   362       by (auto simp: UNIV_bool)
   363     then show "prob (\<Inter>j\<in>J. X j) = (\<Prod>j\<in>J. prob (X j))"
   364       using X * by auto
   365   qed
   366 qed
   367 
   368 lemma (in prob_space) indep_set_sigma_sets:
   369   assumes "indep_set A B"
   370   assumes A: "Int_stable A" and B: "Int_stable B"
   371   shows "indep_set (sigma_sets (space M) A) (sigma_sets (space M) B)"
   372 proof -
   373   have "indep_sets (\<lambda>i. sigma_sets (space M) (case i of True \<Rightarrow> A | False \<Rightarrow> B)) UNIV"
   374   proof (rule indep_sets_sigma)
   375     show "indep_sets (bool_case A B) UNIV"
   376       by (rule `indep_set A B`[unfolded indep_set_def])
   377     fix i show "Int_stable (case i of True \<Rightarrow> A | False \<Rightarrow> B)"
   378       using A B by (cases i) auto
   379   qed
   380   then show ?thesis
   381     unfolding indep_set_def
   382     by (rule indep_sets_mono_sets) (auto split: bool.split)
   383 qed
   384 
   385 lemma (in prob_space) indep_sets_collect_sigma:
   386   fixes I :: "'j \<Rightarrow> 'i set" and J :: "'j set" and E :: "'i \<Rightarrow> 'a set set"
   387   assumes indep: "indep_sets E (\<Union>j\<in>J. I j)"
   388   assumes Int_stable: "\<And>i j. j \<in> J \<Longrightarrow> i \<in> I j \<Longrightarrow> Int_stable (E i)"
   389   assumes disjoint: "disjoint_family_on I J"
   390   shows "indep_sets (\<lambda>j. sigma_sets (space M) (\<Union>i\<in>I j. E i)) J"
   391 proof -
   392   let ?E = "\<lambda>j. {\<Inter>k\<in>K. E' k| E' K. finite K \<and> K \<noteq> {} \<and> K \<subseteq> I j \<and> (\<forall>k\<in>K. E' k \<in> E k) }"
   393 
   394   from indep have E: "\<And>j i. j \<in> J \<Longrightarrow> i \<in> I j \<Longrightarrow> E i \<subseteq> events"
   395     unfolding indep_sets_def by auto
   396   { fix j
   397     let ?S = "sigma_sets (space M) (\<Union>i\<in>I j. E i)"
   398     assume "j \<in> J"
   399     from E[OF this] interpret S: sigma_algebra "space M" ?S
   400       using sets_into_space[of _ M] by (intro sigma_algebra_sigma_sets) auto
   401 
   402     have "sigma_sets (space M) (\<Union>i\<in>I j. E i) = sigma_sets (space M) (?E j)"
   403     proof (rule sigma_sets_eqI)
   404       fix A assume "A \<in> (\<Union>i\<in>I j. E i)"
   405       then guess i ..
   406       then show "A \<in> sigma_sets (space M) (?E j)"
   407         by (auto intro!: sigma_sets.intros(2-) exI[of _ "{i}"] exI[of _ "\<lambda>i. A"])
   408     next
   409       fix A assume "A \<in> ?E j"
   410       then obtain E' K where "finite K" "K \<noteq> {}" "K \<subseteq> I j" "\<And>k. k \<in> K \<Longrightarrow> E' k \<in> E k"
   411         and A: "A = (\<Inter>k\<in>K. E' k)"
   412         by auto
   413       then have "A \<in> ?S" unfolding A
   414         by (safe intro!: S.finite_INT) auto
   415       then show "A \<in> sigma_sets (space M) (\<Union>i\<in>I j. E i)"
   416         by simp
   417     qed }
   418   moreover have "indep_sets (\<lambda>j. sigma_sets (space M) (?E j)) J"
   419   proof (rule indep_sets_sigma)
   420     show "indep_sets ?E J"
   421     proof (intro indep_setsI)
   422       fix j assume "j \<in> J" with E show "?E j \<subseteq> events" by (force  intro!: finite_INT)
   423     next
   424       fix K A assume K: "K \<noteq> {}" "K \<subseteq> J" "finite K"
   425         and "\<forall>j\<in>K. A j \<in> ?E j"
   426       then have "\<forall>j\<in>K. \<exists>E' L. A j = (\<Inter>l\<in>L. E' l) \<and> finite L \<and> L \<noteq> {} \<and> L \<subseteq> I j \<and> (\<forall>l\<in>L. E' l \<in> E l)"
   427         by simp
   428       from bchoice[OF this] guess E' ..
   429       from bchoice[OF this] obtain L
   430         where A: "\<And>j. j\<in>K \<Longrightarrow> A j = (\<Inter>l\<in>L j. E' j l)"
   431         and L: "\<And>j. j\<in>K \<Longrightarrow> finite (L j)" "\<And>j. j\<in>K \<Longrightarrow> L j \<noteq> {}" "\<And>j. j\<in>K \<Longrightarrow> L j \<subseteq> I j"
   432         and E': "\<And>j l. j\<in>K \<Longrightarrow> l \<in> L j \<Longrightarrow> E' j l \<in> E l"
   433         by auto
   434 
   435       { fix k l j assume "k \<in> K" "j \<in> K" "l \<in> L j" "l \<in> L k"
   436         have "k = j"
   437         proof (rule ccontr)
   438           assume "k \<noteq> j"
   439           with disjoint `K \<subseteq> J` `k \<in> K` `j \<in> K` have "I k \<inter> I j = {}"
   440             unfolding disjoint_family_on_def by auto
   441           with L(2,3)[OF `j \<in> K`] L(2,3)[OF `k \<in> K`]
   442           show False using `l \<in> L k` `l \<in> L j` by auto
   443         qed }
   444       note L_inj = this
   445 
   446       def k \<equiv> "\<lambda>l. (SOME k. k \<in> K \<and> l \<in> L k)"
   447       { fix x j l assume *: "j \<in> K" "l \<in> L j"
   448         have "k l = j" unfolding k_def
   449         proof (rule some_equality)
   450           fix k assume "k \<in> K \<and> l \<in> L k"
   451           with * L_inj show "k = j" by auto
   452         qed (insert *, simp) }
   453       note k_simp[simp] = this
   454       let ?E' = "\<lambda>l. E' (k l) l"
   455       have "prob (\<Inter>j\<in>K. A j) = prob (\<Inter>l\<in>(\<Union>k\<in>K. L k). ?E' l)"
   456         by (auto simp: A intro!: arg_cong[where f=prob])
   457       also have "\<dots> = (\<Prod>l\<in>(\<Union>k\<in>K. L k). prob (?E' l))"
   458         using L K E' by (intro indep_setsD[OF indep]) (simp_all add: UN_mono)
   459       also have "\<dots> = (\<Prod>j\<in>K. \<Prod>l\<in>L j. prob (E' j l))"
   460         using K L L_inj by (subst setprod_UN_disjoint) auto
   461       also have "\<dots> = (\<Prod>j\<in>K. prob (A j))"
   462         using K L E' by (auto simp add: A intro!: setprod_cong indep_setsD[OF indep, symmetric]) blast
   463       finally show "prob (\<Inter>j\<in>K. A j) = (\<Prod>j\<in>K. prob (A j))" .
   464     qed
   465   next
   466     fix j assume "j \<in> J"
   467     show "Int_stable (?E j)"
   468     proof (rule Int_stableI)
   469       fix a assume "a \<in> ?E j" then obtain Ka Ea
   470         where a: "a = (\<Inter>k\<in>Ka. Ea k)" "finite Ka" "Ka \<noteq> {}" "Ka \<subseteq> I j" "\<And>k. k\<in>Ka \<Longrightarrow> Ea k \<in> E k" by auto
   471       fix b assume "b \<in> ?E j" then obtain Kb Eb
   472         where b: "b = (\<Inter>k\<in>Kb. Eb k)" "finite Kb" "Kb \<noteq> {}" "Kb \<subseteq> I j" "\<And>k. k\<in>Kb \<Longrightarrow> Eb k \<in> E k" by auto
   473       let ?A = "\<lambda>k. (if k \<in> Ka \<inter> Kb then Ea k \<inter> Eb k else if k \<in> Kb then Eb k else if k \<in> Ka then Ea k else {})"
   474       have "a \<inter> b = INTER (Ka \<union> Kb) ?A"
   475         by (simp add: a b set_eq_iff) auto
   476       with a b `j \<in> J` Int_stableD[OF Int_stable] show "a \<inter> b \<in> ?E j"
   477         by (intro CollectI exI[of _ "Ka \<union> Kb"] exI[of _ ?A]) auto
   478     qed
   479   qed
   480   ultimately show ?thesis
   481     by (simp cong: indep_sets_cong)
   482 qed
   483 
   484 definition (in prob_space) tail_events where
   485   "tail_events A = (\<Inter>n. sigma_sets (space M) (UNION {n..} A))"
   486 
   487 lemma (in prob_space) tail_events_sets:
   488   assumes A: "\<And>i::nat. A i \<subseteq> events"
   489   shows "tail_events A \<subseteq> events"
   490 proof
   491   fix X assume X: "X \<in> tail_events A"
   492   let ?A = "(\<Inter>n. sigma_sets (space M) (UNION {n..} A))"
   493   from X have "\<And>n::nat. X \<in> sigma_sets (space M) (UNION {n..} A)" by (auto simp: tail_events_def)
   494   from this[of 0] have "X \<in> sigma_sets (space M) (UNION UNIV A)" by simp
   495   then show "X \<in> events"
   496     by induct (insert A, auto)
   497 qed
   498 
   499 lemma (in prob_space) sigma_algebra_tail_events:
   500   assumes "\<And>i::nat. sigma_algebra (space M) (A i)"
   501   shows "sigma_algebra (space M) (tail_events A)"
   502   unfolding tail_events_def
   503 proof (simp add: sigma_algebra_iff2, safe)
   504   let ?A = "(\<Inter>n. sigma_sets (space M) (UNION {n..} A))"
   505   interpret A: sigma_algebra "space M" "A i" for i by fact
   506   { fix X x assume "X \<in> ?A" "x \<in> X"
   507     then have "\<And>n. X \<in> sigma_sets (space M) (UNION {n..} A)" by auto
   508     from this[of 0] have "X \<in> sigma_sets (space M) (UNION UNIV A)" by simp
   509     then have "X \<subseteq> space M"
   510       by induct (insert A.sets_into_space, auto)
   511     with `x \<in> X` show "x \<in> space M" by auto }
   512   { fix F :: "nat \<Rightarrow> 'a set" and n assume "range F \<subseteq> ?A"
   513     then show "(UNION UNIV F) \<in> sigma_sets (space M) (UNION {n..} A)"
   514       by (intro sigma_sets.Union) auto }
   515 qed (auto intro!: sigma_sets.Compl sigma_sets.Empty)
   516 
   517 lemma (in prob_space) kolmogorov_0_1_law:
   518   fixes A :: "nat \<Rightarrow> 'a set set"
   519   assumes "\<And>i::nat. sigma_algebra (space M) (A i)"
   520   assumes indep: "indep_sets A UNIV"
   521   and X: "X \<in> tail_events A"
   522   shows "prob X = 0 \<or> prob X = 1"
   523 proof -
   524   have A: "\<And>i. A i \<subseteq> events"
   525     using indep unfolding indep_sets_def by simp
   526 
   527   let ?D = "{D \<in> events. prob (X \<inter> D) = prob X * prob D}"
   528   interpret A: sigma_algebra "space M" "A i" for i by fact
   529   interpret T: sigma_algebra "space M" "tail_events A"
   530     by (rule sigma_algebra_tail_events) fact
   531   have "X \<subseteq> space M" using T.space_closed X by auto
   532 
   533   have X_in: "X \<in> events"
   534     using tail_events_sets A X by auto
   535 
   536   interpret D: dynkin_system "space M" ?D
   537   proof (rule dynkin_systemI)
   538     fix D assume "D \<in> ?D" then show "D \<subseteq> space M"
   539       using sets_into_space by auto
   540   next
   541     show "space M \<in> ?D"
   542       using prob_space `X \<subseteq> space M` by (simp add: Int_absorb2)
   543   next
   544     fix A assume A: "A \<in> ?D"
   545     have "prob (X \<inter> (space M - A)) = prob (X - (X \<inter> A))"
   546       using `X \<subseteq> space M` by (auto intro!: arg_cong[where f=prob])
   547     also have "\<dots> = prob X - prob (X \<inter> A)"
   548       using X_in A by (intro finite_measure_Diff) auto
   549     also have "\<dots> = prob X * prob (space M) - prob X * prob A"
   550       using A prob_space by auto
   551     also have "\<dots> = prob X * prob (space M - A)"
   552       using X_in A sets_into_space
   553       by (subst finite_measure_Diff) (auto simp: field_simps)
   554     finally show "space M - A \<in> ?D"
   555       using A `X \<subseteq> space M` by auto
   556   next
   557     fix F :: "nat \<Rightarrow> 'a set" assume dis: "disjoint_family F" and "range F \<subseteq> ?D"
   558     then have F: "range F \<subseteq> events" "\<And>i. prob (X \<inter> F i) = prob X * prob (F i)"
   559       by auto
   560     have "(\<lambda>i. prob (X \<inter> F i)) sums prob (\<Union>i. X \<inter> F i)"
   561     proof (rule finite_measure_UNION)
   562       show "range (\<lambda>i. X \<inter> F i) \<subseteq> events"
   563         using F X_in by auto
   564       show "disjoint_family (\<lambda>i. X \<inter> F i)"
   565         using dis by (rule disjoint_family_on_bisimulation) auto
   566     qed
   567     with F have "(\<lambda>i. prob X * prob (F i)) sums prob (X \<inter> (\<Union>i. F i))"
   568       by simp
   569     moreover have "(\<lambda>i. prob X * prob (F i)) sums (prob X * prob (\<Union>i. F i))"
   570       by (intro sums_mult finite_measure_UNION F dis)
   571     ultimately have "prob (X \<inter> (\<Union>i. F i)) = prob X * prob (\<Union>i. F i)"
   572       by (auto dest!: sums_unique)
   573     with F show "(\<Union>i. F i) \<in> ?D"
   574       by auto
   575   qed
   576 
   577   { fix n
   578     have "indep_sets (\<lambda>b. sigma_sets (space M) (\<Union>m\<in>bool_case {..n} {Suc n..} b. A m)) UNIV"
   579     proof (rule indep_sets_collect_sigma)
   580       have *: "(\<Union>b. case b of True \<Rightarrow> {..n} | False \<Rightarrow> {Suc n..}) = UNIV" (is "?U = _")
   581         by (simp split: bool.split add: set_eq_iff) (metis not_less_eq_eq)
   582       with indep show "indep_sets A ?U" by simp
   583       show "disjoint_family (bool_case {..n} {Suc n..})"
   584         unfolding disjoint_family_on_def by (auto split: bool.split)
   585       fix m
   586       show "Int_stable (A m)"
   587         unfolding Int_stable_def using A.Int by auto
   588     qed
   589     also have "(\<lambda>b. sigma_sets (space M) (\<Union>m\<in>bool_case {..n} {Suc n..} b. A m)) =
   590       bool_case (sigma_sets (space M) (\<Union>m\<in>{..n}. A m)) (sigma_sets (space M) (\<Union>m\<in>{Suc n..}. A m))"
   591       by (auto intro!: ext split: bool.split)
   592     finally have indep: "indep_set (sigma_sets (space M) (\<Union>m\<in>{..n}. A m)) (sigma_sets (space M) (\<Union>m\<in>{Suc n..}. A m))"
   593       unfolding indep_set_def by simp
   594 
   595     have "sigma_sets (space M) (\<Union>m\<in>{..n}. A m) \<subseteq> ?D"
   596     proof (simp add: subset_eq, rule)
   597       fix D assume D: "D \<in> sigma_sets (space M) (\<Union>m\<in>{..n}. A m)"
   598       have "X \<in> sigma_sets (space M) (\<Union>m\<in>{Suc n..}. A m)"
   599         using X unfolding tail_events_def by simp
   600       from indep_setD[OF indep D this] indep_setD_ev1[OF indep] D
   601       show "D \<in> events \<and> prob (X \<inter> D) = prob X * prob D"
   602         by (auto simp add: ac_simps)
   603     qed }
   604   then have "(\<Union>n. sigma_sets (space M) (\<Union>m\<in>{..n}. A m)) \<subseteq> ?D" (is "?A \<subseteq> _")
   605     by auto
   606 
   607   note `X \<in> tail_events A`
   608   also {
   609     have "\<And>n. sigma_sets (space M) (\<Union>i\<in>{n..}. A i) \<subseteq> sigma_sets (space M) ?A"
   610       by (intro sigma_sets_subseteq UN_mono) auto
   611    then have "tail_events A \<subseteq> sigma_sets (space M) ?A"
   612       unfolding tail_events_def by auto }
   613   also have "sigma_sets (space M) ?A = dynkin (space M) ?A"
   614   proof (rule sigma_eq_dynkin)
   615     { fix B n assume "B \<in> sigma_sets (space M) (\<Union>m\<in>{..n}. A m)"
   616       then have "B \<subseteq> space M"
   617         by induct (insert A sets_into_space[of _ M], auto) }
   618     then show "?A \<subseteq> Pow (space M)" by auto
   619     show "Int_stable ?A"
   620     proof (rule Int_stableI)
   621       fix a assume "a \<in> ?A" then guess n .. note a = this
   622       fix b assume "b \<in> ?A" then guess m .. note b = this
   623       interpret Amn: sigma_algebra "space M" "sigma_sets (space M) (\<Union>i\<in>{..max m n}. A i)"
   624         using A sets_into_space[of _ M] by (intro sigma_algebra_sigma_sets) auto
   625       have "sigma_sets (space M) (\<Union>i\<in>{..n}. A i) \<subseteq> sigma_sets (space M) (\<Union>i\<in>{..max m n}. A i)"
   626         by (intro sigma_sets_subseteq UN_mono) auto
   627       with a have "a \<in> sigma_sets (space M) (\<Union>i\<in>{..max m n}. A i)" by auto
   628       moreover
   629       have "sigma_sets (space M) (\<Union>i\<in>{..m}. A i) \<subseteq> sigma_sets (space M) (\<Union>i\<in>{..max m n}. A i)"
   630         by (intro sigma_sets_subseteq UN_mono) auto
   631       with b have "b \<in> sigma_sets (space M) (\<Union>i\<in>{..max m n}. A i)" by auto
   632       ultimately have "a \<inter> b \<in> sigma_sets (space M) (\<Union>i\<in>{..max m n}. A i)"
   633         using Amn.Int[of a b] by simp
   634       then show "a \<inter> b \<in> (\<Union>n. sigma_sets (space M) (\<Union>i\<in>{..n}. A i))" by auto
   635     qed
   636   qed
   637   also have "dynkin (space M) ?A \<subseteq> ?D"
   638     using `?A \<subseteq> ?D` by (auto intro!: D.dynkin_subset)
   639   finally show ?thesis by auto
   640 qed
   641 
   642 lemma (in prob_space) borel_0_1_law:
   643   fixes F :: "nat \<Rightarrow> 'a set"
   644   assumes F2: "indep_events F UNIV"
   645   shows "prob (\<Inter>n. \<Union>m\<in>{n..}. F m) = 0 \<or> prob (\<Inter>n. \<Union>m\<in>{n..}. F m) = 1"
   646 proof (rule kolmogorov_0_1_law[of "\<lambda>i. sigma_sets (space M) { F i }"])
   647   have F1: "range F \<subseteq> events"
   648     using F2 by (simp add: indep_events_def subset_eq)
   649   { fix i show "sigma_algebra (space M) (sigma_sets (space M) {F i})"
   650       using sigma_algebra_sigma_sets[of "{F i}" "space M"] F1 sets_into_space
   651       by auto }
   652   show "indep_sets (\<lambda>i. sigma_sets (space M) {F i}) UNIV"
   653   proof (rule indep_sets_sigma)
   654     show "indep_sets (\<lambda>i. {F i}) UNIV"
   655       unfolding indep_events_def_alt[symmetric] by fact
   656     fix i show "Int_stable {F i}"
   657       unfolding Int_stable_def by simp
   658   qed
   659   let ?Q = "\<lambda>n. \<Union>i\<in>{n..}. F i"
   660   show "(\<Inter>n. \<Union>m\<in>{n..}. F m) \<in> tail_events (\<lambda>i. sigma_sets (space M) {F i})"
   661     unfolding tail_events_def
   662   proof
   663     fix j
   664     interpret S: sigma_algebra "space M" "sigma_sets (space M) (\<Union>i\<in>{j..}. sigma_sets (space M) {F i})"
   665       using order_trans[OF F1 space_closed]
   666       by (intro sigma_algebra_sigma_sets) (simp add: sigma_sets_singleton subset_eq)
   667     have "(\<Inter>n. ?Q n) = (\<Inter>n\<in>{j..}. ?Q n)"
   668       by (intro decseq_SucI INT_decseq_offset UN_mono) auto
   669     also have "\<dots> \<in> sigma_sets (space M) (\<Union>i\<in>{j..}. sigma_sets (space M) {F i})"
   670       using order_trans[OF F1 space_closed]
   671       by (safe intro!: S.countable_INT S.countable_UN)
   672          (auto simp: sigma_sets_singleton intro!: sigma_sets.Basic bexI)
   673     finally show "(\<Inter>n. ?Q n) \<in> sigma_sets (space M) (\<Union>i\<in>{j..}. sigma_sets (space M) {F i})"
   674       by simp
   675   qed
   676 qed
   677 
   678 lemma (in prob_space) indep_sets_finite:
   679   assumes I: "I \<noteq> {}" "finite I"
   680     and F: "\<And>i. i \<in> I \<Longrightarrow> F i \<subseteq> events" "\<And>i. i \<in> I \<Longrightarrow> space M \<in> F i"
   681   shows "indep_sets F I \<longleftrightarrow> (\<forall>A\<in>Pi I F. prob (\<Inter>j\<in>I. A j) = (\<Prod>j\<in>I. prob (A j)))"
   682 proof
   683   assume *: "indep_sets F I"
   684   from I show "\<forall>A\<in>Pi I F. prob (\<Inter>j\<in>I. A j) = (\<Prod>j\<in>I. prob (A j))"
   685     by (intro indep_setsD[OF *] ballI) auto
   686 next
   687   assume indep: "\<forall>A\<in>Pi I F. prob (\<Inter>j\<in>I. A j) = (\<Prod>j\<in>I. prob (A j))"
   688   show "indep_sets F I"
   689   proof (rule indep_setsI[OF F(1)])
   690     fix A J assume J: "J \<noteq> {}" "J \<subseteq> I" "finite J"
   691     assume A: "\<forall>j\<in>J. A j \<in> F j"
   692     let ?A = "\<lambda>j. if j \<in> J then A j else space M"
   693     have "prob (\<Inter>j\<in>I. ?A j) = prob (\<Inter>j\<in>J. A j)"
   694       using subset_trans[OF F(1) space_closed] J A
   695       by (auto intro!: arg_cong[where f=prob] split: split_if_asm) blast
   696     also
   697     from A F have "(\<lambda>j. if j \<in> J then A j else space M) \<in> Pi I F" (is "?A \<in> _")
   698       by (auto split: split_if_asm)
   699     with indep have "prob (\<Inter>j\<in>I. ?A j) = (\<Prod>j\<in>I. prob (?A j))"
   700       by auto
   701     also have "\<dots> = (\<Prod>j\<in>J. prob (A j))"
   702       unfolding if_distrib setprod.If_cases[OF `finite I`]
   703       using prob_space `J \<subseteq> I` by (simp add: Int_absorb1 setprod_1)
   704     finally show "prob (\<Inter>j\<in>J. A j) = (\<Prod>j\<in>J. prob (A j))" ..
   705   qed
   706 qed
   707 
   708 lemma (in prob_space) indep_vars_finite:
   709   fixes I :: "'i set"
   710   assumes I: "I \<noteq> {}" "finite I"
   711     and M': "\<And>i. i \<in> I \<Longrightarrow> sets (M' i) = sigma_sets (space (M' i)) (E i)"
   712     and rv: "\<And>i. i \<in> I \<Longrightarrow> random_variable (M' i) (X i)"
   713     and Int_stable: "\<And>i. i \<in> I \<Longrightarrow> Int_stable (E i)"
   714     and space: "\<And>i. i \<in> I \<Longrightarrow> space (M' i) \<in> E i" and closed: "\<And>i. i \<in> I \<Longrightarrow> E i \<subseteq> Pow (space (M' i))"
   715   shows "indep_vars M' X I \<longleftrightarrow>
   716     (\<forall>A\<in>(\<Pi> i\<in>I. E i). prob (\<Inter>j\<in>I. X j -` A j \<inter> space M) = (\<Prod>j\<in>I. prob (X j -` A j \<inter> space M)))"
   717 proof -
   718   from rv have X: "\<And>i. i \<in> I \<Longrightarrow> X i \<in> space M \<rightarrow> space (M' i)"
   719     unfolding measurable_def by simp
   720 
   721   { fix i assume "i\<in>I"
   722     from closed[OF `i \<in> I`]
   723     have "sigma_sets (space M) {X i -` A \<inter> space M |A. A \<in> sets (M' i)}
   724       = sigma_sets (space M) {X i -` A \<inter> space M |A. A \<in> E i}"
   725       unfolding sigma_sets_vimage_commute[OF X, OF `i \<in> I`, symmetric] M'[OF `i \<in> I`]
   726       by (subst sigma_sets_sigma_sets_eq) auto }
   727   note sigma_sets_X = this
   728 
   729   { fix i assume "i\<in>I"
   730     have "Int_stable {X i -` A \<inter> space M |A. A \<in> E i}"
   731     proof (rule Int_stableI)
   732       fix a assume "a \<in> {X i -` A \<inter> space M |A. A \<in> E i}"
   733       then obtain A where "a = X i -` A \<inter> space M" "A \<in> E i" by auto
   734       moreover
   735       fix b assume "b \<in> {X i -` A \<inter> space M |A. A \<in> E i}"
   736       then obtain B where "b = X i -` B \<inter> space M" "B \<in> E i" by auto
   737       moreover
   738       have "(X i -` A \<inter> space M) \<inter> (X i -` B \<inter> space M) = X i -` (A \<inter> B) \<inter> space M" by auto
   739       moreover note Int_stable[OF `i \<in> I`]
   740       ultimately
   741       show "a \<inter> b \<in> {X i -` A \<inter> space M |A. A \<in> E i}"
   742         by (auto simp del: vimage_Int intro!: exI[of _ "A \<inter> B"] dest: Int_stableD)
   743     qed }
   744   note indep_sets_X = indep_sets_sigma_sets_iff[OF this]
   745 
   746   { fix i assume "i \<in> I"
   747     { fix A assume "A \<in> E i"
   748       with M'[OF `i \<in> I`] have "A \<in> sets (M' i)" by auto
   749       moreover
   750       from rv[OF `i\<in>I`] have "X i \<in> measurable M (M' i)" by auto
   751       ultimately
   752       have "X i -` A \<inter> space M \<in> sets M" by (auto intro: measurable_sets) }
   753     with X[OF `i\<in>I`] space[OF `i\<in>I`]
   754     have "{X i -` A \<inter> space M |A. A \<in> E i} \<subseteq> events"
   755       "space M \<in> {X i -` A \<inter> space M |A. A \<in> E i}"
   756       by (auto intro!: exI[of _ "space (M' i)"]) }
   757   note indep_sets_finite_X = indep_sets_finite[OF I this]
   758 
   759   have "(\<forall>A\<in>\<Pi> i\<in>I. {X i -` A \<inter> space M |A. A \<in> E i}. prob (INTER I A) = (\<Prod>j\<in>I. prob (A j))) =
   760     (\<forall>A\<in>\<Pi> i\<in>I. E i. prob ((\<Inter>j\<in>I. X j -` A j) \<inter> space M) = (\<Prod>x\<in>I. prob (X x -` A x \<inter> space M)))"
   761     (is "?L = ?R")
   762   proof safe
   763     fix A assume ?L and A: "A \<in> (\<Pi> i\<in>I. E i)"
   764     from `?L`[THEN bspec, of "\<lambda>i. X i -` A i \<inter> space M"] A `I \<noteq> {}`
   765     show "prob ((\<Inter>j\<in>I. X j -` A j) \<inter> space M) = (\<Prod>x\<in>I. prob (X x -` A x \<inter> space M))"
   766       by (auto simp add: Pi_iff)
   767   next
   768     fix A assume ?R and A: "A \<in> (\<Pi> i\<in>I. {X i -` A \<inter> space M |A. A \<in> E i})"
   769     from A have "\<forall>i\<in>I. \<exists>B. A i = X i -` B \<inter> space M \<and> B \<in> E i" by auto
   770     from bchoice[OF this] obtain B where B: "\<forall>i\<in>I. A i = X i -` B i \<inter> space M"
   771       "B \<in> (\<Pi> i\<in>I. E i)" by auto
   772     from `?R`[THEN bspec, OF B(2)] B(1) `I \<noteq> {}`
   773     show "prob (INTER I A) = (\<Prod>j\<in>I. prob (A j))"
   774       by simp
   775   qed
   776   then show ?thesis using `I \<noteq> {}`
   777     by (simp add: rv indep_vars_def indep_sets_X sigma_sets_X indep_sets_finite_X cong: indep_sets_cong)
   778 qed
   779 
   780 lemma (in prob_space) indep_vars_compose:
   781   assumes "indep_vars M' X I"
   782   assumes rv: "\<And>i. i \<in> I \<Longrightarrow> Y i \<in> measurable (M' i) (N i)"
   783   shows "indep_vars N (\<lambda>i. Y i \<circ> X i) I"
   784   unfolding indep_vars_def
   785 proof
   786   from rv `indep_vars M' X I`
   787   show "\<forall>i\<in>I. random_variable (N i) (Y i \<circ> X i)"
   788     by (auto simp: indep_vars_def)
   789 
   790   have "indep_sets (\<lambda>i. sigma_sets (space M) {X i -` A \<inter> space M |A. A \<in> sets (M' i)}) I"
   791     using `indep_vars M' X I` by (simp add: indep_vars_def)
   792   then show "indep_sets (\<lambda>i. sigma_sets (space M) {(Y i \<circ> X i) -` A \<inter> space M |A. A \<in> sets (N i)}) I"
   793   proof (rule indep_sets_mono_sets)
   794     fix i assume "i \<in> I"
   795     with `indep_vars M' X I` have X: "X i \<in> space M \<rightarrow> space (M' i)"
   796       unfolding indep_vars_def measurable_def by auto
   797     { fix A assume "A \<in> sets (N i)"
   798       then have "\<exists>B. (Y i \<circ> X i) -` A \<inter> space M = X i -` B \<inter> space M \<and> B \<in> sets (M' i)"
   799         by (intro exI[of _ "Y i -` A \<inter> space (M' i)"])
   800            (auto simp: vimage_compose intro!: measurable_sets rv `i \<in> I` funcset_mem[OF X]) }
   801     then show "sigma_sets (space M) {(Y i \<circ> X i) -` A \<inter> space M |A. A \<in> sets (N i)} \<subseteq>
   802       sigma_sets (space M) {X i -` A \<inter> space M |A. A \<in> sets (M' i)}"
   803       by (intro sigma_sets_subseteq) (auto simp: vimage_compose)
   804   qed
   805 qed
   806 
   807 lemma (in prob_space) indep_varsD_finite:
   808   assumes X: "indep_vars M' X I"
   809   assumes I: "I \<noteq> {}" "finite I" "\<And>i. i \<in> I \<Longrightarrow> A i \<in> sets (M' i)"
   810   shows "prob (\<Inter>i\<in>I. X i -` A i \<inter> space M) = (\<Prod>i\<in>I. prob (X i -` A i \<inter> space M))"
   811 proof (rule indep_setsD)
   812   show "indep_sets (\<lambda>i. sigma_sets (space M) {X i -` A \<inter> space M |A. A \<in> sets (M' i)}) I"
   813     using X by (auto simp: indep_vars_def)
   814   show "I \<subseteq> I" "I \<noteq> {}" "finite I" using I by auto
   815   show "\<forall>i\<in>I. X i -` A i \<inter> space M \<in> sigma_sets (space M) {X i -` A \<inter> space M |A. A \<in> sets (M' i)}"
   816     using I by auto
   817 qed
   818 
   819 lemma (in prob_space) indep_varsD:
   820   assumes X: "indep_vars M' X I"
   821   assumes I: "J \<noteq> {}" "finite J" "J \<subseteq> I" "\<And>i. i \<in> J \<Longrightarrow> A i \<in> sets (M' i)"
   822   shows "prob (\<Inter>i\<in>J. X i -` A i \<inter> space M) = (\<Prod>i\<in>J. prob (X i -` A i \<inter> space M))"
   823 proof (rule indep_setsD)
   824   show "indep_sets (\<lambda>i. sigma_sets (space M) {X i -` A \<inter> space M |A. A \<in> sets (M' i)}) I"
   825     using X by (auto simp: indep_vars_def)
   826   show "\<forall>i\<in>J. X i -` A i \<inter> space M \<in> sigma_sets (space M) {X i -` A \<inter> space M |A. A \<in> sets (M' i)}"
   827     using I by auto
   828 qed fact+
   829 
   830 lemma prod_algebra_cong:
   831   assumes "I = J" and sets: "(\<And>i. i \<in> I \<Longrightarrow> sets (M i) = sets (N i))"
   832   shows "prod_algebra I M = prod_algebra J N"
   833 proof -
   834   have space: "\<And>i. i \<in> I \<Longrightarrow> space (M i) = space (N i)"
   835     using sets_eq_imp_space_eq[OF sets] by auto
   836   with sets show ?thesis unfolding `I = J`
   837     by (intro antisym prod_algebra_mono) auto
   838 qed
   839 
   840 lemma space_in_prod_algebra:
   841   "(\<Pi>\<^isub>E i\<in>I. space (M i)) \<in> prod_algebra I M"
   842 proof cases
   843   assume "I = {}" then show ?thesis
   844     by (auto simp add: prod_algebra_def image_iff prod_emb_def)
   845 next
   846   assume "I \<noteq> {}"
   847   then obtain i where "i \<in> I" by auto
   848   then have "(\<Pi>\<^isub>E i\<in>I. space (M i)) = prod_emb I M {i} (\<Pi>\<^isub>E i\<in>{i}. space (M i))"
   849     by (auto simp: prod_emb_def Pi_iff)
   850   also have "\<dots> \<in> prod_algebra I M"
   851     using `i \<in> I` by (intro prod_algebraI) auto
   852   finally show ?thesis .
   853 qed
   854 
   855 lemma (in prob_space) indep_vars_iff_distr_eq_PiM:
   856   fixes I :: "'i set" and X :: "'i \<Rightarrow> 'a \<Rightarrow> 'b"
   857   assumes "I \<noteq> {}"
   858   assumes rv: "\<And>i. random_variable (M' i) (X i)"
   859   shows "indep_vars M' X I \<longleftrightarrow>
   860     distr M (\<Pi>\<^isub>M i\<in>I. M' i) (\<lambda>x. \<lambda>i\<in>I. X i x) = (\<Pi>\<^isub>M i\<in>I. distr M (M' i) (X i))"
   861 proof -
   862   let ?P = "\<Pi>\<^isub>M i\<in>I. M' i"
   863   let ?X = "\<lambda>x. \<lambda>i\<in>I. X i x"
   864   let ?D = "distr M ?P ?X"
   865   have X: "random_variable ?P ?X" by (intro measurable_restrict rv)
   866   interpret D: prob_space ?D by (intro prob_space_distr X)
   867 
   868   let ?D' = "\<lambda>i. distr M (M' i) (X i)"
   869   let ?P' = "\<Pi>\<^isub>M i\<in>I. distr M (M' i) (X i)"
   870   interpret D': prob_space "?D' i" for i by (intro prob_space_distr rv)
   871   interpret P: product_prob_space ?D' I ..
   872     
   873   show ?thesis
   874   proof
   875     assume "indep_vars M' X I"
   876     show "?D = ?P'"
   877     proof (rule measure_eqI_generator_eq)
   878       show "Int_stable (prod_algebra I M')"
   879         by (rule Int_stable_prod_algebra)
   880       show "prod_algebra I M' \<subseteq> Pow (space ?P)"
   881         using prod_algebra_sets_into_space by (simp add: space_PiM)
   882       show "sets ?D = sigma_sets (space ?P) (prod_algebra I M')"
   883         by (simp add: sets_PiM space_PiM)
   884       show "sets ?P' = sigma_sets (space ?P) (prod_algebra I M')"
   885         by (simp add: sets_PiM space_PiM cong: prod_algebra_cong)
   886       let ?A = "\<lambda>i. \<Pi>\<^isub>E i\<in>I. space (M' i)"
   887       show "range ?A \<subseteq> prod_algebra I M'" "(\<Union>i. ?A i) = space (Pi\<^isub>M I M')"
   888         by (auto simp: space_PiM intro!: space_in_prod_algebra cong: prod_algebra_cong)
   889       { fix i show "emeasure ?D (\<Pi>\<^isub>E i\<in>I. space (M' i)) \<noteq> \<infinity>" by auto }
   890     next
   891       fix E assume E: "E \<in> prod_algebra I M'"
   892       from prod_algebraE[OF E] guess J Y . note J = this
   893 
   894       from E have "E \<in> sets ?P" by (auto simp: sets_PiM)
   895       then have "emeasure ?D E = emeasure M (?X -` E \<inter> space M)"
   896         by (simp add: emeasure_distr X)
   897       also have "?X -` E \<inter> space M = (\<Inter>i\<in>J. X i -` Y i \<inter> space M)"
   898         using J `I \<noteq> {}` measurable_space[OF rv] by (auto simp: prod_emb_def Pi_iff split: split_if_asm)
   899       also have "emeasure M (\<Inter>i\<in>J. X i -` Y i \<inter> space M) = (\<Prod> i\<in>J. emeasure M (X i -` Y i \<inter> space M))"
   900         using `indep_vars M' X I` J `I \<noteq> {}` using indep_varsD[of M' X I J]
   901         by (auto simp: emeasure_eq_measure setprod_ereal)
   902       also have "\<dots> = (\<Prod> i\<in>J. emeasure (?D' i) (Y i))"
   903         using rv J by (simp add: emeasure_distr)
   904       also have "\<dots> = emeasure ?P' E"
   905         using P.emeasure_PiM_emb[of J Y] J by (simp add: prod_emb_def)
   906       finally show "emeasure ?D E = emeasure ?P' E" .
   907     qed
   908   next
   909     assume "?D = ?P'"
   910     show "indep_vars M' X I" unfolding indep_vars_def
   911     proof (intro conjI indep_setsI ballI rv)
   912       fix i show "sigma_sets (space M) {X i -` A \<inter> space M |A. A \<in> sets (M' i)} \<subseteq> events"
   913         by (auto intro!: sigma_sets_subset measurable_sets rv)
   914     next
   915       fix J Y' assume J: "J \<noteq> {}" "J \<subseteq> I" "finite J"
   916       assume Y': "\<forall>j\<in>J. Y' j \<in> sigma_sets (space M) {X j -` A \<inter> space M |A. A \<in> sets (M' j)}"
   917       have "\<forall>j\<in>J. \<exists>Y. Y' j = X j -` Y \<inter> space M \<and> Y \<in> sets (M' j)"
   918       proof
   919         fix j assume "j \<in> J"
   920         from Y'[rule_format, OF this] rv[of j]
   921         show "\<exists>Y. Y' j = X j -` Y \<inter> space M \<and> Y \<in> sets (M' j)"
   922           by (subst (asm) sigma_sets_vimage_commute[symmetric, of _ _ "space (M' j)"])
   923              (auto dest: measurable_space simp: sigma_sets_eq)
   924       qed
   925       from bchoice[OF this] obtain Y where
   926         Y: "\<And>j. j \<in> J \<Longrightarrow> Y' j = X j -` Y j \<inter> space M" "\<And>j. j \<in> J \<Longrightarrow> Y j \<in> sets (M' j)" by auto
   927       let ?E = "prod_emb I M' J (Pi\<^isub>E J Y)"
   928       from Y have "(\<Inter>j\<in>J. Y' j) = ?X -` ?E \<inter> space M"
   929         using J `I \<noteq> {}` measurable_space[OF rv] by (auto simp: prod_emb_def Pi_iff split: split_if_asm)
   930       then have "emeasure M (\<Inter>j\<in>J. Y' j) = emeasure M (?X -` ?E \<inter> space M)"
   931         by simp
   932       also have "\<dots> = emeasure ?D ?E"
   933         using Y  J by (intro emeasure_distr[symmetric] X sets_PiM_I) auto
   934       also have "\<dots> = emeasure ?P' ?E"
   935         using `?D = ?P'` by simp
   936       also have "\<dots> = (\<Prod> i\<in>J. emeasure (?D' i) (Y i))"
   937         using P.emeasure_PiM_emb[of J Y] J Y by (simp add: prod_emb_def)
   938       also have "\<dots> = (\<Prod> i\<in>J. emeasure M (Y' i))"
   939         using rv J Y by (simp add: emeasure_distr)
   940       finally have "emeasure M (\<Inter>j\<in>J. Y' j) = (\<Prod> i\<in>J. emeasure M (Y' i))" .
   941       then show "prob (\<Inter>j\<in>J. Y' j) = (\<Prod> i\<in>J. prob (Y' i))"
   942         by (auto simp: emeasure_eq_measure setprod_ereal)
   943     qed
   944   qed
   945 qed
   946 
   947 lemma (in prob_space) indep_varD:
   948   assumes indep: "indep_var Ma A Mb B"
   949   assumes sets: "Xa \<in> sets Ma" "Xb \<in> sets Mb"
   950   shows "prob ((\<lambda>x. (A x, B x)) -` (Xa \<times> Xb) \<inter> space M) =
   951     prob (A -` Xa \<inter> space M) * prob (B -` Xb \<inter> space M)"
   952 proof -
   953   have "prob ((\<lambda>x. (A x, B x)) -` (Xa \<times> Xb) \<inter> space M) =
   954     prob (\<Inter>i\<in>UNIV. (bool_case A B i -` bool_case Xa Xb i \<inter> space M))"
   955     by (auto intro!: arg_cong[where f=prob] simp: UNIV_bool)
   956   also have "\<dots> = (\<Prod>i\<in>UNIV. prob (bool_case A B i -` bool_case Xa Xb i \<inter> space M))"
   957     using indep unfolding indep_var_def
   958     by (rule indep_varsD) (auto split: bool.split intro: sets)
   959   also have "\<dots> = prob (A -` Xa \<inter> space M) * prob (B -` Xb \<inter> space M)"
   960     unfolding UNIV_bool by simp
   961   finally show ?thesis .
   962 qed
   963 
   964 lemma (in prob_space)
   965   assumes "indep_var S X T Y"
   966   shows indep_var_rv1: "random_variable S X"
   967     and indep_var_rv2: "random_variable T Y"
   968 proof -
   969   have "\<forall>i\<in>UNIV. random_variable (bool_case S T i) (bool_case X Y i)"
   970     using assms unfolding indep_var_def indep_vars_def by auto
   971   then show "random_variable S X" "random_variable T Y"
   972     unfolding UNIV_bool by auto
   973 qed
   974 
   975 lemma measurable_bool_case[simp, intro]:
   976   "(\<lambda>(x, y). bool_case x y) \<in> measurable (M \<Otimes>\<^isub>M N) (Pi\<^isub>M UNIV (bool_case M N))"
   977     (is "?f \<in> measurable ?B ?P")
   978 proof (rule measurable_PiM_single)
   979   show "?f \<in> space ?B \<rightarrow> (\<Pi>\<^isub>E i\<in>UNIV. space (bool_case M N i))"
   980     by (auto simp: space_pair_measure extensional_def split: bool.split)
   981   fix i A assume "A \<in> sets (case i of True \<Rightarrow> M | False \<Rightarrow> N)"
   982   moreover then have "{\<omega> \<in> space (M \<Otimes>\<^isub>M N). prod_case bool_case \<omega> i \<in> A}
   983     = (case i of True \<Rightarrow> A \<times> space N | False \<Rightarrow> space M \<times> A)" 
   984     by (auto simp: space_pair_measure split: bool.split dest!: sets_into_space)
   985   ultimately show "{\<omega> \<in> space (M \<Otimes>\<^isub>M N). prod_case bool_case \<omega> i \<in> A} \<in> sets ?B"
   986     by (auto split: bool.split)
   987 qed
   988 
   989 lemma borel_measurable_indicator':
   990   "A \<in> sets N \<Longrightarrow> f \<in> measurable M N \<Longrightarrow> (\<lambda>x. indicator A (f x)) \<in> borel_measurable M"
   991   using measurable_comp[OF _ borel_measurable_indicator, of f M N A] by (auto simp add: comp_def)
   992 
   993 lemma (in product_sigma_finite) distr_component:
   994   "distr (M i) (Pi\<^isub>M {i} M) (\<lambda>x. \<lambda>i\<in>{i}. x) = Pi\<^isub>M {i} M" (is "?D = ?P")
   995 proof (intro measure_eqI[symmetric])
   996   interpret I: finite_product_sigma_finite M "{i}" by default simp
   997 
   998   have eq: "\<And>x. x \<in> extensional {i} \<Longrightarrow> (\<lambda>j\<in>{i}. x i) = x"
   999     by (auto simp: extensional_def restrict_def)
  1000 
  1001   fix A assume A: "A \<in> sets ?P"
  1002   then have "emeasure ?P A = (\<integral>\<^isup>+x. indicator A x \<partial>?P)" 
  1003     by simp
  1004   also have "\<dots> = (\<integral>\<^isup>+x. indicator ((\<lambda>x. \<lambda>i\<in>{i}. x) -` A \<inter> space (M i)) (x i) \<partial>PiM {i} M)" 
  1005     by (intro positive_integral_cong) (auto simp: space_PiM indicator_def simp: eq)
  1006   also have "\<dots> = emeasure ?D A"
  1007     using A by (simp add: product_positive_integral_singleton emeasure_distr)
  1008   finally show "emeasure (Pi\<^isub>M {i} M) A = emeasure ?D A" .
  1009 qed simp
  1010 
  1011 lemma pair_measure_eqI:
  1012   assumes "sigma_finite_measure M1" "sigma_finite_measure M2"
  1013   assumes sets: "sets (M1 \<Otimes>\<^isub>M M2) = sets M"
  1014   assumes emeasure: "\<And>A B. A \<in> sets M1 \<Longrightarrow> B \<in> sets M2 \<Longrightarrow> emeasure M1 A * emeasure M2 B = emeasure M (A \<times> B)"
  1015   shows "M1 \<Otimes>\<^isub>M M2 = M"
  1016 proof -
  1017   interpret M1: sigma_finite_measure M1 by fact
  1018   interpret M2: sigma_finite_measure M2 by fact
  1019   interpret pair_sigma_finite M1 M2 by default
  1020   from sigma_finite_up_in_pair_measure_generator guess F :: "nat \<Rightarrow> ('a \<times> 'b) set" .. note F = this
  1021   let ?E = "{a \<times> b |a b. a \<in> sets M1 \<and> b \<in> sets M2}"
  1022   let ?P = "M1 \<Otimes>\<^isub>M M2"
  1023   show ?thesis
  1024   proof (rule measure_eqI_generator_eq[OF Int_stable_pair_measure_generator[of M1 M2]])
  1025     show "?E \<subseteq> Pow (space ?P)"
  1026       using space_closed[of M1] space_closed[of M2] by (auto simp: space_pair_measure)
  1027     show "sets ?P = sigma_sets (space ?P) ?E"
  1028       by (simp add: sets_pair_measure space_pair_measure)
  1029     then show "sets M = sigma_sets (space ?P) ?E"
  1030       using sets[symmetric] by simp
  1031   next
  1032     show "range F \<subseteq> ?E" "(\<Union>i. F i) = space ?P" "\<And>i. emeasure ?P (F i) \<noteq> \<infinity>"
  1033       using F by (auto simp: space_pair_measure)
  1034   next
  1035     fix X assume "X \<in> ?E"
  1036     then obtain A B where X[simp]: "X = A \<times> B" and A: "A \<in> sets M1" and B: "B \<in> sets M2" by auto
  1037     then have "emeasure ?P X = emeasure M1 A * emeasure M2 B"
  1038        by (simp add: M2.emeasure_pair_measure_Times)
  1039     also have "\<dots> = emeasure M (A \<times> B)"
  1040       using A B emeasure by auto
  1041     finally show "emeasure ?P X = emeasure M X"
  1042       by simp
  1043   qed
  1044 qed
  1045 
  1046 lemma pair_measure_eq_distr_PiM:
  1047   fixes M1 :: "'a measure" and M2 :: "'a measure"
  1048   assumes "sigma_finite_measure M1" "sigma_finite_measure M2"
  1049   shows "(M1 \<Otimes>\<^isub>M M2) = distr (Pi\<^isub>M UNIV (bool_case M1 M2)) (M1 \<Otimes>\<^isub>M M2) (\<lambda>x. (x True, x False))"
  1050     (is "?P = ?D")
  1051 proof (rule pair_measure_eqI[OF assms])
  1052   interpret B: product_sigma_finite "bool_case M1 M2"
  1053     unfolding product_sigma_finite_def using assms by (auto split: bool.split)
  1054   let ?B = "Pi\<^isub>M UNIV (bool_case M1 M2)"
  1055 
  1056   have [simp]: "fst \<circ> (\<lambda>x. (x True, x False)) = (\<lambda>x. x True)" "snd \<circ> (\<lambda>x. (x True, x False)) = (\<lambda>x. x False)"
  1057     by auto
  1058   fix A B assume A: "A \<in> sets M1" and B: "B \<in> sets M2"
  1059   have "emeasure M1 A * emeasure M2 B = (\<Prod> i\<in>UNIV. emeasure (bool_case M1 M2 i) (bool_case A B i))"
  1060     by (simp add: UNIV_bool ac_simps)
  1061   also have "\<dots> = emeasure ?B (Pi\<^isub>E UNIV (bool_case A B))"
  1062     using A B by (subst B.emeasure_PiM) (auto split: bool.split)
  1063   also have "Pi\<^isub>E UNIV (bool_case A B) = (\<lambda>x. (x True, x False)) -` (A \<times> B) \<inter> space ?B"
  1064     using A[THEN sets_into_space] B[THEN sets_into_space]
  1065     by (auto simp: Pi_iff all_bool_eq space_PiM split: bool.split)
  1066   finally show "emeasure M1 A * emeasure M2 B = emeasure ?D (A \<times> B)"
  1067     using A B
  1068       measurable_component_singleton[of True UNIV "bool_case M1 M2"]
  1069       measurable_component_singleton[of False UNIV "bool_case M1 M2"]
  1070     by (subst emeasure_distr) (auto simp: measurable_pair_iff)
  1071 qed simp
  1072 
  1073 lemma measurable_Pair:
  1074   assumes rvs: "X \<in> measurable M S" "Y \<in> measurable M T"
  1075   shows "(\<lambda>x. (X x, Y x)) \<in> measurable M (S \<Otimes>\<^isub>M T)"
  1076 proof -
  1077   have [simp]: "fst \<circ> (\<lambda>x. (X x, Y x)) = (\<lambda>x. X x)" "snd \<circ> (\<lambda>x. (X x, Y x)) = (\<lambda>x. Y x)"
  1078     by auto
  1079   show " (\<lambda>x. (X x, Y x)) \<in> measurable M (S \<Otimes>\<^isub>M T)"
  1080     by (auto simp: measurable_pair_iff rvs)
  1081 qed
  1082 
  1083 lemma (in prob_space) indep_var_distribution_eq:
  1084   "indep_var S X T Y \<longleftrightarrow> random_variable S X \<and> random_variable T Y \<and>
  1085     distr M S X \<Otimes>\<^isub>M distr M T Y = distr M (S \<Otimes>\<^isub>M T) (\<lambda>x. (X x, Y x))" (is "_ \<longleftrightarrow> _ \<and> _ \<and> ?S \<Otimes>\<^isub>M ?T = ?J")
  1086 proof safe
  1087   assume "indep_var S X T Y"
  1088   then show rvs: "random_variable S X" "random_variable T Y"
  1089     by (blast dest: indep_var_rv1 indep_var_rv2)+
  1090   then have XY: "random_variable (S \<Otimes>\<^isub>M T) (\<lambda>x. (X x, Y x))"
  1091     by (rule measurable_Pair)
  1092 
  1093   interpret X: prob_space ?S by (rule prob_space_distr) fact
  1094   interpret Y: prob_space ?T by (rule prob_space_distr) fact
  1095   interpret XY: pair_prob_space ?S ?T ..
  1096   show "?S \<Otimes>\<^isub>M ?T = ?J"
  1097   proof (rule pair_measure_eqI)
  1098     show "sigma_finite_measure ?S" ..
  1099     show "sigma_finite_measure ?T" ..
  1100 
  1101     fix A B assume A: "A \<in> sets ?S" and B: "B \<in> sets ?T"
  1102     have "emeasure ?J (A \<times> B) = emeasure M ((\<lambda>x. (X x, Y x)) -` (A \<times> B) \<inter> space M)"
  1103       using A B by (intro emeasure_distr[OF XY]) auto
  1104     also have "\<dots> = emeasure M (X -` A \<inter> space M) * emeasure M (Y -` B \<inter> space M)"
  1105       using indep_varD[OF `indep_var S X T Y`, of A B] A B by (simp add: emeasure_eq_measure)
  1106     also have "\<dots> = emeasure ?S A * emeasure ?T B"
  1107       using rvs A B by (simp add: emeasure_distr)
  1108     finally show "emeasure ?S A * emeasure ?T B = emeasure ?J (A \<times> B)" by simp
  1109   qed simp
  1110 next
  1111   assume rvs: "random_variable S X" "random_variable T Y"
  1112   then have XY: "random_variable (S \<Otimes>\<^isub>M T) (\<lambda>x. (X x, Y x))"
  1113     by (rule measurable_Pair)
  1114 
  1115   let ?S = "distr M S X" and ?T = "distr M T Y"
  1116   interpret X: prob_space ?S by (rule prob_space_distr) fact
  1117   interpret Y: prob_space ?T by (rule prob_space_distr) fact
  1118   interpret XY: pair_prob_space ?S ?T ..
  1119 
  1120   assume "?S \<Otimes>\<^isub>M ?T = ?J"
  1121 
  1122   { fix S and X
  1123     have "Int_stable {X -` A \<inter> space M |A. A \<in> sets S}"
  1124     proof (safe intro!: Int_stableI)
  1125       fix A B assume "A \<in> sets S" "B \<in> sets S"
  1126       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"
  1127         by (intro exI[of _ "A \<inter> B"]) auto
  1128     qed }
  1129   note Int_stable = this
  1130 
  1131   show "indep_var S X T Y" unfolding indep_var_eq
  1132   proof (intro conjI indep_set_sigma_sets Int_stable rvs)
  1133     show "indep_set {X -` A \<inter> space M |A. A \<in> sets S} {Y -` A \<inter> space M |A. A \<in> sets T}"
  1134     proof (safe intro!: indep_setI)
  1135       { fix A assume "A \<in> sets S" then show "X -` A \<inter> space M \<in> sets M"
  1136         using `X \<in> measurable M S` by (auto intro: measurable_sets) }
  1137       { fix A assume "A \<in> sets T" then show "Y -` A \<inter> space M \<in> sets M"
  1138         using `Y \<in> measurable M T` by (auto intro: measurable_sets) }
  1139     next
  1140       fix A B assume ab: "A \<in> sets S" "B \<in> sets T"
  1141       then have "ereal (prob ((X -` A \<inter> space M) \<inter> (Y -` B \<inter> space M))) = emeasure ?J (A \<times> B)"
  1142         using XY by (auto simp add: emeasure_distr emeasure_eq_measure intro!: arg_cong[where f="prob"])
  1143       also have "\<dots> = emeasure (?S \<Otimes>\<^isub>M ?T) (A \<times> B)"
  1144         unfolding `?S \<Otimes>\<^isub>M ?T = ?J` ..
  1145       also have "\<dots> = emeasure ?S A * emeasure ?T B"
  1146         using ab by (simp add: Y.emeasure_pair_measure_Times)
  1147       finally show "prob ((X -` A \<inter> space M) \<inter> (Y -` B \<inter> space M)) =
  1148         prob (X -` A \<inter> space M) * prob (Y -` B \<inter> space M)"
  1149         using rvs ab by (simp add: emeasure_eq_measure emeasure_distr)
  1150     qed
  1151   qed
  1152 qed
  1153 
  1154 lemma (in prob_space) distributed_joint_indep:
  1155   assumes S: "sigma_finite_measure S" and T: "sigma_finite_measure T"
  1156   assumes X: "distributed M S X Px" and Y: "distributed M T Y Py"
  1157   assumes indep: "indep_var S X T Y"
  1158   shows "distributed M (S \<Otimes>\<^isub>M T) (\<lambda>x. (X x, Y x)) (\<lambda>(x, y). Px x * Py y)"
  1159   using indep_var_distribution_eq[of S X T Y] indep
  1160   by (intro distributed_joint_indep'[OF S T X Y]) auto
  1161 
  1162 end