src/HOL/Probability/Independent_Family.thy
author hoelzl
Fri Feb 19 13:40:50 2016 +0100 (2016-02-19)
changeset 62378 85ed00c1fe7c
parent 62343 24106dc44def
child 62390 842917225d56
permissions -rw-r--r--
generalize more theorems to support enat and ennreal
     1 (*  Title:      HOL/Probability/Independent_Family.thy
     2     Author:     Johannes Hölzl, TU München
     3     Author:     Sudeep Kanav, TU München
     4 *)
     5 
     6 section \<open>Independent families of events, event sets, and random variables\<close>
     7 
     8 theory Independent_Family
     9   imports Probability_Measure Infinite_Product_Measure
    10 begin
    11 
    12 definition (in prob_space)
    13   "indep_sets F I \<longleftrightarrow> (\<forall>i\<in>I. F i \<subseteq> events) \<and>
    14     (\<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))))"
    15 
    16 definition (in prob_space)
    17   "indep_set A B \<longleftrightarrow> indep_sets (case_bool A B) UNIV"
    18 
    19 definition (in prob_space)
    20   indep_events_def_alt: "indep_events A I \<longleftrightarrow> indep_sets (\<lambda>i. {A i}) I"
    21 
    22 lemma (in prob_space) indep_events_def:
    23   "indep_events A I \<longleftrightarrow> (A`I \<subseteq> events) \<and>
    24     (\<forall>J\<subseteq>I. J \<noteq> {} \<longrightarrow> finite J \<longrightarrow> prob (\<Inter>j\<in>J. A j) = (\<Prod>j\<in>J. prob (A j)))"
    25   unfolding indep_events_def_alt indep_sets_def
    26   apply (simp add: Ball_def Pi_iff image_subset_iff_funcset)
    27   apply (intro conj_cong refl arg_cong[where f=All] ext imp_cong)
    28   apply auto
    29   done
    30 
    31 lemma (in prob_space) indep_eventsI:
    32   "(\<And>i. i \<in> I \<Longrightarrow> F i \<in> sets M) \<Longrightarrow> (\<And>J. J \<subseteq> I \<Longrightarrow> finite J \<Longrightarrow> J \<noteq> {} \<Longrightarrow> prob (\<Inter>i\<in>J. F i) = (\<Prod>i\<in>J. prob (F i))) \<Longrightarrow> indep_events F I"
    33   by (auto simp: indep_events_def)
    34 
    35 definition (in prob_space)
    36   "indep_event A B \<longleftrightarrow> indep_events (case_bool A B) UNIV"
    37 
    38 lemma (in prob_space) indep_sets_cong:
    39   "I = J \<Longrightarrow> (\<And>i. i \<in> I \<Longrightarrow> F i = G i) \<Longrightarrow> indep_sets F I \<longleftrightarrow> indep_sets G J"
    40   by (simp add: indep_sets_def, intro conj_cong all_cong imp_cong ball_cong) blast+
    41 
    42 lemma (in prob_space) indep_events_finite_index_events:
    43   "indep_events F I \<longleftrightarrow> (\<forall>J\<subseteq>I. J \<noteq> {} \<longrightarrow> finite J \<longrightarrow> indep_events F J)"
    44   by (auto simp: indep_events_def)
    45 
    46 lemma (in prob_space) indep_sets_finite_index_sets:
    47   "indep_sets F I \<longleftrightarrow> (\<forall>J\<subseteq>I. J \<noteq> {} \<longrightarrow> finite J \<longrightarrow> indep_sets F J)"
    48 proof (intro iffI allI impI)
    49   assume *: "\<forall>J\<subseteq>I. J \<noteq> {} \<longrightarrow> finite J \<longrightarrow> indep_sets F J"
    50   show "indep_sets F I" unfolding indep_sets_def
    51   proof (intro conjI ballI allI impI)
    52     fix i assume "i \<in> I"
    53     with *[THEN spec, of "{i}"] show "F i \<subseteq> events"
    54       by (auto simp: indep_sets_def)
    55   qed (insert *, auto simp: indep_sets_def)
    56 qed (auto simp: indep_sets_def)
    57 
    58 lemma (in prob_space) indep_sets_mono_index:
    59   "J \<subseteq> I \<Longrightarrow> indep_sets F I \<Longrightarrow> indep_sets F J"
    60   unfolding indep_sets_def by auto
    61 
    62 lemma (in prob_space) indep_sets_mono_sets:
    63   assumes indep: "indep_sets F I"
    64   assumes mono: "\<And>i. i\<in>I \<Longrightarrow> G i \<subseteq> F i"
    65   shows "indep_sets G I"
    66 proof -
    67   have "(\<forall>i\<in>I. F i \<subseteq> events) \<Longrightarrow> (\<forall>i\<in>I. G i \<subseteq> events)"
    68     using mono by auto
    69   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)"
    70     using mono by (auto simp: Pi_iff)
    71   ultimately show ?thesis
    72     using indep by (auto simp: indep_sets_def)
    73 qed
    74 
    75 lemma (in prob_space) indep_sets_mono:
    76   assumes indep: "indep_sets F I"
    77   assumes mono: "J \<subseteq> I" "\<And>i. i\<in>J \<Longrightarrow> G i \<subseteq> F i"
    78   shows "indep_sets G J"
    79   apply (rule indep_sets_mono_sets)
    80   apply (rule indep_sets_mono_index)
    81   apply (fact +)
    82   done
    83 
    84 lemma (in prob_space) indep_setsI:
    85   assumes "\<And>i. i \<in> I \<Longrightarrow> F i \<subseteq> events"
    86     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))"
    87   shows "indep_sets F I"
    88   using assms unfolding indep_sets_def by (auto simp: Pi_iff)
    89 
    90 lemma (in prob_space) indep_setsD:
    91   assumes "indep_sets F I" and "J \<subseteq> I" "J \<noteq> {}" "finite J" "\<forall>j\<in>J. A j \<in> F j"
    92   shows "prob (\<Inter>j\<in>J. A j) = (\<Prod>j\<in>J. prob (A j))"
    93   using assms unfolding indep_sets_def by auto
    94 
    95 lemma (in prob_space) indep_setI:
    96   assumes ev: "A \<subseteq> events" "B \<subseteq> events"
    97     and indep: "\<And>a b. a \<in> A \<Longrightarrow> b \<in> B \<Longrightarrow> prob (a \<inter> b) = prob a * prob b"
    98   shows "indep_set A B"
    99   unfolding indep_set_def
   100 proof (rule indep_setsI)
   101   fix F J assume "J \<noteq> {}" "J \<subseteq> UNIV"
   102     and F: "\<forall>j\<in>J. F j \<in> (case j of True \<Rightarrow> A | False \<Rightarrow> B)"
   103   have "J \<in> Pow UNIV" by auto
   104   with F \<open>J \<noteq> {}\<close> indep[of "F True" "F False"]
   105   show "prob (\<Inter>j\<in>J. F j) = (\<Prod>j\<in>J. prob (F j))"
   106     unfolding UNIV_bool Pow_insert by (auto simp: ac_simps)
   107 qed (auto split: bool.split simp: ev)
   108 
   109 lemma (in prob_space) indep_setD:
   110   assumes indep: "indep_set A B" and ev: "a \<in> A" "b \<in> B"
   111   shows "prob (a \<inter> b) = prob a * prob b"
   112   using indep[unfolded indep_set_def, THEN indep_setsD, of UNIV "case_bool a b"] ev
   113   by (simp add: ac_simps UNIV_bool)
   114 
   115 lemma (in prob_space)
   116   assumes indep: "indep_set A B"
   117   shows indep_setD_ev1: "A \<subseteq> events"
   118     and indep_setD_ev2: "B \<subseteq> events"
   119   using indep unfolding indep_set_def indep_sets_def UNIV_bool by auto
   120 
   121 lemma (in prob_space) indep_sets_dynkin:
   122   assumes indep: "indep_sets F I"
   123   shows "indep_sets (\<lambda>i. dynkin (space M) (F i)) I"
   124     (is "indep_sets ?F I")
   125 proof (subst indep_sets_finite_index_sets, intro allI impI ballI)
   126   fix J assume "finite J" "J \<subseteq> I" "J \<noteq> {}"
   127   with indep have "indep_sets F J"
   128     by (subst (asm) indep_sets_finite_index_sets) auto
   129   { fix J K assume "indep_sets F K"
   130     let ?G = "\<lambda>S i. if i \<in> S then ?F i else F i"
   131     assume "finite J" "J \<subseteq> K"
   132     then have "indep_sets (?G J) K"
   133     proof induct
   134       case (insert j J)
   135       moreover def G \<equiv> "?G J"
   136       ultimately have G: "indep_sets G K" "\<And>i. i \<in> K \<Longrightarrow> G i \<subseteq> events" and "j \<in> K"
   137         by (auto simp: indep_sets_def)
   138       let ?D = "{E\<in>events. indep_sets (G(j := {E})) K }"
   139       { fix X assume X: "X \<in> events"
   140         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)
   141           \<Longrightarrow> prob ((\<Inter>i\<in>J. A i) \<inter> X) = prob X * (\<Prod>i\<in>J. prob (A i))"
   142         have "indep_sets (G(j := {X})) K"
   143         proof (rule indep_setsI)
   144           fix i assume "i \<in> K" then show "(G(j:={X})) i \<subseteq> events"
   145             using G X by auto
   146         next
   147           fix A J assume J: "J \<noteq> {}" "J \<subseteq> K" "finite J" "\<forall>i\<in>J. A i \<in> (G(j := {X})) i"
   148           show "prob (\<Inter>j\<in>J. A j) = (\<Prod>j\<in>J. prob (A j))"
   149           proof cases
   150             assume "j \<in> J"
   151             with J have "A j = X" by auto
   152             show ?thesis
   153             proof cases
   154               assume "J = {j}" then show ?thesis by simp
   155             next
   156               assume "J \<noteq> {j}"
   157               have "prob (\<Inter>i\<in>J. A i) = prob ((\<Inter>i\<in>J-{j}. A i) \<inter> X)"
   158                 using \<open>j \<in> J\<close> \<open>A j = X\<close> by (auto intro!: arg_cong[where f=prob] split: split_if_asm)
   159               also have "\<dots> = prob X * (\<Prod>i\<in>J-{j}. prob (A i))"
   160               proof (rule indep)
   161                 show "J - {j} \<noteq> {}" "J - {j} \<subseteq> K" "finite (J - {j})" "j \<notin> J - {j}"
   162                   using J \<open>J \<noteq> {j}\<close> \<open>j \<in> J\<close> by auto
   163                 show "\<forall>i\<in>J - {j}. A i \<in> G i"
   164                   using J by auto
   165               qed
   166               also have "\<dots> = prob (A j) * (\<Prod>i\<in>J-{j}. prob (A i))"
   167                 using \<open>A j = X\<close> by simp
   168               also have "\<dots> = (\<Prod>i\<in>J. prob (A i))"
   169                 unfolding setprod.insert_remove[OF \<open>finite J\<close>, symmetric, of "\<lambda>i. prob  (A i)"]
   170                 using \<open>j \<in> J\<close> by (simp add: insert_absorb)
   171               finally show ?thesis .
   172             qed
   173           next
   174             assume "j \<notin> J"
   175             with J have "\<forall>i\<in>J. A i \<in> G i" by (auto split: split_if_asm)
   176             with J show ?thesis
   177               by (intro indep_setsD[OF G(1)]) auto
   178           qed
   179         qed }
   180       note indep_sets_insert = this
   181       have "dynkin_system (space M) ?D"
   182       proof (rule dynkin_systemI', simp_all cong del: indep_sets_cong, safe)
   183         show "indep_sets (G(j := {{}})) K"
   184           by (rule indep_sets_insert) auto
   185       next
   186         fix X assume X: "X \<in> events" and G': "indep_sets (G(j := {X})) K"
   187         show "indep_sets (G(j := {space M - X})) K"
   188         proof (rule indep_sets_insert)
   189           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"
   190           then have A_sets: "\<And>i. i\<in>J \<Longrightarrow> A i \<in> events"
   191             using G by auto
   192           have "prob ((\<Inter>j\<in>J. A j) \<inter> (space M - X)) =
   193               prob ((\<Inter>j\<in>J. A j) - (\<Inter>i\<in>insert j J. (A(j := X)) i))"
   194             using A_sets sets.sets_into_space[of _ M] X \<open>J \<noteq> {}\<close>
   195             by (auto intro!: arg_cong[where f=prob] split: split_if_asm)
   196           also have "\<dots> = prob (\<Inter>j\<in>J. A j) - prob (\<Inter>i\<in>insert j J. (A(j := X)) i)"
   197             using J \<open>J \<noteq> {}\<close> \<open>j \<notin> J\<close> A_sets X sets.sets_into_space
   198             by (auto intro!: finite_measure_Diff sets.finite_INT split: split_if_asm)
   199           finally have "prob ((\<Inter>j\<in>J. A j) \<inter> (space M - X)) =
   200               prob (\<Inter>j\<in>J. A j) - prob (\<Inter>i\<in>insert j J. (A(j := X)) i)" .
   201           moreover {
   202             have "prob (\<Inter>j\<in>J. A j) = (\<Prod>j\<in>J. prob (A j))"
   203               using J A \<open>finite J\<close> by (intro indep_setsD[OF G(1)]) auto
   204             then have "prob (\<Inter>j\<in>J. A j) = prob (space M) * (\<Prod>i\<in>J. prob (A i))"
   205               using prob_space by simp }
   206           moreover {
   207             have "prob (\<Inter>i\<in>insert j J. (A(j := X)) i) = (\<Prod>i\<in>insert j J. prob ((A(j := X)) i))"
   208               using J A \<open>j \<in> K\<close> by (intro indep_setsD[OF G']) auto
   209             then have "prob (\<Inter>i\<in>insert j J. (A(j := X)) i) = prob X * (\<Prod>i\<in>J. prob (A i))"
   210               using \<open>finite J\<close> \<open>j \<notin> J\<close> by (auto intro!: setprod.cong) }
   211           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))"
   212             by (simp add: field_simps)
   213           also have "\<dots> = prob (space M - X) * (\<Prod>i\<in>J. prob (A i))"
   214             using X A by (simp add: finite_measure_compl)
   215           finally show "prob ((\<Inter>j\<in>J. A j) \<inter> (space M - X)) = prob (space M - X) * (\<Prod>i\<in>J. prob (A i))" .
   216         qed (insert X, auto)
   217       next
   218         fix F :: "nat \<Rightarrow> 'a set" assume disj: "disjoint_family F" and "range F \<subseteq> ?D"
   219         then have F: "\<And>i. F i \<in> events" "\<And>i. indep_sets (G(j:={F i})) K" by auto
   220         show "indep_sets (G(j := {\<Union>k. F k})) K"
   221         proof (rule indep_sets_insert)
   222           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"
   223           then have A_sets: "\<And>i. i\<in>J \<Longrightarrow> A i \<in> events"
   224             using G by auto
   225           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))"
   226             using \<open>J \<noteq> {}\<close> \<open>j \<notin> J\<close> \<open>j \<in> K\<close> by (auto intro!: arg_cong[where f=prob] split: split_if_asm)
   227           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))"
   228           proof (rule finite_measure_UNION)
   229             show "disjoint_family (\<lambda>k. \<Inter>i\<in>insert j J. (A(j := F k)) i)"
   230               using disj by (rule disjoint_family_on_bisimulation) auto
   231             show "range (\<lambda>k. \<Inter>i\<in>insert j J. (A(j := F k)) i) \<subseteq> events"
   232               using A_sets F \<open>finite J\<close> \<open>J \<noteq> {}\<close> \<open>j \<notin> J\<close> by (auto intro!: sets.Int)
   233           qed
   234           moreover { fix k
   235             from J A \<open>j \<in> K\<close> have "prob (\<Inter>i\<in>insert j J. (A(j := F k)) i) = prob (F k) * (\<Prod>i\<in>J. prob (A i))"
   236               by (subst indep_setsD[OF F(2)]) (auto intro!: setprod.cong split: split_if_asm)
   237             also have "\<dots> = prob (F k) * prob (\<Inter>i\<in>J. A i)"
   238               using J A \<open>j \<in> K\<close> by (subst indep_setsD[OF G(1)]) auto
   239             finally have "prob (\<Inter>i\<in>insert j J. (A(j := F k)) i) = prob (F k) * prob (\<Inter>i\<in>J. A i)" . }
   240           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)))"
   241             by simp
   242           moreover
   243           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))"
   244             using disj F(1) by (intro finite_measure_UNION sums_mult2) auto
   245           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)))"
   246             using J A \<open>j \<in> K\<close> by (subst indep_setsD[OF G(1), symmetric]) auto
   247           ultimately
   248           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))"
   249             by (auto dest!: sums_unique)
   250         qed (insert F, auto)
   251       qed (insert sets.sets_into_space, auto)
   252       then have mono: "dynkin (space M) (G j) \<subseteq> {E \<in> events. indep_sets (G(j := {E})) K}"
   253       proof (rule dynkin_system.dynkin_subset, safe)
   254         fix X assume "X \<in> G j"
   255         then show "X \<in> events" using G \<open>j \<in> K\<close> by auto
   256         from \<open>indep_sets G K\<close>
   257         show "indep_sets (G(j := {X})) K"
   258           by (rule indep_sets_mono_sets) (insert \<open>X \<in> G j\<close>, auto)
   259       qed
   260       have "indep_sets (G(j:=?D)) K"
   261       proof (rule indep_setsI)
   262         fix i assume "i \<in> K" then show "(G(j := ?D)) i \<subseteq> events"
   263           using G(2) by auto
   264       next
   265         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"
   266         show "prob (\<Inter>j\<in>J. A j) = (\<Prod>j\<in>J. prob (A j))"
   267         proof cases
   268           assume "j \<in> J"
   269           with A have indep: "indep_sets (G(j := {A j})) K" by auto
   270           from J A show ?thesis
   271             by (intro indep_setsD[OF indep]) auto
   272         next
   273           assume "j \<notin> J"
   274           with J A have "\<forall>i\<in>J. A i \<in> G i" by (auto split: split_if_asm)
   275           with J show ?thesis
   276             by (intro indep_setsD[OF G(1)]) auto
   277         qed
   278       qed
   279       then have "indep_sets (G(j := dynkin (space M) (G j))) K"
   280         by (rule indep_sets_mono_sets) (insert mono, auto)
   281       then show ?case
   282         by (rule indep_sets_mono_sets) (insert \<open>j \<in> K\<close> \<open>j \<notin> J\<close>, auto simp: G_def)
   283     qed (insert \<open>indep_sets F K\<close>, simp) }
   284   from this[OF \<open>indep_sets F J\<close> \<open>finite J\<close> subset_refl]
   285   show "indep_sets ?F J"
   286     by (rule indep_sets_mono_sets) auto
   287 qed
   288 
   289 lemma (in prob_space) indep_sets_sigma:
   290   assumes indep: "indep_sets F I"
   291   assumes stable: "\<And>i. i \<in> I \<Longrightarrow> Int_stable (F i)"
   292   shows "indep_sets (\<lambda>i. sigma_sets (space M) (F i)) I"
   293 proof -
   294   from indep_sets_dynkin[OF indep]
   295   show ?thesis
   296   proof (rule indep_sets_mono_sets, subst sigma_eq_dynkin, simp_all add: stable)
   297     fix i assume "i \<in> I"
   298     with indep have "F i \<subseteq> events" by (auto simp: indep_sets_def)
   299     with sets.sets_into_space show "F i \<subseteq> Pow (space M)" by auto
   300   qed
   301 qed
   302 
   303 lemma (in prob_space) indep_sets_sigma_sets_iff:
   304   assumes "\<And>i. i \<in> I \<Longrightarrow> Int_stable (F i)"
   305   shows "indep_sets (\<lambda>i. sigma_sets (space M) (F i)) I \<longleftrightarrow> indep_sets F I"
   306 proof
   307   assume "indep_sets F I" then show "indep_sets (\<lambda>i. sigma_sets (space M) (F i)) I"
   308     by (rule indep_sets_sigma) fact
   309 next
   310   assume "indep_sets (\<lambda>i. sigma_sets (space M) (F i)) I" then show "indep_sets F I"
   311     by (rule indep_sets_mono_sets) (intro subsetI sigma_sets.Basic)
   312 qed
   313 
   314 definition (in prob_space)
   315   indep_vars_def2: "indep_vars M' X I \<longleftrightarrow>
   316     (\<forall>i\<in>I. random_variable (M' i) (X i)) \<and>
   317     indep_sets (\<lambda>i. { X i -` A \<inter> space M | A. A \<in> sets (M' i)}) I"
   318 
   319 definition (in prob_space)
   320   "indep_var Ma A Mb B \<longleftrightarrow> indep_vars (case_bool Ma Mb) (case_bool A B) UNIV"
   321 
   322 lemma (in prob_space) indep_vars_def:
   323   "indep_vars M' X I \<longleftrightarrow>
   324     (\<forall>i\<in>I. random_variable (M' i) (X i)) \<and>
   325     indep_sets (\<lambda>i. sigma_sets (space M) { X i -` A \<inter> space M | A. A \<in> sets (M' i)}) I"
   326   unfolding indep_vars_def2
   327   apply (rule conj_cong[OF refl])
   328   apply (rule indep_sets_sigma_sets_iff[symmetric])
   329   apply (auto simp: Int_stable_def)
   330   apply (rule_tac x="A \<inter> Aa" in exI)
   331   apply auto
   332   done
   333 
   334 lemma (in prob_space) indep_var_eq:
   335   "indep_var S X T Y \<longleftrightarrow>
   336     (random_variable S X \<and> random_variable T Y) \<and>
   337     indep_set
   338       (sigma_sets (space M) { X -` A \<inter> space M | A. A \<in> sets S})
   339       (sigma_sets (space M) { Y -` A \<inter> space M | A. A \<in> sets T})"
   340   unfolding indep_var_def indep_vars_def indep_set_def UNIV_bool
   341   by (intro arg_cong2[where f="op \<and>"] arg_cong2[where f=indep_sets] ext)
   342      (auto split: bool.split)
   343 
   344 lemma (in prob_space) indep_sets2_eq:
   345   "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)"
   346   unfolding indep_set_def
   347 proof (intro iffI ballI conjI)
   348   assume indep: "indep_sets (case_bool A B) UNIV"
   349   { fix a b assume "a \<in> A" "b \<in> B"
   350     with indep_setsD[OF indep, of UNIV "case_bool a b"]
   351     show "prob (a \<inter> b) = prob a * prob b"
   352       unfolding UNIV_bool by (simp add: ac_simps) }
   353   from indep show "A \<subseteq> events" "B \<subseteq> events"
   354     unfolding indep_sets_def UNIV_bool by auto
   355 next
   356   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)"
   357   show "indep_sets (case_bool A B) UNIV"
   358   proof (rule indep_setsI)
   359     fix i show "(case i of True \<Rightarrow> A | False \<Rightarrow> B) \<subseteq> events"
   360       using * by (auto split: bool.split)
   361   next
   362     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)"
   363     then have "J = {True} \<or> J = {False} \<or> J = {True,False}"
   364       by (auto simp: UNIV_bool)
   365     then show "prob (\<Inter>j\<in>J. X j) = (\<Prod>j\<in>J. prob (X j))"
   366       using X * by auto
   367   qed
   368 qed
   369 
   370 lemma (in prob_space) indep_set_sigma_sets:
   371   assumes "indep_set A B"
   372   assumes A: "Int_stable A" and B: "Int_stable B"
   373   shows "indep_set (sigma_sets (space M) A) (sigma_sets (space M) B)"
   374 proof -
   375   have "indep_sets (\<lambda>i. sigma_sets (space M) (case i of True \<Rightarrow> A | False \<Rightarrow> B)) UNIV"
   376   proof (rule indep_sets_sigma)
   377     show "indep_sets (case_bool A B) UNIV"
   378       by (rule \<open>indep_set A B\<close>[unfolded indep_set_def])
   379     fix i show "Int_stable (case i of True \<Rightarrow> A | False \<Rightarrow> B)"
   380       using A B by (cases i) auto
   381   qed
   382   then show ?thesis
   383     unfolding indep_set_def
   384     by (rule indep_sets_mono_sets) (auto split: bool.split)
   385 qed
   386 
   387 lemma (in prob_space) indep_eventsI_indep_vars:
   388   assumes indep: "indep_vars N X I"
   389   assumes P: "\<And>i. i \<in> I \<Longrightarrow> {x\<in>space (N i). P i x} \<in> sets (N i)"
   390   shows "indep_events (\<lambda>i. {x\<in>space M. P i (X i x)}) I"
   391 proof -
   392   have "indep_sets (\<lambda>i. {X i -` A \<inter> space M |A. A \<in> sets (N i)}) I"
   393     using indep unfolding indep_vars_def2 by auto
   394   then show ?thesis
   395     unfolding indep_events_def_alt
   396   proof (rule indep_sets_mono_sets)
   397     fix i assume "i \<in> I"
   398     then have "{{x \<in> space M. P i (X i x)}} = {X i -` {x\<in>space (N i). P i x} \<inter> space M}"
   399       using indep by (auto simp: indep_vars_def dest: measurable_space)
   400     also have "\<dots> \<subseteq> {X i -` A \<inter> space M |A. A \<in> sets (N i)}"
   401       using P[OF \<open>i \<in> I\<close>] by blast
   402     finally show "{{x \<in> space M. P i (X i x)}} \<subseteq> {X i -` A \<inter> space M |A. A \<in> sets (N i)}" .
   403   qed
   404 qed                              
   405 
   406 lemma (in prob_space) indep_sets_collect_sigma:
   407   fixes I :: "'j \<Rightarrow> 'i set" and J :: "'j set" and E :: "'i \<Rightarrow> 'a set set"
   408   assumes indep: "indep_sets E (\<Union>j\<in>J. I j)"
   409   assumes Int_stable: "\<And>i j. j \<in> J \<Longrightarrow> i \<in> I j \<Longrightarrow> Int_stable (E i)"
   410   assumes disjoint: "disjoint_family_on I J"
   411   shows "indep_sets (\<lambda>j. sigma_sets (space M) (\<Union>i\<in>I j. E i)) J"
   412 proof -
   413   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) }"
   414 
   415   from indep have E: "\<And>j i. j \<in> J \<Longrightarrow> i \<in> I j \<Longrightarrow> E i \<subseteq> events"
   416     unfolding indep_sets_def by auto
   417   { fix j
   418     let ?S = "sigma_sets (space M) (\<Union>i\<in>I j. E i)"
   419     assume "j \<in> J"
   420     from E[OF this] interpret S: sigma_algebra "space M" ?S
   421       using sets.sets_into_space[of _ M] by (intro sigma_algebra_sigma_sets) auto
   422 
   423     have "sigma_sets (space M) (\<Union>i\<in>I j. E i) = sigma_sets (space M) (?E j)"
   424     proof (rule sigma_sets_eqI)
   425       fix A assume "A \<in> (\<Union>i\<in>I j. E i)"
   426       then guess i ..
   427       then show "A \<in> sigma_sets (space M) (?E j)"
   428         by (auto intro!: sigma_sets.intros(2-) exI[of _ "{i}"] exI[of _ "\<lambda>i. A"])
   429     next
   430       fix A assume "A \<in> ?E j"
   431       then obtain E' K where "finite K" "K \<noteq> {}" "K \<subseteq> I j" "\<And>k. k \<in> K \<Longrightarrow> E' k \<in> E k"
   432         and A: "A = (\<Inter>k\<in>K. E' k)"
   433         by auto
   434       then have "A \<in> ?S" unfolding A
   435         by (safe intro!: S.finite_INT) auto
   436       then show "A \<in> sigma_sets (space M) (\<Union>i\<in>I j. E i)"
   437         by simp
   438     qed }
   439   moreover have "indep_sets (\<lambda>j. sigma_sets (space M) (?E j)) J"
   440   proof (rule indep_sets_sigma)
   441     show "indep_sets ?E J"
   442     proof (intro indep_setsI)
   443       fix j assume "j \<in> J" with E show "?E j \<subseteq> events" by (force  intro!: sets.finite_INT)
   444     next
   445       fix K A assume K: "K \<noteq> {}" "K \<subseteq> J" "finite K"
   446         and "\<forall>j\<in>K. A j \<in> ?E j"
   447       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)"
   448         by simp
   449       from bchoice[OF this] guess E' ..
   450       from bchoice[OF this] obtain L
   451         where A: "\<And>j. j\<in>K \<Longrightarrow> A j = (\<Inter>l\<in>L j. E' j l)"
   452         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"
   453         and E': "\<And>j l. j\<in>K \<Longrightarrow> l \<in> L j \<Longrightarrow> E' j l \<in> E l"
   454         by auto
   455 
   456       { fix k l j assume "k \<in> K" "j \<in> K" "l \<in> L j" "l \<in> L k"
   457         have "k = j"
   458         proof (rule ccontr)
   459           assume "k \<noteq> j"
   460           with disjoint \<open>K \<subseteq> J\<close> \<open>k \<in> K\<close> \<open>j \<in> K\<close> have "I k \<inter> I j = {}"
   461             unfolding disjoint_family_on_def by auto
   462           with L(2,3)[OF \<open>j \<in> K\<close>] L(2,3)[OF \<open>k \<in> K\<close>]
   463           show False using \<open>l \<in> L k\<close> \<open>l \<in> L j\<close> by auto
   464         qed }
   465       note L_inj = this
   466 
   467       def k \<equiv> "\<lambda>l. (SOME k. k \<in> K \<and> l \<in> L k)"
   468       { fix x j l assume *: "j \<in> K" "l \<in> L j"
   469         have "k l = j" unfolding k_def
   470         proof (rule some_equality)
   471           fix k assume "k \<in> K \<and> l \<in> L k"
   472           with * L_inj show "k = j" by auto
   473         qed (insert *, simp) }
   474       note k_simp[simp] = this
   475       let ?E' = "\<lambda>l. E' (k l) l"
   476       have "prob (\<Inter>j\<in>K. A j) = prob (\<Inter>l\<in>(\<Union>k\<in>K. L k). ?E' l)"
   477         by (auto simp: A intro!: arg_cong[where f=prob])
   478       also have "\<dots> = (\<Prod>l\<in>(\<Union>k\<in>K. L k). prob (?E' l))"
   479         using L K E' by (intro indep_setsD[OF indep]) (simp_all add: UN_mono)
   480       also have "\<dots> = (\<Prod>j\<in>K. \<Prod>l\<in>L j. prob (E' j l))"
   481         using K L L_inj by (subst setprod.UNION_disjoint) auto
   482       also have "\<dots> = (\<Prod>j\<in>K. prob (A j))"
   483         using K L E' by (auto simp add: A intro!: setprod.cong indep_setsD[OF indep, symmetric]) blast
   484       finally show "prob (\<Inter>j\<in>K. A j) = (\<Prod>j\<in>K. prob (A j))" .
   485     qed
   486   next
   487     fix j assume "j \<in> J"
   488     show "Int_stable (?E j)"
   489     proof (rule Int_stableI)
   490       fix a assume "a \<in> ?E j" then obtain Ka Ea
   491         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
   492       fix b assume "b \<in> ?E j" then obtain Kb Eb
   493         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
   494       let ?f = "\<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 {})"
   495       have "Ka \<union> Kb = (Ka \<inter> Kb) \<union> (Kb - Ka) \<union> (Ka - Kb)"
   496         by blast
   497       moreover have "(\<Inter>x\<in>Ka \<inter> Kb. Ea x \<inter> Eb x) \<inter>
   498         (\<Inter>x\<in>Kb - Ka. Eb x) \<inter> (\<Inter>x\<in>Ka - Kb. Ea x) = (\<Inter>k\<in>Ka. Ea k) \<inter> (\<Inter>k\<in>Kb. Eb k)"
   499         by auto
   500       ultimately have "(\<Inter>k\<in>Ka \<union> Kb. ?f k) = (\<Inter>k\<in>Ka. Ea k) \<inter> (\<Inter>k\<in>Kb. Eb k)" (is "?lhs = ?rhs")
   501         by (simp only: image_Un Inter_Un_distrib) simp
   502       then have "a \<inter> b = (\<Inter>k\<in>Ka \<union> Kb. ?f k)"
   503         by (simp only: a(1) b(1))
   504       with a b \<open>j \<in> J\<close> Int_stableD[OF Int_stable] show "a \<inter> b \<in> ?E j"
   505         by (intro CollectI exI[of _ "Ka \<union> Kb"] exI[of _ ?f]) auto
   506     qed
   507   qed
   508   ultimately show ?thesis
   509     by (simp cong: indep_sets_cong)
   510 qed
   511 
   512 lemma (in prob_space) indep_vars_restrict:
   513   assumes ind: "indep_vars M' X I" and K: "\<And>j. j \<in> L \<Longrightarrow> K j \<subseteq> I" and J: "disjoint_family_on K L"
   514   shows "indep_vars (\<lambda>j. PiM (K j) M') (\<lambda>j \<omega>. restrict (\<lambda>i. X i \<omega>) (K j)) L"
   515   unfolding indep_vars_def
   516 proof safe
   517   fix j assume "j \<in> L" then show "random_variable (Pi\<^sub>M (K j) M') (\<lambda>\<omega>. \<lambda>i\<in>K j. X i \<omega>)"
   518     using K ind by (auto simp: indep_vars_def intro!: measurable_restrict)
   519 next
   520   have X: "\<And>i. i \<in> I \<Longrightarrow> X i \<in> measurable M (M' i)"
   521     using ind by (auto simp: indep_vars_def)
   522   let ?proj = "\<lambda>j S. {(\<lambda>\<omega>. \<lambda>i\<in>K j. X i \<omega>) -` A \<inter> space M |A. A \<in> S}"
   523   let ?UN = "\<lambda>j. sigma_sets (space M) (\<Union>i\<in>K j. { X i -` A \<inter> space M| A. A \<in> sets (M' i) })"
   524   show "indep_sets (\<lambda>i. sigma_sets (space M) (?proj i (sets (Pi\<^sub>M (K i) M')))) L"
   525   proof (rule indep_sets_mono_sets)
   526     fix j assume j: "j \<in> L"
   527     have "sigma_sets (space M) (?proj j (sets (Pi\<^sub>M (K j) M'))) = 
   528       sigma_sets (space M) (sigma_sets (space M) (?proj j (prod_algebra (K j) M')))"
   529       using j K X[THEN measurable_space] unfolding sets_PiM
   530       by (subst sigma_sets_vimage_commute) (auto simp add: Pi_iff)
   531     also have "\<dots> = sigma_sets (space M) (?proj j (prod_algebra (K j) M'))"
   532       by (rule sigma_sets_sigma_sets_eq) auto
   533     also have "\<dots> \<subseteq> ?UN j"
   534     proof (rule sigma_sets_mono, safe del: disjE elim!: prod_algebraE)
   535       fix J E assume J: "finite J" "J \<noteq> {} \<or> K j = {}"  "J \<subseteq> K j" and E: "\<forall>i. i \<in> J \<longrightarrow> E i \<in> sets (M' i)"
   536       show "(\<lambda>\<omega>. \<lambda>i\<in>K j. X i \<omega>) -` prod_emb (K j) M' J (Pi\<^sub>E J E) \<inter> space M \<in> ?UN j"
   537       proof cases
   538         assume "K j = {}" with J show ?thesis
   539           by (auto simp add: sigma_sets_empty_eq prod_emb_def)
   540       next
   541         assume "K j \<noteq> {}" with J have "J \<noteq> {}"
   542           by auto
   543         { interpret sigma_algebra "space M" "?UN j"
   544             by (rule sigma_algebra_sigma_sets) auto 
   545           have "\<And>A. (\<And>i. i \<in> J \<Longrightarrow> A i \<in> ?UN j) \<Longrightarrow> INTER J A \<in> ?UN j"
   546             using \<open>finite J\<close> \<open>J \<noteq> {}\<close> by (rule finite_INT) blast }
   547         note INT = this
   548 
   549         from \<open>J \<noteq> {}\<close> J K E[rule_format, THEN sets.sets_into_space] j
   550         have "(\<lambda>\<omega>. \<lambda>i\<in>K j. X i \<omega>) -` prod_emb (K j) M' J (Pi\<^sub>E J E) \<inter> space M
   551           = (\<Inter>i\<in>J. X i -` E i \<inter> space M)"
   552           apply (subst prod_emb_PiE[OF _ ])
   553           apply auto []
   554           apply auto []
   555           apply (auto simp add: Pi_iff intro!: X[THEN measurable_space])
   556           apply (erule_tac x=i in ballE)
   557           apply auto
   558           done
   559         also have "\<dots> \<in> ?UN j"
   560           apply (rule INT)
   561           apply (rule sigma_sets.Basic)
   562           using \<open>J \<subseteq> K j\<close> E
   563           apply auto
   564           done
   565         finally show ?thesis .
   566       qed
   567     qed
   568     finally show "sigma_sets (space M) (?proj j (sets (Pi\<^sub>M (K j) M'))) \<subseteq> ?UN j" .
   569   next
   570     show "indep_sets ?UN L"
   571     proof (rule indep_sets_collect_sigma)
   572       show "indep_sets (\<lambda>i. {X i -` A \<inter> space M |A. A \<in> sets (M' i)}) (\<Union>j\<in>L. K j)"
   573       proof (rule indep_sets_mono_index)
   574         show "indep_sets (\<lambda>i. {X i -` A \<inter> space M |A. A \<in> sets (M' i)}) I"
   575           using ind unfolding indep_vars_def2 by auto
   576         show "(\<Union>l\<in>L. K l) \<subseteq> I"
   577           using K by auto
   578       qed
   579     next
   580       fix l i assume "l \<in> L" "i \<in> K l"
   581       show "Int_stable {X i -` A \<inter> space M |A. A \<in> sets (M' i)}"
   582         apply (auto simp: Int_stable_def)
   583         apply (rule_tac x="A \<inter> Aa" in exI)
   584         apply auto
   585         done
   586     qed fact
   587   qed
   588 qed
   589 
   590 lemma (in prob_space) indep_var_restrict:
   591   assumes ind: "indep_vars M' X I" and AB: "A \<inter> B = {}" "A \<subseteq> I" "B \<subseteq> I"
   592   shows "indep_var (PiM A M') (\<lambda>\<omega>. restrict (\<lambda>i. X i \<omega>) A) (PiM B M') (\<lambda>\<omega>. restrict (\<lambda>i. X i \<omega>) B)"
   593 proof -
   594   have *:
   595     "case_bool (Pi\<^sub>M A M') (Pi\<^sub>M B M') = (\<lambda>b. PiM (case_bool A B b) M')"
   596     "case_bool (\<lambda>\<omega>. \<lambda>i\<in>A. X i \<omega>) (\<lambda>\<omega>. \<lambda>i\<in>B. X i \<omega>) = (\<lambda>b \<omega>. \<lambda>i\<in>case_bool A B b. X i \<omega>)"
   597     by (simp_all add: fun_eq_iff split: bool.split)
   598   show ?thesis
   599     unfolding indep_var_def * using AB
   600     by (intro indep_vars_restrict[OF ind]) (auto simp: disjoint_family_on_def split: bool.split)
   601 qed
   602 
   603 lemma (in prob_space) indep_vars_subset:
   604   assumes "indep_vars M' X I" "J \<subseteq> I"
   605   shows "indep_vars M' X J"
   606   using assms unfolding indep_vars_def indep_sets_def
   607   by auto
   608 
   609 lemma (in prob_space) indep_vars_cong:
   610   "I = J \<Longrightarrow> (\<And>i. i \<in> I \<Longrightarrow> X i = Y i) \<Longrightarrow> (\<And>i. i \<in> I \<Longrightarrow> M' i = N' i) \<Longrightarrow> indep_vars M' X I \<longleftrightarrow> indep_vars N' Y J"
   611   unfolding indep_vars_def2 by (intro conj_cong indep_sets_cong) auto
   612 
   613 definition (in prob_space) tail_events where
   614   "tail_events A = (\<Inter>n. sigma_sets (space M) (UNION {n..} A))"
   615 
   616 lemma (in prob_space) tail_events_sets:
   617   assumes A: "\<And>i::nat. A i \<subseteq> events"
   618   shows "tail_events A \<subseteq> events"
   619 proof
   620   fix X assume X: "X \<in> tail_events A"
   621   let ?A = "(\<Inter>n. sigma_sets (space M) (UNION {n..} A))"
   622   from X have "\<And>n::nat. X \<in> sigma_sets (space M) (UNION {n..} A)" by (auto simp: tail_events_def)
   623   from this[of 0] have "X \<in> sigma_sets (space M) (UNION UNIV A)" by simp
   624   then show "X \<in> events"
   625     by induct (insert A, auto)
   626 qed
   627 
   628 lemma (in prob_space) sigma_algebra_tail_events:
   629   assumes "\<And>i::nat. sigma_algebra (space M) (A i)"
   630   shows "sigma_algebra (space M) (tail_events A)"
   631   unfolding tail_events_def
   632 proof (simp add: sigma_algebra_iff2, safe)
   633   let ?A = "(\<Inter>n. sigma_sets (space M) (UNION {n..} A))"
   634   interpret A: sigma_algebra "space M" "A i" for i by fact
   635   { fix X x assume "X \<in> ?A" "x \<in> X"
   636     then have "\<And>n. X \<in> sigma_sets (space M) (UNION {n..} A)" by auto
   637     from this[of 0] have "X \<in> sigma_sets (space M) (UNION UNIV A)" by simp
   638     then have "X \<subseteq> space M"
   639       by induct (insert A.sets_into_space, auto)
   640     with \<open>x \<in> X\<close> show "x \<in> space M" by auto }
   641   { fix F :: "nat \<Rightarrow> 'a set" and n assume "range F \<subseteq> ?A"
   642     then show "(UNION UNIV F) \<in> sigma_sets (space M) (UNION {n..} A)"
   643       by (intro sigma_sets.Union) auto }
   644 qed (auto intro!: sigma_sets.Compl sigma_sets.Empty)
   645 
   646 lemma (in prob_space) kolmogorov_0_1_law:
   647   fixes A :: "nat \<Rightarrow> 'a set set"
   648   assumes "\<And>i::nat. sigma_algebra (space M) (A i)"
   649   assumes indep: "indep_sets A UNIV"
   650   and X: "X \<in> tail_events A"
   651   shows "prob X = 0 \<or> prob X = 1"
   652 proof -
   653   have A: "\<And>i. A i \<subseteq> events"
   654     using indep unfolding indep_sets_def by simp
   655 
   656   let ?D = "{D \<in> events. prob (X \<inter> D) = prob X * prob D}"
   657   interpret A: sigma_algebra "space M" "A i" for i by fact
   658   interpret T: sigma_algebra "space M" "tail_events A"
   659     by (rule sigma_algebra_tail_events) fact
   660   have "X \<subseteq> space M" using T.space_closed X by auto
   661 
   662   have X_in: "X \<in> events"
   663     using tail_events_sets A X by auto
   664 
   665   interpret D: dynkin_system "space M" ?D
   666   proof (rule dynkin_systemI)
   667     fix D assume "D \<in> ?D" then show "D \<subseteq> space M"
   668       using sets.sets_into_space by auto
   669   next
   670     show "space M \<in> ?D"
   671       using prob_space \<open>X \<subseteq> space M\<close> by (simp add: Int_absorb2)
   672   next
   673     fix A assume A: "A \<in> ?D"
   674     have "prob (X \<inter> (space M - A)) = prob (X - (X \<inter> A))"
   675       using \<open>X \<subseteq> space M\<close> by (auto intro!: arg_cong[where f=prob])
   676     also have "\<dots> = prob X - prob (X \<inter> A)"
   677       using X_in A by (intro finite_measure_Diff) auto
   678     also have "\<dots> = prob X * prob (space M) - prob X * prob A"
   679       using A prob_space by auto
   680     also have "\<dots> = prob X * prob (space M - A)"
   681       using X_in A sets.sets_into_space
   682       by (subst finite_measure_Diff) (auto simp: field_simps)
   683     finally show "space M - A \<in> ?D"
   684       using A \<open>X \<subseteq> space M\<close> by auto
   685   next
   686     fix F :: "nat \<Rightarrow> 'a set" assume dis: "disjoint_family F" and "range F \<subseteq> ?D"
   687     then have F: "range F \<subseteq> events" "\<And>i. prob (X \<inter> F i) = prob X * prob (F i)"
   688       by auto
   689     have "(\<lambda>i. prob (X \<inter> F i)) sums prob (\<Union>i. X \<inter> F i)"
   690     proof (rule finite_measure_UNION)
   691       show "range (\<lambda>i. X \<inter> F i) \<subseteq> events"
   692         using F X_in by auto
   693       show "disjoint_family (\<lambda>i. X \<inter> F i)"
   694         using dis by (rule disjoint_family_on_bisimulation) auto
   695     qed
   696     with F have "(\<lambda>i. prob X * prob (F i)) sums prob (X \<inter> (\<Union>i. F i))"
   697       by simp
   698     moreover have "(\<lambda>i. prob X * prob (F i)) sums (prob X * prob (\<Union>i. F i))"
   699       by (intro sums_mult finite_measure_UNION F dis)
   700     ultimately have "prob (X \<inter> (\<Union>i. F i)) = prob X * prob (\<Union>i. F i)"
   701       by (auto dest!: sums_unique)
   702     with F show "(\<Union>i. F i) \<in> ?D"
   703       by auto
   704   qed
   705 
   706   { fix n
   707     have "indep_sets (\<lambda>b. sigma_sets (space M) (\<Union>m\<in>case_bool {..n} {Suc n..} b. A m)) UNIV"
   708     proof (rule indep_sets_collect_sigma)
   709       have *: "(\<Union>b. case b of True \<Rightarrow> {..n} | False \<Rightarrow> {Suc n..}) = UNIV" (is "?U = _")
   710         by (simp split: bool.split add: set_eq_iff) (metis not_less_eq_eq)
   711       with indep show "indep_sets A ?U" by simp
   712       show "disjoint_family (case_bool {..n} {Suc n..})"
   713         unfolding disjoint_family_on_def by (auto split: bool.split)
   714       fix m
   715       show "Int_stable (A m)"
   716         unfolding Int_stable_def using A.Int by auto
   717     qed
   718     also have "(\<lambda>b. sigma_sets (space M) (\<Union>m\<in>case_bool {..n} {Suc n..} b. A m)) =
   719       case_bool (sigma_sets (space M) (\<Union>m\<in>{..n}. A m)) (sigma_sets (space M) (\<Union>m\<in>{Suc n..}. A m))"
   720       by (auto intro!: ext split: bool.split)
   721     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))"
   722       unfolding indep_set_def by simp
   723 
   724     have "sigma_sets (space M) (\<Union>m\<in>{..n}. A m) \<subseteq> ?D"
   725     proof (simp add: subset_eq, rule)
   726       fix D assume D: "D \<in> sigma_sets (space M) (\<Union>m\<in>{..n}. A m)"
   727       have "X \<in> sigma_sets (space M) (\<Union>m\<in>{Suc n..}. A m)"
   728         using X unfolding tail_events_def by simp
   729       from indep_setD[OF indep D this] indep_setD_ev1[OF indep] D
   730       show "D \<in> events \<and> prob (X \<inter> D) = prob X * prob D"
   731         by (auto simp add: ac_simps)
   732     qed }
   733   then have "(\<Union>n. sigma_sets (space M) (\<Union>m\<in>{..n}. A m)) \<subseteq> ?D" (is "?A \<subseteq> _")
   734     by auto
   735 
   736   note \<open>X \<in> tail_events A\<close>
   737   also {
   738     have "\<And>n. sigma_sets (space M) (\<Union>i\<in>{n..}. A i) \<subseteq> sigma_sets (space M) ?A"
   739       by (intro sigma_sets_subseteq UN_mono) auto
   740    then have "tail_events A \<subseteq> sigma_sets (space M) ?A"
   741       unfolding tail_events_def by auto }
   742   also have "sigma_sets (space M) ?A = dynkin (space M) ?A"
   743   proof (rule sigma_eq_dynkin)
   744     { fix B n assume "B \<in> sigma_sets (space M) (\<Union>m\<in>{..n}. A m)"
   745       then have "B \<subseteq> space M"
   746         by induct (insert A sets.sets_into_space[of _ M], auto) }
   747     then show "?A \<subseteq> Pow (space M)" by auto
   748     show "Int_stable ?A"
   749     proof (rule Int_stableI)
   750       fix a assume "a \<in> ?A" then guess n .. note a = this
   751       fix b assume "b \<in> ?A" then guess m .. note b = this
   752       interpret Amn: sigma_algebra "space M" "sigma_sets (space M) (\<Union>i\<in>{..max m n}. A i)"
   753         using A sets.sets_into_space[of _ M] by (intro sigma_algebra_sigma_sets) auto
   754       have "sigma_sets (space M) (\<Union>i\<in>{..n}. A i) \<subseteq> sigma_sets (space M) (\<Union>i\<in>{..max m n}. A i)"
   755         by (intro sigma_sets_subseteq UN_mono) auto
   756       with a have "a \<in> sigma_sets (space M) (\<Union>i\<in>{..max m n}. A i)" by auto
   757       moreover
   758       have "sigma_sets (space M) (\<Union>i\<in>{..m}. A i) \<subseteq> sigma_sets (space M) (\<Union>i\<in>{..max m n}. A i)"
   759         by (intro sigma_sets_subseteq UN_mono) auto
   760       with b have "b \<in> sigma_sets (space M) (\<Union>i\<in>{..max m n}. A i)" by auto
   761       ultimately have "a \<inter> b \<in> sigma_sets (space M) (\<Union>i\<in>{..max m n}. A i)"
   762         using Amn.Int[of a b] by simp
   763       then show "a \<inter> b \<in> (\<Union>n. sigma_sets (space M) (\<Union>i\<in>{..n}. A i))" by auto
   764     qed
   765   qed
   766   also have "dynkin (space M) ?A \<subseteq> ?D"
   767     using \<open>?A \<subseteq> ?D\<close> by (auto intro!: D.dynkin_subset)
   768   finally show ?thesis by auto
   769 qed
   770 
   771 lemma (in prob_space) borel_0_1_law:
   772   fixes F :: "nat \<Rightarrow> 'a set"
   773   assumes F2: "indep_events F UNIV"
   774   shows "prob (\<Inter>n. \<Union>m\<in>{n..}. F m) = 0 \<or> prob (\<Inter>n. \<Union>m\<in>{n..}. F m) = 1"
   775 proof (rule kolmogorov_0_1_law[of "\<lambda>i. sigma_sets (space M) { F i }"])
   776   have F1: "range F \<subseteq> events"
   777     using F2 by (simp add: indep_events_def subset_eq)
   778   { fix i show "sigma_algebra (space M) (sigma_sets (space M) {F i})"
   779       using sigma_algebra_sigma_sets[of "{F i}" "space M"] F1 sets.sets_into_space
   780       by auto }
   781   show "indep_sets (\<lambda>i. sigma_sets (space M) {F i}) UNIV"
   782   proof (rule indep_sets_sigma)
   783     show "indep_sets (\<lambda>i. {F i}) UNIV"
   784       unfolding indep_events_def_alt[symmetric] by fact
   785     fix i show "Int_stable {F i}"
   786       unfolding Int_stable_def by simp
   787   qed
   788   let ?Q = "\<lambda>n. \<Union>i\<in>{n..}. F i"
   789   show "(\<Inter>n. \<Union>m\<in>{n..}. F m) \<in> tail_events (\<lambda>i. sigma_sets (space M) {F i})"
   790     unfolding tail_events_def
   791   proof
   792     fix j
   793     interpret S: sigma_algebra "space M" "sigma_sets (space M) (\<Union>i\<in>{j..}. sigma_sets (space M) {F i})"
   794       using order_trans[OF F1 sets.space_closed]
   795       by (intro sigma_algebra_sigma_sets) (simp add: sigma_sets_singleton subset_eq)
   796     have "(\<Inter>n. ?Q n) = (\<Inter>n\<in>{j..}. ?Q n)"
   797       by (intro decseq_SucI INT_decseq_offset UN_mono) auto
   798     also have "\<dots> \<in> sigma_sets (space M) (\<Union>i\<in>{j..}. sigma_sets (space M) {F i})"
   799       using order_trans[OF F1 sets.space_closed]
   800       by (safe intro!: S.countable_INT S.countable_UN)
   801          (auto simp: sigma_sets_singleton intro!: sigma_sets.Basic bexI)
   802     finally show "(\<Inter>n. ?Q n) \<in> sigma_sets (space M) (\<Union>i\<in>{j..}. sigma_sets (space M) {F i})"
   803       by simp
   804   qed
   805 qed
   806 
   807 lemma (in prob_space) borel_0_1_law_AE:
   808   fixes P :: "nat \<Rightarrow> 'a \<Rightarrow> bool"
   809   assumes "indep_events (\<lambda>m. {x\<in>space M. P m x}) UNIV" (is "indep_events ?P _")
   810   shows "(AE x in M. infinite {m. P m x}) \<or> (AE x in M. finite {m. P m x})"
   811 proof -
   812   have [measurable]: "\<And>m. {x\<in>space M. P m x} \<in> sets M"
   813     using assms by (auto simp: indep_events_def)
   814   have *: "(\<Inter>n. \<Union>m\<in>{n..}. {x \<in> space M. P m x}) \<in> events"
   815     by simp
   816   from assms have "prob (\<Inter>n. \<Union>m\<in>{n..}. ?P m) = 0 \<or> prob (\<Inter>n. \<Union>m\<in>{n..}. ?P m) = 1"
   817     by (rule borel_0_1_law)
   818   also have "prob (\<Inter>n. \<Union>m\<in>{n..}. ?P m) = 1 \<longleftrightarrow> (AE x in M. infinite {m. P m x})"
   819     using * by (simp add: prob_eq_1)
   820       (simp add: Bex_def infinite_nat_iff_unbounded_le)
   821   also have "prob (\<Inter>n. \<Union>m\<in>{n..}. ?P m) = 0 \<longleftrightarrow> (AE x in M. finite {m. P m x})"
   822     using * by (simp add: prob_eq_0)
   823       (auto simp add: Ball_def finite_nat_iff_bounded not_less [symmetric])
   824   finally show ?thesis
   825     by blast
   826 qed
   827 
   828 lemma (in prob_space) indep_sets_finite:
   829   assumes I: "I \<noteq> {}" "finite I"
   830     and F: "\<And>i. i \<in> I \<Longrightarrow> F i \<subseteq> events" "\<And>i. i \<in> I \<Longrightarrow> space M \<in> F i"
   831   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)))"
   832 proof
   833   assume *: "indep_sets F I"
   834   from I show "\<forall>A\<in>Pi I F. prob (\<Inter>j\<in>I. A j) = (\<Prod>j\<in>I. prob (A j))"
   835     by (intro indep_setsD[OF *] ballI) auto
   836 next
   837   assume indep: "\<forall>A\<in>Pi I F. prob (\<Inter>j\<in>I. A j) = (\<Prod>j\<in>I. prob (A j))"
   838   show "indep_sets F I"
   839   proof (rule indep_setsI[OF F(1)])
   840     fix A J assume J: "J \<noteq> {}" "J \<subseteq> I" "finite J"
   841     assume A: "\<forall>j\<in>J. A j \<in> F j"
   842     let ?A = "\<lambda>j. if j \<in> J then A j else space M"
   843     have "prob (\<Inter>j\<in>I. ?A j) = prob (\<Inter>j\<in>J. A j)"
   844       using subset_trans[OF F(1) sets.space_closed] J A
   845       by (auto intro!: arg_cong[where f=prob] split: split_if_asm) blast
   846     also
   847     from A F have "(\<lambda>j. if j \<in> J then A j else space M) \<in> Pi I F" (is "?A \<in> _")
   848       by (auto split: split_if_asm)
   849     with indep have "prob (\<Inter>j\<in>I. ?A j) = (\<Prod>j\<in>I. prob (?A j))"
   850       by auto
   851     also have "\<dots> = (\<Prod>j\<in>J. prob (A j))"
   852       unfolding if_distrib setprod.If_cases[OF \<open>finite I\<close>]
   853       using prob_space \<open>J \<subseteq> I\<close> by (simp add: Int_absorb1 setprod.neutral_const)
   854     finally show "prob (\<Inter>j\<in>J. A j) = (\<Prod>j\<in>J. prob (A j))" ..
   855   qed
   856 qed
   857 
   858 lemma (in prob_space) indep_vars_finite:
   859   fixes I :: "'i set"
   860   assumes I: "I \<noteq> {}" "finite I"
   861     and M': "\<And>i. i \<in> I \<Longrightarrow> sets (M' i) = sigma_sets (space (M' i)) (E i)"
   862     and rv: "\<And>i. i \<in> I \<Longrightarrow> random_variable (M' i) (X i)"
   863     and Int_stable: "\<And>i. i \<in> I \<Longrightarrow> Int_stable (E i)"
   864     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))"
   865   shows "indep_vars M' X I \<longleftrightarrow>
   866     (\<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)))"
   867 proof -
   868   from rv have X: "\<And>i. i \<in> I \<Longrightarrow> X i \<in> space M \<rightarrow> space (M' i)"
   869     unfolding measurable_def by simp
   870 
   871   { fix i assume "i\<in>I"
   872     from closed[OF \<open>i \<in> I\<close>]
   873     have "sigma_sets (space M) {X i -` A \<inter> space M |A. A \<in> sets (M' i)}
   874       = sigma_sets (space M) {X i -` A \<inter> space M |A. A \<in> E i}"
   875       unfolding sigma_sets_vimage_commute[OF X, OF \<open>i \<in> I\<close>, symmetric] M'[OF \<open>i \<in> I\<close>]
   876       by (subst sigma_sets_sigma_sets_eq) auto }
   877   note sigma_sets_X = this
   878 
   879   { fix i assume "i\<in>I"
   880     have "Int_stable {X i -` A \<inter> space M |A. A \<in> E i}"
   881     proof (rule Int_stableI)
   882       fix a assume "a \<in> {X i -` A \<inter> space M |A. A \<in> E i}"
   883       then obtain A where "a = X i -` A \<inter> space M" "A \<in> E i" by auto
   884       moreover
   885       fix b assume "b \<in> {X i -` A \<inter> space M |A. A \<in> E i}"
   886       then obtain B where "b = X i -` B \<inter> space M" "B \<in> E i" by auto
   887       moreover
   888       have "(X i -` A \<inter> space M) \<inter> (X i -` B \<inter> space M) = X i -` (A \<inter> B) \<inter> space M" by auto
   889       moreover note Int_stable[OF \<open>i \<in> I\<close>]
   890       ultimately
   891       show "a \<inter> b \<in> {X i -` A \<inter> space M |A. A \<in> E i}"
   892         by (auto simp del: vimage_Int intro!: exI[of _ "A \<inter> B"] dest: Int_stableD)
   893     qed }
   894   note indep_sets_X = indep_sets_sigma_sets_iff[OF this]
   895 
   896   { fix i assume "i \<in> I"
   897     { fix A assume "A \<in> E i"
   898       with M'[OF \<open>i \<in> I\<close>] have "A \<in> sets (M' i)" by auto
   899       moreover
   900       from rv[OF \<open>i\<in>I\<close>] have "X i \<in> measurable M (M' i)" by auto
   901       ultimately
   902       have "X i -` A \<inter> space M \<in> sets M" by (auto intro: measurable_sets) }
   903     with X[OF \<open>i\<in>I\<close>] space[OF \<open>i\<in>I\<close>]
   904     have "{X i -` A \<inter> space M |A. A \<in> E i} \<subseteq> events"
   905       "space M \<in> {X i -` A \<inter> space M |A. A \<in> E i}"
   906       by (auto intro!: exI[of _ "space (M' i)"]) }
   907   note indep_sets_finite_X = indep_sets_finite[OF I this]
   908 
   909   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))) =
   910     (\<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)))"
   911     (is "?L = ?R")
   912   proof safe
   913     fix A assume ?L and A: "A \<in> (\<Pi> i\<in>I. E i)"
   914     from \<open>?L\<close>[THEN bspec, of "\<lambda>i. X i -` A i \<inter> space M"] A \<open>I \<noteq> {}\<close>
   915     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))"
   916       by (auto simp add: Pi_iff)
   917   next
   918     fix A assume ?R and A: "A \<in> (\<Pi> i\<in>I. {X i -` A \<inter> space M |A. A \<in> E i})"
   919     from A have "\<forall>i\<in>I. \<exists>B. A i = X i -` B \<inter> space M \<and> B \<in> E i" by auto
   920     from bchoice[OF this] obtain B where B: "\<forall>i\<in>I. A i = X i -` B i \<inter> space M"
   921       "B \<in> (\<Pi> i\<in>I. E i)" by auto
   922     from \<open>?R\<close>[THEN bspec, OF B(2)] B(1) \<open>I \<noteq> {}\<close>
   923     show "prob (INTER I A) = (\<Prod>j\<in>I. prob (A j))"
   924       by simp
   925   qed
   926   then show ?thesis using \<open>I \<noteq> {}\<close>
   927     by (simp add: rv indep_vars_def indep_sets_X sigma_sets_X indep_sets_finite_X cong: indep_sets_cong)
   928 qed
   929 
   930 lemma (in prob_space) indep_vars_compose:
   931   assumes "indep_vars M' X I"
   932   assumes rv: "\<And>i. i \<in> I \<Longrightarrow> Y i \<in> measurable (M' i) (N i)"
   933   shows "indep_vars N (\<lambda>i. Y i \<circ> X i) I"
   934   unfolding indep_vars_def
   935 proof
   936   from rv \<open>indep_vars M' X I\<close>
   937   show "\<forall>i\<in>I. random_variable (N i) (Y i \<circ> X i)"
   938     by (auto simp: indep_vars_def)
   939 
   940   have "indep_sets (\<lambda>i. sigma_sets (space M) {X i -` A \<inter> space M |A. A \<in> sets (M' i)}) I"
   941     using \<open>indep_vars M' X I\<close> by (simp add: indep_vars_def)
   942   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"
   943   proof (rule indep_sets_mono_sets)
   944     fix i assume "i \<in> I"
   945     with \<open>indep_vars M' X I\<close> have X: "X i \<in> space M \<rightarrow> space (M' i)"
   946       unfolding indep_vars_def measurable_def by auto
   947     { fix A assume "A \<in> sets (N i)"
   948       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)"
   949         by (intro exI[of _ "Y i -` A \<inter> space (M' i)"])
   950            (auto simp: vimage_comp intro!: measurable_sets rv \<open>i \<in> I\<close> funcset_mem[OF X]) }
   951     then show "sigma_sets (space M) {(Y i \<circ> X i) -` A \<inter> space M |A. A \<in> sets (N i)} \<subseteq>
   952       sigma_sets (space M) {X i -` A \<inter> space M |A. A \<in> sets (M' i)}"
   953       by (intro sigma_sets_subseteq) (auto simp: vimage_comp)
   954   qed
   955 qed
   956 
   957 lemma (in prob_space) indep_vars_compose2:
   958   assumes "indep_vars M' X I"
   959   assumes rv: "\<And>i. i \<in> I \<Longrightarrow> Y i \<in> measurable (M' i) (N i)"
   960   shows "indep_vars N (\<lambda>i x. Y i (X i x)) I"
   961   using indep_vars_compose [OF assms] by (simp add: comp_def)
   962 
   963 lemma (in prob_space) indep_var_compose:
   964   assumes "indep_var M1 X1 M2 X2" "Y1 \<in> measurable M1 N1" "Y2 \<in> measurable M2 N2"
   965   shows "indep_var N1 (Y1 \<circ> X1) N2 (Y2 \<circ> X2)"
   966 proof -
   967   have "indep_vars (case_bool N1 N2) (\<lambda>b. case_bool Y1 Y2 b \<circ> case_bool X1 X2 b) UNIV"
   968     using assms
   969     by (intro indep_vars_compose[where M'="case_bool M1 M2"])
   970        (auto simp: indep_var_def split: bool.split)
   971   also have "(\<lambda>b. case_bool Y1 Y2 b \<circ> case_bool X1 X2 b) = case_bool (Y1 \<circ> X1) (Y2 \<circ> X2)"
   972     by (simp add: fun_eq_iff split: bool.split)
   973   finally show ?thesis
   974     unfolding indep_var_def .
   975 qed
   976 
   977 lemma (in prob_space) indep_vars_Min:
   978   fixes X :: "'i \<Rightarrow> 'a \<Rightarrow> real"
   979   assumes I: "finite I" "i \<notin> I" and indep: "indep_vars (\<lambda>_. borel) X (insert i I)"
   980   shows "indep_var borel (X i) borel (\<lambda>\<omega>. Min ((\<lambda>i. X i \<omega>)`I))"
   981 proof -
   982   have "indep_var
   983     borel ((\<lambda>f. f i) \<circ> (\<lambda>\<omega>. restrict (\<lambda>i. X i \<omega>) {i}))
   984     borel ((\<lambda>f. Min (f`I)) \<circ> (\<lambda>\<omega>. restrict (\<lambda>i. X i \<omega>) I))"
   985     using I by (intro indep_var_compose[OF indep_var_restrict[OF indep]] borel_measurable_Min) auto
   986   also have "((\<lambda>f. f i) \<circ> (\<lambda>\<omega>. restrict (\<lambda>i. X i \<omega>) {i})) = X i"
   987     by auto
   988   also have "((\<lambda>f. Min (f`I)) \<circ> (\<lambda>\<omega>. restrict (\<lambda>i. X i \<omega>) I)) = (\<lambda>\<omega>. Min ((\<lambda>i. X i \<omega>)`I))"
   989     by (auto cong: rev_conj_cong)
   990   finally show ?thesis
   991     unfolding indep_var_def .
   992 qed
   993 
   994 lemma (in prob_space) indep_vars_setsum:
   995   fixes X :: "'i \<Rightarrow> 'a \<Rightarrow> real"
   996   assumes I: "finite I" "i \<notin> I" and indep: "indep_vars (\<lambda>_. borel) X (insert i I)"
   997   shows "indep_var borel (X i) borel (\<lambda>\<omega>. \<Sum>i\<in>I. X i \<omega>)"
   998 proof -
   999   have "indep_var 
  1000     borel ((\<lambda>f. f i) \<circ> (\<lambda>\<omega>. restrict (\<lambda>i. X i \<omega>) {i}))
  1001     borel ((\<lambda>f. \<Sum>i\<in>I. f i) \<circ> (\<lambda>\<omega>. restrict (\<lambda>i. X i \<omega>) I))"
  1002     using I by (intro indep_var_compose[OF indep_var_restrict[OF indep]] ) auto
  1003   also have "((\<lambda>f. f i) \<circ> (\<lambda>\<omega>. restrict (\<lambda>i. X i \<omega>) {i})) = X i"
  1004     by auto
  1005   also have "((\<lambda>f. \<Sum>i\<in>I. f i) \<circ> (\<lambda>\<omega>. restrict (\<lambda>i. X i \<omega>) I)) = (\<lambda>\<omega>. \<Sum>i\<in>I. X i \<omega>)"
  1006     by (auto cong: rev_conj_cong)
  1007   finally show ?thesis .
  1008 qed
  1009 
  1010 lemma (in prob_space) indep_vars_setprod:
  1011   fixes X :: "'i \<Rightarrow> 'a \<Rightarrow> real"
  1012   assumes I: "finite I" "i \<notin> I" and indep: "indep_vars (\<lambda>_. borel) X (insert i I)"
  1013   shows "indep_var borel (X i) borel (\<lambda>\<omega>. \<Prod>i\<in>I. X i \<omega>)"
  1014 proof -
  1015   have "indep_var 
  1016     borel ((\<lambda>f. f i) \<circ> (\<lambda>\<omega>. restrict (\<lambda>i. X i \<omega>) {i}))
  1017     borel ((\<lambda>f. \<Prod>i\<in>I. f i) \<circ> (\<lambda>\<omega>. restrict (\<lambda>i. X i \<omega>) I))"
  1018     using I by (intro indep_var_compose[OF indep_var_restrict[OF indep]] ) auto
  1019   also have "((\<lambda>f. f i) \<circ> (\<lambda>\<omega>. restrict (\<lambda>i. X i \<omega>) {i})) = X i"
  1020     by auto
  1021   also have "((\<lambda>f. \<Prod>i\<in>I. f i) \<circ> (\<lambda>\<omega>. restrict (\<lambda>i. X i \<omega>) I)) = (\<lambda>\<omega>. \<Prod>i\<in>I. X i \<omega>)"
  1022     by (auto cong: rev_conj_cong)
  1023   finally show ?thesis .
  1024 qed
  1025 
  1026 lemma (in prob_space) indep_varsD_finite:
  1027   assumes X: "indep_vars M' X I"
  1028   assumes I: "I \<noteq> {}" "finite I" "\<And>i. i \<in> I \<Longrightarrow> A i \<in> sets (M' i)"
  1029   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))"
  1030 proof (rule indep_setsD)
  1031   show "indep_sets (\<lambda>i. sigma_sets (space M) {X i -` A \<inter> space M |A. A \<in> sets (M' i)}) I"
  1032     using X by (auto simp: indep_vars_def)
  1033   show "I \<subseteq> I" "I \<noteq> {}" "finite I" using I by auto
  1034   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)}"
  1035     using I by auto
  1036 qed
  1037 
  1038 lemma (in prob_space) indep_varsD:
  1039   assumes X: "indep_vars M' X I"
  1040   assumes I: "J \<noteq> {}" "finite J" "J \<subseteq> I" "\<And>i. i \<in> J \<Longrightarrow> A i \<in> sets (M' i)"
  1041   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))"
  1042 proof (rule indep_setsD)
  1043   show "indep_sets (\<lambda>i. sigma_sets (space M) {X i -` A \<inter> space M |A. A \<in> sets (M' i)}) I"
  1044     using X by (auto simp: indep_vars_def)
  1045   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)}"
  1046     using I by auto
  1047 qed fact+
  1048 
  1049 lemma (in prob_space) indep_vars_iff_distr_eq_PiM:
  1050   fixes I :: "'i set" and X :: "'i \<Rightarrow> 'a \<Rightarrow> 'b"
  1051   assumes "I \<noteq> {}"
  1052   assumes rv: "\<And>i. random_variable (M' i) (X i)"
  1053   shows "indep_vars M' X I \<longleftrightarrow>
  1054     distr M (\<Pi>\<^sub>M i\<in>I. M' i) (\<lambda>x. \<lambda>i\<in>I. X i x) = (\<Pi>\<^sub>M i\<in>I. distr M (M' i) (X i))"
  1055 proof -
  1056   let ?P = "\<Pi>\<^sub>M i\<in>I. M' i"
  1057   let ?X = "\<lambda>x. \<lambda>i\<in>I. X i x"
  1058   let ?D = "distr M ?P ?X"
  1059   have X: "random_variable ?P ?X" by (intro measurable_restrict rv)
  1060   interpret D: prob_space ?D by (intro prob_space_distr X)
  1061 
  1062   let ?D' = "\<lambda>i. distr M (M' i) (X i)"
  1063   let ?P' = "\<Pi>\<^sub>M i\<in>I. distr M (M' i) (X i)"
  1064   interpret D': prob_space "?D' i" for i by (intro prob_space_distr rv)
  1065   interpret P: product_prob_space ?D' I ..
  1066     
  1067   show ?thesis
  1068   proof
  1069     assume "indep_vars M' X I"
  1070     show "?D = ?P'"
  1071     proof (rule measure_eqI_generator_eq)
  1072       show "Int_stable (prod_algebra I M')"
  1073         by (rule Int_stable_prod_algebra)
  1074       show "prod_algebra I M' \<subseteq> Pow (space ?P)"
  1075         using prod_algebra_sets_into_space by (simp add: space_PiM)
  1076       show "sets ?D = sigma_sets (space ?P) (prod_algebra I M')"
  1077         by (simp add: sets_PiM space_PiM)
  1078       show "sets ?P' = sigma_sets (space ?P) (prod_algebra I M')"
  1079         by (simp add: sets_PiM space_PiM cong: prod_algebra_cong)
  1080       let ?A = "\<lambda>i. \<Pi>\<^sub>E i\<in>I. space (M' i)"
  1081       show "range ?A \<subseteq> prod_algebra I M'" "(\<Union>i. ?A i) = space (Pi\<^sub>M I M')"
  1082         by (auto simp: space_PiM intro!: space_in_prod_algebra cong: prod_algebra_cong)
  1083       { fix i show "emeasure ?D (\<Pi>\<^sub>E i\<in>I. space (M' i)) \<noteq> \<infinity>" by auto }
  1084     next
  1085       fix E assume E: "E \<in> prod_algebra I M'"
  1086       from prod_algebraE[OF E] guess J Y . note J = this
  1087 
  1088       from E have "E \<in> sets ?P" by (auto simp: sets_PiM)
  1089       then have "emeasure ?D E = emeasure M (?X -` E \<inter> space M)"
  1090         by (simp add: emeasure_distr X)
  1091       also have "?X -` E \<inter> space M = (\<Inter>i\<in>J. X i -` Y i \<inter> space M)"
  1092         using J \<open>I \<noteq> {}\<close> measurable_space[OF rv] by (auto simp: prod_emb_def PiE_iff split: split_if_asm)
  1093       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))"
  1094         using \<open>indep_vars M' X I\<close> J \<open>I \<noteq> {}\<close> using indep_varsD[of M' X I J]
  1095         by (auto simp: emeasure_eq_measure setprod_ereal)
  1096       also have "\<dots> = (\<Prod> i\<in>J. emeasure (?D' i) (Y i))"
  1097         using rv J by (simp add: emeasure_distr)
  1098       also have "\<dots> = emeasure ?P' E"
  1099         using P.emeasure_PiM_emb[of J Y] J by (simp add: prod_emb_def)
  1100       finally show "emeasure ?D E = emeasure ?P' E" .
  1101     qed
  1102   next
  1103     assume "?D = ?P'"
  1104     show "indep_vars M' X I" unfolding indep_vars_def
  1105     proof (intro conjI indep_setsI ballI rv)
  1106       fix i show "sigma_sets (space M) {X i -` A \<inter> space M |A. A \<in> sets (M' i)} \<subseteq> events"
  1107         by (auto intro!: sets.sigma_sets_subset measurable_sets rv)
  1108     next
  1109       fix J Y' assume J: "J \<noteq> {}" "J \<subseteq> I" "finite J"
  1110       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)}"
  1111       have "\<forall>j\<in>J. \<exists>Y. Y' j = X j -` Y \<inter> space M \<and> Y \<in> sets (M' j)"
  1112       proof
  1113         fix j assume "j \<in> J"
  1114         from Y'[rule_format, OF this] rv[of j]
  1115         show "\<exists>Y. Y' j = X j -` Y \<inter> space M \<and> Y \<in> sets (M' j)"
  1116           by (subst (asm) sigma_sets_vimage_commute[symmetric, of _ _ "space (M' j)"])
  1117              (auto dest: measurable_space simp: sets.sigma_sets_eq)
  1118       qed
  1119       from bchoice[OF this] obtain Y where
  1120         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
  1121       let ?E = "prod_emb I M' J (Pi\<^sub>E J Y)"
  1122       from Y have "(\<Inter>j\<in>J. Y' j) = ?X -` ?E \<inter> space M"
  1123         using J \<open>I \<noteq> {}\<close> measurable_space[OF rv] by (auto simp: prod_emb_def PiE_iff split: split_if_asm)
  1124       then have "emeasure M (\<Inter>j\<in>J. Y' j) = emeasure M (?X -` ?E \<inter> space M)"
  1125         by simp
  1126       also have "\<dots> = emeasure ?D ?E"
  1127         using Y  J by (intro emeasure_distr[symmetric] X sets_PiM_I) auto
  1128       also have "\<dots> = emeasure ?P' ?E"
  1129         using \<open>?D = ?P'\<close> by simp
  1130       also have "\<dots> = (\<Prod> i\<in>J. emeasure (?D' i) (Y i))"
  1131         using P.emeasure_PiM_emb[of J Y] J Y by (simp add: prod_emb_def)
  1132       also have "\<dots> = (\<Prod> i\<in>J. emeasure M (Y' i))"
  1133         using rv J Y by (simp add: emeasure_distr)
  1134       finally have "emeasure M (\<Inter>j\<in>J. Y' j) = (\<Prod> i\<in>J. emeasure M (Y' i))" .
  1135       then show "prob (\<Inter>j\<in>J. Y' j) = (\<Prod> i\<in>J. prob (Y' i))"
  1136         by (auto simp: emeasure_eq_measure setprod_ereal)
  1137     qed
  1138   qed
  1139 qed
  1140 
  1141 lemma (in prob_space) indep_varD:
  1142   assumes indep: "indep_var Ma A Mb B"
  1143   assumes sets: "Xa \<in> sets Ma" "Xb \<in> sets Mb"
  1144   shows "prob ((\<lambda>x. (A x, B x)) -` (Xa \<times> Xb) \<inter> space M) =
  1145     prob (A -` Xa \<inter> space M) * prob (B -` Xb \<inter> space M)"
  1146 proof -
  1147   have "prob ((\<lambda>x. (A x, B x)) -` (Xa \<times> Xb) \<inter> space M) =
  1148     prob (\<Inter>i\<in>UNIV. (case_bool A B i -` case_bool Xa Xb i \<inter> space M))"
  1149     by (auto intro!: arg_cong[where f=prob] simp: UNIV_bool)
  1150   also have "\<dots> = (\<Prod>i\<in>UNIV. prob (case_bool A B i -` case_bool Xa Xb i \<inter> space M))"
  1151     using indep unfolding indep_var_def
  1152     by (rule indep_varsD) (auto split: bool.split intro: sets)
  1153   also have "\<dots> = prob (A -` Xa \<inter> space M) * prob (B -` Xb \<inter> space M)"
  1154     unfolding UNIV_bool by simp
  1155   finally show ?thesis .
  1156 qed
  1157 
  1158 lemma (in prob_space) prob_indep_random_variable:
  1159   assumes ind[simp]: "indep_var N X N Y"
  1160   assumes [simp]: "A \<in> sets N" "B \<in> sets N"
  1161   shows "\<P>(x in M. X x \<in> A \<and> Y x \<in> B) = \<P>(x in M. X x \<in> A) * \<P>(x in M. Y x \<in> B)"
  1162 proof-
  1163   have  " \<P>(x in M. (X x)\<in>A \<and>  (Y x)\<in> B ) = prob ((\<lambda>x. (X x, Y x)) -` (A \<times> B) \<inter> space M)" 
  1164     by (auto intro!: arg_cong[where f= prob])
  1165   also have "...=  prob (X -` A \<inter> space M) * prob (Y -` B \<inter> space M)"  
  1166     by (auto intro!: indep_varD[where Ma=N and Mb=N])     
  1167   also have "... = \<P>(x in M. X x \<in> A) * \<P>(x in M. Y x \<in> B)"
  1168     by (auto intro!: arg_cong2[where f= "op *"] arg_cong[where f= prob])
  1169   finally show ?thesis .
  1170 qed
  1171 
  1172 lemma (in prob_space)
  1173   assumes "indep_var S X T Y"
  1174   shows indep_var_rv1: "random_variable S X"
  1175     and indep_var_rv2: "random_variable T Y"
  1176 proof -
  1177   have "\<forall>i\<in>UNIV. random_variable (case_bool S T i) (case_bool X Y i)"
  1178     using assms unfolding indep_var_def indep_vars_def by auto
  1179   then show "random_variable S X" "random_variable T Y"
  1180     unfolding UNIV_bool by auto
  1181 qed
  1182 
  1183 lemma (in prob_space) indep_var_distribution_eq:
  1184   "indep_var S X T Y \<longleftrightarrow> random_variable S X \<and> random_variable T Y \<and>
  1185     distr M S X \<Otimes>\<^sub>M distr M T Y = distr M (S \<Otimes>\<^sub>M T) (\<lambda>x. (X x, Y x))" (is "_ \<longleftrightarrow> _ \<and> _ \<and> ?S \<Otimes>\<^sub>M ?T = ?J")
  1186 proof safe
  1187   assume "indep_var S X T Y"
  1188   then show rvs: "random_variable S X" "random_variable T Y"
  1189     by (blast dest: indep_var_rv1 indep_var_rv2)+
  1190   then have XY: "random_variable (S \<Otimes>\<^sub>M T) (\<lambda>x. (X x, Y x))"
  1191     by (rule measurable_Pair)
  1192 
  1193   interpret X: prob_space ?S by (rule prob_space_distr) fact
  1194   interpret Y: prob_space ?T by (rule prob_space_distr) fact
  1195   interpret XY: pair_prob_space ?S ?T ..
  1196   show "?S \<Otimes>\<^sub>M ?T = ?J"
  1197   proof (rule pair_measure_eqI)
  1198     show "sigma_finite_measure ?S" ..
  1199     show "sigma_finite_measure ?T" ..
  1200 
  1201     fix A B assume A: "A \<in> sets ?S" and B: "B \<in> sets ?T"
  1202     have "emeasure ?J (A \<times> B) = emeasure M ((\<lambda>x. (X x, Y x)) -` (A \<times> B) \<inter> space M)"
  1203       using A B by (intro emeasure_distr[OF XY]) auto
  1204     also have "\<dots> = emeasure M (X -` A \<inter> space M) * emeasure M (Y -` B \<inter> space M)"
  1205       using indep_varD[OF \<open>indep_var S X T Y\<close>, of A B] A B by (simp add: emeasure_eq_measure)
  1206     also have "\<dots> = emeasure ?S A * emeasure ?T B"
  1207       using rvs A B by (simp add: emeasure_distr)
  1208     finally show "emeasure ?S A * emeasure ?T B = emeasure ?J (A \<times> B)" by simp
  1209   qed simp
  1210 next
  1211   assume rvs: "random_variable S X" "random_variable T Y"
  1212   then have XY: "random_variable (S \<Otimes>\<^sub>M T) (\<lambda>x. (X x, Y x))"
  1213     by (rule measurable_Pair)
  1214 
  1215   let ?S = "distr M S X" and ?T = "distr M T Y"
  1216   interpret X: prob_space ?S by (rule prob_space_distr) fact
  1217   interpret Y: prob_space ?T by (rule prob_space_distr) fact
  1218   interpret XY: pair_prob_space ?S ?T ..
  1219 
  1220   assume "?S \<Otimes>\<^sub>M ?T = ?J"
  1221 
  1222   { fix S and X
  1223     have "Int_stable {X -` A \<inter> space M |A. A \<in> sets S}"
  1224     proof (safe intro!: Int_stableI)
  1225       fix A B assume "A \<in> sets S" "B \<in> sets S"
  1226       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"
  1227         by (intro exI[of _ "A \<inter> B"]) auto
  1228     qed }
  1229   note Int_stable = this
  1230 
  1231   show "indep_var S X T Y" unfolding indep_var_eq
  1232   proof (intro conjI indep_set_sigma_sets Int_stable rvs)
  1233     show "indep_set {X -` A \<inter> space M |A. A \<in> sets S} {Y -` A \<inter> space M |A. A \<in> sets T}"
  1234     proof (safe intro!: indep_setI)
  1235       { fix A assume "A \<in> sets S" then show "X -` A \<inter> space M \<in> sets M"
  1236         using \<open>X \<in> measurable M S\<close> by (auto intro: measurable_sets) }
  1237       { fix A assume "A \<in> sets T" then show "Y -` A \<inter> space M \<in> sets M"
  1238         using \<open>Y \<in> measurable M T\<close> by (auto intro: measurable_sets) }
  1239     next
  1240       fix A B assume ab: "A \<in> sets S" "B \<in> sets T"
  1241       then have "ereal (prob ((X -` A \<inter> space M) \<inter> (Y -` B \<inter> space M))) = emeasure ?J (A \<times> B)"
  1242         using XY by (auto simp add: emeasure_distr emeasure_eq_measure intro!: arg_cong[where f="prob"])
  1243       also have "\<dots> = emeasure (?S \<Otimes>\<^sub>M ?T) (A \<times> B)"
  1244         unfolding \<open>?S \<Otimes>\<^sub>M ?T = ?J\<close> ..
  1245       also have "\<dots> = emeasure ?S A * emeasure ?T B"
  1246         using ab by (simp add: Y.emeasure_pair_measure_Times)
  1247       finally show "prob ((X -` A \<inter> space M) \<inter> (Y -` B \<inter> space M)) =
  1248         prob (X -` A \<inter> space M) * prob (Y -` B \<inter> space M)"
  1249         using rvs ab by (simp add: emeasure_eq_measure emeasure_distr)
  1250     qed
  1251   qed
  1252 qed
  1253 
  1254 lemma (in prob_space) distributed_joint_indep:
  1255   assumes S: "sigma_finite_measure S" and T: "sigma_finite_measure T"
  1256   assumes X: "distributed M S X Px" and Y: "distributed M T Y Py"
  1257   assumes indep: "indep_var S X T Y"
  1258   shows "distributed M (S \<Otimes>\<^sub>M T) (\<lambda>x. (X x, Y x)) (\<lambda>(x, y). Px x * Py y)"
  1259   using indep_var_distribution_eq[of S X T Y] indep
  1260   by (intro distributed_joint_indep'[OF S T X Y]) auto
  1261 
  1262 lemma (in prob_space) indep_vars_nn_integral:
  1263   assumes I: "finite I" "indep_vars (\<lambda>_. borel) X I" "\<And>i \<omega>. i \<in> I \<Longrightarrow> 0 \<le> X i \<omega>"
  1264   shows "(\<integral>\<^sup>+\<omega>. (\<Prod>i\<in>I. X i \<omega>) \<partial>M) = (\<Prod>i\<in>I. \<integral>\<^sup>+\<omega>. X i \<omega> \<partial>M)"
  1265 proof cases
  1266   assume "I \<noteq> {}"
  1267   def Y \<equiv> "\<lambda>i \<omega>. if i \<in> I then X i \<omega> else 0"
  1268   { fix i have "i \<in> I \<Longrightarrow> random_variable borel (X i)"
  1269     using I(2) by (cases "i\<in>I") (auto simp: indep_vars_def) }
  1270   note rv_X = this
  1271 
  1272   { fix i have "random_variable borel (Y i)"
  1273     using I(2) by (cases "i\<in>I") (auto simp: Y_def rv_X) }
  1274   note rv_Y = this[measurable]
  1275     
  1276   interpret Y: prob_space "distr M borel (Y i)" for i
  1277     using I(2) by (cases "i \<in> I") (auto intro!: prob_space_distr simp: indep_vars_def prob_space_return)
  1278   interpret product_sigma_finite "\<lambda>i. distr M borel (Y i)"
  1279     ..
  1280   
  1281   have indep_Y: "indep_vars (\<lambda>i. borel) Y I"
  1282     by (rule indep_vars_cong[THEN iffD1, OF _ _ _ I(2)]) (auto simp: Y_def)
  1283 
  1284   have "(\<integral>\<^sup>+\<omega>. (\<Prod>i\<in>I. X i \<omega>) \<partial>M) = (\<integral>\<^sup>+\<omega>. (\<Prod>i\<in>I. max 0 (Y i \<omega>)) \<partial>M)"
  1285     using I(3) by (auto intro!: nn_integral_cong setprod.cong simp add: Y_def max_def)
  1286   also have "\<dots> = (\<integral>\<^sup>+\<omega>. (\<Prod>i\<in>I. max 0 (\<omega> i)) \<partial>distr M (Pi\<^sub>M I (\<lambda>i. borel)) (\<lambda>x. \<lambda>i\<in>I. Y i x))"
  1287     by (subst nn_integral_distr) auto
  1288   also have "\<dots> = (\<integral>\<^sup>+\<omega>. (\<Prod>i\<in>I. max 0 (\<omega> i)) \<partial>Pi\<^sub>M I (\<lambda>i. distr M borel (Y i)))"
  1289     unfolding indep_vars_iff_distr_eq_PiM[THEN iffD1, OF \<open>I \<noteq> {}\<close> rv_Y indep_Y] ..
  1290   also have "\<dots> = (\<Prod>i\<in>I. (\<integral>\<^sup>+\<omega>. max 0 \<omega> \<partial>distr M borel (Y i)))"
  1291     by (rule product_nn_integral_setprod) (auto intro: \<open>finite I\<close>)
  1292   also have "\<dots> = (\<Prod>i\<in>I. \<integral>\<^sup>+\<omega>. X i \<omega> \<partial>M)"
  1293     by (intro setprod.cong nn_integral_cong)
  1294        (auto simp: nn_integral_distr nn_integral_max_0 Y_def rv_X)
  1295   finally show ?thesis .
  1296 qed (simp add: emeasure_space_1)
  1297 
  1298 lemma (in prob_space)
  1299   fixes X :: "'i \<Rightarrow> 'a \<Rightarrow> 'b::{real_normed_field, banach, second_countable_topology}"
  1300   assumes I: "finite I" "indep_vars (\<lambda>_. borel) X I" "\<And>i. i \<in> I \<Longrightarrow> integrable M (X i)"
  1301   shows indep_vars_lebesgue_integral: "(\<integral>\<omega>. (\<Prod>i\<in>I. X i \<omega>) \<partial>M) = (\<Prod>i\<in>I. \<integral>\<omega>. X i \<omega> \<partial>M)" (is ?eq)
  1302     and indep_vars_integrable: "integrable M (\<lambda>\<omega>. (\<Prod>i\<in>I. X i \<omega>))" (is ?int)
  1303 proof (induct rule: case_split)
  1304   assume "I \<noteq> {}"
  1305   def Y \<equiv> "\<lambda>i \<omega>. if i \<in> I then X i \<omega> else 0"
  1306   { fix i have "i \<in> I \<Longrightarrow> random_variable borel (X i)"
  1307     using I(2) by (cases "i\<in>I") (auto simp: indep_vars_def) }
  1308   note rv_X = this[measurable]
  1309 
  1310   { fix i have "random_variable borel (Y i)"
  1311     using I(2) by (cases "i\<in>I") (auto simp: Y_def rv_X) }
  1312   note rv_Y = this[measurable]
  1313 
  1314   { fix i have "integrable M (Y i)"
  1315     using I(3) by (cases "i\<in>I") (auto simp: Y_def) }
  1316   note int_Y = this
  1317     
  1318   interpret Y: prob_space "distr M borel (Y i)" for i
  1319     using I(2) by (cases "i \<in> I") (auto intro!: prob_space_distr simp: indep_vars_def prob_space_return)
  1320   interpret product_sigma_finite "\<lambda>i. distr M borel (Y i)"
  1321     ..
  1322   
  1323   have indep_Y: "indep_vars (\<lambda>i. borel) Y I"
  1324     by (rule indep_vars_cong[THEN iffD1, OF _ _ _ I(2)]) (auto simp: Y_def)
  1325 
  1326   have "(\<integral>\<omega>. (\<Prod>i\<in>I. X i \<omega>) \<partial>M) = (\<integral>\<omega>. (\<Prod>i\<in>I. Y i \<omega>) \<partial>M)"
  1327     using I(3) by (simp add: Y_def)
  1328   also have "\<dots> = (\<integral>\<omega>. (\<Prod>i\<in>I. \<omega> i) \<partial>distr M (Pi\<^sub>M I (\<lambda>i. borel)) (\<lambda>x. \<lambda>i\<in>I. Y i x))"
  1329     by (subst integral_distr) auto
  1330   also have "\<dots> = (\<integral>\<omega>. (\<Prod>i\<in>I. \<omega> i) \<partial>Pi\<^sub>M I (\<lambda>i. distr M borel (Y i)))"
  1331     unfolding indep_vars_iff_distr_eq_PiM[THEN iffD1, OF \<open>I \<noteq> {}\<close> rv_Y indep_Y] ..
  1332   also have "\<dots> = (\<Prod>i\<in>I. (\<integral>\<omega>. \<omega> \<partial>distr M borel (Y i)))"
  1333     by (rule product_integral_setprod) (auto intro: \<open>finite I\<close> simp: integrable_distr_eq int_Y)
  1334   also have "\<dots> = (\<Prod>i\<in>I. \<integral>\<omega>. X i \<omega> \<partial>M)"
  1335     by (intro setprod.cong integral_cong)
  1336        (auto simp: integral_distr Y_def rv_X)
  1337   finally show ?eq .
  1338 
  1339   have "integrable (distr M (Pi\<^sub>M I (\<lambda>i. borel)) (\<lambda>x. \<lambda>i\<in>I. Y i x)) (\<lambda>\<omega>. (\<Prod>i\<in>I. \<omega> i))"
  1340     unfolding indep_vars_iff_distr_eq_PiM[THEN iffD1, OF \<open>I \<noteq> {}\<close> rv_Y indep_Y]
  1341     by (intro product_integrable_setprod[OF \<open>finite I\<close>])
  1342        (simp add: integrable_distr_eq int_Y)
  1343   then show ?int
  1344     by (simp add: integrable_distr_eq Y_def)
  1345 qed (simp_all add: prob_space)
  1346 
  1347 lemma (in prob_space)
  1348   fixes X1 X2 :: "'a \<Rightarrow> 'b::{real_normed_field, banach, second_countable_topology}"
  1349   assumes "indep_var borel X1 borel X2" "integrable M X1" "integrable M X2"
  1350   shows indep_var_lebesgue_integral: "(\<integral>\<omega>. X1 \<omega> * X2 \<omega> \<partial>M) = (\<integral>\<omega>. X1 \<omega> \<partial>M) * (\<integral>\<omega>. X2 \<omega> \<partial>M)" (is ?eq)
  1351     and indep_var_integrable: "integrable M (\<lambda>\<omega>. X1 \<omega> * X2 \<omega>)" (is ?int)
  1352 unfolding indep_var_def
  1353 proof -
  1354   have *: "(\<lambda>\<omega>. X1 \<omega> * X2 \<omega>) = (\<lambda>\<omega>. \<Prod>i\<in>UNIV. (case_bool X1 X2 i \<omega>))"
  1355     by (simp add: UNIV_bool mult.commute)
  1356   have **: "(\<lambda> _. borel) = case_bool borel borel"
  1357     by (rule ext, metis (full_types) bool.simps(3) bool.simps(4))
  1358   show ?eq
  1359     apply (subst *)
  1360     apply (subst indep_vars_lebesgue_integral)
  1361     apply (auto)
  1362     apply (subst **, subst indep_var_def [symmetric], rule assms)
  1363     apply (simp split: bool.split add: assms)
  1364     by (simp add: UNIV_bool mult.commute)
  1365   show ?int
  1366     apply (subst *)
  1367     apply (rule indep_vars_integrable)
  1368     apply auto
  1369     apply (subst **, subst indep_var_def [symmetric], rule assms)
  1370     by (simp split: bool.split add: assms)
  1371 qed
  1372 
  1373 end