src/HOL/Probability/Probability_Mass_Function.thy
author hoelzl
Tue Mar 10 11:56:32 2015 +0100 (2015-03-10)
changeset 59665 37adca7fd48f
parent 59664 224741ede5ae
child 59667 651ea265d568
permissions -rw-r--r--
add set_pmf lemmas to simpset
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@59665
   387
lemma set_bind_pmf[simp]: "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@59665
   427
lemma set_return_pmf[simp]: "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@59665
   480
  by (auto simp add: comp_def fun_eq_iff map_pmf_def)
hoelzl@59664
   481
hoelzl@59665
   482
lemma set_map_pmf[simp]: "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@59665
   532
lemma map_pmf_eq_return_pmf_iff:
hoelzl@59665
   533
  "map_pmf f p = return_pmf x \<longleftrightarrow> (\<forall>y \<in> set_pmf p. f y = x)"
hoelzl@59665
   534
proof
hoelzl@59665
   535
  assume "map_pmf f p = return_pmf x"
hoelzl@59665
   536
  then have "set_pmf (map_pmf f p) = set_pmf (return_pmf x)" by simp
hoelzl@59665
   537
  then show "\<forall>y \<in> set_pmf p. f y = x" by auto
hoelzl@59665
   538
next
hoelzl@59665
   539
  assume "\<forall>y \<in> set_pmf p. f y = x"
hoelzl@59665
   540
  then show "map_pmf f p = return_pmf x"
hoelzl@59665
   541
    unfolding map_pmf_const[symmetric, of _ p] by (intro map_pmf_cong) auto
hoelzl@59665
   542
qed
hoelzl@59665
   543
hoelzl@59664
   544
definition "pair_pmf A B = bind_pmf A (\<lambda>x. bind_pmf B (\<lambda>y. return_pmf (x, y)))"
hoelzl@59664
   545
hoelzl@59664
   546
lemma pmf_pair: "pmf (pair_pmf M N) (a, b) = pmf M a * pmf N b"
hoelzl@59664
   547
  unfolding pair_pmf_def pmf_bind pmf_return
hoelzl@59664
   548
  apply (subst integral_measure_pmf[where A="{b}"])
hoelzl@59664
   549
  apply (auto simp: indicator_eq_0_iff)
hoelzl@59664
   550
  apply (subst integral_measure_pmf[where A="{a}"])
hoelzl@59664
   551
  apply (auto simp: indicator_eq_0_iff setsum_nonneg_eq_0_iff pmf_nonneg)
hoelzl@59664
   552
  done
hoelzl@59664
   553
hoelzl@59665
   554
lemma set_pair_pmf[simp]: "set_pmf (pair_pmf A B) = set_pmf A \<times> set_pmf B"
hoelzl@59664
   555
  unfolding pair_pmf_def set_bind_pmf set_return_pmf by auto
hoelzl@59664
   556
hoelzl@59664
   557
lemma measure_pmf_in_subprob_space[measurable (raw)]:
hoelzl@59664
   558
  "measure_pmf M \<in> space (subprob_algebra (count_space UNIV))"
hoelzl@59664
   559
  by (simp add: space_subprob_algebra) intro_locales
hoelzl@59664
   560
hoelzl@59664
   561
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
   562
proof -
hoelzl@59664
   563
  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
   564
    by (subst nn_integral_max_0[symmetric])
hoelzl@59665
   565
       (auto simp: AE_measure_pmf_iff intro!: nn_integral_cong_AE)
hoelzl@59664
   566
  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
   567
    by (simp add: pair_pmf_def)
hoelzl@59664
   568
  also have "\<dots> = (\<integral>\<^sup>+a. \<integral>\<^sup>+b. max 0 (f (a, b)) \<partial>B \<partial>A)"
hoelzl@59664
   569
    by (auto intro!: nn_integral_cong_AE simp: AE_measure_pmf_iff)
hoelzl@59664
   570
  finally show ?thesis
hoelzl@59664
   571
    unfolding nn_integral_max_0 .
hoelzl@59664
   572
qed
hoelzl@59664
   573
hoelzl@59664
   574
lemma bind_pair_pmf:
hoelzl@59664
   575
  assumes M[measurable]: "M \<in> measurable (count_space UNIV \<Otimes>\<^sub>M count_space UNIV) (subprob_algebra N)"
hoelzl@59664
   576
  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
   577
    (is "?L = ?R")
hoelzl@59664
   578
proof (rule measure_eqI)
hoelzl@59664
   579
  have M'[measurable]: "M \<in> measurable (pair_pmf A B) (subprob_algebra N)"
hoelzl@59664
   580
    using M[THEN measurable_space] by (simp_all add: space_pair_measure)
hoelzl@59664
   581
hoelzl@59664
   582
  note measurable_bind[where N="count_space UNIV", measurable]
hoelzl@59664
   583
  note measure_pmf_in_subprob_space[simp]
hoelzl@59664
   584
hoelzl@59664
   585
  have sets_eq_N: "sets ?L = N"
hoelzl@59664
   586
    by (subst sets_bind[OF sets_kernel[OF M']]) auto
hoelzl@59664
   587
  show "sets ?L = sets ?R"
hoelzl@59664
   588
    using measurable_space[OF M]
hoelzl@59664
   589
    by (simp add: sets_eq_N space_pair_measure space_subprob_algebra)
hoelzl@59664
   590
  fix X assume "X \<in> sets ?L"
hoelzl@59664
   591
  then have X[measurable]: "X \<in> sets N"
hoelzl@59664
   592
    unfolding sets_eq_N .
hoelzl@59664
   593
  then show "emeasure ?L X = emeasure ?R X"
hoelzl@59664
   594
    apply (simp add: emeasure_bind[OF _ M' X])
hoelzl@59664
   595
    apply (simp add: nn_integral_bind[where B="count_space UNIV"] pair_pmf_def measure_pmf_bind[of A]
hoelzl@59665
   596
                     nn_integral_measure_pmf_finite emeasure_nonneg pmf_return one_ereal_def[symmetric])
hoelzl@59664
   597
    apply (subst emeasure_bind[OF _ _ X])
hoelzl@59664
   598
    apply measurable
hoelzl@59664
   599
    apply (subst emeasure_bind[OF _ _ X])
hoelzl@59664
   600
    apply measurable
hoelzl@59664
   601
    done
hoelzl@59664
   602
qed
hoelzl@59664
   603
hoelzl@59664
   604
lemma map_fst_pair_pmf: "map_pmf fst (pair_pmf A B) = A"
hoelzl@59664
   605
  by (simp add: pair_pmf_def map_pmf_def bind_assoc_pmf bind_return_pmf bind_return_pmf')
hoelzl@59664
   606
hoelzl@59664
   607
lemma map_snd_pair_pmf: "map_pmf snd (pair_pmf A B) = B"
hoelzl@59664
   608
  by (simp add: pair_pmf_def map_pmf_def bind_assoc_pmf bind_return_pmf bind_return_pmf')
hoelzl@59664
   609
hoelzl@59664
   610
lemma nn_integral_pmf':
hoelzl@59664
   611
  "inj_on f A \<Longrightarrow> (\<integral>\<^sup>+x. pmf p (f x) \<partial>count_space A) = emeasure p (f ` A)"
hoelzl@59664
   612
  by (subst nn_integral_bij_count_space[where g=f and B="f`A"])
hoelzl@59664
   613
     (auto simp: bij_betw_def nn_integral_pmf)
hoelzl@59664
   614
hoelzl@59664
   615
lemma pmf_le_0_iff[simp]: "pmf M p \<le> 0 \<longleftrightarrow> pmf M p = 0"
hoelzl@59664
   616
  using pmf_nonneg[of M p] by simp
hoelzl@59664
   617
hoelzl@59664
   618
lemma min_pmf_0[simp]: "min (pmf M p) 0 = 0" "min 0 (pmf M p) = 0"
hoelzl@59664
   619
  using pmf_nonneg[of M p] by simp_all
hoelzl@59664
   620
hoelzl@59664
   621
lemma pmf_eq_0_set_pmf: "pmf M p = 0 \<longleftrightarrow> p \<notin> set_pmf M"
hoelzl@59664
   622
  unfolding set_pmf_iff by simp
hoelzl@59664
   623
hoelzl@59664
   624
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
   625
  by (auto simp: pmf.rep_eq map_pmf_rep_eq measure_distr AE_measure_pmf_iff inj_onD
hoelzl@59664
   626
           intro!: measure_pmf.finite_measure_eq_AE)
hoelzl@59664
   627
hoelzl@59664
   628
subsection \<open> PMFs as function \<close>
hoelzl@59000
   629
hoelzl@58587
   630
context
hoelzl@58587
   631
  fixes f :: "'a \<Rightarrow> real"
hoelzl@58587
   632
  assumes nonneg: "\<And>x. 0 \<le> f x"
hoelzl@58587
   633
  assumes prob: "(\<integral>\<^sup>+x. f x \<partial>count_space UNIV) = 1"
hoelzl@58587
   634
begin
hoelzl@58587
   635
hoelzl@58587
   636
lift_definition embed_pmf :: "'a pmf" is "density (count_space UNIV) (ereal \<circ> f)"
hoelzl@58587
   637
proof (intro conjI)
hoelzl@58587
   638
  have *[simp]: "\<And>x y. ereal (f y) * indicator {x} y = ereal (f x) * indicator {x} y"
hoelzl@58587
   639
    by (simp split: split_indicator)
hoelzl@58587
   640
  show "AE x in density (count_space UNIV) (ereal \<circ> f).
hoelzl@58587
   641
    measure (density (count_space UNIV) (ereal \<circ> f)) {x} \<noteq> 0"
hoelzl@59092
   642
    by (simp add: AE_density nonneg measure_def emeasure_density max_def)
hoelzl@58587
   643
  show "prob_space (density (count_space UNIV) (ereal \<circ> f))"
hoelzl@58587
   644
    by default (simp add: emeasure_density prob)
hoelzl@58587
   645
qed simp
hoelzl@58587
   646
hoelzl@58587
   647
lemma pmf_embed_pmf: "pmf embed_pmf x = f x"
hoelzl@58587
   648
proof transfer
hoelzl@58587
   649
  have *[simp]: "\<And>x y. ereal (f y) * indicator {x} y = ereal (f x) * indicator {x} y"
hoelzl@58587
   650
    by (simp split: split_indicator)
hoelzl@58587
   651
  fix x show "measure (density (count_space UNIV) (ereal \<circ> f)) {x} = f x"
hoelzl@59092
   652
    by transfer (simp add: measure_def emeasure_density nonneg max_def)
hoelzl@58587
   653
qed
hoelzl@58587
   654
hoelzl@58587
   655
end
hoelzl@58587
   656
hoelzl@58587
   657
lemma embed_pmf_transfer:
hoelzl@58730
   658
  "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
   659
  by (auto simp: rel_fun_def eq_onp_def embed_pmf.transfer)
hoelzl@58587
   660
hoelzl@59000
   661
lemma measure_pmf_eq_density: "measure_pmf p = density (count_space UNIV) (pmf p)"
hoelzl@59000
   662
proof (transfer, elim conjE)
hoelzl@59000
   663
  fix M :: "'a measure" assume [simp]: "sets M = UNIV" and ae: "AE x in M. measure M {x} \<noteq> 0"
hoelzl@59000
   664
  assume "prob_space M" then interpret prob_space M .
hoelzl@59000
   665
  show "M = density (count_space UNIV) (\<lambda>x. ereal (measure M {x}))"
hoelzl@59000
   666
  proof (rule measure_eqI)
hoelzl@59000
   667
    fix A :: "'a set"
hoelzl@59000
   668
    have "(\<integral>\<^sup>+ x. ereal (measure M {x}) * indicator A x \<partial>count_space UNIV) = 
hoelzl@59000
   669
      (\<integral>\<^sup>+ x. emeasure M {x} * indicator (A \<inter> {x. measure M {x} \<noteq> 0}) x \<partial>count_space UNIV)"
hoelzl@59000
   670
      by (auto intro!: nn_integral_cong simp: emeasure_eq_measure split: split_indicator)
hoelzl@59000
   671
    also have "\<dots> = (\<integral>\<^sup>+ x. emeasure M {x} \<partial>count_space (A \<inter> {x. measure M {x} \<noteq> 0}))"
hoelzl@59000
   672
      by (subst nn_integral_restrict_space[symmetric]) (auto simp: restrict_count_space)
hoelzl@59000
   673
    also have "\<dots> = emeasure M (\<Union>x\<in>(A \<inter> {x. measure M {x} \<noteq> 0}). {x})"
hoelzl@59000
   674
      by (intro emeasure_UN_countable[symmetric] countable_Int2 countable_support)
hoelzl@59000
   675
         (auto simp: disjoint_family_on_def)
hoelzl@59000
   676
    also have "\<dots> = emeasure M A"
hoelzl@59000
   677
      using ae by (intro emeasure_eq_AE) auto
hoelzl@59000
   678
    finally show " emeasure M A = emeasure (density (count_space UNIV) (\<lambda>x. ereal (measure M {x}))) A"
hoelzl@59000
   679
      using emeasure_space_1 by (simp add: emeasure_density)
hoelzl@59000
   680
  qed simp
hoelzl@59000
   681
qed
hoelzl@59000
   682
hoelzl@58587
   683
lemma td_pmf_embed_pmf:
hoelzl@58587
   684
  "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
   685
  unfolding type_definition_def
hoelzl@58587
   686
proof safe
hoelzl@58587
   687
  fix p :: "'a pmf"
hoelzl@58587
   688
  have "(\<integral>\<^sup>+ x. 1 \<partial>measure_pmf p) = 1"
hoelzl@58587
   689
    using measure_pmf.emeasure_space_1[of p] by simp
hoelzl@58587
   690
  then show *: "(\<integral>\<^sup>+ x. ereal (pmf p x) \<partial>count_space UNIV) = 1"
hoelzl@58587
   691
    by (simp add: measure_pmf_eq_density nn_integral_density pmf_nonneg del: nn_integral_const)
hoelzl@58587
   692
hoelzl@58587
   693
  show "embed_pmf (pmf p) = p"
hoelzl@58587
   694
    by (intro measure_pmf_inject[THEN iffD1])
hoelzl@58587
   695
       (simp add: * embed_pmf.rep_eq pmf_nonneg measure_pmf_eq_density[of p] comp_def)
hoelzl@58587
   696
next
hoelzl@58587
   697
  fix f :: "'a \<Rightarrow> real" assume "\<forall>x. 0 \<le> f x" "(\<integral>\<^sup>+x. f x \<partial>count_space UNIV) = 1"
hoelzl@58587
   698
  then show "pmf (embed_pmf f) = f"
hoelzl@58587
   699
    by (auto intro!: pmf_embed_pmf)
hoelzl@58587
   700
qed (rule pmf_nonneg)
hoelzl@58587
   701
hoelzl@58587
   702
end
hoelzl@58587
   703
hoelzl@58587
   704
locale pmf_as_function
hoelzl@58587
   705
begin
hoelzl@58587
   706
hoelzl@58587
   707
setup_lifting td_pmf_embed_pmf
hoelzl@58587
   708
hoelzl@58730
   709
lemma set_pmf_transfer[transfer_rule]: 
hoelzl@58730
   710
  assumes "bi_total A"
hoelzl@58730
   711
  shows "rel_fun (pcr_pmf A) (rel_set A) (\<lambda>f. {x. f x \<noteq> 0}) set_pmf"  
hoelzl@58730
   712
  using `bi_total A`
hoelzl@58730
   713
  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
   714
     metis+
hoelzl@58730
   715
hoelzl@59000
   716
end
hoelzl@59000
   717
hoelzl@59000
   718
context
hoelzl@59000
   719
begin
hoelzl@59000
   720
hoelzl@59000
   721
interpretation pmf_as_function .
hoelzl@59000
   722
hoelzl@59000
   723
lemma pmf_eqI: "(\<And>i. pmf M i = pmf N i) \<Longrightarrow> M = N"
hoelzl@59000
   724
  by transfer auto
hoelzl@59000
   725
hoelzl@59000
   726
lemma pmf_eq_iff: "M = N \<longleftrightarrow> (\<forall>i. pmf M i = pmf N i)"
hoelzl@59000
   727
  by (auto intro: pmf_eqI)
hoelzl@59000
   728
hoelzl@59664
   729
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
   730
  unfolding pmf_eq_iff pmf_bind
hoelzl@59664
   731
proof
hoelzl@59664
   732
  fix i
hoelzl@59664
   733
  interpret B: prob_space "restrict_space B B"
hoelzl@59664
   734
    by (intro prob_space_restrict_space measure_pmf.emeasure_eq_1_AE)
hoelzl@59664
   735
       (auto simp: AE_measure_pmf_iff)
hoelzl@59664
   736
  interpret A: prob_space "restrict_space A A"
hoelzl@59664
   737
    by (intro prob_space_restrict_space measure_pmf.emeasure_eq_1_AE)
hoelzl@59664
   738
       (auto simp: AE_measure_pmf_iff)
hoelzl@59664
   739
hoelzl@59664
   740
  interpret AB: pair_prob_space "restrict_space A A" "restrict_space B B"
hoelzl@59664
   741
    by unfold_locales
hoelzl@59664
   742
hoelzl@59664
   743
  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
   744
    by (rule integral_cong) (auto intro!: integral_pmf_restrict)
hoelzl@59664
   745
  also have "\<dots> = (\<integral> x. (\<integral> y. pmf (C x y) i \<partial>restrict_space B B) \<partial>restrict_space A A)"
hoelzl@59664
   746
    by (intro integral_pmf_restrict B.borel_measurable_lebesgue_integral measurable_pair_restrict_pmf2
hoelzl@59664
   747
              countable_set_pmf borel_measurable_count_space)
hoelzl@59664
   748
  also have "\<dots> = (\<integral> y. \<integral> x. pmf (C x y) i \<partial>restrict_space A A \<partial>restrict_space B B)"
hoelzl@59664
   749
    by (rule AB.Fubini_integral[symmetric])
hoelzl@59664
   750
       (auto intro!: AB.integrable_const_bound[where B=1] measurable_pair_restrict_pmf2
hoelzl@59664
   751
             simp: pmf_nonneg pmf_le_1 measurable_restrict_space1)
hoelzl@59664
   752
  also have "\<dots> = (\<integral> y. \<integral> x. pmf (C x y) i \<partial>restrict_space A A \<partial>B)"
hoelzl@59664
   753
    by (intro integral_pmf_restrict[symmetric] A.borel_measurable_lebesgue_integral measurable_pair_restrict_pmf2
hoelzl@59664
   754
              countable_set_pmf borel_measurable_count_space)
hoelzl@59664
   755
  also have "\<dots> = (\<integral> y. \<integral> x. pmf (C x y) i \<partial>A \<partial>B)"
hoelzl@59664
   756
    by (rule integral_cong) (auto intro!: integral_pmf_restrict[symmetric])
hoelzl@59664
   757
  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
   758
qed
hoelzl@59664
   759
hoelzl@59664
   760
lemma pair_map_pmf1: "pair_pmf (map_pmf f A) B = map_pmf (apfst f) (pair_pmf A B)"
hoelzl@59664
   761
proof (safe intro!: pmf_eqI)
hoelzl@59664
   762
  fix a :: "'a" and b :: "'b"
hoelzl@59664
   763
  have [simp]: "\<And>c d. indicator (apfst f -` {(a, b)}) (c, d) = indicator (f -` {a}) c * (indicator {b} d::ereal)"
hoelzl@59664
   764
    by (auto split: split_indicator)
hoelzl@59664
   765
hoelzl@59664
   766
  have "ereal (pmf (pair_pmf (map_pmf f A) B) (a, b)) =
hoelzl@59664
   767
         ereal (pmf (map_pmf (apfst f) (pair_pmf A B)) (a, b))"
hoelzl@59664
   768
    unfolding pmf_pair ereal_pmf_map
hoelzl@59664
   769
    by (simp add: nn_integral_pair_pmf' max_def emeasure_pmf_single nn_integral_multc pmf_nonneg
hoelzl@59664
   770
                  emeasure_map_pmf[symmetric] del: emeasure_map_pmf)
hoelzl@59664
   771
  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
   772
    by simp
hoelzl@59664
   773
qed
hoelzl@59664
   774
hoelzl@59664
   775
lemma pair_map_pmf2: "pair_pmf A (map_pmf f B) = map_pmf (apsnd f) (pair_pmf A B)"
hoelzl@59664
   776
proof (safe intro!: pmf_eqI)
hoelzl@59664
   777
  fix a :: "'a" and b :: "'b"
hoelzl@59664
   778
  have [simp]: "\<And>c d. indicator (apsnd f -` {(a, b)}) (c, d) = indicator {a} c * (indicator (f -` {b}) d::ereal)"
hoelzl@59664
   779
    by (auto split: split_indicator)
hoelzl@59664
   780
hoelzl@59664
   781
  have "ereal (pmf (pair_pmf A (map_pmf f B)) (a, b)) =
hoelzl@59664
   782
         ereal (pmf (map_pmf (apsnd f) (pair_pmf A B)) (a, b))"
hoelzl@59664
   783
    unfolding pmf_pair ereal_pmf_map
hoelzl@59664
   784
    by (simp add: nn_integral_pair_pmf' max_def emeasure_pmf_single nn_integral_cmult nn_integral_multc pmf_nonneg
hoelzl@59664
   785
                  emeasure_map_pmf[symmetric] del: emeasure_map_pmf)
hoelzl@59664
   786
  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
   787
    by simp
hoelzl@59664
   788
qed
hoelzl@59664
   789
hoelzl@59664
   790
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
   791
  by (simp add: pair_map_pmf2 pair_map_pmf1 map_pmf_comp split_beta')
hoelzl@59664
   792
hoelzl@59000
   793
end
hoelzl@59000
   794
hoelzl@59664
   795
subsection \<open> Conditional Probabilities \<close>
hoelzl@59664
   796
hoelzl@59664
   797
context
hoelzl@59664
   798
  fixes p :: "'a pmf" and s :: "'a set"
hoelzl@59664
   799
  assumes not_empty: "set_pmf p \<inter> s \<noteq> {}"
hoelzl@59664
   800
begin
hoelzl@59664
   801
hoelzl@59664
   802
interpretation pmf_as_measure .
hoelzl@59664
   803
hoelzl@59664
   804
lemma emeasure_measure_pmf_not_zero: "emeasure (measure_pmf p) s \<noteq> 0"
hoelzl@59664
   805
proof
hoelzl@59664
   806
  assume "emeasure (measure_pmf p) s = 0"
hoelzl@59664
   807
  then have "AE x in measure_pmf p. x \<notin> s"
hoelzl@59664
   808
    by (rule AE_I[rotated]) auto
hoelzl@59664
   809
  with not_empty show False
hoelzl@59664
   810
    by (auto simp: AE_measure_pmf_iff)
hoelzl@59664
   811
qed
hoelzl@59664
   812
hoelzl@59664
   813
lemma measure_measure_pmf_not_zero: "measure (measure_pmf p) s \<noteq> 0"
hoelzl@59664
   814
  using emeasure_measure_pmf_not_zero unfolding measure_pmf.emeasure_eq_measure by simp
hoelzl@59664
   815
hoelzl@59664
   816
lift_definition cond_pmf :: "'a pmf" is
hoelzl@59664
   817
  "uniform_measure (measure_pmf p) s"
hoelzl@59664
   818
proof (intro conjI)
hoelzl@59664
   819
  show "prob_space (uniform_measure (measure_pmf p) s)"
hoelzl@59664
   820
    by (intro prob_space_uniform_measure) (auto simp: emeasure_measure_pmf_not_zero)
hoelzl@59664
   821
  show "AE x in uniform_measure (measure_pmf p) s. measure (uniform_measure (measure_pmf p) s) {x} \<noteq> 0"
hoelzl@59664
   822
    by (simp add: emeasure_measure_pmf_not_zero measure_measure_pmf_not_zero AE_uniform_measure
hoelzl@59664
   823
                  AE_measure_pmf_iff set_pmf.rep_eq)
hoelzl@59664
   824
qed simp
hoelzl@59664
   825
hoelzl@59664
   826
lemma pmf_cond: "pmf cond_pmf x = (if x \<in> s then pmf p x / measure p s else 0)"
hoelzl@59664
   827
  by transfer (simp add: emeasure_measure_pmf_not_zero pmf.rep_eq)
hoelzl@59664
   828
hoelzl@59665
   829
lemma set_cond_pmf[simp]: "set_pmf cond_pmf = set_pmf p \<inter> s"
hoelzl@59664
   830
  by (auto simp add: set_pmf_iff pmf_cond measure_measure_pmf_not_zero split: split_if_asm)
hoelzl@59664
   831
hoelzl@59664
   832
end
hoelzl@59664
   833
hoelzl@59664
   834
lemma cond_map_pmf:
hoelzl@59664
   835
  assumes "set_pmf p \<inter> f -` s \<noteq> {}"
hoelzl@59664
   836
  shows "cond_pmf (map_pmf f p) s = map_pmf f (cond_pmf p (f -` s))"
hoelzl@59664
   837
proof -
hoelzl@59664
   838
  have *: "set_pmf (map_pmf f p) \<inter> s \<noteq> {}"
hoelzl@59665
   839
    using assms by auto
hoelzl@59664
   840
  { fix x
hoelzl@59664
   841
    have "ereal (pmf (map_pmf f (cond_pmf p (f -` s))) x) =
hoelzl@59664
   842
      emeasure p (f -` s \<inter> f -` {x}) / emeasure p (f -` s)"
hoelzl@59664
   843
      unfolding ereal_pmf_map cond_pmf.rep_eq[OF assms] by (simp add: nn_integral_uniform_measure)
hoelzl@59664
   844
    also have "f -` s \<inter> f -` {x} = (if x \<in> s then f -` {x} else {})"
hoelzl@59664
   845
      by auto
hoelzl@59664
   846
    also have "emeasure p (if x \<in> s then f -` {x} else {}) / emeasure p (f -` s) =
hoelzl@59664
   847
      ereal (pmf (cond_pmf (map_pmf f p) s) x)"
hoelzl@59664
   848
      using measure_measure_pmf_not_zero[OF *]
hoelzl@59664
   849
      by (simp add: pmf_cond[OF *] ereal_divide' ereal_pmf_map measure_pmf.emeasure_eq_measure[symmetric]
hoelzl@59664
   850
               del: ereal_divide)
hoelzl@59664
   851
    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
   852
      by simp }
hoelzl@59664
   853
  then show ?thesis
hoelzl@59664
   854
    by (intro pmf_eqI) simp
hoelzl@59664
   855
qed
hoelzl@59664
   856
hoelzl@59664
   857
lemma bind_cond_pmf_cancel:
hoelzl@59664
   858
  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
   859
  assumes S_eq: "\<And>x y. x \<in> S y \<Longrightarrow> S x = S y"
hoelzl@59664
   860
  and same: "\<And>x. measure (measure_pmf p) (S x) = measure (measure_pmf q) (S x)"
hoelzl@59664
   861
  shows "bind_pmf p (\<lambda>x. cond_pmf q (S x)) = q" (is "?lhs = _")
hoelzl@59664
   862
proof (rule pmf_eqI)
hoelzl@59664
   863
  { fix x
hoelzl@59664
   864
    assume "x \<in> set_pmf p"
hoelzl@59664
   865
    hence "set_pmf p \<inter> (S x) \<noteq> {}" using in_S by auto
hoelzl@59664
   866
    hence "measure (measure_pmf p) (S x) \<noteq> 0"
hoelzl@59664
   867
      by(auto simp add: measure_pmf.prob_eq_0 AE_measure_pmf_iff)
hoelzl@59664
   868
    with same have "measure (measure_pmf q) (S x) \<noteq> 0" by simp
hoelzl@59664
   869
    hence "set_pmf q \<inter> S x \<noteq> {}"
hoelzl@59664
   870
      by(auto simp add: measure_pmf.prob_eq_0 AE_measure_pmf_iff) }
hoelzl@59664
   871
  note [simp] = this
hoelzl@59664
   872
hoelzl@59664
   873
  fix z
hoelzl@59664
   874
  have pmf_q_z: "z \<notin> S z \<Longrightarrow> pmf q z = 0"
hoelzl@59664
   875
    by(erule contrapos_np)(simp add: pmf_eq_0_set_pmf in_S)
hoelzl@59664
   876
hoelzl@59664
   877
  have "ereal (pmf ?lhs z) = \<integral>\<^sup>+ x. ereal (pmf (cond_pmf q (S x)) z) \<partial>measure_pmf p"
hoelzl@59664
   878
    by(simp add: ereal_pmf_bind)
hoelzl@59664
   879
  also have "\<dots> = \<integral>\<^sup>+ x. ereal (pmf q z / measure p (S z)) * indicator (S z) x \<partial>measure_pmf p"
hoelzl@59664
   880
    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
   881
  also have "\<dots> = pmf q z" using pmf_nonneg[of q z]
hoelzl@59664
   882
    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
   883
  finally show "pmf ?lhs z = pmf q z" by simp
hoelzl@59664
   884
qed
hoelzl@59664
   885
hoelzl@59664
   886
subsection \<open> Relator \<close>
hoelzl@59664
   887
hoelzl@59664
   888
inductive rel_pmf :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> 'a pmf \<Rightarrow> 'b pmf \<Rightarrow> bool"
hoelzl@59664
   889
for R p q
hoelzl@59664
   890
where
hoelzl@59664
   891
  "\<lbrakk> \<And>x y. (x, y) \<in> set_pmf pq \<Longrightarrow> R x y; 
hoelzl@59664
   892
     map_pmf fst pq = p; map_pmf snd pq = q \<rbrakk>
hoelzl@59664
   893
  \<Longrightarrow> rel_pmf R p q"
hoelzl@59664
   894
hoelzl@59664
   895
bnf pmf: "'a pmf" map: map_pmf sets: set_pmf bd : "natLeq" rel: rel_pmf
hoelzl@59664
   896
proof -
hoelzl@59664
   897
  show "map_pmf id = id" by (rule map_pmf_id)
hoelzl@59664
   898
  show "\<And>f g. map_pmf (f \<circ> g) = map_pmf f \<circ> map_pmf g" by (rule map_pmf_compose) 
hoelzl@59664
   899
  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
   900
    by (intro map_pmf_cong refl)
hoelzl@59664
   901
hoelzl@59664
   902
  show "\<And>f::'a \<Rightarrow> 'b. set_pmf \<circ> map_pmf f = op ` f \<circ> set_pmf"
hoelzl@59664
   903
    by (rule pmf_set_map)
hoelzl@59664
   904
hoelzl@59664
   905
  { fix p :: "'s pmf"
hoelzl@59664
   906
    have "(card_of (set_pmf p), card_of (UNIV :: nat set)) \<in> ordLeq"
hoelzl@59664
   907
      by (rule card_of_ordLeqI[where f="to_nat_on (set_pmf p)"])
hoelzl@59664
   908
         (auto intro: countable_set_pmf)
hoelzl@59664
   909
    also have "(card_of (UNIV :: nat set), natLeq) \<in> ordLeq"
hoelzl@59664
   910
      by (metis Field_natLeq card_of_least natLeq_Well_order)
hoelzl@59664
   911
    finally show "(card_of (set_pmf p), natLeq) \<in> ordLeq" . }
hoelzl@59664
   912
hoelzl@59664
   913
  show "\<And>R. rel_pmf R =
hoelzl@59664
   914
         (BNF_Def.Grp {x. set_pmf x \<subseteq> {(x, y). R x y}} (map_pmf fst))\<inverse>\<inverse> OO
hoelzl@59664
   915
         BNF_Def.Grp {x. set_pmf x \<subseteq> {(x, y). R x y}} (map_pmf snd)"
hoelzl@59664
   916
     by (auto simp add: fun_eq_iff BNF_Def.Grp_def OO_def rel_pmf.simps)
hoelzl@59664
   917
hoelzl@59664
   918
  { fix p :: "'a pmf" and f :: "'a \<Rightarrow> 'b" and g x
hoelzl@59664
   919
    assume p: "\<And>z. z \<in> set_pmf p \<Longrightarrow> f z = g z"
hoelzl@59664
   920
      and x: "x \<in> set_pmf p"
hoelzl@59664
   921
    thus "f x = g x" by simp }
hoelzl@59664
   922
hoelzl@59664
   923
  fix R :: "'a => 'b \<Rightarrow> bool" and S :: "'b \<Rightarrow> 'c \<Rightarrow> bool"
hoelzl@59664
   924
  { fix p q r
hoelzl@59664
   925
    assume pq: "rel_pmf R p q"
hoelzl@59664
   926
      and qr:"rel_pmf S q r"
hoelzl@59664
   927
    from pq obtain pq where pq: "\<And>x y. (x, y) \<in> set_pmf pq \<Longrightarrow> R x y"
hoelzl@59664
   928
      and p: "p = map_pmf fst pq" and q: "q = map_pmf snd pq" by cases auto
hoelzl@59664
   929
    from qr obtain qr where qr: "\<And>y z. (y, z) \<in> set_pmf qr \<Longrightarrow> S y z"
hoelzl@59664
   930
      and q': "q = map_pmf fst qr" and r: "r = map_pmf snd qr" by cases auto
hoelzl@59664
   931
hoelzl@59664
   932
    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
   933
    have pr_welldefined: "\<And>y. y \<in> q \<Longrightarrow> qr \<inter> {(y', z). y' = y} \<noteq> {}"
hoelzl@59665
   934
      by (force simp: q')
hoelzl@59664
   935
hoelzl@59664
   936
    have "rel_pmf (R OO S) p r"
hoelzl@59664
   937
    proof (rule rel_pmf.intros)
hoelzl@59664
   938
      fix x z assume "(x, z) \<in> pr"
hoelzl@59664
   939
      then have "\<exists>y. (x, y) \<in> pq \<and> (y, z) \<in> qr"
hoelzl@59665
   940
        by (auto simp: q pr_welldefined pr_def split_beta)
hoelzl@59664
   941
      with pq qr show "(R OO S) x z"
hoelzl@59664
   942
        by blast
hoelzl@59664
   943
    next
hoelzl@59664
   944
      have "map_pmf snd pr = map_pmf snd (bind_pmf q (\<lambda>y. cond_pmf qr {(y', z). y' = y}))"
hoelzl@59664
   945
        by (simp add: pr_def q split_beta bind_map_pmf map_pmf_def[symmetric] map_bind_pmf map_return_pmf)
hoelzl@59664
   946
      then show "map_pmf snd pr = r"
hoelzl@59664
   947
        unfolding r q' bind_map_pmf by (subst (asm) bind_cond_pmf_cancel) auto
hoelzl@59664
   948
    qed (simp add: pr_def map_bind_pmf split_beta map_return_pmf map_pmf_def[symmetric] p) }
hoelzl@59664
   949
  then show "rel_pmf R OO rel_pmf S \<le> rel_pmf (R OO S)"
hoelzl@59664
   950
    by(auto simp add: le_fun_def)
hoelzl@59664
   951
qed (fact natLeq_card_order natLeq_cinfinite)+
hoelzl@59664
   952
hoelzl@59665
   953
lemma rel_pmf_conj[simp]:
hoelzl@59665
   954
  "rel_pmf (\<lambda>x y. P \<and> Q x y) x y \<longleftrightarrow> P \<and> rel_pmf Q x y"
hoelzl@59665
   955
  "rel_pmf (\<lambda>x y. Q x y \<and> P) x y \<longleftrightarrow> P \<and> rel_pmf Q x y"
hoelzl@59665
   956
  using set_pmf_not_empty by (fastforce simp: pmf.in_rel subset_eq)+
hoelzl@59665
   957
hoelzl@59665
   958
lemma rel_pmf_top[simp]: "rel_pmf top = top"
hoelzl@59665
   959
  by (auto simp: pmf.in_rel[abs_def] fun_eq_iff map_fst_pair_pmf map_snd_pair_pmf
hoelzl@59665
   960
           intro: exI[of _ "pair_pmf x y" for x y])
hoelzl@59665
   961
hoelzl@59664
   962
lemma rel_pmf_return_pmf1: "rel_pmf R (return_pmf x) M \<longleftrightarrow> (\<forall>a\<in>M. R x a)"
hoelzl@59664
   963
proof safe
hoelzl@59664
   964
  fix a assume "a \<in> M" "rel_pmf R (return_pmf x) M"
hoelzl@59664
   965
  then obtain pq where *: "\<And>a b. (a, b) \<in> set_pmf pq \<Longrightarrow> R a b"
hoelzl@59664
   966
    and eq: "return_pmf x = map_pmf fst pq" "M = map_pmf snd pq"
hoelzl@59664
   967
    by (force elim: rel_pmf.cases)
hoelzl@59664
   968
  moreover have "set_pmf (return_pmf x) = {x}"
hoelzl@59665
   969
    by simp
hoelzl@59664
   970
  with `a \<in> M` have "(x, a) \<in> pq"
hoelzl@59665
   971
    by (force simp: eq)
hoelzl@59664
   972
  with * show "R x a"
hoelzl@59664
   973
    by auto
hoelzl@59664
   974
qed (auto intro!: rel_pmf.intros[where pq="pair_pmf (return_pmf x) M"]
hoelzl@59665
   975
          simp: map_fst_pair_pmf map_snd_pair_pmf)
hoelzl@59664
   976
hoelzl@59664
   977
lemma rel_pmf_return_pmf2: "rel_pmf R M (return_pmf x) \<longleftrightarrow> (\<forall>a\<in>M. R a x)"
hoelzl@59664
   978
  by (subst pmf.rel_flip[symmetric]) (simp add: rel_pmf_return_pmf1)
hoelzl@59664
   979
hoelzl@59664
   980
lemma rel_return_pmf[simp]: "rel_pmf R (return_pmf x1) (return_pmf x2) = R x1 x2"
hoelzl@59664
   981
  unfolding rel_pmf_return_pmf2 set_return_pmf by simp
hoelzl@59664
   982
hoelzl@59664
   983
lemma rel_pmf_False[simp]: "rel_pmf (\<lambda>x y. False) x y = False"
hoelzl@59664
   984
  unfolding pmf.in_rel fun_eq_iff using set_pmf_not_empty by fastforce
hoelzl@59664
   985
hoelzl@59664
   986
lemma rel_pmf_rel_prod:
hoelzl@59664
   987
  "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
   988
proof safe
hoelzl@59664
   989
  assume "rel_pmf (rel_prod R S) (pair_pmf A A') (pair_pmf B B')"
hoelzl@59664
   990
  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
   991
    and eq: "map_pmf fst pq = pair_pmf A A'" "map_pmf snd pq = pair_pmf B B'"
hoelzl@59664
   992
    by (force elim: rel_pmf.cases)
hoelzl@59664
   993
  show "rel_pmf R A B"
hoelzl@59664
   994
  proof (rule rel_pmf.intros)
hoelzl@59664
   995
    let ?f = "\<lambda>(a, b). (fst a, fst b)"
hoelzl@59664
   996
    have [simp]: "(\<lambda>x. fst (?f x)) = fst o fst" "(\<lambda>x. snd (?f x)) = fst o snd"
hoelzl@59664
   997
      by auto
hoelzl@59664
   998
hoelzl@59664
   999
    show "map_pmf fst (map_pmf ?f pq) = A"
hoelzl@59664
  1000
      by (simp add: map_pmf_comp pmf.map_comp[symmetric] eq map_fst_pair_pmf)
hoelzl@59664
  1001
    show "map_pmf snd (map_pmf ?f pq) = B"
hoelzl@59664
  1002
      by (simp add: map_pmf_comp pmf.map_comp[symmetric] eq map_fst_pair_pmf)
hoelzl@59664
  1003
hoelzl@59664
  1004
    fix a b assume "(a, b) \<in> set_pmf (map_pmf ?f pq)"
hoelzl@59664
  1005
    then obtain c d where "((a, c), (b, d)) \<in> set_pmf pq"
hoelzl@59665
  1006
      by auto
hoelzl@59664
  1007
    from pq[OF this] show "R a b" ..
hoelzl@59664
  1008
  qed
hoelzl@59664
  1009
  show "rel_pmf S A' B'"
hoelzl@59664
  1010
  proof (rule rel_pmf.intros)
hoelzl@59664
  1011
    let ?f = "\<lambda>(a, b). (snd a, snd b)"
hoelzl@59664
  1012
    have [simp]: "(\<lambda>x. fst (?f x)) = snd o fst" "(\<lambda>x. snd (?f x)) = snd o snd"
hoelzl@59664
  1013
      by auto
hoelzl@59664
  1014
hoelzl@59664
  1015
    show "map_pmf fst (map_pmf ?f pq) = A'"
hoelzl@59664
  1016
      by (simp add: map_pmf_comp pmf.map_comp[symmetric] eq map_snd_pair_pmf)
hoelzl@59664
  1017
    show "map_pmf snd (map_pmf ?f pq) = B'"
hoelzl@59664
  1018
      by (simp add: map_pmf_comp pmf.map_comp[symmetric] eq map_snd_pair_pmf)
hoelzl@59664
  1019
hoelzl@59664
  1020
    fix c d assume "(c, d) \<in> set_pmf (map_pmf ?f pq)"
hoelzl@59664
  1021
    then obtain a b where "((a, c), (b, d)) \<in> set_pmf pq"
hoelzl@59665
  1022
      by auto
hoelzl@59664
  1023
    from pq[OF this] show "S c d" ..
hoelzl@59664
  1024
  qed
hoelzl@59664
  1025
next
hoelzl@59664
  1026
  assume "rel_pmf R A B" "rel_pmf S A' B'"
hoelzl@59664
  1027
  then obtain Rpq Spq
hoelzl@59664
  1028
    where Rpq: "\<And>a b. (a, b) \<in> set_pmf Rpq \<Longrightarrow> R a b"
hoelzl@59664
  1029
        "map_pmf fst Rpq = A" "map_pmf snd Rpq = B"
hoelzl@59664
  1030
      and Spq: "\<And>a b. (a, b) \<in> set_pmf Spq \<Longrightarrow> S a b"
hoelzl@59664
  1031
        "map_pmf fst Spq = A'" "map_pmf snd Spq = B'"
hoelzl@59664
  1032
    by (force elim: rel_pmf.cases)
hoelzl@59664
  1033
hoelzl@59664
  1034
  let ?f = "(\<lambda>((a, c), (b, d)). ((a, b), (c, d)))"
hoelzl@59664
  1035
  let ?pq = "map_pmf ?f (pair_pmf Rpq Spq)"
hoelzl@59664
  1036
  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
  1037
    by auto
hoelzl@59664
  1038
hoelzl@59664
  1039
  show "rel_pmf (rel_prod R S) (pair_pmf A A') (pair_pmf B B')"
hoelzl@59664
  1040
    by (rule rel_pmf.intros[where pq="?pq"])
hoelzl@59665
  1041
       (auto simp: map_snd_pair_pmf map_fst_pair_pmf map_pmf_comp Rpq Spq
hoelzl@59664
  1042
                   map_pair)
hoelzl@59664
  1043
qed
hoelzl@59664
  1044
hoelzl@59664
  1045
lemma rel_pmf_reflI: 
hoelzl@59664
  1046
  assumes "\<And>x. x \<in> set_pmf p \<Longrightarrow> P x x"
hoelzl@59664
  1047
  shows "rel_pmf P p p"
hoelzl@59665
  1048
  by (rule rel_pmf.intros[where pq="map_pmf (\<lambda>x. (x, x)) p"])
hoelzl@59665
  1049
     (auto simp add: pmf.map_comp o_def assms)
hoelzl@59664
  1050
hoelzl@59664
  1051
context
hoelzl@59664
  1052
begin
hoelzl@59664
  1053
hoelzl@59664
  1054
interpretation pmf_as_measure .
hoelzl@59664
  1055
hoelzl@59664
  1056
definition "join_pmf M = bind_pmf M (\<lambda>x. x)"
hoelzl@59664
  1057
hoelzl@59664
  1058
lemma bind_eq_join_pmf: "bind_pmf M f = join_pmf (map_pmf f M)"
hoelzl@59664
  1059
  unfolding join_pmf_def bind_map_pmf ..
hoelzl@59664
  1060
hoelzl@59664
  1061
lemma join_eq_bind_pmf: "join_pmf M = bind_pmf M id"
hoelzl@59664
  1062
  by (simp add: join_pmf_def id_def)
hoelzl@59664
  1063
hoelzl@59664
  1064
lemma pmf_join: "pmf (join_pmf N) i = (\<integral>M. pmf M i \<partial>measure_pmf N)"
hoelzl@59664
  1065
  unfolding join_pmf_def pmf_bind ..
hoelzl@59664
  1066
hoelzl@59664
  1067
lemma ereal_pmf_join: "ereal (pmf (join_pmf N) i) = (\<integral>\<^sup>+M. pmf M i \<partial>measure_pmf N)"
hoelzl@59664
  1068
  unfolding join_pmf_def ereal_pmf_bind ..
hoelzl@59664
  1069
hoelzl@59665
  1070
lemma set_pmf_join_pmf[simp]: "set_pmf (join_pmf f) = (\<Union>p\<in>set_pmf f. set_pmf p)"
hoelzl@59665
  1071
  by (simp add: join_pmf_def)
hoelzl@59664
  1072
hoelzl@59664
  1073
lemma join_return_pmf: "join_pmf (return_pmf M) = M"
hoelzl@59664
  1074
  by (simp add: integral_return pmf_eq_iff pmf_join return_pmf.rep_eq)
hoelzl@59664
  1075
hoelzl@59664
  1076
lemma map_join_pmf: "map_pmf f (join_pmf AA) = join_pmf (map_pmf (map_pmf f) AA)"
hoelzl@59664
  1077
  by (simp add: join_pmf_def map_pmf_def bind_assoc_pmf bind_return_pmf)
hoelzl@59664
  1078
hoelzl@59664
  1079
lemma join_map_return_pmf: "join_pmf (map_pmf return_pmf A) = A"
hoelzl@59664
  1080
  by (simp add: join_pmf_def map_pmf_def bind_assoc_pmf bind_return_pmf bind_return_pmf')
hoelzl@59664
  1081
hoelzl@59664
  1082
end
hoelzl@59664
  1083
hoelzl@59664
  1084
lemma rel_pmf_joinI:
hoelzl@59664
  1085
  assumes "rel_pmf (rel_pmf P) p q"
hoelzl@59664
  1086
  shows "rel_pmf P (join_pmf p) (join_pmf q)"
hoelzl@59664
  1087
proof -
hoelzl@59664
  1088
  from assms obtain pq where p: "p = map_pmf fst pq"
hoelzl@59664
  1089
    and q: "q = map_pmf snd pq"
hoelzl@59664
  1090
    and P: "\<And>x y. (x, y) \<in> set_pmf pq \<Longrightarrow> rel_pmf P x y"
hoelzl@59664
  1091
    by cases auto
hoelzl@59664
  1092
  from P obtain PQ 
hoelzl@59664
  1093
    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
  1094
    and x: "\<And>x y. (x, y) \<in> set_pmf pq \<Longrightarrow> map_pmf fst (PQ x y) = x"
hoelzl@59664
  1095
    and y: "\<And>x y. (x, y) \<in> set_pmf pq \<Longrightarrow> map_pmf snd (PQ x y) = y"
hoelzl@59664
  1096
    by(metis rel_pmf.simps)
hoelzl@59664
  1097
hoelzl@59664
  1098
  let ?r = "bind_pmf pq (\<lambda>(x, y). PQ x y)"
hoelzl@59665
  1099
  have "\<And>a b. (a, b) \<in> set_pmf ?r \<Longrightarrow> P a b" by (auto intro: PQ)
hoelzl@59664
  1100
  moreover have "map_pmf fst ?r = join_pmf p" "map_pmf snd ?r = join_pmf q"
hoelzl@59664
  1101
    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
  1102
  ultimately show ?thesis ..
hoelzl@59664
  1103
qed
hoelzl@59664
  1104
hoelzl@59664
  1105
lemma rel_pmf_bindI:
hoelzl@59664
  1106
  assumes pq: "rel_pmf R p q"
hoelzl@59664
  1107
  and fg: "\<And>x y. R x y \<Longrightarrow> rel_pmf P (f x) (g y)"
hoelzl@59664
  1108
  shows "rel_pmf P (bind_pmf p f) (bind_pmf q g)"
hoelzl@59664
  1109
  unfolding bind_eq_join_pmf
hoelzl@59664
  1110
  by (rule rel_pmf_joinI)
hoelzl@59664
  1111
     (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
  1112
hoelzl@59664
  1113
text {*
hoelzl@59664
  1114
  Proof that @{const rel_pmf} preserves orders.
hoelzl@59664
  1115
  Antisymmetry proof follows Thm. 1 in N. Saheb-Djahromi, Cpo's of measures for nondeterminism, 
hoelzl@59664
  1116
  Theoretical Computer Science 12(1):19--37, 1980, 
hoelzl@59664
  1117
  @{url "http://dx.doi.org/10.1016/0304-3975(80)90003-1"}
hoelzl@59664
  1118
*}
hoelzl@59664
  1119
hoelzl@59664
  1120
lemma 
hoelzl@59664
  1121
  assumes *: "rel_pmf R p q"
hoelzl@59664
  1122
  and refl: "reflp R" and trans: "transp R"
hoelzl@59664
  1123
  shows measure_Ici: "measure p {y. R x y} \<le> measure q {y. R x y}" (is ?thesis1)
hoelzl@59664
  1124
  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
  1125
proof -
hoelzl@59664
  1126
  from * obtain pq
hoelzl@59664
  1127
    where pq: "\<And>x y. (x, y) \<in> set_pmf pq \<Longrightarrow> R x y"
hoelzl@59664
  1128
    and p: "p = map_pmf fst pq"
hoelzl@59664
  1129
    and q: "q = map_pmf snd pq"
hoelzl@59664
  1130
    by cases auto
hoelzl@59664
  1131
  show ?thesis1 ?thesis2 unfolding p q map_pmf_rep_eq using refl trans
hoelzl@59664
  1132
    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
  1133
qed
hoelzl@59664
  1134
hoelzl@59664
  1135
lemma rel_pmf_inf:
hoelzl@59664
  1136
  fixes p q :: "'a pmf"
hoelzl@59664
  1137
  assumes 1: "rel_pmf R p q"
hoelzl@59664
  1138
  assumes 2: "rel_pmf R q p"
hoelzl@59664
  1139
  and refl: "reflp R" and trans: "transp R"
hoelzl@59664
  1140
  shows "rel_pmf (inf R R\<inverse>\<inverse>) p q"
hoelzl@59664
  1141
proof
hoelzl@59664
  1142
  let ?E = "\<lambda>x. {y. R x y \<and> R y x}"
hoelzl@59664
  1143
  let ?\<mu>E = "\<lambda>x. measure q (?E x)"
hoelzl@59664
  1144
  { fix x
hoelzl@59664
  1145
    have "measure p (?E x) = measure p ({y. R x y} - {y. R x y \<and> \<not> R y x})"
hoelzl@59664
  1146
      by(auto intro!: arg_cong[where f="measure p"])
hoelzl@59664
  1147
    also have "\<dots> = measure p {y. R x y} - measure p {y. R x y \<and> \<not> R y x}"
hoelzl@59664
  1148
      by (rule measure_pmf.finite_measure_Diff) auto
hoelzl@59664
  1149
    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
  1150
      using 1 2 refl trans by(auto intro!: Orderings.antisym measure_Ioi)
hoelzl@59664
  1151
    also have "measure p {y. R x y} = measure q {y. R x y}"
hoelzl@59664
  1152
      using 1 2 refl trans by(auto intro!: Orderings.antisym measure_Ici)
hoelzl@59664
  1153
    also have "measure q {y. R x y} - measure q {y. R x y \<and> ~ R y x} =
hoelzl@59664
  1154
      measure q ({y. R x y} - {y. R x y \<and> \<not> R y x})"
hoelzl@59664
  1155
      by(rule measure_pmf.finite_measure_Diff[symmetric]) auto
hoelzl@59664
  1156
    also have "\<dots> = ?\<mu>E x"
hoelzl@59664
  1157
      by(auto intro!: arg_cong[where f="measure q"])
hoelzl@59664
  1158
    also note calculation }
hoelzl@59664
  1159
  note eq = this
hoelzl@59664
  1160
hoelzl@59664
  1161
  def pq \<equiv> "bind_pmf p (\<lambda>x. bind_pmf (cond_pmf q (?E x)) (\<lambda>y. return_pmf (x, y)))"
hoelzl@59664
  1162
hoelzl@59664
  1163
  show "map_pmf fst pq = p"
hoelzl@59664
  1164
    by(simp add: pq_def map_bind_pmf map_return_pmf bind_return_pmf')
hoelzl@59664
  1165
hoelzl@59664
  1166
  show "map_pmf snd pq = q"
hoelzl@59664
  1167
    unfolding pq_def map_bind_pmf map_return_pmf bind_return_pmf' snd_conv
hoelzl@59664
  1168
    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
  1169
hoelzl@59664
  1170
  fix x y
hoelzl@59664
  1171
  assume "(x, y) \<in> set_pmf pq"
hoelzl@59664
  1172
  moreover
hoelzl@59664
  1173
  { assume "x \<in> set_pmf p"
hoelzl@59664
  1174
    hence "measure (measure_pmf p) (?E x) \<noteq> 0"
hoelzl@59665
  1175
      by (auto simp add: measure_pmf.prob_eq_0 AE_measure_pmf_iff intro: reflpD[OF \<open>reflp R\<close>])
hoelzl@59664
  1176
    hence "measure (measure_pmf q) (?E x) \<noteq> 0" using eq by simp
hoelzl@59664
  1177
    hence "set_pmf q \<inter> {y. R x y \<and> R y x} \<noteq> {}" 
hoelzl@59665
  1178
      by (auto simp add: measure_pmf.prob_eq_0 AE_measure_pmf_iff) }
hoelzl@59664
  1179
  ultimately show "inf R R\<inverse>\<inverse> x y"
hoelzl@59665
  1180
    by (auto simp add: pq_def)
hoelzl@59664
  1181
qed
hoelzl@59664
  1182
hoelzl@59664
  1183
lemma rel_pmf_antisym:
hoelzl@59664
  1184
  fixes p q :: "'a pmf"
hoelzl@59664
  1185
  assumes 1: "rel_pmf R p q"
hoelzl@59664
  1186
  assumes 2: "rel_pmf R q p"
hoelzl@59664
  1187
  and refl: "reflp R" and trans: "transp R" and antisym: "antisymP R"
hoelzl@59664
  1188
  shows "p = q"
hoelzl@59664
  1189
proof -
hoelzl@59664
  1190
  from 1 2 refl trans have "rel_pmf (inf R R\<inverse>\<inverse>) p q" by(rule rel_pmf_inf)
hoelzl@59664
  1191
  also have "inf R R\<inverse>\<inverse> = op ="
hoelzl@59665
  1192
    using refl antisym by (auto intro!: ext simp add: reflpD dest: antisymD)
hoelzl@59664
  1193
  finally show ?thesis unfolding pmf.rel_eq .
hoelzl@59664
  1194
qed
hoelzl@59664
  1195
hoelzl@59664
  1196
lemma reflp_rel_pmf: "reflp R \<Longrightarrow> reflp (rel_pmf R)"
hoelzl@59664
  1197
by(blast intro: reflpI rel_pmf_reflI reflpD)
hoelzl@59664
  1198
hoelzl@59664
  1199
lemma antisymP_rel_pmf:
hoelzl@59664
  1200
  "\<lbrakk> reflp R; transp R; antisymP R \<rbrakk>
hoelzl@59664
  1201
  \<Longrightarrow> antisymP (rel_pmf R)"
hoelzl@59664
  1202
by(rule antisymI)(blast intro: rel_pmf_antisym)
hoelzl@59664
  1203
hoelzl@59664
  1204
lemma transp_rel_pmf:
hoelzl@59664
  1205
  assumes "transp R"
hoelzl@59664
  1206
  shows "transp (rel_pmf R)"
hoelzl@59664
  1207
proof (rule transpI)
hoelzl@59664
  1208
  fix x y z
hoelzl@59664
  1209
  assume "rel_pmf R x y" and "rel_pmf R y z"
hoelzl@59664
  1210
  hence "rel_pmf (R OO R) x z" by (simp add: pmf.rel_compp relcompp.relcompI)
hoelzl@59664
  1211
  thus "rel_pmf R x z"
hoelzl@59664
  1212
    using assms by (metis (no_types) pmf.rel_mono rev_predicate2D transp_relcompp_less_eq)
hoelzl@59664
  1213
qed
hoelzl@59664
  1214
hoelzl@59664
  1215
subsection \<open> Distributions \<close>
hoelzl@59664
  1216
hoelzl@59000
  1217
context
hoelzl@59000
  1218
begin
hoelzl@59000
  1219
hoelzl@59000
  1220
interpretation pmf_as_function .
hoelzl@59000
  1221
hoelzl@59093
  1222
subsubsection \<open> Bernoulli Distribution \<close>
hoelzl@59093
  1223
hoelzl@59000
  1224
lift_definition bernoulli_pmf :: "real \<Rightarrow> bool pmf" is
hoelzl@59000
  1225
  "\<lambda>p b. ((\<lambda>p. if b then p else 1 - p) \<circ> min 1 \<circ> max 0) p"
hoelzl@59000
  1226
  by (auto simp: nn_integral_count_space_finite[where A="{False, True}"] UNIV_bool
hoelzl@59000
  1227
           split: split_max split_min)
hoelzl@59000
  1228
hoelzl@59000
  1229
lemma pmf_bernoulli_True[simp]: "0 \<le> p \<Longrightarrow> p \<le> 1 \<Longrightarrow> pmf (bernoulli_pmf p) True = p"
hoelzl@59000
  1230
  by transfer simp
hoelzl@59000
  1231
hoelzl@59000
  1232
lemma pmf_bernoulli_False[simp]: "0 \<le> p \<Longrightarrow> p \<le> 1 \<Longrightarrow> pmf (bernoulli_pmf p) False = 1 - p"
hoelzl@59000
  1233
  by transfer simp
hoelzl@59000
  1234
hoelzl@59000
  1235
lemma set_pmf_bernoulli: "0 < p \<Longrightarrow> p < 1 \<Longrightarrow> set_pmf (bernoulli_pmf p) = UNIV"
hoelzl@59000
  1236
  by (auto simp add: set_pmf_iff UNIV_bool)
hoelzl@59000
  1237
hoelzl@59002
  1238
lemma nn_integral_bernoulli_pmf[simp]: 
hoelzl@59002
  1239
  assumes [simp]: "0 \<le> p" "p \<le> 1" "\<And>x. 0 \<le> f x"
hoelzl@59002
  1240
  shows "(\<integral>\<^sup>+x. f x \<partial>bernoulli_pmf p) = f True * p + f False * (1 - p)"
hoelzl@59002
  1241
  by (subst nn_integral_measure_pmf_support[of UNIV])
hoelzl@59002
  1242
     (auto simp: UNIV_bool field_simps)
hoelzl@59002
  1243
hoelzl@59002
  1244
lemma integral_bernoulli_pmf[simp]: 
hoelzl@59002
  1245
  assumes [simp]: "0 \<le> p" "p \<le> 1"
hoelzl@59002
  1246
  shows "(\<integral>x. f x \<partial>bernoulli_pmf p) = f True * p + f False * (1 - p)"
hoelzl@59002
  1247
  by (subst integral_measure_pmf[of UNIV]) (auto simp: UNIV_bool)
hoelzl@59002
  1248
Andreas@59525
  1249
lemma pmf_bernoulli_half [simp]: "pmf (bernoulli_pmf (1 / 2)) x = 1 / 2"
Andreas@59525
  1250
by(cases x) simp_all
Andreas@59525
  1251
Andreas@59525
  1252
lemma measure_pmf_bernoulli_half: "measure_pmf (bernoulli_pmf (1 / 2)) = uniform_count_measure UNIV"
Andreas@59525
  1253
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
  1254
hoelzl@59093
  1255
subsubsection \<open> Geometric Distribution \<close>
hoelzl@59093
  1256
hoelzl@59000
  1257
lift_definition geometric_pmf :: "nat pmf" is "\<lambda>n. 1 / 2^Suc n"
hoelzl@59000
  1258
proof
hoelzl@59000
  1259
  note geometric_sums[of "1 / 2"]
hoelzl@59000
  1260
  note sums_mult[OF this, of "1 / 2"]
hoelzl@59000
  1261
  from sums_suminf_ereal[OF this]
hoelzl@59000
  1262
  show "(\<integral>\<^sup>+ x. ereal (1 / 2 ^ Suc x) \<partial>count_space UNIV) = 1"
hoelzl@59000
  1263
    by (simp add: nn_integral_count_space_nat field_simps)
hoelzl@59000
  1264
qed simp
hoelzl@59000
  1265
hoelzl@59000
  1266
lemma pmf_geometric[simp]: "pmf geometric_pmf n = 1 / 2^Suc n"
hoelzl@59000
  1267
  by transfer rule
hoelzl@59000
  1268
hoelzl@59002
  1269
lemma set_pmf_geometric[simp]: "set_pmf geometric_pmf = UNIV"
hoelzl@59000
  1270
  by (auto simp: set_pmf_iff)
hoelzl@59000
  1271
hoelzl@59093
  1272
subsubsection \<open> Uniform Multiset Distribution \<close>
hoelzl@59093
  1273
hoelzl@59000
  1274
context
hoelzl@59000
  1275
  fixes M :: "'a multiset" assumes M_not_empty: "M \<noteq> {#}"
hoelzl@59000
  1276
begin
hoelzl@59000
  1277
hoelzl@59000
  1278
lift_definition pmf_of_multiset :: "'a pmf" is "\<lambda>x. count M x / size M"
hoelzl@59000
  1279
proof
hoelzl@59000
  1280
  show "(\<integral>\<^sup>+ x. ereal (real (count M x) / real (size M)) \<partial>count_space UNIV) = 1"  
hoelzl@59000
  1281
    using M_not_empty
hoelzl@59000
  1282
    by (simp add: zero_less_divide_iff nn_integral_count_space nonempty_has_size
hoelzl@59000
  1283
                  setsum_divide_distrib[symmetric])
hoelzl@59000
  1284
       (auto simp: size_multiset_overloaded_eq intro!: setsum.cong)
hoelzl@59000
  1285
qed simp
hoelzl@59000
  1286
hoelzl@59000
  1287
lemma pmf_of_multiset[simp]: "pmf pmf_of_multiset x = count M x / size M"
hoelzl@59000
  1288
  by transfer rule
hoelzl@59000
  1289
hoelzl@59000
  1290
lemma set_pmf_of_multiset[simp]: "set_pmf pmf_of_multiset = set_of M"
hoelzl@59000
  1291
  by (auto simp: set_pmf_iff)
hoelzl@59000
  1292
hoelzl@59000
  1293
end
hoelzl@59000
  1294
hoelzl@59093
  1295
subsubsection \<open> Uniform Distribution \<close>
hoelzl@59093
  1296
hoelzl@59000
  1297
context
hoelzl@59000
  1298
  fixes S :: "'a set" assumes S_not_empty: "S \<noteq> {}" and S_finite: "finite S"
hoelzl@59000
  1299
begin
hoelzl@59000
  1300
hoelzl@59000
  1301
lift_definition pmf_of_set :: "'a pmf" is "\<lambda>x. indicator S x / card S"
hoelzl@59000
  1302
proof
hoelzl@59000
  1303
  show "(\<integral>\<^sup>+ x. ereal (indicator S x / real (card S)) \<partial>count_space UNIV) = 1"  
hoelzl@59000
  1304
    using S_not_empty S_finite by (subst nn_integral_count_space'[of S]) auto
hoelzl@59000
  1305
qed simp
hoelzl@59000
  1306
hoelzl@59000
  1307
lemma pmf_of_set[simp]: "pmf pmf_of_set x = indicator S x / card S"
hoelzl@59000
  1308
  by transfer rule
hoelzl@59000
  1309
hoelzl@59000
  1310
lemma set_pmf_of_set[simp]: "set_pmf pmf_of_set = S"
hoelzl@59000
  1311
  using S_finite S_not_empty by (auto simp: set_pmf_iff)
hoelzl@59000
  1312
hoelzl@59002
  1313
lemma emeasure_pmf_of_set[simp]: "emeasure pmf_of_set S = 1"
hoelzl@59002
  1314
  by (rule measure_pmf.emeasure_eq_1_AE) (auto simp: AE_measure_pmf_iff)
hoelzl@59002
  1315
hoelzl@59000
  1316
end
hoelzl@59000
  1317
hoelzl@59093
  1318
subsubsection \<open> Poisson Distribution \<close>
hoelzl@59093
  1319
hoelzl@59093
  1320
context
hoelzl@59093
  1321
  fixes rate :: real assumes rate_pos: "0 < rate"
hoelzl@59093
  1322
begin
hoelzl@59093
  1323
hoelzl@59093
  1324
lift_definition poisson_pmf :: "nat pmf" is "\<lambda>k. rate ^ k / fact k * exp (-rate)"
hoelzl@59093
  1325
proof
hoelzl@59093
  1326
  (* Proof by Manuel Eberl *)
hoelzl@59093
  1327
hoelzl@59093
  1328
  have summable: "summable (\<lambda>x::nat. rate ^ x / fact x)" using summable_exp
haftmann@59557
  1329
    by (simp add: field_simps divide_inverse [symmetric])
hoelzl@59093
  1330
  have "(\<integral>\<^sup>+(x::nat). rate ^ x / fact x * exp (-rate) \<partial>count_space UNIV) =
hoelzl@59093
  1331
          exp (-rate) * (\<integral>\<^sup>+(x::nat). rate ^ x / fact x \<partial>count_space UNIV)"
hoelzl@59093
  1332
    by (simp add: field_simps nn_integral_cmult[symmetric])
hoelzl@59093
  1333
  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
  1334
    by (simp_all add: nn_integral_count_space_nat suminf_ereal summable suminf_ereal_finite)
hoelzl@59093
  1335
  also have "... = exp rate" unfolding exp_def
haftmann@59557
  1336
    by (simp add: field_simps divide_inverse [symmetric] transfer_int_nat_factorial)
hoelzl@59093
  1337
  also have "ereal (exp (-rate)) * ereal (exp rate) = 1"
hoelzl@59093
  1338
    by (simp add: mult_exp_exp)
hoelzl@59093
  1339
  finally show "(\<integral>\<^sup>+ x. ereal (rate ^ x / real (fact x) * exp (- rate)) \<partial>count_space UNIV) = 1" .
hoelzl@59093
  1340
qed (simp add: rate_pos[THEN less_imp_le])
hoelzl@59093
  1341
hoelzl@59093
  1342
lemma pmf_poisson[simp]: "pmf poisson_pmf k = rate ^ k / fact k * exp (-rate)"
hoelzl@59093
  1343
  by transfer rule
hoelzl@59093
  1344
hoelzl@59093
  1345
lemma set_pmf_poisson[simp]: "set_pmf poisson_pmf = UNIV"
hoelzl@59093
  1346
  using rate_pos by (auto simp: set_pmf_iff)
hoelzl@59093
  1347
hoelzl@59000
  1348
end
hoelzl@59000
  1349
hoelzl@59093
  1350
subsubsection \<open> Binomial Distribution \<close>
hoelzl@59093
  1351
hoelzl@59093
  1352
context
hoelzl@59093
  1353
  fixes n :: nat and p :: real assumes p_nonneg: "0 \<le> p" and p_le_1: "p \<le> 1"
hoelzl@59093
  1354
begin
hoelzl@59093
  1355
hoelzl@59093
  1356
lift_definition binomial_pmf :: "nat pmf" is "\<lambda>k. (n choose k) * p^k * (1 - p)^(n - k)"
hoelzl@59093
  1357
proof
hoelzl@59093
  1358
  have "(\<integral>\<^sup>+k. ereal (real (n choose k) * p ^ k * (1 - p) ^ (n - k)) \<partial>count_space UNIV) =
hoelzl@59093
  1359
    ereal (\<Sum>k\<le>n. real (n choose k) * p ^ k * (1 - p) ^ (n - k))"
hoelzl@59093
  1360
    using p_le_1 p_nonneg by (subst nn_integral_count_space') auto
hoelzl@59093
  1361
  also have "(\<Sum>k\<le>n. real (n choose k) * p ^ k * (1 - p) ^ (n - k)) = (p + (1 - p)) ^ n"
hoelzl@59093
  1362
    by (subst binomial_ring) (simp add: atLeast0AtMost real_of_nat_def)
hoelzl@59093
  1363
  finally show "(\<integral>\<^sup>+ x. ereal (real (n choose x) * p ^ x * (1 - p) ^ (n - x)) \<partial>count_space UNIV) = 1"
hoelzl@59093
  1364
    by simp
hoelzl@59093
  1365
qed (insert p_nonneg p_le_1, simp)
hoelzl@59093
  1366
hoelzl@59093
  1367
lemma pmf_binomial[simp]: "pmf binomial_pmf k = (n choose k) * p^k * (1 - p)^(n - k)"
hoelzl@59093
  1368
  by transfer rule
hoelzl@59093
  1369
hoelzl@59093
  1370
lemma set_pmf_binomial_eq: "set_pmf binomial_pmf = (if p = 0 then {0} else if p = 1 then {n} else {.. n})"
hoelzl@59093
  1371
  using p_nonneg p_le_1 unfolding set_eq_iff set_pmf_iff pmf_binomial by (auto simp: set_pmf_iff)
hoelzl@59093
  1372
hoelzl@59093
  1373
end
hoelzl@59093
  1374
hoelzl@59093
  1375
end
hoelzl@59093
  1376
hoelzl@59093
  1377
lemma set_pmf_binomial_0[simp]: "set_pmf (binomial_pmf n 0) = {0}"
hoelzl@59093
  1378
  by (simp add: set_pmf_binomial_eq)
hoelzl@59093
  1379
hoelzl@59093
  1380
lemma set_pmf_binomial_1[simp]: "set_pmf (binomial_pmf n 1) = {n}"
hoelzl@59093
  1381
  by (simp add: set_pmf_binomial_eq)
hoelzl@59093
  1382
hoelzl@59093
  1383
lemma set_pmf_binomial[simp]: "0 < p \<Longrightarrow> p < 1 \<Longrightarrow> set_pmf (binomial_pmf n p) = {..n}"
hoelzl@59093
  1384
  by (simp add: set_pmf_binomial_eq)
hoelzl@59093
  1385
hoelzl@59000
  1386
end