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