src/HOL/Probability/Probability_Mass_Function.thy
author hoelzl
Mon Oct 06 16:27:07 2014 +0200 (2014-10-06)
changeset 58587 5484f6079bcd
child 58606 9c66f7c541fb
permissions -rw-r--r--
add type for probability mass functions, i.e. discrete probability distribution
     1 theory Probability_Mass_Function
     2   imports Probability_Measure
     3 begin
     4 
     5 lemma sets_Pair: "{x} \<in> sets M1 \<Longrightarrow> {y} \<in> sets M2 \<Longrightarrow> {(x, y)} \<in> sets (M1 \<Otimes>\<^sub>M M2)"
     6   using pair_measureI[of "{x}" M1 "{y}" M2] by simp
     7 
     8 lemma finite_subset_card:
     9   assumes X: "infinite X" shows "\<exists>A\<subseteq>X. finite A \<and> card A = n"
    10 proof (induct n)
    11   case (Suc n) then guess A .. note A = this
    12   with X obtain x where "x \<in> X" "x \<notin> A"
    13     by (metis subset_antisym subset_eq)
    14   with A show ?case  
    15     by (intro exI[of _ "insert x A"]) auto
    16 qed (simp cong: conj_cong)
    17 
    18 lemma (in prob_space) countable_support:
    19   "countable {x. measure M {x} \<noteq> 0}"
    20 proof -
    21   let ?m = "\<lambda>x. measure M {x}"
    22   have *: "{x. ?m x \<noteq> 0} = (\<Union>n. {x. inverse (real (Suc n)) < ?m x})"
    23     by (auto intro!: measure_nonneg reals_Archimedean order_le_neq_trans)
    24   have **: "\<And>n. finite {x. inverse (Suc n) < ?m x}"
    25   proof (rule ccontr)
    26     fix n assume "infinite {x. inverse (Suc n) < ?m x}" (is "infinite ?X")
    27     then obtain X where "finite X" "card X = Suc (Suc n)" "X \<subseteq> ?X"
    28       by (metis finite_subset_card)
    29     from this(3) have *: "\<And>x. x \<in> X \<Longrightarrow> 1 / Suc n \<le> ?m x" 
    30       by (auto simp: inverse_eq_divide)
    31     { fix x assume "x \<in> X"
    32       from *[OF this] have "?m x \<noteq> 0" by auto
    33       then have "{x} \<in> sets M" by (auto dest: measure_notin_sets) }
    34     note singleton_sets = this
    35     have "1 < (\<Sum>x\<in>X. 1 / Suc n)"
    36       by (simp add: `card X = Suc (Suc n)` real_eq_of_nat[symmetric] real_of_nat_Suc)
    37     also have "\<dots> \<le> (\<Sum>x\<in>X. ?m x)"
    38       by (rule setsum_mono) fact
    39     also have "\<dots> = measure M (\<Union>x\<in>X. {x})"
    40       using singleton_sets `finite X`
    41       by (intro finite_measure_finite_Union[symmetric]) (auto simp: disjoint_family_on_def)
    42     finally show False
    43       using prob_le_1[of "\<Union>x\<in>X. {x}"] by arith
    44   qed
    45   show ?thesis
    46     unfolding * by (intro countable_UN countableI_type countable_finite[OF **])
    47 qed
    48 
    49 lemma measure_count_space: "measure (count_space A) X = (if X \<subseteq> A then card X else 0)"
    50   unfolding measure_def
    51   by (cases "finite X") (simp_all add: emeasure_notin_sets)
    52 
    53 typedef 'a pmf = "{M :: 'a measure. prob_space M \<and> sets M = UNIV \<and> (AE x in M. measure M {x} \<noteq> 0)}"
    54   morphisms measure_pmf Abs_pmf
    55   apply (intro exI[of _ "uniform_measure (count_space UNIV) {undefined}"])
    56   apply (auto intro!: prob_space_uniform_measure simp: measure_count_space)
    57   apply (subst uniform_measure_def)
    58   apply (simp add: AE_density AE_count_space split: split_indicator)
    59   done
    60 
    61 declare [[coercion measure_pmf]]
    62 
    63 lemma prob_space_measure_pmf: "prob_space (measure_pmf p)"
    64   using pmf.measure_pmf[of p] by auto
    65 
    66 interpretation measure_pmf!: prob_space "measure_pmf M" for M
    67   by (rule prob_space_measure_pmf)
    68 
    69 locale pmf_as_measure
    70 begin
    71 
    72 setup_lifting type_definition_pmf
    73 
    74 end
    75 
    76 context
    77 begin
    78 
    79 interpretation pmf_as_measure .
    80 
    81 lift_definition pmf :: "'a pmf \<Rightarrow> 'a \<Rightarrow> real" is "\<lambda>M x. measure M {x}" .
    82 
    83 lift_definition set_pmf :: "'a pmf \<Rightarrow> 'a set" is "\<lambda>M. {x. measure M {x} \<noteq> 0}" .
    84 
    85 lift_definition map_pmf :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a pmf \<Rightarrow> 'b pmf" is
    86   "\<lambda>f M. distr M (count_space UNIV) f"
    87 proof safe
    88   fix M and f :: "'a \<Rightarrow> 'b"
    89   let ?D = "distr M (count_space UNIV) f"
    90   assume "prob_space M" and [simp]: "sets M = UNIV" and ae: "AE x in M. measure M {x} \<noteq> 0"
    91   interpret prob_space M by fact
    92   from ae have "AE x in M. measure M (f -` {f x}) \<noteq> 0"
    93   proof eventually_elim
    94     fix x
    95     have "measure M {x} \<le> measure M (f -` {f x})"
    96       by (intro finite_measure_mono) auto
    97     then show "measure M {x} \<noteq> 0 \<Longrightarrow> measure M (f -` {f x}) \<noteq> 0"
    98       using measure_nonneg[of M "{x}"] by auto
    99   qed
   100   then show "AE x in ?D. measure ?D {x} \<noteq> 0"
   101     by (simp add: AE_distr_iff measure_distr measurable_def)
   102 qed (auto simp: measurable_def prob_space.prob_space_distr)
   103 
   104 declare [[coercion set_pmf]]
   105 
   106 lemma countable_set_pmf: "countable (set_pmf p)"
   107   by transfer (metis prob_space.countable_support)
   108 
   109 lemma sets_measure_pmf[simp]: "sets (measure_pmf p) = UNIV"
   110   by transfer metis
   111 
   112 lemma space_measure_pmf[simp]: "space (measure_pmf p) = UNIV"
   113   using sets_eq_imp_space_eq[of "measure_pmf p" "count_space UNIV"] by simp
   114 
   115 lemma measurable_pmf_measure1[simp]: "measurable (M :: 'a pmf) N = UNIV \<rightarrow> space N"
   116   by (auto simp: measurable_def)
   117 
   118 lemma measurable_pmf_measure2[simp]: "measurable N (M :: 'a pmf) = measurable N (count_space UNIV)"
   119   by (intro measurable_cong_sets) simp_all
   120 
   121 lemma pmf_positive: "x \<in> set_pmf p \<Longrightarrow> 0 < pmf p x"
   122   by transfer (simp add: less_le measure_nonneg)
   123 
   124 lemma pmf_nonneg: "0 \<le> pmf p x"
   125   by transfer (simp add: measure_nonneg)
   126 
   127 lemma emeasure_pmf_single:
   128   fixes M :: "'a pmf"
   129   shows "emeasure M {x} = pmf M x"
   130   by transfer (simp add: finite_measure.emeasure_eq_measure[OF prob_space.finite_measure])
   131 
   132 lemma AE_measure_pmf: "AE x in (M::'a pmf). x \<in> M"
   133   by transfer simp
   134 
   135 lemma emeasure_pmf_single_eq_zero_iff:
   136   fixes M :: "'a pmf"
   137   shows "emeasure M {y} = 0 \<longleftrightarrow> y \<notin> M"
   138   by transfer (simp add: finite_measure.emeasure_eq_measure[OF prob_space.finite_measure])
   139 
   140 lemma AE_measure_pmf_iff: "(AE x in measure_pmf M. P x) \<longleftrightarrow> (\<forall>y\<in>M. P y)"
   141 proof -
   142   { fix y assume y: "y \<in> M" and P: "AE x in M. P x" "\<not> P y"
   143     with P have "AE x in M. x \<noteq> y"
   144       by auto
   145     with y have False
   146       by (simp add: emeasure_pmf_single_eq_zero_iff AE_iff_measurable[OF _ refl]) }
   147   then show ?thesis
   148     using AE_measure_pmf[of M] by auto
   149 qed
   150 
   151 lemma measure_pmf_eq_density: "measure_pmf p = density (count_space UNIV) (pmf p)"
   152 proof (transfer, elim conjE)
   153   fix M :: "'a measure" assume [simp]: "sets M = UNIV" and ae: "AE x in M. measure M {x} \<noteq> 0"
   154   assume "prob_space M" then interpret prob_space M .
   155   show "M = density (count_space UNIV) (\<lambda>x. ereal (measure M {x}))"
   156   proof (rule measure_eqI)
   157     fix A :: "'a set"
   158     have "(\<integral>\<^sup>+ x. ereal (measure M {x}) * indicator A x \<partial>count_space UNIV) = 
   159       (\<integral>\<^sup>+ x. emeasure M {x} * indicator (A \<inter> {x. measure M {x} \<noteq> 0}) x \<partial>count_space UNIV)"
   160       by (auto intro!: nn_integral_cong simp: emeasure_eq_measure split: split_indicator)
   161     also have "\<dots> = (\<integral>\<^sup>+ x. emeasure M {x} \<partial>count_space (A \<inter> {x. measure M {x} \<noteq> 0}))"
   162       by (subst nn_integral_restrict_space[symmetric]) (auto simp: restrict_count_space)
   163     also have "\<dots> = emeasure M (\<Union>x\<in>(A \<inter> {x. measure M {x} \<noteq> 0}). {x})"
   164       by (intro emeasure_UN_countable[symmetric] countable_Int2 countable_support)
   165          (auto simp: disjoint_family_on_def)
   166     also have "\<dots> = emeasure M A"
   167       using ae by (intro emeasure_eq_AE) auto
   168     finally show " emeasure M A = emeasure (density (count_space UNIV) (\<lambda>x. ereal (measure M {x}))) A"
   169       using emeasure_space_1 by (simp add: emeasure_density)
   170   qed simp
   171 qed
   172 
   173 lemma set_pmf_not_empty: "set_pmf M \<noteq> {}"
   174   using AE_measure_pmf[of M] by (intro notI) simp
   175 
   176 lemma set_pmf_iff: "x \<in> set_pmf M \<longleftrightarrow> pmf M x \<noteq> 0"
   177   by transfer simp
   178 
   179 lemma emeasure_pmf: "emeasure (M::'a pmf) M = 1"
   180 proof -
   181   have "emeasure (M::'a pmf) M = emeasure (M::'a pmf) (space M)"
   182     by (intro emeasure_eq_AE) (simp_all add: AE_measure_pmf)
   183   then show ?thesis
   184     using measure_pmf.emeasure_space_1 by simp
   185 qed
   186 
   187 lemma map_pmf_id[simp]: "map_pmf id = id"
   188   by (rule, transfer) (auto simp: emeasure_distr measurable_def intro!: measure_eqI)
   189 
   190 lemma map_pmf_compose: "map_pmf (f \<circ> g) = map_pmf f \<circ> map_pmf g"
   191   by (rule, transfer) (simp add: distr_distr[symmetric, where N="count_space UNIV"] measurable_def) 
   192 
   193 lemma map_pmf_cong:
   194   assumes "p = q"
   195   shows "(\<And>x. x \<in> set_pmf q \<Longrightarrow> f x = g x) \<Longrightarrow> map_pmf f p = map_pmf g q"
   196   unfolding `p = q`[symmetric] measure_pmf_inject[symmetric] map_pmf.rep_eq
   197   by (auto simp add: emeasure_distr AE_measure_pmf_iff intro!: emeasure_eq_AE measure_eqI)
   198 
   199 lemma pmf_set_map: 
   200   fixes f :: "'a \<Rightarrow> 'b"
   201   shows "set_pmf \<circ> map_pmf f = op ` f \<circ> set_pmf"
   202 proof (rule, transfer, clarsimp simp add: measure_distr measurable_def)
   203   fix f :: "'a \<Rightarrow> 'b" and M :: "'a measure"
   204   assume "prob_space M" and ae: "AE x in M. measure M {x} \<noteq> 0" and [simp]: "sets M = UNIV"
   205   interpret prob_space M by fact
   206   show "{x. measure M (f -` {x}) \<noteq> 0} = f ` {x. measure M {x} \<noteq> 0}"
   207   proof safe
   208     fix x assume "measure M (f -` {x}) \<noteq> 0"
   209     moreover have "measure M (f -` {x}) = measure M {y. f y = x \<and> measure M {y} \<noteq> 0}"
   210       using ae by (intro finite_measure_eq_AE) auto
   211     ultimately have "{y. f y = x \<and> measure M {y} \<noteq> 0} \<noteq> {}"
   212       by (metis measure_empty)
   213     then show "x \<in> f ` {x. measure M {x} \<noteq> 0}"
   214       by auto
   215   next
   216     fix x assume "measure M {x} \<noteq> 0"
   217     then have "0 < measure M {x}"
   218       using measure_nonneg[of M "{x}"] by auto
   219     also have "measure M {x} \<le> measure M (f -` {f x})"
   220       by (intro finite_measure_mono) auto
   221     finally show "measure M (f -` {f x}) = 0 \<Longrightarrow> False"
   222       by simp
   223   qed
   224 qed
   225 
   226 context
   227   fixes f :: "'a \<Rightarrow> real"
   228   assumes nonneg: "\<And>x. 0 \<le> f x"
   229   assumes prob: "(\<integral>\<^sup>+x. f x \<partial>count_space UNIV) = 1"
   230 begin
   231 
   232 lift_definition embed_pmf :: "'a pmf" is "density (count_space UNIV) (ereal \<circ> f)"
   233 proof (intro conjI)
   234   have *[simp]: "\<And>x y. ereal (f y) * indicator {x} y = ereal (f x) * indicator {x} y"
   235     by (simp split: split_indicator)
   236   show "AE x in density (count_space UNIV) (ereal \<circ> f).
   237     measure (density (count_space UNIV) (ereal \<circ> f)) {x} \<noteq> 0"
   238     by (simp add: AE_density nonneg emeasure_density measure_def nn_integral_cmult_indicator)
   239   show "prob_space (density (count_space UNIV) (ereal \<circ> f))"
   240     by default (simp add: emeasure_density prob)
   241 qed simp
   242 
   243 lemma pmf_embed_pmf: "pmf embed_pmf x = f x"
   244 proof transfer
   245   have *[simp]: "\<And>x y. ereal (f y) * indicator {x} y = ereal (f x) * indicator {x} y"
   246     by (simp split: split_indicator)
   247   fix x show "measure (density (count_space UNIV) (ereal \<circ> f)) {x} = f x"
   248     by transfer (simp add: measure_def emeasure_density nn_integral_cmult_indicator nonneg)
   249 qed
   250 
   251 end
   252 
   253 lemma embed_pmf_transfer:
   254   "rel_fun (eq_onp (\<lambda>f::'a \<Rightarrow> real. (\<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"
   255   by (auto simp: rel_fun_def eq_onp_def embed_pmf.transfer)
   256 
   257 lemma td_pmf_embed_pmf:
   258   "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}"
   259   unfolding type_definition_def
   260 proof safe
   261   fix p :: "'a pmf"
   262   have "(\<integral>\<^sup>+ x. 1 \<partial>measure_pmf p) = 1"
   263     using measure_pmf.emeasure_space_1[of p] by simp
   264   then show *: "(\<integral>\<^sup>+ x. ereal (pmf p x) \<partial>count_space UNIV) = 1"
   265     by (simp add: measure_pmf_eq_density nn_integral_density pmf_nonneg del: nn_integral_const)
   266 
   267   show "embed_pmf (pmf p) = p"
   268     by (intro measure_pmf_inject[THEN iffD1])
   269        (simp add: * embed_pmf.rep_eq pmf_nonneg measure_pmf_eq_density[of p] comp_def)
   270 next
   271   fix f :: "'a \<Rightarrow> real" assume "\<forall>x. 0 \<le> f x" "(\<integral>\<^sup>+x. f x \<partial>count_space UNIV) = 1"
   272   then show "pmf (embed_pmf f) = f"
   273     by (auto intro!: pmf_embed_pmf)
   274 qed (rule pmf_nonneg)
   275 
   276 end
   277 
   278 locale pmf_as_function
   279 begin
   280 
   281 setup_lifting td_pmf_embed_pmf
   282 
   283 end 
   284 
   285 (*
   286 
   287 definition
   288   "rel_pmf P d1 d2 \<longleftrightarrow> (\<exists>p3. (\<forall>(x, y) \<in> set_pmf p3. P x y) \<and> map_pmf fst p3 = d1 \<and> map_pmf snd p3 = d2)"
   289 
   290 lift_definition pmf_join :: "real \<Rightarrow> 'a pmf \<Rightarrow> 'a pmf \<Rightarrow> 'a pmf" is
   291   "\<lambda>p M1 M2. density (count_space UNIV) (\<lambda>x. p * measure M1 {x} + (1 - p) * measure M2 {x})"
   292 sorry
   293 
   294 lift_definition pmf_single :: "'a \<Rightarrow> 'a pmf" is
   295   "\<lambda>x. uniform_measure (count_space UNIV) {x}"
   296 sorry
   297 
   298 bnf pmf: "'a pmf" map: map_pmf sets: set_pmf bd : "natLeq" rel: pmf_rel
   299 proof -
   300   show "map_pmf id = id" by (rule map_pmf_id)
   301   show "\<And>f g. map_pmf (f \<circ> g) = map_pmf f \<circ> map_pmf g" by (rule map_pmf_compose) 
   302   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"
   303     by (intro map_pmg_cong refl)
   304 
   305   show "\<And>f::'a \<Rightarrow> 'b. set_pmf \<circ> map_pmf f = op ` f \<circ> set_pmf"
   306     by (rule pmf_set_map)
   307 
   308   { fix p :: "'s pmf"
   309     have "(card_of (set_pmf p), card_of (UNIV :: nat set)) \<in> ordLeq"
   310       by (rule card_of_ordLeqI[where f="to_nat_on (set_pmf p)"])
   311          (auto intro: countable_set_pmf inj_on_to_nat_on)
   312     also have "(card_of (UNIV :: nat set), natLeq) \<in> ordLeq"
   313       by (metis Field_natLeq card_of_least natLeq_Well_order)
   314     finally show "(card_of (set_pmf p), natLeq) \<in> ordLeq" . }
   315 
   316   show "\<And>R. pmf_rel R =
   317          (BNF_Util.Grp {x. set_pmf x \<subseteq> {(x, y). R x y}} (map_pmf fst))\<inverse>\<inverse> OO
   318          BNF_Util.Grp {x. set_pmf x \<subseteq> {(x, y). R x y}} (map_pmf snd)"
   319      by (auto simp add: fun_eq_iff pmf_rel_def BNF_Util.Grp_def OO_def)
   320 
   321   { let ?f = "map_pmf fst" and ?s = "map_pmf snd"
   322     fix R :: "'a \<Rightarrow> 'b \<Rightarrow> bool" and A assume "\<And>x y. (x, y) \<in> set_pmf A \<Longrightarrow> R x y"
   323     fix S :: "'b \<Rightarrow> 'c \<Rightarrow> bool" and B assume "\<And>y z. (y, z) \<in> set_pmf B \<Longrightarrow> S y z"
   324     assume "?f B = ?s A"
   325     have "\<exists>C. (\<forall>(x, z)\<in>set_pmf C. \<exists>y. R x y \<and> S y z) \<and> ?f C = ?f A \<and> ?s C = ?s B"
   326       sorry }
   327 oops
   328   then show "\<And>R::'a \<Rightarrow> 'b \<Rightarrow> bool. \<And>S::'b \<Rightarrow> 'c \<Rightarrow> bool. pmf_rel R OO pmf_rel S \<le> pmf_rel (R OO S)"
   329       by (auto simp add: subset_eq pmf_rel_def fun_eq_iff OO_def Ball_def)
   330 qed (fact natLeq_card_order natLeq_cinfinite)+
   331 
   332 notepad
   333 begin
   334   fix x y :: "nat \<Rightarrow> real"
   335   def IJz \<equiv> "rec_nat ((0, 0), \<lambda>_. 0) (\<lambda>n ((I, J), z).
   336     let a = x I - (\<Sum>j<J. z (I, j)) ; b = y J - (\<Sum>i<I. z (i, J)) in
   337       ((if a \<le> b then I + 1 else I, if b \<le> a then J + 1 else J), z((I, J) := min a b)))"
   338   def I == "fst \<circ> fst \<circ> IJz" def J == "snd \<circ> fst \<circ> IJz" def z == "snd \<circ> IJz"
   339   let ?a = "\<lambda>n. x (I n) - (\<Sum>j<J n. z n (I n, j))" and ?b = "\<lambda>n. y (J n) - (\<Sum>i<I n. z n (i, J n))"
   340   have IJz_0[simp]: "\<And>p. z 0 p = 0" "I 0 = 0" "J 0 = 0"
   341     by (simp_all add: I_def J_def z_def IJz_def)
   342   have z_Suc[simp]: "\<And>n. z (Suc n) = (z n)((I n, J n) := min (?a n) (?b n))"
   343     by (simp add: z_def I_def J_def IJz_def Let_def split_beta)
   344   have I_Suc[simp]: "\<And>n. I (Suc n) = (if ?a n \<le> ?b n then I n + 1 else I n)"
   345     by (simp add: z_def I_def J_def IJz_def Let_def split_beta)
   346   have J_Suc[simp]: "\<And>n. J (Suc n) = (if ?b n \<le> ?a n then J n + 1 else J n)"
   347     by (simp add: z_def I_def J_def IJz_def Let_def split_beta)
   348   
   349   { fix N have "\<And>p. z N p \<noteq> 0 \<Longrightarrow> \<exists>n<N. p = (I n, J n)"
   350       by (induct N) (auto simp add: less_Suc_eq split: split_if_asm) }
   351   
   352   { fix i n assume "i < I n"
   353     then have "(\<Sum>j. z n (i, j)) = x i" 
   354     oops
   355 *)
   356 
   357 end
   358