Theory Gaussian_Process

theory Gaussian_Process
  imports Gaussian_Finite_Dimensional "HOL-Probability.Projective_Limit"
begin

section ‹From consistent laws to a Gaussian process›

definition ordered_times :: "real set ⇒ real list" where
  "ordered_times T = sorted_list_of_set T"

definition time_index :: "real set ⇒ real ⇒ nat" where
  "time_index T t = (THE i. i < length (ordered_times T) ∧ ordered_times T ! i = t)"

lemma ordered_times_finite:
  assumes "finite T"
  shows "set (ordered_times T) = T" "distinct (ordered_times T)"
    "sorted (ordered_times T)" "length (ordered_times T) = card T"
  using assms by (simp_all add: ordered_times_def)

lemma time_index_unique:
  assumes finite: "finite T" and t: "t ∈ T"
  shows "time_index T t < length (ordered_times T)"
    "ordered_times T ! time_index T t = t"
proof -
  have membership: "t ∈ set (ordered_times T)"
    using ordered_times_finite[OF finite] t by simp
  then obtain i where i: "i < length (ordered_times T)"
    "ordered_times T ! i = t" by (auto simp: in_set_conv_nth)
  have unique: "j = i" if "j < length (ordered_times T)"
    "ordered_times T ! j = t" for j
    using ordered_times_finite(2)[OF finite] that i
    by (auto simp: distinct_conv_nth)
  have equivalence: "(j < length (ordered_times T) ∧ ordered_times T ! j = t) ⟷ j = i"
    for j using i unique by blast
  have index: "time_index T t = i"
    unfolding time_index_def by (simp only: equivalence; rule the_equality; simp)
  show "time_index T t < length (ordered_times T)"
    "ordered_times T ! time_index T t = t" using index i by auto
qed

lemma ordered_times_delete:
  assumes finite: "finite T" and t: "t ∈ T"
  shows "ordered_times (T - {t}) =
    delete_time (ordered_times T) (time_index T t)"
proof -
  let ?xs = "ordered_times T"
  let ?j = "time_index T t"
  have j: "?j < length ?xs" and nth: "?xs ! ?j = t"
    using time_index_unique[OF finite t] by auto
  have absent: "t ∉ set (take ?j ?xs)"
  proof
    assume "t ∈ set (take ?j ?xs)"
    then obtain k where k: "k < ?j" "?xs ! k = t"
      by (auto simp: in_set_conv_nth)
    have "k = ?j"
      using k nth j ordered_times_finite(2)[OF finite]
      by (auto simp: distinct_conv_nth)
    with k show False by simp
  qed
  have split: "?xs = take ?j ?xs @ t # drop (Suc ?j) ?xs"
    using id_take_nth_drop[OF j] nth by simp
  have remove: "remove1 t ?xs = delete_time ?xs ?j"
    by (subst split) (simp add: delete_time_def remove1_append absent)
  have "ordered_times (T - {t}) = remove1 t ?xs"
    using finite by (simp add: ordered_times_def sorted_list_of_set_remove)
  with remove show ?thesis by simp
qed

definition set_coordinates :: "real set ⇒ (nat ⇒ real) ⇒ real ⇒ real" where
  "set_coordinates T w = (λt∈T. w (time_index T t))"

lemma set_coordinates_delete:
  assumes finite: "finite T" and t: "t ∈ T"
  shows "set_coordinates (T - {t})
      (delete_coordinate (length (ordered_times T)) (time_index T t) w) =
    restrict (set_coordinates T w) (T - {t})"
proof (rule ext)
  fix s
  show "set_coordinates (T - {t})
      (delete_coordinate (length (ordered_times T)) (time_index T t) w) s =
    restrict (set_coordinates T w) (T - {t}) s"
  proof (cases "s ∈ T - {t}")
    case False
    have lhs: "set_coordinates (T - {t})
      (delete_coordinate (length (ordered_times T)) (time_index T t) w) s = undefined"
      using False by (simp only: set_coordinates_def restrict_def if_False)
    have rhs: "restrict (set_coordinates T w) (T - {t}) s = undefined"
      using False by (simp only: restrict_def if_False)
    show ?thesis using lhs rhs by simp
  next
    case True
    let ?xs = "ordered_times T"
    let ?ys = "ordered_times (T - {t})"
    let ?j = "time_index T t"
    let ?i = "time_index (T - {t}) s"
    let ?k = "if ?i < ?j then ?i else Suc ?i"
    have j: "?j < length ?xs" by (rule time_index_unique(1)[OF finite t])
    have i: "?i < length ?ys" and y: "?ys ! ?i = s"
      using time_index_unique[OF _ True] finite by auto
    have deletion: "?ys = delete_time ?xs ?j"
      by (rule ordered_times_delete[OF finite t])
    have len: "length ?ys = length ?xs - 1"
      by (simp add: deletion delete_time_length[OF j])
    have k: "?k < length ?xs" using i len by auto
    have xy: "?ys ! ?i = ?xs ! ?k"
      using delete_time_nth[OF j, of ?i] i deletion
      by (cases "?i < ?j") auto
    have x: "?xs ! ?k = s" using xy y by simp
    have s_index: "time_index T s < length ?xs" "?xs ! time_index T s = s"
      using time_index_unique[OF finite] True by auto
    have inj: "inj_on (nth ?xs) {..<length ?xs}"
      using ordered_times_finite(2)[OF finite]
      by (auto simp: distinct_conv_nth inj_on_def)
    have index: "time_index T s = ?k"
      by (rule inj_onD[OF inj]) (use s_index k x in auto)
    show ?thesis using True i len index
      by (simp add: set_coordinates_def delete_coordinate_def restrict_def)
  qed
qed

lemma ordered_times_set:
  assumes sorted: "sorted ts" and distinct: "distinct ts"
  shows "ordered_times (set ts) = ts"
  using assms by (simp add: ordered_times_def sorted_list_of_set_sort_remdups sorted_sort_id)

lemma time_index_nth:
  assumes sorted: "sorted ts" and distinct: "distinct ts" and i: "i < length ts"
  shows "time_index (set ts) (ts ! i) = i"
proof -
  have finite: "finite (set ts)" by simp
  have member: "ts ! i ∈ set ts" using i by simp
  have nth: "ordered_times (set ts) ! time_index (set ts) (ts ! i) = ts ! i"
    using time_index_unique(2)[OF finite member] .
  have bound: "time_index (set ts) (ts ! i) < length ts"
    using time_index_unique(1)[OF finite member]
      ordered_times_set[OF sorted distinct] by simp
  show ?thesis using distinct nth bound i ordered_times_set[OF sorted distinct]
    by (metis distinct_conv_nth)
qed

definition list_coordinates :: "real list ⇒ (real ⇒ real) ⇒ nat ⇒ real" where
  "list_coordinates ts w = (λi∈{..<length ts}. w (ts ! i))"

lemma measurable_list_coordinates:
  "list_coordinates ts ∈
    measurable (PiM (set ts) (λ_. borel))
      (PiM {..<length ts} (λ_. borel))"
proof (rule measurable_PiM_single')
  fix i assume i: "i ∈ {..<length ts}"
  have coord: "(λw :: real ⇒ real. w (ts ! i)) ∈
    borel_measurable (PiM (set ts) (λ_. borel))"
    by (rule measurable_component_singleton) (use i in auto)
  show "(λw. list_coordinates ts w i) ∈
    borel_measurable (PiM (set ts) (λ_. borel))"
    using i coord by (simp add: list_coordinates_def)
next
  show "list_coordinates ts ∈
    space (PiM (set ts) (λ_. borel)) →
      (ΠE i∈{..<length ts}. space borel)"
    by (auto simp: list_coordinates_def space_PiM PiE_iff split: if_splits)
qed

lemma list_coordinates_set_coordinates:
  assumes sorted: "sorted ts" and distinct: "distinct ts"
  shows "list_coordinates ts (set_coordinates (set ts) w) =
    restrict w {..<length ts}"
  using assms
  by (auto simp: list_coordinates_def set_coordinates_def restrict_def
    fun_eq_iff time_index_nth)

definition gaussian_set_law :: "real set ⇒ (real ⇒ real) measure" where
  "gaussian_set_law T =
    distr (gaussian_finite_law (ordered_times T)) (PiM T (λ_. borel))
      (set_coordinates T)"

lemma measurable_set_coordinates:
  assumes finite: "finite T"
  shows "set_coordinates T ∈
    measurable (PiM {..<length (ordered_times T)} (λ_. borel))
      (PiM T (λ_. borel))"
proof (rule measurable_PiM_single')
  fix t assume t: "t ∈ T"
  show "(λw. set_coordinates T w t) ∈
    borel_measurable (PiM {..<length (ordered_times T)} (λ_. borel))"
    using time_index_unique(1)[OF finite t]
    by (simp add: set_coordinates_def measurable_component_singleton t)
next
  show "set_coordinates T ∈
    space (PiM {..<length (ordered_times T)} (λ_. borel)) →
      (ΠE t∈T. space borel)"
    by (auto simp: set_coordinates_def space_PiM PiE_iff split: if_splits)
qed

lemma prob_space_gaussian_set_law:
  assumes "finite T"
  shows "prob_space (gaussian_set_law T)"
proof -
  have measurable: "set_coordinates T ∈
    measurable (gaussian_finite_law (ordered_times T)) (PiM T (λ_. borel))"
    using measurable_set_coordinates[OF assms]
    by (simp only: measurable_cong_sets[OF sets_gaussian_finite_law refl])
  show ?thesis unfolding gaussian_set_law_def
    by (rule prob_space.prob_space_distr[OF prob_space_gaussian_finite_law measurable])
qed

lemma sets_gaussian_set_law:
  "sets (gaussian_set_law T) = sets (PiM T (λ_. borel))"
  by (simp add: gaussian_set_law_def)

lemma gaussian_set_law_ordered_list:
  assumes sorted: "sorted ts" and distinct: "distinct ts"
  shows "distr (gaussian_set_law (set ts))
    (PiM {..<length ts} (λ_. borel)) (list_coordinates ts) =
    gaussian_finite_law ts"
proof -
  let ?I = "{..<length ts}"
  let ?B = "λ_. borel"
  let ?T = "set ts"
  have ordered: "ordered_times ?T = ts"
    by (rule ordered_times_set[OF sorted distinct])
  have set_meas: "set_coordinates ?T ∈
    measurable (gaussian_finite_law ts) (PiM ?T ?B)"
    using measurable_set_coordinates[of ?T]
    by (simp add: ordered measurable_cong_sets[OF sets_gaussian_finite_law refl])
  have list_meas: "list_coordinates ts ∈
    measurable (PiM ?T ?B) (PiM ?I ?B)"
    by (rule measurable_list_coordinates)
  have identity: "restrict w ?I = w" if "w ∈ space (gaussian_finite_law ts)" for w
  proof -
    have space: "space (gaussian_finite_law ts) = space (PiM ?I ?B)"
      by (rule sets_eq_imp_space_eq) (rule sets_gaussian_finite_law)
    show ?thesis using that by (auto simp: space space_PiM PiE_iff
      extensional_def restrict_def fun_eq_iff)
  qed
  have "distr (gaussian_set_law ?T) (PiM ?I ?B) (list_coordinates ts) =
    distr (gaussian_finite_law ts) (PiM ?I ?B)
      (list_coordinates ts ∘ set_coordinates ?T)"
    unfolding gaussian_set_law_def
    using distr_distr[OF list_meas set_meas] by (simp only: ordered)
  also have "... = distr (gaussian_finite_law ts) (PiM ?I ?B)
    (λw. restrict w ?I)"
    using list_coordinates_set_coordinates[OF sorted distinct]
    by (intro distr_cong) (auto simp: comp_def)
  also have "... = distr (gaussian_finite_law ts) (PiM ?I ?B) id"
    by (rule distr_cong) (use identity in auto)
  also have "... = gaussian_finite_law ts"
    by (simp add: id_def distr_id2)
  finally show ?thesis .
qed

lemma gaussian_set_law_delete:
  assumes finite: "finite T" and nonnegative: "T ⊆ {0..}"
    and t: "t ∈ T"
  shows "distr (gaussian_set_law T) (PiM (T - {t}) (λ_. borel))
    (λw. restrict w (T - {t})) = gaussian_set_law (T - {t})"
proof -
  let ?S = "T - {t}"
  let ?xs = "ordered_times T"
  let ?ys = "ordered_times ?S"
  let ?j = "time_index T t"
  let ?B = "λ_. borel"
  let ?del = "delete_coordinate (length ?xs) ?j"
  have j: "?j < length ?xs" by (rule time_index_unique(1)[OF finite t])
  have deletion: "?ys = delete_time ?xs ?j"
    by (rule ordered_times_delete[OF finite t])
  have ordered: "sorted ?xs" using ordered_times_finite(3)[OF finite] .
  have nonneg: "∀u∈set ?xs. 0 ≤ u"
    using nonnegative ordered_times_finite(1)[OF finite] by auto
  have finite_S: "finite ?S" using finite by simp
  have coord_meas: "set_coordinates T ∈
    measurable (gaussian_finite_law ?xs) (PiM T ?B)"
    using measurable_set_coordinates[OF finite]
    by (simp only: measurable_cong_sets[OF sets_gaussian_finite_law refl])
  have restrict_meas: "(λw. restrict w ?S) ∈
    measurable (PiM T ?B) (PiM ?S ?B)"
    by (rule measurable_restrict_subset) auto
  have del_meas: "?del ∈ measurable (gaussian_finite_law ?xs)
      (PiM {..<length ?ys} ?B)"
    using measurable_delete_coordinate[of ?j ?xs, OF j]
    by (simp only: deletion measurable_cong_sets[OF sets_gaussian_finite_law refl])
  have set_meas: "set_coordinates ?S ∈
    measurable (PiM {..<length ?ys} ?B) (PiM ?S ?B)"
    by (rule measurable_set_coordinates[OF finite_S])
  have "distr (gaussian_set_law T) (PiM ?S ?B) (λw. restrict w ?S) =
    distr (gaussian_finite_law ?xs) (PiM ?S ?B)
      ((λw. restrict w ?S) ∘ set_coordinates T)"
    unfolding gaussian_set_law_def
    by (rule distr_distr[OF restrict_meas coord_meas])
  also have "... = distr (gaussian_finite_law ?xs) (PiM ?S ?B)
    (set_coordinates ?S ∘ ?del)"
    using set_coordinates_delete[OF finite t]
    by (intro distr_cong) (auto simp: comp_def)
  also have "... = distr (distr (gaussian_finite_law ?xs)
      (PiM {..<length ?ys} ?B) ?del) (PiM ?S ?B) (set_coordinates ?S)"
    by (rule distr_distr[OF set_meas del_meas, symmetric])
  also have "... = gaussian_set_law ?S"
    using gaussian_finite_law_delete_time_any[OF ordered nonneg j]
    by (simp only: deletion gaussian_set_law_def)
  finally show ?thesis .
qed

lemma gaussian_set_law_consistent:
  assumes subset: "J ⊆ H" and finite: "finite H"
    and nonnegative: "H ⊆ {0..}"
  shows "gaussian_set_law J =
    distr (gaussian_set_law H) (PiM J (λ_. borel)) (λw. restrict w J)"
proof -
  let ?B = "λ_. borel"
  have finite_J: "finite J" by (rule finite_subset[OF subset finite])
  have J_nonnegative: "J ⊆ {0..}" using subset nonnegative by auto
  have consistent: "distr (gaussian_set_law (J ∪ K)) (PiM J ?B)
      (λw. restrict w J) = gaussian_set_law J"
    if finK: "finite K" and nonnegK: "K ⊆ {0..}" and disjK: "K ∩ J = {}"
    for K
    using finK nonnegK disjK
  proof (induction K rule: finite_induct)
    case empty
    have space: "space (gaussian_set_law J) = space (PiM J ?B)"
      using sets_gaussian_set_law by (rule sets_eq_imp_space_eq)
    have identity: "restrict w J = w" if "w ∈ space (gaussian_set_law J)" for w
      using that by (auto simp: space space_PiM PiE_iff extensional_def
        restrict_def fun_eq_iff)
    have "distr (gaussian_set_law J) (PiM J ?B) (λw. restrict w J) =
      distr (gaussian_set_law J) (PiM J ?B) id"
      by (rule distr_cong) (use identity in auto)
    also have "... = gaussian_set_law J"
      by (simp add: id_def distr_id2 sets_gaussian_set_law)
    finally show ?case by (simp only: Un_empty_right)
  next
    case (insert t K)
    let ?M = "J ∪ K"
    let ?L = "J ∪ insert t K"
    have t: "t ∉ ?M" using insert by auto
    have nonnegM: "?M ⊆ {0..}" using insert J_nonnegative by auto
    have nonnegL: "?L ⊆ {0..}" using insert J_nonnegative by auto
    have finiteL: "finite ?L" using insert finite_J by simp
    have del: "?L - {t} = ?M" using t by auto
    have one: "distr (gaussian_set_law ?L) (PiM ?M ?B)
      (λw. restrict w ?M) = gaussian_set_law ?M"
      using gaussian_set_law_delete[OF finiteL nonnegL, of t]
      by (simp only: del) simp
    have IH: "distr (gaussian_set_law ?M) (PiM J ?B)
      (λw. restrict w J) = gaussian_set_law J"
      by (rule insert.IH) (use insert in auto)
    have restrict_J: "(λw. restrict w J) ∈
      measurable (PiM ?M ?B) (PiM J ?B)"
      by (rule measurable_restrict_subset) auto
    have restrict_M_base: "(λw. restrict w ?M) ∈
      measurable (PiM ?L ?B) (PiM ?M ?B)"
      by (rule measurable_restrict_subset) auto
    have restrict_M: "(λw. restrict w ?M) ∈
      measurable (gaussian_set_law ?L) (PiM ?M ?B)"
      using restrict_M_base
      by (simp only: measurable_cong_sets[OF sets_gaussian_set_law refl])
    have "distr (gaussian_set_law ?L) (PiM J ?B) (λw. restrict w J) =
      distr (gaussian_set_law ?L) (PiM J ?B)
        ((λw. restrict w J) ∘ (λw. restrict w ?M))"
      by (intro distr_cong) (auto simp: comp_def restrict_def fun_eq_iff)
    also have "... = distr (distr (gaussian_set_law ?L) (PiM ?M ?B)
        (λw. restrict w ?M)) (PiM J ?B) (λw. restrict w J)"
      by (rule distr_distr[OF restrict_J restrict_M, symmetric])
    also have "... = gaussian_set_law J" using one IH by simp
    finally show ?case .
  qed
  have diff: "finite (H - J)" using finite by simp
  have H_eq: "H = J ∪ (H - J)" using subset by auto
  have law: "distr (gaussian_set_law (J ∪ (H - J))) (PiM J ?B)
      (λw. restrict w J) = gaussian_set_law J"
    by (rule consistent[OF diff]) (use nonnegative in auto)
  show ?thesis using law H_eq by metis
qed

interpretation gaussian: projective_family "{0..} :: real set"
  gaussian_set_law "λ_. borel :: real measure"
proof (intro projective_family.intro)
  fix J H :: "real set"
  assume "J ⊆ H" "finite H" "H ⊆ {0..}"
  then show "gaussian_set_law J =
    distr (gaussian_set_law H) (PiM J (λ_. borel)) (λw. restrict w J)"
    by (rule gaussian_set_law_consistent)
next
  fix J :: "real set"
  assume "finite J" "J ⊆ {0..}"
  show "prob_space (gaussian_set_law J)"
    by (rule prob_space_gaussian_set_law[OF ‹finite J›])
qed

interpretation gaussian_limit: polish_projective "{0..} :: real set" gaussian_set_law
  by (rule polish_projective.intro[OF gaussian.projective_family_axioms])

definition gaussian_path_law :: "(real ⇒ real) measure" where
  "gaussian_path_law =
    projective_family.lim ({0..} :: real set) gaussian_set_law (λ_. borel)"

lemma prob_space_gaussian_path_law: "prob_space gaussian_path_law"
  unfolding gaussian_path_law_def
  by (rule gaussian_limit.P.prob_space_axioms)

lemma sets_gaussian_path_law:
  "sets gaussian_path_law = sets (PiM {0..} (λ_. borel :: real measure))"
  by (simp only: gaussian_path_law_def gaussian.sets_lim)

lemma gaussian_limit_marginal:
  assumes finite: "finite J" and subset: "J ⊆ {0..}"
  shows "distr gaussian_path_law (PiM J (λ_. borel))
      (λw. restrict w J) = gaussian_set_law J"
proof (rule measure_eqI)
  show "sets (distr gaussian_path_law (PiM J (λ_. borel))
      (λw. restrict w J)) = sets (gaussian_set_law J)"
    by (simp add: sets_gaussian_set_law)
next
  fix A assume A: "A ∈ sets (distr gaussian_path_law
    (PiM J (λ_. borel)) (λw. restrict w J))"
  have A': "A ∈ sets (PiM J (λ_. borel))" using A by simp
  have measurable: "(λw. restrict w J) ∈
      measurable gaussian_path_law (PiM J (λ_. borel))"
    using measurable_restrict_subset[OF subset, of "λ_. borel"]
    by (simp only: gaussian_path_law_def
      measurable_cong_sets[OF gaussian.sets_lim refl])
  have emb_eq: "prod_emb ({0..} :: real set) (λ_. borel :: real measure) J A =
    (λw. restrict w J) -` A ∩ space gaussian_path_law"
    by (simp add: prod_emb_def gaussian_path_law_def space_PiM)
  have "emeasure (distr gaussian_path_law (PiM J (λ_. borel))
      (λw. restrict w J)) A =
    emeasure gaussian_path_law ((λw. restrict w J) -` A ∩ space gaussian_path_law)"
    by (rule emeasure_distr[OF measurable A'])
  also have "... = emeasure gaussian_path_law
    (prod_emb {0..} (λ_. borel) J A)"
    using emb_eq by simp
  also have "... = emeasure (gaussian_set_law J) A"
    using gaussian_limit.emeasure_lim_emb[OF subset finite A']
    by (simp only: gaussian_path_law_def)
  finally show "emeasure (distr gaussian_path_law (PiM J (λ_. borel))
      (λw. restrict w J)) A = emeasure (gaussian_set_law J) A" .
qed

interpretation gaussian_path: prob_space gaussian_path_law
  by (rule prob_space_gaussian_path_law)

lemma gaussian_path_coordinate_measurable:
  assumes "t ∈ {0..}"
  shows "(λw :: real ⇒ real. w t) ∈ borel_measurable gaussian_path_law"
  using measurable_component_singleton[of t "{0..}" "λ_. borel :: real measure"]
    assms
  by (simp only: measurable_cong_sets[OF sets_gaussian_path_law refl])

lemma gaussian_path_coordinates_measurable:
  "∀t∈{0..}. (λw :: real ⇒ real. w t) ∈ borel_measurable gaussian_path_law"
  using gaussian_path_coordinate_measurable by auto

definition gaussian_process :: "(real, real ⇒ real, real) stochastic_process" where
  "gaussian_process = prob_space.process_of gaussian_path_law borel {0..}
    (λt w. w t) 0"

lemma gaussian_process_index: "proc_index gaussian_process = {0..}"
  by (simp add: gaussian_process_def)

lemma gaussian_process_source: "proc_source gaussian_process = gaussian_path_law"
  using gaussian_path_coordinates_measurable
  by (simp add: gaussian_process_def)

lemma gaussian_process_target: "proc_target gaussian_process = borel"
  using gaussian_path_coordinates_measurable
  by (simp add: gaussian_process_def)

lemma gaussian_process_coordinate:
  assumes "t ∈ {0..}"
  shows "process gaussian_process t = (λw. w t)"
  using gaussian_path_coordinates_measurable assms
  by (simp add: gaussian_process_def gaussian_path.process_of_apply)

lemma gaussian_process_joint_law:
  assumes finite: "finite J" and subset: "J ⊆ {0..}"
  shows "distr (proc_source gaussian_process)
      (PiM J (λ_. proc_target gaussian_process))
      (λw. λt∈J. process gaussian_process t w) = gaussian_set_law J"
proof -
  have equal: "(λw. λt∈J. process gaussian_process t w) =
    (λw. restrict w J)"
    using subset by (auto simp: fun_eq_iff restrict_def gaussian_process_coordinate)
  show ?thesis using gaussian_limit_marginal[OF finite subset]
    by (simp only: gaussian_process_source gaussian_process_target equal)
qed

lemma measurable_gaussian_process_ordered_coordinates:
  "(λw. λi∈{..<length ts}. process gaussian_process (ts ! i) w) ∈
    measurable (proc_source gaussian_process)
      (PiM {..<length ts} (λ_. borel))"
proof (rule measurable_PiM_single')
  fix i assume "i ∈ {..<length ts}"
  show "(λw. (λi∈{..<length ts}. process gaussian_process (ts ! i) w) i) ∈
    borel_measurable (proc_source gaussian_process)"
    using stochastic_process_measurable[of gaussian_process "ts ! i"]
    by (simp add: gaussian_process_target ‹i ∈ {..<length ts}›)
next
  show "(λw. λi∈{..<length ts}. process gaussian_process (ts ! i) w) ∈
    space (proc_source gaussian_process) →
      (ΠE i∈{..<length ts}. space borel)"
    by (auto simp: PiE_iff split: if_splits)
qed

lemma gaussian_process_ordered_list_joint:
  assumes sorted: "sorted ts" and distinct: "distinct ts"
    and nonnegative: "∀t∈set ts. 0 ≤ t"
  shows "distr (proc_source gaussian_process)
    (PiM {..<length ts} (λ_. borel))
    (λw. λi∈{..<length ts}. process gaussian_process (ts ! i) w) =
    gaussian_finite_law ts"
proof -
  let ?I = "{..<length ts}"
  let ?T = "set ts"
  let ?B = "λ_. borel"
  have subset: "?T ⊆ {0..}" using nonnegative by auto
  have fin: "finite ?T" by simp
  have restrict_base: "(λw. restrict w ?T) ∈
    measurable (PiM ({0..} :: real set) ?B) (PiM ?T ?B)"
    by (rule measurable_restrict_subset[OF subset])
  have restrict_meas: "(λw. restrict w ?T) ∈
    measurable gaussian_path_law (PiM ?T ?B)"
    using restrict_base
    by (simp only: measurable_cong_sets[OF sets_gaussian_path_law refl])
  have list_meas: "list_coordinates ts ∈
    measurable (PiM ?T ?B) (PiM ?I ?B)"
    by (rule measurable_list_coordinates)
  have proc: "process gaussian_process (ts ! i) = (λw. w (ts ! i))"
    if "i < length ts" for i
    by (rule gaussian_process_coordinate) (use nonnegative that in auto)
  have mapping: "(λw. λi∈?I. process gaussian_process (ts ! i) w) =
    list_coordinates ts ∘ (λw. restrict w ?T)"
    using proc by (auto simp: fun_eq_iff list_coordinates_def restrict_def)
  have "distr (proc_source gaussian_process) (PiM ?I ?B)
      (λw. λi∈?I. process gaussian_process (ts ! i) w) =
    distr gaussian_path_law (PiM ?I ?B)
      (list_coordinates ts ∘ (λw. restrict w ?T))"
    by (simp only: gaussian_process_source mapping)
  also have "... = distr (distr gaussian_path_law (PiM ?T ?B)
    (λw. restrict w ?T)) (PiM ?I ?B) (list_coordinates ts)"
    by (rule distr_distr[OF list_meas restrict_meas, symmetric])
  also have "... = gaussian_finite_law ts"
    using gaussian_limit_marginal[OF fin subset]
      gaussian_set_law_ordered_list[OF sorted distinct] by simp
  finally show ?thesis .
qed

lemma time_index_adjacent:
  assumes sorted: "sorted ts" and j: "0 < j" "j < length ts"
    and jump: "ts ! (j - 1) < ts ! j"
  shows "time_index (set ts) (ts ! j) =
    Suc (time_index (set ts) (ts ! (j - 1)))"
proof -
  let ?T = "set ts"
  let ?u = "ordered_times ?T"
  let ?a = "time_index ?T (ts ! (j - 1))"
  let ?b = "time_index ?T (ts ! j)"
  have prev: "ts ! (j - 1) ∈ ?T" using j by simp
  have curr: "ts ! j ∈ ?T" using j by simp
  have ab: "?a < length ?u" "?b < length ?u"
    using time_index_unique(1)[of ?T "ts ! (j - 1)"]
      time_index_unique(1)[of ?T "ts ! j"] prev curr by auto
  have coords: "?u ! ?a = ts ! (j - 1)" "?u ! ?b = ts ! j"
    using time_index_unique(2)[of ?T "ts ! (j - 1)"]
      time_index_unique(2)[of ?T "ts ! j"] prev curr by auto
  have strict: "sorted_wrt (<) ?u"
    by (simp add: ordered_times_def strict_sorted_iff)
  have less: "?a < ?b"
  proof (rule ccontr)
    assume "¬ ?a < ?b"
    then have "?b ≤ ?a" by simp
    then have "?u ! ?b ≤ ?u ! ?a"
      using sorted_nth_mono[of ?u ?b ?a] ab strict
      by (simp add: strict_sorted_iff)
    with jump coords show False by simp
  qed
  have no_middle: "¬ Suc ?a < ?b"
  proof
    assume mid: "Suc ?a < ?b"
    then have m: "Suc ?a < length ?u" using ab by linarith
    have bounds: "ts ! (j - 1) < ?u ! Suc ?a"
      "?u ! Suc ?a < ts ! j"
      using sorted_wrt_nth_less[OF strict, of ?a "Suc ?a"]
        sorted_wrt_nth_less[OF strict, of "Suc ?a" ?b]
        m mid ab coords by auto
    have member: "?u ! Suc ?a ∈ set ts"
      using nth_mem[OF m] ordered_times_finite(1)[of ?T] by simp
    then obtain k where k: "k < length ts" "ts ! k = ?u ! Suc ?a"
      by (auto simp: in_set_conv_nth)
    have "k ≤ j - 1 ∨ j ≤ k" by arith
    then show False
    proof
      assume "k ≤ j - 1"
      then have "ts ! k ≤ ts ! (j - 1)"
        by (rule sorted_nth_mono[OF sorted]) (use j in auto)
      with bounds k show False by simp
    next
      assume "j ≤ k"
      then have "ts ! j ≤ ts ! k"
        by (rule sorted_nth_mono[OF sorted]) (use k in auto)
      with bounds k show False by simp
    qed
  qed
  show ?thesis using less no_middle by arith
qed

lemma gaussian_process_ordered_increment_law:
  assumes sorted: "sorted ts" and distinct: "distinct ts"
    and nonnegative: "∀t∈set ts. 0 ≤ t"
  shows "distr (proc_source gaussian_process) (increment_law ts)
    (λw. coordinate_increments (length ts)
      (λi∈{..<length ts}. process gaussian_process (ts ! i) w)) =
    increment_law ts"
proof -
  let ?F = "λw. λi∈{..<length ts}. process gaussian_process (ts ! i) w"
  have meas_F: "?F ∈ measurable (proc_source gaussian_process)
    (PiM {..<length ts} (λ_. borel))"
    by (rule measurable_gaussian_process_ordered_coordinates)
  have "distr (proc_source gaussian_process) (increment_law ts)
      (coordinate_increments (length ts) ∘ ?F) =
    distr (distr (proc_source gaussian_process)
      (PiM {..<length ts} (λ_. borel)) ?F)
      (increment_law ts) (coordinate_increments (length ts))"
    by (rule distr_distr[OF measurable_coordinate_increments[of ts] meas_F, symmetric])
  also have "... = increment_law ts"
    by (simp only: gaussian_process_ordered_list_joint[OF sorted distinct nonnegative]
      gaussian_finite_law_coordinate_increments)
  finally show ?thesis by (simp only: comp_def)
qed

lemma gaussian_process_ordered_independent_increments:
  assumes sorted: "sorted ts" and distinct: "distinct ts"
    and nonnegative: "∀t∈set ts. 0 ≤ t" and nonempty: "ts ≠ []"
  shows "prob_space.indep_vars (proc_source gaussian_process) (λ_. borel)
    (λi w. coordinate_increments (length ts)
      (λk∈{..<length ts}. process gaussian_process (ts ! k) w) i)
    {..<length ts}"
proof -
  have prob: "prob_space (proc_source gaussian_process)"
    by (simp add: gaussian_process_source prob_space_gaussian_path_law)
  let ?I = "{..<length ts}"
  let ?F = "λw. λi∈?I. process gaussian_process (ts ! i) w"
  let ?D = "λw. coordinate_increments (length ts) (?F w)"
  have meas_F: "?F ∈ measurable (proc_source gaussian_process)
    (PiM ?I (λ_. borel))"
    by (rule measurable_gaussian_process_ordered_coordinates)
  have meas_D: "?D ∈ measurable (proc_source gaussian_process) (increment_law ts)"
    using measurable_compose[OF meas_F measurable_coordinate_increments[of ts]]
    by (simp add: comp_def)
  have component: "(λw :: nat ⇒ real. w i) ∈ borel_measurable (increment_law ts)"
    if i: "i ∈ ?I" for i
    using measurable_component_singleton[of i ?I "λ_. borel :: real measure"] i
    by (simp only: measurable_cong_sets[OF sets_increment_law refl])
  have rv: "(λw. ?D w i) ∈ borel_measurable (proc_source gaussian_process)"
    if i: "i ∈ ?I" for i
    using measurable_compose[OF meas_D component[OF i]]
    by (simp add: comp_def)
  have law: "distr (proc_source gaussian_process) (increment_law ts) ?D = increment_law ts"
    by (rule gaussian_process_ordered_increment_law[OF sorted distinct nonnegative])
  have joint: "distr (proc_source gaussian_process) (PiM ?I (λ_. borel)) ?D =
    increment_law ts"
  proof -
    have "distr (proc_source gaussian_process) (PiM ?I (λ_. borel)) ?D =
      distr (proc_source gaussian_process) (increment_law ts) ?D"
      by (rule distr_cong) auto
    then show ?thesis using law by simp
  qed
  have marg: "distr (proc_source gaussian_process) borel (λw. ?D w i) =
    centered_normal (increment_variance ts i)" if i: "i ∈ ?I" for i
  proof -
    have "distr (proc_source gaussian_process) borel (λw. ?D w i) =
      distr (distr (proc_source gaussian_process) (increment_law ts) ?D)
        borel (λw. w i)"
      using distr_distr[OF component[OF i] meas_D] by (simp add: comp_def)
    also have "... = centered_normal (increment_variance ts i)"
    proof -
      have target: "distr (increment_law ts) borel (λw. w i) =
        distr (increment_law ts) (centered_normal (increment_variance ts i))
          (λw. w i)"
        by (rule distr_cong) simp_all
      show ?thesis using target increment_law_component[of i ts] i
        by (simp only: law) simp
    qed
    finally show ?thesis .
  qed
  have identity: "(λw. λi∈?I. ?D w i) = ?D"
    by (auto simp: fun_eq_iff coordinate_increments_def)
  have iff: "prob_space.indep_vars (proc_source gaussian_process)
      (λ_. borel) (λi w. ?D w i) ?I ⟷
    distr (proc_source gaussian_process) (PiM ?I (λ_. borel))
      (λw. λi∈?I. ?D w i) =
    PiM ?I (λi. distr (proc_source gaussian_process) borel (λw. ?D w i))"
    by (rule prob_space.indep_vars_iff_distr_eq_PiM'[OF prob])
       (use nonempty rv in auto)
  have product: "PiM ?I (λi. distr (proc_source gaussian_process) borel (λw. ?D w i)) =
    increment_law ts"
    unfolding increment_law_def by (rule PiM_cong) (use marg in auto)
  show ?thesis using iff joint product by (simp only: identity)
qed

corollary gaussian_process_independent_successive_increments:
  assumes sorted: "sorted ts" and distinct: "distinct ts"
    and nonnegative: "∀t∈set ts. 0 ≤ t" and nonempty: "ts ≠ []"
  shows "prob_space.indep_vars (proc_source gaussian_process) (λ_. borel)
    (λi w. if i = 0 then process gaussian_process (ts ! 0) w
      else process gaussian_process (ts ! i) w -
        process gaussian_process (ts ! (i - 1)) w) {..<length ts}"
proof -
  let ?I = "{..<length ts}"
  let ?F = "λw. λi∈?I. process gaussian_process (ts ! i) w"
  have prob: "prob_space (proc_source gaussian_process)"
    by (simp add: gaussian_process_source prob_space_gaussian_path_law)
  have indep: "prob_space.indep_vars (proc_source gaussian_process)
    (λ_. borel)
    (λi w. coordinate_increments (length ts) (?F w) i) ?I"
    by (rule gaussian_process_ordered_independent_increments
      [OF sorted distinct nonnegative nonempty])
  have agree: "(λw. coordinate_increments (length ts) (?F w) i) =
    (λw. if i = 0 then process gaussian_process (ts ! 0) w
      else process gaussian_process (ts ! i) w -
        process gaussian_process (ts ! (i - 1)) w)"
    if "i ∈ ?I" for i
    using that by (auto simp: fun_eq_iff coordinate_increments_def)
  have same: "prob_space.indep_vars (proc_source gaussian_process) (λ_. borel)
      (λi w. coordinate_increments (length ts) (?F w) i) ?I =
    prob_space.indep_vars (proc_source gaussian_process) (λ_. borel)
      (λi w. if i = 0 then process gaussian_process (ts ! 0) w
        else process gaussian_process (ts ! i) w -
          process gaussian_process (ts ! (i - 1)) w) ?I"
    by (rule prob_space.indep_vars_cong[OF prob]) (use agree in auto)
  show ?thesis using indep same by simp
qed

corollary gaussian_process_jointly_independent_increments:
  assumes sorted: "sorted ts" and distinct: "distinct ts"
    and nonnegative: "∀t∈set ts. 0 ≤ t"
    and nonempty: "ts ≠ []"
  shows "prob_space.indep_vars (proc_source gaussian_process) (λ_. borel)
    (λi w. process gaussian_process (ts ! i) w -
      process gaussian_process (ts ! (i - 1)) w) {1..<length ts}"
proof -
  have prob: "prob_space (proc_source gaussian_process)"
    by (simp add: gaussian_process_source prob_space_gaussian_path_law)
  have indep: "prob_space.indep_vars (proc_source gaussian_process) (λ_. borel)
    (λi w. if i = 0 then process gaussian_process (ts ! 0) w
      else process gaussian_process (ts ! i) w -
        process gaussian_process (ts ! (i - 1)) w) {..<length ts}"
    by (rule gaussian_process_independent_successive_increments
      [OF sorted distinct nonnegative nonempty])
  have restricted: "prob_space.indep_vars (proc_source gaussian_process) (λ_. borel)
    (λi w. if i = 0 then process gaussian_process (ts ! 0) w
      else process gaussian_process (ts ! i) w -
        process gaussian_process (ts ! (i - 1)) w) {1..<length ts}"
    by (rule prob_space.indep_vars_subset[OF prob indep]) auto
  have same: "prob_space.indep_vars (proc_source gaussian_process) (λ_. borel)
    (λi w. if i = 0 then process gaussian_process (ts ! 0) w
      else process gaussian_process (ts ! i) w -
        process gaussian_process (ts ! (i - 1)) w) {1..<length ts} =
    prob_space.indep_vars (proc_source gaussian_process) (λ_. borel)
      (λi w. process gaussian_process (ts ! i) w -
        process gaussian_process (ts ! (i - 1)) w) {1..<length ts}"
    by (rule prob_space.indep_vars_cong[OF prob]) auto
  show ?thesis using restricted same by simp
qed

theorem gaussian_process_indep_increments:
  "indep_increments gaussian_process"
proof (rule indep_incrementsI)
  fix ts :: "real list"
  assume times: "set ts ⊆ proc_index gaussian_process"
    and sorted: "sorted ts" and length: "2 ≤ length ts"
  let ?T = "set ts"
  let ?u = "ordered_times ?T"
  let ?I = "{1..<length ?u}"
  let ?J = "{1..<length ts}"
  let ?X = "λi w. process gaussian_process (?u ! i) w -
    process gaussian_process (?u ! (i - 1)) w"
  let ?r = "λj. time_index ?T (ts ! j)"
  let ?K = "λj. if ts ! (j - 1) = ts ! j then {} else {?r j}"
  let ?Y = "λj f :: nat ⇒ real. if ?K j = {} then 0 else f (?r j)"
  have prob: "prob_space (proc_source gaussian_process)"
    by (simp add: gaussian_process_source prob_space_gaussian_path_law)
  have nonnegative: "∀t∈?T. 0 ≤ t"
    using times by (auto simp: gaussian_process_index)
  have fin: "finite ?T" by simp
  have u_sorted: "sorted ?u" and u_distinct: "distinct ?u"
    and u_set: "set ?u = ?T"
    using ordered_times_finite[OF fin] by auto
  have u_nonempty: "?u ≠ []" using u_set length by auto
  have indep: "prob_space.indep_vars (proc_source gaussian_process)
    (λ_. borel) ?X ?I"
    by (rule gaussian_process_jointly_independent_increments
      [OF u_sorted u_distinct _ u_nonempty]) (use u_set nonnegative in auto)
  have jump: "ts ! (j - 1) < ts ! j"
    if j: "j ∈ ?J" and neq: "ts ! (j - 1) ≠ ts ! j" for j
    using sorted_nth_mono[OF sorted, of "j - 1" j] j neq by auto
  have rank: "?r j = Suc (time_index ?T (ts ! (j - 1)))"
    if j: "j ∈ ?J" and neq: "ts ! (j - 1) ≠ ts ! j" for j
    by (rule time_index_adjacent[OF sorted]) (use j jump[OF j neq] in auto)
  have sub: "?K j ⊆ ?I" if j: "j ∈ ?J" for j
  proof (cases "ts ! (j - 1) = ts ! j")
    case True then show ?thesis by simp
  next
    case False
    have member: "ts ! j ∈ ?T" using j by auto
    have bound: "?r j < length ?u"
      by (rule time_index_unique(1)[OF fin member])
    show ?thesis using rank[OF j False] bound False by auto
  qed
  have ends: "ts ! j ≠ ts ! k"
    if j: "j ∈ ?J" and k: "k ∈ ?J" and neq: "j ≠ k"
      and jump_j: "ts ! (j - 1) ≠ ts ! j"
      and jump_k: "ts ! (k - 1) ≠ ts ! k" for j k
  proof (cases "j < k")
    case True
    have "ts ! j ≤ ts ! (k - 1)"
      by (rule sorted_nth_mono[OF sorted]) (use True k in auto)
    moreover have "ts ! (k - 1) < ts ! k" by (rule jump[OF k jump_k])
    ultimately show ?thesis by simp
  next
    case False
    with neq have kj: "k < j" by simp
    have "ts ! k ≤ ts ! (j - 1)"
      by (rule sorted_nth_mono[OF sorted]) (use kj j in auto)
    moreover have "ts ! (j - 1) < ts ! j" by (rule jump[OF j jump_j])
    ultimately show ?thesis by simp
  qed
  have disjoint: "disjoint_family_on ?K ?J"
  proof (unfold disjoint_family_on_def, intro ballI impI)
    fix j k assume j: "j ∈ ?J" and k: "k ∈ ?J" and neq: "j ≠ k"
    show "?K j ∩ ?K k = {}"
    proof (cases "ts ! (j - 1) = ts ! j ∨ ts ! (k - 1) = ts ! k")
      case True then show ?thesis by auto
    next
      case False
      then have diff: "ts ! j ≠ ts ! k" using ends[OF j k neq] by auto
      have members: "ts ! j ∈ ?T" "ts ! k ∈ ?T" using j k by auto
      have ranks: "?r j ≠ ?r k"
        using time_index_unique(2)[OF fin members(1)]
          time_index_unique(2)[OF fin members(2)] diff by auto
      with False show ?thesis by auto
    qed
  qed
  have parts: "prob_space.indep_vars (proc_source gaussian_process)
    (λj. PiM (?K j) (λ_. borel))
    (λj w. restrict (λi. ?X i w) (?K j)) ?J"
    by (rule prob_space.indep_vars_restrict[OF prob indep sub disjoint])
  have Y_meas: "?Y j ∈ borel_measurable (PiM (?K j) (λ_. borel))"
    if j: "j ∈ ?J" for j
    by (cases "ts ! (j - 1) = ts ! j")
       (auto intro: measurable_component_singleton)
  have independent: "prob_space.indep_vars (proc_source gaussian_process)
    (λ_. borel)
    (λj w. ?Y j (restrict (λi. ?X i w) (?K j))) ?J"
    by (rule prob_space.indep_vars_compose2[OF prob parts Y_meas])
  have agree: "(λw. ?Y j (restrict (λi. ?X i w) (?K j))) =
    (λw. process gaussian_process (ts ! j) w -
      process gaussian_process (ts ! (j - 1)) w)"
    if j: "j ∈ ?J" for j
  proof (cases "ts ! (j - 1) = ts ! j")
    case True then show ?thesis by simp
  next
    case False
    have prev: "ts ! (j - 1) ∈ ?T" and curr: "ts ! j ∈ ?T"
      using j by auto
    show ?thesis
      using time_index_unique(2)[OF fin prev]
        time_index_unique(2)[OF fin curr] rank[OF j False]
      by (simp add: False fun_eq_iff restrict_def)
  qed
  have eq: "prob_space.indep_vars (proc_source gaussian_process) (λ_. borel)
    (λj w. ?Y j (restrict (λi. ?X i w) (?K j))) ?J =
    prob_space.indep_vars (proc_source gaussian_process) (λ_. borel)
      (λj w. process gaussian_process (ts ! j) w -
        process gaussian_process (ts ! (j - 1)) w) ?J"
    by (rule prob_space.indep_vars_cong[OF prob]) (use agree in auto)
  show "prob_space.indep_vars (proc_source gaussian_process)
      (λ_. proc_target gaussian_process)
      (λk v. process gaussian_process (ts ! k) v -
        process gaussian_process (ts ! (k - 1)) v) ?J"
    using independent eq by (simp only: gaussian_process_target)
qed

lemma gaussian_process_increment_law:
  assumes s: "0 ≤ s" and st: "s ≤ t"
  shows "distr (proc_source gaussian_process) borel
    (λw. process gaussian_process t w - process gaussian_process s w) =
    centered_normal (t - s)"
proof (cases "s = t")
  case True
  have prob: "prob_space (proc_source gaussian_process)"
    by (simp add: gaussian_process_source prob_space_gaussian_path_law)
  have "distr (proc_source gaussian_process) borel
    (λw. process gaussian_process t w - process gaussian_process s w) =
    distr (proc_source gaussian_process) borel (λ_. 0)"
    using True by (intro distr_cong) auto
  also have "... = centered_normal (t - s)"
    using True prob by (simp add: prob_space.distr_const)
  finally show ?thesis .
next
  case False
  with st have strict: "s < t" by simp
  let ?ts = "[s,t]"
  let ?I = "{..<length ?ts}"
  let ?F = "λw. λi∈?I. process gaussian_process (?ts ! i) w"
  let ?g = "λw :: nat ⇒ real. w 1 - w 0"
  have sorted: "sorted ?ts" using strict by simp
  have distinct: "distinct ?ts" using strict by simp
  have nonnegative: "∀u∈set ?ts. 0 ≤ u" using s strict by auto
  have F_meas: "?F ∈ measurable (proc_source gaussian_process)
    (PiM ?I (λ_. borel))"
    by (rule measurable_gaussian_process_ordered_coordinates)
  have g_meas: "?g ∈ borel_measurable (PiM ?I (λ_. borel))"
    by (intro borel_measurable_diff measurable_component_singleton) auto
  have "distr (proc_source gaussian_process) borel (?g ∘ ?F) =
    distr (distr (proc_source gaussian_process) (PiM ?I (λ_. borel)) ?F)
      borel ?g"
    by (rule distr_distr[OF g_meas F_meas, symmetric])
  also have "... = centered_normal (t - s)"
    using gaussian_process_ordered_list_joint[OF sorted distinct nonnegative]
      gaussian_finite_law_increment[of 1 ?ts]
    by (simp add: increment_variance_def)
  finally show ?thesis by (simp add: comp_def)
qed

lemma gaussian_set_law_zero_component:
  "distr (gaussian_set_law {0}) borel (λw. w 0) = return borel 0"
proof -
  have ordered: "ordered_times {0} = [0]"
    by (simp add: ordered_times_def)
  have index: "time_index {0} 0 = 0"
    using time_index_unique(1)[of "{0}" 0] by (simp add: ordered)
  have coord: "(λw :: real ⇒ real. w 0) ∈
    borel_measurable (PiM {0} (λ_. borel))"
    by (rule measurable_component_singleton) simp
  have base: "set_coordinates {0} ∈
    measurable (PiM {..<length (ordered_times {0})} (λ_. borel))
      (PiM {0} (λ_. borel))"
    by (rule measurable_set_coordinates) simp
  have map: "set_coordinates {0} ∈
    measurable (gaussian_finite_law [0]) (PiM {0} (λ_. borel))"
    using base
    by (simp only: ordered measurable_cong_sets[OF sets_gaussian_finite_law refl])
  have comp: "(λw. w 0) ∘ set_coordinates {0} = (λw. w 0)"
    using index by (auto simp: fun_eq_iff set_coordinates_def)
  have "distr (gaussian_set_law {0}) borel (λw. w 0) =
    distr (gaussian_finite_law [0]) borel ((λw. w 0) ∘ set_coordinates {0})"
    unfolding gaussian_set_law_def
    using distr_distr[OF coord map] by (simp only: ordered)
  also have "... = return borel 0"
  proof -
    have initial: "distr (gaussian_finite_law [0]) borel (λw. w 0) =
      centered_normal 0"
      using gaussian_finite_law_initial[of "[0]"] by simp
    show ?thesis using initial by (simp only: comp centered_normal_zero)
  qed
  finally show ?thesis .
qed

lemma gaussian_process_starts_at_zero:
  "AE w in proc_source gaussian_process. process gaussian_process 0 w = 0"
proof -
  have coord: "(λw :: real ⇒ real. w 0) ∈
    borel_measurable (PiM {0} (λ_. borel))"
    by (rule measurable_component_singleton) simp
  have subset_zero: "({0} :: real set) ⊆ {0..}" by simp
  have restrict_base: "(λ(w :: real ⇒ real). restrict w {0}) ∈
    measurable (PiM ({0..} :: real set) (λ_. borel :: real measure))
      (PiM {0} (λ_. borel))"
    by (rule measurable_restrict_subset[OF subset_zero])
  have restrict: "(λw. restrict w {0}) ∈
    measurable gaussian_path_law (PiM {0} (λ_. borel))"
    using restrict_base
    by (simp only: measurable_cong_sets[OF sets_gaussian_path_law refl])
  have comp: "(λw. w 0) ∘ (λw. restrict w {0}) = (λw. w 0)"
    by (simp add: fun_eq_iff restrict_def)
  have law: "distr gaussian_path_law borel (λw. w 0) = return borel 0"
    using distr_distr[OF coord restrict] gaussian_limit_marginal[of "{0}"]
      gaussian_set_law_zero_component
    by (simp only: comp) simp
  have ae_zero: "AE x in return borel (0::real). x = 0"
    by (subst AE_return) (auto simp: Measurable.pred_def)
  have measurable_zero: "(λw :: real ⇒ real. w 0) ∈
    borel_measurable gaussian_path_law"
    by (rule gaussian_path_coordinate_measurable) simp
  have ae_law: "AE x in distr gaussian_path_law borel (λw. w 0). x = 0"
    using ae_zero by (simp only: law)
  have ae: "AE w in gaussian_path_law. w 0 = 0"
    by (rule AE_distrD[OF measurable_zero ae_law])
  have proc0: "process gaussian_process 0 = (λw. w 0)"
    by (rule gaussian_process_coordinate) simp
  show ?thesis using ae
    by (simp only: gaussian_process_source proc0)
qed

text ‹The projective-limit process starts at zero almost surely. Its
  independent normal increments are established above; the fourth-moment
  bound and continuous modification follow in the final theory.›

end