src/HOL/Probability/Probability_Mass_Function.thy
 author hoelzl Wed Jan 06 12:18:53 2016 +0100 (2016-01-06) changeset 62083 7582b39f51ed parent 62026 ea3b1b0413b4 child 62324 ae44f16dcea5 permissions -rw-r--r--
add the proof of the central limit theorem
1 (*  Title:      HOL/Probability/Probability_Mass_Function.thy
2     Author:     Johannes Hölzl, TU München
3     Author:     Andreas Lochbihler, ETH Zurich
4 *)
6 section \<open> Probability mass function \<close>
8 theory Probability_Mass_Function
9 imports
11   "~~/src/HOL/Library/Multiset"
12 begin
14 lemma AE_emeasure_singleton:
15   assumes x: "emeasure M {x} \<noteq> 0" and ae: "AE x in M. P x" shows "P x"
16 proof -
17   from x have x_M: "{x} \<in> sets M"
18     by (auto intro: emeasure_notin_sets)
19   from ae obtain N where N: "{x\<in>space M. \<not> P x} \<subseteq> N" "emeasure M N = 0" "N \<in> sets M"
20     by (auto elim: AE_E)
21   { assume "\<not> P x"
22     with x_M[THEN sets.sets_into_space] N have "emeasure M {x} \<le> emeasure M N"
23       by (intro emeasure_mono) auto
24     with x N have False
25       by (auto simp: emeasure_le_0_iff) }
26   then show "P x" by auto
27 qed
29 lemma AE_measure_singleton: "measure M {x} \<noteq> 0 \<Longrightarrow> AE x in M. P x \<Longrightarrow> P x"
30   by (metis AE_emeasure_singleton measure_def emeasure_empty measure_empty)
32 lemma ereal_divide': "b \<noteq> 0 \<Longrightarrow> ereal (a / b) = ereal a / ereal b"
33   using ereal_divide[of a b] by simp
35 lemma (in finite_measure) AE_support_countable:
36   assumes [simp]: "sets M = UNIV"
37   shows "(AE x in M. measure M {x} \<noteq> 0) \<longleftrightarrow> (\<exists>S. countable S \<and> (AE x in M. x \<in> S))"
38 proof
39   assume "\<exists>S. countable S \<and> (AE x in M. x \<in> S)"
40   then obtain S where S[intro]: "countable S" and ae: "AE x in M. x \<in> S"
41     by auto
42   then have "emeasure M (\<Union>x\<in>{x\<in>S. emeasure M {x} \<noteq> 0}. {x}) =
43     (\<integral>\<^sup>+ x. emeasure M {x} * indicator {x\<in>S. emeasure M {x} \<noteq> 0} x \<partial>count_space UNIV)"
44     by (subst emeasure_UN_countable)
45        (auto simp: disjoint_family_on_def nn_integral_restrict_space[symmetric] restrict_count_space)
46   also have "\<dots> = (\<integral>\<^sup>+ x. emeasure M {x} * indicator S x \<partial>count_space UNIV)"
47     by (auto intro!: nn_integral_cong split: split_indicator)
48   also have "\<dots> = emeasure M (\<Union>x\<in>S. {x})"
49     by (subst emeasure_UN_countable)
50        (auto simp: disjoint_family_on_def nn_integral_restrict_space[symmetric] restrict_count_space)
51   also have "\<dots> = emeasure M (space M)"
52     using ae by (intro emeasure_eq_AE) auto
53   finally have "emeasure M {x \<in> space M. x\<in>S \<and> emeasure M {x} \<noteq> 0} = emeasure M (space M)"
54     by (simp add: emeasure_single_in_space cong: rev_conj_cong)
55   with finite_measure_compl[of "{x \<in> space M. x\<in>S \<and> emeasure M {x} \<noteq> 0}"]
56   have "AE x in M. x \<in> S \<and> emeasure M {x} \<noteq> 0"
57     by (intro AE_I[OF order_refl]) (auto simp: emeasure_eq_measure set_diff_eq cong: conj_cong)
58   then show "AE x in M. measure M {x} \<noteq> 0"
59     by (auto simp: emeasure_eq_measure)
60 qed (auto intro!: exI[of _ "{x. measure M {x} \<noteq> 0}"] countable_support)
62 subsection \<open> PMF as measure \<close>
64 typedef 'a pmf = "{M :: 'a measure. prob_space M \<and> sets M = UNIV \<and> (AE x in M. measure M {x} \<noteq> 0)}"
65   morphisms measure_pmf Abs_pmf
66   by (intro exI[of _ "uniform_measure (count_space UNIV) {undefined}"])
67      (auto intro!: prob_space_uniform_measure AE_uniform_measureI)
69 declare [[coercion measure_pmf]]
71 lemma prob_space_measure_pmf: "prob_space (measure_pmf p)"
72   using pmf.measure_pmf[of p] by auto
74 interpretation measure_pmf: prob_space "measure_pmf M" for M
75   by (rule prob_space_measure_pmf)
77 interpretation measure_pmf: subprob_space "measure_pmf M" for M
78   by (rule prob_space_imp_subprob_space) unfold_locales
80 lemma subprob_space_measure_pmf: "subprob_space (measure_pmf x)"
81   by unfold_locales
83 locale pmf_as_measure
84 begin
86 setup_lifting type_definition_pmf
88 end
90 context
91 begin
93 interpretation pmf_as_measure .
95 lemma sets_measure_pmf[simp]: "sets (measure_pmf p) = UNIV"
96   by transfer blast
98 lemma sets_measure_pmf_count_space[measurable_cong]:
99   "sets (measure_pmf M) = sets (count_space UNIV)"
100   by simp
102 lemma space_measure_pmf[simp]: "space (measure_pmf p) = UNIV"
103   using sets_eq_imp_space_eq[of "measure_pmf p" "count_space UNIV"] by simp
105 lemma measure_pmf_UNIV [simp]: "measure (measure_pmf p) UNIV = 1"
106 using measure_pmf.prob_space[of p] by simp
108 lemma measure_pmf_in_subprob_algebra[measurable (raw)]: "measure_pmf x \<in> space (subprob_algebra (count_space UNIV))"
109   by (simp add: space_subprob_algebra subprob_space_measure_pmf)
111 lemma measurable_pmf_measure1[simp]: "measurable (M :: 'a pmf) N = UNIV \<rightarrow> space N"
112   by (auto simp: measurable_def)
114 lemma measurable_pmf_measure2[simp]: "measurable N (M :: 'a pmf) = measurable N (count_space UNIV)"
115   by (intro measurable_cong_sets) simp_all
117 lemma measurable_pair_restrict_pmf2:
118   assumes "countable A"
119   assumes [measurable]: "\<And>y. y \<in> A \<Longrightarrow> (\<lambda>x. f (x, y)) \<in> measurable M L"
120   shows "f \<in> measurable (M \<Otimes>\<^sub>M restrict_space (measure_pmf N) A) L" (is "f \<in> measurable ?M _")
121 proof -
122   have [measurable_cong]: "sets (restrict_space (count_space UNIV) A) = sets (count_space A)"
125   show ?thesis
126     by (intro measurable_compose_countable'[where f="\<lambda>a b. f (fst b, a)" and g=snd and I=A,
127                                             unfolded prod.collapse] assms)
128         measurable
129 qed
131 lemma measurable_pair_restrict_pmf1:
132   assumes "countable A"
133   assumes [measurable]: "\<And>x. x \<in> A \<Longrightarrow> (\<lambda>y. f (x, y)) \<in> measurable N L"
134   shows "f \<in> measurable (restrict_space (measure_pmf M) A \<Otimes>\<^sub>M N) L"
135 proof -
136   have [measurable_cong]: "sets (restrict_space (count_space UNIV) A) = sets (count_space A)"
139   show ?thesis
140     by (intro measurable_compose_countable'[where f="\<lambda>a b. f (a, snd b)" and g=fst and I=A,
141                                             unfolded prod.collapse] assms)
142         measurable
143 qed
145 lift_definition pmf :: "'a pmf \<Rightarrow> 'a \<Rightarrow> real" is "\<lambda>M x. measure M {x}" .
147 lift_definition set_pmf :: "'a pmf \<Rightarrow> 'a set" is "\<lambda>M. {x. measure M {x} \<noteq> 0}" .
148 declare [[coercion set_pmf]]
150 lemma AE_measure_pmf: "AE x in (M::'a pmf). x \<in> M"
151   by transfer simp
153 lemma emeasure_pmf_single_eq_zero_iff:
154   fixes M :: "'a pmf"
155   shows "emeasure M {y} = 0 \<longleftrightarrow> y \<notin> M"
156   by transfer (simp add: finite_measure.emeasure_eq_measure[OF prob_space.finite_measure])
158 lemma AE_measure_pmf_iff: "(AE x in measure_pmf M. P x) \<longleftrightarrow> (\<forall>y\<in>M. P y)"
159   using AE_measure_singleton[of M] AE_measure_pmf[of M]
160   by (auto simp: set_pmf.rep_eq)
162 lemma AE_pmfI: "(\<And>y. y \<in> set_pmf M \<Longrightarrow> P y) \<Longrightarrow> almost_everywhere (measure_pmf M) P"
165 lemma countable_set_pmf [simp]: "countable (set_pmf p)"
166   by transfer (metis prob_space.finite_measure finite_measure.countable_support)
168 lemma pmf_positive: "x \<in> set_pmf p \<Longrightarrow> 0 < pmf p x"
169   by transfer (simp add: less_le measure_nonneg)
171 lemma pmf_nonneg: "0 \<le> pmf p x"
172   by transfer (simp add: measure_nonneg)
174 lemma pmf_le_1: "pmf p x \<le> 1"
177 lemma set_pmf_not_empty: "set_pmf M \<noteq> {}"
178   using AE_measure_pmf[of M] by (intro notI) simp
180 lemma set_pmf_iff: "x \<in> set_pmf M \<longleftrightarrow> pmf M x \<noteq> 0"
181   by transfer simp
183 lemma set_pmf_eq: "set_pmf M = {x. pmf M x \<noteq> 0}"
184   by (auto simp: set_pmf_iff)
186 lemma emeasure_pmf_single:
187   fixes M :: "'a pmf"
188   shows "emeasure M {x} = pmf M x"
189   by transfer (simp add: finite_measure.emeasure_eq_measure[OF prob_space.finite_measure])
191 lemma measure_pmf_single: "measure (measure_pmf M) {x} = pmf M x"
192 using emeasure_pmf_single[of M x] by(simp add: measure_pmf.emeasure_eq_measure)
194 lemma emeasure_measure_pmf_finite: "finite S \<Longrightarrow> emeasure (measure_pmf M) S = (\<Sum>s\<in>S. pmf M s)"
195   by (subst emeasure_eq_setsum_singleton) (auto simp: emeasure_pmf_single)
197 lemma measure_measure_pmf_finite: "finite S \<Longrightarrow> measure (measure_pmf M) S = setsum (pmf M) S"
198   using emeasure_measure_pmf_finite[of S M] by(simp add: measure_pmf.emeasure_eq_measure)
200 lemma nn_integral_measure_pmf_support:
201   fixes f :: "'a \<Rightarrow> ereal"
202   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"
203   shows "(\<integral>\<^sup>+x. f x \<partial>measure_pmf M) = (\<Sum>x\<in>A. f x * pmf M x)"
204 proof -
205   have "(\<integral>\<^sup>+x. f x \<partial>M) = (\<integral>\<^sup>+x. f x * indicator A x \<partial>M)"
206     using nn by (intro nn_integral_cong_AE) (auto simp: AE_measure_pmf_iff split: split_indicator)
207   also have "\<dots> = (\<Sum>x\<in>A. f x * emeasure M {x})"
208     using assms by (intro nn_integral_indicator_finite) auto
209   finally show ?thesis
211 qed
213 lemma nn_integral_measure_pmf_finite:
214   fixes f :: "'a \<Rightarrow> ereal"
215   assumes f: "finite (set_pmf M)" and nn: "\<And>x. x \<in> set_pmf M \<Longrightarrow> 0 \<le> f x"
216   shows "(\<integral>\<^sup>+x. f x \<partial>measure_pmf M) = (\<Sum>x\<in>set_pmf M. f x * pmf M x)"
217   using assms by (intro nn_integral_measure_pmf_support) auto
218 lemma integrable_measure_pmf_finite:
219   fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}"
220   shows "finite (set_pmf M) \<Longrightarrow> integrable M f"
221   by (auto intro!: integrableI_bounded simp: nn_integral_measure_pmf_finite)
223 lemma integral_measure_pmf:
224   assumes [simp]: "finite A" and "\<And>a. a \<in> set_pmf M \<Longrightarrow> f a \<noteq> 0 \<Longrightarrow> a \<in> A"
225   shows "(\<integral>x. f x \<partial>measure_pmf M) = (\<Sum>a\<in>A. f a * pmf M a)"
226 proof -
227   have "(\<integral>x. f x \<partial>measure_pmf M) = (\<integral>x. f x * indicator A x \<partial>measure_pmf M)"
228     using assms(2) by (intro integral_cong_AE) (auto split: split_indicator simp: AE_measure_pmf_iff)
229   also have "\<dots> = (\<Sum>a\<in>A. f a * pmf M a)"
230     by (subst integral_indicator_finite_real) (auto simp: measure_def emeasure_measure_pmf_finite)
231   finally show ?thesis .
232 qed
234 lemma integrable_pmf: "integrable (count_space X) (pmf M)"
235 proof -
236   have " (\<integral>\<^sup>+ x. pmf M x \<partial>count_space X) = (\<integral>\<^sup>+ x. pmf M x \<partial>count_space (M \<inter> X))"
237     by (auto simp add: nn_integral_count_space_indicator set_pmf_iff intro!: nn_integral_cong split: split_indicator)
238   then have "integrable (count_space X) (pmf M) = integrable (count_space (M \<inter> X)) (pmf M)"
239     by (simp add: integrable_iff_bounded pmf_nonneg)
240   then show ?thesis
241     by (simp add: pmf.rep_eq measure_pmf.integrable_measure disjoint_family_on_def)
242 qed
244 lemma integral_pmf: "(\<integral>x. pmf M x \<partial>count_space X) = measure M X"
245 proof -
246   have "(\<integral>x. pmf M x \<partial>count_space X) = (\<integral>\<^sup>+x. pmf M x \<partial>count_space X)"
247     by (simp add: pmf_nonneg integrable_pmf nn_integral_eq_integral)
248   also have "\<dots> = (\<integral>\<^sup>+x. emeasure M {x} \<partial>count_space (X \<inter> M))"
249     by (auto intro!: nn_integral_cong_AE split: split_indicator
250              simp: pmf.rep_eq measure_pmf.emeasure_eq_measure nn_integral_count_space_indicator
251                    AE_count_space set_pmf_iff)
252   also have "\<dots> = emeasure M (X \<inter> M)"
253     by (rule emeasure_countable_singleton[symmetric]) (auto intro: countable_set_pmf)
254   also have "\<dots> = emeasure M X"
255     by (auto intro!: emeasure_eq_AE simp: AE_measure_pmf_iff)
256   finally show ?thesis
258 qed
260 lemma integral_pmf_restrict:
261   "(f::'a \<Rightarrow> 'b::{banach, second_countable_topology}) \<in> borel_measurable (count_space UNIV) \<Longrightarrow>
262     (\<integral>x. f x \<partial>measure_pmf M) = (\<integral>x. f x \<partial>restrict_space M M)"
263   by (auto intro!: integral_cong_AE simp add: integral_restrict_space AE_measure_pmf_iff)
265 lemma emeasure_pmf: "emeasure (M::'a pmf) M = 1"
266 proof -
267   have "emeasure (M::'a pmf) M = emeasure (M::'a pmf) (space M)"
268     by (intro emeasure_eq_AE) (simp_all add: AE_measure_pmf)
269   then show ?thesis
270     using measure_pmf.emeasure_space_1 by simp
271 qed
273 lemma emeasure_pmf_UNIV [simp]: "emeasure (measure_pmf M) UNIV = 1"
274 using measure_pmf.emeasure_space_1[of M] by simp
276 lemma in_null_sets_measure_pmfI:
277   "A \<inter> set_pmf p = {} \<Longrightarrow> A \<in> null_sets (measure_pmf p)"
278 using emeasure_eq_0_AE[where ?P="\<lambda>x. x \<in> A" and M="measure_pmf p"]
279 by(auto simp add: null_sets_def AE_measure_pmf_iff)
281 lemma measure_subprob: "measure_pmf M \<in> space (subprob_algebra (count_space UNIV))"
282   by (simp add: space_subprob_algebra subprob_space_measure_pmf)
284 subsection \<open> Monad Interpretation \<close>
286 lemma measurable_measure_pmf[measurable]:
287   "(\<lambda>x. measure_pmf (M x)) \<in> measurable (count_space UNIV) (subprob_algebra (count_space UNIV))"
288   by (auto simp: space_subprob_algebra intro!: prob_space_imp_subprob_space) unfold_locales
290 lemma bind_measure_pmf_cong:
291   assumes "\<And>x. A x \<in> space (subprob_algebra N)" "\<And>x. B x \<in> space (subprob_algebra N)"
292   assumes "\<And>i. i \<in> set_pmf x \<Longrightarrow> A i = B i"
293   shows "bind (measure_pmf x) A = bind (measure_pmf x) B"
294 proof (rule measure_eqI)
295   show "sets (measure_pmf x \<bind> A) = sets (measure_pmf x \<bind> B)"
296     using assms by (subst (1 2) sets_bind) (auto simp: space_subprob_algebra)
297 next
298   fix X assume "X \<in> sets (measure_pmf x \<bind> A)"
299   then have X: "X \<in> sets N"
300     using assms by (subst (asm) sets_bind) (auto simp: space_subprob_algebra)
301   show "emeasure (measure_pmf x \<bind> A) X = emeasure (measure_pmf x \<bind> B) X"
302     using assms
303     by (subst (1 2) emeasure_bind[where N=N, OF _ _ X])
304        (auto intro!: nn_integral_cong_AE simp: AE_measure_pmf_iff)
305 qed
307 lift_definition bind_pmf :: "'a pmf \<Rightarrow> ('a \<Rightarrow> 'b pmf ) \<Rightarrow> 'b pmf" is bind
308 proof (clarify, intro conjI)
309   fix f :: "'a measure" and g :: "'a \<Rightarrow> 'b measure"
310   assume "prob_space f"
311   then interpret f: prob_space f .
312   assume "sets f = UNIV" and ae_f: "AE x in f. measure f {x} \<noteq> 0"
313   then have s_f[simp]: "sets f = sets (count_space UNIV)"
314     by simp
315   assume g: "\<And>x. prob_space (g x) \<and> sets (g x) = UNIV \<and> (AE y in g x. measure (g x) {y} \<noteq> 0)"
316   then have g: "\<And>x. prob_space (g x)" and s_g[simp]: "\<And>x. sets (g x) = sets (count_space UNIV)"
317     and ae_g: "\<And>x. AE y in g x. measure (g x) {y} \<noteq> 0"
318     by auto
320   have [measurable]: "g \<in> measurable f (subprob_algebra (count_space UNIV))"
321     by (auto simp: measurable_def space_subprob_algebra prob_space_imp_subprob_space g)
323   show "prob_space (f \<bind> g)"
324     using g by (intro f.prob_space_bind[where S="count_space UNIV"]) auto
325   then interpret fg: prob_space "f \<bind> g" .
326   show [simp]: "sets (f \<bind> g) = UNIV"
327     using sets_eq_imp_space_eq[OF s_f]
328     by (subst sets_bind[where N="count_space UNIV"]) auto
329   show "AE x in f \<bind> g. measure (f \<bind> g) {x} \<noteq> 0"
330     apply (simp add: fg.prob_eq_0 AE_bind[where B="count_space UNIV"])
331     using ae_f
332     apply eventually_elim
333     using ae_g
334     apply eventually_elim
335     apply (auto dest: AE_measure_singleton)
336     done
337 qed
339 lemma ereal_pmf_bind: "pmf (bind_pmf N f) i = (\<integral>\<^sup>+x. pmf (f x) i \<partial>measure_pmf N)"
340   unfolding pmf.rep_eq bind_pmf.rep_eq
341   by (auto simp: measure_pmf.measure_bind[where N="count_space UNIV"] measure_subprob measure_nonneg
342            intro!: nn_integral_eq_integral[symmetric] measure_pmf.integrable_const_bound[where B=1])
344 lemma pmf_bind: "pmf (bind_pmf N f) i = (\<integral>x. pmf (f x) i \<partial>measure_pmf N)"
345   using ereal_pmf_bind[of N f i]
346   by (subst (asm) nn_integral_eq_integral)
347      (auto simp: pmf_nonneg pmf_le_1
348            intro!: nn_integral_eq_integral[symmetric] measure_pmf.integrable_const_bound[where B=1])
350 lemma bind_pmf_const[simp]: "bind_pmf M (\<lambda>x. c) = c"
351   by transfer (simp add: bind_const' prob_space_imp_subprob_space)
353 lemma set_bind_pmf[simp]: "set_pmf (bind_pmf M N) = (\<Union>M\<in>set_pmf M. set_pmf (N M))"
354   unfolding set_pmf_eq ereal_eq_0(1)[symmetric] ereal_pmf_bind
355   by (auto simp add: nn_integral_0_iff_AE AE_measure_pmf_iff set_pmf_eq not_le less_le pmf_nonneg)
357 lemma bind_pmf_cong:
358   assumes "p = q"
359   shows "(\<And>x. x \<in> set_pmf q \<Longrightarrow> f x = g x) \<Longrightarrow> bind_pmf p f = bind_pmf q g"
360   unfolding \<open>p = q\<close>[symmetric] measure_pmf_inject[symmetric] bind_pmf.rep_eq
361   by (auto simp: AE_measure_pmf_iff Pi_iff space_subprob_algebra subprob_space_measure_pmf
362                  sets_bind[where N="count_space UNIV"] emeasure_bind[where N="count_space UNIV"]
363            intro!: nn_integral_cong_AE measure_eqI)
365 lemma bind_pmf_cong_simp:
366   "p = q \<Longrightarrow> (\<And>x. x \<in> set_pmf q =simp=> f x = g x) \<Longrightarrow> bind_pmf p f = bind_pmf q g"
367   by (simp add: simp_implies_def cong: bind_pmf_cong)
369 lemma measure_pmf_bind: "measure_pmf (bind_pmf M f) = (measure_pmf M \<bind> (\<lambda>x. measure_pmf (f x)))"
370   by transfer simp
372 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)"
373   using measurable_measure_pmf[of N]
374   unfolding measure_pmf_bind
375   apply (subst (1 3) nn_integral_max_0[symmetric])
376   apply (intro nn_integral_bind[where B="count_space UNIV"])
377   apply auto
378   done
380 lemma emeasure_bind_pmf[simp]: "emeasure (bind_pmf M N) X = (\<integral>\<^sup>+x. emeasure (N x) X \<partial>M)"
381   using measurable_measure_pmf[of N]
382   unfolding measure_pmf_bind
383   by (subst emeasure_bind[where N="count_space UNIV"]) auto
385 lift_definition return_pmf :: "'a \<Rightarrow> 'a pmf" is "return (count_space UNIV)"
386   by (auto intro!: prob_space_return simp: AE_return measure_return)
388 lemma bind_return_pmf: "bind_pmf (return_pmf x) f = f x"
389   by transfer
390      (auto intro!: prob_space_imp_subprob_space bind_return[where N="count_space UNIV"]
391            simp: space_subprob_algebra)
393 lemma set_return_pmf[simp]: "set_pmf (return_pmf x) = {x}"
394   by transfer (auto simp add: measure_return split: split_indicator)
396 lemma bind_return_pmf': "bind_pmf N return_pmf = N"
397 proof (transfer, clarify)
398   fix N :: "'a measure" assume "sets N = UNIV" then show "N \<bind> return (count_space UNIV) = N"
399     by (subst return_sets_cong[where N=N]) (simp_all add: bind_return')
400 qed
402 lemma bind_assoc_pmf: "bind_pmf (bind_pmf A B) C = bind_pmf A (\<lambda>x. bind_pmf (B x) C)"
403   by transfer
404      (auto intro!: bind_assoc[where N="count_space UNIV" and R="count_space UNIV"]
405            simp: measurable_def space_subprob_algebra prob_space_imp_subprob_space)
407 definition "map_pmf f M = bind_pmf M (\<lambda>x. return_pmf (f x))"
409 lemma map_bind_pmf: "map_pmf f (bind_pmf M g) = bind_pmf M (\<lambda>x. map_pmf f (g x))"
410   by (simp add: map_pmf_def bind_assoc_pmf)
412 lemma bind_map_pmf: "bind_pmf (map_pmf f M) g = bind_pmf M (\<lambda>x. g (f x))"
413   by (simp add: map_pmf_def bind_assoc_pmf bind_return_pmf)
415 lemma map_pmf_transfer[transfer_rule]:
416   "rel_fun op = (rel_fun cr_pmf cr_pmf) (\<lambda>f M. distr M (count_space UNIV) f) map_pmf"
417 proof -
418   have "rel_fun op = (rel_fun pmf_as_measure.cr_pmf pmf_as_measure.cr_pmf)
419      (\<lambda>f M. M \<bind> (return (count_space UNIV) o f)) map_pmf"
420     unfolding map_pmf_def[abs_def] comp_def by transfer_prover
421   then show ?thesis
422     by (force simp: rel_fun_def cr_pmf_def bind_return_distr)
423 qed
425 lemma map_pmf_rep_eq:
426   "measure_pmf (map_pmf f M) = distr (measure_pmf M) (count_space UNIV) f"
427   unfolding map_pmf_def bind_pmf.rep_eq comp_def return_pmf.rep_eq
428   using bind_return_distr[of M f "count_space UNIV"] by (simp add: comp_def)
430 lemma map_pmf_id[simp]: "map_pmf id = id"
431   by (rule, transfer) (auto simp: emeasure_distr measurable_def intro!: measure_eqI)
433 lemma map_pmf_ident[simp]: "map_pmf (\<lambda>x. x) = (\<lambda>x. x)"
434   using map_pmf_id unfolding id_def .
436 lemma map_pmf_compose: "map_pmf (f \<circ> g) = map_pmf f \<circ> map_pmf g"
437   by (rule, transfer) (simp add: distr_distr[symmetric, where N="count_space UNIV"] measurable_def)
439 lemma map_pmf_comp: "map_pmf f (map_pmf g M) = map_pmf (\<lambda>x. f (g x)) M"
440   using map_pmf_compose[of f g] by (simp add: comp_def)
442 lemma map_pmf_cong: "p = q \<Longrightarrow> (\<And>x. x \<in> set_pmf q \<Longrightarrow> f x = g x) \<Longrightarrow> map_pmf f p = map_pmf g q"
443   unfolding map_pmf_def by (rule bind_pmf_cong) auto
445 lemma pmf_set_map: "set_pmf \<circ> map_pmf f = op ` f \<circ> set_pmf"
446   by (auto simp add: comp_def fun_eq_iff map_pmf_def)
448 lemma set_map_pmf[simp]: "set_pmf (map_pmf f M) = f`set_pmf M"
449   using pmf_set_map[of f] by (auto simp: comp_def fun_eq_iff)
451 lemma emeasure_map_pmf[simp]: "emeasure (map_pmf f M) X = emeasure M (f -` X)"
452   unfolding map_pmf_rep_eq by (subst emeasure_distr) auto
454 lemma measure_map_pmf[simp]: "measure (map_pmf f M) X = measure M (f -` X)"
455 using emeasure_map_pmf[of f M X] by(simp add: measure_pmf.emeasure_eq_measure)
457 lemma nn_integral_map_pmf[simp]: "(\<integral>\<^sup>+x. f x \<partial>map_pmf g M) = (\<integral>\<^sup>+x. f (g x) \<partial>M)"
458   unfolding map_pmf_rep_eq by (intro nn_integral_distr) auto
460 lemma ereal_pmf_map: "pmf (map_pmf f p) x = (\<integral>\<^sup>+ y. indicator (f -` {x}) y \<partial>measure_pmf p)"
461 proof (transfer fixing: f x)
462   fix p :: "'b measure"
463   presume "prob_space p"
464   then interpret prob_space p .
465   presume "sets p = UNIV"
466   then show "ereal (measure (distr p (count_space UNIV) f) {x}) = integral\<^sup>N p (indicator (f -` {x}))"
467     by(simp add: measure_distr measurable_def emeasure_eq_measure)
468 qed simp_all
470 lemma nn_integral_pmf: "(\<integral>\<^sup>+ x. pmf p x \<partial>count_space A) = emeasure (measure_pmf p) A"
471 proof -
472   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))"
473     by(auto simp add: nn_integral_count_space_indicator indicator_def set_pmf_iff intro: nn_integral_cong)
474   also have "\<dots> = emeasure (measure_pmf p) (\<Union>x\<in>A \<inter> set_pmf p. {x})"
475     by(subst emeasure_UN_countable)(auto simp add: emeasure_pmf_single disjoint_family_on_def)
476   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})"
477     by(rule emeasure_Un_null_set[symmetric])(auto intro: in_null_sets_measure_pmfI)
478   also have "\<dots> = emeasure (measure_pmf p) A"
479     by(auto intro: arg_cong2[where f=emeasure])
480   finally show ?thesis .
481 qed
483 lemma map_return_pmf [simp]: "map_pmf f (return_pmf x) = return_pmf (f x)"
484   by transfer (simp add: distr_return)
486 lemma map_pmf_const[simp]: "map_pmf (\<lambda>_. c) M = return_pmf c"
487   by transfer (auto simp: prob_space.distr_const)
489 lemma pmf_return [simp]: "pmf (return_pmf x) y = indicator {y} x"
490   by transfer (simp add: measure_return)
492 lemma nn_integral_return_pmf[simp]: "0 \<le> f x \<Longrightarrow> (\<integral>\<^sup>+x. f x \<partial>return_pmf x) = f x"
493   unfolding return_pmf.rep_eq by (intro nn_integral_return) auto
495 lemma emeasure_return_pmf[simp]: "emeasure (return_pmf x) X = indicator X x"
496   unfolding return_pmf.rep_eq by (intro emeasure_return) auto
498 lemma return_pmf_inj[simp]: "return_pmf x = return_pmf y \<longleftrightarrow> x = y"
499   by (metis insertI1 set_return_pmf singletonD)
501 lemma map_pmf_eq_return_pmf_iff:
502   "map_pmf f p = return_pmf x \<longleftrightarrow> (\<forall>y \<in> set_pmf p. f y = x)"
503 proof
504   assume "map_pmf f p = return_pmf x"
505   then have "set_pmf (map_pmf f p) = set_pmf (return_pmf x)" by simp
506   then show "\<forall>y \<in> set_pmf p. f y = x" by auto
507 next
508   assume "\<forall>y \<in> set_pmf p. f y = x"
509   then show "map_pmf f p = return_pmf x"
510     unfolding map_pmf_const[symmetric, of _ p] by (intro map_pmf_cong) auto
511 qed
513 definition "pair_pmf A B = bind_pmf A (\<lambda>x. bind_pmf B (\<lambda>y. return_pmf (x, y)))"
515 lemma pmf_pair: "pmf (pair_pmf M N) (a, b) = pmf M a * pmf N b"
516   unfolding pair_pmf_def pmf_bind pmf_return
517   apply (subst integral_measure_pmf[where A="{b}"])
518   apply (auto simp: indicator_eq_0_iff)
519   apply (subst integral_measure_pmf[where A="{a}"])
520   apply (auto simp: indicator_eq_0_iff setsum_nonneg_eq_0_iff pmf_nonneg)
521   done
523 lemma set_pair_pmf[simp]: "set_pmf (pair_pmf A B) = set_pmf A \<times> set_pmf B"
524   unfolding pair_pmf_def set_bind_pmf set_return_pmf by auto
526 lemma measure_pmf_in_subprob_space[measurable (raw)]:
527   "measure_pmf M \<in> space (subprob_algebra (count_space UNIV))"
528   by (simp add: space_subprob_algebra) intro_locales
530 lemma nn_integral_pair_pmf': "(\<integral>\<^sup>+x. f x \<partial>pair_pmf A B) = (\<integral>\<^sup>+a. \<integral>\<^sup>+b. f (a, b) \<partial>B \<partial>A)"
531 proof -
532   have "(\<integral>\<^sup>+x. f x \<partial>pair_pmf A B) = (\<integral>\<^sup>+x. max 0 (f x) * indicator (A \<times> B) x \<partial>pair_pmf A B)"
533     by (subst nn_integral_max_0[symmetric])
534        (auto simp: AE_measure_pmf_iff intro!: nn_integral_cong_AE)
535   also have "\<dots> = (\<integral>\<^sup>+a. \<integral>\<^sup>+b. max 0 (f (a, b)) * indicator (A \<times> B) (a, b) \<partial>B \<partial>A)"
537   also have "\<dots> = (\<integral>\<^sup>+a. \<integral>\<^sup>+b. max 0 (f (a, b)) \<partial>B \<partial>A)"
538     by (auto intro!: nn_integral_cong_AE simp: AE_measure_pmf_iff)
539   finally show ?thesis
540     unfolding nn_integral_max_0 .
541 qed
543 lemma bind_pair_pmf:
544   assumes M[measurable]: "M \<in> measurable (count_space UNIV \<Otimes>\<^sub>M count_space UNIV) (subprob_algebra N)"
545   shows "measure_pmf (pair_pmf A B) \<bind> M = (measure_pmf A \<bind> (\<lambda>x. measure_pmf B \<bind> (\<lambda>y. M (x, y))))"
546     (is "?L = ?R")
547 proof (rule measure_eqI)
548   have M'[measurable]: "M \<in> measurable (pair_pmf A B) (subprob_algebra N)"
549     using M[THEN measurable_space] by (simp_all add: space_pair_measure)
551   note measurable_bind[where N="count_space UNIV", measurable]
552   note measure_pmf_in_subprob_space[simp]
554   have sets_eq_N: "sets ?L = N"
555     by (subst sets_bind[OF sets_kernel[OF M']]) auto
556   show "sets ?L = sets ?R"
557     using measurable_space[OF M]
558     by (simp add: sets_eq_N space_pair_measure space_subprob_algebra)
559   fix X assume "X \<in> sets ?L"
560   then have X[measurable]: "X \<in> sets N"
561     unfolding sets_eq_N .
562   then show "emeasure ?L X = emeasure ?R X"
563     apply (simp add: emeasure_bind[OF _ M' X])
564     apply (simp add: nn_integral_bind[where B="count_space UNIV"] pair_pmf_def measure_pmf_bind[of A]
565                      nn_integral_measure_pmf_finite emeasure_nonneg one_ereal_def[symmetric])
566     apply (subst emeasure_bind[OF _ _ X])
567     apply measurable
568     apply (subst emeasure_bind[OF _ _ X])
569     apply measurable
570     done
571 qed
573 lemma map_fst_pair_pmf: "map_pmf fst (pair_pmf A B) = A"
574   by (simp add: pair_pmf_def map_pmf_def bind_assoc_pmf bind_return_pmf bind_return_pmf')
576 lemma map_snd_pair_pmf: "map_pmf snd (pair_pmf A B) = B"
577   by (simp add: pair_pmf_def map_pmf_def bind_assoc_pmf bind_return_pmf bind_return_pmf')
579 lemma nn_integral_pmf':
580   "inj_on f A \<Longrightarrow> (\<integral>\<^sup>+x. pmf p (f x) \<partial>count_space A) = emeasure p (f ` A)"
581   by (subst nn_integral_bij_count_space[where g=f and B="f`A"])
582      (auto simp: bij_betw_def nn_integral_pmf)
584 lemma pmf_le_0_iff[simp]: "pmf M p \<le> 0 \<longleftrightarrow> pmf M p = 0"
585   using pmf_nonneg[of M p] by simp
587 lemma min_pmf_0[simp]: "min (pmf M p) 0 = 0" "min 0 (pmf M p) = 0"
588   using pmf_nonneg[of M p] by simp_all
590 lemma pmf_eq_0_set_pmf: "pmf M p = 0 \<longleftrightarrow> p \<notin> set_pmf M"
591   unfolding set_pmf_iff by simp
593 lemma pmf_map_inj: "inj_on f (set_pmf M) \<Longrightarrow> x \<in> set_pmf M \<Longrightarrow> pmf (map_pmf f M) (f x) = pmf M x"
594   by (auto simp: pmf.rep_eq map_pmf_rep_eq measure_distr AE_measure_pmf_iff inj_onD
595            intro!: measure_pmf.finite_measure_eq_AE)
597 lemma pmf_map_inj': "inj f \<Longrightarrow> pmf (map_pmf f M) (f x) = pmf M x"
598 apply(cases "x \<in> set_pmf M")
601 apply(auto simp add: pmf_eq_0_set_pmf dest: injD)
602 done
604 lemma pmf_map_outside: "x \<notin> f ` set_pmf M \<Longrightarrow> pmf (map_pmf f M) x = 0"
605 unfolding pmf_eq_0_set_pmf by simp
607 subsection \<open> PMFs as function \<close>
609 context
610   fixes f :: "'a \<Rightarrow> real"
611   assumes nonneg: "\<And>x. 0 \<le> f x"
612   assumes prob: "(\<integral>\<^sup>+x. f x \<partial>count_space UNIV) = 1"
613 begin
615 lift_definition embed_pmf :: "'a pmf" is "density (count_space UNIV) (ereal \<circ> f)"
616 proof (intro conjI)
617   have *[simp]: "\<And>x y. ereal (f y) * indicator {x} y = ereal (f x) * indicator {x} y"
618     by (simp split: split_indicator)
619   show "AE x in density (count_space UNIV) (ereal \<circ> f).
620     measure (density (count_space UNIV) (ereal \<circ> f)) {x} \<noteq> 0"
621     by (simp add: AE_density nonneg measure_def emeasure_density max_def)
622   show "prob_space (density (count_space UNIV) (ereal \<circ> f))"
623     by standard (simp add: emeasure_density prob)
624 qed simp
626 lemma pmf_embed_pmf: "pmf embed_pmf x = f x"
627 proof transfer
628   have *[simp]: "\<And>x y. ereal (f y) * indicator {x} y = ereal (f x) * indicator {x} y"
629     by (simp split: split_indicator)
630   fix x show "measure (density (count_space UNIV) (ereal \<circ> f)) {x} = f x"
631     by transfer (simp add: measure_def emeasure_density nonneg max_def)
632 qed
634 lemma set_embed_pmf: "set_pmf embed_pmf = {x. f x \<noteq> 0}"
635 by(auto simp add: set_pmf_eq assms pmf_embed_pmf)
637 end
639 lemma embed_pmf_transfer:
640   "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"
641   by (auto simp: rel_fun_def eq_onp_def embed_pmf.transfer)
643 lemma measure_pmf_eq_density: "measure_pmf p = density (count_space UNIV) (pmf p)"
644 proof (transfer, elim conjE)
645   fix M :: "'a measure" assume [simp]: "sets M = UNIV" and ae: "AE x in M. measure M {x} \<noteq> 0"
646   assume "prob_space M" then interpret prob_space M .
647   show "M = density (count_space UNIV) (\<lambda>x. ereal (measure M {x}))"
648   proof (rule measure_eqI)
649     fix A :: "'a set"
650     have "(\<integral>\<^sup>+ x. ereal (measure M {x}) * indicator A x \<partial>count_space UNIV) =
651       (\<integral>\<^sup>+ x. emeasure M {x} * indicator (A \<inter> {x. measure M {x} \<noteq> 0}) x \<partial>count_space UNIV)"
652       by (auto intro!: nn_integral_cong simp: emeasure_eq_measure split: split_indicator)
653     also have "\<dots> = (\<integral>\<^sup>+ x. emeasure M {x} \<partial>count_space (A \<inter> {x. measure M {x} \<noteq> 0}))"
654       by (subst nn_integral_restrict_space[symmetric]) (auto simp: restrict_count_space)
655     also have "\<dots> = emeasure M (\<Union>x\<in>(A \<inter> {x. measure M {x} \<noteq> 0}). {x})"
656       by (intro emeasure_UN_countable[symmetric] countable_Int2 countable_support)
657          (auto simp: disjoint_family_on_def)
658     also have "\<dots> = emeasure M A"
659       using ae by (intro emeasure_eq_AE) auto
660     finally show " emeasure M A = emeasure (density (count_space UNIV) (\<lambda>x. ereal (measure M {x}))) A"
661       using emeasure_space_1 by (simp add: emeasure_density)
662   qed simp
663 qed
665 lemma td_pmf_embed_pmf:
666   "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}"
667   unfolding type_definition_def
668 proof safe
669   fix p :: "'a pmf"
670   have "(\<integral>\<^sup>+ x. 1 \<partial>measure_pmf p) = 1"
671     using measure_pmf.emeasure_space_1[of p] by simp
672   then show *: "(\<integral>\<^sup>+ x. ereal (pmf p x) \<partial>count_space UNIV) = 1"
673     by (simp add: measure_pmf_eq_density nn_integral_density pmf_nonneg del: nn_integral_const)
675   show "embed_pmf (pmf p) = p"
676     by (intro measure_pmf_inject[THEN iffD1])
677        (simp add: * embed_pmf.rep_eq pmf_nonneg measure_pmf_eq_density[of p] comp_def)
678 next
679   fix f :: "'a \<Rightarrow> real" assume "\<forall>x. 0 \<le> f x" "(\<integral>\<^sup>+x. f x \<partial>count_space UNIV) = 1"
680   then show "pmf (embed_pmf f) = f"
681     by (auto intro!: pmf_embed_pmf)
682 qed (rule pmf_nonneg)
684 end
686 lemma nn_integral_measure_pmf: "(\<integral>\<^sup>+ x. f x \<partial>measure_pmf p) = \<integral>\<^sup>+ x. ereal (pmf p x) * f x \<partial>count_space UNIV"
687 by(simp add: measure_pmf_eq_density nn_integral_density pmf_nonneg)
689 locale pmf_as_function
690 begin
692 setup_lifting td_pmf_embed_pmf
694 lemma set_pmf_transfer[transfer_rule]:
695   assumes "bi_total A"
696   shows "rel_fun (pcr_pmf A) (rel_set A) (\<lambda>f. {x. f x \<noteq> 0}) set_pmf"
697   using \<open>bi_total A\<close>
698   by (auto simp: pcr_pmf_def cr_pmf_def rel_fun_def rel_set_def bi_total_def Bex_def set_pmf_iff)
699      metis+
701 end
703 context
704 begin
706 interpretation pmf_as_function .
708 lemma pmf_eqI: "(\<And>i. pmf M i = pmf N i) \<Longrightarrow> M = N"
709   by transfer auto
711 lemma pmf_eq_iff: "M = N \<longleftrightarrow> (\<forall>i. pmf M i = pmf N i)"
712   by (auto intro: pmf_eqI)
714 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))"
715   unfolding pmf_eq_iff pmf_bind
716 proof
717   fix i
718   interpret B: prob_space "restrict_space B B"
719     by (intro prob_space_restrict_space measure_pmf.emeasure_eq_1_AE)
720        (auto simp: AE_measure_pmf_iff)
721   interpret A: prob_space "restrict_space A A"
722     by (intro prob_space_restrict_space measure_pmf.emeasure_eq_1_AE)
723        (auto simp: AE_measure_pmf_iff)
725   interpret AB: pair_prob_space "restrict_space A A" "restrict_space B B"
726     by unfold_locales
728   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)"
729     by (rule integral_cong) (auto intro!: integral_pmf_restrict)
730   also have "\<dots> = (\<integral> x. (\<integral> y. pmf (C x y) i \<partial>restrict_space B B) \<partial>restrict_space A A)"
731     by (intro integral_pmf_restrict B.borel_measurable_lebesgue_integral measurable_pair_restrict_pmf2
732               countable_set_pmf borel_measurable_count_space)
733   also have "\<dots> = (\<integral> y. \<integral> x. pmf (C x y) i \<partial>restrict_space A A \<partial>restrict_space B B)"
734     by (rule AB.Fubini_integral[symmetric])
735        (auto intro!: AB.integrable_const_bound[where B=1] measurable_pair_restrict_pmf2
736              simp: pmf_nonneg pmf_le_1 measurable_restrict_space1)
737   also have "\<dots> = (\<integral> y. \<integral> x. pmf (C x y) i \<partial>restrict_space A A \<partial>B)"
738     by (intro integral_pmf_restrict[symmetric] A.borel_measurable_lebesgue_integral measurable_pair_restrict_pmf2
739               countable_set_pmf borel_measurable_count_space)
740   also have "\<dots> = (\<integral> y. \<integral> x. pmf (C x y) i \<partial>A \<partial>B)"
741     by (rule integral_cong) (auto intro!: integral_pmf_restrict[symmetric])
742   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)" .
743 qed
745 lemma pair_map_pmf1: "pair_pmf (map_pmf f A) B = map_pmf (apfst f) (pair_pmf A B)"
746 proof (safe intro!: pmf_eqI)
747   fix a :: "'a" and b :: "'b"
748   have [simp]: "\<And>c d. indicator (apfst f -` {(a, b)}) (c, d) = indicator (f -` {a}) c * (indicator {b} d::ereal)"
749     by (auto split: split_indicator)
751   have "ereal (pmf (pair_pmf (map_pmf f A) B) (a, b)) =
752          ereal (pmf (map_pmf (apfst f) (pair_pmf A B)) (a, b))"
753     unfolding pmf_pair ereal_pmf_map
754     by (simp add: nn_integral_pair_pmf' max_def emeasure_pmf_single nn_integral_multc pmf_nonneg
755                   emeasure_map_pmf[symmetric] del: emeasure_map_pmf)
756   then show "pmf (pair_pmf (map_pmf f A) B) (a, b) = pmf (map_pmf (apfst f) (pair_pmf A B)) (a, b)"
757     by simp
758 qed
760 lemma pair_map_pmf2: "pair_pmf A (map_pmf f B) = map_pmf (apsnd f) (pair_pmf A B)"
761 proof (safe intro!: pmf_eqI)
762   fix a :: "'a" and b :: "'b"
763   have [simp]: "\<And>c d. indicator (apsnd f -` {(a, b)}) (c, d) = indicator {a} c * (indicator (f -` {b}) d::ereal)"
764     by (auto split: split_indicator)
766   have "ereal (pmf (pair_pmf A (map_pmf f B)) (a, b)) =
767          ereal (pmf (map_pmf (apsnd f) (pair_pmf A B)) (a, b))"
768     unfolding pmf_pair ereal_pmf_map
769     by (simp add: nn_integral_pair_pmf' max_def emeasure_pmf_single nn_integral_cmult nn_integral_multc pmf_nonneg
770                   emeasure_map_pmf[symmetric] del: emeasure_map_pmf)
771   then show "pmf (pair_pmf A (map_pmf f B)) (a, b) = pmf (map_pmf (apsnd f) (pair_pmf A B)) (a, b)"
772     by simp
773 qed
775 lemma map_pair: "map_pmf (\<lambda>(a, b). (f a, g b)) (pair_pmf A B) = pair_pmf (map_pmf f A) (map_pmf g B)"
776   by (simp add: pair_map_pmf2 pair_map_pmf1 map_pmf_comp split_beta')
778 end
780 lemma pair_return_pmf1: "pair_pmf (return_pmf x) y = map_pmf (Pair x) y"
781 by(simp add: pair_pmf_def bind_return_pmf map_pmf_def)
783 lemma pair_return_pmf2: "pair_pmf x (return_pmf y) = map_pmf (\<lambda>x. (x, y)) x"
784 by(simp add: pair_pmf_def bind_return_pmf map_pmf_def)
786 lemma pair_pair_pmf: "pair_pmf (pair_pmf u v) w = map_pmf (\<lambda>(x, (y, z)). ((x, y), z)) (pair_pmf u (pair_pmf v w))"
787 by(simp add: pair_pmf_def bind_return_pmf map_pmf_def bind_assoc_pmf)
789 lemma pair_commute_pmf: "pair_pmf x y = map_pmf (\<lambda>(x, y). (y, x)) (pair_pmf y x)"
790 unfolding pair_pmf_def by(subst bind_commute_pmf)(simp add: map_pmf_def bind_assoc_pmf bind_return_pmf)
792 lemma set_pmf_subset_singleton: "set_pmf p \<subseteq> {x} \<longleftrightarrow> p = return_pmf x"
793 proof(intro iffI pmf_eqI)
794   fix i
795   assume x: "set_pmf p \<subseteq> {x}"
796   hence *: "set_pmf p = {x}" using set_pmf_not_empty[of p] by auto
797   have "ereal (pmf p x) = \<integral>\<^sup>+ i. indicator {x} i \<partial>p" by(simp add: emeasure_pmf_single)
798   also have "\<dots> = \<integral>\<^sup>+ i. 1 \<partial>p" by(rule nn_integral_cong_AE)(simp add: AE_measure_pmf_iff * )
799   also have "\<dots> = 1" by simp
800   finally show "pmf p i = pmf (return_pmf x) i" using x
801     by(auto split: split_indicator simp add: pmf_eq_0_set_pmf)
802 qed auto
804 lemma bind_eq_return_pmf:
805   "bind_pmf p f = return_pmf x \<longleftrightarrow> (\<forall>y\<in>set_pmf p. f y = return_pmf x)"
806   (is "?lhs \<longleftrightarrow> ?rhs")
807 proof(intro iffI strip)
808   fix y
809   assume y: "y \<in> set_pmf p"
810   assume "?lhs"
811   hence "set_pmf (bind_pmf p f) = {x}" by simp
812   hence "(\<Union>y\<in>set_pmf p. set_pmf (f y)) = {x}" by simp
813   hence "set_pmf (f y) \<subseteq> {x}" using y by auto
814   thus "f y = return_pmf x" by(simp add: set_pmf_subset_singleton)
815 next
816   assume *: ?rhs
817   show ?lhs
818   proof(rule pmf_eqI)
819     fix i
820     have "ereal (pmf (bind_pmf p f) i) = \<integral>\<^sup>+ y. ereal (pmf (f y) i) \<partial>p" by(simp add: ereal_pmf_bind)
821     also have "\<dots> = \<integral>\<^sup>+ y. ereal (pmf (return_pmf x) i) \<partial>p"
822       by(rule nn_integral_cong_AE)(simp add: AE_measure_pmf_iff * )
823     also have "\<dots> = ereal (pmf (return_pmf x) i)" by simp
824     finally show "pmf (bind_pmf p f) i = pmf (return_pmf x) i" by simp
825   qed
826 qed
828 lemma pmf_False_conv_True: "pmf p False = 1 - pmf p True"
829 proof -
830   have "pmf p False + pmf p True = measure p {False} + measure p {True}"
832   also have "\<dots> = measure p ({False} \<union> {True})"
833     by(subst measure_pmf.finite_measure_Union) simp_all
834   also have "{False} \<union> {True} = space p" by auto
835   finally show ?thesis by simp
836 qed
838 lemma pmf_True_conv_False: "pmf p True = 1 - pmf p False"
841 subsection \<open> Conditional Probabilities \<close>
843 lemma measure_pmf_zero_iff: "measure (measure_pmf p) s = 0 \<longleftrightarrow> set_pmf p \<inter> s = {}"
844   by (subst measure_pmf.prob_eq_0) (auto simp: AE_measure_pmf_iff)
846 context
847   fixes p :: "'a pmf" and s :: "'a set"
848   assumes not_empty: "set_pmf p \<inter> s \<noteq> {}"
849 begin
851 interpretation pmf_as_measure .
853 lemma emeasure_measure_pmf_not_zero: "emeasure (measure_pmf p) s \<noteq> 0"
854 proof
855   assume "emeasure (measure_pmf p) s = 0"
856   then have "AE x in measure_pmf p. x \<notin> s"
857     by (rule AE_I[rotated]) auto
858   with not_empty show False
859     by (auto simp: AE_measure_pmf_iff)
860 qed
862 lemma measure_measure_pmf_not_zero: "measure (measure_pmf p) s \<noteq> 0"
863   using emeasure_measure_pmf_not_zero unfolding measure_pmf.emeasure_eq_measure by simp
865 lift_definition cond_pmf :: "'a pmf" is
866   "uniform_measure (measure_pmf p) s"
867 proof (intro conjI)
868   show "prob_space (uniform_measure (measure_pmf p) s)"
869     by (intro prob_space_uniform_measure) (auto simp: emeasure_measure_pmf_not_zero)
870   show "AE x in uniform_measure (measure_pmf p) s. measure (uniform_measure (measure_pmf p) s) {x} \<noteq> 0"
871     by (simp add: emeasure_measure_pmf_not_zero measure_measure_pmf_not_zero AE_uniform_measure
872                   AE_measure_pmf_iff set_pmf.rep_eq)
873 qed simp
875 lemma pmf_cond: "pmf cond_pmf x = (if x \<in> s then pmf p x / measure p s else 0)"
876   by transfer (simp add: emeasure_measure_pmf_not_zero pmf.rep_eq)
878 lemma set_cond_pmf[simp]: "set_pmf cond_pmf = set_pmf p \<inter> s"
879   by (auto simp add: set_pmf_iff pmf_cond measure_measure_pmf_not_zero split: split_if_asm)
881 end
883 lemma cond_map_pmf:
884   assumes "set_pmf p \<inter> f -` s \<noteq> {}"
885   shows "cond_pmf (map_pmf f p) s = map_pmf f (cond_pmf p (f -` s))"
886 proof -
887   have *: "set_pmf (map_pmf f p) \<inter> s \<noteq> {}"
888     using assms by auto
889   { fix x
890     have "ereal (pmf (map_pmf f (cond_pmf p (f -` s))) x) =
891       emeasure p (f -` s \<inter> f -` {x}) / emeasure p (f -` s)"
892       unfolding ereal_pmf_map cond_pmf.rep_eq[OF assms] by (simp add: nn_integral_uniform_measure)
893     also have "f -` s \<inter> f -` {x} = (if x \<in> s then f -` {x} else {})"
894       by auto
895     also have "emeasure p (if x \<in> s then f -` {x} else {}) / emeasure p (f -` s) =
896       ereal (pmf (cond_pmf (map_pmf f p) s) x)"
897       using measure_measure_pmf_not_zero[OF *]
898       by (simp add: pmf_cond[OF *] ereal_divide' ereal_pmf_map measure_pmf.emeasure_eq_measure[symmetric]
899                del: ereal_divide)
900     finally have "ereal (pmf (cond_pmf (map_pmf f p) s) x) = ereal (pmf (map_pmf f (cond_pmf p (f -` s))) x)"
901       by simp }
902   then show ?thesis
903     by (intro pmf_eqI) simp
904 qed
906 lemma bind_cond_pmf_cancel:
907   assumes [simp]: "\<And>x. x \<in> set_pmf p \<Longrightarrow> set_pmf q \<inter> {y. R x y} \<noteq> {}"
908   assumes [simp]: "\<And>y. y \<in> set_pmf q \<Longrightarrow> set_pmf p \<inter> {x. R x y} \<noteq> {}"
909   assumes [simp]: "\<And>x y. x \<in> set_pmf p \<Longrightarrow> y \<in> set_pmf q \<Longrightarrow> R x y \<Longrightarrow> measure q {y. R x y} = measure p {x. R x y}"
910   shows "bind_pmf p (\<lambda>x. cond_pmf q {y. R x y}) = q"
911 proof (rule pmf_eqI)
912   fix i
913   have "ereal (pmf (bind_pmf p (\<lambda>x. cond_pmf q {y. R x y})) i) =
914     (\<integral>\<^sup>+x. ereal (pmf q i / measure p {x. R x i}) * ereal (indicator {x. R x i} x) \<partial>p)"
915     by (auto simp add: ereal_pmf_bind AE_measure_pmf_iff pmf_cond pmf_eq_0_set_pmf intro!: nn_integral_cong_AE)
916   also have "\<dots> = (pmf q i * measure p {x. R x i}) / measure p {x. R x i}"
917     by (simp add: pmf_nonneg measure_nonneg zero_ereal_def[symmetric] ereal_indicator
918                   nn_integral_cmult measure_pmf.emeasure_eq_measure)
919   also have "\<dots> = pmf q i"
920     by (cases "pmf q i = 0") (simp_all add: pmf_eq_0_set_pmf measure_measure_pmf_not_zero)
921   finally show "pmf (bind_pmf p (\<lambda>x. cond_pmf q {y. R x y})) i = pmf q i"
922     by simp
923 qed
925 subsection \<open> Relator \<close>
927 inductive rel_pmf :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> 'a pmf \<Rightarrow> 'b pmf \<Rightarrow> bool"
928 for R p q
929 where
930   "\<lbrakk> \<And>x y. (x, y) \<in> set_pmf pq \<Longrightarrow> R x y;
931      map_pmf fst pq = p; map_pmf snd pq = q \<rbrakk>
932   \<Longrightarrow> rel_pmf R p q"
934 lemma rel_pmfI:
935   assumes R: "rel_set R (set_pmf p) (set_pmf q)"
936   assumes eq: "\<And>x y. x \<in> set_pmf p \<Longrightarrow> y \<in> set_pmf q \<Longrightarrow> R x y \<Longrightarrow>
937     measure p {x. R x y} = measure q {y. R x y}"
938   shows "rel_pmf R p q"
939 proof
940   let ?pq = "bind_pmf p (\<lambda>x. bind_pmf (cond_pmf q {y. R x y}) (\<lambda>y. return_pmf (x, y)))"
941   have "\<And>x. x \<in> set_pmf p \<Longrightarrow> set_pmf q \<inter> {y. R x y} \<noteq> {}"
942     using R by (auto simp: rel_set_def)
943   then show "\<And>x y. (x, y) \<in> set_pmf ?pq \<Longrightarrow> R x y"
944     by auto
945   show "map_pmf fst ?pq = p"
946     by (simp add: map_bind_pmf bind_return_pmf')
948   show "map_pmf snd ?pq = q"
949     using R eq
950     apply (simp add: bind_cond_pmf_cancel map_bind_pmf bind_return_pmf')
951     apply (rule bind_cond_pmf_cancel)
952     apply (auto simp: rel_set_def)
953     done
954 qed
956 lemma rel_pmf_imp_rel_set: "rel_pmf R p q \<Longrightarrow> rel_set R (set_pmf p) (set_pmf q)"
957   by (force simp add: rel_pmf.simps rel_set_def)
959 lemma rel_pmfD_measure:
960   assumes rel_R: "rel_pmf R p q" and R: "\<And>a b. R a b \<Longrightarrow> R a y \<longleftrightarrow> R x b"
961   assumes "x \<in> set_pmf p" "y \<in> set_pmf q"
962   shows "measure p {x. R x y} = measure q {y. R x y}"
963 proof -
964   from rel_R obtain pq where pq: "\<And>x y. (x, y) \<in> set_pmf pq \<Longrightarrow> R x y"
965     and eq: "p = map_pmf fst pq" "q = map_pmf snd pq"
966     by (auto elim: rel_pmf.cases)
967   have "measure p {x. R x y} = measure pq {x. R (fst x) y}"
968     by (simp add: eq map_pmf_rep_eq measure_distr)
969   also have "\<dots> = measure pq {y. R x (snd y)}"
970     by (intro measure_pmf.finite_measure_eq_AE)
971        (auto simp: AE_measure_pmf_iff R dest!: pq)
972   also have "\<dots> = measure q {y. R x y}"
973     by (simp add: eq map_pmf_rep_eq measure_distr)
974   finally show "measure p {x. R x y} = measure q {y. R x y}" .
975 qed
977 lemma rel_pmf_measureD:
978   assumes "rel_pmf R p q"
979   shows "measure (measure_pmf p) A \<le> measure (measure_pmf q) {y. \<exists>x\<in>A. R x y}" (is "?lhs \<le> ?rhs")
980 using assms
981 proof cases
982   fix pq
983   assume R: "\<And>x y. (x, y) \<in> set_pmf pq \<Longrightarrow> R x y"
984     and p[symmetric]: "map_pmf fst pq = p"
985     and q[symmetric]: "map_pmf snd pq = q"
986   have "?lhs = measure (measure_pmf pq) (fst -` A)" by(simp add: p)
987   also have "\<dots> \<le> measure (measure_pmf pq) {y. \<exists>x\<in>A. R x (snd y)}"
988     by(rule measure_pmf.finite_measure_mono_AE)(auto 4 3 simp add: AE_measure_pmf_iff dest: R)
989   also have "\<dots> = ?rhs" by(simp add: q)
990   finally show ?thesis .
991 qed
993 lemma rel_pmf_iff_measure:
994   assumes "symp R" "transp R"
995   shows "rel_pmf R p q \<longleftrightarrow>
996     rel_set R (set_pmf p) (set_pmf q) \<and>
997     (\<forall>x\<in>set_pmf p. \<forall>y\<in>set_pmf q. R x y \<longrightarrow> measure p {x. R x y} = measure q {y. R x y})"
998   by (safe intro!: rel_pmf_imp_rel_set rel_pmfI)
999      (auto intro!: rel_pmfD_measure dest: sympD[OF \<open>symp R\<close>] transpD[OF \<open>transp R\<close>])
1001 lemma quotient_rel_set_disjoint:
1002   "equivp R \<Longrightarrow> C \<in> UNIV // {(x, y). R x y} \<Longrightarrow> rel_set R A B \<Longrightarrow> A \<inter> C = {} \<longleftrightarrow> B \<inter> C = {}"
1003   using in_quotient_imp_closed[of UNIV "{(x, y). R x y}" C]
1004   by (auto 0 0 simp: equivp_equiv rel_set_def set_eq_iff elim: equivpE)
1005      (blast dest: equivp_symp)+
1007 lemma quotientD: "equiv X R \<Longrightarrow> A \<in> X // R \<Longrightarrow> x \<in> A \<Longrightarrow> A = R `` {x}"
1008   by (metis Image_singleton_iff equiv_class_eq_iff quotientE)
1010 lemma rel_pmf_iff_equivp:
1011   assumes "equivp R"
1012   shows "rel_pmf R p q \<longleftrightarrow> (\<forall>C\<in>UNIV // {(x, y). R x y}. measure p C = measure q C)"
1013     (is "_ \<longleftrightarrow>   (\<forall>C\<in>_//?R. _)")
1014 proof (subst rel_pmf_iff_measure, safe)
1015   show "symp R" "transp R"
1016     using assms by (auto simp: equivp_reflp_symp_transp)
1017 next
1018   fix C assume C: "C \<in> UNIV // ?R" and R: "rel_set R (set_pmf p) (set_pmf q)"
1019   assume eq: "\<forall>x\<in>set_pmf p. \<forall>y\<in>set_pmf q. R x y \<longrightarrow> measure p {x. R x y} = measure q {y. R x y}"
1021   show "measure p C = measure q C"
1022   proof cases
1023     assume "p \<inter> C = {}"
1024     moreover then have "q \<inter> C = {}"
1025       using quotient_rel_set_disjoint[OF assms C R] by simp
1026     ultimately show ?thesis
1027       unfolding measure_pmf_zero_iff[symmetric] by simp
1028   next
1029     assume "p \<inter> C \<noteq> {}"
1030     moreover then have "q \<inter> C \<noteq> {}"
1031       using quotient_rel_set_disjoint[OF assms C R] by simp
1032     ultimately obtain x y where in_set: "x \<in> set_pmf p" "y \<in> set_pmf q" and in_C: "x \<in> C" "y \<in> C"
1033       by auto
1034     then have "R x y"
1035       using in_quotient_imp_in_rel[of UNIV ?R C x y] C assms
1037     with in_set eq have "measure p {x. R x y} = measure q {y. R x y}"
1038       by auto
1039     moreover have "{y. R x y} = C"
1040       using assms \<open>x \<in> C\<close> C quotientD[of UNIV ?R C x] by (simp add: equivp_equiv)
1041     moreover have "{x. R x y} = C"
1042       using assms \<open>y \<in> C\<close> C quotientD[of UNIV "?R" C y] sympD[of R]
1043       by (auto simp add: equivp_equiv elim: equivpE)
1044     ultimately show ?thesis
1045       by auto
1046   qed
1047 next
1048   assume eq: "\<forall>C\<in>UNIV // ?R. measure p C = measure q C"
1049   show "rel_set R (set_pmf p) (set_pmf q)"
1050     unfolding rel_set_def
1051   proof safe
1052     fix x assume x: "x \<in> set_pmf p"
1053     have "{y. R x y} \<in> UNIV // ?R"
1054       by (auto simp: quotient_def)
1055     with eq have *: "measure q {y. R x y} = measure p {y. R x y}"
1056       by auto
1057     have "measure q {y. R x y} \<noteq> 0"
1058       using x assms unfolding * by (auto simp: measure_pmf_zero_iff set_eq_iff dest: equivp_reflp)
1059     then show "\<exists>y\<in>set_pmf q. R x y"
1060       unfolding measure_pmf_zero_iff by auto
1061   next
1062     fix y assume y: "y \<in> set_pmf q"
1063     have "{x. R x y} \<in> UNIV // ?R"
1064       using assms by (auto simp: quotient_def dest: equivp_symp)
1065     with eq have *: "measure p {x. R x y} = measure q {x. R x y}"
1066       by auto
1067     have "measure p {x. R x y} \<noteq> 0"
1068       using y assms unfolding * by (auto simp: measure_pmf_zero_iff set_eq_iff dest: equivp_reflp)
1069     then show "\<exists>x\<in>set_pmf p. R x y"
1070       unfolding measure_pmf_zero_iff by auto
1071   qed
1073   fix x y assume "x \<in> set_pmf p" "y \<in> set_pmf q" "R x y"
1074   have "{y. R x y} \<in> UNIV // ?R" "{x. R x y} = {y. R x y}"
1075     using assms \<open>R x y\<close> by (auto simp: quotient_def dest: equivp_symp equivp_transp)
1076   with eq show "measure p {x. R x y} = measure q {y. R x y}"
1077     by auto
1078 qed
1080 bnf pmf: "'a pmf" map: map_pmf sets: set_pmf bd : "natLeq" rel: rel_pmf
1081 proof -
1082   show "map_pmf id = id" by (rule map_pmf_id)
1083   show "\<And>f g. map_pmf (f \<circ> g) = map_pmf f \<circ> map_pmf g" by (rule map_pmf_compose)
1084   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"
1085     by (intro map_pmf_cong refl)
1087   show "\<And>f::'a \<Rightarrow> 'b. set_pmf \<circ> map_pmf f = op ` f \<circ> set_pmf"
1088     by (rule pmf_set_map)
1090   show "(card_of (set_pmf p), natLeq) \<in> ordLeq" for p :: "'s pmf"
1091   proof -
1092     have "(card_of (set_pmf p), card_of (UNIV :: nat set)) \<in> ordLeq"
1093       by (rule card_of_ordLeqI[where f="to_nat_on (set_pmf p)"])
1094          (auto intro: countable_set_pmf)
1095     also have "(card_of (UNIV :: nat set), natLeq) \<in> ordLeq"
1096       by (metis Field_natLeq card_of_least natLeq_Well_order)
1097     finally show ?thesis .
1098   qed
1100   show "\<And>R. rel_pmf R =
1101          (BNF_Def.Grp {x. set_pmf x \<subseteq> {(x, y). R x y}} (map_pmf fst))\<inverse>\<inverse> OO
1102          BNF_Def.Grp {x. set_pmf x \<subseteq> {(x, y). R x y}} (map_pmf snd)"
1103      by (auto simp add: fun_eq_iff BNF_Def.Grp_def OO_def rel_pmf.simps)
1105   show "rel_pmf R OO rel_pmf S \<le> rel_pmf (R OO S)"
1106     for R :: "'a \<Rightarrow> 'b \<Rightarrow> bool" and S :: "'b \<Rightarrow> 'c \<Rightarrow> bool"
1107   proof -
1108     { fix p q r
1109       assume pq: "rel_pmf R p q"
1110         and qr:"rel_pmf S q r"
1111       from pq obtain pq where pq: "\<And>x y. (x, y) \<in> set_pmf pq \<Longrightarrow> R x y"
1112         and p: "p = map_pmf fst pq" and q: "q = map_pmf snd pq" by cases auto
1113       from qr obtain qr where qr: "\<And>y z. (y, z) \<in> set_pmf qr \<Longrightarrow> S y z"
1114         and q': "q = map_pmf fst qr" and r: "r = map_pmf snd qr" by cases auto
1116       def pr \<equiv> "bind_pmf pq (\<lambda>xy. bind_pmf (cond_pmf qr {yz. fst yz = snd xy}) (\<lambda>yz. return_pmf (fst xy, snd yz)))"
1117       have pr_welldefined: "\<And>y. y \<in> q \<Longrightarrow> qr \<inter> {yz. fst yz = y} \<noteq> {}"
1118         by (force simp: q')
1120       have "rel_pmf (R OO S) p r"
1121       proof (rule rel_pmf.intros)
1122         fix x z assume "(x, z) \<in> pr"
1123         then have "\<exists>y. (x, y) \<in> pq \<and> (y, z) \<in> qr"
1124           by (auto simp: q pr_welldefined pr_def split_beta)
1125         with pq qr show "(R OO S) x z"
1126           by blast
1127       next
1128         have "map_pmf snd pr = map_pmf snd (bind_pmf q (\<lambda>y. cond_pmf qr {yz. fst yz = y}))"
1129           by (simp add: pr_def q split_beta bind_map_pmf map_pmf_def[symmetric] map_bind_pmf map_pmf_comp)
1130         then show "map_pmf snd pr = r"
1131           unfolding r q' bind_map_pmf by (subst (asm) bind_cond_pmf_cancel) (auto simp: eq_commute)
1132       qed (simp add: pr_def map_bind_pmf split_beta map_pmf_def[symmetric] p map_pmf_comp)
1133     }
1134     then show ?thesis
1136   qed
1137 qed (fact natLeq_card_order natLeq_cinfinite)+
1139 lemma map_pmf_idI: "(\<And>x. x \<in> set_pmf p \<Longrightarrow> f x = x) \<Longrightarrow> map_pmf f p = p"
1140 by(simp cong: pmf.map_cong)
1142 lemma rel_pmf_conj[simp]:
1143   "rel_pmf (\<lambda>x y. P \<and> Q x y) x y \<longleftrightarrow> P \<and> rel_pmf Q x y"
1144   "rel_pmf (\<lambda>x y. Q x y \<and> P) x y \<longleftrightarrow> P \<and> rel_pmf Q x y"
1145   using set_pmf_not_empty by (fastforce simp: pmf.in_rel subset_eq)+
1147 lemma rel_pmf_top[simp]: "rel_pmf top = top"
1148   by (auto simp: pmf.in_rel[abs_def] fun_eq_iff map_fst_pair_pmf map_snd_pair_pmf
1149            intro: exI[of _ "pair_pmf x y" for x y])
1151 lemma rel_pmf_return_pmf1: "rel_pmf R (return_pmf x) M \<longleftrightarrow> (\<forall>a\<in>M. R x a)"
1152 proof safe
1153   fix a assume "a \<in> M" "rel_pmf R (return_pmf x) M"
1154   then obtain pq where *: "\<And>a b. (a, b) \<in> set_pmf pq \<Longrightarrow> R a b"
1155     and eq: "return_pmf x = map_pmf fst pq" "M = map_pmf snd pq"
1156     by (force elim: rel_pmf.cases)
1157   moreover have "set_pmf (return_pmf x) = {x}"
1158     by simp
1159   with \<open>a \<in> M\<close> have "(x, a) \<in> pq"
1160     by (force simp: eq)
1161   with * show "R x a"
1162     by auto
1163 qed (auto intro!: rel_pmf.intros[where pq="pair_pmf (return_pmf x) M"]
1164           simp: map_fst_pair_pmf map_snd_pair_pmf)
1166 lemma rel_pmf_return_pmf2: "rel_pmf R M (return_pmf x) \<longleftrightarrow> (\<forall>a\<in>M. R a x)"
1167   by (subst pmf.rel_flip[symmetric]) (simp add: rel_pmf_return_pmf1)
1169 lemma rel_return_pmf[simp]: "rel_pmf R (return_pmf x1) (return_pmf x2) = R x1 x2"
1170   unfolding rel_pmf_return_pmf2 set_return_pmf by simp
1172 lemma rel_pmf_False[simp]: "rel_pmf (\<lambda>x y. False) x y = False"
1173   unfolding pmf.in_rel fun_eq_iff using set_pmf_not_empty by fastforce
1175 lemma rel_pmf_rel_prod:
1176   "rel_pmf (rel_prod R S) (pair_pmf A A') (pair_pmf B B') \<longleftrightarrow> rel_pmf R A B \<and> rel_pmf S A' B'"
1177 proof safe
1178   assume "rel_pmf (rel_prod R S) (pair_pmf A A') (pair_pmf B B')"
1179   then obtain pq where pq: "\<And>a b c d. ((a, c), (b, d)) \<in> set_pmf pq \<Longrightarrow> R a b \<and> S c d"
1180     and eq: "map_pmf fst pq = pair_pmf A A'" "map_pmf snd pq = pair_pmf B B'"
1181     by (force elim: rel_pmf.cases)
1182   show "rel_pmf R A B"
1183   proof (rule rel_pmf.intros)
1184     let ?f = "\<lambda>(a, b). (fst a, fst b)"
1185     have [simp]: "(\<lambda>x. fst (?f x)) = fst o fst" "(\<lambda>x. snd (?f x)) = fst o snd"
1186       by auto
1188     show "map_pmf fst (map_pmf ?f pq) = A"
1189       by (simp add: map_pmf_comp pmf.map_comp[symmetric] eq map_fst_pair_pmf)
1190     show "map_pmf snd (map_pmf ?f pq) = B"
1191       by (simp add: map_pmf_comp pmf.map_comp[symmetric] eq map_fst_pair_pmf)
1193     fix a b assume "(a, b) \<in> set_pmf (map_pmf ?f pq)"
1194     then obtain c d where "((a, c), (b, d)) \<in> set_pmf pq"
1195       by auto
1196     from pq[OF this] show "R a b" ..
1197   qed
1198   show "rel_pmf S A' B'"
1199   proof (rule rel_pmf.intros)
1200     let ?f = "\<lambda>(a, b). (snd a, snd b)"
1201     have [simp]: "(\<lambda>x. fst (?f x)) = snd o fst" "(\<lambda>x. snd (?f x)) = snd o snd"
1202       by auto
1204     show "map_pmf fst (map_pmf ?f pq) = A'"
1205       by (simp add: map_pmf_comp pmf.map_comp[symmetric] eq map_snd_pair_pmf)
1206     show "map_pmf snd (map_pmf ?f pq) = B'"
1207       by (simp add: map_pmf_comp pmf.map_comp[symmetric] eq map_snd_pair_pmf)
1209     fix c d assume "(c, d) \<in> set_pmf (map_pmf ?f pq)"
1210     then obtain a b where "((a, c), (b, d)) \<in> set_pmf pq"
1211       by auto
1212     from pq[OF this] show "S c d" ..
1213   qed
1214 next
1215   assume "rel_pmf R A B" "rel_pmf S A' B'"
1216   then obtain Rpq Spq
1217     where Rpq: "\<And>a b. (a, b) \<in> set_pmf Rpq \<Longrightarrow> R a b"
1218         "map_pmf fst Rpq = A" "map_pmf snd Rpq = B"
1219       and Spq: "\<And>a b. (a, b) \<in> set_pmf Spq \<Longrightarrow> S a b"
1220         "map_pmf fst Spq = A'" "map_pmf snd Spq = B'"
1221     by (force elim: rel_pmf.cases)
1223   let ?f = "(\<lambda>((a, c), (b, d)). ((a, b), (c, d)))"
1224   let ?pq = "map_pmf ?f (pair_pmf Rpq Spq)"
1225   have [simp]: "(\<lambda>x. fst (?f x)) = (\<lambda>(a, b). (fst a, fst b))" "(\<lambda>x. snd (?f x)) = (\<lambda>(a, b). (snd a, snd b))"
1226     by auto
1228   show "rel_pmf (rel_prod R S) (pair_pmf A A') (pair_pmf B B')"
1229     by (rule rel_pmf.intros[where pq="?pq"])
1230        (auto simp: map_snd_pair_pmf map_fst_pair_pmf map_pmf_comp Rpq Spq
1231                    map_pair)
1232 qed
1234 lemma rel_pmf_reflI:
1235   assumes "\<And>x. x \<in> set_pmf p \<Longrightarrow> P x x"
1236   shows "rel_pmf P p p"
1237   by (rule rel_pmf.intros[where pq="map_pmf (\<lambda>x. (x, x)) p"])
1238      (auto simp add: pmf.map_comp o_def assms)
1240 lemma rel_pmf_bij_betw:
1241   assumes f: "bij_betw f (set_pmf p) (set_pmf q)"
1242   and eq: "\<And>x. x \<in> set_pmf p \<Longrightarrow> pmf p x = pmf q (f x)"
1243   shows "rel_pmf (\<lambda>x y. f x = y) p q"
1244 proof(rule rel_pmf.intros)
1245   let ?pq = "map_pmf (\<lambda>x. (x, f x)) p"
1246   show "map_pmf fst ?pq = p" by(simp add: pmf.map_comp o_def)
1248   have "map_pmf f p = q"
1249   proof(rule pmf_eqI)
1250     fix i
1251     show "pmf (map_pmf f p) i = pmf q i"
1252     proof(cases "i \<in> set_pmf q")
1253       case True
1254       with f obtain j where "i = f j" "j \<in> set_pmf p"
1255         by(auto simp add: bij_betw_def image_iff)
1256       thus ?thesis using f by(simp add: bij_betw_def pmf_map_inj eq)
1257     next
1258       case False thus ?thesis
1259         by(subst pmf_map_outside)(auto simp add: set_pmf_iff eq[symmetric])
1260     qed
1261   qed
1262   then show "map_pmf snd ?pq = q" by(simp add: pmf.map_comp o_def)
1263 qed auto
1265 context
1266 begin
1268 interpretation pmf_as_measure .
1270 definition "join_pmf M = bind_pmf M (\<lambda>x. x)"
1272 lemma bind_eq_join_pmf: "bind_pmf M f = join_pmf (map_pmf f M)"
1273   unfolding join_pmf_def bind_map_pmf ..
1275 lemma join_eq_bind_pmf: "join_pmf M = bind_pmf M id"
1276   by (simp add: join_pmf_def id_def)
1278 lemma pmf_join: "pmf (join_pmf N) i = (\<integral>M. pmf M i \<partial>measure_pmf N)"
1279   unfolding join_pmf_def pmf_bind ..
1281 lemma ereal_pmf_join: "ereal (pmf (join_pmf N) i) = (\<integral>\<^sup>+M. pmf M i \<partial>measure_pmf N)"
1282   unfolding join_pmf_def ereal_pmf_bind ..
1284 lemma set_pmf_join_pmf[simp]: "set_pmf (join_pmf f) = (\<Union>p\<in>set_pmf f. set_pmf p)"
1287 lemma join_return_pmf: "join_pmf (return_pmf M) = M"
1288   by (simp add: integral_return pmf_eq_iff pmf_join return_pmf.rep_eq)
1290 lemma map_join_pmf: "map_pmf f (join_pmf AA) = join_pmf (map_pmf (map_pmf f) AA)"
1291   by (simp add: join_pmf_def map_pmf_def bind_assoc_pmf bind_return_pmf)
1293 lemma join_map_return_pmf: "join_pmf (map_pmf return_pmf A) = A"
1294   by (simp add: join_pmf_def map_pmf_def bind_assoc_pmf bind_return_pmf bind_return_pmf')
1296 end
1298 lemma rel_pmf_joinI:
1299   assumes "rel_pmf (rel_pmf P) p q"
1300   shows "rel_pmf P (join_pmf p) (join_pmf q)"
1301 proof -
1302   from assms obtain pq where p: "p = map_pmf fst pq"
1303     and q: "q = map_pmf snd pq"
1304     and P: "\<And>x y. (x, y) \<in> set_pmf pq \<Longrightarrow> rel_pmf P x y"
1305     by cases auto
1306   from P obtain PQ
1307     where PQ: "\<And>x y a b. \<lbrakk> (x, y) \<in> set_pmf pq; (a, b) \<in> set_pmf (PQ x y) \<rbrakk> \<Longrightarrow> P a b"
1308     and x: "\<And>x y. (x, y) \<in> set_pmf pq \<Longrightarrow> map_pmf fst (PQ x y) = x"
1309     and y: "\<And>x y. (x, y) \<in> set_pmf pq \<Longrightarrow> map_pmf snd (PQ x y) = y"
1310     by(metis rel_pmf.simps)
1312   let ?r = "bind_pmf pq (\<lambda>(x, y). PQ x y)"
1313   have "\<And>a b. (a, b) \<in> set_pmf ?r \<Longrightarrow> P a b" by (auto intro: PQ)
1314   moreover have "map_pmf fst ?r = join_pmf p" "map_pmf snd ?r = join_pmf q"
1315     by (simp_all add: p q x y join_pmf_def map_bind_pmf bind_map_pmf split_def cong: bind_pmf_cong)
1316   ultimately show ?thesis ..
1317 qed
1319 lemma rel_pmf_bindI:
1320   assumes pq: "rel_pmf R p q"
1321   and fg: "\<And>x y. R x y \<Longrightarrow> rel_pmf P (f x) (g y)"
1322   shows "rel_pmf P (bind_pmf p f) (bind_pmf q g)"
1323   unfolding bind_eq_join_pmf
1324   by (rule rel_pmf_joinI)
1325      (auto simp add: pmf.rel_map intro: pmf.rel_mono[THEN le_funD, THEN le_funD, THEN le_boolD, THEN mp, OF _ pq] fg)
1327 text \<open>
1328   Proof that @{const rel_pmf} preserves orders.
1329   Antisymmetry proof follows Thm. 1 in N. Saheb-Djahromi, Cpo's of measures for nondeterminism,
1330   Theoretical Computer Science 12(1):19--37, 1980,
1331   @{url "http://dx.doi.org/10.1016/0304-3975(80)90003-1"}
1332 \<close>
1334 lemma
1335   assumes *: "rel_pmf R p q"
1336   and refl: "reflp R" and trans: "transp R"
1337   shows measure_Ici: "measure p {y. R x y} \<le> measure q {y. R x y}" (is ?thesis1)
1338   and measure_Ioi: "measure p {y. R x y \<and> \<not> R y x} \<le> measure q {y. R x y \<and> \<not> R y x}" (is ?thesis2)
1339 proof -
1340   from * obtain pq
1341     where pq: "\<And>x y. (x, y) \<in> set_pmf pq \<Longrightarrow> R x y"
1342     and p: "p = map_pmf fst pq"
1343     and q: "q = map_pmf snd pq"
1344     by cases auto
1345   show ?thesis1 ?thesis2 unfolding p q map_pmf_rep_eq using refl trans
1346     by(auto 4 3 simp add: measure_distr reflpD AE_measure_pmf_iff intro!: measure_pmf.finite_measure_mono_AE dest!: pq elim: transpE)
1347 qed
1349 lemma rel_pmf_inf:
1350   fixes p q :: "'a pmf"
1351   assumes 1: "rel_pmf R p q"
1352   assumes 2: "rel_pmf R q p"
1353   and refl: "reflp R" and trans: "transp R"
1354   shows "rel_pmf (inf R R\<inverse>\<inverse>) p q"
1355 proof (subst rel_pmf_iff_equivp, safe)
1356   show "equivp (inf R R\<inverse>\<inverse>)"
1357     using trans refl by (auto simp: equivp_reflp_symp_transp intro: sympI transpI reflpI dest: transpD reflpD)
1359   fix C assume "C \<in> UNIV // {(x, y). inf R R\<inverse>\<inverse> x y}"
1360   then obtain x where C: "C = {y. R x y \<and> R y x}"
1361     by (auto elim: quotientE)
1363   let ?R = "\<lambda>x y. R x y \<and> R y x"
1364   let ?\<mu>R = "\<lambda>y. measure q {x. ?R x y}"
1365   have "measure p {y. ?R x y} = measure p ({y. R x y} - {y. R x y \<and> \<not> R y x})"
1366     by(auto intro!: arg_cong[where f="measure p"])
1367   also have "\<dots> = measure p {y. R x y} - measure p {y. R x y \<and> \<not> R y x}"
1368     by (rule measure_pmf.finite_measure_Diff) auto
1369   also have "measure p {y. R x y \<and> \<not> R y x} = measure q {y. R x y \<and> \<not> R y x}"
1370     using 1 2 refl trans by(auto intro!: Orderings.antisym measure_Ioi)
1371   also have "measure p {y. R x y} = measure q {y. R x y}"
1372     using 1 2 refl trans by(auto intro!: Orderings.antisym measure_Ici)
1373   also have "measure q {y. R x y} - measure q {y. R x y \<and> \<not> R y x} =
1374     measure q ({y. R x y} - {y. R x y \<and> \<not> R y x})"
1375     by(rule measure_pmf.finite_measure_Diff[symmetric]) auto
1376   also have "\<dots> = ?\<mu>R x"
1377     by(auto intro!: arg_cong[where f="measure q"])
1378   finally show "measure p C = measure q C"
1379     by (simp add: C conj_commute)
1380 qed
1382 lemma rel_pmf_antisym:
1383   fixes p q :: "'a pmf"
1384   assumes 1: "rel_pmf R p q"
1385   assumes 2: "rel_pmf R q p"
1386   and refl: "reflp R" and trans: "transp R" and antisym: "antisymP R"
1387   shows "p = q"
1388 proof -
1389   from 1 2 refl trans have "rel_pmf (inf R R\<inverse>\<inverse>) p q" by(rule rel_pmf_inf)
1390   also have "inf R R\<inverse>\<inverse> = op ="
1391     using refl antisym by (auto intro!: ext simp add: reflpD dest: antisymD)
1392   finally show ?thesis unfolding pmf.rel_eq .
1393 qed
1395 lemma reflp_rel_pmf: "reflp R \<Longrightarrow> reflp (rel_pmf R)"
1396 by(blast intro: reflpI rel_pmf_reflI reflpD)
1398 lemma antisymP_rel_pmf:
1399   "\<lbrakk> reflp R; transp R; antisymP R \<rbrakk>
1400   \<Longrightarrow> antisymP (rel_pmf R)"
1401 by(rule antisymI)(blast intro: rel_pmf_antisym)
1403 lemma transp_rel_pmf:
1404   assumes "transp R"
1405   shows "transp (rel_pmf R)"
1406 proof (rule transpI)
1407   fix x y z
1408   assume "rel_pmf R x y" and "rel_pmf R y z"
1409   hence "rel_pmf (R OO R) x z" by (simp add: pmf.rel_compp relcompp.relcompI)
1410   thus "rel_pmf R x z"
1411     using assms by (metis (no_types) pmf.rel_mono rev_predicate2D transp_relcompp_less_eq)
1412 qed
1414 subsection \<open> Distributions \<close>
1416 context
1417 begin
1419 interpretation pmf_as_function .
1421 subsubsection \<open> Bernoulli Distribution \<close>
1423 lift_definition bernoulli_pmf :: "real \<Rightarrow> bool pmf" is
1424   "\<lambda>p b. ((\<lambda>p. if b then p else 1 - p) \<circ> min 1 \<circ> max 0) p"
1425   by (auto simp: nn_integral_count_space_finite[where A="{False, True}"] UNIV_bool
1426            split: split_max split_min)
1428 lemma pmf_bernoulli_True[simp]: "0 \<le> p \<Longrightarrow> p \<le> 1 \<Longrightarrow> pmf (bernoulli_pmf p) True = p"
1429   by transfer simp
1431 lemma pmf_bernoulli_False[simp]: "0 \<le> p \<Longrightarrow> p \<le> 1 \<Longrightarrow> pmf (bernoulli_pmf p) False = 1 - p"
1432   by transfer simp
1434 lemma set_pmf_bernoulli: "0 < p \<Longrightarrow> p < 1 \<Longrightarrow> set_pmf (bernoulli_pmf p) = UNIV"
1435   by (auto simp add: set_pmf_iff UNIV_bool)
1437 lemma nn_integral_bernoulli_pmf[simp]:
1438   assumes [simp]: "0 \<le> p" "p \<le> 1" "\<And>x. 0 \<le> f x"
1439   shows "(\<integral>\<^sup>+x. f x \<partial>bernoulli_pmf p) = f True * p + f False * (1 - p)"
1440   by (subst nn_integral_measure_pmf_support[of UNIV])
1441      (auto simp: UNIV_bool field_simps)
1443 lemma integral_bernoulli_pmf[simp]:
1444   assumes [simp]: "0 \<le> p" "p \<le> 1"
1445   shows "(\<integral>x. f x \<partial>bernoulli_pmf p) = f True * p + f False * (1 - p)"
1446   by (subst integral_measure_pmf[of UNIV]) (auto simp: UNIV_bool)
1448 lemma pmf_bernoulli_half [simp]: "pmf (bernoulli_pmf (1 / 2)) x = 1 / 2"
1449 by(cases x) simp_all
1451 lemma measure_pmf_bernoulli_half: "measure_pmf (bernoulli_pmf (1 / 2)) = uniform_count_measure UNIV"
1452 by(rule measure_eqI)(simp_all add: nn_integral_pmf[symmetric] emeasure_uniform_count_measure nn_integral_count_space_finite sets_uniform_count_measure)
1454 subsubsection \<open> Geometric Distribution \<close>
1456 context
1457   fixes p :: real assumes p[arith]: "0 < p" "p \<le> 1"
1458 begin
1460 lift_definition geometric_pmf :: "nat pmf" is "\<lambda>n. (1 - p)^n * p"
1461 proof
1462   have "(\<Sum>i. ereal (p * (1 - p) ^ i)) = ereal (p * (1 / (1 - (1 - p))))"
1463     by (intro sums_suminf_ereal sums_mult geometric_sums) auto
1464   then show "(\<integral>\<^sup>+ x. ereal ((1 - p)^x * p) \<partial>count_space UNIV) = 1"
1465     by (simp add: nn_integral_count_space_nat field_simps)
1466 qed simp
1468 lemma pmf_geometric[simp]: "pmf geometric_pmf n = (1 - p)^n * p"
1469   by transfer rule
1471 end
1473 lemma set_pmf_geometric: "0 < p \<Longrightarrow> p < 1 \<Longrightarrow> set_pmf (geometric_pmf p) = UNIV"
1474   by (auto simp: set_pmf_iff)
1476 subsubsection \<open> Uniform Multiset Distribution \<close>
1478 context
1479   fixes M :: "'a multiset" assumes M_not_empty: "M \<noteq> {#}"
1480 begin
1482 lift_definition pmf_of_multiset :: "'a pmf" is "\<lambda>x. count M x / size M"
1483 proof
1484   show "(\<integral>\<^sup>+ x. ereal (real (count M x) / real (size M)) \<partial>count_space UNIV) = 1"
1485     using M_not_empty
1486     by (simp add: zero_less_divide_iff nn_integral_count_space nonempty_has_size
1487                   setsum_divide_distrib[symmetric])
1488        (auto simp: size_multiset_overloaded_eq intro!: setsum.cong)
1489 qed simp
1491 lemma pmf_of_multiset[simp]: "pmf pmf_of_multiset x = count M x / size M"
1492   by transfer rule
1494 lemma set_pmf_of_multiset[simp]: "set_pmf pmf_of_multiset = set_mset M"
1495   by (auto simp: set_pmf_iff)
1497 end
1499 subsubsection \<open> Uniform Distribution \<close>
1501 context
1502   fixes S :: "'a set" assumes S_not_empty: "S \<noteq> {}" and S_finite: "finite S"
1503 begin
1505 lift_definition pmf_of_set :: "'a pmf" is "\<lambda>x. indicator S x / card S"
1506 proof
1507   show "(\<integral>\<^sup>+ x. ereal (indicator S x / real (card S)) \<partial>count_space UNIV) = 1"
1508     using S_not_empty S_finite by (subst nn_integral_count_space'[of S]) auto
1509 qed simp
1511 lemma pmf_of_set[simp]: "pmf pmf_of_set x = indicator S x / card S"
1512   by transfer rule
1514 lemma set_pmf_of_set[simp]: "set_pmf pmf_of_set = S"
1515   using S_finite S_not_empty by (auto simp: set_pmf_iff)
1517 lemma emeasure_pmf_of_set_space[simp]: "emeasure pmf_of_set S = 1"
1518   by (rule measure_pmf.emeasure_eq_1_AE) (auto simp: AE_measure_pmf_iff)
1520 lemma nn_integral_pmf_of_set':
1521   "(\<And>x. x \<in> S \<Longrightarrow> f x \<ge> 0) \<Longrightarrow> nn_integral (measure_pmf pmf_of_set) f = setsum f S / card S"
1522 apply(subst nn_integral_measure_pmf_finite, simp_all add: S_finite)
1524 apply(subst ereal_divide', simp add: S_not_empty S_finite)
1526 done
1528 lemma nn_integral_pmf_of_set:
1529   "nn_integral (measure_pmf pmf_of_set) f = setsum (max 0 \<circ> f) S / card S"
1530 apply(subst nn_integral_max_0[symmetric])
1531 apply(subst nn_integral_pmf_of_set')
1532 apply simp_all
1533 done
1535 lemma integral_pmf_of_set:
1536   "integral\<^sup>L (measure_pmf pmf_of_set) f = setsum f S / card S"
1537 apply(simp add: real_lebesgue_integral_def integrable_measure_pmf_finite nn_integral_pmf_of_set S_finite)
1538 apply(subst real_of_ereal_minus')
1539  apply(simp add: ereal_max_0 S_finite del: ereal_max)
1540 apply(simp add: ereal_max_0 S_finite S_not_empty del: ereal_max)
1541 apply(simp add: field_simps S_finite S_not_empty)
1542 apply(subst setsum.distrib[symmetric])
1543 apply(rule setsum.cong; simp_all)
1544 done
1546 lemma emeasure_pmf_of_set:
1547   "emeasure (measure_pmf pmf_of_set) A = card (S \<inter> A) / card S"
1548 apply(subst nn_integral_indicator[symmetric], simp)
1549 apply(subst nn_integral_pmf_of_set)
1550 apply(simp add: o_def max_def ereal_indicator[symmetric] S_not_empty S_finite real_of_nat_indicator[symmetric] of_nat_setsum[symmetric] setsum_indicator_eq_card del: of_nat_setsum)
1551 done
1553 end
1555 lemma pmf_of_set_singleton: "pmf_of_set {x} = return_pmf x"
1558 lemma map_pmf_of_set_inj:
1559   assumes f: "inj_on f A"
1560   and [simp]: "A \<noteq> {}" "finite A"
1561   shows "map_pmf f (pmf_of_set A) = pmf_of_set (f ` A)" (is "?lhs = ?rhs")
1562 proof(rule pmf_eqI)
1563   fix i
1564   show "pmf ?lhs i = pmf ?rhs i"
1565   proof(cases "i \<in> f ` A")
1566     case True
1567     then obtain i' where "i = f i'" "i' \<in> A" by auto
1568     thus ?thesis using f by(simp add: card_image pmf_map_inj)
1569   next
1570     case False
1571     hence "pmf ?lhs i = 0" by(simp add: pmf_eq_0_set_pmf set_map_pmf)
1572     moreover have "pmf ?rhs i = 0" using False by simp
1573     ultimately show ?thesis by simp
1574   qed
1575 qed
1577 lemma bernoulli_pmf_half_conv_pmf_of_set: "bernoulli_pmf (1 / 2) = pmf_of_set UNIV"
1578 by(rule pmf_eqI) simp_all
1582 lemma measure_pmf_of_set:
1583   assumes "S \<noteq> {}" "finite S"
1584   shows "measure (measure_pmf (pmf_of_set S)) A = card (S \<inter> A) / card S"
1585 using emeasure_pmf_of_set[OF assms, of A]
1586 unfolding measure_pmf.emeasure_eq_measure by simp
1588 subsubsection \<open> Poisson Distribution \<close>
1590 context
1591   fixes rate :: real assumes rate_pos: "0 < rate"
1592 begin
1594 lift_definition poisson_pmf :: "nat pmf" is "\<lambda>k. rate ^ k / fact k * exp (-rate)"
1595 proof  (* by Manuel Eberl *)
1596   have summable: "summable (\<lambda>x::nat. rate ^ x / fact x)" using summable_exp
1597     by (simp add: field_simps divide_inverse [symmetric])
1598   have "(\<integral>\<^sup>+(x::nat). rate ^ x / fact x * exp (-rate) \<partial>count_space UNIV) =
1599           exp (-rate) * (\<integral>\<^sup>+(x::nat). rate ^ x / fact x \<partial>count_space UNIV)"
1600     by (simp add: field_simps nn_integral_cmult[symmetric])
1601   also from rate_pos have "(\<integral>\<^sup>+(x::nat). rate ^ x / fact x \<partial>count_space UNIV) = (\<Sum>x. rate ^ x / fact x)"
1602     by (simp_all add: nn_integral_count_space_nat suminf_ereal summable suminf_ereal_finite)
1603   also have "... = exp rate" unfolding exp_def
1604     by (simp add: field_simps divide_inverse [symmetric])
1605   also have "ereal (exp (-rate)) * ereal (exp rate) = 1"
1607   finally show "(\<integral>\<^sup>+ x. ereal (rate ^ x / (fact x) * exp (- rate)) \<partial>count_space UNIV) = 1" .
1608 qed (simp add: rate_pos[THEN less_imp_le])
1610 lemma pmf_poisson[simp]: "pmf poisson_pmf k = rate ^ k / fact k * exp (-rate)"
1611   by transfer rule
1613 lemma set_pmf_poisson[simp]: "set_pmf poisson_pmf = UNIV"
1614   using rate_pos by (auto simp: set_pmf_iff)
1616 end
1618 subsubsection \<open> Binomial Distribution \<close>
1620 context
1621   fixes n :: nat and p :: real assumes p_nonneg: "0 \<le> p" and p_le_1: "p \<le> 1"
1622 begin
1624 lift_definition binomial_pmf :: "nat pmf" is "\<lambda>k. (n choose k) * p^k * (1 - p)^(n - k)"
1625 proof
1626   have "(\<integral>\<^sup>+k. ereal (real (n choose k) * p ^ k * (1 - p) ^ (n - k)) \<partial>count_space UNIV) =
1627     ereal (\<Sum>k\<le>n. real (n choose k) * p ^ k * (1 - p) ^ (n - k))"
1628     using p_le_1 p_nonneg by (subst nn_integral_count_space') auto
1629   also have "(\<Sum>k\<le>n. real (n choose k) * p ^ k * (1 - p) ^ (n - k)) = (p + (1 - p)) ^ n"
1630     by (subst binomial_ring) (simp add: atLeast0AtMost)
1631   finally show "(\<integral>\<^sup>+ x. ereal (real (n choose x) * p ^ x * (1 - p) ^ (n - x)) \<partial>count_space UNIV) = 1"
1632     by simp
1633 qed (insert p_nonneg p_le_1, simp)
1635 lemma pmf_binomial[simp]: "pmf binomial_pmf k = (n choose k) * p^k * (1 - p)^(n - k)"
1636   by transfer rule
1638 lemma set_pmf_binomial_eq: "set_pmf binomial_pmf = (if p = 0 then {0} else if p = 1 then {n} else {.. n})"
1639   using p_nonneg p_le_1 unfolding set_eq_iff set_pmf_iff pmf_binomial by (auto simp: set_pmf_iff)
1641 end
1643 end
1645 lemma set_pmf_binomial_0[simp]: "set_pmf (binomial_pmf n 0) = {0}"