author  Andreas Lochbihler 
Fri, 21 Nov 2014 12:11:44 +0100  
changeset 59023  4999a616336c 
parent 59002  2c8b2fb54b88 
child 59024  5fcfeae84b96 
permissions  rwrr 
58606  1 
(* Title: HOL/Probability/Probability_Mass_Function.thy 
59023  2 
Author: Johannes Hölzl, TU München 
3 
Author: Andreas Lochbihler, ETH Zurich 

4 
*) 

58606  5 

59000  6 
section \<open> Probability mass function \<close> 
7 

58587
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

8 
theory Probability_Mass_Function 
59000  9 
imports 
10 
Giry_Monad 

11 
"~~/src/HOL/Library/Multiset" 

58587
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

12 
begin 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

13 

59000  14 
lemma (in finite_measure) countable_support: (* replace version in pmf *) 
58587
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

15 
"countable {x. measure M {x} \<noteq> 0}" 
59000  16 
proof cases 
17 
assume "measure M (space M) = 0" 

18 
with bounded_measure measure_le_0_iff have "{x. measure M {x} \<noteq> 0} = {}" 

19 
by auto 

20 
then show ?thesis 

21 
by simp 

22 
next 

23 
let ?M = "measure M (space M)" and ?m = "\<lambda>x. measure M {x}" 

24 
assume "?M \<noteq> 0" 

25 
then have *: "{x. ?m x \<noteq> 0} = (\<Union>n. {x. ?M / Suc n < ?m x})" 

26 
using reals_Archimedean[of "?m x / ?M" for x] 

27 
by (auto simp: field_simps not_le[symmetric] measure_nonneg divide_le_0_iff measure_le_0_iff) 

28 
have **: "\<And>n. finite {x. ?M / Suc n < ?m x}" 

58587
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

29 
proof (rule ccontr) 
59000  30 
fix n assume "infinite {x. ?M / Suc n < ?m x}" (is "infinite ?X") 
58587
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

31 
then obtain X where "finite X" "card X = Suc (Suc n)" "X \<subseteq> ?X" 
58606  32 
by (metis infinite_arbitrarily_large) 
59000  33 
from this(3) have *: "\<And>x. x \<in> X \<Longrightarrow> ?M / Suc n \<le> ?m x" 
34 
by auto 

58587
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

35 
{ fix x assume "x \<in> X" 
59000  36 
from `?M \<noteq> 0` *[OF this] have "?m x \<noteq> 0" by (auto simp: field_simps measure_le_0_iff) 
58587
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

37 
then have "{x} \<in> sets M" by (auto dest: measure_notin_sets) } 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

38 
note singleton_sets = this 
59000  39 
have "?M < (\<Sum>x\<in>X. ?M / Suc n)" 
40 
using `?M \<noteq> 0` 

41 
by (simp add: `card X = Suc (Suc n)` real_eq_of_nat[symmetric] real_of_nat_Suc field_simps less_le measure_nonneg) 

58587
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

42 
also have "\<dots> \<le> (\<Sum>x\<in>X. ?m x)" 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

43 
by (rule setsum_mono) fact 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

44 
also have "\<dots> = measure M (\<Union>x\<in>X. {x})" 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

45 
using singleton_sets `finite X` 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

46 
by (intro finite_measure_finite_Union[symmetric]) (auto simp: disjoint_family_on_def) 
59000  47 
finally have "?M < measure M (\<Union>x\<in>X. {x})" . 
48 
moreover have "measure M (\<Union>x\<in>X. {x}) \<le> ?M" 

49 
using singleton_sets[THEN sets.sets_into_space] by (intro finite_measure_mono) auto 

50 
ultimately show False by simp 

58587
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

51 
qed 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

52 
show ?thesis 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

53 
unfolding * by (intro countable_UN countableI_type countable_finite[OF **]) 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

54 
qed 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

55 

59000  56 
lemma (in finite_measure) AE_support_countable: 
57 
assumes [simp]: "sets M = UNIV" 

58 
shows "(AE x in M. measure M {x} \<noteq> 0) \<longleftrightarrow> (\<exists>S. countable S \<and> (AE x in M. x \<in> S))" 

59 
proof 

60 
assume "\<exists>S. countable S \<and> (AE x in M. x \<in> S)" 

61 
then obtain S where S[intro]: "countable S" and ae: "AE x in M. x \<in> S" 

62 
by auto 

63 
then have "emeasure M (\<Union>x\<in>{x\<in>S. emeasure M {x} \<noteq> 0}. {x}) = 

64 
(\<integral>\<^sup>+ x. emeasure M {x} * indicator {x\<in>S. emeasure M {x} \<noteq> 0} x \<partial>count_space UNIV)" 

65 
by (subst emeasure_UN_countable) 

66 
(auto simp: disjoint_family_on_def nn_integral_restrict_space[symmetric] restrict_count_space) 

67 
also have "\<dots> = (\<integral>\<^sup>+ x. emeasure M {x} * indicator S x \<partial>count_space UNIV)" 

68 
by (auto intro!: nn_integral_cong split: split_indicator) 

69 
also have "\<dots> = emeasure M (\<Union>x\<in>S. {x})" 

70 
by (subst emeasure_UN_countable) 

71 
(auto simp: disjoint_family_on_def nn_integral_restrict_space[symmetric] restrict_count_space) 

72 
also have "\<dots> = emeasure M (space M)" 

73 
using ae by (intro emeasure_eq_AE) auto 

74 
finally have "emeasure M {x \<in> space M. x\<in>S \<and> emeasure M {x} \<noteq> 0} = emeasure M (space M)" 

75 
by (simp add: emeasure_single_in_space cong: rev_conj_cong) 

76 
with finite_measure_compl[of "{x \<in> space M. x\<in>S \<and> emeasure M {x} \<noteq> 0}"] 

77 
have "AE x in M. x \<in> S \<and> emeasure M {x} \<noteq> 0" 

78 
by (intro AE_I[OF order_refl]) (auto simp: emeasure_eq_measure set_diff_eq cong: conj_cong) 

79 
then show "AE x in M. measure M {x} \<noteq> 0" 

80 
by (auto simp: emeasure_eq_measure) 

81 
qed (auto intro!: exI[of _ "{x. measure M {x} \<noteq> 0}"] countable_support) 

82 

83 
subsection {* PMF as measure *} 

84 

58587
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

85 
typedef 'a pmf = "{M :: 'a measure. prob_space M \<and> sets M = UNIV \<and> (AE x in M. measure M {x} \<noteq> 0)}" 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

86 
morphisms measure_pmf Abs_pmf 
58606  87 
by (intro exI[of _ "uniform_measure (count_space UNIV) {undefined}"]) 
88 
(auto intro!: prob_space_uniform_measure AE_uniform_measureI) 

58587
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

89 

5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

90 
declare [[coercion measure_pmf]] 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

91 

5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

92 
lemma prob_space_measure_pmf: "prob_space (measure_pmf p)" 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

93 
using pmf.measure_pmf[of p] by auto 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

94 

5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

95 
interpretation measure_pmf!: prob_space "measure_pmf M" for M 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

96 
by (rule prob_space_measure_pmf) 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

97 

59000  98 
interpretation measure_pmf!: subprob_space "measure_pmf M" for M 
99 
by (rule prob_space_imp_subprob_space) unfold_locales 

100 

58587
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

101 
locale pmf_as_measure 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

102 
begin 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

103 

5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

104 
setup_lifting type_definition_pmf 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

105 

5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

106 
end 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

107 

5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

108 
context 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

109 
begin 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

110 

5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

111 
interpretation pmf_as_measure . 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

112 

5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

113 
lift_definition pmf :: "'a pmf \<Rightarrow> 'a \<Rightarrow> real" is "\<lambda>M x. measure M {x}" . 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

114 

5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

115 
lift_definition set_pmf :: "'a pmf \<Rightarrow> 'a set" is "\<lambda>M. {x. measure M {x} \<noteq> 0}" . 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

116 

5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

117 
lift_definition map_pmf :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a pmf \<Rightarrow> 'b pmf" is 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

118 
"\<lambda>f M. distr M (count_space UNIV) f" 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

119 
proof safe 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

120 
fix M and f :: "'a \<Rightarrow> 'b" 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

121 
let ?D = "distr M (count_space UNIV) f" 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

122 
assume "prob_space M" and [simp]: "sets M = UNIV" and ae: "AE x in M. measure M {x} \<noteq> 0" 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

123 
interpret prob_space M by fact 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

124 
from ae have "AE x in M. measure M (f ` {f x}) \<noteq> 0" 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

125 
proof eventually_elim 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

126 
fix x 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

127 
have "measure M {x} \<le> measure M (f ` {f x})" 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

128 
by (intro finite_measure_mono) auto 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

129 
then show "measure M {x} \<noteq> 0 \<Longrightarrow> measure M (f ` {f x}) \<noteq> 0" 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

130 
using measure_nonneg[of M "{x}"] by auto 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

131 
qed 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

132 
then show "AE x in ?D. measure ?D {x} \<noteq> 0" 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

133 
by (simp add: AE_distr_iff measure_distr measurable_def) 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

134 
qed (auto simp: measurable_def prob_space.prob_space_distr) 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

135 

5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

136 
declare [[coercion set_pmf]] 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

137 

59023  138 
lemma countable_set_pmf [simp]: "countable (set_pmf p)" 
59000  139 
by transfer (metis prob_space.finite_measure finite_measure.countable_support) 
58587
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

140 

5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

141 
lemma sets_measure_pmf[simp]: "sets (measure_pmf p) = UNIV" 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

142 
by transfer metis 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

143 

59000  144 
lemma sets_measure_pmf_count_space: "sets (measure_pmf M) = sets (count_space UNIV)" 
145 
by simp 

146 

58587
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

147 
lemma space_measure_pmf[simp]: "space (measure_pmf p) = UNIV" 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

148 
using sets_eq_imp_space_eq[of "measure_pmf p" "count_space UNIV"] by simp 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

149 

5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

150 
lemma measurable_pmf_measure1[simp]: "measurable (M :: 'a pmf) N = UNIV \<rightarrow> space N" 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

151 
by (auto simp: measurable_def) 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

152 

5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

153 
lemma measurable_pmf_measure2[simp]: "measurable N (M :: 'a pmf) = measurable N (count_space UNIV)" 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

154 
by (intro measurable_cong_sets) simp_all 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

155 

5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

156 
lemma pmf_positive: "x \<in> set_pmf p \<Longrightarrow> 0 < pmf p x" 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

157 
by transfer (simp add: less_le measure_nonneg) 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

158 

5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

159 
lemma pmf_nonneg: "0 \<le> pmf p x" 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

160 
by transfer (simp add: measure_nonneg) 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

161 

59000  162 
lemma pmf_le_1: "pmf p x \<le> 1" 
163 
by (simp add: pmf.rep_eq) 

164 

58587
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

165 
lemma emeasure_pmf_single: 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

166 
fixes M :: "'a pmf" 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

167 
shows "emeasure M {x} = pmf M x" 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

168 
by transfer (simp add: finite_measure.emeasure_eq_measure[OF prob_space.finite_measure]) 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

169 

5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

170 
lemma AE_measure_pmf: "AE x in (M::'a pmf). x \<in> M" 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

171 
by transfer simp 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

172 

5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

173 
lemma emeasure_pmf_single_eq_zero_iff: 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

174 
fixes M :: "'a pmf" 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

175 
shows "emeasure M {y} = 0 \<longleftrightarrow> y \<notin> M" 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

176 
by transfer (simp add: finite_measure.emeasure_eq_measure[OF prob_space.finite_measure]) 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

177 

5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

178 
lemma AE_measure_pmf_iff: "(AE x in measure_pmf M. P x) \<longleftrightarrow> (\<forall>y\<in>M. P y)" 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

179 
proof  
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

180 
{ fix y assume y: "y \<in> M" and P: "AE x in M. P x" "\<not> P y" 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

181 
with P have "AE x in M. x \<noteq> y" 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

182 
by auto 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

183 
with y have False 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

184 
by (simp add: emeasure_pmf_single_eq_zero_iff AE_iff_measurable[OF _ refl]) } 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

185 
then show ?thesis 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

186 
using AE_measure_pmf[of M] by auto 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

187 
qed 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

188 

5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

189 
lemma set_pmf_not_empty: "set_pmf M \<noteq> {}" 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

190 
using AE_measure_pmf[of M] by (intro notI) simp 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

191 

5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

192 
lemma set_pmf_iff: "x \<in> set_pmf M \<longleftrightarrow> pmf M x \<noteq> 0" 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

193 
by transfer simp 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

194 

59000  195 
lemma emeasure_measure_pmf_finite: "finite S \<Longrightarrow> emeasure (measure_pmf M) S = (\<Sum>s\<in>S. pmf M s)" 
196 
by (subst emeasure_eq_setsum_singleton) (auto simp: emeasure_pmf_single) 

197 

59023  198 
lemma measure_measure_pmf_finite: "finite S \<Longrightarrow> measure (measure_pmf M) S = setsum (pmf M) S" 
199 
using emeasure_measure_pmf_finite[of S M] 

200 
by(simp add: measure_pmf.emeasure_eq_measure) 

201 

59000  202 
lemma nn_integral_measure_pmf_support: 
203 
fixes f :: "'a \<Rightarrow> ereal" 

204 
assumes f: "finite A" and nn: "\<And>x. x \<in> A \<Longrightarrow> 0 \<le> f x" "\<And>x. x \<in> set_pmf M \<Longrightarrow> x \<notin> A \<Longrightarrow> f x = 0" 

205 
shows "(\<integral>\<^sup>+x. f x \<partial>measure_pmf M) = (\<Sum>x\<in>A. f x * pmf M x)" 

206 
proof  

207 
have "(\<integral>\<^sup>+x. f x \<partial>M) = (\<integral>\<^sup>+x. f x * indicator A x \<partial>M)" 

208 
using nn by (intro nn_integral_cong_AE) (auto simp: AE_measure_pmf_iff split: split_indicator) 

209 
also have "\<dots> = (\<Sum>x\<in>A. f x * emeasure M {x})" 

210 
using assms by (intro nn_integral_indicator_finite) auto 

211 
finally show ?thesis 

212 
by (simp add: emeasure_measure_pmf_finite) 

213 
qed 

214 

215 
lemma nn_integral_measure_pmf_finite: 

216 
fixes f :: "'a \<Rightarrow> ereal" 

217 
assumes f: "finite (set_pmf M)" and nn: "\<And>x. x \<in> set_pmf M \<Longrightarrow> 0 \<le> f x" 

218 
shows "(\<integral>\<^sup>+x. f x \<partial>measure_pmf M) = (\<Sum>x\<in>set_pmf M. f x * pmf M x)" 

219 
using assms by (intro nn_integral_measure_pmf_support) auto 

220 
lemma integrable_measure_pmf_finite: 

221 
fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}" 

222 
shows "finite (set_pmf M) \<Longrightarrow> integrable M f" 

223 
by (auto intro!: integrableI_bounded simp: nn_integral_measure_pmf_finite) 

224 

225 
lemma integral_measure_pmf: 

226 
assumes [simp]: "finite A" and "\<And>a. a \<in> set_pmf M \<Longrightarrow> f a \<noteq> 0 \<Longrightarrow> a \<in> A" 

227 
shows "(\<integral>x. f x \<partial>measure_pmf M) = (\<Sum>a\<in>A. f a * pmf M a)" 

228 
proof  

229 
have "(\<integral>x. f x \<partial>measure_pmf M) = (\<integral>x. f x * indicator A x \<partial>measure_pmf M)" 

230 
using assms(2) by (intro integral_cong_AE) (auto split: split_indicator simp: AE_measure_pmf_iff) 

231 
also have "\<dots> = (\<Sum>a\<in>A. f a * pmf M a)" 

232 
by (subst integral_indicator_finite_real) (auto simp: measure_def emeasure_measure_pmf_finite) 

233 
finally show ?thesis . 

234 
qed 

235 

236 
lemma integrable_pmf: "integrable (count_space X) (pmf M)" 

237 
proof  

238 
have " (\<integral>\<^sup>+ x. pmf M x \<partial>count_space X) = (\<integral>\<^sup>+ x. pmf M x \<partial>count_space (M \<inter> X))" 

239 
by (auto simp add: nn_integral_count_space_indicator set_pmf_iff intro!: nn_integral_cong split: split_indicator) 

240 
then have "integrable (count_space X) (pmf M) = integrable (count_space (M \<inter> X)) (pmf M)" 

241 
by (simp add: integrable_iff_bounded pmf_nonneg) 

242 
then show ?thesis 

59023  243 
by (simp add: pmf.rep_eq measure_pmf.integrable_measure disjoint_family_on_def) 
59000  244 
qed 
245 

246 
lemma integral_pmf: "(\<integral>x. pmf M x \<partial>count_space X) = measure M X" 

247 
proof  

248 
have "(\<integral>x. pmf M x \<partial>count_space X) = (\<integral>\<^sup>+x. pmf M x \<partial>count_space X)" 

249 
by (simp add: pmf_nonneg integrable_pmf nn_integral_eq_integral) 

250 
also have "\<dots> = (\<integral>\<^sup>+x. emeasure M {x} \<partial>count_space (X \<inter> M))" 

251 
by (auto intro!: nn_integral_cong_AE split: split_indicator 

252 
simp: pmf.rep_eq measure_pmf.emeasure_eq_measure nn_integral_count_space_indicator 

253 
AE_count_space set_pmf_iff) 

254 
also have "\<dots> = emeasure M (X \<inter> M)" 

255 
by (rule emeasure_countable_singleton[symmetric]) (auto intro: countable_set_pmf) 

256 
also have "\<dots> = emeasure M X" 

257 
by (auto intro!: emeasure_eq_AE simp: AE_measure_pmf_iff) 

258 
finally show ?thesis 

259 
by (simp add: measure_pmf.emeasure_eq_measure) 

260 
qed 

261 

262 
lemma integral_pmf_restrict: 

263 
"(f::'a \<Rightarrow> 'b::{banach, second_countable_topology}) \<in> borel_measurable (count_space UNIV) \<Longrightarrow> 

264 
(\<integral>x. f x \<partial>measure_pmf M) = (\<integral>x. f x \<partial>restrict_space M M)" 

265 
by (auto intro!: integral_cong_AE simp add: integral_restrict_space AE_measure_pmf_iff) 

266 

58587
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

267 
lemma emeasure_pmf: "emeasure (M::'a pmf) M = 1" 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

268 
proof  
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

269 
have "emeasure (M::'a pmf) M = emeasure (M::'a pmf) (space M)" 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

270 
by (intro emeasure_eq_AE) (simp_all add: AE_measure_pmf) 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

271 
then show ?thesis 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

272 
using measure_pmf.emeasure_space_1 by simp 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

273 
qed 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

274 

59023  275 
lemma in_null_sets_measure_pmfI: 
276 
"A \<inter> set_pmf p = {} \<Longrightarrow> A \<in> null_sets (measure_pmf p)" 

277 
using emeasure_eq_0_AE[where ?P="\<lambda>x. x \<in> A" and M="measure_pmf p"] 

278 
by(auto simp add: null_sets_def AE_measure_pmf_iff) 

279 

58587
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

280 
lemma map_pmf_id[simp]: "map_pmf id = id" 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

281 
by (rule, transfer) (auto simp: emeasure_distr measurable_def intro!: measure_eqI) 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

282 

5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

283 
lemma map_pmf_compose: "map_pmf (f \<circ> g) = map_pmf f \<circ> map_pmf g" 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

284 
by (rule, transfer) (simp add: distr_distr[symmetric, where N="count_space UNIV"] measurable_def) 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

285 

59000  286 
lemma map_pmf_comp: "map_pmf f (map_pmf g M) = map_pmf (\<lambda>x. f (g x)) M" 
287 
using map_pmf_compose[of f g] by (simp add: comp_def) 

288 

58587
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

289 
lemma map_pmf_cong: 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

290 
assumes "p = q" 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

291 
shows "(\<And>x. x \<in> set_pmf q \<Longrightarrow> f x = g x) \<Longrightarrow> map_pmf f p = map_pmf g q" 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

292 
unfolding `p = q`[symmetric] measure_pmf_inject[symmetric] map_pmf.rep_eq 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

293 
by (auto simp add: emeasure_distr AE_measure_pmf_iff intro!: emeasure_eq_AE measure_eqI) 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

294 

59002
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

295 
lemma emeasure_map_pmf[simp]: "emeasure (map_pmf f M) X = emeasure M (f ` X)" 
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

296 
unfolding map_pmf.rep_eq by (subst emeasure_distr) auto 
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

297 

2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

298 
lemma nn_integral_map_pmf[simp]: "(\<integral>\<^sup>+x. f x \<partial>map_pmf g M) = (\<integral>\<^sup>+x. f (g x) \<partial>M)" 
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

299 
unfolding map_pmf.rep_eq by (intro nn_integral_distr) auto 
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

300 

59023  301 
lemma ereal_pmf_map: "pmf (map_pmf f p) x = (\<integral>\<^sup>+ y. indicator (f ` {x}) y \<partial>measure_pmf p)" 
302 
proof(transfer fixing: f x) 

303 
fix p :: "'b measure" 

304 
presume "prob_space p" 

305 
then interpret prob_space p . 

306 
presume "sets p = UNIV" 

307 
then show "ereal (measure (distr p (count_space UNIV) f) {x}) = integral\<^sup>N p (indicator (f ` {x}))" 

308 
by(simp add: measure_distr measurable_def emeasure_eq_measure) 

309 
qed simp_all 

310 

58587
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

311 
lemma pmf_set_map: 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

312 
fixes f :: "'a \<Rightarrow> 'b" 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

313 
shows "set_pmf \<circ> map_pmf f = op ` f \<circ> set_pmf" 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

314 
proof (rule, transfer, clarsimp simp add: measure_distr measurable_def) 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

315 
fix f :: "'a \<Rightarrow> 'b" and M :: "'a measure" 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

316 
assume "prob_space M" and ae: "AE x in M. measure M {x} \<noteq> 0" and [simp]: "sets M = UNIV" 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

317 
interpret prob_space M by fact 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

318 
show "{x. measure M (f ` {x}) \<noteq> 0} = f ` {x. measure M {x} \<noteq> 0}" 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

319 
proof safe 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

320 
fix x assume "measure M (f ` {x}) \<noteq> 0" 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

321 
moreover have "measure M (f ` {x}) = measure M {y. f y = x \<and> measure M {y} \<noteq> 0}" 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

322 
using ae by (intro finite_measure_eq_AE) auto 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

323 
ultimately have "{y. f y = x \<and> measure M {y} \<noteq> 0} \<noteq> {}" 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

324 
by (metis measure_empty) 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

325 
then show "x \<in> f ` {x. measure M {x} \<noteq> 0}" 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

326 
by auto 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

327 
next 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

328 
fix x assume "measure M {x} \<noteq> 0" 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

329 
then have "0 < measure M {x}" 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

330 
using measure_nonneg[of M "{x}"] by auto 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

331 
also have "measure M {x} \<le> measure M (f ` {f x})" 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

332 
by (intro finite_measure_mono) auto 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

333 
finally show "measure M (f ` {f x}) = 0 \<Longrightarrow> False" 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

334 
by simp 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

335 
qed 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

336 
qed 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

337 

59000  338 
lemma set_map_pmf: "set_pmf (map_pmf f M) = f`set_pmf M" 
339 
using pmf_set_map[of f] by (auto simp: comp_def fun_eq_iff) 

340 

59023  341 
lemma nn_integral_pmf: "(\<integral>\<^sup>+ x. pmf p x \<partial>count_space A) = emeasure (measure_pmf p) A" 
342 
proof  

343 
have "(\<integral>\<^sup>+ x. pmf p x \<partial>count_space A) = (\<integral>\<^sup>+ x. pmf p x \<partial>count_space (A \<inter> set_pmf p))" 

344 
by(auto simp add: nn_integral_count_space_indicator indicator_def set_pmf_iff intro: nn_integral_cong) 

345 
also have "\<dots> = emeasure (measure_pmf p) (\<Union>x\<in>A \<inter> set_pmf p. {x})" 

346 
by(subst emeasure_UN_countable)(auto simp add: emeasure_pmf_single disjoint_family_on_def) 

347 
also have "\<dots> = emeasure (measure_pmf p) ((\<Union>x\<in>A \<inter> set_pmf p. {x}) \<union> {x. x \<in> A \<and> x \<notin> set_pmf p})" 

348 
by(rule emeasure_Un_null_set[symmetric])(auto intro: in_null_sets_measure_pmfI) 

349 
also have "\<dots> = emeasure (measure_pmf p) A" 

350 
by(auto intro: arg_cong2[where f=emeasure]) 

351 
finally show ?thesis . 

352 
qed 

353 

59000  354 
subsection {* PMFs as function *} 
355 

58587
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

356 
context 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

357 
fixes f :: "'a \<Rightarrow> real" 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

358 
assumes nonneg: "\<And>x. 0 \<le> f x" 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

359 
assumes prob: "(\<integral>\<^sup>+x. f x \<partial>count_space UNIV) = 1" 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

360 
begin 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

361 

5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

362 
lift_definition embed_pmf :: "'a pmf" is "density (count_space UNIV) (ereal \<circ> f)" 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

363 
proof (intro conjI) 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

364 
have *[simp]: "\<And>x y. ereal (f y) * indicator {x} y = ereal (f x) * indicator {x} y" 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

365 
by (simp split: split_indicator) 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

366 
show "AE x in density (count_space UNIV) (ereal \<circ> f). 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

367 
measure (density (count_space UNIV) (ereal \<circ> f)) {x} \<noteq> 0" 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

368 
by (simp add: AE_density nonneg emeasure_density measure_def nn_integral_cmult_indicator) 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

369 
show "prob_space (density (count_space UNIV) (ereal \<circ> f))" 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

370 
by default (simp add: emeasure_density prob) 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

371 
qed simp 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

372 

5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

373 
lemma pmf_embed_pmf: "pmf embed_pmf x = f x" 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

374 
proof transfer 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

375 
have *[simp]: "\<And>x y. ereal (f y) * indicator {x} y = ereal (f x) * indicator {x} y" 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

376 
by (simp split: split_indicator) 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

377 
fix x show "measure (density (count_space UNIV) (ereal \<circ> f)) {x} = f x" 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

378 
by transfer (simp add: measure_def emeasure_density nn_integral_cmult_indicator nonneg) 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

379 
qed 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

380 

5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

381 
end 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

382 

5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

383 
lemma embed_pmf_transfer: 
58730  384 
"rel_fun (eq_onp (\<lambda>f. (\<forall>x. 0 \<le> f x) \<and> (\<integral>\<^sup>+x. ereal (f x) \<partial>count_space UNIV) = 1)) pmf_as_measure.cr_pmf (\<lambda>f. density (count_space UNIV) (ereal \<circ> f)) embed_pmf" 
58587
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

385 
by (auto simp: rel_fun_def eq_onp_def embed_pmf.transfer) 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

386 

59000  387 
lemma measure_pmf_eq_density: "measure_pmf p = density (count_space UNIV) (pmf p)" 
388 
proof (transfer, elim conjE) 

389 
fix M :: "'a measure" assume [simp]: "sets M = UNIV" and ae: "AE x in M. measure M {x} \<noteq> 0" 

390 
assume "prob_space M" then interpret prob_space M . 

391 
show "M = density (count_space UNIV) (\<lambda>x. ereal (measure M {x}))" 

392 
proof (rule measure_eqI) 

393 
fix A :: "'a set" 

394 
have "(\<integral>\<^sup>+ x. ereal (measure M {x}) * indicator A x \<partial>count_space UNIV) = 

395 
(\<integral>\<^sup>+ x. emeasure M {x} * indicator (A \<inter> {x. measure M {x} \<noteq> 0}) x \<partial>count_space UNIV)" 

396 
by (auto intro!: nn_integral_cong simp: emeasure_eq_measure split: split_indicator) 

397 
also have "\<dots> = (\<integral>\<^sup>+ x. emeasure M {x} \<partial>count_space (A \<inter> {x. measure M {x} \<noteq> 0}))" 

398 
by (subst nn_integral_restrict_space[symmetric]) (auto simp: restrict_count_space) 

399 
also have "\<dots> = emeasure M (\<Union>x\<in>(A \<inter> {x. measure M {x} \<noteq> 0}). {x})" 

400 
by (intro emeasure_UN_countable[symmetric] countable_Int2 countable_support) 

401 
(auto simp: disjoint_family_on_def) 

402 
also have "\<dots> = emeasure M A" 

403 
using ae by (intro emeasure_eq_AE) auto 

404 
finally show " emeasure M A = emeasure (density (count_space UNIV) (\<lambda>x. ereal (measure M {x}))) A" 

405 
using emeasure_space_1 by (simp add: emeasure_density) 

406 
qed simp 

407 
qed 

408 

58587
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

409 
lemma td_pmf_embed_pmf: 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

410 
"type_definition pmf embed_pmf {f::'a \<Rightarrow> real. (\<forall>x. 0 \<le> f x) \<and> (\<integral>\<^sup>+x. ereal (f x) \<partial>count_space UNIV) = 1}" 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

411 
unfolding type_definition_def 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

412 
proof safe 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

413 
fix p :: "'a pmf" 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

414 
have "(\<integral>\<^sup>+ x. 1 \<partial>measure_pmf p) = 1" 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

415 
using measure_pmf.emeasure_space_1[of p] by simp 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

416 
then show *: "(\<integral>\<^sup>+ x. ereal (pmf p x) \<partial>count_space UNIV) = 1" 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

417 
by (simp add: measure_pmf_eq_density nn_integral_density pmf_nonneg del: nn_integral_const) 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

418 

5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

419 
show "embed_pmf (pmf p) = p" 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

420 
by (intro measure_pmf_inject[THEN iffD1]) 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

421 
(simp add: * embed_pmf.rep_eq pmf_nonneg measure_pmf_eq_density[of p] comp_def) 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

422 
next 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

423 
fix f :: "'a \<Rightarrow> real" assume "\<forall>x. 0 \<le> f x" "(\<integral>\<^sup>+x. f x \<partial>count_space UNIV) = 1" 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

424 
then show "pmf (embed_pmf f) = f" 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

425 
by (auto intro!: pmf_embed_pmf) 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

426 
qed (rule pmf_nonneg) 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

427 

5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

428 
end 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

429 

5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

430 
locale pmf_as_function 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

431 
begin 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

432 

5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

433 
setup_lifting td_pmf_embed_pmf 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

434 

58730  435 
lemma set_pmf_transfer[transfer_rule]: 
436 
assumes "bi_total A" 

437 
shows "rel_fun (pcr_pmf A) (rel_set A) (\<lambda>f. {x. f x \<noteq> 0}) set_pmf" 

438 
using `bi_total A` 

439 
by (auto simp: pcr_pmf_def cr_pmf_def rel_fun_def rel_set_def bi_total_def Bex_def set_pmf_iff) 

440 
metis+ 

441 

59000  442 
end 
443 

444 
context 

445 
begin 

446 

447 
interpretation pmf_as_function . 

448 

449 
lemma pmf_eqI: "(\<And>i. pmf M i = pmf N i) \<Longrightarrow> M = N" 

450 
by transfer auto 

451 

452 
lemma pmf_eq_iff: "M = N \<longleftrightarrow> (\<forall>i. pmf M i = pmf N i)" 

453 
by (auto intro: pmf_eqI) 

454 

455 
end 

456 

457 
context 

458 
begin 

459 

460 
interpretation pmf_as_function . 

461 

462 
lift_definition bernoulli_pmf :: "real \<Rightarrow> bool pmf" is 

463 
"\<lambda>p b. ((\<lambda>p. if b then p else 1  p) \<circ> min 1 \<circ> max 0) p" 

464 
by (auto simp: nn_integral_count_space_finite[where A="{False, True}"] UNIV_bool 

465 
split: split_max split_min) 

466 

467 
lemma pmf_bernoulli_True[simp]: "0 \<le> p \<Longrightarrow> p \<le> 1 \<Longrightarrow> pmf (bernoulli_pmf p) True = p" 

468 
by transfer simp 

469 

470 
lemma pmf_bernoulli_False[simp]: "0 \<le> p \<Longrightarrow> p \<le> 1 \<Longrightarrow> pmf (bernoulli_pmf p) False = 1  p" 

471 
by transfer simp 

472 

473 
lemma set_pmf_bernoulli: "0 < p \<Longrightarrow> p < 1 \<Longrightarrow> set_pmf (bernoulli_pmf p) = UNIV" 

474 
by (auto simp add: set_pmf_iff UNIV_bool) 

475 

59002
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

476 
lemma nn_integral_bernoulli_pmf[simp]: 
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

477 
assumes [simp]: "0 \<le> p" "p \<le> 1" "\<And>x. 0 \<le> f x" 
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

478 
shows "(\<integral>\<^sup>+x. f x \<partial>bernoulli_pmf p) = f True * p + f False * (1  p)" 
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

479 
by (subst nn_integral_measure_pmf_support[of UNIV]) 
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

480 
(auto simp: UNIV_bool field_simps) 
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

481 

2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

482 
lemma integral_bernoulli_pmf[simp]: 
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

483 
assumes [simp]: "0 \<le> p" "p \<le> 1" 
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

484 
shows "(\<integral>x. f x \<partial>bernoulli_pmf p) = f True * p + f False * (1  p)" 
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

485 
by (subst integral_measure_pmf[of UNIV]) (auto simp: UNIV_bool) 
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

486 

59000  487 
lift_definition geometric_pmf :: "nat pmf" is "\<lambda>n. 1 / 2^Suc n" 
488 
proof 

489 
note geometric_sums[of "1 / 2"] 

490 
note sums_mult[OF this, of "1 / 2"] 

491 
from sums_suminf_ereal[OF this] 

492 
show "(\<integral>\<^sup>+ x. ereal (1 / 2 ^ Suc x) \<partial>count_space UNIV) = 1" 

493 
by (simp add: nn_integral_count_space_nat field_simps) 

494 
qed simp 

495 

496 
lemma pmf_geometric[simp]: "pmf geometric_pmf n = 1 / 2^Suc n" 

497 
by transfer rule 

498 

59002
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

499 
lemma set_pmf_geometric[simp]: "set_pmf geometric_pmf = UNIV" 
59000  500 
by (auto simp: set_pmf_iff) 
501 

502 
context 

503 
fixes M :: "'a multiset" assumes M_not_empty: "M \<noteq> {#}" 

504 
begin 

505 

506 
lift_definition pmf_of_multiset :: "'a pmf" is "\<lambda>x. count M x / size M" 

507 
proof 

508 
show "(\<integral>\<^sup>+ x. ereal (real (count M x) / real (size M)) \<partial>count_space UNIV) = 1" 

509 
using M_not_empty 

510 
by (simp add: zero_less_divide_iff nn_integral_count_space nonempty_has_size 

511 
setsum_divide_distrib[symmetric]) 

512 
(auto simp: size_multiset_overloaded_eq intro!: setsum.cong) 

513 
qed simp 

514 

515 
lemma pmf_of_multiset[simp]: "pmf pmf_of_multiset x = count M x / size M" 

516 
by transfer rule 

517 

518 
lemma set_pmf_of_multiset[simp]: "set_pmf pmf_of_multiset = set_of M" 

519 
by (auto simp: set_pmf_iff) 

520 

521 
end 

522 

523 
context 

524 
fixes S :: "'a set" assumes S_not_empty: "S \<noteq> {}" and S_finite: "finite S" 

525 
begin 

526 

527 
lift_definition pmf_of_set :: "'a pmf" is "\<lambda>x. indicator S x / card S" 

528 
proof 

529 
show "(\<integral>\<^sup>+ x. ereal (indicator S x / real (card S)) \<partial>count_space UNIV) = 1" 

530 
using S_not_empty S_finite by (subst nn_integral_count_space'[of S]) auto 

531 
qed simp 

532 

533 
lemma pmf_of_set[simp]: "pmf pmf_of_set x = indicator S x / card S" 

534 
by transfer rule 

535 

536 
lemma set_pmf_of_set[simp]: "set_pmf pmf_of_set = S" 

537 
using S_finite S_not_empty by (auto simp: set_pmf_iff) 

538 

59002
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

539 
lemma emeasure_pmf_of_set[simp]: "emeasure pmf_of_set S = 1" 
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

540 
by (rule measure_pmf.emeasure_eq_1_AE) (auto simp: AE_measure_pmf_iff) 
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

541 

59000  542 
end 
543 

544 
end 

545 

546 
subsection {* Monad interpretation *} 

547 

548 
lemma measurable_measure_pmf[measurable]: 

549 
"(\<lambda>x. measure_pmf (M x)) \<in> measurable (count_space UNIV) (subprob_algebra (count_space UNIV))" 

550 
by (auto simp: space_subprob_algebra intro!: prob_space_imp_subprob_space) unfold_locales 

551 

552 
lemma bind_pmf_cong: 

553 
assumes "\<And>x. A x \<in> space (subprob_algebra N)" "\<And>x. B x \<in> space (subprob_algebra N)" 

554 
assumes "\<And>i. i \<in> set_pmf x \<Longrightarrow> A i = B i" 

555 
shows "bind (measure_pmf x) A = bind (measure_pmf x) B" 

556 
proof (rule measure_eqI) 

557 
show "sets (measure_pmf x \<guillemotright>= A) = sets (measure_pmf x \<guillemotright>= B)" 

558 
using assms by (subst (1 2) sets_bind) auto 

559 
next 

560 
fix X assume "X \<in> sets (measure_pmf x \<guillemotright>= A)" 

561 
then have X: "X \<in> sets N" 

562 
using assms by (subst (asm) sets_bind) auto 

563 
show "emeasure (measure_pmf x \<guillemotright>= A) X = emeasure (measure_pmf x \<guillemotright>= B) X" 

564 
using assms 

565 
by (subst (1 2) emeasure_bind[where N=N, OF _ _ X]) 

566 
(auto intro!: nn_integral_cong_AE simp: AE_measure_pmf_iff) 

567 
qed 

568 

569 
context 

570 
begin 

571 

572 
interpretation pmf_as_measure . 

573 

574 
lift_definition join_pmf :: "'a pmf pmf \<Rightarrow> 'a pmf" is "\<lambda>M. measure_pmf M \<guillemotright>= measure_pmf" 

575 
proof (intro conjI) 

576 
fix M :: "'a pmf pmf" 

577 

578 
have *: "measure_pmf \<in> measurable (measure_pmf M) (subprob_algebra (count_space UNIV))" 

579 
using measurable_measure_pmf[of "\<lambda>x. x"] by simp 

580 

581 
interpret bind: prob_space "measure_pmf M \<guillemotright>= measure_pmf" 

582 
apply (rule measure_pmf.prob_space_bind[OF _ *]) 

583 
apply (auto intro!: AE_I2) 

584 
apply unfold_locales 

585 
done 

586 
show "prob_space (measure_pmf M \<guillemotright>= measure_pmf)" 

587 
by intro_locales 

588 
show "sets (measure_pmf M \<guillemotright>= measure_pmf) = UNIV" 

589 
by (subst sets_bind[OF *]) auto 

590 
have "AE x in measure_pmf M \<guillemotright>= measure_pmf. emeasure (measure_pmf M \<guillemotright>= measure_pmf) {x} \<noteq> 0" 

591 
by (auto simp add: AE_bind[OF _ *] AE_measure_pmf_iff emeasure_bind[OF _ *] 

592 
nn_integral_0_iff_AE measure_pmf.emeasure_eq_measure measure_le_0_iff set_pmf_iff pmf.rep_eq) 

593 
then show "AE x in measure_pmf M \<guillemotright>= measure_pmf. measure (measure_pmf M \<guillemotright>= measure_pmf) {x} \<noteq> 0" 

594 
unfolding bind.emeasure_eq_measure by simp 

595 
qed 

596 

597 
lemma pmf_join: "pmf (join_pmf N) i = (\<integral>M. pmf M i \<partial>measure_pmf N)" 

598 
proof (transfer fixing: N i) 

599 
have N: "subprob_space (measure_pmf N)" 

600 
by (rule prob_space_imp_subprob_space) intro_locales 

601 
show "measure (measure_pmf N \<guillemotright>= measure_pmf) {i} = integral\<^sup>L (measure_pmf N) (\<lambda>M. measure M {i})" 

602 
using measurable_measure_pmf[of "\<lambda>x. x"] 

603 
by (intro subprob_space.measure_bind[where N="count_space UNIV", OF N]) auto 

604 
qed (auto simp: Transfer.Rel_def rel_fun_def cr_pmf_def) 

605 

606 
lift_definition return_pmf :: "'a \<Rightarrow> 'a pmf" is "return (count_space UNIV)" 

607 
by (auto intro!: prob_space_return simp: AE_return measure_return) 

608 

609 
lemma join_return_pmf: "join_pmf (return_pmf M) = M" 

610 
by (simp add: integral_return pmf_eq_iff pmf_join return_pmf.rep_eq) 

611 

612 
lemma map_return_pmf: "map_pmf f (return_pmf x) = return_pmf (f x)" 

613 
by transfer (simp add: distr_return) 

614 

59002
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

615 
lemma set_return_pmf: "set_pmf (return_pmf x) = {x}" 
59000  616 
by transfer (auto simp add: measure_return split: split_indicator) 
617 

618 
lemma pmf_return: "pmf (return_pmf x) y = indicator {y} x" 

619 
by transfer (simp add: measure_return) 

620 

59002
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

621 
lemma nn_integral_return_pmf[simp]: "0 \<le> f x \<Longrightarrow> (\<integral>\<^sup>+x. f x \<partial>return_pmf x) = f x" 
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

622 
unfolding return_pmf.rep_eq by (intro nn_integral_return) auto 
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

623 

2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

624 
lemma emeasure_return_pmf[simp]: "emeasure (return_pmf x) X = indicator X x" 
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

625 
unfolding return_pmf.rep_eq by (intro emeasure_return) auto 
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

626 

59000  627 
end 
628 

629 
definition "bind_pmf M f = join_pmf (map_pmf f M)" 

630 

631 
lemma (in pmf_as_measure) bind_transfer[transfer_rule]: 

632 
"rel_fun pmf_as_measure.cr_pmf (rel_fun (rel_fun op = pmf_as_measure.cr_pmf) pmf_as_measure.cr_pmf) op \<guillemotright>= bind_pmf" 

633 
proof (auto simp: pmf_as_measure.cr_pmf_def rel_fun_def bind_pmf_def join_pmf.rep_eq map_pmf.rep_eq) 

634 
fix M f and g :: "'a \<Rightarrow> 'b pmf" assume "\<forall>x. f x = measure_pmf (g x)" 

635 
then have f: "f = (\<lambda>x. measure_pmf (g x))" 

636 
by auto 

637 
show "measure_pmf M \<guillemotright>= f = distr (measure_pmf M) (count_space UNIV) g \<guillemotright>= measure_pmf" 

638 
unfolding f by (subst bind_distr[OF _ measurable_measure_pmf]) auto 

639 
qed 

640 

641 
lemma pmf_bind: "pmf (bind_pmf N f) i = (\<integral>x. pmf (f x) i \<partial>measure_pmf N)" 

642 
by (auto intro!: integral_distr simp: bind_pmf_def pmf_join map_pmf.rep_eq) 

643 

644 
lemma bind_return_pmf: "bind_pmf (return_pmf x) f = f x" 

645 
unfolding bind_pmf_def map_return_pmf join_return_pmf .. 

646 

59002
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

647 
lemma set_bind_pmf: "set_pmf (bind_pmf M N) = (\<Union>M\<in>set_pmf M. set_pmf (N M))" 
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

648 
apply (simp add: set_eq_iff set_pmf_iff pmf_bind) 
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

649 
apply (subst integral_nonneg_eq_0_iff_AE) 
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

650 
apply (auto simp: pmf_nonneg pmf_le_1 AE_measure_pmf_iff 
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

651 
intro!: measure_pmf.integrable_const_bound[where B=1]) 
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

652 
done 
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

653 

2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

654 
lemma measurable_pair_restrict_pmf2: 
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

655 
assumes "countable A" 
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

656 
assumes "\<And>y. y \<in> A \<Longrightarrow> (\<lambda>x. f (x, y)) \<in> measurable M L" 
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

657 
shows "f \<in> measurable (M \<Otimes>\<^sub>M restrict_space (measure_pmf N) A) L" 
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

658 
apply (subst measurable_cong_sets) 
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

659 
apply (rule sets_pair_measure_cong sets_restrict_space_cong sets_measure_pmf_count_space refl)+ 
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

660 
apply (simp_all add: restrict_count_space) 
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

661 
apply (subst split_eta[symmetric]) 
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

662 
unfolding measurable_split_conv 
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

663 
apply (rule measurable_compose_countable'[OF _ measurable_snd `countable A`]) 
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

664 
apply (rule measurable_compose[OF measurable_fst]) 
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

665 
apply fact 
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

666 
done 
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

667 

2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

668 
lemma measurable_pair_restrict_pmf1: 
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

669 
assumes "countable A" 
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

670 
assumes "\<And>x. x \<in> A \<Longrightarrow> (\<lambda>y. f (x, y)) \<in> measurable N L" 
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

671 
shows "f \<in> measurable (restrict_space (measure_pmf M) A \<Otimes>\<^sub>M N) L" 
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

672 
apply (subst measurable_cong_sets) 
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

673 
apply (rule sets_pair_measure_cong sets_restrict_space_cong sets_measure_pmf_count_space refl)+ 
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

674 
apply (simp_all add: restrict_count_space) 
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

675 
apply (subst split_eta[symmetric]) 
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

676 
unfolding measurable_split_conv 
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

677 
apply (rule measurable_compose_countable'[OF _ measurable_fst `countable A`]) 
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

678 
apply (rule measurable_compose[OF measurable_snd]) 
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

679 
apply fact 
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

680 
done 
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

681 

59000  682 
lemma bind_commute_pmf: "bind_pmf A (\<lambda>x. bind_pmf B (C x)) = bind_pmf B (\<lambda>y. bind_pmf A (\<lambda>x. C x y))" 
683 
unfolding pmf_eq_iff pmf_bind 

684 
proof 

685 
fix i 

686 
interpret B: prob_space "restrict_space B B" 

687 
by (intro prob_space_restrict_space measure_pmf.emeasure_eq_1_AE) 

688 
(auto simp: AE_measure_pmf_iff) 

689 
interpret A: prob_space "restrict_space A A" 

690 
by (intro prob_space_restrict_space measure_pmf.emeasure_eq_1_AE) 

691 
(auto simp: AE_measure_pmf_iff) 

692 

693 
interpret AB: pair_prob_space "restrict_space A A" "restrict_space B B" 

694 
by unfold_locales 

695 

696 
have "(\<integral> x. \<integral> y. pmf (C x y) i \<partial>B \<partial>A) = (\<integral> x. (\<integral> y. pmf (C x y) i \<partial>restrict_space B B) \<partial>A)" 

697 
by (rule integral_cong) (auto intro!: integral_pmf_restrict) 

698 
also have "\<dots> = (\<integral> x. (\<integral> y. pmf (C x y) i \<partial>restrict_space B B) \<partial>restrict_space A A)" 

59002
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

699 
by (intro integral_pmf_restrict B.borel_measurable_lebesgue_integral measurable_pair_restrict_pmf2 
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

700 
countable_set_pmf borel_measurable_count_space) 
59000  701 
also have "\<dots> = (\<integral> y. \<integral> x. pmf (C x y) i \<partial>restrict_space A A \<partial>restrict_space B B)" 
59002
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

702 
by (rule AB.Fubini_integral[symmetric]) 
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

703 
(auto intro!: AB.integrable_const_bound[where B=1] measurable_pair_restrict_pmf2 
59023  704 
simp: pmf_nonneg pmf_le_1 measurable_restrict_space1) 
59000  705 
also have "\<dots> = (\<integral> y. \<integral> x. pmf (C x y) i \<partial>restrict_space A A \<partial>B)" 
59002
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

706 
by (intro integral_pmf_restrict[symmetric] A.borel_measurable_lebesgue_integral measurable_pair_restrict_pmf2 
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

707 
countable_set_pmf borel_measurable_count_space) 
59000  708 
also have "\<dots> = (\<integral> y. \<integral> x. pmf (C x y) i \<partial>A \<partial>B)" 
709 
by (rule integral_cong) (auto intro!: integral_pmf_restrict[symmetric]) 

710 
finally show "(\<integral> x. \<integral> y. pmf (C x y) i \<partial>B \<partial>A) = (\<integral> y. \<integral> x. pmf (C x y) i \<partial>A \<partial>B)" . 

711 
qed 

712 

713 

714 
context 

715 
begin 

716 

717 
interpretation pmf_as_measure . 

718 

59002
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

719 
lemma measure_pmf_bind: "measure_pmf (bind_pmf M f) = (measure_pmf M \<guillemotright>= (\<lambda>x. measure_pmf (f x)))" 
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

720 
by transfer simp 
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

721 

2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

722 
lemma nn_integral_bind_pmf[simp]: "(\<integral>\<^sup>+x. f x \<partial>bind_pmf M N) = (\<integral>\<^sup>+x. \<integral>\<^sup>+y. f y \<partial>N x \<partial>M)" 
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

723 
using measurable_measure_pmf[of N] 
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

724 
unfolding measure_pmf_bind 
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

725 
apply (subst (1 3) nn_integral_max_0[symmetric]) 
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

726 
apply (intro nn_integral_bind[where B="count_space UNIV"]) 
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

727 
apply auto 
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

728 
done 
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

729 

2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

730 
lemma emeasure_bind_pmf[simp]: "emeasure (bind_pmf M N) X = (\<integral>\<^sup>+x. emeasure (N x) X \<partial>M)" 
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

731 
using measurable_measure_pmf[of N] 
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

732 
unfolding measure_pmf_bind 
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

733 
by (subst emeasure_bind[where N="count_space UNIV"]) auto 
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

734 

59000  735 
lemma bind_return_pmf': "bind_pmf N return_pmf = N" 
736 
proof (transfer, clarify) 

737 
fix N :: "'a measure" assume "sets N = UNIV" then show "N \<guillemotright>= return (count_space UNIV) = N" 

738 
by (subst return_sets_cong[where N=N]) (simp_all add: bind_return') 

739 
qed 

740 

741 
lemma bind_return_pmf'': "bind_pmf N (\<lambda>x. return_pmf (f x)) = map_pmf f N" 

742 
proof (transfer, clarify) 

743 
fix N :: "'b measure" and f :: "'b \<Rightarrow> 'a" assume "prob_space N" "sets N = UNIV" 

744 
then show "N \<guillemotright>= (\<lambda>x. return (count_space UNIV) (f x)) = distr N (count_space UNIV) f" 

745 
by (subst bind_return_distr[symmetric]) 

746 
(auto simp: prob_space.not_empty measurable_def comp_def) 

747 
qed 

748 

749 
lemma bind_assoc_pmf: "bind_pmf (bind_pmf A B) C = bind_pmf A (\<lambda>x. bind_pmf (B x) C)" 

750 
by transfer 

751 
(auto intro!: bind_assoc[where N="count_space UNIV" and R="count_space UNIV"] 

752 
simp: measurable_def space_subprob_algebra prob_space_imp_subprob_space) 

753 

754 
end 

755 

756 
definition "pair_pmf A B = bind_pmf A (\<lambda>x. bind_pmf B (\<lambda>y. return_pmf (x, y)))" 

757 

758 
lemma pmf_pair: "pmf (pair_pmf M N) (a, b) = pmf M a * pmf N b" 

759 
unfolding pair_pmf_def pmf_bind pmf_return 

760 
apply (subst integral_measure_pmf[where A="{b}"]) 

761 
apply (auto simp: indicator_eq_0_iff) 

762 
apply (subst integral_measure_pmf[where A="{a}"]) 

763 
apply (auto simp: indicator_eq_0_iff setsum_nonneg_eq_0_iff pmf_nonneg) 

764 
done 

765 

59002
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

766 
lemma set_pair_pmf: "set_pmf (pair_pmf A B) = set_pmf A \<times> set_pmf B" 
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

767 
unfolding pair_pmf_def set_bind_pmf set_return_pmf by auto 
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

768 

59000  769 
lemma bind_pair_pmf: 
770 
assumes M[measurable]: "M \<in> measurable (count_space UNIV \<Otimes>\<^sub>M count_space UNIV) (subprob_algebra N)" 

771 
shows "measure_pmf (pair_pmf A B) \<guillemotright>= M = (measure_pmf A \<guillemotright>= (\<lambda>x. measure_pmf B \<guillemotright>= (\<lambda>y. M (x, y))))" 

772 
(is "?L = ?R") 

773 
proof (rule measure_eqI) 

774 
have M'[measurable]: "M \<in> measurable (pair_pmf A B) (subprob_algebra N)" 

775 
using M[THEN measurable_space] by (simp_all add: space_pair_measure) 

776 

777 
have sets_eq_N: "sets ?L = N" 

778 
by (simp add: sets_bind[OF M']) 

779 
show "sets ?L = sets ?R" 

780 
unfolding sets_eq_N 

781 
apply (subst sets_bind[where N=N]) 

782 
apply (rule measurable_bind) 

783 
apply (rule measurable_compose[OF _ measurable_measure_pmf]) 

784 
apply measurable 

785 
apply (auto intro!: sets_pair_measure_cong sets_measure_pmf_count_space) 

786 
done 

787 
fix X assume "X \<in> sets ?L" 

788 
then have X[measurable]: "X \<in> sets N" 

789 
unfolding sets_eq_N . 

790 
then show "emeasure ?L X = emeasure ?R X" 

791 
apply (simp add: emeasure_bind[OF _ M' X]) 

792 
unfolding pair_pmf_def measure_pmf_bind[of A] 

59002
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

793 
apply (subst nn_integral_bind) 
59000  794 
apply (rule measurable_compose[OF M' measurable_emeasure_subprob_algebra, OF X]) 
795 
apply (subst measurable_cong_sets[OF sets_measure_pmf_count_space refl]) 

796 
apply (subst subprob_algebra_cong[OF sets_measure_pmf_count_space]) 

797 
apply measurable 

798 
unfolding measure_pmf_bind 

59002
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

799 
apply (subst nn_integral_bind) 
59000  800 
apply (rule measurable_compose[OF M' measurable_emeasure_subprob_algebra, OF X]) 
801 
apply (subst measurable_cong_sets[OF sets_measure_pmf_count_space refl]) 

802 
apply (subst subprob_algebra_cong[OF sets_measure_pmf_count_space]) 

803 
apply measurable 

59002
2c8b2fb54b88
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents:
59000
diff
changeset

804 
apply (simp add: nn_integral_measure_pmf_finite set_return_pmf emeasure_nonneg pmf_return one_ereal_def[symmetric]) 
59000  805 
apply (subst emeasure_bind[OF _ _ X]) 
806 
apply simp 

807 
apply (rule measurable_bind[where N="count_space UNIV"]) 

808 
apply (rule measurable_compose[OF _ measurable_measure_pmf]) 

809 
apply measurable 

810 
apply (rule sets_pair_measure_cong sets_measure_pmf_count_space refl)+ 

811 
apply (subst measurable_cong_sets[OF sets_pair_measure_cong[OF sets_measure_pmf_count_space refl] refl]) 

812 
apply simp 

813 
apply (subst emeasure_bind[OF _ _ X]) 

814 
apply simp 

815 
apply (rule measurable_compose[OF _ M]) 

816 
apply (auto simp: space_pair_measure) 

817 
done 

818 
qed 

819 

59023  820 
inductive rel_pmf :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> 'a pmf \<Rightarrow> 'b pmf \<Rightarrow> bool" 
821 
for R p q 

822 
where 

823 
"\<lbrakk> \<And>x y. (x, y) \<in> set_pmf pq \<Longrightarrow> R x y; 

824 
map_pmf fst pq = p; map_pmf snd pq = q \<rbrakk> 

825 
\<Longrightarrow> rel_pmf R p q" 

58587
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

826 

59023  827 
bnf pmf: "'a pmf" map: map_pmf sets: set_pmf bd : "natLeq" rel: rel_pmf 
58587
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

828 
proof  
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

829 
show "map_pmf id = id" by (rule map_pmf_id) 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

830 
show "\<And>f g. map_pmf (f \<circ> g) = map_pmf f \<circ> map_pmf g" by (rule map_pmf_compose) 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

831 
show "\<And>f g::'a \<Rightarrow> 'b. \<And>p. (\<And>x. x \<in> set_pmf p \<Longrightarrow> f x = g x) \<Longrightarrow> map_pmf f p = map_pmf g p" 
59023  832 
by (intro map_pmf_cong refl) 
58587
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

833 

5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

834 
show "\<And>f::'a \<Rightarrow> 'b. set_pmf \<circ> map_pmf f = op ` f \<circ> set_pmf" 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

835 
by (rule pmf_set_map) 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

836 

5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

837 
{ fix p :: "'s pmf" 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

838 
have "(card_of (set_pmf p), card_of (UNIV :: nat set)) \<in> ordLeq" 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

839 
by (rule card_of_ordLeqI[where f="to_nat_on (set_pmf p)"]) 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

840 
(auto intro: countable_set_pmf inj_on_to_nat_on) 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

841 
also have "(card_of (UNIV :: nat set), natLeq) \<in> ordLeq" 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

842 
by (metis Field_natLeq card_of_least natLeq_Well_order) 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

843 
finally show "(card_of (set_pmf p), natLeq) \<in> ordLeq" . } 
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

844 

59023  845 
show "\<And>R. rel_pmf R = 
846 
(BNF_Def.Grp {x. set_pmf x \<subseteq> {(x, y). R x y}} (map_pmf fst))\<inverse>\<inverse> OO 

847 
BNF_Def.Grp {x. set_pmf x \<subseteq> {(x, y). R x y}} (map_pmf snd)" 

848 
by (auto simp add: fun_eq_iff BNF_Def.Grp_def OO_def rel_pmf.simps) 

849 

850 
{ fix p :: "'a pmf" and f :: "'a \<Rightarrow> 'b" and g x 

851 
assume p: "\<And>z. z \<in> set_pmf p \<Longrightarrow> f z = g z" 

852 
and x: "x \<in> set_pmf p" 

853 
thus "f x = g x" by simp } 

854 

855 
fix R :: "'a => 'b \<Rightarrow> bool" and S :: "'b \<Rightarrow> 'c \<Rightarrow> bool" 

856 
{ fix p q r 

857 
assume pq: "rel_pmf R p q" 

858 
and qr:"rel_pmf S q r" 

859 
from pq obtain pq where pq: "\<And>x y. (x, y) \<in> set_pmf pq \<Longrightarrow> R x y" 

860 
and p: "p = map_pmf fst pq" and q: "q = map_pmf snd pq" by cases auto 

861 
from qr obtain qr where qr: "\<And>y z. (y, z) \<in> set_pmf qr \<Longrightarrow> S y z" 

862 
and q': "q = map_pmf fst qr" and r: "r = map_pmf snd qr" by cases auto 

863 

864 
have support_subset: "set_pmf pq O set_pmf qr \<subseteq> set_pmf p \<times> set_pmf r" 

865 
by(auto simp add: p r set_map_pmf intro: rev_image_eqI) 

866 

867 
let ?A = "\<lambda>y. {x. (x, y) \<in> set_pmf pq}" 

868 
and ?B = "\<lambda>y. {z. (y, z) \<in> set_pmf qr}" 

869 

870 

871 
def ppp \<equiv> "\<lambda>A. \<lambda>f :: 'a \<Rightarrow> real. \<lambda>n. if n \<in> to_nat_on A ` A then f (from_nat_into A n) else 0" 

872 
have [simp]: "\<And>A f n. (\<And>x. x \<in> A \<Longrightarrow> 0 \<le> f x) \<Longrightarrow> 0 \<le> ppp A f n" 

873 
"\<And>A f n x. \<lbrakk> x \<in> A; countable A \<rbrakk> \<Longrightarrow> ppp A f (to_nat_on A x) = f x" 

874 
"\<And>A f n. n \<notin> to_nat_on A ` A \<Longrightarrow> ppp A f n = 0" 

875 
by(auto simp add: ppp_def intro: from_nat_into) 

876 
def rrr \<equiv> "\<lambda>A. \<lambda>f :: 'c \<Rightarrow> real. \<lambda>n. if n \<in> to_nat_on A ` A then f (from_nat_into A n) else 0" 

877 
have [simp]: "\<And>A f n. (\<And>x. x \<in> A \<Longrightarrow> 0 \<le> f x) \<Longrightarrow> 0 \<le> rrr A f n" 

878 
"\<And>A f n x. \<lbrakk> x \<in> A; countable A \<rbrakk> \<Longrightarrow> rrr A f (to_nat_on A x) = f x" 

879 
"\<And>A f n. n \<notin> to_nat_on A ` A \<Longrightarrow> rrr A f n = 0" 

880 
by(auto simp add: rrr_def intro: from_nat_into) 

881 

882 
def pp \<equiv> "\<lambda>y. ppp (?A y) (\<lambda>x. pmf pq (x, y))" 

883 
and rr \<equiv> "\<lambda>y. rrr (?B y) (\<lambda>z. pmf qr (y, z))" 

884 

885 
have pos_p [simp]: "\<And>y n. 0 \<le> pp y n" 

886 
and pos_r [simp]: "\<And>y n. 0 \<le> rr y n" 

887 
by(simp_all add: pmf_nonneg pp_def rr_def) 

888 
{ fix y n 

889 
have "pp y n \<le> 0 \<longleftrightarrow> pp y n = 0" "\<not> 0 < pp y n \<longleftrightarrow> pp y n = 0" 

890 
and "min (pp y n) 0 = 0" "min 0 (pp y n) = 0" 

891 
using pos_p[of y n] by(auto simp del: pos_p) } 

892 
note pp_convs [simp] = this 

893 
{ fix y n 

894 
have "rr y n \<le> 0 \<longleftrightarrow> rr y n = 0" "\<not> 0 < rr y n \<longleftrightarrow> rr y n = 0" 

895 
and "min (rr y n) 0 = 0" "min 0 (rr y n) = 0" 

896 
using pos_r[of y n] by(auto simp del: pos_r) } 

897 
note r_convs [simp] = this 

898 

899 
have "\<And>y. ?A y \<subseteq> set_pmf p" by(auto simp add: p set_map_pmf intro: rev_image_eqI) 

900 
then have [simp]: "\<And>y. countable (?A y)" by(rule countable_subset) simp 

901 

902 
have "\<And>y. ?B y \<subseteq> set_pmf r" by(auto simp add: r set_map_pmf intro: rev_image_eqI) 

903 
then have [simp]: "\<And>y. countable (?B y)" by(rule countable_subset) simp 

904 

905 
let ?P = "\<lambda>y. to_nat_on (?A y)" 

906 
and ?R = "\<lambda>y. to_nat_on (?B y)" 

907 

908 
have eq: "\<And>y. (\<integral>\<^sup>+ x. pp y x \<partial>count_space UNIV) = \<integral>\<^sup>+ z. rr y z \<partial>count_space UNIV" 

909 
proof  

910 
fix y 

911 
have "(\<integral>\<^sup>+ x. pp y x \<partial>count_space UNIV) = (\<integral>\<^sup>+ x. pp y x \<partial>count_space (?P y ` ?A y))" 

912 
by(auto simp add: pp_def nn_integral_count_space_indicator indicator_def intro!: nn_integral_cong) 

913 
also have "\<dots> = (\<integral>\<^sup>+ x. pp y (?P y x) \<partial>count_space (?A y))" 

914 
by(intro nn_integral_bij_count_space[symmetric] inj_on_imp_bij_betw inj_on_to_nat_on) simp 

915 
also have "\<dots> = (\<integral>\<^sup>+ x. pmf pq (x, y) \<partial>count_space (?A y))" 

916 
by(rule nn_integral_cong)(simp add: pp_def) 

917 
also have "\<dots> = \<integral>\<^sup>+ x. emeasure (measure_pmf pq) {(x, y)} \<partial>count_space (?A y)" 

918 
by(simp add: emeasure_pmf_single) 

919 
also have "\<dots> = emeasure (measure_pmf pq) (\<Union>x\<in>?A y. {(x, y)})" 

920 
by(subst emeasure_UN_countable)(simp_all add: disjoint_family_on_def) 

921 
also have "\<dots> = emeasure (measure_pmf pq) ((\<Union>x\<in>?A y. {(x, y)}) \<union> {(x, y'). x \<notin> ?A y \<and> y' = y})" 

922 
by(rule emeasure_Un_null_set[symmetric])+ 

923 
(auto simp add: q set_map_pmf split_beta intro!: in_null_sets_measure_pmfI intro: rev_image_eqI) 

924 
also have "\<dots> = emeasure (measure_pmf pq) (snd ` {y})" 

925 
by(rule arg_cong2[where f=emeasure])+auto 

926 
also have "\<dots> = pmf q y" by(simp add: q ereal_pmf_map) 

927 
also have "\<dots> = emeasure (measure_pmf qr) (fst ` {y})" 

928 
by(simp add: q' ereal_pmf_map) 

929 
also have "\<dots> = emeasure (measure_pmf qr) ((\<Union>z\<in>?B y. {(y, z)}) \<union> {(y', z). z \<notin> ?B y \<and> y' = y})" 

930 
by(rule arg_cong2[where f=emeasure])+auto 

931 
also have "\<dots> = emeasure (measure_pmf qr) (\<Union>z\<in>?B y. {(y, z)})" 

932 
by(rule emeasure_Un_null_set) 

933 
(auto simp add: q' set_map_pmf split_beta intro!: in_null_sets_measure_pmfI intro: rev_image_eqI) 

934 
also have "\<dots> = \<integral>\<^sup>+ z. emeasure (measure_pmf qr) {(y, z)} \<partial>count_space (?B y)" 

935 
by(subst emeasure_UN_countable)(simp_all add: disjoint_family_on_def) 

936 
also have "\<dots> = (\<integral>\<^sup>+ z. pmf qr (y, z) \<partial>count_space (?B y))" 

937 
by(simp add: emeasure_pmf_single) 

938 
also have "\<dots> = (\<integral>\<^sup>+ z. rr y (?R y z) \<partial>count_space (?B y))" 

939 
by(rule nn_integral_cong)(simp add: rr_def) 

940 
also have "\<dots> = (\<integral>\<^sup>+ z. rr y z \<partial>count_space (?R y ` ?B y))" 

941 
by(intro nn_integral_bij_count_space inj_on_imp_bij_betw inj_on_to_nat_on) simp 

942 
also have "\<dots> = \<integral>\<^sup>+ z. rr y z \<partial>count_space UNIV" 

943 
by(auto simp add: rr_def nn_integral_count_space_indicator indicator_def intro!: nn_integral_cong) 

944 
finally show "?thesis y" . 

945 
qed 

58587
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

946 

59023  947 
def assign_aux \<equiv> "\<lambda>y remainder start weight z. 
948 
if z < start then 0 

949 
else if z = start then min weight remainder 

950 
else if remainder + setsum (rr y) {Suc start ..<z} < weight then min (weight  remainder  setsum (rr y) {Suc start..<z}) (rr y z) else 0" 

951 
hence assign_aux_alt_def: "\<And>y remainder start weight z. assign_aux y remainder start weight z = 

952 
(if z < start then 0 

953 
else if z = start then min weight remainder 

954 
else if remainder + setsum (rr y) {Suc start ..<z} < weight then min (weight  remainder  setsum (rr y) {Suc start..<z}) (rr y z) else 0)" 

955 
by simp 

956 
{ fix y and remainder :: real and start and weight :: real 

957 
assume weight_nonneg: "0 \<le> weight" 

958 
let ?assign_aux = "assign_aux y remainder start weight" 

959 
{ fix z 

960 
have "setsum ?assign_aux {..<z} = 

961 
(if z \<le> start then 0 else if remainder + setsum (rr y) {Suc start..<z} < weight then remainder + setsum (rr y) {Suc start..<z} else weight)" 

962 
proof(induction z) 

963 
case (Suc z) show ?case 

964 
by(auto simp add: Suc.IH assign_aux_alt_def[where z=z] not_less)(metis add.commute add.left_commute add_increasing pos_r) 

965 
qed(auto simp add: assign_aux_def) } 

966 
note setsum_start_assign_aux = this 

967 
moreover { 

968 
assume remainder_nonneg: "0 \<le> remainder" 

969 
have [simp]: "\<And>z. 0 \<le> ?assign_aux z" 

970 
by(simp add: assign_aux_def weight_nonneg remainder_nonneg) 

971 
moreover have "\<And>z. \<lbrakk> rr y z = 0; remainder \<le> rr y start \<rbrakk> \<Longrightarrow> ?assign_aux z = 0" 

972 
using remainder_nonneg weight_nonneg 

973 
by(auto simp add: assign_aux_def min_def) 

974 
moreover have "(\<integral>\<^sup>+ z. ?assign_aux z \<partial>count_space UNIV) = 

975 
min weight (\<integral>\<^sup>+ z. (if z < start then 0 else if z = start then remainder else rr y z) \<partial>count_space UNIV)" 

976 
(is "?lhs = ?rhs" is "_ = min _ (\<integral>\<^sup>+ y. ?f y \<partial>_)") 

977 
proof  

978 
have "?lhs = (SUP n. \<Sum>z<n. ereal (?assign_aux z))" 

979 
by(simp add: nn_integral_count_space_nat suminf_ereal_eq_SUP) 

980 
also have "\<dots> = (SUP n. min weight (\<Sum>z<n. ?f z))" 

981 
proof(rule arg_cong2[where f=SUPREMUM] ext refl)+ 

982 
fix n 

983 
have "(\<Sum>z<n. ereal (?assign_aux z)) = min weight ((if n > start then remainder else 0) + setsum ?f {Suc start..<n})" 

984 
using weight_nonneg remainder_nonneg by(simp add: setsum_start_assign_aux min_def) 

985 
also have "\<dots> = min weight (setsum ?f {start..<n})" 

986 
by(simp add: setsum_head_upt_Suc) 

987 
also have "\<dots> = min weight (setsum ?f {..<n})" 

988 
by(intro arg_cong2[where f=min] setsum.mono_neutral_left) auto 

989 
finally show "(\<Sum>z<n. ereal (?assign_aux z)) = \<dots>" . 

990 
qed 

991 
also have "\<dots> = min weight (SUP n. setsum ?f {..<n})" 

992 
unfolding inf_min[symmetric] by(subst inf_SUP) simp 

993 
also have "\<dots> = ?rhs" 

994 
by(simp add: nn_integral_count_space_nat suminf_ereal_eq_SUP remainder_nonneg) 

995 
finally show ?thesis . 

996 
qed 

997 
moreover note calculation } 

998 
moreover note calculation } 

999 
note setsum_start_assign_aux = this(1) 

1000 
and assign_aux_nonneg [simp] = this(2) 

1001 
and assign_aux_eq_0_outside = this(3) 

1002 
and nn_integral_assign_aux = this(4) 

1003 
{ fix y and remainder :: real and start target 

1004 
have "setsum (rr y) {Suc start..<target} \<ge> 0" by(simp add: setsum_nonneg) 

1005 
moreover assume "0 \<le> remainder" 

1006 
ultimately have "assign_aux y remainder start 0 target = 0" 

1007 
by(auto simp add: assign_aux_def min_def) } 

1008 
note assign_aux_weight_0 [simp] = this 

1009 

1010 
def find_start \<equiv> "\<lambda>y weight. if \<exists>n. weight \<le> setsum (rr y) {..n} then Some (LEAST n. weight \<le> setsum (rr y) {..n}) else None" 

1011 
have find_start_eq_Some_above: 

1012 
"\<And>y weight n. find_start y weight = Some n \<Longrightarrow> weight \<le> setsum (rr y) {..n}" 

1013 
by(drule sym)(auto simp add: find_start_def split: split_if_asm intro: LeastI) 

1014 
{ fix y weight n 

1015 
assume find_start: "find_start y weight = Some n" 

1016 
and weight: "0 \<le> weight" 

1017 
have "setsum (rr y) {..n} \<le> rr y n + weight" 

1018 
proof(rule ccontr) 

1019 
assume "\<not> ?thesis" 

1020 
hence "rr y n + weight < setsum (rr y) {..n}" by simp 

1021 
moreover with weight obtain n' where "n = Suc n'" by(cases n) auto 

1022 
ultimately have "weight \<le> setsum (rr y) {..n'}" by simp 

1023 
hence "(LEAST n. weight \<le> setsum (rr y) {..n}) \<le> n'" by(rule Least_le) 

1024 
moreover from find_start have "n = (LEAST n. weight \<le> setsum (rr y) {..n})" 

1025 
by(auto simp add: find_start_def split: split_if_asm) 

1026 
ultimately show False using \<open>n = Suc n'\<close> by auto 

1027 
qed } 

1028 
note find_start_eq_Some_least = this 

1029 
have find_start_0 [simp]: "\<And>y. find_start y 0 = Some 0" 

1030 
by(auto simp add: find_start_def intro!: exI[where x=0]) 

1031 
{ fix y and weight :: real 

1032 
assume "weight < \<integral>\<^sup>+ z. rr y z \<partial>count_space UNIV" 

1033 
also have "(\<integral>\<^sup>+ z. rr y z \<partial>count_space UNIV) = (SUP n. \<Sum>z<n. ereal (rr y z))" 

1034 
by(simp add: nn_integral_count_space_nat suminf_ereal_eq_SUP) 

1035 
finally obtain n where "weight < (\<Sum>z<n. rr y z)" by(auto simp add: less_SUP_iff) 

1036 
hence "weight \<in> dom (find_start y)" 

1037 
by(auto simp add: find_start_def)(meson atMost_iff finite_atMost lessThan_iff less_imp_le order_trans pos_r setsum_mono3 subsetI) } 

1038 
note in_dom_find_startI = this 

1039 
{ fix y and w w' :: real and m 

1040 
let ?m' = "LEAST m. w' \<le> setsum (rr y) {..m}" 

1041 
assume "w' \<le> w" 

1042 
also assume "find_start y w = Some m" 

1043 
hence "w \<le> setsum (rr y) {..m}" by(rule find_start_eq_Some_above) 

1044 
finally have "find_start y w' = Some ?m'" by(auto simp add: find_start_def) 

1045 
moreover from \<open>w' \<le> setsum (rr y) {..m}\<close> have "?m' \<le> m" by(rule Least_le) 

1046 
ultimately have "\<exists>m'. find_start y w' = Some m' \<and> m' \<le> m" by blast } 

1047 
note find_start_mono = this[rotated] 

1048 

1049 
def assign \<equiv> "\<lambda>y x z. let used = setsum (pp y) {..<x} 

1050 
in case find_start y used of None \<Rightarrow> 0 

1051 
 Some start \<Rightarrow> assign_aux y (setsum (rr y) {..start}  used) start (pp y x) z" 

1052 
hence assign_alt_def: "\<And>y x z. assign y x z = 

1053 
(let used = setsum (pp y) {..<x} 

1054 
in case find_start y used of None \<Rightarrow> 0 

1055 
 Some start \<Rightarrow> assign_aux y (setsum (rr y) {..start}  used) start (pp y x) z)" 

1056 
by simp 

1057 
have assign_nonneg [simp]: "\<And>y x z. 0 \<le> assign y x z" 

1058 
by(simp add: assign_def diff_le_iff find_start_eq_Some_above split: option.split) 

1059 
have assign_eq_0_outside: "\<And>y x z. \<lbrakk> pp y x = 0 \<or> rr y z = 0 \<rbrakk> \<Longrightarrow> assign y x z = 0" 

1060 
by(auto simp add: assign_def assign_aux_eq_0_outside diff_le_iff find_start_eq_Some_above find_start_eq_Some_least setsum_nonneg split: option.split) 

1061 

1062 
{ fix y x z 

1063 
have "(\<Sum>n<Suc x. assign y n z) = 

1064 
(case find_start y (setsum (pp y) {..<x}) of None \<Rightarrow> rr y z 

1065 
 Some m \<Rightarrow> if z < m then rr y z 

1066 
else min (rr y z) (max 0 (setsum (pp y) {..<x} + pp y x  setsum (rr y) {..<z})))" 

1067 
(is "?lhs x = ?rhs x") 

1068 
proof(induction x) 

1069 
case 0 thus ?case 

1070 
by(auto simp add: assign_def assign_aux_def setsum_head_upt_Suc atLeast0LessThan[symmetric] not_less field_simps max_def) 

1071 
next 

1072 
case (Suc x) 

1073 
have "?lhs (Suc x) = ?lhs x + assign y (Suc x) z" by simp 

1074 
also have "?lhs x = ?rhs x" by(rule Suc.IH) 

1075 
also have "?rhs x + assign y (Suc x) z = ?rhs (Suc x)" 

1076 
proof(cases "find_start y (setsum (pp y) {..<Suc x})") 

1077 
case None 

1078 
thus ?thesis 

1079 
by(auto split: option.split simp add: assign_def min_def max_def diff_le_iff setsum_nonneg not_le field_simps) 

1080 
(metis add.commute add_increasing find_start_def lessThan_Suc_atMost less_imp_le option.distinct(1) setsum_lessThan_Suc)+ 

1081 
next 

1082 
case (Some m) 

1083 
have [simp]: "setsum (rr y) {..m} = rr y m + setsum (rr y) {..<m}" 

1084 
by(simp add: ivl_disj_un(2)[symmetric]) 

1085 
from Some obtain m' where m': "find_start y (setsum (pp y) {..<x}) = Some m'" "m' \<le> m" 

1086 
by(auto dest: find_start_mono[where w'2="setsum (pp y) {..<x}"]) 

1087 
moreover { 

1088 
assume "z < m" 

1089 
then have "setsum (rr y) {..z} \<le> setsum (rr y) {..<m}" 

1090 
by(auto intro: setsum_mono3) 

1091 
also have "\<dots> \<le> setsum (pp y) {..<Suc x}" using find_start_eq_Some_least[OF Some] 

1092 
by(simp add: ivl_disj_un(2)[symmetric] setsum_nonneg) 

1093 
finally have "rr y z \<le> max 0 (setsum (pp y) {..<x} + pp y x  setsum (rr y) {..<z})" 

1094 
by(auto simp add: ivl_disj_un(2)[symmetric] max_def diff_le_iff simp del: r_convs) 

1095 
} moreover { 

1096 
assume "m \<le> z" 

1097 
have "setsum (pp y) {..<Suc x} \<le> setsum (rr y) {..m}" 

1098 
using Some by(rule find_start_eq_Some_above) 

1099 
also have "\<dots> \<le> setsum (rr y) {..<Suc z}" using \<open>m \<le> z\<close> by(intro setsum_mono3) auto 

1100 
finally have "max 0 (setsum (pp y) {..<x} + pp y x  setsum (rr y) {..<z}) \<le> rr y z" by simp 

1101 
moreover have "z \<noteq> m \<Longrightarrow> setsum (rr y) {..m} + setsum (rr y) {Suc m..<z} = setsum (rr y) {..<z}" 

1102 
using \<open>m \<le> z\<close> 

1103 
by(subst ivl_disj_un(8)[where l="Suc m", symmetric]) 

1104 
(simp_all add: setsum_Un ivl_disj_un(2)[symmetric] setsum.neutral) 

1105 
moreover note calculation 

1106 
} moreover { 

1107 
assume "m < z" 

1108 
have "setsum (pp y) {..<Suc x} \<le> setsum (rr y) {..m}" 

1109 
using Some by(rule find_start_eq_Some_above) 

1110 
also have "\<dots> \<le> setsum (rr y) {..<z}" using \<open>m < z\<close> by(intro setsum_mono3) auto 

1111 
finally have "max 0 (setsum (pp y) {..<Suc x}  setsum (rr y) {..<z}) = 0" by simp } 

1112 
moreover have "setsum (pp y) {..<Suc x} \<ge> setsum (rr y) {..<m}" 

1113 
using find_start_eq_Some_least[OF Some] 

1114 
by(simp add: setsum_nonneg ivl_disj_un(2)[symmetric]) 

1115 
moreover hence "setsum (pp y) {..<Suc (Suc x)} \<ge> setsum (rr y) {..<m}" 

1116 
by(fastforce intro: order_trans) 

1117 
ultimately show ?thesis using Some 

1118 
by(auto simp add: assign_def assign_aux_def Let_def field_simps max_def) 

1119 
qed 

1120 
finally show ?case . 

1121 
qed } 

1122 
note setsum_assign = this 

58587
5484f6079bcd
add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff
changeset

1123 

59023  1124 
have nn_integral_assign1: "\<And>y z. (\<integral>\<^sup>+ x. assign y x z \<partial>count_space UNIV) = rr y z" 
1125 
proof  

1126 
fix y z 

1127 
have "(\<integral>\<^sup>+ x. assign y x z \<partial>count_space UNIV) = (SUP n. ereal (\<Sum>x<n. assign y x z))" 

1128 
by(simp add: nn_integral_count_space_nat suminf_ereal_eq_SUP) 

1129 
also have "\<dots> = rr y z" 

1130 
proof(rule antisym) 

1131 
show "(SUP n. ereal (\<Sum>x<n. assign y x z)) \<le> rr y z" 

1132 
proof(rule SUP_least) 

1133 
fix n 

1134 
show "ereal (\<Sum>x<n. (assign y x z)) \<le> rr y z" 

1135 
using setsum_assign[of y z "n  1"] 

1136 
by(cases n)(simp_all split: option.split) 

1137 
qed 

1138 
show "rr y z \<le> (SUP n. ereal (\<Sum>x<n. assign y x z))" 

1139 
proof(cases "setsum (rr y) {..z} < \<integral>\<^sup>+ x. pp y x \<partial>count_space UNIV") 

1140 
case True 

1141 
then obtain n where "setsum (rr y) {..z} < setsum (pp y) {..<n}" 

1142 
by(auto simp add: nn_integral_count_space_nat suminf_ereal_eq_SUP less_SUP_iff) 

1143 
moreover have "\<And>k. k < z \<Longrightarrow> setsum (rr y) {..k} \<le> setsum (rr y) {..<z}" 

1144 
by(auto intro: setsum_mono3) 

1145 
ultimately have "rr y z \<le> (\<Sum>x<Suc n. assign y x z)" 

1146 
by(subst setsum_assign)(auto split: option.split dest!: find_start_eq_Some_above simp add: ivl_disj_un(2)[symmetric] add.commute add_increasing le_diff_eq le_max_iff_disj) 

1147 
also have "\<dots> \<le> (SUP n. ereal (\<Sum>x<n. assign y x z))" 

1148 
by(rule SUP_upper) simp 

1149 
finally show ?thesis by simp 

1150 
next 

1151 
case False 

1152 
have "setsum (rr y) {..z} = \<integral>\<^sup>+ z. rr y z \<partial>count_space {..z}" 

1153 
by(simp add: nn_integral_count_space_finite max_def) 

1154 
also have "\<dots> \<le> \<integral>\<^sup>+ z. rr y z \<partial>count_space UNIV" 

1155 
by(auto simp add: nn_integral_count_space_indicator indicator_def intro: nn_integral_mono) 

1156 
also have "\<dots> = \<integral>\<^sup>+ x. pp y x \<partial>count_space UNIV" by(simp add: eq) 

1157 
finally have *: "setsum (rr y) {..z} = \<dots>" using False by simp 

1158 
also have "\<dots> = (SUP n. ereal (\<Sum>x<n. pp y x))" 

1159 
by(simp add: nn_integral_count_space_nat suminf_ereal_eq_SUP) 

1160 
also have "\<dots> \<le> (SUP n. ereal (\<Sum>x<n. assign y x z)) + setsum (rr y) {..<z}" 

1161 
proof(rule SUP_least) 

1162 
fix n 

1163 
have "setsum (pp y) {..<n} = \<integral>\<^sup>+ x. pp y x \<partial>count_space {..<n}" 

1164 
by(simp add: nn_integral_count_space_finite max_def) 

1165 
also have "\<dots> \<le> \<integral>\<^sup>+ x. pp y x \<partial>count_space UNIV" 

1166 
by(auto simp add: nn_integral_count_space_indicator indicator_def intro: nn_integral_mono) 

1167 
also have "\<dots> = setsum (rr y) {..z}" using * by simp 

1168 
finally obtain k where k: "find_start y (setsum (pp y) {..<n}) = Some k" 

1169 
by(fastforce simp add: find_start_def) 

1170 
with \<open>ereal (setsum (pp y) {..<n}) \<le> setsum (rr y) {..z}\<close> 

1171 
have "k \<le> z" by(auto simp add: find_start_def split: split_if_asm intro: Least_le) 

1172 
then have "setsum (pp y) {..<n}  setsum (rr y) {..<z} \<le> ereal (\<Sum>x<Suc n. assign y x z)" 

1173 
using \<open>ereal (setsum (pp y) {..<n}) \<le> setsum (rr y) {..z}\<close> 

1174 
by(subst setsum_assign)(auto simp add: field_simps max_def k ivl_disj_un(2)[symmetric], metis le_add_same_cancel2 max.bounded_iff max_def pos_p) 

1175 
also have "\<dots> \<le> (SUP n. ereal (\<Sum>x<n. assign y x z))" 

1176 
by(rule SUP_upper) simp 

1177 
finally show "ereal (\<Sum>x<n. pp y x) \<le> \<dots> + setsum (rr y) {..<z}" 
