src/HOL/Probability/Giry_Monad.thy
author hoelzl
Thu, 13 Nov 2014 17:19:52 +0100
changeset 59000 6eb0725503fc
parent 58608 5b7f0b5da884
child 59002 2c8b2fb54b88
permissions -rw-r--r--
import general theorems from AFP/Markov_Models
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
58606
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
     1
(*  Title:      HOL/Probability/Giry_Monad.thy
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
     2
    Author:     Johannes Hölzl, TU München
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
     3
    Author:     Manuel Eberl, TU München
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
     4
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
     5
Defines the subprobability spaces, the subprobability functor and the Giry monad on subprobability
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
     6
spaces.
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
     7
*)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
     8
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
     9
theory Giry_Monad
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
    10
  imports Probability_Measure "~~/src/HOL/Library/Monad_Syntax"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
    11
begin
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
    12
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
    13
section {* Sub-probability spaces *}
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
    14
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
    15
locale subprob_space = finite_measure +
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
    16
  assumes emeasure_space_le_1: "emeasure M (space M) \<le> 1"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
    17
  assumes subprob_not_empty: "space M \<noteq> {}"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
    18
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
    19
lemma subprob_spaceI[Pure.intro!]:
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
    20
  assumes *: "emeasure M (space M) \<le> 1"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
    21
  assumes "space M \<noteq> {}"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
    22
  shows "subprob_space M"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
    23
proof -
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
    24
  interpret finite_measure M
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
    25
  proof
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
    26
    show "emeasure M (space M) \<noteq> \<infinity>" using * by auto
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
    27
  qed
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
    28
  show "subprob_space M" by default fact+
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
    29
qed
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
    30
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
    31
lemma prob_space_imp_subprob_space:
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
    32
  "prob_space M \<Longrightarrow> subprob_space M"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
    33
  by (rule subprob_spaceI) (simp_all add: prob_space.emeasure_space_1 prob_space.not_empty)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
    34
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
    35
sublocale prob_space \<subseteq> subprob_space
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
    36
  by (rule subprob_spaceI) (simp_all add: emeasure_space_1 not_empty)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
    37
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
    38
lemma (in subprob_space) subprob_space_distr:
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
    39
  assumes f: "f \<in> measurable M M'" and "space M' \<noteq> {}" shows "subprob_space (distr M M' f)"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
    40
proof (rule subprob_spaceI)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
    41
  have "f -` space M' \<inter> space M = space M" using f by (auto dest: measurable_space)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
    42
  with f show "emeasure (distr M M' f) (space (distr M M' f)) \<le> 1"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
    43
    by (auto simp: emeasure_distr emeasure_space_le_1)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
    44
  show "space (distr M M' f) \<noteq> {}" by (simp add: assms)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
    45
qed
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
    46
59000
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
    47
lemma (in subprob_space) subprob_emeasure_le_1: "emeasure M X \<le> 1"
58606
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
    48
  by (rule order.trans[OF emeasure_space emeasure_space_le_1])
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
    49
59000
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
    50
lemma (in subprob_space) subprob_measure_le_1: "measure M X \<le> 1"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
    51
  using subprob_emeasure_le_1[of X] by (simp add: emeasure_eq_measure)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
    52
58606
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
    53
locale pair_subprob_space = 
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
    54
  pair_sigma_finite M1 M2 + M1: subprob_space M1 + M2: subprob_space M2 for M1 M2
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
    55
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
    56
sublocale pair_subprob_space \<subseteq> P: subprob_space "M1 \<Otimes>\<^sub>M M2"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
    57
proof
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
    58
  have "\<And>a b. \<lbrakk>a \<ge> 0; b \<ge> 0; a \<le> 1; b \<le> 1\<rbrakk> \<Longrightarrow> a * b \<le> (1::ereal)"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
    59
    by (metis comm_monoid_mult_class.mult.left_neutral dual_order.trans ereal_mult_right_mono)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
    60
  from this[OF _ _ M1.emeasure_space_le_1 M2.emeasure_space_le_1]
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
    61
    show "emeasure (M1 \<Otimes>\<^sub>M M2) (space (M1 \<Otimes>\<^sub>M M2)) \<le> 1"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
    62
    by (simp add: M2.emeasure_pair_measure_Times space_pair_measure emeasure_nonneg)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
    63
  from M1.subprob_not_empty and M2.subprob_not_empty show "space (M1 \<Otimes>\<^sub>M M2) \<noteq> {}"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
    64
    by (simp add: space_pair_measure)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
    65
qed
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
    66
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
    67
definition subprob_algebra :: "'a measure \<Rightarrow> 'a measure measure" where
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
    68
  "subprob_algebra K =
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
    69
    (\<Squnion>\<^sub>\<sigma> A\<in>sets K. vimage_algebra {M. subprob_space M \<and> sets M = sets K} (\<lambda>M. emeasure M A) borel)"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
    70
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
    71
lemma space_subprob_algebra: "space (subprob_algebra A) = {M. subprob_space M \<and> sets M = sets A}"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
    72
  by (auto simp add: subprob_algebra_def space_Sup_sigma)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
    73
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
    74
lemma subprob_algebra_cong: "sets M = sets N \<Longrightarrow> subprob_algebra M = subprob_algebra N"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
    75
  by (simp add: subprob_algebra_def)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
    76
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
    77
lemma measurable_emeasure_subprob_algebra[measurable]: 
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
    78
  "a \<in> sets A \<Longrightarrow> (\<lambda>M. emeasure M a) \<in> borel_measurable (subprob_algebra A)"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
    79
  by (auto intro!: measurable_Sup_sigma1 measurable_vimage_algebra1 simp: subprob_algebra_def)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
    80
59000
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
    81
lemma subprob_measurableD:
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
    82
  assumes N: "N \<in> measurable M (subprob_algebra S)" and x: "x \<in> space M"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
    83
  shows "space (N x) = space S"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
    84
    and "sets (N x) = sets S"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
    85
    and "measurable (N x) K = measurable S K"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
    86
    and "measurable K (N x) = measurable K S"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
    87
  using measurable_space[OF N x]
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
    88
  by (auto simp: space_subprob_algebra intro!: measurable_cong_sets dest: sets_eq_imp_space_eq)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
    89
58606
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
    90
context
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
    91
  fixes K M N assumes K: "K \<in> measurable M (subprob_algebra N)"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
    92
begin
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
    93
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
    94
lemma subprob_space_kernel: "a \<in> space M \<Longrightarrow> subprob_space (K a)"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
    95
  using measurable_space[OF K] by (simp add: space_subprob_algebra)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
    96
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
    97
lemma sets_kernel: "a \<in> space M \<Longrightarrow> sets (K a) = sets N"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
    98
  using measurable_space[OF K] by (simp add: space_subprob_algebra)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
    99
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   100
lemma measurable_emeasure_kernel[measurable]: 
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   101
    "A \<in> sets N \<Longrightarrow> (\<lambda>a. emeasure (K a) A) \<in> borel_measurable M"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   102
  using measurable_compose[OF K measurable_emeasure_subprob_algebra] .
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   103
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   104
end
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   105
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   106
lemma measurable_subprob_algebra:
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   107
  "(\<And>a. a \<in> space M \<Longrightarrow> subprob_space (K a)) \<Longrightarrow>
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   108
  (\<And>a. a \<in> space M \<Longrightarrow> sets (K a) = sets N) \<Longrightarrow>
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   109
  (\<And>A. A \<in> sets N \<Longrightarrow> (\<lambda>a. emeasure (K a) A) \<in> borel_measurable M) \<Longrightarrow>
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   110
  K \<in> measurable M (subprob_algebra N)"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   111
  by (auto intro!: measurable_Sup_sigma2 measurable_vimage_algebra2 simp: subprob_algebra_def)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   112
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   113
lemma space_subprob_algebra_empty_iff:
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   114
  "space (subprob_algebra N) = {} \<longleftrightarrow> space N = {}"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   115
proof
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   116
  have "\<And>x. x \<in> space N \<Longrightarrow> density N (\<lambda>_. 0) \<in> space (subprob_algebra N)"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   117
    by (auto simp: space_subprob_algebra emeasure_density intro!: subprob_spaceI)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   118
  then show "space (subprob_algebra N) = {} \<Longrightarrow> space N = {}"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   119
    by auto
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   120
next
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   121
  assume "space N = {}"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   122
  hence "sets N = {{}}" by (simp add: space_empty_iff)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   123
  moreover have "\<And>M. subprob_space M \<Longrightarrow> sets M \<noteq> {{}}"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   124
    by (simp add: subprob_space.subprob_not_empty space_empty_iff[symmetric])
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   125
  ultimately show "space (subprob_algebra N) = {}" by (auto simp: space_subprob_algebra)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   126
qed
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   127
59000
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   128
lemma nn_integral_measurable_subprob_algebra:
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   129
  assumes f: "f \<in> borel_measurable N" "\<And>x. 0 \<le> f x"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   130
  shows "(\<lambda>M. integral\<^sup>N M f) \<in> borel_measurable (subprob_algebra N)" (is "_ \<in> ?B")
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   131
  using f
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   132
proof induct
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   133
  case (cong f g)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   134
  moreover have "(\<lambda>M'. \<integral>\<^sup>+M''. f M'' \<partial>M') \<in> ?B \<longleftrightarrow> (\<lambda>M'. \<integral>\<^sup>+M''. g M'' \<partial>M') \<in> ?B"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   135
    by (intro measurable_cong nn_integral_cong cong)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   136
       (auto simp: space_subprob_algebra dest!: sets_eq_imp_space_eq)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   137
  ultimately show ?case by simp
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   138
next
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   139
  case (set B)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   140
  moreover then have "(\<lambda>M'. \<integral>\<^sup>+M''. indicator B M'' \<partial>M') \<in> ?B \<longleftrightarrow> (\<lambda>M'. emeasure M' B) \<in> ?B"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   141
    by (intro measurable_cong nn_integral_indicator) (simp add: space_subprob_algebra)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   142
  ultimately show ?case
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   143
    by (simp add: measurable_emeasure_subprob_algebra)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   144
next
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   145
  case (mult f c)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   146
  moreover then have "(\<lambda>M'. \<integral>\<^sup>+M''. c * f M'' \<partial>M') \<in> ?B \<longleftrightarrow> (\<lambda>M'. c * \<integral>\<^sup>+M''. f M'' \<partial>M') \<in> ?B"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   147
    by (intro measurable_cong nn_integral_cmult) (simp add: space_subprob_algebra)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   148
  ultimately show ?case
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   149
    by simp
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   150
next
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   151
  case (add f g)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   152
  moreover then have "(\<lambda>M'. \<integral>\<^sup>+M''. f M'' + g M'' \<partial>M') \<in> ?B \<longleftrightarrow> (\<lambda>M'. (\<integral>\<^sup>+M''. f M'' \<partial>M') + (\<integral>\<^sup>+M''. g M'' \<partial>M')) \<in> ?B"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   153
    by (intro measurable_cong nn_integral_add) (simp_all add: space_subprob_algebra)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   154
  ultimately show ?case
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   155
    by (simp add: ac_simps)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   156
next
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   157
  case (seq F)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   158
  moreover then have "(\<lambda>M'. \<integral>\<^sup>+M''. (SUP i. F i) M'' \<partial>M') \<in> ?B \<longleftrightarrow> (\<lambda>M'. SUP i. (\<integral>\<^sup>+M''. F i M'' \<partial>M')) \<in> ?B"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   159
    unfolding SUP_apply
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   160
    by (intro measurable_cong nn_integral_monotone_convergence_SUP) (simp_all add: space_subprob_algebra)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   161
  ultimately show ?case
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   162
    by (simp add: ac_simps)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   163
qed
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   164
58606
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   165
lemma measurable_distr:
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   166
  assumes [measurable]: "f \<in> measurable M N"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   167
  shows "(\<lambda>M'. distr M' N f) \<in> measurable (subprob_algebra M) (subprob_algebra N)"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   168
proof (cases "space N = {}")
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   169
  assume not_empty: "space N \<noteq> {}"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   170
  show ?thesis
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   171
  proof (rule measurable_subprob_algebra)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   172
    fix A assume A: "A \<in> sets N"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   173
    then have "(\<lambda>M'. emeasure (distr M' N f) A) \<in> borel_measurable (subprob_algebra M) \<longleftrightarrow>
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   174
      (\<lambda>M'. emeasure M' (f -` A \<inter> space M)) \<in> borel_measurable (subprob_algebra M)"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   175
      by (intro measurable_cong)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   176
         (auto simp: emeasure_distr space_subprob_algebra dest: sets_eq_imp_space_eq cong: measurable_cong)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   177
    also have "\<dots>"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   178
      using A by (intro measurable_emeasure_subprob_algebra) simp
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   179
    finally show "(\<lambda>M'. emeasure (distr M' N f) A) \<in> borel_measurable (subprob_algebra M)" .
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   180
  qed (auto intro!: subprob_space.subprob_space_distr simp: space_subprob_algebra not_empty)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   181
qed (insert assms, auto simp: measurable_empty_iff space_subprob_algebra_empty_iff)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   182
59000
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   183
lemma emeasure_space_subprob_algebra[measurable]:
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   184
  "(\<lambda>a. emeasure a (space a)) \<in> borel_measurable (subprob_algebra N)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   185
proof-
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   186
  have "(\<lambda>a. emeasure a (space N)) \<in> borel_measurable (subprob_algebra N)" (is "?f \<in> ?M")
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   187
    by (rule measurable_emeasure_subprob_algebra) simp
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   188
  also have "?f \<in> ?M \<longleftrightarrow> (\<lambda>a. emeasure a (space a)) \<in> ?M"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   189
    by (rule measurable_cong) (auto simp: space_subprob_algebra dest: sets_eq_imp_space_eq)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   190
  finally show ?thesis .
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   191
qed
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   192
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   193
(* TODO: Rename. This name is too general – Manuel *)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   194
lemma measurable_pair_measure:
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   195
  assumes f: "f \<in> measurable M (subprob_algebra N)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   196
  assumes g: "g \<in> measurable M (subprob_algebra L)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   197
  shows "(\<lambda>x. f x \<Otimes>\<^sub>M g x) \<in> measurable M (subprob_algebra (N \<Otimes>\<^sub>M L))"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   198
proof (rule measurable_subprob_algebra)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   199
  { fix x assume "x \<in> space M"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   200
    with measurable_space[OF f] measurable_space[OF g]
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   201
    have fx: "f x \<in> space (subprob_algebra N)" and gx: "g x \<in> space (subprob_algebra L)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   202
      by auto
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   203
    interpret F: subprob_space "f x"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   204
      using fx by (simp add: space_subprob_algebra)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   205
    interpret G: subprob_space "g x"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   206
      using gx by (simp add: space_subprob_algebra)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   207
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   208
    interpret pair_subprob_space "f x" "g x" ..
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   209
    show "subprob_space (f x \<Otimes>\<^sub>M g x)" by unfold_locales
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   210
    show sets_eq: "sets (f x \<Otimes>\<^sub>M g x) = sets (N \<Otimes>\<^sub>M L)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   211
      using fx gx by (simp add: space_subprob_algebra)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   212
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   213
    have 1: "\<And>A B. A \<in> sets N \<Longrightarrow> B \<in> sets L \<Longrightarrow> emeasure (f x \<Otimes>\<^sub>M g x) (A \<times> B) = emeasure (f x) A * emeasure (g x) B"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   214
      using fx gx by (intro G.emeasure_pair_measure_Times) (auto simp: space_subprob_algebra) 
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   215
    have "emeasure (f x \<Otimes>\<^sub>M g x) (space (f x \<Otimes>\<^sub>M g x)) = 
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   216
              emeasure (f x) (space (f x)) * emeasure (g x) (space (g x))"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   217
      by (subst G.emeasure_pair_measure_Times[symmetric]) (simp_all add: space_pair_measure)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   218
    hence 2: "\<And>A. A \<in> sets (N \<Otimes>\<^sub>M L) \<Longrightarrow> emeasure (f x \<Otimes>\<^sub>M g x) (space N \<times> space L - A) =
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   219
                                             ... - emeasure (f x \<Otimes>\<^sub>M g x) A"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   220
      using emeasure_compl[OF _ P.emeasure_finite]
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   221
      unfolding sets_eq
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   222
      unfolding sets_eq_imp_space_eq[OF sets_eq]
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   223
      by (simp add: space_pair_measure G.emeasure_pair_measure_Times)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   224
    note 1 2 sets_eq }
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   225
  note Times = this(1) and Compl = this(2) and sets_eq = this(3)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   226
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   227
  fix A assume A: "A \<in> sets (N \<Otimes>\<^sub>M L)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   228
  show "(\<lambda>a. emeasure (f a \<Otimes>\<^sub>M g a) A) \<in> borel_measurable M"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   229
    using Int_stable_pair_measure_generator pair_measure_closed A
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   230
    unfolding sets_pair_measure
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   231
  proof (induct A rule: sigma_sets_induct_disjoint)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   232
    case (basic A) then show ?case
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   233
      by (auto intro!: borel_measurable_ereal_times simp: Times cong: measurable_cong)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   234
         (auto intro!: measurable_emeasure_kernel f g)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   235
  next
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   236
    case (compl A)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   237
    then have A: "A \<in> sets (N \<Otimes>\<^sub>M L)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   238
      by (auto simp: sets_pair_measure)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   239
    have "(\<lambda>x. emeasure (f x) (space (f x)) * emeasure (g x) (space (g x)) - 
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   240
                   emeasure (f x \<Otimes>\<^sub>M g x) A) \<in> borel_measurable M" (is "?f \<in> ?M")
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   241
      using compl(2) f g by measurable
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   242
    thus ?case by (simp add: Compl A cong: measurable_cong)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   243
  next
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   244
    case (union A)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   245
    then have "range A \<subseteq> sets (N \<Otimes>\<^sub>M L)" "disjoint_family A"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   246
      by (auto simp: sets_pair_measure)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   247
    then have "(\<lambda>a. emeasure (f a \<Otimes>\<^sub>M g a) (\<Union>i. A i)) \<in> borel_measurable M \<longleftrightarrow>
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   248
      (\<lambda>a. \<Sum>i. emeasure (f a \<Otimes>\<^sub>M g a) (A i)) \<in> borel_measurable M"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   249
      by (intro measurable_cong suminf_emeasure[symmetric])
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   250
         (auto simp: sets_eq)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   251
    also have "\<dots>"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   252
      using union by auto
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   253
    finally show ?case .
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   254
  qed simp
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   255
qed
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   256
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   257
lemma restrict_space_measurable:
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   258
  assumes X: "X \<noteq> {}" "X \<in> sets K"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   259
  assumes N: "N \<in> measurable M (subprob_algebra K)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   260
  shows "(\<lambda>x. restrict_space (N x) X) \<in> measurable M (subprob_algebra (restrict_space K X))"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   261
proof (rule measurable_subprob_algebra)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   262
  fix a assume a: "a \<in> space M"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   263
  from N[THEN measurable_space, OF this]
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   264
  have "subprob_space (N a)" and [simp]: "sets (N a) = sets K" "space (N a) = space K"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   265
    by (auto simp add: space_subprob_algebra dest: sets_eq_imp_space_eq)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   266
  then interpret subprob_space "N a"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   267
    by simp
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   268
  show "subprob_space (restrict_space (N a) X)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   269
  proof
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   270
    show "space (restrict_space (N a) X) \<noteq> {}"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   271
      using X by (auto simp add: space_restrict_space)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   272
    show "emeasure (restrict_space (N a) X) (space (restrict_space (N a) X)) \<le> 1"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   273
      using X by (simp add: emeasure_restrict_space space_restrict_space subprob_emeasure_le_1)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   274
  qed
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   275
  show "sets (restrict_space (N a) X) = sets (restrict_space K X)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   276
    by (intro sets_restrict_space_cong) fact
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   277
next
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   278
  fix A assume A: "A \<in> sets (restrict_space K X)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   279
  show "(\<lambda>a. emeasure (restrict_space (N a) X) A) \<in> borel_measurable M"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   280
  proof (subst measurable_cong)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   281
    fix a assume "a \<in> space M"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   282
    from N[THEN measurable_space, OF this]
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   283
    have [simp]: "sets (N a) = sets K" "space (N a) = space K"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   284
      by (auto simp add: space_subprob_algebra dest: sets_eq_imp_space_eq)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   285
    show "emeasure (restrict_space (N a) X) A = emeasure (N a) (A \<inter> X)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   286
      using X A by (subst emeasure_restrict_space) (auto simp add: sets_restrict_space ac_simps)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   287
  next
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   288
    show "(\<lambda>w. emeasure (N w) (A \<inter> X)) \<in> borel_measurable M"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   289
      using A X
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   290
      by (intro measurable_compose[OF N measurable_emeasure_subprob_algebra])
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   291
         (auto simp: sets_restrict_space)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   292
  qed
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   293
qed
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   294
58606
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   295
section {* Properties of return *}
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   296
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   297
definition return :: "'a measure \<Rightarrow> 'a \<Rightarrow> 'a measure" where
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   298
  "return R x = measure_of (space R) (sets R) (\<lambda>A. indicator A x)"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   299
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   300
lemma space_return[simp]: "space (return M x) = space M"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   301
  by (simp add: return_def)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   302
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   303
lemma sets_return[simp]: "sets (return M x) = sets M"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   304
  by (simp add: return_def)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   305
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   306
lemma measurable_return1[simp]: "measurable (return N x) L = measurable N L"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   307
  by (simp cong: measurable_cong_sets) 
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   308
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   309
lemma measurable_return2[simp]: "measurable L (return N x) = measurable L N"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   310
  by (simp cong: measurable_cong_sets) 
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   311
59000
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   312
lemma return_sets_cong: "sets M = sets N \<Longrightarrow> return M = return N"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   313
  by (auto dest: sets_eq_imp_space_eq simp: fun_eq_iff return_def)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   314
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   315
lemma return_cong: "sets A = sets B \<Longrightarrow> return A x = return B x"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   316
  by (auto simp add: return_def dest: sets_eq_imp_space_eq)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   317
58606
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   318
lemma emeasure_return[simp]:
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   319
  assumes "A \<in> sets M"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   320
  shows "emeasure (return M x) A = indicator A x"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   321
proof (rule emeasure_measure_of[OF return_def])
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   322
  show "sets M \<subseteq> Pow (space M)" by (rule sets.space_closed)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   323
  show "positive (sets (return M x)) (\<lambda>A. indicator A x)" by (simp add: positive_def)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   324
  from assms show "A \<in> sets (return M x)" unfolding return_def by simp
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   325
  show "countably_additive (sets (return M x)) (\<lambda>A. indicator A x)"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   326
    by (auto intro: countably_additiveI simp: suminf_indicator)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   327
qed
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   328
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   329
lemma prob_space_return: "x \<in> space M \<Longrightarrow> prob_space (return M x)"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   330
  by rule simp
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   331
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   332
lemma subprob_space_return: "x \<in> space M \<Longrightarrow> subprob_space (return M x)"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   333
  by (intro prob_space_return prob_space_imp_subprob_space)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   334
59000
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   335
lemma subprob_space_return_ne: 
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   336
  assumes "space M \<noteq> {}" shows "subprob_space (return M x)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   337
proof
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   338
  show "emeasure (return M x) (space (return M x)) \<le> 1"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   339
    by (subst emeasure_return) (auto split: split_indicator)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   340
qed (simp, fact)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   341
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   342
lemma measure_return: assumes X: "X \<in> sets M" shows "measure (return M x) X = indicator X x"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   343
  unfolding measure_def emeasure_return[OF X, of x] by (simp split: split_indicator)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   344
58606
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   345
lemma AE_return:
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   346
  assumes [simp]: "x \<in> space M" and [measurable]: "Measurable.pred M P"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   347
  shows "(AE y in return M x. P y) \<longleftrightarrow> P x"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   348
proof -
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   349
  have "(AE y in return M x. y \<notin> {x\<in>space M. \<not> P x}) \<longleftrightarrow> P x"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   350
    by (subst AE_iff_null_sets[symmetric]) (simp_all add: null_sets_def split: split_indicator)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   351
  also have "(AE y in return M x. y \<notin> {x\<in>space M. \<not> P x}) \<longleftrightarrow> (AE y in return M x. P y)"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   352
    by (rule AE_cong) auto
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   353
  finally show ?thesis .
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   354
qed
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   355
  
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   356
lemma nn_integral_return:
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   357
  assumes "g x \<ge> 0" "x \<in> space M" "g \<in> borel_measurable M"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   358
  shows "(\<integral>\<^sup>+ a. g a \<partial>return M x) = g x"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   359
proof-
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   360
  interpret prob_space "return M x" by (rule prob_space_return[OF `x \<in> space M`])
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   361
  have "(\<integral>\<^sup>+ a. g a \<partial>return M x) = (\<integral>\<^sup>+ a. g x \<partial>return M x)" using assms
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   362
    by (intro nn_integral_cong_AE) (auto simp: AE_return)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   363
  also have "... = g x"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   364
    using nn_integral_const[OF `g x \<ge> 0`, of "return M x"] emeasure_space_1 by simp
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   365
  finally show ?thesis .
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   366
qed
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   367
59000
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   368
lemma integral_return:
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   369
  fixes g :: "_ \<Rightarrow> 'a :: {banach, second_countable_topology}"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   370
  assumes "x \<in> space M" "g \<in> borel_measurable M"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   371
  shows "(\<integral>a. g a \<partial>return M x) = g x"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   372
proof-
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   373
  interpret prob_space "return M x" by (rule prob_space_return[OF `x \<in> space M`])
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   374
  have "(\<integral>a. g a \<partial>return M x) = (\<integral>a. g x \<partial>return M x)" using assms
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   375
    by (intro integral_cong_AE) (auto simp: AE_return)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   376
  then show ?thesis
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   377
    using prob_space by simp
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   378
qed
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   379
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   380
lemma return_measurable[measurable]: "return N \<in> measurable N (subprob_algebra N)"
58606
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   381
  by (rule measurable_subprob_algebra) (auto simp: subprob_space_return)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   382
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   383
lemma distr_return:
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   384
  assumes "f \<in> measurable M N" and "x \<in> space M"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   385
  shows "distr (return M x) N f = return N (f x)"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   386
  using assms by (intro measure_eqI) (simp_all add: indicator_def emeasure_distr)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   387
59000
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   388
lemma return_restrict_space:
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   389
  "\<Omega> \<in> sets M \<Longrightarrow> return (restrict_space M \<Omega>) x = restrict_space (return M x) \<Omega>"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   390
  by (auto intro!: measure_eqI simp: sets_restrict_space emeasure_restrict_space)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   391
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   392
lemma measurable_distr2:
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   393
  assumes f[measurable]: "split f \<in> measurable (L \<Otimes>\<^sub>M M) N"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   394
  assumes g[measurable]: "g \<in> measurable L (subprob_algebra M)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   395
  shows "(\<lambda>x. distr (g x) N (f x)) \<in> measurable L (subprob_algebra N)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   396
proof -
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   397
  have "(\<lambda>x. distr (g x) N (f x)) \<in> measurable L (subprob_algebra N)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   398
    \<longleftrightarrow> (\<lambda>x. distr (return L x \<Otimes>\<^sub>M g x) N (split f)) \<in> measurable L (subprob_algebra N)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   399
  proof (rule measurable_cong)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   400
    fix x assume x: "x \<in> space L"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   401
    have gx: "g x \<in> space (subprob_algebra M)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   402
      using measurable_space[OF g x] .
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   403
    then have [simp]: "sets (g x) = sets M"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   404
      by (simp add: space_subprob_algebra)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   405
    then have [simp]: "space (g x) = space M"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   406
      by (rule sets_eq_imp_space_eq)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   407
    let ?R = "return L x"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   408
    from measurable_compose_Pair1[OF x f] have f_M': "f x \<in> measurable M N"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   409
      by simp
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   410
    interpret subprob_space "g x"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   411
      using gx by (simp add: space_subprob_algebra)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   412
    have space_pair_M'[simp]: "\<And>X. space (X \<Otimes>\<^sub>M g x) = space (X \<Otimes>\<^sub>M M)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   413
      by (simp add: space_pair_measure)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   414
    show "distr (g x) N (f x) = distr (?R \<Otimes>\<^sub>M g x) N (split f)" (is "?l = ?r")
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   415
    proof (rule measure_eqI)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   416
      show "sets ?l = sets ?r"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   417
        by simp
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   418
    next
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   419
      fix A assume "A \<in> sets ?l"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   420
      then have A[measurable]: "A \<in> sets N"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   421
        by simp
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   422
      then have "emeasure ?r A = emeasure (?R \<Otimes>\<^sub>M g x) ((\<lambda>(x, y). f x y) -` A \<inter> space (?R \<Otimes>\<^sub>M g x))"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   423
        by (auto simp add: emeasure_distr f_M' cong: measurable_cong_sets)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   424
      also have "\<dots> = (\<integral>\<^sup>+M''. emeasure (g x) (f M'' -` A \<inter> space M) \<partial>?R)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   425
        apply (subst emeasure_pair_measure_alt)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   426
        apply (rule measurable_sets[OF _ A])
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   427
        apply (auto simp add: f_M' cong: measurable_cong_sets)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   428
        apply (intro nn_integral_cong arg_cong[where f="emeasure (g x)"])
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   429
        apply (auto simp: space_subprob_algebra space_pair_measure)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   430
        done
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   431
      also have "\<dots> = emeasure (g x) (f x -` A \<inter> space M)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   432
        by (subst nn_integral_return)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   433
           (auto simp: x intro!: measurable_emeasure)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   434
      also have "\<dots> = emeasure ?l A"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   435
        by (simp add: emeasure_distr f_M' cong: measurable_cong_sets)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   436
      finally show "emeasure ?l A = emeasure ?r A" ..
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   437
    qed
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   438
  qed
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   439
  also have "\<dots>"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   440
    apply (intro measurable_compose[OF measurable_pair_measure measurable_distr])
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   441
    apply (rule return_measurable)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   442
    apply measurable
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   443
    done
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   444
  finally show ?thesis .
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   445
qed
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   446
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   447
lemma nn_integral_measurable_subprob_algebra2:
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   448
  assumes f[measurable]: "(\<lambda>(x, y). f x y) \<in> borel_measurable (M \<Otimes>\<^sub>M N)" and [simp]: "\<And>x y. 0 \<le> f x y"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   449
  assumes N[measurable]: "L \<in> measurable M (subprob_algebra N)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   450
  shows "(\<lambda>x. integral\<^sup>N (L x) (f x)) \<in> borel_measurable M"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   451
proof -
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   452
  have "(\<lambda>x. integral\<^sup>N (distr (L x) (M \<Otimes>\<^sub>M N) (\<lambda>y. (x, y))) (\<lambda>(x, y). f x y)) \<in> borel_measurable M"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   453
    apply (rule measurable_compose[OF _ nn_integral_measurable_subprob_algebra])
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   454
    apply (rule measurable_distr2)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   455
    apply measurable
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   456
    apply (simp split: prod.split)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   457
    done
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   458
  then show "(\<lambda>x. integral\<^sup>N (L x) (f x)) \<in> borel_measurable M"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   459
    apply (rule measurable_cong[THEN iffD1, rotated])
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   460
    apply (subst nn_integral_distr)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   461
    apply measurable
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   462
    apply (rule subprob_measurableD(2)[OF N], assumption)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   463
    apply measurable
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   464
    done
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   465
qed
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   466
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   467
lemma emeasure_measurable_subprob_algebra2:
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   468
  assumes A[measurable]: "(SIGMA x:space M. A x) \<in> sets (M \<Otimes>\<^sub>M N)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   469
  assumes L[measurable]: "L \<in> measurable M (subprob_algebra N)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   470
  shows "(\<lambda>x. emeasure (L x) (A x)) \<in> borel_measurable M"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   471
proof -
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   472
  { fix x assume "x \<in> space M"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   473
    then have "Pair x -` Sigma (space M) A = A x"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   474
      by auto
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   475
    with sets_Pair1[OF A, of x] have "A x \<in> sets N"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   476
      by auto }
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   477
  note ** = this
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   478
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   479
  have *: "\<And>x. fst x \<in> space M \<Longrightarrow> snd x \<in> A (fst x) \<longleftrightarrow> x \<in> (SIGMA x:space M. A x)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   480
    by (auto simp: fun_eq_iff)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   481
  have "(\<lambda>(x, y). indicator (A x) y::ereal) \<in> borel_measurable (M \<Otimes>\<^sub>M N)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   482
    apply measurable
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   483
    apply (subst measurable_cong)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   484
    apply (rule *)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   485
    apply (auto simp: space_pair_measure)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   486
    done
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   487
  then have "(\<lambda>x. integral\<^sup>N (L x) (indicator (A x))) \<in> borel_measurable M"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   488
    by (intro nn_integral_measurable_subprob_algebra2[where N=N] ereal_indicator_nonneg L)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   489
  then show "(\<lambda>x. emeasure (L x) (A x)) \<in> borel_measurable M"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   490
    apply (rule measurable_cong[THEN iffD1, rotated])
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   491
    apply (rule nn_integral_indicator)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   492
    apply (simp add: subprob_measurableD[OF L] **)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   493
    done
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   494
qed
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   495
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   496
lemma measure_measurable_subprob_algebra2:
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   497
  assumes A[measurable]: "(SIGMA x:space M. A x) \<in> sets (M \<Otimes>\<^sub>M N)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   498
  assumes L[measurable]: "L \<in> measurable M (subprob_algebra N)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   499
  shows "(\<lambda>x. measure (L x) (A x)) \<in> borel_measurable M"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   500
  unfolding measure_def
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   501
  by (intro borel_measurable_real_of_ereal emeasure_measurable_subprob_algebra2[OF assms])
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   502
58606
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   503
definition "select_sets M = (SOME N. sets M = sets (subprob_algebra N))"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   504
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   505
lemma select_sets1:
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   506
  "sets M = sets (subprob_algebra N) \<Longrightarrow> sets M = sets (subprob_algebra (select_sets M))"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   507
  unfolding select_sets_def by (rule someI)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   508
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   509
lemma sets_select_sets[simp]:
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   510
  assumes sets: "sets M = sets (subprob_algebra N)"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   511
  shows "sets (select_sets M) = sets N"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   512
  unfolding select_sets_def
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   513
proof (rule someI2)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   514
  show "sets M = sets (subprob_algebra N)"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   515
    by fact
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   516
next
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   517
  fix L assume "sets M = sets (subprob_algebra L)"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   518
  with sets have eq: "space (subprob_algebra N) = space (subprob_algebra L)"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   519
    by (intro sets_eq_imp_space_eq) simp
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   520
  show "sets L = sets N"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   521
  proof cases
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   522
    assume "space (subprob_algebra N) = {}"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   523
    with space_subprob_algebra_empty_iff[of N] space_subprob_algebra_empty_iff[of L]
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   524
    show ?thesis
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   525
      by (simp add: eq space_empty_iff)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   526
  next
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   527
    assume "space (subprob_algebra N) \<noteq> {}"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   528
    with eq show ?thesis
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   529
      by (fastforce simp add: space_subprob_algebra)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   530
  qed
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   531
qed
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   532
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   533
lemma space_select_sets[simp]:
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   534
  "sets M = sets (subprob_algebra N) \<Longrightarrow> space (select_sets M) = space N"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   535
  by (intro sets_eq_imp_space_eq sets_select_sets)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   536
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   537
section {* Join *}
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   538
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   539
definition join :: "'a measure measure \<Rightarrow> 'a measure" where
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   540
  "join M = measure_of (space (select_sets M)) (sets (select_sets M)) (\<lambda>B. \<integral>\<^sup>+ M'. emeasure M' B \<partial>M)"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   541
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   542
lemma
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   543
  shows space_join[simp]: "space (join M) = space (select_sets M)"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   544
    and sets_join[simp]: "sets (join M) = sets (select_sets M)"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   545
  by (simp_all add: join_def)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   546
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   547
lemma emeasure_join:
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   548
  assumes M[simp]: "sets M = sets (subprob_algebra N)" and A: "A \<in> sets N"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   549
  shows "emeasure (join M) A = (\<integral>\<^sup>+ M'. emeasure M' A \<partial>M)"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   550
proof (rule emeasure_measure_of[OF join_def])
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   551
  have eq: "borel_measurable M = borel_measurable (subprob_algebra N)"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   552
    by auto
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   553
  show "countably_additive (sets (join M)) (\<lambda>B. \<integral>\<^sup>+ M'. emeasure M' B \<partial>M)"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   554
  proof (rule countably_additiveI)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   555
    fix A :: "nat \<Rightarrow> 'a set" assume A: "range A \<subseteq> sets (join M)" "disjoint_family A"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   556
    have "(\<Sum>i. \<integral>\<^sup>+ M'. emeasure M' (A i) \<partial>M) = (\<integral>\<^sup>+M'. (\<Sum>i. emeasure M' (A i)) \<partial>M)"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   557
      using A by (subst nn_integral_suminf) (auto simp: measurable_emeasure_subprob_algebra eq)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   558
    also have "\<dots> = (\<integral>\<^sup>+M'. emeasure M' (\<Union>i. A i) \<partial>M)"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   559
    proof (rule nn_integral_cong)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   560
      fix M' assume "M' \<in> space M"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   561
      then show "(\<Sum>i. emeasure M' (A i)) = emeasure M' (\<Union>i. A i)"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   562
        using A sets_eq_imp_space_eq[OF M] by (simp add: suminf_emeasure space_subprob_algebra)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   563
    qed
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   564
    finally show "(\<Sum>i. \<integral>\<^sup>+M'. emeasure M' (A i) \<partial>M) = (\<integral>\<^sup>+M'. emeasure M' (\<Union>i. A i) \<partial>M)" .
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   565
  qed
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   566
qed (auto simp: A sets.space_closed positive_def nn_integral_nonneg)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   567
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   568
lemma measurable_join:
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   569
  "join \<in> measurable (subprob_algebra (subprob_algebra N)) (subprob_algebra N)"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   570
proof (cases "space N \<noteq> {}", rule measurable_subprob_algebra)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   571
  fix A assume "A \<in> sets N"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   572
  let ?B = "borel_measurable (subprob_algebra (subprob_algebra N))"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   573
  have "(\<lambda>M'. emeasure (join M') A) \<in> ?B \<longleftrightarrow> (\<lambda>M'. (\<integral>\<^sup>+ M''. emeasure M'' A \<partial>M')) \<in> ?B"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   574
  proof (rule measurable_cong)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   575
    fix M' assume "M' \<in> space (subprob_algebra (subprob_algebra N))"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   576
    then show "emeasure (join M') A = (\<integral>\<^sup>+ M''. emeasure M'' A \<partial>M')"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   577
      by (intro emeasure_join) (auto simp: space_subprob_algebra `A\<in>sets N`)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   578
  qed
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   579
  also have "(\<lambda>M'. \<integral>\<^sup>+M''. emeasure M'' A \<partial>M') \<in> ?B"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   580
    using measurable_emeasure_subprob_algebra[OF `A\<in>sets N`] emeasure_nonneg[of _ A]
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   581
    by (rule nn_integral_measurable_subprob_algebra)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   582
  finally show "(\<lambda>M'. emeasure (join M') A) \<in> borel_measurable (subprob_algebra (subprob_algebra N))" .
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   583
next
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   584
  assume [simp]: "space N \<noteq> {}"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   585
  fix M assume M: "M \<in> space (subprob_algebra (subprob_algebra N))"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   586
  then have "(\<integral>\<^sup>+M'. emeasure M' (space N) \<partial>M) \<le> (\<integral>\<^sup>+M'. 1 \<partial>M)"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   587
    apply (intro nn_integral_mono)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   588
    apply (auto simp: space_subprob_algebra 
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   589
                 dest!: sets_eq_imp_space_eq subprob_space.emeasure_space_le_1)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   590
    done
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   591
  with M show "subprob_space (join M)"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   592
    by (intro subprob_spaceI)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   593
       (auto simp: emeasure_join space_subprob_algebra M assms dest: subprob_space.emeasure_space_le_1)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   594
next
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   595
  assume "\<not>(space N \<noteq> {})"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   596
  thus ?thesis by (simp add: measurable_empty_iff space_subprob_algebra_empty_iff)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   597
qed (auto simp: space_subprob_algebra)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   598
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   599
lemma nn_integral_join:
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   600
  assumes f: "f \<in> borel_measurable N" "\<And>x. 0 \<le> f x" and M: "sets M = sets (subprob_algebra N)"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   601
  shows "(\<integral>\<^sup>+x. f x \<partial>join M) = (\<integral>\<^sup>+M'. \<integral>\<^sup>+x. f x \<partial>M' \<partial>M)"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   602
  using f
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   603
proof induct
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   604
  case (cong f g)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   605
  moreover have "integral\<^sup>N (join M) f = integral\<^sup>N (join M) g"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   606
    by (intro nn_integral_cong cong) (simp add: M)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   607
  moreover from M have "(\<integral>\<^sup>+ M'. integral\<^sup>N M' f \<partial>M) = (\<integral>\<^sup>+ M'. integral\<^sup>N M' g \<partial>M)"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   608
    by (intro nn_integral_cong cong)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   609
       (auto simp add: space_subprob_algebra dest!: sets_eq_imp_space_eq)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   610
  ultimately show ?case
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   611
    by simp
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   612
next
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   613
  case (set A)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   614
  moreover with M have "(\<integral>\<^sup>+ M'. integral\<^sup>N M' (indicator A) \<partial>M) = (\<integral>\<^sup>+ M'. emeasure M' A \<partial>M)" 
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   615
    by (intro nn_integral_cong nn_integral_indicator)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   616
       (auto simp: space_subprob_algebra dest!: sets_eq_imp_space_eq)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   617
  ultimately show ?case
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   618
    using M by (simp add: emeasure_join)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   619
next
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   620
  case (mult f c)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   621
  have "(\<integral>\<^sup>+ M'. \<integral>\<^sup>+ x. c * f x \<partial>M' \<partial>M) = (\<integral>\<^sup>+ M'. c * \<integral>\<^sup>+ x. f x \<partial>M' \<partial>M)"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   622
    using mult M
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   623
    by (intro nn_integral_cong nn_integral_cmult)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   624
       (auto simp add: space_subprob_algebra cong: measurable_cong dest!: sets_eq_imp_space_eq)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   625
  also have "\<dots> = c * (\<integral>\<^sup>+ M'. \<integral>\<^sup>+ x. f x \<partial>M' \<partial>M)"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   626
    using nn_integral_measurable_subprob_algebra[OF mult(3,4)]
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   627
    by (intro nn_integral_cmult mult) (simp add: M)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   628
  also have "\<dots> = c * (integral\<^sup>N (join M) f)"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   629
    by (simp add: mult)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   630
  also have "\<dots> = (\<integral>\<^sup>+ x. c * f x \<partial>join M)"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   631
    using mult(2,3) by (intro nn_integral_cmult[symmetric] mult) (simp add: M)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   632
  finally show ?case by simp
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   633
next
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   634
  case (add f g)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   635
  have "(\<integral>\<^sup>+ M'. \<integral>\<^sup>+ x. f x + g x \<partial>M' \<partial>M) = (\<integral>\<^sup>+ M'. (\<integral>\<^sup>+ x. f x \<partial>M') + (\<integral>\<^sup>+ x. g x \<partial>M') \<partial>M)"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   636
    using add M
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   637
    by (intro nn_integral_cong nn_integral_add)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   638
       (auto simp add: space_subprob_algebra cong: measurable_cong dest!: sets_eq_imp_space_eq)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   639
  also have "\<dots> = (\<integral>\<^sup>+ M'. \<integral>\<^sup>+ x. f x \<partial>M' \<partial>M) + (\<integral>\<^sup>+ M'. \<integral>\<^sup>+ x. g x \<partial>M' \<partial>M)"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   640
    using nn_integral_measurable_subprob_algebra[OF add(1,2)]
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   641
    using nn_integral_measurable_subprob_algebra[OF add(5,6)]
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   642
    by (intro nn_integral_add add) (simp_all add: M nn_integral_nonneg)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   643
  also have "\<dots> = (integral\<^sup>N (join M) f) + (integral\<^sup>N (join M) g)"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   644
    by (simp add: add)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   645
  also have "\<dots> = (\<integral>\<^sup>+ x. f x + g x \<partial>join M)"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   646
    using add by (intro nn_integral_add[symmetric] add) (simp_all add: M)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   647
  finally show ?case by (simp add: ac_simps)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   648
next
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   649
  case (seq F)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   650
  have "(\<integral>\<^sup>+ M'. \<integral>\<^sup>+ x. (SUP i. F i) x \<partial>M' \<partial>M) = (\<integral>\<^sup>+ M'. (SUP i. \<integral>\<^sup>+ x. F i x \<partial>M') \<partial>M)"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   651
    using seq M unfolding SUP_apply
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   652
    by (intro nn_integral_cong nn_integral_monotone_convergence_SUP)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   653
       (auto simp add: space_subprob_algebra cong: measurable_cong dest!: sets_eq_imp_space_eq)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   654
  also have "\<dots> = (SUP i. \<integral>\<^sup>+ M'. \<integral>\<^sup>+ x. F i x \<partial>M' \<partial>M)"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   655
    using nn_integral_measurable_subprob_algebra[OF seq(1,2)] seq
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   656
    by (intro nn_integral_monotone_convergence_SUP)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   657
       (simp_all add: M nn_integral_nonneg incseq_nn_integral incseq_def le_fun_def nn_integral_mono )
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   658
  also have "\<dots> = (SUP i. integral\<^sup>N (join M) (F i))"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   659
    by (simp add: seq)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   660
  also have "\<dots> = (\<integral>\<^sup>+ x. (SUP i. F i x) \<partial>join M)"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   661
    using seq by (intro nn_integral_monotone_convergence_SUP[symmetric] seq) (simp_all add: M)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   662
  finally show ?case by (simp add: ac_simps)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   663
qed
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   664
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   665
lemma join_assoc:
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   666
  assumes M: "sets M = sets (subprob_algebra (subprob_algebra N))"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   667
  shows "join (distr M (subprob_algebra N) join) = join (join M)"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   668
proof (rule measure_eqI)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   669
  fix A assume "A \<in> sets (join (distr M (subprob_algebra N) join))"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   670
  then have A: "A \<in> sets N" by simp
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   671
  show "emeasure (join (distr M (subprob_algebra N) join)) A = emeasure (join (join M)) A"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   672
    using measurable_join[of N]
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   673
    by (auto simp: M A nn_integral_distr emeasure_join measurable_emeasure_subprob_algebra emeasure_nonneg
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   674
                   sets_eq_imp_space_eq[OF M] space_subprob_algebra nn_integral_join[OF _ _ M]
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   675
             intro!: nn_integral_cong emeasure_join cong: measurable_cong)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   676
qed (simp add: M)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   677
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   678
lemma join_return: 
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   679
  assumes "sets M = sets N" and "subprob_space M"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   680
  shows "join (return (subprob_algebra N) M) = M"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   681
  by (rule measure_eqI)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   682
     (simp_all add: emeasure_join emeasure_nonneg space_subprob_algebra  
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   683
                    measurable_emeasure_subprob_algebra nn_integral_return assms)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   684
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   685
lemma join_return':
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   686
  assumes "sets N = sets M"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   687
  shows "join (distr M (subprob_algebra N) (return N)) = M"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   688
apply (rule measure_eqI)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   689
apply (simp add: assms)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   690
apply (subgoal_tac "return N \<in> measurable M (subprob_algebra N)")
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   691
apply (simp add: emeasure_join nn_integral_distr measurable_emeasure_subprob_algebra assms)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   692
apply (subst measurable_cong_sets, rule assms[symmetric], rule refl, rule return_measurable)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   693
done
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   694
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   695
lemma join_distr_distr:
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   696
  fixes f :: "'a \<Rightarrow> 'b" and M :: "'a measure measure" and N :: "'b measure"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   697
  assumes "sets M = sets (subprob_algebra R)" and "f \<in> measurable R N"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   698
  shows "join (distr M (subprob_algebra N) (\<lambda>M. distr M N f)) = distr (join M) N f" (is "?r = ?l")
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   699
proof (rule measure_eqI)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   700
  fix A assume "A \<in> sets ?r"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   701
  hence A_in_N: "A \<in> sets N" by simp
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   702
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   703
  from assms have "f \<in> measurable (join M) N" 
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   704
      by (simp cong: measurable_cong_sets)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   705
  moreover from assms and A_in_N have "f-`A \<inter> space R \<in> sets R" 
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   706
      by (intro measurable_sets) simp_all
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   707
  ultimately have "emeasure (distr (join M) N f) A = \<integral>\<^sup>+M'. emeasure M' (f-`A \<inter> space R) \<partial>M"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   708
      by (simp_all add: A_in_N emeasure_distr emeasure_join assms)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   709
  
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   710
  also have "... = \<integral>\<^sup>+ x. emeasure (distr x N f) A \<partial>M" using A_in_N
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   711
  proof (intro nn_integral_cong, subst emeasure_distr)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   712
    fix M' assume "M' \<in> space M"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   713
    from assms have "space M = space (subprob_algebra R)"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   714
        using sets_eq_imp_space_eq by blast
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   715
    with `M' \<in> space M` have [simp]: "sets M' = sets R" using space_subprob_algebra by blast
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   716
    show "f \<in> measurable M' N" by (simp cong: measurable_cong_sets add: assms)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   717
    have "space M' = space R" by (rule sets_eq_imp_space_eq) simp
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   718
    thus "emeasure M' (f -` A \<inter> space R) = emeasure M' (f -` A \<inter> space M')" by simp
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   719
  qed
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   720
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   721
  also have "(\<lambda>M. distr M N f) \<in> measurable M (subprob_algebra N)"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   722
      by (simp cong: measurable_cong_sets add: assms measurable_distr)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   723
  hence "(\<integral>\<^sup>+ x. emeasure (distr x N f) A \<partial>M) = 
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   724
             emeasure (join (distr M (subprob_algebra N) (\<lambda>M. distr M N f))) A"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   725
      by (simp_all add: emeasure_join assms A_in_N nn_integral_distr measurable_emeasure_subprob_algebra)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   726
  finally show "emeasure ?r A = emeasure ?l A" ..
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   727
qed simp
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   728
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   729
definition bind :: "'a measure \<Rightarrow> ('a \<Rightarrow> 'b measure) \<Rightarrow> 'b measure" where
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   730
  "bind M f = (if space M = {} then count_space {} else
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   731
    join (distr M (subprob_algebra (f (SOME x. x \<in> space M))) f))"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   732
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   733
adhoc_overloading Monad_Syntax.bind bind
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   734
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   735
lemma bind_empty: 
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   736
  "space M = {} \<Longrightarrow> bind M f = count_space {}"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   737
  by (simp add: bind_def)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   738
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   739
lemma bind_nonempty:
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   740
  "space M \<noteq> {} \<Longrightarrow> bind M f = join (distr M (subprob_algebra (f (SOME x. x \<in> space M))) f)"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   741
  by (simp add: bind_def)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   742
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   743
lemma sets_bind_empty: "sets M = {} \<Longrightarrow> sets (bind M f) = {{}}"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   744
  by (auto simp: bind_def)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   745
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   746
lemma space_bind_empty: "space M = {} \<Longrightarrow> space (bind M f) = {}"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   747
  by (simp add: bind_def)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   748
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   749
lemma sets_bind[simp]: 
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   750
  assumes "f \<in> measurable M (subprob_algebra N)" and "space M \<noteq> {}"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   751
  shows "sets (bind M f) = sets N"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   752
    using assms(2) by (force simp: bind_nonempty intro!: sets_kernel[OF assms(1) someI_ex])
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   753
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   754
lemma space_bind[simp]: 
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   755
  assumes "f \<in> measurable M (subprob_algebra N)" and "space M \<noteq> {}"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   756
  shows "space (bind M f) = space N"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   757
    using assms by (intro sets_eq_imp_space_eq sets_bind)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   758
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   759
lemma bind_cong: 
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   760
  assumes "\<forall>x \<in> space M. f x = g x"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   761
  shows "bind M f = bind M g"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   762
proof (cases "space M = {}")
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   763
  assume "space M \<noteq> {}"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   764
  hence "(SOME x. x \<in> space M) \<in> space M" by (rule_tac someI_ex) blast
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   765
  with assms have "f (SOME x. x \<in> space M) = g (SOME x. x \<in> space M)" by blast
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   766
  with `space M \<noteq> {}` and assms show ?thesis by (simp add: bind_nonempty cong: distr_cong)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   767
qed (simp add: bind_empty)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   768
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   769
lemma bind_nonempty':
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   770
  assumes "f \<in> measurable M (subprob_algebra N)" "x \<in> space M"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   771
  shows "bind M f = join (distr M (subprob_algebra N) f)"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   772
  using assms
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   773
  apply (subst bind_nonempty, blast)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   774
  apply (subst subprob_algebra_cong[OF sets_kernel[OF assms(1) someI_ex]], blast)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   775
  apply (simp add: subprob_algebra_cong[OF sets_kernel[OF assms]])
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   776
  done
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   777
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   778
lemma bind_nonempty'':
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   779
  assumes "f \<in> measurable M (subprob_algebra N)" "space M \<noteq> {}"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   780
  shows "bind M f = join (distr M (subprob_algebra N) f)"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   781
  using assms by (auto intro: bind_nonempty')
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   782
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   783
lemma emeasure_bind:
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   784
    "\<lbrakk>space M \<noteq> {}; f \<in> measurable M (subprob_algebra N);X \<in> sets N\<rbrakk>
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   785
      \<Longrightarrow> emeasure (M \<guillemotright>= f) X = \<integral>\<^sup>+x. emeasure (f x) X \<partial>M"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   786
  by (simp add: bind_nonempty'' emeasure_join nn_integral_distr measurable_emeasure_subprob_algebra)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   787
59000
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   788
lemma nn_integral_bind:
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   789
  assumes f: "f \<in> borel_measurable B" "\<And>x. 0 \<le> f x"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   790
  assumes N: "N \<in> measurable M (subprob_algebra B)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   791
  shows "(\<integral>\<^sup>+x. f x \<partial>(M \<guillemotright>= N)) = (\<integral>\<^sup>+x. \<integral>\<^sup>+y. f y \<partial>N x \<partial>M)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   792
proof cases
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   793
  assume M: "space M \<noteq> {}" show ?thesis
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   794
    unfolding bind_nonempty''[OF N M] nn_integral_join[OF f sets_distr]
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   795
    by (rule nn_integral_distr[OF N nn_integral_measurable_subprob_algebra[OF f]])
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   796
qed (simp add: bind_empty space_empty[of M] nn_integral_count_space)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   797
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   798
lemma AE_bind:
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   799
  assumes P[measurable]: "Measurable.pred B P"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   800
  assumes N[measurable]: "N \<in> measurable M (subprob_algebra B)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   801
  shows "(AE x in M \<guillemotright>= N. P x) \<longleftrightarrow> (AE x in M. AE y in N x. P y)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   802
proof cases
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   803
  assume M: "space M = {}" show ?thesis
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   804
    unfolding bind_empty[OF M] unfolding space_empty[OF M] by (simp add: AE_count_space)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   805
next
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   806
  assume M: "space M \<noteq> {}"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   807
  have *: "(\<integral>\<^sup>+x. indicator {x. \<not> P x} x \<partial>(M \<guillemotright>= N)) = (\<integral>\<^sup>+x. indicator {x\<in>space B. \<not> P x} x \<partial>(M \<guillemotright>= N))"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   808
    by (intro nn_integral_cong) (simp add: space_bind[OF N M] split: split_indicator)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   809
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   810
  have "(AE x in M \<guillemotright>= N. P x) \<longleftrightarrow> (\<integral>\<^sup>+ x. integral\<^sup>N (N x) (indicator {x \<in> space B. \<not> P x}) \<partial>M) = 0"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   811
    by (simp add: AE_iff_nn_integral sets_bind[OF N M] space_bind[OF N M] * nn_integral_bind[where B=B]
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   812
             del: nn_integral_indicator)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   813
  also have "\<dots> = (AE x in M. AE y in N x. P y)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   814
    apply (subst nn_integral_0_iff_AE)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   815
    apply (rule measurable_compose[OF N nn_integral_measurable_subprob_algebra])
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   816
    apply measurable
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   817
    apply (intro eventually_subst AE_I2)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   818
    apply simp
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   819
    apply (subst nn_integral_0_iff_AE)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   820
    apply (simp add: subprob_measurableD(3)[OF N])
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   821
    apply (auto simp add: ereal_indicator_le_0 subprob_measurableD(1)[OF N] intro!: eventually_subst)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   822
    done
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   823
  finally show ?thesis .
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   824
qed
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   825
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   826
lemma measurable_bind':
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   827
  assumes M1: "f \<in> measurable M (subprob_algebra N)" and
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   828
          M2: "split g \<in> measurable (M \<Otimes>\<^sub>M N) (subprob_algebra R)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   829
  shows "(\<lambda>x. bind (f x) (g x)) \<in> measurable M (subprob_algebra R)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   830
proof (subst measurable_cong)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   831
  fix x assume x_in_M: "x \<in> space M"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   832
  with assms have "space (f x) \<noteq> {}" 
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   833
      by (blast dest: subprob_space_kernel subprob_space.subprob_not_empty)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   834
  moreover from M2 x_in_M have "g x \<in> measurable (f x) (subprob_algebra R)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   835
      by (subst measurable_cong_sets[OF sets_kernel[OF M1 x_in_M] refl])
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   836
         (auto dest: measurable_Pair2)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   837
  ultimately show "bind (f x) (g x) = join (distr (f x) (subprob_algebra R) (g x))" 
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   838
      by (simp_all add: bind_nonempty'')
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   839
next
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   840
  show "(\<lambda>w. join (distr (f w) (subprob_algebra R) (g w))) \<in> measurable M (subprob_algebra R)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   841
    apply (rule measurable_compose[OF _ measurable_join])
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   842
    apply (rule measurable_distr2[OF M2 M1])
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   843
    done
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   844
qed
58606
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   845
59000
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   846
lemma measurable_bind:
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   847
  assumes M1: "f \<in> measurable M (subprob_algebra N)" and
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   848
          M2: "(\<lambda>x. g (fst x) (snd x)) \<in> measurable (M \<Otimes>\<^sub>M N) (subprob_algebra R)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   849
  shows "(\<lambda>x. bind (f x) (g x)) \<in> measurable M (subprob_algebra R)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   850
  using assms by (auto intro: measurable_bind' simp: measurable_split_conv)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   851
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   852
lemma measurable_bind2:
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   853
  assumes "f \<in> measurable M (subprob_algebra N)" and "g \<in> measurable N (subprob_algebra R)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   854
  shows "(\<lambda>x. bind (f x) g) \<in> measurable M (subprob_algebra R)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   855
    using assms by (intro measurable_bind' measurable_const) auto
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   856
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   857
lemma subprob_space_bind:
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   858
  assumes "subprob_space M" "f \<in> measurable M (subprob_algebra N)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   859
  shows "subprob_space (M \<guillemotright>= f)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   860
proof (rule subprob_space_kernel[of "\<lambda>x. x \<guillemotright>= f"])
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   861
  show "(\<lambda>x. x \<guillemotright>= f) \<in> measurable (subprob_algebra M) (subprob_algebra N)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   862
    by (rule measurable_bind, rule measurable_ident_sets, rule refl, 
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   863
        rule measurable_compose[OF measurable_snd assms(2)])
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   864
  from assms(1) show "M \<in> space (subprob_algebra M)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   865
    by (simp add: space_subprob_algebra)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   866
qed
58606
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   867
59000
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   868
lemma (in prob_space) prob_space_bind: 
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   869
  assumes ae: "AE x in M. prob_space (N x)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   870
    and N[measurable]: "N \<in> measurable M (subprob_algebra S)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   871
  shows "prob_space (M \<guillemotright>= N)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   872
proof
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   873
  have "emeasure (M \<guillemotright>= N) (space (M \<guillemotright>= N)) = (\<integral>\<^sup>+x. emeasure (N x) (space (N x)) \<partial>M)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   874
    by (subst emeasure_bind[where N=S])
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   875
       (auto simp: not_empty space_bind[OF N] subprob_measurableD[OF N] intro!: nn_integral_cong)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   876
  also have "\<dots> = (\<integral>\<^sup>+x. 1 \<partial>M)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   877
    using ae by (intro nn_integral_cong_AE, eventually_elim) (rule prob_space.emeasure_space_1)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   878
  finally show "emeasure (M \<guillemotright>= N) (space (M \<guillemotright>= N)) = 1"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   879
    by (simp add: emeasure_space_1)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   880
qed
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   881
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   882
lemma (in subprob_space) bind_in_space:
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   883
  "A \<in> measurable M (subprob_algebra N) \<Longrightarrow> (M \<guillemotright>= A) \<in> space (subprob_algebra N)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   884
  by (auto simp add: space_subprob_algebra subprob_not_empty intro!: subprob_space_bind)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   885
     unfold_locales
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   886
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   887
lemma (in subprob_space) measure_bind:
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   888
  assumes f: "f \<in> measurable M (subprob_algebra N)" and X: "X \<in> sets N"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   889
  shows "measure (M \<guillemotright>= f) X = \<integral>x. measure (f x) X \<partial>M"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   890
proof -
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   891
  interpret Mf: subprob_space "M \<guillemotright>= f"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   892
    by (rule subprob_space_bind[OF _ f]) unfold_locales
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   893
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   894
  { fix x assume "x \<in> space M"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   895
    from f[THEN measurable_space, OF this] interpret subprob_space "f x"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   896
      by (simp add: space_subprob_algebra)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   897
    have "emeasure (f x) X = ereal (measure (f x) X)" "measure (f x) X \<le> 1"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   898
      by (auto simp: emeasure_eq_measure subprob_measure_le_1) }
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   899
  note this[simp]
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   900
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   901
  have "emeasure (M \<guillemotright>= f) X = \<integral>\<^sup>+x. emeasure (f x) X \<partial>M"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   902
    using subprob_not_empty f X by (rule emeasure_bind)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   903
  also have "\<dots> = \<integral>\<^sup>+x. ereal (measure (f x) X) \<partial>M"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   904
    by (intro nn_integral_cong) simp
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   905
  also have "\<dots> = \<integral>x. measure (f x) X \<partial>M"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   906
    by (intro nn_integral_eq_integral integrable_const_bound[where B=1]
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   907
              measure_measurable_subprob_algebra2[OF _ f] pair_measureI X)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   908
       (auto simp: measure_nonneg)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   909
  finally show ?thesis
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   910
    by (simp add: Mf.emeasure_eq_measure)
58606
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   911
qed
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   912
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   913
lemma emeasure_bind_const: 
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   914
    "space M \<noteq> {} \<Longrightarrow> X \<in> sets N \<Longrightarrow> subprob_space N \<Longrightarrow> 
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   915
         emeasure (M \<guillemotright>= (\<lambda>x. N)) X = emeasure N X * emeasure M (space M)"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   916
  by (simp add: bind_nonempty emeasure_join nn_integral_distr 
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   917
                space_subprob_algebra measurable_emeasure_subprob_algebra emeasure_nonneg)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   918
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   919
lemma emeasure_bind_const':
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   920
  assumes "subprob_space M" "subprob_space N"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   921
  shows "emeasure (M \<guillemotright>= (\<lambda>x. N)) X = emeasure N X * emeasure M (space M)"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   922
using assms
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   923
proof (case_tac "X \<in> sets N")
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   924
  fix X assume "X \<in> sets N"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   925
  thus "emeasure (M \<guillemotright>= (\<lambda>x. N)) X = emeasure N X * emeasure M (space M)" using assms
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   926
      by (subst emeasure_bind_const) 
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   927
         (simp_all add: subprob_space.subprob_not_empty subprob_space.emeasure_space_le_1)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   928
next
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   929
  fix X assume "X \<notin> sets N"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   930
  with assms show "emeasure (M \<guillemotright>= (\<lambda>x. N)) X = emeasure N X * emeasure M (space M)"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   931
      by (simp add: sets_bind[of _ _ N] subprob_space.subprob_not_empty
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   932
                    space_subprob_algebra emeasure_notin_sets)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   933
qed
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   934
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   935
lemma emeasure_bind_const_prob_space:
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   936
  assumes "prob_space M" "subprob_space N"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   937
  shows "emeasure (M \<guillemotright>= (\<lambda>x. N)) X = emeasure N X"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   938
  using assms by (simp add: emeasure_bind_const' prob_space_imp_subprob_space 
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   939
                            prob_space.emeasure_space_1)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
   940
59000
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   941
lemma bind_return: 
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   942
  assumes "f \<in> measurable M (subprob_algebra N)" and "x \<in> space M"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   943
  shows "bind (return M x) f = f x"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   944
  using sets_kernel[OF assms] assms
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   945
  by (simp_all add: distr_return join_return subprob_space_kernel bind_nonempty'
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   946
               cong: subprob_algebra_cong)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   947
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   948
lemma bind_return':
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   949
  shows "bind M (return M) = M"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   950
  by (cases "space M = {}")
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   951
     (simp_all add: bind_empty space_empty[symmetric] bind_nonempty join_return' 
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   952
               cong: subprob_algebra_cong)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   953
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   954
lemma distr_bind:
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   955
  assumes N: "N \<in> measurable M (subprob_algebra K)" "space M \<noteq> {}"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   956
  assumes f: "f \<in> measurable K R"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   957
  shows "distr (M \<guillemotright>= N) R f = (M \<guillemotright>= (\<lambda>x. distr (N x) R f))"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   958
  unfolding bind_nonempty''[OF N]
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   959
  apply (subst bind_nonempty''[OF measurable_compose[OF N(1) measurable_distr] N(2)])
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   960
  apply (rule f)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   961
  apply (simp add: join_distr_distr[OF _ f, symmetric])
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   962
  apply (subst distr_distr[OF measurable_distr, OF f N(1)])
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   963
  apply (simp add: comp_def)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   964
  done
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   965
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   966
lemma bind_distr:
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   967
  assumes f[measurable]: "f \<in> measurable M X"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   968
  assumes N[measurable]: "N \<in> measurable X (subprob_algebra K)" and "space M \<noteq> {}"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   969
  shows "(distr M X f \<guillemotright>= N) = (M \<guillemotright>= (\<lambda>x. N (f x)))"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   970
proof -
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   971
  have "space X \<noteq> {}" "space M \<noteq> {}"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   972
    using `space M \<noteq> {}` f[THEN measurable_space] by auto
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   973
  then show ?thesis
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   974
    by (simp add: bind_nonempty''[where N=K] distr_distr comp_def)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   975
qed
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   976
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   977
lemma bind_count_space_singleton:
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   978
  assumes "subprob_space (f x)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   979
  shows "count_space {x} \<guillemotright>= f = f x"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   980
proof-
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   981
  have A: "\<And>A. A \<subseteq> {x} \<Longrightarrow> A = {} \<or> A = {x}" by auto
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   982
  have "count_space {x} = return (count_space {x}) x"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   983
    by (intro measure_eqI) (auto dest: A)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   984
  also have "... \<guillemotright>= f = f x"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   985
    by (subst bind_return[of _ _ "f x"]) (auto simp: space_subprob_algebra assms)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   986
  finally show ?thesis .
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   987
qed
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   988
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   989
lemma restrict_space_bind:
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   990
  assumes N: "N \<in> measurable M (subprob_algebra K)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   991
  assumes "space M \<noteq> {}"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   992
  assumes X[simp]: "X \<in> sets K" "X \<noteq> {}"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   993
  shows "restrict_space (bind M N) X = bind M (\<lambda>x. restrict_space (N x) X)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   994
proof (rule measure_eqI)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   995
  fix A assume "A \<in> sets (restrict_space (M \<guillemotright>= N) X)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   996
  with X have "A \<in> sets K" "A \<subseteq> X"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   997
    by (auto simp: sets_restrict_space sets_bind[OF assms(1,2)])
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   998
  then show "emeasure (restrict_space (M \<guillemotright>= N) X) A = emeasure (M \<guillemotright>= (\<lambda>x. restrict_space (N x) X)) A"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
   999
    using assms
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
  1000
    apply (subst emeasure_restrict_space)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
  1001
    apply (simp_all add: space_bind[OF assms(1,2)] sets_bind[OF assms(1,2)] emeasure_bind[OF assms(2,1)])
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
  1002
    apply (subst emeasure_bind[OF _ restrict_space_measurable[OF _ _ N]])
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
  1003
    apply (auto simp: sets_restrict_space emeasure_restrict_space space_subprob_algebra
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
  1004
                intro!: nn_integral_cong dest!: measurable_space)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
  1005
    done
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
  1006
qed (simp add: sets_restrict_space sets_bind[OF assms(1,2)] sets_bind[OF restrict_space_measurable[OF assms(4,3,1)] assms(2)])
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
  1007
58606
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1008
lemma bind_const': "\<lbrakk>prob_space M; subprob_space N\<rbrakk> \<Longrightarrow> M \<guillemotright>= (\<lambda>x. N) = N"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1009
  by (intro measure_eqI) 
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1010
     (simp_all add: space_subprob_algebra prob_space.not_empty emeasure_bind_const_prob_space)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1011
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1012
lemma bind_return_distr: 
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1013
    "space M \<noteq> {} \<Longrightarrow> f \<in> measurable M N \<Longrightarrow> bind M (return N \<circ> f) = distr M N f"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1014
  apply (simp add: bind_nonempty)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1015
  apply (subst subprob_algebra_cong)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1016
  apply (rule sets_return)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1017
  apply (subst distr_distr[symmetric])
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1018
  apply (auto intro!: return_measurable simp: distr_distr[symmetric] join_return')
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1019
  done
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1020
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1021
lemma bind_assoc:
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1022
  fixes f :: "'a \<Rightarrow> 'b measure" and g :: "'b \<Rightarrow> 'c measure"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1023
  assumes M1: "f \<in> measurable M (subprob_algebra N)" and M2: "g \<in> measurable N (subprob_algebra R)"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1024
  shows "bind (bind M f) g = bind M (\<lambda>x. bind (f x) g)"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1025
proof (cases "space M = {}")
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1026
  assume [simp]: "space M \<noteq> {}"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1027
  from assms have [simp]: "space N \<noteq> {}" "space R \<noteq> {}"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1028
      by (auto simp: measurable_empty_iff space_subprob_algebra_empty_iff)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1029
  from assms have sets_fx[simp]: "\<And>x. x \<in> space M \<Longrightarrow> sets (f x) = sets N"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1030
      by (simp add: sets_kernel)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1031
  have ex_in: "\<And>A. A \<noteq> {} \<Longrightarrow> \<exists>x. x \<in> A" by blast
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1032
  note sets_some[simp] = sets_kernel[OF M1 someI_ex[OF ex_in[OF `space M \<noteq> {}`]]]
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1033
                         sets_kernel[OF M2 someI_ex[OF ex_in[OF `space N \<noteq> {}`]]]
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1034
  note space_some[simp] = sets_eq_imp_space_eq[OF this(1)] sets_eq_imp_space_eq[OF this(2)]
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1035
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1036
  have "bind M (\<lambda>x. bind (f x) g) = 
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1037
        join (distr M (subprob_algebra R) (join \<circ> (\<lambda>x. (distr x (subprob_algebra R) g)) \<circ> f))"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1038
    by (simp add: sets_eq_imp_space_eq[OF sets_fx] bind_nonempty o_def
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1039
             cong: subprob_algebra_cong distr_cong)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1040
  also have "distr M (subprob_algebra R) (join \<circ> (\<lambda>x. (distr x (subprob_algebra R) g)) \<circ> f) =
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1041
             distr (distr (distr M (subprob_algebra N) f)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1042
                          (subprob_algebra (subprob_algebra R))
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1043
                          (\<lambda>x. distr x (subprob_algebra R) g)) 
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1044
                   (subprob_algebra R) join"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1045
      apply (subst distr_distr, 
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1046
             (blast intro: measurable_comp measurable_distr measurable_join M1 M2)+)+
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1047
      apply (simp add: o_assoc)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1048
      done
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1049
  also have "join ... = bind (bind M f) g"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1050
      by (simp add: join_assoc join_distr_distr M2 bind_nonempty cong: subprob_algebra_cong)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1051
  finally show ?thesis ..
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1052
qed (simp add: bind_empty)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1053
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1054
lemma double_bind_assoc:
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1055
  assumes Mg: "g \<in> measurable N (subprob_algebra N')"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1056
  assumes Mf: "f \<in> measurable M (subprob_algebra M')"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1057
  assumes Mh: "split h \<in> measurable (M \<Otimes>\<^sub>M M') N"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1058
  shows "do {x \<leftarrow> M; y \<leftarrow> f x; g (h x y)} = do {x \<leftarrow> M; y \<leftarrow> f x; return N (h x y)} \<guillemotright>= g"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1059
proof-
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1060
  have "do {x \<leftarrow> M; y \<leftarrow> f x; return N (h x y)} \<guillemotright>= g = 
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1061
            do {x \<leftarrow> M; do {y \<leftarrow> f x; return N (h x y)} \<guillemotright>= g}"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1062
    using Mh by (auto intro!: bind_assoc measurable_bind'[OF Mf] Mf Mg
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1063
                      measurable_compose[OF _ return_measurable] simp: measurable_split_conv)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1064
  also from Mh have "\<And>x. x \<in> space M \<Longrightarrow> h x \<in> measurable M' N" by measurable
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1065
  hence "do {x \<leftarrow> M; do {y \<leftarrow> f x; return N (h x y)} \<guillemotright>= g} = 
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1066
            do {x \<leftarrow> M; y \<leftarrow> f x; return N (h x y) \<guillemotright>= g}"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1067
    apply (intro ballI bind_cong bind_assoc)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1068
    apply (subst measurable_cong_sets[OF sets_kernel[OF Mf] refl], simp)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1069
    apply (rule measurable_compose[OF _ return_measurable], auto intro: Mg)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1070
    done
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1071
  also have "\<And>x. x \<in> space M \<Longrightarrow> space (f x) = space M'"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1072
    by (intro sets_eq_imp_space_eq sets_kernel[OF Mf])
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1073
  with measurable_space[OF Mh] 
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1074
    have "do {x \<leftarrow> M; y \<leftarrow> f x; return N (h x y) \<guillemotright>= g} = do {x \<leftarrow> M; y \<leftarrow> f x; g (h x y)}"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1075
    by (intro ballI bind_cong bind_return[OF Mg]) (auto simp: space_pair_measure)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1076
  finally show ?thesis ..
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1077
qed
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1078
59000
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
  1079
lemma (in pair_prob_space) pair_measure_eq_bind:
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
  1080
  "(M1 \<Otimes>\<^sub>M M2) = (M1 \<guillemotright>= (\<lambda>x. M2 \<guillemotright>= (\<lambda>y. return (M1 \<Otimes>\<^sub>M M2) (x, y))))"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
  1081
proof (rule measure_eqI)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
  1082
  have ps_M2: "prob_space M2" by unfold_locales
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
  1083
  note return_measurable[measurable]
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
  1084
  have 1: "(\<lambda>x. M2 \<guillemotright>= (\<lambda>y. return (M1 \<Otimes>\<^sub>M M2) (x, y))) \<in> measurable M1 (subprob_algebra (M1 \<Otimes>\<^sub>M M2))"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
  1085
    by (auto simp add: space_subprob_algebra ps_M2
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
  1086
             intro!: measurable_bind[where N=M2] measurable_const prob_space_imp_subprob_space)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
  1087
  show "sets (M1 \<Otimes>\<^sub>M M2) = sets (M1 \<guillemotright>= (\<lambda>x. M2 \<guillemotright>= (\<lambda>y. return (M1 \<Otimes>\<^sub>M M2) (x, y))))"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
  1088
    by (simp add: M1.not_empty sets_bind[OF 1])
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
  1089
  fix A assume [measurable]: "A \<in> sets (M1 \<Otimes>\<^sub>M M2)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
  1090
  show "emeasure (M1 \<Otimes>\<^sub>M M2) A = emeasure (M1 \<guillemotright>= (\<lambda>x. M2 \<guillemotright>= (\<lambda>y. return (M1 \<Otimes>\<^sub>M M2) (x, y)))) A"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
  1091
    by (auto simp: M2.emeasure_pair_measure emeasure_bind[OF _ 1] M1.not_empty
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
  1092
                          emeasure_bind[where N="M1 \<Otimes>\<^sub>M M2"] M2.not_empty
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
  1093
             intro!: nn_integral_cong)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
  1094
qed
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
  1095
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
  1096
lemma (in pair_prob_space) bind_rotate:
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
  1097
  assumes C[measurable]: "(\<lambda>(x, y). C x y) \<in> measurable (M1 \<Otimes>\<^sub>M M2) (subprob_algebra N)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
  1098
  shows "(M1 \<guillemotright>= (\<lambda>x. M2 \<guillemotright>= (\<lambda>y. C x y))) = (M2 \<guillemotright>= (\<lambda>y. M1 \<guillemotright>= (\<lambda>x. C x y)))"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
  1099
proof - 
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
  1100
  interpret swap: pair_prob_space M2 M1 by unfold_locales
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
  1101
  note measurable_bind[where N="M2", measurable]
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
  1102
  note measurable_bind[where N="M1", measurable]
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
  1103
  have [simp]: "M1 \<in> space (subprob_algebra M1)" "M2 \<in> space (subprob_algebra M2)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
  1104
    by (auto simp: space_subprob_algebra) unfold_locales
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
  1105
  have "(M1 \<guillemotright>= (\<lambda>x. M2 \<guillemotright>= (\<lambda>y. C x y))) = 
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
  1106
    (M1 \<guillemotright>= (\<lambda>x. M2 \<guillemotright>= (\<lambda>y. return (M1 \<Otimes>\<^sub>M M2) (x, y)))) \<guillemotright>= (\<lambda>(x, y). C x y)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
  1107
    by (auto intro!: bind_cong simp: bind_return[where N=N] space_pair_measure bind_assoc[where N="M1 \<Otimes>\<^sub>M M2" and R=N])
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
  1108
  also have "\<dots> = (distr (M2 \<Otimes>\<^sub>M M1) (M1 \<Otimes>\<^sub>M M2) (\<lambda>(x, y). (y, x))) \<guillemotright>= (\<lambda>(x, y). C x y)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
  1109
    unfolding pair_measure_eq_bind[symmetric] distr_pair_swap[symmetric] ..
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
  1110
  also have "\<dots> = (M2 \<guillemotright>= (\<lambda>x. M1 \<guillemotright>= (\<lambda>y. return (M2 \<Otimes>\<^sub>M M1) (x, y)))) \<guillemotright>= (\<lambda>(y, x). C x y)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
  1111
    unfolding swap.pair_measure_eq_bind[symmetric]
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
  1112
    by (auto simp add: space_pair_measure M1.not_empty M2.not_empty bind_distr[OF _ C] intro!: bind_cong)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
  1113
  also have "\<dots> = (M2 \<guillemotright>= (\<lambda>y. M1 \<guillemotright>= (\<lambda>x. C x y)))"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
  1114
    by (auto intro!: bind_cong simp: bind_return[where N=N] space_pair_measure bind_assoc[where N="M2 \<Otimes>\<^sub>M M1" and R=N])
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
  1115
  finally show ?thesis .
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
  1116
qed
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58608
diff changeset
  1117
58608
5b7f0b5da884 fix document generation for HOL-Probability
hoelzl
parents: 58606
diff changeset
  1118
section {* Measures form a $\omega$-chain complete partial order *}
58606
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1119
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1120
definition SUP_measure :: "(nat \<Rightarrow> 'a measure) \<Rightarrow> 'a measure" where
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1121
  "SUP_measure M = measure_of (\<Union>i. space (M i)) (\<Union>i. sets (M i)) (\<lambda>A. SUP i. emeasure (M i) A)"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1122
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1123
lemma
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1124
  assumes const: "\<And>i j. sets (M i) = sets (M j)"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1125
  shows space_SUP_measure: "space (SUP_measure M) = space (M i)" (is ?sp)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1126
    and sets_SUP_measure: "sets (SUP_measure M) = sets (M i)" (is ?st)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1127
proof -
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1128
  have "(\<Union>i. sets (M i)) = sets (M i)"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1129
    using const by auto
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1130
  moreover have "(\<Union>i. space (M i)) = space (M i)"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1131
    using const[THEN sets_eq_imp_space_eq] by auto
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1132
  moreover have "\<And>i. sets (M i) \<subseteq> Pow (space (M i))"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1133
    by (auto dest: sets.sets_into_space)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1134
  ultimately show ?sp ?st
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1135
    by (simp_all add: SUP_measure_def)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1136
qed
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1137
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1138
lemma emeasure_SUP_measure:
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1139
  assumes const: "\<And>i j. sets (M i) = sets (M j)"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1140
    and mono: "mono (\<lambda>i. emeasure (M i))"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1141
  shows "emeasure (SUP_measure M) A = (SUP i. emeasure (M i) A)"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1142
proof cases
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1143
  assume "A \<in> sets (SUP_measure M)"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1144
  show ?thesis
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1145
  proof (rule emeasure_measure_of[OF SUP_measure_def])
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1146
    show "countably_additive (sets (SUP_measure M)) (\<lambda>A. SUP i. emeasure (M i) A)"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1147
    proof (rule countably_additiveI)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1148
      fix A :: "nat \<Rightarrow> 'a set" assume "range A \<subseteq> sets (SUP_measure M)"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1149
      then have "\<And>i j. A i \<in> sets (M j)"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1150
        using sets_SUP_measure[of M, OF const] by simp
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1151
      moreover assume "disjoint_family A"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1152
      ultimately show "(\<Sum>i. SUP ia. emeasure (M ia) (A i)) = (SUP i. emeasure (M i) (\<Union>i. A i))"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1153
        using mono by (subst suminf_SUP_eq) (auto simp: mono_def le_fun_def intro!: SUP_cong suminf_emeasure)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1154
    qed
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1155
    show "positive (sets (SUP_measure M)) (\<lambda>A. SUP i. emeasure (M i) A)"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1156
      by (auto simp: positive_def intro: SUP_upper2)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1157
    show "(\<Union>i. sets (M i)) \<subseteq> Pow (\<Union>i. space (M i))"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1158
      using sets.sets_into_space by auto
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1159
  qed fact
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1160
next
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1161
  assume "A \<notin> sets (SUP_measure M)"
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1162
  with sets_SUP_measure[of M, OF const] show ?thesis
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1163
    by (simp add: emeasure_notin_sets)
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1164
qed
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1165
9c66f7c541fb add Giry monad
hoelzl
parents:
diff changeset
  1166
end