195 by (subst emeasure_eq_setsum_singleton) (auto simp: emeasure_pmf_single pmf_nonneg) |
208 by (subst emeasure_eq_setsum_singleton) (auto simp: emeasure_pmf_single pmf_nonneg) |
196 |
209 |
197 lemma measure_measure_pmf_finite: "finite S \<Longrightarrow> measure (measure_pmf M) S = setsum (pmf M) S" |
210 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] |
211 using emeasure_measure_pmf_finite[of S M] |
199 by (simp add: measure_pmf.emeasure_eq_measure measure_nonneg setsum_nonneg pmf_nonneg) |
212 by (simp add: measure_pmf.emeasure_eq_measure measure_nonneg setsum_nonneg pmf_nonneg) |
|
213 |
|
214 lemma setsum_pmf_eq_1: |
|
215 assumes "finite A" "set_pmf p \<subseteq> A" |
|
216 shows "(\<Sum>x\<in>A. pmf p x) = 1" |
|
217 proof - |
|
218 have "(\<Sum>x\<in>A. pmf p x) = measure_pmf.prob p A" |
|
219 by (simp add: measure_measure_pmf_finite assms) |
|
220 also from assms have "\<dots> = 1" |
|
221 by (subst measure_pmf.prob_eq_1) (auto simp: AE_measure_pmf_iff) |
|
222 finally show ?thesis . |
|
223 qed |
200 |
224 |
201 lemma nn_integral_measure_pmf_support: |
225 lemma nn_integral_measure_pmf_support: |
202 fixes f :: "'a \<Rightarrow> ennreal" |
226 fixes f :: "'a \<Rightarrow> ennreal" |
203 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" |
227 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" |
204 shows "(\<integral>\<^sup>+x. f x \<partial>measure_pmf M) = (\<Sum>x\<in>A. f x * pmf M x)" |
228 shows "(\<integral>\<^sup>+x. f x \<partial>measure_pmf M) = (\<Sum>x\<in>A. f x * pmf M x)" |
729 lemma pmf_eqI: "(\<And>i. pmf M i = pmf N i) \<Longrightarrow> M = N" |
764 lemma pmf_eqI: "(\<And>i. pmf M i = pmf N i) \<Longrightarrow> M = N" |
730 by transfer auto |
765 by transfer auto |
731 |
766 |
732 lemma pmf_eq_iff: "M = N \<longleftrightarrow> (\<forall>i. pmf M i = pmf N i)" |
767 lemma pmf_eq_iff: "M = N \<longleftrightarrow> (\<forall>i. pmf M i = pmf N i)" |
733 by (auto intro: pmf_eqI) |
768 by (auto intro: pmf_eqI) |
|
769 |
|
770 lemma pmf_neq_exists_less: |
|
771 assumes "M \<noteq> N" |
|
772 shows "\<exists>x. pmf M x < pmf N x" |
|
773 proof (rule ccontr) |
|
774 assume "\<not>(\<exists>x. pmf M x < pmf N x)" |
|
775 hence ge: "pmf M x \<ge> pmf N x" for x by (auto simp: not_less) |
|
776 from assms obtain x where "pmf M x \<noteq> pmf N x" by (auto simp: pmf_eq_iff) |
|
777 with ge[of x] have gt: "pmf M x > pmf N x" by simp |
|
778 have "1 = measure (measure_pmf M) UNIV" by simp |
|
779 also have "\<dots> = measure (measure_pmf N) {x} + measure (measure_pmf N) (UNIV - {x})" |
|
780 by (subst measure_pmf.finite_measure_Union [symmetric]) simp_all |
|
781 also from gt have "measure (measure_pmf N) {x} < measure (measure_pmf M) {x}" |
|
782 by (simp add: measure_pmf_single) |
|
783 also have "measure (measure_pmf N) (UNIV - {x}) \<le> measure (measure_pmf M) (UNIV - {x})" |
|
784 by (subst (1 2) integral_pmf [symmetric]) |
|
785 (intro integral_mono integrable_pmf, simp_all add: ge) |
|
786 also have "measure (measure_pmf M) {x} + \<dots> = 1" |
|
787 by (subst measure_pmf.finite_measure_Union [symmetric]) simp_all |
|
788 finally show False by simp_all |
|
789 qed |
734 |
790 |
735 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))" |
791 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))" |
736 unfolding pmf_eq_iff pmf_bind |
792 unfolding pmf_eq_iff pmf_bind |
737 proof |
793 proof |
738 fix i |
794 fix i |
1566 using emeasure_pmf_of_set[of A] |
1625 using emeasure_pmf_of_set[of A] |
1567 by (simp add: measure_nonneg measure_pmf.emeasure_eq_measure) |
1626 by (simp add: measure_nonneg measure_pmf.emeasure_eq_measure) |
1568 |
1627 |
1569 end |
1628 end |
1570 |
1629 |
|
1630 lemma map_pmf_of_set: |
|
1631 assumes "finite A" "A \<noteq> {}" |
|
1632 shows "map_pmf f (pmf_of_set A) = pmf_of_multiset (image_mset f (mset_set A))" |
|
1633 (is "?lhs = ?rhs") |
|
1634 proof (intro pmf_eqI) |
|
1635 fix x |
|
1636 from assms have "ennreal (pmf ?lhs x) = ennreal (pmf ?rhs x)" |
|
1637 by (subst ennreal_pmf_map) |
|
1638 (simp_all add: emeasure_pmf_of_set mset_set_empty_iff count_image_mset Int_commute) |
|
1639 thus "pmf ?lhs x = pmf ?rhs x" by simp |
|
1640 qed |
|
1641 |
|
1642 lemma pmf_bind_pmf_of_set: |
|
1643 assumes "A \<noteq> {}" "finite A" |
|
1644 shows "pmf (bind_pmf (pmf_of_set A) f) x = |
|
1645 (\<Sum>xa\<in>A. pmf (f xa) x) / real_of_nat (card A)" (is "?lhs = ?rhs") |
|
1646 proof - |
|
1647 from assms have "card A > 0" by auto |
|
1648 with assms have "ennreal ?lhs = ennreal ?rhs" |
|
1649 by (subst ennreal_pmf_bind) |
|
1650 (simp_all add: nn_integral_pmf_of_set max_def pmf_nonneg divide_ennreal [symmetric] |
|
1651 setsum_nonneg ennreal_of_nat_eq_real_of_nat) |
|
1652 thus ?thesis by (subst (asm) ennreal_inj) (auto intro!: setsum_nonneg divide_nonneg_nonneg) |
|
1653 qed |
|
1654 |
1571 lemma pmf_of_set_singleton: "pmf_of_set {x} = return_pmf x" |
1655 lemma pmf_of_set_singleton: "pmf_of_set {x} = return_pmf x" |
1572 by(rule pmf_eqI)(simp add: indicator_def) |
1656 by(rule pmf_eqI)(simp add: indicator_def) |
1573 |
1657 |
1574 lemma map_pmf_of_set_inj: |
1658 lemma map_pmf_of_set_inj: |
1575 assumes f: "inj_on f A" |
1659 assumes f: "inj_on f A" |
1588 moreover have "pmf ?rhs i = 0" using False by simp |
1672 moreover have "pmf ?rhs i = 0" using False by simp |
1589 ultimately show ?thesis by simp |
1673 ultimately show ?thesis by simp |
1590 qed |
1674 qed |
1591 qed |
1675 qed |
1592 |
1676 |
|
1677 text \<open> |
|
1678 Choosing an element uniformly at random from the union of a disjoint family |
|
1679 of finite non-empty sets with the same size is the same as first choosing a set |
|
1680 from the family uniformly at random and then choosing an element from the chosen set |
|
1681 uniformly at random. |
|
1682 \<close> |
|
1683 lemma pmf_of_set_UN: |
|
1684 assumes "finite (UNION A f)" "A \<noteq> {}" "\<And>x. x \<in> A \<Longrightarrow> f x \<noteq> {}" |
|
1685 "\<And>x. x \<in> A \<Longrightarrow> card (f x) = n" "disjoint_family_on f A" |
|
1686 shows "pmf_of_set (UNION A f) = do {x \<leftarrow> pmf_of_set A; pmf_of_set (f x)}" |
|
1687 (is "?lhs = ?rhs") |
|
1688 proof (intro pmf_eqI) |
|
1689 fix x |
|
1690 from assms have [simp]: "finite A" |
|
1691 using infinite_disjoint_family_imp_infinite_UNION[of A f] by blast |
|
1692 from assms have "ereal (pmf (pmf_of_set (UNION A f)) x) = |
|
1693 ereal (indicator (\<Union>x\<in>A. f x) x / real (card (\<Union>x\<in>A. f x)))" |
|
1694 by (subst pmf_of_set) auto |
|
1695 also from assms have "card (\<Union>x\<in>A. f x) = card A * n" |
|
1696 by (subst card_UN_disjoint) (auto simp: disjoint_family_on_def) |
|
1697 also from assms |
|
1698 have "indicator (\<Union>x\<in>A. f x) x / real \<dots> = |
|
1699 indicator (\<Union>x\<in>A. f x) x / (n * real (card A))" |
|
1700 by (simp add: setsum_divide_distrib [symmetric] mult_ac) |
|
1701 also from assms have "indicator (\<Union>x\<in>A. f x) x = (\<Sum>y\<in>A. indicator (f y) x)" |
|
1702 by (intro indicator_UN_disjoint) simp_all |
|
1703 also from assms have "ereal ((\<Sum>y\<in>A. indicator (f y) x) / (real n * real (card A))) = |
|
1704 ereal (pmf ?rhs x)" |
|
1705 by (subst pmf_bind_pmf_of_set) (simp_all add: setsum_divide_distrib) |
|
1706 finally show "pmf ?lhs x = pmf ?rhs x" by simp |
|
1707 qed |
|
1708 |
1593 lemma bernoulli_pmf_half_conv_pmf_of_set: "bernoulli_pmf (1 / 2) = pmf_of_set UNIV" |
1709 lemma bernoulli_pmf_half_conv_pmf_of_set: "bernoulli_pmf (1 / 2) = pmf_of_set UNIV" |
1594 by (rule pmf_eqI) simp_all |
1710 by (rule pmf_eqI) simp_all |
1595 |
1711 |
1596 subsubsection \<open> Poisson Distribution \<close> |
1712 subsubsection \<open> Poisson Distribution \<close> |
1597 |
1713 |
1668 lemma return_pmf_parametric [transfer_rule]: "(A ===> rel_pmf A) return_pmf return_pmf" |
1784 lemma return_pmf_parametric [transfer_rule]: "(A ===> rel_pmf A) return_pmf return_pmf" |
1669 by(rule rel_funI) simp |
1785 by(rule rel_funI) simp |
1670 |
1786 |
1671 end |
1787 end |
1672 |
1788 |
|
1789 |
|
1790 subsection \<open>PMFs from assiciation lists\<close> |
|
1791 |
|
1792 definition pmf_of_list ::" ('a \<times> real) list \<Rightarrow> 'a pmf" where |
|
1793 "pmf_of_list xs = embed_pmf (\<lambda>x. listsum (map snd (filter (\<lambda>z. fst z = x) xs)))" |
|
1794 |
|
1795 definition pmf_of_list_wf where |
|
1796 "pmf_of_list_wf xs \<longleftrightarrow> (\<forall>x\<in>set (map snd xs) . x \<ge> 0) \<and> listsum (map snd xs) = 1" |
|
1797 |
|
1798 lemma pmf_of_list_wfI: |
|
1799 "(\<And>x. x \<in> set (map snd xs) \<Longrightarrow> x \<ge> 0) \<Longrightarrow> listsum (map snd xs) = 1 \<Longrightarrow> pmf_of_list_wf xs" |
|
1800 unfolding pmf_of_list_wf_def by simp |
|
1801 |
|
1802 context |
|
1803 begin |
|
1804 |
|
1805 private lemma pmf_of_list_aux: |
|
1806 assumes "\<And>x. x \<in> set (map snd xs) \<Longrightarrow> x \<ge> 0" |
|
1807 assumes "listsum (map snd xs) = 1" |
|
1808 shows "(\<integral>\<^sup>+ x. ennreal (listsum (map snd [z\<leftarrow>xs . fst z = x])) \<partial>count_space UNIV) = 1" |
|
1809 proof - |
|
1810 have "(\<integral>\<^sup>+ x. ennreal (listsum (map snd (filter (\<lambda>z. fst z = x) xs))) \<partial>count_space UNIV) = |
|
1811 (\<integral>\<^sup>+ x. ennreal (listsum (map (\<lambda>(x',p). indicator {x'} x * p) xs)) \<partial>count_space UNIV)" |
|
1812 by (intro nn_integral_cong ennreal_cong, subst listsum_map_filter) (auto intro: listsum_cong) |
|
1813 also have "\<dots> = (\<Sum>(x',p)\<leftarrow>xs. (\<integral>\<^sup>+ x. ennreal (indicator {x'} x * p) \<partial>count_space UNIV))" |
|
1814 using assms(1) |
|
1815 proof (induction xs) |
|
1816 case (Cons x xs) |
|
1817 from Cons.prems have "snd x \<ge> 0" by simp |
|
1818 moreover have "b \<ge> 0" if "(a,b) \<in> set xs" for a b |
|
1819 using Cons.prems[of b] that by force |
|
1820 ultimately have "(\<integral>\<^sup>+ y. ennreal (\<Sum>(x', p)\<leftarrow>x # xs. indicator {x'} y * p) \<partial>count_space UNIV) = |
|
1821 (\<integral>\<^sup>+ y. ennreal (indicator {fst x} y * snd x) + |
|
1822 ennreal (\<Sum>(x', p)\<leftarrow>xs. indicator {x'} y * p) \<partial>count_space UNIV)" |
|
1823 by (intro nn_integral_cong, subst ennreal_plus [symmetric]) |
|
1824 (auto simp: case_prod_unfold indicator_def intro!: listsum_nonneg) |
|
1825 also have "\<dots> = (\<integral>\<^sup>+ y. ennreal (indicator {fst x} y * snd x) \<partial>count_space UNIV) + |
|
1826 (\<integral>\<^sup>+ y. ennreal (\<Sum>(x', p)\<leftarrow>xs. indicator {x'} y * p) \<partial>count_space UNIV)" |
|
1827 by (intro nn_integral_add) |
|
1828 (force intro!: listsum_nonneg AE_I2 intro: Cons simp: indicator_def)+ |
|
1829 also have "(\<integral>\<^sup>+ y. ennreal (\<Sum>(x', p)\<leftarrow>xs. indicator {x'} y * p) \<partial>count_space UNIV) = |
|
1830 (\<Sum>(x', p)\<leftarrow>xs. (\<integral>\<^sup>+ y. ennreal (indicator {x'} y * p) \<partial>count_space UNIV))" |
|
1831 using Cons(1) by (intro Cons) simp_all |
|
1832 finally show ?case by (simp add: case_prod_unfold) |
|
1833 qed simp |
|
1834 also have "\<dots> = (\<Sum>(x',p)\<leftarrow>xs. ennreal p * (\<integral>\<^sup>+ x. indicator {x'} x \<partial>count_space UNIV))" |
|
1835 using assms(1) |
|
1836 by (intro listsum_cong, simp only: case_prod_unfold, subst nn_integral_cmult [symmetric]) |
|
1837 (auto intro!: assms(1) simp: max_def times_ereal.simps [symmetric] mult_ac ereal_indicator |
|
1838 simp del: times_ereal.simps)+ |
|
1839 also from assms have "\<dots> = listsum (map snd xs)" by (simp add: case_prod_unfold listsum_ennreal) |
|
1840 also have "\<dots> = 1" using assms(2) by simp |
|
1841 finally show ?thesis . |
|
1842 qed |
|
1843 |
|
1844 lemma pmf_pmf_of_list: |
|
1845 assumes "pmf_of_list_wf xs" |
|
1846 shows "pmf (pmf_of_list xs) x = listsum (map snd (filter (\<lambda>z. fst z = x) xs))" |
|
1847 using assms pmf_of_list_aux[of xs] unfolding pmf_of_list_def pmf_of_list_wf_def |
|
1848 by (subst pmf_embed_pmf) (auto intro!: listsum_nonneg) |
|
1849 |
1673 end |
1850 end |
|
1851 |
|
1852 lemma set_pmf_of_list: |
|
1853 assumes "pmf_of_list_wf xs" |
|
1854 shows "set_pmf (pmf_of_list xs) \<subseteq> set (map fst xs)" |
|
1855 proof clarify |
|
1856 fix x assume A: "x \<in> set_pmf (pmf_of_list xs)" |
|
1857 show "x \<in> set (map fst xs)" |
|
1858 proof (rule ccontr) |
|
1859 assume "x \<notin> set (map fst xs)" |
|
1860 hence "[z\<leftarrow>xs . fst z = x] = []" by (auto simp: filter_empty_conv) |
|
1861 with A assms show False by (simp add: pmf_pmf_of_list set_pmf_eq) |
|
1862 qed |
|
1863 qed |
|
1864 |
|
1865 lemma finite_set_pmf_of_list: |
|
1866 assumes "pmf_of_list_wf xs" |
|
1867 shows "finite (set_pmf (pmf_of_list xs))" |
|
1868 using assms by (rule finite_subset[OF set_pmf_of_list]) simp_all |
|
1869 |
|
1870 lemma emeasure_Int_set_pmf: |
|
1871 "emeasure (measure_pmf p) (A \<inter> set_pmf p) = emeasure (measure_pmf p) A" |
|
1872 by (rule emeasure_eq_AE) (auto simp: AE_measure_pmf_iff) |
|
1873 |
|
1874 lemma measure_Int_set_pmf: |
|
1875 "measure (measure_pmf p) (A \<inter> set_pmf p) = measure (measure_pmf p) A" |
|
1876 using emeasure_Int_set_pmf[of p A] by (simp add: Sigma_Algebra.measure_def) |
|
1877 |
|
1878 lemma emeasure_pmf_of_list: |
|
1879 assumes "pmf_of_list_wf xs" |
|
1880 shows "emeasure (pmf_of_list xs) A = ennreal (listsum (map snd (filter (\<lambda>x. fst x \<in> A) xs)))" |
|
1881 proof - |
|
1882 have "emeasure (pmf_of_list xs) A = nn_integral (measure_pmf (pmf_of_list xs)) (indicator A)" |
|
1883 by simp |
|
1884 also from assms |
|
1885 have "\<dots> = (\<Sum>x\<in>set_pmf (pmf_of_list xs) \<inter> A. ennreal (listsum (map snd [z\<leftarrow>xs . fst z = x])))" |
|
1886 by (subst nn_integral_measure_pmf_finite) (simp_all add: finite_set_pmf_of_list pmf_pmf_of_list) |
|
1887 also from assms |
|
1888 have "\<dots> = ennreal (\<Sum>x\<in>set_pmf (pmf_of_list xs) \<inter> A. listsum (map snd [z\<leftarrow>xs . fst z = x]))" |
|
1889 by (subst setsum_ennreal) (auto simp: pmf_of_list_wf_def intro!: listsum_nonneg) |
|
1890 also have "\<dots> = ennreal (\<Sum>x\<in>set_pmf (pmf_of_list xs) \<inter> A. |
|
1891 indicator A x * pmf (pmf_of_list xs) x)" (is "_ = ennreal ?S") |
|
1892 using assms by (intro ennreal_cong setsum.cong) (auto simp: pmf_pmf_of_list) |
|
1893 also have "?S = (\<Sum>x\<in>set_pmf (pmf_of_list xs). indicator A x * pmf (pmf_of_list xs) x)" |
|
1894 using assms by (intro setsum.mono_neutral_left set_pmf_of_list finite_set_pmf_of_list) auto |
|
1895 also have "\<dots> = (\<Sum>x\<in>set (map fst xs). indicator A x * pmf (pmf_of_list xs) x)" |
|
1896 using assms by (intro setsum.mono_neutral_left set_pmf_of_list) (auto simp: set_pmf_eq) |
|
1897 also have "\<dots> = (\<Sum>x\<in>set (map fst xs). indicator A x * |
|
1898 listsum (map snd (filter (\<lambda>z. fst z = x) xs)))" |
|
1899 using assms by (simp add: pmf_pmf_of_list) |
|
1900 also have "\<dots> = (\<Sum>x\<in>set (map fst xs). listsum (map snd (filter (\<lambda>z. fst z = x \<and> x \<in> A) xs)))" |
|
1901 by (intro setsum.cong) (auto simp: indicator_def) |
|
1902 also have "\<dots> = (\<Sum>x\<in>set (map fst xs). (\<Sum>xa = 0..<length xs. |
|
1903 if fst (xs ! xa) = x \<and> x \<in> A then snd (xs ! xa) else 0))" |
|
1904 by (intro setsum.cong refl, subst listsum_map_filter, subst listsum_setsum_nth) simp |
|
1905 also have "\<dots> = (\<Sum>xa = 0..<length xs. (\<Sum>x\<in>set (map fst xs). |
|
1906 if fst (xs ! xa) = x \<and> x \<in> A then snd (xs ! xa) else 0))" |
|
1907 by (rule setsum.commute) |
|
1908 also have "\<dots> = (\<Sum>xa = 0..<length xs. if fst (xs ! xa) \<in> A then |
|
1909 (\<Sum>x\<in>set (map fst xs). if x = fst (xs ! xa) then snd (xs ! xa) else 0) else 0)" |
|
1910 by (auto intro!: setsum.cong setsum.neutral) |
|
1911 also have "\<dots> = (\<Sum>xa = 0..<length xs. if fst (xs ! xa) \<in> A then snd (xs ! xa) else 0)" |
|
1912 by (intro setsum.cong refl) (simp_all add: setsum.delta) |
|
1913 also have "\<dots> = listsum (map snd (filter (\<lambda>x. fst x \<in> A) xs))" |
|
1914 by (subst listsum_map_filter, subst listsum_setsum_nth) simp_all |
|
1915 finally show ?thesis . |
|
1916 qed |
|
1917 |
|
1918 lemma measure_pmf_of_list: |
|
1919 assumes "pmf_of_list_wf xs" |
|
1920 shows "measure (pmf_of_list xs) A = listsum (map snd (filter (\<lambda>x. fst x \<in> A) xs))" |
|
1921 using assms unfolding pmf_of_list_wf_def Sigma_Algebra.measure_def |
|
1922 by (subst emeasure_pmf_of_list [OF assms], subst enn2real_ennreal) (auto intro!: listsum_nonneg) |
|
1923 |
|
1924 end |