src/HOL/Probability/Probability_Measure.thy
author hoelzl
Thu May 19 19:57:59 2011 +0200 (2011-05-19)
changeset 42859 d9dfc733f25c
parent 42858 348fa5df7d3f
child 42860 b02349e70d5a
permissions -rw-r--r--
add product of probability spaces with finite cardinality
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@42146
     9
imports Lebesgue_Integration Radon_Nikodym Finite_Product_Measure
hoelzl@35582
    10
begin
hoelzl@35582
    11
hoelzl@41981
    12
lemma real_of_extreal_inverse[simp]:
hoelzl@41981
    13
  fixes X :: extreal
hoelzl@40859
    14
  shows "real (inverse X) = 1 / real X"
hoelzl@40859
    15
  by (cases X) (auto simp: inverse_eq_divide)
hoelzl@40859
    16
hoelzl@41981
    17
lemma real_of_extreal_le_0[simp]: "real (X :: extreal) \<le> 0 \<longleftrightarrow> (X \<le> 0 \<or> X = \<infinity>)"
hoelzl@41981
    18
  by (cases X) auto
hoelzl@41981
    19
hoelzl@41981
    20
lemma abs_real_of_extreal[simp]: "\<bar>real (X :: extreal)\<bar> = real \<bar>X\<bar>"
hoelzl@40859
    21
  by (cases X) auto
hoelzl@40859
    22
hoelzl@41981
    23
lemma zero_less_real_of_extreal: "0 < real X \<longleftrightarrow> (0 < X \<and> X \<noteq> \<infinity>)"
hoelzl@40859
    24
  by (cases X) auto
hoelzl@40859
    25
hoelzl@41981
    26
lemma real_of_extreal_le_1: fixes X :: extreal shows "X \<le> 1 \<Longrightarrow> real X \<le> 1"
hoelzl@41981
    27
  by (cases X) (auto simp: one_extreal_def)
hoelzl@41981
    28
hoelzl@35582
    29
locale prob_space = measure_space +
hoelzl@41689
    30
  assumes measure_space_1: "measure M (space M) = 1"
hoelzl@38656
    31
hoelzl@38656
    32
sublocale prob_space < finite_measure
hoelzl@38656
    33
proof
hoelzl@41981
    34
  from measure_space_1 show "\<mu> (space M) \<noteq> \<infinity>" by simp
hoelzl@38656
    35
qed
hoelzl@38656
    36
hoelzl@40859
    37
abbreviation (in prob_space) "events \<equiv> sets M"
hoelzl@41981
    38
abbreviation (in prob_space) "prob \<equiv> \<mu>'"
hoelzl@40859
    39
abbreviation (in prob_space) "random_variable M' X \<equiv> sigma_algebra M' \<and> X \<in> measurable M M'"
hoelzl@41689
    40
abbreviation (in prob_space) "expectation \<equiv> integral\<^isup>L M"
hoelzl@35582
    41
hoelzl@40859
    42
definition (in prob_space)
hoelzl@35582
    43
  "indep A B \<longleftrightarrow> A \<in> events \<and> B \<in> events \<and> prob (A \<inter> B) = prob A * prob B"
hoelzl@35582
    44
hoelzl@40859
    45
definition (in prob_space)
hoelzl@35582
    46
  "indep_families F G \<longleftrightarrow> (\<forall> A \<in> F. \<forall> B \<in> G. indep A B)"
hoelzl@35582
    47
hoelzl@40859
    48
definition (in prob_space)
hoelzl@41981
    49
  "distribution X A = \<mu>' (X -` A \<inter> space M)"
hoelzl@35582
    50
hoelzl@40859
    51
abbreviation (in prob_space)
hoelzl@36624
    52
  "joint_distribution X Y \<equiv> distribution (\<lambda>x. (X x, Y x))"
hoelzl@35582
    53
hoelzl@41981
    54
declare (in finite_measure) positive_measure'[intro, simp]
hoelzl@41981
    55
hoelzl@39097
    56
lemma (in prob_space) distribution_cong:
hoelzl@39097
    57
  assumes "\<And>x. x \<in> space M \<Longrightarrow> X x = Y x"
hoelzl@39097
    58
  shows "distribution X = distribution Y"
nipkow@39302
    59
  unfolding distribution_def fun_eq_iff
hoelzl@41981
    60
  using assms by (auto intro!: arg_cong[where f="\<mu>'"])
hoelzl@39097
    61
hoelzl@39097
    62
lemma (in prob_space) joint_distribution_cong:
hoelzl@39097
    63
  assumes "\<And>x. x \<in> space M \<Longrightarrow> X x = X' x"
hoelzl@39097
    64
  assumes "\<And>x. x \<in> space M \<Longrightarrow> Y x = Y' x"
hoelzl@39097
    65
  shows "joint_distribution X Y = joint_distribution X' Y'"
nipkow@39302
    66
  unfolding distribution_def fun_eq_iff
hoelzl@41981
    67
  using assms by (auto intro!: arg_cong[where f="\<mu>'"])
hoelzl@39097
    68
hoelzl@40859
    69
lemma (in prob_space) distribution_id[simp]:
hoelzl@41981
    70
  "N \<in> events \<Longrightarrow> distribution (\<lambda>x. x) N = prob N"
hoelzl@41981
    71
  by (auto simp: distribution_def intro!: arg_cong[where f=prob])
hoelzl@40859
    72
hoelzl@40859
    73
lemma (in prob_space) prob_space: "prob (space M) = 1"
hoelzl@41981
    74
  using measure_space_1 unfolding \<mu>'_def by (simp add: one_extreal_def)
hoelzl@41981
    75
hoelzl@41981
    76
lemma (in prob_space) prob_le_1[simp, intro]: "prob A \<le> 1"
hoelzl@41981
    77
  using bounded_measure[of A] by (simp add: prob_space)
hoelzl@41981
    78
hoelzl@41981
    79
lemma (in prob_space) distribution_positive[simp, intro]:
hoelzl@41981
    80
  "0 \<le> distribution X A" unfolding distribution_def by auto
hoelzl@35582
    81
hoelzl@41981
    82
lemma (in prob_space) joint_distribution_remove[simp]:
hoelzl@41981
    83
    "joint_distribution X X {(x, x)} = distribution X {x}"
hoelzl@41981
    84
  unfolding distribution_def by (auto intro!: arg_cong[where f=\<mu>'])
hoelzl@41981
    85
hoelzl@41981
    86
lemma (in prob_space) distribution_1:
hoelzl@41981
    87
  "distribution X A \<le> 1"
hoelzl@41981
    88
  unfolding distribution_def by simp
hoelzl@35582
    89
hoelzl@40859
    90
lemma (in prob_space) prob_compl:
hoelzl@41981
    91
  assumes A: "A \<in> events"
hoelzl@38656
    92
  shows "prob (space M - A) = 1 - prob A"
hoelzl@41981
    93
  using finite_measure_compl[OF A] by (simp add: prob_space)
hoelzl@35582
    94
hoelzl@41981
    95
lemma (in prob_space) indep_space: "s \<in> events \<Longrightarrow> indep (space M) s"
hoelzl@41981
    96
  by (simp add: indep_def prob_space)
hoelzl@35582
    97
hoelzl@40859
    98
lemma (in prob_space) prob_space_increasing: "increasing M prob"
hoelzl@41981
    99
  by (auto intro!: finite_measure_mono simp: increasing_def)
hoelzl@35582
   100
hoelzl@40859
   101
lemma (in prob_space) prob_zero_union:
hoelzl@35582
   102
  assumes "s \<in> events" "t \<in> events" "prob t = 0"
hoelzl@35582
   103
  shows "prob (s \<union> t) = prob s"
hoelzl@38656
   104
using assms
hoelzl@35582
   105
proof -
hoelzl@35582
   106
  have "prob (s \<union> t) \<le> prob s"
hoelzl@41981
   107
    using finite_measure_subadditive[of s t] assms by auto
hoelzl@35582
   108
  moreover have "prob (s \<union> t) \<ge> prob s"
hoelzl@41981
   109
    using assms by (blast intro: finite_measure_mono)
hoelzl@35582
   110
  ultimately show ?thesis by simp
hoelzl@35582
   111
qed
hoelzl@35582
   112
hoelzl@40859
   113
lemma (in prob_space) prob_eq_compl:
hoelzl@35582
   114
  assumes "s \<in> events" "t \<in> events"
hoelzl@35582
   115
  assumes "prob (space M - s) = prob (space M - t)"
hoelzl@35582
   116
  shows "prob s = prob t"
hoelzl@38656
   117
  using assms prob_compl by auto
hoelzl@35582
   118
hoelzl@40859
   119
lemma (in prob_space) prob_one_inter:
hoelzl@35582
   120
  assumes events:"s \<in> events" "t \<in> events"
hoelzl@35582
   121
  assumes "prob t = 1"
hoelzl@35582
   122
  shows "prob (s \<inter> t) = prob s"
hoelzl@35582
   123
proof -
hoelzl@38656
   124
  have "prob ((space M - s) \<union> (space M - t)) = prob (space M - s)"
hoelzl@38656
   125
    using events assms  prob_compl[of "t"] by (auto intro!: prob_zero_union)
hoelzl@38656
   126
  also have "(space M - s) \<union> (space M - t) = space M - (s \<inter> t)"
hoelzl@38656
   127
    by blast
hoelzl@38656
   128
  finally show "prob (s \<inter> t) = prob s"
hoelzl@38656
   129
    using events by (auto intro!: prob_eq_compl[of "s \<inter> t" s])
hoelzl@35582
   130
qed
hoelzl@35582
   131
hoelzl@40859
   132
lemma (in prob_space) prob_eq_bigunion_image:
hoelzl@35582
   133
  assumes "range f \<subseteq> events" "range g \<subseteq> events"
hoelzl@35582
   134
  assumes "disjoint_family f" "disjoint_family g"
hoelzl@35582
   135
  assumes "\<And> n :: nat. prob (f n) = prob (g n)"
hoelzl@35582
   136
  shows "(prob (\<Union> i. f i) = prob (\<Union> i. g i))"
hoelzl@35582
   137
using assms
hoelzl@35582
   138
proof -
hoelzl@38656
   139
  have a: "(\<lambda> i. prob (f i)) sums (prob (\<Union> i. f i))"
hoelzl@41981
   140
    by (rule finite_measure_UNION[OF assms(1,3)])
hoelzl@38656
   141
  have b: "(\<lambda> i. prob (g i)) sums (prob (\<Union> i. g i))"
hoelzl@41981
   142
    by (rule finite_measure_UNION[OF assms(2,4)])
hoelzl@38656
   143
  show ?thesis using sums_unique[OF b] sums_unique[OF a] assms by simp
hoelzl@35582
   144
qed
hoelzl@35582
   145
hoelzl@40859
   146
lemma (in prob_space) prob_countably_zero:
hoelzl@35582
   147
  assumes "range c \<subseteq> events"
hoelzl@35582
   148
  assumes "\<And> i. prob (c i) = 0"
hoelzl@38656
   149
  shows "prob (\<Union> i :: nat. c i) = 0"
hoelzl@38656
   150
proof (rule antisym)
hoelzl@38656
   151
  show "prob (\<Union> i :: nat. c i) \<le> 0"
hoelzl@41981
   152
    using finite_measure_countably_subadditive[OF assms(1)]
hoelzl@38656
   153
    by (simp add: assms(2) suminf_zero summable_zero)
hoelzl@41981
   154
qed simp
hoelzl@35582
   155
hoelzl@40859
   156
lemma (in prob_space) indep_sym:
hoelzl@35582
   157
   "indep a b \<Longrightarrow> indep b a"
hoelzl@35582
   158
unfolding indep_def using Int_commute[of a b] by auto
hoelzl@35582
   159
hoelzl@40859
   160
lemma (in prob_space) indep_refl:
hoelzl@35582
   161
  assumes "a \<in> events"
hoelzl@35582
   162
  shows "indep a a = (prob a = 0) \<or> (prob a = 1)"
hoelzl@35582
   163
using assms unfolding indep_def by auto
hoelzl@35582
   164
hoelzl@40859
   165
lemma (in prob_space) prob_equiprobable_finite_unions:
hoelzl@38656
   166
  assumes "s \<in> events"
hoelzl@38656
   167
  assumes s_finite: "finite s" "\<And>x. x \<in> s \<Longrightarrow> {x} \<in> events"
hoelzl@35582
   168
  assumes "\<And> x y. \<lbrakk>x \<in> s; y \<in> s\<rbrakk> \<Longrightarrow> (prob {x} = prob {y})"
hoelzl@38656
   169
  shows "prob s = real (card s) * prob {SOME x. x \<in> s}"
hoelzl@35582
   170
proof (cases "s = {}")
hoelzl@38656
   171
  case False hence "\<exists> x. x \<in> s" by blast
hoelzl@35582
   172
  from someI_ex[OF this] assms
hoelzl@35582
   173
  have prob_some: "\<And> x. x \<in> s \<Longrightarrow> prob {x} = prob {SOME y. y \<in> s}" by blast
hoelzl@35582
   174
  have "prob s = (\<Sum> x \<in> s. prob {x})"
hoelzl@41981
   175
    using finite_measure_finite_singleton[OF s_finite] by simp
hoelzl@35582
   176
  also have "\<dots> = (\<Sum> x \<in> s. prob {SOME y. y \<in> s})" using prob_some by auto
hoelzl@38656
   177
  also have "\<dots> = real (card s) * prob {(SOME x. x \<in> s)}"
hoelzl@38656
   178
    using setsum_constant assms by (simp add: real_eq_of_nat)
hoelzl@35582
   179
  finally show ?thesis by simp
hoelzl@38656
   180
qed simp
hoelzl@35582
   181
hoelzl@40859
   182
lemma (in prob_space) prob_real_sum_image_fn:
hoelzl@35582
   183
  assumes "e \<in> events"
hoelzl@35582
   184
  assumes "\<And> x. x \<in> s \<Longrightarrow> e \<inter> f x \<in> events"
hoelzl@35582
   185
  assumes "finite s"
hoelzl@38656
   186
  assumes disjoint: "\<And> x y. \<lbrakk>x \<in> s ; y \<in> s ; x \<noteq> y\<rbrakk> \<Longrightarrow> f x \<inter> f y = {}"
hoelzl@38656
   187
  assumes upper: "space M \<subseteq> (\<Union> i \<in> s. f i)"
hoelzl@35582
   188
  shows "prob e = (\<Sum> x \<in> s. prob (e \<inter> f x))"
hoelzl@35582
   189
proof -
hoelzl@38656
   190
  have e: "e = (\<Union> i \<in> s. e \<inter> f i)"
hoelzl@38656
   191
    using `e \<in> events` sets_into_space upper by blast
hoelzl@38656
   192
  hence "prob e = prob (\<Union> i \<in> s. e \<inter> f i)" by simp
hoelzl@38656
   193
  also have "\<dots> = (\<Sum> x \<in> s. prob (e \<inter> f x))"
hoelzl@41981
   194
  proof (rule finite_measure_finite_Union)
hoelzl@38656
   195
    show "finite s" by fact
hoelzl@38656
   196
    show "\<And>i. i \<in> s \<Longrightarrow> e \<inter> f i \<in> events" by fact
hoelzl@38656
   197
    show "disjoint_family_on (\<lambda>i. e \<inter> f i) s"
hoelzl@38656
   198
      using disjoint by (auto simp: disjoint_family_on_def)
hoelzl@38656
   199
  qed
hoelzl@38656
   200
  finally show ?thesis .
hoelzl@35582
   201
qed
hoelzl@35582
   202
hoelzl@42199
   203
lemma (in prob_space) prob_space_vimage:
hoelzl@42199
   204
  assumes S: "sigma_algebra S"
hoelzl@42199
   205
  assumes T: "T \<in> measure_preserving M S"
hoelzl@42199
   206
  shows "prob_space S"
hoelzl@35582
   207
proof -
hoelzl@42199
   208
  interpret S: measure_space S
hoelzl@42199
   209
    using S and T by (rule measure_space_vimage)
hoelzl@38656
   210
  show ?thesis
hoelzl@42199
   211
  proof
hoelzl@42199
   212
    from T[THEN measure_preservingD2]
hoelzl@42199
   213
    have "T -` space S \<inter> space M = space M"
hoelzl@42199
   214
      by (auto simp: measurable_def)
hoelzl@42199
   215
    with T[THEN measure_preservingD, of "space S", symmetric]
hoelzl@42199
   216
    show  "measure S (space S) = 1"
hoelzl@42199
   217
      using measure_space_1 by simp
hoelzl@35582
   218
  qed
hoelzl@35582
   219
qed
hoelzl@35582
   220
hoelzl@42199
   221
lemma (in prob_space) distribution_prob_space:
hoelzl@42199
   222
  assumes X: "random_variable S X"
hoelzl@42199
   223
  shows "prob_space (S\<lparr>measure := extreal \<circ> distribution X\<rparr>)" (is "prob_space ?S")
hoelzl@42199
   224
proof (rule prob_space_vimage)
hoelzl@42199
   225
  show "X \<in> measure_preserving M ?S"
hoelzl@42199
   226
    using X
hoelzl@42199
   227
    unfolding measure_preserving_def distribution_def_raw
hoelzl@42199
   228
    by (auto simp: finite_measure_eq measurable_sets)
hoelzl@42199
   229
  show "sigma_algebra ?S" using X by simp
hoelzl@42199
   230
qed
hoelzl@42199
   231
hoelzl@40859
   232
lemma (in prob_space) AE_distribution:
hoelzl@41981
   233
  assumes X: "random_variable MX X" and "AE x in MX\<lparr>measure := extreal \<circ> distribution X\<rparr>. Q x"
hoelzl@40859
   234
  shows "AE x. Q (X x)"
hoelzl@40859
   235
proof -
hoelzl@41981
   236
  interpret X: prob_space "MX\<lparr>measure := extreal \<circ> distribution X\<rparr>" using X by (rule distribution_prob_space)
hoelzl@40859
   237
  obtain N where N: "N \<in> sets MX" "distribution X N = 0" "{x\<in>space MX. \<not> Q x} \<subseteq> N"
hoelzl@40859
   238
    using assms unfolding X.almost_everywhere_def by auto
hoelzl@41981
   239
  from X[unfolded measurable_def] N show "AE x. Q (X x)"
hoelzl@41981
   240
    by (intro AE_I'[where N="X -` N \<inter> space M"])
hoelzl@41981
   241
       (auto simp: finite_measure_eq distribution_def measurable_sets)
hoelzl@40859
   242
qed
hoelzl@40859
   243
hoelzl@41981
   244
lemma (in prob_space) distribution_eq_integral:
hoelzl@41981
   245
  "random_variable S X \<Longrightarrow> A \<in> sets S \<Longrightarrow> distribution X A = expectation (indicator (X -` A \<inter> space M))"
hoelzl@41981
   246
  using finite_measure_eq[of "X -` A \<inter> space M"]
hoelzl@41981
   247
  by (auto simp: measurable_sets distribution_def)
hoelzl@35582
   248
hoelzl@41981
   249
lemma (in prob_space) distribution_eq_translated_integral:
hoelzl@41981
   250
  assumes "random_variable S X" "A \<in> sets S"
hoelzl@41981
   251
  shows "distribution X A = integral\<^isup>P (S\<lparr>measure := extreal \<circ> distribution X\<rparr>) (indicator A)"
hoelzl@35582
   252
proof -
hoelzl@41981
   253
  interpret S: prob_space "S\<lparr>measure := extreal \<circ> distribution X\<rparr>"
hoelzl@41689
   254
    using assms(1) by (rule distribution_prob_space)
hoelzl@35582
   255
  show ?thesis
hoelzl@41981
   256
    using S.positive_integral_indicator(1)[of A] assms by simp
hoelzl@35582
   257
qed
hoelzl@35582
   258
hoelzl@40859
   259
lemma (in prob_space) finite_expectation1:
hoelzl@40859
   260
  assumes f: "finite (X`space M)" and rv: "random_variable borel X"
hoelzl@41981
   261
  shows "expectation X = (\<Sum>r \<in> X ` space M. r * prob (X -` {r} \<inter> space M))" (is "_ = ?r")
hoelzl@41981
   262
proof (subst integral_on_finite)
hoelzl@41981
   263
  show "X \<in> borel_measurable M" "finite (X`space M)" using assms by auto
hoelzl@41981
   264
  show "(\<Sum> r \<in> X ` space M. r * real (\<mu> (X -` {r} \<inter> space M))) = ?r"
hoelzl@41981
   265
    "\<And>x. \<mu> (X -` {x} \<inter> space M) \<noteq> \<infinity>"
hoelzl@41981
   266
    using finite_measure_eq[OF borel_measurable_vimage, of X] rv by auto
hoelzl@38656
   267
qed
hoelzl@35582
   268
hoelzl@40859
   269
lemma (in prob_space) finite_expectation:
hoelzl@41689
   270
  assumes "finite (X`space M)" "random_variable borel X"
hoelzl@41981
   271
  shows "expectation X = (\<Sum> r \<in> X ` (space M). r * distribution X {r})"
hoelzl@38656
   272
  using assms unfolding distribution_def using finite_expectation1 by auto
hoelzl@38656
   273
hoelzl@40859
   274
lemma (in prob_space) prob_x_eq_1_imp_prob_y_eq_0:
hoelzl@35582
   275
  assumes "{x} \<in> events"
hoelzl@38656
   276
  assumes "prob {x} = 1"
hoelzl@35582
   277
  assumes "{y} \<in> events"
hoelzl@35582
   278
  assumes "y \<noteq> x"
hoelzl@35582
   279
  shows "prob {y} = 0"
hoelzl@35582
   280
  using prob_one_inter[of "{y}" "{x}"] assms by auto
hoelzl@35582
   281
hoelzl@40859
   282
lemma (in prob_space) distribution_empty[simp]: "distribution X {} = 0"
hoelzl@38656
   283
  unfolding distribution_def by simp
hoelzl@38656
   284
hoelzl@40859
   285
lemma (in prob_space) distribution_space[simp]: "distribution X (X ` space M) = 1"
hoelzl@38656
   286
proof -
hoelzl@38656
   287
  have "X -` X ` space M \<inter> space M = space M" by auto
hoelzl@41981
   288
  thus ?thesis unfolding distribution_def by (simp add: prob_space)
hoelzl@38656
   289
qed
hoelzl@38656
   290
hoelzl@40859
   291
lemma (in prob_space) distribution_one:
hoelzl@40859
   292
  assumes "random_variable M' X" and "A \<in> sets M'"
hoelzl@38656
   293
  shows "distribution X A \<le> 1"
hoelzl@38656
   294
proof -
hoelzl@41981
   295
  have "distribution X A \<le> \<mu>' (space M)" unfolding distribution_def
hoelzl@41981
   296
    using assms[unfolded measurable_def] by (auto intro!: finite_measure_mono)
hoelzl@41981
   297
  thus ?thesis by (simp add: prob_space)
hoelzl@38656
   298
qed
hoelzl@38656
   299
hoelzl@40859
   300
lemma (in prob_space) distribution_x_eq_1_imp_distribution_y_eq_0:
hoelzl@35582
   301
  assumes X: "random_variable \<lparr>space = X ` (space M), sets = Pow (X ` (space M))\<rparr> X"
hoelzl@38656
   302
    (is "random_variable ?S X")
hoelzl@38656
   303
  assumes "distribution X {x} = 1"
hoelzl@35582
   304
  assumes "y \<noteq> x"
hoelzl@35582
   305
  shows "distribution X {y} = 0"
hoelzl@41689
   306
proof cases
hoelzl@41689
   307
  { fix x have "X -` {x} \<inter> space M \<in> sets M"
hoelzl@41689
   308
    proof cases
hoelzl@41689
   309
      assume "x \<in> X`space M" with X show ?thesis
hoelzl@41689
   310
        by (auto simp: measurable_def image_iff)
hoelzl@41689
   311
    next
hoelzl@41689
   312
      assume "x \<notin> X`space M" then have "X -` {x} \<inter> space M = {}" by auto
hoelzl@41689
   313
      then show ?thesis by auto
hoelzl@41689
   314
    qed } note single = this
hoelzl@41689
   315
  have "X -` {x} \<inter> space M - X -` {y} \<inter> space M = X -` {x} \<inter> space M"
hoelzl@41689
   316
    "X -` {y} \<inter> space M \<inter> (X -` {x} \<inter> space M) = {}"
hoelzl@41689
   317
    using `y \<noteq> x` by auto
hoelzl@41981
   318
  with finite_measure_inter_full_set[OF single single, of x y] assms(2)
hoelzl@41981
   319
  show ?thesis by (auto simp: distribution_def prob_space)
hoelzl@41689
   320
next
hoelzl@41689
   321
  assume "{y} \<notin> sets ?S"
hoelzl@41689
   322
  then have "X -` {y} \<inter> space M = {}" by auto
hoelzl@41689
   323
  thus "distribution X {y} = 0" unfolding distribution_def by auto
hoelzl@35582
   324
qed
hoelzl@35582
   325
hoelzl@40859
   326
lemma (in prob_space) joint_distribution_Times_le_fst:
hoelzl@40859
   327
  assumes X: "random_variable MX X" and Y: "random_variable MY Y"
hoelzl@40859
   328
    and A: "A \<in> sets MX" and B: "B \<in> sets MY"
hoelzl@40859
   329
  shows "joint_distribution X Y (A \<times> B) \<le> distribution X A"
hoelzl@40859
   330
  unfolding distribution_def
hoelzl@41981
   331
proof (intro finite_measure_mono)
hoelzl@40859
   332
  show "(\<lambda>x. (X x, Y x)) -` (A \<times> B) \<inter> space M \<subseteq> X -` A \<inter> space M" by force
hoelzl@40859
   333
  show "X -` A \<inter> space M \<in> events"
hoelzl@40859
   334
    using X A unfolding measurable_def by simp
hoelzl@40859
   335
  have *: "(\<lambda>x. (X x, Y x)) -` (A \<times> B) \<inter> space M =
hoelzl@40859
   336
    (X -` A \<inter> space M) \<inter> (Y -` B \<inter> space M)" by auto
hoelzl@40859
   337
qed
hoelzl@40859
   338
hoelzl@40859
   339
lemma (in prob_space) joint_distribution_commute:
hoelzl@40859
   340
  "joint_distribution X Y x = joint_distribution Y X ((\<lambda>(x,y). (y,x))`x)"
hoelzl@41981
   341
  unfolding distribution_def by (auto intro!: arg_cong[where f=\<mu>'])
hoelzl@40859
   342
hoelzl@40859
   343
lemma (in prob_space) joint_distribution_Times_le_snd:
hoelzl@40859
   344
  assumes X: "random_variable MX X" and Y: "random_variable MY Y"
hoelzl@40859
   345
    and A: "A \<in> sets MX" and B: "B \<in> sets MY"
hoelzl@40859
   346
  shows "joint_distribution X Y (A \<times> B) \<le> distribution Y B"
hoelzl@40859
   347
  using assms
hoelzl@40859
   348
  by (subst joint_distribution_commute)
hoelzl@40859
   349
     (simp add: swap_product joint_distribution_Times_le_fst)
hoelzl@40859
   350
hoelzl@40859
   351
lemma (in prob_space) random_variable_pairI:
hoelzl@40859
   352
  assumes "random_variable MX X"
hoelzl@40859
   353
  assumes "random_variable MY Y"
hoelzl@41689
   354
  shows "random_variable (MX \<Otimes>\<^isub>M MY) (\<lambda>x. (X x, Y x))"
hoelzl@40859
   355
proof
hoelzl@40859
   356
  interpret MX: sigma_algebra MX using assms by simp
hoelzl@40859
   357
  interpret MY: sigma_algebra MY using assms by simp
hoelzl@40859
   358
  interpret P: pair_sigma_algebra MX MY by default
hoelzl@41689
   359
  show "sigma_algebra (MX \<Otimes>\<^isub>M MY)" by default
hoelzl@40859
   360
  have sa: "sigma_algebra M" by default
hoelzl@41689
   361
  show "(\<lambda>x. (X x, Y x)) \<in> measurable M (MX \<Otimes>\<^isub>M MY)"
hoelzl@41095
   362
    unfolding P.measurable_pair_iff[OF sa] using assms by (simp add: comp_def)
hoelzl@40859
   363
qed
hoelzl@40859
   364
hoelzl@40859
   365
lemma (in prob_space) joint_distribution_commute_singleton:
hoelzl@40859
   366
  "joint_distribution X Y {(x, y)} = joint_distribution Y X {(y, x)}"
hoelzl@41981
   367
  unfolding distribution_def by (auto intro!: arg_cong[where f=\<mu>'])
hoelzl@40859
   368
hoelzl@40859
   369
lemma (in prob_space) joint_distribution_assoc_singleton:
hoelzl@40859
   370
  "joint_distribution X (\<lambda>x. (Y x, Z x)) {(x, y, z)} =
hoelzl@40859
   371
   joint_distribution (\<lambda>x. (X x, Y x)) Z {((x, y), z)}"
hoelzl@41981
   372
  unfolding distribution_def by (auto intro!: arg_cong[where f=\<mu>'])
hoelzl@40859
   373
hoelzl@41689
   374
locale pair_prob_space = M1: prob_space M1 + M2: prob_space M2 for M1 M2
hoelzl@40859
   375
hoelzl@41689
   376
sublocale pair_prob_space \<subseteq> pair_sigma_finite M1 M2 by default
hoelzl@41689
   377
hoelzl@41689
   378
sublocale pair_prob_space \<subseteq> P: prob_space P
hoelzl@41689
   379
by default (simp add: pair_measure_times M1.measure_space_1 M2.measure_space_1 space_pair_measure)
hoelzl@40859
   380
hoelzl@40859
   381
lemma countably_additiveI[case_names countably]:
hoelzl@40859
   382
  assumes "\<And>A. \<lbrakk> range A \<subseteq> sets M ; disjoint_family A ; (\<Union>i. A i) \<in> sets M\<rbrakk> \<Longrightarrow>
hoelzl@41981
   383
    (\<Sum>n. \<mu> (A n)) = \<mu> (\<Union>i. A i)"
hoelzl@40859
   384
  shows "countably_additive M \<mu>"
hoelzl@40859
   385
  using assms unfolding countably_additive_def by auto
hoelzl@40859
   386
hoelzl@40859
   387
lemma (in prob_space) joint_distribution_prob_space:
hoelzl@40859
   388
  assumes "random_variable MX X" "random_variable MY Y"
hoelzl@41981
   389
  shows "prob_space ((MX \<Otimes>\<^isub>M MY) \<lparr> measure := extreal \<circ> joint_distribution X Y\<rparr>)"
hoelzl@41689
   390
  using random_variable_pairI[OF assms] by (rule distribution_prob_space)
hoelzl@40859
   391
hoelzl@40859
   392
section "Probability spaces on finite sets"
hoelzl@35582
   393
hoelzl@35977
   394
locale finite_prob_space = prob_space + finite_measure_space
hoelzl@35977
   395
hoelzl@40859
   396
abbreviation (in prob_space) "finite_random_variable M' X \<equiv> finite_sigma_algebra M' \<and> X \<in> measurable M M'"
hoelzl@40859
   397
hoelzl@40859
   398
lemma (in prob_space) finite_random_variableD:
hoelzl@40859
   399
  assumes "finite_random_variable M' X" shows "random_variable M' X"
hoelzl@40859
   400
proof -
hoelzl@40859
   401
  interpret M': finite_sigma_algebra M' using assms by simp
hoelzl@40859
   402
  then show "random_variable M' X" using assms by simp default
hoelzl@40859
   403
qed
hoelzl@40859
   404
hoelzl@40859
   405
lemma (in prob_space) distribution_finite_prob_space:
hoelzl@40859
   406
  assumes "finite_random_variable MX X"
hoelzl@41981
   407
  shows "finite_prob_space (MX\<lparr>measure := extreal \<circ> distribution X\<rparr>)"
hoelzl@40859
   408
proof -
hoelzl@41981
   409
  interpret X: prob_space "MX\<lparr>measure := extreal \<circ> distribution X\<rparr>"
hoelzl@40859
   410
    using assms[THEN finite_random_variableD] by (rule distribution_prob_space)
hoelzl@40859
   411
  interpret MX: finite_sigma_algebra MX
hoelzl@41689
   412
    using assms by auto
hoelzl@41981
   413
  show ?thesis by default (simp_all add: MX.finite_space)
hoelzl@40859
   414
qed
hoelzl@40859
   415
hoelzl@40859
   416
lemma (in prob_space) simple_function_imp_finite_random_variable[simp, intro]:
hoelzl@41689
   417
  assumes "simple_function M X"
hoelzl@41689
   418
  shows "finite_random_variable \<lparr> space = X`space M, sets = Pow (X`space M), \<dots> = x \<rparr> X"
hoelzl@41689
   419
    (is "finite_random_variable ?X _")
hoelzl@40859
   420
proof (intro conjI)
hoelzl@40859
   421
  have [simp]: "finite (X ` space M)" using assms unfolding simple_function_def by simp
hoelzl@41689
   422
  interpret X: sigma_algebra ?X by (rule sigma_algebra_Pow)
hoelzl@41689
   423
  show "finite_sigma_algebra ?X"
hoelzl@40859
   424
    by default auto
hoelzl@41689
   425
  show "X \<in> measurable M ?X"
hoelzl@40859
   426
  proof (unfold measurable_def, clarsimp)
hoelzl@40859
   427
    fix A assume A: "A \<subseteq> X`space M"
hoelzl@40859
   428
    then have "finite A" by (rule finite_subset) simp
hoelzl@40859
   429
    then have "X -` (\<Union>a\<in>A. {a}) \<inter> space M \<in> events"
hoelzl@40859
   430
      unfolding vimage_UN UN_extend_simps
hoelzl@40859
   431
      apply (rule finite_UN)
hoelzl@40859
   432
      using A assms unfolding simple_function_def by auto
hoelzl@40859
   433
    then show "X -` A \<inter> space M \<in> events" by simp
hoelzl@40859
   434
  qed
hoelzl@40859
   435
qed
hoelzl@40859
   436
hoelzl@40859
   437
lemma (in prob_space) simple_function_imp_random_variable[simp, intro]:
hoelzl@41689
   438
  assumes "simple_function M X"
hoelzl@41689
   439
  shows "random_variable \<lparr> space = X`space M, sets = Pow (X`space M), \<dots> = ext \<rparr> X"
hoelzl@41689
   440
  using simple_function_imp_finite_random_variable[OF assms, of ext]
hoelzl@40859
   441
  by (auto dest!: finite_random_variableD)
hoelzl@40859
   442
hoelzl@40859
   443
lemma (in prob_space) sum_over_space_real_distribution:
hoelzl@41981
   444
  "simple_function M X \<Longrightarrow> (\<Sum>x\<in>X`space M. distribution X {x}) = 1"
hoelzl@40859
   445
  unfolding distribution_def prob_space[symmetric]
hoelzl@41981
   446
  by (subst finite_measure_finite_Union[symmetric])
hoelzl@40859
   447
     (auto simp add: disjoint_family_on_def simple_function_def
hoelzl@40859
   448
           intro!: arg_cong[where f=prob])
hoelzl@40859
   449
hoelzl@40859
   450
lemma (in prob_space) finite_random_variable_pairI:
hoelzl@40859
   451
  assumes "finite_random_variable MX X"
hoelzl@40859
   452
  assumes "finite_random_variable MY Y"
hoelzl@41689
   453
  shows "finite_random_variable (MX \<Otimes>\<^isub>M MY) (\<lambda>x. (X x, Y x))"
hoelzl@40859
   454
proof
hoelzl@40859
   455
  interpret MX: finite_sigma_algebra MX using assms by simp
hoelzl@40859
   456
  interpret MY: finite_sigma_algebra MY using assms by simp
hoelzl@40859
   457
  interpret P: pair_finite_sigma_algebra MX MY by default
hoelzl@41689
   458
  show "finite_sigma_algebra (MX \<Otimes>\<^isub>M MY)" by default
hoelzl@40859
   459
  have sa: "sigma_algebra M" by default
hoelzl@41689
   460
  show "(\<lambda>x. (X x, Y x)) \<in> measurable M (MX \<Otimes>\<^isub>M MY)"
hoelzl@41095
   461
    unfolding P.measurable_pair_iff[OF sa] using assms by (simp add: comp_def)
hoelzl@40859
   462
qed
hoelzl@40859
   463
hoelzl@40859
   464
lemma (in prob_space) finite_random_variable_imp_sets:
hoelzl@40859
   465
  "finite_random_variable MX X \<Longrightarrow> x \<in> space MX \<Longrightarrow> {x} \<in> sets MX"
hoelzl@40859
   466
  unfolding finite_sigma_algebra_def finite_sigma_algebra_axioms_def by simp
hoelzl@40859
   467
hoelzl@41981
   468
lemma (in prob_space) finite_random_variable_measurable:
hoelzl@40859
   469
  assumes X: "finite_random_variable MX X" shows "X -` A \<inter> space M \<in> events"
hoelzl@40859
   470
proof -
hoelzl@40859
   471
  interpret X: finite_sigma_algebra MX using X by simp
hoelzl@40859
   472
  from X have vimage: "\<And>A. A \<subseteq> space MX \<Longrightarrow> X -` A \<inter> space M \<in> events" and
hoelzl@40859
   473
    "X \<in> space M \<rightarrow> space MX"
hoelzl@40859
   474
    by (auto simp: measurable_def)
hoelzl@40859
   475
  then have *: "X -` A \<inter> space M = X -` (A \<inter> space MX) \<inter> space M"
hoelzl@40859
   476
    by auto
hoelzl@40859
   477
  show "X -` A \<inter> space M \<in> events"
hoelzl@40859
   478
    unfolding * by (intro vimage) auto
hoelzl@40859
   479
qed
hoelzl@40859
   480
hoelzl@40859
   481
lemma (in prob_space) joint_distribution_finite_Times_le_fst:
hoelzl@40859
   482
  assumes X: "finite_random_variable MX X" and Y: "finite_random_variable MY Y"
hoelzl@40859
   483
  shows "joint_distribution X Y (A \<times> B) \<le> distribution X A"
hoelzl@40859
   484
  unfolding distribution_def
hoelzl@41981
   485
proof (intro finite_measure_mono)
hoelzl@40859
   486
  show "(\<lambda>x. (X x, Y x)) -` (A \<times> B) \<inter> space M \<subseteq> X -` A \<inter> space M" by force
hoelzl@40859
   487
  show "X -` A \<inter> space M \<in> events"
hoelzl@41981
   488
    using finite_random_variable_measurable[OF X] .
hoelzl@40859
   489
  have *: "(\<lambda>x. (X x, Y x)) -` (A \<times> B) \<inter> space M =
hoelzl@40859
   490
    (X -` A \<inter> space M) \<inter> (Y -` B \<inter> space M)" by auto
hoelzl@40859
   491
qed
hoelzl@40859
   492
hoelzl@40859
   493
lemma (in prob_space) joint_distribution_finite_Times_le_snd:
hoelzl@40859
   494
  assumes X: "finite_random_variable MX X" and Y: "finite_random_variable MY Y"
hoelzl@40859
   495
  shows "joint_distribution X Y (A \<times> B) \<le> distribution Y B"
hoelzl@40859
   496
  using assms
hoelzl@40859
   497
  by (subst joint_distribution_commute)
hoelzl@40859
   498
     (simp add: swap_product joint_distribution_finite_Times_le_fst)
hoelzl@40859
   499
hoelzl@40859
   500
lemma (in prob_space) finite_distribution_order:
hoelzl@41981
   501
  fixes MX :: "('c, 'd) measure_space_scheme" and MY :: "('e, 'f) measure_space_scheme"
hoelzl@40859
   502
  assumes "finite_random_variable MX X" "finite_random_variable MY Y"
hoelzl@40859
   503
  shows "r \<le> joint_distribution X Y {(x, y)} \<Longrightarrow> r \<le> distribution X {x}"
hoelzl@40859
   504
    and "r \<le> joint_distribution X Y {(x, y)} \<Longrightarrow> r \<le> distribution Y {y}"
hoelzl@40859
   505
    and "r < joint_distribution X Y {(x, y)} \<Longrightarrow> r < distribution X {x}"
hoelzl@40859
   506
    and "r < joint_distribution X Y {(x, y)} \<Longrightarrow> r < distribution Y {y}"
hoelzl@40859
   507
    and "distribution X {x} = 0 \<Longrightarrow> joint_distribution X Y {(x, y)} = 0"
hoelzl@40859
   508
    and "distribution Y {y} = 0 \<Longrightarrow> joint_distribution X Y {(x, y)} = 0"
hoelzl@40859
   509
  using joint_distribution_finite_Times_le_snd[OF assms, of "{x}" "{y}"]
hoelzl@40859
   510
  using joint_distribution_finite_Times_le_fst[OF assms, of "{x}" "{y}"]
hoelzl@41981
   511
  by (auto intro: antisym)
hoelzl@40859
   512
hoelzl@40859
   513
lemma (in prob_space) setsum_joint_distribution:
hoelzl@40859
   514
  assumes X: "finite_random_variable MX X"
hoelzl@40859
   515
  assumes Y: "random_variable MY Y" "B \<in> sets MY"
hoelzl@40859
   516
  shows "(\<Sum>a\<in>space MX. joint_distribution X Y ({a} \<times> B)) = distribution Y B"
hoelzl@40859
   517
  unfolding distribution_def
hoelzl@41981
   518
proof (subst finite_measure_finite_Union[symmetric])
hoelzl@40859
   519
  interpret MX: finite_sigma_algebra MX using X by auto
hoelzl@40859
   520
  show "finite (space MX)" using MX.finite_space .
hoelzl@40859
   521
  let "?d i" = "(\<lambda>x. (X x, Y x)) -` ({i} \<times> B) \<inter> space M"
hoelzl@40859
   522
  { fix i assume "i \<in> space MX"
hoelzl@40859
   523
    moreover have "?d i = (X -` {i} \<inter> space M) \<inter> (Y -` B \<inter> space M)" by auto
hoelzl@40859
   524
    ultimately show "?d i \<in> events"
hoelzl@40859
   525
      using measurable_sets[of X M MX] measurable_sets[of Y M MY B] X Y
hoelzl@40859
   526
      using MX.sets_eq_Pow by auto }
hoelzl@40859
   527
  show "disjoint_family_on ?d (space MX)" by (auto simp: disjoint_family_on_def)
hoelzl@41981
   528
  show "\<mu>' (\<Union>i\<in>space MX. ?d i) = \<mu>' (Y -` B \<inter> space M)"
hoelzl@41981
   529
    using X[unfolded measurable_def] by (auto intro!: arg_cong[where f=\<mu>'])
hoelzl@40859
   530
qed
hoelzl@40859
   531
hoelzl@40859
   532
lemma (in prob_space) setsum_joint_distribution_singleton:
hoelzl@40859
   533
  assumes X: "finite_random_variable MX X"
hoelzl@40859
   534
  assumes Y: "finite_random_variable MY Y" "b \<in> space MY"
hoelzl@40859
   535
  shows "(\<Sum>a\<in>space MX. joint_distribution X Y {(a, b)}) = distribution Y {b}"
hoelzl@40859
   536
  using setsum_joint_distribution[OF X
hoelzl@40859
   537
    finite_random_variableD[OF Y(1)]
hoelzl@40859
   538
    finite_random_variable_imp_sets[OF Y]] by simp
hoelzl@40859
   539
hoelzl@41689
   540
locale pair_finite_prob_space = M1: finite_prob_space M1 + M2: finite_prob_space M2 for M1 M2
hoelzl@40859
   541
hoelzl@41689
   542
sublocale pair_finite_prob_space \<subseteq> pair_prob_space M1 M2 by default
hoelzl@41689
   543
sublocale pair_finite_prob_space \<subseteq> pair_finite_space M1 M2  by default
hoelzl@41689
   544
sublocale pair_finite_prob_space \<subseteq> finite_prob_space P by default
hoelzl@40859
   545
hoelzl@42859
   546
locale product_finite_prob_space =
hoelzl@42859
   547
  fixes M :: "'i \<Rightarrow> ('a,'b) measure_space_scheme"
hoelzl@42859
   548
    and I :: "'i set"
hoelzl@42859
   549
  assumes finite_space: "\<And>i. finite_prob_space (M i)" and finite_index: "finite I"
hoelzl@42859
   550
hoelzl@42859
   551
sublocale product_finite_prob_space \<subseteq> M!: finite_prob_space "M i" using finite_space .
hoelzl@42859
   552
sublocale product_finite_prob_space \<subseteq> finite_product_sigma_finite M I by default (rule finite_index)
hoelzl@42859
   553
sublocale product_finite_prob_space \<subseteq> prob_space "Pi\<^isub>M I M"
hoelzl@42859
   554
proof
hoelzl@42859
   555
  show "\<mu> (space P) = 1"
hoelzl@42859
   556
    using measure_times[OF M.top] M.measure_space_1
hoelzl@42859
   557
    by (simp add: setprod_1 space_product_algebra)
hoelzl@42859
   558
qed
hoelzl@42859
   559
hoelzl@42859
   560
lemma funset_eq_UN_fun_upd_I:
hoelzl@42859
   561
  assumes *: "\<And>f. f \<in> F (insert a A) \<Longrightarrow> f(a := d) \<in> F A"
hoelzl@42859
   562
  and **: "\<And>f. f \<in> F (insert a A) \<Longrightarrow> f a \<in> G (f(a:=d))"
hoelzl@42859
   563
  and ***: "\<And>f x. \<lbrakk> f \<in> F A ; x \<in> G f \<rbrakk> \<Longrightarrow> f(a:=x) \<in> F (insert a A)"
hoelzl@42859
   564
  shows "F (insert a A) = (\<Union>f\<in>F A. fun_upd f a ` (G f))"
hoelzl@42859
   565
proof safe
hoelzl@42859
   566
  fix f assume f: "f \<in> F (insert a A)"
hoelzl@42859
   567
  show "f \<in> (\<Union>f\<in>F A. fun_upd f a ` G f)"
hoelzl@42859
   568
  proof (rule UN_I[of "f(a := d)"])
hoelzl@42859
   569
    show "f(a := d) \<in> F A" using *[OF f] .
hoelzl@42859
   570
    show "f \<in> fun_upd (f(a:=d)) a ` G (f(a:=d))"
hoelzl@42859
   571
    proof (rule image_eqI[of _ _ "f a"])
hoelzl@42859
   572
      show "f a \<in> G (f(a := d))" using **[OF f] .
hoelzl@42859
   573
    qed simp
hoelzl@42859
   574
  qed
hoelzl@42859
   575
next
hoelzl@42859
   576
  fix f x assume "f \<in> F A" "x \<in> G f"
hoelzl@42859
   577
  from ***[OF this] show "f(a := x) \<in> F (insert a A)" .
hoelzl@42859
   578
qed
hoelzl@42859
   579
hoelzl@42859
   580
lemma extensional_funcset_insert_eq[simp]:
hoelzl@42859
   581
  assumes "a \<notin> A"
hoelzl@42859
   582
  shows "extensional (insert a A) \<inter> (insert a A \<rightarrow> B) = (\<Union>f \<in> extensional A \<inter> (A \<rightarrow> B). (\<lambda>b. f(a := b)) ` B)"
hoelzl@42859
   583
  apply (rule funset_eq_UN_fun_upd_I)
hoelzl@42859
   584
  using assms
hoelzl@42859
   585
  by (auto intro!: inj_onI dest: inj_onD split: split_if_asm simp: extensional_def)
hoelzl@42859
   586
hoelzl@42859
   587
lemma finite_extensional_funcset[simp, intro]:
hoelzl@42859
   588
  assumes "finite A" "finite B"
hoelzl@42859
   589
  shows "finite (extensional A \<inter> (A \<rightarrow> B))"
hoelzl@42859
   590
  using assms by induct (auto simp: extensional_funcset_insert_eq)
hoelzl@42859
   591
hoelzl@42859
   592
lemma finite_PiE[simp, intro]:
hoelzl@42859
   593
  assumes fin: "finite A" "\<And>i. i \<in> A \<Longrightarrow> finite (B i)"
hoelzl@42859
   594
  shows "finite (Pi\<^isub>E A B)"
hoelzl@42859
   595
proof -
hoelzl@42859
   596
  have *: "(Pi\<^isub>E A B) \<subseteq> extensional A \<inter> (A \<rightarrow> (\<Union>i\<in>A. B i))" by auto
hoelzl@42859
   597
  show ?thesis
hoelzl@42859
   598
    using fin by (intro finite_subset[OF *] finite_extensional_funcset) auto
hoelzl@42859
   599
qed
hoelzl@42859
   600
hoelzl@42859
   601
sublocale product_finite_prob_space \<subseteq> finite_prob_space "Pi\<^isub>M I M"
hoelzl@42859
   602
proof
hoelzl@42859
   603
  show "finite (space P)"
hoelzl@42859
   604
    using finite_index M.finite_space by auto
hoelzl@42859
   605
hoelzl@42859
   606
  { fix x assume "x \<in> space P"
hoelzl@42859
   607
    then have x: "{x} = (\<Pi>\<^isub>E i\<in>I. {x i})"
hoelzl@42859
   608
    proof safe
hoelzl@42859
   609
      fix y assume *: "y \<in> (\<Pi> i\<in>I. {x i})" "y \<in> extensional I"
hoelzl@42859
   610
      show "y = x"
hoelzl@42859
   611
      proof
hoelzl@42859
   612
        fix i show "y i = x i"
hoelzl@42859
   613
          using * `x \<in> space P` by (cases "i \<in> I") (auto simp: extensional_def)
hoelzl@42859
   614
      qed
hoelzl@42859
   615
    qed auto
hoelzl@42859
   616
    with `x \<in> space P` have "{x} \<in> sets P"
hoelzl@42859
   617
      by (auto intro!: in_P) }
hoelzl@42859
   618
  note x_in_P = this
hoelzl@42859
   619
hoelzl@42859
   620
  have "Pow (space P) \<subseteq> sets P"
hoelzl@42859
   621
  proof
hoelzl@42859
   622
    fix X assume "X \<in> Pow (space P)"
hoelzl@42859
   623
    moreover then have "finite X"
hoelzl@42859
   624
      using `finite (space P)` by (blast intro: finite_subset)
hoelzl@42859
   625
    ultimately have "(\<Union>x\<in>X. {x}) \<in> sets P"
hoelzl@42859
   626
      by (intro finite_UN x_in_P) auto
hoelzl@42859
   627
    then show "X \<in> sets P" by simp
hoelzl@42859
   628
  qed
hoelzl@42859
   629
  with space_closed show [simp]: "sets P = Pow (space P)" ..
hoelzl@42859
   630
hoelzl@42859
   631
hoelzl@42859
   632
  { fix x assume "x \<in> space P"
hoelzl@42859
   633
    from this top have "\<mu> {x} \<le> \<mu> (space P)" by (intro measure_mono) auto
hoelzl@42859
   634
    then show "\<mu> {x} \<noteq> \<infinity>"
hoelzl@42859
   635
      using measure_space_1 by auto }
hoelzl@42859
   636
qed
hoelzl@42859
   637
hoelzl@42859
   638
lemma (in product_finite_prob_space) measure_finite_times:
hoelzl@42859
   639
  "(\<And>i. i \<in> I \<Longrightarrow> X i \<subseteq> space (M i)) \<Longrightarrow> \<mu> (\<Pi>\<^isub>E i\<in>I. X i) = (\<Prod>i\<in>I. M.\<mu> i (X i))"
hoelzl@42859
   640
  by (rule measure_times) simp
hoelzl@42859
   641
hoelzl@42859
   642
lemma (in product_finite_prob_space) prob_times:
hoelzl@42859
   643
  assumes X: "\<And>i. i \<in> I \<Longrightarrow> X i \<subseteq> space (M i)"
hoelzl@42859
   644
  shows "prob (\<Pi>\<^isub>E i\<in>I. X i) = (\<Prod>i\<in>I. M.prob i (X i))"
hoelzl@42859
   645
proof -
hoelzl@42859
   646
  have "extreal (\<mu>' (\<Pi>\<^isub>E i\<in>I. X i)) = \<mu> (\<Pi>\<^isub>E i\<in>I. X i)"
hoelzl@42859
   647
    using X by (intro finite_measure_eq[symmetric] in_P) auto
hoelzl@42859
   648
  also have "\<dots> = (\<Prod>i\<in>I. M.\<mu> i (X i))"
hoelzl@42859
   649
    using measure_finite_times X by simp
hoelzl@42859
   650
  also have "\<dots> = extreal (\<Prod>i\<in>I. M.\<mu>' i (X i))"
hoelzl@42859
   651
    using X by (simp add: M.finite_measure_eq setprod_extreal)
hoelzl@42859
   652
  finally show ?thesis by simp
hoelzl@42859
   653
qed
hoelzl@42859
   654
hoelzl@40859
   655
lemma (in prob_space) joint_distribution_finite_prob_space:
hoelzl@40859
   656
  assumes X: "finite_random_variable MX X"
hoelzl@40859
   657
  assumes Y: "finite_random_variable MY Y"
hoelzl@41981
   658
  shows "finite_prob_space ((MX \<Otimes>\<^isub>M MY)\<lparr> measure := extreal \<circ> joint_distribution X Y\<rparr>)"
hoelzl@41689
   659
  by (intro distribution_finite_prob_space finite_random_variable_pairI X Y)
hoelzl@40859
   660
hoelzl@36624
   661
lemma finite_prob_space_eq:
hoelzl@41689
   662
  "finite_prob_space M \<longleftrightarrow> finite_measure_space M \<and> measure M (space M) = 1"
hoelzl@36624
   663
  unfolding finite_prob_space_def finite_measure_space_def prob_space_def prob_space_axioms_def
hoelzl@36624
   664
  by auto
hoelzl@36624
   665
hoelzl@36624
   666
lemma (in prob_space) not_empty: "space M \<noteq> {}"
hoelzl@41981
   667
  using prob_space empty_measure' by auto
hoelzl@36624
   668
hoelzl@38656
   669
lemma (in finite_prob_space) sum_over_space_eq_1: "(\<Sum>x\<in>space M. \<mu> {x}) = 1"
hoelzl@38656
   670
  using measure_space_1 sum_over_space by simp
hoelzl@36624
   671
hoelzl@36624
   672
lemma (in finite_prob_space) joint_distribution_restriction_fst:
hoelzl@36624
   673
  "joint_distribution X Y A \<le> distribution X (fst ` A)"
hoelzl@36624
   674
  unfolding distribution_def
hoelzl@41981
   675
proof (safe intro!: finite_measure_mono)
hoelzl@36624
   676
  fix x assume "x \<in> space M" and *: "(X x, Y x) \<in> A"
hoelzl@36624
   677
  show "x \<in> X -` fst ` A"
hoelzl@36624
   678
    by (auto intro!: image_eqI[OF _ *])
hoelzl@36624
   679
qed (simp_all add: sets_eq_Pow)
hoelzl@36624
   680
hoelzl@36624
   681
lemma (in finite_prob_space) joint_distribution_restriction_snd:
hoelzl@36624
   682
  "joint_distribution X Y A \<le> distribution Y (snd ` A)"
hoelzl@36624
   683
  unfolding distribution_def
hoelzl@41981
   684
proof (safe intro!: finite_measure_mono)
hoelzl@36624
   685
  fix x assume "x \<in> space M" and *: "(X x, Y x) \<in> A"
hoelzl@36624
   686
  show "x \<in> Y -` snd ` A"
hoelzl@36624
   687
    by (auto intro!: image_eqI[OF _ *])
hoelzl@36624
   688
qed (simp_all add: sets_eq_Pow)
hoelzl@36624
   689
hoelzl@36624
   690
lemma (in finite_prob_space) distribution_order:
hoelzl@36624
   691
  shows "0 \<le> distribution X x'"
hoelzl@36624
   692
  and "(distribution X x' \<noteq> 0) \<longleftrightarrow> (0 < distribution X x')"
hoelzl@36624
   693
  and "r \<le> joint_distribution X Y {(x, y)} \<Longrightarrow> r \<le> distribution X {x}"
hoelzl@36624
   694
  and "r \<le> joint_distribution X Y {(x, y)} \<Longrightarrow> r \<le> distribution Y {y}"
hoelzl@36624
   695
  and "r < joint_distribution X Y {(x, y)} \<Longrightarrow> r < distribution X {x}"
hoelzl@36624
   696
  and "r < joint_distribution X Y {(x, y)} \<Longrightarrow> r < distribution Y {y}"
hoelzl@36624
   697
  and "distribution X {x} = 0 \<Longrightarrow> joint_distribution X Y {(x, y)} = 0"
hoelzl@36624
   698
  and "distribution Y {y} = 0 \<Longrightarrow> joint_distribution X Y {(x, y)} = 0"
hoelzl@41981
   699
  using
hoelzl@36624
   700
    joint_distribution_restriction_fst[of X Y "{(x, y)}"]
hoelzl@36624
   701
    joint_distribution_restriction_snd[of X Y "{(x, y)}"]
hoelzl@41981
   702
  by (auto intro: antisym)
hoelzl@36624
   703
hoelzl@39097
   704
lemma (in finite_prob_space) distribution_mono:
hoelzl@39097
   705
  assumes "\<And>t. \<lbrakk> t \<in> space M ; X t \<in> x \<rbrakk> \<Longrightarrow> Y t \<in> y"
hoelzl@39097
   706
  shows "distribution X x \<le> distribution Y y"
hoelzl@39097
   707
  unfolding distribution_def
hoelzl@41981
   708
  using assms by (auto simp: sets_eq_Pow intro!: finite_measure_mono)
hoelzl@39097
   709
hoelzl@39097
   710
lemma (in finite_prob_space) distribution_mono_gt_0:
hoelzl@39097
   711
  assumes gt_0: "0 < distribution X x"
hoelzl@39097
   712
  assumes *: "\<And>t. \<lbrakk> t \<in> space M ; X t \<in> x \<rbrakk> \<Longrightarrow> Y t \<in> y"
hoelzl@39097
   713
  shows "0 < distribution Y y"
hoelzl@39097
   714
  by (rule less_le_trans[OF gt_0 distribution_mono]) (rule *)
hoelzl@39097
   715
hoelzl@39097
   716
lemma (in finite_prob_space) sum_over_space_distrib:
hoelzl@39097
   717
  "(\<Sum>x\<in>X`space M. distribution X {x}) = 1"
hoelzl@41981
   718
  unfolding distribution_def prob_space[symmetric] using finite_space
hoelzl@41981
   719
  by (subst finite_measure_finite_Union[symmetric])
hoelzl@41981
   720
     (auto simp add: disjoint_family_on_def sets_eq_Pow
hoelzl@41981
   721
           intro!: arg_cong[where f=\<mu>'])
hoelzl@39097
   722
hoelzl@39097
   723
lemma (in finite_prob_space) finite_sum_over_space_eq_1:
hoelzl@41981
   724
  "(\<Sum>x\<in>space M. prob {x}) = 1"
hoelzl@41981
   725
  using prob_space finite_space
hoelzl@41981
   726
  by (subst (asm) finite_measure_finite_singleton) auto
hoelzl@39097
   727
hoelzl@39097
   728
lemma (in prob_space) distribution_remove_const:
hoelzl@39097
   729
  shows "joint_distribution X (\<lambda>x. ()) {(x, ())} = distribution X {x}"
hoelzl@39097
   730
  and "joint_distribution (\<lambda>x. ()) X {((), x)} = distribution X {x}"
hoelzl@39097
   731
  and "joint_distribution X (\<lambda>x. (Y x, ())) {(x, y, ())} = joint_distribution X Y {(x, y)}"
hoelzl@39097
   732
  and "joint_distribution X (\<lambda>x. ((), Y x)) {(x, (), y)} = joint_distribution X Y {(x, y)}"
hoelzl@39097
   733
  and "distribution (\<lambda>x. ()) {()} = 1"
hoelzl@41981
   734
  by (auto intro!: arg_cong[where f=\<mu>'] simp: distribution_def prob_space[symmetric])
hoelzl@35977
   735
hoelzl@39097
   736
lemma (in finite_prob_space) setsum_distribution_gen:
hoelzl@39097
   737
  assumes "Z -` {c} \<inter> space M = (\<Union>x \<in> X`space M. Y -` {f x}) \<inter> space M"
hoelzl@39097
   738
  and "inj_on f (X`space M)"
hoelzl@39097
   739
  shows "(\<Sum>x \<in> X`space M. distribution Y {f x}) = distribution Z {c}"
hoelzl@39097
   740
  unfolding distribution_def assms
hoelzl@39097
   741
  using finite_space assms
hoelzl@41981
   742
  by (subst finite_measure_finite_Union[symmetric])
hoelzl@39097
   743
     (auto simp add: disjoint_family_on_def sets_eq_Pow inj_on_def
hoelzl@39097
   744
      intro!: arg_cong[where f=prob])
hoelzl@39097
   745
hoelzl@39097
   746
lemma (in finite_prob_space) setsum_distribution:
hoelzl@39097
   747
  "(\<Sum>x \<in> X`space M. joint_distribution X Y {(x, y)}) = distribution Y {y}"
hoelzl@39097
   748
  "(\<Sum>y \<in> Y`space M. joint_distribution X Y {(x, y)}) = distribution X {x}"
hoelzl@39097
   749
  "(\<Sum>x \<in> X`space M. joint_distribution X (\<lambda>x. (Y x, Z x)) {(x, y, z)}) = joint_distribution Y Z {(y, z)}"
hoelzl@39097
   750
  "(\<Sum>y \<in> Y`space M. joint_distribution X (\<lambda>x. (Y x, Z x)) {(x, y, z)}) = joint_distribution X Z {(x, z)}"
hoelzl@39097
   751
  "(\<Sum>z \<in> Z`space M. joint_distribution X (\<lambda>x. (Y x, Z x)) {(x, y, z)}) = joint_distribution X Y {(x, y)}"
hoelzl@39097
   752
  by (auto intro!: inj_onI setsum_distribution_gen)
hoelzl@39097
   753
hoelzl@39097
   754
lemma (in finite_prob_space) uniform_prob:
hoelzl@39097
   755
  assumes "x \<in> space M"
hoelzl@39097
   756
  assumes "\<And> x y. \<lbrakk>x \<in> space M ; y \<in> space M\<rbrakk> \<Longrightarrow> prob {x} = prob {y}"
hoelzl@41981
   757
  shows "prob {x} = 1 / card (space M)"
hoelzl@39097
   758
proof -
hoelzl@39097
   759
  have prob_x: "\<And> y. y \<in> space M \<Longrightarrow> prob {y} = prob {x}"
hoelzl@39097
   760
    using assms(2)[OF _ `x \<in> space M`] by blast
hoelzl@39097
   761
  have "1 = prob (space M)"
hoelzl@39097
   762
    using prob_space by auto
hoelzl@39097
   763
  also have "\<dots> = (\<Sum> x \<in> space M. prob {x})"
hoelzl@41981
   764
    using finite_measure_finite_Union[of "space M" "\<lambda> x. {x}", simplified]
hoelzl@39097
   765
      sets_eq_Pow inj_singleton[unfolded inj_on_def, rule_format]
hoelzl@39097
   766
      finite_space unfolding disjoint_family_on_def  prob_space[symmetric]
hoelzl@39097
   767
    by (auto simp add:setsum_restrict_set)
hoelzl@39097
   768
  also have "\<dots> = (\<Sum> y \<in> space M. prob {x})"
hoelzl@39097
   769
    using prob_x by auto
hoelzl@39097
   770
  also have "\<dots> = real_of_nat (card (space M)) * prob {x}" by simp
hoelzl@39097
   771
  finally have one: "1 = real (card (space M)) * prob {x}"
hoelzl@39097
   772
    using real_eq_of_nat by auto
hoelzl@39097
   773
  hence two: "real (card (space M)) \<noteq> 0" by fastsimp 
hoelzl@39097
   774
  from one have three: "prob {x} \<noteq> 0" by fastsimp
hoelzl@39097
   775
  thus ?thesis using one two three divide_cancel_right
hoelzl@39097
   776
    by (auto simp:field_simps)
hoelzl@39092
   777
qed
hoelzl@35977
   778
hoelzl@39092
   779
lemma (in prob_space) prob_space_subalgebra:
hoelzl@41545
   780
  assumes "sigma_algebra N" "sets N \<subseteq> sets M" "space N = space M"
hoelzl@41689
   781
    and "\<And>A. A \<in> sets N \<Longrightarrow> measure N A = \<mu> A"
hoelzl@41689
   782
  shows "prob_space N"
hoelzl@39092
   783
proof -
hoelzl@41689
   784
  interpret N: measure_space N
hoelzl@41689
   785
    by (rule measure_space_subalgebra[OF assms])
hoelzl@39092
   786
  show ?thesis
hoelzl@41689
   787
  proof qed (insert assms(4)[OF N.top], simp add: assms measure_space_1)
hoelzl@35977
   788
qed
hoelzl@35977
   789
hoelzl@39092
   790
lemma (in prob_space) prob_space_of_restricted_space:
hoelzl@41981
   791
  assumes "\<mu> A \<noteq> 0" "A \<in> sets M"
hoelzl@41689
   792
  shows "prob_space (restricted_space A \<lparr>measure := \<lambda>S. \<mu> S / \<mu> A\<rparr>)"
hoelzl@41689
   793
    (is "prob_space ?P")
hoelzl@41689
   794
proof -
hoelzl@41689
   795
  interpret A: measure_space "restricted_space A"
hoelzl@39092
   796
    using `A \<in> sets M` by (rule restricted_measure_space)
hoelzl@41689
   797
  interpret A': sigma_algebra ?P
hoelzl@41689
   798
    by (rule A.sigma_algebra_cong) auto
hoelzl@41689
   799
  show "prob_space ?P"
hoelzl@39092
   800
  proof
hoelzl@41689
   801
    show "measure ?P (space ?P) = 1"
hoelzl@41981
   802
      using real_measure[OF `A \<in> events`] `\<mu> A \<noteq> 0` by auto
hoelzl@41981
   803
    show "positive ?P (measure ?P)"
hoelzl@41981
   804
    proof (simp add: positive_def, safe)
hoelzl@41981
   805
      show "0 / \<mu> A = 0" using `\<mu> A \<noteq> 0` by (cases "\<mu> A") (auto simp: zero_extreal_def)
hoelzl@41981
   806
      fix B assume "B \<in> events"
hoelzl@41981
   807
      with real_measure[of "A \<inter> B"] real_measure[OF `A \<in> events`] `A \<in> sets M`
hoelzl@41981
   808
      show "0 \<le> \<mu> (A \<inter> B) / \<mu> A" by (auto simp: Int)
hoelzl@41981
   809
    qed
hoelzl@41981
   810
    show "countably_additive ?P (measure ?P)"
hoelzl@41981
   811
    proof (simp add: countably_additive_def, safe)
hoelzl@41981
   812
      fix B and F :: "nat \<Rightarrow> 'a set"
hoelzl@41981
   813
      assume F: "range F \<subseteq> op \<inter> A ` events" "disjoint_family F"
hoelzl@41981
   814
      { fix i
hoelzl@41981
   815
        from F have "F i \<in> op \<inter> A ` events" by auto
hoelzl@41981
   816
        with `A \<in> events` have "F i \<in> events" by auto }
hoelzl@41981
   817
      moreover then have "range F \<subseteq> events" by auto
hoelzl@41981
   818
      moreover have "\<And>S. \<mu> S / \<mu> A = inverse (\<mu> A) * \<mu> S"
hoelzl@41981
   819
        by (simp add: mult_commute divide_extreal_def)
hoelzl@41981
   820
      moreover have "0 \<le> inverse (\<mu> A)"
hoelzl@41981
   821
        using real_measure[OF `A \<in> events`] by auto
hoelzl@41981
   822
      ultimately show "(\<Sum>i. \<mu> (F i) / \<mu> A) = \<mu> (\<Union>i. F i) / \<mu> A"
hoelzl@41981
   823
        using measure_countably_additive[of F] F
hoelzl@41981
   824
        by (auto simp: suminf_cmult_extreal)
hoelzl@41981
   825
    qed
hoelzl@39092
   826
  qed
hoelzl@39092
   827
qed
hoelzl@39092
   828
hoelzl@39092
   829
lemma finite_prob_spaceI:
hoelzl@41981
   830
  assumes "finite (space M)" "sets M = Pow(space M)"
hoelzl@41981
   831
    and "measure M (space M) = 1" "measure M {} = 0" "\<And>A. A \<subseteq> space M \<Longrightarrow> 0 \<le> measure M A"
hoelzl@41689
   832
    and "\<And>A B. A\<subseteq>space M \<Longrightarrow> B\<subseteq>space M \<Longrightarrow> A \<inter> B = {} \<Longrightarrow> measure M (A \<union> B) = measure M A + measure M B"
hoelzl@41689
   833
  shows "finite_prob_space M"
hoelzl@39092
   834
  unfolding finite_prob_space_eq
hoelzl@39092
   835
proof
hoelzl@41689
   836
  show "finite_measure_space M" using assms
hoelzl@41981
   837
    by (auto intro!: finite_measure_spaceI)
hoelzl@41689
   838
  show "measure M (space M) = 1" by fact
hoelzl@39092
   839
qed
hoelzl@36624
   840
hoelzl@36624
   841
lemma (in finite_prob_space) finite_measure_space:
hoelzl@39097
   842
  fixes X :: "'a \<Rightarrow> 'x"
hoelzl@41981
   843
  shows "finite_measure_space \<lparr>space = X ` space M, sets = Pow (X ` space M), measure = extreal \<circ> distribution X\<rparr>"
hoelzl@41689
   844
    (is "finite_measure_space ?S")
hoelzl@39092
   845
proof (rule finite_measure_spaceI, simp_all)
hoelzl@36624
   846
  show "finite (X ` space M)" using finite_space by simp
hoelzl@39097
   847
next
hoelzl@39097
   848
  fix A B :: "'x set" assume "A \<inter> B = {}"
hoelzl@39097
   849
  then show "distribution X (A \<union> B) = distribution X A + distribution X B"
hoelzl@39097
   850
    unfolding distribution_def
hoelzl@41981
   851
    by (subst finite_measure_Union[symmetric])
hoelzl@41981
   852
       (auto intro!: arg_cong[where f=\<mu>'] simp: sets_eq_Pow)
hoelzl@36624
   853
qed
hoelzl@36624
   854
hoelzl@39097
   855
lemma (in finite_prob_space) finite_prob_space_of_images:
hoelzl@41981
   856
  "finite_prob_space \<lparr> space = X ` space M, sets = Pow (X ` space M), measure = extreal \<circ> distribution X \<rparr>"
hoelzl@41981
   857
  by (simp add: finite_prob_space_eq finite_measure_space measure_space_1 one_extreal_def)
hoelzl@39097
   858
hoelzl@39096
   859
lemma (in finite_prob_space) finite_product_measure_space:
hoelzl@39097
   860
  fixes X :: "'a \<Rightarrow> 'x" and Y :: "'a \<Rightarrow> 'y"
hoelzl@39096
   861
  assumes "finite s1" "finite s2"
hoelzl@41981
   862
  shows "finite_measure_space \<lparr> space = s1 \<times> s2, sets = Pow (s1 \<times> s2), measure = extreal \<circ> joint_distribution X Y\<rparr>"
hoelzl@41689
   863
    (is "finite_measure_space ?M")
hoelzl@39097
   864
proof (rule finite_measure_spaceI, simp_all)
hoelzl@39097
   865
  show "finite (s1 \<times> s2)"
hoelzl@39096
   866
    using assms by auto
hoelzl@39097
   867
next
hoelzl@39097
   868
  fix A B :: "('x*'y) set" assume "A \<inter> B = {}"
hoelzl@39097
   869
  then show "joint_distribution X Y (A \<union> B) = joint_distribution X Y A + joint_distribution X Y B"
hoelzl@39097
   870
    unfolding distribution_def
hoelzl@41981
   871
    by (subst finite_measure_Union[symmetric])
hoelzl@41981
   872
       (auto intro!: arg_cong[where f=\<mu>'] simp: sets_eq_Pow)
hoelzl@39096
   873
qed
hoelzl@39096
   874
hoelzl@39097
   875
lemma (in finite_prob_space) finite_product_measure_space_of_images:
hoelzl@39096
   876
  shows "finite_measure_space \<lparr> space = X ` space M \<times> Y ` space M,
hoelzl@41689
   877
                                sets = Pow (X ` space M \<times> Y ` space M),
hoelzl@41981
   878
                                measure = extreal \<circ> joint_distribution X Y \<rparr>"
hoelzl@39096
   879
  using finite_space by (auto intro!: finite_product_measure_space)
hoelzl@39096
   880
hoelzl@40859
   881
lemma (in finite_prob_space) finite_product_prob_space_of_images:
hoelzl@41689
   882
  "finite_prob_space \<lparr> space = X ` space M \<times> Y ` space M, sets = Pow (X ` space M \<times> Y ` space M),
hoelzl@41981
   883
                       measure = extreal \<circ> joint_distribution X Y \<rparr>"
hoelzl@41689
   884
  (is "finite_prob_space ?S")
hoelzl@41981
   885
proof (simp add: finite_prob_space_eq finite_product_measure_space_of_images one_extreal_def)
hoelzl@40859
   886
  have "X -` X ` space M \<inter> Y -` Y ` space M \<inter> space M = space M" by auto
hoelzl@40859
   887
  thus "joint_distribution X Y (X ` space M \<times> Y ` space M) = 1"
hoelzl@40859
   888
    by (simp add: distribution_def prob_space vimage_Times comp_def measure_space_1)
hoelzl@40859
   889
qed
hoelzl@40859
   890
hoelzl@39085
   891
section "Conditional Expectation and Probability"
hoelzl@39085
   892
hoelzl@39085
   893
lemma (in prob_space) conditional_expectation_exists:
hoelzl@41981
   894
  fixes X :: "'a \<Rightarrow> extreal" and N :: "('a, 'b) measure_space_scheme"
hoelzl@41981
   895
  assumes borel: "X \<in> borel_measurable M" "AE x. 0 \<le> X x"
hoelzl@41689
   896
  and N: "sigma_algebra N" "sets N \<subseteq> sets M" "space N = space M" "\<And>A. measure N A = \<mu> A"
hoelzl@41981
   897
  shows "\<exists>Y\<in>borel_measurable N. (\<forall>x. 0 \<le> Y x) \<and> (\<forall>C\<in>sets N.
hoelzl@41981
   898
      (\<integral>\<^isup>+x. Y x * indicator C x \<partial>M) = (\<integral>\<^isup>+x. X x * indicator C x \<partial>M))"
hoelzl@39083
   899
proof -
hoelzl@41689
   900
  note N(4)[simp]
hoelzl@41689
   901
  interpret P: prob_space N
hoelzl@41545
   902
    using prob_space_subalgebra[OF N] .
hoelzl@39083
   903
hoelzl@39083
   904
  let "?f A" = "\<lambda>x. X x * indicator A x"
hoelzl@41689
   905
  let "?Q A" = "integral\<^isup>P M (?f A)"
hoelzl@39083
   906
hoelzl@39083
   907
  from measure_space_density[OF borel]
hoelzl@41689
   908
  have Q: "measure_space (N\<lparr> measure := ?Q \<rparr>)"
hoelzl@41689
   909
    apply (rule measure_space.measure_space_subalgebra[of "M\<lparr> measure := ?Q \<rparr>"])
hoelzl@41689
   910
    using N by (auto intro!: P.sigma_algebra_cong)
hoelzl@41689
   911
  then interpret Q: measure_space "N\<lparr> measure := ?Q \<rparr>" .
hoelzl@39083
   912
hoelzl@39083
   913
  have "P.absolutely_continuous ?Q"
hoelzl@39083
   914
    unfolding P.absolutely_continuous_def
hoelzl@41545
   915
  proof safe
hoelzl@41689
   916
    fix A assume "A \<in> sets N" "P.\<mu> A = 0"
hoelzl@41981
   917
    then have f_borel: "?f A \<in> borel_measurable M" "AE x. x \<notin> A"
hoelzl@41981
   918
      using borel N by (auto intro!: borel_measurable_indicator AE_not_in)
hoelzl@41981
   919
    then show "?Q A = 0"
hoelzl@41981
   920
      by (auto simp add: positive_integral_0_iff_AE)
hoelzl@39083
   921
  qed
hoelzl@39083
   922
  from P.Radon_Nikodym[OF Q this]
hoelzl@41981
   923
  obtain Y where Y: "Y \<in> borel_measurable N" "\<And>x. 0 \<le> Y x"
hoelzl@41689
   924
    "\<And>A. A \<in> sets N \<Longrightarrow> ?Q A =(\<integral>\<^isup>+x. Y x * indicator A x \<partial>N)"
hoelzl@39083
   925
    by blast
hoelzl@41545
   926
  with N(2) show ?thesis
hoelzl@41981
   927
    by (auto intro!: bexI[OF _ Y(1)] simp: positive_integral_subalgebra[OF _ _ N(2,3,4,1)])
hoelzl@39083
   928
qed
hoelzl@39083
   929
hoelzl@39085
   930
definition (in prob_space)
hoelzl@41981
   931
  "conditional_expectation N X = (SOME Y. Y\<in>borel_measurable N \<and> (\<forall>x. 0 \<le> Y x)
hoelzl@41689
   932
    \<and> (\<forall>C\<in>sets N. (\<integral>\<^isup>+x. Y x * indicator C x\<partial>M) = (\<integral>\<^isup>+x. X x * indicator C x\<partial>M)))"
hoelzl@39085
   933
hoelzl@39085
   934
abbreviation (in prob_space)
hoelzl@39092
   935
  "conditional_prob N A \<equiv> conditional_expectation N (indicator A)"
hoelzl@39085
   936
hoelzl@39085
   937
lemma (in prob_space)
hoelzl@41981
   938
  fixes X :: "'a \<Rightarrow> extreal" and N :: "('a, 'b) measure_space_scheme"
hoelzl@41981
   939
  assumes borel: "X \<in> borel_measurable M" "AE x. 0 \<le> X x"
hoelzl@41689
   940
  and N: "sigma_algebra N" "sets N \<subseteq> sets M" "space N = space M" "\<And>A. measure N A = \<mu> A"
hoelzl@39085
   941
  shows borel_measurable_conditional_expectation:
hoelzl@41545
   942
    "conditional_expectation N X \<in> borel_measurable N"
hoelzl@41545
   943
  and conditional_expectation: "\<And>C. C \<in> sets N \<Longrightarrow>
hoelzl@41689
   944
      (\<integral>\<^isup>+x. conditional_expectation N X x * indicator C x \<partial>M) =
hoelzl@41689
   945
      (\<integral>\<^isup>+x. X x * indicator C x \<partial>M)"
hoelzl@41545
   946
   (is "\<And>C. C \<in> sets N \<Longrightarrow> ?eq C")
hoelzl@39085
   947
proof -
hoelzl@39085
   948
  note CE = conditional_expectation_exists[OF assms, unfolded Bex_def]
hoelzl@41545
   949
  then show "conditional_expectation N X \<in> borel_measurable N"
hoelzl@39085
   950
    unfolding conditional_expectation_def by (rule someI2_ex) blast
hoelzl@39085
   951
hoelzl@41545
   952
  from CE show "\<And>C. C \<in> sets N \<Longrightarrow> ?eq C"
hoelzl@39085
   953
    unfolding conditional_expectation_def by (rule someI2_ex) blast
hoelzl@39085
   954
qed
hoelzl@39085
   955
hoelzl@41981
   956
lemma (in sigma_algebra) factorize_measurable_function_pos:
hoelzl@41981
   957
  fixes Z :: "'a \<Rightarrow> extreal" and Y :: "'a \<Rightarrow> 'c"
hoelzl@39091
   958
  assumes "sigma_algebra M'" and "Y \<in> measurable M M'" "Z \<in> borel_measurable M"
hoelzl@41981
   959
  assumes Z: "Z \<in> borel_measurable (sigma_algebra.vimage_algebra M' (space M) Y)"
hoelzl@41981
   960
  shows "\<exists>g\<in>borel_measurable M'. \<forall>x\<in>space M. max 0 (Z x) = g (Y x)"
hoelzl@41981
   961
proof -
hoelzl@39091
   962
  interpret M': sigma_algebra M' by fact
hoelzl@39091
   963
  have Y: "Y \<in> space M \<rightarrow> space M'" using assms unfolding measurable_def by auto
hoelzl@39091
   964
  from M'.sigma_algebra_vimage[OF this]
hoelzl@39091
   965
  interpret va: sigma_algebra "M'.vimage_algebra (space M) Y" .
hoelzl@39091
   966
hoelzl@41981
   967
  from va.borel_measurable_implies_simple_function_sequence'[OF Z] guess f . note f = this
hoelzl@39091
   968
hoelzl@41689
   969
  have "\<forall>i. \<exists>g. simple_function M' g \<and> (\<forall>x\<in>space M. f i x = g (Y x))"
hoelzl@39091
   970
  proof
hoelzl@39091
   971
    fix i
hoelzl@41981
   972
    from f(1)[of i] have "finite (f i`space M)" and B_ex:
hoelzl@39091
   973
      "\<forall>z\<in>(f i)`space M. \<exists>B. B \<in> sets M' \<and> (f i) -` {z} \<inter> space M = Y -` B \<inter> space M"
hoelzl@41689
   974
      unfolding simple_function_def by auto
hoelzl@39091
   975
    from B_ex[THEN bchoice] guess B .. note B = this
hoelzl@39091
   976
hoelzl@39091
   977
    let ?g = "\<lambda>x. \<Sum>z\<in>f i`space M. z * indicator (B z) x"
hoelzl@39091
   978
hoelzl@41689
   979
    show "\<exists>g. simple_function M' g \<and> (\<forall>x\<in>space M. f i x = g (Y x))"
hoelzl@39091
   980
    proof (intro exI[of _ ?g] conjI ballI)
hoelzl@41689
   981
      show "simple_function M' ?g" using B by auto
hoelzl@39091
   982
hoelzl@39091
   983
      fix x assume "x \<in> space M"
hoelzl@41981
   984
      then have "\<And>z. z \<in> f i`space M \<Longrightarrow> indicator (B z) (Y x) = (indicator (f i -` {z} \<inter> space M) x::extreal)"
hoelzl@39091
   985
        unfolding indicator_def using B by auto
hoelzl@41981
   986
      then show "f i x = ?g (Y x)" using `x \<in> space M` f(1)[of i]
hoelzl@39091
   987
        by (subst va.simple_function_indicator_representation) auto
hoelzl@39091
   988
    qed
hoelzl@39091
   989
  qed
hoelzl@39091
   990
  from choice[OF this] guess g .. note g = this
hoelzl@39091
   991
hoelzl@41981
   992
  show ?thesis
hoelzl@39091
   993
  proof (intro ballI bexI)
hoelzl@41097
   994
    show "(\<lambda>x. SUP i. g i x) \<in> borel_measurable M'"
hoelzl@39091
   995
      using g by (auto intro: M'.borel_measurable_simple_function)
hoelzl@39091
   996
    fix x assume "x \<in> space M"
hoelzl@41981
   997
    have "max 0 (Z x) = (SUP i. f i x)" using f by simp
hoelzl@41981
   998
    also have "\<dots> = (SUP i. g i (Y x))"
hoelzl@39091
   999
      using g `x \<in> space M` by simp
hoelzl@41981
  1000
    finally show "max 0 (Z x) = (SUP i. g i (Y x))" .
hoelzl@41981
  1001
  qed
hoelzl@41981
  1002
qed
hoelzl@41981
  1003
hoelzl@41981
  1004
lemma extreal_0_le_iff_le_0[simp]:
hoelzl@41981
  1005
  fixes a :: extreal shows "0 \<le> -a \<longleftrightarrow> a \<le> 0"
hoelzl@41981
  1006
  by (cases rule: extreal2_cases[of a]) auto
hoelzl@41981
  1007
hoelzl@41981
  1008
lemma (in sigma_algebra) factorize_measurable_function:
hoelzl@41981
  1009
  fixes Z :: "'a \<Rightarrow> extreal" and Y :: "'a \<Rightarrow> 'c"
hoelzl@41981
  1010
  assumes "sigma_algebra M'" and "Y \<in> measurable M M'" "Z \<in> borel_measurable M"
hoelzl@41981
  1011
  shows "Z \<in> borel_measurable (sigma_algebra.vimage_algebra M' (space M) Y)
hoelzl@41981
  1012
    \<longleftrightarrow> (\<exists>g\<in>borel_measurable M'. \<forall>x\<in>space M. Z x = g (Y x))"
hoelzl@41981
  1013
proof safe
hoelzl@41981
  1014
  interpret M': sigma_algebra M' by fact
hoelzl@41981
  1015
  have Y: "Y \<in> space M \<rightarrow> space M'" using assms unfolding measurable_def by auto
hoelzl@41981
  1016
  from M'.sigma_algebra_vimage[OF this]
hoelzl@41981
  1017
  interpret va: sigma_algebra "M'.vimage_algebra (space M) Y" .
hoelzl@41981
  1018
hoelzl@41981
  1019
  { fix g :: "'c \<Rightarrow> extreal" assume "g \<in> borel_measurable M'"
hoelzl@41981
  1020
    with M'.measurable_vimage_algebra[OF Y]
hoelzl@41981
  1021
    have "g \<circ> Y \<in> borel_measurable (M'.vimage_algebra (space M) Y)"
hoelzl@41981
  1022
      by (rule measurable_comp)
hoelzl@41981
  1023
    moreover assume "\<forall>x\<in>space M. Z x = g (Y x)"
hoelzl@41981
  1024
    then have "Z \<in> borel_measurable (M'.vimage_algebra (space M) Y) \<longleftrightarrow>
hoelzl@41981
  1025
       g \<circ> Y \<in> borel_measurable (M'.vimage_algebra (space M) Y)"
hoelzl@41981
  1026
       by (auto intro!: measurable_cong)
hoelzl@41981
  1027
    ultimately show "Z \<in> borel_measurable (M'.vimage_algebra (space M) Y)"
hoelzl@41981
  1028
      by simp }
hoelzl@41981
  1029
hoelzl@41981
  1030
  assume Z: "Z \<in> borel_measurable (M'.vimage_algebra (space M) Y)"
hoelzl@41981
  1031
  with assms have "(\<lambda>x. - Z x) \<in> borel_measurable M"
hoelzl@41981
  1032
    "(\<lambda>x. - Z x) \<in> borel_measurable (M'.vimage_algebra (space M) Y)"
hoelzl@41981
  1033
    by auto
hoelzl@41981
  1034
  from factorize_measurable_function_pos[OF assms(1,2) this] guess n .. note n = this
hoelzl@41981
  1035
  from factorize_measurable_function_pos[OF assms Z] guess p .. note p = this
hoelzl@41981
  1036
  let "?g x" = "p x - n x"
hoelzl@41981
  1037
  show "\<exists>g\<in>borel_measurable M'. \<forall>x\<in>space M. Z x = g (Y x)"
hoelzl@41981
  1038
  proof (intro bexI ballI)
hoelzl@41981
  1039
    show "?g \<in> borel_measurable M'" using p n by auto
hoelzl@41981
  1040
    fix x assume "x \<in> space M"
hoelzl@41981
  1041
    then have "p (Y x) = max 0 (Z x)" "n (Y x) = max 0 (- Z x)"
hoelzl@41981
  1042
      using p n by auto
hoelzl@41981
  1043
    then show "Z x = ?g (Y x)"
hoelzl@41981
  1044
      by (auto split: split_max)
hoelzl@39091
  1045
  qed
hoelzl@39091
  1046
qed
hoelzl@39090
  1047
hoelzl@35582
  1048
end