src/HOL/Probability/Conditional_Expectation.thy
changeset 67226 ec32cdaab97b
parent 64283 979cdfdf7a79
child 67977 557ea2740125
equal deleted inserted replaced
67225:cb34f5f49a08 67226:ec32cdaab97b
     6 
     6 
     7 theory Conditional_Expectation
     7 theory Conditional_Expectation
     8 imports Probability_Measure
     8 imports Probability_Measure
     9 begin
     9 begin
    10 
    10 
    11 subsection {*Restricting a measure to a sub-sigma-algebra*}
    11 subsection \<open>Restricting a measure to a sub-sigma-algebra\<close>
    12 
    12 
    13 definition subalgebra::"'a measure \<Rightarrow> 'a measure \<Rightarrow> bool" where
    13 definition subalgebra::"'a measure \<Rightarrow> 'a measure \<Rightarrow> bool" where
    14   "subalgebra M F = ((space F = space M) \<and> (sets F \<subseteq> sets M))"
    14   "subalgebra M F = ((space F = space M) \<and> (sets F \<subseteq> sets M))"
    15 
    15 
    16 lemma sub_measure_space:
    16 lemma sub_measure_space:
    81   define U where "U = {x \<in> space M. \<not>(P x)}"
    81   define U where "U = {x \<in> space M. \<not>(P x)}"
    82   then have *: "U = {x \<in> space F. \<not>(P x)}" using assms(1) by (simp add: subalgebra_def)
    82   then have *: "U = {x \<in> space F. \<not>(P x)}" using assms(1) by (simp add: subalgebra_def)
    83   then have "U \<in> sets F" by simp
    83   then have "U \<in> sets F" by simp
    84   then have "U \<in> sets M" using assms(1) by (meson subalgebra_def subsetD)
    84   then have "U \<in> sets M" using assms(1) by (meson subalgebra_def subsetD)
    85   then have "U \<in> null_sets M" unfolding U_def using assms(2) using AE_iff_measurable by blast
    85   then have "U \<in> null_sets M" unfolding U_def using assms(2) using AE_iff_measurable by blast
    86   then have "U \<in> null_sets (restr_to_subalg M F)" using null_sets_restr_to_subalg[OF assms(1)] `U \<in> sets F` by auto
    86   then have "U \<in> null_sets (restr_to_subalg M F)" using null_sets_restr_to_subalg[OF assms(1)] \<open>U \<in> sets F\<close> by auto
    87   then show ?thesis using * by (metis (no_types, lifting) Collect_mono U_def eventually_ae_filter space_restr_to_subalg)
    87   then show ?thesis using * by (metis (no_types, lifting) Collect_mono U_def eventually_ae_filter space_restr_to_subalg)
    88 qed
    88 qed
    89 
    89 
    90 lemma prob_space_restr_to_subalg:
    90 lemma prob_space_restr_to_subalg:
    91   assumes "subalgebra M F"
    91   assumes "subalgebra M F"
   117   assumes "subalgebra M F"
   117   assumes "subalgebra M F"
   118           "f \<in> measurable F N"
   118           "f \<in> measurable F N"
   119   shows "f \<in> measurable M N"
   119   shows "f \<in> measurable M N"
   120 using assms unfolding measurable_def subalgebra_def by auto
   120 using assms unfolding measurable_def subalgebra_def by auto
   121 
   121 
   122 text{*The following is the direct transposition of \verb+nn_integral_subalgebra+
   122 text\<open>The following is the direct transposition of \verb+nn_integral_subalgebra+
   123 (from \verb+Nonnegative_Lebesgue_Integration+) in the
   123 (from \verb+Nonnegative_Lebesgue_Integration+) in the
   124 current notations, with the removal of the useless assumption $f \geq 0$.*}
   124 current notations, with the removal of the useless assumption $f \geq 0$.\<close>
   125 
   125 
   126 lemma nn_integral_subalgebra2:
   126 lemma nn_integral_subalgebra2:
   127   assumes "subalgebra M F" and [measurable]: "f \<in> borel_measurable F"
   127   assumes "subalgebra M F" and [measurable]: "f \<in> borel_measurable F"
   128   shows "(\<integral>\<^sup>+ x. f x \<partial>(restr_to_subalg M F)) = (\<integral>\<^sup>+ x. f x \<partial>M)"
   128   shows "(\<integral>\<^sup>+ x. f x \<partial>(restr_to_subalg M F)) = (\<integral>\<^sup>+ x. f x \<partial>M)"
   129 proof (rule nn_integral_subalgebra)
   129 proof (rule nn_integral_subalgebra)
   133   fix A assume "A \<in> sets (restr_to_subalg M F)"
   133   fix A assume "A \<in> sets (restr_to_subalg M F)"
   134   then show "emeasure (restr_to_subalg M F) A = emeasure M A"
   134   then show "emeasure (restr_to_subalg M F) A = emeasure M A"
   135     by (metis sets_restr_to_subalg[OF assms(1)] emeasure_restr_to_subalg[OF assms(1)])
   135     by (metis sets_restr_to_subalg[OF assms(1)] emeasure_restr_to_subalg[OF assms(1)])
   136 qed (auto simp add: assms space_restr_to_subalg sets_restr_to_subalg[OF assms(1)])
   136 qed (auto simp add: assms space_restr_to_subalg sets_restr_to_subalg[OF assms(1)])
   137 
   137 
   138 text{*The following is the direct transposition of \verb+integral_subalgebra+
   138 text\<open>The following is the direct transposition of \verb+integral_subalgebra+
   139 (from \verb+Bochner_Integration+) in the current notations.*}
   139 (from \verb+Bochner_Integration+) in the current notations.\<close>
   140 
   140 
   141 lemma integral_subalgebra2:
   141 lemma integral_subalgebra2:
   142   fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}"
   142   fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}"
   143   assumes "subalgebra M F" and
   143   assumes "subalgebra M F" and
   144     [measurable]: "f \<in> borel_measurable F"
   144     [measurable]: "f \<in> borel_measurable F"
   175     by (rule nn_integral_subalgebra2, auto simp add: assms)
   175     by (rule nn_integral_subalgebra2, auto simp add: assms)
   176   also have "... < \<infinity>" using integrable_iff_bounded assms by auto
   176   also have "... < \<infinity>" using integrable_iff_bounded assms by auto
   177   finally show "(\<integral>\<^sup>+ x. ennreal (norm (f x)) \<partial>(restr_to_subalg M F)) < \<infinity>" by simp
   177   finally show "(\<integral>\<^sup>+ x. ennreal (norm (f x)) \<partial>(restr_to_subalg M F)) < \<infinity>" by simp
   178 qed
   178 qed
   179 
   179 
   180 subsection {*Nonnegative conditional expectation*}
   180 subsection \<open>Nonnegative conditional expectation\<close>
   181 
   181 
   182 text {* The conditional expectation of a function $f$, on a measure space $M$, with respect to a
   182 text \<open>The conditional expectation of a function $f$, on a measure space $M$, with respect to a
   183 sub sigma algebra $F$, should be a function $g$ which is $F$-measurable whose integral on any
   183 sub sigma algebra $F$, should be a function $g$ which is $F$-measurable whose integral on any
   184 $F$-set coincides with the integral of $f$.
   184 $F$-set coincides with the integral of $f$.
   185 Such a function is uniquely defined almost everywhere.
   185 Such a function is uniquely defined almost everywhere.
   186 The most direct construction is to use the measure $f dM$, restrict it to the sigma-algebra $F$,
   186 The most direct construction is to use the measure $f dM$, restrict it to the sigma-algebra $F$,
   187 and apply the Radon-Nikodym theorem to write it as $g dM_{|F}$ for some $F$-measurable function $g$.
   187 and apply the Radon-Nikodym theorem to write it as $g dM_{|F}$ for some $F$-measurable function $g$.
   190 machinery, and works for all positive functions.
   190 machinery, and works for all positive functions.
   191 
   191 
   192 In this paragraph, we develop the definition and basic properties for nonnegative functions,
   192 In this paragraph, we develop the definition and basic properties for nonnegative functions,
   193 as the basics of the general case. As in the definition of integrals, the nonnegative case is done
   193 as the basics of the general case. As in the definition of integrals, the nonnegative case is done
   194 with ennreal-valued functions, without any integrability assumption.
   194 with ennreal-valued functions, without any integrability assumption.
   195 *}
   195 \<close>
   196 
   196 
   197 definition nn_cond_exp :: "'a measure \<Rightarrow> 'a measure \<Rightarrow> ('a \<Rightarrow> ennreal) \<Rightarrow> ('a \<Rightarrow> ennreal)"
   197 definition nn_cond_exp :: "'a measure \<Rightarrow> 'a measure \<Rightarrow> ('a \<Rightarrow> ennreal) \<Rightarrow> ('a \<Rightarrow> ennreal)"
   198 where
   198 where
   199   "nn_cond_exp M F f =
   199   "nn_cond_exp M F f =
   200     (if f \<in> borel_measurable M \<and> subalgebra M F
   200     (if f \<in> borel_measurable M \<and> subalgebra M F
   205   shows borel_measurable_nn_cond_exp [measurable]: "nn_cond_exp M F f \<in> borel_measurable F"
   205   shows borel_measurable_nn_cond_exp [measurable]: "nn_cond_exp M F f \<in> borel_measurable F"
   206   and borel_measurable_nn_cond_exp2 [measurable]: "nn_cond_exp M F f \<in> borel_measurable M"
   206   and borel_measurable_nn_cond_exp2 [measurable]: "nn_cond_exp M F f \<in> borel_measurable M"
   207 by (simp_all add: nn_cond_exp_def)
   207 by (simp_all add: nn_cond_exp_def)
   208   (metis borel_measurable_RN_deriv borel_measurable_subalgebra sets_restr_to_subalg space_restr_to_subalg subalgebra_def)
   208   (metis borel_measurable_RN_deriv borel_measurable_subalgebra sets_restr_to_subalg space_restr_to_subalg subalgebra_def)
   209 
   209 
   210 text {* The good setting for conditional expectations is the situation where the subalgebra $F$
   210 text \<open>The good setting for conditional expectations is the situation where the subalgebra $F$
   211 gives rise to a sigma-finite measure space. To see what goes wrong if it is not sigma-finite,
   211 gives rise to a sigma-finite measure space. To see what goes wrong if it is not sigma-finite,
   212 think of $\mathbb{R}$ with the trivial sigma-algebra $\{\emptyset, \mathbb{R}\}$. In this case,
   212 think of $\mathbb{R}$ with the trivial sigma-algebra $\{\emptyset, \mathbb{R}\}$. In this case,
   213 conditional expectations have to be constant functions, so they have integral $0$ or $\infty$.
   213 conditional expectations have to be constant functions, so they have integral $0$ or $\infty$.
   214 This means that a positive integrable function can have no meaningful conditional expectation.*}
   214 This means that a positive integrable function can have no meaningful conditional expectation.\<close>
   215 
   215 
   216 locale sigma_finite_subalgebra =
   216 locale sigma_finite_subalgebra =
   217   fixes M F::"'a measure"
   217   fixes M F::"'a measure"
   218   assumes subalg: "subalgebra M F"
   218   assumes subalg: "subalgebra M F"
   219       and sigma_fin_subalg: "sigma_finite_measure (restr_to_subalg M F)"
   219       and sigma_fin_subalg: "sigma_finite_measure (restr_to_subalg M F)"
   228   obtain A where Ap: "countable A \<and> A \<subseteq> sets (restr_to_subalg M F) \<and> \<Union>A = space (restr_to_subalg M F) \<and> (\<forall>a\<in>A. emeasure (restr_to_subalg M F) a \<noteq> \<infinity>)"
   228   obtain A where Ap: "countable A \<and> A \<subseteq> sets (restr_to_subalg M F) \<and> \<Union>A = space (restr_to_subalg M F) \<and> (\<forall>a\<in>A. emeasure (restr_to_subalg M F) a \<noteq> \<infinity>)"
   229     using sigma_finite_measure.sigma_finite_countable[OF sigma_fin_subalg] by fastforce
   229     using sigma_finite_measure.sigma_finite_countable[OF sigma_fin_subalg] by fastforce
   230   have "A \<subseteq> sets F" using Ap sets_restr_to_subalg[OF subalg] by fastforce
   230   have "A \<subseteq> sets F" using Ap sets_restr_to_subalg[OF subalg] by fastforce
   231   then have "A \<subseteq> sets M" using subalg subalgebra_def by force
   231   then have "A \<subseteq> sets M" using subalg subalgebra_def by force
   232   moreover have "\<Union>A = space M" using Ap space_restr_to_subalg by simp
   232   moreover have "\<Union>A = space M" using Ap space_restr_to_subalg by simp
   233   moreover have "\<forall>a\<in>A. emeasure M a \<noteq> \<infinity>" by (metis subsetD emeasure_restr_to_subalg[OF subalg] `A \<subseteq> sets F` Ap)
   233   moreover have "\<forall>a\<in>A. emeasure M a \<noteq> \<infinity>" by (metis subsetD emeasure_restr_to_subalg[OF subalg] \<open>A \<subseteq> sets F\<close> Ap)
   234   ultimately show "\<exists>A. countable A \<and> A \<subseteq> sets M \<and> \<Union>A = space M \<and> (\<forall>a\<in>A. emeasure M a \<noteq> \<infinity>)" using Ap by auto
   234   ultimately show "\<exists>A. countable A \<and> A \<subseteq> sets M \<and> \<Union>A = space M \<and> (\<forall>a\<in>A. emeasure M a \<noteq> \<infinity>)" using Ap by auto
   235 qed
   235 qed
   236 
   236 
   237 sublocale sigma_finite_subalgebra \<subseteq> sigma_finite_measure
   237 sublocale sigma_finite_subalgebra \<subseteq> sigma_finite_measure
   238 using sigma_finite_subalgebra_is_sigma_finite sigma_finite_subalgebra_axioms by blast
   238 using sigma_finite_subalgebra_is_sigma_finite sigma_finite_subalgebra_axioms by blast
   239 
   239 
   240 text {* Conditional expectations are very often used in probability spaces. This is a special case
   240 text \<open>Conditional expectations are very often used in probability spaces. This is a special case
   241 of the previous one, as we prove now. *}
   241 of the previous one, as we prove now.\<close>
   242 
   242 
   243 locale finite_measure_subalgebra = finite_measure +
   243 locale finite_measure_subalgebra = finite_measure +
   244   fixes F::"'a measure"
   244   fixes F::"'a measure"
   245   assumes subalg: "subalgebra M F"
   245   assumes subalg: "subalgebra M F"
   246 
   246 
   266 qed
   266 qed
   267 
   267 
   268 context sigma_finite_subalgebra
   268 context sigma_finite_subalgebra
   269 begin
   269 begin
   270 
   270 
   271 text{* The next lemma is arguably the most fundamental property of conditional expectation:
   271 text\<open>The next lemma is arguably the most fundamental property of conditional expectation:
   272 when computing an expectation against an $F$-measurable function, it is equivalent to work
   272 when computing an expectation against an $F$-measurable function, it is equivalent to work
   273 with a function or with its $F$-conditional expectation.
   273 with a function or with its $F$-conditional expectation.
   274 
   274 
   275 This property (even for bounded test functions) characterizes conditional expectations, as the second lemma below shows.
   275 This property (even for bounded test functions) characterizes conditional expectations, as the second lemma below shows.
   276 From this point on, we will only work with it, and forget completely about
   276 From this point on, we will only work with it, and forget completely about
   277 the definition using Radon-Nikodym derivatives.
   277 the definition using Radon-Nikodym derivatives.
   278 *}
   278 \<close>
   279 
   279 
   280 lemma nn_cond_exp_intg:
   280 lemma nn_cond_exp_intg:
   281   assumes [measurable]: "f \<in> borel_measurable F" "g \<in> borel_measurable M"
   281   assumes [measurable]: "f \<in> borel_measurable F" "g \<in> borel_measurable M"
   282   shows "(\<integral>\<^sup>+ x. f x * nn_cond_exp M F g x \<partial>M) = (\<integral>\<^sup>+ x. f x * g x \<partial>M)"
   282   shows "(\<integral>\<^sup>+ x. f x * nn_cond_exp M F g x \<partial>M) = (\<integral>\<^sup>+ x. f x * g x \<partial>M)"
   283 proof -
   283 proof -
   369   shows "AE x in M. nn_cond_exp M F f x + nn_cond_exp M F g x = nn_cond_exp M F (\<lambda>x. f x + g x) x"
   369   shows "AE x in M. nn_cond_exp M F f x + nn_cond_exp M F g x = nn_cond_exp M F (\<lambda>x. f x + g x) x"
   370 proof (rule nn_cond_exp_charact)
   370 proof (rule nn_cond_exp_charact)
   371   fix A assume [measurable]: "A \<in> sets F"
   371   fix A assume [measurable]: "A \<in> sets F"
   372   then have "A \<in> sets M" by (meson subalg subalgebra_def subsetD)
   372   then have "A \<in> sets M" by (meson subalg subalgebra_def subsetD)
   373   have "\<integral>\<^sup>+x\<in>A. (nn_cond_exp M F f x + nn_cond_exp M F g x)\<partial>M = (\<integral>\<^sup>+x\<in>A. nn_cond_exp M F f x \<partial>M) + (\<integral>\<^sup>+x\<in>A. nn_cond_exp M F g x \<partial>M)"
   373   have "\<integral>\<^sup>+x\<in>A. (nn_cond_exp M F f x + nn_cond_exp M F g x)\<partial>M = (\<integral>\<^sup>+x\<in>A. nn_cond_exp M F f x \<partial>M) + (\<integral>\<^sup>+x\<in>A. nn_cond_exp M F g x \<partial>M)"
   374     by (rule nn_set_integral_add) (auto simp add: assms `A \<in> sets M`)
   374     by (rule nn_set_integral_add) (auto simp add: assms \<open>A \<in> sets M\<close>)
   375   also have "... = (\<integral>\<^sup>+x. indicator A x * nn_cond_exp M F f x \<partial>M) + (\<integral>\<^sup>+x. indicator A x * nn_cond_exp M F g x \<partial>M)"
   375   also have "... = (\<integral>\<^sup>+x. indicator A x * nn_cond_exp M F f x \<partial>M) + (\<integral>\<^sup>+x. indicator A x * nn_cond_exp M F g x \<partial>M)"
   376     by (metis (no_types, lifting) mult.commute nn_integral_cong)
   376     by (metis (no_types, lifting) mult.commute nn_integral_cong)
   377   also have "... = (\<integral>\<^sup>+x. indicator A x * f x \<partial>M) + (\<integral>\<^sup>+x. indicator A x * g x \<partial>M)"
   377   also have "... = (\<integral>\<^sup>+x. indicator A x * f x \<partial>M) + (\<integral>\<^sup>+x. indicator A x * g x \<partial>M)"
   378     by (simp add: nn_cond_exp_intg)
   378     by (simp add: nn_cond_exp_intg)
   379   also have "... = (\<integral>\<^sup>+x\<in>A. f x \<partial>M) + (\<integral>\<^sup>+x\<in>A. g x \<partial>M)"
   379   also have "... = (\<integral>\<^sup>+x\<in>A. f x \<partial>M) + (\<integral>\<^sup>+x\<in>A. g x \<partial>M)"
   380     by (metis (no_types, lifting) mult.commute nn_integral_cong)
   380     by (metis (no_types, lifting) mult.commute nn_integral_cong)
   381   also have "... = \<integral>\<^sup>+x\<in>A. (f x + g x)\<partial>M"
   381   also have "... = \<integral>\<^sup>+x\<in>A. (f x + g x)\<partial>M"
   382     by (rule nn_set_integral_add[symmetric]) (auto simp add: assms `A \<in> sets M`)
   382     by (rule nn_set_integral_add[symmetric]) (auto simp add: assms \<open>A \<in> sets M\<close>)
   383   finally show "\<integral>\<^sup>+x\<in>A. (f x + g x)\<partial>M = \<integral>\<^sup>+x\<in>A. (nn_cond_exp M F f x + nn_cond_exp M F g x)\<partial>M"
   383   finally show "\<integral>\<^sup>+x\<in>A. (f x + g x)\<partial>M = \<integral>\<^sup>+x\<in>A. (nn_cond_exp M F f x + nn_cond_exp M F g x)\<partial>M"
   384     by simp
   384     by simp
   385 qed (auto simp add: assms)
   385 qed (auto simp add: assms)
   386 
   386 
   387 lemma nn_cond_exp_cong:
   387 lemma nn_cond_exp_cong:
   448   finally show "set_nn_integral M A (nn_cond_exp M G f) = set_nn_integral M A (nn_cond_exp M F f)".
   448   finally show "set_nn_integral M A (nn_cond_exp M G f) = set_nn_integral M A (nn_cond_exp M F f)".
   449 qed
   449 qed
   450 
   450 
   451 end
   451 end
   452 
   452 
   453 subsection {*Real conditional expectation*}
   453 subsection \<open>Real conditional expectation\<close>
   454 
   454 
   455 text {*Once conditional expectations of positive functions are defined, the definition for real-valued functions
   455 text \<open>Once conditional expectations of positive functions are defined, the definition for real-valued functions
   456 follows readily, by taking the difference of positive and negative parts.
   456 follows readily, by taking the difference of positive and negative parts.
   457 One could also define a conditional expectation of vector-space valued functions, as in
   457 One could also define a conditional expectation of vector-space valued functions, as in
   458 \verb+Bochner_Integral+, but since the real-valued case is the most important, and quicker to formalize, I
   458 \verb+Bochner_Integral+, but since the real-valued case is the most important, and quicker to formalize, I
   459 concentrate on it. (It is also essential for the case of the most general Pettis integral.)
   459 concentrate on it. (It is also essential for the case of the most general Pettis integral.)
   460 *}
   460 \<close>
   461 
   461 
   462 definition real_cond_exp :: "'a measure \<Rightarrow> 'a measure \<Rightarrow> ('a \<Rightarrow> real) \<Rightarrow> ('a \<Rightarrow> real)" where
   462 definition real_cond_exp :: "'a measure \<Rightarrow> 'a measure \<Rightarrow> ('a \<Rightarrow> real) \<Rightarrow> ('a \<Rightarrow> real)" where
   463   "real_cond_exp M F f =
   463   "real_cond_exp M F f =
   464     (\<lambda>x. enn2real(nn_cond_exp M F (\<lambda>x. ennreal (f x)) x) - enn2real(nn_cond_exp M F (\<lambda>x. ennreal (-f x)) x))"
   464     (\<lambda>x. enn2real(nn_cond_exp M F (\<lambda>x. ennreal (f x)) x) - enn2real(nn_cond_exp M F (\<lambda>x. ennreal (-f x)) x))"
   465 
   465 
   495   ultimately have "AE x in M. abs(real_cond_exp M F f x) \<le> nn_cond_exp M F (\<lambda>x. fp x + fm x) x"
   495   ultimately have "AE x in M. abs(real_cond_exp M F f x) \<le> nn_cond_exp M F (\<lambda>x. fp x + fm x) x"
   496     by auto
   496     by auto
   497   then show ?thesis using eq by simp
   497   then show ?thesis using eq by simp
   498 qed
   498 qed
   499 
   499 
   500 text{* The next lemma shows that the conditional expectation
   500 text\<open>The next lemma shows that the conditional expectation
   501 is an $F$-measurable function whose average against an $F$-measurable
   501 is an $F$-measurable function whose average against an $F$-measurable
   502 function $f$ coincides with the average of the original function against $f$.
   502 function $f$ coincides with the average of the original function against $f$.
   503 It is obtained as a consequence of the same property for the positive conditional
   503 It is obtained as a consequence of the same property for the positive conditional
   504 expectation, taking the difference of the positive and the negative part. The proof
   504 expectation, taking the difference of the positive and the negative part. The proof
   505 is given first assuming $f \geq 0$ for simplicity, and then extended to the general case in
   505 is given first assuming $f \geq 0$ for simplicity, and then extended to the general case in
   509 functions and ennreal-valued functions.
   509 functions and ennreal-valued functions.
   510 
   510 
   511 Once this lemma is available, we will use it to characterize the conditional expectation,
   511 Once this lemma is available, we will use it to characterize the conditional expectation,
   512 and never come back to the original technical definition, as we did in the case of the
   512 and never come back to the original technical definition, as we did in the case of the
   513 nonnegative conditional expectation.
   513 nonnegative conditional expectation.
   514 *}
   514 \<close>
   515 
   515 
   516 lemma real_cond_exp_intg_fpos:
   516 lemma real_cond_exp_intg_fpos:
   517   assumes "integrable M (\<lambda>x. f x * g x)" and f_pos[simp]: "\<And>x. f x \<ge> 0" and
   517   assumes "integrable M (\<lambda>x. f x * g x)" and f_pos[simp]: "\<And>x. f x \<ge> 0" and
   518           [measurable]: "f \<in> borel_measurable F" "g \<in> borel_measurable M"
   518           [measurable]: "f \<in> borel_measurable F" "g \<in> borel_measurable M"
   519   shows "integrable M (\<lambda>x. f x * real_cond_exp M F g x)"
   519   shows "integrable M (\<lambda>x. f x * real_cond_exp M F g x)"
   736 lemma real_cond_exp_intA:
   736 lemma real_cond_exp_intA:
   737   assumes [measurable]: "integrable M f" "A \<in> sets F"
   737   assumes [measurable]: "integrable M f" "A \<in> sets F"
   738   shows "(\<integral> x \<in> A. f x \<partial>M) = (\<integral>x \<in> A. real_cond_exp M F f x \<partial>M)"
   738   shows "(\<integral> x \<in> A. f x \<partial>M) = (\<integral>x \<in> A. real_cond_exp M F f x \<partial>M)"
   739 proof -
   739 proof -
   740   have "A \<in> sets M" by (meson assms(2) subalg subalgebra_def subsetD)
   740   have "A \<in> sets M" by (meson assms(2) subalg subalgebra_def subsetD)
   741   have "integrable M (\<lambda>x. indicator A x * f x)" using integrable_mult_indicator[OF `A \<in> sets M` assms(1)] by auto
   741   have "integrable M (\<lambda>x. indicator A x * f x)" using integrable_mult_indicator[OF \<open>A \<in> sets M\<close> assms(1)] by auto
   742   then show ?thesis using real_cond_exp_intg(2)[where ?f = "indicator A" and ?g = f, symmetric] by auto
   742   then show ?thesis using real_cond_exp_intg(2)[where ?f = "indicator A" and ?g = f, symmetric] by auto
   743 qed
   743 qed
   744 
   744 
   745 lemma real_cond_exp_int [intro]:
   745 lemma real_cond_exp_int [intro]:
   746   assumes "integrable M f"
   746   assumes "integrable M f"
   787   assumes [measurable]:"f \<in> borel_measurable F" "g \<in> borel_measurable M" "integrable M (\<lambda>x. f x * g x)"
   787   assumes [measurable]:"f \<in> borel_measurable F" "g \<in> borel_measurable M" "integrable M (\<lambda>x. f x * g x)"
   788   shows "AE x in M. real_cond_exp M F (\<lambda>x. f x * g x) x = f x * real_cond_exp M F g x"
   788   shows "AE x in M. real_cond_exp M F (\<lambda>x. f x * g x) x = f x * real_cond_exp M F g x"
   789 proof (rule real_cond_exp_charact)
   789 proof (rule real_cond_exp_charact)
   790   fix A assume "A \<in> sets F"
   790   fix A assume "A \<in> sets F"
   791   then have [measurable]: "(\<lambda>x. f x * indicator A x) \<in> borel_measurable F" by measurable
   791   then have [measurable]: "(\<lambda>x. f x * indicator A x) \<in> borel_measurable F" by measurable
   792   have [measurable]: "A \<in> sets M" using subalg by (meson `A \<in> sets F` subalgebra_def subsetD)
   792   have [measurable]: "A \<in> sets M" using subalg by (meson \<open>A \<in> sets F\<close> subalgebra_def subsetD)
   793   have "\<integral>x\<in>A. (f x * g x) \<partial>M = \<integral>x. (f x * indicator A x) * g x \<partial>M"
   793   have "\<integral>x\<in>A. (f x * g x) \<partial>M = \<integral>x. (f x * indicator A x) * g x \<partial>M"
   794     by (simp add: mult.commute mult.left_commute)
   794     by (simp add: mult.commute mult.left_commute)
   795   also have "... = \<integral>x. (f x * indicator A x) * real_cond_exp M F g x \<partial>M"
   795   also have "... = \<integral>x. (f x * indicator A x) * real_cond_exp M F g x \<partial>M"
   796     apply (rule real_cond_exp_intg(2)[symmetric], auto simp add: assms)
   796     apply (rule real_cond_exp_intg(2)[symmetric], auto simp add: assms)
   797     using integrable_mult_indicator[OF `A \<in> sets M` assms(3)] by (simp add: mult.commute mult.left_commute)
   797     using integrable_mult_indicator[OF \<open>A \<in> sets M\<close> assms(3)] by (simp add: mult.commute mult.left_commute)
   798   also have "... = \<integral>x\<in>A. (f x * real_cond_exp M F g x)\<partial>M"
   798   also have "... = \<integral>x\<in>A. (f x * real_cond_exp M F g x)\<partial>M"
   799     by (simp add: mult.commute mult.left_commute)
   799     by (simp add: mult.commute mult.left_commute)
   800   finally show "\<integral>x\<in>A. (f x * g x) \<partial>M = \<integral>x\<in>A. (f x * real_cond_exp M F g x)\<partial>M" by simp
   800   finally show "\<integral>x\<in>A. (f x * g x) \<partial>M = \<integral>x\<in>A. (f x * real_cond_exp M F g x)\<partial>M" by simp
   801 qed (auto simp add: real_cond_exp_intg(1) assms)
   801 qed (auto simp add: real_cond_exp_intg(1) assms)
   802 
   802 
   810     by auto
   810     by auto
   811 
   811 
   812   fix A assume [measurable]: "A \<in> sets F"
   812   fix A assume [measurable]: "A \<in> sets F"
   813   then have "A \<in> sets M" by (meson subalg subalgebra_def subsetD)
   813   then have "A \<in> sets M" by (meson subalg subalgebra_def subsetD)
   814   have intAf: "integrable M (\<lambda>x. indicator A x * f x)"
   814   have intAf: "integrable M (\<lambda>x. indicator A x * f x)"
   815     using integrable_mult_indicator[OF `A \<in> sets M` assms(1)] by auto
   815     using integrable_mult_indicator[OF \<open>A \<in> sets M\<close> assms(1)] by auto
   816   have intAg: "integrable M (\<lambda>x. indicator A x * g x)"
   816   have intAg: "integrable M (\<lambda>x. indicator A x * g x)"
   817     using integrable_mult_indicator[OF `A \<in> sets M` assms(2)] by auto
   817     using integrable_mult_indicator[OF \<open>A \<in> sets M\<close> assms(2)] by auto
   818 
   818 
   819   have "\<integral>x\<in>A. (real_cond_exp M F f x + real_cond_exp M F g x)\<partial>M = (\<integral>x\<in>A. real_cond_exp M F f x \<partial>M) + (\<integral>x\<in>A. real_cond_exp M F g x \<partial>M)"
   819   have "\<integral>x\<in>A. (real_cond_exp M F f x + real_cond_exp M F g x)\<partial>M = (\<integral>x\<in>A. real_cond_exp M F f x \<partial>M) + (\<integral>x\<in>A. real_cond_exp M F g x \<partial>M)"
   820     apply (rule set_integral_add, auto simp add: assms)
   820     apply (rule set_integral_add, auto simp add: assms)
   821     using integrable_mult_indicator[OF `A \<in> sets M` real_cond_exp_int(1)[OF assms(1)]]
   821     using integrable_mult_indicator[OF \<open>A \<in> sets M\<close> real_cond_exp_int(1)[OF assms(1)]]
   822           integrable_mult_indicator[OF `A \<in> sets M` real_cond_exp_int(1)[OF assms(2)]] by simp_all
   822           integrable_mult_indicator[OF \<open>A \<in> sets M\<close> real_cond_exp_int(1)[OF assms(2)]] by simp_all
   823   also have "... = (\<integral>x. indicator A x * real_cond_exp M F f x \<partial>M) + (\<integral>x. indicator A x * real_cond_exp M F g x \<partial>M)"
   823   also have "... = (\<integral>x. indicator A x * real_cond_exp M F f x \<partial>M) + (\<integral>x. indicator A x * real_cond_exp M F g x \<partial>M)"
   824     by auto
   824     by auto
   825   also have "... = (\<integral>x. indicator A x * f x \<partial>M) + (\<integral>x. indicator A x * g x \<partial>M)"
   825   also have "... = (\<integral>x. indicator A x * f x \<partial>M) + (\<integral>x. indicator A x * g x \<partial>M)"
   826     using real_cond_exp_intg(2) assms `A \<in> sets F` intAf intAg by auto
   826     using real_cond_exp_intg(2) assms \<open>A \<in> sets F\<close> intAf intAg by auto
   827   also have "... = (\<integral>x\<in>A. f x \<partial>M) + (\<integral>x\<in>A. g x \<partial>M)"
   827   also have "... = (\<integral>x\<in>A. f x \<partial>M) + (\<integral>x\<in>A. g x \<partial>M)"
   828     by auto
   828     by auto
   829   also have "... = \<integral>x\<in>A. (f x + g x)\<partial>M"
   829   also have "... = \<integral>x\<in>A. (f x + g x)\<partial>M"
   830     by (rule set_integral_add(2)[symmetric]) (auto simp add: assms `A \<in> sets M` intAf intAg)
   830     by (rule set_integral_add(2)[symmetric]) (auto simp add: assms \<open>A \<in> sets M\<close> intAf intAg)
   831   finally show "\<integral>x\<in>A. (f x + g x)\<partial>M = \<integral>x\<in>A. (real_cond_exp M F f x + real_cond_exp M F g x)\<partial>M"
   831   finally show "\<integral>x\<in>A. (f x + g x)\<partial>M = \<integral>x\<in>A. (real_cond_exp M F f x + real_cond_exp M F g x)\<partial>M"
   832     by simp
   832     by simp
   833 qed (auto simp add: assms)
   833 qed (auto simp add: assms)
   834 
   834 
   835 lemma real_cond_exp_cong:
   835 lemma real_cond_exp_cong:
   922   proof (rule ccontr)
   922   proof (rule ccontr)
   923     assume "\<not>(emeasure M X) = 0"
   923     assume "\<not>(emeasure M X) = 0"
   924     have "emeasure (restr_to_subalg M F) X = emeasure M X"
   924     have "emeasure (restr_to_subalg M F) X = emeasure M X"
   925       by (simp add: emeasure_restr_to_subalg subalg)
   925       by (simp add: emeasure_restr_to_subalg subalg)
   926     then have "emeasure (restr_to_subalg M F) X > 0"
   926     then have "emeasure (restr_to_subalg M F) X > 0"
   927       using `\<not>(emeasure M X) = 0` gr_zeroI by auto
   927       using \<open>\<not>(emeasure M X) = 0\<close> gr_zeroI by auto
   928     then obtain A where "A \<in> sets (restr_to_subalg M F)" "A \<subseteq> X" "emeasure (restr_to_subalg M F) A > 0" "emeasure (restr_to_subalg M F) A < \<infinity>"
   928     then obtain A where "A \<in> sets (restr_to_subalg M F)" "A \<subseteq> X" "emeasure (restr_to_subalg M F) A > 0" "emeasure (restr_to_subalg M F) A < \<infinity>"
   929       using sigma_fin_subalg by (metis emeasure_notin_sets ennreal_0 infinity_ennreal_def le_less_linear neq_top_trans
   929       using sigma_fin_subalg by (metis emeasure_notin_sets ennreal_0 infinity_ennreal_def le_less_linear neq_top_trans
   930       not_gr_zero order_refl sigma_finite_measure.approx_PInf_emeasure_with_finite)
   930       not_gr_zero order_refl sigma_finite_measure.approx_PInf_emeasure_with_finite)
   931     then have [measurable]: "A \<in> sets F" using subalg sets_restr_to_subalg by blast
   931     then have [measurable]: "A \<in> sets F" using subalg sets_restr_to_subalg by blast
   932     then have [measurable]: "A \<in> sets M" using sets_restr_to_subalg subalg subalgebra_def by blast
   932     then have [measurable]: "A \<in> sets M" using sets_restr_to_subalg subalg subalgebra_def by blast
   933     have Ic: "set_integrable M A (\<lambda>x. c)"
   933     have Ic: "set_integrable M A (\<lambda>x. c)"
   934       using \<open>emeasure (restr_to_subalg M F) A < \<infinity>\<close> emeasure_restr_to_subalg subalg by fastforce
   934       using \<open>emeasure (restr_to_subalg M F) A < \<infinity>\<close> emeasure_restr_to_subalg subalg by fastforce
   935     have If: "set_integrable M A f"
   935     have If: "set_integrable M A f"
   936       by (rule integrable_mult_indicator, auto simp add: `integrable M f`)
   936       by (rule integrable_mult_indicator, auto simp add: \<open>integrable M f\<close>)
   937     have *: "(\<integral>x\<in>A. c \<partial>M) = (\<integral>x\<in>A. f x \<partial>M)"
   937     have *: "(\<integral>x\<in>A. c \<partial>M) = (\<integral>x\<in>A. f x \<partial>M)"
   938     proof (rule antisym)
   938     proof (rule antisym)
   939       show "(\<integral>x\<in>A. c \<partial>M) \<le> (\<integral>x\<in>A. f x \<partial>M)"
   939       show "(\<integral>x\<in>A. c \<partial>M) \<le> (\<integral>x\<in>A. f x \<partial>M)"
   940         apply (rule set_integral_mono_AE) using Ic If assms(2) by auto
   940         apply (rule set_integral_mono_AE) using Ic If assms(2) by auto
   941       have "(\<integral>x\<in>A. f x \<partial>M) = (\<integral>x\<in>A. real_cond_exp M F f x \<partial>M)"
   941       have "(\<integral>x\<in>A. f x \<partial>M) = (\<integral>x\<in>A. real_cond_exp M F f x \<partial>M)"
   942         by (rule real_cond_exp_intA, auto simp add: `integrable M f`)
   942         by (rule real_cond_exp_intA, auto simp add: \<open>integrable M f\<close>)
   943       also have "... \<le> (\<integral>x\<in>A. c \<partial>M)"
   943       also have "... \<le> (\<integral>x\<in>A. c \<partial>M)"
   944         apply (rule set_integral_mono)
   944         apply (rule set_integral_mono)
   945         apply (rule integrable_mult_indicator, simp, simp add: real_cond_exp_int(1)[OF `integrable M f`])
   945         apply (rule integrable_mult_indicator, simp, simp add: real_cond_exp_int(1)[OF \<open>integrable M f\<close>])
   946         using Ic X_def \<open>A \<subseteq> X\<close> by auto
   946         using Ic X_def \<open>A \<subseteq> X\<close> by auto
   947       finally show "(\<integral>x\<in>A. f x \<partial>M) \<le> (\<integral>x\<in>A. c \<partial>M)" by simp
   947       finally show "(\<integral>x\<in>A. f x \<partial>M) \<le> (\<integral>x\<in>A. c \<partial>M)" by simp
   948     qed
   948     qed
   949     have "AE x in M. indicator A x * c = indicator A x * f x"
   949     have "AE x in M. indicator A x * c = indicator A x * f x"
   950       apply (rule integral_ineq_eq_0_then_AE) using Ic If * apply auto
   950       apply (rule integral_ineq_eq_0_then_AE) using Ic If * apply auto
   951       using assms(2) unfolding indicator_def by auto
   951       using assms(2) unfolding indicator_def by auto
   952     then have "AE x\<in>A in M. c = f x" by auto
   952     then have "AE x\<in>A in M. c = f x" by auto
   953     then have "AE x\<in>A in M. False" using assms(2) by auto
   953     then have "AE x\<in>A in M. False" using assms(2) by auto
   954     have "A \<in> null_sets M" unfolding ae_filter_def by (meson AE_iff_null_sets \<open>A \<in> sets M\<close> \<open>AE x\<in>A in M. False\<close>)
   954     have "A \<in> null_sets M" unfolding ae_filter_def by (meson AE_iff_null_sets \<open>A \<in> sets M\<close> \<open>AE x\<in>A in M. False\<close>)
   955     then show False using `emeasure (restr_to_subalg M F) A > 0`
   955     then show False using \<open>emeasure (restr_to_subalg M F) A > 0\<close>
   956       by (simp add: emeasure_restr_to_subalg null_setsD1 subalg)
   956       by (simp add: emeasure_restr_to_subalg null_setsD1 subalg)
   957   qed
   957   qed
   958   then show ?thesis using AE_iff_null_sets[OF `X \<in> sets M`] unfolding X_def by auto
   958   then show ?thesis using AE_iff_null_sets[OF \<open>X \<in> sets M\<close>] unfolding X_def by auto
   959 qed
   959 qed
   960 
   960 
   961 lemma real_cond_exp_less_c:
   961 lemma real_cond_exp_less_c:
   962   assumes [measurable]: "integrable M f"
   962   assumes [measurable]: "integrable M f"
   963       and "AE x in M. f x < c"
   963       and "AE x in M. f x < c"
   964   shows "AE x in M. real_cond_exp M F f x < c"
   964   shows "AE x in M. real_cond_exp M F f x < c"
   965 proof -
   965 proof -
   966   have "AE x in M. real_cond_exp M F f x = -real_cond_exp M F (\<lambda>x. -f x) x"
   966   have "AE x in M. real_cond_exp M F f x = -real_cond_exp M F (\<lambda>x. -f x) x"
   967     using real_cond_exp_cmult[OF `integrable M f`, of "-1"] by auto
   967     using real_cond_exp_cmult[OF \<open>integrable M f\<close>, of "-1"] by auto
   968   moreover have "AE x in M. real_cond_exp M F (\<lambda>x. -f x) x > -c"
   968   moreover have "AE x in M. real_cond_exp M F (\<lambda>x. -f x) x > -c"
   969     apply (rule real_cond_exp_gr_c) using assms by auto
   969     apply (rule real_cond_exp_gr_c) using assms by auto
   970   ultimately show ?thesis by auto
   970   ultimately show ?thesis by auto
   971 qed
   971 qed
   972 
   972 
   976   shows "AE x in M. real_cond_exp M F f x \<ge> c"
   976   shows "AE x in M. real_cond_exp M F f x \<ge> c"
   977 proof -
   977 proof -
   978   obtain u::"nat \<Rightarrow> real" where u: "\<And>n. u n < c" "u \<longlonglongrightarrow> c"
   978   obtain u::"nat \<Rightarrow> real" where u: "\<And>n. u n < c" "u \<longlonglongrightarrow> c"
   979     using approx_from_below_dense_linorder[of "c-1" c] by auto
   979     using approx_from_below_dense_linorder[of "c-1" c] by auto
   980   have *: "AE x in M. real_cond_exp M F f x > u n" for n::nat
   980   have *: "AE x in M. real_cond_exp M F f x > u n" for n::nat
   981     apply (rule real_cond_exp_gr_c) using assms `u n < c` by auto
   981     apply (rule real_cond_exp_gr_c) using assms \<open>u n < c\<close> by auto
   982   have "AE x in M. \<forall>n. real_cond_exp M F f x > u n"
   982   have "AE x in M. \<forall>n. real_cond_exp M F f x > u n"
   983     by (subst AE_all_countable, auto simp add: *)
   983     by (subst AE_all_countable, auto simp add: *)
   984   moreover have "real_cond_exp M F f x \<ge> c" if "\<forall>n. real_cond_exp M F f x > u n" for x
   984   moreover have "real_cond_exp M F f x \<ge> c" if "\<forall>n. real_cond_exp M F f x > u n" for x
   985   proof -
   985   proof -
   986     have "real_cond_exp M F f x \<ge> u n" for n using that less_imp_le by auto
   986     have "real_cond_exp M F f x \<ge> u n" for n using that less_imp_le by auto
   993   assumes [measurable]: "integrable M f"
   993   assumes [measurable]: "integrable M f"
   994       and "AE x in M. f x \<le> c"
   994       and "AE x in M. f x \<le> c"
   995   shows "AE x in M. real_cond_exp M F f x \<le> c"
   995   shows "AE x in M. real_cond_exp M F f x \<le> c"
   996 proof -
   996 proof -
   997   have "AE x in M. real_cond_exp M F f x = -real_cond_exp M F (\<lambda>x. -f x) x"
   997   have "AE x in M. real_cond_exp M F f x = -real_cond_exp M F (\<lambda>x. -f x) x"
   998     using real_cond_exp_cmult[OF `integrable M f`, of "-1"] by auto
   998     using real_cond_exp_cmult[OF \<open>integrable M f\<close>, of "-1"] by auto
   999   moreover have "AE x in M. real_cond_exp M F (\<lambda>x. -f x) x \<ge> -c"
   999   moreover have "AE x in M. real_cond_exp M F (\<lambda>x. -f x) x \<ge> -c"
  1000     apply (rule real_cond_exp_ge_c) using assms by auto
  1000     apply (rule real_cond_exp_ge_c) using assms by auto
  1001   ultimately show ?thesis by auto
  1001   ultimately show ?thesis by auto
  1002 qed
  1002 qed
  1003 
  1003 
  1036   shows "AE x in M. real_cond_exp M F (\<lambda>x. \<Sum>i\<in>I. f i x) x = (\<Sum>i\<in>I. real_cond_exp M F (f i) x)"
  1036   shows "AE x in M. real_cond_exp M F (\<lambda>x. \<Sum>i\<in>I. f i x) x = (\<Sum>i\<in>I. real_cond_exp M F (f i) x)"
  1037 proof (rule real_cond_exp_charact)
  1037 proof (rule real_cond_exp_charact)
  1038   fix A assume [measurable]: "A \<in> sets F"
  1038   fix A assume [measurable]: "A \<in> sets F"
  1039   then have A_meas [measurable]: "A \<in> sets M" by (meson set_mp subalg subalgebra_def)
  1039   then have A_meas [measurable]: "A \<in> sets M" by (meson set_mp subalg subalgebra_def)
  1040   have *: "integrable M (\<lambda>x. indicator A x * f i x)" for i
  1040   have *: "integrable M (\<lambda>x. indicator A x * f i x)" for i
  1041     using integrable_mult_indicator[OF `A \<in> sets M` assms(1)] by auto
  1041     using integrable_mult_indicator[OF \<open>A \<in> sets M\<close> assms(1)] by auto
  1042   have **: "integrable M (\<lambda>x. indicator A x * real_cond_exp M F (f i) x)" for i
  1042   have **: "integrable M (\<lambda>x. indicator A x * real_cond_exp M F (f i) x)" for i
  1043     using integrable_mult_indicator[OF `A \<in> sets M` real_cond_exp_int(1)[OF assms(1)]] by auto
  1043     using integrable_mult_indicator[OF \<open>A \<in> sets M\<close> real_cond_exp_int(1)[OF assms(1)]] by auto
  1044   have inti: "(\<integral>x. indicator A x * f i x \<partial>M) = (\<integral>x. indicator A x * real_cond_exp M F (f i) x \<partial>M)" for i
  1044   have inti: "(\<integral>x. indicator A x * f i x \<partial>M) = (\<integral>x. indicator A x * real_cond_exp M F (f i) x \<partial>M)" for i
  1045     by (rule real_cond_exp_intg(2)[symmetric], auto simp add: *)
  1045     by (rule real_cond_exp_intg(2)[symmetric], auto simp add: *)
  1046 
  1046 
  1047   have "(\<integral>x\<in>A. (\<Sum>i\<in>I. f i x)\<partial>M) = (\<integral>x. (\<Sum>i\<in>I. indicator A x * f i x)\<partial>M)"
  1047   have "(\<integral>x\<in>A. (\<Sum>i\<in>I. f i x)\<partial>M) = (\<integral>x. (\<Sum>i\<in>I. indicator A x * f i x)\<partial>M)"
  1048     by (simp add: sum_distrib_left)
  1048     by (simp add: sum_distrib_left)
  1055   also have "... = (\<integral>x\<in>A. (\<Sum>i\<in>I. real_cond_exp M F (f i) x)\<partial>M)"
  1055   also have "... = (\<integral>x\<in>A. (\<Sum>i\<in>I. real_cond_exp M F (f i) x)\<partial>M)"
  1056     by (simp add: sum_distrib_left)
  1056     by (simp add: sum_distrib_left)
  1057   finally show "(\<integral>x\<in>A. (\<Sum>i\<in>I. f i x)\<partial>M) = (\<integral>x\<in>A. (\<Sum>i\<in>I. real_cond_exp M F (f i) x)\<partial>M)" by auto
  1057   finally show "(\<integral>x\<in>A. (\<Sum>i\<in>I. f i x)\<partial>M) = (\<integral>x\<in>A. (\<Sum>i\<in>I. real_cond_exp M F (f i) x)\<partial>M)" by auto
  1058 qed (auto simp add: assms real_cond_exp_int(1)[OF assms(1)])
  1058 qed (auto simp add: assms real_cond_exp_int(1)[OF assms(1)])
  1059 
  1059 
  1060 text {*Jensen's inequality, describing the behavior of the integral under a convex function, admits
  1060 text \<open>Jensen's inequality, describing the behavior of the integral under a convex function, admits
  1061 a version for the conditional expectation, as follows.*}
  1061 a version for the conditional expectation, as follows.\<close>
  1062 
  1062 
  1063 theorem real_cond_exp_jensens_inequality:
  1063 theorem real_cond_exp_jensens_inequality:
  1064   fixes q :: "real \<Rightarrow> real"
  1064   fixes q :: "real \<Rightarrow> real"
  1065   assumes X: "integrable M X" "AE x in M. X x \<in> I"
  1065   assumes X: "integrable M X" "AE x in M. X x \<in> I"
  1066   assumes I: "I = {a <..< b} \<or> I = {a <..} \<or> I = {..< b} \<or> I = UNIV"
  1066   assumes I: "I = {a <..< b} \<or> I = {a <..} \<or> I = {..< b} \<or> I = UNIV"
  1073   have [measurable]: "I \<in> sets borel" using I by auto
  1073   have [measurable]: "I \<in> sets borel" using I by auto
  1074   define phi where "phi = (\<lambda>x. Inf ((\<lambda>t. (q x - q t) / (x - t)) ` ({x<..} \<inter> I)))"
  1074   define phi where "phi = (\<lambda>x. Inf ((\<lambda>t. (q x - q t) / (x - t)) ` ({x<..} \<inter> I)))"
  1075   have **: "q (X x) \<ge> q (real_cond_exp M F X x) + phi (real_cond_exp M F X x) * (X x - real_cond_exp M F X x)"
  1075   have **: "q (X x) \<ge> q (real_cond_exp M F X x) + phi (real_cond_exp M F X x) * (X x - real_cond_exp M F X x)"
  1076         if "X x \<in> I" "real_cond_exp M F X x \<in> I" for x
  1076         if "X x \<in> I" "real_cond_exp M F X x \<in> I" for x
  1077     unfolding phi_def apply (rule convex_le_Inf_differential)
  1077     unfolding phi_def apply (rule convex_le_Inf_differential)
  1078     using `convex_on I q` that `interior I = I` by auto
  1078     using \<open>convex_on I q\<close> that \<open>interior I = I\<close> by auto
  1079   text {*It is not clear that the function $\phi$ is measurable. We replace it by a version which
  1079   text \<open>It is not clear that the function $\phi$ is measurable. We replace it by a version which
  1080         is better behaved.*}
  1080         is better behaved.\<close>
  1081   define psi where "psi = (\<lambda>x. phi x * indicator I x)"
  1081   define psi where "psi = (\<lambda>x. phi x * indicator I x)"
  1082   have A: "psi y = phi y" if "y \<in> I" for y unfolding psi_def indicator_def using that by auto
  1082   have A: "psi y = phi y" if "y \<in> I" for y unfolding psi_def indicator_def using that by auto
  1083   have *: "q (X x) \<ge> q (real_cond_exp M F X x) + psi (real_cond_exp M F X x) * (X x - real_cond_exp M F X x)"
  1083   have *: "q (X x) \<ge> q (real_cond_exp M F X x) + psi (real_cond_exp M F X x) * (X x - real_cond_exp M F X x)"
  1084         if "X x \<in> I" "real_cond_exp M F X x \<in> I" for x
  1084         if "X x \<in> I" "real_cond_exp M F X x \<in> I" for x
  1085     unfolding A[OF `real_cond_exp M F X x \<in> I`] using ** that by auto
  1085     unfolding A[OF \<open>real_cond_exp M F X x \<in> I\<close>] using ** that by auto
  1086 
  1086 
  1087   note I
  1087   note I
  1088   moreover have "AE x in M. real_cond_exp M F X x > a" if "I \<subseteq> {a <..}" for a
  1088   moreover have "AE x in M. real_cond_exp M F X x > a" if "I \<subseteq> {a <..}" for a
  1089     apply (rule real_cond_exp_gr_c) using X that by auto
  1089     apply (rule real_cond_exp_gr_c) using X that by auto
  1090   moreover have "AE x in M. real_cond_exp M F X x < b" if "I \<subseteq> {..<b}" for b
  1090   moreover have "AE x in M. real_cond_exp M F X x < b" if "I \<subseteq> {..<b}" for b
  1092   ultimately show "AE x in M. real_cond_exp M F X x \<in> I"
  1092   ultimately show "AE x in M. real_cond_exp M F X x \<in> I"
  1093     by (elim disjE) (auto simp: subset_eq)
  1093     by (elim disjE) (auto simp: subset_eq)
  1094   then have main_ineq: "AE x in M. q (X x) \<ge> q (real_cond_exp M F X x) + psi (real_cond_exp M F X x) * (X x - real_cond_exp M F X x)"
  1094   then have main_ineq: "AE x in M. q (X x) \<ge> q (real_cond_exp M F X x) + psi (real_cond_exp M F X x) * (X x - real_cond_exp M F X x)"
  1095     using * X(2) by auto
  1095     using * X(2) by auto
  1096 
  1096 
  1097   text {*Then, one wants to take the conditional expectation of this inequality. On the left, one gets
  1097   text \<open>Then, one wants to take the conditional expectation of this inequality. On the left, one gets
  1098          the conditional expectation of $q \circ X$. On the right, the last term vanishes, and one
  1098          the conditional expectation of $q \circ X$. On the right, the last term vanishes, and one
  1099          is left with $q$ of the conditional expectation, as desired. Unfortunately, this argument only
  1099          is left with $q$ of the conditional expectation, as desired. Unfortunately, this argument only
  1100          works if $\psi \cdot X$ and $q(E(X | F))$ are integrable, and there is no reason why this should be true. The
  1100          works if $\psi \cdot X$ and $q(E(X | F))$ are integrable, and there is no reason why this should be true. The
  1101          trick is to multiply by a $F$-measurable function which is small enough to make
  1101          trick is to multiply by a $F$-measurable function which is small enough to make
  1102          everything integrable.*}
  1102          everything integrable.\<close>
  1103 
  1103 
  1104   obtain f::"'a \<Rightarrow> real" where [measurable]: "f \<in> borel_measurable (restr_to_subalg M F)"
  1104   obtain f::"'a \<Rightarrow> real" where [measurable]: "f \<in> borel_measurable (restr_to_subalg M F)"
  1105                                "integrable (restr_to_subalg M F) f"
  1105                                "integrable (restr_to_subalg M F) f"
  1106                            and f: "\<And>x. f x > 0" "\<And>x. f x \<le> 1"
  1106                            and f: "\<And>x. f x > 0" "\<And>x. f x \<le> 1"
  1107     using sigma_finite_measure.obtain_positive_integrable_function[OF sigma_fin_subalg] by metis
  1107     using sigma_finite_measure.obtain_positive_integrable_function[OF sigma_fin_subalg] by metis
  1121   have "AE x in M. g x * q (X x) \<ge> g x * (q (real_cond_exp M F X x) + psi (real_cond_exp M F X x) * (X x - real_cond_exp M F X x))"
  1121   have "AE x in M. g x * q (X x) \<ge> g x * (q (real_cond_exp M F X x) + psi (real_cond_exp M F X x) * (X x - real_cond_exp M F X x))"
  1122     using main_ineq g by (auto simp add: divide_simps)
  1122     using main_ineq g by (auto simp add: divide_simps)
  1123   then have main_G: "AE x in M. g x * q (X x) \<ge> g x * q (real_cond_exp M F X x) + G x * (X x - real_cond_exp M F X x)"
  1123   then have main_G: "AE x in M. g x * q (X x) \<ge> g x * q (real_cond_exp M F X x) + G x * (X x - real_cond_exp M F X x)"
  1124     unfolding G_def by (auto simp add: algebra_simps)
  1124     unfolding G_def by (auto simp add: algebra_simps)
  1125 
  1125 
  1126   text {*To proceed, we need to know that $\psi$ is measurable.*}
  1126   text \<open>To proceed, we need to know that $\psi$ is measurable.\<close>
  1127   have phi_mono: "phi x \<le> phi y" if "x \<le> y" "x \<in> I" "y \<in> I" for x y
  1127   have phi_mono: "phi x \<le> phi y" if "x \<le> y" "x \<in> I" "y \<in> I" for x y
  1128   proof (cases "x < y")
  1128   proof (cases "x < y")
  1129     case True
  1129     case True
  1130     have "q x + phi x * (y-x) \<le> q y"
  1130     have "q x + phi x * (y-x) \<le> q y"
  1131       unfolding phi_def apply (rule convex_le_Inf_differential) using `convex_on I q` that `interior I = I` by auto
  1131       unfolding phi_def apply (rule convex_le_Inf_differential) using \<open>convex_on I q\<close> that \<open>interior I = I\<close> by auto
  1132     then have "phi x \<le> (q x - q y) / (x - y)"
  1132     then have "phi x \<le> (q x - q y) / (x - y)"
  1133       using that `x < y` by (auto simp add: divide_simps algebra_simps)
  1133       using that \<open>x < y\<close> by (auto simp add: divide_simps algebra_simps)
  1134     moreover have "(q x - q y)/(x - y) \<le> phi y"
  1134     moreover have "(q x - q y)/(x - y) \<le> phi y"
  1135     unfolding phi_def proof (rule cInf_greatest, auto)
  1135     unfolding phi_def proof (rule cInf_greatest, auto)
  1136       fix t assume "t \<in> I" "y < t"
  1136       fix t assume "t \<in> I" "y < t"
  1137       have "(q x - q y) / (x - y) \<le> (q x - q t) / (x - t)"
  1137       have "(q x - q y) / (x - y) \<le> (q x - q t) / (x - t)"
  1138         apply (rule convex_on_diff[OF q(2)]) using `y < t` `x < y` `t \<in> I` `x \<in> I` by auto
  1138         apply (rule convex_on_diff[OF q(2)]) using \<open>y < t\<close> \<open>x < y\<close> \<open>t \<in> I\<close> \<open>x \<in> I\<close> by auto
  1139       also have "... \<le> (q y - q t) / (y - t)"
  1139       also have "... \<le> (q y - q t) / (y - t)"
  1140         apply (rule convex_on_diff[OF q(2)]) using `y < t` `x < y` `t \<in> I` `x \<in> I` by auto
  1140         apply (rule convex_on_diff[OF q(2)]) using \<open>y < t\<close> \<open>x < y\<close> \<open>t \<in> I\<close> \<open>x \<in> I\<close> by auto
  1141       finally show "(q x - q y) / (x - y) \<le> (q y - q t) / (y - t)" by simp
  1141       finally show "(q x - q y) / (x - y) \<le> (q y - q t) / (y - t)" by simp
  1142     next
  1142     next
  1143       obtain e where "0 < e" "ball y e \<subseteq> I" using `open I` `y \<in> I` openE by blast
  1143       obtain e where "0 < e" "ball y e \<subseteq> I" using \<open>open I\<close> \<open>y \<in> I\<close> openE by blast
  1144       then have "y + e/2 \<in> {y<..} \<inter> I" by (auto simp: dist_real_def)
  1144       then have "y + e/2 \<in> {y<..} \<inter> I" by (auto simp: dist_real_def)
  1145       then show "{y<..} \<inter> I = {} \<Longrightarrow> False" by auto
  1145       then show "{y<..} \<inter> I = {} \<Longrightarrow> False" by auto
  1146     qed
  1146     qed
  1147     ultimately show "phi x \<le> phi y" by auto
  1147     ultimately show "phi x \<le> phi y" by auto
  1148   next
  1148   next
  1149     case False
  1149     case False
  1150     then have "x = y" using `x \<le> y` by auto
  1150     then have "x = y" using \<open>x \<le> y\<close> by auto
  1151     then show ?thesis by auto
  1151     then show ?thesis by auto
  1152   qed
  1152   qed
  1153   have [measurable]: "psi \<in> borel_measurable borel"
  1153   have [measurable]: "psi \<in> borel_measurable borel"
  1154     by (rule borel_measurable_piecewise_mono[of "{I, -I}"])
  1154     by (rule borel_measurable_piecewise_mono[of "{I, -I}"])
  1155        (auto simp add: psi_def indicator_def phi_mono intro: mono_onI)
  1155        (auto simp add: psi_def indicator_def phi_mono intro: mono_onI)
  1159                      "real_cond_exp M F X \<in> borel_measurable F"
  1159                      "real_cond_exp M F X \<in> borel_measurable F"
  1160                      "g \<in> borel_measurable F" "g \<in> borel_measurable M"
  1160                      "g \<in> borel_measurable F" "g \<in> borel_measurable M"
  1161                      "G \<in> borel_measurable F" "G \<in> borel_measurable M"
  1161                      "G \<in> borel_measurable F" "G \<in> borel_measurable M"
  1162     using X measurable_from_subalg[OF subalg] unfolding G_def g_def by auto
  1162     using X measurable_from_subalg[OF subalg] unfolding G_def g_def by auto
  1163   have int1: "integrable (restr_to_subalg M F) (\<lambda>x. g x * q (real_cond_exp M F X x))"
  1163   have int1: "integrable (restr_to_subalg M F) (\<lambda>x. g x * q (real_cond_exp M F X x))"
  1164     apply (rule Bochner_Integration.integrable_bound[of _ f], auto simp add: subalg `integrable (restr_to_subalg M F) f`)
  1164     apply (rule Bochner_Integration.integrable_bound[of _ f], auto simp add: subalg \<open>integrable (restr_to_subalg M F) f\<close>)
  1165     unfolding g_def by (auto simp add: divide_simps abs_mult algebra_simps)
  1165     unfolding g_def by (auto simp add: divide_simps abs_mult algebra_simps)
  1166   have int2: "integrable M (\<lambda>x. G x * (X x - real_cond_exp M F X x))"
  1166   have int2: "integrable M (\<lambda>x. G x * (X x - real_cond_exp M F X x))"
  1167     apply (rule Bochner_Integration.integrable_bound[of _ "\<lambda>x. \<bar>X x\<bar> + \<bar>real_cond_exp M F X x\<bar>"])
  1167     apply (rule Bochner_Integration.integrable_bound[of _ "\<lambda>x. \<bar>X x\<bar> + \<bar>real_cond_exp M F X x\<bar>"])
  1168     apply (auto intro!: Bochner_Integration.integrable_add integrable_abs real_cond_exp_int `integrable M X` AE_I2)
  1168     apply (auto intro!: Bochner_Integration.integrable_add integrable_abs real_cond_exp_int \<open>integrable M X\<close> AE_I2)
  1169     using G unfolding abs_mult by (meson abs_ge_zero abs_triangle_ineq4 dual_order.trans mult_left_le_one_le)
  1169     using G unfolding abs_mult by (meson abs_ge_zero abs_triangle_ineq4 dual_order.trans mult_left_le_one_le)
  1170   have int3: "integrable M (\<lambda>x. g x * q (X x))"
  1170   have int3: "integrable M (\<lambda>x. g x * q (X x))"
  1171     apply (rule Bochner_Integration.integrable_bound[of _ "\<lambda>x. q(X x)"], auto simp add: q(1) abs_mult)
  1171     apply (rule Bochner_Integration.integrable_bound[of _ "\<lambda>x. q(X x)"], auto simp add: q(1) abs_mult)
  1172     using g by (simp add: less_imp_le mult_left_le_one_le)
  1172     using g by (simp add: less_imp_le mult_left_le_one_le)
  1173 
  1173 
  1174   text {*Taking the conditional expectation of the main convexity inequality \verb+main_G+, we get
  1174   text \<open>Taking the conditional expectation of the main convexity inequality \verb+main_G+, we get
  1175          the following.*}
  1175          the following.\<close>
  1176   have "AE x in M. real_cond_exp M F (\<lambda>x. g x * q (X x)) x \<ge> real_cond_exp M F (\<lambda>x. g x * q (real_cond_exp M F X x) + G x * (X x - real_cond_exp M F X x)) x"
  1176   have "AE x in M. real_cond_exp M F (\<lambda>x. g x * q (X x)) x \<ge> real_cond_exp M F (\<lambda>x. g x * q (real_cond_exp M F X x) + G x * (X x - real_cond_exp M F X x)) x"
  1177     apply (rule real_cond_exp_mono[OF main_G])
  1177     apply (rule real_cond_exp_mono[OF main_G])
  1178     apply (rule Bochner_Integration.integrable_add[OF integrable_from_subalg[OF subalg int1]])
  1178     apply (rule Bochner_Integration.integrable_add[OF integrable_from_subalg[OF subalg int1]])
  1179     using int2 int3 by auto
  1179     using int2 int3 by auto
  1180   text {*This reduces to the desired inequality thanks to the properties of conditional expectation,
  1180   text \<open>This reduces to the desired inequality thanks to the properties of conditional expectation,
  1181          i.e., the conditional expectation of an $F$-measurable function is this function, and one can
  1181          i.e., the conditional expectation of an $F$-measurable function is this function, and one can
  1182          multiply an $F$-measurable function outside of conditional expectations.
  1182          multiply an $F$-measurable function outside of conditional expectations.
  1183          Since all these equalities only hold almost everywhere, we formulate them separately,
  1183          Since all these equalities only hold almost everywhere, we formulate them separately,
  1184          and then combine all of them to simplify the above equation, again almost everywhere.*}
  1184          and then combine all of them to simplify the above equation, again almost everywhere.\<close>
  1185   moreover have "AE x in M. real_cond_exp M F (\<lambda>x. g x * q (X x)) x = g x * real_cond_exp M F (\<lambda>x. q (X x)) x"
  1185   moreover have "AE x in M. real_cond_exp M F (\<lambda>x. g x * q (X x)) x = g x * real_cond_exp M F (\<lambda>x. q (X x)) x"
  1186     by (rule real_cond_exp_mult, auto simp add: int3)
  1186     by (rule real_cond_exp_mult, auto simp add: int3)
  1187   moreover have "AE x in M. real_cond_exp M F (\<lambda>x. g x * q (real_cond_exp M F X x) + G x * (X x - real_cond_exp M F X x)) x
  1187   moreover have "AE x in M. real_cond_exp M F (\<lambda>x. g x * q (real_cond_exp M F X x) + G x * (X x - real_cond_exp M F X x)) x
  1188       = real_cond_exp M F (\<lambda>x. g x * q (real_cond_exp M F X x)) x + real_cond_exp M F (\<lambda>x. G x * (X x - real_cond_exp M F X x)) x"
  1188       = real_cond_exp M F (\<lambda>x. g x * q (real_cond_exp M F X x)) x + real_cond_exp M F (\<lambda>x. G x * (X x - real_cond_exp M F X x)) x"
  1189     by (rule real_cond_exp_add, auto simp add: integrable_from_subalg[OF subalg int1] int2)
  1189     by (rule real_cond_exp_add, auto simp add: integrable_from_subalg[OF subalg int1] int2)
  1190   moreover have "AE x in M. real_cond_exp M F (\<lambda>x. g x * q (real_cond_exp M F X x)) x = g x * q (real_cond_exp M F X x)"
  1190   moreover have "AE x in M. real_cond_exp M F (\<lambda>x. g x * q (real_cond_exp M F X x)) x = g x * q (real_cond_exp M F X x)"
  1191     by (rule real_cond_exp_F_meas, auto simp add: integrable_from_subalg[OF subalg int1])
  1191     by (rule real_cond_exp_F_meas, auto simp add: integrable_from_subalg[OF subalg int1])
  1192   moreover have "AE x in M. real_cond_exp M F (\<lambda>x. G x * (X x - real_cond_exp M F X x)) x = G x * real_cond_exp M F (\<lambda>x. (X x - real_cond_exp M F X x)) x"
  1192   moreover have "AE x in M. real_cond_exp M F (\<lambda>x. G x * (X x - real_cond_exp M F X x)) x = G x * real_cond_exp M F (\<lambda>x. (X x - real_cond_exp M F X x)) x"
  1193     by (rule real_cond_exp_mult, auto simp add: int2)
  1193     by (rule real_cond_exp_mult, auto simp add: int2)
  1194   moreover have "AE x in M. real_cond_exp M F (\<lambda>x. (X x - real_cond_exp M F X x)) x = real_cond_exp M F X x - real_cond_exp M F (\<lambda>x. real_cond_exp M F X x) x"
  1194   moreover have "AE x in M. real_cond_exp M F (\<lambda>x. (X x - real_cond_exp M F X x)) x = real_cond_exp M F X x - real_cond_exp M F (\<lambda>x. real_cond_exp M F X x) x"
  1195     by (rule real_cond_exp_diff, auto intro!: real_cond_exp_int `integrable M X`)
  1195     by (rule real_cond_exp_diff, auto intro!: real_cond_exp_int \<open>integrable M X\<close>)
  1196   moreover have "AE x in M. real_cond_exp M F (\<lambda>x. real_cond_exp M F X x) x = real_cond_exp M F X x "
  1196   moreover have "AE x in M. real_cond_exp M F (\<lambda>x. real_cond_exp M F X x) x = real_cond_exp M F X x "
  1197     by (rule real_cond_exp_F_meas, auto intro!: real_cond_exp_int `integrable M X`)
  1197     by (rule real_cond_exp_F_meas, auto intro!: real_cond_exp_int \<open>integrable M X\<close>)
  1198   ultimately have "AE x in M. g x * real_cond_exp M F (\<lambda>x. q (X x)) x \<ge> g x * q (real_cond_exp M F X x)"
  1198   ultimately have "AE x in M. g x * real_cond_exp M F (\<lambda>x. q (X x)) x \<ge> g x * q (real_cond_exp M F X x)"
  1199     by auto
  1199     by auto
  1200   then show "AE x in M. real_cond_exp M F (\<lambda>x. q (X x)) x \<ge> q (real_cond_exp M F X x)"
  1200   then show "AE x in M. real_cond_exp M F (\<lambda>x. q (X x)) x \<ge> q (real_cond_exp M F X x)"
  1201     using g(1) by (auto simp add: divide_simps)
  1201     using g(1) by (auto simp add: divide_simps)
  1202 qed
  1202 qed
  1203 
  1203 
  1204 text {*Jensen's inequality does not imply that $q(E(X|F))$ is integrable, as it only proves an upper
  1204 text \<open>Jensen's inequality does not imply that $q(E(X|F))$ is integrable, as it only proves an upper
  1205 bound for it. Indeed, this is not true in general, as the following counterexample shows:
  1205 bound for it. Indeed, this is not true in general, as the following counterexample shows:
  1206 
  1206 
  1207 on $[1,\infty)$ with Lebesgue measure, let $F$ be the sigma-algebra generated by the intervals $[n, n+1)$
  1207 on $[1,\infty)$ with Lebesgue measure, let $F$ be the sigma-algebra generated by the intervals $[n, n+1)$
  1208 for integer $n$. Let $q(x) = - \sqrt{x}$ for $x\geq 0$. Define $X$ which is equal to $1/n$ over
  1208 for integer $n$. Let $q(x) = - \sqrt{x}$ for $x\geq 0$. Define $X$ which is equal to $1/n$ over
  1209 $[n, n+1/n)$ and $2^{-n}$ on $[n+1/n, n+1)$. Then $X$ is integrable as $\sum 1/n^2 < \infty$, and
  1209 $[n, n+1/n)$ and $2^{-n}$ on $[n+1/n, n+1)$. Then $X$ is integrable as $\sum 1/n^2 < \infty$, and
  1212 its values in $I=(0,\infty)$). Hence, $q(E(X|F))$ is equal to $-1/n$ on $[n, n+1)$, hence it is not
  1212 its values in $I=(0,\infty)$). Hence, $q(E(X|F))$ is equal to $-1/n$ on $[n, n+1)$, hence it is not
  1213 integrable.
  1213 integrable.
  1214 
  1214 
  1215 However, this counterexample is essentially the only situation where this function is not
  1215 However, this counterexample is essentially the only situation where this function is not
  1216 integrable, as shown by the next lemma.
  1216 integrable, as shown by the next lemma.
  1217 *}
  1217 \<close>
  1218 
  1218 
  1219 lemma integrable_convex_cond_exp:
  1219 lemma integrable_convex_cond_exp:
  1220   fixes q :: "real \<Rightarrow> real"
  1220   fixes q :: "real \<Rightarrow> real"
  1221   assumes X: "integrable M X" "AE x in M. X x \<in> I"
  1221   assumes X: "integrable M X" "AE x in M. X x \<in> I"
  1222   assumes I: "I = {a <..< b} \<or> I = {a <..} \<or> I = {..< b} \<or> I = UNIV"
  1222   assumes I: "I = {a <..< b} \<or> I = {a <..} \<or> I = {..< b} \<or> I = UNIV"
  1240   next
  1240   next
  1241     case 2
  1241     case 2
  1242     interpret finite_measure M using 2 by (auto intro!: finite_measureI)
  1242     interpret finite_measure M using 2 by (auto intro!: finite_measureI)
  1243 
  1243 
  1244     have "I \<noteq> {}"
  1244     have "I \<noteq> {}"
  1245       using `AE x in M. X x \<in> I` 2 eventually_mono integral_less_AE_space by fastforce
  1245       using \<open>AE x in M. X x \<in> I\<close> 2 eventually_mono integral_less_AE_space by fastforce
  1246     then obtain z where "z \<in> I" by auto
  1246     then obtain z where "z \<in> I" by auto
  1247 
  1247 
  1248     define A where "A = Inf ((\<lambda>t. (q z - q t) / (z - t)) ` ({z<..} \<inter> I))"
  1248     define A where "A = Inf ((\<lambda>t. (q z - q t) / (z - t)) ` ({z<..} \<inter> I))"
  1249     have "q y \<ge> q z + A * (y - z)" if "y \<in> I" for y unfolding A_def apply (rule convex_le_Inf_differential)
  1249     have "q y \<ge> q z + A * (y - z)" if "y \<in> I" for y unfolding A_def apply (rule convex_le_Inf_differential)
  1250       using `z \<in> I` `y \<in> I` `interior I = I` q(2) by auto
  1250       using \<open>z \<in> I\<close> \<open>y \<in> I\<close> \<open>interior I = I\<close> q(2) by auto
  1251     then have "AE x in M. q (real_cond_exp M F X x) \<ge> q z + A * (real_cond_exp M F X x - z)"
  1251     then have "AE x in M. q (real_cond_exp M F X x) \<ge> q z + A * (real_cond_exp M F X x - z)"
  1252       using real_cond_exp_jensens_inequality(1)[OF X I q] by auto
  1252       using real_cond_exp_jensens_inequality(1)[OF X I q] by auto
  1253     moreover have "AE x in M. q (real_cond_exp M F X x) \<le> real_cond_exp M F (\<lambda>x. q (X x)) x"
  1253     moreover have "AE x in M. q (real_cond_exp M F X x) \<le> real_cond_exp M F (\<lambda>x. q (X x)) x"
  1254       using real_cond_exp_jensens_inequality(2)[OF X I q] by auto
  1254       using real_cond_exp_jensens_inequality(2)[OF X I q] by auto
  1255     moreover have "\<bar>a\<bar> \<le> \<bar>b\<bar> + \<bar>c\<bar>" if "b \<le> a \<and> a \<le> c" for a b c::real
  1255     moreover have "\<bar>a\<bar> \<le> \<bar>b\<bar> + \<bar>c\<bar>" if "b \<le> a \<and> a \<le> c" for a b c::real
  1269     proof (rule ccontr)
  1269     proof (rule ccontr)
  1270       assume *: "\<not>(q(0) = 0)"
  1270       assume *: "\<not>(q(0) = 0)"
  1271       define e where "e = \<bar>q(0)\<bar> / 2"
  1271       define e where "e = \<bar>q(0)\<bar> / 2"
  1272       then have "e > 0" using * by auto
  1272       then have "e > 0" using * by auto
  1273       have "continuous (at 0) q"
  1273       have "continuous (at 0) q"
  1274         using q(2) `0 \<in> I` `open I` \<open>interior I = I\<close> continuous_on_interior convex_on_continuous by blast
  1274         using q(2) \<open>0 \<in> I\<close> \<open>open I\<close> \<open>interior I = I\<close> continuous_on_interior convex_on_continuous by blast
  1275       then obtain d where d: "d > 0" "\<And>y. \<bar>y - 0\<bar> < d \<Longrightarrow> \<bar>q y - q 0\<bar> < e" using `e > 0`
  1275       then obtain d where d: "d > 0" "\<And>y. \<bar>y - 0\<bar> < d \<Longrightarrow> \<bar>q y - q 0\<bar> < e" using \<open>e > 0\<close>
  1276         by (metis continuous_at_real_range real_norm_def)
  1276         by (metis continuous_at_real_range real_norm_def)
  1277       then have *: "\<bar>q(y)\<bar> > e" if "\<bar>y\<bar> < d" for y
  1277       then have *: "\<bar>q(y)\<bar> > e" if "\<bar>y\<bar> < d" for y
  1278       proof -
  1278       proof -
  1279         have "\<bar>q 0\<bar> \<le> \<bar>q 0 - q y\<bar> + \<bar>q y\<bar>" by auto
  1279         have "\<bar>q 0\<bar> \<le> \<bar>q 0 - q y\<bar> + \<bar>q y\<bar>" by auto
  1280         also have "... < e + \<bar>q y\<bar>" using d(2) that by force
  1280         also have "... < e + \<bar>q y\<bar>" using d(2) that by force
  1281         finally have "\<bar>q y\<bar> > \<bar>q 0\<bar> - e" by auto
  1281         finally have "\<bar>q y\<bar> > \<bar>q 0\<bar> - e" by auto
  1282         then show ?thesis unfolding e_def by simp
  1282         then show ?thesis unfolding e_def by simp
  1283       qed
  1283       qed
  1284       have "emeasure M {x \<in> space M. \<bar>X x\<bar> < d} \<le> emeasure M ({x \<in> space M. 1 \<le> ennreal(1/e) * \<bar>q(X x)\<bar>} \<inter> space M)"
  1284       have "emeasure M {x \<in> space M. \<bar>X x\<bar> < d} \<le> emeasure M ({x \<in> space M. 1 \<le> ennreal(1/e) * \<bar>q(X x)\<bar>} \<inter> space M)"
  1285         by (rule emeasure_mono, auto simp add: * `e>0` less_imp_le ennreal_mult''[symmetric])
  1285         by (rule emeasure_mono, auto simp add: * \<open>e>0\<close> less_imp_le ennreal_mult''[symmetric])
  1286       also have "... \<le> (1/e) * (\<integral>\<^sup>+x. ennreal(\<bar>q(X x)\<bar>) * indicator (space M) x \<partial>M)"
  1286       also have "... \<le> (1/e) * (\<integral>\<^sup>+x. ennreal(\<bar>q(X x)\<bar>) * indicator (space M) x \<partial>M)"
  1287         by (rule nn_integral_Markov_inequality, auto)
  1287         by (rule nn_integral_Markov_inequality, auto)
  1288       also have "... = (1/e) * (\<integral>\<^sup>+x. ennreal(\<bar>q(X x)\<bar>) \<partial>M)" by auto
  1288       also have "... = (1/e) * (\<integral>\<^sup>+x. ennreal(\<bar>q(X x)\<bar>) \<partial>M)" by auto
  1289       also have "... = (1/e) * ennreal(\<integral>x. \<bar>q(X x)\<bar> \<partial>M)"
  1289       also have "... = (1/e) * ennreal(\<integral>x. \<bar>q(X x)\<bar> \<partial>M)"
  1290         using nn_integral_eq_integral[OF integrable_abs[OF q(1)]] by auto
  1290         using nn_integral_eq_integral[OF integrable_abs[OF q(1)]] by auto
  1291       also have "... < \<infinity>"
  1291       also have "... < \<infinity>"
  1292         by (simp add: ennreal_mult_less_top)
  1292         by (simp add: ennreal_mult_less_top)
  1293       finally have A: "emeasure M {x \<in> space M. \<bar>X x\<bar> < d} < \<infinity>" by simp
  1293       finally have A: "emeasure M {x \<in> space M. \<bar>X x\<bar> < d} < \<infinity>" by simp
  1294 
  1294 
  1295       have "{x \<in> space M. \<bar>X x\<bar> \<ge> d} = {x \<in> space M. 1 \<le> ennreal(1/d) * \<bar>X x\<bar>} \<inter> space M"
  1295       have "{x \<in> space M. \<bar>X x\<bar> \<ge> d} = {x \<in> space M. 1 \<le> ennreal(1/d) * \<bar>X x\<bar>} \<inter> space M"
  1296         by (auto simp add: `d>0` ennreal_mult''[symmetric])
  1296         by (auto simp add: \<open>d>0\<close> ennreal_mult''[symmetric])
  1297       then have "emeasure M {x \<in> space M. \<bar>X x\<bar> \<ge> d} = emeasure M ({x \<in> space M. 1 \<le> ennreal(1/d) * \<bar>X x\<bar>} \<inter> space M)"
  1297       then have "emeasure M {x \<in> space M. \<bar>X x\<bar> \<ge> d} = emeasure M ({x \<in> space M. 1 \<le> ennreal(1/d) * \<bar>X x\<bar>} \<inter> space M)"
  1298         by auto
  1298         by auto
  1299       also have "... \<le> (1/d) * (\<integral>\<^sup>+x. ennreal(\<bar>X x\<bar>) * indicator (space M) x \<partial>M)"
  1299       also have "... \<le> (1/d) * (\<integral>\<^sup>+x. ennreal(\<bar>X x\<bar>) * indicator (space M) x \<partial>M)"
  1300         by (rule nn_integral_Markov_inequality, auto)
  1300         by (rule nn_integral_Markov_inequality, auto)
  1301       also have "... = (1/d) * (\<integral>\<^sup>+x. ennreal(\<bar>X x\<bar>) \<partial>M)" by auto
  1301       also have "... = (1/d) * (\<integral>\<^sup>+x. ennreal(\<bar>X x\<bar>) \<partial>M)" by auto
  1309       then have "emeasure M (space M) = emeasure M ({x \<in> space M. \<bar>X x\<bar> < d} \<union> {x \<in> space M. \<bar>X x\<bar> \<ge> d})"
  1309       then have "emeasure M (space M) = emeasure M ({x \<in> space M. \<bar>X x\<bar> < d} \<union> {x \<in> space M. \<bar>X x\<bar> \<ge> d})"
  1310         by simp
  1310         by simp
  1311       also have "... \<le> emeasure M {x \<in> space M. \<bar>X x\<bar> < d} + emeasure M {x \<in> space M. \<bar>X x\<bar> \<ge> d}"
  1311       also have "... \<le> emeasure M {x \<in> space M. \<bar>X x\<bar> < d} + emeasure M {x \<in> space M. \<bar>X x\<bar> \<ge> d}"
  1312         by (auto intro!: emeasure_subadditive)
  1312         by (auto intro!: emeasure_subadditive)
  1313       also have "... < \<infinity>" using A B by auto
  1313       also have "... < \<infinity>" using A B by auto
  1314       finally show False using `emeasure M (space M) = \<infinity>` by auto
  1314       finally show False using \<open>emeasure M (space M) = \<infinity>\<close> by auto
  1315     qed
  1315     qed
  1316 
  1316 
  1317     define A where "A = Inf ((\<lambda>t. (q 0 - q t) / (0 - t)) ` ({0<..} \<inter> I))"
  1317     define A where "A = Inf ((\<lambda>t. (q 0 - q t) / (0 - t)) ` ({0<..} \<inter> I))"
  1318     have "q y \<ge> q 0 + A * (y - 0)" if "y \<in> I" for y unfolding A_def apply (rule convex_le_Inf_differential)
  1318     have "q y \<ge> q 0 + A * (y - 0)" if "y \<in> I" for y unfolding A_def apply (rule convex_le_Inf_differential)
  1319       using `0 \<in> I` `y \<in> I` `interior I = I` q(2) by auto
  1319       using \<open>0 \<in> I\<close> \<open>y \<in> I\<close> \<open>interior I = I\<close> q(2) by auto
  1320     then have "q y \<ge> A * y" if "y \<in> I" for y using `q 0 = 0` that by auto
  1320     then have "q y \<ge> A * y" if "y \<in> I" for y using \<open>q 0 = 0\<close> that by auto
  1321     then have "AE x in M. q (real_cond_exp M F X x) \<ge> A * real_cond_exp M F X x"
  1321     then have "AE x in M. q (real_cond_exp M F X x) \<ge> A * real_cond_exp M F X x"
  1322       using real_cond_exp_jensens_inequality(1)[OF X I q] by auto
  1322       using real_cond_exp_jensens_inequality(1)[OF X I q] by auto
  1323     moreover have "AE x in M. q (real_cond_exp M F X x) \<le> real_cond_exp M F (\<lambda>x. q (X x)) x"
  1323     moreover have "AE x in M. q (real_cond_exp M F X x) \<le> real_cond_exp M F (\<lambda>x. q (X x)) x"
  1324       using real_cond_exp_jensens_inequality(2)[OF X I q] by auto
  1324       using real_cond_exp_jensens_inequality(2)[OF X I q] by auto
  1325     moreover have "\<bar>a\<bar> \<le> \<bar>b\<bar> + \<bar>c\<bar>" if "b \<le> a \<and> a \<le> c" for a b c::real
  1325     moreover have "\<bar>a\<bar> \<le> \<bar>b\<bar> + \<bar>c\<bar>" if "b \<le> a \<and> a \<le> c" for a b c::real