add the proof of the central limit theorem
authorhoelzl
Wed Jan 06 12:18:53 2016 +0100 (2016-01-06)
changeset 620837582b39f51ed
parent 62082 614ef6d7a6b6
child 62084 969119292e25
add the proof of the central limit theorem
CONTRIBUTORS
NEWS
src/HOL/Library/ContNotDenum.thy
src/HOL/Library/Extended_Real.thy
src/HOL/Multivariate_Analysis/Topology_Euclidean_Space.thy
src/HOL/Parity.thy
src/HOL/Power.thy
src/HOL/Probability/Binary_Product_Measure.thy
src/HOL/Probability/Bochner_Integration.thy
src/HOL/Probability/Borel_Space.thy
src/HOL/Probability/Central_Limit_Theorem.thy
src/HOL/Probability/Characteristic_Functions.thy
src/HOL/Probability/Distribution_Functions.thy
src/HOL/Probability/Helly_Selection.thy
src/HOL/Probability/Interval_Integral.thy
src/HOL/Probability/Lebesgue_Integral_Substitution.thy
src/HOL/Probability/Levy.thy
src/HOL/Probability/Nonnegative_Lebesgue_Integration.thy
src/HOL/Probability/Probability.thy
src/HOL/Probability/Probability_Mass_Function.thy
src/HOL/Probability/Probability_Measure.thy
src/HOL/Probability/Radon_Nikodym.thy
src/HOL/Probability/Set_Integral.thy
src/HOL/Probability/Sigma_Algebra.thy
src/HOL/Probability/Sinc_Integral.thy
src/HOL/Probability/Weak_Convergence.thy
src/HOL/Real.thy
src/HOL/Set.thy
src/HOL/Topological_Spaces.thy
src/HOL/Transcendental.thy
     1.1 --- a/CONTRIBUTORS	Wed Jan 06 13:04:31 2016 +0100
     1.2 +++ b/CONTRIBUTORS	Wed Jan 06 12:18:53 2016 +0100
     1.3 @@ -6,6 +6,10 @@
     1.4  Contributions to Isabelle2016
     1.5  -----------------------------
     1.6  
     1.7 +* Summer 2014: Jeremy Avigad, Luke Serafin, CMU, and Johannes Hölzl, TUM
     1.8 +  Proof of the central limit theorem: includes weak convergence, characteristic
     1.9 +  functions, and Levy's uniqueness and continuity theorem.
    1.10 +
    1.11  * Winter 2015: Manuel Eberl, TUM
    1.12    The radius of convergence of power series and various summability tests.
    1.13    Harmonic numbers and the Euler-Mascheroni constant.
     2.1 --- a/NEWS	Wed Jan 06 13:04:31 2016 +0100
     2.2 +++ b/NEWS	Wed Jan 06 12:18:53 2016 +0100
     2.3 @@ -660,6 +660,9 @@
     2.4  Gamma/log-Gamma/Digamma/ Polygamma functions and their most important
     2.5  properties.
     2.6  
     2.7 +* Probability: The central limit theorem based on Levy's uniqueness and
     2.8 +continuity theorems, weak convergence, and characterisitc functions.
     2.9 +
    2.10  * Data_Structures: new and growing session of standard data structures.
    2.11  
    2.12  * Imperative_HOL: obsolete theory Legacy_Mrec has been removed.
     3.1 --- a/src/HOL/Library/ContNotDenum.thy	Wed Jan 06 13:04:31 2016 +0100
     3.2 +++ b/src/HOL/Library/ContNotDenum.thy	Wed Jan 06 12:18:53 2016 +0100
     3.3 @@ -159,4 +159,21 @@
     3.4    thus ?thesis by auto
     3.5  qed
     3.6  
     3.7 +lemma open_minus_countable:
     3.8 +  fixes S A :: "real set" assumes "countable A" "S \<noteq> {}" "open S"
     3.9 +  shows "\<exists>x\<in>S. x \<notin> A"
    3.10 +proof -
    3.11 +  obtain x where "x \<in> S"
    3.12 +    using \<open>S \<noteq> {}\<close> by auto
    3.13 +  then obtain e where "0 < e" "{y. dist y x < e} \<subseteq> S"
    3.14 +    using \<open>open S\<close> by (auto simp: open_dist subset_eq)
    3.15 +  moreover have "{y. dist y x < e} = {x - e <..< x + e}"
    3.16 +    by (auto simp: dist_real_def)
    3.17 +  ultimately have "uncountable (S - A)"
    3.18 +    using uncountable_open_interval[of "x - e" "x + e"] \<open>countable A\<close>
    3.19 +    by (intro uncountable_minus_countable) (auto dest: countable_subset)
    3.20 +  then show ?thesis
    3.21 +    unfolding uncountable_def by auto
    3.22 +qed
    3.23 +
    3.24  end
     4.1 --- a/src/HOL/Library/Extended_Real.thy	Wed Jan 06 13:04:31 2016 +0100
     4.2 +++ b/src/HOL/Library/Extended_Real.thy	Wed Jan 06 12:18:53 2016 +0100
     4.3 @@ -1882,6 +1882,18 @@
     4.4      by (auto intro!: INF_lower bdd_belowI[of _ r] simp add: ereal_less_eq(3)[symmetric] simp del: ereal_less_eq)
     4.5  qed (auto simp: mono_def continuous_at_imp_continuous_at_within continuous_at_ereal)
     4.6  
     4.7 +lemma ereal_Inf':
     4.8 +  assumes *: "bdd_below A" "A \<noteq> {}"
     4.9 +  shows "ereal (Inf A) = (INF a:A. ereal a)"
    4.10 +proof (rule ereal_Inf)
    4.11 +  from * obtain l u where "\<And>x. x \<in> A \<Longrightarrow> l \<le> x" "u \<in> A"
    4.12 +    by (auto simp: bdd_below_def)
    4.13 +  then have "l \<le> (INF x:A. ereal x)" "(INF x:A. ereal x) \<le> u"
    4.14 +    by (auto intro!: INF_greatest INF_lower)
    4.15 +  then show "\<bar>INF a:A. ereal a\<bar> \<noteq> \<infinity>"
    4.16 +    by auto
    4.17 +qed
    4.18 +
    4.19  lemma ereal_INF: "\<bar>INF a:A. ereal (f a)\<bar> \<noteq> \<infinity> \<Longrightarrow> ereal (INF a:A. f a) = (INF a:A. ereal (f a))"
    4.20    using ereal_Inf[of "f`A"] by auto
    4.21  
     5.1 --- a/src/HOL/Multivariate_Analysis/Topology_Euclidean_Space.thy	Wed Jan 06 13:04:31 2016 +0100
     5.2 +++ b/src/HOL/Multivariate_Analysis/Topology_Euclidean_Space.thy	Wed Jan 06 12:18:53 2016 +0100
     5.3 @@ -1851,6 +1851,16 @@
     5.4      "closed s \<Longrightarrow> (closedin (subtopology euclidean s) t \<longleftrightarrow> closed t \<and> t \<subseteq> s)"
     5.5    by (meson closed_in_limpt closed_subset closedin_closed_trans)
     5.6  
     5.7 +lemma bdd_below_closure:
     5.8 +  fixes A :: "real set"
     5.9 +  assumes "bdd_below A"
    5.10 +  shows "bdd_below (closure A)"
    5.11 +proof -
    5.12 +  from assms obtain m where "\<And>x. x \<in> A \<Longrightarrow> m \<le> x" unfolding bdd_below_def by auto
    5.13 +  hence "A \<subseteq> {m..}" by auto
    5.14 +  hence "closure A \<subseteq> {m..}" using closed_real_atLeast by (rule closure_minimal)
    5.15 +  thus ?thesis unfolding bdd_below_def by auto
    5.16 +qed
    5.17  
    5.18  subsection\<open>Connected components, considered as a connectedness relation or a set\<close>
    5.19  
    5.20 @@ -2649,6 +2659,17 @@
    5.21    shows "S \<noteq> {} \<Longrightarrow> bdd_below S \<Longrightarrow> closed S \<Longrightarrow> Inf S \<in> S"
    5.22    by (metis closure_contains_Inf closure_closed assms)
    5.23  
    5.24 +lemma closed_subset_contains_Inf:
    5.25 +  fixes A C :: "real set"
    5.26 +  shows "closed C \<Longrightarrow> A \<subseteq> C \<Longrightarrow> A \<noteq> {} \<Longrightarrow> bdd_below A \<Longrightarrow> Inf A \<in> C"
    5.27 +  by (metis closure_contains_Inf closure_minimal subset_eq)
    5.28 +
    5.29 +lemma atLeastAtMost_subset_contains_Inf:
    5.30 +  fixes A :: "real set" and a b :: real 
    5.31 +  shows "A \<noteq> {} \<Longrightarrow> a \<le> b \<Longrightarrow> A \<subseteq> {a..b} \<Longrightarrow> Inf A \<in> {a..b}"
    5.32 +  by (rule closed_subset_contains_Inf) 
    5.33 +     (auto intro: closed_real_atLeastAtMost intro!: bdd_belowI[of A a])
    5.34 +
    5.35  lemma not_trivial_limit_within_ball:
    5.36    "\<not> trivial_limit (at x within S) \<longleftrightarrow> (\<forall>e>0. S \<inter> ball x e - {x} \<noteq> {})"
    5.37    (is "?lhs \<longleftrightarrow> ?rhs")
     6.1 --- a/src/HOL/Parity.thy	Wed Jan 06 13:04:31 2016 +0100
     6.2 +++ b/src/HOL/Parity.thy	Wed Jan 06 12:18:53 2016 +0100
     6.3 @@ -321,7 +321,10 @@
     6.4      with \<open>a \<le> b\<close> show ?thesis using power_mono by auto
     6.5    qed
     6.6  qed
     6.7 - 
     6.8 +
     6.9 +lemma (in comm_ring_1) uminus_power_if: "(- x) ^ n = (if even n then x^n else - (x ^ n))"
    6.10 +  by auto
    6.11 +
    6.12  text \<open>Simplify, when the exponent is a numeral\<close>
    6.13  
    6.14  lemma zero_le_power_eq_numeral [simp]:
     7.1 --- a/src/HOL/Power.thy	Wed Jan 06 13:04:31 2016 +0100
     7.2 +++ b/src/HOL/Power.thy	Wed Jan 06 12:18:53 2016 +0100
     7.3 @@ -755,7 +755,6 @@
     7.4    "(x - y)\<^sup>2 = (y - x)\<^sup>2"
     7.5    by (simp add: algebra_simps power2_eq_square)
     7.6  
     7.7 -
     7.8  text \<open>Simprules for comparisons where common factors can be cancelled.\<close>
     7.9  
    7.10  lemmas zero_compare_simps =
     8.1 --- a/src/HOL/Probability/Binary_Product_Measure.thy	Wed Jan 06 13:04:31 2016 +0100
     8.2 +++ b/src/HOL/Probability/Binary_Product_Measure.thy	Wed Jan 06 12:18:53 2016 +0100
     8.3 @@ -1012,7 +1012,7 @@
     8.4      also have "\<dots> = (\<integral>\<^sup>+ y. ereal (1 / Suc n) * indicator C' (x, y) \<partial>count_space UNIV)"
     8.5        by(rule nn_integral_cong)(force split: split_indicator intro: rev_image_eqI simp add: C')
     8.6      also have "\<dots> = (\<integral>\<^sup>+ x'. (\<integral>\<^sup>+ y. ereal (1 / Suc n) * indicator C' (x, y) \<partial>count_space UNIV) * indicator {x} x' \<partial>count_space UNIV)"
     8.7 -      by(simp add: one_ereal_def[symmetric] nn_integral_nonneg nn_integral_cmult_indicator)
     8.8 +      by(simp add: one_ereal_def[symmetric] nn_integral_nonneg max_def)
     8.9      also have "\<dots> \<le> (\<integral>\<^sup>+ x. \<integral>\<^sup>+ y. ereal (1 / Suc n) * indicator C' (x, y) \<partial>count_space UNIV \<partial>count_space UNIV)"
    8.10        by(rule nn_integral_mono)(simp split: split_indicator add: nn_integral_nonneg)
    8.11      also have "\<dots> \<le> ?lhs" using **
    8.12 @@ -1024,7 +1024,7 @@
    8.13      have "\<infinity> = \<integral>\<^sup>+ x. ereal (1 / Suc n) * indicator C' x \<partial>count_space UNIV"
    8.14        using C'_def False by(simp add: nn_integral_cmult)
    8.15      also have "\<dots> = \<integral>\<^sup>+ x. \<integral>\<^sup>+ y. ereal (1 / Suc n) * indicator C' x * indicator {SOME y. (x, y) \<in> C} y \<partial>count_space UNIV \<partial>count_space UNIV"
    8.16 -      by(auto simp add: one_ereal_def[symmetric] nn_integral_cmult_indicator intro: nn_integral_cong)
    8.17 +      by(auto simp add: one_ereal_def[symmetric] max_def intro: nn_integral_cong)
    8.18      also have "\<dots> \<le> \<integral>\<^sup>+ x. \<integral>\<^sup>+ y. ereal (1 / Suc n) * indicator C (x, y) \<partial>count_space UNIV \<partial>count_space UNIV"
    8.19        by(intro nn_integral_mono)(auto simp add: C'_def split: split_indicator intro: someI)
    8.20      also have "\<dots> \<le> ?lhs" using C
    8.21 @@ -1121,4 +1121,4 @@
    8.22      by (auto simp: space_pair_measure M.emeasure_pair_measure_Times)
    8.23  qed
    8.24  
    8.25 -end
    8.26 \ No newline at end of file
    8.27 +end
     9.1 --- a/src/HOL/Probability/Bochner_Integration.thy	Wed Jan 06 13:04:31 2016 +0100
     9.2 +++ b/src/HOL/Probability/Bochner_Integration.thy	Wed Jan 06 12:18:53 2016 +0100
     9.3 @@ -1042,10 +1042,14 @@
     9.4      integral\<^sup>L M (\<lambda>x. f x - g x) = integral\<^sup>L M f - integral\<^sup>L M g"
     9.5    by (intro has_bochner_integral_integral_eq has_bochner_integral_diff has_bochner_integral_integrable)
     9.6  
     9.7 -lemma integral_setsum[simp]: "(\<And>i. i \<in> I \<Longrightarrow> integrable M (f i)) \<Longrightarrow>
     9.8 +lemma integral_setsum: "(\<And>i. i \<in> I \<Longrightarrow> integrable M (f i)) \<Longrightarrow>
     9.9    integral\<^sup>L M (\<lambda>x. \<Sum>i\<in>I. f i x) = (\<Sum>i\<in>I. integral\<^sup>L M (f i))"
    9.10    by (intro has_bochner_integral_integral_eq has_bochner_integral_setsum has_bochner_integral_integrable)
    9.11  
    9.12 +lemma integral_setsum'[simp]: "(\<And>i. i \<in> I =simp=> integrable M (f i)) \<Longrightarrow>
    9.13 +  integral\<^sup>L M (\<lambda>x. \<Sum>i\<in>I. f i x) = (\<Sum>i\<in>I. integral\<^sup>L M (f i))"
    9.14 +  unfolding simp_implies_def by (rule integral_setsum)
    9.15 +
    9.16  lemma integral_bounded_linear: "bounded_linear T \<Longrightarrow> integrable M f \<Longrightarrow>
    9.17      integral\<^sup>L M (\<lambda>x. T (f x)) = T (integral\<^sup>L M f)"
    9.18    by (metis has_bochner_integral_bounded_linear has_bochner_integral_integrable has_bochner_integral_integral_eq)
    10.1 --- a/src/HOL/Probability/Borel_Space.thy	Wed Jan 06 13:04:31 2016 +0100
    10.2 +++ b/src/HOL/Probability/Borel_Space.thy	Wed Jan 06 12:18:53 2016 +0100
    10.3 @@ -22,6 +22,272 @@
    10.4      by (auto intro: topological_basis_prod topological_basis_trivial topological_basis_imp_subbasis)  
    10.5  qed
    10.6  
    10.7 +definition "mono_on f A \<equiv> \<forall>r s. r \<in> A \<and> s \<in> A \<and> r \<le> s \<longrightarrow> f r \<le> f s"
    10.8 +
    10.9 +lemma mono_onI:
   10.10 +  "(\<And>r s. r \<in> A \<Longrightarrow> s \<in> A \<Longrightarrow> r \<le> s \<Longrightarrow> f r \<le> f s) \<Longrightarrow> mono_on f A"
   10.11 +  unfolding mono_on_def by simp
   10.12 +
   10.13 +lemma mono_onD:
   10.14 +  "\<lbrakk>mono_on f A; r \<in> A; s \<in> A; r \<le> s\<rbrakk> \<Longrightarrow> f r \<le> f s"
   10.15 +  unfolding mono_on_def by simp
   10.16 +
   10.17 +lemma mono_imp_mono_on: "mono f \<Longrightarrow> mono_on f A"
   10.18 +  unfolding mono_def mono_on_def by auto
   10.19 +
   10.20 +lemma mono_on_subset: "mono_on f A \<Longrightarrow> B \<subseteq> A \<Longrightarrow> mono_on f B"
   10.21 +  unfolding mono_on_def by auto
   10.22 +
   10.23 +definition "strict_mono_on f A \<equiv> \<forall>r s. r \<in> A \<and> s \<in> A \<and> r < s \<longrightarrow> f r < f s"
   10.24 +
   10.25 +lemma strict_mono_onI:
   10.26 +  "(\<And>r s. r \<in> A \<Longrightarrow> s \<in> A \<Longrightarrow> r < s \<Longrightarrow> f r < f s) \<Longrightarrow> strict_mono_on f A"
   10.27 +  unfolding strict_mono_on_def by simp
   10.28 +
   10.29 +lemma strict_mono_onD:
   10.30 +  "\<lbrakk>strict_mono_on f A; r \<in> A; s \<in> A; r < s\<rbrakk> \<Longrightarrow> f r < f s"
   10.31 +  unfolding strict_mono_on_def by simp
   10.32 +
   10.33 +lemma mono_on_greaterD:
   10.34 +  assumes "mono_on g A" "x \<in> A" "y \<in> A" "g x > (g (y::_::linorder) :: _ :: linorder)"
   10.35 +  shows "x > y"
   10.36 +proof (rule ccontr)
   10.37 +  assume "\<not>x > y"
   10.38 +  hence "x \<le> y" by (simp add: not_less)
   10.39 +  from assms(1-3) and this have "g x \<le> g y" by (rule mono_onD)
   10.40 +  with assms(4) show False by simp
   10.41 +qed
   10.42 +
   10.43 +lemma strict_mono_inv:
   10.44 +  fixes f :: "('a::linorder) \<Rightarrow> ('b::linorder)"
   10.45 +  assumes "strict_mono f" and "surj f" and inv: "\<And>x. g (f x) = x"
   10.46 +  shows "strict_mono g"
   10.47 +proof
   10.48 +  fix x y :: 'b assume "x < y"
   10.49 +  from \<open>surj f\<close> obtain x' y' where [simp]: "x = f x'" "y = f y'" by blast
   10.50 +  with \<open>x < y\<close> and \<open>strict_mono f\<close> have "x' < y'" by (simp add: strict_mono_less)
   10.51 +  with inv show "g x < g y" by simp
   10.52 +qed
   10.53 +
   10.54 +lemma strict_mono_on_imp_inj_on:
   10.55 +  assumes "strict_mono_on (f :: (_ :: linorder) \<Rightarrow> (_ :: preorder)) A"
   10.56 +  shows "inj_on f A"
   10.57 +proof (rule inj_onI)
   10.58 +  fix x y assume "x \<in> A" "y \<in> A" "f x = f y"
   10.59 +  thus "x = y"
   10.60 +    by (cases x y rule: linorder_cases)
   10.61 +       (auto dest: strict_mono_onD[OF assms, of x y] strict_mono_onD[OF assms, of y x]) 
   10.62 +qed
   10.63 +
   10.64 +lemma strict_mono_on_leD:
   10.65 +  assumes "strict_mono_on (f :: (_ :: linorder) \<Rightarrow> _ :: preorder) A" "x \<in> A" "y \<in> A" "x \<le> y"
   10.66 +  shows "f x \<le> f y"
   10.67 +proof (insert le_less_linear[of y x], elim disjE)
   10.68 +  assume "x < y"
   10.69 +  with assms have "f x < f y" by (rule_tac strict_mono_onD[OF assms(1)]) simp_all
   10.70 +  thus ?thesis by (rule less_imp_le)
   10.71 +qed (insert assms, simp)
   10.72 +
   10.73 +lemma strict_mono_on_eqD:
   10.74 +  fixes f :: "(_ :: linorder) \<Rightarrow> (_ :: preorder)"
   10.75 +  assumes "strict_mono_on f A" "f x = f y" "x \<in> A" "y \<in> A"
   10.76 +  shows "y = x"
   10.77 +  using assms by (rule_tac linorder_cases[of x y]) (auto dest: strict_mono_onD)
   10.78 +
   10.79 +lemma mono_on_imp_deriv_nonneg:
   10.80 +  assumes mono: "mono_on f A" and deriv: "(f has_real_derivative D) (at x)"
   10.81 +  assumes "x \<in> interior A"
   10.82 +  shows "D \<ge> 0"
   10.83 +proof (rule tendsto_le_const)
   10.84 +  let ?A' = "(\<lambda>y. y - x) ` interior A"
   10.85 +  from deriv show "((\<lambda>h. (f (x + h) - f x) / h) \<longlongrightarrow> D) (at 0)"
   10.86 +      by (simp add: field_has_derivative_at has_field_derivative_def)
   10.87 +  from mono have mono': "mono_on f (interior A)" by (rule mono_on_subset) (rule interior_subset)
   10.88 +
   10.89 +  show "eventually (\<lambda>h. (f (x + h) - f x) / h \<ge> 0) (at 0)"
   10.90 +  proof (subst eventually_at_topological, intro exI conjI ballI impI)
   10.91 +    have "open (interior A)" by simp
   10.92 +    hence "open (op + (-x) ` interior A)" by (rule open_translation)
   10.93 +    also have "(op + (-x) ` interior A) = ?A'" by auto
   10.94 +    finally show "open ?A'" .
   10.95 +  next
   10.96 +    from \<open>x \<in> interior A\<close> show "0 \<in> ?A'" by auto
   10.97 +  next
   10.98 +    fix h assume "h \<in> ?A'"
   10.99 +    hence "x + h \<in> interior A" by auto
  10.100 +    with mono' and \<open>x \<in> interior A\<close> show "(f (x + h) - f x) / h \<ge> 0"
  10.101 +      by (cases h rule: linorder_cases[of _ 0])
  10.102 +         (simp_all add: divide_nonpos_neg divide_nonneg_pos mono_onD field_simps)
  10.103 +  qed
  10.104 +qed simp
  10.105 +
  10.106 +lemma strict_mono_on_imp_mono_on: 
  10.107 +  "strict_mono_on (f :: (_ :: linorder) \<Rightarrow> _ :: preorder) A \<Longrightarrow> mono_on f A"
  10.108 +  by (rule mono_onI, rule strict_mono_on_leD)
  10.109 +
  10.110 +lemma mono_on_ctble_discont:
  10.111 +  fixes f :: "real \<Rightarrow> real"
  10.112 +  fixes A :: "real set"
  10.113 +  assumes "mono_on f A"
  10.114 +  shows "countable {a\<in>A. \<not> continuous (at a within A) f}"
  10.115 +proof -
  10.116 +  have mono: "\<And>x y. x \<in> A \<Longrightarrow> y \<in> A \<Longrightarrow> x \<le> y \<Longrightarrow> f x \<le> f y"
  10.117 +    using `mono_on f A` by (simp add: mono_on_def)
  10.118 +  have "\<forall>a \<in> {a\<in>A. \<not> continuous (at a within A) f}. \<exists>q :: nat \<times> rat.
  10.119 +      (fst q = 0 \<and> of_rat (snd q) < f a \<and> (\<forall>x \<in> A. x < a \<longrightarrow> f x < of_rat (snd q))) \<or>
  10.120 +      (fst q = 1 \<and> of_rat (snd q) > f a \<and> (\<forall>x \<in> A. x > a \<longrightarrow> f x > of_rat (snd q)))"
  10.121 +  proof (clarsimp simp del: One_nat_def)
  10.122 +    fix a assume "a \<in> A" assume "\<not> continuous (at a within A) f"
  10.123 +    thus "\<exists>q1 q2.
  10.124 +            q1 = 0 \<and> real_of_rat q2 < f a \<and> (\<forall>x\<in>A. x < a \<longrightarrow> f x < real_of_rat q2) \<or>
  10.125 +            q1 = 1 \<and> f a < real_of_rat q2 \<and> (\<forall>x\<in>A. a < x \<longrightarrow> real_of_rat q2 < f x)"
  10.126 +    proof (auto simp add: continuous_within order_tendsto_iff eventually_at)
  10.127 +      fix l assume "l < f a"
  10.128 +      then obtain q2 where q2: "l < of_rat q2" "of_rat q2 < f a"
  10.129 +        using of_rat_dense by blast
  10.130 +      assume * [rule_format]: "\<forall>d>0. \<exists>x\<in>A. x \<noteq> a \<and> dist x a < d \<and> \<not> l < f x"
  10.131 +      from q2 have "real_of_rat q2 < f a \<and> (\<forall>x\<in>A. x < a \<longrightarrow> f x < real_of_rat q2)"
  10.132 +      proof auto
  10.133 +        fix x assume "x \<in> A" "x < a"
  10.134 +        with q2 *[of "a - x"] show "f x < real_of_rat q2"
  10.135 +          apply (auto simp add: dist_real_def not_less)
  10.136 +          apply (subgoal_tac "f x \<le> f xa")
  10.137 +          by (auto intro: mono)
  10.138 +      qed 
  10.139 +      thus ?thesis by auto
  10.140 +    next
  10.141 +      fix u assume "u > f a"
  10.142 +      then obtain q2 where q2: "f a < of_rat q2" "of_rat q2 < u"
  10.143 +        using of_rat_dense by blast
  10.144 +      assume *[rule_format]: "\<forall>d>0. \<exists>x\<in>A. x \<noteq> a \<and> dist x a < d \<and> \<not> u > f x"
  10.145 +      from q2 have "real_of_rat q2 > f a \<and> (\<forall>x\<in>A. x > a \<longrightarrow> f x > real_of_rat q2)"
  10.146 +      proof auto
  10.147 +        fix x assume "x \<in> A" "x > a"
  10.148 +        with q2 *[of "x - a"] show "f x > real_of_rat q2"
  10.149 +          apply (auto simp add: dist_real_def)
  10.150 +          apply (subgoal_tac "f x \<ge> f xa")
  10.151 +          by (auto intro: mono)
  10.152 +      qed 
  10.153 +      thus ?thesis by auto
  10.154 +    qed
  10.155 +  qed
  10.156 +  hence "\<exists>g :: real \<Rightarrow> nat \<times> rat . \<forall>a \<in> {a\<in>A. \<not> continuous (at a within A) f}. 
  10.157 +      (fst (g a) = 0 \<and> of_rat (snd (g a)) < f a \<and> (\<forall>x \<in> A. x < a \<longrightarrow> f x < of_rat (snd (g a)))) |
  10.158 +      (fst (g a) = 1 \<and> of_rat (snd (g a)) > f a \<and> (\<forall>x \<in> A. x > a \<longrightarrow> f x > of_rat (snd (g a))))"
  10.159 +    by (rule bchoice)
  10.160 +  then guess g ..
  10.161 +  hence g: "\<And>a x. a \<in> A \<Longrightarrow> \<not> continuous (at a within A) f \<Longrightarrow> x \<in> A \<Longrightarrow>
  10.162 +      (fst (g a) = 0 \<and> of_rat (snd (g a)) < f a \<and> (x < a \<longrightarrow> f x < of_rat (snd (g a)))) |
  10.163 +      (fst (g a) = 1 \<and> of_rat (snd (g a)) > f a \<and> (x > a \<longrightarrow> f x > of_rat (snd (g a))))"
  10.164 +    by auto
  10.165 +  have "inj_on g {a\<in>A. \<not> continuous (at a within A) f}"
  10.166 +  proof (auto simp add: inj_on_def)
  10.167 +    fix w z
  10.168 +    assume 1: "w \<in> A" and 2: "\<not> continuous (at w within A) f" and
  10.169 +           3: "z \<in> A" and 4: "\<not> continuous (at z within A) f" and
  10.170 +           5: "g w = g z"
  10.171 +    from g [OF 1 2 3] g [OF 3 4 1] 5 
  10.172 +    show "w = z" by auto
  10.173 +  qed
  10.174 +  thus ?thesis 
  10.175 +    by (rule countableI') 
  10.176 +qed
  10.177 +
  10.178 +lemma mono_on_ctble_discont_open:
  10.179 +  fixes f :: "real \<Rightarrow> real"
  10.180 +  fixes A :: "real set"
  10.181 +  assumes "open A" "mono_on f A"
  10.182 +  shows "countable {a\<in>A. \<not>isCont f a}"
  10.183 +proof -
  10.184 +  have "{a\<in>A. \<not>isCont f a} = {a\<in>A. \<not>(continuous (at a within A) f)}"
  10.185 +    by (auto simp add: continuous_within_open [OF _ `open A`])
  10.186 +  thus ?thesis
  10.187 +    apply (elim ssubst)
  10.188 +    by (rule mono_on_ctble_discont, rule assms)
  10.189 +qed
  10.190 +
  10.191 +lemma mono_ctble_discont:
  10.192 +  fixes f :: "real \<Rightarrow> real"
  10.193 +  assumes "mono f"
  10.194 +  shows "countable {a. \<not> isCont f a}"
  10.195 +using assms mono_on_ctble_discont [of f UNIV] unfolding mono_on_def mono_def by auto
  10.196 +
  10.197 +lemma has_real_derivative_imp_continuous_on:
  10.198 +  assumes "\<And>x. x \<in> A \<Longrightarrow> (f has_real_derivative f' x) (at x)"
  10.199 +  shows "continuous_on A f"
  10.200 +  apply (intro differentiable_imp_continuous_on, unfold differentiable_on_def)
  10.201 +  apply (intro ballI Deriv.differentiableI)
  10.202 +  apply (rule has_field_derivative_subset[OF assms])
  10.203 +  apply simp_all
  10.204 +  done
  10.205 +
  10.206 +lemma closure_contains_Sup:
  10.207 +  fixes S :: "real set"
  10.208 +  assumes "S \<noteq> {}" "bdd_above S"
  10.209 +  shows "Sup S \<in> closure S"
  10.210 +proof-
  10.211 +  have "Inf (uminus ` S) \<in> closure (uminus ` S)" 
  10.212 +      using assms by (intro closure_contains_Inf) auto
  10.213 +  also have "Inf (uminus ` S) = -Sup S" by (simp add: Inf_real_def)
  10.214 +  also have "closure (uminus ` S) = uminus ` closure S"
  10.215 +      by (rule sym, intro closure_injective_linear_image) (auto intro: linearI)
  10.216 +  finally show ?thesis by auto
  10.217 +qed
  10.218 +
  10.219 +lemma closed_contains_Sup:
  10.220 +  fixes S :: "real set"
  10.221 +  shows "S \<noteq> {} \<Longrightarrow> bdd_above S \<Longrightarrow> closed S \<Longrightarrow> Sup S \<in> S"
  10.222 +  by (subst closure_closed[symmetric], assumption, rule closure_contains_Sup)
  10.223 +
  10.224 +lemma deriv_nonneg_imp_mono:
  10.225 +  assumes deriv: "\<And>x. x \<in> {a..b} \<Longrightarrow> (g has_real_derivative g' x) (at x)"
  10.226 +  assumes nonneg: "\<And>x. x \<in> {a..b} \<Longrightarrow> g' x \<ge> 0"
  10.227 +  assumes ab: "a \<le> b"
  10.228 +  shows "g a \<le> g b"
  10.229 +proof (cases "a < b")
  10.230 +  assume "a < b"
  10.231 +  from deriv have "\<forall>x. x \<ge> a \<and> x \<le> b \<longrightarrow> (g has_real_derivative g' x) (at x)" by simp
  10.232 +  from MVT2[OF \<open>a < b\<close> this] and deriv 
  10.233 +    obtain \<xi> where \<xi>_ab: "\<xi> > a" "\<xi> < b" and g_ab: "g b - g a = (b - a) * g' \<xi>" by blast
  10.234 +  from \<xi>_ab ab nonneg have "(b - a) * g' \<xi> \<ge> 0" by simp
  10.235 +  with g_ab show ?thesis by simp
  10.236 +qed (insert ab, simp)
  10.237 +
  10.238 +lemma continuous_interval_vimage_Int:
  10.239 +  assumes "continuous_on {a::real..b} g" and mono: "\<And>x y. a \<le> x \<Longrightarrow> x \<le> y \<Longrightarrow> y \<le> b \<Longrightarrow> g x \<le> g y"
  10.240 +  assumes "a \<le> b" "(c::real) \<le> d" "{c..d} \<subseteq> {g a..g b}"
  10.241 +  obtains c' d' where "{a..b} \<inter> g -` {c..d} = {c'..d'}" "c' \<le> d'" "g c' = c" "g d' = d"
  10.242 +proof-
  10.243 +    let ?A = "{a..b} \<inter> g -` {c..d}"
  10.244 +    from IVT'[of g a c b, OF _ _ \<open>a \<le> b\<close> assms(1)] assms(4,5) 
  10.245 +         obtain c'' where c'': "c'' \<in> ?A" "g c'' = c" by auto
  10.246 +    from IVT'[of g a d b, OF _ _ \<open>a \<le> b\<close> assms(1)] assms(4,5) 
  10.247 +         obtain d'' where d'': "d'' \<in> ?A" "g d'' = d" by auto
  10.248 +    hence [simp]: "?A \<noteq> {}" by blast
  10.249 +
  10.250 +    def c' \<equiv> "Inf ?A" and d' \<equiv> "Sup ?A"
  10.251 +    have "?A \<subseteq> {c'..d'}" unfolding c'_def d'_def
  10.252 +        by (intro subsetI) (auto intro: cInf_lower cSup_upper)
  10.253 +    moreover from assms have "closed ?A" 
  10.254 +        using continuous_on_closed_vimage[of "{a..b}" g] by (subst Int_commute) simp
  10.255 +    hence c'd'_in_set: "c' \<in> ?A" "d' \<in> ?A" unfolding c'_def d'_def
  10.256 +        by ((intro closed_contains_Inf closed_contains_Sup, simp_all)[])+
  10.257 +    hence "{c'..d'} \<subseteq> ?A" using assms 
  10.258 +        by (intro subsetI)
  10.259 +           (auto intro!: order_trans[of c "g c'" "g x" for x] order_trans[of "g x" "g d'" d for x] 
  10.260 +                 intro!: mono)
  10.261 +    moreover have "c' \<le> d'" using c'd'_in_set(2) unfolding c'_def by (intro cInf_lower) auto
  10.262 +    moreover have "g c' \<le> c" "g d' \<ge> d"
  10.263 +      apply (insert c'' d'' c'd'_in_set)
  10.264 +      apply (subst c''(2)[symmetric])
  10.265 +      apply (auto simp: c'_def intro!: mono cInf_lower c'') []
  10.266 +      apply (subst d''(2)[symmetric])
  10.267 +      apply (auto simp: d'_def intro!: mono cSup_upper d'') []
  10.268 +      done
  10.269 +    with c'd'_in_set have "g c' = c" "g d' = d" by auto
  10.270 +    ultimately show ?thesis using that by blast
  10.271 +qed
  10.272 +
  10.273  subsection \<open>Generic Borel spaces\<close>
  10.274  
  10.275  definition borel :: "'a::topological_space measure" where
  10.276 @@ -49,6 +315,10 @@
  10.277  lemma sets_borel: "sets borel = sigma_sets UNIV {S. open S}"
  10.278    unfolding borel_def by (rule sets_measure_of) simp
  10.279  
  10.280 +lemma measurable_sets_borel:
  10.281 +    "\<lbrakk>f \<in> measurable borel M; A \<in> sets M\<rbrakk> \<Longrightarrow> f -` A \<in> sets borel"
  10.282 +  by (drule (1) measurable_sets) simp
  10.283 +
  10.284  lemma pred_Collect_borel[measurable (raw)]: "Measurable.pred borel P \<Longrightarrow> {x. P x} \<in> sets borel"
  10.285    unfolding borel_def pred_def by auto
  10.286  
  10.287 @@ -888,6 +1158,28 @@
  10.288      by (subst borel_measurable_iff_halfspace_le) auto
  10.289  qed auto
  10.290  
  10.291 +lemma borel_set_induct[consumes 1, case_names empty interval compl union]:
  10.292 +  assumes "A \<in> sets borel" 
  10.293 +  assumes empty: "P {}" and int: "\<And>a b. a \<le> b \<Longrightarrow> P {a..b}" and compl: "\<And>A. A \<in> sets borel \<Longrightarrow> P A \<Longrightarrow> P (-A)" and
  10.294 +          un: "\<And>f. disjoint_family f \<Longrightarrow> (\<And>i. f i \<in> sets borel) \<Longrightarrow>  (\<And>i. P (f i)) \<Longrightarrow> P (\<Union>i::nat. f i)"
  10.295 +  shows "P (A::real set)"
  10.296 +proof-
  10.297 +  let ?G = "range (\<lambda>(a,b). {a..b::real})"
  10.298 +  have "Int_stable ?G" "?G \<subseteq> Pow UNIV" "A \<in> sigma_sets UNIV ?G" 
  10.299 +      using assms(1) by (auto simp add: borel_eq_atLeastAtMost Int_stable_def)
  10.300 +  thus ?thesis
  10.301 +  proof (induction rule: sigma_sets_induct_disjoint) 
  10.302 +    case (union f)
  10.303 +      from union.hyps(2) have "\<And>i. f i \<in> sets borel" by (auto simp: borel_eq_atLeastAtMost)
  10.304 +      with union show ?case by (auto intro: un)
  10.305 +  next
  10.306 +    case (basic A)
  10.307 +    then obtain a b where "A = {a .. b}" by auto
  10.308 +    then show ?case
  10.309 +      by (cases "a \<le> b") (auto intro: int empty)
  10.310 +  qed (auto intro: empty compl simp: Compl_eq_Diff_UNIV[symmetric] borel_eq_atLeastAtMost)
  10.311 +qed
  10.312 +
  10.313  subsection "Borel measurable operators"
  10.314  
  10.315  lemma borel_measurable_norm[measurable]: "norm \<in> borel_measurable borel"
  10.316 @@ -1467,6 +1759,11 @@
  10.317      by simp
  10.318  qed
  10.319  
  10.320 +lemma isCont_borel_pred[measurable]:
  10.321 +  fixes f :: "'b::metric_space \<Rightarrow> 'a::metric_space"
  10.322 +  shows "Measurable.pred borel (isCont f)"
  10.323 +  unfolding pred_def by (simp add: isCont_borel)
  10.324 +
  10.325  lemma is_real_interval:
  10.326    assumes S: "is_interval S"
  10.327    shows "\<exists>a b::real. S = {} \<or> S = UNIV \<or> S = {..<b} \<or> S = {..b} \<or> S = {a<..} \<or> S = {a..} \<or>
  10.328 @@ -1485,16 +1782,21 @@
  10.329      by auto
  10.330  qed
  10.331  
  10.332 +lemma borel_measurable_mono_on_fnc:
  10.333 +  fixes f :: "real \<Rightarrow> real" and A :: "real set"
  10.334 +  assumes "mono_on f A"
  10.335 +  shows "f \<in> borel_measurable (restrict_space borel A)"
  10.336 +  apply (rule measurable_restrict_countable[OF mono_on_ctble_discont[OF assms]])
  10.337 +  apply (auto intro!: image_eqI[where x="{x}" for x] simp: sets_restrict_space)
  10.338 +  apply (auto simp add: sets_restrict_restrict_space continuous_on_eq_continuous_within
  10.339 +              cong: measurable_cong_sets 
  10.340 +              intro!: borel_measurable_continuous_on_restrict intro: continuous_within_subset)
  10.341 +  done
  10.342 +
  10.343  lemma borel_measurable_mono:
  10.344    fixes f :: "real \<Rightarrow> real"
  10.345 -  assumes "mono f"
  10.346 -  shows "f \<in> borel_measurable borel"
  10.347 -proof (subst borel_measurable_iff_ge, auto simp add:)
  10.348 -  fix a :: real
  10.349 -  have "is_interval {w. a \<le> f w}"
  10.350 -    unfolding is_interval_1 using assms by (auto dest: monoD intro: order.trans)
  10.351 -  thus "{w. a \<le> f w} \<in> sets borel" using real_interval_borel_measurable by auto  
  10.352 -qed
  10.353 +  shows "mono f \<Longrightarrow> f \<in> borel_measurable borel"
  10.354 +  using borel_measurable_mono_on_fnc[of f UNIV] by (simp add: mono_def mono_on_def)
  10.355  
  10.356  no_notation
  10.357    eucl_less (infix "<e" 50)
    11.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    11.2 +++ b/src/HOL/Probability/Central_Limit_Theorem.thy	Wed Jan 06 12:18:53 2016 +0100
    11.3 @@ -0,0 +1,117 @@
    11.4 +(*  Theory: Central_Limit_Theorem.thy
    11.5 +    Authors: Jeremy Avigad, Luke Serafin
    11.6 +*)
    11.7 +
    11.8 +section \<open>The Central Limit Theorem\<close>
    11.9 +
   11.10 +theory Central_Limit_Theorem
   11.11 +  imports Levy
   11.12 +begin
   11.13 +
   11.14 +theorem (in prob_space) central_limit_theorem:
   11.15 +  fixes X :: "nat \<Rightarrow> 'a \<Rightarrow> real"
   11.16 +    and \<mu> :: "real measure"
   11.17 +    and \<sigma> :: real
   11.18 +    and S :: "nat \<Rightarrow> 'a \<Rightarrow> real"
   11.19 +  assumes X_indep: "indep_vars (\<lambda>i. borel) X UNIV"
   11.20 +    and X_integrable: "\<And>n. integrable M (X n)"
   11.21 +    and X_mean_0: "\<And>n. expectation (X n) = 0"
   11.22 +    and \<sigma>_pos: "\<sigma> > 0"
   11.23 +    and X_square_integrable: "\<And>n. integrable M (\<lambda>x. (X n x)\<^sup>2)"
   11.24 +    and X_variance: "\<And>n. variance (X n) = \<sigma>\<^sup>2"
   11.25 +    and X_distrib: "\<And>n. distr M borel (X n) = \<mu>"
   11.26 +  defines "S n \<equiv> \<lambda>x. \<Sum>i<n. X i x"
   11.27 +  shows "weak_conv_m (\<lambda>n. distr M borel (\<lambda>x. S n x / sqrt (n * \<sigma>\<^sup>2))) std_normal_distribution"
   11.28 +proof -
   11.29 +  let ?S' = "\<lambda>n x. S n x / sqrt (real n * \<sigma>\<^sup>2)"
   11.30 +  def \<phi> \<equiv> "\<lambda>n. char (distr M borel (?S' n))"
   11.31 +  def \<psi> \<equiv> "\<lambda>n t. char \<mu> (t / sqrt (\<sigma>\<^sup>2 * n))"
   11.32 +
   11.33 +  have X_rv [simp, measurable]: "\<And>n. random_variable borel (X n)"
   11.34 +    using X_indep unfolding indep_vars_def2 by simp
   11.35 +  interpret \<mu>: real_distribution \<mu>
   11.36 +    by (subst X_distrib [symmetric, of 0], rule real_distribution_distr, simp)
   11.37 +
   11.38 +  (* these are equivalent to the hypotheses on X, given X_distr *)
   11.39 +  have \<mu>_integrable [simp]: "integrable \<mu> (\<lambda>x. x)"
   11.40 +    and \<mu>_mean_integrable [simp]: "\<mu>.expectation (\<lambda>x. x) = 0"
   11.41 +    and \<mu>_square_integrable [simp]: "integrable \<mu> (\<lambda>x. x^2)"
   11.42 +    and \<mu>_variance [simp]: "\<mu>.expectation (\<lambda>x. x^2) = \<sigma>\<^sup>2"
   11.43 +    using assms by (simp_all add: X_distrib [symmetric, of 0] integrable_distr_eq integral_distr)
   11.44 +
   11.45 +  have main: "\<forall>\<^sub>F n in sequentially.
   11.46 +      cmod (\<phi> n t - (1 + (-(t^2) / 2) / n)^n) \<le>
   11.47 +      t\<^sup>2 / (6 * \<sigma>\<^sup>2) * (LINT x|\<mu>. min (6 * x\<^sup>2) (\<bar>t / sqrt (\<sigma>\<^sup>2 * n)\<bar> * \<bar>x\<bar> ^ 3))" for t
   11.48 +  proof (rule eventually_sequentiallyI)
   11.49 +    fix n :: nat
   11.50 +    assume "n \<ge> nat (ceiling (t^2 / 4))"
   11.51 +    hence n: "n \<ge> t^2 / 4" by (subst nat_ceiling_le_eq [symmetric])
   11.52 +    let ?t = "t / sqrt (\<sigma>\<^sup>2 * n)"
   11.53 +
   11.54 +    def \<psi>' \<equiv> "\<lambda>n i. char (distr M borel (\<lambda>x. X i x / sqrt (\<sigma>\<^sup>2 * n)))"
   11.55 +    have *: "\<And>n i t. \<psi>' n i t = \<psi> n t"
   11.56 +      unfolding \<psi>_def \<psi>'_def char_def
   11.57 +      by (subst X_distrib [symmetric]) (auto simp: integral_distr)
   11.58 +
   11.59 +    have "\<phi> n t = char (distr M borel (\<lambda>x. \<Sum>i<n. X i x / sqrt (\<sigma>\<^sup>2 * real n))) t"
   11.60 +      by (auto simp: \<phi>_def S_def setsum_divide_distrib ac_simps)
   11.61 +    also have "\<dots> = (\<Prod> i < n. \<psi>' n i t)"
   11.62 +      unfolding \<psi>'_def
   11.63 +      apply (rule char_distr_setsum)
   11.64 +      apply (rule indep_vars_compose2[where X=X])
   11.65 +      apply (rule indep_vars_subset)
   11.66 +      apply (rule X_indep)
   11.67 +      apply auto
   11.68 +      done
   11.69 +    also have "\<dots> = (\<psi> n t)^n"
   11.70 +      by (auto simp add: * setprod_constant)
   11.71 +    finally have \<phi>_eq: "\<phi> n t =(\<psi> n t)^n" .
   11.72 +
   11.73 +    have "norm (\<psi> n t - (1 - ?t^2 * \<sigma>\<^sup>2 / 2)) \<le> ?t\<^sup>2 / 6 * (LINT x|\<mu>. min (6 * x\<^sup>2) (\<bar>?t\<bar> * \<bar>x\<bar> ^ 3))"
   11.74 +      unfolding \<psi>_def by (rule \<mu>.char_approx3, auto)
   11.75 +    also have "?t^2 * \<sigma>\<^sup>2 = t^2 / n"
   11.76 +      using \<sigma>_pos by (simp add: power_divide)
   11.77 +    also have "t^2 / n / 2 = (t^2 / 2) / n"
   11.78 +      by simp
   11.79 +    finally have **: "norm (\<psi> n t - (1 + (-(t^2) / 2) / n)) \<le> 
   11.80 +      ?t\<^sup>2 / 6 * (LINT x|\<mu>. min (6 * x\<^sup>2) (\<bar>?t\<bar> * \<bar>x\<bar> ^ 3))" by simp
   11.81 +
   11.82 +    have "norm (\<phi> n t - (complex_of_real (1 + (-(t^2) / 2) / n))^n) \<le> 
   11.83 +         n * norm (\<psi> n t - (complex_of_real (1 + (-(t^2) / 2) / n)))"
   11.84 +      using n
   11.85 +      by (auto intro!: norm_power_diff \<mu>.cmod_char_le_1 abs_leI
   11.86 +               simp del: of_real_diff simp: of_real_diff[symmetric] divide_le_eq \<phi>_eq \<psi>_def)
   11.87 +    also have "\<dots> \<le> n * (?t\<^sup>2 / 6 * (LINT x|\<mu>. min (6 * x\<^sup>2) (\<bar>?t\<bar> * \<bar>x\<bar> ^ 3)))"
   11.88 +      by (rule mult_left_mono [OF **], simp)
   11.89 +    also have "\<dots> = (t\<^sup>2 / (6 * \<sigma>\<^sup>2) * (LINT x|\<mu>. min (6 * x\<^sup>2) (\<bar>?t\<bar> * \<bar>x\<bar> ^ 3)))" 
   11.90 +      using \<sigma>_pos by (simp add: field_simps min_absorb2)
   11.91 +    finally show "norm (\<phi> n t - (1 + (-(t^2) / 2) / n)^n) \<le> 
   11.92 +        (t\<^sup>2 / (6 * \<sigma>\<^sup>2) * (LINT x|\<mu>. min (6 * x\<^sup>2) (\<bar>?t\<bar> * \<bar>x\<bar> ^ 3)))" 
   11.93 +      by simp
   11.94 +  qed
   11.95 +
   11.96 +  show ?thesis
   11.97 +  proof (rule levy_continuity)
   11.98 +    fix t
   11.99 +    let ?t = "\<lambda>n. t / sqrt (\<sigma>\<^sup>2 * n)"
  11.100 +    have "\<And>x. (\<lambda>n. min (6 * x\<^sup>2) (\<bar>t\<bar> * \<bar>x\<bar> ^ 3 / \<bar>sqrt (\<sigma>\<^sup>2 * real n)\<bar>)) \<longlonglongrightarrow> 0"
  11.101 +      using \<sigma>_pos 
  11.102 +      by (auto simp: real_sqrt_mult min_absorb2
  11.103 +               intro!: tendsto_min[THEN tendsto_eq_rhs] sqrt_at_top[THEN filterlim_compose]
  11.104 +                       filterlim_tendsto_pos_mult_at_top filterlim_at_top_imp_at_infinity
  11.105 +                       tendsto_divide_0 filterlim_real_sequentially)
  11.106 +    then have "(\<lambda>n. LINT x|\<mu>. min (6 * x\<^sup>2) (\<bar>?t n\<bar> * \<bar>x\<bar> ^ 3)) \<longlonglongrightarrow> (LINT x|\<mu>. 0)"
  11.107 +      by (intro integral_dominated_convergence [where w = "\<lambda>x. 6 * x^2"]) auto
  11.108 +    then have *: "(\<lambda>n. t\<^sup>2 / (6 * \<sigma>\<^sup>2) * (LINT x|\<mu>. min (6 * x\<^sup>2) (\<bar>?t n\<bar> * \<bar>x\<bar> ^ 3))) \<longlonglongrightarrow> 0"
  11.109 +      by (simp only: integral_zero tendsto_mult_right_zero)
  11.110 +
  11.111 +    have "(\<lambda>n. complex_of_real ((1 + (-(t^2) / 2) / n)^n)) \<longlonglongrightarrow> complex_of_real (exp (-(t^2) / 2))"
  11.112 +      by (rule isCont_tendsto_compose [OF _ tendsto_exp_limit_sequentially]) auto
  11.113 +    then have "(\<lambda>n. \<phi> n t) \<longlonglongrightarrow> complex_of_real (exp (-(t^2) / 2))"
  11.114 +      by (rule Lim_transform) (rule Lim_null_comparison [OF main *])
  11.115 +    then show "(\<lambda>n. char (distr M borel (?S' n)) t) \<longlonglongrightarrow> char std_normal_distribution t"
  11.116 +      by (simp add: \<phi>_def char_std_normal_distribution)
  11.117 +  qed (auto intro!: real_dist_normal_dist simp: S_def)
  11.118 +qed
  11.119 +
  11.120 +end
    12.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    12.2 +++ b/src/HOL/Probability/Characteristic_Functions.thy	Wed Jan 06 12:18:53 2016 +0100
    12.3 @@ -0,0 +1,554 @@
    12.4 +(*
    12.5 +  Theory: Characteristic_Functions.thy
    12.6 +  Authors: Jeremy Avigad, Luke Serafin
    12.7 +*)
    12.8 +
    12.9 +section \<open>Characteristic Functions\<close>
   12.10 +
   12.11 +theory Characteristic_Functions
   12.12 +  imports Weak_Convergence Interval_Integral Independent_Family Distributions
   12.13 +begin
   12.14 +
   12.15 +lemma mult_min_right: "a \<ge> 0 \<Longrightarrow> (a :: real) * min b c = min (a * b) (a * c)"
   12.16 +  by (metis min.absorb_iff2 min_def mult_left_mono)
   12.17 +
   12.18 +lemma sequentially_even_odd:
   12.19 +  assumes E: "eventually (\<lambda>n. P (2 * n)) sequentially" and O: "eventually (\<lambda>n. P (2 * n + 1)) sequentially"
   12.20 +  shows "eventually P sequentially"
   12.21 +proof -
   12.22 +  from E obtain n_e where [intro]: "\<And>n. n \<ge> n_e \<Longrightarrow> P (2 * n)"
   12.23 +    by (auto simp: eventually_sequentially)
   12.24 +  moreover
   12.25 +  from O obtain n_o where [intro]: "\<And>n. n \<ge> n_o \<Longrightarrow> P (Suc (2 * n))"
   12.26 +    by (auto simp: eventually_sequentially)
   12.27 +  show ?thesis
   12.28 +    unfolding eventually_sequentially
   12.29 +  proof (intro exI allI impI)
   12.30 +    fix n assume "max (2 * n_e) (2 * n_o + 1) \<le> n" then show "P n"
   12.31 +      by (cases "even n") (auto elim!: evenE oddE )
   12.32 +  qed
   12.33 +qed
   12.34 +
   12.35 +lemma limseq_even_odd: 
   12.36 +  assumes "(\<lambda>n. f (2 * n)) \<longlonglongrightarrow> (l :: 'a :: topological_space)"
   12.37 +      and "(\<lambda>n. f (2 * n + 1)) \<longlonglongrightarrow> l"
   12.38 +  shows "f \<longlonglongrightarrow> l"
   12.39 +  using assms by (auto simp: filterlim_iff intro: sequentially_even_odd)
   12.40 +
   12.41 +subsection \<open>Application of the FTC: integrating $e^ix$\<close>
   12.42 +
   12.43 +abbreviation iexp :: "real \<Rightarrow> complex" where
   12.44 +  "iexp \<equiv> (\<lambda>x. exp (\<i> * complex_of_real x))"
   12.45 +
   12.46 +lemma isCont_iexp [simp]: "isCont iexp x"
   12.47 +  by (intro continuous_intros)
   12.48 +
   12.49 +lemma has_vector_derivative_iexp[derivative_intros]:
   12.50 +  "(iexp has_vector_derivative \<i> * iexp x) (at x within s)"
   12.51 +  by (auto intro!: derivative_eq_intros simp: Re_exp Im_exp has_vector_derivative_complex_iff)
   12.52 +
   12.53 +lemma interval_integral_iexp:
   12.54 +  fixes a b :: real
   12.55 +  shows "(CLBINT x=a..b. iexp x) = ii * iexp a - ii * iexp b"
   12.56 +  by (subst interval_integral_FTC_finite [where F = "\<lambda>x. -ii * iexp x"])
   12.57 +     (auto intro!: derivative_eq_intros continuous_intros)
   12.58 +
   12.59 +subsection \<open>The Characteristic Function of a Real Measure.\<close>
   12.60 +
   12.61 +definition
   12.62 +  char :: "real measure \<Rightarrow> real \<Rightarrow> complex"
   12.63 +where
   12.64 +  "char M t = CLINT x|M. iexp (t * x)"
   12.65 +
   12.66 +lemma (in real_distribution) char_zero: "char M 0 = 1"
   12.67 +  unfolding char_def by (simp del: space_eq_univ add: prob_space)
   12.68 +
   12.69 +lemma (in prob_space) integrable_iexp: 
   12.70 +  assumes f: "f \<in> borel_measurable M" "\<And>x. Im (f x) = 0"
   12.71 +  shows "integrable M (\<lambda>x. exp (ii * (f x)))"
   12.72 +proof (intro integrable_const_bound [of _ 1])
   12.73 +  from f have "\<And>x. of_real (Re (f x)) = f x"
   12.74 +    by (simp add: complex_eq_iff)
   12.75 +  then show "AE x in M. cmod (exp (\<i> * f x)) \<le> 1"
   12.76 +    using norm_exp_ii_times[of "Re (f x)" for x] by simp
   12.77 +qed (insert f, simp)
   12.78 +
   12.79 +lemma (in real_distribution) cmod_char_le_1: "norm (char M t) \<le> 1"
   12.80 +proof -
   12.81 +  have "norm (char M t) \<le> (\<integral>x. norm (iexp (t * x)) \<partial>M)"
   12.82 +    unfolding char_def by (intro integral_norm_bound integrable_iexp) auto
   12.83 +  also have "\<dots> \<le> 1"
   12.84 +    by (simp del: of_real_mult)
   12.85 +  finally show ?thesis .
   12.86 +qed
   12.87 +
   12.88 +lemma (in real_distribution) isCont_char: "isCont (char M) t"
   12.89 +  unfolding continuous_at_sequentially
   12.90 +proof safe
   12.91 +  fix X assume X: "X \<longlonglongrightarrow> t"
   12.92 +  show "(char M \<circ> X) \<longlonglongrightarrow> char M t"
   12.93 +    unfolding comp_def char_def
   12.94 +    by (rule integral_dominated_convergence[where w="\<lambda>_. 1"]) (auto intro!: tendsto_intros X)
   12.95 +qed
   12.96 +
   12.97 +lemma (in real_distribution) char_measurable [measurable]: "char M \<in> borel_measurable borel"
   12.98 +  by (auto intro!: borel_measurable_continuous_on1 continuous_at_imp_continuous_on isCont_char)
   12.99 +
  12.100 +subsection \<open>Independence\<close>
  12.101 +
  12.102 +(* the automation can probably be improved *)  
  12.103 +lemma (in prob_space) char_distr_sum:
  12.104 +  fixes X1 X2 :: "'a \<Rightarrow> real" and t :: real
  12.105 +  assumes "indep_var borel X1 borel X2"
  12.106 +  shows "char (distr M borel (\<lambda>\<omega>. X1 \<omega> + X2 \<omega>)) t =
  12.107 +    char (distr M borel X1) t * char (distr M borel X2) t"
  12.108 +proof -
  12.109 +  from assms have [measurable]: "random_variable borel X1" by (elim indep_var_rv1)
  12.110 +  from assms have [measurable]: "random_variable borel X2" by (elim indep_var_rv2)
  12.111 +
  12.112 +  have "char (distr M borel (\<lambda>\<omega>. X1 \<omega> + X2 \<omega>)) t = (CLINT x|M. iexp (t * (X1 x + X2 x)))"
  12.113 +    by (simp add: char_def integral_distr)
  12.114 +  also have "\<dots> = (CLINT x|M. iexp (t * (X1 x)) * iexp (t * (X2 x))) "
  12.115 +    by (simp add: field_simps exp_add)
  12.116 +  also have "\<dots> = (CLINT x|M. iexp (t * (X1 x))) * (CLINT x|M. iexp (t * (X2 x)))"
  12.117 +    by (intro indep_var_lebesgue_integral indep_var_compose[unfolded comp_def, OF assms])
  12.118 +       (auto intro!: integrable_iexp)
  12.119 +  also have "\<dots> = char (distr M borel X1) t * char (distr M borel X2) t"
  12.120 +    by (simp add: char_def integral_distr)
  12.121 +  finally show ?thesis .
  12.122 +qed
  12.123 +
  12.124 +lemma (in prob_space) char_distr_setsum:
  12.125 +  "indep_vars (\<lambda>i. borel) X A \<Longrightarrow>
  12.126 +    char (distr M borel (\<lambda>\<omega>. \<Sum>i\<in>A. X i \<omega>)) t = (\<Prod>i\<in>A. char (distr M borel (X i)) t)"
  12.127 +proof (induct A rule: infinite_finite_induct)
  12.128 +  case (insert x F) with indep_vars_subset[of "\<lambda>_. borel" X "insert x F" F] show ?case
  12.129 +    by (auto simp add: char_distr_sum indep_vars_setsum)
  12.130 +qed (simp_all add: char_def integral_distr prob_space del: distr_const)
  12.131 +
  12.132 +subsection \<open>Approximations to $e^{ix}$\<close>
  12.133 +
  12.134 +text \<open>Proofs from Billingsley, page 343.\<close>
  12.135 +
  12.136 +lemma CLBINT_I0c_power_mirror_iexp:
  12.137 +  fixes x :: real and n :: nat
  12.138 +  defines "f s m \<equiv> complex_of_real ((x - s) ^ m)"
  12.139 +  shows "(CLBINT s=0..x. f s n * iexp s) =
  12.140 +    x^Suc n / Suc n + (ii / Suc n) * (CLBINT s=0..x. f s (Suc n) * iexp s)"
  12.141 +proof -
  12.142 +  have 1:
  12.143 +    "((\<lambda>s. complex_of_real(-((x - s) ^ (Suc n) / (Suc n))) * iexp s)
  12.144 +      has_vector_derivative complex_of_real((x - s)^n) * iexp s + (ii * iexp s) * complex_of_real(-((x - s) ^ (Suc n) / (Suc n))))
  12.145 +      (at s within A)" for s A
  12.146 +    by (intro derivative_eq_intros) auto
  12.147 +
  12.148 +  let ?F = "\<lambda>s. complex_of_real(-((x - s) ^ (Suc n) / (Suc n))) * iexp s"
  12.149 +  have "x^(Suc n) / (Suc n) = (CLBINT s=0..x. (f s n * iexp s + (ii * iexp s) * -(f s (Suc n) / (Suc n))))" (is "?LHS = ?RHS")
  12.150 +  proof -
  12.151 +    have "?RHS = (CLBINT s=0..x. (f s n * iexp s + (ii * iexp s) * 
  12.152 +      complex_of_real(-((x - s) ^ (Suc n) / (Suc n)))))"
  12.153 +      by (cases "0 \<le> x") (auto intro!: simp: f_def[abs_def])
  12.154 +    also have "... = ?F x - ?F 0"
  12.155 +      unfolding zero_ereal_def using 1
  12.156 +      by (intro interval_integral_FTC_finite)
  12.157 +         (auto simp: f_def add_nonneg_eq_0_iff complex_eq_iff 
  12.158 +               intro!: continuous_at_imp_continuous_on continuous_intros)
  12.159 +    finally show ?thesis
  12.160 +      by auto
  12.161 +  qed
  12.162 +  show ?thesis
  12.163 +    unfolding \<open>?LHS = ?RHS\<close> f_def interval_lebesgue_integral_mult_right [symmetric]
  12.164 +    by (subst interval_lebesgue_integral_add(2) [symmetric])
  12.165 +       (auto intro!: interval_integrable_isCont continuous_intros simp: zero_ereal_def complex_eq_iff)
  12.166 +qed
  12.167 +
  12.168 +lemma iexp_eq1:
  12.169 +  fixes x :: real
  12.170 +  defines "f s m \<equiv> complex_of_real ((x - s) ^ m)"
  12.171 +  shows "iexp x =
  12.172 +    (\<Sum>k \<le> n. (ii * x)^k / (fact k)) + ((ii ^ (Suc n)) / (fact n)) * (CLBINT s=0..x. (f s n) * (iexp s))" (is "?P n")
  12.173 +proof (induction n)
  12.174 +  show "?P 0"
  12.175 +    by (auto simp add: field_simps interval_integral_iexp f_def zero_ereal_def)
  12.176 +next
  12.177 +  fix n assume ih: "?P n"
  12.178 +  have **: "\<And>a b :: real. a = -b \<longleftrightarrow> a + b = 0"
  12.179 +    by linarith
  12.180 +  have *: "of_nat n * of_nat (fact n) \<noteq> - (of_nat (fact n)::complex)"
  12.181 +    unfolding of_nat_mult[symmetric]
  12.182 +    by (simp add: complex_eq_iff ** of_nat_add[symmetric] del: of_nat_mult of_nat_fact of_nat_add)
  12.183 +  show "?P (Suc n)"
  12.184 +    unfolding setsum_atMost_Suc ih f_def CLBINT_I0c_power_mirror_iexp[of _ n]
  12.185 +    by (simp add: divide_simps add_eq_0_iff *) (simp add: field_simps)
  12.186 +qed
  12.187 +
  12.188 +lemma iexp_eq2:
  12.189 +  fixes x :: real
  12.190 +  defines "f s m \<equiv> complex_of_real ((x - s) ^ m)" 
  12.191 +  shows "iexp x = (\<Sum>k\<le>Suc n. (ii*x)^k/fact k) + ii^Suc n/fact n * (CLBINT s=0..x. f s n*(iexp s - 1))"
  12.192 +proof -
  12.193 +  have isCont_f: "isCont (\<lambda>s. f s n) x" for n x
  12.194 +    by (auto simp: f_def)
  12.195 +  let ?F = "\<lambda>s. complex_of_real (-((x - s) ^ (Suc n) / real (Suc n)))"
  12.196 +  have calc1: "(CLBINT s=0..x. f s n * (iexp s - 1)) =
  12.197 +    (CLBINT s=0..x. f s n * iexp s) - (CLBINT s=0..x. f s n)"
  12.198 +    unfolding zero_ereal_def
  12.199 +    by (subst interval_lebesgue_integral_diff(2) [symmetric])
  12.200 +       (simp_all add: interval_integrable_isCont isCont_f field_simps)
  12.201 +
  12.202 +  have calc2: "(CLBINT s=0..x. f s n) = x^Suc n / Suc n"
  12.203 +    unfolding zero_ereal_def
  12.204 +  proof (subst interval_integral_FTC_finite [where a = 0 and b = x and f = "\<lambda>s. f s n" and F = ?F])
  12.205 +    show "(?F has_vector_derivative f y n) (at y within {min 0 x..max 0 x})" for y
  12.206 +      unfolding f_def
  12.207 +      by (intro has_vector_derivative_of_real)
  12.208 +         (auto intro!: derivative_eq_intros simp del: power_Suc simp add: add_nonneg_eq_0_iff)
  12.209 +  qed (auto intro: continuous_at_imp_continuous_on isCont_f)
  12.210 +
  12.211 +  have calc3: "ii ^ (Suc (Suc n)) / (fact (Suc n)) = (ii ^ (Suc n) / (fact n)) * (ii / (Suc n))"
  12.212 +    by (simp add: field_simps)
  12.213 +
  12.214 +  show ?thesis
  12.215 +    unfolding iexp_eq1 [where n = "Suc n" and x=x] calc1 calc2 calc3 unfolding f_def
  12.216 +    by (subst CLBINT_I0c_power_mirror_iexp [where n = n]) auto
  12.217 +qed
  12.218 +
  12.219 +lemma abs_LBINT_I0c_abs_power_diff:
  12.220 +  "\<bar>LBINT s=0..x. \<bar>(x - s)^n\<bar>\<bar> = \<bar>x ^ (Suc n) / (Suc n)\<bar>"
  12.221 +proof -
  12.222 + have "\<bar>LBINT s=0..x. \<bar>(x - s)^n\<bar>\<bar> = \<bar>LBINT s=0..x. (x - s)^n\<bar>"
  12.223 +  proof cases
  12.224 +    assume "0 \<le> x \<or> even n"
  12.225 +    then have "(LBINT s=0..x. \<bar>(x - s)^n\<bar>) = LBINT s=0..x. (x - s)^n"
  12.226 +      by (auto simp add: zero_ereal_def power_even_abs power_abs min_absorb1 max_absorb2
  12.227 +               intro!: interval_integral_cong)
  12.228 +    then show ?thesis by simp
  12.229 +  next
  12.230 +    assume "\<not> (0 \<le> x \<or> even n)" 
  12.231 +    then have "(LBINT s=0..x. \<bar>(x - s)^n\<bar>) = LBINT s=0..x. -((x - s)^n)"
  12.232 +      by (auto simp add: zero_ereal_def power_abs min_absorb1 max_absorb2
  12.233 +                         ereal_min[symmetric] ereal_max[symmetric] power_minus_odd[symmetric]
  12.234 +               simp del: ereal_min ereal_max intro!: interval_integral_cong)
  12.235 +    also have "\<dots> = - (LBINT s=0..x. (x - s)^n)"
  12.236 +      by (subst interval_lebesgue_integral_uminus, rule refl)
  12.237 +    finally show ?thesis by simp
  12.238 +  qed
  12.239 +  also have "LBINT s=0..x. (x - s)^n = x^Suc n / Suc n"
  12.240 +  proof -
  12.241 +    let ?F = "\<lambda>t. - ((x - t)^(Suc n) / Suc n)"
  12.242 +    have "LBINT s=0..x. (x - s)^n = ?F x - ?F 0"
  12.243 +      unfolding zero_ereal_def
  12.244 +      by (intro interval_integral_FTC_finite continuous_at_imp_continuous_on
  12.245 +                has_field_derivative_iff_has_vector_derivative[THEN iffD1])
  12.246 +         (auto simp del: power_Suc intro!: derivative_eq_intros simp add: add_nonneg_eq_0_iff)
  12.247 +    also have "\<dots> = x ^ (Suc n) / (Suc n)" by simp
  12.248 +    finally show ?thesis .
  12.249 +  qed
  12.250 +  finally show ?thesis .
  12.251 +qed
  12.252 +
  12.253 +lemma iexp_approx1: "cmod (iexp x - (\<Sum>k \<le> n. (ii * x)^k / fact k)) \<le> \<bar>x\<bar>^(Suc n) / fact (Suc n)"
  12.254 +proof -
  12.255 +  have "iexp x - (\<Sum>k \<le> n. (ii * x)^k / fact k) =
  12.256 +      ((ii ^ (Suc n)) / (fact n)) * (CLBINT s=0..x. (x - s)^n * (iexp s))" (is "?t1 = ?t2")
  12.257 +    by (subst iexp_eq1 [of _ n], simp add: field_simps)
  12.258 +  then have "cmod (?t1) = cmod (?t2)"
  12.259 +    by simp
  12.260 +  also have "\<dots> =  (1 / of_nat (fact n)) * cmod (CLBINT s=0..x. (x - s)^n * (iexp s))"
  12.261 +    by (simp add: norm_mult norm_divide norm_power)
  12.262 +  also have "\<dots> \<le> (1 / of_nat (fact n)) * \<bar>LBINT s=0..x. cmod ((x - s)^n * (iexp s))\<bar>"
  12.263 +    by (intro mult_left_mono interval_integral_norm2)
  12.264 +       (auto simp: zero_ereal_def intro: interval_integrable_isCont)
  12.265 +  also have "\<dots> \<le> (1 / of_nat (fact n)) * \<bar>LBINT s=0..x. \<bar>(x - s)^n\<bar>\<bar>"
  12.266 +    by (simp add: norm_mult del: of_real_diff of_real_power)
  12.267 +  also have "\<dots> \<le> (1 / of_nat (fact n)) * \<bar>x ^ (Suc n) / (Suc n)\<bar>"
  12.268 +    by (simp add: abs_LBINT_I0c_abs_power_diff)
  12.269 +  also have "1 / real_of_nat (fact n::nat) * \<bar>x ^ Suc n / real (Suc n)\<bar> =
  12.270 +      \<bar>x\<bar> ^ Suc n / fact (Suc n)"
  12.271 +    by (simp add: abs_mult power_abs)
  12.272 +  finally show ?thesis .
  12.273 +qed
  12.274 +
  12.275 +lemma iexp_approx2: "cmod (iexp x - (\<Sum>k \<le> n. (ii * x)^k / fact k)) \<le> 2 * \<bar>x\<bar>^n / fact n"
  12.276 +proof (induction n) -- \<open>really cases\<close>
  12.277 +  case (Suc n)
  12.278 +  have *: "\<And>a b. interval_lebesgue_integrable lborel a b f \<Longrightarrow> interval_lebesgue_integrable lborel a b g \<Longrightarrow>
  12.279 +      \<bar>LBINT s=a..b. f s\<bar> \<le> \<bar>LBINT s=a..b. g s\<bar>"
  12.280 +    if f: "\<And>s. 0 \<le> f s" and g: "\<And>s. f s \<le> g s" for f g :: "_ \<Rightarrow> real"
  12.281 +    using order_trans[OF f g] f g unfolding interval_lebesgue_integral_def interval_lebesgue_integrable_def
  12.282 +    by (auto simp: integral_nonneg_AE[OF AE_I2] intro!: integral_mono mult_mono)
  12.283 +
  12.284 +  have "iexp x - (\<Sum>k \<le> Suc n. (ii * x)^k / fact k) =
  12.285 +      ((ii ^ (Suc n)) / (fact n)) * (CLBINT s=0..x. (x - s)^n * (iexp s - 1))" (is "?t1 = ?t2")
  12.286 +    unfolding iexp_eq2 [of x n] by (simp add: field_simps)
  12.287 +  then have "cmod (?t1) = cmod (?t2)"
  12.288 +    by simp
  12.289 +  also have "\<dots> =  (1 / (fact n)) * cmod (CLBINT s=0..x. (x - s)^n * (iexp s - 1))"
  12.290 +    by (simp add: norm_mult norm_divide norm_power)
  12.291 +  also have "\<dots> \<le> (1 / (fact n)) * \<bar>LBINT s=0..x. cmod ((x - s)^n * (iexp s - 1))\<bar>"
  12.292 +    by (intro mult_left_mono interval_integral_norm2)
  12.293 +       (auto intro!: interval_integrable_isCont simp: zero_ereal_def)
  12.294 +  also have "\<dots> = (1 / (fact n)) * \<bar>LBINT s=0..x. abs ((x - s)^n) * cmod((iexp s - 1))\<bar>"
  12.295 +    by (simp add: norm_mult del: of_real_diff of_real_power)
  12.296 +  also have "\<dots> \<le> (1 / (fact n)) * \<bar>LBINT s=0..x. abs ((x - s)^n) * 2\<bar>"
  12.297 +    by (intro mult_left_mono * order_trans [OF norm_triangle_ineq4])
  12.298 +       (auto simp: mult_ac zero_ereal_def intro!: interval_integrable_isCont)
  12.299 +  also have "\<dots> = (2 / (fact n)) * \<bar>x ^ (Suc n) / (Suc n)\<bar>"
  12.300 +   by (simp add: abs_LBINT_I0c_abs_power_diff abs_mult)
  12.301 +  also have "2 / fact n * \<bar>x ^ Suc n / real (Suc n)\<bar> = 2 * \<bar>x\<bar> ^ Suc n / (fact (Suc n))"
  12.302 +    by (simp add: abs_mult power_abs)
  12.303 +  finally show ?case .
  12.304 +qed (insert norm_triangle_ineq4[of "iexp x" 1], simp)
  12.305 +
  12.306 +lemma (in real_distribution) char_approx1:
  12.307 +  assumes integrable_moments: "\<And>k. k \<le> n \<Longrightarrow> integrable M (\<lambda>x. x^k)"
  12.308 +  shows "cmod (char M t - (\<Sum>k \<le> n. ((ii * t)^k / fact k) * expectation (\<lambda>x. x^k))) \<le>
  12.309 +    (2*\<bar>t\<bar>^n / fact n) * expectation (\<lambda>x. \<bar>x\<bar>^n)" (is "cmod (char M t - ?t1) \<le> _")
  12.310 +proof -
  12.311 +  have integ_iexp: "integrable M (\<lambda>x. iexp (t * x))"
  12.312 +    by (intro integrable_const_bound) auto
  12.313 +  
  12.314 +  def c \<equiv> "\<lambda>k x. (ii * t)^k / fact k * complex_of_real (x^k)"
  12.315 +  have integ_c: "\<And>k. k \<le> n \<Longrightarrow> integrable M (\<lambda>x. c k x)"
  12.316 +    unfolding c_def by (intro integrable_mult_right integrable_of_real integrable_moments)
  12.317 +  
  12.318 +  have "k \<le> n \<Longrightarrow> expectation (c k) = (\<i>*t) ^ k * (expectation (\<lambda>x. x ^ k)) / fact k" for k
  12.319 +    unfolding c_def integral_mult_right_zero integral_complex_of_real by simp
  12.320 +  then have "norm (char M t - ?t1) = norm (char M t - (CLINT x | M. (\<Sum>k \<le> n. c k x)))"
  12.321 +    by (simp add: integ_c)
  12.322 +  also have "\<dots> = norm ((CLINT x | M. iexp (t * x) - (\<Sum>k \<le> n. c k x)))"
  12.323 +    unfolding char_def by (subst integral_diff[OF integ_iexp]) (auto intro!: integ_c)
  12.324 +  also have "\<dots> \<le> expectation (\<lambda>x. cmod (iexp (t * x) - (\<Sum>k \<le> n. c k x)))"
  12.325 +    by (intro integral_norm_bound integrable_diff integ_iexp integrable_setsum integ_c) simp
  12.326 +  also have "\<dots> \<le> expectation (\<lambda>x. 2 * \<bar>t\<bar> ^ n / fact n * \<bar>x\<bar> ^ n)"
  12.327 +  proof (rule integral_mono)
  12.328 +    show "integrable M (\<lambda>x. cmod (iexp (t * x) - (\<Sum>k\<le>n. c k x)))"
  12.329 +      by (intro integrable_norm integrable_diff integ_iexp integrable_setsum integ_c) simp
  12.330 +    show "integrable M (\<lambda>x. 2 * \<bar>t\<bar> ^ n / fact n * \<bar>x\<bar> ^ n)"
  12.331 +      unfolding power_abs[symmetric]
  12.332 +      by (intro integrable_mult_right integrable_abs integrable_moments) simp
  12.333 +    show "cmod (iexp (t * x) - (\<Sum>k\<le>n. c k x)) \<le> 2 * \<bar>t\<bar> ^ n / fact n * \<bar>x\<bar> ^ n" for x
  12.334 +      using iexp_approx2[of "t * x" n] by (auto simp add: abs_mult field_simps c_def)
  12.335 +  qed
  12.336 +  finally show ?thesis
  12.337 +    unfolding integral_mult_right_zero .
  12.338 +qed
  12.339 +
  12.340 +lemma (in real_distribution) char_approx2:
  12.341 +  assumes integrable_moments: "\<And>k. k \<le> n \<Longrightarrow> integrable M (\<lambda>x. x ^ k)"
  12.342 +  shows "cmod (char M t - (\<Sum>k \<le> n. ((ii * t)^k / fact k) * expectation (\<lambda>x. x^k))) \<le>
  12.343 +    (\<bar>t\<bar>^n / fact (Suc n)) * expectation (\<lambda>x. min (2 * \<bar>x\<bar>^n * Suc n) (\<bar>t\<bar> * \<bar>x\<bar>^Suc n))"
  12.344 +    (is "cmod (char M t - ?t1) \<le> _")
  12.345 +proof -
  12.346 +  have integ_iexp: "integrable M (\<lambda>x. iexp (t * x))"
  12.347 +    by (intro integrable_const_bound) auto
  12.348 +  
  12.349 +  def c \<equiv> "\<lambda>k x. (ii * t)^k / fact k * complex_of_real (x^k)"
  12.350 +  have integ_c: "\<And>k. k \<le> n \<Longrightarrow> integrable M (\<lambda>x. c k x)"
  12.351 +    unfolding c_def by (intro integrable_mult_right integrable_of_real integrable_moments)
  12.352 +
  12.353 +  have *: "min (2 * \<bar>t * x\<bar>^n / fact n) (\<bar>t * x\<bar>^Suc n / fact (Suc n)) =
  12.354 +      \<bar>t\<bar>^n / fact (Suc n) * min (2 * \<bar>x\<bar>^n * real (Suc n)) (\<bar>t\<bar> * \<bar>x\<bar>^(Suc n))" for x
  12.355 +    apply (subst mult_min_right)
  12.356 +    apply simp
  12.357 +    apply (rule arg_cong2[where f=min])
  12.358 +    apply (simp_all add: field_simps abs_mult del: fact_Suc)
  12.359 +    apply (simp_all add: field_simps)
  12.360 +    done
  12.361 +  
  12.362 +  have "k \<le> n \<Longrightarrow> expectation (c k) = (\<i>*t) ^ k * (expectation (\<lambda>x. x ^ k)) / fact k" for k
  12.363 +    unfolding c_def integral_mult_right_zero integral_complex_of_real by simp
  12.364 +  then have "norm (char M t - ?t1) = norm (char M t - (CLINT x | M. (\<Sum>k \<le> n. c k x)))"
  12.365 +    by (simp add: integ_c)
  12.366 +  also have "\<dots> = norm ((CLINT x | M. iexp (t * x) - (\<Sum>k \<le> n. c k x)))"
  12.367 +    unfolding char_def by (subst integral_diff[OF integ_iexp]) (auto intro!: integ_c)
  12.368 +  also have "\<dots> \<le> expectation (\<lambda>x. cmod (iexp (t * x) - (\<Sum>k \<le> n. c k x)))"
  12.369 +    by (intro integral_norm_bound integrable_diff integ_iexp integrable_setsum integ_c) simp
  12.370 +  also have "\<dots> \<le> expectation (\<lambda>x. min (2 * \<bar>t * x\<bar>^n / fact n) (\<bar>t * x\<bar>^(Suc n) / fact (Suc n)))"
  12.371 +    (is "_ \<le> expectation ?f")
  12.372 +  proof (rule integral_mono)
  12.373 +    show "integrable M (\<lambda>x. cmod (iexp (t * x) - (\<Sum>k\<le>n. c k x)))"
  12.374 +      by (intro integrable_norm integrable_diff integ_iexp integrable_setsum integ_c) simp
  12.375 +    show "integrable M ?f"
  12.376 +      by (rule integrable_bound[where f="\<lambda>x. 2 * \<bar>t * x\<bar> ^ n / fact n"])
  12.377 +         (auto simp: integrable_moments power_abs[symmetric] power_mult_distrib)
  12.378 +    show "cmod (iexp (t * x) - (\<Sum>k\<le>n. c k x)) \<le> ?f x" for x
  12.379 +      using iexp_approx1[of "t * x" n] iexp_approx2[of "t * x" n]
  12.380 +      by (auto simp add: abs_mult field_simps c_def intro!: min.boundedI)
  12.381 +  qed
  12.382 +  also have "\<dots> = (\<bar>t\<bar>^n / fact (Suc n)) * expectation (\<lambda>x. min (2 * \<bar>x\<bar>^n * Suc n) (\<bar>t\<bar> * \<bar>x\<bar>^Suc n))"
  12.383 +    unfolding *
  12.384 +  proof (rule integral_mult_right)
  12.385 +    show "integrable M (\<lambda>x. min (2 * \<bar>x\<bar> ^ n * real (Suc n)) (\<bar>t\<bar> * \<bar>x\<bar> ^ Suc n))"
  12.386 +      by (rule integrable_bound[where f="\<lambda>x. 2 * \<bar>x\<bar> ^ n * real (Suc n)"])
  12.387 +         (auto simp: integrable_moments power_abs[symmetric] power_mult_distrib)
  12.388 +  qed
  12.389 +  finally show ?thesis
  12.390 +    unfolding integral_mult_right_zero .
  12.391 +qed
  12.392 +
  12.393 +lemma (in real_distribution) char_approx3:
  12.394 +  fixes t
  12.395 +  assumes
  12.396 +    integrable_1: "integrable M (\<lambda>x. x)" and
  12.397 +    integral_1: "expectation (\<lambda>x. x) = 0" and
  12.398 +    integrable_2: "integrable M (\<lambda>x. x^2)" and
  12.399 +    integral_2: "variance (\<lambda>x. x) = \<sigma>2"
  12.400 +  shows "cmod (char M t - (1 - t^2 * \<sigma>2 / 2)) \<le>
  12.401 +    (t^2 / 6) * expectation (\<lambda>x. min (6 * x^2) (abs t * (abs x)^3) )"
  12.402 +proof -
  12.403 +  note real_distribution.char_approx2 [of M 2 t, simplified]
  12.404 +  have [simp]: "prob UNIV = 1" by (metis prob_space space_eq_univ)
  12.405 +  from integral_2 have [simp]: "expectation (\<lambda>x. x * x) = \<sigma>2" 
  12.406 +    by (simp add: integral_1 numeral_eq_Suc)
  12.407 +  have 1: "k \<le> 2 \<Longrightarrow> integrable M (\<lambda>x. x^k)" for k 
  12.408 +    using assms by (auto simp: eval_nat_numeral le_Suc_eq)
  12.409 +  note char_approx1
  12.410 +  note 2 = char_approx1 [of 2 t, OF 1, simplified]
  12.411 +  have "cmod (char M t - (\<Sum>k\<le>2. (\<i> * t) ^ k * (expectation (\<lambda>x. x ^ k)) / (fact k))) \<le>
  12.412 +      t\<^sup>2 * expectation (\<lambda>x. min (6 * x\<^sup>2) (\<bar>t\<bar> * \<bar>x\<bar> ^ 3)) / fact (3::nat)"
  12.413 +    using char_approx2 [of 2 t, OF 1] by simp
  12.414 +  also have "(\<Sum>k\<le>2. (\<i> * t) ^ k * expectation (\<lambda>x. x ^ k) / (fact k)) = 1 - t^2 * \<sigma>2 / 2"
  12.415 +    by (simp add: complex_eq_iff numeral_eq_Suc integral_1 Re_divide Im_divide)
  12.416 +  also have "fact 3 = 6" by (simp add: eval_nat_numeral)
  12.417 +  also have "t\<^sup>2 * expectation (\<lambda>x. min (6 * x\<^sup>2) (\<bar>t\<bar> * \<bar>x\<bar> ^ 3)) / 6 =
  12.418 +     t\<^sup>2 / 6 * expectation (\<lambda>x. min (6 * x\<^sup>2) (\<bar>t\<bar> * \<bar>x\<bar> ^ 3))" by (simp add: field_simps)
  12.419 +  finally show ?thesis .
  12.420 +qed
  12.421 +
  12.422 +text \<open>
  12.423 +  This is a more familiar textbook formulation in terms of random variables, but 
  12.424 +  we will use the previous version for the CLT.
  12.425 +\<close>
  12.426 +
  12.427 +lemma (in prob_space) char_approx3':
  12.428 +  fixes \<mu> :: "real measure" and X
  12.429 +  assumes rv_X [simp]: "random_variable borel X"
  12.430 +    and [simp]: "integrable M X" "integrable M (\<lambda>x. (X x)^2)" "expectation X = 0"
  12.431 +    and var_X: "variance X = \<sigma>2"
  12.432 +    and \<mu>_def: "\<mu> = distr M borel X"
  12.433 +  shows "cmod (char \<mu> t - (1 - t^2 * \<sigma>2 / 2)) \<le>
  12.434 +    (t^2 / 6) * expectation (\<lambda>x. min (6 * (X x)^2) (\<bar>t\<bar> * \<bar>X x\<bar>^3))"
  12.435 +  using var_X unfolding \<mu>_def
  12.436 +  apply (subst integral_distr [symmetric, OF rv_X], simp)
  12.437 +  apply (intro real_distribution.char_approx3)
  12.438 +  apply (auto simp add: integrable_distr_eq integral_distr)
  12.439 +  done
  12.440 +
  12.441 +text \<open>
  12.442 +  this is the formulation in the book -- in terms of a random variable *with* the distribution,
  12.443 +  rather the distribution itself. I don't know which is more useful, though in principal we can
  12.444 +  go back and forth between them.
  12.445 +\<close>
  12.446 +
  12.447 +lemma (in prob_space) char_approx1':
  12.448 +  fixes \<mu> :: "real measure" and X
  12.449 +  assumes integrable_moments : "\<And>k. k \<le> n \<Longrightarrow> integrable M (\<lambda>x. X x ^ k)"
  12.450 +    and rv_X[measurable]: "random_variable borel X"
  12.451 +    and \<mu>_distr : "distr M borel X = \<mu>"
  12.452 +  shows "cmod (char \<mu> t - (\<Sum>k \<le> n. ((ii * t)^k / fact k) * expectation (\<lambda>x. (X x)^k))) \<le>
  12.453 +    (2 * \<bar>t\<bar>^n / fact n) * expectation (\<lambda>x. \<bar>X x\<bar>^n)"
  12.454 +  unfolding \<mu>_distr[symmetric]
  12.455 +  apply (subst (1 2) integral_distr [symmetric, OF rv_X], simp, simp)
  12.456 +  apply (intro real_distribution.char_approx1[of "distr M borel X" n t] real_distribution_distr rv_X)
  12.457 +  apply (auto simp: integrable_distr_eq integrable_moments)
  12.458 +  done
  12.459 +
  12.460 +subsection \<open>Calculation of the Characteristic Function of the Standard Distribution\<close>
  12.461 +
  12.462 +abbreviation
  12.463 +  "std_normal_distribution \<equiv> density lborel std_normal_density"
  12.464 +
  12.465 +(* TODO: should this be an instance statement? *)
  12.466 +lemma real_dist_normal_dist: "real_distribution std_normal_distribution"
  12.467 +  using prob_space_normal_density by (auto simp: real_distribution_def real_distribution_axioms_def)
  12.468 +
  12.469 +lemma std_normal_distribution_even_moments:
  12.470 +  fixes k :: nat
  12.471 +  shows "(LINT x|std_normal_distribution. x^(2 * k)) = fact (2 * k) / (2^k * fact k)"
  12.472 +    and "integrable std_normal_distribution (\<lambda>x. x^(2 * k))"
  12.473 +  using integral_std_normal_moment_even[of k]
  12.474 +  by (subst integral_density)
  12.475 +     (auto simp: normal_density_nonneg integrable_density
  12.476 +           intro: integrable.intros std_normal_moment_even)
  12.477 +
  12.478 +lemma integrable_std_normal_distribution_moment: "integrable std_normal_distribution (\<lambda>x. x^k)"
  12.479 +  by (auto simp: normal_density_nonneg integrable_std_normal_moment integrable_density)
  12.480 +
  12.481 +lemma integral_std_normal_distribution_moment_odd:
  12.482 +  "odd k \<Longrightarrow> integral\<^sup>L std_normal_distribution (\<lambda>x. x^k) = 0"
  12.483 +  using integral_std_normal_moment_odd[of "(k - 1) div 2"]
  12.484 +  by (auto simp: integral_density normal_density_nonneg elim: oddE)
  12.485 +
  12.486 +lemma std_normal_distribution_even_moments_abs:
  12.487 +  fixes k :: nat
  12.488 +  shows "(LINT x|std_normal_distribution. \<bar>x\<bar>^(2 * k)) = fact (2 * k) / (2^k * fact k)"
  12.489 +  using integral_std_normal_moment_even[of k]
  12.490 +  by (subst integral_density) (auto simp: normal_density_nonneg power_even_abs)
  12.491 +
  12.492 +lemma std_normal_distribution_odd_moments_abs:
  12.493 +  fixes k :: nat
  12.494 +  shows "(LINT x|std_normal_distribution. \<bar>x\<bar>^(2 * k + 1)) = sqrt (2 / pi) * 2 ^ k * fact k"
  12.495 +  using integral_std_normal_moment_abs_odd[of k]
  12.496 +  by (subst integral_density) (auto simp: normal_density_nonneg)
  12.497 +
  12.498 +theorem char_std_normal_distribution:
  12.499 +  "char std_normal_distribution = (\<lambda>t. complex_of_real (exp (- (t^2) / 2)))"
  12.500 +proof (intro ext LIMSEQ_unique)
  12.501 +  fix t :: real
  12.502 +  let ?f' = "\<lambda>k. (ii * t)^k / fact k * (LINT x | std_normal_distribution. x^k)"
  12.503 +  let ?f = "\<lambda>n. (\<Sum>k \<le> n. ?f' k)"
  12.504 +  show "?f \<longlonglongrightarrow> exp (-(t^2) / 2)"
  12.505 +  proof (rule limseq_even_odd)
  12.506 +    have "(\<i> * complex_of_real t) ^ (2 * a) / (2 ^ a * fact a) = (- ((complex_of_real t)\<^sup>2 / 2)) ^ a / fact a" for a
  12.507 +      by (subst power_mult) (simp add: field_simps uminus_power_if power_mult)
  12.508 +    then have *: "?f (2 * n) = complex_of_real (\<Sum>k < Suc n. (1 / fact k) * (- (t^2) / 2)^k)" for n :: nat
  12.509 +      unfolding of_real_setsum
  12.510 +      by (intro setsum.reindex_bij_witness_not_neutral[symmetric, where
  12.511 +           i="\<lambda>n. n div 2" and j="\<lambda>n. 2 * n" and T'="{i. i \<le> 2 * n \<and> odd i}" and S'="{}"])
  12.512 +         (auto simp: integral_std_normal_distribution_moment_odd std_normal_distribution_even_moments)
  12.513 +    show "(\<lambda>n. ?f (2 * n)) \<longlonglongrightarrow> exp (-(t^2) / 2)"
  12.514 +      unfolding * using exp_converges[where 'a=real]
  12.515 +      by (intro tendsto_of_real LIMSEQ_Suc) (auto simp: inverse_eq_divide sums_def [symmetric])
  12.516 +    have **: "?f (2 * n + 1) = ?f (2 * n)" for n
  12.517 +    proof -
  12.518 +      have "?f (2 * n + 1) = ?f (2 * n) + ?f' (2 * n + 1)"
  12.519 +        by simp
  12.520 +      also have "?f' (2 * n + 1) = 0"
  12.521 +        by (subst integral_std_normal_distribution_moment_odd) simp_all
  12.522 +      finally show "?f (2 * n + 1) = ?f (2 * n)"
  12.523 +        by simp
  12.524 +    qed
  12.525 +    show "(\<lambda>n. ?f (2 * n + 1)) \<longlonglongrightarrow> exp (-(t^2) / 2)"
  12.526 +      unfolding ** by fact
  12.527 +  qed
  12.528 +
  12.529 +  have **: "(\<lambda>n. x ^ n / fact n) \<longlonglongrightarrow> 0" for x :: real
  12.530 +    using summable_LIMSEQ_zero [OF summable_exp] by (auto simp add: inverse_eq_divide)
  12.531 +
  12.532 +  let ?F = "\<lambda>n. 2 * \<bar>t\<bar> ^ n / fact n * (LINT x|std_normal_distribution. \<bar>x\<bar> ^ n)"
  12.533 +
  12.534 +  show "?f \<longlonglongrightarrow> char std_normal_distribution t"
  12.535 +  proof (rule metric_tendsto_imp_tendsto[OF limseq_even_odd])
  12.536 +    show "(\<lambda>n. ?F (2 * n)) \<longlonglongrightarrow> 0"
  12.537 +    proof (rule Lim_transform_eventually)
  12.538 +      show "\<forall>\<^sub>F n in sequentially. 2 * ((t^2 / 2)^n / fact n) = ?F (2 * n)"
  12.539 +        unfolding std_normal_distribution_even_moments_abs by (simp add: power_mult power_divide)
  12.540 +    qed (intro tendsto_mult_right_zero **)
  12.541 +
  12.542 +    have *: "?F (2 * n + 1) = (2 * \<bar>t\<bar> * sqrt (2 / pi)) * ((2 * t^2)^n * fact n / fact (2 * n + 1))" for n
  12.543 +      unfolding std_normal_distribution_odd_moments_abs
  12.544 +      by (simp add: field_simps power_mult[symmetric] power_even_abs)
  12.545 +    have "norm ((2 * t\<^sup>2) ^ n * fact n / fact (2 * n + 1)) \<le> (2 * t\<^sup>2) ^ n / fact n" for n
  12.546 +      using mult_mono[OF _ square_fact_le_2_fact, of 1 "1 + 2 * real n" n]
  12.547 +      by (auto simp add: divide_simps intro!: mult_left_mono)
  12.548 +    then show "(\<lambda>n. ?F (2 * n + 1)) \<longlonglongrightarrow> 0"
  12.549 +      unfolding * by (intro tendsto_mult_right_zero Lim_null_comparison [OF _ ** [of "2 * t\<^sup>2"]]) auto
  12.550 +
  12.551 +    show "\<forall>\<^sub>F n in sequentially. dist (?f n) (char std_normal_distribution t) \<le> dist (?F n) 0"
  12.552 +      using real_distribution.char_approx1[OF real_dist_normal_dist integrable_std_normal_distribution_moment]
  12.553 +      by (auto simp: dist_norm integral_nonneg_AE norm_minus_commute)
  12.554 +  qed
  12.555 +qed
  12.556 +
  12.557 +end
    13.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    13.2 +++ b/src/HOL/Probability/Distribution_Functions.thy	Wed Jan 06 12:18:53 2016 +0100
    13.3 @@ -0,0 +1,259 @@
    13.4 +(*
    13.5 +  Title    : Distribution_Functions.thy
    13.6 +  Authors  : Jeremy Avigad and Luke Serafin
    13.7 +*)
    13.8 +
    13.9 +section \<open>Distribution Functions\<close>
   13.10 +
   13.11 +text \<open>
   13.12 +Shows that the cumulative distribution function (cdf) of a distribution (a measure on the reals) is 
   13.13 +nondecreasing and right continuous, which tends to 0 and 1 in either direction.
   13.14 +
   13.15 +Conversely, every such function is the cdf of a unique distribution. This direction defines the 
   13.16 +measure in the obvious way on half-open intervals, and then applies the Caratheodory extension 
   13.17 +theorem.
   13.18 +\<close>
   13.19 +
   13.20 +(* TODO: the locales "finite_borel_measure" and "real_distribution" are defined here, but maybe they
   13.21 + should be somewhere else. *)
   13.22 +
   13.23 +theory Distribution_Functions
   13.24 +  imports Probability_Measure "~~/src/HOL/Library/ContNotDenum"
   13.25 +begin
   13.26 +
   13.27 +lemma UN_Ioc_eq_UNIV: "(\<Union>n. { -real n <.. real n}) = UNIV"
   13.28 +  by auto
   13.29 +     (metis le_less_trans minus_minus neg_less_iff_less not_le real_arch_simple
   13.30 +            of_nat_0_le_iff reals_Archimedean2)
   13.31 +
   13.32 +subsection {* Properties of cdf's *}
   13.33 +
   13.34 +definition
   13.35 +  cdf :: "real measure \<Rightarrow> real \<Rightarrow> real"
   13.36 +where
   13.37 +  "cdf M \<equiv> \<lambda>x. measure M {..x}"
   13.38 +
   13.39 +lemma cdf_def2: "cdf M x = measure M {..x}"
   13.40 +  by (simp add: cdf_def)
   13.41 +
   13.42 +locale finite_borel_measure = finite_measure M for M :: "real measure" +
   13.43 +  assumes M_super_borel: "sets borel \<subseteq> sets M"
   13.44 +begin
   13.45 +
   13.46 +lemma sets_M[intro]: "a \<in> sets borel \<Longrightarrow> a \<in> sets M"
   13.47 +  using M_super_borel by auto
   13.48 +
   13.49 +lemma cdf_diff_eq: 
   13.50 +  assumes "x < y"
   13.51 +  shows "cdf M y - cdf M x = measure M {x<..y}"
   13.52 +proof -
   13.53 +  from assms have *: "{..x} \<union> {x<..y} = {..y}" by auto
   13.54 +  have "measure M {..y} = measure M {..x} + measure M {x<..y}"
   13.55 +    by (subst finite_measure_Union [symmetric], auto simp add: *)
   13.56 +  thus ?thesis
   13.57 +    unfolding cdf_def by auto
   13.58 +qed
   13.59 +
   13.60 +lemma cdf_nondecreasing: "x \<le> y \<Longrightarrow> cdf M x \<le> cdf M y"
   13.61 +  unfolding cdf_def by (auto intro!: finite_measure_mono)
   13.62 +
   13.63 +lemma borel_UNIV: "space M = UNIV"
   13.64 + by (metis in_mono sets.sets_into_space space_in_borel top_le M_super_borel)
   13.65 + 
   13.66 +lemma cdf_nonneg: "cdf M x \<ge> 0"
   13.67 +  unfolding cdf_def by (rule measure_nonneg)
   13.68 +
   13.69 +lemma cdf_bounded: "cdf M x \<le> measure M (space M)"
   13.70 +  unfolding cdf_def using assms by (intro bounded_measure)
   13.71 +
   13.72 +lemma cdf_lim_infty:
   13.73 +  "((\<lambda>i. cdf M (real i)) \<longlonglongrightarrow> measure M (space M))"
   13.74 +proof -
   13.75 +  have "(\<lambda>i. cdf M (real i)) \<longlonglongrightarrow> measure M (\<Union> i::nat. {..real i})"
   13.76 +    unfolding cdf_def by (rule finite_Lim_measure_incseq) (auto simp: incseq_def)
   13.77 +  also have "(\<Union> i::nat. {..real i}) = space M"
   13.78 +    by (auto simp: borel_UNIV intro: real_arch_simple)
   13.79 +  finally show ?thesis .
   13.80 +qed
   13.81 +
   13.82 +lemma cdf_lim_at_top: "(cdf M \<longlongrightarrow> measure M (space M)) at_top" 
   13.83 +  by (rule tendsto_at_topI_sequentially_real)
   13.84 +     (simp_all add: mono_def cdf_nondecreasing cdf_lim_infty)
   13.85 +
   13.86 +lemma cdf_lim_neg_infty: "((\<lambda>i. cdf M (- real i)) \<longlonglongrightarrow> 0)" 
   13.87 +proof -
   13.88 +  have "(\<lambda>i. cdf M (- real i)) \<longlonglongrightarrow> measure M (\<Inter> i::nat. {.. - real i })"
   13.89 +    unfolding cdf_def by (rule finite_Lim_measure_decseq) (auto simp: decseq_def)
   13.90 +  also have "(\<Inter> i::nat. {..- real i}) = {}"
   13.91 +    by auto (metis leD le_minus_iff reals_Archimedean2)
   13.92 +  finally show ?thesis
   13.93 +    by simp
   13.94 +qed
   13.95 +
   13.96 +lemma cdf_lim_at_bot: "(cdf M \<longlongrightarrow> 0) at_bot"
   13.97 +proof - 
   13.98 +  have *: "((\<lambda>x :: real. - cdf M (- x)) \<longlongrightarrow> 0) at_top"
   13.99 +    by (intro tendsto_at_topI_sequentially_real monoI)
  13.100 +       (auto simp: cdf_nondecreasing cdf_lim_neg_infty tendsto_minus_cancel_left[symmetric])
  13.101 +  from filterlim_compose [OF *, OF filterlim_uminus_at_top_at_bot]
  13.102 +  show ?thesis
  13.103 +    unfolding tendsto_minus_cancel_left[symmetric] by simp
  13.104 +qed
  13.105 +
  13.106 +lemma cdf_is_right_cont: "continuous (at_right a) (cdf M)"
  13.107 +  unfolding continuous_within
  13.108 +proof (rule tendsto_at_right_sequentially[where b="a + 1"])
  13.109 +  fix f :: "nat \<Rightarrow> real" and x assume f: "decseq f" "f \<longlonglongrightarrow> a"
  13.110 +  then have "(\<lambda>n. cdf M (f n)) \<longlonglongrightarrow> measure M (\<Inter>i. {.. f i})"
  13.111 +    using `decseq f` unfolding cdf_def 
  13.112 +    by (intro finite_Lim_measure_decseq) (auto simp: decseq_def)
  13.113 +  also have "(\<Inter>i. {.. f i}) = {.. a}"
  13.114 +    using decseq_le[OF f] by (auto intro: order_trans LIMSEQ_le_const[OF f(2)])
  13.115 +  finally show "(\<lambda>n. cdf M (f n)) \<longlonglongrightarrow> cdf M a"
  13.116 +    by (simp add: cdf_def)
  13.117 +qed simp
  13.118 +
  13.119 +lemma cdf_at_left: "(cdf M \<longlongrightarrow> measure M {..<a}) (at_left a)"
  13.120 +proof (rule tendsto_at_left_sequentially[of "a - 1"])
  13.121 +  fix f :: "nat \<Rightarrow> real" and x assume f: "incseq f" "f \<longlonglongrightarrow> a" "\<And>x. f x < a" "\<And>x. a - 1 < f x"
  13.122 +  then have "(\<lambda>n. cdf M (f n)) \<longlonglongrightarrow> measure M (\<Union>i. {.. f i})"
  13.123 +    using `incseq f` unfolding cdf_def 
  13.124 +    by (intro finite_Lim_measure_incseq) (auto simp: incseq_def)
  13.125 +  also have "(\<Union>i. {.. f i}) = {..<a}"
  13.126 +    by (auto dest!: order_tendstoD(1)[OF f(2)] eventually_happens'[OF sequentially_bot]
  13.127 +             intro: less_imp_le le_less_trans f(3))
  13.128 +  finally show "(\<lambda>n. cdf M (f n)) \<longlonglongrightarrow> measure M {..<a}"
  13.129 +    by (simp add: cdf_def)
  13.130 +qed auto
  13.131 +
  13.132 +lemma isCont_cdf: "isCont (cdf M) x \<longleftrightarrow> measure M {x} = 0"
  13.133 +proof -
  13.134 +  have "isCont (cdf M) x \<longleftrightarrow> cdf M x = measure M {..<x}"
  13.135 +    by (auto simp: continuous_at_split cdf_is_right_cont continuous_within[where s="{..< _}"]
  13.136 +                   cdf_at_left tendsto_unique[OF _ cdf_at_left])
  13.137 +  also have "cdf M x = measure M {..<x} \<longleftrightarrow> measure M {x} = 0"
  13.138 +    unfolding cdf_def ivl_disj_un(2)[symmetric]
  13.139 +    by (subst finite_measure_Union) auto
  13.140 +  finally show ?thesis .
  13.141 +qed
  13.142 +
  13.143 +lemma countable_atoms: "countable {x. measure M {x} > 0}"
  13.144 +  using countable_support unfolding zero_less_measure_iff .
  13.145 +    
  13.146 +end
  13.147 +
  13.148 +locale real_distribution = prob_space M for M :: "real measure" +
  13.149 +  assumes events_eq_borel [simp, measurable_cong]: "sets M = sets borel" and space_eq_univ [simp]: "space M = UNIV"
  13.150 +begin
  13.151 +
  13.152 +sublocale finite_borel_measure M
  13.153 +  by standard auto
  13.154 +
  13.155 +lemma cdf_bounded_prob: "\<And>x. cdf M x \<le> 1"
  13.156 +  by (subst prob_space [symmetric], rule cdf_bounded)
  13.157 +
  13.158 +lemma cdf_lim_infty_prob: "(\<lambda>i. cdf M (real i)) \<longlonglongrightarrow> 1"
  13.159 +  by (subst prob_space [symmetric], rule cdf_lim_infty)
  13.160 +
  13.161 +lemma cdf_lim_at_top_prob: "(cdf M \<longlongrightarrow> 1) at_top" 
  13.162 +  by (subst prob_space [symmetric], rule cdf_lim_at_top)
  13.163 +
  13.164 +lemma measurable_finite_borel [simp]:
  13.165 +  "f \<in> borel_measurable borel \<Longrightarrow> f \<in> borel_measurable M"
  13.166 +  by (rule borel_measurable_subalgebra[where N=borel]) auto
  13.167 +
  13.168 +end
  13.169 +
  13.170 +lemma (in prob_space) real_distribution_distr [intro, simp]:
  13.171 +  "random_variable borel X \<Longrightarrow> real_distribution (distr M borel X)"
  13.172 +  unfolding real_distribution_def real_distribution_axioms_def by (auto intro!: prob_space_distr)
  13.173 +
  13.174 +subsection {* uniqueness *}
  13.175 +
  13.176 +lemma (in real_distribution) emeasure_Ioc:
  13.177 +  assumes "a \<le> b" shows "emeasure M {a <.. b} = cdf M b - cdf M a"
  13.178 +proof -
  13.179 +  have "{a <.. b} = {..b} - {..a}"
  13.180 +    by auto
  13.181 +  with `a \<le> b` show ?thesis
  13.182 +    by (simp add: emeasure_eq_measure finite_measure_Diff cdf_def)
  13.183 +qed
  13.184 +
  13.185 +lemma cdf_unique:
  13.186 +  fixes M1 M2
  13.187 +  assumes "real_distribution M1" and "real_distribution M2"
  13.188 +  assumes "cdf M1 = cdf M2"
  13.189 +  shows "M1 = M2"
  13.190 +proof (rule measure_eqI_generator_eq[where \<Omega>=UNIV])
  13.191 +  fix X assume "X \<in> range (\<lambda>(a, b). {a<..b::real})"
  13.192 +  then obtain a b where Xeq: "X = {a<..b}" by auto
  13.193 +  then show "emeasure M1 X = emeasure M2 X"
  13.194 +    by (cases "a \<le> b")
  13.195 +       (simp_all add: assms(1,2)[THEN real_distribution.emeasure_Ioc] assms(3))
  13.196 +next
  13.197 +  show "(\<Union>i. {- real (i::nat)<..real i}) = UNIV"
  13.198 +    by (rule UN_Ioc_eq_UNIV)
  13.199 +qed (auto simp: real_distribution.emeasure_Ioc[OF assms(1)]
  13.200 +  assms(1,2)[THEN real_distribution.events_eq_borel] borel_sigma_sets_Ioc
  13.201 +  Int_stable_def)
  13.202 +
  13.203 +lemma real_distribution_interval_measure:
  13.204 +  fixes F :: "real \<Rightarrow> real"
  13.205 +  assumes nondecF : "\<And> x y. x \<le> y \<Longrightarrow> F x \<le> F y" and
  13.206 +    right_cont_F : "\<And>a. continuous (at_right a) F" and 
  13.207 +    lim_F_at_bot : "(F \<longlongrightarrow> 0) at_bot" and
  13.208 +    lim_F_at_top : "(F \<longlongrightarrow> 1) at_top"
  13.209 +  shows "real_distribution (interval_measure F)"
  13.210 +proof -
  13.211 +  let ?F = "interval_measure F"
  13.212 +  interpret prob_space ?F
  13.213 +  proof
  13.214 +    have "ereal (1 - 0) = (SUP i::nat. ereal (F (real i) - F (- real i)))"
  13.215 +      by (intro LIMSEQ_unique[OF _ LIMSEQ_SUP] lim_ereal[THEN iffD2] tendsto_intros
  13.216 +         lim_F_at_bot[THEN filterlim_compose] lim_F_at_top[THEN filterlim_compose]
  13.217 +         lim_F_at_bot[THEN filterlim_compose] filterlim_real_sequentially
  13.218 +         filterlim_uminus_at_top[THEN iffD1])
  13.219 +         (auto simp: incseq_def intro!: diff_mono nondecF)
  13.220 +    also have "\<dots> = (SUP i::nat. emeasure ?F {- real i<..real i})"
  13.221 +      by (subst emeasure_interval_measure_Ioc) (simp_all add: nondecF right_cont_F)
  13.222 +    also have "\<dots> = emeasure ?F (\<Union>i::nat. {- real i<..real i})"
  13.223 +      by (rule SUP_emeasure_incseq) (auto simp: incseq_def)
  13.224 +    also have "(\<Union>i. {- real (i::nat)<..real i}) = space ?F"
  13.225 +      by (simp add: UN_Ioc_eq_UNIV)
  13.226 +    finally show "emeasure ?F (space ?F) = 1"
  13.227 +      by (simp add: one_ereal_def)
  13.228 +  qed
  13.229 +  show ?thesis
  13.230 +    proof qed simp_all
  13.231 +qed
  13.232 +
  13.233 +lemma cdf_interval_measure:
  13.234 +  fixes F :: "real \<Rightarrow> real"
  13.235 +  assumes nondecF : "\<And> x y. x \<le> y \<Longrightarrow> F x \<le> F y" and
  13.236 +    right_cont_F : "\<And>a. continuous (at_right a) F" and 
  13.237 +    lim_F_at_bot : "(F \<longlongrightarrow> 0) at_bot" and
  13.238 +    lim_F_at_top : "(F \<longlongrightarrow> 1) at_top"
  13.239 +  shows "cdf (interval_measure F) = F"
  13.240 +  unfolding cdf_def
  13.241 +proof (intro ext)
  13.242 +  interpret real_distribution "interval_measure F"
  13.243 +    by (rule real_distribution_interval_measure) fact+
  13.244 +  fix x
  13.245 +  have "F x - 0 = measure (interval_measure F) (\<Union>i::nat. {-real i <.. x})"
  13.246 +  proof (intro LIMSEQ_unique[OF _ finite_Lim_measure_incseq])
  13.247 +    have "(\<lambda>i. F x - F (- real i)) \<longlonglongrightarrow> F x - 0"
  13.248 +      by (intro tendsto_intros lim_F_at_bot[THEN filterlim_compose] filterlim_real_sequentially
  13.249 +                filterlim_uminus_at_top[THEN iffD1])
  13.250 +    then show "(\<lambda>i. measure (interval_measure F) {- real i<..x}) \<longlonglongrightarrow> F x - 0"
  13.251 +      apply (rule filterlim_cong[OF refl refl, THEN iffD1, rotated])
  13.252 +      apply (rule eventually_sequentiallyI[where c="nat (ceiling (- x))"])
  13.253 +      apply (simp add: measure_interval_measure_Ioc right_cont_F nondecF)
  13.254 +      done
  13.255 +  qed (auto simp: incseq_def)
  13.256 +  also have "(\<Union>i::nat. {-real i <.. x}) = {..x}"
  13.257 +    by auto (metis minus_minus neg_less_iff_less reals_Archimedean2)
  13.258 +  finally show "measure (interval_measure F) {..x} = F x"
  13.259 +    by simp
  13.260 +qed
  13.261 +
  13.262 +end
    14.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    14.2 +++ b/src/HOL/Probability/Helly_Selection.thy	Wed Jan 06 12:18:53 2016 +0100
    14.3 @@ -0,0 +1,297 @@
    14.4 +(*
    14.5 +  Theory: Helly_Selection.thy
    14.6 +  Authors: Jeremy Avigad, Luke Serafin
    14.7 +*)
    14.8 +
    14.9 +section \<open>Helly's selection theorem\<close>
   14.10 +
   14.11 +text \<open>The set of bounded, monotone, right continuous functions is sequentially compact\<close>
   14.12 +
   14.13 +theory Helly_Selection
   14.14 +  imports "~~/src/HOL/Library/Diagonal_Subsequence" Weak_Convergence
   14.15 +begin
   14.16 +
   14.17 +lemma minus_one_less: "x - 1 < (x::real)"
   14.18 +  by simp
   14.19 +
   14.20 +theorem Helly_selection:
   14.21 +  fixes f :: "nat \<Rightarrow> real \<Rightarrow> real"
   14.22 +  assumes rcont: "\<And>n x. continuous (at_right x) (f n)"
   14.23 +  assumes mono: "\<And>n. mono (f n)"
   14.24 +  assumes bdd: "\<And>n x. \<bar>f n x\<bar> \<le> M"
   14.25 +  shows "\<exists>s. subseq s \<and> (\<exists>F. (\<forall>x. continuous (at_right x) F) \<and> mono F \<and> (\<forall>x. \<bar>F x\<bar> \<le> M) \<and>
   14.26 +    (\<forall>x. continuous (at x) F \<longrightarrow> (\<lambda>n. f (s n) x) \<longlonglongrightarrow> F x))"
   14.27 +proof -
   14.28 +  obtain m :: "real \<Rightarrow> nat" where "bij_betw m \<rat> UNIV"
   14.29 +    using countable_rat Rats_infinite by (erule countableE_infinite)
   14.30 +  then obtain r :: "nat \<Rightarrow> real" where bij: "bij_betw r UNIV \<rat>"
   14.31 +    using bij_betw_inv by blast
   14.32 +
   14.33 +  have dense_r: "\<And>x y. x < y \<Longrightarrow> \<exists>n. x < r n \<and> r n < y"
   14.34 +    by (metis Rats_dense_in_real bij f_the_inv_into_f bij_betw_def)
   14.35 +
   14.36 +  let ?P = "\<lambda>n. \<lambda>s. convergent (\<lambda>k. f (s k) (r n))"
   14.37 +  interpret nat: subseqs ?P
   14.38 +  proof (unfold convergent_def, unfold subseqs_def, auto)
   14.39 +    fix n :: nat and s :: "nat \<Rightarrow> nat" assume s: "subseq s"
   14.40 +    have "bounded {-M..M}"
   14.41 +      using bounded_closed_interval by auto
   14.42 +    moreover have "\<And>k. f (s k) (r n) \<in> {-M..M}" 
   14.43 +      using bdd by (simp add: abs_le_iff minus_le_iff)
   14.44 +    ultimately have "\<exists>l s'. subseq s' \<and> ((\<lambda>k. f (s k) (r n)) \<circ> s') \<longlonglongrightarrow> l"
   14.45 +      using compact_Icc compact_imp_seq_compact seq_compactE by metis
   14.46 +    thus "\<exists>s'. subseq s' \<and> (\<exists>l. (\<lambda>k. f (s (s' k)) (r n)) \<longlonglongrightarrow> l)"
   14.47 +      by (auto simp: comp_def)
   14.48 +  qed
   14.49 +  def d \<equiv> "nat.diagseq"
   14.50 +  have subseq: "subseq d"
   14.51 +    unfolding d_def using nat.subseq_diagseq by auto
   14.52 +  have rat_cnv: "?P n d" for n
   14.53 +  proof -
   14.54 +    have Pn_seqseq: "?P n (nat.seqseq (Suc n))"
   14.55 +      by (rule nat.seqseq_holds)
   14.56 +    have 1: "(\<lambda>k. f ((nat.seqseq (Suc n) \<circ> (\<lambda>k. nat.fold_reduce (Suc n) k
   14.57 +      (Suc n + k))) k) (r n)) = (\<lambda>k. f (nat.seqseq (Suc n) k) (r n)) \<circ>
   14.58 +      (\<lambda>k. nat.fold_reduce (Suc n) k (Suc n + k))"
   14.59 +      by auto
   14.60 +    have 2: "?P n (d \<circ> (op + (Suc n)))"
   14.61 +      unfolding d_def nat.diagseq_seqseq 1
   14.62 +      by (intro convergent_subseq_convergent Pn_seqseq nat.subseq_diagonal_rest)
   14.63 +    then obtain L where 3: "(\<lambda>na. f (d (na + Suc n)) (r n)) \<longlonglongrightarrow> L"
   14.64 +      by (auto simp: add.commute dest: convergentD)
   14.65 +    then have "(\<lambda>k. f (d k) (r n)) \<longlonglongrightarrow> L"
   14.66 +      by (rule LIMSEQ_offset)
   14.67 +    then show ?thesis
   14.68 +      by (auto simp: convergent_def)
   14.69 +  qed
   14.70 +  let ?f = "\<lambda>n. \<lambda>k. f (d k) (r n)"
   14.71 +  have lim_f: "?f n \<longlonglongrightarrow> lim (?f n)" for n
   14.72 +    using rat_cnv convergent_LIMSEQ_iff by auto
   14.73 +  have lim_bdd: "lim (?f n) \<in> {-M..M}" for n
   14.74 +  proof -
   14.75 +    have "closed {-M..M}" using closed_real_atLeastAtMost by auto
   14.76 +    hence "(\<forall>i. ?f n i \<in> {-M..M}) \<and> ?f n \<longlonglongrightarrow> lim (?f n) \<longrightarrow> lim (?f n) \<in> {-M..M}"
   14.77 +      unfolding closed_sequential_limits by (drule_tac x = "\<lambda>k. f (d k) (r n)" in spec) blast
   14.78 +    moreover have "\<forall>i. ?f n i \<in> {-M..M}"
   14.79 +      using bdd by (simp add: abs_le_iff minus_le_iff)
   14.80 +    ultimately show "lim (?f n) \<in> {-M..M}"
   14.81 +      using lim_f by auto
   14.82 +  qed
   14.83 +  then have limset_bdd: "\<And>x. {lim (?f n) |n. x < r n} \<subseteq> {-M..M}"
   14.84 +    by auto
   14.85 +  then have bdd_below: "bdd_below {lim (?f n) |n. x < r n}" for x
   14.86 +    by (metis (mono_tags) bdd_below_Icc bdd_below_mono)
   14.87 +  have r_unbdd: "\<exists>n. x < r n" for x
   14.88 +    using dense_r[OF less_add_one, of x] by auto
   14.89 +  then have nonempty: "{lim (?f n) |n. x < r n} \<noteq> {}" for x
   14.90 +    by auto
   14.91 +
   14.92 +  def F \<equiv> "\<lambda>x. Inf {lim (?f n) |n. x < r n}"
   14.93 +  have F_eq: "ereal (F x) = (INF n:{n. x < r n}. ereal (lim (?f n)))" for x
   14.94 +    unfolding F_def by (subst ereal_Inf'[OF bdd_below nonempty]) (simp add: setcompr_eq_image)
   14.95 +  have mono_F: "mono F"
   14.96 +    using nonempty by (auto intro!: cInf_superset_mono simp: F_def bdd_below mono_def)
   14.97 +  moreover have "\<And>x. continuous (at_right x) F"
   14.98 +    unfolding continuous_within order_tendsto_iff eventually_at_right[OF less_add_one]
   14.99 +  proof safe
  14.100 +    show "F x < u \<Longrightarrow> \<exists>b>x. \<forall>y>x. y < b \<longrightarrow> F y < u" for x u
  14.101 +      unfolding F_def cInf_less_iff[OF nonempty bdd_below] by auto
  14.102 +  next
  14.103 +    show "\<exists>b>x. \<forall>y>x. y < b \<longrightarrow> l < F y" if l: "l < F x" for x l
  14.104 +      using less_le_trans[OF l mono_F[THEN monoD, of x]] by (auto intro: less_add_one)
  14.105 +  qed
  14.106 +  moreover
  14.107 +  { fix x
  14.108 +    have "F x \<in> {-M..M}"
  14.109 +      unfolding F_def using limset_bdd bdd_below r_unbdd by (intro closed_subset_contains_Inf) auto
  14.110 +    then have "\<bar>F x\<bar> \<le> M" by auto }
  14.111 +  moreover have "(\<lambda>n. f (d n) x) \<longlonglongrightarrow> F x" if cts: "continuous (at x) F" for x
  14.112 +  proof (rule limsup_le_liminf_real)
  14.113 +    show "limsup (\<lambda>n. f (d n) x) \<le> F x"
  14.114 +    proof (rule tendsto_le_const)
  14.115 +      show "(F \<longlongrightarrow> ereal (F x)) (at_right x)"
  14.116 +        using cts unfolding continuous_at_split by (auto simp: continuous_within)
  14.117 +      show "\<forall>\<^sub>F i in at_right x. limsup (\<lambda>n. f (d n) x) \<le> F i"
  14.118 +        unfolding eventually_at_right[OF less_add_one]
  14.119 +      proof (rule, rule, rule less_add_one, safe)
  14.120 +        fix y assume y: "x < y"
  14.121 +        with dense_r obtain N where "x < r N" "r N < y" by auto
  14.122 +        have *: "y < r n' \<Longrightarrow> lim (?f N) \<le> lim (?f n')" for n'
  14.123 +          using \<open>r N < y\<close> by (intro LIMSEQ_le[OF lim_f lim_f]) (auto intro!: mono[THEN monoD])
  14.124 +        have "limsup (\<lambda>n. f (d n) x) \<le> limsup (?f N)"
  14.125 +          using \<open>x < r N\<close> by (auto intro!: Limsup_mono always_eventually mono[THEN monoD])
  14.126 +        also have "\<dots> = lim (\<lambda>n. ereal (?f N n))"
  14.127 +          using rat_cnv[of N] by (force intro!: convergent_limsup_cl simp: convergent_def)
  14.128 +        also have "\<dots> \<le> F y"
  14.129 +          by (auto intro!: INF_greatest * simp: convergent_real_imp_convergent_ereal rat_cnv F_eq)
  14.130 +        finally show "limsup (\<lambda>n. f (d n) x) \<le> F y" .
  14.131 +      qed
  14.132 +    qed simp
  14.133 +    show "F x \<le> liminf (\<lambda>n. f (d n) x)"
  14.134 +    proof (rule tendsto_ge_const)
  14.135 +      show "(F \<longlongrightarrow> ereal (F x)) (at_left x)"
  14.136 +        using cts unfolding continuous_at_split by (auto simp: continuous_within)
  14.137 +      show "\<forall>\<^sub>F i in at_left x. F i \<le> liminf (\<lambda>n. f (d n) x)"
  14.138 +        unfolding eventually_at_left[OF minus_one_less]
  14.139 +      proof (rule, rule, rule minus_one_less, safe)
  14.140 +        fix y assume y: "y < x"
  14.141 +        with dense_r obtain N where "y < r N" "r N < x" by auto
  14.142 +        have "F y \<le> liminf (?f N)"
  14.143 +          using \<open>y < r N\<close> by (auto simp: F_eq convergent_real_imp_convergent_ereal
  14.144 +            rat_cnv convergent_liminf_cl intro!: INF_lower2)
  14.145 +        also have "\<dots> \<le> liminf (\<lambda>n. f (d n) x)"
  14.146 +          using \<open>r N < x\<close> by (auto intro!: Liminf_mono monoD[OF mono] always_eventually)
  14.147 +        finally show "F y \<le> liminf (\<lambda>n. f (d n) x)" .
  14.148 +      qed
  14.149 +    qed simp
  14.150 +  qed
  14.151 +  ultimately show ?thesis using subseq by auto
  14.152 +qed
  14.153 +
  14.154 +(** Weak convergence corollaries to Helly's theorem. **)
  14.155 +
  14.156 +definition
  14.157 +  tight :: "(nat \<Rightarrow> real measure) \<Rightarrow> bool"
  14.158 +where
  14.159 +  "tight \<mu> \<equiv> (\<forall>n. real_distribution (\<mu> n)) \<and> (\<forall>(\<epsilon>::real)>0. \<exists>a b::real. a < b \<and> (\<forall>n. measure (\<mu> n) {a<..b} > 1 - \<epsilon>))"
  14.160 +
  14.161 +(* Can strengthen to equivalence. *)
  14.162 +theorem tight_imp_convergent_subsubsequence:
  14.163 +  assumes \<mu>: "tight \<mu>" "subseq s"
  14.164 +  shows "\<exists>r M. subseq r \<and> real_distribution M \<and> weak_conv_m (\<mu> \<circ> s \<circ> r) M"
  14.165 +proof -
  14.166 +  def f \<equiv> "\<lambda>k. cdf (\<mu> (s k))"
  14.167 +  interpret \<mu>: real_distribution "\<mu> k" for k
  14.168 +    using \<mu> unfolding tight_def by auto
  14.169 +
  14.170 +  have rcont: "\<And>x. continuous (at_right x) (f k)"
  14.171 +    and mono: "mono (f k)"
  14.172 +    and top: "(f k \<longlongrightarrow> 1) at_top"
  14.173 +    and bot: "(f k \<longlongrightarrow> 0) at_bot" for k
  14.174 +    unfolding f_def mono_def
  14.175 +    using \<mu>.cdf_nondecreasing \<mu>.cdf_is_right_cont \<mu>.cdf_lim_at_top_prob \<mu>.cdf_lim_at_bot by auto
  14.176 +  have bdd: "\<bar>f k x\<bar> \<le> 1" for k x
  14.177 +    by (auto simp add: abs_le_iff minus_le_iff f_def \<mu>.cdf_nonneg \<mu>.cdf_bounded_prob)
  14.178 +
  14.179 +  from Helly_selection[OF rcont mono bdd, of "\<lambda>x. x"] obtain r F
  14.180 +    where F: "subseq r" "\<And>x. continuous (at_right x) F" "mono F" "\<And>x. \<bar>F x\<bar> \<le> 1"
  14.181 +    and lim_F: "\<And>x. continuous (at x) F \<Longrightarrow> (\<lambda>n. f (r n) x) \<longlonglongrightarrow> F x"
  14.182 +    by blast
  14.183 +
  14.184 +  have "0 \<le> f n x" for n x
  14.185 +    unfolding f_def by (rule \<mu>.cdf_nonneg)
  14.186 +  have F_nonneg: "0 \<le> F x" for x
  14.187 +  proof -
  14.188 +    obtain y where "y < x" "isCont F y"
  14.189 +      using open_minus_countable[OF mono_ctble_discont[OF \<open>mono F\<close>], of "{..< x}"] by auto
  14.190 +    then have "0 \<le> F y"
  14.191 +      by (intro LIMSEQ_le_const[OF lim_F]) (auto simp: f_def \<mu>.cdf_nonneg)
  14.192 +    also have "\<dots> \<le> F x"
  14.193 +      using \<open>y < x\<close> by (auto intro!: monoD[OF \<open>mono F\<close>])
  14.194 +    finally show "0 \<le> F x" .
  14.195 +  qed
  14.196 +
  14.197 +  have Fab: "\<exists>a b. (\<forall>x\<ge>b. F x \<ge> 1 - \<epsilon>) \<and> (\<forall>x\<le>a. F x \<le> \<epsilon>)" if \<epsilon>: "0 < \<epsilon>" for \<epsilon>
  14.198 +  proof auto
  14.199 +    obtain a' b' where a'b': "a' < b'" "\<And>k. measure (\<mu> k) {a'<..b'} > 1 - \<epsilon>"
  14.200 +      using \<epsilon> \<mu> by (auto simp: tight_def)
  14.201 +    obtain a where a: "a < a'" "isCont F a"
  14.202 +      using open_minus_countable[OF mono_ctble_discont[OF \<open>mono F\<close>], of "{..< a'}"] by auto
  14.203 +    obtain b where b: "b' < b" "isCont F b"
  14.204 +      using open_minus_countable[OF mono_ctble_discont[OF \<open>mono F\<close>], of "{b' <..}"] by auto
  14.205 +    have "a < b"
  14.206 +      using a b a'b' by simp
  14.207 +
  14.208 +    let ?\<mu> = "\<lambda>k. measure (\<mu> (s (r k)))"
  14.209 +    have ab: "?\<mu> k {a<..b} > 1 - \<epsilon>" for k
  14.210 +    proof -
  14.211 +      have "?\<mu> k {a'<..b'} \<le> ?\<mu> k {a<..b}"
  14.212 +        using a b by (intro \<mu>.finite_measure_mono) auto
  14.213 +      then show ?thesis
  14.214 +        using a'b'(2) by (metis less_eq_real_def less_trans)
  14.215 +    qed
  14.216 +
  14.217 +    have "(\<lambda>k. ?\<mu> k {..b}) \<longlonglongrightarrow> F b"
  14.218 +      using b(2) lim_F unfolding f_def cdf_def o_def by auto
  14.219 +    then have "1 - \<epsilon> \<le> F b"
  14.220 +    proof (rule tendsto_le_const[OF sequentially_bot], intro always_eventually allI)
  14.221 +      fix k
  14.222 +      have "1 - \<epsilon> < ?\<mu> k {a<..b}"
  14.223 +        using ab by auto
  14.224 +      also have "\<dots> \<le> ?\<mu> k {..b}"
  14.225 +        by (auto intro!: \<mu>.finite_measure_mono)
  14.226 +      finally show "1 - \<epsilon> \<le> ?\<mu> k {..b}"
  14.227 +        by (rule less_imp_le)
  14.228 +    qed
  14.229 +    then show "\<exists>b. \<forall>x\<ge>b. 1 - \<epsilon> \<le> F x"
  14.230 +      using F unfolding mono_def by (metis order.trans)
  14.231 +
  14.232 +    have "(\<lambda>k. ?\<mu> k {..a}) \<longlonglongrightarrow> F a"
  14.233 +      using a(2) lim_F unfolding f_def cdf_def o_def by auto
  14.234 +    then have Fa: "F a \<le> \<epsilon>"
  14.235 +    proof (rule tendsto_ge_const[OF sequentially_bot], intro always_eventually allI)
  14.236 +      fix k
  14.237 +      have "?\<mu> k {..a} + ?\<mu> k {a<..b} \<le> 1"
  14.238 +        by (subst \<mu>.finite_measure_Union[symmetric]) auto
  14.239 +      then show "?\<mu> k {..a} \<le> \<epsilon>"
  14.240 +        using ab[of k] by simp
  14.241 +    qed
  14.242 +    then show "\<exists>a. \<forall>x\<le>a. F x \<le> \<epsilon>"
  14.243 +      using F unfolding mono_def by (metis order.trans)
  14.244 +  qed
  14.245 +
  14.246 +  have "(F \<longlongrightarrow> 1) at_top"
  14.247 +  proof (rule order_tendstoI)
  14.248 +    show "1 < y \<Longrightarrow> \<forall>\<^sub>F x in at_top. F x < y" for y
  14.249 +      using \<open>\<And>x. \<bar>F x\<bar> \<le> 1\<close> \<open>\<And>x. 0 \<le> F x\<close> by (auto intro: le_less_trans always_eventually)
  14.250 +    fix y :: real assume "y < 1"
  14.251 +    then obtain z where "y < z" "z < 1"
  14.252 +      using dense[of y 1] by auto
  14.253 +    with Fab[of "1 - z"] show "\<forall>\<^sub>F x in at_top. y < F x"
  14.254 +      by (auto simp: eventually_at_top_linorder intro: less_le_trans)
  14.255 +  qed
  14.256 +  moreover
  14.257 +  have "(F \<longlongrightarrow> 0) at_bot"
  14.258 +  proof (rule order_tendstoI)
  14.259 +    show "y < 0 \<Longrightarrow> \<forall>\<^sub>F x in at_bot. y < F x" for y
  14.260 +      using \<open>\<And>x. 0 \<le> F x\<close> by (auto intro: less_le_trans always_eventually)
  14.261 +    fix y :: real assume "0 < y"
  14.262 +    then obtain z where "0 < z" "z < y"
  14.263 +      using dense[of 0 y] by auto
  14.264 +    with Fab[of z] show "\<forall>\<^sub>F x in at_bot. F x < y"
  14.265 +      by (auto simp: eventually_at_bot_linorder intro: le_less_trans)
  14.266 +  qed
  14.267 +  ultimately have M: "real_distribution (interval_measure F)" "cdf (interval_measure F) = F"
  14.268 +    using F by (auto intro!: real_distribution_interval_measure cdf_interval_measure simp: mono_def)
  14.269 +  with lim_F lim_subseq M have "weak_conv_m (\<mu> \<circ> s \<circ> r) (interval_measure F)"
  14.270 +    by (auto simp: weak_conv_def weak_conv_m_def f_def comp_def)
  14.271 +  then show "\<exists>r M. subseq r \<and> (real_distribution M \<and> weak_conv_m (\<mu> \<circ> s \<circ> r) M)"
  14.272 +    using F M by auto
  14.273 +qed
  14.274 +
  14.275 +corollary tight_subseq_weak_converge:
  14.276 +  fixes \<mu> :: "nat \<Rightarrow> real measure" and M :: "real measure"
  14.277 +  assumes "\<And>n. real_distribution (\<mu> n)" "real_distribution M" and tight: "tight \<mu>" and
  14.278 +    subseq: "\<And>s \<nu>. subseq s \<Longrightarrow> real_distribution \<nu> \<Longrightarrow> weak_conv_m (\<mu> \<circ> s) \<nu> \<Longrightarrow> weak_conv_m (\<mu> \<circ> s) M"
  14.279 +  shows "weak_conv_m \<mu> M"
  14.280 +proof (rule ccontr)
  14.281 +  def f \<equiv> "\<lambda>n. cdf (\<mu> n)" and F \<equiv> "cdf M"
  14.282 +
  14.283 +  assume "\<not> weak_conv_m \<mu> M"
  14.284 +  then obtain x where x: "isCont F x" "\<not> (\<lambda>n. f n x) \<longlonglongrightarrow> F x"
  14.285 +    by (auto simp: weak_conv_m_def weak_conv_def f_def F_def)
  14.286 +  then obtain \<epsilon> where "\<epsilon> > 0" and "infinite {n. \<not> dist (f n x) (F x) < \<epsilon>}"
  14.287 +    by (auto simp: tendsto_iff not_eventually INFM_iff_infinite cofinite_eq_sequentially[symmetric])
  14.288 +  then obtain s where s: "\<And>n. \<not> dist (f (s n) x) (F x) < \<epsilon>" and "subseq s"
  14.289 +    using enumerate_in_set enumerate_mono by (fastforce simp: subseq_def)
  14.290 +  then obtain r \<nu> where r: "subseq r" "real_distribution \<nu>" "weak_conv_m (\<mu> \<circ> s \<circ> r) \<nu>"
  14.291 +    using tight_imp_convergent_subsubsequence[OF tight] by blast
  14.292 +  then have "weak_conv_m (\<mu> \<circ> (s \<circ> r)) M"
  14.293 +    using \<open>subseq s\<close> r by (intro subseq subseq_o) (auto simp: comp_assoc)
  14.294 +  then have "(\<lambda>n. f (s (r n)) x) \<longlonglongrightarrow> F x"
  14.295 +    using x by (auto simp: weak_conv_m_def weak_conv_def F_def f_def)
  14.296 +  then show False
  14.297 +    using s \<open>\<epsilon> > 0\<close> by (auto dest: tendstoD)
  14.298 +qed
  14.299 +
  14.300 +end
    15.1 --- a/src/HOL/Probability/Interval_Integral.thy	Wed Jan 06 13:04:31 2016 +0100
    15.2 +++ b/src/HOL/Probability/Interval_Integral.thy	Wed Jan 06 12:18:53 2016 +0100
    15.3 @@ -190,6 +190,19 @@
    15.4      interval_lebesgue_integral M a b f = interval_lebesgue_integral M a b g"
    15.5    by (auto intro: set_lebesgue_integral_cong_AE simp: interval_lebesgue_integral_def)
    15.6  
    15.7 +lemma interval_integrable_mirror:
    15.8 +  shows "interval_lebesgue_integrable lborel a b (\<lambda>x. f (-x)) \<longleftrightarrow>
    15.9 +    interval_lebesgue_integrable lborel (-b) (-a) f"
   15.10 +proof -
   15.11 +  have *: "indicator (einterval a b) (- x) = (indicator (einterval (-b) (-a)) x :: real)"
   15.12 +    for a b :: ereal and x :: real
   15.13 +    by (cases a b rule: ereal2_cases) (auto simp: einterval_def split: split_indicator)
   15.14 +  show ?thesis
   15.15 +    unfolding interval_lebesgue_integrable_def
   15.16 +    using lborel_integrable_real_affine_iff[symmetric, of "-1" "\<lambda>x. indicator (einterval _ _) x *\<^sub>R f x" 0]
   15.17 +    by (simp add: *)
   15.18 +qed
   15.19 +
   15.20  lemma interval_lebesgue_integral_add [intro, simp]: 
   15.21    fixes M a b f 
   15.22    assumes "interval_lebesgue_integrable M a b f" "interval_lebesgue_integrable M a b g"
    16.1 --- a/src/HOL/Probability/Lebesgue_Integral_Substitution.thy	Wed Jan 06 13:04:31 2016 +0100
    16.2 +++ b/src/HOL/Probability/Lebesgue_Integral_Substitution.thy	Wed Jan 06 12:18:53 2016 +0100
    16.3 @@ -12,243 +12,6 @@
    16.4  imports Interval_Integral
    16.5  begin
    16.6  
    16.7 -lemma measurable_sets_borel:
    16.8 -    "\<lbrakk>f \<in> measurable borel M; A \<in> sets M\<rbrakk> \<Longrightarrow> f -` A \<in> sets borel"
    16.9 -  by (drule (1) measurable_sets) simp
   16.10 -
   16.11 -lemma nn_integral_indicator_singleton[simp]:
   16.12 -  assumes [measurable]: "{y} \<in> sets M"
   16.13 -  shows "(\<integral>\<^sup>+x. f x * indicator {y} x \<partial>M) = max 0 (f y) * emeasure M {y}"
   16.14 -proof-
   16.15 -  have "(\<integral>\<^sup>+x. f x * indicator {y} x \<partial>M) = (\<integral>\<^sup>+x. max 0 (f y) * indicator {y} x \<partial>M)"
   16.16 -    by (subst nn_integral_max_0[symmetric]) (auto intro!: nn_integral_cong split: split_indicator)
   16.17 -  then show ?thesis
   16.18 -    by (simp add: nn_integral_cmult)
   16.19 -qed
   16.20 -
   16.21 -lemma nn_integral_set_ereal:
   16.22 -  "(\<integral>\<^sup>+x. ereal (f x) * indicator A x \<partial>M) = (\<integral>\<^sup>+x. ereal (f x * indicator A x) \<partial>M)"
   16.23 -  by (rule nn_integral_cong) (simp split: split_indicator)
   16.24 -
   16.25 -lemma nn_integral_indicator_singleton'[simp]:
   16.26 -  assumes [measurable]: "{y} \<in> sets M"
   16.27 -  shows "(\<integral>\<^sup>+x. ereal (f x * indicator {y} x) \<partial>M) = max 0 (f y) * emeasure M {y}"
   16.28 -  by (subst nn_integral_set_ereal[symmetric]) simp
   16.29 -
   16.30 -lemma set_borel_measurable_sets:
   16.31 -  fixes f :: "_ \<Rightarrow> _::real_normed_vector"
   16.32 -  assumes "set_borel_measurable M X f" "B \<in> sets borel" "X \<in> sets M"
   16.33 -  shows "f -` B \<inter> X \<in> sets M"
   16.34 -proof -
   16.35 -  have "f \<in> borel_measurable (restrict_space M X)"
   16.36 -    using assms by (subst borel_measurable_restrict_space_iff) auto
   16.37 -  then have "f -` B \<inter> space (restrict_space M X) \<in> sets (restrict_space M X)"
   16.38 -    by (rule measurable_sets) fact
   16.39 -  with \<open>X \<in> sets M\<close> show ?thesis
   16.40 -    by (subst (asm) sets_restrict_space_iff) (auto simp: space_restrict_space)
   16.41 -qed
   16.42 -
   16.43 -lemma borel_set_induct[consumes 1, case_names empty interval compl union]:
   16.44 -  assumes "A \<in> sets borel" 
   16.45 -  assumes empty: "P {}" and int: "\<And>a b. a \<le> b \<Longrightarrow> P {a..b}" and compl: "\<And>A. A \<in> sets borel \<Longrightarrow> P A \<Longrightarrow> P (-A)" and
   16.46 -          un: "\<And>f. disjoint_family f \<Longrightarrow> (\<And>i. f i \<in> sets borel) \<Longrightarrow>  (\<And>i. P (f i)) \<Longrightarrow> P (\<Union>i::nat. f i)"
   16.47 -  shows "P (A::real set)"
   16.48 -proof-
   16.49 -  let ?G = "range (\<lambda>(a,b). {a..b::real})"
   16.50 -  have "Int_stable ?G" "?G \<subseteq> Pow UNIV" "A \<in> sigma_sets UNIV ?G" 
   16.51 -      using assms(1) by (auto simp add: borel_eq_atLeastAtMost Int_stable_def)
   16.52 -  thus ?thesis
   16.53 -  proof (induction rule: sigma_sets_induct_disjoint) 
   16.54 -    case (union f)
   16.55 -      from union.hyps(2) have "\<And>i. f i \<in> sets borel" by (auto simp: borel_eq_atLeastAtMost)
   16.56 -      with union show ?case by (auto intro: un)
   16.57 -  next
   16.58 -    case (basic A)
   16.59 -    then obtain a b where "A = {a .. b}" by auto
   16.60 -    then show ?case
   16.61 -      by (cases "a \<le> b") (auto intro: int empty)
   16.62 -  qed (auto intro: empty compl simp: Compl_eq_Diff_UNIV[symmetric] borel_eq_atLeastAtMost)
   16.63 -qed
   16.64 -
   16.65 -definition "mono_on f A \<equiv> \<forall>r s. r \<in> A \<and> s \<in> A \<and> r \<le> s \<longrightarrow> f r \<le> f s"
   16.66 -
   16.67 -lemma mono_onI:
   16.68 -  "(\<And>r s. r \<in> A \<Longrightarrow> s \<in> A \<Longrightarrow> r \<le> s \<Longrightarrow> f r \<le> f s) \<Longrightarrow> mono_on f A"
   16.69 -  unfolding mono_on_def by simp
   16.70 -
   16.71 -lemma mono_onD:
   16.72 -  "\<lbrakk>mono_on f A; r \<in> A; s \<in> A; r \<le> s\<rbrakk> \<Longrightarrow> f r \<le> f s"
   16.73 -  unfolding mono_on_def by simp
   16.74 -
   16.75 -lemma mono_imp_mono_on: "mono f \<Longrightarrow> mono_on f A"
   16.76 -  unfolding mono_def mono_on_def by auto
   16.77 -
   16.78 -lemma mono_on_subset: "mono_on f A \<Longrightarrow> B \<subseteq> A \<Longrightarrow> mono_on f B"
   16.79 -  unfolding mono_on_def by auto
   16.80 -
   16.81 -definition "strict_mono_on f A \<equiv> \<forall>r s. r \<in> A \<and> s \<in> A \<and> r < s \<longrightarrow> f r < f s"
   16.82 -
   16.83 -lemma strict_mono_onI:
   16.84 -  "(\<And>r s. r \<in> A \<Longrightarrow> s \<in> A \<Longrightarrow> r < s \<Longrightarrow> f r < f s) \<Longrightarrow> strict_mono_on f A"
   16.85 -  unfolding strict_mono_on_def by simp
   16.86 -
   16.87 -lemma strict_mono_onD:
   16.88 -  "\<lbrakk>strict_mono_on f A; r \<in> A; s \<in> A; r < s\<rbrakk> \<Longrightarrow> f r < f s"
   16.89 -  unfolding strict_mono_on_def by simp
   16.90 -
   16.91 -lemma mono_on_greaterD:
   16.92 -  assumes "mono_on g A" "x \<in> A" "y \<in> A" "g x > (g (y::_::linorder) :: _ :: linorder)"
   16.93 -  shows "x > y"
   16.94 -proof (rule ccontr)
   16.95 -  assume "\<not>x > y"
   16.96 -  hence "x \<le> y" by (simp add: not_less)
   16.97 -  from assms(1-3) and this have "g x \<le> g y" by (rule mono_onD)
   16.98 -  with assms(4) show False by simp
   16.99 -qed
  16.100 -
  16.101 -lemma strict_mono_inv:
  16.102 -  fixes f :: "('a::linorder) \<Rightarrow> ('b::linorder)"
  16.103 -  assumes "strict_mono f" and "surj f" and inv: "\<And>x. g (f x) = x"
  16.104 -  shows "strict_mono g"
  16.105 -proof
  16.106 -  fix x y :: 'b assume "x < y"
  16.107 -  from \<open>surj f\<close> obtain x' y' where [simp]: "x = f x'" "y = f y'" by blast
  16.108 -  with \<open>x < y\<close> and \<open>strict_mono f\<close> have "x' < y'" by (simp add: strict_mono_less)
  16.109 -  with inv show "g x < g y" by simp
  16.110 -qed
  16.111 -
  16.112 -lemma strict_mono_on_imp_inj_on:
  16.113 -  assumes "strict_mono_on (f :: (_ :: linorder) \<Rightarrow> (_ :: preorder)) A"
  16.114 -  shows "inj_on f A"
  16.115 -proof (rule inj_onI)
  16.116 -  fix x y assume "x \<in> A" "y \<in> A" "f x = f y"
  16.117 -  thus "x = y"
  16.118 -    by (cases x y rule: linorder_cases)
  16.119 -       (auto dest: strict_mono_onD[OF assms, of x y] strict_mono_onD[OF assms, of y x]) 
  16.120 -qed
  16.121 -
  16.122 -lemma strict_mono_on_leD:
  16.123 -  assumes "strict_mono_on (f :: (_ :: linorder) \<Rightarrow> _ :: preorder) A" "x \<in> A" "y \<in> A" "x \<le> y"
  16.124 -  shows "f x \<le> f y"
  16.125 -proof (insert le_less_linear[of y x], elim disjE)
  16.126 -  assume "x < y"
  16.127 -  with assms have "f x < f y" by (rule_tac strict_mono_onD[OF assms(1)]) simp_all
  16.128 -  thus ?thesis by (rule less_imp_le)
  16.129 -qed (insert assms, simp)
  16.130 -
  16.131 -lemma strict_mono_on_eqD:
  16.132 -  fixes f :: "(_ :: linorder) \<Rightarrow> (_ :: preorder)"
  16.133 -  assumes "strict_mono_on f A" "f x = f y" "x \<in> A" "y \<in> A"
  16.134 -  shows "y = x"
  16.135 -  using assms by (rule_tac linorder_cases[of x y]) (auto dest: strict_mono_onD)
  16.136 -
  16.137 -lemma mono_on_imp_deriv_nonneg:
  16.138 -  assumes mono: "mono_on f A" and deriv: "(f has_real_derivative D) (at x)"
  16.139 -  assumes "x \<in> interior A"
  16.140 -  shows "D \<ge> 0"
  16.141 -proof (rule tendsto_le_const)
  16.142 -  let ?A' = "(\<lambda>y. y - x) ` interior A"
  16.143 -  from deriv show "((\<lambda>h. (f (x + h) - f x) / h) \<longlongrightarrow> D) (at 0)"
  16.144 -      by (simp add: field_has_derivative_at has_field_derivative_def)
  16.145 -  from mono have mono': "mono_on f (interior A)" by (rule mono_on_subset) (rule interior_subset)
  16.146 -
  16.147 -  show "eventually (\<lambda>h. (f (x + h) - f x) / h \<ge> 0) (at 0)"
  16.148 -  proof (subst eventually_at_topological, intro exI conjI ballI impI)
  16.149 -    have "open (interior A)" by simp
  16.150 -    hence "open (op + (-x) ` interior A)" by (rule open_translation)
  16.151 -    also have "(op + (-x) ` interior A) = ?A'" by auto
  16.152 -    finally show "open ?A'" .
  16.153 -  next
  16.154 -    from \<open>x \<in> interior A\<close> show "0 \<in> ?A'" by auto
  16.155 -  next
  16.156 -    fix h assume "h \<in> ?A'"
  16.157 -    hence "x + h \<in> interior A" by auto
  16.158 -    with mono' and \<open>x \<in> interior A\<close> show "(f (x + h) - f x) / h \<ge> 0"
  16.159 -      by (cases h rule: linorder_cases[of _ 0])
  16.160 -         (simp_all add: divide_nonpos_neg divide_nonneg_pos mono_onD field_simps)
  16.161 -  qed
  16.162 -qed simp
  16.163 -
  16.164 -lemma strict_mono_on_imp_mono_on: 
  16.165 -  "strict_mono_on (f :: (_ :: linorder) \<Rightarrow> _ :: preorder) A \<Longrightarrow> mono_on f A"
  16.166 -  by (rule mono_onI, rule strict_mono_on_leD)
  16.167 -
  16.168 -lemma has_real_derivative_imp_continuous_on:
  16.169 -  assumes "\<And>x. x \<in> A \<Longrightarrow> (f has_real_derivative f' x) (at x)"
  16.170 -  shows "continuous_on A f"
  16.171 -  apply (intro differentiable_imp_continuous_on, unfold differentiable_on_def)
  16.172 -  apply (intro ballI Deriv.differentiableI)
  16.173 -  apply (rule has_field_derivative_subset[OF assms])
  16.174 -  apply simp_all
  16.175 -  done
  16.176 -
  16.177 -lemma closure_contains_Sup:
  16.178 -  fixes S :: "real set"
  16.179 -  assumes "S \<noteq> {}" "bdd_above S"
  16.180 -  shows "Sup S \<in> closure S"
  16.181 -proof-
  16.182 -  have "Inf (uminus ` S) \<in> closure (uminus ` S)" 
  16.183 -      using assms by (intro closure_contains_Inf) auto
  16.184 -  also have "Inf (uminus ` S) = -Sup S" by (simp add: Inf_real_def)
  16.185 -  also have "closure (uminus ` S) = uminus ` closure S"
  16.186 -      by (rule sym, intro closure_injective_linear_image) (auto intro: linearI)
  16.187 -  finally show ?thesis by auto
  16.188 -qed
  16.189 -
  16.190 -lemma closed_contains_Sup:
  16.191 -  fixes S :: "real set"
  16.192 -  shows "S \<noteq> {} \<Longrightarrow> bdd_above S \<Longrightarrow> closed S \<Longrightarrow> Sup S \<in> S"
  16.193 -  by (subst closure_closed[symmetric], assumption, rule closure_contains_Sup)
  16.194 -
  16.195 -lemma deriv_nonneg_imp_mono:
  16.196 -  assumes deriv: "\<And>x. x \<in> {a..b} \<Longrightarrow> (g has_real_derivative g' x) (at x)"
  16.197 -  assumes nonneg: "\<And>x. x \<in> {a..b} \<Longrightarrow> g' x \<ge> 0"
  16.198 -  assumes ab: "a \<le> b"
  16.199 -  shows "g a \<le> g b"
  16.200 -proof (cases "a < b")
  16.201 -  assume "a < b"
  16.202 -  from deriv have "\<forall>x. x \<ge> a \<and> x \<le> b \<longrightarrow> (g has_real_derivative g' x) (at x)" by simp
  16.203 -  from MVT2[OF \<open>a < b\<close> this] and deriv 
  16.204 -    obtain \<xi> where \<xi>_ab: "\<xi> > a" "\<xi> < b" and g_ab: "g b - g a = (b - a) * g' \<xi>" by blast
  16.205 -  from \<xi>_ab ab nonneg have "(b - a) * g' \<xi> \<ge> 0" by simp
  16.206 -  with g_ab show ?thesis by simp
  16.207 -qed (insert ab, simp)
  16.208 -
  16.209 -lemma continuous_interval_vimage_Int:
  16.210 -  assumes "continuous_on {a::real..b} g" and mono: "\<And>x y. a \<le> x \<Longrightarrow> x \<le> y \<Longrightarrow> y \<le> b \<Longrightarrow> g x \<le> g y"
  16.211 -  assumes "a \<le> b" "(c::real) \<le> d" "{c..d} \<subseteq> {g a..g b}"
  16.212 -  obtains c' d' where "{a..b} \<inter> g -` {c..d} = {c'..d'}" "c' \<le> d'" "g c' = c" "g d' = d"
  16.213 -proof-
  16.214 -    let ?A = "{a..b} \<inter> g -` {c..d}"
  16.215 -    from IVT'[of g a c b, OF _ _ \<open>a \<le> b\<close> assms(1)] assms(4,5) 
  16.216 -         obtain c'' where c'': "c'' \<in> ?A" "g c'' = c" by auto
  16.217 -    from IVT'[of g a d b, OF _ _ \<open>a \<le> b\<close> assms(1)] assms(4,5) 
  16.218 -         obtain d'' where d'': "d'' \<in> ?A" "g d'' = d" by auto
  16.219 -    hence [simp]: "?A \<noteq> {}" by blast
  16.220 -
  16.221 -    def c' \<equiv> "Inf ?A" and d' \<equiv> "Sup ?A"
  16.222 -    have "?A \<subseteq> {c'..d'}" unfolding c'_def d'_def
  16.223 -        by (intro subsetI) (auto intro: cInf_lower cSup_upper)
  16.224 -    moreover from assms have "closed ?A" 
  16.225 -        using continuous_on_closed_vimage[of "{a..b}" g] by (subst Int_commute) simp
  16.226 -    hence c'd'_in_set: "c' \<in> ?A" "d' \<in> ?A" unfolding c'_def d'_def
  16.227 -        by ((intro closed_contains_Inf closed_contains_Sup, simp_all)[])+
  16.228 -    hence "{c'..d'} \<subseteq> ?A" using assms 
  16.229 -        by (intro subsetI)
  16.230 -           (auto intro!: order_trans[of c "g c'" "g x" for x] order_trans[of "g x" "g d'" d for x] 
  16.231 -                 intro!: mono)
  16.232 -    moreover have "c' \<le> d'" using c'd'_in_set(2) unfolding c'_def by (intro cInf_lower) auto
  16.233 -    moreover have "g c' \<le> c" "g d' \<ge> d"
  16.234 -      apply (insert c'' d'' c'd'_in_set)
  16.235 -      apply (subst c''(2)[symmetric])
  16.236 -      apply (auto simp: c'_def intro!: mono cInf_lower c'') []
  16.237 -      apply (subst d''(2)[symmetric])
  16.238 -      apply (auto simp: d'_def intro!: mono cSup_upper d'') []
  16.239 -      done
  16.240 -    with c'd'_in_set have "g c' = c" "g d' = d" by auto
  16.241 -    ultimately show ?thesis using that by blast
  16.242 -qed
  16.243 -
  16.244  lemma nn_integral_substitution_aux:
  16.245    fixes f :: "real \<Rightarrow> ereal"
  16.246    assumes Mf: "f \<in> borel_measurable borel"
    17.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    17.2 +++ b/src/HOL/Probability/Levy.thy	Wed Jan 06 12:18:53 2016 +0100
    17.3 @@ -0,0 +1,539 @@
    17.4 +(*  Theory: Levy.thy
    17.5 +    Author: Jeremy Avigad
    17.6 +*)
    17.7 +
    17.8 +section \<open>The Levy inversion theorem, and the Levy continuity theorem.\<close>
    17.9 +
   17.10 +theory Levy
   17.11 +  imports Characteristic_Functions Helly_Selection Sinc_Integral
   17.12 +begin
   17.13 +
   17.14 +lemma LIM_zero_cancel:
   17.15 +  fixes f :: "_ \<Rightarrow> 'b::real_normed_vector"
   17.16 +  shows "((\<lambda>x. f x - l) \<longlongrightarrow> 0) F \<Longrightarrow> (f \<longlongrightarrow> l) F"
   17.17 +unfolding tendsto_iff dist_norm by simp
   17.18 +
   17.19 +subsection \<open>The Levy inversion theorem\<close>
   17.20 +
   17.21 +(* Actually, this is not needed for us -- but it is useful for other purposes. (See Billingsley.) *)
   17.22 +lemma Levy_Inversion_aux1:
   17.23 +  fixes a b :: real
   17.24 +  assumes "a \<le> b"
   17.25 +  shows "((\<lambda>t. (iexp (-(t * a)) - iexp (-(t * b))) / (ii * t)) \<longlongrightarrow> b - a) (at 0)"
   17.26 +    (is "(?F \<longlongrightarrow> _) (at _)")
   17.27 +proof -
   17.28 +  have 1: "cmod (?F t - (b - a)) \<le> a^2 / 2 * abs t + b^2 / 2 * abs t" if "t \<noteq> 0" for t
   17.29 +  proof -
   17.30 +    have "cmod (?F t - (b - a)) = cmod (
   17.31 +        (iexp (-(t * a)) - (1 + ii * -(t * a))) / (ii * t) - 
   17.32 +        (iexp (-(t * b)) - (1 + ii * -(t * b))) / (ii * t))"  
   17.33 +           (is "_ = cmod (?one / (ii * t) - ?two / (ii * t))")
   17.34 +      using `t \<noteq> 0` by (intro arg_cong[where f=norm]) (simp add: field_simps)
   17.35 +    also have "\<dots> \<le> cmod (?one / (ii * t)) + cmod (?two / (ii * t))" 
   17.36 +      by (rule norm_triangle_ineq4)
   17.37 +    also have "cmod (?one / (ii * t)) = cmod ?one / abs t"
   17.38 +      by (simp add: norm_divide norm_mult)
   17.39 +    also have "cmod (?two / (ii * t)) = cmod ?two / abs t"
   17.40 +      by (simp add: norm_divide norm_mult)      
   17.41 +    also have "cmod ?one / abs t + cmod ?two / abs t \<le> 
   17.42 +        ((- (a * t))^2 / 2) / abs t + ((- (b * t))^2 / 2) / abs t"
   17.43 +      apply (rule add_mono)
   17.44 +      apply (rule divide_right_mono)
   17.45 +      using iexp_approx1 [of "-(t * a)" 1] apply (simp add: field_simps eval_nat_numeral)
   17.46 +      apply force
   17.47 +      apply (rule divide_right_mono)
   17.48 +      using iexp_approx1 [of "-(t * b)" 1] apply (simp add: field_simps eval_nat_numeral)
   17.49 +      by force
   17.50 +    also have "\<dots> = a^2 / 2 * abs t + b^2 / 2 * abs t"
   17.51 +      using `t \<noteq> 0` apply (case_tac "t \<ge> 0", simp add: field_simps power2_eq_square)
   17.52 +      using `t \<noteq> 0` by (subst (1 2) abs_of_neg, auto simp add: field_simps power2_eq_square)
   17.53 +    finally show "cmod (?F t - (b - a)) \<le> a^2 / 2 * abs t + b^2 / 2 * abs t" .
   17.54 +  qed
   17.55 +  show ?thesis
   17.56 +    apply (rule LIM_zero_cancel)
   17.57 +    apply (rule tendsto_norm_zero_cancel)
   17.58 +    apply (rule real_LIM_sandwich_zero [OF _ _ 1])
   17.59 +    apply (auto intro!: tendsto_eq_intros)
   17.60 +    done
   17.61 +qed
   17.62 +
   17.63 +lemma Levy_Inversion_aux2:
   17.64 +  fixes a b t :: real
   17.65 +  assumes "a \<le> b" and "t \<noteq> 0"
   17.66 +  shows "cmod ((iexp (t * b) - iexp (t * a)) / (ii * t)) \<le> b - a" (is "?F \<le> _")
   17.67 +proof -
   17.68 +  have "?F = cmod (iexp (t * a) * (iexp (t * (b - a)) - 1) / (ii * t))"
   17.69 +    using `t \<noteq> 0` by (intro arg_cong[where f=norm]) (simp add: field_simps exp_diff exp_minus)
   17.70 +  also have "\<dots> = cmod (iexp (t * (b - a)) - 1) / abs t"
   17.71 +    unfolding norm_divide norm_mult norm_exp_ii_times using `t \<noteq> 0`
   17.72 +    by (simp add: complex_eq_iff norm_mult)
   17.73 +  also have "\<dots> \<le> abs (t * (b - a)) / abs t"
   17.74 +    using iexp_approx1 [of "t * (b - a)" 0]
   17.75 +    by (intro divide_right_mono) (auto simp add: field_simps eval_nat_numeral)
   17.76 +  also have "\<dots> = b - a"
   17.77 +    using assms by (auto simp add: abs_mult) 
   17.78 +  finally show ?thesis .
   17.79 +qed
   17.80 +
   17.81 +(* TODO: refactor! *)
   17.82 +theorem (in real_distribution) Levy_Inversion:
   17.83 +  fixes a b :: real
   17.84 +  assumes "a \<le> b"
   17.85 +  defines "\<mu> \<equiv> measure M" and "\<phi> \<equiv> char M"
   17.86 +  assumes "\<mu> {a} = 0" and "\<mu> {b} = 0"
   17.87 +  shows "(\<lambda>T. 1 / (2 * pi) * (CLBINT t=-T..T. (iexp (-(t * a)) - iexp (-(t * b))) / (ii * t) * \<phi> t))
   17.88 +    \<longlonglongrightarrow> \<mu> {a<..b}"
   17.89 +    (is "(\<lambda>T. 1 / (2 * pi) * (CLBINT t=-T..T. ?F t * \<phi> t)) \<longlonglongrightarrow> of_real (\<mu> {a<..b})")
   17.90 +proof -
   17.91 +  interpret P: pair_sigma_finite lborel M ..
   17.92 +  from bounded_Si obtain B where Bprop: "\<And>T. abs (Si T) \<le> B" by auto
   17.93 +  from Bprop [of 0] have [simp]: "B \<ge> 0" by auto
   17.94 +  let ?f = "\<lambda>t x :: real. (iexp (t * (x - a)) - iexp(t * (x - b))) / (ii * t)"
   17.95 +  { fix T :: real
   17.96 +    assume "T \<ge> 0"
   17.97 +    let ?f' = "\<lambda>(t, x). indicator {-T<..<T} t *\<^sub>R ?f t x"
   17.98 +    { fix x
   17.99 +      have 1: "complex_interval_lebesgue_integrable lborel u v (\<lambda>t. ?f t x)" for u v :: real
  17.100 +        using Levy_Inversion_aux2[of "x - b" "x - a"]
  17.101 +        apply (simp add: interval_lebesgue_integrable_def del: times_divide_eq_left)
  17.102 +        apply (intro integrableI_bounded_set_indicator[where B="b - a"] conjI impI)
  17.103 +        apply (auto intro!: AE_I [of _ _ "{0}"] simp: assms)
  17.104 +        done
  17.105 +      have "(CLBINT t. ?f' (t, x)) = (CLBINT t=-T..T. ?f t x)"
  17.106 +        using `T \<ge> 0` by (simp add: interval_lebesgue_integral_def)
  17.107 +      also have "\<dots> = (CLBINT t=-T..(0 :: real). ?f t x) + (CLBINT t=(0 :: real)..T. ?f t x)"
  17.108 +          (is "_ = _ + ?t")
  17.109 +        using 1 by (intro interval_integral_sum[symmetric]) (simp add: min_absorb1 max_absorb2 \<open>T \<ge> 0\<close>)
  17.110 +      also have "(CLBINT t=-T..(0 :: real). ?f t x) = (CLBINT t=(0::real)..T. ?f (-t) x)"
  17.111 +        by (subst interval_integral_reflect) auto
  17.112 +      also have "\<dots> + ?t = (CLBINT t=(0::real)..T. ?f (-t) x + ?f t x)"
  17.113 +        using 1
  17.114 +        by (intro interval_lebesgue_integral_add(2) [symmetric] interval_integrable_mirror[THEN iffD2]) simp_all
  17.115 +      also have "\<dots> = (CLBINT t=(0::real)..T. ((iexp(t * (x - a)) - iexp (-(t * (x - a)))) -  
  17.116 +          (iexp(t * (x - b)) - iexp (-(t * (x - b))))) / (ii * t))"
  17.117 +        using `T \<ge> 0` by (intro interval_integral_cong) (auto simp add: divide_simps)
  17.118 +      also have "\<dots> = (CLBINT t=(0::real)..T. complex_of_real(
  17.119 +          2 * (sin (t * (x - a)) / t) - 2 * (sin (t * (x - b)) / t)))"
  17.120 +        using `T \<ge> 0`
  17.121 +        apply (intro interval_integral_cong)
  17.122 +        apply (simp add: field_simps cis.ctr Im_divide Re_divide Im_exp Re_exp complex_eq_iff)
  17.123 +        unfolding minus_diff_eq[symmetric, of "y * x" "y * a" for y a] sin_minus cos_minus
  17.124 +        apply (simp add: field_simps power2_eq_square)
  17.125 +        done
  17.126 +      also have "\<dots> = complex_of_real (LBINT t=(0::real)..T. 
  17.127 +          2 * (sin (t * (x - a)) / t) - 2 * (sin (t * (x - b)) / t))"
  17.128 +        by (rule interval_lebesgue_integral_of_real)
  17.129 +      also have "\<dots> = complex_of_real (2 * (sgn (x - a) * Si (T * abs (x - a)) -
  17.130 +          sgn (x - b) * Si (T * abs (x - b))))"
  17.131 +        apply (subst interval_lebesgue_integral_diff)
  17.132 +        apply (rule interval_lebesgue_integrable_mult_right, rule integrable_sinc')+
  17.133 +        apply (subst interval_lebesgue_integral_mult_right)+
  17.134 +        apply (simp add: zero_ereal_def[symmetric] LBINT_I0c_sin_scale_divide[OF `T \<ge> 0`])
  17.135 +        done
  17.136 +      finally have "(CLBINT t. ?f' (t, x)) =
  17.137 +          2 * (sgn (x - a) * Si (T * abs (x - a)) - sgn (x - b) * Si (T * abs (x - b)))" .
  17.138 +    } note main_eq = this
  17.139 +    have "(CLBINT t=-T..T. ?F t * \<phi> t) = 
  17.140 +      (CLBINT t. (CLINT x | M. ?F t * iexp (t * x) * indicator {-T<..<T} t))"
  17.141 +      using `T \<ge> 0` unfolding \<phi>_def char_def interval_lebesgue_integral_def
  17.142 +      by (auto split: split_indicator intro!: integral_cong)
  17.143 +    also have "\<dots> = (CLBINT t. (CLINT x | M. ?f' (t, x)))"
  17.144 +      by (auto intro!: integral_cong simp: field_simps exp_diff exp_minus split: split_indicator)
  17.145 +    also have "\<dots> = (CLINT x | M. (CLBINT t. ?f' (t, x)))"
  17.146 +    proof (intro P.Fubini_integral [symmetric] integrableI_bounded_set [where B="b - a"])
  17.147 +      show "emeasure (lborel \<Otimes>\<^sub>M M) ({- T<..<T} \<times> space M) < \<infinity>"
  17.148 +        using \<open>T \<ge> 0\<close> by (subst emeasure_pair_measure_Times) auto
  17.149 +      show "AE x\<in>{- T<..<T} \<times> space M in lborel \<Otimes>\<^sub>M M. cmod (case x of (t, x) \<Rightarrow> ?f' (t, x)) \<le> b - a"
  17.150 +        using Levy_Inversion_aux2[of "x - b" "x - a" for x] `a \<le> b`
  17.151 +        by (intro AE_I [of _ _ "{0} \<times> UNIV"]) (force simp: emeasure_pair_measure_Times)+
  17.152 +    qed (auto split: split_indicator split_indicator_asm)
  17.153 +    also have "\<dots> = (CLINT x | M. (complex_of_real (2 * (sgn (x - a) * 
  17.154 +         Si (T * abs (x - a)) - sgn (x - b) * Si (T * abs (x - b))))))"
  17.155 +       using main_eq by (intro integral_cong, auto)
  17.156 +    also have "\<dots> = complex_of_real (LINT x | M. (2 * (sgn (x - a) * 
  17.157 +         Si (T * abs (x - a)) - sgn (x - b) * Si (T * abs (x - b)))))"
  17.158 +       by (rule integral_complex_of_real)
  17.159 +    finally have "(CLBINT t=-T..T. ?F t * \<phi> t) = 
  17.160 +        complex_of_real (LINT x | M. (2 * (sgn (x - a) * 
  17.161 +         Si (T * abs (x - a)) - sgn (x - b) * Si (T * abs (x - b)))))" .
  17.162 +  } note main_eq2 = this
  17.163 +
  17.164 +  have "(\<lambda>T :: nat. LINT x | M. (2 * (sgn (x - a) * 
  17.165 +         Si (T * abs (x - a)) - sgn (x - b) * Si (T * abs (x - b))))) \<longlonglongrightarrow> 
  17.166 +       (LINT x | M. 2 * pi * indicator {a<..b} x)"
  17.167 +  proof (rule integral_dominated_convergence [where w="\<lambda>x. 4 * B"])
  17.168 +    show "integrable M (\<lambda>x. 4 * B)"
  17.169 +      by (rule integrable_const_bound [of _ "4 * B"]) auto
  17.170 +  next
  17.171 +    let ?S = "\<lambda>n::nat. \<lambda>x. sgn (x - a) * Si (n * \<bar>x - a\<bar>) - sgn (x - b) * Si (n * \<bar>x - b\<bar>)"
  17.172 +    { fix n x
  17.173 +      have "norm (?S n x) \<le> norm (sgn (x - a) * Si (n * \<bar>x - a\<bar>)) + norm (sgn (x - b) * Si (n * \<bar>x - b\<bar>))"
  17.174 +        by (rule norm_triangle_ineq4)
  17.175 +      also have "\<dots> \<le> B + B"
  17.176 +        using Bprop by (intro add_mono) (auto simp: abs_mult abs_sgn_eq)
  17.177 +      finally have "norm (2 * ?S n x) \<le> 4 * B"
  17.178 +        by simp }
  17.179 +    then show "\<And>n. AE x in M. norm (2 * ?S n x) \<le> 4 * B"
  17.180 +      by auto
  17.181 +    have "AE x in M. x \<noteq> a" "AE x in M. x \<noteq> b"
  17.182 +      using prob_eq_0[of "{a}"] prob_eq_0[of "{b}"] `\<mu> {a} = 0` `\<mu> {b} = 0` by (auto simp: \<mu>_def)
  17.183 +    then show "AE x in M. (\<lambda>n. 2 * ?S n x) \<longlonglongrightarrow> 2 * pi * indicator {a<..b} x"
  17.184 +    proof eventually_elim
  17.185 +      fix x assume x: "x \<noteq> a" "x \<noteq> b"
  17.186 +      then have "(\<lambda>n. 2 * (sgn (x - a) * Si (\<bar>x - a\<bar> * n) - sgn (x - b) * Si (\<bar>x - b\<bar> * n)))
  17.187 +          \<longlonglongrightarrow> 2 * (sgn (x - a) * (pi / 2) - sgn (x - b) * (pi / 2))"
  17.188 +        by (intro tendsto_intros filterlim_compose[OF Si_at_top]
  17.189 +            filterlim_tendsto_pos_mult_at_top[OF tendsto_const] filterlim_real_sequentially)
  17.190 +           auto
  17.191 +      also have "(\<lambda>n. 2 * (sgn (x - a) * Si (\<bar>x - a\<bar> * n) - sgn (x - b) * Si (\<bar>x - b\<bar> * n))) = (\<lambda>n. 2 * ?S n x)"
  17.192 +        by (auto simp: ac_simps)
  17.193 +      also have "2 * (sgn (x - a) * (pi / 2) - sgn (x - b) * (pi / 2)) = 2 * pi * indicator {a<..b} x"
  17.194 +        using x `a \<le> b` by (auto split: split_indicator)
  17.195 +      finally show "(\<lambda>n. 2 * ?S n x) \<longlonglongrightarrow> 2 * pi * indicator {a<..b} x" .
  17.196 +    qed
  17.197 +  qed simp_all 
  17.198 +  also have "(LINT x | M. 2 * pi * indicator {a<..b} x) = 2 * pi * \<mu> {a<..b}"
  17.199 +    by (simp add: \<mu>_def)
  17.200 +  finally have "(\<lambda>T. LINT x | M. (2 * (sgn (x - a) * 
  17.201 +         Si (T * abs (x - a)) - sgn (x - b) * Si (T * abs (x - b))))) \<longlonglongrightarrow> 
  17.202 +       2 * pi * \<mu> {a<..b}" .
  17.203 +  with main_eq2 show ?thesis
  17.204 +    by (auto intro!: tendsto_eq_intros)
  17.205 +qed
  17.206 +
  17.207 +theorem Levy_uniqueness:
  17.208 +  fixes M1 M2 :: "real measure"
  17.209 +  assumes "real_distribution M1" "real_distribution M2" and
  17.210 +    "char M1 = char M2"
  17.211 +  shows "M1 = M2"
  17.212 +proof -
  17.213 +  interpret M1: real_distribution M1 by (rule assms)
  17.214 +  interpret M2: real_distribution M2 by (rule assms)
  17.215 +  have "countable ({x. measure M1 {x} \<noteq> 0} \<union> {x. measure M2 {x} \<noteq> 0})"
  17.216 +    by (intro countable_Un M2.countable_support M1.countable_support)
  17.217 +  then have count: "countable {x. measure M1 {x} \<noteq> 0 \<or> measure M2 {x} \<noteq> 0}"
  17.218 +    by (simp add: Un_def)
  17.219 +
  17.220 +  have "cdf M1 = cdf M2"
  17.221 +  proof (rule ext)
  17.222 +    fix x
  17.223 +    from M1.cdf_is_right_cont [of x] have "(cdf M1 \<longlongrightarrow> cdf M1 x) (at_right x)"
  17.224 +      by (simp add: continuous_within)
  17.225 +    from M2.cdf_is_right_cont [of x] have "(cdf M2 \<longlongrightarrow> cdf M2 x) (at_right x)"
  17.226 +      by (simp add: continuous_within)
  17.227 +
  17.228 +    { fix \<epsilon> :: real
  17.229 +      assume "\<epsilon> > 0"
  17.230 +      from `\<epsilon> > 0` `(cdf M1 \<longlongrightarrow> 0) at_bot` `(cdf M2 \<longlongrightarrow> 0) at_bot`
  17.231 +      have "eventually (\<lambda>y. \<bar>cdf M1 y\<bar> < \<epsilon> / 4 \<and> \<bar>cdf M2 y\<bar> < \<epsilon> / 4 \<and> y \<le> x) at_bot"
  17.232 +        by (simp only: tendsto_iff dist_real_def diff_0_right eventually_conj eventually_le_at_bot)
  17.233 +      then obtain M where "\<And>y. y \<le> M \<Longrightarrow> \<bar>cdf M1 y\<bar> < \<epsilon> / 4" "\<And>y. y \<le> M \<Longrightarrow> \<bar>cdf M2 y\<bar> < \<epsilon> / 4" "M \<le> x"
  17.234 +        unfolding eventually_at_bot_linorder by auto
  17.235 +      with open_minus_countable[OF count, of "{..< M}"] obtain a where
  17.236 +        "measure M1 {a} = 0" "measure M2 {a} = 0" "a < M" "a \<le> x" "\<bar>cdf M1 a\<bar> < \<epsilon> / 4" "\<bar>cdf M2 a\<bar> < \<epsilon> / 4"
  17.237 +        by auto
  17.238 +
  17.239 +      from `\<epsilon> > 0` `(cdf M1 \<longlongrightarrow> cdf M1 x) (at_right x)` `(cdf M2 \<longlongrightarrow> cdf M2 x) (at_right x)` 
  17.240 +      have "eventually (\<lambda>y. \<bar>cdf M1 y - cdf M1 x\<bar> < \<epsilon> / 4 \<and> \<bar>cdf M2 y - cdf M2 x\<bar> < \<epsilon> / 4 \<and> x < y) (at_right x)"
  17.241 +        by (simp only: tendsto_iff dist_real_def eventually_conj eventually_at_right_less)
  17.242 +      then obtain N where "N > x" "\<And>y. x < y \<Longrightarrow> y < N \<Longrightarrow> \<bar>cdf M1 y - cdf M1 x\<bar> < \<epsilon> / 4"
  17.243 +        "\<And>y. x < y \<Longrightarrow> y < N \<Longrightarrow> \<bar>cdf M2 y - cdf M2 x\<bar> < \<epsilon> / 4" "\<And>y. x < y \<Longrightarrow> y < N \<Longrightarrow> x < y"
  17.244 +        by (auto simp add: eventually_at_right[OF less_add_one])
  17.245 +      with open_minus_countable[OF count, of "{x <..< N}"] obtain b where "x < b" "b < N"
  17.246 +          "measure M1 {b} = 0" "measure M2 {b} = 0" "\<bar>cdf M2 x - cdf M2 b\<bar> < \<epsilon> / 4" "\<bar>cdf M1 x - cdf M1 b\<bar> < \<epsilon> / 4"
  17.247 +        by (auto simp: abs_minus_commute)
  17.248 +      from `a \<le> x` `x < b` have "a < b" "a \<le> b" by auto
  17.249 +
  17.250 +      from `char M1 = char M2`
  17.251 +        M1.Levy_Inversion [OF `a \<le> b` `measure M1 {a} = 0`  `measure M1 {b} = 0`]
  17.252 +        M2.Levy_Inversion [OF `a \<le> b` `measure M2 {a} = 0` `measure M2 {b} = 0`]
  17.253 +      have "complex_of_real (measure M1 {a<..b}) = complex_of_real (measure M2 {a<..b})"
  17.254 +        by (intro LIMSEQ_unique) auto
  17.255 +      then have "measure M1 {a<..b} = measure M2 {a<..b}" by auto
  17.256 +      then have *: "cdf M1 b - cdf M1 a = cdf M2 b - cdf M2 a"
  17.257 +        unfolding M1.cdf_diff_eq [OF `a < b`] M2.cdf_diff_eq [OF `a < b`] .
  17.258 +
  17.259 +      have "abs (cdf M1 x - (cdf M1 b - cdf M1 a)) = abs (cdf M1 x - cdf M1 b + cdf M1 a)"
  17.260 +        by simp
  17.261 +      also have "\<dots> \<le> abs (cdf M1 x - cdf M1 b) + abs (cdf M1 a)"
  17.262 +        by (rule abs_triangle_ineq)
  17.263 +      also have "\<dots> \<le> \<epsilon> / 4 + \<epsilon> / 4"
  17.264 +        by (intro add_mono less_imp_le \<open>\<bar>cdf M1 a\<bar> < \<epsilon> / 4\<close> \<open>\<bar>cdf M1 x - cdf M1 b\<bar> < \<epsilon> / 4\<close>)
  17.265 +      finally have 1: "abs (cdf M1 x - (cdf M1 b - cdf M1 a)) \<le> \<epsilon> / 2" by simp
  17.266 +
  17.267 +      have "abs (cdf M2 x - (cdf M2 b - cdf M2 a)) = abs (cdf M2 x - cdf M2 b + cdf M2 a)"
  17.268 +        by simp
  17.269 +      also have "\<dots> \<le> abs (cdf M2 x - cdf M2 b) + abs (cdf M2 a)"
  17.270 +        by (rule abs_triangle_ineq)
  17.271 +      also have "\<dots> \<le> \<epsilon> / 4 + \<epsilon> / 4"
  17.272 +        by (intro add_mono less_imp_le \<open>\<bar>cdf M2 x - cdf M2 b\<bar> < \<epsilon> / 4\<close> \<open>\<bar>cdf M2 a\<bar> < \<epsilon> / 4\<close>)
  17.273 +      finally have 2: "abs (cdf M2 x - (cdf M2 b - cdf M2 a)) \<le> \<epsilon> / 2" by simp
  17.274 +
  17.275 +      have "abs (cdf M1 x - cdf M2 x) = abs ((cdf M1 x - (cdf M1 b - cdf M1 a)) - 
  17.276 +          (cdf M2 x - (cdf M2 b - cdf M2 a)))" by (subst *, simp)
  17.277 +      also have "\<dots> \<le> abs (cdf M1 x - (cdf M1 b - cdf M1 a)) + 
  17.278 +          abs (cdf M2 x - (cdf M2 b - cdf M2 a))" by (rule abs_triangle_ineq4)
  17.279 +      also have "\<dots> \<le> \<epsilon> / 2 + \<epsilon> / 2" by (rule add_mono [OF 1 2])
  17.280 +      finally have "abs (cdf M1 x - cdf M2 x) \<le> \<epsilon>" by simp }
  17.281 +    then show "cdf M1 x = cdf M2 x"
  17.282 +      by (metis abs_le_zero_iff dense_ge eq_iff_diff_eq_0)
  17.283 +  qed
  17.284 +  thus ?thesis
  17.285 +    by (rule cdf_unique [OF `real_distribution M1` `real_distribution M2`])
  17.286 +qed
  17.287 +
  17.288 +
  17.289 +subsection \<open>The Levy continuity theorem\<close>
  17.290 +
  17.291 +theorem levy_continuity1:
  17.292 +  fixes M :: "nat \<Rightarrow> real measure" and M' :: "real measure"
  17.293 +  assumes "\<And>n. real_distribution (M n)" "real_distribution M'" "weak_conv_m M M'"
  17.294 +  shows "(\<lambda>n. char (M n) t) \<longlonglongrightarrow> char M' t"
  17.295 +  unfolding char_def using assms by (rule weak_conv_imp_integral_bdd_continuous_conv) auto
  17.296 +
  17.297 +theorem levy_continuity:
  17.298 +  fixes M :: "nat \<Rightarrow> real measure" and M' :: "real measure"
  17.299 +  assumes real_distr_M : "\<And>n. real_distribution (M n)"
  17.300 +    and real_distr_M': "real_distribution M'"
  17.301 +    and char_conv: "\<And>t. (\<lambda>n. char (M n) t) \<longlonglongrightarrow> char M' t" 
  17.302 +  shows "weak_conv_m M M'"
  17.303 +proof -
  17.304 +  interpret Mn: real_distribution "M n" for n by fact
  17.305 +  interpret M': real_distribution M' by fact
  17.306 +
  17.307 +  have *: "\<And>u x. u > 0 \<Longrightarrow> x \<noteq> 0 \<Longrightarrow> (CLBINT t:{-u..u}. 1 - iexp (t * x)) = 
  17.308 +      2 * (u  - sin (u * x) / x)"
  17.309 +  proof -
  17.310 +    fix u :: real and x :: real
  17.311 +    assume "u > 0" and "x \<noteq> 0"
  17.312 +    hence "(CLBINT t:{-u..u}. 1 - iexp (t * x)) = (CLBINT t=-u..u. 1 - iexp (t * x))"
  17.313 +      by (subst interval_integral_Icc, auto)
  17.314 +    also have "\<dots> = (CLBINT t=-u..0. 1 - iexp (t * x)) + (CLBINT t=0..u. 1 - iexp (t * x))"
  17.315 +      using `u > 0`
  17.316 +      apply (subst interval_integral_sum)
  17.317 +      apply (simp add: min_absorb1 min_absorb2 max_absorb1 max_absorb2)
  17.318 +      apply (rule interval_integrable_isCont)
  17.319 +      apply auto
  17.320 +      done
  17.321 +    also have "\<dots> = (CLBINT t=ereal 0..u. 1 - iexp (t * -x)) + (CLBINT t=ereal 0..u. 1 - iexp (t * x))"
  17.322 +      apply (subgoal_tac "0 = ereal 0", erule ssubst)
  17.323 +      by (subst interval_integral_reflect, auto)
  17.324 +    also have "\<dots> = (LBINT t=ereal 0..u. 2 - 2 * cos (t * x))"
  17.325 +      apply (subst interval_lebesgue_integral_add (2) [symmetric])
  17.326 +      apply ((rule interval_integrable_isCont, auto)+) [2]
  17.327 +      unfolding exp_Euler cos_of_real
  17.328 +      apply (simp add: of_real_mult interval_lebesgue_integral_of_real[symmetric])
  17.329 +      done
  17.330 +    also have "\<dots> = 2 * u - 2 * sin (u * x) / x"
  17.331 +      by (subst interval_lebesgue_integral_diff)
  17.332 +         (auto intro!: interval_integrable_isCont
  17.333 +               simp: interval_lebesgue_integral_of_real integral_cos [OF `x \<noteq> 0`] mult.commute[of _ x])
  17.334 +    finally show "(CLBINT t:{-u..u}. 1 - iexp (t * x)) = 2 * (u  - sin (u * x) / x)"
  17.335 +      by (simp add: field_simps)
  17.336 +  qed
  17.337 +  have main_bound: "\<And>u n. u > 0 \<Longrightarrow> Re (CLBINT t:{-u..u}. 1 - char (M n) t) \<ge> 
  17.338 +    u * measure (M n) {x. abs x \<ge> 2 / u}"
  17.339 +  proof -
  17.340 +    fix u :: real and n
  17.341 +    assume "u > 0"
  17.342 +    interpret P: pair_sigma_finite "M n" lborel ..
  17.343 +    (* TODO: put this in the real_distribution locale as a simp rule? *)
  17.344 +    have Mn1 [simp]: "measure (M n) UNIV = 1" by (metis Mn.prob_space Mn.space_eq_univ)
  17.345 +    (* TODO: make this automatic somehow? *)
  17.346 +    have Mn2 [simp]: "\<And>x. complex_integrable (M n) (\<lambda>t. exp (\<i> * complex_of_real (x * t)))"
  17.347 +      by (rule Mn.integrable_const_bound [where B = 1], auto)
  17.348 +    have Mn3: "set_integrable (M n \<Otimes>\<^sub>M lborel) (UNIV \<times> {- u..u}) (\<lambda>a. 1 - exp (\<i> * complex_of_real (snd a * fst a)))"
  17.349 +      using `0 < u`
  17.350 +      by (intro integrableI_bounded_set_indicator [where B="2"])
  17.351 +         (auto simp: lborel.emeasure_pair_measure_Times split: split_indicator
  17.352 +               intro!: order_trans [OF norm_triangle_ineq4])
  17.353 +    have "(CLBINT t:{-u..u}. 1 - char (M n) t) = 
  17.354 +        (CLBINT t:{-u..u}. (CLINT x | M n. 1 - iexp (t * x)))"
  17.355 +      unfolding char_def by (rule set_lebesgue_integral_cong, auto simp del: of_real_mult)
  17.356 +    also have "\<dots> = (CLBINT t. (CLINT x | M n. indicator {-u..u} t *\<^sub>R (1 - iexp (t * x))))"
  17.357 +      by (rule integral_cong) (auto split: split_indicator)
  17.358 +    also have "\<dots> = (CLINT x | M n. (CLBINT t:{-u..u}. 1 - iexp (t * x)))"
  17.359 +      using Mn3 by (subst P.Fubini_integral) (auto simp: indicator_times split_beta')
  17.360 +    also have "\<dots> = (CLINT x | M n. (if x = 0 then 0 else 2 * (u  - sin (u * x) / x)))"
  17.361 +      using `u > 0` by (intro integral_cong, auto simp add: * simp del: of_real_mult)
  17.362 +    also have "\<dots> = (LINT x | M n. (if x = 0 then 0 else 2 * (u  - sin (u * x) / x)))"
  17.363 +      by (rule integral_complex_of_real)
  17.364 +    finally have "Re (CLBINT t:{-u..u}. 1 - char (M n) t) = 
  17.365 +       (LINT x | M n. (if x = 0 then 0 else 2 * (u  - sin (u * x) / x)))" by simp
  17.366 +    also have "\<dots> \<ge> (LINT x : {x. abs x \<ge> 2 / u} | M n. u)"
  17.367 +    proof -
  17.368 +      have "complex_integrable (M n) (\<lambda>x. CLBINT t:{-u..u}. 1 - iexp (snd (x, t) * fst (x, t)))"
  17.369 +        using Mn3 by (intro P.integrable_fst) (simp add: indicator_times split_beta')
  17.370 +      hence "complex_integrable (M n) (\<lambda>x. if x = 0 then 0 else 2 * (u  - sin (u * x) / x))"
  17.371 +        using `u > 0` by (subst integrable_cong) (auto simp add: * simp del: of_real_mult)
  17.372 +      hence **: "integrable (M n) (\<lambda>x. if x = 0 then 0 else 2 * (u  - sin (u * x) / x))"
  17.373 +        unfolding complex_of_real_integrable_eq .
  17.374 +      have "2 * sin x \<le> x" if "2 \<le> x" for x :: real
  17.375 +        by (rule order_trans[OF _ \<open>2 \<le> x\<close>]) auto
  17.376 +      moreover have "x \<le> 2 * sin x" if "x \<le> - 2" for x :: real
  17.377 +        by (rule order_trans[OF \<open>x \<le> - 2\<close>]) auto
  17.378 +      moreover have "x < 0 \<Longrightarrow> x \<le> sin x" for x :: real
  17.379 +        using sin_x_le_x[of "-x"] by simp
  17.380 +      ultimately show ?thesis
  17.381 +        using `u > 0`
  17.382 +        by (intro integral_mono [OF _ **])
  17.383 +           (auto simp: divide_simps sin_x_le_x mult.commute[of u] mult_neg_pos split: split_indicator)
  17.384 +    qed
  17.385 +    also (xtrans) have "(LINT x : {x. abs x \<ge> 2 / u} | M n. u) = 
  17.386 +        u * measure (M n) {x. abs x \<ge> 2 / u}"
  17.387 +      by (simp add: Mn.emeasure_eq_measure)
  17.388 +    finally show "Re (CLBINT t:{-u..u}. 1 - char (M n) t) \<ge> u * measure (M n) {x. abs x \<ge> 2 / u}" .
  17.389 +  qed
  17.390 +
  17.391 +  have tight_aux: "\<And>\<epsilon>. \<epsilon> > 0 \<Longrightarrow> \<exists>a b. a < b \<and> (\<forall>n. 1 - \<epsilon> < measure (M n) {a<..b})"
  17.392 +  proof -
  17.393 +    fix \<epsilon> :: real
  17.394 +    assume "\<epsilon> > 0"
  17.395 +    note M'.isCont_char [of 0]
  17.396 +    hence "\<exists>d>0. \<forall>t. abs t < d \<longrightarrow> cmod (char M' t - 1) < \<epsilon> / 4"
  17.397 +      apply (subst (asm) continuous_at_eps_delta)
  17.398 +      apply (drule_tac x = "\<epsilon> / 4" in spec)
  17.399 +      using `\<epsilon> > 0` by (auto simp add: dist_real_def dist_complex_def M'.char_zero)
  17.400 +    then obtain d where "d > 0 \<and> (\<forall>t. (abs t < d \<longrightarrow> cmod (char M' t - 1) < \<epsilon> / 4))" ..
  17.401 +    hence d0: "d > 0" and d1: "\<And>t. abs t < d \<Longrightarrow> cmod (char M' t - 1) < \<epsilon> / 4" by auto
  17.402 +    have 1: "\<And>x. cmod (1 - char M' x) \<le> 2"
  17.403 +      by (rule order_trans [OF norm_triangle_ineq4], auto simp add: M'.cmod_char_le_1)
  17.404 +    then have 2: "\<And>u v. complex_set_integrable lborel {u..v} (\<lambda>x. 1 - char M' x)"
  17.405 +      by (intro integrableI_bounded_set_indicator[where B=2]) (auto simp: emeasure_lborel_Icc_eq)
  17.406 +    have 3: "\<And>u v. set_integrable lborel {u..v} (\<lambda>x. cmod (1 - char M' x))"
  17.407 +      by (intro borel_integrable_compact[OF compact_Icc] continuous_at_imp_continuous_on
  17.408 +                continuous_intros ballI M'.isCont_char continuous_intros)
  17.409 +    have "cmod (CLBINT t:{-d/2..d/2}. 1 - char M' t) \<le> LBINT t:{-d/2..d/2}. cmod (1 - char M' t)"
  17.410 +      using integral_norm_bound[OF 2] by simp
  17.411 +    also have "\<dots> \<le> LBINT t:{-d/2..d/2}. \<epsilon> / 4"
  17.412 +      apply (rule integral_mono [OF 3])
  17.413 +      apply (simp add: emeasure_lborel_Icc_eq)
  17.414 +      apply (case_tac "x \<in> {-d/2..d/2}", auto)
  17.415 +      apply (subst norm_minus_commute)
  17.416 +      apply (rule less_imp_le)
  17.417 +      apply (rule d1 [simplified])
  17.418 +      using d0 by auto
  17.419 +    also with d0 have "\<dots> = d * \<epsilon> / 4"
  17.420 +      by simp
  17.421 +    finally have bound: "cmod (CLBINT t:{-d/2..d/2}. 1 - char M' t) \<le> d * \<epsilon> / 4" .
  17.422 +    { fix n x
  17.423 +      have "cmod (1 - char (M n) x) \<le> 2"
  17.424 +        by (rule order_trans [OF norm_triangle_ineq4], auto simp add: Mn.cmod_char_le_1)
  17.425 +    } note bd1 = this
  17.426 +    have "(\<lambda>n. CLBINT t:{-d/2..d/2}. 1 - char (M n) t) \<longlonglongrightarrow> (CLBINT t:{-d/2..d/2}. 1 - char M' t)"
  17.427 +      using bd1
  17.428 +      apply (intro integral_dominated_convergence[where w="\<lambda>x. indicator {-d/2..d/2} x *\<^sub>R 2"])
  17.429 +      apply (auto intro!: char_conv tendsto_intros 
  17.430 +                  simp: emeasure_lborel_Icc_eq
  17.431 +                  split: split_indicator)
  17.432 +      done
  17.433 +    hence "eventually (\<lambda>n. cmod ((CLBINT t:{-d/2..d/2}. 1 - char (M n) t) -
  17.434 +        (CLBINT t:{-d/2..d/2}. 1 - char M' t)) < d * \<epsilon> / 4) sequentially"
  17.435 +      using d0 `\<epsilon> > 0` apply (subst (asm) tendsto_iff)
  17.436 +      by (subst (asm) dist_complex_def, drule spec, erule mp, auto)
  17.437 +    hence "\<exists>N. \<forall>n \<ge> N. cmod ((CLBINT t:{-d/2..d/2}. 1 - char (M n) t) -
  17.438 +        (CLBINT t:{-d/2..d/2}. 1 - char M' t)) < d * \<epsilon> / 4" by (simp add: eventually_sequentially)
  17.439 +    then guess N ..
  17.440 +    hence N: "\<And>n. n \<ge> N \<Longrightarrow> cmod ((CLBINT t:{-d/2..d/2}. 1 - char (M n) t) -
  17.441 +        (CLBINT t:{-d/2..d/2}. 1 - char M' t)) < d * \<epsilon> / 4" by auto
  17.442 +    { fix n
  17.443 +      assume "n \<ge> N"
  17.444 +      have "cmod (CLBINT t:{-d/2..d/2}. 1 - char (M n) t) = 
  17.445 +        cmod ((CLBINT t:{-d/2..d/2}. 1 - char (M n) t) - (CLBINT t:{-d/2..d/2}. 1 - char M' t)
  17.446 +          + (CLBINT t:{-d/2..d/2}. 1 - char M' t))" by simp
  17.447 +      also have "\<dots> \<le> cmod ((CLBINT t:{-d/2..d/2}. 1 - char (M n) t) - 
  17.448 +          (CLBINT t:{-d/2..d/2}. 1 - char M' t)) + cmod(CLBINT t:{-d/2..d/2}. 1 - char M' t)"
  17.449 +        by (rule norm_triangle_ineq)
  17.450 +      also have "\<dots> < d * \<epsilon> / 4 + d * \<epsilon> / 4" 
  17.451 +        by (rule add_less_le_mono [OF N [OF `n \<ge> N`] bound])
  17.452 +      also have "\<dots> = d * \<epsilon> / 2" by auto
  17.453 +      finally have "cmod (CLBINT t:{-d/2..d/2}. 1 - char (M n) t) < d * \<epsilon> / 2" .
  17.454 +      hence "d * \<epsilon> / 2 > Re (CLBINT t:{-d/2..d/2}. 1 - char (M n) t)"
  17.455 +        by (rule order_le_less_trans [OF complex_Re_le_cmod])
  17.456 +      hence "d * \<epsilon> / 2 > Re (CLBINT t:{-(d/2)..d/2}. 1 - char (M n) t)" (is "_ > ?lhs") by simp
  17.457 +      also have "?lhs \<ge> (d / 2) * measure (M n) {x. abs x \<ge> 2 / (d / 2)}" 
  17.458 +        using d0 by (intro main_bound, simp)
  17.459 +      finally (xtrans) have "d * \<epsilon> / 2 > (d / 2) * measure (M n) {x. abs x \<ge> 2 / (d / 2)}" .
  17.460 +      with d0 `\<epsilon> > 0` have "\<epsilon> > measure (M n) {x. abs x \<ge> 2 / (d / 2)}" by (simp add: field_simps)
  17.461 +      hence "\<epsilon> > 1 - measure (M n) (UNIV - {x. abs x \<ge> 2 / (d / 2)})"
  17.462 +        apply (subst Mn.borel_UNIV [symmetric])
  17.463 +        by (subst Mn.prob_compl, auto)
  17.464 +      also have "UNIV - {x. abs x \<ge> 2 / (d / 2)} = {x. -(4 / d) < x \<and> x < (4 / d)}"
  17.465 +        using d0 apply (auto simp add: field_simps)
  17.466 +        (* very annoying -- this should be automatic *)
  17.467 +        apply (case_tac "x \<ge> 0", auto simp add: field_simps)
  17.468 +        apply (subgoal_tac "0 \<le> x * d", arith, rule mult_nonneg_nonneg, auto)
  17.469 +        apply (case_tac "x \<ge> 0", auto simp add: field_simps)
  17.470 +        apply (subgoal_tac "x * d \<le> 0", arith)
  17.471 +        apply (rule mult_nonpos_nonneg, auto)
  17.472 +        by (case_tac "x \<ge> 0", auto simp add: field_simps)
  17.473 +      finally have "measure (M n) {x. -(4 / d) < x \<and> x < (4 / d)} > 1 - \<epsilon>"
  17.474 +        by auto
  17.475 +    } note 6 = this
  17.476 +    { fix n :: nat
  17.477 +      have *: "(UN (k :: nat). {- real k<..real k}) = UNIV"
  17.478 +        by (auto, metis leI le_less_trans less_imp_le minus_less_iff reals_Archimedean2)
  17.479 +      have "(\<lambda>k. measure (M n) {- real k<..real k}) \<longlonglongrightarrow> 
  17.480 +          measure (M n) (UN (k :: nat). {- real k<..real k})"
  17.481 +        by (rule Mn.finite_Lim_measure_incseq, auto simp add: incseq_def)
  17.482 +      hence "(\<lambda>k. measure (M n) {- real k<..real k}) \<longlonglongrightarrow> 1"
  17.483 +        using Mn.prob_space unfolding * Mn.borel_UNIV by simp
  17.484 +      hence "eventually (\<lambda>k. measure (M n) {- real k<..real k} > 1 - \<epsilon>) sequentially"
  17.485 +        apply (elim order_tendstoD (1))
  17.486 +        using `\<epsilon> > 0` by auto
  17.487 +    } note 7 = this
  17.488 +    { fix n :: nat
  17.489 +      have "eventually (\<lambda>k. \<forall>m < n. measure (M m) {- real k<..real k} > 1 - \<epsilon>) sequentially"
  17.490 +        (is "?P n")
  17.491 +      proof (induct n)
  17.492 +        case (Suc n) with 7[of n] show ?case
  17.493 +          by eventually_elim (auto simp add: less_Suc_eq)
  17.494 +      qed simp
  17.495 +    } note 8 = this
  17.496 +    from 8 [of N] have "\<exists>K :: nat. \<forall>k \<ge> K. \<forall>m<N. 1 - \<epsilon> < 
  17.497 +        Sigma_Algebra.measure (M m) {- real k<..real k}"
  17.498 +      by (auto simp add: eventually_sequentially)
  17.499 +    hence "\<exists>K :: nat. \<forall>m<N. 1 - \<epsilon> < Sigma_Algebra.measure (M m) {- real K<..real K}" by auto
  17.500 +    then obtain K :: nat where 
  17.501 +      "\<forall>m<N. 1 - \<epsilon> < Sigma_Algebra.measure (M m) {- real K<..real K}" ..
  17.502 +    hence K: "\<And>m. m < N \<Longrightarrow> 1 - \<epsilon> < Sigma_Algebra.measure (M m) {- real K<..real K}"
  17.503 +      by auto
  17.504 +    let ?K' = "max K (4 / d)"
  17.505 +    have "-?K' < ?K' \<and> (\<forall>n. 1 - \<epsilon> < measure (M n) {-?K'<..?K'})"
  17.506 +      using d0 apply auto
  17.507 +      apply (rule max.strict_coboundedI2, auto)
  17.508 +    proof -
  17.509 +      fix n
  17.510 +      show " 1 - \<epsilon> < measure (M n) {- max (real K) (4 / d)<..max (real K) (4 / d)}"      
  17.511 +        apply (case_tac "n < N")
  17.512 +        apply (rule order_less_le_trans)
  17.513 +        apply (erule K)
  17.514 +        apply (rule Mn.finite_measure_mono, auto)
  17.515 +        apply (rule order_less_le_trans)
  17.516 +        apply (rule 6, erule leI)
  17.517 +        by (rule Mn.finite_measure_mono, auto)
  17.518 +    qed 
  17.519 +    thus "\<exists>a b. a < b \<and> (\<forall>n. 1 - \<epsilon> < measure (M n) {a<..b})" by (intro exI)
  17.520 +  qed
  17.521 +  have tight: "tight M"
  17.522 +    by (auto simp: tight_def intro: assms tight_aux)
  17.523 +  show ?thesis
  17.524 +  proof (rule tight_subseq_weak_converge [OF real_distr_M real_distr_M' tight])
  17.525 +    fix s \<nu>
  17.526 +    assume s: "subseq s"
  17.527 +    assume nu: "weak_conv_m (M \<circ> s) \<nu>"
  17.528 +    assume *: "real_distribution \<nu>"
  17.529 +    have 2: "\<And>n. real_distribution ((M \<circ> s) n)" unfolding comp_def by (rule assms)
  17.530 +    have 3: "\<And>t. (\<lambda>n. char ((M \<circ> s) n) t) \<longlonglongrightarrow> char \<nu> t" by (intro levy_continuity1 [OF 2 * nu])
  17.531 +    have 4: "\<And>t. (\<lambda>n. char ((M \<circ> s) n) t) = ((\<lambda>n. char (M n) t) \<circ> s)" by (rule ext, simp)
  17.532 +    have 5: "\<And>t. (\<lambda>n. char ((M \<circ> s) n) t) \<longlonglongrightarrow> char M' t"
  17.533 +      by (subst 4, rule lim_subseq [OF s], rule assms)
  17.534 +    hence "char \<nu> = char M'" by (intro ext, intro LIMSEQ_unique [OF 3 5])
  17.535 +    hence "\<nu> = M'" by (rule Levy_uniqueness [OF * `real_distribution M'`])
  17.536 +    thus "weak_conv_m (M \<circ> s) M'" 
  17.537 +      apply (elim subst)
  17.538 +      by (rule nu)  
  17.539 +  qed
  17.540 +qed
  17.541 +
  17.542 +end
    18.1 --- a/src/HOL/Probability/Nonnegative_Lebesgue_Integration.thy	Wed Jan 06 13:04:31 2016 +0100
    18.2 +++ b/src/HOL/Probability/Nonnegative_Lebesgue_Integration.thy	Wed Jan 06 12:18:53 2016 +0100
    18.3 @@ -1186,6 +1186,25 @@
    18.4    finally show ?thesis .
    18.5  qed
    18.6  
    18.7 +lemma nn_integral_indicator_singleton[simp]:
    18.8 +  assumes [measurable]: "{y} \<in> sets M"
    18.9 +  shows "(\<integral>\<^sup>+x. f x * indicator {y} x \<partial>M) = max 0 (f y) * emeasure M {y}"
   18.10 +proof-
   18.11 +  have "(\<integral>\<^sup>+x. f x * indicator {y} x \<partial>M) = (\<integral>\<^sup>+x. max 0 (f y) * indicator {y} x \<partial>M)"
   18.12 +    by (subst nn_integral_max_0[symmetric]) (auto intro!: nn_integral_cong split: split_indicator)
   18.13 +  then show ?thesis
   18.14 +    by (simp add: nn_integral_cmult)
   18.15 +qed
   18.16 +
   18.17 +lemma nn_integral_set_ereal:
   18.18 +  "(\<integral>\<^sup>+x. ereal (f x) * indicator A x \<partial>M) = (\<integral>\<^sup>+x. ereal (f x * indicator A x) \<partial>M)"
   18.19 +  by (rule nn_integral_cong) (simp split: split_indicator)
   18.20 +
   18.21 +lemma nn_integral_indicator_singleton'[simp]:
   18.22 +  assumes [measurable]: "{y} \<in> sets M"
   18.23 +  shows "(\<integral>\<^sup>+x. ereal (f x * indicator {y} x) \<partial>M) = max 0 (f y) * emeasure M {y}"
   18.24 +  by (subst nn_integral_set_ereal[symmetric]) (simp add: nn_integral_indicator_singleton)
   18.25 +
   18.26  lemma nn_integral_add:
   18.27    assumes f: "f \<in> borel_measurable M" "AE x in M. 0 \<le> f x"
   18.28    and g: "g \<in> borel_measurable M" "AE x in M. 0 \<le> g x"
   18.29 @@ -1854,7 +1873,7 @@
   18.30      by (subst nn_integral_setsum)
   18.31         (simp_all add: AE_count_space ereal_zero_le_0_iff less_imp_le)
   18.32    also have "\<dots> = (\<Sum>a|a\<in>A \<and> 0 < f a. f a)"
   18.33 -    by (auto intro!: setsum.cong simp: nn_integral_cmult_indicator one_ereal_def[symmetric])
   18.34 +    by (auto intro!: setsum.cong simp: one_ereal_def[symmetric] max_def)
   18.35    finally show ?thesis by (simp add: nn_integral_max_0)
   18.36  qed
   18.37  
   18.38 @@ -1890,7 +1909,7 @@
   18.39    from f have "(\<integral>\<^sup>+x. f x * indicator A x \<partial>M) = (\<integral>\<^sup>+x. (\<Sum>a\<in>A. f a * indicator {a} x) \<partial>M)"
   18.40      by (intro nn_integral_cong) (auto simp: indicator_def if_distrib[where f="\<lambda>a. x * a" for x] setsum.If_cases)
   18.41    also have "\<dots> = (\<Sum>a\<in>A. f a * emeasure M {a})"
   18.42 -    using nn by (subst nn_integral_setsum) (auto simp: nn_integral_cmult_indicator)
   18.43 +    using nn by (subst nn_integral_setsum) (auto simp: max_def)
   18.44    finally show ?thesis .
   18.45  qed
   18.46  
   18.47 @@ -1912,7 +1931,7 @@
   18.48    also have "\<dots> = (\<Sum>j. (\<integral>\<^sup>+i. f j * indicator {j} i \<partial>count_space UNIV))"
   18.49      by (rule nn_integral_suminf) (auto simp: nonneg)
   18.50    also have "\<dots> = (\<Sum>j. f j)"
   18.51 -    by (simp add: nonneg nn_integral_cmult_indicator one_ereal_def[symmetric])
   18.52 +    by (simp add: nonneg one_ereal_def[symmetric] max_def)
   18.53    finally show ?thesis .
   18.54  qed
   18.55  
    19.1 --- a/src/HOL/Probability/Probability.thy	Wed Jan 06 13:04:31 2016 +0100
    19.2 +++ b/src/HOL/Probability/Probability.thy	Wed Jan 06 12:18:53 2016 +0100
    19.3 @@ -7,12 +7,10 @@
    19.4    Discrete_Topology
    19.5    Complete_Measure
    19.6    Projective_Limit
    19.7 -  Independent_Family
    19.8 -  Distributions
    19.9    Probability_Mass_Function
   19.10    Stream_Space
   19.11    Embed_Measure
   19.12 +  Central_Limit_Theorem
   19.13  begin
   19.14  
   19.15  end
   19.16 -
    20.1 --- a/src/HOL/Probability/Probability_Mass_Function.thy	Wed Jan 06 13:04:31 2016 +0100
    20.2 +++ b/src/HOL/Probability/Probability_Mass_Function.thy	Wed Jan 06 12:18:53 2016 +0100
    20.3 @@ -32,48 +32,6 @@
    20.4  lemma ereal_divide': "b \<noteq> 0 \<Longrightarrow> ereal (a / b) = ereal a / ereal b"
    20.5    using ereal_divide[of a b] by simp
    20.6  
    20.7 -lemma (in finite_measure) countable_support:
    20.8 -  "countable {x. measure M {x} \<noteq> 0}"
    20.9 -proof cases
   20.10 -  assume "measure M (space M) = 0"
   20.11 -  with bounded_measure measure_le_0_iff have "{x. measure M {x} \<noteq> 0} = {}"
   20.12 -    by auto
   20.13 -  then show ?thesis
   20.14 -    by simp
   20.15 -next
   20.16 -  let ?M = "measure M (space M)" and ?m = "\<lambda>x. measure M {x}"
   20.17 -  assume "?M \<noteq> 0"
   20.18 -  then have *: "{x. ?m x \<noteq> 0} = (\<Union>n. {x. ?M / Suc n < ?m x})"
   20.19 -    using reals_Archimedean[of "?m x / ?M" for x]
   20.20 -    by (auto simp: field_simps not_le[symmetric] measure_nonneg divide_le_0_iff measure_le_0_iff)
   20.21 -  have **: "\<And>n. finite {x. ?M / Suc n < ?m x}"
   20.22 -  proof (rule ccontr)
   20.23 -    fix n assume "infinite {x. ?M / Suc n < ?m x}" (is "infinite ?X")
   20.24 -    then obtain X where "finite X" "card X = Suc (Suc n)" "X \<subseteq> ?X"
   20.25 -      by (metis infinite_arbitrarily_large)
   20.26 -    from this(3) have *: "\<And>x. x \<in> X \<Longrightarrow> ?M / Suc n \<le> ?m x"
   20.27 -      by auto
   20.28 -    { fix x assume "x \<in> X"
   20.29 -      from \<open>?M \<noteq> 0\<close> *[OF this] have "?m x \<noteq> 0" by (auto simp: field_simps measure_le_0_iff)
   20.30 -      then have "{x} \<in> sets M" by (auto dest: measure_notin_sets) }
   20.31 -    note singleton_sets = this
   20.32 -    have "?M < (\<Sum>x\<in>X. ?M / Suc n)"
   20.33 -      using \<open>?M \<noteq> 0\<close>
   20.34 -      by (simp add: \<open>card X = Suc (Suc n)\<close> of_nat_Suc field_simps less_le measure_nonneg)
   20.35 -    also have "\<dots> \<le> (\<Sum>x\<in>X. ?m x)"
   20.36 -      by (rule setsum_mono) fact
   20.37 -    also have "\<dots> = measure M (\<Union>x\<in>X. {x})"
   20.38 -      using singleton_sets \<open>finite X\<close>
   20.39 -      by (intro finite_measure_finite_Union[symmetric]) (auto simp: disjoint_family_on_def)
   20.40 -    finally have "?M < measure M (\<Union>x\<in>X. {x})" .
   20.41 -    moreover have "measure M (\<Union>x\<in>X. {x}) \<le> ?M"
   20.42 -      using singleton_sets[THEN sets.sets_into_space] by (intro finite_measure_mono) auto
   20.43 -    ultimately show False by simp
   20.44 -  qed
   20.45 -  show ?thesis
   20.46 -    unfolding * by (intro countable_UN countableI_type countable_finite[OF **])
   20.47 -qed
   20.48 -
   20.49  lemma (in finite_measure) AE_support_countable:
   20.50    assumes [simp]: "sets M = UNIV"
   20.51    shows "(AE x in M. measure M {x} \<noteq> 0) \<longleftrightarrow> (\<exists>S. countable S \<and> (AE x in M. x \<in> S))"
    21.1 --- a/src/HOL/Probability/Probability_Measure.thy	Wed Jan 06 13:04:31 2016 +0100
    21.2 +++ b/src/HOL/Probability/Probability_Measure.thy	Wed Jan 06 12:18:53 2016 +0100
    21.3 @@ -9,6 +9,48 @@
    21.4    imports Lebesgue_Measure Radon_Nikodym
    21.5  begin
    21.6  
    21.7 +lemma (in finite_measure) countable_support:
    21.8 +  "countable {x. measure M {x} \<noteq> 0}"
    21.9 +proof cases
   21.10 +  assume "measure M (space M) = 0"
   21.11 +  with bounded_measure measure_le_0_iff have "{x. measure M {x} \<noteq> 0} = {}"
   21.12 +    by auto
   21.13 +  then show ?thesis
   21.14 +    by simp
   21.15 +next
   21.16 +  let ?M = "measure M (space M)" and ?m = "\<lambda>x. measure M {x}"
   21.17 +  assume "?M \<noteq> 0"
   21.18 +  then have *: "{x. ?m x \<noteq> 0} = (\<Union>n. {x. ?M / Suc n < ?m x})"
   21.19 +    using reals_Archimedean[of "?m x / ?M" for x]
   21.20 +    by (auto simp: field_simps not_le[symmetric] measure_nonneg divide_le_0_iff measure_le_0_iff)
   21.21 +  have **: "\<And>n. finite {x. ?M / Suc n < ?m x}"
   21.22 +  proof (rule ccontr)
   21.23 +    fix n assume "infinite {x. ?M / Suc n < ?m x}" (is "infinite ?X")
   21.24 +    then obtain X where "finite X" "card X = Suc (Suc n)" "X \<subseteq> ?X"
   21.25 +      by (metis infinite_arbitrarily_large)
   21.26 +    from this(3) have *: "\<And>x. x \<in> X \<Longrightarrow> ?M / Suc n \<le> ?m x"
   21.27 +      by auto
   21.28 +    { fix x assume "x \<in> X"
   21.29 +      from \<open>?M \<noteq> 0\<close> *[OF this] have "?m x \<noteq> 0" by (auto simp: field_simps measure_le_0_iff)
   21.30 +      then have "{x} \<in> sets M" by (auto dest: measure_notin_sets) }
   21.31 +    note singleton_sets = this
   21.32 +    have "?M < (\<Sum>x\<in>X. ?M / Suc n)"
   21.33 +      using \<open>?M \<noteq> 0\<close>
   21.34 +      by (simp add: \<open>card X = Suc (Suc n)\<close> of_nat_Suc field_simps less_le measure_nonneg)
   21.35 +    also have "\<dots> \<le> (\<Sum>x\<in>X. ?m x)"
   21.36 +      by (rule setsum_mono) fact
   21.37 +    also have "\<dots> = measure M (\<Union>x\<in>X. {x})"
   21.38 +      using singleton_sets \<open>finite X\<close>
   21.39 +      by (intro finite_measure_finite_Union[symmetric]) (auto simp: disjoint_family_on_def)
   21.40 +    finally have "?M < measure M (\<Union>x\<in>X. {x})" .
   21.41 +    moreover have "measure M (\<Union>x\<in>X. {x}) \<le> ?M"
   21.42 +      using singleton_sets[THEN sets.sets_into_space] by (intro finite_measure_mono) auto
   21.43 +    ultimately show False by simp
   21.44 +  qed
   21.45 +  show ?thesis
   21.46 +    unfolding * by (intro countable_UN countableI_type countable_finite[OF **])
   21.47 +qed
   21.48 +
   21.49  locale prob_space = finite_measure +
   21.50    assumes emeasure_space_1: "emeasure M (space M) = 1"
   21.51  
    22.1 --- a/src/HOL/Probability/Radon_Nikodym.thy	Wed Jan 06 13:04:31 2016 +0100
    22.2 +++ b/src/HOL/Probability/Radon_Nikodym.thy	Wed Jan 06 12:18:53 2016 +0100
    22.3 @@ -1295,7 +1295,7 @@
    22.4    have "density M (RN_deriv M N) {x} = (\<integral>\<^sup>+w. RN_deriv M N x * indicator {x} w \<partial>M)"
    22.5      by (auto simp: indicator_def emeasure_density intro!: nn_integral_cong)
    22.6    with x density_RN_deriv[OF ac] RN_deriv_nonneg[of M N] show ?thesis
    22.7 -    by (auto simp: nn_integral_cmult_indicator)
    22.8 +    by (auto simp: max_def)
    22.9  qed
   22.10  
   22.11  end
    23.1 --- a/src/HOL/Probability/Set_Integral.thy	Wed Jan 06 13:04:31 2016 +0100
    23.2 +++ b/src/HOL/Probability/Set_Integral.thy	Wed Jan 06 12:18:53 2016 +0100
    23.3 @@ -69,6 +69,19 @@
    23.4    by (auto simp add: indicator_def)
    23.5  *)
    23.6  
    23.7 +lemma set_borel_measurable_sets:
    23.8 +  fixes f :: "_ \<Rightarrow> _::real_normed_vector"
    23.9 +  assumes "set_borel_measurable M X f" "B \<in> sets borel" "X \<in> sets M"
   23.10 +  shows "f -` B \<inter> X \<in> sets M"
   23.11 +proof -
   23.12 +  have "f \<in> borel_measurable (restrict_space M X)"
   23.13 +    using assms by (subst borel_measurable_restrict_space_iff) auto
   23.14 +  then have "f -` B \<inter> space (restrict_space M X) \<in> sets (restrict_space M X)"
   23.15 +    by (rule measurable_sets) fact
   23.16 +  with \<open>X \<in> sets M\<close> show ?thesis
   23.17 +    by (subst (asm) sets_restrict_space_iff) (auto simp: space_restrict_space)
   23.18 +qed
   23.19 +
   23.20  lemma set_lebesgue_integral_cong:
   23.21    assumes "A \<in> sets M" and "\<forall>x. x \<in> A \<longrightarrow> f x = g x"
   23.22    shows "(LINT x:A|M. f x) = (LINT x:A|M. g x)"
    24.1 --- a/src/HOL/Probability/Sigma_Algebra.thy	Wed Jan 06 13:04:31 2016 +0100
    24.2 +++ b/src/HOL/Probability/Sigma_Algebra.thy	Wed Jan 06 12:18:53 2016 +0100
    24.3 @@ -2206,6 +2206,10 @@
    24.4      by simp
    24.5  qed
    24.6  
    24.7 +lemma restrict_space_sets_cong:
    24.8 +  "A = B \<Longrightarrow> sets M = sets N \<Longrightarrow> sets (restrict_space M A) = sets (restrict_space N B)"
    24.9 +  by (auto simp: sets_restrict_space)
   24.10 +
   24.11  lemma sets_restrict_space_count_space :
   24.12    "sets (restrict_space (count_space A) B) = sets (count_space (A \<inter> B))"
   24.13  by(auto simp add: sets_restrict_space)
    25.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    25.2 +++ b/src/HOL/Probability/Sinc_Integral.thy	Wed Jan 06 12:18:53 2016 +0100
    25.3 @@ -0,0 +1,403 @@
    25.4 +(*
    25.5 +  Theory: Sinc_Integral.thy
    25.6 +  Authors: Jeremy Avigad, Luke Serafin, Johannes Hölzl 
    25.7 +*)
    25.8 +
    25.9 +section \<open>Integral of sinc\<close>
   25.10 +
   25.11 +theory Sinc_Integral
   25.12 +  imports Distributions
   25.13 +begin
   25.14 +
   25.15 +subsection \<open>Various preparatory integrals\<close>
   25.16 +
   25.17 +text \<open> Naming convention
   25.18 +The theorem name consists of the following parts:
   25.19 +  \<^item> Kind of integral: @{text has_bochner_integral} / @{text integrable} / @{text LBINT}
   25.20 +  \<^item> Interval: Interval (0 / infinity / open / closed) (infinity / open / closed) 
   25.21 +  \<^item> Name of the occurring constants: power, exp, m (for minus), scale, sin, $\ldots$
   25.22 +\<close>
   25.23 +
   25.24 +lemma has_bochner_integral_I0i_power_exp_m':
   25.25 +  "has_bochner_integral lborel (\<lambda>x. x^k * exp (-x) * indicator {0 ..} x::real) (fact k)"
   25.26 +  using nn_intergal_power_times_exp_Ici[of k]
   25.27 +  by (intro has_bochner_integral_nn_integral)
   25.28 +     (auto simp: ereal_indicator[symmetric] split: split_indicator)
   25.29 +
   25.30 +lemma has_bochner_integral_I0i_power_exp_m:
   25.31 +  "has_bochner_integral lborel (\<lambda>x. x^k * exp (-x) * indicator {0 <..} x::real) (fact k)"
   25.32 +  using AE_lborel_singleton[of 0]
   25.33 +  by (intro has_bochner_integral_cong_AE[THEN iffD1, OF _ _ _ has_bochner_integral_I0i_power_exp_m'])
   25.34 +     (auto split: split_indicator)
   25.35 +
   25.36 +lemma integrable_I0i_exp_mscale: "0 < (u::real) \<Longrightarrow> set_integrable lborel {0 <..} (\<lambda>x. exp (-(x * u)))" 
   25.37 +  using lborel_integrable_real_affine_iff[of u "\<lambda>x. indicator {0 <..} x *\<^sub>R exp (- x)" 0]
   25.38 +        has_bochner_integral_I0i_power_exp_m[of 0]
   25.39 +  by (simp add: indicator_def zero_less_mult_iff mult_ac integrable.intros)
   25.40 +
   25.41 +lemma LBINT_I0i_exp_mscale: "0 < (u::real) \<Longrightarrow> LBINT x=0..\<infinity>. exp (-(x * u)) = 1 / u" 
   25.42 +  using lborel_integral_real_affine[of u "\<lambda>x. indicator {0<..} x *\<^sub>R exp (- x)" 0]
   25.43 +        has_bochner_integral_I0i_power_exp_m[of 0]
   25.44 +  by (auto simp: indicator_def zero_less_mult_iff interval_lebesgue_integral_0_infty field_simps
   25.45 +           dest!: has_bochner_integral_integral_eq)
   25.46 +
   25.47 +lemma LBINT_I0c_exp_mscale_sin:
   25.48 +  "LBINT x=0..t. exp (-(u * x)) * sin x =
   25.49 +    (1 / (1 + u^2)) * (1 - exp (-(u * t)) * (u * sin t + cos t))" (is "_ = ?F t")
   25.50 +  unfolding zero_ereal_def
   25.51 +proof (subst interval_integral_FTC_finite)
   25.52 +  show "(?F has_vector_derivative exp (- (u * x)) * sin x) (at x within {min 0 t..max 0 t})" for x
   25.53 +    by (auto intro!: derivative_eq_intros
   25.54 +             simp: has_field_derivative_iff_has_vector_derivative[symmetric] power2_eq_square)
   25.55 +       (simp_all add: field_simps add_nonneg_eq_0_iff)
   25.56 +qed (auto intro: continuous_at_imp_continuous_on)
   25.57 +
   25.58 +lemma LBINT_I0i_exp_mscale_sin:
   25.59 +  assumes "0 < x"
   25.60 +  shows "LBINT u=0..\<infinity>. \<bar>exp (-u * x) * sin x\<bar> = \<bar>sin x\<bar> / x" 
   25.61 +proof (subst interval_integral_FTC_nonneg)
   25.62 +  let ?F = "\<lambda>u. 1 / x * (1 - exp (- u * x)) * \<bar>sin x\<bar>"
   25.63 +  show "\<And>t. (?F has_real_derivative \<bar>exp (- t * x) * sin x\<bar>) (at t)"
   25.64 +    using \<open>0 < x\<close> by (auto intro!: derivative_eq_intros simp: abs_mult)
   25.65 +  show "((?F \<circ> real_of_ereal) \<longlongrightarrow> 0) (at_right 0)"
   25.66 +    using \<open>0 < x\<close> by (auto simp: zero_ereal_def ereal_tendsto_simps intro!: tendsto_eq_intros)
   25.67 +  have *: "((\<lambda>t. exp (- t * x)) \<longlongrightarrow> 0) at_top"
   25.68 +    using \<open>0 < x\<close>
   25.69 +    by (auto intro!: exp_at_bot[THEN filterlim_compose] filterlim_tendsto_pos_mult_at_top filterlim_ident
   25.70 +             simp: filterlim_uminus_at_bot mult.commute[of _ x])
   25.71 +  show "((?F \<circ> real_of_ereal) \<longlongrightarrow> \<bar>sin x\<bar> / x) (at_left \<infinity>)"
   25.72 +    using \<open>0 < x\<close> unfolding ereal_tendsto_simps
   25.73 +    by (intro filterlim_compose[OF _ *]) (auto intro!: tendsto_eq_intros filterlim_ident)
   25.74 +qed auto
   25.75 +
   25.76 +lemma
   25.77 +  shows integrable_inverse_1_plus_square:
   25.78 +      "set_integrable lborel (einterval (-\<infinity>) \<infinity>) (\<lambda>x. inverse (1 + x^2))"
   25.79 +  and LBINT_inverse_1_plus_square:
   25.80 +      "LBINT x=-\<infinity>..\<infinity>. inverse (1 + x^2) = pi"
   25.81 +proof -
   25.82 +  have 1: "- (pi / 2) < x \<Longrightarrow> x * 2 < pi \<Longrightarrow> (tan has_real_derivative 1 + (tan x)\<^sup>2) (at x)" for x
   25.83 +    using cos_gt_zero_pi[of x] by (subst tan_sec) (auto intro!: DERIV_tan simp: power_inverse)
   25.84 +  have 2: "- (pi / 2) < x \<Longrightarrow> x * 2 < pi \<Longrightarrow> isCont (\<lambda>x. 1 + (tan x)\<^sup>2) x" for x
   25.85 +    using cos_gt_zero_pi[of x] by auto
   25.86 +  show "LBINT x=-\<infinity>..\<infinity>. inverse (1 + x^2) = pi"
   25.87 +    by (subst interval_integral_substitution_nonneg[of "-pi/2" "pi/2" tan "\<lambda>x. 1 + (tan x)^2"])
   25.88 +       (auto intro: derivative_eq_intros 1 2 filterlim_tan_at_right
   25.89 +             simp add: ereal_tendsto_simps filterlim_tan_at_left add_nonneg_eq_0_iff)
   25.90 +  show "set_integrable lborel (einterval (-\<infinity>) \<infinity>) (\<lambda>x. inverse (1 + x^2))"
   25.91 +    by (subst interval_integral_substitution_nonneg[of "-pi/2" "pi/2" tan "\<lambda>x. 1 + (tan x)^2"])
   25.92 +       (auto intro: derivative_eq_intros 1 2 filterlim_tan_at_right
   25.93 +             simp add: ereal_tendsto_simps filterlim_tan_at_left add_nonneg_eq_0_iff)
   25.94 +qed
   25.95 +
   25.96 +lemma 
   25.97 +  shows integrable_I0i_1_div_plus_square:
   25.98 +    "interval_lebesgue_integrable lborel 0 \<infinity> (\<lambda>x. 1 / (1 + x^2))"
   25.99 +  and LBINT_I0i_1_div_plus_square:
  25.100 +    "LBINT x=0..\<infinity>. 1 / (1 + x^2) = pi / 2"
  25.101 +proof -
  25.102 +  have 1: "0 < x \<Longrightarrow> x * 2 < pi \<Longrightarrow> (tan has_real_derivative 1 + (tan x)\<^sup>2) (at x)" for x
  25.103 +    using cos_gt_zero_pi[of x] by (subst tan_sec) (auto intro!: DERIV_tan simp: power_inverse)
  25.104 +  have 2: "0 < x \<Longrightarrow> x * 2 < pi \<Longrightarrow> isCont (\<lambda>x. 1 + (tan x)\<^sup>2) x" for x
  25.105 +    using cos_gt_zero_pi[of x] by auto
  25.106 +  show "LBINT x=0..\<infinity>. 1 / (1 + x^2) = pi / 2"
  25.107 +    by (subst interval_integral_substitution_nonneg[of "0" "pi/2" tan "\<lambda>x. 1 + (tan x)^2"])
  25.108 +       (auto intro: derivative_eq_intros 1 2 tendsto_eq_intros
  25.109 +             simp add: ereal_tendsto_simps filterlim_tan_at_left zero_ereal_def add_nonneg_eq_0_iff)
  25.110 +  show "interval_lebesgue_integrable lborel 0 \<infinity> (\<lambda>x. 1 / (1 + x^2))"
  25.111 +    unfolding interval_lebesgue_integrable_def 
  25.112 +    by (subst interval_integral_substitution_nonneg[of "0" "pi/2" tan "\<lambda>x. 1 + (tan x)^2"])
  25.113 +       (auto intro: derivative_eq_intros 1 2 tendsto_eq_intros
  25.114 +             simp add: ereal_tendsto_simps filterlim_tan_at_left zero_ereal_def add_nonneg_eq_0_iff)
  25.115 +qed
  25.116 +
  25.117 +section \<open>The sinc function, and the sine integral (Si)\<close>
  25.118 +
  25.119 +abbreviation sinc :: "real \<Rightarrow> real" where
  25.120 +  "sinc \<equiv> (\<lambda>x. if x = 0 then 1 else sin x / x)"
  25.121 +
  25.122 +lemma sinc_at_0: "((\<lambda>x. sin x / x::real) \<longlongrightarrow> 1) (at 0)"
  25.123 +  using DERIV_sin [of 0] by (auto simp add: has_field_derivative_def field_has_derivative_at)
  25.124 +
  25.125 +lemma isCont_sinc: "isCont sinc x"
  25.126 +proof cases
  25.127 +  assume "x = 0" then show ?thesis
  25.128 +    using LIM_equal [where g = "\<lambda>x. sin x / x" and a=0 and f=sinc and l=1]
  25.129 +    by (auto simp: isCont_def sinc_at_0)
  25.130 +next
  25.131 +  assume "x \<noteq> 0" show ?thesis
  25.132 +    by (rule continuous_transform_at [where d = "abs x" and f = "\<lambda>x. sin x / x"])
  25.133 +       (auto simp add: dist_real_def \<open>x \<noteq> 0\<close>)
  25.134 +qed
  25.135 +
  25.136 +lemma continuous_on_sinc[continuous_intros]:
  25.137 +  "continuous_on S f \<Longrightarrow> continuous_on S (\<lambda>x. sinc (f x))"
  25.138 +  using continuous_on_compose[of S f sinc, OF _ continuous_at_imp_continuous_on]
  25.139 +  by (auto simp: isCont_sinc)
  25.140 +
  25.141 +lemma borel_measurable_sinc[measurable]: "sinc \<in> borel_measurable borel"
  25.142 +  by (intro borel_measurable_continuous_on1 continuous_at_imp_continuous_on ballI isCont_sinc)
  25.143 +
  25.144 +lemma sinc_AE: "AE x in lborel. sin x / x = sinc x"
  25.145 +  by (rule AE_I [where N = "{0}"], auto)
  25.146 +
  25.147 +definition Si :: "real \<Rightarrow> real" where "Si t \<equiv> LBINT x=0..t. sin x / x"
  25.148 +
  25.149 +lemma sinc_neg [simp]: "sinc (- x) = sinc x"
  25.150 +  by auto
  25.151 +
  25.152 +(* TODO: Determine whether this can reasonably be eliminated by redefining Si. *)
  25.153 +lemma Si_alt_def : "Si t = LBINT x=0..t. sinc x"
  25.154 +proof cases
  25.155 +  assume "0 \<le> t" then show ?thesis
  25.156 +    using AE_lborel_singleton[of 0]
  25.157 +    by (auto simp: Si_def intro!: interval_lebesgue_integral_cong_AE)
  25.158 +next
  25.159 +  assume "\<not> 0 \<le> t" then show ?thesis
  25.160 +    unfolding Si_def using AE_lborel_singleton[of 0]
  25.161 +    by (subst (1 2) interval_integral_endpoints_reverse)
  25.162 +       (auto simp: Si_def intro!: interval_lebesgue_integral_cong_AE)
  25.163 +qed
  25.164 +
  25.165 +lemma Si_neg: 
  25.166 +  assumes "T \<ge> 0" shows "Si (- T) = - Si T"
  25.167 +proof -
  25.168 +  have "LBINT x=ereal 0..T. -1 *\<^sub>R sinc (- x) = LBINT y= ereal (- 0)..ereal (- T). sinc y"
  25.169 +    by (rule interval_integral_substitution_finite [OF assms])
  25.170 +       (auto intro: derivative_intros continuous_at_imp_continuous_on isCont_sinc)
  25.171 +  also have "(LBINT x=ereal 0..T. -1 *\<^sub>R sinc (- x)) = -(LBINT x=ereal 0..T. sinc x)"
  25.172 +    by (subst sinc_neg) (simp_all add: interval_lebesgue_integral_uminus)
  25.173 +  finally have *: "-(LBINT x=ereal 0..T. sinc x) = LBINT y= ereal 0..ereal (- T). sinc y"
  25.174 +    by simp
  25.175 +  show ?thesis
  25.176 +    using assms unfolding Si_alt_def
  25.177 +    by (subst zero_ereal_def)+ (auto simp add: * [symmetric])
  25.178 +qed
  25.179 +
  25.180 +lemma integrable_sinc':
  25.181 +  "interval_lebesgue_integrable lborel (ereal 0) (ereal T) (\<lambda>t. sin (t * \<theta>) / t)"
  25.182 +proof -
  25.183 +  have *: "interval_lebesgue_integrable lborel (ereal 0) (ereal T) (\<lambda>t. \<theta> * sinc (t * \<theta>))"
  25.184 +    by (intro interval_lebesgue_integrable_mult_right interval_integrable_isCont continuous_within_compose3 [OF isCont_sinc])
  25.185 +       auto
  25.186 +  show ?thesis
  25.187 +    by (rule interval_lebesgue_integrable_cong_AE[THEN iffD1, OF _ _ _ *])
  25.188 +       (insert AE_lborel_singleton[of 0], auto)
  25.189 +qed
  25.190 +
  25.191 +(* TODO: need a better version of FTC2 *)
  25.192 +lemma DERIV_Si: "(Si has_real_derivative sinc x) (at x)"
  25.193 +proof -
  25.194 +  have "(at x within {min 0 (x - 1)..max 0 (x + 1)}) = at x"
  25.195 +    by (intro at_within_interior) auto
  25.196 +  moreover have "min 0 (x - 1) \<le> x" "x \<le> max 0 (x + 1)"
  25.197 +    by auto
  25.198 +  ultimately show ?thesis
  25.199 +    using interval_integral_FTC2[of "min 0 (x - 1)" 0 "max 0 (x + 1)" sinc x]
  25.200 +    by (auto simp: continuous_at_imp_continuous_on isCont_sinc Si_alt_def[abs_def] zero_ereal_def
  25.201 +                   has_field_derivative_iff_has_vector_derivative
  25.202 +             split del: split_if)
  25.203 +qed
  25.204 +
  25.205 +lemma isCont_Si: "isCont Si x"
  25.206 +  using DERIV_Si by (rule DERIV_isCont)
  25.207 +
  25.208 +lemma borel_measurable_Si[measurable]: "Si \<in> borel_measurable borel"
  25.209 +  by (auto intro: isCont_Si continuous_at_imp_continuous_on borel_measurable_continuous_on1)
  25.210 +
  25.211 +lemma Si_at_top_LBINT:
  25.212 +  "((\<lambda>t. (LBINT x=0..\<infinity>. exp (-(x * t)) * (x * sin t + cos t) / (1 + x^2))) \<longlongrightarrow> 0) at_top"
  25.213 +proof -
  25.214 +  let ?F = "\<lambda>x t. exp (- (x * t)) * (x * sin t + cos t) / (1 + x\<^sup>2) :: real"
  25.215 +  have int: "set_integrable lborel {0<..} (\<lambda>x. exp (- x) * (x + 1) :: real)"
  25.216 +    unfolding distrib_left
  25.217 +    using has_bochner_integral_I0i_power_exp_m[of 0] has_bochner_integral_I0i_power_exp_m[of 1]
  25.218 +    by (intro set_integral_add) (auto dest!: integrable.intros simp: ac_simps)
  25.219 +
  25.220 +  have "((\<lambda>t::real. LBINT x:{0<..}. ?F x t) \<longlongrightarrow> LBINT x::real:{0<..}. 0) at_top"
  25.221 +  proof (rule integral_dominated_convergence_at_top[OF _ _ int], simp_all del: abs_divide split: split_indicator)
  25.222 +    have *: "0 < x \<Longrightarrow> \<bar>x * sin t + cos t\<bar> / (1 + x\<^sup>2) \<le> (x * 1 + 1) / 1" for x t :: real
  25.223 +      by (intro frac_le abs_triangle_ineq[THEN order_trans] add_mono)
  25.224 +         (auto simp add: abs_mult simp del: mult_1_right intro!: mult_mono)
  25.225 +    then have **: "1 \<le> t \<Longrightarrow> 0 < x \<Longrightarrow> \<bar>?F x t\<bar> \<le> exp (- x) * (x + 1)" for x t :: real
  25.226 +        by (auto simp add: abs_mult times_divide_eq_right[symmetric] simp del: times_divide_eq_right
  25.227 +                 intro!:  mult_mono)
  25.228 +  
  25.229 +    show "\<forall>\<^sub>F i in at_top. AE x in lborel. 0 < x \<longrightarrow> \<bar>?F x i\<bar> \<le> exp (- x) * (x + 1)"
  25.230 +      using eventually_ge_at_top[of "1::real"] ** by (auto elim: eventually_mono)
  25.231 +    show "AE x in lborel. 0 < x \<longrightarrow> (?F x \<longlongrightarrow> 0) at_top"
  25.232 +    proof (intro always_eventually impI allI)
  25.233 +      fix x :: real assume "0 < x"
  25.234 +      show "((\<lambda>t. exp (- (x * t)) * (x * sin t + cos t) / (1 + x\<^sup>2)) \<longlongrightarrow> 0) at_top"
  25.235 +      proof (rule Lim_null_comparison)
  25.236 +        show "\<forall>\<^sub>F t in at_top. norm (?F x t) \<le> exp (- (x * t)) * (x + 1)"
  25.237 +          using eventually_ge_at_top[of "1::real"] * \<open>0 < x\<close>
  25.238 +          by (auto simp add: abs_mult times_divide_eq_right[symmetric] simp del: times_divide_eq_right
  25.239 +                   intro!: mult_mono elim: eventually_mono)
  25.240 +        show "((\<lambda>t. exp (- (x * t)) * (x + 1)) \<longlongrightarrow> 0) at_top"
  25.241 +          by (auto simp: filterlim_uminus_at_top [symmetric]
  25.242 +                   intro!: filterlim_tendsto_pos_mult_at_top[OF tendsto_const] \<open>0<x\<close> filterlim_ident
  25.243 +                           tendsto_mult_left_zero filterlim_compose[OF exp_at_bot])
  25.244 +      qed
  25.245 +    qed
  25.246 +  qed
  25.247 +  then show "((\<lambda>t. (LBINT x=0..\<infinity>. exp (-(x * t)) * (x * sin t + cos t) / (1 + x^2))) \<longlongrightarrow> 0) at_top"
  25.248 +    by (simp add: interval_lebesgue_integral_0_infty)
  25.249 +qed
  25.250 +
  25.251 +lemma Si_at_top_integrable:
  25.252 +  assumes "t \<ge> 0"
  25.253 +  shows "interval_lebesgue_integrable lborel 0 \<infinity> (\<lambda>x. exp (- (x * t)) * (x * sin t + cos t) / (1 + x\<^sup>2))"
  25.254 +  using \<open>0 \<le> t\<close> unfolding le_less
  25.255 +proof
  25.256 +  assume "0 = t" then show ?thesis
  25.257 +    using integrable_I0i_1_div_plus_square by simp
  25.258 +next
  25.259 +  assume [arith]: "0 < t"
  25.260 +  have *: "0 \<le> a \<Longrightarrow> 0 < x \<Longrightarrow> a / (1 + x\<^sup>2) \<le> a" for a x :: real
  25.261 +    using zero_le_power2[of x, arith] using divide_left_mono[of 1 "1+x\<^sup>2" a] by auto
  25.262 +  have "set_integrable lborel {0<..} (\<lambda>x. (exp (- x) * x) * (sin t/t) + exp (- x) * cos t)"
  25.263 +    using has_bochner_integral_I0i_power_exp_m[of 0] has_bochner_integral_I0i_power_exp_m[of 1]
  25.264 +    by (intro set_integral_add set_integrable_mult_left)
  25.265 +       (auto dest!: integrable.intros simp: ac_simps)
  25.266 +  from lborel_integrable_real_affine[OF this, of t 0]
  25.267 +  show ?thesis
  25.268 +    unfolding interval_lebesgue_integral_0_infty
  25.269 +    by (rule integrable_bound) (auto simp: field_simps * split: split_indicator)
  25.270 +qed
  25.271 +
  25.272 +lemma Si_at_top: "(Si \<longlongrightarrow> pi / 2) at_top"
  25.273 +proof -
  25.274 +  have "\<forall>\<^sub>F t in at_top. pi / 2 - (LBINT u=0..\<infinity>. exp (-(u * t)) * (u * sin t + cos t) / (1 + u^2)) = Si t"
  25.275 +    using eventually_ge_at_top[of 0]
  25.276 +  proof eventually_elim
  25.277 +    fix t :: real assume "t \<ge> 0"
  25.278 +    have "Si t = LBINT x=0..t. sin x * (LBINT u=0..\<infinity>. exp (-(u * x)))"
  25.279 +      unfolding Si_def using `0 \<le> t`
  25.280 +      by (intro interval_integral_discrete_difference[where X="{0}"]) (auto simp: LBINT_I0i_exp_mscale)
  25.281 +    also have "\<dots> = LBINT x. (LBINT u=ereal 0..\<infinity>. indicator {0 <..< t} x *\<^sub>R sin x * exp (-(u * x)))"
  25.282 +      using \<open>0\<le>t\<close> by (simp add: zero_ereal_def interval_lebesgue_integral_le_eq mult_ac)
  25.283 +    also have "\<dots> = LBINT x. (LBINT u. indicator ({0<..} \<times> {0 <..< t}) (u, x) *\<^sub>R (sin x * exp (-(u * x))))"
  25.284 +      by (subst interval_integral_Ioi) (simp_all add: indicator_times ac_simps)
  25.285 +    also have "\<dots> = LBINT u. (LBINT x. indicator ({0<..} \<times> {0 <..< t}) (u, x) *\<^sub>R (sin x * exp (-(u * x))))"
  25.286 +    proof (intro lborel_pair.Fubini_integral[symmetric] lborel_pair.Fubini_integrable)
  25.287 +      show "(\<lambda>(x, y). indicator ({0<..} \<times> {0<..<t}) (y, x) *\<^sub>R (sin x * exp (- (y * x))))
  25.288 +          \<in> borel_measurable (lborel \<Otimes>\<^sub>M lborel)" (is "?f \<in> borel_measurable _")
  25.289 +        by measurable
  25.290 +
  25.291 +      have "AE x in lborel. indicator {0..t} x *\<^sub>R abs (sinc x) = LBINT y. norm (?f (x, y))"
  25.292 +        using AE_lborel_singleton[of 0] AE_lborel_singleton[of t]
  25.293 +      proof eventually_elim
  25.294 +        fix x :: real assume x: "x \<noteq> 0" "x \<noteq> t"
  25.295 +        have "LBINT y. \<bar>indicator ({0<..} \<times> {0<..<t}) (y, x) *\<^sub>R (sin x * exp (- (y * x)))\<bar> = 
  25.296 +            LBINT y. \<bar>sin x\<bar> * exp (- (y * x)) * indicator {0<..} y * indicator {0<..<t} x"
  25.297 +          by (intro integral_cong) (auto split: split_indicator simp: abs_mult)
  25.298 +        also have "\<dots> = \<bar>sin x\<bar> * indicator {0<..<t} x * (LBINT y=0..\<infinity>.  exp (- (y * x)))"
  25.299 +          by (cases "x > 0")                                       
  25.300 +             (auto simp: field_simps interval_lebesgue_integral_0_infty split: split_indicator)
  25.301 +        also have "\<dots> = \<bar>sin x\<bar> * indicator {0<..<t} x * (1 / x)"
  25.302 +          by (cases "x > 0") (auto simp add: LBINT_I0i_exp_mscale)
  25.303 +        also have "\<dots> = indicator {0..t} x *\<^sub>R \<bar>sinc x\<bar>"
  25.304 +          using x by (simp add: field_simps split: split_indicator)
  25.305 +        finally show "indicator {0..t} x *\<^sub>R abs (sinc x) = LBINT y. norm (?f (x, y))"
  25.306 +          by simp
  25.307 +      qed
  25.308 +      moreover have "set_integrable lborel {0 .. t} (\<lambda>x. abs (sinc x))"
  25.309 +        by (auto intro!: borel_integrable_compact continuous_intros simp del: real_scaleR_def)
  25.310 +      ultimately show "integrable lborel (\<lambda>x. LBINT y. norm (?f (x, y)))"
  25.311 +        by (rule integrable_cong_AE_imp[rotated 2]) simp
  25.312 +  
  25.313 +      have "0 < x \<Longrightarrow> set_integrable lborel {0<..} (\<lambda>y. sin x * exp (- (y * x)))" for x :: real
  25.314 +          by (intro set_integrable_mult_right integrable_I0i_exp_mscale)
  25.315 +      then show "AE x in lborel. integrable lborel (\<lambda>y. ?f (x, y))"
  25.316 +        by (intro AE_I2) (auto simp: indicator_times split: split_indicator)
  25.317 +    qed
  25.318 +    also have "... = LBINT u=0..\<infinity>. (LBINT x=0..t. exp (-(u * x)) * sin x)"
  25.319 +      using \<open>0\<le>t\<close>
  25.320 +      by (auto simp: interval_lebesgue_integral_def zero_ereal_def ac_simps
  25.321 +               split: split_indicator intro!: integral_cong)
  25.322 +    also have "\<dots> = LBINT u=0..\<infinity>. 1 / (1 + u\<^sup>2) - 1 / (1 + u\<^sup>2) * (exp (- (u * t)) * (u * sin t + cos t))"
  25.323 +      by (auto simp: divide_simps LBINT_I0c_exp_mscale_sin intro!: interval_integral_cong)
  25.324 +    also have "... = pi / 2 - (LBINT u=0..\<infinity>. exp (- (u * t)) * (u * sin t + cos t) / (1 + u^2))"
  25.325 +      using Si_at_top_integrable[OF \<open>0\<le>t\<close>] by (simp add: integrable_I0i_1_div_plus_square LBINT_I0i_1_div_plus_square)
  25.326 +    finally show "pi / 2 - (LBINT u=0..\<infinity>. exp (-(u * t)) * (u * sin t + cos t) / (1 + u^2)) = Si t" ..
  25.327 +  qed
  25.328 +  then show ?thesis
  25.329 +    by (rule Lim_transform_eventually)
  25.330 +       (auto intro!: tendsto_eq_intros Si_at_top_LBINT)
  25.331 +qed
  25.332 +
  25.333 +subsection \<open>The final theorems: boundedness and scalability\<close>
  25.334 +
  25.335 +lemma bounded_Si: "\<exists>B. \<forall>T. \<bar>Si T\<bar> \<le> B"
  25.336 +proof -
  25.337 +  have *: "0 \<le> y \<Longrightarrow> dist x y < z \<Longrightarrow> abs x \<le> y + z" for x y z :: real
  25.338 +    by (simp add: dist_real_def)
  25.339 +
  25.340 +  have "eventually (\<lambda>T. dist (Si T) (pi / 2) < 1) at_top"
  25.341 +    using Si_at_top by (elim tendstoD) simp
  25.342 +  then have "eventually (\<lambda>T. 0 \<le> T \<and> \<bar>Si T\<bar> \<le> pi / 2 + 1) at_top"
  25.343 +    using eventually_ge_at_top[of 0] by eventually_elim (insert *[of "pi/2" "Si _" 1], auto)
  25.344 +  then have "\<exists>T. 0 \<le> T \<and> (\<forall>t \<ge> T. \<bar>Si t\<bar> \<le> pi / 2 + 1)"
  25.345 +    by (auto simp add: eventually_at_top_linorder)
  25.346 +  then obtain T where T: "0 \<le> T" "\<And>t. t \<ge> T \<Longrightarrow> \<bar>Si t\<bar> \<le> pi / 2 + 1"
  25.347 +    by auto
  25.348 +  moreover have "t \<le> - T \<Longrightarrow> \<bar>Si t\<bar> \<le> pi / 2 + 1" for t
  25.349 +    using T(1) T(2)[of "-t"] Si_neg[of "- t"] by simp
  25.350 +  moreover have "\<exists>M. \<forall>t. -T \<le> t \<and> t \<le> T \<longrightarrow> \<bar>Si t\<bar> \<le> M"
  25.351 +    by (rule isCont_bounded) (auto intro!: isCont_Si continuous_intros \<open>0 \<le> T\<close>)
  25.352 +  then obtain M where M: "\<And>t. -T \<le> t \<and> t \<le> T \<Longrightarrow> \<bar>Si t\<bar> \<le> M"
  25.353 +    by auto
  25.354 +  ultimately show ?thesis
  25.355 +    by (intro exI[of _ "max M (pi/2 + 1)"]) (meson le_max_iff_disj linorder_not_le order_le_less)
  25.356 +qed    
  25.357 +
  25.358 +lemma LBINT_I0c_sin_scale_divide:
  25.359 +  assumes "T \<ge> 0"
  25.360 +  shows "LBINT t=0..T. sin (t * \<theta>) / t = sgn \<theta> * Si (T * \<bar>\<theta>\<bar>)"
  25.361 +proof -
  25.362 +  { assume "0 < \<theta>"
  25.363 +    have "(LBINT t=ereal 0..T. sin (t * \<theta>) / t) = (LBINT t=ereal 0..T. \<theta> *\<^sub>R sinc (t * \<theta>))"
  25.364 +      by (rule interval_integral_discrete_difference[of "{0}"]) auto
  25.365 +    also have "\<dots> = (LBINT t=ereal (0 * \<theta>)..T * \<theta>. sinc t)"
  25.366 +      apply (rule interval_integral_substitution_finite [OF assms])
  25.367 +      apply (subst mult.commute, rule DERIV_subset)
  25.368 +      by (auto intro!: derivative_intros continuous_at_imp_continuous_on isCont_sinc)
  25.369 +    also have "\<dots> = (LBINT t=ereal (0 * \<theta>)..T * \<theta>. sin t / t)"
  25.370 +      by (rule interval_integral_discrete_difference[of "{0}"]) auto
  25.371 +    finally have "(LBINT t=ereal 0..T. sin (t * \<theta>) / t) = (LBINT t=ereal 0..T * \<theta>. sin t / t)"
  25.372 +      by simp
  25.373 +    hence "LBINT x. indicator {0<..<T} x * sin (x * \<theta>) / x =
  25.374 +        LBINT x. indicator {0<..<T * \<theta>} x * sin x / x"
  25.375 +      using assms `0 < \<theta>` unfolding interval_lebesgue_integral_def einterval_eq zero_ereal_def 
  25.376 +        by (auto simp: ac_simps)
  25.377 +  } note aux1 = this
  25.378 +  { assume "0 > \<theta>"
  25.379 +    have [simp]: "integrable lborel (\<lambda>x. sin (x * \<theta>) * indicator {0<..<T} x / x)"
  25.380 +      using integrable_sinc' [of T \<theta>] assms 
  25.381 +      by (simp add: interval_lebesgue_integrable_def ac_simps)
  25.382 +    have "(LBINT t=ereal 0..T. sin (t * -\<theta>) / t) = (LBINT t=ereal 0..T. -\<theta> *\<^sub>R sinc (t * -\<theta>))"
  25.383 +      by (rule interval_integral_discrete_difference[of "{0}"]) auto
  25.384 +    also have "\<dots> = (LBINT t=ereal (0 * -\<theta>)..T * -\<theta>. sinc t)"
  25.385 +      apply (rule interval_integral_substitution_finite [OF assms])
  25.386 +      apply (subst mult.commute, rule DERIV_subset)
  25.387 +      by (auto intro!: derivative_intros continuous_at_imp_continuous_on isCont_sinc)
  25.388 +    also have "\<dots> = (LBINT t=ereal (0 * -\<theta>)..T * -\<theta>. sin t / t)"
  25.389 +      by (rule interval_integral_discrete_difference[of "{0}"]) auto
  25.390 +    finally have "(LBINT t=ereal 0..T. sin (t * -\<theta>) / t) = (LBINT t=ereal 0..T * -\<theta>. sin t / t)"
  25.391 +      by simp
  25.392 +    hence "LBINT x. indicator {0<..<T} x * sin (x * \<theta>) / x =
  25.393 +       - (LBINT x. indicator {0<..<- (T * \<theta>)} x * sin x / x)"
  25.394 +      using assms `0 > \<theta>` unfolding interval_lebesgue_integral_def einterval_eq zero_ereal_def
  25.395 +        by (auto simp add: field_simps mult_le_0_iff split: split_if_asm)
  25.396 +  } note aux2 = this
  25.397 +  show ?thesis
  25.398 +    using assms unfolding Si_def interval_lebesgue_integral_def sgn_real_def einterval_eq zero_ereal_def
  25.399 +    apply auto
  25.400 +    apply (erule aux1)
  25.401 +    apply (rule aux2)
  25.402 +    apply auto
  25.403 +    done
  25.404 +qed
  25.405 +
  25.406 +end
    26.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    26.2 +++ b/src/HOL/Probability/Weak_Convergence.thy	Wed Jan 06 12:18:53 2016 +0100
    26.3 @@ -0,0 +1,420 @@
    26.4 +(*
    26.5 +  Theory: Weak_Convergence.thy
    26.6 +  Authors: Jeremy Avigad, Luke Serafin
    26.7 +*)
    26.8 +
    26.9 +section \<open>Weak Convergence of Functions and Distributions\<close>
   26.10 +
   26.11 +text \<open>Properties of weak convergence of functions and measures, including the portmanteau theorem.\<close>
   26.12 +
   26.13 +theory Weak_Convergence
   26.14 +  imports Distribution_Functions
   26.15 +begin
   26.16 +
   26.17 +section \<open>Weak Convergence of Functions\<close>
   26.18 +
   26.19 +definition
   26.20 +  weak_conv :: "(nat \<Rightarrow> (real \<Rightarrow> real)) \<Rightarrow> (real \<Rightarrow> real) \<Rightarrow> bool"
   26.21 +where
   26.22 +  "weak_conv F_seq F \<equiv> \<forall>x. isCont F x \<longrightarrow> (\<lambda>n. F_seq n x) \<longlonglongrightarrow> F x"
   26.23 +
   26.24 +section \<open>Weak Convergence of Distributions\<close>
   26.25 +
   26.26 +definition
   26.27 +  weak_conv_m :: "(nat \<Rightarrow> real measure) \<Rightarrow> real measure \<Rightarrow> bool"
   26.28 +where
   26.29 +  "weak_conv_m M_seq M \<equiv> weak_conv (\<lambda>n. cdf (M_seq n)) (cdf M)"
   26.30 +
   26.31 +section \<open>Skorohod's theorem\<close>
   26.32 +
   26.33 +locale right_continuous_mono = 
   26.34 +  fixes f :: "real \<Rightarrow> real" and a b :: real
   26.35 +  assumes cont: "\<And>x. continuous (at_right x) f"
   26.36 +  assumes mono: "mono f"
   26.37 +  assumes bot: "(f \<longlongrightarrow> a) at_bot" 
   26.38 +  assumes top: "(f \<longlongrightarrow> b) at_top"
   26.39 +begin
   26.40 +
   26.41 +abbreviation I :: "real \<Rightarrow> real" where
   26.42 +  "I \<omega> \<equiv> Inf {x. \<omega> \<le> f x}"
   26.43 +
   26.44 +lemma pseudoinverse: assumes "a < \<omega>" "\<omega> < b" shows "\<omega> \<le> f x \<longleftrightarrow> I \<omega> \<le> x"
   26.45 +proof
   26.46 +  let ?F = "{x. \<omega> \<le> f x}"
   26.47 +  obtain y where "f y < \<omega>"
   26.48 +    by (metis eventually_happens' trivial_limit_at_bot_linorder order_tendstoD(2) bot \<open>a < \<omega>\<close>)
   26.49 +  with mono have bdd: "bdd_below ?F"
   26.50 +    by (auto intro!: bdd_belowI[of _ y] elim: mono_invE[OF _ less_le_trans])
   26.51 +
   26.52 +  have ne: "?F \<noteq> {}"
   26.53 +    using order_tendstoD(1)[OF top \<open>\<omega> < b\<close>] 
   26.54 +    by (auto dest!: eventually_happens'[OF trivial_limit_at_top_linorder] intro: less_imp_le)
   26.55 +
   26.56 +  show "\<omega> \<le> f x \<Longrightarrow> I \<omega> \<le> x"
   26.57 +    by (auto intro!: cInf_lower bdd)
   26.58 +
   26.59 +  { assume *: "I \<omega> \<le> x"
   26.60 +    have "\<omega> \<le> (INF s:{x. \<omega> \<le> f x}. f s)"
   26.61 +      by (rule cINF_greatest[OF ne]) auto
   26.62 +    also have "\<dots> = f (I \<omega>)"
   26.63 +      using continuous_at_Inf_mono[OF mono cont ne bdd] ..
   26.64 +    also have "\<dots> \<le> f x"
   26.65 +      using * by (rule monoD[OF \<open>mono f\<close>])
   26.66 +    finally show "\<omega> \<le> f x" . }
   26.67 +qed
   26.68 +
   26.69 +lemma pseudoinverse': "\<forall>\<omega>\<in>{a<..<b}. \<forall>x. \<omega> \<le> f x \<longleftrightarrow> I \<omega> \<le> x"
   26.70 +  by (intro ballI allI impI pseudoinverse) auto
   26.71 +
   26.72 +lemma mono_I: "mono_on I {a <..< b}"
   26.73 +  unfolding mono_on_def by (metis order.trans order.refl pseudoinverse')
   26.74 +
   26.75 +end
   26.76 +
   26.77 +locale cdf_distribution = real_distribution
   26.78 +begin
   26.79 +
   26.80 +abbreviation "C \<equiv> cdf M"
   26.81 +
   26.82 +sublocale right_continuous_mono C 0 1
   26.83 +  by standard
   26.84 +     (auto intro: cdf_nondecreasing cdf_is_right_cont cdf_lim_at_top_prob cdf_lim_at_bot monoI)
   26.85 +
   26.86 +lemma measurable_C[measurable]: "C \<in> borel_measurable borel"
   26.87 +  by (intro borel_measurable_mono mono)
   26.88 +
   26.89 +lemma measurable_CI[measurable]: "I \<in> borel_measurable (restrict_space borel {0<..<1})"
   26.90 +  by (intro borel_measurable_mono_on_fnc mono_I)
   26.91 +
   26.92 +lemma emeasure_distr_I: "emeasure (distr (restrict_space lborel {0<..<1::real}) borel I) UNIV = 1"
   26.93 +  by (simp add: emeasure_distr space_restrict_space emeasure_restrict_space )
   26.94 +
   26.95 +lemma distr_I_eq_M: "distr (restrict_space lborel {0<..<1::real}) borel I = M" (is "?I = _")
   26.96 +proof (intro cdf_unique ext)
   26.97 +  let ?\<Omega> = "restrict_space lborel {0<..<1}::real measure"
   26.98 +  interpret \<Omega>: prob_space ?\<Omega>
   26.99 +    by (auto simp add: emeasure_restrict_space space_restrict_space intro!: prob_spaceI)
  26.100 +  show "real_distribution ?I"
  26.101 +    by auto
  26.102 +
  26.103 +  fix x
  26.104 +  have "cdf ?I x = measure lborel {\<omega>\<in>{0<..<1}. \<omega> \<le> C x}"
  26.105 +    by (subst cdf_def)
  26.106 +       (auto simp: pseudoinverse[symmetric] measure_distr space_restrict_space measure_restrict_space
  26.107 +             intro!: arg_cong2[where f="measure"])
  26.108 +  also have "\<dots> = measure lborel {0 <..< C x}"
  26.109 +    using cdf_bounded_prob[of x] AE_lborel_singleton[of "C x"]
  26.110 +    by (auto intro!: arg_cong[where f=real_of_ereal] emeasure_eq_AE simp: measure_def)
  26.111 +  also have "\<dots> = C x"
  26.112 +    by (simp add: cdf_nonneg)
  26.113 +  finally show "cdf (distr ?\<Omega> borel I) x = C x" .
  26.114 +qed standard
  26.115 +
  26.116 +end
  26.117 +
  26.118 +context
  26.119 +  fixes \<mu> :: "nat \<Rightarrow> real measure"
  26.120 +    and M :: "real measure"
  26.121 +  assumes \<mu>: "\<And>n. real_distribution (\<mu> n)" 
  26.122 +  assumes M: "real_distribution M"
  26.123 +  assumes \<mu>_to_M: "weak_conv_m \<mu> M"
  26.124 +begin  
  26.125 +
  26.126 +(* state using obtains? *)
  26.127 +theorem Skorohod:
  26.128 + "\<exists> (\<Omega> :: real measure) (Y_seq :: nat \<Rightarrow> real \<Rightarrow> real) (Y :: real \<Rightarrow> real). 
  26.129 +    prob_space \<Omega> \<and>
  26.130 +    (\<forall>n. Y_seq n \<in> measurable \<Omega> borel) \<and>
  26.131 +    (\<forall>n. distr \<Omega> borel (Y_seq n) = \<mu> n) \<and>
  26.132 +    Y \<in> measurable \<Omega> lborel \<and>
  26.133 +    distr \<Omega> borel Y = M \<and>
  26.134 +    (\<forall>x \<in> space \<Omega>. (\<lambda>n. Y_seq n x) \<longlonglongrightarrow> Y x)"
  26.135 +proof -
  26.136 +  interpret \<mu>: cdf_distribution "\<mu> n" for n
  26.137 +    unfolding cdf_distribution_def by (rule \<mu>)
  26.138 +  interpret M: cdf_distribution M
  26.139 +    unfolding cdf_distribution_def by (rule M)
  26.140 +
  26.141 +  have conv: "measure M {x} = 0 \<Longrightarrow> (\<lambda>n. \<mu>.C n x) \<longlonglongrightarrow> M.C x" for x
  26.142 +    using \<mu>_to_M M.isCont_cdf by (auto simp: weak_conv_m_def weak_conv_def)
  26.143 +
  26.144 +  let ?\<Omega> = "restrict_space lborel {0<..<1} :: real measure"
  26.145 +  have "prob_space ?\<Omega>"
  26.146 +    by (auto simp: space_restrict_space emeasure_restrict_space intro!: prob_spaceI)
  26.147 +  interpret \<Omega>: prob_space ?\<Omega>
  26.148 +    by fact
  26.149 +
  26.150 +  have Y_distr: "distr ?\<Omega> borel M.I = M"
  26.151 +    by (rule M.distr_I_eq_M)
  26.152 +
  26.153 +  have Y_cts_cnv: "(\<lambda>n. \<mu>.I n \<omega>) \<longlonglongrightarrow> M.I \<omega>" 
  26.154 +    if \<omega>: "\<omega> \<in> {0<..<1}" "isCont M.I \<omega>" for \<omega> :: real
  26.155 +  proof (intro limsup_le_liminf_real)
  26.156 +    show "liminf (\<lambda>n. \<mu>.I n \<omega>) \<ge> M.I \<omega>"
  26.157 +      unfolding le_Liminf_iff
  26.158 +    proof safe
  26.159 +      fix B :: ereal assume B: "B < M.I \<omega>"
  26.160 +      then show "\<forall>\<^sub>F n in sequentially. B < \<mu>.I n \<omega>"
  26.161 +      proof (cases B)
  26.162 +        case (real r)
  26.163 +        with B have r: "r < M.I \<omega>"
  26.164 +          by simp
  26.165 +        then obtain x where x: "r < x" "x < M.I \<omega>" "measure M {x} = 0"
  26.166 +          using open_minus_countable[OF M.countable_support, of "{r<..<M.I \<omega>}"] by auto
  26.167 +        then have Fx_less: "M.C x < \<omega>"
  26.168 +          using M.pseudoinverse' \<omega> not_less by blast
  26.169 +
  26.170 +        have "\<forall>\<^sub>F n in sequentially. \<mu>.C n x < \<omega>"
  26.171 +          using order_tendstoD(2)[OF conv[OF x(3)] Fx_less] .
  26.172 +        then have "\<forall>\<^sub>F n in sequentially. x < \<mu>.I n \<omega>"
  26.173 +          by eventually_elim (insert \<omega> \<mu>.pseudoinverse[symmetric], simp add: not_le[symmetric])
  26.174 +        then show ?thesis
  26.175 +          by eventually_elim (insert x(1), simp add: real)
  26.176 +      qed auto
  26.177 +    qed
  26.178 +
  26.179 +    have *: "limsup (\<lambda>n. \<mu>.I n \<omega>) \<le> M.I \<omega>'"
  26.180 +      if \<omega>': "0 < \<omega>'" "\<omega>' < 1" "\<omega> < \<omega>'" for \<omega>' :: real
  26.181 +    proof (rule dense_ge_bounded)
  26.182 +      fix B' assume "ereal (M.I \<omega>') < B'" "B' < ereal (M.I \<omega>' + 1)"
  26.183 +      then obtain B where "M.I \<omega>' < B" and [simp]: "B' = ereal B"
  26.184 +        by (cases B') auto
  26.185 +      then obtain y where y: "M.I \<omega>' < y" "y < B" "measure M {y} = 0"
  26.186 +        using open_minus_countable[OF M.countable_support, of "{M.I \<omega>'<..<B}"] by auto
  26.187 +      then have "\<omega>' \<le> M.C (M.I \<omega>')"
  26.188 +        using M.pseudoinverse' \<omega>' by (metis greaterThanLessThan_iff order_refl)
  26.189 +      also have "... \<le> M.C y"
  26.190 +        using M.mono y unfolding mono_def by auto
  26.191 +      finally have Fy_gt: "\<omega> < M.C y"
  26.192 +        using \<omega>'(3) by simp
  26.193 +
  26.194 +      have "\<forall>\<^sub>F n in sequentially. \<omega> \<le> \<mu>.C n y"
  26.195 +        using order_tendstoD(1)[OF conv[OF y(3)] Fy_gt] by eventually_elim (rule less_imp_le)
  26.196 +      then have 2: "\<forall>\<^sub>F n in sequentially. \<mu>.I n \<omega> \<le> ereal y"
  26.197 +        by simp (subst \<mu>.pseudoinverse'[rule_format, OF \<omega>(1), symmetric])
  26.198 +      then show "limsup (\<lambda>n. \<mu>.I n \<omega>) \<le> B'"
  26.199 +        using \<open>y < B\<close>
  26.200 +        by (intro Limsup_bounded[rotated]) (auto intro: le_less_trans elim: eventually_mono)
  26.201 +    qed simp
  26.202 +    
  26.203 +    have **: "(M.I \<longlongrightarrow> ereal (M.I \<omega>)) (at_right \<omega>)"
  26.204 +      using \<omega>(2) by (auto intro: tendsto_within_subset simp: continuous_at)
  26.205 +    show "limsup (\<lambda>n. \<mu>.I n \<omega>) \<le> M.I \<omega>"
  26.206 +      using \<omega>
  26.207 +      by (intro tendsto_le_const[OF trivial_limit_at_right_real **])
  26.208 +         (auto intro!: exI[of _ 1] * simp: eventually_at_right[of _ 1])
  26.209 +  qed
  26.210 +
  26.211 +  let ?D = "{\<omega>\<in>{0<..<1}. \<not> isCont M.I \<omega>}"
  26.212 +  have D_countable: "countable ?D"
  26.213 +    using mono_on_ctble_discont[OF M.mono_I] by (simp add: at_within_open[of _ "{0 <..< 1}"] cong: conj_cong)
  26.214 +  hence D: "emeasure ?\<Omega> ?D = 0"
  26.215 +    using emeasure_lborel_countable[OF D_countable]
  26.216 +    by (subst emeasure_restrict_space) auto
  26.217 +
  26.218 +  def Y' \<equiv> "\<lambda>\<omega>. if \<omega> \<in> ?D then 0 else M.I \<omega>"
  26.219 +  have Y'_AE: "AE \<omega> in ?\<Omega>. Y' \<omega> = M.I \<omega>"
  26.220 +    by (rule AE_I [OF _ D]) (auto simp: space_restrict_space sets_restrict_space_iff Y'_def)
  26.221 +
  26.222 +  def Y_seq' \<equiv> "\<lambda>n \<omega>. if \<omega> \<in> ?D then 0 else \<mu>.I n \<omega>"
  26.223 +  have Y_seq'_AE: "\<And>n. AE \<omega> in ?\<Omega>. Y_seq' n \<omega> = \<mu>.I n \<omega>"
  26.224 +    by (rule AE_I [OF _ D]) (auto simp: space_restrict_space sets_restrict_space_iff Y_seq'_def)
  26.225 +
  26.226 +  have Y'_cnv: "\<forall>\<omega>\<in>{0<..<1}. (\<lambda>n. Y_seq' n \<omega>) \<longlonglongrightarrow> Y' \<omega>"
  26.227 +    by (auto simp: Y'_def Y_seq'_def Y_cts_cnv)
  26.228 +
  26.229 +  have [simp]: "Y_seq' n \<in> borel_measurable ?\<Omega>" for n
  26.230 +    by (rule measurable_discrete_difference[of "\<mu>.I n" _ _ ?D])
  26.231 +       (insert \<mu>.measurable_CI[of n] D_countable, auto simp: sets_restrict_space Y_seq'_def)
  26.232 +  moreover have "distr ?\<Omega> borel (Y_seq' n) = \<mu> n" for n
  26.233 +    using \<mu>.distr_I_eq_M [of n] Y_seq'_AE [of n]
  26.234 +    by (subst distr_cong_AE[where f = "Y_seq' n" and g = "\<mu>.I n"], auto)
  26.235 +  moreover have [simp]: "Y' \<in> borel_measurable ?\<Omega>"
  26.236 +    by (rule measurable_discrete_difference[of M.I _ _ ?D])
  26.237 +       (insert M.measurable_CI D_countable, auto simp: sets_restrict_space Y'_def)
  26.238 +  moreover have "distr ?\<Omega> borel Y' = M"
  26.239 +    using M.distr_I_eq_M Y'_AE
  26.240 +    by (subst distr_cong_AE[where f = Y' and g = M.I], auto)
  26.241 +  ultimately have "prob_space ?\<Omega> \<and> (\<forall>n. Y_seq' n \<in> borel_measurable ?\<Omega>) \<and>
  26.242 +    (\<forall>n. distr ?\<Omega> borel (Y_seq' n) = \<mu> n) \<and> Y' \<in> measurable ?\<Omega> lborel \<and> distr ?\<Omega> borel Y' = M \<and>
  26.243 +    (\<forall>x\<in>space ?\<Omega>. (\<lambda>n. Y_seq' n x) \<longlonglongrightarrow> Y' x)"
  26.244 +    using Y'_cnv \<open>prob_space ?\<Omega>\<close> by (auto simp: space_restrict_space)
  26.245 +  thus ?thesis by metis
  26.246 +qed
  26.247 +
  26.248 +text \<open>
  26.249 +  The Portmanteau theorem, that is, the equivalence of various definitions of weak convergence.
  26.250 +\<close>
  26.251 +
  26.252 +theorem weak_conv_imp_bdd_ae_continuous_conv:
  26.253 +  fixes 
  26.254 +    f :: "real \<Rightarrow> 'a::{banach, second_countable_topology}"
  26.255 +  assumes 
  26.256 +    discont_null: "M ({x. \<not> isCont f x}) = 0" and
  26.257 +    f_bdd: "\<And>x. norm (f x) \<le> B" and
  26.258 +    [measurable]: "f \<in> borel_measurable borel"
  26.259 +  shows 
  26.260 +    "(\<lambda> n. integral\<^sup>L (\<mu> n) f) \<longlonglongrightarrow> integral\<^sup>L M f"
  26.261 +proof -
  26.262 +  have "0 \<le> B"
  26.263 +    using norm_ge_zero f_bdd by (rule order_trans)
  26.264 +  note Skorohod
  26.265 +  then obtain Omega Y_seq Y where
  26.266 +    ps_Omega [simp]: "prob_space Omega" and
  26.267 +    Y_seq_measurable [measurable]: "\<And>n. Y_seq n \<in> borel_measurable Omega" and
  26.268 +    distr_Y_seq: "\<And>n. distr Omega borel (Y_seq n) = \<mu> n" and
  26.269 +    Y_measurable [measurable]: "Y \<in> borel_measurable Omega" and
  26.270 +    distr_Y: "distr Omega borel Y = M" and
  26.271 +    YnY: "\<And>x :: real. x \<in> space Omega \<Longrightarrow> (\<lambda>n. Y_seq n x) \<longlonglongrightarrow> Y x"  by force
  26.272 +  interpret prob_space Omega by fact
  26.273 +  have *: "emeasure Omega (Y -` {x. \<not> isCont f x} \<inter> space Omega) = 0"
  26.274 +    by (subst emeasure_distr [symmetric, where N=borel]) (auto simp: distr_Y discont_null)
  26.275 +  have *: "AE x in Omega. (\<lambda>n. f (Y_seq n x)) \<longlonglongrightarrow> f (Y x)"
  26.276 +    by (rule AE_I [OF _ *]) (auto intro: isCont_tendsto_compose YnY)
  26.277 +  show ?thesis
  26.278 +    by (auto intro!: integral_dominated_convergence[where w="\<lambda>x. B"]
  26.279 +             simp: f_bdd * integral_distr distr_Y_seq [symmetric] distr_Y [symmetric])
  26.280 +qed
  26.281 +
  26.282 +theorem weak_conv_imp_integral_bdd_continuous_conv:
  26.283 +  fixes f :: "real \<Rightarrow> 'a::{banach, second_countable_topology}"
  26.284 +  assumes 
  26.285 +    "\<And>x. isCont f x" and
  26.286 +    "\<And>x. norm (f x) \<le> B"
  26.287 +  shows 
  26.288 +    "(\<lambda> n. integral\<^sup>L (\<mu> n) f) \<longlonglongrightarrow> integral\<^sup>L M f"
  26.289 +  using assms
  26.290 +  by (intro weak_conv_imp_bdd_ae_continuous_conv)
  26.291 +     (auto intro!: borel_measurable_continuous_on1 continuous_at_imp_continuous_on)
  26.292 +
  26.293 +theorem weak_conv_imp_continuity_set_conv:
  26.294 +  fixes f :: "real \<Rightarrow> real"
  26.295 +  assumes [measurable]: "A \<in> sets borel" and "M (frontier A) = 0"
  26.296 +  shows "(\<lambda>n. measure (\<mu> n) A) \<longlonglongrightarrow> measure M A"
  26.297 +proof -
  26.298 +  interpret M: real_distribution M by fact
  26.299 +  interpret \<mu>: real_distribution "\<mu> n" for n by fact
  26.300 +  
  26.301 +  have "(\<lambda>n. (\<integral>x. indicator A x \<partial>\<mu> n) :: real) \<longlonglongrightarrow> (\<integral>x. indicator A x \<partial>M)"
  26.302 +    by (intro weak_conv_imp_bdd_ae_continuous_conv[where B=1])
  26.303 +       (auto intro: assms simp: isCont_indicator)
  26.304 +  then show ?thesis
  26.305 +    by simp
  26.306 +qed
  26.307 +
  26.308 +end
  26.309 +
  26.310 +definition
  26.311 +  cts_step :: "real \<Rightarrow> real \<Rightarrow> real \<Rightarrow> real"
  26.312 +where
  26.313 +  "cts_step a b x \<equiv> if x \<le> a then 1 else if x \<ge> b then 0 else (b - x) / (b - a)"
  26.314 +
  26.315 +lemma cts_step_uniformly_continuous:
  26.316 +  assumes [arith]: "a < b"
  26.317 +  shows "uniformly_continuous_on UNIV (cts_step a b)"
  26.318 +  unfolding uniformly_continuous_on_def
  26.319 +proof clarsimp
  26.320 +  fix e :: real assume [arith]: "0 < e"
  26.321 +  let ?d = "min (e * (b - a)) (b - a)"
  26.322 +  have "?d > 0"
  26.323 +    by (auto simp add: field_simps)
  26.324 +  moreover have "dist x' x < ?d \<Longrightarrow> dist (cts_step a b x') (cts_step a b x) < e" for x x'
  26.325 +    by (auto simp: dist_real_def divide_simps cts_step_def)
  26.326 +  ultimately show "\<exists>d > 0. \<forall>x x'. dist x' x < d \<longrightarrow> dist (cts_step a b x') (cts_step a b x) < e"
  26.327 +    by blast
  26.328 +qed
  26.329 +
  26.330 +lemma (in real_distribution) integrable_cts_step: "a < b \<Longrightarrow> integrable M (cts_step a b)"
  26.331 +  by (rule integrable_const_bound [of _ 1]) (auto simp: cts_step_def[abs_def])
  26.332 +
  26.333 +lemma (in real_distribution) cdf_cts_step:
  26.334 +  assumes [arith]: "x < y"
  26.335 +  shows "cdf M x \<le> integral\<^sup>L M (cts_step x y)" and "integral\<^sup>L M (cts_step x y) \<le> cdf M y"
  26.336 +proof -
  26.337 +  have "cdf M x = integral\<^sup>L M (indicator {..x})"
  26.338 +    by (simp add: cdf_def)
  26.339 +  also have "\<dots> \<le> expectation (cts_step x y)"
  26.340 +    by (intro integral_mono integrable_cts_step) (auto simp: cts_step_def split: split_indicator)
  26.341 +  finally show "cdf M x \<le> expectation (cts_step x y)" .
  26.342 +next
  26.343 +  have "expectation (cts_step x y) \<le> integral\<^sup>L M (indicator {..y})"
  26.344 +    by (intro integral_mono integrable_cts_step) (auto simp: cts_step_def split: split_indicator)
  26.345 +  also have "\<dots> = cdf M y"
  26.346 +    by (simp add: cdf_def)
  26.347 +  finally show "expectation (cts_step x y) \<le> cdf M y" .
  26.348 +qed
  26.349 +
  26.350 +context
  26.351 +  fixes M_seq :: "nat \<Rightarrow> real measure"
  26.352 +    and M :: "real measure"
  26.353 +  assumes distr_M_seq [simp]: "\<And>n. real_distribution (M_seq n)" 
  26.354 +  assumes distr_M [simp]: "real_distribution M"
  26.355 +begin  
  26.356 +
  26.357 +theorem continuity_set_conv_imp_weak_conv:
  26.358 +  fixes f :: "real \<Rightarrow> real"
  26.359 +  assumes *: "\<And>A. A \<in> sets borel \<Longrightarrow> M (frontier A) = 0 \<Longrightarrow> (\<lambda> n. (measure (M_seq n) A)) \<longlonglongrightarrow> measure M A"
  26.360 +  shows "weak_conv_m M_seq M"
  26.361 +proof -
  26.362 +  interpret real_distribution M by simp
  26.363 +  show ?thesis
  26.364 +    by (auto intro!: * simp: frontier_real_Iic isCont_cdf emeasure_eq_measure weak_conv_m_def weak_conv_def cdf_def2)
  26.365 +qed
  26.366 +
  26.367 +theorem integral_cts_step_conv_imp_weak_conv:
  26.368 +  assumes integral_conv: "\<And>x y. x < y \<Longrightarrow> (\<lambda>n. integral\<^sup>L (M_seq n) (cts_step x y)) \<longlonglongrightarrow> integral\<^sup>L M (cts_step x y)"
  26.369 +  shows "weak_conv_m M_seq M"
  26.370 +  unfolding weak_conv_m_def weak_conv_def 
  26.371 +proof (clarsimp)
  26.372 +  interpret real_distribution M by (rule distr_M) 
  26.373 +  fix x assume "isCont (cdf M) x"
  26.374 +  hence left_cont: "continuous (at_left x) (cdf M)"
  26.375 +    unfolding continuous_at_split ..
  26.376 +  { fix y :: real assume [arith]: "x < y"
  26.377 +    have "limsup (\<lambda>n. cdf (M_seq n) x) \<le> limsup (\<lambda>n. integral\<^sup>L (M_seq n) (cts_step x y))"
  26.378 +      by (auto intro!: Limsup_mono always_eventually real_distribution.cdf_cts_step)
  26.379 +    also have "\<dots> = integral\<^sup>L M (cts_step x y)"
  26.380 +      by (intro lim_imp_Limsup) (auto intro: integral_conv)
  26.381 +    also have "\<dots> \<le> cdf M y"
  26.382 +      by (simp add: cdf_cts_step)
  26.383 +    finally have "limsup (\<lambda>n. cdf (M_seq n) x) \<le> cdf M y" .
  26.384 +  } note * = this
  26.385 +  { fix y :: real assume [arith]: "x > y"
  26.386 +    have "cdf M y \<le> ereal (integral\<^sup>L M (cts_step y x))"
  26.387 +      by (simp add: cdf_cts_step)
  26.388 +    also have "\<dots> = liminf (\<lambda>n. integral\<^sup>L (M_seq n) (cts_step y x))"
  26.389 +      by (intro lim_imp_Liminf[symmetric]) (auto intro: integral_conv)
  26.390 +    also have "\<dots> \<le> liminf (\<lambda>n. cdf (M_seq n) x)"
  26.391 +      by (auto intro!: Liminf_mono always_eventually real_distribution.cdf_cts_step)
  26.392 +    finally have "liminf (\<lambda>n. cdf (M_seq n) x) \<ge> cdf M y" .
  26.393 +  } note ** = this
  26.394 +
  26.395 +  have "limsup (\<lambda>n. cdf (M_seq n) x) \<le> cdf M x"
  26.396 +  proof (rule tendsto_le_const)
  26.397 +    show "\<forall>\<^sub>F i in at_right x. limsup (\<lambda>xa. ereal (cdf (M_seq xa) x)) \<le> ereal (cdf M i)"
  26.398 +      by (subst eventually_at_right[of _ "x + 1"]) (auto simp: * intro: exI [of _ "x+1"])
  26.399 +  qed (insert cdf_is_right_cont, auto simp: continuous_within)
  26.400 +  moreover have "cdf M x \<le> liminf (\<lambda>n. cdf (M_seq n) x)"
  26.401 +  proof (rule tendsto_ge_const)
  26.402 +    show "\<forall>\<^sub>F i in at_left x. ereal (cdf M i) \<le> liminf (\<lambda>xa. ereal (cdf (M_seq xa) x))"
  26.403 +      by (subst eventually_at_left[of "x - 1"]) (auto simp: ** intro: exI [of _ "x-1"])
  26.404 +  qed (insert left_cont, auto simp: continuous_within)
  26.405 +  ultimately show "(\<lambda>n. cdf (M_seq n) x) \<longlonglongrightarrow> cdf M x"
  26.406 +    by (elim limsup_le_liminf_real) 
  26.407 +qed
  26.408 +
  26.409 +theorem integral_bdd_continuous_conv_imp_weak_conv:
  26.410 +  assumes 
  26.411 +    "\<And>f. (\<And>x. isCont f x) \<Longrightarrow> (\<And>x. abs (f x) \<le> 1) \<Longrightarrow> (\<lambda>n. integral\<^sup>L (M_seq n) f::real) \<longlonglongrightarrow> integral\<^sup>L M f"
  26.412 +  shows 
  26.413 +    "weak_conv_m M_seq M"
  26.414 +  apply (rule integral_cts_step_conv_imp_weak_conv [OF assms])
  26.415 +  apply (rule continuous_on_interior)
  26.416 +  apply (rule uniformly_continuous_imp_continuous)
  26.417 +  apply (rule cts_step_uniformly_continuous)
  26.418 +  apply (auto simp: cts_step_def)
  26.419 +  done
  26.420 +
  26.421 +end
  26.422 +
  26.423 +end
    27.1 --- a/src/HOL/Real.thy	Wed Jan 06 13:04:31 2016 +0100
    27.2 +++ b/src/HOL/Real.thy	Wed Jan 06 12:18:53 2016 +0100
    27.3 @@ -1583,7 +1583,6 @@
    27.4    "\<lceil>numeral x ^ n\<rceil> = numeral x ^ n"
    27.5    by (metis ceiling_of_int of_int_numeral of_int_power)
    27.6  
    27.7 -
    27.8  subsection \<open>Implementation of rational real numbers\<close>
    27.9  
   27.10  text \<open>Formal constructor\<close>
    28.1 --- a/src/HOL/Set.thy	Wed Jan 06 13:04:31 2016 +0100
    28.2 +++ b/src/HOL/Set.thy	Wed Jan 06 12:18:53 2016 +0100
    28.3 @@ -989,6 +989,9 @@
    28.4  lemma Setcompr_eq_image: "{f x | x. x \<in> A} = f ` A"
    28.5    by blast
    28.6  
    28.7 +lemma setcompr_eq_image: "{f x |x. P x} = f ` {x. P x}"
    28.8 +  by auto
    28.9 +
   28.10  lemma ball_imageD:
   28.11    assumes "\<forall>x\<in>f ` A. P x"
   28.12    shows "\<forall>x\<in>A. P (f x)"
    29.1 --- a/src/HOL/Topological_Spaces.thy	Wed Jan 06 13:04:31 2016 +0100
    29.2 +++ b/src/HOL/Topological_Spaces.thy	Wed Jan 06 12:18:53 2016 +0100
    29.3 @@ -453,6 +453,9 @@
    29.4    "x < y \<Longrightarrow> eventually P (at_right x) \<longleftrightarrow> (\<exists>b>x. \<forall>y>x. y < b \<longrightarrow> P y)"
    29.5    unfolding at_right_eq by (subst eventually_INF_base) (auto simp: eventually_principal Ball_def)
    29.6  
    29.7 +lemma eventually_at_right_less: "\<forall>\<^sub>F y in at_right (x::'a::{linorder_topology, no_top}). x < y"
    29.8 +  using gt_ex[of x] eventually_at_right[of x] by auto
    29.9 +
   29.10  lemma trivial_limit_at_right_top: "at_right (top::_::{order_top, linorder_topology}) = bot"
   29.11    unfolding filter_eq_iff eventually_at_topological by auto
   29.12  
   29.13 @@ -1610,6 +1613,9 @@
   29.14  lemma continuous_on_eq_continuous_at: "open s \<Longrightarrow> continuous_on s f \<longleftrightarrow> (\<forall>x\<in>s. isCont f x)"
   29.15    by (simp add: continuous_on_def continuous_at at_within_open[of _ s])
   29.16  
   29.17 +lemma continuous_within_open: "a \<in> A \<Longrightarrow> open A \<Longrightarrow> continuous (at a within A) f \<longleftrightarrow> isCont f a"
   29.18 +  by (simp add: at_within_open_NO_MATCH)
   29.19 +
   29.20  lemma continuous_at_imp_continuous_on: "\<forall>x\<in>s. isCont f x \<Longrightarrow> continuous_on s f"
   29.21    by (auto intro: continuous_at_imp_continuous_at_within simp: continuous_on_eq_continuous_within)
   29.22  
    30.1 --- a/src/HOL/Transcendental.thy	Wed Jan 06 13:04:31 2016 +0100
    30.2 +++ b/src/HOL/Transcendental.thy	Wed Jan 06 12:18:53 2016 +0100
    30.3 @@ -10,6 +10,25 @@
    30.4  imports Binomial Series Deriv NthRoot
    30.5  begin
    30.6  
    30.7 +text \<open>A fact theorem on reals.\<close>
    30.8 +
    30.9 +lemma square_fact_le_2_fact: 
   30.10 +  shows "fact n * fact n \<le> (fact (2 * n) :: real)"
   30.11 +proof (induct n)
   30.12 +  case 0 then show ?case by simp
   30.13 +next
   30.14 +  case (Suc n)
   30.15 +  have "(fact (Suc n)) * (fact (Suc n)) = of_nat (Suc n) * of_nat (Suc n) * (fact n * fact n :: real)"
   30.16 +    by (simp add: field_simps)
   30.17 +  also have "\<dots> \<le> of_nat (Suc n) * of_nat (Suc n) * fact (2 * n)"
   30.18 +    by (rule mult_left_mono [OF Suc]) simp
   30.19 +  also have "\<dots> \<le> of_nat (Suc (Suc (2 * n))) * of_nat (Suc (2 * n)) * fact (2 * n)"
   30.20 +    by (rule mult_right_mono)+ (auto simp: field_simps)
   30.21 +  also have "\<dots> = fact (2 * Suc n)" by (simp add: field_simps)
   30.22 +  finally show ?case .
   30.23 +qed
   30.24 +
   30.25 +
   30.26  lemma of_int_leD: 
   30.27    fixes x :: "'a :: linordered_idom"
   30.28    shows "\<bar>of_int n\<bar> \<le> x \<Longrightarrow> n=0 \<or> x\<ge>1"