moved material from AFP to distribution
authoreberlm <eberlm@in.tum.de>
Tue, 04 Apr 2017 08:57:21 +0200
changeset 65395 7504569a73c7
parent 65354 4ff2ba82d668
child 65396 b42167902f57
moved material from AFP to distribution
src/HOL/Analysis/Harmonic_Numbers.thy
src/HOL/Probability/Probability_Mass_Function.thy
src/HOL/Probability/Random_Permutations.thy
--- a/src/HOL/Analysis/Harmonic_Numbers.thy	Mon Apr 03 22:18:56 2017 +0200
+++ b/src/HOL/Analysis/Harmonic_Numbers.thy	Tue Apr 04 08:57:21 2017 +0200
@@ -37,6 +37,9 @@
 lemma of_real_harm: "of_real (harm n) = harm n"
   unfolding harm_def by simp
 
+lemma abs_harm [simp]: "(abs (harm n) :: real) = harm n"
+  using harm_nonneg[of n] by (rule abs_of_nonneg)    
+
 lemma norm_harm: "norm (harm n) = harm n"
   by (subst of_real_harm [symmetric]) (simp add: harm_nonneg)
 
@@ -91,6 +94,15 @@
   finally show "ln (real (Suc n) + 1) \<le> harm (Suc n)" by - simp
 qed (simp_all add: harm_def)
 
+lemma harm_at_top: "filterlim (harm :: nat \<Rightarrow> real) at_top sequentially"
+proof (rule filterlim_at_top_mono)
+  show "eventually (\<lambda>n. harm n \<ge> ln (real (Suc n))) at_top"
+    using ln_le_harm by (intro always_eventually allI) (simp_all add: add_ac)
+  show "filterlim (\<lambda>n. ln (real (Suc n))) at_top sequentially"
+    by (intro filterlim_compose[OF ln_at_top] filterlim_compose[OF filterlim_real_sequentially] 
+              filterlim_Suc)
+qed
+
 
 subsection \<open>The Euler--Mascheroni constant\<close>
 
--- a/src/HOL/Probability/Probability_Mass_Function.thy	Mon Apr 03 22:18:56 2017 +0200
+++ b/src/HOL/Probability/Probability_Mass_Function.thy	Tue Apr 04 08:57:21 2017 +0200
@@ -663,6 +663,7 @@
 lemma measurable_set_pmf[measurable]: "Measurable.pred (count_space UNIV) (\<lambda>x. x \<in> set_pmf M)"
   by simp
 
+
 subsection \<open> PMFs as function \<close>
 
 context
@@ -754,6 +755,39 @@
   apply (subst lebesgue_integral_count_space_finite_support)
   apply (auto intro!: finite_subset[OF _ \<open>finite A\<close>] sum.mono_neutral_left simp: pmf_eq_0_set_pmf)
   done
+    
+lemma expectation_return_pmf [simp]:
+  fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}"
+  shows "measure_pmf.expectation (return_pmf x) f = f x"
+  by (subst integral_measure_pmf[of "{x}"]) simp_all
+
+lemma pmf_expectation_bind:
+  fixes p :: "'a pmf" and f :: "'a \<Rightarrow> 'b pmf"
+    and  h :: "'b \<Rightarrow> 'c::{banach, second_countable_topology}"
+  assumes "finite A" "\<And>x. x \<in> A \<Longrightarrow> finite (set_pmf (f x))" "set_pmf p \<subseteq> A"
+  shows "measure_pmf.expectation (p \<bind> f) h =
+           (\<Sum>a\<in>A. pmf p a *\<^sub>R measure_pmf.expectation (f a) h)"
+proof -
+  have "measure_pmf.expectation (p \<bind> f) h = (\<Sum>a\<in>(\<Union>x\<in>A. set_pmf (f x)). pmf (p \<bind> f) a *\<^sub>R h a)"
+    using assms by (intro integral_measure_pmf) auto
+  also have "\<dots> = (\<Sum>x\<in>(\<Union>x\<in>A. set_pmf (f x)). (\<Sum>a\<in>A. (pmf p a * pmf (f a) x) *\<^sub>R h x))"
+  proof (intro sum.cong refl, goal_cases)
+    case (1 x)
+    thus ?case
+      by (subst pmf_bind, subst integral_measure_pmf[of A]) 
+         (insert assms, auto simp: scaleR_sum_left)
+  qed
+  also have "\<dots> = (\<Sum>j\<in>A. pmf p j *\<^sub>R (\<Sum>i\<in>(\<Union>x\<in>A. set_pmf (f x)). pmf (f j) i *\<^sub>R h i))"
+    by (subst sum.commute) (simp add: scaleR_sum_right)
+  also have "\<dots> = (\<Sum>j\<in>A. pmf p j *\<^sub>R measure_pmf.expectation (f j) h)"
+  proof (intro sum.cong refl, goal_cases)
+    case (1 x)
+    thus ?case
+      by (subst integral_measure_pmf[of "(\<Union>x\<in>A. set_pmf (f x))"]) 
+         (insert assms, auto simp: scaleR_sum_left)
+  qed
+  finally show ?thesis .
+qed
 
 lemma continuous_on_LINT_pmf: -- \<open>This is dominated convergence!?\<close>
   fixes f :: "'i \<Rightarrow> 'a::topological_space \<Rightarrow> 'b::{banach, second_countable_topology}"
@@ -1725,6 +1759,14 @@
   by (simp add: measure_nonneg measure_pmf.emeasure_eq_measure)
 
 end
+  
+lemma pmf_expectation_bind_pmf_of_set:
+  fixes A :: "'a set" and f :: "'a \<Rightarrow> 'b pmf"
+    and  h :: "'b \<Rightarrow> 'c::{banach, second_countable_topology}"
+  assumes "A \<noteq> {}" "finite A" "\<And>x. x \<in> A \<Longrightarrow> finite (set_pmf (f x))"
+  shows "measure_pmf.expectation (pmf_of_set A \<bind> f) h =
+           (\<Sum>a\<in>A. measure_pmf.expectation (f a) h /\<^sub>R real (card A))"
+  using assms by (subst pmf_expectation_bind[of A]) (auto simp: divide_simps)
 
 lemma map_pmf_of_set:
   assumes "finite A" "A \<noteq> {}"
@@ -1773,6 +1815,16 @@
   qed
 qed
 
+lemma map_pmf_of_set_bij_betw:
+  assumes "bij_betw f A B" "A \<noteq> {}" "finite A"
+  shows   "map_pmf f (pmf_of_set A) = pmf_of_set B"
+proof -
+  have "map_pmf f (pmf_of_set A) = pmf_of_set (f ` A)"
+    by (intro map_pmf_of_set_inj assms bij_betw_imp_inj_on[OF assms(1)])
+  also from assms have "f ` A = B" by (simp add: bij_betw_def)
+  finally show ?thesis .
+qed
+
 text \<open>
   Choosing an element uniformly at random from the union of a disjoint family
   of finite non-empty sets with the same size is the same as first choosing a set
--- a/src/HOL/Probability/Random_Permutations.thy	Mon Apr 03 22:18:56 2017 +0200
+++ b/src/HOL/Probability/Random_Permutations.thy	Tue Apr 04 08:57:21 2017 +0200
@@ -176,4 +176,56 @@
   using assms by (simp add: fold_bind_random_permutation_altdef bind_assoc_pmf
                             fold_random_permutation_fold bind_return_pmf map_pmf_def)
 
+text \<open>
+  The following useful lemma allows us to swap partitioning a set w.\,r.\,t.\ a 
+  predicate and drawing a random permutation of that set.
+\<close>
+lemma partition_random_permutations:
+  assumes "finite A"
+  shows   "map_pmf (partition P) (pmf_of_set (permutations_of_set A)) = 
+             pair_pmf (pmf_of_set (permutations_of_set {x\<in>A. P x}))
+                      (pmf_of_set (permutations_of_set {x\<in>A. \<not>P x}))" (is "?lhs = ?rhs")
+proof (rule pmf_eqI, clarify, goal_cases)
+  case (1 xs ys)
+  show ?case
+  proof (cases "xs \<in> permutations_of_set {x\<in>A. P x} \<and> ys \<in> permutations_of_set {x\<in>A. \<not>P x}")
+    case True
+    let ?n1 = "card {x\<in>A. P x}" and ?n2 = "card {x\<in>A. \<not>P x}"
+    have card_eq: "card A = ?n1 + ?n2"
+    proof -
+      have "?n1 + ?n2 = card ({x\<in>A. P x} \<union> {x\<in>A. \<not>P x})"
+        using assms by (intro card_Un_disjoint [symmetric]) auto
+      also have "{x\<in>A. P x} \<union> {x\<in>A. \<not>P x} = A" by blast
+      finally show ?thesis ..
+    qed
+
+    from True have lengths [simp]: "length xs = ?n1" "length ys = ?n2"
+      by (auto intro!: length_finite_permutations_of_set)
+    have "pmf ?lhs (xs, ys) = 
+            real (card (permutations_of_set A \<inter> partition P -` {(xs, ys)})) / fact (card A)"
+      using assms by (auto simp: pmf_map measure_pmf_of_set)
+    also have "partition P -` {(xs, ys)} = shuffle xs ys" 
+      using True by (intro inv_image_partition) (auto simp: permutations_of_set_def)
+    also have "permutations_of_set A \<inter> shuffle xs ys = shuffle xs ys"
+      using True distinct_disjoint_shuffle[of xs ys] 
+      by (auto simp: permutations_of_set_def dest: set_shuffle)
+    also have "card (shuffle xs ys) = length xs + length ys choose length xs"
+      using True by (intro card_disjoint_shuffle) (auto simp: permutations_of_set_def)
+    also have "length xs + length ys = card A" by (simp add: card_eq)
+    also have "real (card A choose length xs) = fact (card A) / (fact ?n1 * fact (card A - ?n1))"
+      by (subst binomial_fact) (auto intro!: card_mono assms)
+    also have "\<dots> / fact (card A) = 1 / (fact ?n1 * fact ?n2)"
+      by (simp add: divide_simps card_eq)
+    also have "\<dots> = pmf ?rhs (xs, ys)" using True assms by (simp add: pmf_pair)
+    finally show ?thesis .
+  next
+    case False
+    hence *: "xs \<notin> permutations_of_set {x\<in>A. P x} \<or> ys \<notin> permutations_of_set {x\<in>A. \<not>P x}" by blast
+    hence eq: "permutations_of_set A \<inter> (partition P -` {(xs, ys)}) = {}"
+      by (auto simp: o_def permutations_of_set_def)
+    from * show ?thesis
+      by (elim disjE) (insert assms eq, simp_all add: pmf_pair pmf_map measure_pmf_of_set)
+  qed
+qed
+
 end