src/HOL/Probability/Probability_Measure.thy
author hoelzl
Mon May 19 14:26:58 2014 +0200 (2014-05-19)
changeset 56996 891e992e510f
parent 56994 8d5e5ec1cac3
child 57025 e7fd64f82876
permissions -rw-r--r--
renamed positive_integral to nn_integral
hoelzl@42148
     1
(*  Title:      HOL/Probability/Probability_Measure.thy
hoelzl@42067
     2
    Author:     Johannes Hölzl, TU München
hoelzl@42067
     3
    Author:     Armin Heller, TU München
hoelzl@42067
     4
*)
hoelzl@42067
     5
hoelzl@42148
     6
header {*Probability measure*}
hoelzl@42067
     7
hoelzl@42148
     8
theory Probability_Measure
hoelzl@47694
     9
  imports Lebesgue_Measure Radon_Nikodym
hoelzl@35582
    10
begin
hoelzl@35582
    11
hoelzl@45777
    12
locale prob_space = finite_measure +
hoelzl@47694
    13
  assumes emeasure_space_1: "emeasure M (space M) = 1"
hoelzl@38656
    14
hoelzl@45777
    15
lemma prob_spaceI[Pure.intro!]:
hoelzl@47694
    16
  assumes *: "emeasure M (space M) = 1"
hoelzl@45777
    17
  shows "prob_space M"
hoelzl@45777
    18
proof -
hoelzl@45777
    19
  interpret finite_measure M
hoelzl@45777
    20
  proof
hoelzl@47694
    21
    show "emeasure M (space M) \<noteq> \<infinity>" using * by simp 
hoelzl@45777
    22
  qed
hoelzl@45777
    23
  show "prob_space M" by default fact
hoelzl@38656
    24
qed
hoelzl@38656
    25
hoelzl@40859
    26
abbreviation (in prob_space) "events \<equiv> sets M"
hoelzl@47694
    27
abbreviation (in prob_space) "prob \<equiv> measure M"
hoelzl@47694
    28
abbreviation (in prob_space) "random_variable M' X \<equiv> X \<in> measurable M M'"
wenzelm@53015
    29
abbreviation (in prob_space) "expectation \<equiv> integral\<^sup>L M"
hoelzl@35582
    30
hoelzl@47694
    31
lemma (in prob_space) prob_space_distr:
hoelzl@47694
    32
  assumes f: "f \<in> measurable M M'" shows "prob_space (distr M M' f)"
hoelzl@47694
    33
proof (rule prob_spaceI)
hoelzl@47694
    34
  have "f -` space M' \<inter> space M = space M" using f by (auto dest: measurable_space)
hoelzl@47694
    35
  with f show "emeasure (distr M M' f) (space (distr M M' f)) = 1"
hoelzl@47694
    36
    by (auto simp: emeasure_distr emeasure_space_1)
hoelzl@43339
    37
qed
hoelzl@43339
    38
hoelzl@40859
    39
lemma (in prob_space) prob_space: "prob (space M) = 1"
hoelzl@47694
    40
  using emeasure_space_1 unfolding measure_def by (simp add: one_ereal_def)
hoelzl@41981
    41
hoelzl@41981
    42
lemma (in prob_space) prob_le_1[simp, intro]: "prob A \<le> 1"
hoelzl@41981
    43
  using bounded_measure[of A] by (simp add: prob_space)
hoelzl@41981
    44
hoelzl@47694
    45
lemma (in prob_space) not_empty: "space M \<noteq> {}"
hoelzl@47694
    46
  using prob_space by auto
hoelzl@41981
    47
hoelzl@47694
    48
lemma (in prob_space) measure_le_1: "emeasure M X \<le> 1"
hoelzl@47694
    49
  using emeasure_space[of M X] by (simp add: emeasure_space_1)
hoelzl@42950
    50
hoelzl@43339
    51
lemma (in prob_space) AE_I_eq_1:
hoelzl@47694
    52
  assumes "emeasure M {x\<in>space M. P x} = 1" "{x\<in>space M. P x} \<in> sets M"
hoelzl@47694
    53
  shows "AE x in M. P x"
hoelzl@43339
    54
proof (rule AE_I)
hoelzl@47694
    55
  show "emeasure M (space M - {x \<in> space M. P x}) = 0"
hoelzl@47694
    56
    using assms emeasure_space_1 by (simp add: emeasure_compl)
hoelzl@43339
    57
qed (insert assms, auto)
hoelzl@43339
    58
hoelzl@40859
    59
lemma (in prob_space) prob_compl:
hoelzl@41981
    60
  assumes A: "A \<in> events"
hoelzl@38656
    61
  shows "prob (space M - A) = 1 - prob A"
hoelzl@41981
    62
  using finite_measure_compl[OF A] by (simp add: prob_space)
hoelzl@35582
    63
hoelzl@47694
    64
lemma (in prob_space) AE_in_set_eq_1:
hoelzl@47694
    65
  assumes "A \<in> events" shows "(AE x in M. x \<in> A) \<longleftrightarrow> prob A = 1"
hoelzl@47694
    66
proof
hoelzl@47694
    67
  assume ae: "AE x in M. x \<in> A"
hoelzl@47694
    68
  have "{x \<in> space M. x \<in> A} = A" "{x \<in> space M. x \<notin> A} = space M - A"
immler@50244
    69
    using `A \<in> events`[THEN sets.sets_into_space] by auto
hoelzl@47694
    70
  with AE_E2[OF ae] `A \<in> events` have "1 - emeasure M A = 0"
hoelzl@47694
    71
    by (simp add: emeasure_compl emeasure_space_1)
hoelzl@47694
    72
  then show "prob A = 1"
hoelzl@47694
    73
    using `A \<in> events` by (simp add: emeasure_eq_measure one_ereal_def)
hoelzl@47694
    74
next
hoelzl@47694
    75
  assume prob: "prob A = 1"
hoelzl@47694
    76
  show "AE x in M. x \<in> A"
hoelzl@47694
    77
  proof (rule AE_I)
hoelzl@47694
    78
    show "{x \<in> space M. x \<notin> A} \<subseteq> space M - A" by auto
hoelzl@47694
    79
    show "emeasure M (space M - A) = 0"
hoelzl@47694
    80
      using `A \<in> events` prob
hoelzl@47694
    81
      by (simp add: prob_compl emeasure_space_1 emeasure_eq_measure one_ereal_def)
hoelzl@47694
    82
    show "space M - A \<in> events"
hoelzl@47694
    83
      using `A \<in> events` by auto
hoelzl@47694
    84
  qed
hoelzl@47694
    85
qed
hoelzl@47694
    86
hoelzl@47694
    87
lemma (in prob_space) AE_False: "(AE x in M. False) \<longleftrightarrow> False"
hoelzl@47694
    88
proof
hoelzl@47694
    89
  assume "AE x in M. False"
hoelzl@47694
    90
  then have "AE x in M. x \<in> {}" by simp
hoelzl@47694
    91
  then show False
hoelzl@47694
    92
    by (subst (asm) AE_in_set_eq_1) auto
hoelzl@47694
    93
qed simp
hoelzl@47694
    94
hoelzl@47694
    95
lemma (in prob_space) AE_prob_1:
hoelzl@47694
    96
  assumes "prob A = 1" shows "AE x in M. x \<in> A"
hoelzl@47694
    97
proof -
hoelzl@47694
    98
  from `prob A = 1` have "A \<in> events"
hoelzl@47694
    99
    by (metis measure_notin_sets zero_neq_one)
hoelzl@47694
   100
  with AE_in_set_eq_1 assms show ?thesis by simp
hoelzl@47694
   101
qed
hoelzl@47694
   102
hoelzl@50098
   103
lemma (in prob_space) AE_const[simp]: "(AE x in M. P) \<longleftrightarrow> P"
hoelzl@50098
   104
  by (cases P) (auto simp: AE_False)
hoelzl@50098
   105
hoelzl@50098
   106
lemma (in prob_space) AE_contr:
hoelzl@50098
   107
  assumes ae: "AE \<omega> in M. P \<omega>" "AE \<omega> in M. \<not> P \<omega>"
hoelzl@50098
   108
  shows False
hoelzl@50098
   109
proof -
hoelzl@50098
   110
  from ae have "AE \<omega> in M. False" by eventually_elim auto
hoelzl@50098
   111
  then show False by auto
hoelzl@50098
   112
qed
hoelzl@50098
   113
hoelzl@43339
   114
lemma (in prob_space) expectation_less:
hoelzl@56993
   115
  fixes X :: "_ \<Rightarrow> real"
hoelzl@43339
   116
  assumes [simp]: "integrable M X"
hoelzl@49786
   117
  assumes gt: "AE x in M. X x < b"
hoelzl@43339
   118
  shows "expectation X < b"
hoelzl@43339
   119
proof -
hoelzl@43339
   120
  have "expectation X < expectation (\<lambda>x. b)"
hoelzl@47694
   121
    using gt emeasure_space_1
hoelzl@43340
   122
    by (intro integral_less_AE_space) auto
hoelzl@43339
   123
  then show ?thesis using prob_space by simp
hoelzl@43339
   124
qed
hoelzl@43339
   125
hoelzl@43339
   126
lemma (in prob_space) expectation_greater:
hoelzl@56993
   127
  fixes X :: "_ \<Rightarrow> real"
hoelzl@43339
   128
  assumes [simp]: "integrable M X"
hoelzl@49786
   129
  assumes gt: "AE x in M. a < X x"
hoelzl@43339
   130
  shows "a < expectation X"
hoelzl@43339
   131
proof -
hoelzl@43339
   132
  have "expectation (\<lambda>x. a) < expectation X"
hoelzl@47694
   133
    using gt emeasure_space_1
hoelzl@43340
   134
    by (intro integral_less_AE_space) auto
hoelzl@43339
   135
  then show ?thesis using prob_space by simp
hoelzl@43339
   136
qed
hoelzl@43339
   137
hoelzl@43339
   138
lemma (in prob_space) jensens_inequality:
hoelzl@56993
   139
  fixes q :: "real \<Rightarrow> real"
hoelzl@49786
   140
  assumes X: "integrable M X" "AE x in M. X x \<in> I"
hoelzl@43339
   141
  assumes I: "I = {a <..< b} \<or> I = {a <..} \<or> I = {..< b} \<or> I = UNIV"
hoelzl@43339
   142
  assumes q: "integrable M (\<lambda>x. q (X x))" "convex_on I q"
hoelzl@43339
   143
  shows "q (expectation X) \<le> expectation (\<lambda>x. q (X x))"
hoelzl@43339
   144
proof -
wenzelm@46731
   145
  let ?F = "\<lambda>x. Inf ((\<lambda>t. (q x - q t) / (x - t)) ` ({x<..} \<inter> I))"
hoelzl@49786
   146
  from X(2) AE_False have "I \<noteq> {}" by auto
hoelzl@43339
   147
hoelzl@43339
   148
  from I have "open I" by auto
hoelzl@43339
   149
hoelzl@43339
   150
  note I
hoelzl@43339
   151
  moreover
hoelzl@43339
   152
  { assume "I \<subseteq> {a <..}"
hoelzl@43339
   153
    with X have "a < expectation X"
hoelzl@43339
   154
      by (intro expectation_greater) auto }
hoelzl@43339
   155
  moreover
hoelzl@43339
   156
  { assume "I \<subseteq> {..< b}"
hoelzl@43339
   157
    with X have "expectation X < b"
hoelzl@43339
   158
      by (intro expectation_less) auto }
hoelzl@43339
   159
  ultimately have "expectation X \<in> I"
hoelzl@43339
   160
    by (elim disjE)  (auto simp: subset_eq)
hoelzl@43339
   161
  moreover
hoelzl@43339
   162
  { fix y assume y: "y \<in> I"
hoelzl@43339
   163
    with q(2) `open I` have "Sup ((\<lambda>x. q x + ?F x * (y - x)) ` I) = q y"
haftmann@56166
   164
      by (auto intro!: cSup_eq_maximum convex_le_Inf_differential image_eqI [OF _ y] simp: interior_open simp del: Sup_image_eq Inf_image_eq) }
hoelzl@43339
   165
  ultimately have "q (expectation X) = Sup ((\<lambda>x. q x + ?F x * (expectation X - x)) ` I)"
hoelzl@43339
   166
    by simp
hoelzl@43339
   167
  also have "\<dots> \<le> expectation (\<lambda>w. q (X w))"
hoelzl@51475
   168
  proof (rule cSup_least)
hoelzl@43339
   169
    show "(\<lambda>x. q x + ?F x * (expectation X - x)) ` I \<noteq> {}"
hoelzl@43339
   170
      using `I \<noteq> {}` by auto
hoelzl@43339
   171
  next
hoelzl@43339
   172
    fix k assume "k \<in> (\<lambda>x. q x + ?F x * (expectation X - x)) ` I"
hoelzl@43339
   173
    then guess x .. note x = this
hoelzl@43339
   174
    have "q x + ?F x * (expectation X  - x) = expectation (\<lambda>w. q x + ?F x * (X w - x))"
hoelzl@47694
   175
      using prob_space by (simp add: X)
hoelzl@43339
   176
    also have "\<dots> \<le> expectation (\<lambda>w. q (X w))"
hoelzl@43339
   177
      using `x \<in> I` `open I` X(2)
hoelzl@56993
   178
      apply (intro integral_mono_AE integrable_add integrable_mult_right integrable_diff
hoelzl@56993
   179
                integrable_const X q)
hoelzl@49786
   180
      apply (elim eventually_elim1)
hoelzl@49786
   181
      apply (intro convex_le_Inf_differential)
hoelzl@49786
   182
      apply (auto simp: interior_open q)
hoelzl@49786
   183
      done
hoelzl@43339
   184
    finally show "k \<le> expectation (\<lambda>w. q (X w))" using x by auto
hoelzl@43339
   185
  qed
hoelzl@43339
   186
  finally show "q (expectation X) \<le> expectation (\<lambda>x. q (X x))" .
hoelzl@43339
   187
qed
hoelzl@43339
   188
hoelzl@50001
   189
subsection  {* Introduce binder for probability *}
hoelzl@50001
   190
hoelzl@50001
   191
syntax
hoelzl@50001
   192
  "_prob" :: "pttrn \<Rightarrow> logic \<Rightarrow> logic \<Rightarrow> logic" ("('\<P>'(_ in _. _'))")
hoelzl@50001
   193
hoelzl@50001
   194
translations
hoelzl@50001
   195
  "\<P>(x in M. P)" => "CONST measure M {x \<in> CONST space M. P}"
hoelzl@50001
   196
hoelzl@50001
   197
definition
hoelzl@50001
   198
  "cond_prob M P Q = \<P>(\<omega> in M. P \<omega> \<and> Q \<omega>) / \<P>(\<omega> in M. Q \<omega>)"
hoelzl@50001
   199
hoelzl@50001
   200
syntax
hoelzl@50001
   201
  "_conditional_prob" :: "pttrn \<Rightarrow> logic \<Rightarrow> logic \<Rightarrow> logic \<Rightarrow> logic" ("('\<P>'(_ in _. _ \<bar>/ _'))")
hoelzl@50001
   202
hoelzl@50001
   203
translations
hoelzl@50001
   204
  "\<P>(x in M. P \<bar> Q)" => "CONST cond_prob M (\<lambda>x. P) (\<lambda>x. Q)"
hoelzl@50001
   205
hoelzl@50001
   206
lemma (in prob_space) AE_E_prob:
hoelzl@50001
   207
  assumes ae: "AE x in M. P x"
hoelzl@50001
   208
  obtains S where "S \<subseteq> {x \<in> space M. P x}" "S \<in> events" "prob S = 1"
hoelzl@50001
   209
proof -
hoelzl@50001
   210
  from ae[THEN AE_E] guess N .
hoelzl@50001
   211
  then show thesis
hoelzl@50001
   212
    by (intro that[of "space M - N"])
hoelzl@50001
   213
       (auto simp: prob_compl prob_space emeasure_eq_measure)
hoelzl@50001
   214
qed
hoelzl@50001
   215
hoelzl@50001
   216
lemma (in prob_space) prob_neg: "{x\<in>space M. P x} \<in> events \<Longrightarrow> \<P>(x in M. \<not> P x) = 1 - \<P>(x in M. P x)"
hoelzl@50001
   217
  by (auto intro!: arg_cong[where f=prob] simp add: prob_compl[symmetric])
hoelzl@50001
   218
hoelzl@50001
   219
lemma (in prob_space) prob_eq_AE:
hoelzl@50001
   220
  "(AE x in M. P x \<longleftrightarrow> Q x) \<Longrightarrow> {x\<in>space M. P x} \<in> events \<Longrightarrow> {x\<in>space M. Q x} \<in> events \<Longrightarrow> \<P>(x in M. P x) = \<P>(x in M. Q x)"
hoelzl@50001
   221
  by (rule finite_measure_eq_AE) auto
hoelzl@50001
   222
hoelzl@50001
   223
lemma (in prob_space) prob_eq_0_AE:
hoelzl@50001
   224
  assumes not: "AE x in M. \<not> P x" shows "\<P>(x in M. P x) = 0"
hoelzl@50001
   225
proof cases
hoelzl@50001
   226
  assume "{x\<in>space M. P x} \<in> events"
hoelzl@50001
   227
  with not have "\<P>(x in M. P x) = \<P>(x in M. False)"
hoelzl@50001
   228
    by (intro prob_eq_AE) auto
hoelzl@50001
   229
  then show ?thesis by simp
hoelzl@50001
   230
qed (simp add: measure_notin_sets)
hoelzl@50001
   231
hoelzl@50098
   232
lemma (in prob_space) prob_Collect_eq_0:
hoelzl@50098
   233
  "{x \<in> space M. P x} \<in> sets M \<Longrightarrow> \<P>(x in M. P x) = 0 \<longleftrightarrow> (AE x in M. \<not> P x)"
hoelzl@50098
   234
  using AE_iff_measurable[OF _ refl, of M "\<lambda>x. \<not> P x"] by (simp add: emeasure_eq_measure)
hoelzl@50098
   235
hoelzl@50098
   236
lemma (in prob_space) prob_Collect_eq_1:
hoelzl@50098
   237
  "{x \<in> space M. P x} \<in> sets M \<Longrightarrow> \<P>(x in M. P x) = 1 \<longleftrightarrow> (AE x in M. P x)"
hoelzl@50098
   238
  using AE_in_set_eq_1[of "{x\<in>space M. P x}"] by simp
hoelzl@50098
   239
hoelzl@50098
   240
lemma (in prob_space) prob_eq_0:
hoelzl@50098
   241
  "A \<in> sets M \<Longrightarrow> prob A = 0 \<longleftrightarrow> (AE x in M. x \<notin> A)"
hoelzl@50098
   242
  using AE_iff_measurable[OF _ refl, of M "\<lambda>x. x \<notin> A"]
hoelzl@50098
   243
  by (auto simp add: emeasure_eq_measure Int_def[symmetric])
hoelzl@50098
   244
hoelzl@50098
   245
lemma (in prob_space) prob_eq_1:
hoelzl@50098
   246
  "A \<in> sets M \<Longrightarrow> prob A = 1 \<longleftrightarrow> (AE x in M. x \<in> A)"
hoelzl@50098
   247
  using AE_in_set_eq_1[of A] by simp
hoelzl@50098
   248
hoelzl@50001
   249
lemma (in prob_space) prob_sums:
hoelzl@50001
   250
  assumes P: "\<And>n. {x\<in>space M. P n x} \<in> events"
hoelzl@50001
   251
  assumes Q: "{x\<in>space M. Q x} \<in> events"
hoelzl@50001
   252
  assumes ae: "AE x in M. (\<forall>n. P n x \<longrightarrow> Q x) \<and> (Q x \<longrightarrow> (\<exists>!n. P n x))"
hoelzl@50001
   253
  shows "(\<lambda>n. \<P>(x in M. P n x)) sums \<P>(x in M. Q x)"
hoelzl@50001
   254
proof -
hoelzl@50001
   255
  from ae[THEN AE_E_prob] guess S . note S = this
hoelzl@50001
   256
  then have disj: "disjoint_family (\<lambda>n. {x\<in>space M. P n x} \<inter> S)"
hoelzl@50001
   257
    by (auto simp: disjoint_family_on_def)
hoelzl@50001
   258
  from S have ae_S:
hoelzl@50001
   259
    "AE x in M. x \<in> {x\<in>space M. Q x} \<longleftrightarrow> x \<in> (\<Union>n. {x\<in>space M. P n x} \<inter> S)"
hoelzl@50001
   260
    "\<And>n. AE x in M. x \<in> {x\<in>space M. P n x} \<longleftrightarrow> x \<in> {x\<in>space M. P n x} \<inter> S"
hoelzl@50001
   261
    using ae by (auto dest!: AE_prob_1)
hoelzl@50001
   262
  from ae_S have *:
hoelzl@50001
   263
    "\<P>(x in M. Q x) = prob (\<Union>n. {x\<in>space M. P n x} \<inter> S)"
hoelzl@50001
   264
    using P Q S by (intro finite_measure_eq_AE) auto
hoelzl@50001
   265
  from ae_S have **:
hoelzl@50001
   266
    "\<And>n. \<P>(x in M. P n x) = prob ({x\<in>space M. P n x} \<inter> S)"
hoelzl@50001
   267
    using P Q S by (intro finite_measure_eq_AE) auto
hoelzl@50001
   268
  show ?thesis
hoelzl@50001
   269
    unfolding * ** using S P disj
hoelzl@50001
   270
    by (intro finite_measure_UNION) auto
hoelzl@50001
   271
qed
hoelzl@50001
   272
hoelzl@54418
   273
lemma (in prob_space) prob_EX_countable:
hoelzl@54418
   274
  assumes sets: "\<And>i. i \<in> I \<Longrightarrow> {x\<in>space M. P i x} \<in> sets M" and I: "countable I" 
hoelzl@54418
   275
  assumes disj: "AE x in M. \<forall>i\<in>I. \<forall>j\<in>I. P i x \<longrightarrow> P j x \<longrightarrow> i = j"
hoelzl@54418
   276
  shows "\<P>(x in M. \<exists>i\<in>I. P i x) = (\<integral>\<^sup>+i. \<P>(x in M. P i x) \<partial>count_space I)"
hoelzl@54418
   277
proof -
hoelzl@54418
   278
  let ?N= "\<lambda>x. \<exists>!i\<in>I. P i x"
hoelzl@54418
   279
  have "ereal (\<P>(x in M. \<exists>i\<in>I. P i x)) = \<P>(x in M. (\<exists>i\<in>I. P i x \<and> ?N x))"
hoelzl@54418
   280
    unfolding ereal.inject
hoelzl@54418
   281
  proof (rule prob_eq_AE)
hoelzl@54418
   282
    show "AE x in M. (\<exists>i\<in>I. P i x) = (\<exists>i\<in>I. P i x \<and> ?N x)"
hoelzl@54418
   283
      using disj by eventually_elim blast
hoelzl@54418
   284
  qed (auto intro!: sets.sets_Collect_countable_Ex' sets.sets_Collect_conj sets.sets_Collect_countable_Ex1' I sets)+
hoelzl@54418
   285
  also have "\<P>(x in M. (\<exists>i\<in>I. P i x \<and> ?N x)) = emeasure M (\<Union>i\<in>I. {x\<in>space M. P i x \<and> ?N x})"
hoelzl@54418
   286
    unfolding emeasure_eq_measure by (auto intro!: arg_cong[where f=prob])
hoelzl@54418
   287
  also have "\<dots> = (\<integral>\<^sup>+i. emeasure M {x\<in>space M. P i x \<and> ?N x} \<partial>count_space I)"
hoelzl@54418
   288
    by (rule emeasure_UN_countable)
hoelzl@54418
   289
       (auto intro!: sets.sets_Collect_countable_Ex' sets.sets_Collect_conj sets.sets_Collect_countable_Ex1' I sets
hoelzl@54418
   290
             simp: disjoint_family_on_def)
hoelzl@54418
   291
  also have "\<dots> = (\<integral>\<^sup>+i. \<P>(x in M. P i x) \<partial>count_space I)"
hoelzl@54418
   292
    unfolding emeasure_eq_measure using disj
hoelzl@56996
   293
    by (intro nn_integral_cong ereal.inject[THEN iffD2] prob_eq_AE)
hoelzl@54418
   294
       (auto intro!: sets.sets_Collect_countable_Ex' sets.sets_Collect_conj sets.sets_Collect_countable_Ex1' I sets)+
hoelzl@54418
   295
  finally show ?thesis .
hoelzl@54418
   296
qed
hoelzl@54418
   297
hoelzl@50001
   298
lemma (in prob_space) cond_prob_eq_AE:
hoelzl@50001
   299
  assumes P: "AE x in M. Q x \<longrightarrow> P x \<longleftrightarrow> P' x" "{x\<in>space M. P x} \<in> events" "{x\<in>space M. P' x} \<in> events"
hoelzl@50001
   300
  assumes Q: "AE x in M. Q x \<longleftrightarrow> Q' x" "{x\<in>space M. Q x} \<in> events" "{x\<in>space M. Q' x} \<in> events"
hoelzl@50001
   301
  shows "cond_prob M P Q = cond_prob M P' Q'"
hoelzl@50001
   302
  using P Q
immler@50244
   303
  by (auto simp: cond_prob_def intro!: arg_cong2[where f="op /"] prob_eq_AE sets.sets_Collect_conj)
hoelzl@50001
   304
hoelzl@50001
   305
hoelzl@40859
   306
lemma (in prob_space) joint_distribution_Times_le_fst:
hoelzl@47694
   307
  "random_variable MX X \<Longrightarrow> random_variable MY Y \<Longrightarrow> A \<in> sets MX \<Longrightarrow> B \<in> sets MY
wenzelm@53015
   308
    \<Longrightarrow> emeasure (distr M (MX \<Otimes>\<^sub>M MY) (\<lambda>x. (X x, Y x))) (A \<times> B) \<le> emeasure (distr M MX X) A"
hoelzl@47694
   309
  by (auto simp: emeasure_distr measurable_pair_iff comp_def intro!: emeasure_mono measurable_sets)
hoelzl@40859
   310
hoelzl@40859
   311
lemma (in prob_space) joint_distribution_Times_le_snd:
hoelzl@47694
   312
  "random_variable MX X \<Longrightarrow> random_variable MY Y \<Longrightarrow> A \<in> sets MX \<Longrightarrow> B \<in> sets MY
wenzelm@53015
   313
    \<Longrightarrow> emeasure (distr M (MX \<Otimes>\<^sub>M MY) (\<lambda>x. (X x, Y x))) (A \<times> B) \<le> emeasure (distr M MY Y) B"
hoelzl@47694
   314
  by (auto simp: emeasure_distr measurable_pair_iff comp_def intro!: emeasure_mono measurable_sets)
hoelzl@40859
   315
hoelzl@45777
   316
locale pair_prob_space = pair_sigma_finite M1 M2 + M1: prob_space M1 + M2: prob_space M2 for M1 M2
hoelzl@41689
   317
wenzelm@53015
   318
sublocale pair_prob_space \<subseteq> P: prob_space "M1 \<Otimes>\<^sub>M M2"
hoelzl@45777
   319
proof
wenzelm@53015
   320
  show "emeasure (M1 \<Otimes>\<^sub>M M2) (space (M1 \<Otimes>\<^sub>M M2)) = 1"
hoelzl@49776
   321
    by (simp add: M2.emeasure_pair_measure_Times M1.emeasure_space_1 M2.emeasure_space_1 space_pair_measure)
hoelzl@45777
   322
qed
hoelzl@40859
   323
hoelzl@47694
   324
locale product_prob_space = product_sigma_finite M for M :: "'i \<Rightarrow> 'a measure" +
hoelzl@45777
   325
  fixes I :: "'i set"
hoelzl@45777
   326
  assumes prob_space: "\<And>i. prob_space (M i)"
hoelzl@42988
   327
hoelzl@45777
   328
sublocale product_prob_space \<subseteq> M: prob_space "M i" for i
hoelzl@42988
   329
  by (rule prob_space)
hoelzl@42988
   330
hoelzl@45777
   331
locale finite_product_prob_space = finite_product_sigma_finite M I + product_prob_space M I for M I
hoelzl@42988
   332
wenzelm@53015
   333
sublocale finite_product_prob_space \<subseteq> prob_space "\<Pi>\<^sub>M i\<in>I. M i"
hoelzl@45777
   334
proof
wenzelm@53015
   335
  show "emeasure (\<Pi>\<^sub>M i\<in>I. M i) (space (\<Pi>\<^sub>M i\<in>I. M i)) = 1"
hoelzl@47694
   336
    by (simp add: measure_times M.emeasure_space_1 setprod_1 space_PiM)
hoelzl@45777
   337
qed
hoelzl@42988
   338
hoelzl@42988
   339
lemma (in finite_product_prob_space) prob_times:
hoelzl@42988
   340
  assumes X: "\<And>i. i \<in> I \<Longrightarrow> X i \<in> sets (M i)"
wenzelm@53015
   341
  shows "prob (\<Pi>\<^sub>E i\<in>I. X i) = (\<Prod>i\<in>I. M.prob i (X i))"
hoelzl@42988
   342
proof -
wenzelm@53015
   343
  have "ereal (measure (\<Pi>\<^sub>M i\<in>I. M i) (\<Pi>\<^sub>E i\<in>I. X i)) = emeasure (\<Pi>\<^sub>M i\<in>I. M i) (\<Pi>\<^sub>E i\<in>I. X i)"
hoelzl@47694
   344
    using X by (simp add: emeasure_eq_measure)
hoelzl@47694
   345
  also have "\<dots> = (\<Prod>i\<in>I. emeasure (M i) (X i))"
hoelzl@42988
   346
    using measure_times X by simp
hoelzl@47694
   347
  also have "\<dots> = ereal (\<Prod>i\<in>I. measure (M i) (X i))"
hoelzl@47694
   348
    using X by (simp add: M.emeasure_eq_measure setprod_ereal)
hoelzl@42859
   349
  finally show ?thesis by simp
hoelzl@42859
   350
qed
hoelzl@42859
   351
hoelzl@56994
   352
subsection {* Distributions *}
hoelzl@42892
   353
hoelzl@47694
   354
definition "distributed M N X f \<longleftrightarrow> distr M N X = density N f \<and> 
hoelzl@47694
   355
  f \<in> borel_measurable N \<and> (AE x in N. 0 \<le> f x) \<and> X \<in> measurable M N"
hoelzl@36624
   356
hoelzl@47694
   357
lemma
hoelzl@50003
   358
  assumes "distributed M N X f"
hoelzl@50003
   359
  shows distributed_distr_eq_density: "distr M N X = density N f"
hoelzl@50003
   360
    and distributed_measurable: "X \<in> measurable M N"
hoelzl@50003
   361
    and distributed_borel_measurable: "f \<in> borel_measurable N"
hoelzl@50003
   362
    and distributed_AE: "(AE x in N. 0 \<le> f x)"
hoelzl@50003
   363
  using assms by (simp_all add: distributed_def)
hoelzl@50003
   364
hoelzl@50003
   365
lemma
hoelzl@50003
   366
  assumes D: "distributed M N X f"
hoelzl@50003
   367
  shows distributed_measurable'[measurable_dest]:
hoelzl@50003
   368
      "g \<in> measurable L M \<Longrightarrow> (\<lambda>x. X (g x)) \<in> measurable L N"
hoelzl@50003
   369
    and distributed_borel_measurable'[measurable_dest]:
hoelzl@50003
   370
      "h \<in> measurable L N \<Longrightarrow> (\<lambda>x. f (h x)) \<in> borel_measurable L"
hoelzl@50003
   371
  using distributed_measurable[OF D] distributed_borel_measurable[OF D]
hoelzl@50003
   372
  by simp_all
hoelzl@39097
   373
hoelzl@47694
   374
lemma
hoelzl@47694
   375
  shows distributed_real_measurable: "distributed M N X (\<lambda>x. ereal (f x)) \<Longrightarrow> f \<in> borel_measurable N"
hoelzl@47694
   376
    and distributed_real_AE: "distributed M N X (\<lambda>x. ereal (f x)) \<Longrightarrow> (AE x in N. 0 \<le> f x)"
hoelzl@47694
   377
  by (simp_all add: distributed_def borel_measurable_ereal_iff)
hoelzl@35977
   378
hoelzl@50003
   379
lemma
hoelzl@50003
   380
  assumes D: "distributed M N X (\<lambda>x. ereal (f x))"
hoelzl@50003
   381
  shows distributed_real_measurable'[measurable_dest]:
hoelzl@50003
   382
      "h \<in> measurable L N \<Longrightarrow> (\<lambda>x. f (h x)) \<in> borel_measurable L"
hoelzl@50003
   383
  using distributed_real_measurable[OF D]
hoelzl@50003
   384
  by simp_all
hoelzl@50003
   385
hoelzl@50003
   386
lemma
wenzelm@53015
   387
  assumes D: "distributed M (S \<Otimes>\<^sub>M T) (\<lambda>x. (X x, Y x)) f"
hoelzl@50003
   388
  shows joint_distributed_measurable1[measurable_dest]:
hoelzl@50003
   389
      "h1 \<in> measurable N M \<Longrightarrow> (\<lambda>x. X (h1 x)) \<in> measurable N S"
hoelzl@50003
   390
    and joint_distributed_measurable2[measurable_dest]:
hoelzl@50003
   391
      "h2 \<in> measurable N M \<Longrightarrow> (\<lambda>x. Y (h2 x)) \<in> measurable N T"
hoelzl@50003
   392
  using measurable_compose[OF distributed_measurable[OF D] measurable_fst]
hoelzl@50003
   393
  using measurable_compose[OF distributed_measurable[OF D] measurable_snd]
hoelzl@50003
   394
  by auto
hoelzl@50003
   395
hoelzl@47694
   396
lemma distributed_count_space:
hoelzl@47694
   397
  assumes X: "distributed M (count_space A) X P" and a: "a \<in> A" and A: "finite A"
hoelzl@47694
   398
  shows "P a = emeasure M (X -` {a} \<inter> space M)"
hoelzl@39097
   399
proof -
hoelzl@47694
   400
  have "emeasure M (X -` {a} \<inter> space M) = emeasure (distr M (count_space A) X) {a}"
hoelzl@50003
   401
    using X a A by (simp add: emeasure_distr)
hoelzl@47694
   402
  also have "\<dots> = emeasure (density (count_space A) P) {a}"
hoelzl@47694
   403
    using X by (simp add: distributed_distr_eq_density)
wenzelm@53015
   404
  also have "\<dots> = (\<integral>\<^sup>+x. P a * indicator {a} x \<partial>count_space A)"
hoelzl@56996
   405
    using X a by (auto simp add: emeasure_density distributed_def indicator_def intro!: nn_integral_cong)
hoelzl@47694
   406
  also have "\<dots> = P a"
hoelzl@56996
   407
    using X a by (subst nn_integral_cmult_indicator) (auto simp: distributed_def one_ereal_def[symmetric] AE_count_space)
hoelzl@47694
   408
  finally show ?thesis ..
hoelzl@39092
   409
qed
hoelzl@35977
   410
hoelzl@47694
   411
lemma distributed_cong_density:
hoelzl@47694
   412
  "(AE x in N. f x = g x) \<Longrightarrow> g \<in> borel_measurable N \<Longrightarrow> f \<in> borel_measurable N \<Longrightarrow>
hoelzl@47694
   413
    distributed M N X f \<longleftrightarrow> distributed M N X g"
hoelzl@47694
   414
  by (auto simp: distributed_def intro!: density_cong)
hoelzl@47694
   415
hoelzl@47694
   416
lemma subdensity:
hoelzl@47694
   417
  assumes T: "T \<in> measurable P Q"
hoelzl@47694
   418
  assumes f: "distributed M P X f"
hoelzl@47694
   419
  assumes g: "distributed M Q Y g"
hoelzl@47694
   420
  assumes Y: "Y = T \<circ> X"
hoelzl@47694
   421
  shows "AE x in P. g (T x) = 0 \<longrightarrow> f x = 0"
hoelzl@47694
   422
proof -
hoelzl@47694
   423
  have "{x\<in>space Q. g x = 0} \<in> null_sets (distr M Q (T \<circ> X))"
hoelzl@47694
   424
    using g Y by (auto simp: null_sets_density_iff distributed_def)
hoelzl@47694
   425
  also have "distr M Q (T \<circ> X) = distr (distr M P X) Q T"
hoelzl@47694
   426
    using T f[THEN distributed_measurable] by (rule distr_distr[symmetric])
hoelzl@47694
   427
  finally have "T -` {x\<in>space Q. g x = 0} \<inter> space P \<in> null_sets (distr M P X)"
hoelzl@47694
   428
    using T by (subst (asm) null_sets_distr_iff) auto
hoelzl@47694
   429
  also have "T -` {x\<in>space Q. g x = 0} \<inter> space P = {x\<in>space P. g (T x) = 0}"
hoelzl@47694
   430
    using T by (auto dest: measurable_space)
hoelzl@47694
   431
  finally show ?thesis
hoelzl@47694
   432
    using f g by (auto simp add: null_sets_density_iff distributed_def)
hoelzl@35977
   433
qed
hoelzl@35977
   434
hoelzl@47694
   435
lemma subdensity_real:
hoelzl@47694
   436
  fixes g :: "'a \<Rightarrow> real" and f :: "'b \<Rightarrow> real"
hoelzl@47694
   437
  assumes T: "T \<in> measurable P Q"
hoelzl@47694
   438
  assumes f: "distributed M P X f"
hoelzl@47694
   439
  assumes g: "distributed M Q Y g"
hoelzl@47694
   440
  assumes Y: "Y = T \<circ> X"
hoelzl@47694
   441
  shows "AE x in P. g (T x) = 0 \<longrightarrow> f x = 0"
hoelzl@47694
   442
  using subdensity[OF T, of M X "\<lambda>x. ereal (f x)" Y "\<lambda>x. ereal (g x)"] assms by auto
hoelzl@47694
   443
hoelzl@49788
   444
lemma distributed_emeasure:
wenzelm@53015
   445
  "distributed M N X f \<Longrightarrow> A \<in> sets N \<Longrightarrow> emeasure M (X -` A \<inter> space M) = (\<integral>\<^sup>+x. f x * indicator A x \<partial>N)"
hoelzl@50003
   446
  by (auto simp: distributed_AE
hoelzl@49788
   447
                 distributed_distr_eq_density[symmetric] emeasure_density[symmetric] emeasure_distr)
hoelzl@49788
   448
hoelzl@56996
   449
lemma distributed_nn_integral:
wenzelm@53015
   450
  "distributed M N X f \<Longrightarrow> g \<in> borel_measurable N \<Longrightarrow> (\<integral>\<^sup>+x. f x * g x \<partial>N) = (\<integral>\<^sup>+x. g (X x) \<partial>M)"
hoelzl@50003
   451
  by (auto simp: distributed_AE
hoelzl@56996
   452
                 distributed_distr_eq_density[symmetric] nn_integral_density[symmetric] nn_integral_distr)
hoelzl@49788
   453
hoelzl@47694
   454
lemma distributed_integral:
hoelzl@47694
   455
  "distributed M N X f \<Longrightarrow> g \<in> borel_measurable N \<Longrightarrow> (\<integral>x. f x * g x \<partial>N) = (\<integral>x. g (X x) \<partial>M)"
hoelzl@50003
   456
  by (auto simp: distributed_real_AE
hoelzl@56993
   457
                 distributed_distr_eq_density[symmetric] integral_real_density[symmetric] integral_distr)
hoelzl@47694
   458
  
hoelzl@47694
   459
lemma distributed_transform_integral:
hoelzl@47694
   460
  assumes Px: "distributed M N X Px"
hoelzl@47694
   461
  assumes "distributed M P Y Py"
hoelzl@47694
   462
  assumes Y: "Y = T \<circ> X" and T: "T \<in> measurable N P" and f: "f \<in> borel_measurable P"
hoelzl@47694
   463
  shows "(\<integral>x. Py x * f x \<partial>P) = (\<integral>x. Px x * f (T x) \<partial>N)"
hoelzl@41689
   464
proof -
hoelzl@47694
   465
  have "(\<integral>x. Py x * f x \<partial>P) = (\<integral>x. f (Y x) \<partial>M)"
hoelzl@47694
   466
    by (rule distributed_integral) fact+
hoelzl@47694
   467
  also have "\<dots> = (\<integral>x. f (T (X x)) \<partial>M)"
hoelzl@47694
   468
    using Y by simp
hoelzl@47694
   469
  also have "\<dots> = (\<integral>x. Px x * f (T x) \<partial>N)"
hoelzl@47694
   470
    using measurable_comp[OF T f] Px by (intro distributed_integral[symmetric]) (auto simp: comp_def)
hoelzl@45777
   471
  finally show ?thesis .
hoelzl@39092
   472
qed
hoelzl@36624
   473
hoelzl@49788
   474
lemma (in prob_space) distributed_unique:
hoelzl@47694
   475
  assumes Px: "distributed M S X Px"
hoelzl@49788
   476
  assumes Py: "distributed M S X Py"
hoelzl@49788
   477
  shows "AE x in S. Px x = Py x"
hoelzl@49788
   478
proof -
hoelzl@49788
   479
  interpret X: prob_space "distr M S X"
hoelzl@50003
   480
    using Px by (intro prob_space_distr) simp
hoelzl@49788
   481
  have "sigma_finite_measure (distr M S X)" ..
hoelzl@49788
   482
  with sigma_finite_density_unique[of Px S Py ] Px Py
hoelzl@49788
   483
  show ?thesis
hoelzl@49788
   484
    by (auto simp: distributed_def)
hoelzl@49788
   485
qed
hoelzl@49788
   486
hoelzl@49788
   487
lemma (in prob_space) distributed_jointI:
hoelzl@49788
   488
  assumes "sigma_finite_measure S" "sigma_finite_measure T"
hoelzl@50003
   489
  assumes X[measurable]: "X \<in> measurable M S" and Y[measurable]: "Y \<in> measurable M T"
wenzelm@53015
   490
  assumes [measurable]: "f \<in> borel_measurable (S \<Otimes>\<^sub>M T)" and f: "AE x in S \<Otimes>\<^sub>M T. 0 \<le> f x"
hoelzl@49788
   491
  assumes eq: "\<And>A B. A \<in> sets S \<Longrightarrow> B \<in> sets T \<Longrightarrow> 
wenzelm@53015
   492
    emeasure M {x \<in> space M. X x \<in> A \<and> Y x \<in> B} = (\<integral>\<^sup>+x. (\<integral>\<^sup>+y. f (x, y) * indicator B y \<partial>T) * indicator A x \<partial>S)"
wenzelm@53015
   493
  shows "distributed M (S \<Otimes>\<^sub>M T) (\<lambda>x. (X x, Y x)) f"
hoelzl@49788
   494
  unfolding distributed_def
hoelzl@49788
   495
proof safe
hoelzl@49788
   496
  interpret S: sigma_finite_measure S by fact
hoelzl@49788
   497
  interpret T: sigma_finite_measure T by fact
hoelzl@49788
   498
  interpret ST: pair_sigma_finite S T by default
hoelzl@47694
   499
hoelzl@49788
   500
  from ST.sigma_finite_up_in_pair_measure_generator guess F :: "nat \<Rightarrow> ('b \<times> 'c) set" .. note F = this
hoelzl@49788
   501
  let ?E = "{a \<times> b |a b. a \<in> sets S \<and> b \<in> sets T}"
wenzelm@53015
   502
  let ?P = "S \<Otimes>\<^sub>M T"
hoelzl@49788
   503
  show "distr M ?P (\<lambda>x. (X x, Y x)) = density ?P f" (is "?L = ?R")
hoelzl@49788
   504
  proof (rule measure_eqI_generator_eq[OF Int_stable_pair_measure_generator[of S T]])
hoelzl@49788
   505
    show "?E \<subseteq> Pow (space ?P)"
immler@50244
   506
      using sets.space_closed[of S] sets.space_closed[of T] by (auto simp: space_pair_measure)
hoelzl@49788
   507
    show "sets ?L = sigma_sets (space ?P) ?E"
hoelzl@49788
   508
      by (simp add: sets_pair_measure space_pair_measure)
hoelzl@49788
   509
    then show "sets ?R = sigma_sets (space ?P) ?E"
hoelzl@49788
   510
      by simp
hoelzl@49788
   511
  next
hoelzl@49788
   512
    interpret L: prob_space ?L
hoelzl@49788
   513
      by (rule prob_space_distr) (auto intro!: measurable_Pair)
hoelzl@49788
   514
    show "range F \<subseteq> ?E" "(\<Union>i. F i) = space ?P" "\<And>i. emeasure ?L (F i) \<noteq> \<infinity>"
hoelzl@49788
   515
      using F by (auto simp: space_pair_measure)
hoelzl@49788
   516
  next
hoelzl@49788
   517
    fix E assume "E \<in> ?E"
hoelzl@50003
   518
    then obtain A B where E[simp]: "E = A \<times> B"
hoelzl@50003
   519
      and A[measurable]: "A \<in> sets S" and B[measurable]: "B \<in> sets T" by auto
hoelzl@49788
   520
    have "emeasure ?L E = emeasure M {x \<in> space M. X x \<in> A \<and> Y x \<in> B}"
hoelzl@49788
   521
      by (auto intro!: arg_cong[where f="emeasure M"] simp add: emeasure_distr measurable_Pair)
wenzelm@53015
   522
    also have "\<dots> = (\<integral>\<^sup>+x. (\<integral>\<^sup>+y. (f (x, y) * indicator B y) * indicator A x \<partial>T) \<partial>S)"
hoelzl@56996
   523
      using f by (auto simp add: eq nn_integral_multc intro!: nn_integral_cong)
hoelzl@49788
   524
    also have "\<dots> = emeasure ?R E"
hoelzl@56996
   525
      by (auto simp add: emeasure_density T.nn_integral_fst[symmetric]
hoelzl@56996
   526
               intro!: nn_integral_cong split: split_indicator)
hoelzl@49788
   527
    finally show "emeasure ?L E = emeasure ?R E" .
hoelzl@49788
   528
  qed
hoelzl@50003
   529
qed (auto simp: f)
hoelzl@49788
   530
hoelzl@49788
   531
lemma (in prob_space) distributed_swap:
hoelzl@49788
   532
  assumes "sigma_finite_measure S" "sigma_finite_measure T"
wenzelm@53015
   533
  assumes Pxy: "distributed M (S \<Otimes>\<^sub>M T) (\<lambda>x. (X x, Y x)) Pxy"
wenzelm@53015
   534
  shows "distributed M (T \<Otimes>\<^sub>M S) (\<lambda>x. (Y x, X x)) (\<lambda>(x, y). Pxy (y, x))"
hoelzl@49788
   535
proof -
hoelzl@49788
   536
  interpret S: sigma_finite_measure S by fact
hoelzl@49788
   537
  interpret T: sigma_finite_measure T by fact
hoelzl@49788
   538
  interpret ST: pair_sigma_finite S T by default
hoelzl@49788
   539
  interpret TS: pair_sigma_finite T S by default
hoelzl@49788
   540
hoelzl@50003
   541
  note Pxy[measurable]
hoelzl@49788
   542
  show ?thesis 
hoelzl@49788
   543
    apply (subst TS.distr_pair_swap)
hoelzl@49788
   544
    unfolding distributed_def
hoelzl@49788
   545
  proof safe
wenzelm@53015
   546
    let ?D = "distr (S \<Otimes>\<^sub>M T) (T \<Otimes>\<^sub>M S) (\<lambda>(x, y). (y, x))"
hoelzl@49788
   547
    show 1: "(\<lambda>(x, y). Pxy (y, x)) \<in> borel_measurable ?D"
hoelzl@50003
   548
      by auto
hoelzl@49788
   549
    with Pxy
wenzelm@53015
   550
    show "AE x in distr (S \<Otimes>\<^sub>M T) (T \<Otimes>\<^sub>M S) (\<lambda>(x, y). (y, x)). 0 \<le> (case x of (x, y) \<Rightarrow> Pxy (y, x))"
hoelzl@49788
   551
      by (subst AE_distr_iff)
hoelzl@49788
   552
         (auto dest!: distributed_AE
hoelzl@49788
   553
               simp: measurable_split_conv split_beta
hoelzl@51683
   554
               intro!: measurable_Pair)
wenzelm@53015
   555
    show 2: "random_variable (distr (S \<Otimes>\<^sub>M T) (T \<Otimes>\<^sub>M S) (\<lambda>(x, y). (y, x))) (\<lambda>x. (Y x, X x))"
hoelzl@50003
   556
      using Pxy by auto
wenzelm@53015
   557
    { fix A assume A: "A \<in> sets (T \<Otimes>\<^sub>M S)"
wenzelm@53015
   558
      let ?B = "(\<lambda>(x, y). (y, x)) -` A \<inter> space (S \<Otimes>\<^sub>M T)"
immler@50244
   559
      from sets.sets_into_space[OF A]
hoelzl@49788
   560
      have "emeasure M ((\<lambda>x. (Y x, X x)) -` A \<inter> space M) =
hoelzl@49788
   561
        emeasure M ((\<lambda>x. (X x, Y x)) -` ?B \<inter> space M)"
hoelzl@49788
   562
        by (auto intro!: arg_cong2[where f=emeasure] simp: space_pair_measure)
wenzelm@53015
   563
      also have "\<dots> = (\<integral>\<^sup>+ x. Pxy x * indicator ?B x \<partial>(S \<Otimes>\<^sub>M T))"
hoelzl@50003
   564
        using Pxy A by (intro distributed_emeasure) auto
hoelzl@49788
   565
      finally have "emeasure M ((\<lambda>x. (Y x, X x)) -` A \<inter> space M) =
wenzelm@53015
   566
        (\<integral>\<^sup>+ x. Pxy x * indicator A (snd x, fst x) \<partial>(S \<Otimes>\<^sub>M T))"
hoelzl@56996
   567
        by (auto intro!: nn_integral_cong split: split_indicator) }
hoelzl@49788
   568
    note * = this
hoelzl@49788
   569
    show "distr M ?D (\<lambda>x. (Y x, X x)) = density ?D (\<lambda>(x, y). Pxy (y, x))"
hoelzl@49788
   570
      apply (intro measure_eqI)
hoelzl@49788
   571
      apply (simp_all add: emeasure_distr[OF 2] emeasure_density[OF 1])
hoelzl@56996
   572
      apply (subst nn_integral_distr)
hoelzl@50003
   573
      apply (auto intro!: * simp: comp_def split_beta)
hoelzl@49788
   574
      done
hoelzl@49788
   575
  qed
hoelzl@36624
   576
qed
hoelzl@36624
   577
hoelzl@47694
   578
lemma (in prob_space) distr_marginal1:
hoelzl@47694
   579
  assumes "sigma_finite_measure S" "sigma_finite_measure T"
wenzelm@53015
   580
  assumes Pxy: "distributed M (S \<Otimes>\<^sub>M T) (\<lambda>x. (X x, Y x)) Pxy"
wenzelm@53015
   581
  defines "Px \<equiv> \<lambda>x. (\<integral>\<^sup>+z. Pxy (x, z) \<partial>T)"
hoelzl@47694
   582
  shows "distributed M S X Px"
hoelzl@47694
   583
  unfolding distributed_def
hoelzl@47694
   584
proof safe
hoelzl@47694
   585
  interpret S: sigma_finite_measure S by fact
hoelzl@47694
   586
  interpret T: sigma_finite_measure T by fact
hoelzl@47694
   587
  interpret ST: pair_sigma_finite S T by default
hoelzl@47694
   588
hoelzl@50003
   589
  note Pxy[measurable]
hoelzl@50003
   590
  show X: "X \<in> measurable M S" by simp
hoelzl@47694
   591
hoelzl@50003
   592
  show borel: "Px \<in> borel_measurable S"
hoelzl@56996
   593
    by (auto intro!: T.nn_integral_fst simp: Px_def)
hoelzl@39097
   594
wenzelm@53015
   595
  interpret Pxy: prob_space "distr M (S \<Otimes>\<^sub>M T) (\<lambda>x. (X x, Y x))"
hoelzl@50003
   596
    by (intro prob_space_distr) simp
wenzelm@53015
   597
  have "(\<integral>\<^sup>+ x. max 0 (- Pxy x) \<partial>(S \<Otimes>\<^sub>M T)) = (\<integral>\<^sup>+ x. 0 \<partial>(S \<Otimes>\<^sub>M T))"
hoelzl@47694
   598
    using Pxy
hoelzl@56996
   599
    by (intro nn_integral_cong_AE) (auto simp: max_def dest: distributed_AE)
hoelzl@49788
   600
hoelzl@47694
   601
  show "distr M S X = density S Px"
hoelzl@47694
   602
  proof (rule measure_eqI)
hoelzl@47694
   603
    fix A assume A: "A \<in> sets (distr M S X)"
hoelzl@50003
   604
    with X measurable_space[of Y M T]
wenzelm@53015
   605
    have "emeasure (distr M S X) A = emeasure (distr M (S \<Otimes>\<^sub>M T) (\<lambda>x. (X x, Y x))) (A \<times> space T)"
hoelzl@50003
   606
      by (auto simp add: emeasure_distr intro!: arg_cong[where f="emeasure M"])
wenzelm@53015
   607
    also have "\<dots> = emeasure (density (S \<Otimes>\<^sub>M T) Pxy) (A \<times> space T)"
hoelzl@47694
   608
      using Pxy by (simp add: distributed_def)
wenzelm@53015
   609
    also have "\<dots> = \<integral>\<^sup>+ x. \<integral>\<^sup>+ y. Pxy (x, y) * indicator (A \<times> space T) (x, y) \<partial>T \<partial>S"
hoelzl@47694
   610
      using A borel Pxy
hoelzl@56996
   611
      by (simp add: emeasure_density T.nn_integral_fst[symmetric])
wenzelm@53015
   612
    also have "\<dots> = \<integral>\<^sup>+ x. Px x * indicator A x \<partial>S"
hoelzl@56996
   613
      apply (rule nn_integral_cong_AE)
hoelzl@49788
   614
      using Pxy[THEN distributed_AE, THEN ST.AE_pair] AE_space
hoelzl@47694
   615
    proof eventually_elim
hoelzl@49788
   616
      fix x assume "x \<in> space S" "AE y in T. 0 \<le> Pxy (x, y)"
hoelzl@47694
   617
      moreover have eq: "\<And>y. y \<in> space T \<Longrightarrow> indicator (A \<times> space T) (x, y) = indicator A x"
hoelzl@47694
   618
        by (auto simp: indicator_def)
wenzelm@53015
   619
      ultimately have "(\<integral>\<^sup>+ y. Pxy (x, y) * indicator (A \<times> space T) (x, y) \<partial>T) = (\<integral>\<^sup>+ y. Pxy (x, y) \<partial>T) * indicator A x"
hoelzl@56996
   620
        by (simp add: eq nn_integral_multc cong: nn_integral_cong)
wenzelm@53015
   621
      also have "(\<integral>\<^sup>+ y. Pxy (x, y) \<partial>T) = Px x"
hoelzl@56996
   622
        by (simp add: Px_def ereal_real nn_integral_nonneg)
wenzelm@53015
   623
      finally show "(\<integral>\<^sup>+ y. Pxy (x, y) * indicator (A \<times> space T) (x, y) \<partial>T) = Px x * indicator A x" .
hoelzl@47694
   624
    qed
hoelzl@47694
   625
    finally show "emeasure (distr M S X) A = emeasure (density S Px) A"
hoelzl@47694
   626
      using A borel Pxy by (simp add: emeasure_density)
hoelzl@47694
   627
  qed simp
hoelzl@47694
   628
  
hoelzl@49788
   629
  show "AE x in S. 0 \<le> Px x"
hoelzl@56996
   630
    by (simp add: Px_def nn_integral_nonneg real_of_ereal_pos)
hoelzl@40859
   631
qed
hoelzl@40859
   632
hoelzl@49788
   633
lemma (in prob_space) distr_marginal2:
hoelzl@49788
   634
  assumes S: "sigma_finite_measure S" and T: "sigma_finite_measure T"
wenzelm@53015
   635
  assumes Pxy: "distributed M (S \<Otimes>\<^sub>M T) (\<lambda>x. (X x, Y x)) Pxy"
wenzelm@53015
   636
  shows "distributed M T Y (\<lambda>y. (\<integral>\<^sup>+x. Pxy (x, y) \<partial>S))"
hoelzl@49788
   637
  using distr_marginal1[OF T S distributed_swap[OF S T]] Pxy by simp
hoelzl@49788
   638
hoelzl@49788
   639
lemma (in prob_space) distributed_marginal_eq_joint1:
hoelzl@49788
   640
  assumes T: "sigma_finite_measure T"
hoelzl@49788
   641
  assumes S: "sigma_finite_measure S"
hoelzl@49788
   642
  assumes Px: "distributed M S X Px"
wenzelm@53015
   643
  assumes Pxy: "distributed M (S \<Otimes>\<^sub>M T) (\<lambda>x. (X x, Y x)) Pxy"
wenzelm@53015
   644
  shows "AE x in S. Px x = (\<integral>\<^sup>+y. Pxy (x, y) \<partial>T)"
hoelzl@49788
   645
  using Px distr_marginal1[OF S T Pxy] by (rule distributed_unique)
hoelzl@49788
   646
hoelzl@49788
   647
lemma (in prob_space) distributed_marginal_eq_joint2:
hoelzl@49788
   648
  assumes T: "sigma_finite_measure T"
hoelzl@49788
   649
  assumes S: "sigma_finite_measure S"
hoelzl@49788
   650
  assumes Py: "distributed M T Y Py"
wenzelm@53015
   651
  assumes Pxy: "distributed M (S \<Otimes>\<^sub>M T) (\<lambda>x. (X x, Y x)) Pxy"
wenzelm@53015
   652
  shows "AE y in T. Py y = (\<integral>\<^sup>+x. Pxy (x, y) \<partial>S)"
hoelzl@49788
   653
  using Py distr_marginal2[OF S T Pxy] by (rule distributed_unique)
hoelzl@49788
   654
hoelzl@49795
   655
lemma (in prob_space) distributed_joint_indep':
hoelzl@49795
   656
  assumes S: "sigma_finite_measure S" and T: "sigma_finite_measure T"
hoelzl@50003
   657
  assumes X[measurable]: "distributed M S X Px" and Y[measurable]: "distributed M T Y Py"
wenzelm@53015
   658
  assumes indep: "distr M S X \<Otimes>\<^sub>M distr M T Y = distr M (S \<Otimes>\<^sub>M T) (\<lambda>x. (X x, Y x))"
wenzelm@53015
   659
  shows "distributed M (S \<Otimes>\<^sub>M T) (\<lambda>x. (X x, Y x)) (\<lambda>(x, y). Px x * Py y)"
hoelzl@49795
   660
  unfolding distributed_def
hoelzl@49795
   661
proof safe
hoelzl@49795
   662
  interpret S: sigma_finite_measure S by fact
hoelzl@49795
   663
  interpret T: sigma_finite_measure T by fact
hoelzl@49795
   664
  interpret ST: pair_sigma_finite S T by default
hoelzl@49795
   665
hoelzl@49795
   666
  interpret X: prob_space "density S Px"
hoelzl@49795
   667
    unfolding distributed_distr_eq_density[OF X, symmetric]
hoelzl@50003
   668
    by (rule prob_space_distr) simp
hoelzl@49795
   669
  have sf_X: "sigma_finite_measure (density S Px)" ..
hoelzl@49795
   670
hoelzl@49795
   671
  interpret Y: prob_space "density T Py"
hoelzl@49795
   672
    unfolding distributed_distr_eq_density[OF Y, symmetric]
hoelzl@50003
   673
    by (rule prob_space_distr) simp
hoelzl@49795
   674
  have sf_Y: "sigma_finite_measure (density T Py)" ..
hoelzl@49795
   675
wenzelm@53015
   676
  show "distr M (S \<Otimes>\<^sub>M T) (\<lambda>x. (X x, Y x)) = density (S \<Otimes>\<^sub>M T) (\<lambda>(x, y). Px x * Py y)"
hoelzl@49795
   677
    unfolding indep[symmetric] distributed_distr_eq_density[OF X] distributed_distr_eq_density[OF Y]
hoelzl@49795
   678
    using distributed_borel_measurable[OF X] distributed_AE[OF X]
hoelzl@49795
   679
    using distributed_borel_measurable[OF Y] distributed_AE[OF Y]
hoelzl@50003
   680
    by (rule pair_measure_density[OF _ _ _ _ T sf_Y])
hoelzl@49795
   681
wenzelm@53015
   682
  show "random_variable (S \<Otimes>\<^sub>M T) (\<lambda>x. (X x, Y x))" by auto
hoelzl@49795
   683
wenzelm@53015
   684
  show Pxy: "(\<lambda>(x, y). Px x * Py y) \<in> borel_measurable (S \<Otimes>\<^sub>M T)" by auto
hoelzl@49795
   685
wenzelm@53015
   686
  show "AE x in S \<Otimes>\<^sub>M T. 0 \<le> (case x of (x, y) \<Rightarrow> Px x * Py y)"
hoelzl@51683
   687
    apply (intro ST.AE_pair_measure borel_measurable_le Pxy borel_measurable_const)
hoelzl@49795
   688
    using distributed_AE[OF X]
hoelzl@49795
   689
    apply eventually_elim
hoelzl@49795
   690
    using distributed_AE[OF Y]
hoelzl@49795
   691
    apply eventually_elim
hoelzl@49795
   692
    apply auto
hoelzl@49795
   693
    done
hoelzl@49795
   694
qed
hoelzl@49795
   695
hoelzl@47694
   696
definition
hoelzl@47694
   697
  "simple_distributed M X f \<longleftrightarrow> distributed M (count_space (X`space M)) X (\<lambda>x. ereal (f x)) \<and>
hoelzl@47694
   698
    finite (X`space M)"
hoelzl@42902
   699
hoelzl@47694
   700
lemma simple_distributed:
hoelzl@47694
   701
  "simple_distributed M X Px \<Longrightarrow> distributed M (count_space (X`space M)) X Px"
hoelzl@47694
   702
  unfolding simple_distributed_def by auto
hoelzl@42902
   703
hoelzl@47694
   704
lemma simple_distributed_finite[dest]: "simple_distributed M X P \<Longrightarrow> finite (X`space M)"
hoelzl@47694
   705
  by (simp add: simple_distributed_def)
hoelzl@42902
   706
hoelzl@47694
   707
lemma (in prob_space) distributed_simple_function_superset:
hoelzl@47694
   708
  assumes X: "simple_function M X" "\<And>x. x \<in> X ` space M \<Longrightarrow> P x = measure M (X -` {x} \<inter> space M)"
hoelzl@47694
   709
  assumes A: "X`space M \<subseteq> A" "finite A"
hoelzl@47694
   710
  defines "S \<equiv> count_space A" and "P' \<equiv> (\<lambda>x. if x \<in> X`space M then P x else 0)"
hoelzl@47694
   711
  shows "distributed M S X P'"
hoelzl@47694
   712
  unfolding distributed_def
hoelzl@47694
   713
proof safe
hoelzl@47694
   714
  show "(\<lambda>x. ereal (P' x)) \<in> borel_measurable S" unfolding S_def by simp
hoelzl@47694
   715
  show "AE x in S. 0 \<le> ereal (P' x)"
hoelzl@47694
   716
    using X by (auto simp: S_def P'_def simple_distributed_def intro!: measure_nonneg)
hoelzl@47694
   717
  show "distr M S X = density S P'"
hoelzl@47694
   718
  proof (rule measure_eqI_finite)
hoelzl@47694
   719
    show "sets (distr M S X) = Pow A" "sets (density S P') = Pow A"
hoelzl@47694
   720
      using A unfolding S_def by auto
hoelzl@47694
   721
    show "finite A" by fact
hoelzl@47694
   722
    fix a assume a: "a \<in> A"
hoelzl@47694
   723
    then have "a \<notin> X`space M \<Longrightarrow> X -` {a} \<inter> space M = {}" by auto
hoelzl@47694
   724
    with A a X have "emeasure (distr M S X) {a} = P' a"
hoelzl@47694
   725
      by (subst emeasure_distr)
hoelzl@50002
   726
         (auto simp add: S_def P'_def simple_functionD emeasure_eq_measure measurable_count_space_eq2
hoelzl@47694
   727
               intro!: arg_cong[where f=prob])
wenzelm@53015
   728
    also have "\<dots> = (\<integral>\<^sup>+x. ereal (P' a) * indicator {a} x \<partial>S)"
hoelzl@47694
   729
      using A X a
hoelzl@56996
   730
      by (subst nn_integral_cmult_indicator)
hoelzl@47694
   731
         (auto simp: S_def P'_def simple_distributed_def simple_functionD measure_nonneg)
wenzelm@53015
   732
    also have "\<dots> = (\<integral>\<^sup>+x. ereal (P' x) * indicator {a} x \<partial>S)"
hoelzl@56996
   733
      by (auto simp: indicator_def intro!: nn_integral_cong)
hoelzl@47694
   734
    also have "\<dots> = emeasure (density S P') {a}"
hoelzl@47694
   735
      using a A by (intro emeasure_density[symmetric]) (auto simp: S_def)
hoelzl@47694
   736
    finally show "emeasure (distr M S X) {a} = emeasure (density S P') {a}" .
hoelzl@47694
   737
  qed
hoelzl@47694
   738
  show "random_variable S X"
hoelzl@47694
   739
    using X(1) A by (auto simp: measurable_def simple_functionD S_def)
hoelzl@47694
   740
qed
hoelzl@42902
   741
hoelzl@47694
   742
lemma (in prob_space) simple_distributedI:
hoelzl@47694
   743
  assumes X: "simple_function M X" "\<And>x. x \<in> X ` space M \<Longrightarrow> P x = measure M (X -` {x} \<inter> space M)"
hoelzl@47694
   744
  shows "simple_distributed M X P"
hoelzl@47694
   745
  unfolding simple_distributed_def
hoelzl@47694
   746
proof
hoelzl@47694
   747
  have "distributed M (count_space (X ` space M)) X (\<lambda>x. ereal (if x \<in> X`space M then P x else 0))"
hoelzl@47694
   748
    (is "?A")
hoelzl@47694
   749
    using simple_functionD[OF X(1)] by (intro distributed_simple_function_superset[OF X]) auto
hoelzl@47694
   750
  also have "?A \<longleftrightarrow> distributed M (count_space (X ` space M)) X (\<lambda>x. ereal (P x))"
hoelzl@47694
   751
    by (rule distributed_cong_density) auto
hoelzl@47694
   752
  finally show "\<dots>" .
hoelzl@47694
   753
qed (rule simple_functionD[OF X(1)])
hoelzl@47694
   754
hoelzl@47694
   755
lemma simple_distributed_joint_finite:
hoelzl@47694
   756
  assumes X: "simple_distributed M (\<lambda>x. (X x, Y x)) Px"
hoelzl@47694
   757
  shows "finite (X ` space M)" "finite (Y ` space M)"
hoelzl@42902
   758
proof -
hoelzl@47694
   759
  have "finite ((\<lambda>x. (X x, Y x)) ` space M)"
hoelzl@47694
   760
    using X by (auto simp: simple_distributed_def simple_functionD)
hoelzl@47694
   761
  then have "finite (fst ` (\<lambda>x. (X x, Y x)) ` space M)" "finite (snd ` (\<lambda>x. (X x, Y x)) ` space M)"
hoelzl@47694
   762
    by auto
hoelzl@47694
   763
  then show fin: "finite (X ` space M)" "finite (Y ` space M)"
hoelzl@47694
   764
    by (auto simp: image_image)
hoelzl@47694
   765
qed
hoelzl@47694
   766
hoelzl@47694
   767
lemma simple_distributed_joint2_finite:
hoelzl@47694
   768
  assumes X: "simple_distributed M (\<lambda>x. (X x, Y x, Z x)) Px"
hoelzl@47694
   769
  shows "finite (X ` space M)" "finite (Y ` space M)" "finite (Z ` space M)"
hoelzl@47694
   770
proof -
hoelzl@47694
   771
  have "finite ((\<lambda>x. (X x, Y x, Z x)) ` space M)"
hoelzl@47694
   772
    using X by (auto simp: simple_distributed_def simple_functionD)
hoelzl@47694
   773
  then have "finite (fst ` (\<lambda>x. (X x, Y x, Z x)) ` space M)"
hoelzl@47694
   774
    "finite ((fst \<circ> snd) ` (\<lambda>x. (X x, Y x, Z x)) ` space M)"
hoelzl@47694
   775
    "finite ((snd \<circ> snd) ` (\<lambda>x. (X x, Y x, Z x)) ` space M)"
hoelzl@47694
   776
    by auto
hoelzl@47694
   777
  then show fin: "finite (X ` space M)" "finite (Y ` space M)" "finite (Z ` space M)"
hoelzl@47694
   778
    by (auto simp: image_image)
hoelzl@42902
   779
qed
hoelzl@42902
   780
hoelzl@47694
   781
lemma simple_distributed_simple_function:
hoelzl@47694
   782
  "simple_distributed M X Px \<Longrightarrow> simple_function M X"
hoelzl@47694
   783
  unfolding simple_distributed_def distributed_def
hoelzl@50002
   784
  by (auto simp: simple_function_def measurable_count_space_eq2)
hoelzl@47694
   785
hoelzl@47694
   786
lemma simple_distributed_measure:
hoelzl@47694
   787
  "simple_distributed M X P \<Longrightarrow> a \<in> X`space M \<Longrightarrow> P a = measure M (X -` {a} \<inter> space M)"
hoelzl@47694
   788
  using distributed_count_space[of M "X`space M" X P a, symmetric]
hoelzl@47694
   789
  by (auto simp: simple_distributed_def measure_def)
hoelzl@47694
   790
hoelzl@47694
   791
lemma simple_distributed_nonneg: "simple_distributed M X f \<Longrightarrow> x \<in> space M \<Longrightarrow> 0 \<le> f (X x)"
hoelzl@47694
   792
  by (auto simp: simple_distributed_measure measure_nonneg)
hoelzl@42860
   793
hoelzl@47694
   794
lemma (in prob_space) simple_distributed_joint:
hoelzl@47694
   795
  assumes X: "simple_distributed M (\<lambda>x. (X x, Y x)) Px"
wenzelm@53015
   796
  defines "S \<equiv> count_space (X`space M) \<Otimes>\<^sub>M count_space (Y`space M)"
hoelzl@47694
   797
  defines "P \<equiv> (\<lambda>x. if x \<in> (\<lambda>x. (X x, Y x))`space M then Px x else 0)"
hoelzl@47694
   798
  shows "distributed M S (\<lambda>x. (X x, Y x)) P"
hoelzl@47694
   799
proof -
hoelzl@47694
   800
  from simple_distributed_joint_finite[OF X, simp]
hoelzl@47694
   801
  have S_eq: "S = count_space (X`space M \<times> Y`space M)"
hoelzl@47694
   802
    by (simp add: S_def pair_measure_count_space)
hoelzl@47694
   803
  show ?thesis
hoelzl@47694
   804
    unfolding S_eq P_def
hoelzl@47694
   805
  proof (rule distributed_simple_function_superset)
hoelzl@47694
   806
    show "simple_function M (\<lambda>x. (X x, Y x))"
hoelzl@47694
   807
      using X by (rule simple_distributed_simple_function)
hoelzl@47694
   808
    fix x assume "x \<in> (\<lambda>x. (X x, Y x)) ` space M"
hoelzl@47694
   809
    from simple_distributed_measure[OF X this]
hoelzl@47694
   810
    show "Px x = prob ((\<lambda>x. (X x, Y x)) -` {x} \<inter> space M)" .
hoelzl@47694
   811
  qed auto
hoelzl@47694
   812
qed
hoelzl@42860
   813
hoelzl@47694
   814
lemma (in prob_space) simple_distributed_joint2:
hoelzl@47694
   815
  assumes X: "simple_distributed M (\<lambda>x. (X x, Y x, Z x)) Px"
wenzelm@53015
   816
  defines "S \<equiv> count_space (X`space M) \<Otimes>\<^sub>M count_space (Y`space M) \<Otimes>\<^sub>M count_space (Z`space M)"
hoelzl@47694
   817
  defines "P \<equiv> (\<lambda>x. if x \<in> (\<lambda>x. (X x, Y x, Z x))`space M then Px x else 0)"
hoelzl@47694
   818
  shows "distributed M S (\<lambda>x. (X x, Y x, Z x)) P"
hoelzl@47694
   819
proof -
hoelzl@47694
   820
  from simple_distributed_joint2_finite[OF X, simp]
hoelzl@47694
   821
  have S_eq: "S = count_space (X`space M \<times> Y`space M \<times> Z`space M)"
hoelzl@47694
   822
    by (simp add: S_def pair_measure_count_space)
hoelzl@47694
   823
  show ?thesis
hoelzl@47694
   824
    unfolding S_eq P_def
hoelzl@47694
   825
  proof (rule distributed_simple_function_superset)
hoelzl@47694
   826
    show "simple_function M (\<lambda>x. (X x, Y x, Z x))"
hoelzl@47694
   827
      using X by (rule simple_distributed_simple_function)
hoelzl@47694
   828
    fix x assume "x \<in> (\<lambda>x. (X x, Y x, Z x)) ` space M"
hoelzl@47694
   829
    from simple_distributed_measure[OF X this]
hoelzl@47694
   830
    show "Px x = prob ((\<lambda>x. (X x, Y x, Z x)) -` {x} \<inter> space M)" .
hoelzl@47694
   831
  qed auto
hoelzl@47694
   832
qed
hoelzl@47694
   833
hoelzl@47694
   834
lemma (in prob_space) simple_distributed_setsum_space:
hoelzl@47694
   835
  assumes X: "simple_distributed M X f"
hoelzl@47694
   836
  shows "setsum f (X`space M) = 1"
hoelzl@47694
   837
proof -
hoelzl@47694
   838
  from X have "setsum f (X`space M) = prob (\<Union>i\<in>X`space M. X -` {i} \<inter> space M)"
hoelzl@47694
   839
    by (subst finite_measure_finite_Union)
hoelzl@47694
   840
       (auto simp add: disjoint_family_on_def simple_distributed_measure simple_distributed_simple_function simple_functionD
hoelzl@47694
   841
             intro!: setsum_cong arg_cong[where f="prob"])
hoelzl@47694
   842
  also have "\<dots> = prob (space M)"
hoelzl@47694
   843
    by (auto intro!: arg_cong[where f=prob])
hoelzl@47694
   844
  finally show ?thesis
hoelzl@47694
   845
    using emeasure_space_1 by (simp add: emeasure_eq_measure one_ereal_def)
hoelzl@47694
   846
qed
hoelzl@42860
   847
hoelzl@47694
   848
lemma (in prob_space) distributed_marginal_eq_joint_simple:
hoelzl@47694
   849
  assumes Px: "simple_function M X"
hoelzl@47694
   850
  assumes Py: "simple_distributed M Y Py"
hoelzl@47694
   851
  assumes Pxy: "simple_distributed M (\<lambda>x. (X x, Y x)) Pxy"
hoelzl@47694
   852
  assumes y: "y \<in> Y`space M"
hoelzl@47694
   853
  shows "Py y = (\<Sum>x\<in>X`space M. if (x, y) \<in> (\<lambda>x. (X x, Y x)) ` space M then Pxy (x, y) else 0)"
hoelzl@47694
   854
proof -
hoelzl@47694
   855
  note Px = simple_distributedI[OF Px refl]
hoelzl@47694
   856
  have *: "\<And>f A. setsum (\<lambda>x. max 0 (ereal (f x))) A = ereal (setsum (\<lambda>x. max 0 (f x)) A)"
hoelzl@47694
   857
    by (simp add: setsum_ereal[symmetric] zero_ereal_def)
hoelzl@49788
   858
  from distributed_marginal_eq_joint2[OF
hoelzl@49788
   859
    sigma_finite_measure_count_space_finite
hoelzl@49788
   860
    sigma_finite_measure_count_space_finite
hoelzl@49788
   861
    simple_distributed[OF Py] simple_distributed_joint[OF Pxy],
hoelzl@47694
   862
    OF Py[THEN simple_distributed_finite] Px[THEN simple_distributed_finite]]
hoelzl@49788
   863
    y
hoelzl@49788
   864
    Px[THEN simple_distributed_finite]
hoelzl@49788
   865
    Py[THEN simple_distributed_finite]
hoelzl@47694
   866
    Pxy[THEN simple_distributed, THEN distributed_real_AE]
hoelzl@47694
   867
  show ?thesis
hoelzl@47694
   868
    unfolding AE_count_space
hoelzl@56996
   869
    apply (auto simp add: nn_integral_count_space_finite * intro!: setsum_cong split: split_max)
hoelzl@47694
   870
    done
hoelzl@47694
   871
qed
hoelzl@42860
   872
hoelzl@50419
   873
lemma distributedI_real:
hoelzl@50419
   874
  fixes f :: "'a \<Rightarrow> real"
hoelzl@50419
   875
  assumes gen: "sets M1 = sigma_sets (space M1) E" and "Int_stable E"
hoelzl@50419
   876
    and A: "range A \<subseteq> E" "(\<Union>i::nat. A i) = space M1" "\<And>i. emeasure (distr M M1 X) (A i) \<noteq> \<infinity>"
hoelzl@50419
   877
    and X: "X \<in> measurable M M1"
hoelzl@50419
   878
    and f: "f \<in> borel_measurable M1" "AE x in M1. 0 \<le> f x"
wenzelm@53015
   879
    and eq: "\<And>A. A \<in> E \<Longrightarrow> emeasure M (X -` A \<inter> space M) = (\<integral>\<^sup>+ x. f x * indicator A x \<partial>M1)"
hoelzl@50419
   880
  shows "distributed M M1 X f"
hoelzl@50419
   881
  unfolding distributed_def
hoelzl@50419
   882
proof (intro conjI)
hoelzl@50419
   883
  show "distr M M1 X = density M1 f"
hoelzl@50419
   884
  proof (rule measure_eqI_generator_eq[where A=A])
hoelzl@50419
   885
    { fix A assume A: "A \<in> E"
hoelzl@50419
   886
      then have "A \<in> sigma_sets (space M1) E" by auto
hoelzl@50419
   887
      then have "A \<in> sets M1"
hoelzl@50419
   888
        using gen by simp
hoelzl@50419
   889
      with f A eq[of A] X show "emeasure (distr M M1 X) A = emeasure (density M1 f) A"
hoelzl@50419
   890
        by (simp add: emeasure_distr emeasure_density borel_measurable_ereal
hoelzl@50419
   891
                      times_ereal.simps[symmetric] ereal_indicator
hoelzl@50419
   892
                 del: times_ereal.simps) }
hoelzl@50419
   893
    note eq_E = this
hoelzl@50419
   894
    show "Int_stable E" by fact
hoelzl@50419
   895
    { fix e assume "e \<in> E"
hoelzl@50419
   896
      then have "e \<in> sigma_sets (space M1) E" by auto
hoelzl@50419
   897
      then have "e \<in> sets M1" unfolding gen .
hoelzl@50419
   898
      then have "e \<subseteq> space M1" by (rule sets.sets_into_space) }
hoelzl@50419
   899
    then show "E \<subseteq> Pow (space M1)" by auto
hoelzl@50419
   900
    show "sets (distr M M1 X) = sigma_sets (space M1) E"
hoelzl@50419
   901
      "sets (density M1 (\<lambda>x. ereal (f x))) = sigma_sets (space M1) E"
hoelzl@50419
   902
      unfolding gen[symmetric] by auto
hoelzl@50419
   903
  qed fact+
hoelzl@50419
   904
qed (insert X f, auto)
hoelzl@50419
   905
hoelzl@50419
   906
lemma distributedI_borel_atMost:
hoelzl@50419
   907
  fixes f :: "real \<Rightarrow> real"
hoelzl@50419
   908
  assumes [measurable]: "X \<in> borel_measurable M"
hoelzl@50419
   909
    and [measurable]: "f \<in> borel_measurable borel" and f[simp]: "AE x in lborel. 0 \<le> f x"
wenzelm@53015
   910
    and g_eq: "\<And>a. (\<integral>\<^sup>+x. f x * indicator {..a} x \<partial>lborel)  = ereal (g a)"
hoelzl@50419
   911
    and M_eq: "\<And>a. emeasure M {x\<in>space M. X x \<le> a} = ereal (g a)"
hoelzl@50419
   912
  shows "distributed M lborel X f"
hoelzl@50419
   913
proof (rule distributedI_real)
hoelzl@50419
   914
  show "sets lborel = sigma_sets (space lborel) (range atMost)"
hoelzl@50419
   915
    by (simp add: borel_eq_atMost)
hoelzl@50419
   916
  show "Int_stable (range atMost :: real set set)"
hoelzl@50419
   917
    by (auto simp: Int_stable_def)
hoelzl@50419
   918
  have vimage_eq: "\<And>a. (X -` {..a} \<inter> space M) = {x\<in>space M. X x \<le> a}" by auto
hoelzl@50419
   919
  def A \<equiv> "\<lambda>i::nat. {.. real i}"
hoelzl@50419
   920
  then show "range A \<subseteq> range atMost" "(\<Union>i. A i) = space lborel"
hoelzl@50419
   921
    "\<And>i. emeasure (distr M lborel X) (A i) \<noteq> \<infinity>"
hoelzl@50419
   922
    by (auto simp: real_arch_simple emeasure_distr vimage_eq M_eq)
hoelzl@50419
   923
hoelzl@50419
   924
  fix A :: "real set" assume "A \<in> range atMost"
hoelzl@50419
   925
  then obtain a where A: "A = {..a}" by auto
wenzelm@53015
   926
  show "emeasure M (X -` A \<inter> space M) = (\<integral>\<^sup>+x. f x * indicator A x \<partial>lborel)"
hoelzl@50419
   927
    unfolding vimage_eq A M_eq g_eq ..
hoelzl@50419
   928
qed auto
hoelzl@50419
   929
hoelzl@50419
   930
lemma (in prob_space) uniform_distributed_params:
hoelzl@50419
   931
  assumes X: "distributed M MX X (\<lambda>x. indicator A x / measure MX A)"
hoelzl@50419
   932
  shows "A \<in> sets MX" "measure MX A \<noteq> 0"
hoelzl@50419
   933
proof -
hoelzl@50419
   934
  interpret X: prob_space "distr M MX X"
hoelzl@50419
   935
    using distributed_measurable[OF X] by (rule prob_space_distr)
hoelzl@50419
   936
hoelzl@50419
   937
  show "measure MX A \<noteq> 0"
hoelzl@50419
   938
  proof
hoelzl@50419
   939
    assume "measure MX A = 0"
hoelzl@50419
   940
    with X.emeasure_space_1 X.prob_space distributed_distr_eq_density[OF X]
hoelzl@50419
   941
    show False
hoelzl@50419
   942
      by (simp add: emeasure_density zero_ereal_def[symmetric])
hoelzl@50419
   943
  qed
hoelzl@50419
   944
  with measure_notin_sets[of A MX] show "A \<in> sets MX"
hoelzl@50419
   945
    by blast
hoelzl@50419
   946
qed
hoelzl@50419
   947
hoelzl@47694
   948
lemma prob_space_uniform_measure:
hoelzl@47694
   949
  assumes A: "emeasure M A \<noteq> 0" "emeasure M A \<noteq> \<infinity>"
hoelzl@47694
   950
  shows "prob_space (uniform_measure M A)"
hoelzl@47694
   951
proof
hoelzl@47694
   952
  show "emeasure (uniform_measure M A) (space (uniform_measure M A)) = 1"
hoelzl@47694
   953
    using emeasure_uniform_measure[OF emeasure_neq_0_sets[OF A(1)], of "space M"]
immler@50244
   954
    using sets.sets_into_space[OF emeasure_neq_0_sets[OF A(1)]] A
hoelzl@47694
   955
    by (simp add: Int_absorb2 emeasure_nonneg)
hoelzl@47694
   956
qed
hoelzl@47694
   957
hoelzl@47694
   958
lemma prob_space_uniform_count_measure: "finite A \<Longrightarrow> A \<noteq> {} \<Longrightarrow> prob_space (uniform_count_measure A)"
hoelzl@47694
   959
  by default (auto simp: emeasure_uniform_count_measure space_uniform_count_measure one_ereal_def)
hoelzl@42860
   960
hoelzl@35582
   961
end