src/HOL/Probability/Probability_Mass_Function.thy
author Andreas Lochbihler
Fri, 21 Nov 2014 12:11:44 +0100
changeset 59023 4999a616336c
parent 59002 2c8b2fb54b88
child 59024 5fcfeae84b96
permissions -rw-r--r--
register pmf as BNF
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
58606
9c66f7c541fb add Giry monad
hoelzl
parents: 58587
diff changeset
     1
(*  Title:      HOL/Probability/Probability_Mass_Function.thy
59023
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
     2
    Author:     Johannes Hölzl, TU München 
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
     3
    Author:     Andreas Lochbihler, ETH Zurich
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
     4
*)
58606
9c66f7c541fb add Giry monad
hoelzl
parents: 58587
diff changeset
     5
59000
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
     6
section \<open> Probability mass function \<close>
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
     7
58587
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
     8
theory Probability_Mass_Function
59000
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
     9
imports
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
    10
  Giry_Monad
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
    11
  "~~/src/HOL/Library/Multiset"
58587
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
    12
begin
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
    13
59000
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
    14
lemma (in finite_measure) countable_support: (* replace version in pmf *)
58587
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
    15
  "countable {x. measure M {x} \<noteq> 0}"
59000
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
    16
proof cases
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
    17
  assume "measure M (space M) = 0"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
    18
  with bounded_measure measure_le_0_iff have "{x. measure M {x} \<noteq> 0} = {}"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
    19
    by auto
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
    20
  then show ?thesis
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
    21
    by simp
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
    22
next
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
    23
  let ?M = "measure M (space M)" and ?m = "\<lambda>x. measure M {x}"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
    24
  assume "?M \<noteq> 0"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
    25
  then have *: "{x. ?m x \<noteq> 0} = (\<Union>n. {x. ?M / Suc n < ?m x})"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
    26
    using reals_Archimedean[of "?m x / ?M" for x]
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
    27
    by (auto simp: field_simps not_le[symmetric] measure_nonneg divide_le_0_iff measure_le_0_iff)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
    28
  have **: "\<And>n. finite {x. ?M / Suc n < ?m x}"
58587
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
    29
  proof (rule ccontr)
59000
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
    30
    fix n assume "infinite {x. ?M / Suc n < ?m x}" (is "infinite ?X")
58587
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
    31
    then obtain X where "finite X" "card X = Suc (Suc n)" "X \<subseteq> ?X"
58606
9c66f7c541fb add Giry monad
hoelzl
parents: 58587
diff changeset
    32
      by (metis infinite_arbitrarily_large)
59000
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
    33
    from this(3) have *: "\<And>x. x \<in> X \<Longrightarrow> ?M / Suc n \<le> ?m x" 
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
    34
      by auto
58587
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
    35
    { fix x assume "x \<in> X"
59000
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
    36
      from `?M \<noteq> 0` *[OF this] have "?m x \<noteq> 0" by (auto simp: field_simps measure_le_0_iff)
58587
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
    37
      then have "{x} \<in> sets M" by (auto dest: measure_notin_sets) }
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
    38
    note singleton_sets = this
59000
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
    39
    have "?M < (\<Sum>x\<in>X. ?M / Suc n)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
    40
      using `?M \<noteq> 0` 
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
    41
      by (simp add: `card X = Suc (Suc n)` real_eq_of_nat[symmetric] real_of_nat_Suc field_simps less_le measure_nonneg)
58587
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
    42
    also have "\<dots> \<le> (\<Sum>x\<in>X. ?m x)"
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
    43
      by (rule setsum_mono) fact
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
    44
    also have "\<dots> = measure M (\<Union>x\<in>X. {x})"
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
    45
      using singleton_sets `finite X`
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
    46
      by (intro finite_measure_finite_Union[symmetric]) (auto simp: disjoint_family_on_def)
59000
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
    47
    finally have "?M < measure M (\<Union>x\<in>X. {x})" .
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
    48
    moreover have "measure M (\<Union>x\<in>X. {x}) \<le> ?M"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
    49
      using singleton_sets[THEN sets.sets_into_space] by (intro finite_measure_mono) auto
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
    50
    ultimately show False by simp
58587
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
    51
  qed
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
    52
  show ?thesis
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
    53
    unfolding * by (intro countable_UN countableI_type countable_finite[OF **])
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
    54
qed
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
    55
59000
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
    56
lemma (in finite_measure) AE_support_countable:
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
    57
  assumes [simp]: "sets M = UNIV"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
    58
  shows "(AE x in M. measure M {x} \<noteq> 0) \<longleftrightarrow> (\<exists>S. countable S \<and> (AE x in M. x \<in> S))"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
    59
proof
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
    60
  assume "\<exists>S. countable S \<and> (AE x in M. x \<in> S)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
    61
  then obtain S where S[intro]: "countable S" and ae: "AE x in M. x \<in> S"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
    62
    by auto
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
    63
  then have "emeasure M (\<Union>x\<in>{x\<in>S. emeasure M {x} \<noteq> 0}. {x}) = 
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
    64
    (\<integral>\<^sup>+ x. emeasure M {x} * indicator {x\<in>S. emeasure M {x} \<noteq> 0} x \<partial>count_space UNIV)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
    65
    by (subst emeasure_UN_countable)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
    66
       (auto simp: disjoint_family_on_def nn_integral_restrict_space[symmetric] restrict_count_space)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
    67
  also have "\<dots> = (\<integral>\<^sup>+ x. emeasure M {x} * indicator S x \<partial>count_space UNIV)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
    68
    by (auto intro!: nn_integral_cong split: split_indicator)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
    69
  also have "\<dots> = emeasure M (\<Union>x\<in>S. {x})"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
    70
    by (subst emeasure_UN_countable)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
    71
       (auto simp: disjoint_family_on_def nn_integral_restrict_space[symmetric] restrict_count_space)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
    72
  also have "\<dots> = emeasure M (space M)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
    73
    using ae by (intro emeasure_eq_AE) auto
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
    74
  finally have "emeasure M {x \<in> space M. x\<in>S \<and> emeasure M {x} \<noteq> 0} = emeasure M (space M)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
    75
    by (simp add: emeasure_single_in_space cong: rev_conj_cong)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
    76
  with finite_measure_compl[of "{x \<in> space M. x\<in>S \<and> emeasure M {x} \<noteq> 0}"]
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
    77
  have "AE x in M. x \<in> S \<and> emeasure M {x} \<noteq> 0"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
    78
    by (intro AE_I[OF order_refl]) (auto simp: emeasure_eq_measure set_diff_eq cong: conj_cong)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
    79
  then show "AE x in M. measure M {x} \<noteq> 0"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
    80
    by (auto simp: emeasure_eq_measure)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
    81
qed (auto intro!: exI[of _ "{x. measure M {x} \<noteq> 0}"] countable_support)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
    82
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
    83
subsection {* PMF as measure *}
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
    84
58587
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
    85
typedef 'a pmf = "{M :: 'a measure. prob_space M \<and> sets M = UNIV \<and> (AE x in M. measure M {x} \<noteq> 0)}"
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
    86
  morphisms measure_pmf Abs_pmf
58606
9c66f7c541fb add Giry monad
hoelzl
parents: 58587
diff changeset
    87
  by (intro exI[of _ "uniform_measure (count_space UNIV) {undefined}"])
9c66f7c541fb add Giry monad
hoelzl
parents: 58587
diff changeset
    88
     (auto intro!: prob_space_uniform_measure AE_uniform_measureI)
58587
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
    89
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
    90
declare [[coercion measure_pmf]]
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
    91
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
    92
lemma prob_space_measure_pmf: "prob_space (measure_pmf p)"
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
    93
  using pmf.measure_pmf[of p] by auto
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
    94
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
    95
interpretation measure_pmf!: prob_space "measure_pmf M" for M
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
    96
  by (rule prob_space_measure_pmf)
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
    97
59000
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
    98
interpretation measure_pmf!: subprob_space "measure_pmf M" for M
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
    99
  by (rule prob_space_imp_subprob_space) unfold_locales
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   100
58587
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   101
locale pmf_as_measure
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   102
begin
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   103
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   104
setup_lifting type_definition_pmf
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   105
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   106
end
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   107
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   108
context
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   109
begin
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   110
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   111
interpretation pmf_as_measure .
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   112
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   113
lift_definition pmf :: "'a pmf \<Rightarrow> 'a \<Rightarrow> real" is "\<lambda>M x. measure M {x}" .
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   114
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   115
lift_definition set_pmf :: "'a pmf \<Rightarrow> 'a set" is "\<lambda>M. {x. measure M {x} \<noteq> 0}" .
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   116
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   117
lift_definition map_pmf :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a pmf \<Rightarrow> 'b pmf" is
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   118
  "\<lambda>f M. distr M (count_space UNIV) f"
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   119
proof safe
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   120
  fix M and f :: "'a \<Rightarrow> 'b"
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   121
  let ?D = "distr M (count_space UNIV) f"
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   122
  assume "prob_space M" and [simp]: "sets M = UNIV" and ae: "AE x in M. measure M {x} \<noteq> 0"
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   123
  interpret prob_space M by fact
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   124
  from ae have "AE x in M. measure M (f -` {f x}) \<noteq> 0"
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   125
  proof eventually_elim
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   126
    fix x
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   127
    have "measure M {x} \<le> measure M (f -` {f x})"
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   128
      by (intro finite_measure_mono) auto
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   129
    then show "measure M {x} \<noteq> 0 \<Longrightarrow> measure M (f -` {f x}) \<noteq> 0"
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   130
      using measure_nonneg[of M "{x}"] by auto
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   131
  qed
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   132
  then show "AE x in ?D. measure ?D {x} \<noteq> 0"
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   133
    by (simp add: AE_distr_iff measure_distr measurable_def)
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   134
qed (auto simp: measurable_def prob_space.prob_space_distr)
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   135
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   136
declare [[coercion set_pmf]]
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   137
59023
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   138
lemma countable_set_pmf [simp]: "countable (set_pmf p)"
59000
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   139
  by transfer (metis prob_space.finite_measure finite_measure.countable_support)
58587
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   140
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   141
lemma sets_measure_pmf[simp]: "sets (measure_pmf p) = UNIV"
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   142
  by transfer metis
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   143
59000
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   144
lemma sets_measure_pmf_count_space: "sets (measure_pmf M) = sets (count_space UNIV)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   145
  by simp
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   146
58587
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   147
lemma space_measure_pmf[simp]: "space (measure_pmf p) = UNIV"
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   148
  using sets_eq_imp_space_eq[of "measure_pmf p" "count_space UNIV"] by simp
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   149
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   150
lemma measurable_pmf_measure1[simp]: "measurable (M :: 'a pmf) N = UNIV \<rightarrow> space N"
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   151
  by (auto simp: measurable_def)
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   152
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   153
lemma measurable_pmf_measure2[simp]: "measurable N (M :: 'a pmf) = measurable N (count_space UNIV)"
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   154
  by (intro measurable_cong_sets) simp_all
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   155
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   156
lemma pmf_positive: "x \<in> set_pmf p \<Longrightarrow> 0 < pmf p x"
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   157
  by transfer (simp add: less_le measure_nonneg)
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   158
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   159
lemma pmf_nonneg: "0 \<le> pmf p x"
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   160
  by transfer (simp add: measure_nonneg)
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   161
59000
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   162
lemma pmf_le_1: "pmf p x \<le> 1"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   163
  by (simp add: pmf.rep_eq)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   164
58587
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   165
lemma emeasure_pmf_single:
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   166
  fixes M :: "'a pmf"
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   167
  shows "emeasure M {x} = pmf M x"
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   168
  by transfer (simp add: finite_measure.emeasure_eq_measure[OF prob_space.finite_measure])
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   169
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   170
lemma AE_measure_pmf: "AE x in (M::'a pmf). x \<in> M"
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   171
  by transfer simp
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   172
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   173
lemma emeasure_pmf_single_eq_zero_iff:
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   174
  fixes M :: "'a pmf"
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   175
  shows "emeasure M {y} = 0 \<longleftrightarrow> y \<notin> M"
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   176
  by transfer (simp add: finite_measure.emeasure_eq_measure[OF prob_space.finite_measure])
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   177
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   178
lemma AE_measure_pmf_iff: "(AE x in measure_pmf M. P x) \<longleftrightarrow> (\<forall>y\<in>M. P y)"
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   179
proof -
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   180
  { fix y assume y: "y \<in> M" and P: "AE x in M. P x" "\<not> P y"
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   181
    with P have "AE x in M. x \<noteq> y"
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   182
      by auto
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   183
    with y have False
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   184
      by (simp add: emeasure_pmf_single_eq_zero_iff AE_iff_measurable[OF _ refl]) }
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   185
  then show ?thesis
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   186
    using AE_measure_pmf[of M] by auto
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   187
qed
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   188
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   189
lemma set_pmf_not_empty: "set_pmf M \<noteq> {}"
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   190
  using AE_measure_pmf[of M] by (intro notI) simp
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   191
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   192
lemma set_pmf_iff: "x \<in> set_pmf M \<longleftrightarrow> pmf M x \<noteq> 0"
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   193
  by transfer simp
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   194
59000
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   195
lemma emeasure_measure_pmf_finite: "finite S \<Longrightarrow> emeasure (measure_pmf M) S = (\<Sum>s\<in>S. pmf M s)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   196
  by (subst emeasure_eq_setsum_singleton) (auto simp: emeasure_pmf_single)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   197
59023
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   198
lemma measure_measure_pmf_finite: "finite S \<Longrightarrow> measure (measure_pmf M) S = setsum (pmf M) S"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   199
using emeasure_measure_pmf_finite[of S M]
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   200
by(simp add: measure_pmf.emeasure_eq_measure)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   201
59000
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   202
lemma nn_integral_measure_pmf_support:
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   203
  fixes f :: "'a \<Rightarrow> ereal"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   204
  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"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   205
  shows "(\<integral>\<^sup>+x. f x \<partial>measure_pmf M) = (\<Sum>x\<in>A. f x * pmf M x)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   206
proof -
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   207
  have "(\<integral>\<^sup>+x. f x \<partial>M) = (\<integral>\<^sup>+x. f x * indicator A x \<partial>M)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   208
    using nn by (intro nn_integral_cong_AE) (auto simp: AE_measure_pmf_iff split: split_indicator)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   209
  also have "\<dots> = (\<Sum>x\<in>A. f x * emeasure M {x})"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   210
    using assms by (intro nn_integral_indicator_finite) auto
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   211
  finally show ?thesis
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   212
    by (simp add: emeasure_measure_pmf_finite)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   213
qed
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   214
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   215
lemma nn_integral_measure_pmf_finite:
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   216
  fixes f :: "'a \<Rightarrow> ereal"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   217
  assumes f: "finite (set_pmf M)" and nn: "\<And>x. x \<in> set_pmf M \<Longrightarrow> 0 \<le> f x"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   218
  shows "(\<integral>\<^sup>+x. f x \<partial>measure_pmf M) = (\<Sum>x\<in>set_pmf M. f x * pmf M x)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   219
  using assms by (intro nn_integral_measure_pmf_support) auto
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   220
lemma integrable_measure_pmf_finite:
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   221
  fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   222
  shows "finite (set_pmf M) \<Longrightarrow> integrable M f"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   223
  by (auto intro!: integrableI_bounded simp: nn_integral_measure_pmf_finite)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   224
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   225
lemma integral_measure_pmf:
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   226
  assumes [simp]: "finite A" and "\<And>a. a \<in> set_pmf M \<Longrightarrow> f a \<noteq> 0 \<Longrightarrow> a \<in> A"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   227
  shows "(\<integral>x. f x \<partial>measure_pmf M) = (\<Sum>a\<in>A. f a * pmf M a)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   228
proof -
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   229
  have "(\<integral>x. f x \<partial>measure_pmf M) = (\<integral>x. f x * indicator A x \<partial>measure_pmf M)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   230
    using assms(2) by (intro integral_cong_AE) (auto split: split_indicator simp: AE_measure_pmf_iff)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   231
  also have "\<dots> = (\<Sum>a\<in>A. f a * pmf M a)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   232
    by (subst integral_indicator_finite_real) (auto simp: measure_def emeasure_measure_pmf_finite)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   233
  finally show ?thesis .
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   234
qed
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   235
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   236
lemma integrable_pmf: "integrable (count_space X) (pmf M)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   237
proof -
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   238
  have " (\<integral>\<^sup>+ x. pmf M x \<partial>count_space X) = (\<integral>\<^sup>+ x. pmf M x \<partial>count_space (M \<inter> X))"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   239
    by (auto simp add: nn_integral_count_space_indicator set_pmf_iff intro!: nn_integral_cong split: split_indicator)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   240
  then have "integrable (count_space X) (pmf M) = integrable (count_space (M \<inter> X)) (pmf M)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   241
    by (simp add: integrable_iff_bounded pmf_nonneg)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   242
  then show ?thesis
59023
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   243
    by (simp add: pmf.rep_eq measure_pmf.integrable_measure disjoint_family_on_def)
59000
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   244
qed
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   245
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   246
lemma integral_pmf: "(\<integral>x. pmf M x \<partial>count_space X) = measure M X"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   247
proof -
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   248
  have "(\<integral>x. pmf M x \<partial>count_space X) = (\<integral>\<^sup>+x. pmf M x \<partial>count_space X)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   249
    by (simp add: pmf_nonneg integrable_pmf nn_integral_eq_integral)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   250
  also have "\<dots> = (\<integral>\<^sup>+x. emeasure M {x} \<partial>count_space (X \<inter> M))"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   251
    by (auto intro!: nn_integral_cong_AE split: split_indicator
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   252
             simp: pmf.rep_eq measure_pmf.emeasure_eq_measure nn_integral_count_space_indicator
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   253
                   AE_count_space set_pmf_iff)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   254
  also have "\<dots> = emeasure M (X \<inter> M)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   255
    by (rule emeasure_countable_singleton[symmetric]) (auto intro: countable_set_pmf)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   256
  also have "\<dots> = emeasure M X"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   257
    by (auto intro!: emeasure_eq_AE simp: AE_measure_pmf_iff)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   258
  finally show ?thesis
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   259
    by (simp add: measure_pmf.emeasure_eq_measure)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   260
qed
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   261
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   262
lemma integral_pmf_restrict:
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   263
  "(f::'a \<Rightarrow> 'b::{banach, second_countable_topology}) \<in> borel_measurable (count_space UNIV) \<Longrightarrow>
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   264
    (\<integral>x. f x \<partial>measure_pmf M) = (\<integral>x. f x \<partial>restrict_space M M)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   265
  by (auto intro!: integral_cong_AE simp add: integral_restrict_space AE_measure_pmf_iff)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   266
58587
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   267
lemma emeasure_pmf: "emeasure (M::'a pmf) M = 1"
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   268
proof -
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   269
  have "emeasure (M::'a pmf) M = emeasure (M::'a pmf) (space M)"
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   270
    by (intro emeasure_eq_AE) (simp_all add: AE_measure_pmf)
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   271
  then show ?thesis
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   272
    using measure_pmf.emeasure_space_1 by simp
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   273
qed
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   274
59023
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   275
lemma in_null_sets_measure_pmfI:
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   276
  "A \<inter> set_pmf p = {} \<Longrightarrow> A \<in> null_sets (measure_pmf p)"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   277
using emeasure_eq_0_AE[where ?P="\<lambda>x. x \<in> A" and M="measure_pmf p"]
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   278
by(auto simp add: null_sets_def AE_measure_pmf_iff)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   279
58587
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   280
lemma map_pmf_id[simp]: "map_pmf id = id"
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   281
  by (rule, transfer) (auto simp: emeasure_distr measurable_def intro!: measure_eqI)
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   282
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   283
lemma map_pmf_compose: "map_pmf (f \<circ> g) = map_pmf f \<circ> map_pmf g"
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   284
  by (rule, transfer) (simp add: distr_distr[symmetric, where N="count_space UNIV"] measurable_def) 
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   285
59000
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   286
lemma map_pmf_comp: "map_pmf f (map_pmf g M) = map_pmf (\<lambda>x. f (g x)) M"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   287
  using map_pmf_compose[of f g] by (simp add: comp_def)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   288
58587
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   289
lemma map_pmf_cong:
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   290
  assumes "p = q"
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   291
  shows "(\<And>x. x \<in> set_pmf q \<Longrightarrow> f x = g x) \<Longrightarrow> map_pmf f p = map_pmf g q"
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   292
  unfolding `p = q`[symmetric] measure_pmf_inject[symmetric] map_pmf.rep_eq
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   293
  by (auto simp add: emeasure_distr AE_measure_pmf_iff intro!: emeasure_eq_AE measure_eqI)
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   294
59002
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   295
lemma emeasure_map_pmf[simp]: "emeasure (map_pmf f M) X = emeasure M (f -` X)"
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   296
  unfolding map_pmf.rep_eq by (subst emeasure_distr) auto
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   297
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   298
lemma nn_integral_map_pmf[simp]: "(\<integral>\<^sup>+x. f x \<partial>map_pmf g M) = (\<integral>\<^sup>+x. f (g x) \<partial>M)"
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   299
  unfolding map_pmf.rep_eq by (intro nn_integral_distr) auto
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   300
59023
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   301
lemma ereal_pmf_map: "pmf (map_pmf f p) x = (\<integral>\<^sup>+ y. indicator (f -` {x}) y \<partial>measure_pmf p)"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   302
proof(transfer fixing: f x)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   303
  fix p :: "'b measure"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   304
  presume "prob_space p"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   305
  then interpret prob_space p .
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   306
  presume "sets p = UNIV"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   307
  then show "ereal (measure (distr p (count_space UNIV) f) {x}) = integral\<^sup>N p (indicator (f -` {x}))"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   308
    by(simp add: measure_distr measurable_def emeasure_eq_measure)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   309
qed simp_all
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   310
58587
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   311
lemma pmf_set_map: 
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   312
  fixes f :: "'a \<Rightarrow> 'b"
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   313
  shows "set_pmf \<circ> map_pmf f = op ` f \<circ> set_pmf"
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   314
proof (rule, transfer, clarsimp simp add: measure_distr measurable_def)
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   315
  fix f :: "'a \<Rightarrow> 'b" and M :: "'a measure"
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   316
  assume "prob_space M" and ae: "AE x in M. measure M {x} \<noteq> 0" and [simp]: "sets M = UNIV"
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   317
  interpret prob_space M by fact
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   318
  show "{x. measure M (f -` {x}) \<noteq> 0} = f ` {x. measure M {x} \<noteq> 0}"
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   319
  proof safe
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   320
    fix x assume "measure M (f -` {x}) \<noteq> 0"
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   321
    moreover have "measure M (f -` {x}) = measure M {y. f y = x \<and> measure M {y} \<noteq> 0}"
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   322
      using ae by (intro finite_measure_eq_AE) auto
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   323
    ultimately have "{y. f y = x \<and> measure M {y} \<noteq> 0} \<noteq> {}"
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   324
      by (metis measure_empty)
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   325
    then show "x \<in> f ` {x. measure M {x} \<noteq> 0}"
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   326
      by auto
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   327
  next
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   328
    fix x assume "measure M {x} \<noteq> 0"
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   329
    then have "0 < measure M {x}"
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   330
      using measure_nonneg[of M "{x}"] by auto
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   331
    also have "measure M {x} \<le> measure M (f -` {f x})"
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   332
      by (intro finite_measure_mono) auto
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   333
    finally show "measure M (f -` {f x}) = 0 \<Longrightarrow> False"
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   334
      by simp
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   335
  qed
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   336
qed
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   337
59000
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   338
lemma set_map_pmf: "set_pmf (map_pmf f M) = f`set_pmf M"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   339
  using pmf_set_map[of f] by (auto simp: comp_def fun_eq_iff)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   340
59023
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   341
lemma nn_integral_pmf: "(\<integral>\<^sup>+ x. pmf p x \<partial>count_space A) = emeasure (measure_pmf p) A"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   342
proof -
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   343
  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))"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   344
    by(auto simp add: nn_integral_count_space_indicator indicator_def set_pmf_iff intro: nn_integral_cong)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   345
  also have "\<dots> = emeasure (measure_pmf p) (\<Union>x\<in>A \<inter> set_pmf p. {x})"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   346
    by(subst emeasure_UN_countable)(auto simp add: emeasure_pmf_single disjoint_family_on_def)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   347
  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})"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   348
    by(rule emeasure_Un_null_set[symmetric])(auto intro: in_null_sets_measure_pmfI)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   349
  also have "\<dots> = emeasure (measure_pmf p) A"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   350
    by(auto intro: arg_cong2[where f=emeasure])
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   351
  finally show ?thesis .
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   352
qed
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   353
59000
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   354
subsection {* PMFs as function *}
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   355
58587
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   356
context
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   357
  fixes f :: "'a \<Rightarrow> real"
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   358
  assumes nonneg: "\<And>x. 0 \<le> f x"
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   359
  assumes prob: "(\<integral>\<^sup>+x. f x \<partial>count_space UNIV) = 1"
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   360
begin
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   361
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   362
lift_definition embed_pmf :: "'a pmf" is "density (count_space UNIV) (ereal \<circ> f)"
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   363
proof (intro conjI)
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   364
  have *[simp]: "\<And>x y. ereal (f y) * indicator {x} y = ereal (f x) * indicator {x} y"
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   365
    by (simp split: split_indicator)
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   366
  show "AE x in density (count_space UNIV) (ereal \<circ> f).
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   367
    measure (density (count_space UNIV) (ereal \<circ> f)) {x} \<noteq> 0"
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   368
    by (simp add: AE_density nonneg emeasure_density measure_def nn_integral_cmult_indicator)
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   369
  show "prob_space (density (count_space UNIV) (ereal \<circ> f))"
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   370
    by default (simp add: emeasure_density prob)
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   371
qed simp
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   372
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   373
lemma pmf_embed_pmf: "pmf embed_pmf x = f x"
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   374
proof transfer
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   375
  have *[simp]: "\<And>x y. ereal (f y) * indicator {x} y = ereal (f x) * indicator {x} y"
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   376
    by (simp split: split_indicator)
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   377
  fix x show "measure (density (count_space UNIV) (ereal \<circ> f)) {x} = f x"
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   378
    by transfer (simp add: measure_def emeasure_density nn_integral_cmult_indicator nonneg)
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   379
qed
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   380
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   381
end
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   382
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   383
lemma embed_pmf_transfer:
58730
b3fd0628f849 add transfer rule for set_pmf
hoelzl
parents: 58606
diff changeset
   384
  "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"
58587
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   385
  by (auto simp: rel_fun_def eq_onp_def embed_pmf.transfer)
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   386
59000
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   387
lemma measure_pmf_eq_density: "measure_pmf p = density (count_space UNIV) (pmf p)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   388
proof (transfer, elim conjE)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   389
  fix M :: "'a measure" assume [simp]: "sets M = UNIV" and ae: "AE x in M. measure M {x} \<noteq> 0"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   390
  assume "prob_space M" then interpret prob_space M .
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   391
  show "M = density (count_space UNIV) (\<lambda>x. ereal (measure M {x}))"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   392
  proof (rule measure_eqI)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   393
    fix A :: "'a set"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   394
    have "(\<integral>\<^sup>+ x. ereal (measure M {x}) * indicator A x \<partial>count_space UNIV) = 
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   395
      (\<integral>\<^sup>+ x. emeasure M {x} * indicator (A \<inter> {x. measure M {x} \<noteq> 0}) x \<partial>count_space UNIV)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   396
      by (auto intro!: nn_integral_cong simp: emeasure_eq_measure split: split_indicator)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   397
    also have "\<dots> = (\<integral>\<^sup>+ x. emeasure M {x} \<partial>count_space (A \<inter> {x. measure M {x} \<noteq> 0}))"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   398
      by (subst nn_integral_restrict_space[symmetric]) (auto simp: restrict_count_space)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   399
    also have "\<dots> = emeasure M (\<Union>x\<in>(A \<inter> {x. measure M {x} \<noteq> 0}). {x})"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   400
      by (intro emeasure_UN_countable[symmetric] countable_Int2 countable_support)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   401
         (auto simp: disjoint_family_on_def)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   402
    also have "\<dots> = emeasure M A"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   403
      using ae by (intro emeasure_eq_AE) auto
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   404
    finally show " emeasure M A = emeasure (density (count_space UNIV) (\<lambda>x. ereal (measure M {x}))) A"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   405
      using emeasure_space_1 by (simp add: emeasure_density)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   406
  qed simp
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   407
qed
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   408
58587
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   409
lemma td_pmf_embed_pmf:
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   410
  "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}"
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   411
  unfolding type_definition_def
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   412
proof safe
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   413
  fix p :: "'a pmf"
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   414
  have "(\<integral>\<^sup>+ x. 1 \<partial>measure_pmf p) = 1"
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   415
    using measure_pmf.emeasure_space_1[of p] by simp
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   416
  then show *: "(\<integral>\<^sup>+ x. ereal (pmf p x) \<partial>count_space UNIV) = 1"
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   417
    by (simp add: measure_pmf_eq_density nn_integral_density pmf_nonneg del: nn_integral_const)
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   418
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   419
  show "embed_pmf (pmf p) = p"
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   420
    by (intro measure_pmf_inject[THEN iffD1])
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   421
       (simp add: * embed_pmf.rep_eq pmf_nonneg measure_pmf_eq_density[of p] comp_def)
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   422
next
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   423
  fix f :: "'a \<Rightarrow> real" assume "\<forall>x. 0 \<le> f x" "(\<integral>\<^sup>+x. f x \<partial>count_space UNIV) = 1"
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   424
  then show "pmf (embed_pmf f) = f"
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   425
    by (auto intro!: pmf_embed_pmf)
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   426
qed (rule pmf_nonneg)
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   427
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   428
end
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   429
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   430
locale pmf_as_function
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   431
begin
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   432
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   433
setup_lifting td_pmf_embed_pmf
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   434
58730
b3fd0628f849 add transfer rule for set_pmf
hoelzl
parents: 58606
diff changeset
   435
lemma set_pmf_transfer[transfer_rule]: 
b3fd0628f849 add transfer rule for set_pmf
hoelzl
parents: 58606
diff changeset
   436
  assumes "bi_total A"
b3fd0628f849 add transfer rule for set_pmf
hoelzl
parents: 58606
diff changeset
   437
  shows "rel_fun (pcr_pmf A) (rel_set A) (\<lambda>f. {x. f x \<noteq> 0}) set_pmf"  
b3fd0628f849 add transfer rule for set_pmf
hoelzl
parents: 58606
diff changeset
   438
  using `bi_total A`
b3fd0628f849 add transfer rule for set_pmf
hoelzl
parents: 58606
diff changeset
   439
  by (auto simp: pcr_pmf_def cr_pmf_def rel_fun_def rel_set_def bi_total_def Bex_def set_pmf_iff)
b3fd0628f849 add transfer rule for set_pmf
hoelzl
parents: 58606
diff changeset
   440
     metis+
b3fd0628f849 add transfer rule for set_pmf
hoelzl
parents: 58606
diff changeset
   441
59000
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   442
end
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   443
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   444
context
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   445
begin
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   446
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   447
interpretation pmf_as_function .
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   448
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   449
lemma pmf_eqI: "(\<And>i. pmf M i = pmf N i) \<Longrightarrow> M = N"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   450
  by transfer auto
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   451
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   452
lemma pmf_eq_iff: "M = N \<longleftrightarrow> (\<forall>i. pmf M i = pmf N i)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   453
  by (auto intro: pmf_eqI)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   454
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   455
end
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   456
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   457
context
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   458
begin
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   459
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   460
interpretation pmf_as_function .
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   461
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   462
lift_definition bernoulli_pmf :: "real \<Rightarrow> bool pmf" is
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   463
  "\<lambda>p b. ((\<lambda>p. if b then p else 1 - p) \<circ> min 1 \<circ> max 0) p"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   464
  by (auto simp: nn_integral_count_space_finite[where A="{False, True}"] UNIV_bool
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   465
           split: split_max split_min)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   466
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   467
lemma pmf_bernoulli_True[simp]: "0 \<le> p \<Longrightarrow> p \<le> 1 \<Longrightarrow> pmf (bernoulli_pmf p) True = p"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   468
  by transfer simp
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   469
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   470
lemma pmf_bernoulli_False[simp]: "0 \<le> p \<Longrightarrow> p \<le> 1 \<Longrightarrow> pmf (bernoulli_pmf p) False = 1 - p"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   471
  by transfer simp
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   472
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   473
lemma set_pmf_bernoulli: "0 < p \<Longrightarrow> p < 1 \<Longrightarrow> set_pmf (bernoulli_pmf p) = UNIV"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   474
  by (auto simp add: set_pmf_iff UNIV_bool)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   475
59002
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   476
lemma nn_integral_bernoulli_pmf[simp]: 
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   477
  assumes [simp]: "0 \<le> p" "p \<le> 1" "\<And>x. 0 \<le> f x"
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   478
  shows "(\<integral>\<^sup>+x. f x \<partial>bernoulli_pmf p) = f True * p + f False * (1 - p)"
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   479
  by (subst nn_integral_measure_pmf_support[of UNIV])
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   480
     (auto simp: UNIV_bool field_simps)
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   481
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   482
lemma integral_bernoulli_pmf[simp]: 
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   483
  assumes [simp]: "0 \<le> p" "p \<le> 1"
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   484
  shows "(\<integral>x. f x \<partial>bernoulli_pmf p) = f True * p + f False * (1 - p)"
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   485
  by (subst integral_measure_pmf[of UNIV]) (auto simp: UNIV_bool)
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   486
59000
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   487
lift_definition geometric_pmf :: "nat pmf" is "\<lambda>n. 1 / 2^Suc n"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   488
proof
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   489
  note geometric_sums[of "1 / 2"]
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   490
  note sums_mult[OF this, of "1 / 2"]
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   491
  from sums_suminf_ereal[OF this]
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   492
  show "(\<integral>\<^sup>+ x. ereal (1 / 2 ^ Suc x) \<partial>count_space UNIV) = 1"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   493
    by (simp add: nn_integral_count_space_nat field_simps)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   494
qed simp
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   495
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   496
lemma pmf_geometric[simp]: "pmf geometric_pmf n = 1 / 2^Suc n"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   497
  by transfer rule
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   498
59002
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   499
lemma set_pmf_geometric[simp]: "set_pmf geometric_pmf = UNIV"
59000
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   500
  by (auto simp: set_pmf_iff)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   501
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   502
context
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   503
  fixes M :: "'a multiset" assumes M_not_empty: "M \<noteq> {#}"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   504
begin
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   505
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   506
lift_definition pmf_of_multiset :: "'a pmf" is "\<lambda>x. count M x / size M"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   507
proof
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   508
  show "(\<integral>\<^sup>+ x. ereal (real (count M x) / real (size M)) \<partial>count_space UNIV) = 1"  
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   509
    using M_not_empty
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   510
    by (simp add: zero_less_divide_iff nn_integral_count_space nonempty_has_size
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   511
                  setsum_divide_distrib[symmetric])
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   512
       (auto simp: size_multiset_overloaded_eq intro!: setsum.cong)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   513
qed simp
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   514
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   515
lemma pmf_of_multiset[simp]: "pmf pmf_of_multiset x = count M x / size M"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   516
  by transfer rule
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   517
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   518
lemma set_pmf_of_multiset[simp]: "set_pmf pmf_of_multiset = set_of M"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   519
  by (auto simp: set_pmf_iff)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   520
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   521
end
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   522
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   523
context
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   524
  fixes S :: "'a set" assumes S_not_empty: "S \<noteq> {}" and S_finite: "finite S"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   525
begin
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   526
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   527
lift_definition pmf_of_set :: "'a pmf" is "\<lambda>x. indicator S x / card S"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   528
proof
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   529
  show "(\<integral>\<^sup>+ x. ereal (indicator S x / real (card S)) \<partial>count_space UNIV) = 1"  
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   530
    using S_not_empty S_finite by (subst nn_integral_count_space'[of S]) auto
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   531
qed simp
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   532
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   533
lemma pmf_of_set[simp]: "pmf pmf_of_set x = indicator S x / card S"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   534
  by transfer rule
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   535
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   536
lemma set_pmf_of_set[simp]: "set_pmf pmf_of_set = S"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   537
  using S_finite S_not_empty by (auto simp: set_pmf_iff)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   538
59002
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   539
lemma emeasure_pmf_of_set[simp]: "emeasure pmf_of_set S = 1"
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   540
  by (rule measure_pmf.emeasure_eq_1_AE) (auto simp: AE_measure_pmf_iff)
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   541
59000
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   542
end
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   543
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   544
end
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   545
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   546
subsection {* Monad interpretation *}
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   547
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   548
lemma measurable_measure_pmf[measurable]:
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   549
  "(\<lambda>x. measure_pmf (M x)) \<in> measurable (count_space UNIV) (subprob_algebra (count_space UNIV))"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   550
  by (auto simp: space_subprob_algebra intro!: prob_space_imp_subprob_space) unfold_locales
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   551
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   552
lemma bind_pmf_cong:
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   553
  assumes "\<And>x. A x \<in> space (subprob_algebra N)" "\<And>x. B x \<in> space (subprob_algebra N)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   554
  assumes "\<And>i. i \<in> set_pmf x \<Longrightarrow> A i = B i"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   555
  shows "bind (measure_pmf x) A = bind (measure_pmf x) B"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   556
proof (rule measure_eqI)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   557
  show "sets (measure_pmf x \<guillemotright>= A) = sets (measure_pmf x \<guillemotright>= B)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   558
    using assms by (subst (1 2) sets_bind) auto
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   559
next
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   560
  fix X assume "X \<in> sets (measure_pmf x \<guillemotright>= A)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   561
  then have X: "X \<in> sets N"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   562
    using assms by (subst (asm) sets_bind) auto
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   563
  show "emeasure (measure_pmf x \<guillemotright>= A) X = emeasure (measure_pmf x \<guillemotright>= B) X"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   564
    using assms
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   565
    by (subst (1 2) emeasure_bind[where N=N, OF _ _ X])
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   566
       (auto intro!: nn_integral_cong_AE simp: AE_measure_pmf_iff)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   567
qed
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   568
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   569
context
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   570
begin
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   571
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   572
interpretation pmf_as_measure .
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   573
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   574
lift_definition join_pmf :: "'a pmf pmf \<Rightarrow> 'a pmf" is "\<lambda>M. measure_pmf M \<guillemotright>= measure_pmf"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   575
proof (intro conjI)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   576
  fix M :: "'a pmf pmf"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   577
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   578
  have *: "measure_pmf \<in> measurable (measure_pmf M) (subprob_algebra (count_space UNIV))"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   579
    using measurable_measure_pmf[of "\<lambda>x. x"] by simp
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   580
  
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   581
  interpret bind: prob_space "measure_pmf M \<guillemotright>= measure_pmf"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   582
    apply (rule measure_pmf.prob_space_bind[OF _ *])
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   583
    apply (auto intro!: AE_I2)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   584
    apply unfold_locales
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   585
    done
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   586
  show "prob_space (measure_pmf M \<guillemotright>= measure_pmf)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   587
    by intro_locales
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   588
  show "sets (measure_pmf M \<guillemotright>= measure_pmf) = UNIV"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   589
    by (subst sets_bind[OF *]) auto
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   590
  have "AE x in measure_pmf M \<guillemotright>= measure_pmf. emeasure (measure_pmf M \<guillemotright>= measure_pmf) {x} \<noteq> 0"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   591
    by (auto simp add: AE_bind[OF _ *] AE_measure_pmf_iff emeasure_bind[OF _ *]
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   592
        nn_integral_0_iff_AE measure_pmf.emeasure_eq_measure measure_le_0_iff set_pmf_iff pmf.rep_eq)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   593
  then show "AE x in measure_pmf M \<guillemotright>= measure_pmf. measure (measure_pmf M \<guillemotright>= measure_pmf) {x} \<noteq> 0"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   594
    unfolding bind.emeasure_eq_measure by simp
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   595
qed
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   596
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   597
lemma pmf_join: "pmf (join_pmf N) i = (\<integral>M. pmf M i \<partial>measure_pmf N)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   598
proof (transfer fixing: N i)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   599
  have N: "subprob_space (measure_pmf N)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   600
    by (rule prob_space_imp_subprob_space) intro_locales
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   601
  show "measure (measure_pmf N \<guillemotright>= measure_pmf) {i} = integral\<^sup>L (measure_pmf N) (\<lambda>M. measure M {i})"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   602
    using measurable_measure_pmf[of "\<lambda>x. x"]
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   603
    by (intro subprob_space.measure_bind[where N="count_space UNIV", OF N]) auto
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   604
qed (auto simp: Transfer.Rel_def rel_fun_def cr_pmf_def)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   605
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   606
lift_definition return_pmf :: "'a \<Rightarrow> 'a pmf" is "return (count_space UNIV)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   607
  by (auto intro!: prob_space_return simp: AE_return measure_return)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   608
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   609
lemma join_return_pmf: "join_pmf (return_pmf M) = M"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   610
  by (simp add: integral_return pmf_eq_iff pmf_join return_pmf.rep_eq)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   611
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   612
lemma map_return_pmf: "map_pmf f (return_pmf x) = return_pmf (f x)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   613
  by transfer (simp add: distr_return)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   614
59002
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   615
lemma set_return_pmf: "set_pmf (return_pmf x) = {x}"
59000
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   616
  by transfer (auto simp add: measure_return split: split_indicator)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   617
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   618
lemma pmf_return: "pmf (return_pmf x) y = indicator {y} x"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   619
  by transfer (simp add: measure_return)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   620
59002
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   621
lemma nn_integral_return_pmf[simp]: "0 \<le> f x \<Longrightarrow> (\<integral>\<^sup>+x. f x \<partial>return_pmf x) = f x"
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   622
  unfolding return_pmf.rep_eq by (intro nn_integral_return) auto
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   623
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   624
lemma emeasure_return_pmf[simp]: "emeasure (return_pmf x) X = indicator X x"
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   625
  unfolding return_pmf.rep_eq by (intro emeasure_return) auto
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   626
59000
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   627
end
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   628
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   629
definition "bind_pmf M f = join_pmf (map_pmf f M)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   630
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   631
lemma (in pmf_as_measure) bind_transfer[transfer_rule]:
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   632
  "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"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   633
proof (auto simp: pmf_as_measure.cr_pmf_def rel_fun_def bind_pmf_def join_pmf.rep_eq map_pmf.rep_eq)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   634
  fix M f and g :: "'a \<Rightarrow> 'b pmf" assume "\<forall>x. f x = measure_pmf (g x)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   635
  then have f: "f = (\<lambda>x. measure_pmf (g x))"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   636
    by auto
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   637
  show "measure_pmf M \<guillemotright>= f = distr (measure_pmf M) (count_space UNIV) g \<guillemotright>= measure_pmf"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   638
    unfolding f by (subst bind_distr[OF _ measurable_measure_pmf]) auto
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   639
qed
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   640
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   641
lemma pmf_bind: "pmf (bind_pmf N f) i = (\<integral>x. pmf (f x) i \<partial>measure_pmf N)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   642
  by (auto intro!: integral_distr simp: bind_pmf_def pmf_join map_pmf.rep_eq)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   643
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   644
lemma bind_return_pmf: "bind_pmf (return_pmf x) f = f x"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   645
  unfolding bind_pmf_def map_return_pmf join_return_pmf ..
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   646
59002
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   647
lemma set_bind_pmf: "set_pmf (bind_pmf M N) = (\<Union>M\<in>set_pmf M. set_pmf (N M))"
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   648
  apply (simp add: set_eq_iff set_pmf_iff pmf_bind)
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   649
  apply (subst integral_nonneg_eq_0_iff_AE)
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   650
  apply (auto simp: pmf_nonneg pmf_le_1 AE_measure_pmf_iff
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   651
              intro!: measure_pmf.integrable_const_bound[where B=1])
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   652
  done
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   653
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   654
lemma measurable_pair_restrict_pmf2:
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   655
  assumes "countable A"
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   656
  assumes "\<And>y. y \<in> A \<Longrightarrow> (\<lambda>x. f (x, y)) \<in> measurable M L"
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   657
  shows "f \<in> measurable (M \<Otimes>\<^sub>M restrict_space (measure_pmf N) A) L"
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   658
  apply (subst measurable_cong_sets)
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   659
  apply (rule sets_pair_measure_cong sets_restrict_space_cong sets_measure_pmf_count_space refl)+
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   660
  apply (simp_all add: restrict_count_space)
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   661
  apply (subst split_eta[symmetric])
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   662
  unfolding measurable_split_conv
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   663
  apply (rule measurable_compose_countable'[OF _ measurable_snd `countable A`])
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   664
  apply (rule measurable_compose[OF measurable_fst])
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   665
  apply fact
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   666
  done
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   667
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   668
lemma measurable_pair_restrict_pmf1:
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   669
  assumes "countable A"
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   670
  assumes "\<And>x. x \<in> A \<Longrightarrow> (\<lambda>y. f (x, y)) \<in> measurable N L"
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   671
  shows "f \<in> measurable (restrict_space (measure_pmf M) A \<Otimes>\<^sub>M N) L"
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   672
  apply (subst measurable_cong_sets)
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   673
  apply (rule sets_pair_measure_cong sets_restrict_space_cong sets_measure_pmf_count_space refl)+
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   674
  apply (simp_all add: restrict_count_space)
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   675
  apply (subst split_eta[symmetric])
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   676
  unfolding measurable_split_conv
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   677
  apply (rule measurable_compose_countable'[OF _ measurable_fst `countable A`])
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   678
  apply (rule measurable_compose[OF measurable_snd])
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   679
  apply fact
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   680
  done
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   681
                                
59000
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   682
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))"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   683
  unfolding pmf_eq_iff pmf_bind
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   684
proof
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   685
  fix i
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   686
  interpret B: prob_space "restrict_space B B"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   687
    by (intro prob_space_restrict_space measure_pmf.emeasure_eq_1_AE)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   688
       (auto simp: AE_measure_pmf_iff)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   689
  interpret A: prob_space "restrict_space A A"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   690
    by (intro prob_space_restrict_space measure_pmf.emeasure_eq_1_AE)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   691
       (auto simp: AE_measure_pmf_iff)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   692
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   693
  interpret AB: pair_prob_space "restrict_space A A" "restrict_space B B"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   694
    by unfold_locales
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   695
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   696
  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)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   697
    by (rule integral_cong) (auto intro!: integral_pmf_restrict)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   698
  also have "\<dots> = (\<integral> x. (\<integral> y. pmf (C x y) i \<partial>restrict_space B B) \<partial>restrict_space A A)"
59002
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   699
    by (intro integral_pmf_restrict B.borel_measurable_lebesgue_integral measurable_pair_restrict_pmf2
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   700
              countable_set_pmf borel_measurable_count_space)
59000
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   701
  also have "\<dots> = (\<integral> y. \<integral> x. pmf (C x y) i \<partial>restrict_space A A \<partial>restrict_space B B)"
59002
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   702
    by (rule AB.Fubini_integral[symmetric])
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   703
       (auto intro!: AB.integrable_const_bound[where B=1] measurable_pair_restrict_pmf2
59023
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   704
             simp: pmf_nonneg pmf_le_1 measurable_restrict_space1)
59000
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   705
  also have "\<dots> = (\<integral> y. \<integral> x. pmf (C x y) i \<partial>restrict_space A A \<partial>B)"
59002
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   706
    by (intro integral_pmf_restrict[symmetric] A.borel_measurable_lebesgue_integral measurable_pair_restrict_pmf2
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   707
              countable_set_pmf borel_measurable_count_space)
59000
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   708
  also have "\<dots> = (\<integral> y. \<integral> x. pmf (C x y) i \<partial>A \<partial>B)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   709
    by (rule integral_cong) (auto intro!: integral_pmf_restrict[symmetric])
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   710
  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)" .
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   711
qed
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   712
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   713
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   714
context
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   715
begin
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   716
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   717
interpretation pmf_as_measure .
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   718
59002
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   719
lemma measure_pmf_bind: "measure_pmf (bind_pmf M f) = (measure_pmf M \<guillemotright>= (\<lambda>x. measure_pmf (f x)))"
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   720
  by transfer simp
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   721
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   722
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)"
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   723
  using measurable_measure_pmf[of N]
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   724
  unfolding measure_pmf_bind
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   725
  apply (subst (1 3) nn_integral_max_0[symmetric])
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   726
  apply (intro nn_integral_bind[where B="count_space UNIV"])
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   727
  apply auto
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   728
  done
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   729
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   730
lemma emeasure_bind_pmf[simp]: "emeasure (bind_pmf M N) X = (\<integral>\<^sup>+x. emeasure (N x) X \<partial>M)"
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   731
  using measurable_measure_pmf[of N]
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   732
  unfolding measure_pmf_bind
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   733
  by (subst emeasure_bind[where N="count_space UNIV"]) auto
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   734
59000
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   735
lemma bind_return_pmf': "bind_pmf N return_pmf = N"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   736
proof (transfer, clarify)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   737
  fix N :: "'a measure" assume "sets N = UNIV" then show "N \<guillemotright>= return (count_space UNIV) = N"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   738
    by (subst return_sets_cong[where N=N]) (simp_all add: bind_return')
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   739
qed
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   740
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   741
lemma bind_return_pmf'': "bind_pmf N (\<lambda>x. return_pmf (f x)) = map_pmf f N"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   742
proof (transfer, clarify)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   743
  fix N :: "'b measure" and f :: "'b \<Rightarrow> 'a" assume "prob_space N" "sets N = UNIV"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   744
  then show "N \<guillemotright>= (\<lambda>x. return (count_space UNIV) (f x)) = distr N (count_space UNIV) f"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   745
    by (subst bind_return_distr[symmetric])
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   746
       (auto simp: prob_space.not_empty measurable_def comp_def)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   747
qed
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   748
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   749
lemma bind_assoc_pmf: "bind_pmf (bind_pmf A B) C = bind_pmf A (\<lambda>x. bind_pmf (B x) C)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   750
  by transfer
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   751
     (auto intro!: bind_assoc[where N="count_space UNIV" and R="count_space UNIV"]
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   752
           simp: measurable_def space_subprob_algebra prob_space_imp_subprob_space)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   753
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   754
end
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   755
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   756
definition "pair_pmf A B = bind_pmf A (\<lambda>x. bind_pmf B (\<lambda>y. return_pmf (x, y)))"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   757
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   758
lemma pmf_pair: "pmf (pair_pmf M N) (a, b) = pmf M a * pmf N b"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   759
  unfolding pair_pmf_def pmf_bind pmf_return
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   760
  apply (subst integral_measure_pmf[where A="{b}"])
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   761
  apply (auto simp: indicator_eq_0_iff)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   762
  apply (subst integral_measure_pmf[where A="{a}"])
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   763
  apply (auto simp: indicator_eq_0_iff setsum_nonneg_eq_0_iff pmf_nonneg)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   764
  done
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   765
59002
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   766
lemma set_pair_pmf: "set_pmf (pair_pmf A B) = set_pmf A \<times> set_pmf B"
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   767
  unfolding pair_pmf_def set_bind_pmf set_return_pmf by auto
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   768
59000
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   769
lemma bind_pair_pmf:
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   770
  assumes M[measurable]: "M \<in> measurable (count_space UNIV \<Otimes>\<^sub>M count_space UNIV) (subprob_algebra N)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   771
  shows "measure_pmf (pair_pmf A B) \<guillemotright>= M = (measure_pmf A \<guillemotright>= (\<lambda>x. measure_pmf B \<guillemotright>= (\<lambda>y. M (x, y))))"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   772
    (is "?L = ?R")
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   773
proof (rule measure_eqI)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   774
  have M'[measurable]: "M \<in> measurable (pair_pmf A B) (subprob_algebra N)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   775
    using M[THEN measurable_space] by (simp_all add: space_pair_measure)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   776
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   777
  have sets_eq_N: "sets ?L = N"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   778
    by (simp add: sets_bind[OF M'])
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   779
  show "sets ?L = sets ?R"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   780
    unfolding sets_eq_N
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   781
    apply (subst sets_bind[where N=N])
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   782
    apply (rule measurable_bind)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   783
    apply (rule measurable_compose[OF _ measurable_measure_pmf])
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   784
    apply measurable
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   785
    apply (auto intro!: sets_pair_measure_cong sets_measure_pmf_count_space)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   786
    done
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   787
  fix X assume "X \<in> sets ?L"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   788
  then have X[measurable]: "X \<in> sets N"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   789
    unfolding sets_eq_N .
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   790
  then show "emeasure ?L X = emeasure ?R X"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   791
    apply (simp add: emeasure_bind[OF _ M' X])
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   792
    unfolding pair_pmf_def measure_pmf_bind[of A]
59002
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   793
    apply (subst nn_integral_bind)
59000
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   794
    apply (rule measurable_compose[OF M' measurable_emeasure_subprob_algebra, OF X])
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   795
    apply (subst measurable_cong_sets[OF sets_measure_pmf_count_space refl])
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   796
    apply (subst subprob_algebra_cong[OF sets_measure_pmf_count_space])
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   797
    apply measurable
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   798
    unfolding measure_pmf_bind
59002
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   799
    apply (subst nn_integral_bind)
59000
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   800
    apply (rule measurable_compose[OF M' measurable_emeasure_subprob_algebra, OF X])
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   801
    apply (subst measurable_cong_sets[OF sets_measure_pmf_count_space refl])
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   802
    apply (subst subprob_algebra_cong[OF sets_measure_pmf_count_space])
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   803
    apply measurable
59002
2c8b2fb54b88 cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
hoelzl
parents: 59000
diff changeset
   804
    apply (simp add: nn_integral_measure_pmf_finite set_return_pmf emeasure_nonneg pmf_return one_ereal_def[symmetric])
59000
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   805
    apply (subst emeasure_bind[OF _ _ X])
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   806
    apply simp
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   807
    apply (rule measurable_bind[where N="count_space UNIV"])
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   808
    apply (rule measurable_compose[OF _ measurable_measure_pmf])
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   809
    apply measurable
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   810
    apply (rule sets_pair_measure_cong sets_measure_pmf_count_space refl)+
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   811
    apply (subst measurable_cong_sets[OF sets_pair_measure_cong[OF sets_measure_pmf_count_space refl] refl])
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   812
    apply simp
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   813
    apply (subst emeasure_bind[OF _ _ X])
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   814
    apply simp
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   815
    apply (rule measurable_compose[OF _ M])
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   816
    apply (auto simp: space_pair_measure)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   817
    done
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   818
qed
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58730
diff changeset
   819
59023
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   820
inductive rel_pmf :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> 'a pmf \<Rightarrow> 'b pmf \<Rightarrow> bool"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   821
for R p q
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   822
where
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   823
  "\<lbrakk> \<And>x y. (x, y) \<in> set_pmf pq \<Longrightarrow> R x y; 
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   824
     map_pmf fst pq = p; map_pmf snd pq = q \<rbrakk>
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   825
  \<Longrightarrow> rel_pmf R p q"
58587
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   826
59023
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   827
bnf pmf: "'a pmf" map: map_pmf sets: set_pmf bd : "natLeq" rel: rel_pmf
58587
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   828
proof -
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   829
  show "map_pmf id = id" by (rule map_pmf_id)
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   830
  show "\<And>f g. map_pmf (f \<circ> g) = map_pmf f \<circ> map_pmf g" by (rule map_pmf_compose) 
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   831
  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"
59023
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   832
    by (intro map_pmf_cong refl)
58587
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   833
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   834
  show "\<And>f::'a \<Rightarrow> 'b. set_pmf \<circ> map_pmf f = op ` f \<circ> set_pmf"
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   835
    by (rule pmf_set_map)
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   836
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   837
  { fix p :: "'s pmf"
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   838
    have "(card_of (set_pmf p), card_of (UNIV :: nat set)) \<in> ordLeq"
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   839
      by (rule card_of_ordLeqI[where f="to_nat_on (set_pmf p)"])
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   840
         (auto intro: countable_set_pmf inj_on_to_nat_on)
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   841
    also have "(card_of (UNIV :: nat set), natLeq) \<in> ordLeq"
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   842
      by (metis Field_natLeq card_of_least natLeq_Well_order)
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   843
    finally show "(card_of (set_pmf p), natLeq) \<in> ordLeq" . }
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   844
59023
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   845
  show "\<And>R. rel_pmf R =
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   846
         (BNF_Def.Grp {x. set_pmf x \<subseteq> {(x, y). R x y}} (map_pmf fst))\<inverse>\<inverse> OO
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   847
         BNF_Def.Grp {x. set_pmf x \<subseteq> {(x, y). R x y}} (map_pmf snd)"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   848
     by (auto simp add: fun_eq_iff BNF_Def.Grp_def OO_def rel_pmf.simps)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   849
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   850
  { fix p :: "'a pmf" and f :: "'a \<Rightarrow> 'b" and g x
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   851
    assume p: "\<And>z. z \<in> set_pmf p \<Longrightarrow> f z = g z"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   852
      and x: "x \<in> set_pmf p"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   853
    thus "f x = g x" by simp }
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   854
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   855
  fix R :: "'a => 'b \<Rightarrow> bool" and S :: "'b \<Rightarrow> 'c \<Rightarrow> bool"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   856
  { fix p q r
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   857
    assume pq: "rel_pmf R p q"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   858
      and qr:"rel_pmf S q r"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   859
    from pq obtain pq where pq: "\<And>x y. (x, y) \<in> set_pmf pq \<Longrightarrow> R x y"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   860
      and p: "p = map_pmf fst pq" and q: "q = map_pmf snd pq" by cases auto
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   861
    from qr obtain qr where qr: "\<And>y z. (y, z) \<in> set_pmf qr \<Longrightarrow> S y z"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   862
      and q': "q = map_pmf fst qr" and r: "r = map_pmf snd qr" by cases auto
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   863
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   864
    have support_subset: "set_pmf pq O set_pmf qr \<subseteq> set_pmf p \<times> set_pmf r"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   865
      by(auto simp add: p r set_map_pmf intro: rev_image_eqI)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   866
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   867
    let ?A = "\<lambda>y. {x. (x, y) \<in> set_pmf pq}"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   868
      and ?B = "\<lambda>y. {z. (y, z) \<in> set_pmf qr}"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   869
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   870
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   871
    def ppp \<equiv> "\<lambda>A. \<lambda>f :: 'a \<Rightarrow> real. \<lambda>n. if n \<in> to_nat_on A ` A then f (from_nat_into A n) else 0"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   872
    have [simp]: "\<And>A f n. (\<And>x. x \<in> A \<Longrightarrow> 0 \<le> f x) \<Longrightarrow> 0 \<le> ppp A f n"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   873
                 "\<And>A f n x. \<lbrakk> x \<in> A; countable A \<rbrakk> \<Longrightarrow> ppp A f (to_nat_on A x) = f x"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   874
                 "\<And>A f n. n \<notin> to_nat_on A ` A \<Longrightarrow> ppp A f n = 0"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   875
      by(auto simp add: ppp_def intro: from_nat_into)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   876
    def rrr \<equiv> "\<lambda>A. \<lambda>f :: 'c \<Rightarrow> real. \<lambda>n. if n \<in> to_nat_on A ` A then f (from_nat_into A n) else 0"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   877
    have [simp]: "\<And>A f n. (\<And>x. x \<in> A \<Longrightarrow> 0 \<le> f x) \<Longrightarrow> 0 \<le> rrr A f n"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   878
                 "\<And>A f n x. \<lbrakk> x \<in> A; countable A \<rbrakk> \<Longrightarrow> rrr A f (to_nat_on A x) = f x"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   879
                 "\<And>A f n. n \<notin> to_nat_on A ` A \<Longrightarrow> rrr A f n = 0"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   880
      by(auto simp add: rrr_def intro: from_nat_into)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   881
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   882
    def pp \<equiv> "\<lambda>y. ppp (?A y) (\<lambda>x. pmf pq (x, y))"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   883
     and rr \<equiv> "\<lambda>y. rrr (?B y) (\<lambda>z. pmf qr (y, z))"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   884
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   885
    have pos_p [simp]: "\<And>y n. 0 \<le> pp y n"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   886
      and pos_r [simp]: "\<And>y n. 0 \<le> rr y n"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   887
      by(simp_all add: pmf_nonneg pp_def rr_def)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   888
    { fix y n
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   889
      have "pp y n \<le> 0 \<longleftrightarrow> pp y n = 0" "\<not> 0 < pp y n \<longleftrightarrow> pp y n = 0"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   890
        and "min (pp y n) 0 = 0" "min 0 (pp y n) = 0"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   891
        using pos_p[of y n] by(auto simp del: pos_p) }
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   892
    note pp_convs [simp] = this
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   893
    { fix y n
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   894
      have "rr y n \<le> 0 \<longleftrightarrow> rr y n = 0" "\<not> 0 < rr y n \<longleftrightarrow> rr y n = 0"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   895
        and "min (rr y n) 0 = 0" "min 0 (rr y n) = 0"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   896
        using pos_r[of y n] by(auto simp del: pos_r) }
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   897
    note r_convs [simp] = this
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   898
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   899
    have "\<And>y. ?A y \<subseteq> set_pmf p" by(auto simp add: p set_map_pmf intro: rev_image_eqI)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   900
    then have [simp]: "\<And>y. countable (?A y)" by(rule countable_subset) simp
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   901
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   902
    have "\<And>y. ?B y \<subseteq> set_pmf r" by(auto simp add: r set_map_pmf intro: rev_image_eqI)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   903
    then have [simp]: "\<And>y. countable (?B y)" by(rule countable_subset) simp
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   904
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   905
    let ?P = "\<lambda>y. to_nat_on (?A y)"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   906
      and ?R = "\<lambda>y. to_nat_on (?B y)"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   907
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   908
    have eq: "\<And>y. (\<integral>\<^sup>+ x. pp y x \<partial>count_space UNIV) = \<integral>\<^sup>+ z. rr y z \<partial>count_space UNIV"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   909
    proof -
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   910
      fix y
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   911
      have "(\<integral>\<^sup>+ x. pp y x \<partial>count_space UNIV) = (\<integral>\<^sup>+ x. pp y x \<partial>count_space (?P y ` ?A y))"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   912
        by(auto simp add: pp_def nn_integral_count_space_indicator indicator_def intro!: nn_integral_cong)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   913
      also have "\<dots> = (\<integral>\<^sup>+ x. pp y (?P y x) \<partial>count_space (?A y))"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   914
        by(intro nn_integral_bij_count_space[symmetric] inj_on_imp_bij_betw inj_on_to_nat_on) simp
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   915
      also have "\<dots> = (\<integral>\<^sup>+ x. pmf pq (x, y) \<partial>count_space (?A y))"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   916
        by(rule nn_integral_cong)(simp add: pp_def)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   917
      also have "\<dots> = \<integral>\<^sup>+ x. emeasure (measure_pmf pq) {(x, y)} \<partial>count_space (?A y)"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   918
        by(simp add: emeasure_pmf_single)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   919
      also have "\<dots> = emeasure (measure_pmf pq) (\<Union>x\<in>?A y. {(x, y)})"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   920
        by(subst emeasure_UN_countable)(simp_all add: disjoint_family_on_def)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   921
      also have "\<dots> = emeasure (measure_pmf pq) ((\<Union>x\<in>?A y. {(x, y)}) \<union> {(x, y'). x \<notin> ?A y \<and> y' = y})"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   922
        by(rule emeasure_Un_null_set[symmetric])+
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   923
          (auto simp add: q set_map_pmf split_beta intro!: in_null_sets_measure_pmfI intro: rev_image_eqI)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   924
      also have "\<dots> = emeasure (measure_pmf pq) (snd -` {y})"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   925
        by(rule arg_cong2[where f=emeasure])+auto
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   926
      also have "\<dots> = pmf q y" by(simp add: q ereal_pmf_map)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   927
      also have "\<dots> = emeasure (measure_pmf qr) (fst -` {y})"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   928
        by(simp add: q' ereal_pmf_map)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   929
      also have "\<dots> = emeasure (measure_pmf qr) ((\<Union>z\<in>?B y. {(y, z)}) \<union> {(y', z). z \<notin> ?B y \<and> y' = y})"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   930
        by(rule arg_cong2[where f=emeasure])+auto
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   931
      also have "\<dots> = emeasure (measure_pmf qr) (\<Union>z\<in>?B y. {(y, z)})"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   932
        by(rule emeasure_Un_null_set)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   933
          (auto simp add: q' set_map_pmf split_beta intro!: in_null_sets_measure_pmfI intro: rev_image_eqI)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   934
      also have "\<dots> = \<integral>\<^sup>+ z. emeasure (measure_pmf qr) {(y, z)} \<partial>count_space (?B y)"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   935
        by(subst emeasure_UN_countable)(simp_all add: disjoint_family_on_def)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   936
      also have "\<dots> = (\<integral>\<^sup>+ z. pmf qr (y, z) \<partial>count_space (?B y))"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   937
        by(simp add: emeasure_pmf_single)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   938
      also have "\<dots> = (\<integral>\<^sup>+ z. rr y (?R y z) \<partial>count_space (?B y))"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   939
        by(rule nn_integral_cong)(simp add: rr_def)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   940
      also have "\<dots> = (\<integral>\<^sup>+ z. rr y z \<partial>count_space (?R y ` ?B y))"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   941
        by(intro nn_integral_bij_count_space inj_on_imp_bij_betw inj_on_to_nat_on) simp
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   942
      also have "\<dots> = \<integral>\<^sup>+ z. rr y z \<partial>count_space UNIV"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   943
        by(auto simp add: rr_def nn_integral_count_space_indicator indicator_def intro!: nn_integral_cong)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   944
      finally show "?thesis y" .
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   945
    qed
58587
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
   946
59023
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   947
    def assign_aux \<equiv> "\<lambda>y remainder start weight z.
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   948
       if z < start then 0
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   949
       else if z = start then min weight remainder
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   950
       else if remainder + setsum (rr y) {Suc start ..<z} < weight then min (weight - remainder - setsum (rr y) {Suc start..<z}) (rr y z) else 0"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   951
    hence assign_aux_alt_def: "\<And>y remainder start weight z. assign_aux y remainder start weight z = 
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   952
       (if z < start then 0
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   953
        else if z = start then min weight remainder
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   954
        else if remainder + setsum (rr y) {Suc start ..<z} < weight then min (weight - remainder - setsum (rr y) {Suc start..<z}) (rr y z) else 0)"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   955
       by simp
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   956
    { fix y and remainder :: real and start and weight :: real
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   957
      assume weight_nonneg: "0 \<le> weight"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   958
      let ?assign_aux = "assign_aux y remainder start weight"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   959
      { fix z
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   960
        have "setsum ?assign_aux {..<z} =
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   961
           (if z \<le> start then 0 else if remainder + setsum (rr y) {Suc start..<z} < weight then remainder + setsum (rr y) {Suc start..<z} else weight)"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   962
        proof(induction z)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   963
          case (Suc z) show ?case
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   964
            by(auto simp add: Suc.IH assign_aux_alt_def[where z=z] not_less)(metis add.commute add.left_commute add_increasing pos_r)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   965
        qed(auto simp add: assign_aux_def) }
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   966
      note setsum_start_assign_aux = this
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   967
      moreover {
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   968
        assume remainder_nonneg: "0 \<le> remainder"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   969
        have [simp]: "\<And>z. 0 \<le> ?assign_aux z"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   970
          by(simp add: assign_aux_def weight_nonneg remainder_nonneg)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   971
        moreover have "\<And>z. \<lbrakk> rr y z = 0; remainder \<le> rr y start \<rbrakk> \<Longrightarrow> ?assign_aux z = 0"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   972
          using remainder_nonneg weight_nonneg
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   973
          by(auto simp add: assign_aux_def min_def)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   974
        moreover have "(\<integral>\<^sup>+ z. ?assign_aux z \<partial>count_space UNIV) = 
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   975
          min weight (\<integral>\<^sup>+ z. (if z < start then 0 else if z = start then remainder else rr y z) \<partial>count_space UNIV)"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   976
          (is "?lhs = ?rhs" is "_ = min _ (\<integral>\<^sup>+ y. ?f y \<partial>_)")
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   977
        proof -
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   978
          have "?lhs = (SUP n. \<Sum>z<n. ereal (?assign_aux z))"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   979
            by(simp add: nn_integral_count_space_nat suminf_ereal_eq_SUP)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   980
          also have "\<dots> = (SUP n. min weight (\<Sum>z<n. ?f z))"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   981
          proof(rule arg_cong2[where f=SUPREMUM] ext refl)+
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   982
            fix n
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   983
            have "(\<Sum>z<n. ereal (?assign_aux z)) = min weight ((if n > start then remainder else 0) + setsum ?f {Suc start..<n})"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   984
              using weight_nonneg remainder_nonneg by(simp add: setsum_start_assign_aux min_def)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   985
            also have "\<dots> = min weight (setsum ?f {start..<n})"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   986
              by(simp add: setsum_head_upt_Suc)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   987
            also have "\<dots> = min weight (setsum ?f {..<n})"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   988
              by(intro arg_cong2[where f=min] setsum.mono_neutral_left) auto
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   989
            finally show "(\<Sum>z<n. ereal (?assign_aux z)) = \<dots>" .
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   990
          qed
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   991
          also have "\<dots> = min weight (SUP n. setsum ?f {..<n})"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   992
            unfolding inf_min[symmetric] by(subst inf_SUP) simp
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   993
          also have "\<dots> = ?rhs"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   994
            by(simp add: nn_integral_count_space_nat suminf_ereal_eq_SUP remainder_nonneg)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   995
          finally show ?thesis .
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   996
        qed
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   997
        moreover note calculation }
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   998
      moreover note calculation }
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
   999
    note setsum_start_assign_aux = this(1)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1000
      and assign_aux_nonneg [simp] = this(2)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1001
      and assign_aux_eq_0_outside = this(3)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1002
      and nn_integral_assign_aux = this(4)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1003
    { fix y and remainder :: real and start target
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1004
      have "setsum (rr y) {Suc start..<target} \<ge> 0" by(simp add: setsum_nonneg)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1005
      moreover assume "0 \<le> remainder"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1006
      ultimately have "assign_aux y remainder start 0 target = 0"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1007
        by(auto simp add: assign_aux_def min_def) }
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1008
    note assign_aux_weight_0 [simp] = this
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1009
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1010
    def find_start \<equiv> "\<lambda>y weight. if \<exists>n. weight \<le> setsum (rr y)  {..n} then Some (LEAST n. weight \<le> setsum (rr y) {..n}) else None"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1011
    have find_start_eq_Some_above:
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1012
      "\<And>y weight n. find_start y weight = Some n \<Longrightarrow> weight \<le> setsum (rr y) {..n}"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1013
      by(drule sym)(auto simp add: find_start_def split: split_if_asm intro: LeastI)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1014
    { fix y weight n
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1015
      assume find_start: "find_start y weight = Some n"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1016
      and weight: "0 \<le> weight"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1017
      have "setsum (rr y) {..n} \<le> rr y n + weight"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1018
      proof(rule ccontr)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1019
        assume "\<not> ?thesis"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1020
        hence "rr y n + weight < setsum (rr y) {..n}" by simp
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1021
        moreover with weight obtain n' where "n = Suc n'" by(cases n) auto
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1022
        ultimately have "weight \<le> setsum (rr y) {..n'}" by simp
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1023
        hence "(LEAST n. weight \<le> setsum (rr y) {..n}) \<le> n'" by(rule Least_le)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1024
        moreover from find_start have "n = (LEAST n. weight \<le> setsum (rr y) {..n})"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1025
          by(auto simp add: find_start_def split: split_if_asm)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1026
        ultimately show False using \<open>n = Suc n'\<close> by auto
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1027
      qed }
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1028
    note find_start_eq_Some_least = this
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1029
    have find_start_0 [simp]: "\<And>y. find_start y 0 = Some 0"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1030
      by(auto simp add: find_start_def intro!: exI[where x=0])
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1031
    { fix y and weight :: real
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1032
      assume "weight < \<integral>\<^sup>+ z. rr y z \<partial>count_space UNIV"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1033
      also have "(\<integral>\<^sup>+ z. rr y z \<partial>count_space UNIV) = (SUP n. \<Sum>z<n. ereal (rr y z))"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1034
        by(simp add: nn_integral_count_space_nat suminf_ereal_eq_SUP)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1035
      finally obtain n where "weight < (\<Sum>z<n. rr y z)" by(auto simp add: less_SUP_iff)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1036
      hence "weight \<in> dom (find_start y)"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1037
        by(auto simp add: find_start_def)(meson atMost_iff finite_atMost lessThan_iff less_imp_le order_trans pos_r setsum_mono3 subsetI) }
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1038
    note in_dom_find_startI = this
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1039
    { fix y and w w' :: real and m
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1040
      let ?m' = "LEAST m. w' \<le> setsum (rr y) {..m}"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1041
      assume "w' \<le> w"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1042
      also  assume "find_start y w = Some m"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1043
      hence "w \<le> setsum (rr y) {..m}" by(rule find_start_eq_Some_above)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1044
      finally have "find_start y w' = Some ?m'" by(auto simp add: find_start_def)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1045
      moreover from \<open>w' \<le> setsum (rr y) {..m}\<close> have "?m' \<le> m" by(rule Least_le)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1046
      ultimately have "\<exists>m'. find_start y w' = Some m' \<and> m' \<le> m" by blast }
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1047
    note find_start_mono = this[rotated]
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1048
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1049
    def assign \<equiv> "\<lambda>y x z. let used = setsum (pp y) {..<x}
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1050
      in case find_start y used of None \<Rightarrow> 0
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1051
         | Some start \<Rightarrow> assign_aux y (setsum (rr y) {..start} - used) start (pp y x) z"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1052
    hence assign_alt_def: "\<And>y x z. assign y x z = 
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1053
      (let used = setsum (pp y) {..<x}
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1054
       in case find_start y used of None \<Rightarrow> 0
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1055
          | Some start \<Rightarrow> assign_aux y (setsum (rr y) {..start} - used) start (pp y x) z)"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1056
      by simp
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1057
    have assign_nonneg [simp]: "\<And>y x z. 0 \<le> assign y x z"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1058
      by(simp add: assign_def diff_le_iff find_start_eq_Some_above split: option.split)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1059
    have assign_eq_0_outside: "\<And>y x z. \<lbrakk> pp y x = 0 \<or> rr y z = 0 \<rbrakk> \<Longrightarrow> assign y x z = 0"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1060
      by(auto simp add: assign_def assign_aux_eq_0_outside diff_le_iff find_start_eq_Some_above find_start_eq_Some_least setsum_nonneg split: option.split)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1061
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1062
    { fix y x z
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1063
      have "(\<Sum>n<Suc x. assign y n z) =
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1064
            (case find_start y (setsum (pp y) {..<x}) of None \<Rightarrow> rr y z
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1065
             | Some m \<Rightarrow> if z < m then rr y z 
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1066
                         else min (rr y z) (max 0 (setsum (pp y) {..<x} + pp y x - setsum (rr y) {..<z})))"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1067
        (is "?lhs x = ?rhs x")
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1068
      proof(induction x)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1069
        case 0 thus ?case 
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1070
          by(auto simp add: assign_def assign_aux_def setsum_head_upt_Suc atLeast0LessThan[symmetric] not_less field_simps max_def)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1071
      next
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1072
        case (Suc x)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1073
        have "?lhs (Suc x) = ?lhs x + assign y (Suc x) z" by simp
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1074
        also have "?lhs x = ?rhs x" by(rule Suc.IH)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1075
        also have "?rhs x + assign y (Suc x) z = ?rhs (Suc x)"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1076
        proof(cases "find_start y (setsum (pp y) {..<Suc x})")
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1077
          case None
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1078
          thus ?thesis
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1079
            by(auto split: option.split simp add: assign_def min_def max_def diff_le_iff setsum_nonneg not_le field_simps)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1080
              (metis add.commute add_increasing find_start_def lessThan_Suc_atMost less_imp_le option.distinct(1) setsum_lessThan_Suc)+
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1081
        next
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1082
          case (Some m)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1083
          have [simp]: "setsum (rr y) {..m} = rr y m + setsum (rr y) {..<m}"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1084
            by(simp add: ivl_disj_un(2)[symmetric])
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1085
          from Some obtain m' where m': "find_start y (setsum (pp y) {..<x}) = Some m'" "m' \<le> m"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1086
            by(auto dest: find_start_mono[where w'2="setsum (pp y) {..<x}"])
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1087
          moreover {
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1088
            assume "z < m"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1089
            then have "setsum (rr y) {..z} \<le> setsum (rr y) {..<m}"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1090
              by(auto intro: setsum_mono3)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1091
            also have "\<dots> \<le> setsum (pp y) {..<Suc x}" using find_start_eq_Some_least[OF Some]
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1092
              by(simp add: ivl_disj_un(2)[symmetric] setsum_nonneg)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1093
            finally have "rr y z \<le> max 0 (setsum (pp y) {..<x} + pp y x - setsum (rr y) {..<z})"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1094
              by(auto simp add: ivl_disj_un(2)[symmetric] max_def diff_le_iff simp del: r_convs)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1095
          } moreover {
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1096
            assume "m \<le> z"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1097
            have "setsum (pp y) {..<Suc x} \<le> setsum (rr y) {..m}"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1098
              using Some by(rule find_start_eq_Some_above)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1099
            also have "\<dots> \<le> setsum (rr y) {..<Suc z}" using \<open>m \<le> z\<close> by(intro setsum_mono3) auto
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1100
            finally have "max 0 (setsum (pp y) {..<x} + pp y x - setsum (rr y) {..<z}) \<le> rr y z" by simp
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1101
            moreover have "z \<noteq> m \<Longrightarrow> setsum (rr y) {..m} + setsum (rr y) {Suc m..<z} = setsum (rr y) {..<z}"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1102
              using \<open>m \<le> z\<close>
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1103
              by(subst ivl_disj_un(8)[where l="Suc m", symmetric])
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1104
                (simp_all add: setsum_Un ivl_disj_un(2)[symmetric] setsum.neutral)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1105
            moreover note calculation
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1106
          } moreover {
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1107
            assume "m < z"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1108
            have "setsum (pp y) {..<Suc x} \<le> setsum (rr y) {..m}"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1109
              using Some by(rule find_start_eq_Some_above)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1110
            also have "\<dots> \<le> setsum (rr y) {..<z}" using \<open>m < z\<close> by(intro setsum_mono3) auto
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1111
            finally have "max 0 (setsum (pp y) {..<Suc x} - setsum (rr y) {..<z}) = 0" by simp }
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1112
          moreover have "setsum (pp y) {..<Suc x} \<ge> setsum (rr y) {..<m}"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1113
            using find_start_eq_Some_least[OF Some]
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1114
            by(simp add: setsum_nonneg ivl_disj_un(2)[symmetric])
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1115
          moreover hence "setsum (pp y) {..<Suc (Suc x)} \<ge> setsum (rr y) {..<m}"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1116
            by(fastforce intro: order_trans)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1117
          ultimately show ?thesis using Some
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1118
            by(auto simp add: assign_def assign_aux_def Let_def field_simps max_def)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1119
        qed
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1120
        finally show ?case .
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1121
      qed }
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1122
    note setsum_assign = this
58587
5484f6079bcd add type for probability mass functions, i.e. discrete probability distribution
hoelzl
parents:
diff changeset
  1123
59023
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1124
    have nn_integral_assign1: "\<And>y z. (\<integral>\<^sup>+ x. assign y x z \<partial>count_space UNIV) = rr y z"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1125
    proof -
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1126
      fix y z
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1127
      have "(\<integral>\<^sup>+ x. assign y x z \<partial>count_space UNIV) = (SUP n. ereal (\<Sum>x<n. assign y x z))"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1128
        by(simp add: nn_integral_count_space_nat suminf_ereal_eq_SUP)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1129
      also have "\<dots> = rr y z"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1130
      proof(rule antisym)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1131
        show "(SUP n. ereal (\<Sum>x<n. assign y x z)) \<le> rr y z"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1132
        proof(rule SUP_least)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1133
          fix n
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1134
          show "ereal (\<Sum>x<n. (assign y x z)) \<le> rr y z"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1135
            using setsum_assign[of y z "n - 1"]
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1136
            by(cases n)(simp_all split: option.split)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1137
        qed
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1138
        show "rr y z \<le> (SUP n. ereal (\<Sum>x<n. assign y x z))"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1139
        proof(cases "setsum (rr y) {..z} < \<integral>\<^sup>+ x. pp y x \<partial>count_space UNIV")
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1140
          case True
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1141
          then obtain n where "setsum (rr y) {..z} < setsum (pp y) {..<n}"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1142
            by(auto simp add: nn_integral_count_space_nat suminf_ereal_eq_SUP less_SUP_iff)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1143
          moreover have "\<And>k. k < z \<Longrightarrow> setsum (rr y) {..k} \<le> setsum (rr y) {..<z}"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1144
            by(auto intro: setsum_mono3)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1145
          ultimately have "rr y z \<le> (\<Sum>x<Suc n. assign y x z)"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1146
            by(subst setsum_assign)(auto split: option.split dest!: find_start_eq_Some_above simp add: ivl_disj_un(2)[symmetric] add.commute add_increasing le_diff_eq le_max_iff_disj)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1147
          also have "\<dots> \<le> (SUP n. ereal (\<Sum>x<n. assign y x z))" 
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1148
            by(rule SUP_upper) simp
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1149
          finally show ?thesis by simp
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1150
        next
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1151
          case False
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1152
          have "setsum (rr y) {..z} = \<integral>\<^sup>+ z. rr y z \<partial>count_space {..z}"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1153
            by(simp add: nn_integral_count_space_finite max_def)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1154
          also have "\<dots> \<le> \<integral>\<^sup>+ z. rr y z \<partial>count_space UNIV"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1155
            by(auto simp add: nn_integral_count_space_indicator indicator_def intro: nn_integral_mono)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1156
          also have "\<dots> = \<integral>\<^sup>+ x. pp y x \<partial>count_space UNIV" by(simp add: eq)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1157
          finally have *: "setsum (rr y) {..z} = \<dots>" using False by simp
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1158
          also have "\<dots> = (SUP n. ereal (\<Sum>x<n. pp y x))"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1159
            by(simp add: nn_integral_count_space_nat suminf_ereal_eq_SUP)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1160
          also have "\<dots> \<le> (SUP n. ereal (\<Sum>x<n. assign y x z)) + setsum (rr y) {..<z}"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1161
          proof(rule SUP_least)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1162
            fix n
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1163
            have "setsum (pp y) {..<n} = \<integral>\<^sup>+ x. pp y x \<partial>count_space {..<n}"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1164
              by(simp add: nn_integral_count_space_finite max_def)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1165
            also have "\<dots> \<le> \<integral>\<^sup>+ x. pp y x \<partial>count_space UNIV"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1166
              by(auto simp add: nn_integral_count_space_indicator indicator_def intro: nn_integral_mono)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1167
            also have "\<dots> = setsum (rr y) {..z}" using * by simp
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1168
            finally obtain k where k: "find_start y (setsum (pp y) {..<n}) = Some k"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1169
              by(fastforce simp add: find_start_def)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1170
            with \<open>ereal (setsum (pp y) {..<n}) \<le> setsum (rr y) {..z}\<close>
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1171
            have "k \<le> z" by(auto simp add: find_start_def split: split_if_asm intro: Least_le)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1172
            then have "setsum (pp y) {..<n} - setsum (rr y) {..<z} \<le> ereal (\<Sum>x<Suc n. assign y x z)"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1173
              using \<open>ereal (setsum (pp y) {..<n}) \<le> setsum (rr y) {..z}\<close>
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1174
              by(subst setsum_assign)(auto simp add: field_simps max_def k ivl_disj_un(2)[symmetric], metis le_add_same_cancel2 max.bounded_iff max_def pos_p)
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1175
            also have "\<dots> \<le> (SUP n. ereal (\<Sum>x<n. assign y x z))"
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1176
              by(rule SUP_upper) simp
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset
  1177
            finally show "ereal (\<Sum>x<n. pp y x) \<le> \<dots> + setsum (rr y) {..<z}" 
4999a616336c register pmf as BNF
Andreas Lochbihler
parents: 59002
diff changeset