src/HOL/Probability/Probability_Mass_Function.thy
author hoelzl
Thu Nov 13 17:19:52 2014 +0100 (2014-11-13)
changeset 59000 6eb0725503fc
parent 58730 b3fd0628f849
child 59002 2c8b2fb54b88
permissions -rw-r--r--
import general theorems from AFP/Markov_Models
hoelzl@58606
     1
(*  Title:      HOL/Probability/Probability_Mass_Function.thy
hoelzl@58606
     2
    Author:     Johannes Hölzl, TU München *)
hoelzl@58606
     3
hoelzl@59000
     4
section \<open> Probability mass function \<close>
hoelzl@59000
     5
hoelzl@58587
     6
theory Probability_Mass_Function
hoelzl@59000
     7
imports
hoelzl@59000
     8
  Giry_Monad
hoelzl@59000
     9
  "~~/src/HOL/Library/Multiset"
hoelzl@58587
    10
begin
hoelzl@58587
    11
hoelzl@59000
    12
lemma (in finite_measure) countable_support: (* replace version in pmf *)
hoelzl@58587
    13
  "countable {x. measure M {x} \<noteq> 0}"
hoelzl@59000
    14
proof cases
hoelzl@59000
    15
  assume "measure M (space M) = 0"
hoelzl@59000
    16
  with bounded_measure measure_le_0_iff have "{x. measure M {x} \<noteq> 0} = {}"
hoelzl@59000
    17
    by auto
hoelzl@59000
    18
  then show ?thesis
hoelzl@59000
    19
    by simp
hoelzl@59000
    20
next
hoelzl@59000
    21
  let ?M = "measure M (space M)" and ?m = "\<lambda>x. measure M {x}"
hoelzl@59000
    22
  assume "?M \<noteq> 0"
hoelzl@59000
    23
  then have *: "{x. ?m x \<noteq> 0} = (\<Union>n. {x. ?M / Suc n < ?m x})"
hoelzl@59000
    24
    using reals_Archimedean[of "?m x / ?M" for x]
hoelzl@59000
    25
    by (auto simp: field_simps not_le[symmetric] measure_nonneg divide_le_0_iff measure_le_0_iff)
hoelzl@59000
    26
  have **: "\<And>n. finite {x. ?M / Suc n < ?m x}"
hoelzl@58587
    27
  proof (rule ccontr)
hoelzl@59000
    28
    fix n assume "infinite {x. ?M / Suc n < ?m x}" (is "infinite ?X")
hoelzl@58587
    29
    then obtain X where "finite X" "card X = Suc (Suc n)" "X \<subseteq> ?X"
hoelzl@58606
    30
      by (metis infinite_arbitrarily_large)
hoelzl@59000
    31
    from this(3) have *: "\<And>x. x \<in> X \<Longrightarrow> ?M / Suc n \<le> ?m x" 
hoelzl@59000
    32
      by auto
hoelzl@58587
    33
    { fix x assume "x \<in> X"
hoelzl@59000
    34
      from `?M \<noteq> 0` *[OF this] have "?m x \<noteq> 0" by (auto simp: field_simps measure_le_0_iff)
hoelzl@58587
    35
      then have "{x} \<in> sets M" by (auto dest: measure_notin_sets) }
hoelzl@58587
    36
    note singleton_sets = this
hoelzl@59000
    37
    have "?M < (\<Sum>x\<in>X. ?M / Suc n)"
hoelzl@59000
    38
      using `?M \<noteq> 0` 
hoelzl@59000
    39
      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
    40
    also have "\<dots> \<le> (\<Sum>x\<in>X. ?m x)"
hoelzl@58587
    41
      by (rule setsum_mono) fact
hoelzl@58587
    42
    also have "\<dots> = measure M (\<Union>x\<in>X. {x})"
hoelzl@58587
    43
      using singleton_sets `finite X`
hoelzl@58587
    44
      by (intro finite_measure_finite_Union[symmetric]) (auto simp: disjoint_family_on_def)
hoelzl@59000
    45
    finally have "?M < measure M (\<Union>x\<in>X. {x})" .
hoelzl@59000
    46
    moreover have "measure M (\<Union>x\<in>X. {x}) \<le> ?M"
hoelzl@59000
    47
      using singleton_sets[THEN sets.sets_into_space] by (intro finite_measure_mono) auto
hoelzl@59000
    48
    ultimately show False by simp
hoelzl@58587
    49
  qed
hoelzl@58587
    50
  show ?thesis
hoelzl@58587
    51
    unfolding * by (intro countable_UN countableI_type countable_finite[OF **])
hoelzl@58587
    52
qed
hoelzl@58587
    53
hoelzl@59000
    54
lemma (in finite_measure) AE_support_countable:
hoelzl@59000
    55
  assumes [simp]: "sets M = UNIV"
hoelzl@59000
    56
  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
    57
proof
hoelzl@59000
    58
  assume "\<exists>S. countable S \<and> (AE x in M. x \<in> S)"
hoelzl@59000
    59
  then obtain S where S[intro]: "countable S" and ae: "AE x in M. x \<in> S"
hoelzl@59000
    60
    by auto
hoelzl@59000
    61
  then have "emeasure M (\<Union>x\<in>{x\<in>S. emeasure M {x} \<noteq> 0}. {x}) = 
hoelzl@59000
    62
    (\<integral>\<^sup>+ x. emeasure M {x} * indicator {x\<in>S. emeasure M {x} \<noteq> 0} x \<partial>count_space UNIV)"
hoelzl@59000
    63
    by (subst emeasure_UN_countable)
hoelzl@59000
    64
       (auto simp: disjoint_family_on_def nn_integral_restrict_space[symmetric] restrict_count_space)
hoelzl@59000
    65
  also have "\<dots> = (\<integral>\<^sup>+ x. emeasure M {x} * indicator S x \<partial>count_space UNIV)"
hoelzl@59000
    66
    by (auto intro!: nn_integral_cong split: split_indicator)
hoelzl@59000
    67
  also have "\<dots> = emeasure M (\<Union>x\<in>S. {x})"
hoelzl@59000
    68
    by (subst emeasure_UN_countable)
hoelzl@59000
    69
       (auto simp: disjoint_family_on_def nn_integral_restrict_space[symmetric] restrict_count_space)
hoelzl@59000
    70
  also have "\<dots> = emeasure M (space M)"
hoelzl@59000
    71
    using ae by (intro emeasure_eq_AE) auto
hoelzl@59000
    72
  finally have "emeasure M {x \<in> space M. x\<in>S \<and> emeasure M {x} \<noteq> 0} = emeasure M (space M)"
hoelzl@59000
    73
    by (simp add: emeasure_single_in_space cong: rev_conj_cong)
hoelzl@59000
    74
  with finite_measure_compl[of "{x \<in> space M. x\<in>S \<and> emeasure M {x} \<noteq> 0}"]
hoelzl@59000
    75
  have "AE x in M. x \<in> S \<and> emeasure M {x} \<noteq> 0"
hoelzl@59000
    76
    by (intro AE_I[OF order_refl]) (auto simp: emeasure_eq_measure set_diff_eq cong: conj_cong)
hoelzl@59000
    77
  then show "AE x in M. measure M {x} \<noteq> 0"
hoelzl@59000
    78
    by (auto simp: emeasure_eq_measure)
hoelzl@59000
    79
qed (auto intro!: exI[of _ "{x. measure M {x} \<noteq> 0}"] countable_support)
hoelzl@59000
    80
hoelzl@59000
    81
subsection {* PMF as measure *}
hoelzl@59000
    82
hoelzl@58587
    83
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
    84
  morphisms measure_pmf Abs_pmf
hoelzl@58606
    85
  by (intro exI[of _ "uniform_measure (count_space UNIV) {undefined}"])
hoelzl@58606
    86
     (auto intro!: prob_space_uniform_measure AE_uniform_measureI)
hoelzl@58587
    87
hoelzl@58587
    88
declare [[coercion measure_pmf]]
hoelzl@58587
    89
hoelzl@58587
    90
lemma prob_space_measure_pmf: "prob_space (measure_pmf p)"
hoelzl@58587
    91
  using pmf.measure_pmf[of p] by auto
hoelzl@58587
    92
hoelzl@58587
    93
interpretation measure_pmf!: prob_space "measure_pmf M" for M
hoelzl@58587
    94
  by (rule prob_space_measure_pmf)
hoelzl@58587
    95
hoelzl@59000
    96
interpretation measure_pmf!: subprob_space "measure_pmf M" for M
hoelzl@59000
    97
  by (rule prob_space_imp_subprob_space) unfold_locales
hoelzl@59000
    98
hoelzl@58587
    99
locale pmf_as_measure
hoelzl@58587
   100
begin
hoelzl@58587
   101
hoelzl@58587
   102
setup_lifting type_definition_pmf
hoelzl@58587
   103
hoelzl@58587
   104
end
hoelzl@58587
   105
hoelzl@58587
   106
context
hoelzl@58587
   107
begin
hoelzl@58587
   108
hoelzl@58587
   109
interpretation pmf_as_measure .
hoelzl@58587
   110
hoelzl@58587
   111
lift_definition pmf :: "'a pmf \<Rightarrow> 'a \<Rightarrow> real" is "\<lambda>M x. measure M {x}" .
hoelzl@58587
   112
hoelzl@58587
   113
lift_definition set_pmf :: "'a pmf \<Rightarrow> 'a set" is "\<lambda>M. {x. measure M {x} \<noteq> 0}" .
hoelzl@58587
   114
hoelzl@58587
   115
lift_definition map_pmf :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a pmf \<Rightarrow> 'b pmf" is
hoelzl@58587
   116
  "\<lambda>f M. distr M (count_space UNIV) f"
hoelzl@58587
   117
proof safe
hoelzl@58587
   118
  fix M and f :: "'a \<Rightarrow> 'b"
hoelzl@58587
   119
  let ?D = "distr M (count_space UNIV) f"
hoelzl@58587
   120
  assume "prob_space M" and [simp]: "sets M = UNIV" and ae: "AE x in M. measure M {x} \<noteq> 0"
hoelzl@58587
   121
  interpret prob_space M by fact
hoelzl@58587
   122
  from ae have "AE x in M. measure M (f -` {f x}) \<noteq> 0"
hoelzl@58587
   123
  proof eventually_elim
hoelzl@58587
   124
    fix x
hoelzl@58587
   125
    have "measure M {x} \<le> measure M (f -` {f x})"
hoelzl@58587
   126
      by (intro finite_measure_mono) auto
hoelzl@58587
   127
    then show "measure M {x} \<noteq> 0 \<Longrightarrow> measure M (f -` {f x}) \<noteq> 0"
hoelzl@58587
   128
      using measure_nonneg[of M "{x}"] by auto
hoelzl@58587
   129
  qed
hoelzl@58587
   130
  then show "AE x in ?D. measure ?D {x} \<noteq> 0"
hoelzl@58587
   131
    by (simp add: AE_distr_iff measure_distr measurable_def)
hoelzl@58587
   132
qed (auto simp: measurable_def prob_space.prob_space_distr)
hoelzl@58587
   133
hoelzl@58587
   134
declare [[coercion set_pmf]]
hoelzl@58587
   135
hoelzl@58587
   136
lemma countable_set_pmf: "countable (set_pmf p)"
hoelzl@59000
   137
  by transfer (metis prob_space.finite_measure finite_measure.countable_support)
hoelzl@58587
   138
hoelzl@58587
   139
lemma sets_measure_pmf[simp]: "sets (measure_pmf p) = UNIV"
hoelzl@58587
   140
  by transfer metis
hoelzl@58587
   141
hoelzl@59000
   142
lemma sets_measure_pmf_count_space: "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@58587
   148
lemma measurable_pmf_measure1[simp]: "measurable (M :: 'a pmf) N = UNIV \<rightarrow> space N"
hoelzl@58587
   149
  by (auto simp: measurable_def)
hoelzl@58587
   150
hoelzl@58587
   151
lemma measurable_pmf_measure2[simp]: "measurable N (M :: 'a pmf) = measurable N (count_space UNIV)"
hoelzl@58587
   152
  by (intro measurable_cong_sets) simp_all
hoelzl@58587
   153
hoelzl@58587
   154
lemma pmf_positive: "x \<in> set_pmf p \<Longrightarrow> 0 < pmf p x"
hoelzl@58587
   155
  by transfer (simp add: less_le measure_nonneg)
hoelzl@58587
   156
hoelzl@58587
   157
lemma pmf_nonneg: "0 \<le> pmf p x"
hoelzl@58587
   158
  by transfer (simp add: measure_nonneg)
hoelzl@58587
   159
hoelzl@59000
   160
lemma pmf_le_1: "pmf p x \<le> 1"
hoelzl@59000
   161
  by (simp add: pmf.rep_eq)
hoelzl@59000
   162
hoelzl@58587
   163
lemma emeasure_pmf_single:
hoelzl@58587
   164
  fixes M :: "'a pmf"
hoelzl@58587
   165
  shows "emeasure M {x} = pmf M x"
hoelzl@58587
   166
  by transfer (simp add: finite_measure.emeasure_eq_measure[OF prob_space.finite_measure])
hoelzl@58587
   167
hoelzl@58587
   168
lemma AE_measure_pmf: "AE x in (M::'a pmf). x \<in> M"
hoelzl@58587
   169
  by transfer simp
hoelzl@58587
   170
hoelzl@58587
   171
lemma emeasure_pmf_single_eq_zero_iff:
hoelzl@58587
   172
  fixes M :: "'a pmf"
hoelzl@58587
   173
  shows "emeasure M {y} = 0 \<longleftrightarrow> y \<notin> M"
hoelzl@58587
   174
  by transfer (simp add: finite_measure.emeasure_eq_measure[OF prob_space.finite_measure])
hoelzl@58587
   175
hoelzl@58587
   176
lemma AE_measure_pmf_iff: "(AE x in measure_pmf M. P x) \<longleftrightarrow> (\<forall>y\<in>M. P y)"
hoelzl@58587
   177
proof -
hoelzl@58587
   178
  { fix y assume y: "y \<in> M" and P: "AE x in M. P x" "\<not> P y"
hoelzl@58587
   179
    with P have "AE x in M. x \<noteq> y"
hoelzl@58587
   180
      by auto
hoelzl@58587
   181
    with y have False
hoelzl@58587
   182
      by (simp add: emeasure_pmf_single_eq_zero_iff AE_iff_measurable[OF _ refl]) }
hoelzl@58587
   183
  then show ?thesis
hoelzl@58587
   184
    using AE_measure_pmf[of M] by auto
hoelzl@58587
   185
qed
hoelzl@58587
   186
hoelzl@58587
   187
lemma set_pmf_not_empty: "set_pmf M \<noteq> {}"
hoelzl@58587
   188
  using AE_measure_pmf[of M] by (intro notI) simp
hoelzl@58587
   189
hoelzl@58587
   190
lemma set_pmf_iff: "x \<in> set_pmf M \<longleftrightarrow> pmf M x \<noteq> 0"
hoelzl@58587
   191
  by transfer simp
hoelzl@58587
   192
hoelzl@59000
   193
lemma emeasure_measure_pmf_finite: "finite S \<Longrightarrow> emeasure (measure_pmf M) S = (\<Sum>s\<in>S. pmf M s)"
hoelzl@59000
   194
  by (subst emeasure_eq_setsum_singleton) (auto simp: emeasure_pmf_single)
hoelzl@59000
   195
hoelzl@59000
   196
lemma nn_integral_measure_pmf_support:
hoelzl@59000
   197
  fixes f :: "'a \<Rightarrow> ereal"
hoelzl@59000
   198
  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
   199
  shows "(\<integral>\<^sup>+x. f x \<partial>measure_pmf M) = (\<Sum>x\<in>A. f x * pmf M x)"
hoelzl@59000
   200
proof -
hoelzl@59000
   201
  have "(\<integral>\<^sup>+x. f x \<partial>M) = (\<integral>\<^sup>+x. f x * indicator A x \<partial>M)"
hoelzl@59000
   202
    using nn by (intro nn_integral_cong_AE) (auto simp: AE_measure_pmf_iff split: split_indicator)
hoelzl@59000
   203
  also have "\<dots> = (\<Sum>x\<in>A. f x * emeasure M {x})"
hoelzl@59000
   204
    using assms by (intro nn_integral_indicator_finite) auto
hoelzl@59000
   205
  finally show ?thesis
hoelzl@59000
   206
    by (simp add: emeasure_measure_pmf_finite)
hoelzl@59000
   207
qed
hoelzl@59000
   208
hoelzl@59000
   209
lemma nn_integral_measure_pmf_finite:
hoelzl@59000
   210
  fixes f :: "'a \<Rightarrow> ereal"
hoelzl@59000
   211
  assumes f: "finite (set_pmf M)" and nn: "\<And>x. x \<in> set_pmf M \<Longrightarrow> 0 \<le> f x"
hoelzl@59000
   212
  shows "(\<integral>\<^sup>+x. f x \<partial>measure_pmf M) = (\<Sum>x\<in>set_pmf M. f x * pmf M x)"
hoelzl@59000
   213
  using assms by (intro nn_integral_measure_pmf_support) auto
hoelzl@59000
   214
lemma integrable_measure_pmf_finite:
hoelzl@59000
   215
  fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}"
hoelzl@59000
   216
  shows "finite (set_pmf M) \<Longrightarrow> integrable M f"
hoelzl@59000
   217
  by (auto intro!: integrableI_bounded simp: nn_integral_measure_pmf_finite)
hoelzl@59000
   218
hoelzl@59000
   219
lemma integral_measure_pmf:
hoelzl@59000
   220
  assumes [simp]: "finite A" and "\<And>a. a \<in> set_pmf M \<Longrightarrow> f a \<noteq> 0 \<Longrightarrow> a \<in> A"
hoelzl@59000
   221
  shows "(\<integral>x. f x \<partial>measure_pmf M) = (\<Sum>a\<in>A. f a * pmf M a)"
hoelzl@59000
   222
proof -
hoelzl@59000
   223
  have "(\<integral>x. f x \<partial>measure_pmf M) = (\<integral>x. f x * indicator A x \<partial>measure_pmf M)"
hoelzl@59000
   224
    using assms(2) by (intro integral_cong_AE) (auto split: split_indicator simp: AE_measure_pmf_iff)
hoelzl@59000
   225
  also have "\<dots> = (\<Sum>a\<in>A. f a * pmf M a)"
hoelzl@59000
   226
    by (subst integral_indicator_finite_real) (auto simp: measure_def emeasure_measure_pmf_finite)
hoelzl@59000
   227
  finally show ?thesis .
hoelzl@59000
   228
qed
hoelzl@59000
   229
hoelzl@59000
   230
lemma integrable_pmf: "integrable (count_space X) (pmf M)"
hoelzl@59000
   231
proof -
hoelzl@59000
   232
  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
   233
    by (auto simp add: nn_integral_count_space_indicator set_pmf_iff intro!: nn_integral_cong split: split_indicator)
hoelzl@59000
   234
  then have "integrable (count_space X) (pmf M) = integrable (count_space (M \<inter> X)) (pmf M)"
hoelzl@59000
   235
    by (simp add: integrable_iff_bounded pmf_nonneg)
hoelzl@59000
   236
  then show ?thesis
hoelzl@59000
   237
    by (simp add: pmf.rep_eq measure_pmf.integrable_measure countable_set_pmf disjoint_family_on_def)
hoelzl@59000
   238
qed
hoelzl@59000
   239
hoelzl@59000
   240
lemma integral_pmf: "(\<integral>x. pmf M x \<partial>count_space X) = measure M X"
hoelzl@59000
   241
proof -
hoelzl@59000
   242
  have "(\<integral>x. pmf M x \<partial>count_space X) = (\<integral>\<^sup>+x. pmf M x \<partial>count_space X)"
hoelzl@59000
   243
    by (simp add: pmf_nonneg integrable_pmf nn_integral_eq_integral)
hoelzl@59000
   244
  also have "\<dots> = (\<integral>\<^sup>+x. emeasure M {x} \<partial>count_space (X \<inter> M))"
hoelzl@59000
   245
    by (auto intro!: nn_integral_cong_AE split: split_indicator
hoelzl@59000
   246
             simp: pmf.rep_eq measure_pmf.emeasure_eq_measure nn_integral_count_space_indicator
hoelzl@59000
   247
                   AE_count_space set_pmf_iff)
hoelzl@59000
   248
  also have "\<dots> = emeasure M (X \<inter> M)"
hoelzl@59000
   249
    by (rule emeasure_countable_singleton[symmetric]) (auto intro: countable_set_pmf)
hoelzl@59000
   250
  also have "\<dots> = emeasure M X"
hoelzl@59000
   251
    by (auto intro!: emeasure_eq_AE simp: AE_measure_pmf_iff)
hoelzl@59000
   252
  finally show ?thesis
hoelzl@59000
   253
    by (simp add: measure_pmf.emeasure_eq_measure)
hoelzl@59000
   254
qed
hoelzl@59000
   255
hoelzl@59000
   256
lemma integral_pmf_restrict:
hoelzl@59000
   257
  "(f::'a \<Rightarrow> 'b::{banach, second_countable_topology}) \<in> borel_measurable (count_space UNIV) \<Longrightarrow>
hoelzl@59000
   258
    (\<integral>x. f x \<partial>measure_pmf M) = (\<integral>x. f x \<partial>restrict_space M M)"
hoelzl@59000
   259
  by (auto intro!: integral_cong_AE simp add: integral_restrict_space AE_measure_pmf_iff)
hoelzl@59000
   260
hoelzl@58587
   261
lemma emeasure_pmf: "emeasure (M::'a pmf) M = 1"
hoelzl@58587
   262
proof -
hoelzl@58587
   263
  have "emeasure (M::'a pmf) M = emeasure (M::'a pmf) (space M)"
hoelzl@58587
   264
    by (intro emeasure_eq_AE) (simp_all add: AE_measure_pmf)
hoelzl@58587
   265
  then show ?thesis
hoelzl@58587
   266
    using measure_pmf.emeasure_space_1 by simp
hoelzl@58587
   267
qed
hoelzl@58587
   268
hoelzl@58587
   269
lemma map_pmf_id[simp]: "map_pmf id = id"
hoelzl@58587
   270
  by (rule, transfer) (auto simp: emeasure_distr measurable_def intro!: measure_eqI)
hoelzl@58587
   271
hoelzl@58587
   272
lemma map_pmf_compose: "map_pmf (f \<circ> g) = map_pmf f \<circ> map_pmf g"
hoelzl@58587
   273
  by (rule, transfer) (simp add: distr_distr[symmetric, where N="count_space UNIV"] measurable_def) 
hoelzl@58587
   274
hoelzl@59000
   275
lemma map_pmf_comp: "map_pmf f (map_pmf g M) = map_pmf (\<lambda>x. f (g x)) M"
hoelzl@59000
   276
  using map_pmf_compose[of f g] by (simp add: comp_def)
hoelzl@59000
   277
hoelzl@58587
   278
lemma map_pmf_cong:
hoelzl@58587
   279
  assumes "p = q"
hoelzl@58587
   280
  shows "(\<And>x. x \<in> set_pmf q \<Longrightarrow> f x = g x) \<Longrightarrow> map_pmf f p = map_pmf g q"
hoelzl@58587
   281
  unfolding `p = q`[symmetric] measure_pmf_inject[symmetric] map_pmf.rep_eq
hoelzl@58587
   282
  by (auto simp add: emeasure_distr AE_measure_pmf_iff intro!: emeasure_eq_AE measure_eqI)
hoelzl@58587
   283
hoelzl@58587
   284
lemma pmf_set_map: 
hoelzl@58587
   285
  fixes f :: "'a \<Rightarrow> 'b"
hoelzl@58587
   286
  shows "set_pmf \<circ> map_pmf f = op ` f \<circ> set_pmf"
hoelzl@58587
   287
proof (rule, transfer, clarsimp simp add: measure_distr measurable_def)
hoelzl@58587
   288
  fix f :: "'a \<Rightarrow> 'b" and M :: "'a measure"
hoelzl@58587
   289
  assume "prob_space M" and ae: "AE x in M. measure M {x} \<noteq> 0" and [simp]: "sets M = UNIV"
hoelzl@58587
   290
  interpret prob_space M by fact
hoelzl@58587
   291
  show "{x. measure M (f -` {x}) \<noteq> 0} = f ` {x. measure M {x} \<noteq> 0}"
hoelzl@58587
   292
  proof safe
hoelzl@58587
   293
    fix x assume "measure M (f -` {x}) \<noteq> 0"
hoelzl@58587
   294
    moreover have "measure M (f -` {x}) = measure M {y. f y = x \<and> measure M {y} \<noteq> 0}"
hoelzl@58587
   295
      using ae by (intro finite_measure_eq_AE) auto
hoelzl@58587
   296
    ultimately have "{y. f y = x \<and> measure M {y} \<noteq> 0} \<noteq> {}"
hoelzl@58587
   297
      by (metis measure_empty)
hoelzl@58587
   298
    then show "x \<in> f ` {x. measure M {x} \<noteq> 0}"
hoelzl@58587
   299
      by auto
hoelzl@58587
   300
  next
hoelzl@58587
   301
    fix x assume "measure M {x} \<noteq> 0"
hoelzl@58587
   302
    then have "0 < measure M {x}"
hoelzl@58587
   303
      using measure_nonneg[of M "{x}"] by auto
hoelzl@58587
   304
    also have "measure M {x} \<le> measure M (f -` {f x})"
hoelzl@58587
   305
      by (intro finite_measure_mono) auto
hoelzl@58587
   306
    finally show "measure M (f -` {f x}) = 0 \<Longrightarrow> False"
hoelzl@58587
   307
      by simp
hoelzl@58587
   308
  qed
hoelzl@58587
   309
qed
hoelzl@58587
   310
hoelzl@59000
   311
lemma set_map_pmf: "set_pmf (map_pmf f M) = f`set_pmf M"
hoelzl@59000
   312
  using pmf_set_map[of f] by (auto simp: comp_def fun_eq_iff)
hoelzl@59000
   313
hoelzl@59000
   314
subsection {* PMFs as function *}
hoelzl@59000
   315
hoelzl@58587
   316
context
hoelzl@58587
   317
  fixes f :: "'a \<Rightarrow> real"
hoelzl@58587
   318
  assumes nonneg: "\<And>x. 0 \<le> f x"
hoelzl@58587
   319
  assumes prob: "(\<integral>\<^sup>+x. f x \<partial>count_space UNIV) = 1"
hoelzl@58587
   320
begin
hoelzl@58587
   321
hoelzl@58587
   322
lift_definition embed_pmf :: "'a pmf" is "density (count_space UNIV) (ereal \<circ> f)"
hoelzl@58587
   323
proof (intro conjI)
hoelzl@58587
   324
  have *[simp]: "\<And>x y. ereal (f y) * indicator {x} y = ereal (f x) * indicator {x} y"
hoelzl@58587
   325
    by (simp split: split_indicator)
hoelzl@58587
   326
  show "AE x in density (count_space UNIV) (ereal \<circ> f).
hoelzl@58587
   327
    measure (density (count_space UNIV) (ereal \<circ> f)) {x} \<noteq> 0"
hoelzl@58587
   328
    by (simp add: AE_density nonneg emeasure_density measure_def nn_integral_cmult_indicator)
hoelzl@58587
   329
  show "prob_space (density (count_space UNIV) (ereal \<circ> f))"
hoelzl@58587
   330
    by default (simp add: emeasure_density prob)
hoelzl@58587
   331
qed simp
hoelzl@58587
   332
hoelzl@58587
   333
lemma pmf_embed_pmf: "pmf embed_pmf x = f x"
hoelzl@58587
   334
proof transfer
hoelzl@58587
   335
  have *[simp]: "\<And>x y. ereal (f y) * indicator {x} y = ereal (f x) * indicator {x} y"
hoelzl@58587
   336
    by (simp split: split_indicator)
hoelzl@58587
   337
  fix x show "measure (density (count_space UNIV) (ereal \<circ> f)) {x} = f x"
hoelzl@58587
   338
    by transfer (simp add: measure_def emeasure_density nn_integral_cmult_indicator nonneg)
hoelzl@58587
   339
qed
hoelzl@58587
   340
hoelzl@58587
   341
end
hoelzl@58587
   342
hoelzl@58587
   343
lemma embed_pmf_transfer:
hoelzl@58730
   344
  "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
   345
  by (auto simp: rel_fun_def eq_onp_def embed_pmf.transfer)
hoelzl@58587
   346
hoelzl@59000
   347
lemma measure_pmf_eq_density: "measure_pmf p = density (count_space UNIV) (pmf p)"
hoelzl@59000
   348
proof (transfer, elim conjE)
hoelzl@59000
   349
  fix M :: "'a measure" assume [simp]: "sets M = UNIV" and ae: "AE x in M. measure M {x} \<noteq> 0"
hoelzl@59000
   350
  assume "prob_space M" then interpret prob_space M .
hoelzl@59000
   351
  show "M = density (count_space UNIV) (\<lambda>x. ereal (measure M {x}))"
hoelzl@59000
   352
  proof (rule measure_eqI)
hoelzl@59000
   353
    fix A :: "'a set"
hoelzl@59000
   354
    have "(\<integral>\<^sup>+ x. ereal (measure M {x}) * indicator A x \<partial>count_space UNIV) = 
hoelzl@59000
   355
      (\<integral>\<^sup>+ x. emeasure M {x} * indicator (A \<inter> {x. measure M {x} \<noteq> 0}) x \<partial>count_space UNIV)"
hoelzl@59000
   356
      by (auto intro!: nn_integral_cong simp: emeasure_eq_measure split: split_indicator)
hoelzl@59000
   357
    also have "\<dots> = (\<integral>\<^sup>+ x. emeasure M {x} \<partial>count_space (A \<inter> {x. measure M {x} \<noteq> 0}))"
hoelzl@59000
   358
      by (subst nn_integral_restrict_space[symmetric]) (auto simp: restrict_count_space)
hoelzl@59000
   359
    also have "\<dots> = emeasure M (\<Union>x\<in>(A \<inter> {x. measure M {x} \<noteq> 0}). {x})"
hoelzl@59000
   360
      by (intro emeasure_UN_countable[symmetric] countable_Int2 countable_support)
hoelzl@59000
   361
         (auto simp: disjoint_family_on_def)
hoelzl@59000
   362
    also have "\<dots> = emeasure M A"
hoelzl@59000
   363
      using ae by (intro emeasure_eq_AE) auto
hoelzl@59000
   364
    finally show " emeasure M A = emeasure (density (count_space UNIV) (\<lambda>x. ereal (measure M {x}))) A"
hoelzl@59000
   365
      using emeasure_space_1 by (simp add: emeasure_density)
hoelzl@59000
   366
  qed simp
hoelzl@59000
   367
qed
hoelzl@59000
   368
hoelzl@58587
   369
lemma td_pmf_embed_pmf:
hoelzl@58587
   370
  "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
   371
  unfolding type_definition_def
hoelzl@58587
   372
proof safe
hoelzl@58587
   373
  fix p :: "'a pmf"
hoelzl@58587
   374
  have "(\<integral>\<^sup>+ x. 1 \<partial>measure_pmf p) = 1"
hoelzl@58587
   375
    using measure_pmf.emeasure_space_1[of p] by simp
hoelzl@58587
   376
  then show *: "(\<integral>\<^sup>+ x. ereal (pmf p x) \<partial>count_space UNIV) = 1"
hoelzl@58587
   377
    by (simp add: measure_pmf_eq_density nn_integral_density pmf_nonneg del: nn_integral_const)
hoelzl@58587
   378
hoelzl@58587
   379
  show "embed_pmf (pmf p) = p"
hoelzl@58587
   380
    by (intro measure_pmf_inject[THEN iffD1])
hoelzl@58587
   381
       (simp add: * embed_pmf.rep_eq pmf_nonneg measure_pmf_eq_density[of p] comp_def)
hoelzl@58587
   382
next
hoelzl@58587
   383
  fix f :: "'a \<Rightarrow> real" assume "\<forall>x. 0 \<le> f x" "(\<integral>\<^sup>+x. f x \<partial>count_space UNIV) = 1"
hoelzl@58587
   384
  then show "pmf (embed_pmf f) = f"
hoelzl@58587
   385
    by (auto intro!: pmf_embed_pmf)
hoelzl@58587
   386
qed (rule pmf_nonneg)
hoelzl@58587
   387
hoelzl@58587
   388
end
hoelzl@58587
   389
hoelzl@58587
   390
locale pmf_as_function
hoelzl@58587
   391
begin
hoelzl@58587
   392
hoelzl@58587
   393
setup_lifting td_pmf_embed_pmf
hoelzl@58587
   394
hoelzl@58730
   395
lemma set_pmf_transfer[transfer_rule]: 
hoelzl@58730
   396
  assumes "bi_total A"
hoelzl@58730
   397
  shows "rel_fun (pcr_pmf A) (rel_set A) (\<lambda>f. {x. f x \<noteq> 0}) set_pmf"  
hoelzl@58730
   398
  using `bi_total A`
hoelzl@58730
   399
  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
   400
     metis+
hoelzl@58730
   401
hoelzl@59000
   402
end
hoelzl@59000
   403
hoelzl@59000
   404
context
hoelzl@59000
   405
begin
hoelzl@59000
   406
hoelzl@59000
   407
interpretation pmf_as_function .
hoelzl@59000
   408
hoelzl@59000
   409
lemma pmf_eqI: "(\<And>i. pmf M i = pmf N i) \<Longrightarrow> M = N"
hoelzl@59000
   410
  by transfer auto
hoelzl@59000
   411
hoelzl@59000
   412
lemma pmf_eq_iff: "M = N \<longleftrightarrow> (\<forall>i. pmf M i = pmf N i)"
hoelzl@59000
   413
  by (auto intro: pmf_eqI)
hoelzl@59000
   414
hoelzl@59000
   415
end
hoelzl@59000
   416
hoelzl@59000
   417
context
hoelzl@59000
   418
begin
hoelzl@59000
   419
hoelzl@59000
   420
interpretation pmf_as_function .
hoelzl@59000
   421
hoelzl@59000
   422
lift_definition bernoulli_pmf :: "real \<Rightarrow> bool pmf" is
hoelzl@59000
   423
  "\<lambda>p b. ((\<lambda>p. if b then p else 1 - p) \<circ> min 1 \<circ> max 0) p"
hoelzl@59000
   424
  by (auto simp: nn_integral_count_space_finite[where A="{False, True}"] UNIV_bool
hoelzl@59000
   425
           split: split_max split_min)
hoelzl@59000
   426
hoelzl@59000
   427
lemma pmf_bernoulli_True[simp]: "0 \<le> p \<Longrightarrow> p \<le> 1 \<Longrightarrow> pmf (bernoulli_pmf p) True = p"
hoelzl@59000
   428
  by transfer simp
hoelzl@59000
   429
hoelzl@59000
   430
lemma pmf_bernoulli_False[simp]: "0 \<le> p \<Longrightarrow> p \<le> 1 \<Longrightarrow> pmf (bernoulli_pmf p) False = 1 - p"
hoelzl@59000
   431
  by transfer simp
hoelzl@59000
   432
hoelzl@59000
   433
lemma set_pmf_bernoulli: "0 < p \<Longrightarrow> p < 1 \<Longrightarrow> set_pmf (bernoulli_pmf p) = UNIV"
hoelzl@59000
   434
  by (auto simp add: set_pmf_iff UNIV_bool)
hoelzl@59000
   435
hoelzl@59000
   436
lift_definition geometric_pmf :: "nat pmf" is "\<lambda>n. 1 / 2^Suc n"
hoelzl@59000
   437
proof
hoelzl@59000
   438
  note geometric_sums[of "1 / 2"]
hoelzl@59000
   439
  note sums_mult[OF this, of "1 / 2"]
hoelzl@59000
   440
  from sums_suminf_ereal[OF this]
hoelzl@59000
   441
  show "(\<integral>\<^sup>+ x. ereal (1 / 2 ^ Suc x) \<partial>count_space UNIV) = 1"
hoelzl@59000
   442
    by (simp add: nn_integral_count_space_nat field_simps)
hoelzl@59000
   443
qed simp
hoelzl@59000
   444
hoelzl@59000
   445
lemma pmf_geometric[simp]: "pmf geometric_pmf n = 1 / 2^Suc n"
hoelzl@59000
   446
  by transfer rule
hoelzl@59000
   447
hoelzl@59000
   448
lemma set_pmf_geometric: "set_pmf geometric_pmf = UNIV"
hoelzl@59000
   449
  by (auto simp: set_pmf_iff)
hoelzl@59000
   450
hoelzl@59000
   451
context
hoelzl@59000
   452
  fixes M :: "'a multiset" assumes M_not_empty: "M \<noteq> {#}"
hoelzl@59000
   453
begin
hoelzl@59000
   454
hoelzl@59000
   455
lift_definition pmf_of_multiset :: "'a pmf" is "\<lambda>x. count M x / size M"
hoelzl@59000
   456
proof
hoelzl@59000
   457
  show "(\<integral>\<^sup>+ x. ereal (real (count M x) / real (size M)) \<partial>count_space UNIV) = 1"  
hoelzl@59000
   458
    using M_not_empty
hoelzl@59000
   459
    by (simp add: zero_less_divide_iff nn_integral_count_space nonempty_has_size
hoelzl@59000
   460
                  setsum_divide_distrib[symmetric])
hoelzl@59000
   461
       (auto simp: size_multiset_overloaded_eq intro!: setsum.cong)
hoelzl@59000
   462
qed simp
hoelzl@59000
   463
hoelzl@59000
   464
lemma pmf_of_multiset[simp]: "pmf pmf_of_multiset x = count M x / size M"
hoelzl@59000
   465
  by transfer rule
hoelzl@59000
   466
hoelzl@59000
   467
lemma set_pmf_of_multiset[simp]: "set_pmf pmf_of_multiset = set_of M"
hoelzl@59000
   468
  by (auto simp: set_pmf_iff)
hoelzl@59000
   469
hoelzl@59000
   470
end
hoelzl@59000
   471
hoelzl@59000
   472
context
hoelzl@59000
   473
  fixes S :: "'a set" assumes S_not_empty: "S \<noteq> {}" and S_finite: "finite S"
hoelzl@59000
   474
begin
hoelzl@59000
   475
hoelzl@59000
   476
lift_definition pmf_of_set :: "'a pmf" is "\<lambda>x. indicator S x / card S"
hoelzl@59000
   477
proof
hoelzl@59000
   478
  show "(\<integral>\<^sup>+ x. ereal (indicator S x / real (card S)) \<partial>count_space UNIV) = 1"  
hoelzl@59000
   479
    using S_not_empty S_finite by (subst nn_integral_count_space'[of S]) auto
hoelzl@59000
   480
qed simp
hoelzl@59000
   481
hoelzl@59000
   482
lemma pmf_of_set[simp]: "pmf pmf_of_set x = indicator S x / card S"
hoelzl@59000
   483
  by transfer rule
hoelzl@59000
   484
hoelzl@59000
   485
lemma set_pmf_of_set[simp]: "set_pmf pmf_of_set = S"
hoelzl@59000
   486
  using S_finite S_not_empty by (auto simp: set_pmf_iff)
hoelzl@59000
   487
hoelzl@59000
   488
end
hoelzl@59000
   489
hoelzl@59000
   490
end
hoelzl@59000
   491
hoelzl@59000
   492
subsection {* Monad interpretation *}
hoelzl@59000
   493
hoelzl@59000
   494
lemma measurable_measure_pmf[measurable]:
hoelzl@59000
   495
  "(\<lambda>x. measure_pmf (M x)) \<in> measurable (count_space UNIV) (subprob_algebra (count_space UNIV))"
hoelzl@59000
   496
  by (auto simp: space_subprob_algebra intro!: prob_space_imp_subprob_space) unfold_locales
hoelzl@59000
   497
hoelzl@59000
   498
lemma bind_pmf_cong:
hoelzl@59000
   499
  assumes "\<And>x. A x \<in> space (subprob_algebra N)" "\<And>x. B x \<in> space (subprob_algebra N)"
hoelzl@59000
   500
  assumes "\<And>i. i \<in> set_pmf x \<Longrightarrow> A i = B i"
hoelzl@59000
   501
  shows "bind (measure_pmf x) A = bind (measure_pmf x) B"
hoelzl@59000
   502
proof (rule measure_eqI)
hoelzl@59000
   503
  show "sets (measure_pmf x \<guillemotright>= A) = sets (measure_pmf x \<guillemotright>= B)"
hoelzl@59000
   504
    using assms by (subst (1 2) sets_bind) auto
hoelzl@59000
   505
next
hoelzl@59000
   506
  fix X assume "X \<in> sets (measure_pmf x \<guillemotright>= A)"
hoelzl@59000
   507
  then have X: "X \<in> sets N"
hoelzl@59000
   508
    using assms by (subst (asm) sets_bind) auto
hoelzl@59000
   509
  show "emeasure (measure_pmf x \<guillemotright>= A) X = emeasure (measure_pmf x \<guillemotright>= B) X"
hoelzl@59000
   510
    using assms
hoelzl@59000
   511
    by (subst (1 2) emeasure_bind[where N=N, OF _ _ X])
hoelzl@59000
   512
       (auto intro!: nn_integral_cong_AE simp: AE_measure_pmf_iff)
hoelzl@59000
   513
qed
hoelzl@59000
   514
hoelzl@59000
   515
context
hoelzl@59000
   516
begin
hoelzl@59000
   517
hoelzl@59000
   518
interpretation pmf_as_measure .
hoelzl@59000
   519
hoelzl@59000
   520
lift_definition join_pmf :: "'a pmf pmf \<Rightarrow> 'a pmf" is "\<lambda>M. measure_pmf M \<guillemotright>= measure_pmf"
hoelzl@59000
   521
proof (intro conjI)
hoelzl@59000
   522
  fix M :: "'a pmf pmf"
hoelzl@59000
   523
hoelzl@59000
   524
  have *: "measure_pmf \<in> measurable (measure_pmf M) (subprob_algebra (count_space UNIV))"
hoelzl@59000
   525
    using measurable_measure_pmf[of "\<lambda>x. x"] by simp
hoelzl@59000
   526
  
hoelzl@59000
   527
  interpret bind: prob_space "measure_pmf M \<guillemotright>= measure_pmf"
hoelzl@59000
   528
    apply (rule measure_pmf.prob_space_bind[OF _ *])
hoelzl@59000
   529
    apply (auto intro!: AE_I2)
hoelzl@59000
   530
    apply unfold_locales
hoelzl@59000
   531
    done
hoelzl@59000
   532
  show "prob_space (measure_pmf M \<guillemotright>= measure_pmf)"
hoelzl@59000
   533
    by intro_locales
hoelzl@59000
   534
  show "sets (measure_pmf M \<guillemotright>= measure_pmf) = UNIV"
hoelzl@59000
   535
    by (subst sets_bind[OF *]) auto
hoelzl@59000
   536
  have "AE x in measure_pmf M \<guillemotright>= measure_pmf. emeasure (measure_pmf M \<guillemotright>= measure_pmf) {x} \<noteq> 0"
hoelzl@59000
   537
    by (auto simp add: AE_bind[OF _ *] AE_measure_pmf_iff emeasure_bind[OF _ *]
hoelzl@59000
   538
        nn_integral_0_iff_AE measure_pmf.emeasure_eq_measure measure_le_0_iff set_pmf_iff pmf.rep_eq)
hoelzl@59000
   539
  then show "AE x in measure_pmf M \<guillemotright>= measure_pmf. measure (measure_pmf M \<guillemotright>= measure_pmf) {x} \<noteq> 0"
hoelzl@59000
   540
    unfolding bind.emeasure_eq_measure by simp
hoelzl@59000
   541
qed
hoelzl@59000
   542
hoelzl@59000
   543
lemma pmf_join: "pmf (join_pmf N) i = (\<integral>M. pmf M i \<partial>measure_pmf N)"
hoelzl@59000
   544
proof (transfer fixing: N i)
hoelzl@59000
   545
  have N: "subprob_space (measure_pmf N)"
hoelzl@59000
   546
    by (rule prob_space_imp_subprob_space) intro_locales
hoelzl@59000
   547
  show "measure (measure_pmf N \<guillemotright>= measure_pmf) {i} = integral\<^sup>L (measure_pmf N) (\<lambda>M. measure M {i})"
hoelzl@59000
   548
    using measurable_measure_pmf[of "\<lambda>x. x"]
hoelzl@59000
   549
    by (intro subprob_space.measure_bind[where N="count_space UNIV", OF N]) auto
hoelzl@59000
   550
qed (auto simp: Transfer.Rel_def rel_fun_def cr_pmf_def)
hoelzl@59000
   551
hoelzl@59000
   552
lift_definition return_pmf :: "'a \<Rightarrow> 'a pmf" is "return (count_space UNIV)"
hoelzl@59000
   553
  by (auto intro!: prob_space_return simp: AE_return measure_return)
hoelzl@59000
   554
hoelzl@59000
   555
lemma join_return_pmf: "join_pmf (return_pmf M) = M"
hoelzl@59000
   556
  by (simp add: integral_return pmf_eq_iff pmf_join return_pmf.rep_eq)
hoelzl@59000
   557
hoelzl@59000
   558
lemma map_return_pmf: "map_pmf f (return_pmf x) = return_pmf (f x)"
hoelzl@59000
   559
  by transfer (simp add: distr_return)
hoelzl@59000
   560
hoelzl@59000
   561
lemma set_pmf_return: "set_pmf (return_pmf x) = {x}"
hoelzl@59000
   562
  by transfer (auto simp add: measure_return split: split_indicator)
hoelzl@59000
   563
hoelzl@59000
   564
lemma pmf_return: "pmf (return_pmf x) y = indicator {y} x"
hoelzl@59000
   565
  by transfer (simp add: measure_return)
hoelzl@59000
   566
hoelzl@59000
   567
end
hoelzl@59000
   568
hoelzl@59000
   569
definition "bind_pmf M f = join_pmf (map_pmf f M)"
hoelzl@59000
   570
hoelzl@59000
   571
lemma (in pmf_as_measure) bind_transfer[transfer_rule]:
hoelzl@59000
   572
  "rel_fun pmf_as_measure.cr_pmf (rel_fun (rel_fun op = pmf_as_measure.cr_pmf) pmf_as_measure.cr_pmf) op \<guillemotright>= bind_pmf"
hoelzl@59000
   573
proof (auto simp: pmf_as_measure.cr_pmf_def rel_fun_def bind_pmf_def join_pmf.rep_eq map_pmf.rep_eq)
hoelzl@59000
   574
  fix M f and g :: "'a \<Rightarrow> 'b pmf" assume "\<forall>x. f x = measure_pmf (g x)"
hoelzl@59000
   575
  then have f: "f = (\<lambda>x. measure_pmf (g x))"
hoelzl@59000
   576
    by auto
hoelzl@59000
   577
  show "measure_pmf M \<guillemotright>= f = distr (measure_pmf M) (count_space UNIV) g \<guillemotright>= measure_pmf"
hoelzl@59000
   578
    unfolding f by (subst bind_distr[OF _ measurable_measure_pmf]) auto
hoelzl@59000
   579
qed
hoelzl@59000
   580
hoelzl@59000
   581
lemma pmf_bind: "pmf (bind_pmf N f) i = (\<integral>x. pmf (f x) i \<partial>measure_pmf N)"
hoelzl@59000
   582
  by (auto intro!: integral_distr simp: bind_pmf_def pmf_join map_pmf.rep_eq)
hoelzl@59000
   583
hoelzl@59000
   584
lemma bind_return_pmf: "bind_pmf (return_pmf x) f = f x"
hoelzl@59000
   585
  unfolding bind_pmf_def map_return_pmf join_return_pmf ..
hoelzl@59000
   586
hoelzl@59000
   587
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@59000
   588
  unfolding pmf_eq_iff pmf_bind
hoelzl@59000
   589
proof
hoelzl@59000
   590
  fix i
hoelzl@59000
   591
  interpret B: prob_space "restrict_space B B"
hoelzl@59000
   592
    by (intro prob_space_restrict_space measure_pmf.emeasure_eq_1_AE)
hoelzl@59000
   593
       (auto simp: AE_measure_pmf_iff)
hoelzl@59000
   594
  interpret A: prob_space "restrict_space A A"
hoelzl@59000
   595
    by (intro prob_space_restrict_space measure_pmf.emeasure_eq_1_AE)
hoelzl@59000
   596
       (auto simp: AE_measure_pmf_iff)
hoelzl@59000
   597
hoelzl@59000
   598
  interpret AB: pair_prob_space "restrict_space A A" "restrict_space B B"
hoelzl@59000
   599
    by unfold_locales
hoelzl@59000
   600
hoelzl@59000
   601
  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@59000
   602
    by (rule integral_cong) (auto intro!: integral_pmf_restrict)
hoelzl@59000
   603
  also have "\<dots> = (\<integral> x. (\<integral> y. pmf (C x y) i \<partial>restrict_space B B) \<partial>restrict_space A A)"
hoelzl@59000
   604
    apply (intro integral_pmf_restrict B.borel_measurable_lebesgue_integral)
hoelzl@59000
   605
    apply (auto simp: measurable_split_conv)
hoelzl@59000
   606
    apply (subst measurable_cong_sets)
hoelzl@59000
   607
    apply (rule sets_pair_measure_cong sets_restrict_space_cong sets_measure_pmf_count_space refl)+
hoelzl@59000
   608
    apply (simp add: restrict_count_space)
hoelzl@59000
   609
    apply (rule measurable_compose_countable'[OF _ measurable_snd])
hoelzl@59000
   610
    apply (rule measurable_compose[OF measurable_fst])
hoelzl@59000
   611
    apply (auto intro: countable_set_pmf)
hoelzl@59000
   612
    done
hoelzl@59000
   613
  also have "\<dots> = (\<integral> y. \<integral> x. pmf (C x y) i \<partial>restrict_space A A \<partial>restrict_space B B)"
hoelzl@59000
   614
    apply (rule AB.Fubini_integral[symmetric])
hoelzl@59000
   615
    apply (auto intro!: AB.integrable_const_bound[where B=1] simp: pmf_nonneg pmf_le_1)
hoelzl@59000
   616
    apply (auto simp: measurable_split_conv)
hoelzl@59000
   617
    apply (subst measurable_cong_sets)
hoelzl@59000
   618
    apply (rule sets_pair_measure_cong sets_restrict_space_cong sets_measure_pmf_count_space refl)+
hoelzl@59000
   619
    apply (simp add: restrict_count_space)
hoelzl@59000
   620
    apply (rule measurable_compose_countable'[OF _ measurable_snd])
hoelzl@59000
   621
    apply (rule measurable_compose[OF measurable_fst])
hoelzl@59000
   622
    apply (auto intro: countable_set_pmf)
hoelzl@59000
   623
    done
hoelzl@59000
   624
  also have "\<dots> = (\<integral> y. \<integral> x. pmf (C x y) i \<partial>restrict_space A A \<partial>B)"
hoelzl@59000
   625
    apply (intro integral_pmf_restrict[symmetric] A.borel_measurable_lebesgue_integral)
hoelzl@59000
   626
    apply (auto simp: measurable_split_conv)
hoelzl@59000
   627
    apply (subst measurable_cong_sets)
hoelzl@59000
   628
    apply (rule sets_pair_measure_cong sets_restrict_space_cong sets_measure_pmf_count_space refl)+
hoelzl@59000
   629
    apply (simp add: restrict_count_space)
hoelzl@59000
   630
    apply (rule measurable_compose_countable'[OF _ measurable_snd])
hoelzl@59000
   631
    apply (rule measurable_compose[OF measurable_fst])
hoelzl@59000
   632
    apply (auto intro: countable_set_pmf)
hoelzl@59000
   633
    done
hoelzl@59000
   634
  also have "\<dots> = (\<integral> y. \<integral> x. pmf (C x y) i \<partial>A \<partial>B)"
hoelzl@59000
   635
    by (rule integral_cong) (auto intro!: integral_pmf_restrict[symmetric])
hoelzl@59000
   636
  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@59000
   637
qed
hoelzl@59000
   638
hoelzl@59000
   639
hoelzl@59000
   640
context
hoelzl@59000
   641
begin
hoelzl@59000
   642
hoelzl@59000
   643
interpretation pmf_as_measure .
hoelzl@59000
   644
hoelzl@59000
   645
lemma bind_return_pmf': "bind_pmf N return_pmf = N"
hoelzl@59000
   646
proof (transfer, clarify)
hoelzl@59000
   647
  fix N :: "'a measure" assume "sets N = UNIV" then show "N \<guillemotright>= return (count_space UNIV) = N"
hoelzl@59000
   648
    by (subst return_sets_cong[where N=N]) (simp_all add: bind_return')
hoelzl@59000
   649
qed
hoelzl@59000
   650
hoelzl@59000
   651
lemma bind_return_pmf'': "bind_pmf N (\<lambda>x. return_pmf (f x)) = map_pmf f N"
hoelzl@59000
   652
proof (transfer, clarify)
hoelzl@59000
   653
  fix N :: "'b measure" and f :: "'b \<Rightarrow> 'a" assume "prob_space N" "sets N = UNIV"
hoelzl@59000
   654
  then show "N \<guillemotright>= (\<lambda>x. return (count_space UNIV) (f x)) = distr N (count_space UNIV) f"
hoelzl@59000
   655
    by (subst bind_return_distr[symmetric])
hoelzl@59000
   656
       (auto simp: prob_space.not_empty measurable_def comp_def)
hoelzl@59000
   657
qed
hoelzl@59000
   658
hoelzl@59000
   659
lemma bind_assoc_pmf: "bind_pmf (bind_pmf A B) C = bind_pmf A (\<lambda>x. bind_pmf (B x) C)"
hoelzl@59000
   660
  by transfer
hoelzl@59000
   661
     (auto intro!: bind_assoc[where N="count_space UNIV" and R="count_space UNIV"]
hoelzl@59000
   662
           simp: measurable_def space_subprob_algebra prob_space_imp_subprob_space)
hoelzl@59000
   663
hoelzl@59000
   664
lemma measure_pmf_bind: "measure_pmf (bind_pmf M f) = (measure_pmf M \<guillemotright>= (\<lambda>x. measure_pmf (f x)))"
hoelzl@59000
   665
  by transfer simp
hoelzl@59000
   666
hoelzl@59000
   667
end
hoelzl@59000
   668
hoelzl@59000
   669
definition "pair_pmf A B = bind_pmf A (\<lambda>x. bind_pmf B (\<lambda>y. return_pmf (x, y)))"
hoelzl@59000
   670
hoelzl@59000
   671
lemma pmf_pair: "pmf (pair_pmf M N) (a, b) = pmf M a * pmf N b"
hoelzl@59000
   672
  unfolding pair_pmf_def pmf_bind pmf_return
hoelzl@59000
   673
  apply (subst integral_measure_pmf[where A="{b}"])
hoelzl@59000
   674
  apply (auto simp: indicator_eq_0_iff)
hoelzl@59000
   675
  apply (subst integral_measure_pmf[where A="{a}"])
hoelzl@59000
   676
  apply (auto simp: indicator_eq_0_iff setsum_nonneg_eq_0_iff pmf_nonneg)
hoelzl@59000
   677
  done
hoelzl@59000
   678
hoelzl@59000
   679
lemma bind_pair_pmf:
hoelzl@59000
   680
  assumes M[measurable]: "M \<in> measurable (count_space UNIV \<Otimes>\<^sub>M count_space UNIV) (subprob_algebra N)"
hoelzl@59000
   681
  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@59000
   682
    (is "?L = ?R")
hoelzl@59000
   683
proof (rule measure_eqI)
hoelzl@59000
   684
  have M'[measurable]: "M \<in> measurable (pair_pmf A B) (subprob_algebra N)"
hoelzl@59000
   685
    using M[THEN measurable_space] by (simp_all add: space_pair_measure)
hoelzl@59000
   686
hoelzl@59000
   687
  have sets_eq_N: "sets ?L = N"
hoelzl@59000
   688
    by (simp add: sets_bind[OF M'])
hoelzl@59000
   689
  show "sets ?L = sets ?R"
hoelzl@59000
   690
    unfolding sets_eq_N
hoelzl@59000
   691
    apply (subst sets_bind[where N=N])
hoelzl@59000
   692
    apply (rule measurable_bind)
hoelzl@59000
   693
    apply (rule measurable_compose[OF _ measurable_measure_pmf])
hoelzl@59000
   694
    apply measurable
hoelzl@59000
   695
    apply (auto intro!: sets_pair_measure_cong sets_measure_pmf_count_space)
hoelzl@59000
   696
    done
hoelzl@59000
   697
  fix X assume "X \<in> sets ?L"
hoelzl@59000
   698
  then have X[measurable]: "X \<in> sets N"
hoelzl@59000
   699
    unfolding sets_eq_N .
hoelzl@59000
   700
  then show "emeasure ?L X = emeasure ?R X"
hoelzl@59000
   701
    apply (simp add: emeasure_bind[OF _ M' X])
hoelzl@59000
   702
    unfolding pair_pmf_def measure_pmf_bind[of A]
hoelzl@59000
   703
    apply (subst nn_integral_bind[OF _ emeasure_nonneg])
hoelzl@59000
   704
    apply (rule measurable_compose[OF M' measurable_emeasure_subprob_algebra, OF X])
hoelzl@59000
   705
    apply (subst measurable_cong_sets[OF sets_measure_pmf_count_space refl])
hoelzl@59000
   706
    apply (subst subprob_algebra_cong[OF sets_measure_pmf_count_space])
hoelzl@59000
   707
    apply measurable
hoelzl@59000
   708
    unfolding measure_pmf_bind
hoelzl@59000
   709
    apply (subst nn_integral_bind[OF _ emeasure_nonneg])
hoelzl@59000
   710
    apply (rule measurable_compose[OF M' measurable_emeasure_subprob_algebra, OF X])
hoelzl@59000
   711
    apply (subst measurable_cong_sets[OF sets_measure_pmf_count_space refl])
hoelzl@59000
   712
    apply (subst subprob_algebra_cong[OF sets_measure_pmf_count_space])
hoelzl@59000
   713
    apply measurable
hoelzl@59000
   714
    apply (simp add: nn_integral_measure_pmf_finite set_pmf_return emeasure_nonneg pmf_return one_ereal_def[symmetric])
hoelzl@59000
   715
    apply (subst emeasure_bind[OF _ _ X])
hoelzl@59000
   716
    apply simp
hoelzl@59000
   717
    apply (rule measurable_bind[where N="count_space UNIV"])
hoelzl@59000
   718
    apply (rule measurable_compose[OF _ measurable_measure_pmf])
hoelzl@59000
   719
    apply measurable
hoelzl@59000
   720
    apply (rule sets_pair_measure_cong sets_measure_pmf_count_space refl)+
hoelzl@59000
   721
    apply (subst measurable_cong_sets[OF sets_pair_measure_cong[OF sets_measure_pmf_count_space refl] refl])
hoelzl@59000
   722
    apply simp
hoelzl@59000
   723
    apply (subst emeasure_bind[OF _ _ X])
hoelzl@59000
   724
    apply simp
hoelzl@59000
   725
    apply (rule measurable_compose[OF _ M])
hoelzl@59000
   726
    apply (auto simp: space_pair_measure)
hoelzl@59000
   727
    done
hoelzl@59000
   728
qed
hoelzl@59000
   729
hoelzl@59000
   730
lemma set_pmf_bind: "set_pmf (bind_pmf M N) = (\<Union>M\<in>set_pmf M. set_pmf (N M))"
hoelzl@59000
   731
  apply (simp add: set_eq_iff set_pmf_iff pmf_bind)
hoelzl@59000
   732
  apply (subst integral_nonneg_eq_0_iff_AE)
hoelzl@59000
   733
  apply (auto simp: pmf_nonneg pmf_le_1 AE_measure_pmf_iff
hoelzl@59000
   734
              intro!: measure_pmf.integrable_const_bound[where B=1])
hoelzl@59000
   735
  done
hoelzl@59000
   736
hoelzl@59000
   737
lemma set_pmf_pair_pmf: "set_pmf (pair_pmf A B) = set_pmf A \<times> set_pmf B"
hoelzl@59000
   738
  unfolding pair_pmf_def set_pmf_bind set_pmf_return by auto
hoelzl@58587
   739
hoelzl@58587
   740
(*
hoelzl@58587
   741
hoelzl@58587
   742
definition
hoelzl@58587
   743
  "rel_pmf P d1 d2 \<longleftrightarrow> (\<exists>p3. (\<forall>(x, y) \<in> set_pmf p3. P x y) \<and> map_pmf fst p3 = d1 \<and> map_pmf snd p3 = d2)"
hoelzl@58587
   744
hoelzl@58587
   745
bnf pmf: "'a pmf" map: map_pmf sets: set_pmf bd : "natLeq" rel: pmf_rel
hoelzl@58587
   746
proof -
hoelzl@58587
   747
  show "map_pmf id = id" by (rule map_pmf_id)
hoelzl@58587
   748
  show "\<And>f g. map_pmf (f \<circ> g) = map_pmf f \<circ> map_pmf g" by (rule map_pmf_compose) 
hoelzl@58587
   749
  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@58587
   750
    by (intro map_pmg_cong refl)
hoelzl@58587
   751
hoelzl@58587
   752
  show "\<And>f::'a \<Rightarrow> 'b. set_pmf \<circ> map_pmf f = op ` f \<circ> set_pmf"
hoelzl@58587
   753
    by (rule pmf_set_map)
hoelzl@58587
   754
hoelzl@58587
   755
  { fix p :: "'s pmf"
hoelzl@58587
   756
    have "(card_of (set_pmf p), card_of (UNIV :: nat set)) \<in> ordLeq"
hoelzl@58587
   757
      by (rule card_of_ordLeqI[where f="to_nat_on (set_pmf p)"])
hoelzl@58587
   758
         (auto intro: countable_set_pmf inj_on_to_nat_on)
hoelzl@58587
   759
    also have "(card_of (UNIV :: nat set), natLeq) \<in> ordLeq"
hoelzl@58587
   760
      by (metis Field_natLeq card_of_least natLeq_Well_order)
hoelzl@58587
   761
    finally show "(card_of (set_pmf p), natLeq) \<in> ordLeq" . }
hoelzl@58587
   762
hoelzl@58587
   763
  show "\<And>R. pmf_rel R =
hoelzl@58587
   764
         (BNF_Util.Grp {x. set_pmf x \<subseteq> {(x, y). R x y}} (map_pmf fst))\<inverse>\<inverse> OO
hoelzl@58587
   765
         BNF_Util.Grp {x. set_pmf x \<subseteq> {(x, y). R x y}} (map_pmf snd)"
hoelzl@58587
   766
     by (auto simp add: fun_eq_iff pmf_rel_def BNF_Util.Grp_def OO_def)
hoelzl@58587
   767
hoelzl@58587
   768
  { let ?f = "map_pmf fst" and ?s = "map_pmf snd"
hoelzl@58587
   769
    fix R :: "'a \<Rightarrow> 'b \<Rightarrow> bool" and A assume "\<And>x y. (x, y) \<in> set_pmf A \<Longrightarrow> R x y"
hoelzl@58587
   770
    fix S :: "'b \<Rightarrow> 'c \<Rightarrow> bool" and B assume "\<And>y z. (y, z) \<in> set_pmf B \<Longrightarrow> S y z"
hoelzl@58587
   771
    assume "?f B = ?s A"
hoelzl@58587
   772
    have "\<exists>C. (\<forall>(x, z)\<in>set_pmf C. \<exists>y. R x y \<and> S y z) \<and> ?f C = ?f A \<and> ?s C = ?s B"
hoelzl@58587
   773
      sorry }
hoelzl@58587
   774
oops
hoelzl@58587
   775
  then show "\<And>R::'a \<Rightarrow> 'b \<Rightarrow> bool. \<And>S::'b \<Rightarrow> 'c \<Rightarrow> bool. pmf_rel R OO pmf_rel S \<le> pmf_rel (R OO S)"
hoelzl@58587
   776
      by (auto simp add: subset_eq pmf_rel_def fun_eq_iff OO_def Ball_def)
hoelzl@58587
   777
qed (fact natLeq_card_order natLeq_cinfinite)+
hoelzl@58587
   778
hoelzl@58587
   779
notepad
hoelzl@58587
   780
begin
hoelzl@58587
   781
  fix x y :: "nat \<Rightarrow> real"
hoelzl@58587
   782
  def IJz \<equiv> "rec_nat ((0, 0), \<lambda>_. 0) (\<lambda>n ((I, J), z).
hoelzl@58587
   783
    let a = x I - (\<Sum>j<J. z (I, j)) ; b = y J - (\<Sum>i<I. z (i, J)) in
hoelzl@58587
   784
      ((if a \<le> b then I + 1 else I, if b \<le> a then J + 1 else J), z((I, J) := min a b)))"
hoelzl@58587
   785
  def I == "fst \<circ> fst \<circ> IJz" def J == "snd \<circ> fst \<circ> IJz" def z == "snd \<circ> IJz"
hoelzl@58587
   786
  let ?a = "\<lambda>n. x (I n) - (\<Sum>j<J n. z n (I n, j))" and ?b = "\<lambda>n. y (J n) - (\<Sum>i<I n. z n (i, J n))"
hoelzl@58587
   787
  have IJz_0[simp]: "\<And>p. z 0 p = 0" "I 0 = 0" "J 0 = 0"
hoelzl@58587
   788
    by (simp_all add: I_def J_def z_def IJz_def)
hoelzl@58587
   789
  have z_Suc[simp]: "\<And>n. z (Suc n) = (z n)((I n, J n) := min (?a n) (?b n))"
hoelzl@58587
   790
    by (simp add: z_def I_def J_def IJz_def Let_def split_beta)
hoelzl@58587
   791
  have I_Suc[simp]: "\<And>n. I (Suc n) = (if ?a n \<le> ?b n then I n + 1 else I n)"
hoelzl@58587
   792
    by (simp add: z_def I_def J_def IJz_def Let_def split_beta)
hoelzl@58587
   793
  have J_Suc[simp]: "\<And>n. J (Suc n) = (if ?b n \<le> ?a n then J n + 1 else J n)"
hoelzl@58587
   794
    by (simp add: z_def I_def J_def IJz_def Let_def split_beta)
hoelzl@58587
   795
  
hoelzl@58587
   796
  { fix N have "\<And>p. z N p \<noteq> 0 \<Longrightarrow> \<exists>n<N. p = (I n, J n)"
hoelzl@58587
   797
      by (induct N) (auto simp add: less_Suc_eq split: split_if_asm) }
hoelzl@58587
   798
  
hoelzl@58587
   799
  { fix i n assume "i < I n"
hoelzl@58587
   800
    then have "(\<Sum>j. z n (i, j)) = x i" 
hoelzl@58587
   801
    oops
hoelzl@58587
   802
*)
hoelzl@58587
   803
hoelzl@58587
   804
end
hoelzl@58587
   805