Theory Infinite_Product_Measure

theory Infinite_Product_Measure
imports Projective_Family
(*  Title:      HOL/Probability/Infinite_Product_Measure.thy
    Author:     Johannes Hölzl, TU München
*)

section ‹Infinite Product Measure›

theory Infinite_Product_Measure
  imports Probability_Measure Caratheodory Projective_Family
begin

lemma (in product_prob_space) distr_PiM_restrict_finite:
  assumes "finite J" "J ⊆ I"
  shows "distr (PiM I M) (PiM J M) (λx. restrict x J) = PiM J M"
proof (rule PiM_eqI)
  fix X assume X: "⋀i. i ∈ J ⟹ X i ∈ sets (M i)"
  { fix J X assume J: "J ≠ {} ∨ I = {}" "finite J" "J ⊆ I" and X: "⋀i. i ∈ J ⟹ X i ∈ sets (M i)"
    have "emeasure (PiM I M) (emb I J (PiE J X)) = (∏i∈J. M i (X i))"
    proof (subst emeasure_extend_measure_Pair[OF PiM_def, where μ'=lim], goal_cases)
      case 1 then show ?case
        by (simp add: M.emeasure_space_1 emeasure_PiM Pi_iff sets_PiM_I_finite emeasure_lim_emb)
    next
      case (2 J X)
      then have "emb I J (PiE J X) ∈ sets (PiM I M)"
        by (intro measurable_prod_emb sets_PiM_I_finite) auto
      from this[THEN sets.sets_into_space] show ?case
        by (simp add: space_PiM)
    qed (insert assms J X, simp_all del: sets_lim
      add: M.emeasure_space_1 sets_lim[symmetric] emeasure_countably_additive emeasure_positive) }
  note * = this

  have "emeasure (PiM I M) (emb I J (PiE J X)) = (∏i∈J. M i (X i))"
  proof cases
    assume "¬ (J ≠ {} ∨ I = {})"
    then obtain i where "J = {}" "i ∈ I" by auto
    moreover then have "emb I {} {λx. undefined} = emb I {i} (ΠE i∈{i}. space (M i))"
      by (auto simp: space_PiM prod_emb_def)
    ultimately show ?thesis
      by (simp add: * M.emeasure_space_1)
  qed (simp add: *[OF _ assms X])
  with assms show "emeasure (distr (PiM I M) (PiM J M) (λx. restrict x J)) (PiE J X) = (∏i∈J. emeasure (M i) (X i))"
    by (subst emeasure_distr_restrict[OF _ refl]) (auto intro!: sets_PiM_I_finite X)
qed (insert assms, auto)

lemma (in product_prob_space) emeasure_PiM_emb':
  "J ⊆ I ⟹ finite J ⟹ X ∈ sets (PiM J M) ⟹ emeasure (PiM I M) (emb I J X) = PiM J M X"
  by (subst distr_PiM_restrict_finite[symmetric, of J])
     (auto intro!: emeasure_distr_restrict[symmetric])

lemma (in product_prob_space) emeasure_PiM_emb:
  "J ⊆ I ⟹ finite J ⟹ (⋀i. i ∈ J ⟹ X i ∈ sets (M i)) ⟹
    emeasure (PiM I M) (emb I J (PiE J X)) = (∏ i∈J. emeasure (M i) (X i))"
  by (subst emeasure_PiM_emb') (auto intro!: emeasure_PiM)

sublocale product_prob_space  P?: prob_space "PiM I M"
proof
  have *: "emb I {} {λx. undefined} = space (PiM I M)"
    by (auto simp: prod_emb_def space_PiM)
  show "emeasure (PiM I M) (space (PiM I M)) = 1"
    using emeasure_PiM_emb[of "{}" "λ_. {}"] by (simp add: *)
qed

lemma (in product_prob_space) emeasure_PiM_Collect:
  assumes X: "J ⊆ I" "finite J" "⋀i. i ∈ J ⟹ X i ∈ sets (M i)"
  shows "emeasure (PiM I M) {x∈space (PiM I M). ∀i∈J. x i ∈ X i} = (∏ i∈J. emeasure (M i) (X i))"
proof -
  have "{x∈space (PiM I M). ∀i∈J. x i ∈ X i} = emb I J (PiE J X)"
    unfolding prod_emb_def using assms by (auto simp: space_PiM Pi_iff)
  with emeasure_PiM_emb[OF assms] show ?thesis by simp
qed

lemma (in product_prob_space) emeasure_PiM_Collect_single:
  assumes X: "i ∈ I" "A ∈ sets (M i)"
  shows "emeasure (PiM I M) {x∈space (PiM I M). x i ∈ A} = emeasure (M i) A"
  using emeasure_PiM_Collect[of "{i}" "λi. A"] assms
  by simp

lemma (in product_prob_space) measure_PiM_emb:
  assumes "J ⊆ I" "finite J" "⋀i. i ∈ J ⟹ X i ∈ sets (M i)"
  shows "measure (PiM I M) (emb I J (PiE J X)) = (∏ i∈J. measure (M i) (X i))"
  using emeasure_PiM_emb[OF assms]
  unfolding emeasure_eq_measure M.emeasure_eq_measure
  by (simp add: setprod_ennreal measure_nonneg setprod_nonneg)

lemma sets_Collect_single':
  "i ∈ I ⟹ {x∈space (M i). P x} ∈ sets (M i) ⟹ {x∈space (PiM I M). P (x i)} ∈ sets (PiM I M)"
  using sets_Collect_single[of i I "{x∈space (M i). P x}" M]
  by (simp add: space_PiM PiE_iff cong: conj_cong)

lemma (in finite_product_prob_space) finite_measure_PiM_emb:
  "(⋀i. i ∈ I ⟹ A i ∈ sets (M i)) ⟹ measure (PiM I M) (PiE I A) = (∏i∈I. measure (M i) (A i))"
  using measure_PiM_emb[of I A] finite_index prod_emb_PiE_same_index[OF sets.sets_into_space, of I A M]
  by auto

lemma (in product_prob_space) PiM_component:
  assumes "i ∈ I"
  shows "distr (PiM I M) (M i) (λω. ω i) = M i"
proof (rule measure_eqI[symmetric])
  fix A assume "A ∈ sets (M i)"
  moreover have "((λω. ω i) -` A ∩ space (PiM I M)) = {x∈space (PiM I M). x i ∈ A}"
    by auto
  ultimately show "emeasure (M i) A = emeasure (distr (PiM I M) (M i) (λω. ω i)) A"
    by (auto simp: ‹i∈I› emeasure_distr measurable_component_singleton emeasure_PiM_Collect_single)
qed simp

lemma (in product_prob_space) PiM_eq:
  assumes M': "sets M' = sets (PiM I M)"
  assumes eq: "⋀J F. finite J ⟹ J ⊆ I ⟹ (⋀j. j ∈ J ⟹ F j ∈ sets (M j)) ⟹
    emeasure M' (prod_emb I M J (ΠE j∈J. F j)) = (∏j∈J. emeasure (M j) (F j))"
  shows "M' = (PiM I M)"
proof (rule measure_eqI_PiM_infinite[symmetric, OF refl M'])
  show "finite_measure (PiM I M)"
    by standard (simp add: P.emeasure_space_1)
qed (simp add: eq emeasure_PiM_emb)

lemma (in product_prob_space) AE_component: "i ∈ I ⟹ AE x in M i. P x ⟹ AE x in PiM I M. P (x i)"
  apply (rule AE_distrD[of "λω. ω i" "PiM I M" "M i" P])
  apply simp
  apply (subst PiM_component)
  apply simp_all
  done

subsection ‹Sequence space›

definition comb_seq :: "nat ⇒ (nat ⇒ 'a) ⇒ (nat ⇒ 'a) ⇒ (nat ⇒ 'a)" where
  "comb_seq i ω ω' j = (if j < i then ω j else ω' (j - i))"

lemma split_comb_seq: "P (comb_seq i ω ω' j) ⟷ (j < i ⟶ P (ω j)) ∧ (∀k. j = i + k ⟶ P (ω' k))"
  by (auto simp: comb_seq_def not_less)

lemma split_comb_seq_asm: "P (comb_seq i ω ω' j) ⟷ ¬ ((j < i ∧ ¬ P (ω j)) ∨ (∃k. j = i + k ∧ ¬ P (ω' k)))"
  by (auto simp: comb_seq_def)

lemma measurable_comb_seq:
  "(λ(ω, ω'). comb_seq i ω ω') ∈ measurable ((ΠM i∈UNIV. M) ⨂MM i∈UNIV. M)) (ΠM i∈UNIV. M)"
proof (rule measurable_PiM_single)
  show "(λ(ω, ω'). comb_seq i ω ω') ∈ space ((ΠM i∈UNIV. M) ⨂MM i∈UNIV. M)) → (UNIV →E space M)"
    by (auto simp: space_pair_measure space_PiM PiE_iff split: split_comb_seq)
  fix j :: nat and A assume A: "A ∈ sets M"
  then have *: "{ω ∈ space ((ΠM i∈UNIV. M) ⨂MM i∈UNIV. M)). case_prod (comb_seq i) ω j ∈ A} =
    (if j < i then {ω ∈ space (ΠM i∈UNIV. M). ω j ∈ A} × space (ΠM i∈UNIV. M)
              else space (ΠM i∈UNIV. M) × {ω ∈ space (ΠM i∈UNIV. M). ω (j - i) ∈ A})"
    by (auto simp: space_PiM space_pair_measure comb_seq_def dest: sets.sets_into_space)
  show "{ω ∈ space ((ΠM i∈UNIV. M) ⨂MM i∈UNIV. M)). case_prod (comb_seq i) ω j ∈ A} ∈ sets ((ΠM i∈UNIV. M) ⨂MM i∈UNIV. M))"
    unfolding * by (auto simp: A intro!: sets_Collect_single)
qed

lemma measurable_comb_seq'[measurable (raw)]:
  assumes f: "f ∈ measurable N (ΠM i∈UNIV. M)" and g: "g ∈ measurable N (ΠM i∈UNIV. M)"
  shows "(λx. comb_seq i (f x) (g x)) ∈ measurable N (ΠM i∈UNIV. M)"
  using measurable_compose[OF measurable_Pair[OF f g] measurable_comb_seq] by simp

lemma comb_seq_0: "comb_seq 0 ω ω' = ω'"
  by (auto simp add: comb_seq_def)

lemma comb_seq_Suc: "comb_seq (Suc n) ω ω' = comb_seq n ω (case_nat (ω n) ω')"
  by (auto simp add: comb_seq_def not_less less_Suc_eq le_imp_diff_is_add intro!: ext split: nat.split)

lemma comb_seq_Suc_0[simp]: "comb_seq (Suc 0) ω = case_nat (ω 0)"
  by (intro ext) (simp add: comb_seq_Suc comb_seq_0)

lemma comb_seq_less: "i < n ⟹ comb_seq n ω ω' i = ω i"
  by (auto split: split_comb_seq)

lemma comb_seq_add: "comb_seq n ω ω' (i + n) = ω' i"
  by (auto split: nat.split split_comb_seq)

lemma case_nat_comb_seq: "case_nat s' (comb_seq n ω ω') (i + n) = case_nat (case_nat s' ω n) ω' i"
  by (auto split: nat.split split_comb_seq)

lemma case_nat_comb_seq':
  "case_nat s (comb_seq i ω ω') = comb_seq (Suc i) (case_nat s ω) ω'"
  by (auto split: split_comb_seq nat.split)

locale sequence_space = product_prob_space "λi. M" "UNIV :: nat set" for M
begin

abbreviation "S ≡ ΠM i∈UNIV::nat set. M"

lemma infprod_in_sets[intro]:
  fixes E :: "nat ⇒ 'a set" assumes E: "⋀i. E i ∈ sets M"
  shows "Pi UNIV E ∈ sets S"
proof -
  have "Pi UNIV E = (⋂i. emb UNIV {..i} (ΠE j∈{..i}. E j))"
    using E E[THEN sets.sets_into_space]
    by (auto simp: prod_emb_def Pi_iff extensional_def)
  with E show ?thesis by auto
qed

lemma measure_PiM_countable:
  fixes E :: "nat ⇒ 'a set" assumes E: "⋀i. E i ∈ sets M"
  shows "(λn. ∏i≤n. measure M (E i)) ⇢ measure S (Pi UNIV E)"
proof -
  let ?E = "λn. emb UNIV {..n} (PiE {.. n} E)"
  have "⋀n. (∏i≤n. measure M (E i)) = measure S (?E n)"
    using E by (simp add: measure_PiM_emb)
  moreover have "Pi UNIV E = (⋂n. ?E n)"
    using E E[THEN sets.sets_into_space]
    by (auto simp: prod_emb_def extensional_def Pi_iff)
  moreover have "range ?E ⊆ sets S"
    using E by auto
  moreover have "decseq ?E"
    by (auto simp: prod_emb_def Pi_iff decseq_def)
  ultimately show ?thesis
    by (simp add: finite_Lim_measure_decseq)
qed

lemma nat_eq_diff_eq:
  fixes a b c :: nat
  shows "c ≤ b ⟹ a = b - c ⟷ a + c = b"
  by auto

lemma PiM_comb_seq:
  "distr (S ⨂M S) S (λ(ω, ω'). comb_seq i ω ω') = S" (is "?D = _")
proof (rule PiM_eq)
  let ?I = "UNIV::nat set" and ?M = "λn. M"
  let "distr _ _ ?f" = "?D"

  fix J E assume J: "finite J" "J ⊆ ?I" "⋀j. j ∈ J ⟹ E j ∈ sets M"
  let ?X = "prod_emb ?I ?M J (ΠE j∈J. E j)"
  have "⋀j x. j ∈ J ⟹ x ∈ E j ⟹ x ∈ space M"
    using J(3)[THEN sets.sets_into_space] by (auto simp: space_PiM Pi_iff subset_eq)
  with J have "?f -` ?X ∩ space (S ⨂M S) =
    (prod_emb ?I ?M (J ∩ {..<i}) (PIE j:J ∩ {..<i}. E j)) ×
    (prod_emb ?I ?M ((op + i) -` J) (PIE j:(op + i) -` J. E (i + j)))" (is "_ = ?E × ?F")
   by (auto simp: space_pair_measure space_PiM prod_emb_def all_conj_distrib PiE_iff
               split: split_comb_seq split_comb_seq_asm)
  then have "emeasure ?D ?X = emeasure (S ⨂M S) (?E × ?F)"
    by (subst emeasure_distr[OF measurable_comb_seq])
       (auto intro!: sets_PiM_I simp: split_beta' J)
  also have "… = emeasure S ?E * emeasure S ?F"
    using J by (intro P.emeasure_pair_measure_Times)  (auto intro!: sets_PiM_I finite_vimageI simp: inj_on_def)
  also have "emeasure S ?F = (∏j∈(op + i) -` J. emeasure M (E (i + j)))"
    using J by (intro emeasure_PiM_emb) (simp_all add: finite_vimageI inj_on_def)
  also have "… = (∏j∈J - (J ∩ {..<i}). emeasure M (E j))"
    by (rule setprod.reindex_cong [of "λx. x - i"])
       (auto simp: image_iff Bex_def not_less nat_eq_diff_eq ac_simps cong: conj_cong intro!: inj_onI)
  also have "emeasure S ?E = (∏j∈J ∩ {..<i}. emeasure M (E j))"
    using J by (intro emeasure_PiM_emb) simp_all
  also have "(∏j∈J ∩ {..<i}. emeasure M (E j)) * (∏j∈J - (J ∩ {..<i}). emeasure M (E j)) = (∏j∈J. emeasure M (E j))"
    by (subst mult.commute) (auto simp: J setprod.subset_diff[symmetric])
  finally show "emeasure ?D ?X = (∏j∈J. emeasure M (E j))" .
qed simp_all

lemma PiM_iter:
  "distr (M ⨂M S) S (λ(s, ω). case_nat s ω) = S" (is "?D = _")
proof (rule PiM_eq)
  let ?I = "UNIV::nat set" and ?M = "λn. M"
  let "distr _ _ ?f" = "?D"

  fix J E assume J: "finite J" "J ⊆ ?I" "⋀j. j ∈ J ⟹ E j ∈ sets M"
  let ?X = "prod_emb ?I ?M J (PIE j:J. E j)"
  have "⋀j x. j ∈ J ⟹ x ∈ E j ⟹ x ∈ space M"
    using J(3)[THEN sets.sets_into_space] by (auto simp: space_PiM Pi_iff subset_eq)
  with J have "?f -` ?X ∩ space (M ⨂M S) = (if 0 ∈ J then E 0 else space M) ×
    (prod_emb ?I ?M (Suc -` J) (PIE j:Suc -` J. E (Suc j)))" (is "_ = ?E × ?F")
   by (auto simp: space_pair_measure space_PiM PiE_iff prod_emb_def all_conj_distrib
      split: nat.split nat.split_asm)
  then have "emeasure ?D ?X = emeasure (M ⨂M S) (?E × ?F)"
    by (subst emeasure_distr)
       (auto intro!: sets_PiM_I simp: split_beta' J)
  also have "… = emeasure M ?E * emeasure S ?F"
    using J by (intro P.emeasure_pair_measure_Times) (auto intro!: sets_PiM_I finite_vimageI)
  also have "emeasure S ?F = (∏j∈Suc -` J. emeasure M (E (Suc j)))"
    using J by (intro emeasure_PiM_emb) (simp_all add: finite_vimageI)
  also have "… = (∏j∈J - {0}. emeasure M (E j))"
    by (rule setprod.reindex_cong [of "λx. x - 1"])
       (auto simp: image_iff Bex_def not_less nat_eq_diff_eq ac_simps cong: conj_cong intro!: inj_onI)
  also have "emeasure M ?E * (∏j∈J - {0}. emeasure M (E j)) = (∏j∈J. emeasure M (E j))"
    by (auto simp: M.emeasure_space_1 setprod.remove J)
  finally show "emeasure ?D ?X = (∏j∈J. emeasure M (E j))" .
qed simp_all

end

end