Theory Euclidean_Brownian_Motion

theory Euclidean_Brownian_Motion
  imports Brownian_Motion
begin

section ‹Brownian motion in finite-dimensional Euclidean space›

text ‹The construction uses one independent copy of the real Brownian motion for
  every coordinate.  The dimension type is finite but otherwise arbitrary.›

definition euclidean_brownian_source ::
    "('d::finite ⇒ real ⇒ real) measure" where
  "euclidean_brownian_source = PiM UNIV (λ_. proc_source brownian_process)"

lemma prob_space_euclidean_brownian_source:
  "prob_space (euclidean_brownian_source :: ('d::finite ⇒ real ⇒ real) measure)"
  unfolding euclidean_brownian_source_def
  by (rule prob_space_PiM) (simp add: proc_source.prob_space_axioms)

interpretation euclidean_source: prob_space
  "euclidean_brownian_source :: ('d::finite ⇒ real ⇒ real) measure"
  by (rule prob_space_euclidean_brownian_source)

lemma borel_measurable_vec_lambda:
  fixes f :: "'a ⇒ 'd::finite ⇒ real"
  assumes coord: "⋀i. (λx. f x i) ∈ borel_measurable M"
  shows "(λx. (χ i. f x i) :: real^'d) ∈ borel_measurable M"
proof (rule borel_measurable_iff_halfspace_less[THEN iffD2])
  show "∀b∈Basis. ∀a. {x ∈ space M. (χ i. f x i) ∙ b < a} ∈ sets M"
  proof (intro ballI allI)
    fix b :: "real^'d" and a :: real
    have scalar: "(λx. (χ i. f x i) ∙ b) ∈ borel_measurable M"
      unfolding inner_vec_def by (measurable; simp add: coord)
    show "{x ∈ space M. (χ i. f x i) ∙ b < a} ∈ sets M"
      using scalar unfolding borel_measurable_iff_less by blast
  qed
qed

definition euclidean_brownian_path ::
    "real ⇒ ('d::finite ⇒ real ⇒ real) ⇒ real^'d" where
  "euclidean_brownian_path t w = (χ i. process brownian_process t (w i))"

lemma euclidean_brownian_path_measurable [measurable]:
  "euclidean_brownian_path t ∈
    borel_measurable (euclidean_brownian_source ::
      ('d::finite ⇒ real ⇒ real) measure)"
proof -
  have target: "sets (proc_target brownian_process) = sets borel"
    using compatible_target[OF modificationD(1)[OF brownian_process_modification]]
    by (simp add: gaussian_process_target)
  have process_meas: "process brownian_process t ∈
      borel_measurable (proc_source brownian_process)"
    using stochastic_process_measurable[of brownian_process t]
    by (simp only: measurable_cong_sets[OF refl target])
  have coord: "(λw. process brownian_process t (w i)) ∈
      borel_measurable (PiM UNIV (λ_. proc_source brownian_process))" for i :: 'd
    by (rule measurable_PiM_component_rev) (simp_all add: process_meas)
  show ?thesis
    unfolding euclidean_brownian_path_def euclidean_brownian_source_def
    apply (rule borel_measurable_iff_halfspace_less[THEN iffD2])
    apply (intro ballI allI)
    subgoal for b a
    proof -
      have scalar: "(λw. (χ i. process brownian_process t (w i)) ∙ b) ∈
          borel_measurable (PiM UNIV (λ_. proc_source brownian_process))"
        unfolding inner_vec_def by (measurable; simp add: coord)
      show "{w ∈ space (PiM UNIV (λ_. proc_source brownian_process)).
          (χ i. process brownian_process t (w i)) ∙ b < a} ∈
          sets (PiM UNIV (λ_. proc_source brownian_process))"
        using scalar unfolding borel_measurable_iff_less by blast
    qed
    done
qed

definition euclidean_brownian_process ::
    "(real, 'd::finite ⇒ real ⇒ real, real^'d) stochastic_process" where
  "euclidean_brownian_process =
    euclidean_source.process_of borel {0..} euclidean_brownian_path 0"

lemma euclidean_brownian_process_index [simp]:
  "proc_index (euclidean_brownian_process ::
    (real, 'd::finite ⇒ real ⇒ real, real^'d) stochastic_process) = {0..}"
  unfolding euclidean_brownian_process_def by simp

lemma euclidean_brownian_process_source [simp]:
  "proc_source (euclidean_brownian_process ::
    (real, 'd::finite ⇒ real ⇒ real, real^'d) stochastic_process) =
    euclidean_brownian_source"
  unfolding euclidean_brownian_process_def
  by (rule euclidean_source.source_process_of) (auto simp: euclidean_brownian_path_measurable)

lemma euclidean_brownian_process_target [simp]:
  "proc_target (euclidean_brownian_process ::
    (real, 'd::finite ⇒ real ⇒ real, real^'d) stochastic_process) = borel"
  unfolding euclidean_brownian_process_def
  by (rule euclidean_source.target_process_of) (auto simp: euclidean_brownian_path_measurable)

lemma euclidean_brownian_process_apply [simp]:
  assumes "0 ≤ t"
  shows "process (euclidean_brownian_process ::
      (real, 'd::finite ⇒ real ⇒ real, real^'d) stochastic_process) t w =
    (χ i. process brownian_process t (w i))"
proof -
  have eq: "process (euclidean_brownian_process ::
      (real, 'd ⇒ real ⇒ real, real^'d) stochastic_process) t =
      euclidean_brownian_path t"
    unfolding euclidean_brownian_process_def
    by (rule euclidean_source.process_of_apply) (use assms in
        ‹auto simp: euclidean_brownian_path_measurable›)
  show ?thesis
    using fun_cong[OF eq, of w]
    unfolding euclidean_brownian_path_def by simp
qed

subsection ‹Centred Euclidean normal distributions›

definition euclidean_centered_normal ::
    "real ⇒ (real^'d::finite) measure" where
  "euclidean_centered_normal v =
    distr (PiM UNIV (λ_::'d. centered_normal v)) borel (λx. χ i. x i)"

lemma prob_space_euclidean_centered_normal:
  assumes "0 ≤ v"
  shows "prob_space (euclidean_centered_normal v :: (real^'d::finite) measure)"
  unfolding euclidean_centered_normal_def
  apply (rule prob_space.prob_space_distr)
   apply (rule prob_space_PiM)
   apply (simp add: prob_space_centered_normal)
  apply (rule borel_measurable_vec_lambda)
  apply (rule measurable_PiM_component_rev)
   apply simp
  apply (rule measurable_ident_sets)
  by simp

lemma sets_euclidean_centered_normal [simp]:
  "sets (euclidean_centered_normal v :: (real^'d::finite) measure) = sets borel"
  unfolding euclidean_centered_normal_def by simp

lemma euclidean_centered_normal_coordinate:
  assumes "0 ≤ v"
  shows "distr (euclidean_centered_normal v :: (real^'d::finite) measure) borel
      (λx. x $ i) = centered_normal v"
proof -
  let ?M = "PiM UNIV (λ_::'d. centered_normal v)"
  have ps: "prob_space ?M"
    by (rule prob_space_PiM) (simp add: prob_space_centered_normal)
  have comp': "distr ?M (centered_normal v) (λx. x i) = centered_normal v"
    by (rule distr_PiM_component) (use assms in
        ‹auto simp: prob_space_centered_normal›)
  have target_eq: "distr ?M borel (λx. x i) =
      distr ?M (centered_normal v) (λx. x i)"
    by (rule distr_cong) auto
  have comp: "distr ?M borel (λx. x i) = centered_normal v"
    using target_eq comp' by simp
  have Vmeas: "(λx. χ i. x i :: real^'d) ∈ borel_measurable ?M"
    apply (rule borel_measurable_vec_lambda)
    apply (rule measurable_PiM_component_rev)
     apply simp
    apply (rule measurable_ident_sets)
    by simp
  have "distr (euclidean_centered_normal v :: (real^'d) measure) borel (λx. x $ i) =
      distr ?M borel ((λx. x $ i) ∘ (λx. χ i. x i))"
    unfolding euclidean_centered_normal_def
    by (rule distr_distr) (measurable, rule Vmeas)
  also have "... = centered_normal v"
    using comp by (simp add: comp_def)
  finally show ?thesis .
qed

lemma euclidean_centered_normal_independent_coordinates:
  assumes "0 ≤ v"
  shows "prob_space.indep_vars (euclidean_centered_normal v ::
      (real^'d::finite) measure) (λ_. borel) (λi x. x $ i) UNIV"
proof -
  let ?N = "PiM UNIV (λ_::'d. centered_normal v)"
  let ?V = "λx. χ i. x i :: real^'d"
  have psN: "prob_space ?N"
    by (rule prob_space_PiM) (simp add: prob_space_centered_normal)
  have psE: "prob_space (euclidean_centered_normal v :: (real^'d) measure)"
    by (rule prob_space_euclidean_centered_normal[OF assms])
  have Vmeas: "?V ∈ borel_measurable ?N"
    apply (rule borel_measurable_vec_lambda)
    apply (rule measurable_PiM_component_rev)
     apply simp
    apply (rule measurable_ident_sets)
    by simp
  have rv: "(λx. restrict (λi. x $ i) UNIV) ∈
      measurable (euclidean_centered_normal v :: (real^'d) measure)
        (PiM UNIV (λ_::'d. borel))"
    apply measurable
    by (simp only: measurable_cong_sets[OF sets_euclidean_centered_normal refl]; measurable)
  have joint: "distr (euclidean_centered_normal v :: (real^'d) measure)
      (PiM UNIV (λ_::'d. borel))
      (λx. restrict (λi. x $ i) UNIV) = ?N"
  proof -
    have "distr (euclidean_centered_normal v :: (real^'d) measure)
        (PiM UNIV (λ_::'d. borel))
        (λx. restrict (λi. x $ i) UNIV) =
      distr ?N (PiM UNIV (λ_::'d. borel))
        ((λx. restrict (λi. x $ i) UNIV) ∘ ?V)"
      unfolding euclidean_centered_normal_def
      by (rule distr_distr) (measurable, rule Vmeas)
    also have "... = distr ?N ?N id"
    proof (rule distr_cong)
      show "?N = ?N" by simp
      show "sets (PiM UNIV (λ_::'d. borel)) = sets ?N"
        by (rule sets_PiM_cong) auto
      show "⋀x. x ∈ space ?N ⟹
        ((λx. restrict (λi. x $ i) UNIV) ∘ ?V) x = id x"
        by (auto simp: space_PiM fun_eq_iff)
    qed
    also have "... = ?N"
      by (simp only: id_def distr_id)
    finally show ?thesis .
  qed
  show ?thesis
  proof (rule prob_space.indep_vars_iff_distr_eq_PiM'[OF psE, THEN iffD2])
    show "UNIV ≠ ({}::'d set)" by simp
    show "⋀i. i ∈ UNIV ⟹
      (λx::real^'d. x $ i) ∈ borel_measurable (euclidean_centered_normal v)"
      by (simp only: measurable_cong_sets[OF sets_euclidean_centered_normal refl]; measurable)
    show "distr (euclidean_centered_normal v :: (real^'d) measure)
        (PiM UNIV (λ_::'d. borel))
        (λx. restrict (λi. x $ i) UNIV) =
      PiM UNIV (λi. distr (euclidean_centered_normal v) borel
        (λx. x $ i))"
      unfolding joint
      by (rule PiM_cong) (auto simp: euclidean_centered_normal_coordinate[OF assms])
  qed
qed

lemma euclidean_centered_normal_zero:
  "euclidean_centered_normal 0 = return borel (0 :: real^'d::finite)"
  unfolding euclidean_centered_normal_def centered_normal_zero
  apply (subst PiM_return[where a="λ_. 0"])
    apply simp
   apply measurable
  apply (subst distr_return)
   apply (rule borel_measurable_vec_lambda)
   apply measurable
   apply (auto simp: space_PiM)[1]
  apply (rule arg_cong[where f="return borel"])
  by (simp add: vec_eq_iff)

subsection ‹Independent families on a finite product›

lemma indep_vars_PiM_finite_family:
  fixes I :: "'i set" and J :: "'j set"
    and M :: "'i ⇒ 'a measure" and N :: "'i ⇒ 'j ⇒ 'b measure"
    and X :: "'i ⇒ 'j ⇒ 'a ⇒ 'b"
  assumes finite_I: "finite I" and finite_J: "finite J"
    and nonempty_I: "I ≠ {}" and nonempty_J: "J ≠ {}"
    and ps_M: "⋀i. i ∈ I ⟹ prob_space (M i)"
    and indep: "⋀i. i ∈ I ⟹
      prob_space.indep_vars (M i) (N i) (X i) J"
  shows "prob_space.indep_vars (PiM I M) (λ(i,j). N i j)
    (λ(i,j) w. X i j (w i)) (I × J)"
proof -
  let ?S = "PiM I M"
  let ?K = "I × J"
  let ?N = "λ(i,j). N i j"
  let ?Q = "λp. if p ∈ ?K then
    (case p of (i,j) ⇒ distr (M i) (N i j) (X i j))
    else return (count_space UNIV) undefined"
  let ?Y = "λ(i,j) w. X i j (w i)"
  have psS: "prob_space ?S"
    by (rule prob_space_PiM) (simp add: ps_M)
  have meas_X: "X i j ∈ measurable (M i) (N i j)"
    if "i ∈ I" "j ∈ J" for i j
    using indep[OF that(1)] that(2)
      prob_space.indep_vars_def2[OF ps_M[OF that(1)]] by blast
  have psQ: "⋀p. prob_space (?Q p)"
  proof -
    fix p
    show "prob_space (?Q p)"
      by (cases p) (auto intro!: prob_space.prob_space_distr prob_space_return ps_M meas_X)
  qed
  have meas: "⋀p. p ∈ ?K ⟹ ?Y p ∈ measurable ?S (?N p)"
    using meas_X by (auto intro!: measurable_PiM_component_rev)
  have sets_Q: "sets (?Q (i,j)) = sets (N i j)"
    if "i ∈ I" "j ∈ J" for i j
    using meas_X[OF that] by (simp add: that)
  have space_Q: "space (?Q (i,j)) = space (N i j)"
    if "i ∈ I" "j ∈ J" for i j
    by (rule sets_eq_imp_space_eq[OF sets_Q[OF that]])
  have joint: "distr ?S (PiM ?K ?N)
      (λw. restrict (λp. ?Y p w) ?K) = PiM ?K ?Q"
  proof -
    interpret K: product_prob_space ?Q ?K
      by (rule product_prob_spaceI) (rule psQ)
    show ?thesis
    proof (rule K.PiM_eq)
      show "sets (distr ?S (PiM ?K ?N)
          (λw. restrict (λp. ?Y p w) ?K)) = sets (PiM ?K ?Q)"
      proof -
        have "sets (PiM ?K ?N) = sets (PiM ?K ?Q)"
          by (rule sets_PiM_cong) (auto simp: meas_X)
        then show ?thesis by simp
      qed
      fix L F
      assume L: "finite L" "L ⊆ ?K"
        and F: "⋀p. p ∈ L ⟹ F p ∈ sets (?Q p)"
      let ?L = "λi. {j ∈ J. (i,j) ∈ L}"
      let ?F = "λi. prod_emb J (λj. ?Q (i,j)) (?L i)
        (PiE (?L i) (λj. F (i,j)))"
      have F_eq: "?F i = prod_emb J (N i) (?L i)
          (PiE (?L i) (λj. F (i,j)))" if "i ∈ I" for i
        unfolding prod_emb_def
        apply (rule arg_cong2[where f="(∩)"])
         apply simp
        apply (rule set_eqI)
        using space_Q[OF that] by (auto simp: PiE_iff)
      have FL: "?F i ∈ sets (PiM J (N i))" if iI: "i ∈ I" for i
        unfolding F_eq[OF iI]
      proof (rule sets_PiM_I)
        show "finite (?L i)" using finite_J by auto
        show "?L i ⊆ J" using L F sets_Q iI by auto
        show "∀j∈?L i. F (i,j) ∈ sets (N i j)"
        proof (intro ballI)
          fix j
          assume jL: "j ∈ ?L i"
          have jJ: "j ∈ J" using jL by simp
          have "F (i,j) ∈ sets (?Q (i,j))"
            by (rule F) (use jL in auto)
          then show "F (i,j) ∈ sets (N i j)"
            using sets_Q[OF iI jJ] by simp
        qed
      qed
      have event_eq: "(λw. restrict (λp. ?Y p w) ?K) -`
          prod_emb ?K ?Q L (PiE L F) ∩ space ?S =
        prod_emb I M I (PiE I (λi.
          (λx. restrict (λj. X i j x) J) -` ?F i ∩ space (M i)))"
        apply (rule set_eqI)
        using L apply (auto simp: prod_emb_iff PiE_iff restrict_def
          space_PiM extensional_def)
        done
      have F_N: "F p ∈ sets (?N p)" if "p ∈ L" for p
      proof -
        obtain i j where p: "p = (i,j)" by (cases p)
        have iI: "i ∈ I" and jJ: "j ∈ J"
          using L that p by auto
        have Fij: "F (i,j) ∈ sets (?Q (i,j))"
          by (rule F) (use that p in simp)
        show ?thesis
          unfolding p using Fij sets_Q[OF iI jJ] by simp
      qed
      have emb_eq: "prod_emb ?K ?Q L (PiE L F) =
          prod_emb ?K ?N L (PiE L F)"
        unfolding prod_emb_def
        apply (rule arg_cong2[where f="(∩)"])
         apply simp
        apply (rule set_eqI)
        using space_Q by (auto simp: PiE_iff)
      have emb_sets: "prod_emb ?K ?Q L (PiE L F) ∈ sets (PiM ?K ?N)"
        unfolding emb_eq
        apply (rule measurable_prod_emb[OF L(2)])
        apply (rule sets_PiM_I_finite[OF L(1)])
        using F_N by auto
      have factor: "emeasure (M i)
          ((λx. restrict (λj. X i j x) J) -` ?F i ∩ space (M i)) =
          (∏j∈?L i. emeasure (?Q (i,j)) (F (i,j)))"
        if "i ∈ I" for i
      proof -
        have d: "distr (M i) (PiM J (N i))
            (λx. restrict (λj. X i j x) J) =
            PiM J (λj. distr (M i) (N i j) (X i j))"
        proof (rule prob_space.indep_vars_iff_distr_eq_PiM'
            [OF ps_M[OF that] nonempty_J, THEN iffD1])
          show "⋀j. j ∈ J ⟹ X i j ∈ measurable (M i) (N i j)"
            by (rule meas_X[OF that])
          show "prob_space.indep_vars (M i) (N i) (X i) J"
            by (rule indep[OF that])
        qed
        have m: "(λx. restrict (λj. X i j x) J) ∈
            measurable (M i) (PiM J (N i))"
          using meas_X[OF that] by measurable
        have product_eq: "PiM J (λj. ?Q (i,j)) =
            PiM J (λj. distr (M i) (N i j) (X i j))"
          by (rule PiM_cong) (auto simp: that)
        have "emeasure (M i)
            ((λx. restrict (λj. X i j x) J) -` ?F i ∩ space (M i)) =
            emeasure (distr (M i) (PiM J (N i))
              (λx. restrict (λj. X i j x) J)) (?F i)"
          using FL[OF that] m by (subst emeasure_distr) auto
        also have "... = emeasure (PiM J
            (λj. distr (M i) (N i j) (X i j))) (?F i)"
          by (simp only: d)
        also have "... = emeasure (PiM J (λj. ?Q (i,j))) (?F i)"
          using arg_cong[OF product_eq, of "λR. emeasure R (?F i)"] by simp
        finally have "emeasure (M i)
            ((λx. restrict (λj. X i j x) J) -` ?F i ∩ space (M i)) =
            emeasure (PiM J (λj. ?Q (i,j))) (?F i)" .
        also have "... = (∏j∈?L i. emeasure (?Q (i,j)) (F (i,j)))"
          apply (rule emeasure_PiM_emb)
            apply (rule psQ)
           apply (use finite_J in auto)
          using F sets_Q that by blast
        finally show ?thesis .
      qed
      have event_sets: "(λx. restrict (λj. X i j x) J) -` ?F i ∩
          space (M i) ∈ sets (M i)" if iI: "i ∈ I" for i
      proof -
        have m: "(λx. restrict (λj. X i j x) J) ∈
            measurable (M i) (PiM J (N i))"
          using meas_X[OF iI] by measurable
        show ?thesis by (rule measurable_sets[OF m FL[OF iI]])
      qed
      have "emeasure (distr ?S (PiM ?K ?N)
          (λw. restrict (λp. ?Y p w) ?K))
          (prod_emb ?K ?Q L (PiE L F)) =
        emeasure ?S (prod_emb I M I
          (PiE I (λi. (λx. restrict (λj. X i j x) J) -` ?F i ∩
            space (M i))))"
        using L F meas event_eq emb_sets by (subst emeasure_distr) (measurable, auto)
      also have "... = (∏i∈I. emeasure (M i)
          ((λx. restrict (λj. X i j x) J) -` ?F i ∩ space (M i)))"
        apply (rule emeasure_PiM_emb)
          apply (rule ps_M, assumption)
         apply simp
        apply (rule finite_I)
        by (rule event_sets, assumption)
      also have "... = (∏i∈I. ∏j∈?L i.
          emeasure (?Q (i,j)) (F (i,j)))"
        apply (rule prod.cong)
         apply simp
        by (rule factor) assumption
      also have "... = (∏p∈L. emeasure (?Q p) (F p))"
        using finite_I finite_J L
        by (subst prod.Sigma) (auto intro!: prod.cong)
      finally show "emeasure (distr ?S (PiM ?K ?N)
          (λw. restrict (λp. ?Y p w) ?K))
          (prod_emb ?K ?Q L (PiE L F)) =
        (∏p∈L. emeasure (?Q p) (F p))" .
    qed
  qed
  have marginal: "distr ?S (N i j) (λw. X i j (w i)) =
      distr (M i) (N i j) (X i j)" if iI: "i ∈ I" and jJ: "j ∈ J"
    for i j
  proof -
    have xm: "X i j ∈ measurable (M i) (N i j)"
      by (rule meas_X[OF iI jJ])
    have cm: "(λw. w i) ∈ measurable ?S (M i)"
      by (rule measurable_PiM_component_rev[OF iI]) measurable
    have component: "distr ?S (M i) (λw. w i) = M i"
      by (rule distr_PiM_component[OF ps_M iI])
    have "distr ?S (N i j) (λw. X i j (w i)) =
        distr (distr ?S (M i) (λw. w i)) (N i j) (X i j)"
      using distr_distr[OF xm cm] by (simp add: comp_def)
    also have "... = distr (M i) (N i j) (X i j)"
      by (simp only: component)
    finally show ?thesis .
  qed
  show ?thesis
  proof (rule prob_space.indep_vars_iff_distr_eq_PiM'
      [OF psS, THEN iffD2])
    show "I × J ≠ {}" using nonempty_I nonempty_J by auto
    show "⋀p. p ∈ I × J ⟹ ?Y p ∈ measurable ?S (?N p)"
      by (rule meas)
    show "distr ?S (PiM ?K ?N)
        (λw. restrict (λp. ?Y p w) ?K) =
      PiM ?K (λp. distr ?S (?N p) (?Y p))"
      unfolding joint
      apply (rule PiM_cong)
       apply simp
      using marginal by auto
  qed
qed

subsection ‹The Brownian-motion specification›

locale euclidean_brownian_motion =
  fixes W :: "(real, 'a, real^'d::finite) stochastic_process"
  assumes index: "proc_index W = {0..}"
    and init_0: "AE w in proc_source W. process W 0 w = 0"
    and indep_increments: "indep_increments W"
    and normal_increments: "⋀s t. 0 ≤ s ⟹ s ≤ t ⟹
      distr (proc_source W) borel
        (λw. process W t w - process W s w) =
      euclidean_centered_normal (t - s)"
    and AE_continuous: "AE w in proc_source W.
      continuous_on {0..} (λt. process W t w)"

subsection ‹Basic Brownian properties›

lemma euclidean_brownian_process_starts_at_zero:
  "AE w in proc_source (euclidean_brownian_process ::
      (real, 'd::finite ⇒ real ⇒ real, real^'d) stochastic_process).
    process euclidean_brownian_process 0 w = 0"
proof -
  have coordinate: "AE w in (euclidean_brownian_source ::
      ('d ⇒ real ⇒ real) measure).
      process brownian_process 0 (w i) = 0" for i
    unfolding euclidean_brownian_source_def
    by (rule AE_PiM_component) (auto simp: proc_source.prob_space_axioms
        brownian_process_starts_at_zero)
  have "AE w in (euclidean_brownian_source ::
      ('d ⇒ real ⇒ real) measure).
      ∀i. process brownian_process 0 (w i) = 0"
    apply (simp only: AE_all_countable)
    by (intro allI, rule coordinate)
  show ?thesis
    apply (simp only: euclidean_brownian_process_source)
    using ‹AE w in euclidean_brownian_source.
      ∀i. process brownian_process 0 (w i) = 0›
    by eventually_elim (simp add: vec_eq_iff)
qed

lemma euclidean_brownian_increment_product_law:
  fixes s t :: real
  assumes s: "0 ≤ s" and st: "s ≤ t"
  defines "X ≡ λ(_::'d::finite) p.
    process brownian_process t p - process brownian_process s p"
  shows "distr (euclidean_brownian_source ::
      ('d ⇒ real ⇒ real) measure)
      (PiM UNIV (λ_::'d. centered_normal (t - s)))
      (λw. restrict (λi. X i (w i)) UNIV) =
    PiM UNIV (λ_::'d. centered_normal (t - s))"
proof -
  let ?S = "λ_::'d. proc_source brownian_process"
  let ?N = "λ_::'d. centered_normal (t - s)"
  have source: "product_prob_space ?S"
    by (rule product_prob_spaceI) (simp add: proc_source.prob_space_axioms)
  have target: "product_prob_space ?N"
    by (rule product_prob_spaceI) (simp add: prob_space_centered_normal)
  have target_sets: "sets (proc_target brownian_process) = sets borel"
    using compatible_target[OF modificationD(1)[OF brownian_process_modification]]
    by (simp add: gaussian_process_target)
  have process_meas: "process brownian_process u ∈
      borel_measurable (proc_source brownian_process)" for u
    using stochastic_process_measurable[of brownian_process u]
    by (simp only: measurable_cong_sets[OF refl target_sets])
  have meas: "X i ∈ measurable (?S i) (?N i)" for i
  proof -
    have "X i ∈ borel_measurable (proc_source brownian_process)"
      unfolding X_def by (measurable; rule process_meas)
    then show ?thesis
      by (simp only: measurable_cong_sets[OF refl sets_centered_normal])
  qed
  have law: "distr (?S i) (?N i) (X i) = ?N i" for i
  proof -
    have "distr (proc_source brownian_process) borel (X i) =
        centered_normal (t - s)"
      unfolding X_def by (rule brownian_process_increment_law[OF s st])
    moreover have "distr (proc_source brownian_process) (?N i) (X i) =
        distr (proc_source brownian_process) borel (X i)"
      by (rule distr_cong) auto
    ultimately show ?thesis by simp
  qed
  have "distr (PiM UNIV ?S) (PiM UNIV ?N)
      (compose UNIV (X undefined)) =
    PiM UNIV (λi. distr (?S i) (?N i) (X undefined))"
    apply (rule distr_PiM_finite_prob_space)
       apply simp
      apply (rule source)
     apply (rule target)
    by (rule meas)
  also have "... = PiM UNIV ?N"
    apply (rule PiM_cong)
     apply simp
    using law by (simp add: X_def)
  finally show ?thesis
    unfolding euclidean_brownian_source_def compose_def X_def by simp
qed

lemma euclidean_brownian_process_increment_law:
  assumes s: "0 ≤ s" and st: "s ≤ t"
  shows "distr (proc_source (euclidean_brownian_process ::
      (real, 'd::finite ⇒ real ⇒ real, real^'d) stochastic_process)) borel
      (λw. process euclidean_brownian_process t w -
        process euclidean_brownian_process s w) =
    euclidean_centered_normal (t - s)"
proof -
  let ?S = "euclidean_brownian_source :: ('d ⇒ real ⇒ real) measure"
  let ?N = "PiM UNIV (λ_::'d. centered_normal (t - s))"
  let ?I = "λw i. process brownian_process t (w i) -
    process brownian_process s (w i)"
  let ?V = "λx. χ i. x i :: real^'d"
  have ps: "prob_space ?S" by (rule prob_space_euclidean_brownian_source)
  have nps: "prob_space ?N"
    by (rule prob_space_PiM) (simp add: prob_space_centered_normal)
  have target_sets: "sets (proc_target brownian_process) = sets borel"
    using compatible_target[OF modificationD(1)[OF brownian_process_modification]]
    by (simp add: gaussian_process_target)
  have scalar_meas: "(λp. process brownian_process t p -
      process brownian_process s p) ∈
      borel_measurable (proc_source brownian_process)"
    using stochastic_process_measurable[of brownian_process t]
      stochastic_process_measurable[of brownian_process s]
    by (simp only: measurable_cong_sets[OF refl target_sets]) measurable
  have coord_meas: "(λw. ?I w i) ∈
      measurable ?S (centered_normal (t - s))" for i
  proof -
    have "(λw. ?I w i) ∈ borel_measurable ?S"
      unfolding euclidean_brownian_source_def
      by (rule measurable_PiM_component_rev) (simp_all add: scalar_meas)
    then show ?thesis
      by (simp only: measurable_cong_sets[OF refl sets_centered_normal])
  qed
  have Imeas: "(λw. restrict (?I w) UNIV) ∈ measurable ?S ?N"
    apply (rule measurable_PiM_single')
     apply (simp add: coord_meas)
    using measurable_space[OF coord_meas] by (auto simp: space_PiM)
  have Vmeas: "?V ∈ borel_measurable ?N"
    apply (rule borel_measurable_vec_lambda)
    apply (rule measurable_PiM_component_rev)
     apply simp
    apply (rule measurable_ident_sets)
    by simp
  have inc: "distr ?S ?N (λw. restrict (?I w) UNIV) = ?N"
    by (rule euclidean_brownian_increment_product_law[OF s st, unfolded Let_def])
  have "distr ?S borel (λw. ?V (restrict (?I w) UNIV)) =
      distr (distr ?S ?N (λw. restrict (?I w) UNIV)) borel ?V"
    using distr_distr[OF Vmeas Imeas, symmetric]
    by (simp only: comp_def)
  also have "... = euclidean_centered_normal (t - s)"
    unfolding euclidean_centered_normal_def inc by simp
  finally have raw_law: "distr ?S borel
      (λw. ?V (restrict (?I w) UNIV)) =
      euclidean_centered_normal (t - s)" .
  have coord_eq: "(λw. ?V (restrict (?I w) UNIV)) =
      (λw. χ i. process brownian_process t (w i) -
        process brownian_process s (w i))"
    by (simp add: fun_eq_iff vec_eq_iff)
  have law: "distr ?S borel
      (λw. χ i. process brownian_process t (w i) -
        process brownian_process s (w i)) =
      euclidean_centered_normal (t - s)"
    using raw_law by (simp only: coord_eq)
  have map_eq: "(λw. process (euclidean_brownian_process ::
      (real, 'd ⇒ real ⇒ real, real^'d) stochastic_process) t w -
      process euclidean_brownian_process s w) =
    (λw. χ i. process brownian_process t (w i) -
      process brownian_process s (w i))"
    using s st by (simp add: fun_eq_iff vec_eq_iff)
  show ?thesis
    by (simp only: euclidean_brownian_process_source map_eq law)
qed

lemma euclidean_brownian_process_local_holder:
  "local_holder_on (1/8) {0..}
    (λt. process (euclidean_brownian_process ::
      (real, 'd::finite ⇒ real ⇒ real, real^'d) stochastic_process) t w)"
proof -
  let ?g = "(1/8 :: real)"
  have scalar: "local_holder_on ?g {0..}
      (λt. process brownian_process t (w i))" for i
    by (rule brownian_process_local_holder)
  have data: "∃e C. 0 < e ∧ 0 ≤ C ∧
      (∀r∈{0..}. ∀s∈{0..}.
        dist s t < e ∧ dist r t < e ⟶
        dist (process brownian_process r (w i))
          (process brownian_process s (w i)) ≤ C * dist r s powr ?g)"
    if "t ∈ {0..}" for i t
    using scalar that unfolding local_holder_on_def by blast
  show ?thesis
  proof (unfold local_holder_on_def, intro conjI ballI)
    show "?g ∈ {0<..1}" by simp
    fix t :: real
    assume t: "t ∈ {0..}"
    obtain e C where e: "⋀i. 0 < e i" and C: "⋀i. 0 ≤ C i"
      and bound: "⋀i r s. r ∈ {0..} ⟹ s ∈ {0..} ⟹
        dist s t < e i ⟹ dist r t < e i ⟹
        dist (process brownian_process r (w i))
          (process brownian_process s (w i)) ≤ C i * dist r s powr ?g"
      using data[OF t] by metis
    let ?e = "Min (e ` (UNIV::'d set))"
    let ?C = "sum C (UNIV::'d set)"
    have epos: "0 < ?e"
      by (rule Min_gr_iff[THEN iffD2]) (auto simp: e)
    have e_le: "?e ≤ e i" for i
      by (rule Min_le) auto
    have Cnonneg: "0 ≤ ?C" by (simp add: sum_nonneg C)
    show "∃e>0. ∃K≥0. ∀r∈{0..}. ∀s∈{0..}.
        dist s t < e ∧ dist r t < e ⟶
        dist (process euclidean_brownian_process r w)
          (process euclidean_brownian_process s w) ≤ K * dist r s powr ?g"
    proof (intro exI conjI ballI impI)
      show "0 < ?e" by (rule epos)
      show "0 ≤ ?C" by (rule Cnonneg)
      fix r s assume r: "r ∈ {0..}" and s: "s ∈ {0..}"
        and near: "dist s t < ?e ∧ dist r t < ?e"
      have component: "dist (process brownian_process r (w i))
          (process brownian_process s (w i)) ≤
          C i * dist r s powr ?g" for i
        by (rule bound[OF r s]) (use near e_le[of i] in linarith)+
      have "dist (process euclidean_brownian_process r w)
          (process euclidean_brownian_process s w) ≤
          (∑i∈(UNIV::'d set). dist (process brownian_process r (w i))
            (process brownian_process s (w i)))"
        using r s unfolding dist_vec_def
        by (simp add: L2_set_le_sum)
      also have "... ≤ (∑i∈(UNIV::'d set). C i * dist r s powr ?g)"
        by (rule sum_mono) (rule component)
      also have "... = ?C * dist r s powr ?g"
        by (simp add: sum_distrib_right)
      finally show "dist (process euclidean_brownian_process r w)
          (process euclidean_brownian_process s w) ≤
          ?C * dist r s powr ?g" .
    qed
  qed
qed

lemma euclidean_brownian_process_indep_increments:
  "indep_increments (euclidean_brownian_process ::
    (real, 'd::finite ⇒ real ⇒ real, real^'d) stochastic_process)"
proof (rule indep_incrementsI)
  fix l :: "real list"
  assume times: "set l ⊆ proc_index (euclidean_brownian_process ::
      (real, 'd ⇒ real ⇒ real, real^'d) stochastic_process)"
    and sorted: "sorted l" and length: "2 ≤ length l"
  let ?J = "{1..<length l}"
  let ?D = "λ(i,k) p. process brownian_process (l ! k) p -
    process brownian_process (l ! (k - 1)) p"
  have scalar: "prob_space.indep_vars (proc_source brownian_process)
      (λ_. borel) (λk p. ?D (i,k) p) ?J" for i :: 'd
  proof -
    have times_scalar: "set l ⊆ proc_index brownian_process"
      using times constructed_brownian_motion.index by simp
    have iv0: "prob_space.indep_vars (proc_source brownian_process)
        (λ_. proc_target brownian_process)
        (λk p. process brownian_process (l ! k) p -
          process brownian_process (l ! (k - 1)) p) ?J"
      by (rule indep_incrementsE[OF brownian_process_indep_increments])
         (use times_scalar sorted length in auto)
    have iv: "prob_space.indep_vars (proc_source brownian_process)
        (λ_. proc_target brownian_process) (λk p. ?D (i,k) p) ?J"
      using iv0 by simp
    have target_sets: "sets (proc_target brownian_process) = sets borel"
      using compatible_target[OF modificationD(1)[OF brownian_process_modification]]
      by (simp add: gaussian_process_target)
    show ?thesis
      using iv
      unfolding prob_space.indep_vars_def2[OF proc_source.prob_space_axioms]
      by (simp only: measurable_cong_sets[OF refl target_sets] target_sets; blast)
  qed
  have flat0: "prob_space.indep_vars (euclidean_brownian_source ::
      ('d ⇒ real ⇒ real) measure) (λ_. borel)
      (λ(i,k) w. process brownian_process (l ! k) (w i) -
        process brownian_process (l ! (k - 1)) (w i)) (UNIV × ?J)"
  proof -
    let ?B = "λ(_::'d) (_::nat). borel"
    let ?E = "λ(_::'d) k p. process brownian_process (l ! k) p -
      process brownian_process (l ! (k - 1)) p"
    have raw: "prob_space.indep_vars
        (PiM (UNIV::'d set) (λ_. proc_source brownian_process))
        (λ(i,k). ?B i k) (λ(i,k) w. ?E i k (w i))
        (UNIV × ?J)"
      apply (rule indep_vars_PiM_finite_family)
           apply simp
          apply simp
         apply simp
        apply (use length in auto)
       apply (simp add: proc_source.prob_space_axioms)
      using scalar by simp
    have B_eq: "(λ(i,k). ?B i k) = (λ_. borel)"
      by (rule ext) (auto split: prod.splits)
    show ?thesis
      unfolding euclidean_brownian_source_def
      using raw by (simp only: B_eq)
  qed
  have flat: "prob_space.indep_vars (euclidean_brownian_source ::
      ('d ⇒ real ⇒ real) measure) (λ_. borel)
      (λ(i,k) w. ?D (i,k) (w i)) (UNIV × ?J)"
    using flat0 by simp
  let ?K = "λk. UNIV × {k} :: ('d × nat) set"
  have grouped: "prob_space.indep_vars (euclidean_brownian_source ::
      ('d ⇒ real ⇒ real) measure)
      (λk. PiM (?K k) (λ_. borel))
      (λk w. restrict (λp. ?D p (w (fst p))) (?K k)) ?J"
  proof -
    let ?Z = "λ(i,k) w. ?D (i,k) (w i)"
    have raw: "prob_space.indep_vars (euclidean_brownian_source ::
        ('d ⇒ real ⇒ real) measure)
        (λk. PiM (?K k) (λ_. borel))
        (λk w. restrict (λp. ?Z p w) (?K k)) ?J"
    proof (rule prob_space.indep_vars_restrict[OF
          euclidean_source.prob_space_axioms flat])
      show "?K k ⊆ UNIV × ?J" if "k ∈ ?J" for k
        using that by auto
      show "disjoint_family_on ?K ?J"
        by (auto simp: disjoint_family_on_def)
    qed
    have map_eq: "(λw. restrict (λp. ?Z p w) (?K k)) =
        (λw. restrict (λp. ?D p (w (fst p))) (?K k))" for k
      by (rule ext, rule ext) (auto split: prod.splits)
    have transfer: "prob_space.indep_vars (euclidean_brownian_source ::
        ('d ⇒ real ⇒ real) measure)
        (λk. PiM (?K k) (λ_. borel))
        (λk w. restrict (λp. ?Z p w) (?K k)) ?J =
      prob_space.indep_vars euclidean_brownian_source
        (λk. PiM (?K k) (λ_. borel))
        (λk w. restrict (λp. ?D p (w (fst p))) (?K k)) ?J"
    proof (rule euclidean_source.indep_vars_cong)
      show "?J = ?J" by simp
      show "⋀k. k ∈ ?J ⟹
        (λw. restrict (λp. ?Z p w) (?K k)) =
        (λw. restrict (λp. ?D p (w (fst p))) (?K k))"
        by (rule map_eq)
      show "⋀k. k ∈ ?J ⟹
        PiM (?K k) (λ_. borel) = PiM (?K k) (λ_. borel)"
        by simp
    qed
    show ?thesis using raw transfer by blast
  qed
  have vec_meas: "(λf. (χ i. f (i,k)) :: real^'d) ∈
      measurable (PiM (?K k) (λ_. borel)) borel" if kJ: "k ∈ ?J" for k
  proof -
    have coord: "(λf. f (i,k)) ∈
        borel_measurable (PiM (?K k) (λ_. borel))" for i
      by (rule measurable_PiM_component_rev) (use kJ in auto)
    show ?thesis
      by (rule borel_measurable_vec_lambda[where f="λx i. x (i,k)"])
         (rule coord)
  qed
  have vectors: "prob_space.indep_vars (euclidean_brownian_source ::
      ('d ⇒ real ⇒ real) measure) (λ_. borel)
      (λk w. χ i. ?D (i,k) (w i)) ?J"
    using euclidean_source.indep_vars_compose2[OF grouped vec_meas]
    by (simp add: fun_eq_iff)
  have nonneg: "0 ≤ l ! n" if "n < length l" for n
    using times nth_mem[OF that] by auto
  have map_eq: "(λw. process (euclidean_brownian_process ::
      (real, 'd ⇒ real ⇒ real, real^'d) stochastic_process)
        (l ! k) w - process euclidean_brownian_process (l ! (k - 1)) w) =
    (λw. χ i. ?D (i,k) (w i))" if kJ: "k ∈ ?J" for k
  proof -
    have klen: "k < length l" and km1len: "k - 1 < length l"
      using kJ by auto
    show ?thesis
      apply (rule ext)
      using nonneg[OF klen] nonneg[OF km1len]
      by (simp add: vec_eq_iff)
  qed
  have desired: "prob_space.indep_vars (euclidean_brownian_source ::
      ('d ⇒ real ⇒ real) measure) (λ_. borel)
      (λk w. process (euclidean_brownian_process ::
        (real, 'd ⇒ real ⇒ real, real^'d) stochastic_process)
          (l ! k) w - process euclidean_brownian_process (l ! (k - 1)) w) ?J"
  proof -
    have transfer: "prob_space.indep_vars (euclidean_brownian_source ::
        ('d ⇒ real ⇒ real) measure) (λ_. borel)
        (λk w. χ i. ?D (i,k) (w i)) ?J =
      prob_space.indep_vars euclidean_brownian_source (λ_. borel)
        (λk w. process (euclidean_brownian_process ::
          (real, 'd ⇒ real ⇒ real, real^'d) stochastic_process)
            (l ! k) w - process euclidean_brownian_process (l ! (k - 1)) w) ?J"
    proof (rule euclidean_source.indep_vars_cong)
      show "?J = ?J" by simp
      show "⋀k. k ∈ ?J ⟹
        (λw. χ i. ?D (i,k) (w i)) =
        (λw. process (euclidean_brownian_process ::
          (real, 'd ⇒ real ⇒ real, real^'d) stochastic_process)
            (l ! k) w - process euclidean_brownian_process (l ! (k - 1)) w)"
        by (rule sym, rule map_eq) assumption
      show "⋀k. k ∈ ?J ⟹ borel = borel" by simp
    qed
    show ?thesis by (rule transfer[THEN iffD1, OF vectors])
  qed
  show "prob_space.indep_vars
      (proc_source (euclidean_brownian_process ::
        (real, 'd ⇒ real ⇒ real, real^'d) stochastic_process))
      (λ_. proc_target euclidean_brownian_process)
      (λk w. process euclidean_brownian_process (l ! k) w -
        process euclidean_brownian_process (l ! (k - 1)) w) ?J"
    using desired by simp
qed

lemma euclidean_brownian_process_continuous:
  "continuous_on {0..}
    (λt. process (euclidean_brownian_process ::
      (real, 'd::finite ⇒ real ⇒ real, real^'d) stochastic_process) t w)"
  using euclidean_brownian_process_local_holder local_holder_imp_continuous by blast

interpretation constructed_euclidean_brownian_motion: euclidean_brownian_motion
  "euclidean_brownian_process ::
    (real, 'd::finite ⇒ real ⇒ real, real^'d) stochastic_process"
proof
  show "proc_index (euclidean_brownian_process ::
      (real, 'd ⇒ real ⇒ real, real^'d) stochastic_process) = {0..}"
    by simp
  show "AE w in proc_source (euclidean_brownian_process ::
      (real, 'd ⇒ real ⇒ real, real^'d) stochastic_process).
      process euclidean_brownian_process 0 w = 0"
    by (rule euclidean_brownian_process_starts_at_zero)
  show "indep_increments (euclidean_brownian_process ::
      (real, 'd ⇒ real ⇒ real, real^'d) stochastic_process)"
    by (rule euclidean_brownian_process_indep_increments)
  show "distr (proc_source (euclidean_brownian_process ::
      (real, 'd ⇒ real ⇒ real, real^'d) stochastic_process)) borel
      (λw. process euclidean_brownian_process t w -
        process euclidean_brownian_process s w) = euclidean_centered_normal (t - s)"
    if "0 ≤ s" "s ≤ t" for s t
    by (rule euclidean_brownian_process_increment_law[OF that])
  show "AE w in proc_source (euclidean_brownian_process ::
      (real, 'd ⇒ real ⇒ real, real^'d) stochastic_process).
      continuous_on {0..} (λt. process euclidean_brownian_process t w)"
    by (rule AE_I2) (rule euclidean_brownian_process_continuous)
qed

end