src/HOL/Probability/Probability_Mass_Function.thy
author hoelzl
Tue Mar 10 10:53:48 2015 +0100 (2015-03-10)
changeset 59664 224741ede5ae
parent 59557 ebd8ecacfba6
child 59665 37adca7fd48f
permissions -rw-r--r--
build pmf's on bind
hoelzl@58606
     1
(*  Title:      HOL/Probability/Probability_Mass_Function.thy
Andreas@59023
     2
    Author:     Johannes Hölzl, TU München 
Andreas@59023
     3
    Author:     Andreas Lochbihler, ETH Zurich
Andreas@59023
     4
*)
hoelzl@58606
     5
hoelzl@59000
     6
section \<open> Probability mass function \<close>
hoelzl@59000
     7
hoelzl@58587
     8
theory Probability_Mass_Function
hoelzl@59000
     9
imports
hoelzl@59000
    10
  Giry_Monad
hoelzl@59093
    11
  "~~/src/HOL/Number_Theory/Binomial"
hoelzl@59000
    12
  "~~/src/HOL/Library/Multiset"
hoelzl@58587
    13
begin
hoelzl@58587
    14
hoelzl@59664
    15
lemma AE_emeasure_singleton:
hoelzl@59664
    16
  assumes x: "emeasure M {x} \<noteq> 0" and ae: "AE x in M. P x" shows "P x"
hoelzl@59664
    17
proof -
hoelzl@59664
    18
  from x have x_M: "{x} \<in> sets M"
hoelzl@59664
    19
    by (auto intro: emeasure_notin_sets)
hoelzl@59664
    20
  from ae obtain N where N: "{x\<in>space M. \<not> P x} \<subseteq> N" "emeasure M N = 0" "N \<in> sets M"
hoelzl@59664
    21
    by (auto elim: AE_E)
hoelzl@59664
    22
  { assume "\<not> P x"
hoelzl@59664
    23
    with x_M[THEN sets.sets_into_space] N have "emeasure M {x} \<le> emeasure M N"
hoelzl@59664
    24
      by (intro emeasure_mono) auto
hoelzl@59664
    25
    with x N have False
hoelzl@59664
    26
      by (auto simp: emeasure_le_0_iff) }
hoelzl@59664
    27
  then show "P x" by auto
hoelzl@59664
    28
qed
hoelzl@59664
    29
hoelzl@59664
    30
lemma AE_measure_singleton: "measure M {x} \<noteq> 0 \<Longrightarrow> AE x in M. P x \<Longrightarrow> P x"
hoelzl@59664
    31
  by (metis AE_emeasure_singleton measure_def emeasure_empty measure_empty)
hoelzl@59664
    32
hoelzl@59494
    33
lemma ereal_divide': "b \<noteq> 0 \<Longrightarrow> ereal (a / b) = ereal a / ereal b"
hoelzl@59494
    34
  using ereal_divide[of a b] by simp
hoelzl@59494
    35
hoelzl@59052
    36
lemma (in finite_measure) countable_support:
hoelzl@58587
    37
  "countable {x. measure M {x} \<noteq> 0}"
hoelzl@59000
    38
proof cases
hoelzl@59000
    39
  assume "measure M (space M) = 0"
hoelzl@59000
    40
  with bounded_measure measure_le_0_iff have "{x. measure M {x} \<noteq> 0} = {}"
hoelzl@59000
    41
    by auto
hoelzl@59000
    42
  then show ?thesis
hoelzl@59000
    43
    by simp
hoelzl@59000
    44
next
hoelzl@59000
    45
  let ?M = "measure M (space M)" and ?m = "\<lambda>x. measure M {x}"
hoelzl@59000
    46
  assume "?M \<noteq> 0"
hoelzl@59000
    47
  then have *: "{x. ?m x \<noteq> 0} = (\<Union>n. {x. ?M / Suc n < ?m x})"
hoelzl@59000
    48
    using reals_Archimedean[of "?m x / ?M" for x]
hoelzl@59000
    49
    by (auto simp: field_simps not_le[symmetric] measure_nonneg divide_le_0_iff measure_le_0_iff)
hoelzl@59000
    50
  have **: "\<And>n. finite {x. ?M / Suc n < ?m x}"
hoelzl@58587
    51
  proof (rule ccontr)
hoelzl@59000
    52
    fix n assume "infinite {x. ?M / Suc n < ?m x}" (is "infinite ?X")
hoelzl@58587
    53
    then obtain X where "finite X" "card X = Suc (Suc n)" "X \<subseteq> ?X"
hoelzl@58606
    54
      by (metis infinite_arbitrarily_large)
hoelzl@59000
    55
    from this(3) have *: "\<And>x. x \<in> X \<Longrightarrow> ?M / Suc n \<le> ?m x" 
hoelzl@59000
    56
      by auto
hoelzl@58587
    57
    { fix x assume "x \<in> X"
hoelzl@59000
    58
      from `?M \<noteq> 0` *[OF this] have "?m x \<noteq> 0" by (auto simp: field_simps measure_le_0_iff)
hoelzl@58587
    59
      then have "{x} \<in> sets M" by (auto dest: measure_notin_sets) }
hoelzl@58587
    60
    note singleton_sets = this
hoelzl@59000
    61
    have "?M < (\<Sum>x\<in>X. ?M / Suc n)"
hoelzl@59000
    62
      using `?M \<noteq> 0` 
hoelzl@59000
    63
      by (simp add: `card X = Suc (Suc n)` real_eq_of_nat[symmetric] real_of_nat_Suc field_simps less_le measure_nonneg)
hoelzl@58587
    64
    also have "\<dots> \<le> (\<Sum>x\<in>X. ?m x)"
hoelzl@58587
    65
      by (rule setsum_mono) fact
hoelzl@58587
    66
    also have "\<dots> = measure M (\<Union>x\<in>X. {x})"
hoelzl@58587
    67
      using singleton_sets `finite X`
hoelzl@58587
    68
      by (intro finite_measure_finite_Union[symmetric]) (auto simp: disjoint_family_on_def)
hoelzl@59000
    69
    finally have "?M < measure M (\<Union>x\<in>X. {x})" .
hoelzl@59000
    70
    moreover have "measure M (\<Union>x\<in>X. {x}) \<le> ?M"
hoelzl@59000
    71
      using singleton_sets[THEN sets.sets_into_space] by (intro finite_measure_mono) auto
hoelzl@59000
    72
    ultimately show False by simp
hoelzl@58587
    73
  qed
hoelzl@58587
    74
  show ?thesis
hoelzl@58587
    75
    unfolding * by (intro countable_UN countableI_type countable_finite[OF **])
hoelzl@58587
    76
qed
hoelzl@58587
    77
hoelzl@59000
    78
lemma (in finite_measure) AE_support_countable:
hoelzl@59000
    79
  assumes [simp]: "sets M = UNIV"
hoelzl@59000
    80
  shows "(AE x in M. measure M {x} \<noteq> 0) \<longleftrightarrow> (\<exists>S. countable S \<and> (AE x in M. x \<in> S))"
hoelzl@59000
    81
proof
hoelzl@59000
    82
  assume "\<exists>S. countable S \<and> (AE x in M. x \<in> S)"
hoelzl@59000
    83
  then obtain S where S[intro]: "countable S" and ae: "AE x in M. x \<in> S"
hoelzl@59000
    84
    by auto
hoelzl@59000
    85
  then have "emeasure M (\<Union>x\<in>{x\<in>S. emeasure M {x} \<noteq> 0}. {x}) = 
hoelzl@59000
    86
    (\<integral>\<^sup>+ x. emeasure M {x} * indicator {x\<in>S. emeasure M {x} \<noteq> 0} x \<partial>count_space UNIV)"
hoelzl@59000
    87
    by (subst emeasure_UN_countable)
hoelzl@59000
    88
       (auto simp: disjoint_family_on_def nn_integral_restrict_space[symmetric] restrict_count_space)
hoelzl@59000
    89
  also have "\<dots> = (\<integral>\<^sup>+ x. emeasure M {x} * indicator S x \<partial>count_space UNIV)"
hoelzl@59000
    90
    by (auto intro!: nn_integral_cong split: split_indicator)
hoelzl@59000
    91
  also have "\<dots> = emeasure M (\<Union>x\<in>S. {x})"
hoelzl@59000
    92
    by (subst emeasure_UN_countable)
hoelzl@59000
    93
       (auto simp: disjoint_family_on_def nn_integral_restrict_space[symmetric] restrict_count_space)
hoelzl@59000
    94
  also have "\<dots> = emeasure M (space M)"
hoelzl@59000
    95
    using ae by (intro emeasure_eq_AE) auto
hoelzl@59000
    96
  finally have "emeasure M {x \<in> space M. x\<in>S \<and> emeasure M {x} \<noteq> 0} = emeasure M (space M)"
hoelzl@59000
    97
    by (simp add: emeasure_single_in_space cong: rev_conj_cong)
hoelzl@59000
    98
  with finite_measure_compl[of "{x \<in> space M. x\<in>S \<and> emeasure M {x} \<noteq> 0}"]
hoelzl@59000
    99
  have "AE x in M. x \<in> S \<and> emeasure M {x} \<noteq> 0"
hoelzl@59000
   100
    by (intro AE_I[OF order_refl]) (auto simp: emeasure_eq_measure set_diff_eq cong: conj_cong)
hoelzl@59000
   101
  then show "AE x in M. measure M {x} \<noteq> 0"
hoelzl@59000
   102
    by (auto simp: emeasure_eq_measure)
hoelzl@59000
   103
qed (auto intro!: exI[of _ "{x. measure M {x} \<noteq> 0}"] countable_support)
hoelzl@59000
   104
hoelzl@59664
   105
subsection \<open> PMF as measure \<close>
hoelzl@59000
   106
hoelzl@58587
   107
typedef 'a pmf = "{M :: 'a measure. prob_space M \<and> sets M = UNIV \<and> (AE x in M. measure M {x} \<noteq> 0)}"
hoelzl@58587
   108
  morphisms measure_pmf Abs_pmf
hoelzl@58606
   109
  by (intro exI[of _ "uniform_measure (count_space UNIV) {undefined}"])
hoelzl@58606
   110
     (auto intro!: prob_space_uniform_measure AE_uniform_measureI)
hoelzl@58587
   111
hoelzl@58587
   112
declare [[coercion measure_pmf]]
hoelzl@58587
   113
hoelzl@58587
   114
lemma prob_space_measure_pmf: "prob_space (measure_pmf p)"
hoelzl@58587
   115
  using pmf.measure_pmf[of p] by auto
hoelzl@58587
   116
hoelzl@58587
   117
interpretation measure_pmf!: prob_space "measure_pmf M" for M
hoelzl@58587
   118
  by (rule prob_space_measure_pmf)
hoelzl@58587
   119
hoelzl@59000
   120
interpretation measure_pmf!: subprob_space "measure_pmf M" for M
hoelzl@59000
   121
  by (rule prob_space_imp_subprob_space) unfold_locales
hoelzl@59000
   122
hoelzl@59048
   123
lemma subprob_space_measure_pmf: "subprob_space (measure_pmf x)"
hoelzl@59048
   124
  by unfold_locales
hoelzl@59048
   125
hoelzl@58587
   126
locale pmf_as_measure
hoelzl@58587
   127
begin
hoelzl@58587
   128
hoelzl@58587
   129
setup_lifting type_definition_pmf
hoelzl@58587
   130
hoelzl@58587
   131
end
hoelzl@58587
   132
hoelzl@58587
   133
context
hoelzl@58587
   134
begin
hoelzl@58587
   135
hoelzl@58587
   136
interpretation pmf_as_measure .
hoelzl@58587
   137
hoelzl@58587
   138
lemma sets_measure_pmf[simp]: "sets (measure_pmf p) = UNIV"
hoelzl@59664
   139
  by transfer blast 
hoelzl@58587
   140
hoelzl@59048
   141
lemma sets_measure_pmf_count_space[measurable_cong]:
hoelzl@59048
   142
  "sets (measure_pmf M) = sets (count_space UNIV)"
hoelzl@59000
   143
  by simp
hoelzl@59000
   144
hoelzl@58587
   145
lemma space_measure_pmf[simp]: "space (measure_pmf p) = UNIV"
hoelzl@58587
   146
  using sets_eq_imp_space_eq[of "measure_pmf p" "count_space UNIV"] by simp
hoelzl@58587
   147
hoelzl@59048
   148
lemma measure_pmf_in_subprob_algebra[measurable (raw)]: "measure_pmf x \<in> space (subprob_algebra (count_space UNIV))"
hoelzl@59048
   149
  by (simp add: space_subprob_algebra subprob_space_measure_pmf)
hoelzl@59048
   150
hoelzl@58587
   151
lemma measurable_pmf_measure1[simp]: "measurable (M :: 'a pmf) N = UNIV \<rightarrow> space N"
hoelzl@58587
   152
  by (auto simp: measurable_def)
hoelzl@58587
   153
hoelzl@58587
   154
lemma measurable_pmf_measure2[simp]: "measurable N (M :: 'a pmf) = measurable N (count_space UNIV)"
hoelzl@58587
   155
  by (intro measurable_cong_sets) simp_all
hoelzl@58587
   156
hoelzl@59664
   157
lemma measurable_pair_restrict_pmf2:
hoelzl@59664
   158
  assumes "countable A"
hoelzl@59664
   159
  assumes [measurable]: "\<And>y. y \<in> A \<Longrightarrow> (\<lambda>x. f (x, y)) \<in> measurable M L"
hoelzl@59664
   160
  shows "f \<in> measurable (M \<Otimes>\<^sub>M restrict_space (measure_pmf N) A) L" (is "f \<in> measurable ?M _")
hoelzl@59664
   161
proof -
hoelzl@59664
   162
  have [measurable_cong]: "sets (restrict_space (count_space UNIV) A) = sets (count_space A)"
hoelzl@59664
   163
    by (simp add: restrict_count_space)
hoelzl@58587
   164
hoelzl@59664
   165
  show ?thesis
hoelzl@59664
   166
    by (intro measurable_compose_countable'[where f="\<lambda>a b. f (fst b, a)" and g=snd and I=A,
hoelzl@59664
   167
                                            unfolded pair_collapse] assms)
hoelzl@59664
   168
        measurable
hoelzl@59664
   169
qed
hoelzl@58587
   170
hoelzl@59664
   171
lemma measurable_pair_restrict_pmf1:
hoelzl@59664
   172
  assumes "countable A"
hoelzl@59664
   173
  assumes [measurable]: "\<And>x. x \<in> A \<Longrightarrow> (\<lambda>y. f (x, y)) \<in> measurable N L"
hoelzl@59664
   174
  shows "f \<in> measurable (restrict_space (measure_pmf M) A \<Otimes>\<^sub>M N) L"
hoelzl@59664
   175
proof -
hoelzl@59664
   176
  have [measurable_cong]: "sets (restrict_space (count_space UNIV) A) = sets (count_space A)"
hoelzl@59664
   177
    by (simp add: restrict_count_space)
hoelzl@59000
   178
hoelzl@59664
   179
  show ?thesis
hoelzl@59664
   180
    by (intro measurable_compose_countable'[where f="\<lambda>a b. f (a, snd b)" and g=fst and I=A,
hoelzl@59664
   181
                                            unfolded pair_collapse] assms)
hoelzl@59664
   182
        measurable
hoelzl@59664
   183
qed
hoelzl@59664
   184
hoelzl@59664
   185
lift_definition pmf :: "'a pmf \<Rightarrow> 'a \<Rightarrow> real" is "\<lambda>M x. measure M {x}" .
hoelzl@59664
   186
hoelzl@59664
   187
lift_definition set_pmf :: "'a pmf \<Rightarrow> 'a set" is "\<lambda>M. {x. measure M {x} \<noteq> 0}" .
hoelzl@59664
   188
declare [[coercion set_pmf]]
hoelzl@58587
   189
hoelzl@58587
   190
lemma AE_measure_pmf: "AE x in (M::'a pmf). x \<in> M"
hoelzl@58587
   191
  by transfer simp
hoelzl@58587
   192
hoelzl@58587
   193
lemma emeasure_pmf_single_eq_zero_iff:
hoelzl@58587
   194
  fixes M :: "'a pmf"
hoelzl@58587
   195
  shows "emeasure M {y} = 0 \<longleftrightarrow> y \<notin> M"
hoelzl@58587
   196
  by transfer (simp add: finite_measure.emeasure_eq_measure[OF prob_space.finite_measure])
hoelzl@58587
   197
hoelzl@58587
   198
lemma AE_measure_pmf_iff: "(AE x in measure_pmf M. P x) \<longleftrightarrow> (\<forall>y\<in>M. P y)"
hoelzl@59664
   199
  using AE_measure_singleton[of M] AE_measure_pmf[of M]
hoelzl@59664
   200
  by (auto simp: set_pmf.rep_eq)
hoelzl@59664
   201
hoelzl@59664
   202
lemma countable_set_pmf [simp]: "countable (set_pmf p)"
hoelzl@59664
   203
  by transfer (metis prob_space.finite_measure finite_measure.countable_support)
hoelzl@59664
   204
hoelzl@59664
   205
lemma pmf_positive: "x \<in> set_pmf p \<Longrightarrow> 0 < pmf p x"
hoelzl@59664
   206
  by transfer (simp add: less_le measure_nonneg)
hoelzl@59664
   207
hoelzl@59664
   208
lemma pmf_nonneg: "0 \<le> pmf p x"
hoelzl@59664
   209
  by transfer (simp add: measure_nonneg)
hoelzl@59664
   210
hoelzl@59664
   211
lemma pmf_le_1: "pmf p x \<le> 1"
hoelzl@59664
   212
  by (simp add: pmf.rep_eq)
hoelzl@58587
   213
hoelzl@58587
   214
lemma set_pmf_not_empty: "set_pmf M \<noteq> {}"
hoelzl@58587
   215
  using AE_measure_pmf[of M] by (intro notI) simp
hoelzl@58587
   216
hoelzl@58587
   217
lemma set_pmf_iff: "x \<in> set_pmf M \<longleftrightarrow> pmf M x \<noteq> 0"
hoelzl@58587
   218
  by transfer simp
hoelzl@58587
   219
hoelzl@59664
   220
lemma set_pmf_eq: "set_pmf M = {x. pmf M x \<noteq> 0}"
hoelzl@59664
   221
  by (auto simp: set_pmf_iff)
hoelzl@59664
   222
hoelzl@59664
   223
lemma emeasure_pmf_single:
hoelzl@59664
   224
  fixes M :: "'a pmf"
hoelzl@59664
   225
  shows "emeasure M {x} = pmf M x"
hoelzl@59664
   226
  by transfer (simp add: finite_measure.emeasure_eq_measure[OF prob_space.finite_measure])
hoelzl@59664
   227
hoelzl@59000
   228
lemma emeasure_measure_pmf_finite: "finite S \<Longrightarrow> emeasure (measure_pmf M) S = (\<Sum>s\<in>S. pmf M s)"
hoelzl@59000
   229
  by (subst emeasure_eq_setsum_singleton) (auto simp: emeasure_pmf_single)
hoelzl@59000
   230
Andreas@59023
   231
lemma measure_measure_pmf_finite: "finite S \<Longrightarrow> measure (measure_pmf M) S = setsum (pmf M) S"
hoelzl@59425
   232
  using emeasure_measure_pmf_finite[of S M] by(simp add: measure_pmf.emeasure_eq_measure)
Andreas@59023
   233
hoelzl@59000
   234
lemma nn_integral_measure_pmf_support:
hoelzl@59000
   235
  fixes f :: "'a \<Rightarrow> ereal"
hoelzl@59000
   236
  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"
hoelzl@59000
   237
  shows "(\<integral>\<^sup>+x. f x \<partial>measure_pmf M) = (\<Sum>x\<in>A. f x * pmf M x)"
hoelzl@59000
   238
proof -
hoelzl@59000
   239
  have "(\<integral>\<^sup>+x. f x \<partial>M) = (\<integral>\<^sup>+x. f x * indicator A x \<partial>M)"
hoelzl@59000
   240
    using nn by (intro nn_integral_cong_AE) (auto simp: AE_measure_pmf_iff split: split_indicator)
hoelzl@59000
   241
  also have "\<dots> = (\<Sum>x\<in>A. f x * emeasure M {x})"
hoelzl@59000
   242
    using assms by (intro nn_integral_indicator_finite) auto
hoelzl@59000
   243
  finally show ?thesis
hoelzl@59000
   244
    by (simp add: emeasure_measure_pmf_finite)
hoelzl@59000
   245
qed
hoelzl@59000
   246
hoelzl@59000
   247
lemma nn_integral_measure_pmf_finite:
hoelzl@59000
   248
  fixes f :: "'a \<Rightarrow> ereal"
hoelzl@59000
   249
  assumes f: "finite (set_pmf M)" and nn: "\<And>x. x \<in> set_pmf M \<Longrightarrow> 0 \<le> f x"
hoelzl@59000
   250
  shows "(\<integral>\<^sup>+x. f x \<partial>measure_pmf M) = (\<Sum>x\<in>set_pmf M. f x * pmf M x)"
hoelzl@59000
   251
  using assms by (intro nn_integral_measure_pmf_support) auto
hoelzl@59000
   252
lemma integrable_measure_pmf_finite:
hoelzl@59000
   253
  fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}"
hoelzl@59000
   254
  shows "finite (set_pmf M) \<Longrightarrow> integrable M f"
hoelzl@59000
   255
  by (auto intro!: integrableI_bounded simp: nn_integral_measure_pmf_finite)
hoelzl@59000
   256
hoelzl@59000
   257
lemma integral_measure_pmf:
hoelzl@59000
   258
  assumes [simp]: "finite A" and "\<And>a. a \<in> set_pmf M \<Longrightarrow> f a \<noteq> 0 \<Longrightarrow> a \<in> A"
hoelzl@59000
   259
  shows "(\<integral>x. f x \<partial>measure_pmf M) = (\<Sum>a\<in>A. f a * pmf M a)"
hoelzl@59000
   260
proof -
hoelzl@59000
   261
  have "(\<integral>x. f x \<partial>measure_pmf M) = (\<integral>x. f x * indicator A x \<partial>measure_pmf M)"
hoelzl@59000
   262
    using assms(2) by (intro integral_cong_AE) (auto split: split_indicator simp: AE_measure_pmf_iff)
hoelzl@59000
   263
  also have "\<dots> = (\<Sum>a\<in>A. f a * pmf M a)"
hoelzl@59000
   264
    by (subst integral_indicator_finite_real) (auto simp: measure_def emeasure_measure_pmf_finite)
hoelzl@59000
   265
  finally show ?thesis .
hoelzl@59000
   266
qed
hoelzl@59000
   267
hoelzl@59000
   268
lemma integrable_pmf: "integrable (count_space X) (pmf M)"
hoelzl@59000
   269
proof -
hoelzl@59000
   270
  have " (\<integral>\<^sup>+ x. pmf M x \<partial>count_space X) = (\<integral>\<^sup>+ x. pmf M x \<partial>count_space (M \<inter> X))"
hoelzl@59000
   271
    by (auto simp add: nn_integral_count_space_indicator set_pmf_iff intro!: nn_integral_cong split: split_indicator)
hoelzl@59000
   272
  then have "integrable (count_space X) (pmf M) = integrable (count_space (M \<inter> X)) (pmf M)"
hoelzl@59000
   273
    by (simp add: integrable_iff_bounded pmf_nonneg)
hoelzl@59000
   274
  then show ?thesis
Andreas@59023
   275
    by (simp add: pmf.rep_eq measure_pmf.integrable_measure disjoint_family_on_def)
hoelzl@59000
   276
qed
hoelzl@59000
   277
hoelzl@59000
   278
lemma integral_pmf: "(\<integral>x. pmf M x \<partial>count_space X) = measure M X"
hoelzl@59000
   279
proof -
hoelzl@59000
   280
  have "(\<integral>x. pmf M x \<partial>count_space X) = (\<integral>\<^sup>+x. pmf M x \<partial>count_space X)"
hoelzl@59000
   281
    by (simp add: pmf_nonneg integrable_pmf nn_integral_eq_integral)
hoelzl@59000
   282
  also have "\<dots> = (\<integral>\<^sup>+x. emeasure M {x} \<partial>count_space (X \<inter> M))"
hoelzl@59000
   283
    by (auto intro!: nn_integral_cong_AE split: split_indicator
hoelzl@59000
   284
             simp: pmf.rep_eq measure_pmf.emeasure_eq_measure nn_integral_count_space_indicator
hoelzl@59000
   285
                   AE_count_space set_pmf_iff)
hoelzl@59000
   286
  also have "\<dots> = emeasure M (X \<inter> M)"
hoelzl@59000
   287
    by (rule emeasure_countable_singleton[symmetric]) (auto intro: countable_set_pmf)
hoelzl@59000
   288
  also have "\<dots> = emeasure M X"
hoelzl@59000
   289
    by (auto intro!: emeasure_eq_AE simp: AE_measure_pmf_iff)
hoelzl@59000
   290
  finally show ?thesis
hoelzl@59000
   291
    by (simp add: measure_pmf.emeasure_eq_measure)
hoelzl@59000
   292
qed
hoelzl@59000
   293
hoelzl@59000
   294
lemma integral_pmf_restrict:
hoelzl@59000
   295
  "(f::'a \<Rightarrow> 'b::{banach, second_countable_topology}) \<in> borel_measurable (count_space UNIV) \<Longrightarrow>
hoelzl@59000
   296
    (\<integral>x. f x \<partial>measure_pmf M) = (\<integral>x. f x \<partial>restrict_space M M)"
hoelzl@59000
   297
  by (auto intro!: integral_cong_AE simp add: integral_restrict_space AE_measure_pmf_iff)
hoelzl@59000
   298
hoelzl@58587
   299
lemma emeasure_pmf: "emeasure (M::'a pmf) M = 1"
hoelzl@58587
   300
proof -
hoelzl@58587
   301
  have "emeasure (M::'a pmf) M = emeasure (M::'a pmf) (space M)"
hoelzl@58587
   302
    by (intro emeasure_eq_AE) (simp_all add: AE_measure_pmf)
hoelzl@58587
   303
  then show ?thesis
hoelzl@58587
   304
    using measure_pmf.emeasure_space_1 by simp
hoelzl@58587
   305
qed
hoelzl@58587
   306
Andreas@59490
   307
lemma emeasure_pmf_UNIV [simp]: "emeasure (measure_pmf M) UNIV = 1"
Andreas@59490
   308
using measure_pmf.emeasure_space_1[of M] by simp
Andreas@59490
   309
Andreas@59023
   310
lemma in_null_sets_measure_pmfI:
Andreas@59023
   311
  "A \<inter> set_pmf p = {} \<Longrightarrow> A \<in> null_sets (measure_pmf p)"
Andreas@59023
   312
using emeasure_eq_0_AE[where ?P="\<lambda>x. x \<in> A" and M="measure_pmf p"]
Andreas@59023
   313
by(auto simp add: null_sets_def AE_measure_pmf_iff)
Andreas@59023
   314
hoelzl@59664
   315
lemma measure_subprob: "measure_pmf M \<in> space (subprob_algebra (count_space UNIV))"
hoelzl@59664
   316
  by (simp add: space_subprob_algebra subprob_space_measure_pmf)
hoelzl@59664
   317
hoelzl@59664
   318
subsection \<open> Monad Interpretation \<close>
hoelzl@59664
   319
hoelzl@59664
   320
lemma measurable_measure_pmf[measurable]:
hoelzl@59664
   321
  "(\<lambda>x. measure_pmf (M x)) \<in> measurable (count_space UNIV) (subprob_algebra (count_space UNIV))"
hoelzl@59664
   322
  by (auto simp: space_subprob_algebra intro!: prob_space_imp_subprob_space) unfold_locales
hoelzl@59664
   323
hoelzl@59664
   324
lemma bind_measure_pmf_cong:
hoelzl@59664
   325
  assumes "\<And>x. A x \<in> space (subprob_algebra N)" "\<And>x. B x \<in> space (subprob_algebra N)"
hoelzl@59664
   326
  assumes "\<And>i. i \<in> set_pmf x \<Longrightarrow> A i = B i"
hoelzl@59664
   327
  shows "bind (measure_pmf x) A = bind (measure_pmf x) B"
hoelzl@59664
   328
proof (rule measure_eqI)
hoelzl@59664
   329
  show "sets (measure_pmf x \<guillemotright>= A) = sets (measure_pmf x \<guillemotright>= B)"
hoelzl@59664
   330
    using assms by (subst (1 2) sets_bind) (auto simp: space_subprob_algebra)
hoelzl@59664
   331
next
hoelzl@59664
   332
  fix X assume "X \<in> sets (measure_pmf x \<guillemotright>= A)"
hoelzl@59664
   333
  then have X: "X \<in> sets N"
hoelzl@59664
   334
    using assms by (subst (asm) sets_bind) (auto simp: space_subprob_algebra)
hoelzl@59664
   335
  show "emeasure (measure_pmf x \<guillemotright>= A) X = emeasure (measure_pmf x \<guillemotright>= B) X"
hoelzl@59664
   336
    using assms
hoelzl@59664
   337
    by (subst (1 2) emeasure_bind[where N=N, OF _ _ X])
hoelzl@59664
   338
       (auto intro!: nn_integral_cong_AE simp: AE_measure_pmf_iff)
hoelzl@59664
   339
qed
hoelzl@59664
   340
hoelzl@59664
   341
lift_definition bind_pmf :: "'a pmf \<Rightarrow> ('a \<Rightarrow> 'b pmf ) \<Rightarrow> 'b pmf" is bind
hoelzl@59664
   342
proof (clarify, intro conjI)
hoelzl@59664
   343
  fix f :: "'a measure" and g :: "'a \<Rightarrow> 'b measure"
hoelzl@59664
   344
  assume "prob_space f"
hoelzl@59664
   345
  then interpret f: prob_space f .
hoelzl@59664
   346
  assume "sets f = UNIV" and ae_f: "AE x in f. measure f {x} \<noteq> 0"
hoelzl@59664
   347
  then have s_f[simp]: "sets f = sets (count_space UNIV)"
hoelzl@59664
   348
    by simp
hoelzl@59664
   349
  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)"
hoelzl@59664
   350
  then have g: "\<And>x. prob_space (g x)" and s_g[simp]: "\<And>x. sets (g x) = sets (count_space UNIV)"
hoelzl@59664
   351
    and ae_g: "\<And>x. AE y in g x. measure (g x) {y} \<noteq> 0"
hoelzl@59664
   352
    by auto
hoelzl@59664
   353
hoelzl@59664
   354
  have [measurable]: "g \<in> measurable f (subprob_algebra (count_space UNIV))"
hoelzl@59664
   355
    by (auto simp: measurable_def space_subprob_algebra prob_space_imp_subprob_space g)
hoelzl@59664
   356
    
hoelzl@59664
   357
  show "prob_space (f \<guillemotright>= g)"
hoelzl@59664
   358
    using g by (intro f.prob_space_bind[where S="count_space UNIV"]) auto
hoelzl@59664
   359
  then interpret fg: prob_space "f \<guillemotright>= g" . 
hoelzl@59664
   360
  show [simp]: "sets (f \<guillemotright>= g) = UNIV"
hoelzl@59664
   361
    using sets_eq_imp_space_eq[OF s_f]
hoelzl@59664
   362
    by (subst sets_bind[where N="count_space UNIV"]) auto
hoelzl@59664
   363
  show "AE x in f \<guillemotright>= g. measure (f \<guillemotright>= g) {x} \<noteq> 0"
hoelzl@59664
   364
    apply (simp add: fg.prob_eq_0 AE_bind[where B="count_space UNIV"])
hoelzl@59664
   365
    using ae_f
hoelzl@59664
   366
    apply eventually_elim
hoelzl@59664
   367
    using ae_g
hoelzl@59664
   368
    apply eventually_elim
hoelzl@59664
   369
    apply (auto dest: AE_measure_singleton)
hoelzl@59664
   370
    done
hoelzl@59664
   371
qed
hoelzl@59664
   372
hoelzl@59664
   373
lemma ereal_pmf_bind: "pmf (bind_pmf N f) i = (\<integral>\<^sup>+x. pmf (f x) i \<partial>measure_pmf N)"
hoelzl@59664
   374
  unfolding pmf.rep_eq bind_pmf.rep_eq
hoelzl@59664
   375
  by (auto simp: measure_pmf.measure_bind[where N="count_space UNIV"] measure_subprob measure_nonneg
hoelzl@59664
   376
           intro!: nn_integral_eq_integral[symmetric] measure_pmf.integrable_const_bound[where B=1])
hoelzl@59664
   377
hoelzl@59664
   378
lemma pmf_bind: "pmf (bind_pmf N f) i = (\<integral>x. pmf (f x) i \<partial>measure_pmf N)"
hoelzl@59664
   379
  using ereal_pmf_bind[of N f i]
hoelzl@59664
   380
  by (subst (asm) nn_integral_eq_integral)
hoelzl@59664
   381
     (auto simp: pmf_nonneg pmf_le_1
hoelzl@59664
   382
           intro!: nn_integral_eq_integral[symmetric] measure_pmf.integrable_const_bound[where B=1])
hoelzl@59664
   383
hoelzl@59664
   384
lemma bind_pmf_const[simp]: "bind_pmf M (\<lambda>x. c) = c"
hoelzl@59664
   385
  by transfer (simp add: bind_const' prob_space_imp_subprob_space)
hoelzl@59664
   386
hoelzl@59664
   387
lemma set_bind_pmf: "set_pmf (bind_pmf M N) = (\<Union>M\<in>set_pmf M. set_pmf (N M))"
hoelzl@59664
   388
  unfolding set_pmf_eq ereal_eq_0(1)[symmetric] ereal_pmf_bind  
hoelzl@59664
   389
  by (auto simp add: nn_integral_0_iff_AE AE_measure_pmf_iff set_pmf_eq not_le less_le pmf_nonneg)
hoelzl@59664
   390
hoelzl@59664
   391
lemma bind_pmf_cong:
hoelzl@59664
   392
  assumes "p = q"
hoelzl@59664
   393
  shows "(\<And>x. x \<in> set_pmf q \<Longrightarrow> f x = g x) \<Longrightarrow> bind_pmf p f = bind_pmf q g"
hoelzl@59664
   394
  unfolding `p = q`[symmetric] measure_pmf_inject[symmetric] bind_pmf.rep_eq
hoelzl@59664
   395
  by (auto simp: AE_measure_pmf_iff Pi_iff space_subprob_algebra subprob_space_measure_pmf
hoelzl@59664
   396
                 sets_bind[where N="count_space UNIV"] emeasure_bind[where N="count_space UNIV"]
hoelzl@59664
   397
           intro!: nn_integral_cong_AE measure_eqI)
hoelzl@59664
   398
hoelzl@59664
   399
lemma bind_pmf_cong_simp:
hoelzl@59664
   400
  "p = q \<Longrightarrow> (\<And>x. x \<in> set_pmf q =simp=> f x = g x) \<Longrightarrow> bind_pmf p f = bind_pmf q g"
hoelzl@59664
   401
  by (simp add: simp_implies_def cong: bind_pmf_cong)
hoelzl@59664
   402
hoelzl@59664
   403
lemma measure_pmf_bind: "measure_pmf (bind_pmf M f) = (measure_pmf M \<guillemotright>= (\<lambda>x. measure_pmf (f x)))"
hoelzl@59664
   404
  by transfer simp
hoelzl@59664
   405
hoelzl@59664
   406
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)"
hoelzl@59664
   407
  using measurable_measure_pmf[of N]
hoelzl@59664
   408
  unfolding measure_pmf_bind
hoelzl@59664
   409
  apply (subst (1 3) nn_integral_max_0[symmetric])
hoelzl@59664
   410
  apply (intro nn_integral_bind[where B="count_space UNIV"])
hoelzl@59664
   411
  apply auto
hoelzl@59664
   412
  done
hoelzl@59664
   413
hoelzl@59664
   414
lemma emeasure_bind_pmf[simp]: "emeasure (bind_pmf M N) X = (\<integral>\<^sup>+x. emeasure (N x) X \<partial>M)"
hoelzl@59664
   415
  using measurable_measure_pmf[of N]
hoelzl@59664
   416
  unfolding measure_pmf_bind
hoelzl@59664
   417
  by (subst emeasure_bind[where N="count_space UNIV"]) auto
hoelzl@59664
   418
                                
hoelzl@59664
   419
lift_definition return_pmf :: "'a \<Rightarrow> 'a pmf" is "return (count_space UNIV)"
hoelzl@59664
   420
  by (auto intro!: prob_space_return simp: AE_return measure_return)
hoelzl@59664
   421
hoelzl@59664
   422
lemma bind_return_pmf: "bind_pmf (return_pmf x) f = f x"
hoelzl@59664
   423
  by transfer
hoelzl@59664
   424
     (auto intro!: prob_space_imp_subprob_space bind_return[where N="count_space UNIV"]
hoelzl@59664
   425
           simp: space_subprob_algebra)
hoelzl@59664
   426
hoelzl@59664
   427
lemma set_return_pmf: "set_pmf (return_pmf x) = {x}"
hoelzl@59664
   428
  by transfer (auto simp add: measure_return split: split_indicator)
hoelzl@59664
   429
hoelzl@59664
   430
lemma bind_return_pmf': "bind_pmf N return_pmf = N"
hoelzl@59664
   431
proof (transfer, clarify)
hoelzl@59664
   432
  fix N :: "'a measure" assume "sets N = UNIV" then show "N \<guillemotright>= return (count_space UNIV) = N"
hoelzl@59664
   433
    by (subst return_sets_cong[where N=N]) (simp_all add: bind_return')
hoelzl@59664
   434
qed
hoelzl@59664
   435
hoelzl@59664
   436
lemma bind_assoc_pmf: "bind_pmf (bind_pmf A B) C = bind_pmf A (\<lambda>x. bind_pmf (B x) C)"
hoelzl@59664
   437
  by transfer
hoelzl@59664
   438
     (auto intro!: bind_assoc[where N="count_space UNIV" and R="count_space UNIV"]
hoelzl@59664
   439
           simp: measurable_def space_subprob_algebra prob_space_imp_subprob_space)
hoelzl@59664
   440
hoelzl@59664
   441
definition "map_pmf f M = bind_pmf M (\<lambda>x. return_pmf (f x))"
hoelzl@59664
   442
hoelzl@59664
   443
lemma map_bind_pmf: "map_pmf f (bind_pmf M g) = bind_pmf M (\<lambda>x. map_pmf f (g x))"
hoelzl@59664
   444
  by (simp add: map_pmf_def bind_assoc_pmf)
hoelzl@59664
   445
hoelzl@59664
   446
lemma bind_map_pmf: "bind_pmf (map_pmf f M) g = bind_pmf M (\<lambda>x. g (f x))"
hoelzl@59664
   447
  by (simp add: map_pmf_def bind_assoc_pmf bind_return_pmf)
hoelzl@59664
   448
hoelzl@59664
   449
lemma map_pmf_transfer[transfer_rule]:
hoelzl@59664
   450
  "rel_fun op = (rel_fun cr_pmf cr_pmf) (\<lambda>f M. distr M (count_space UNIV) f) map_pmf"
hoelzl@59664
   451
proof -
hoelzl@59664
   452
  have "rel_fun op = (rel_fun pmf_as_measure.cr_pmf pmf_as_measure.cr_pmf)
hoelzl@59664
   453
     (\<lambda>f M. M \<guillemotright>= (return (count_space UNIV) o f)) map_pmf"
hoelzl@59664
   454
    unfolding map_pmf_def[abs_def] comp_def by transfer_prover 
hoelzl@59664
   455
  then show ?thesis
hoelzl@59664
   456
    by (force simp: rel_fun_def cr_pmf_def bind_return_distr)
hoelzl@59664
   457
qed
hoelzl@59664
   458
hoelzl@59664
   459
lemma map_pmf_rep_eq:
hoelzl@59664
   460
  "measure_pmf (map_pmf f M) = distr (measure_pmf M) (count_space UNIV) f"
hoelzl@59664
   461
  unfolding map_pmf_def bind_pmf.rep_eq comp_def return_pmf.rep_eq
hoelzl@59664
   462
  using bind_return_distr[of M f "count_space UNIV"] by (simp add: comp_def)
hoelzl@59664
   463
hoelzl@58587
   464
lemma map_pmf_id[simp]: "map_pmf id = id"
hoelzl@58587
   465
  by (rule, transfer) (auto simp: emeasure_distr measurable_def intro!: measure_eqI)
hoelzl@58587
   466
hoelzl@59053
   467
lemma map_pmf_ident[simp]: "map_pmf (\<lambda>x. x) = (\<lambda>x. x)"
hoelzl@59053
   468
  using map_pmf_id unfolding id_def .
hoelzl@59053
   469
hoelzl@58587
   470
lemma map_pmf_compose: "map_pmf (f \<circ> g) = map_pmf f \<circ> map_pmf g"
hoelzl@58587
   471
  by (rule, transfer) (simp add: distr_distr[symmetric, where N="count_space UNIV"] measurable_def) 
hoelzl@58587
   472
hoelzl@59000
   473
lemma map_pmf_comp: "map_pmf f (map_pmf g M) = map_pmf (\<lambda>x. f (g x)) M"
hoelzl@59000
   474
  using map_pmf_compose[of f g] by (simp add: comp_def)
hoelzl@59000
   475
hoelzl@59664
   476
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"
hoelzl@59664
   477
  unfolding map_pmf_def by (rule bind_pmf_cong) auto
hoelzl@59664
   478
hoelzl@59664
   479
lemma pmf_set_map: "set_pmf \<circ> map_pmf f = op ` f \<circ> set_pmf"
hoelzl@59664
   480
  by (auto simp add: comp_def fun_eq_iff map_pmf_def set_bind_pmf set_return_pmf)
hoelzl@59664
   481
hoelzl@59664
   482
lemma set_map_pmf: "set_pmf (map_pmf f M) = f`set_pmf M"
hoelzl@59664
   483
  using pmf_set_map[of f] by (auto simp: comp_def fun_eq_iff)
hoelzl@58587
   484
hoelzl@59002
   485
lemma emeasure_map_pmf[simp]: "emeasure (map_pmf f M) X = emeasure M (f -` X)"
hoelzl@59664
   486
  unfolding map_pmf_rep_eq by (subst emeasure_distr) auto
hoelzl@59002
   487
hoelzl@59002
   488
lemma nn_integral_map_pmf[simp]: "(\<integral>\<^sup>+x. f x \<partial>map_pmf g M) = (\<integral>\<^sup>+x. f (g x) \<partial>M)"
hoelzl@59664
   489
  unfolding map_pmf_rep_eq by (intro nn_integral_distr) auto
hoelzl@59002
   490
Andreas@59023
   491
lemma ereal_pmf_map: "pmf (map_pmf f p) x = (\<integral>\<^sup>+ y. indicator (f -` {x}) y \<partial>measure_pmf p)"
hoelzl@59664
   492
proof (transfer fixing: f x)
Andreas@59023
   493
  fix p :: "'b measure"
Andreas@59023
   494
  presume "prob_space p"
Andreas@59023
   495
  then interpret prob_space p .
Andreas@59023
   496
  presume "sets p = UNIV"
Andreas@59023
   497
  then show "ereal (measure (distr p (count_space UNIV) f) {x}) = integral\<^sup>N p (indicator (f -` {x}))"
Andreas@59023
   498
    by(simp add: measure_distr measurable_def emeasure_eq_measure)
Andreas@59023
   499
qed simp_all
Andreas@59023
   500
Andreas@59023
   501
lemma nn_integral_pmf: "(\<integral>\<^sup>+ x. pmf p x \<partial>count_space A) = emeasure (measure_pmf p) A"
Andreas@59023
   502
proof -
Andreas@59023
   503
  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))"
Andreas@59023
   504
    by(auto simp add: nn_integral_count_space_indicator indicator_def set_pmf_iff intro: nn_integral_cong)
Andreas@59023
   505
  also have "\<dots> = emeasure (measure_pmf p) (\<Union>x\<in>A \<inter> set_pmf p. {x})"
Andreas@59023
   506
    by(subst emeasure_UN_countable)(auto simp add: emeasure_pmf_single disjoint_family_on_def)
Andreas@59023
   507
  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})"
Andreas@59023
   508
    by(rule emeasure_Un_null_set[symmetric])(auto intro: in_null_sets_measure_pmfI)
Andreas@59023
   509
  also have "\<dots> = emeasure (measure_pmf p) A"
Andreas@59023
   510
    by(auto intro: arg_cong2[where f=emeasure])
Andreas@59023
   511
  finally show ?thesis .
Andreas@59023
   512
qed
Andreas@59023
   513
hoelzl@59664
   514
lemma map_return_pmf: "map_pmf f (return_pmf x) = return_pmf (f x)"
hoelzl@59664
   515
  by transfer (simp add: distr_return)
hoelzl@59664
   516
hoelzl@59664
   517
lemma map_pmf_const[simp]: "map_pmf (\<lambda>_. c) M = return_pmf c"
hoelzl@59664
   518
  by transfer (auto simp: prob_space.distr_const)
hoelzl@59664
   519
hoelzl@59664
   520
lemma pmf_return: "pmf (return_pmf x) y = indicator {y} x"
hoelzl@59664
   521
  by transfer (simp add: measure_return)
hoelzl@59664
   522
hoelzl@59664
   523
lemma nn_integral_return_pmf[simp]: "0 \<le> f x \<Longrightarrow> (\<integral>\<^sup>+x. f x \<partial>return_pmf x) = f x"
hoelzl@59664
   524
  unfolding return_pmf.rep_eq by (intro nn_integral_return) auto
hoelzl@59664
   525
hoelzl@59664
   526
lemma emeasure_return_pmf[simp]: "emeasure (return_pmf x) X = indicator X x"
hoelzl@59664
   527
  unfolding return_pmf.rep_eq by (intro emeasure_return) auto
hoelzl@59664
   528
hoelzl@59664
   529
lemma return_pmf_inj[simp]: "return_pmf x = return_pmf y \<longleftrightarrow> x = y"
hoelzl@59664
   530
  by (metis insertI1 set_return_pmf singletonD)
hoelzl@59664
   531
hoelzl@59664
   532
definition "pair_pmf A B = bind_pmf A (\<lambda>x. bind_pmf B (\<lambda>y. return_pmf (x, y)))"
hoelzl@59664
   533
hoelzl@59664
   534
lemma pmf_pair: "pmf (pair_pmf M N) (a, b) = pmf M a * pmf N b"
hoelzl@59664
   535
  unfolding pair_pmf_def pmf_bind pmf_return
hoelzl@59664
   536
  apply (subst integral_measure_pmf[where A="{b}"])
hoelzl@59664
   537
  apply (auto simp: indicator_eq_0_iff)
hoelzl@59664
   538
  apply (subst integral_measure_pmf[where A="{a}"])
hoelzl@59664
   539
  apply (auto simp: indicator_eq_0_iff setsum_nonneg_eq_0_iff pmf_nonneg)
hoelzl@59664
   540
  done
hoelzl@59664
   541
hoelzl@59664
   542
lemma set_pair_pmf: "set_pmf (pair_pmf A B) = set_pmf A \<times> set_pmf B"
hoelzl@59664
   543
  unfolding pair_pmf_def set_bind_pmf set_return_pmf by auto
hoelzl@59664
   544
hoelzl@59664
   545
lemma measure_pmf_in_subprob_space[measurable (raw)]:
hoelzl@59664
   546
  "measure_pmf M \<in> space (subprob_algebra (count_space UNIV))"
hoelzl@59664
   547
  by (simp add: space_subprob_algebra) intro_locales
hoelzl@59664
   548
hoelzl@59664
   549
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)"
hoelzl@59664
   550
proof -
hoelzl@59664
   551
  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)"
hoelzl@59664
   552
    by (subst nn_integral_max_0[symmetric])
hoelzl@59664
   553
       (auto simp: AE_measure_pmf_iff set_pair_pmf intro!: nn_integral_cong_AE)
hoelzl@59664
   554
  also have "\<dots> = (\<integral>\<^sup>+a. \<integral>\<^sup>+b. max 0 (f (a, b)) * indicator (A \<times> B) (a, b) \<partial>B \<partial>A)"
hoelzl@59664
   555
    by (simp add: pair_pmf_def)
hoelzl@59664
   556
  also have "\<dots> = (\<integral>\<^sup>+a. \<integral>\<^sup>+b. max 0 (f (a, b)) \<partial>B \<partial>A)"
hoelzl@59664
   557
    by (auto intro!: nn_integral_cong_AE simp: AE_measure_pmf_iff)
hoelzl@59664
   558
  finally show ?thesis
hoelzl@59664
   559
    unfolding nn_integral_max_0 .
hoelzl@59664
   560
qed
hoelzl@59664
   561
hoelzl@59664
   562
lemma bind_pair_pmf:
hoelzl@59664
   563
  assumes M[measurable]: "M \<in> measurable (count_space UNIV \<Otimes>\<^sub>M count_space UNIV) (subprob_algebra N)"
hoelzl@59664
   564
  shows "measure_pmf (pair_pmf A B) \<guillemotright>= M = (measure_pmf A \<guillemotright>= (\<lambda>x. measure_pmf B \<guillemotright>= (\<lambda>y. M (x, y))))"
hoelzl@59664
   565
    (is "?L = ?R")
hoelzl@59664
   566
proof (rule measure_eqI)
hoelzl@59664
   567
  have M'[measurable]: "M \<in> measurable (pair_pmf A B) (subprob_algebra N)"
hoelzl@59664
   568
    using M[THEN measurable_space] by (simp_all add: space_pair_measure)
hoelzl@59664
   569
hoelzl@59664
   570
  note measurable_bind[where N="count_space UNIV", measurable]
hoelzl@59664
   571
  note measure_pmf_in_subprob_space[simp]
hoelzl@59664
   572
hoelzl@59664
   573
  have sets_eq_N: "sets ?L = N"
hoelzl@59664
   574
    by (subst sets_bind[OF sets_kernel[OF M']]) auto
hoelzl@59664
   575
  show "sets ?L = sets ?R"
hoelzl@59664
   576
    using measurable_space[OF M]
hoelzl@59664
   577
    by (simp add: sets_eq_N space_pair_measure space_subprob_algebra)
hoelzl@59664
   578
  fix X assume "X \<in> sets ?L"
hoelzl@59664
   579
  then have X[measurable]: "X \<in> sets N"
hoelzl@59664
   580
    unfolding sets_eq_N .
hoelzl@59664
   581
  then show "emeasure ?L X = emeasure ?R X"
hoelzl@59664
   582
    apply (simp add: emeasure_bind[OF _ M' X])
hoelzl@59664
   583
    apply (simp add: nn_integral_bind[where B="count_space UNIV"] pair_pmf_def measure_pmf_bind[of A]
hoelzl@59664
   584
      nn_integral_measure_pmf_finite set_return_pmf emeasure_nonneg pmf_return one_ereal_def[symmetric])
hoelzl@59664
   585
    apply (subst emeasure_bind[OF _ _ X])
hoelzl@59664
   586
    apply measurable
hoelzl@59664
   587
    apply (subst emeasure_bind[OF _ _ X])
hoelzl@59664
   588
    apply measurable
hoelzl@59664
   589
    done
hoelzl@59664
   590
qed
hoelzl@59664
   591
hoelzl@59664
   592
lemma map_fst_pair_pmf: "map_pmf fst (pair_pmf A B) = A"
hoelzl@59664
   593
  by (simp add: pair_pmf_def map_pmf_def bind_assoc_pmf bind_return_pmf bind_return_pmf')
hoelzl@59664
   594
hoelzl@59664
   595
lemma map_snd_pair_pmf: "map_pmf snd (pair_pmf A B) = B"
hoelzl@59664
   596
  by (simp add: pair_pmf_def map_pmf_def bind_assoc_pmf bind_return_pmf bind_return_pmf')
hoelzl@59664
   597
hoelzl@59664
   598
lemma nn_integral_pmf':
hoelzl@59664
   599
  "inj_on f A \<Longrightarrow> (\<integral>\<^sup>+x. pmf p (f x) \<partial>count_space A) = emeasure p (f ` A)"
hoelzl@59664
   600
  by (subst nn_integral_bij_count_space[where g=f and B="f`A"])
hoelzl@59664
   601
     (auto simp: bij_betw_def nn_integral_pmf)
hoelzl@59664
   602
hoelzl@59664
   603
lemma pmf_le_0_iff[simp]: "pmf M p \<le> 0 \<longleftrightarrow> pmf M p = 0"
hoelzl@59664
   604
  using pmf_nonneg[of M p] by simp
hoelzl@59664
   605
hoelzl@59664
   606
lemma min_pmf_0[simp]: "min (pmf M p) 0 = 0" "min 0 (pmf M p) = 0"
hoelzl@59664
   607
  using pmf_nonneg[of M p] by simp_all
hoelzl@59664
   608
hoelzl@59664
   609
lemma pmf_eq_0_set_pmf: "pmf M p = 0 \<longleftrightarrow> p \<notin> set_pmf M"
hoelzl@59664
   610
  unfolding set_pmf_iff by simp
hoelzl@59664
   611
hoelzl@59664
   612
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"
hoelzl@59664
   613
  by (auto simp: pmf.rep_eq map_pmf_rep_eq measure_distr AE_measure_pmf_iff inj_onD
hoelzl@59664
   614
           intro!: measure_pmf.finite_measure_eq_AE)
hoelzl@59664
   615
hoelzl@59664
   616
subsection \<open> PMFs as function \<close>
hoelzl@59000
   617
hoelzl@58587
   618
context
hoelzl@58587
   619
  fixes f :: "'a \<Rightarrow> real"
hoelzl@58587
   620
  assumes nonneg: "\<And>x. 0 \<le> f x"
hoelzl@58587
   621
  assumes prob: "(\<integral>\<^sup>+x. f x \<partial>count_space UNIV) = 1"
hoelzl@58587
   622
begin
hoelzl@58587
   623
hoelzl@58587
   624
lift_definition embed_pmf :: "'a pmf" is "density (count_space UNIV) (ereal \<circ> f)"
hoelzl@58587
   625
proof (intro conjI)
hoelzl@58587
   626
  have *[simp]: "\<And>x y. ereal (f y) * indicator {x} y = ereal (f x) * indicator {x} y"
hoelzl@58587
   627
    by (simp split: split_indicator)
hoelzl@58587
   628
  show "AE x in density (count_space UNIV) (ereal \<circ> f).
hoelzl@58587
   629
    measure (density (count_space UNIV) (ereal \<circ> f)) {x} \<noteq> 0"
hoelzl@59092
   630
    by (simp add: AE_density nonneg measure_def emeasure_density max_def)
hoelzl@58587
   631
  show "prob_space (density (count_space UNIV) (ereal \<circ> f))"
hoelzl@58587
   632
    by default (simp add: emeasure_density prob)
hoelzl@58587
   633
qed simp
hoelzl@58587
   634
hoelzl@58587
   635
lemma pmf_embed_pmf: "pmf embed_pmf x = f x"
hoelzl@58587
   636
proof transfer
hoelzl@58587
   637
  have *[simp]: "\<And>x y. ereal (f y) * indicator {x} y = ereal (f x) * indicator {x} y"
hoelzl@58587
   638
    by (simp split: split_indicator)
hoelzl@58587
   639
  fix x show "measure (density (count_space UNIV) (ereal \<circ> f)) {x} = f x"
hoelzl@59092
   640
    by transfer (simp add: measure_def emeasure_density nonneg max_def)
hoelzl@58587
   641
qed
hoelzl@58587
   642
hoelzl@58587
   643
end
hoelzl@58587
   644
hoelzl@58587
   645
lemma embed_pmf_transfer:
hoelzl@58730
   646
  "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"
hoelzl@58587
   647
  by (auto simp: rel_fun_def eq_onp_def embed_pmf.transfer)
hoelzl@58587
   648
hoelzl@59000
   649
lemma measure_pmf_eq_density: "measure_pmf p = density (count_space UNIV) (pmf p)"
hoelzl@59000
   650
proof (transfer, elim conjE)
hoelzl@59000
   651
  fix M :: "'a measure" assume [simp]: "sets M = UNIV" and ae: "AE x in M. measure M {x} \<noteq> 0"
hoelzl@59000
   652
  assume "prob_space M" then interpret prob_space M .
hoelzl@59000
   653
  show "M = density (count_space UNIV) (\<lambda>x. ereal (measure M {x}))"
hoelzl@59000
   654
  proof (rule measure_eqI)
hoelzl@59000
   655
    fix A :: "'a set"
hoelzl@59000
   656
    have "(\<integral>\<^sup>+ x. ereal (measure M {x}) * indicator A x \<partial>count_space UNIV) = 
hoelzl@59000
   657
      (\<integral>\<^sup>+ x. emeasure M {x} * indicator (A \<inter> {x. measure M {x} \<noteq> 0}) x \<partial>count_space UNIV)"
hoelzl@59000
   658
      by (auto intro!: nn_integral_cong simp: emeasure_eq_measure split: split_indicator)
hoelzl@59000
   659
    also have "\<dots> = (\<integral>\<^sup>+ x. emeasure M {x} \<partial>count_space (A \<inter> {x. measure M {x} \<noteq> 0}))"
hoelzl@59000
   660
      by (subst nn_integral_restrict_space[symmetric]) (auto simp: restrict_count_space)
hoelzl@59000
   661
    also have "\<dots> = emeasure M (\<Union>x\<in>(A \<inter> {x. measure M {x} \<noteq> 0}). {x})"
hoelzl@59000
   662
      by (intro emeasure_UN_countable[symmetric] countable_Int2 countable_support)
hoelzl@59000
   663
         (auto simp: disjoint_family_on_def)
hoelzl@59000
   664
    also have "\<dots> = emeasure M A"
hoelzl@59000
   665
      using ae by (intro emeasure_eq_AE) auto
hoelzl@59000
   666
    finally show " emeasure M A = emeasure (density (count_space UNIV) (\<lambda>x. ereal (measure M {x}))) A"
hoelzl@59000
   667
      using emeasure_space_1 by (simp add: emeasure_density)
hoelzl@59000
   668
  qed simp
hoelzl@59000
   669
qed
hoelzl@59000
   670
hoelzl@58587
   671
lemma td_pmf_embed_pmf:
hoelzl@58587
   672
  "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}"
hoelzl@58587
   673
  unfolding type_definition_def
hoelzl@58587
   674
proof safe
hoelzl@58587
   675
  fix p :: "'a pmf"
hoelzl@58587
   676
  have "(\<integral>\<^sup>+ x. 1 \<partial>measure_pmf p) = 1"
hoelzl@58587
   677
    using measure_pmf.emeasure_space_1[of p] by simp
hoelzl@58587
   678
  then show *: "(\<integral>\<^sup>+ x. ereal (pmf p x) \<partial>count_space UNIV) = 1"
hoelzl@58587
   679
    by (simp add: measure_pmf_eq_density nn_integral_density pmf_nonneg del: nn_integral_const)
hoelzl@58587
   680
hoelzl@58587
   681
  show "embed_pmf (pmf p) = p"
hoelzl@58587
   682
    by (intro measure_pmf_inject[THEN iffD1])
hoelzl@58587
   683
       (simp add: * embed_pmf.rep_eq pmf_nonneg measure_pmf_eq_density[of p] comp_def)
hoelzl@58587
   684
next
hoelzl@58587
   685
  fix f :: "'a \<Rightarrow> real" assume "\<forall>x. 0 \<le> f x" "(\<integral>\<^sup>+x. f x \<partial>count_space UNIV) = 1"
hoelzl@58587
   686
  then show "pmf (embed_pmf f) = f"
hoelzl@58587
   687
    by (auto intro!: pmf_embed_pmf)
hoelzl@58587
   688
qed (rule pmf_nonneg)
hoelzl@58587
   689
hoelzl@58587
   690
end
hoelzl@58587
   691
hoelzl@58587
   692
locale pmf_as_function
hoelzl@58587
   693
begin
hoelzl@58587
   694
hoelzl@58587
   695
setup_lifting td_pmf_embed_pmf
hoelzl@58587
   696
hoelzl@58730
   697
lemma set_pmf_transfer[transfer_rule]: 
hoelzl@58730
   698
  assumes "bi_total A"
hoelzl@58730
   699
  shows "rel_fun (pcr_pmf A) (rel_set A) (\<lambda>f. {x. f x \<noteq> 0}) set_pmf"  
hoelzl@58730
   700
  using `bi_total A`
hoelzl@58730
   701
  by (auto simp: pcr_pmf_def cr_pmf_def rel_fun_def rel_set_def bi_total_def Bex_def set_pmf_iff)
hoelzl@58730
   702
     metis+
hoelzl@58730
   703
hoelzl@59000
   704
end
hoelzl@59000
   705
hoelzl@59000
   706
context
hoelzl@59000
   707
begin
hoelzl@59000
   708
hoelzl@59000
   709
interpretation pmf_as_function .
hoelzl@59000
   710
hoelzl@59000
   711
lemma pmf_eqI: "(\<And>i. pmf M i = pmf N i) \<Longrightarrow> M = N"
hoelzl@59000
   712
  by transfer auto
hoelzl@59000
   713
hoelzl@59000
   714
lemma pmf_eq_iff: "M = N \<longleftrightarrow> (\<forall>i. pmf M i = pmf N i)"
hoelzl@59000
   715
  by (auto intro: pmf_eqI)
hoelzl@59000
   716
hoelzl@59664
   717
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))"
hoelzl@59664
   718
  unfolding pmf_eq_iff pmf_bind
hoelzl@59664
   719
proof
hoelzl@59664
   720
  fix i
hoelzl@59664
   721
  interpret B: prob_space "restrict_space B B"
hoelzl@59664
   722
    by (intro prob_space_restrict_space measure_pmf.emeasure_eq_1_AE)
hoelzl@59664
   723
       (auto simp: AE_measure_pmf_iff)
hoelzl@59664
   724
  interpret A: prob_space "restrict_space A A"
hoelzl@59664
   725
    by (intro prob_space_restrict_space measure_pmf.emeasure_eq_1_AE)
hoelzl@59664
   726
       (auto simp: AE_measure_pmf_iff)
hoelzl@59664
   727
hoelzl@59664
   728
  interpret AB: pair_prob_space "restrict_space A A" "restrict_space B B"
hoelzl@59664
   729
    by unfold_locales
hoelzl@59664
   730
hoelzl@59664
   731
  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)"
hoelzl@59664
   732
    by (rule integral_cong) (auto intro!: integral_pmf_restrict)
hoelzl@59664
   733
  also have "\<dots> = (\<integral> x. (\<integral> y. pmf (C x y) i \<partial>restrict_space B B) \<partial>restrict_space A A)"
hoelzl@59664
   734
    by (intro integral_pmf_restrict B.borel_measurable_lebesgue_integral measurable_pair_restrict_pmf2
hoelzl@59664
   735
              countable_set_pmf borel_measurable_count_space)
hoelzl@59664
   736
  also have "\<dots> = (\<integral> y. \<integral> x. pmf (C x y) i \<partial>restrict_space A A \<partial>restrict_space B B)"
hoelzl@59664
   737
    by (rule AB.Fubini_integral[symmetric])
hoelzl@59664
   738
       (auto intro!: AB.integrable_const_bound[where B=1] measurable_pair_restrict_pmf2
hoelzl@59664
   739
             simp: pmf_nonneg pmf_le_1 measurable_restrict_space1)
hoelzl@59664
   740
  also have "\<dots> = (\<integral> y. \<integral> x. pmf (C x y) i \<partial>restrict_space A A \<partial>B)"
hoelzl@59664
   741
    by (intro integral_pmf_restrict[symmetric] A.borel_measurable_lebesgue_integral measurable_pair_restrict_pmf2
hoelzl@59664
   742
              countable_set_pmf borel_measurable_count_space)
hoelzl@59664
   743
  also have "\<dots> = (\<integral> y. \<integral> x. pmf (C x y) i \<partial>A \<partial>B)"
hoelzl@59664
   744
    by (rule integral_cong) (auto intro!: integral_pmf_restrict[symmetric])
hoelzl@59664
   745
  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)" .
hoelzl@59664
   746
qed
hoelzl@59664
   747
hoelzl@59664
   748
lemma pair_map_pmf1: "pair_pmf (map_pmf f A) B = map_pmf (apfst f) (pair_pmf A B)"
hoelzl@59664
   749
proof (safe intro!: pmf_eqI)
hoelzl@59664
   750
  fix a :: "'a" and b :: "'b"
hoelzl@59664
   751
  have [simp]: "\<And>c d. indicator (apfst f -` {(a, b)}) (c, d) = indicator (f -` {a}) c * (indicator {b} d::ereal)"
hoelzl@59664
   752
    by (auto split: split_indicator)
hoelzl@59664
   753
hoelzl@59664
   754
  have "ereal (pmf (pair_pmf (map_pmf f A) B) (a, b)) =
hoelzl@59664
   755
         ereal (pmf (map_pmf (apfst f) (pair_pmf A B)) (a, b))"
hoelzl@59664
   756
    unfolding pmf_pair ereal_pmf_map
hoelzl@59664
   757
    by (simp add: nn_integral_pair_pmf' max_def emeasure_pmf_single nn_integral_multc pmf_nonneg
hoelzl@59664
   758
                  emeasure_map_pmf[symmetric] del: emeasure_map_pmf)
hoelzl@59664
   759
  then show "pmf (pair_pmf (map_pmf f A) B) (a, b) = pmf (map_pmf (apfst f) (pair_pmf A B)) (a, b)"
hoelzl@59664
   760
    by simp
hoelzl@59664
   761
qed
hoelzl@59664
   762
hoelzl@59664
   763
lemma pair_map_pmf2: "pair_pmf A (map_pmf f B) = map_pmf (apsnd f) (pair_pmf A B)"
hoelzl@59664
   764
proof (safe intro!: pmf_eqI)
hoelzl@59664
   765
  fix a :: "'a" and b :: "'b"
hoelzl@59664
   766
  have [simp]: "\<And>c d. indicator (apsnd f -` {(a, b)}) (c, d) = indicator {a} c * (indicator (f -` {b}) d::ereal)"
hoelzl@59664
   767
    by (auto split: split_indicator)
hoelzl@59664
   768
hoelzl@59664
   769
  have "ereal (pmf (pair_pmf A (map_pmf f B)) (a, b)) =
hoelzl@59664
   770
         ereal (pmf (map_pmf (apsnd f) (pair_pmf A B)) (a, b))"
hoelzl@59664
   771
    unfolding pmf_pair ereal_pmf_map
hoelzl@59664
   772
    by (simp add: nn_integral_pair_pmf' max_def emeasure_pmf_single nn_integral_cmult nn_integral_multc pmf_nonneg
hoelzl@59664
   773
                  emeasure_map_pmf[symmetric] del: emeasure_map_pmf)
hoelzl@59664
   774
  then show "pmf (pair_pmf A (map_pmf f B)) (a, b) = pmf (map_pmf (apsnd f) (pair_pmf A B)) (a, b)"
hoelzl@59664
   775
    by simp
hoelzl@59664
   776
qed
hoelzl@59664
   777
hoelzl@59664
   778
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)"
hoelzl@59664
   779
  by (simp add: pair_map_pmf2 pair_map_pmf1 map_pmf_comp split_beta')
hoelzl@59664
   780
hoelzl@59000
   781
end
hoelzl@59000
   782
hoelzl@59664
   783
subsection \<open> Conditional Probabilities \<close>
hoelzl@59664
   784
hoelzl@59664
   785
context
hoelzl@59664
   786
  fixes p :: "'a pmf" and s :: "'a set"
hoelzl@59664
   787
  assumes not_empty: "set_pmf p \<inter> s \<noteq> {}"
hoelzl@59664
   788
begin
hoelzl@59664
   789
hoelzl@59664
   790
interpretation pmf_as_measure .
hoelzl@59664
   791
hoelzl@59664
   792
lemma emeasure_measure_pmf_not_zero: "emeasure (measure_pmf p) s \<noteq> 0"
hoelzl@59664
   793
proof
hoelzl@59664
   794
  assume "emeasure (measure_pmf p) s = 0"
hoelzl@59664
   795
  then have "AE x in measure_pmf p. x \<notin> s"
hoelzl@59664
   796
    by (rule AE_I[rotated]) auto
hoelzl@59664
   797
  with not_empty show False
hoelzl@59664
   798
    by (auto simp: AE_measure_pmf_iff)
hoelzl@59664
   799
qed
hoelzl@59664
   800
hoelzl@59664
   801
lemma measure_measure_pmf_not_zero: "measure (measure_pmf p) s \<noteq> 0"
hoelzl@59664
   802
  using emeasure_measure_pmf_not_zero unfolding measure_pmf.emeasure_eq_measure by simp
hoelzl@59664
   803
hoelzl@59664
   804
lift_definition cond_pmf :: "'a pmf" is
hoelzl@59664
   805
  "uniform_measure (measure_pmf p) s"
hoelzl@59664
   806
proof (intro conjI)
hoelzl@59664
   807
  show "prob_space (uniform_measure (measure_pmf p) s)"
hoelzl@59664
   808
    by (intro prob_space_uniform_measure) (auto simp: emeasure_measure_pmf_not_zero)
hoelzl@59664
   809
  show "AE x in uniform_measure (measure_pmf p) s. measure (uniform_measure (measure_pmf p) s) {x} \<noteq> 0"
hoelzl@59664
   810
    by (simp add: emeasure_measure_pmf_not_zero measure_measure_pmf_not_zero AE_uniform_measure
hoelzl@59664
   811
                  AE_measure_pmf_iff set_pmf.rep_eq)
hoelzl@59664
   812
qed simp
hoelzl@59664
   813
hoelzl@59664
   814
lemma pmf_cond: "pmf cond_pmf x = (if x \<in> s then pmf p x / measure p s else 0)"
hoelzl@59664
   815
  by transfer (simp add: emeasure_measure_pmf_not_zero pmf.rep_eq)
hoelzl@59664
   816
hoelzl@59664
   817
lemma set_cond_pmf: "set_pmf cond_pmf = set_pmf p \<inter> s"
hoelzl@59664
   818
  by (auto simp add: set_pmf_iff pmf_cond measure_measure_pmf_not_zero split: split_if_asm)
hoelzl@59664
   819
hoelzl@59664
   820
end
hoelzl@59664
   821
hoelzl@59664
   822
lemma cond_map_pmf:
hoelzl@59664
   823
  assumes "set_pmf p \<inter> f -` s \<noteq> {}"
hoelzl@59664
   824
  shows "cond_pmf (map_pmf f p) s = map_pmf f (cond_pmf p (f -` s))"
hoelzl@59664
   825
proof -
hoelzl@59664
   826
  have *: "set_pmf (map_pmf f p) \<inter> s \<noteq> {}"
hoelzl@59664
   827
    using assms by (simp add: set_map_pmf) auto
hoelzl@59664
   828
  { fix x
hoelzl@59664
   829
    have "ereal (pmf (map_pmf f (cond_pmf p (f -` s))) x) =
hoelzl@59664
   830
      emeasure p (f -` s \<inter> f -` {x}) / emeasure p (f -` s)"
hoelzl@59664
   831
      unfolding ereal_pmf_map cond_pmf.rep_eq[OF assms] by (simp add: nn_integral_uniform_measure)
hoelzl@59664
   832
    also have "f -` s \<inter> f -` {x} = (if x \<in> s then f -` {x} else {})"
hoelzl@59664
   833
      by auto
hoelzl@59664
   834
    also have "emeasure p (if x \<in> s then f -` {x} else {}) / emeasure p (f -` s) =
hoelzl@59664
   835
      ereal (pmf (cond_pmf (map_pmf f p) s) x)"
hoelzl@59664
   836
      using measure_measure_pmf_not_zero[OF *]
hoelzl@59664
   837
      by (simp add: pmf_cond[OF *] ereal_divide' ereal_pmf_map measure_pmf.emeasure_eq_measure[symmetric]
hoelzl@59664
   838
               del: ereal_divide)
hoelzl@59664
   839
    finally have "ereal (pmf (cond_pmf (map_pmf f p) s) x) = ereal (pmf (map_pmf f (cond_pmf p (f -` s))) x)"
hoelzl@59664
   840
      by simp }
hoelzl@59664
   841
  then show ?thesis
hoelzl@59664
   842
    by (intro pmf_eqI) simp
hoelzl@59664
   843
qed
hoelzl@59664
   844
hoelzl@59664
   845
lemma bind_cond_pmf_cancel:
hoelzl@59664
   846
  assumes in_S: "\<And>x. x \<in> set_pmf p \<Longrightarrow> x \<in> S x" "\<And>x. x \<in> set_pmf q \<Longrightarrow> x \<in> S x"
hoelzl@59664
   847
  assumes S_eq: "\<And>x y. x \<in> S y \<Longrightarrow> S x = S y"
hoelzl@59664
   848
  and same: "\<And>x. measure (measure_pmf p) (S x) = measure (measure_pmf q) (S x)"
hoelzl@59664
   849
  shows "bind_pmf p (\<lambda>x. cond_pmf q (S x)) = q" (is "?lhs = _")
hoelzl@59664
   850
proof (rule pmf_eqI)
hoelzl@59664
   851
  { fix x
hoelzl@59664
   852
    assume "x \<in> set_pmf p"
hoelzl@59664
   853
    hence "set_pmf p \<inter> (S x) \<noteq> {}" using in_S by auto
hoelzl@59664
   854
    hence "measure (measure_pmf p) (S x) \<noteq> 0"
hoelzl@59664
   855
      by(auto simp add: measure_pmf.prob_eq_0 AE_measure_pmf_iff)
hoelzl@59664
   856
    with same have "measure (measure_pmf q) (S x) \<noteq> 0" by simp
hoelzl@59664
   857
    hence "set_pmf q \<inter> S x \<noteq> {}"
hoelzl@59664
   858
      by(auto simp add: measure_pmf.prob_eq_0 AE_measure_pmf_iff) }
hoelzl@59664
   859
  note [simp] = this
hoelzl@59664
   860
hoelzl@59664
   861
  fix z
hoelzl@59664
   862
  have pmf_q_z: "z \<notin> S z \<Longrightarrow> pmf q z = 0"
hoelzl@59664
   863
    by(erule contrapos_np)(simp add: pmf_eq_0_set_pmf in_S)
hoelzl@59664
   864
hoelzl@59664
   865
  have "ereal (pmf ?lhs z) = \<integral>\<^sup>+ x. ereal (pmf (cond_pmf q (S x)) z) \<partial>measure_pmf p"
hoelzl@59664
   866
    by(simp add: ereal_pmf_bind)
hoelzl@59664
   867
  also have "\<dots> = \<integral>\<^sup>+ x. ereal (pmf q z / measure p (S z)) * indicator (S z) x \<partial>measure_pmf p"
hoelzl@59664
   868
    by(rule nn_integral_cong_AE)(auto simp add: AE_measure_pmf_iff pmf_cond same pmf_q_z in_S dest!: S_eq split: split_indicator)
hoelzl@59664
   869
  also have "\<dots> = pmf q z" using pmf_nonneg[of q z]
hoelzl@59664
   870
    by (subst nn_integral_cmult)(auto simp add: measure_nonneg measure_pmf.emeasure_eq_measure same measure_pmf.prob_eq_0 AE_measure_pmf_iff pmf_eq_0_set_pmf in_S)
hoelzl@59664
   871
  finally show "pmf ?lhs z = pmf q z" by simp
hoelzl@59664
   872
qed
hoelzl@59664
   873
hoelzl@59664
   874
subsection \<open> Relator \<close>
hoelzl@59664
   875
hoelzl@59664
   876
inductive rel_pmf :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> 'a pmf \<Rightarrow> 'b pmf \<Rightarrow> bool"
hoelzl@59664
   877
for R p q
hoelzl@59664
   878
where
hoelzl@59664
   879
  "\<lbrakk> \<And>x y. (x, y) \<in> set_pmf pq \<Longrightarrow> R x y; 
hoelzl@59664
   880
     map_pmf fst pq = p; map_pmf snd pq = q \<rbrakk>
hoelzl@59664
   881
  \<Longrightarrow> rel_pmf R p q"
hoelzl@59664
   882
hoelzl@59664
   883
bnf pmf: "'a pmf" map: map_pmf sets: set_pmf bd : "natLeq" rel: rel_pmf
hoelzl@59664
   884
proof -
hoelzl@59664
   885
  show "map_pmf id = id" by (rule map_pmf_id)
hoelzl@59664
   886
  show "\<And>f g. map_pmf (f \<circ> g) = map_pmf f \<circ> map_pmf g" by (rule map_pmf_compose) 
hoelzl@59664
   887
  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"
hoelzl@59664
   888
    by (intro map_pmf_cong refl)
hoelzl@59664
   889
hoelzl@59664
   890
  show "\<And>f::'a \<Rightarrow> 'b. set_pmf \<circ> map_pmf f = op ` f \<circ> set_pmf"
hoelzl@59664
   891
    by (rule pmf_set_map)
hoelzl@59664
   892
hoelzl@59664
   893
  { fix p :: "'s pmf"
hoelzl@59664
   894
    have "(card_of (set_pmf p), card_of (UNIV :: nat set)) \<in> ordLeq"
hoelzl@59664
   895
      by (rule card_of_ordLeqI[where f="to_nat_on (set_pmf p)"])
hoelzl@59664
   896
         (auto intro: countable_set_pmf)
hoelzl@59664
   897
    also have "(card_of (UNIV :: nat set), natLeq) \<in> ordLeq"
hoelzl@59664
   898
      by (metis Field_natLeq card_of_least natLeq_Well_order)
hoelzl@59664
   899
    finally show "(card_of (set_pmf p), natLeq) \<in> ordLeq" . }
hoelzl@59664
   900
hoelzl@59664
   901
  show "\<And>R. rel_pmf R =
hoelzl@59664
   902
         (BNF_Def.Grp {x. set_pmf x \<subseteq> {(x, y). R x y}} (map_pmf fst))\<inverse>\<inverse> OO
hoelzl@59664
   903
         BNF_Def.Grp {x. set_pmf x \<subseteq> {(x, y). R x y}} (map_pmf snd)"
hoelzl@59664
   904
     by (auto simp add: fun_eq_iff BNF_Def.Grp_def OO_def rel_pmf.simps)
hoelzl@59664
   905
hoelzl@59664
   906
  { fix p :: "'a pmf" and f :: "'a \<Rightarrow> 'b" and g x
hoelzl@59664
   907
    assume p: "\<And>z. z \<in> set_pmf p \<Longrightarrow> f z = g z"
hoelzl@59664
   908
      and x: "x \<in> set_pmf p"
hoelzl@59664
   909
    thus "f x = g x" by simp }
hoelzl@59664
   910
hoelzl@59664
   911
  fix R :: "'a => 'b \<Rightarrow> bool" and S :: "'b \<Rightarrow> 'c \<Rightarrow> bool"
hoelzl@59664
   912
  { fix p q r
hoelzl@59664
   913
    assume pq: "rel_pmf R p q"
hoelzl@59664
   914
      and qr:"rel_pmf S q r"
hoelzl@59664
   915
    from pq obtain pq where pq: "\<And>x y. (x, y) \<in> set_pmf pq \<Longrightarrow> R x y"
hoelzl@59664
   916
      and p: "p = map_pmf fst pq" and q: "q = map_pmf snd pq" by cases auto
hoelzl@59664
   917
    from qr obtain qr where qr: "\<And>y z. (y, z) \<in> set_pmf qr \<Longrightarrow> S y z"
hoelzl@59664
   918
      and q': "q = map_pmf fst qr" and r: "r = map_pmf snd qr" by cases auto
hoelzl@59664
   919
hoelzl@59664
   920
    def pr \<equiv> "bind_pmf pq (\<lambda>(x, y). bind_pmf (cond_pmf qr {(y', z). y' = y}) (\<lambda>(y', z). return_pmf (x, z)))"
hoelzl@59664
   921
    have pr_welldefined: "\<And>y. y \<in> q \<Longrightarrow> qr \<inter> {(y', z). y' = y} \<noteq> {}"
hoelzl@59664
   922
      by (force simp: q' set_map_pmf)
hoelzl@59664
   923
hoelzl@59664
   924
    have "rel_pmf (R OO S) p r"
hoelzl@59664
   925
    proof (rule rel_pmf.intros)
hoelzl@59664
   926
      fix x z assume "(x, z) \<in> pr"
hoelzl@59664
   927
      then have "\<exists>y. (x, y) \<in> pq \<and> (y, z) \<in> qr"
hoelzl@59664
   928
        by (auto simp: q pr_welldefined pr_def set_bind_pmf split_beta set_return_pmf set_cond_pmf set_map_pmf)
hoelzl@59664
   929
      with pq qr show "(R OO S) x z"
hoelzl@59664
   930
        by blast
hoelzl@59664
   931
    next
hoelzl@59664
   932
      have "map_pmf snd pr = map_pmf snd (bind_pmf q (\<lambda>y. cond_pmf qr {(y', z). y' = y}))"
hoelzl@59664
   933
        by (simp add: pr_def q split_beta bind_map_pmf map_pmf_def[symmetric] map_bind_pmf map_return_pmf)
hoelzl@59664
   934
      then show "map_pmf snd pr = r"
hoelzl@59664
   935
        unfolding r q' bind_map_pmf by (subst (asm) bind_cond_pmf_cancel) auto
hoelzl@59664
   936
    qed (simp add: pr_def map_bind_pmf split_beta map_return_pmf map_pmf_def[symmetric] p) }
hoelzl@59664
   937
  then show "rel_pmf R OO rel_pmf S \<le> rel_pmf (R OO S)"
hoelzl@59664
   938
    by(auto simp add: le_fun_def)
hoelzl@59664
   939
qed (fact natLeq_card_order natLeq_cinfinite)+
hoelzl@59664
   940
hoelzl@59664
   941
lemma rel_pmf_return_pmf1: "rel_pmf R (return_pmf x) M \<longleftrightarrow> (\<forall>a\<in>M. R x a)"
hoelzl@59664
   942
proof safe
hoelzl@59664
   943
  fix a assume "a \<in> M" "rel_pmf R (return_pmf x) M"
hoelzl@59664
   944
  then obtain pq where *: "\<And>a b. (a, b) \<in> set_pmf pq \<Longrightarrow> R a b"
hoelzl@59664
   945
    and eq: "return_pmf x = map_pmf fst pq" "M = map_pmf snd pq"
hoelzl@59664
   946
    by (force elim: rel_pmf.cases)
hoelzl@59664
   947
  moreover have "set_pmf (return_pmf x) = {x}"
hoelzl@59664
   948
    by (simp add: set_return_pmf)
hoelzl@59664
   949
  with `a \<in> M` have "(x, a) \<in> pq"
hoelzl@59664
   950
    by (force simp: eq set_map_pmf)
hoelzl@59664
   951
  with * show "R x a"
hoelzl@59664
   952
    by auto
hoelzl@59664
   953
qed (auto intro!: rel_pmf.intros[where pq="pair_pmf (return_pmf x) M"]
hoelzl@59664
   954
          simp: map_fst_pair_pmf map_snd_pair_pmf set_pair_pmf set_return_pmf)
hoelzl@59664
   955
hoelzl@59664
   956
lemma rel_pmf_return_pmf2: "rel_pmf R M (return_pmf x) \<longleftrightarrow> (\<forall>a\<in>M. R a x)"
hoelzl@59664
   957
  by (subst pmf.rel_flip[symmetric]) (simp add: rel_pmf_return_pmf1)
hoelzl@59664
   958
hoelzl@59664
   959
lemma rel_return_pmf[simp]: "rel_pmf R (return_pmf x1) (return_pmf x2) = R x1 x2"
hoelzl@59664
   960
  unfolding rel_pmf_return_pmf2 set_return_pmf by simp
hoelzl@59664
   961
hoelzl@59664
   962
lemma rel_pmf_False[simp]: "rel_pmf (\<lambda>x y. False) x y = False"
hoelzl@59664
   963
  unfolding pmf.in_rel fun_eq_iff using set_pmf_not_empty by fastforce
hoelzl@59664
   964
hoelzl@59664
   965
lemma rel_pmf_rel_prod:
hoelzl@59664
   966
  "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'"
hoelzl@59664
   967
proof safe
hoelzl@59664
   968
  assume "rel_pmf (rel_prod R S) (pair_pmf A A') (pair_pmf B B')"
hoelzl@59664
   969
  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"
hoelzl@59664
   970
    and eq: "map_pmf fst pq = pair_pmf A A'" "map_pmf snd pq = pair_pmf B B'"
hoelzl@59664
   971
    by (force elim: rel_pmf.cases)
hoelzl@59664
   972
  show "rel_pmf R A B"
hoelzl@59664
   973
  proof (rule rel_pmf.intros)
hoelzl@59664
   974
    let ?f = "\<lambda>(a, b). (fst a, fst b)"
hoelzl@59664
   975
    have [simp]: "(\<lambda>x. fst (?f x)) = fst o fst" "(\<lambda>x. snd (?f x)) = fst o snd"
hoelzl@59664
   976
      by auto
hoelzl@59664
   977
hoelzl@59664
   978
    show "map_pmf fst (map_pmf ?f pq) = A"
hoelzl@59664
   979
      by (simp add: map_pmf_comp pmf.map_comp[symmetric] eq map_fst_pair_pmf)
hoelzl@59664
   980
    show "map_pmf snd (map_pmf ?f pq) = B"
hoelzl@59664
   981
      by (simp add: map_pmf_comp pmf.map_comp[symmetric] eq map_fst_pair_pmf)
hoelzl@59664
   982
hoelzl@59664
   983
    fix a b assume "(a, b) \<in> set_pmf (map_pmf ?f pq)"
hoelzl@59664
   984
    then obtain c d where "((a, c), (b, d)) \<in> set_pmf pq"
hoelzl@59664
   985
      by (auto simp: set_map_pmf)
hoelzl@59664
   986
    from pq[OF this] show "R a b" ..
hoelzl@59664
   987
  qed
hoelzl@59664
   988
  show "rel_pmf S A' B'"
hoelzl@59664
   989
  proof (rule rel_pmf.intros)
hoelzl@59664
   990
    let ?f = "\<lambda>(a, b). (snd a, snd b)"
hoelzl@59664
   991
    have [simp]: "(\<lambda>x. fst (?f x)) = snd o fst" "(\<lambda>x. snd (?f x)) = snd o snd"
hoelzl@59664
   992
      by auto
hoelzl@59664
   993
hoelzl@59664
   994
    show "map_pmf fst (map_pmf ?f pq) = A'"
hoelzl@59664
   995
      by (simp add: map_pmf_comp pmf.map_comp[symmetric] eq map_snd_pair_pmf)
hoelzl@59664
   996
    show "map_pmf snd (map_pmf ?f pq) = B'"
hoelzl@59664
   997
      by (simp add: map_pmf_comp pmf.map_comp[symmetric] eq map_snd_pair_pmf)
hoelzl@59664
   998
hoelzl@59664
   999
    fix c d assume "(c, d) \<in> set_pmf (map_pmf ?f pq)"
hoelzl@59664
  1000
    then obtain a b where "((a, c), (b, d)) \<in> set_pmf pq"
hoelzl@59664
  1001
      by (auto simp: set_map_pmf)
hoelzl@59664
  1002
    from pq[OF this] show "S c d" ..
hoelzl@59664
  1003
  qed
hoelzl@59664
  1004
next
hoelzl@59664
  1005
  assume "rel_pmf R A B" "rel_pmf S A' B'"
hoelzl@59664
  1006
  then obtain Rpq Spq
hoelzl@59664
  1007
    where Rpq: "\<And>a b. (a, b) \<in> set_pmf Rpq \<Longrightarrow> R a b"
hoelzl@59664
  1008
        "map_pmf fst Rpq = A" "map_pmf snd Rpq = B"
hoelzl@59664
  1009
      and Spq: "\<And>a b. (a, b) \<in> set_pmf Spq \<Longrightarrow> S a b"
hoelzl@59664
  1010
        "map_pmf fst Spq = A'" "map_pmf snd Spq = B'"
hoelzl@59664
  1011
    by (force elim: rel_pmf.cases)
hoelzl@59664
  1012
hoelzl@59664
  1013
  let ?f = "(\<lambda>((a, c), (b, d)). ((a, b), (c, d)))"
hoelzl@59664
  1014
  let ?pq = "map_pmf ?f (pair_pmf Rpq Spq)"
hoelzl@59664
  1015
  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))"
hoelzl@59664
  1016
    by auto
hoelzl@59664
  1017
hoelzl@59664
  1018
  show "rel_pmf (rel_prod R S) (pair_pmf A A') (pair_pmf B B')"
hoelzl@59664
  1019
    by (rule rel_pmf.intros[where pq="?pq"])
hoelzl@59664
  1020
       (auto simp: map_snd_pair_pmf map_fst_pair_pmf set_pair_pmf set_map_pmf map_pmf_comp Rpq Spq
hoelzl@59664
  1021
                   map_pair)
hoelzl@59664
  1022
qed
hoelzl@59664
  1023
hoelzl@59664
  1024
lemma rel_pmf_reflI: 
hoelzl@59664
  1025
  assumes "\<And>x. x \<in> set_pmf p \<Longrightarrow> P x x"
hoelzl@59664
  1026
  shows "rel_pmf P p p"
hoelzl@59664
  1027
by(rule rel_pmf.intros[where pq="map_pmf (\<lambda>x. (x, x)) p"])(auto simp add: pmf.map_comp o_def set_map_pmf assms)
hoelzl@59664
  1028
hoelzl@59664
  1029
context
hoelzl@59664
  1030
begin
hoelzl@59664
  1031
hoelzl@59664
  1032
interpretation pmf_as_measure .
hoelzl@59664
  1033
hoelzl@59664
  1034
definition "join_pmf M = bind_pmf M (\<lambda>x. x)"
hoelzl@59664
  1035
hoelzl@59664
  1036
lemma bind_eq_join_pmf: "bind_pmf M f = join_pmf (map_pmf f M)"
hoelzl@59664
  1037
  unfolding join_pmf_def bind_map_pmf ..
hoelzl@59664
  1038
hoelzl@59664
  1039
lemma join_eq_bind_pmf: "join_pmf M = bind_pmf M id"
hoelzl@59664
  1040
  by (simp add: join_pmf_def id_def)
hoelzl@59664
  1041
hoelzl@59664
  1042
lemma pmf_join: "pmf (join_pmf N) i = (\<integral>M. pmf M i \<partial>measure_pmf N)"
hoelzl@59664
  1043
  unfolding join_pmf_def pmf_bind ..
hoelzl@59664
  1044
hoelzl@59664
  1045
lemma ereal_pmf_join: "ereal (pmf (join_pmf N) i) = (\<integral>\<^sup>+M. pmf M i \<partial>measure_pmf N)"
hoelzl@59664
  1046
  unfolding join_pmf_def ereal_pmf_bind ..
hoelzl@59664
  1047
hoelzl@59664
  1048
lemma set_pmf_join_pmf: "set_pmf (join_pmf f) = (\<Union>p\<in>set_pmf f. set_pmf p)"
hoelzl@59664
  1049
  by (simp add: join_pmf_def set_bind_pmf)
hoelzl@59664
  1050
hoelzl@59664
  1051
lemma join_return_pmf: "join_pmf (return_pmf M) = M"
hoelzl@59664
  1052
  by (simp add: integral_return pmf_eq_iff pmf_join return_pmf.rep_eq)
hoelzl@59664
  1053
hoelzl@59664
  1054
lemma map_join_pmf: "map_pmf f (join_pmf AA) = join_pmf (map_pmf (map_pmf f) AA)"
hoelzl@59664
  1055
  by (simp add: join_pmf_def map_pmf_def bind_assoc_pmf bind_return_pmf)
hoelzl@59664
  1056
hoelzl@59664
  1057
lemma join_map_return_pmf: "join_pmf (map_pmf return_pmf A) = A"
hoelzl@59664
  1058
  by (simp add: join_pmf_def map_pmf_def bind_assoc_pmf bind_return_pmf bind_return_pmf')
hoelzl@59664
  1059
hoelzl@59664
  1060
end
hoelzl@59664
  1061
hoelzl@59664
  1062
lemma rel_pmf_joinI:
hoelzl@59664
  1063
  assumes "rel_pmf (rel_pmf P) p q"
hoelzl@59664
  1064
  shows "rel_pmf P (join_pmf p) (join_pmf q)"
hoelzl@59664
  1065
proof -
hoelzl@59664
  1066
  from assms obtain pq where p: "p = map_pmf fst pq"
hoelzl@59664
  1067
    and q: "q = map_pmf snd pq"
hoelzl@59664
  1068
    and P: "\<And>x y. (x, y) \<in> set_pmf pq \<Longrightarrow> rel_pmf P x y"
hoelzl@59664
  1069
    by cases auto
hoelzl@59664
  1070
  from P obtain PQ 
hoelzl@59664
  1071
    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"
hoelzl@59664
  1072
    and x: "\<And>x y. (x, y) \<in> set_pmf pq \<Longrightarrow> map_pmf fst (PQ x y) = x"
hoelzl@59664
  1073
    and y: "\<And>x y. (x, y) \<in> set_pmf pq \<Longrightarrow> map_pmf snd (PQ x y) = y"
hoelzl@59664
  1074
    by(metis rel_pmf.simps)
hoelzl@59664
  1075
hoelzl@59664
  1076
  let ?r = "bind_pmf pq (\<lambda>(x, y). PQ x y)"
hoelzl@59664
  1077
  have "\<And>a b. (a, b) \<in> set_pmf ?r \<Longrightarrow> P a b" by(auto simp add: set_bind_pmf intro: PQ)
hoelzl@59664
  1078
  moreover have "map_pmf fst ?r = join_pmf p" "map_pmf snd ?r = join_pmf q"
hoelzl@59664
  1079
    by (simp_all add: p q x y join_pmf_def map_bind_pmf bind_map_pmf split_def cong: bind_pmf_cong)
hoelzl@59664
  1080
  ultimately show ?thesis ..
hoelzl@59664
  1081
qed
hoelzl@59664
  1082
hoelzl@59664
  1083
lemma rel_pmf_bindI:
hoelzl@59664
  1084
  assumes pq: "rel_pmf R p q"
hoelzl@59664
  1085
  and fg: "\<And>x y. R x y \<Longrightarrow> rel_pmf P (f x) (g y)"
hoelzl@59664
  1086
  shows "rel_pmf P (bind_pmf p f) (bind_pmf q g)"
hoelzl@59664
  1087
  unfolding bind_eq_join_pmf
hoelzl@59664
  1088
  by (rule rel_pmf_joinI)
hoelzl@59664
  1089
     (auto simp add: pmf.rel_map intro: pmf.rel_mono[THEN le_funD, THEN le_funD, THEN le_boolD, THEN mp, OF _ pq] fg)
hoelzl@59664
  1090
hoelzl@59664
  1091
text {*
hoelzl@59664
  1092
  Proof that @{const rel_pmf} preserves orders.
hoelzl@59664
  1093
  Antisymmetry proof follows Thm. 1 in N. Saheb-Djahromi, Cpo's of measures for nondeterminism, 
hoelzl@59664
  1094
  Theoretical Computer Science 12(1):19--37, 1980, 
hoelzl@59664
  1095
  @{url "http://dx.doi.org/10.1016/0304-3975(80)90003-1"}
hoelzl@59664
  1096
*}
hoelzl@59664
  1097
hoelzl@59664
  1098
lemma 
hoelzl@59664
  1099
  assumes *: "rel_pmf R p q"
hoelzl@59664
  1100
  and refl: "reflp R" and trans: "transp R"
hoelzl@59664
  1101
  shows measure_Ici: "measure p {y. R x y} \<le> measure q {y. R x y}" (is ?thesis1)
hoelzl@59664
  1102
  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)
hoelzl@59664
  1103
proof -
hoelzl@59664
  1104
  from * obtain pq
hoelzl@59664
  1105
    where pq: "\<And>x y. (x, y) \<in> set_pmf pq \<Longrightarrow> R x y"
hoelzl@59664
  1106
    and p: "p = map_pmf fst pq"
hoelzl@59664
  1107
    and q: "q = map_pmf snd pq"
hoelzl@59664
  1108
    by cases auto
hoelzl@59664
  1109
  show ?thesis1 ?thesis2 unfolding p q map_pmf_rep_eq using refl trans
hoelzl@59664
  1110
    by(auto 4 3 simp add: measure_distr reflpD AE_measure_pmf_iff intro!: measure_pmf.finite_measure_mono_AE dest!: pq elim: transpE)
hoelzl@59664
  1111
qed
hoelzl@59664
  1112
hoelzl@59664
  1113
lemma rel_pmf_inf:
hoelzl@59664
  1114
  fixes p q :: "'a pmf"
hoelzl@59664
  1115
  assumes 1: "rel_pmf R p q"
hoelzl@59664
  1116
  assumes 2: "rel_pmf R q p"
hoelzl@59664
  1117
  and refl: "reflp R" and trans: "transp R"
hoelzl@59664
  1118
  shows "rel_pmf (inf R R\<inverse>\<inverse>) p q"
hoelzl@59664
  1119
proof
hoelzl@59664
  1120
  let ?E = "\<lambda>x. {y. R x y \<and> R y x}"
hoelzl@59664
  1121
  let ?\<mu>E = "\<lambda>x. measure q (?E x)"
hoelzl@59664
  1122
  { fix x
hoelzl@59664
  1123
    have "measure p (?E x) = measure p ({y. R x y} - {y. R x y \<and> \<not> R y x})"
hoelzl@59664
  1124
      by(auto intro!: arg_cong[where f="measure p"])
hoelzl@59664
  1125
    also have "\<dots> = measure p {y. R x y} - measure p {y. R x y \<and> \<not> R y x}"
hoelzl@59664
  1126
      by (rule measure_pmf.finite_measure_Diff) auto
hoelzl@59664
  1127
    also have "measure p {y. R x y \<and> \<not> R y x} = measure q {y. R x y \<and> \<not> R y x}"
hoelzl@59664
  1128
      using 1 2 refl trans by(auto intro!: Orderings.antisym measure_Ioi)
hoelzl@59664
  1129
    also have "measure p {y. R x y} = measure q {y. R x y}"
hoelzl@59664
  1130
      using 1 2 refl trans by(auto intro!: Orderings.antisym measure_Ici)
hoelzl@59664
  1131
    also have "measure q {y. R x y} - measure q {y. R x y \<and> ~ R y x} =
hoelzl@59664
  1132
      measure q ({y. R x y} - {y. R x y \<and> \<not> R y x})"
hoelzl@59664
  1133
      by(rule measure_pmf.finite_measure_Diff[symmetric]) auto
hoelzl@59664
  1134
    also have "\<dots> = ?\<mu>E x"
hoelzl@59664
  1135
      by(auto intro!: arg_cong[where f="measure q"])
hoelzl@59664
  1136
    also note calculation }
hoelzl@59664
  1137
  note eq = this
hoelzl@59664
  1138
hoelzl@59664
  1139
  def pq \<equiv> "bind_pmf p (\<lambda>x. bind_pmf (cond_pmf q (?E x)) (\<lambda>y. return_pmf (x, y)))"
hoelzl@59664
  1140
hoelzl@59664
  1141
  show "map_pmf fst pq = p"
hoelzl@59664
  1142
    by(simp add: pq_def map_bind_pmf map_return_pmf bind_return_pmf')
hoelzl@59664
  1143
hoelzl@59664
  1144
  show "map_pmf snd pq = q"
hoelzl@59664
  1145
    unfolding pq_def map_bind_pmf map_return_pmf bind_return_pmf' snd_conv
hoelzl@59664
  1146
    by(subst bind_cond_pmf_cancel)(auto simp add: reflpD[OF \<open>reflp R\<close>] eq  intro: transpD[OF \<open>transp R\<close>])
hoelzl@59664
  1147
hoelzl@59664
  1148
  fix x y
hoelzl@59664
  1149
  assume "(x, y) \<in> set_pmf pq"
hoelzl@59664
  1150
  moreover
hoelzl@59664
  1151
  { assume "x \<in> set_pmf p"
hoelzl@59664
  1152
    hence "measure (measure_pmf p) (?E x) \<noteq> 0"
hoelzl@59664
  1153
      by(auto simp add: measure_pmf.prob_eq_0 AE_measure_pmf_iff intro: reflpD[OF \<open>reflp R\<close>])
hoelzl@59664
  1154
    hence "measure (measure_pmf q) (?E x) \<noteq> 0" using eq by simp
hoelzl@59664
  1155
    hence "set_pmf q \<inter> {y. R x y \<and> R y x} \<noteq> {}" 
hoelzl@59664
  1156
      by(auto simp add: measure_pmf.prob_eq_0 AE_measure_pmf_iff) }
hoelzl@59664
  1157
  ultimately show "inf R R\<inverse>\<inverse> x y"
hoelzl@59664
  1158
    by(auto simp add: pq_def set_bind_pmf set_return_pmf set_cond_pmf)
hoelzl@59664
  1159
qed
hoelzl@59664
  1160
hoelzl@59664
  1161
lemma rel_pmf_antisym:
hoelzl@59664
  1162
  fixes p q :: "'a pmf"
hoelzl@59664
  1163
  assumes 1: "rel_pmf R p q"
hoelzl@59664
  1164
  assumes 2: "rel_pmf R q p"
hoelzl@59664
  1165
  and refl: "reflp R" and trans: "transp R" and antisym: "antisymP R"
hoelzl@59664
  1166
  shows "p = q"
hoelzl@59664
  1167
proof -
hoelzl@59664
  1168
  from 1 2 refl trans have "rel_pmf (inf R R\<inverse>\<inverse>) p q" by(rule rel_pmf_inf)
hoelzl@59664
  1169
  also have "inf R R\<inverse>\<inverse> = op ="
hoelzl@59664
  1170
    using refl antisym by(auto intro!: ext simp add: reflpD dest: antisymD)
hoelzl@59664
  1171
  finally show ?thesis unfolding pmf.rel_eq .
hoelzl@59664
  1172
qed
hoelzl@59664
  1173
hoelzl@59664
  1174
lemma reflp_rel_pmf: "reflp R \<Longrightarrow> reflp (rel_pmf R)"
hoelzl@59664
  1175
by(blast intro: reflpI rel_pmf_reflI reflpD)
hoelzl@59664
  1176
hoelzl@59664
  1177
lemma antisymP_rel_pmf:
hoelzl@59664
  1178
  "\<lbrakk> reflp R; transp R; antisymP R \<rbrakk>
hoelzl@59664
  1179
  \<Longrightarrow> antisymP (rel_pmf R)"
hoelzl@59664
  1180
by(rule antisymI)(blast intro: rel_pmf_antisym)
hoelzl@59664
  1181
hoelzl@59664
  1182
lemma transp_rel_pmf:
hoelzl@59664
  1183
  assumes "transp R"
hoelzl@59664
  1184
  shows "transp (rel_pmf R)"
hoelzl@59664
  1185
proof (rule transpI)
hoelzl@59664
  1186
  fix x y z
hoelzl@59664
  1187
  assume "rel_pmf R x y" and "rel_pmf R y z"
hoelzl@59664
  1188
  hence "rel_pmf (R OO R) x z" by (simp add: pmf.rel_compp relcompp.relcompI)
hoelzl@59664
  1189
  thus "rel_pmf R x z"
hoelzl@59664
  1190
    using assms by (metis (no_types) pmf.rel_mono rev_predicate2D transp_relcompp_less_eq)
hoelzl@59664
  1191
qed
hoelzl@59664
  1192
hoelzl@59664
  1193
subsection \<open> Distributions \<close>
hoelzl@59664
  1194
hoelzl@59000
  1195
context
hoelzl@59000
  1196
begin
hoelzl@59000
  1197
hoelzl@59000
  1198
interpretation pmf_as_function .
hoelzl@59000
  1199
hoelzl@59093
  1200
subsubsection \<open> Bernoulli Distribution \<close>
hoelzl@59093
  1201
hoelzl@59000
  1202
lift_definition bernoulli_pmf :: "real \<Rightarrow> bool pmf" is
hoelzl@59000
  1203
  "\<lambda>p b. ((\<lambda>p. if b then p else 1 - p) \<circ> min 1 \<circ> max 0) p"
hoelzl@59000
  1204
  by (auto simp: nn_integral_count_space_finite[where A="{False, True}"] UNIV_bool
hoelzl@59000
  1205
           split: split_max split_min)
hoelzl@59000
  1206
hoelzl@59000
  1207
lemma pmf_bernoulli_True[simp]: "0 \<le> p \<Longrightarrow> p \<le> 1 \<Longrightarrow> pmf (bernoulli_pmf p) True = p"
hoelzl@59000
  1208
  by transfer simp
hoelzl@59000
  1209
hoelzl@59000
  1210
lemma pmf_bernoulli_False[simp]: "0 \<le> p \<Longrightarrow> p \<le> 1 \<Longrightarrow> pmf (bernoulli_pmf p) False = 1 - p"
hoelzl@59000
  1211
  by transfer simp
hoelzl@59000
  1212
hoelzl@59000
  1213
lemma set_pmf_bernoulli: "0 < p \<Longrightarrow> p < 1 \<Longrightarrow> set_pmf (bernoulli_pmf p) = UNIV"
hoelzl@59000
  1214
  by (auto simp add: set_pmf_iff UNIV_bool)
hoelzl@59000
  1215
hoelzl@59002
  1216
lemma nn_integral_bernoulli_pmf[simp]: 
hoelzl@59002
  1217
  assumes [simp]: "0 \<le> p" "p \<le> 1" "\<And>x. 0 \<le> f x"
hoelzl@59002
  1218
  shows "(\<integral>\<^sup>+x. f x \<partial>bernoulli_pmf p) = f True * p + f False * (1 - p)"
hoelzl@59002
  1219
  by (subst nn_integral_measure_pmf_support[of UNIV])
hoelzl@59002
  1220
     (auto simp: UNIV_bool field_simps)
hoelzl@59002
  1221
hoelzl@59002
  1222
lemma integral_bernoulli_pmf[simp]: 
hoelzl@59002
  1223
  assumes [simp]: "0 \<le> p" "p \<le> 1"
hoelzl@59002
  1224
  shows "(\<integral>x. f x \<partial>bernoulli_pmf p) = f True * p + f False * (1 - p)"
hoelzl@59002
  1225
  by (subst integral_measure_pmf[of UNIV]) (auto simp: UNIV_bool)
hoelzl@59002
  1226
Andreas@59525
  1227
lemma pmf_bernoulli_half [simp]: "pmf (bernoulli_pmf (1 / 2)) x = 1 / 2"
Andreas@59525
  1228
by(cases x) simp_all
Andreas@59525
  1229
Andreas@59525
  1230
lemma measure_pmf_bernoulli_half: "measure_pmf (bernoulli_pmf (1 / 2)) = uniform_count_measure UNIV"
Andreas@59525
  1231
by(rule measure_eqI)(simp_all add: nn_integral_pmf[symmetric] emeasure_uniform_count_measure nn_integral_count_space_finite sets_uniform_count_measure)
Andreas@59525
  1232
hoelzl@59093
  1233
subsubsection \<open> Geometric Distribution \<close>
hoelzl@59093
  1234
hoelzl@59000
  1235
lift_definition geometric_pmf :: "nat pmf" is "\<lambda>n. 1 / 2^Suc n"
hoelzl@59000
  1236
proof
hoelzl@59000
  1237
  note geometric_sums[of "1 / 2"]
hoelzl@59000
  1238
  note sums_mult[OF this, of "1 / 2"]
hoelzl@59000
  1239
  from sums_suminf_ereal[OF this]
hoelzl@59000
  1240
  show "(\<integral>\<^sup>+ x. ereal (1 / 2 ^ Suc x) \<partial>count_space UNIV) = 1"
hoelzl@59000
  1241
    by (simp add: nn_integral_count_space_nat field_simps)
hoelzl@59000
  1242
qed simp
hoelzl@59000
  1243
hoelzl@59000
  1244
lemma pmf_geometric[simp]: "pmf geometric_pmf n = 1 / 2^Suc n"
hoelzl@59000
  1245
  by transfer rule
hoelzl@59000
  1246
hoelzl@59002
  1247
lemma set_pmf_geometric[simp]: "set_pmf geometric_pmf = UNIV"
hoelzl@59000
  1248
  by (auto simp: set_pmf_iff)
hoelzl@59000
  1249
hoelzl@59093
  1250
subsubsection \<open> Uniform Multiset Distribution \<close>
hoelzl@59093
  1251
hoelzl@59000
  1252
context
hoelzl@59000
  1253
  fixes M :: "'a multiset" assumes M_not_empty: "M \<noteq> {#}"
hoelzl@59000
  1254
begin
hoelzl@59000
  1255
hoelzl@59000
  1256
lift_definition pmf_of_multiset :: "'a pmf" is "\<lambda>x. count M x / size M"
hoelzl@59000
  1257
proof
hoelzl@59000
  1258
  show "(\<integral>\<^sup>+ x. ereal (real (count M x) / real (size M)) \<partial>count_space UNIV) = 1"  
hoelzl@59000
  1259
    using M_not_empty
hoelzl@59000
  1260
    by (simp add: zero_less_divide_iff nn_integral_count_space nonempty_has_size
hoelzl@59000
  1261
                  setsum_divide_distrib[symmetric])
hoelzl@59000
  1262
       (auto simp: size_multiset_overloaded_eq intro!: setsum.cong)
hoelzl@59000
  1263
qed simp
hoelzl@59000
  1264
hoelzl@59000
  1265
lemma pmf_of_multiset[simp]: "pmf pmf_of_multiset x = count M x / size M"
hoelzl@59000
  1266
  by transfer rule
hoelzl@59000
  1267
hoelzl@59000
  1268
lemma set_pmf_of_multiset[simp]: "set_pmf pmf_of_multiset = set_of M"
hoelzl@59000
  1269
  by (auto simp: set_pmf_iff)
hoelzl@59000
  1270
hoelzl@59000
  1271
end
hoelzl@59000
  1272
hoelzl@59093
  1273
subsubsection \<open> Uniform Distribution \<close>
hoelzl@59093
  1274
hoelzl@59000
  1275
context
hoelzl@59000
  1276
  fixes S :: "'a set" assumes S_not_empty: "S \<noteq> {}" and S_finite: "finite S"
hoelzl@59000
  1277
begin
hoelzl@59000
  1278
hoelzl@59000
  1279
lift_definition pmf_of_set :: "'a pmf" is "\<lambda>x. indicator S x / card S"
hoelzl@59000
  1280
proof
hoelzl@59000
  1281
  show "(\<integral>\<^sup>+ x. ereal (indicator S x / real (card S)) \<partial>count_space UNIV) = 1"  
hoelzl@59000
  1282
    using S_not_empty S_finite by (subst nn_integral_count_space'[of S]) auto
hoelzl@59000
  1283
qed simp
hoelzl@59000
  1284
hoelzl@59000
  1285
lemma pmf_of_set[simp]: "pmf pmf_of_set x = indicator S x / card S"
hoelzl@59000
  1286
  by transfer rule
hoelzl@59000
  1287
hoelzl@59000
  1288
lemma set_pmf_of_set[simp]: "set_pmf pmf_of_set = S"
hoelzl@59000
  1289
  using S_finite S_not_empty by (auto simp: set_pmf_iff)
hoelzl@59000
  1290
hoelzl@59002
  1291
lemma emeasure_pmf_of_set[simp]: "emeasure pmf_of_set S = 1"
hoelzl@59002
  1292
  by (rule measure_pmf.emeasure_eq_1_AE) (auto simp: AE_measure_pmf_iff)
hoelzl@59002
  1293
hoelzl@59000
  1294
end
hoelzl@59000
  1295
hoelzl@59093
  1296
subsubsection \<open> Poisson Distribution \<close>
hoelzl@59093
  1297
hoelzl@59093
  1298
context
hoelzl@59093
  1299
  fixes rate :: real assumes rate_pos: "0 < rate"
hoelzl@59093
  1300
begin
hoelzl@59093
  1301
hoelzl@59093
  1302
lift_definition poisson_pmf :: "nat pmf" is "\<lambda>k. rate ^ k / fact k * exp (-rate)"
hoelzl@59093
  1303
proof
hoelzl@59093
  1304
  (* Proof by Manuel Eberl *)
hoelzl@59093
  1305
hoelzl@59093
  1306
  have summable: "summable (\<lambda>x::nat. rate ^ x / fact x)" using summable_exp
haftmann@59557
  1307
    by (simp add: field_simps divide_inverse [symmetric])
hoelzl@59093
  1308
  have "(\<integral>\<^sup>+(x::nat). rate ^ x / fact x * exp (-rate) \<partial>count_space UNIV) =
hoelzl@59093
  1309
          exp (-rate) * (\<integral>\<^sup>+(x::nat). rate ^ x / fact x \<partial>count_space UNIV)"
hoelzl@59093
  1310
    by (simp add: field_simps nn_integral_cmult[symmetric])
hoelzl@59093
  1311
  also from rate_pos have "(\<integral>\<^sup>+(x::nat). rate ^ x / fact x \<partial>count_space UNIV) = (\<Sum>x. rate ^ x / fact x)"
hoelzl@59093
  1312
    by (simp_all add: nn_integral_count_space_nat suminf_ereal summable suminf_ereal_finite)
hoelzl@59093
  1313
  also have "... = exp rate" unfolding exp_def
haftmann@59557
  1314
    by (simp add: field_simps divide_inverse [symmetric] transfer_int_nat_factorial)
hoelzl@59093
  1315
  also have "ereal (exp (-rate)) * ereal (exp rate) = 1"
hoelzl@59093
  1316
    by (simp add: mult_exp_exp)
hoelzl@59093
  1317
  finally show "(\<integral>\<^sup>+ x. ereal (rate ^ x / real (fact x) * exp (- rate)) \<partial>count_space UNIV) = 1" .
hoelzl@59093
  1318
qed (simp add: rate_pos[THEN less_imp_le])
hoelzl@59093
  1319
hoelzl@59093
  1320
lemma pmf_poisson[simp]: "pmf poisson_pmf k = rate ^ k / fact k * exp (-rate)"
hoelzl@59093
  1321
  by transfer rule
hoelzl@59093
  1322
hoelzl@59093
  1323
lemma set_pmf_poisson[simp]: "set_pmf poisson_pmf = UNIV"
hoelzl@59093
  1324
  using rate_pos by (auto simp: set_pmf_iff)
hoelzl@59093
  1325
hoelzl@59000
  1326
end
hoelzl@59000
  1327
hoelzl@59093
  1328
subsubsection \<open> Binomial Distribution \<close>
hoelzl@59093
  1329
hoelzl@59093
  1330
context
hoelzl@59093
  1331
  fixes n :: nat and p :: real assumes p_nonneg: "0 \<le> p" and p_le_1: "p \<le> 1"
hoelzl@59093
  1332
begin
hoelzl@59093
  1333
hoelzl@59093
  1334
lift_definition binomial_pmf :: "nat pmf" is "\<lambda>k. (n choose k) * p^k * (1 - p)^(n - k)"
hoelzl@59093
  1335
proof
hoelzl@59093
  1336
  have "(\<integral>\<^sup>+k. ereal (real (n choose k) * p ^ k * (1 - p) ^ (n - k)) \<partial>count_space UNIV) =
hoelzl@59093
  1337
    ereal (\<Sum>k\<le>n. real (n choose k) * p ^ k * (1 - p) ^ (n - k))"
hoelzl@59093
  1338
    using p_le_1 p_nonneg by (subst nn_integral_count_space') auto
hoelzl@59093
  1339
  also have "(\<Sum>k\<le>n. real (n choose k) * p ^ k * (1 - p) ^ (n - k)) = (p + (1 - p)) ^ n"
hoelzl@59093
  1340
    by (subst binomial_ring) (simp add: atLeast0AtMost real_of_nat_def)
hoelzl@59093
  1341
  finally show "(\<integral>\<^sup>+ x. ereal (real (n choose x) * p ^ x * (1 - p) ^ (n - x)) \<partial>count_space UNIV) = 1"
hoelzl@59093
  1342
    by simp
hoelzl@59093
  1343
qed (insert p_nonneg p_le_1, simp)
hoelzl@59093
  1344
hoelzl@59093
  1345
lemma pmf_binomial[simp]: "pmf binomial_pmf k = (n choose k) * p^k * (1 - p)^(n - k)"
hoelzl@59093
  1346
  by transfer rule
hoelzl@59093
  1347
hoelzl@59093
  1348
lemma set_pmf_binomial_eq: "set_pmf binomial_pmf = (if p = 0 then {0} else if p = 1 then {n} else {.. n})"
hoelzl@59093
  1349
  using p_nonneg p_le_1 unfolding set_eq_iff set_pmf_iff pmf_binomial by (auto simp: set_pmf_iff)
hoelzl@59093
  1350
hoelzl@59093
  1351
end
hoelzl@59093
  1352
hoelzl@59093
  1353
end
hoelzl@59093
  1354
hoelzl@59093
  1355
lemma set_pmf_binomial_0[simp]: "set_pmf (binomial_pmf n 0) = {0}"
hoelzl@59093
  1356
  by (simp add: set_pmf_binomial_eq)
hoelzl@59093
  1357
hoelzl@59093
  1358
lemma set_pmf_binomial_1[simp]: "set_pmf (binomial_pmf n 1) = {n}"
hoelzl@59093
  1359
  by (simp add: set_pmf_binomial_eq)
hoelzl@59093
  1360
hoelzl@59093
  1361
lemma set_pmf_binomial[simp]: "0 < p \<Longrightarrow> p < 1 \<Longrightarrow> set_pmf (binomial_pmf n p) = {..n}"
hoelzl@59093
  1362
  by (simp add: set_pmf_binomial_eq)
hoelzl@59093
  1363
hoelzl@59000
  1364
end