src/HOL/Probability/Probability_Space.thy
author hoelzl
Thu Sep 02 19:51:53 2010 +0200 (2010-09-02)
changeset 39097 943c7b348524
parent 39096 111756225292
child 39198 f967a16dfcdd
permissions -rw-r--r--
Moved lemmas to appropriate locations
hoelzl@35582
     1
theory Probability_Space
hoelzl@39083
     2
imports Lebesgue_Integration Radon_Nikodym
hoelzl@35582
     3
begin
hoelzl@35582
     4
hoelzl@35582
     5
locale prob_space = measure_space +
hoelzl@38656
     6
  assumes measure_space_1: "\<mu> (space M) = 1"
hoelzl@38656
     7
hoelzl@38656
     8
sublocale prob_space < finite_measure
hoelzl@38656
     9
proof
hoelzl@38656
    10
  from measure_space_1 show "\<mu> (space M) \<noteq> \<omega>" by simp
hoelzl@38656
    11
qed
hoelzl@38656
    12
hoelzl@38656
    13
context prob_space
hoelzl@35582
    14
begin
hoelzl@35582
    15
hoelzl@35582
    16
abbreviation "events \<equiv> sets M"
hoelzl@38656
    17
abbreviation "prob \<equiv> \<lambda>A. real (\<mu> A)"
hoelzl@35582
    18
abbreviation "prob_preserving \<equiv> measure_preserving"
hoelzl@35582
    19
abbreviation "random_variable \<equiv> \<lambda> s X. X \<in> measurable M s"
hoelzl@35582
    20
abbreviation "expectation \<equiv> integral"
hoelzl@35582
    21
hoelzl@35582
    22
definition
hoelzl@35582
    23
  "indep A B \<longleftrightarrow> A \<in> events \<and> B \<in> events \<and> prob (A \<inter> B) = prob A * prob B"
hoelzl@35582
    24
hoelzl@35582
    25
definition
hoelzl@35582
    26
  "indep_families F G \<longleftrightarrow> (\<forall> A \<in> F. \<forall> B \<in> G. indep A B)"
hoelzl@35582
    27
hoelzl@35582
    28
definition
hoelzl@38656
    29
  "distribution X = (\<lambda>s. \<mu> ((X -` s) \<inter> (space M)))"
hoelzl@35582
    30
hoelzl@36624
    31
abbreviation
hoelzl@36624
    32
  "joint_distribution X Y \<equiv> distribution (\<lambda>x. (X x, Y x))"
hoelzl@35582
    33
hoelzl@39097
    34
lemma (in prob_space) distribution_cong:
hoelzl@39097
    35
  assumes "\<And>x. x \<in> space M \<Longrightarrow> X x = Y x"
hoelzl@39097
    36
  shows "distribution X = distribution Y"
hoelzl@39097
    37
  unfolding distribution_def expand_fun_eq
hoelzl@39097
    38
  using assms by (auto intro!: arg_cong[where f="\<mu>"])
hoelzl@39097
    39
hoelzl@39097
    40
lemma (in prob_space) joint_distribution_cong:
hoelzl@39097
    41
  assumes "\<And>x. x \<in> space M \<Longrightarrow> X x = X' x"
hoelzl@39097
    42
  assumes "\<And>x. x \<in> space M \<Longrightarrow> Y x = Y' x"
hoelzl@39097
    43
  shows "joint_distribution X Y = joint_distribution X' Y'"
hoelzl@39097
    44
  unfolding distribution_def expand_fun_eq
hoelzl@39097
    45
  using assms by (auto intro!: arg_cong[where f="\<mu>"])
hoelzl@39097
    46
hoelzl@38656
    47
lemma prob_space: "prob (space M) = 1"
hoelzl@38656
    48
  unfolding measure_space_1 by simp
hoelzl@35582
    49
hoelzl@38656
    50
lemma measure_le_1[simp, intro]:
hoelzl@38656
    51
  assumes "A \<in> events" shows "\<mu> A \<le> 1"
hoelzl@38656
    52
proof -
hoelzl@38656
    53
  have "\<mu> A \<le> \<mu> (space M)"
hoelzl@38656
    54
    using assms sets_into_space by(auto intro!: measure_mono)
hoelzl@38656
    55
  also note measure_space_1
hoelzl@38656
    56
  finally show ?thesis .
hoelzl@38656
    57
qed
hoelzl@35582
    58
hoelzl@35582
    59
lemma prob_compl:
hoelzl@38656
    60
  assumes "A \<in> events"
hoelzl@38656
    61
  shows "prob (space M - A) = 1 - prob A"
hoelzl@38656
    62
  using `A \<in> events`[THEN sets_into_space] `A \<in> events` measure_space_1
hoelzl@38656
    63
  by (subst real_finite_measure_Diff) auto
hoelzl@35582
    64
hoelzl@35582
    65
lemma indep_space:
hoelzl@35582
    66
  assumes "s \<in> events"
hoelzl@35582
    67
  shows "indep (space M) s"
hoelzl@38656
    68
  using assms prob_space by (simp add: indep_def)
hoelzl@35582
    69
hoelzl@38656
    70
lemma prob_space_increasing: "increasing M prob"
hoelzl@38656
    71
  by (auto intro!: real_measure_mono simp: increasing_def)
hoelzl@35582
    72
hoelzl@35582
    73
lemma prob_zero_union:
hoelzl@35582
    74
  assumes "s \<in> events" "t \<in> events" "prob t = 0"
hoelzl@35582
    75
  shows "prob (s \<union> t) = prob s"
hoelzl@38656
    76
using assms
hoelzl@35582
    77
proof -
hoelzl@35582
    78
  have "prob (s \<union> t) \<le> prob s"
hoelzl@38656
    79
    using real_finite_measure_subadditive[of s t] assms by auto
hoelzl@35582
    80
  moreover have "prob (s \<union> t) \<ge> prob s"
hoelzl@38656
    81
    using assms by (blast intro: real_measure_mono)
hoelzl@35582
    82
  ultimately show ?thesis by simp
hoelzl@35582
    83
qed
hoelzl@35582
    84
hoelzl@35582
    85
lemma prob_eq_compl:
hoelzl@35582
    86
  assumes "s \<in> events" "t \<in> events"
hoelzl@35582
    87
  assumes "prob (space M - s) = prob (space M - t)"
hoelzl@35582
    88
  shows "prob s = prob t"
hoelzl@38656
    89
  using assms prob_compl by auto
hoelzl@35582
    90
hoelzl@35582
    91
lemma prob_one_inter:
hoelzl@35582
    92
  assumes events:"s \<in> events" "t \<in> events"
hoelzl@35582
    93
  assumes "prob t = 1"
hoelzl@35582
    94
  shows "prob (s \<inter> t) = prob s"
hoelzl@35582
    95
proof -
hoelzl@38656
    96
  have "prob ((space M - s) \<union> (space M - t)) = prob (space M - s)"
hoelzl@38656
    97
    using events assms  prob_compl[of "t"] by (auto intro!: prob_zero_union)
hoelzl@38656
    98
  also have "(space M - s) \<union> (space M - t) = space M - (s \<inter> t)"
hoelzl@38656
    99
    by blast
hoelzl@38656
   100
  finally show "prob (s \<inter> t) = prob s"
hoelzl@38656
   101
    using events by (auto intro!: prob_eq_compl[of "s \<inter> t" s])
hoelzl@35582
   102
qed
hoelzl@35582
   103
hoelzl@35582
   104
lemma prob_eq_bigunion_image:
hoelzl@35582
   105
  assumes "range f \<subseteq> events" "range g \<subseteq> events"
hoelzl@35582
   106
  assumes "disjoint_family f" "disjoint_family g"
hoelzl@35582
   107
  assumes "\<And> n :: nat. prob (f n) = prob (g n)"
hoelzl@35582
   108
  shows "(prob (\<Union> i. f i) = prob (\<Union> i. g i))"
hoelzl@35582
   109
using assms
hoelzl@35582
   110
proof -
hoelzl@38656
   111
  have a: "(\<lambda> i. prob (f i)) sums (prob (\<Union> i. f i))"
hoelzl@38656
   112
    by (rule real_finite_measure_UNION[OF assms(1,3)])
hoelzl@38656
   113
  have b: "(\<lambda> i. prob (g i)) sums (prob (\<Union> i. g i))"
hoelzl@38656
   114
    by (rule real_finite_measure_UNION[OF assms(2,4)])
hoelzl@38656
   115
  show ?thesis using sums_unique[OF b] sums_unique[OF a] assms by simp
hoelzl@35582
   116
qed
hoelzl@35582
   117
hoelzl@35582
   118
lemma prob_countably_zero:
hoelzl@35582
   119
  assumes "range c \<subseteq> events"
hoelzl@35582
   120
  assumes "\<And> i. prob (c i) = 0"
hoelzl@38656
   121
  shows "prob (\<Union> i :: nat. c i) = 0"
hoelzl@38656
   122
proof (rule antisym)
hoelzl@38656
   123
  show "prob (\<Union> i :: nat. c i) \<le> 0"
hoelzl@38656
   124
    using real_finite_measurable_countably_subadditive[OF assms(1)]
hoelzl@38656
   125
    by (simp add: assms(2) suminf_zero summable_zero)
hoelzl@38656
   126
  show "0 \<le> prob (\<Union> i :: nat. c i)" by (rule real_pinfreal_nonneg)
hoelzl@35582
   127
qed
hoelzl@35582
   128
hoelzl@35582
   129
lemma indep_sym:
hoelzl@35582
   130
   "indep a b \<Longrightarrow> indep b a"
hoelzl@35582
   131
unfolding indep_def using Int_commute[of a b] by auto
hoelzl@35582
   132
hoelzl@35582
   133
lemma indep_refl:
hoelzl@35582
   134
  assumes "a \<in> events"
hoelzl@35582
   135
  shows "indep a a = (prob a = 0) \<or> (prob a = 1)"
hoelzl@35582
   136
using assms unfolding indep_def by auto
hoelzl@35582
   137
hoelzl@35582
   138
lemma prob_equiprobable_finite_unions:
hoelzl@38656
   139
  assumes "s \<in> events"
hoelzl@38656
   140
  assumes s_finite: "finite s" "\<And>x. x \<in> s \<Longrightarrow> {x} \<in> events"
hoelzl@35582
   141
  assumes "\<And> x y. \<lbrakk>x \<in> s; y \<in> s\<rbrakk> \<Longrightarrow> (prob {x} = prob {y})"
hoelzl@38656
   142
  shows "prob s = real (card s) * prob {SOME x. x \<in> s}"
hoelzl@35582
   143
proof (cases "s = {}")
hoelzl@38656
   144
  case False hence "\<exists> x. x \<in> s" by blast
hoelzl@35582
   145
  from someI_ex[OF this] assms
hoelzl@35582
   146
  have prob_some: "\<And> x. x \<in> s \<Longrightarrow> prob {x} = prob {SOME y. y \<in> s}" by blast
hoelzl@35582
   147
  have "prob s = (\<Sum> x \<in> s. prob {x})"
hoelzl@38656
   148
    using real_finite_measure_finite_singelton[OF s_finite] by simp
hoelzl@35582
   149
  also have "\<dots> = (\<Sum> x \<in> s. prob {SOME y. y \<in> s})" using prob_some by auto
hoelzl@38656
   150
  also have "\<dots> = real (card s) * prob {(SOME x. x \<in> s)}"
hoelzl@38656
   151
    using setsum_constant assms by (simp add: real_eq_of_nat)
hoelzl@35582
   152
  finally show ?thesis by simp
hoelzl@38656
   153
qed simp
hoelzl@35582
   154
hoelzl@35582
   155
lemma prob_real_sum_image_fn:
hoelzl@35582
   156
  assumes "e \<in> events"
hoelzl@35582
   157
  assumes "\<And> x. x \<in> s \<Longrightarrow> e \<inter> f x \<in> events"
hoelzl@35582
   158
  assumes "finite s"
hoelzl@38656
   159
  assumes disjoint: "\<And> x y. \<lbrakk>x \<in> s ; y \<in> s ; x \<noteq> y\<rbrakk> \<Longrightarrow> f x \<inter> f y = {}"
hoelzl@38656
   160
  assumes upper: "space M \<subseteq> (\<Union> i \<in> s. f i)"
hoelzl@35582
   161
  shows "prob e = (\<Sum> x \<in> s. prob (e \<inter> f x))"
hoelzl@35582
   162
proof -
hoelzl@38656
   163
  have e: "e = (\<Union> i \<in> s. e \<inter> f i)"
hoelzl@38656
   164
    using `e \<in> events` sets_into_space upper by blast
hoelzl@38656
   165
  hence "prob e = prob (\<Union> i \<in> s. e \<inter> f i)" by simp
hoelzl@38656
   166
  also have "\<dots> = (\<Sum> x \<in> s. prob (e \<inter> f x))"
hoelzl@38656
   167
  proof (rule real_finite_measure_finite_Union)
hoelzl@38656
   168
    show "finite s" by fact
hoelzl@38656
   169
    show "\<And>i. i \<in> s \<Longrightarrow> e \<inter> f i \<in> events" by fact
hoelzl@38656
   170
    show "disjoint_family_on (\<lambda>i. e \<inter> f i) s"
hoelzl@38656
   171
      using disjoint by (auto simp: disjoint_family_on_def)
hoelzl@38656
   172
  qed
hoelzl@38656
   173
  finally show ?thesis .
hoelzl@35582
   174
qed
hoelzl@35582
   175
hoelzl@35582
   176
lemma distribution_prob_space:
hoelzl@39089
   177
  assumes S: "sigma_algebra S" "random_variable S X"
hoelzl@38656
   178
  shows "prob_space S (distribution X)"
hoelzl@35582
   179
proof -
hoelzl@39089
   180
  interpret S: measure_space S "distribution X"
hoelzl@39089
   181
    using measure_space_vimage[OF S(2,1)] unfolding distribution_def .
hoelzl@38656
   182
  show ?thesis
hoelzl@38656
   183
  proof
hoelzl@38656
   184
    have "X -` space S \<inter> space M = space M"
hoelzl@38656
   185
      using `random_variable S X` by (auto simp: measurable_def)
hoelzl@39089
   186
    then show "distribution X (space S) = 1"
hoelzl@39089
   187
      using measure_space_1 by (simp add: distribution_def)
hoelzl@35582
   188
  qed
hoelzl@35582
   189
qed
hoelzl@35582
   190
hoelzl@35582
   191
lemma distribution_lebesgue_thm1:
hoelzl@35582
   192
  assumes "random_variable s X"
hoelzl@35582
   193
  assumes "A \<in> sets s"
hoelzl@38656
   194
  shows "real (distribution X A) = expectation (indicator (X -` A \<inter> space M))"
hoelzl@35582
   195
unfolding distribution_def
hoelzl@35582
   196
using assms unfolding measurable_def
hoelzl@38656
   197
using integral_indicator by auto
hoelzl@35582
   198
hoelzl@35582
   199
lemma distribution_lebesgue_thm2:
hoelzl@38656
   200
  assumes "sigma_algebra S" "random_variable S X" and "A \<in> sets S"
hoelzl@38656
   201
  shows "distribution X A =
hoelzl@38656
   202
    measure_space.positive_integral S (distribution X) (indicator A)"
hoelzl@38656
   203
  (is "_ = measure_space.positive_integral _ ?D _")
hoelzl@35582
   204
proof -
hoelzl@38656
   205
  interpret S: prob_space S "distribution X" using assms(1,2) by (rule distribution_prob_space)
hoelzl@35582
   206
hoelzl@35582
   207
  show ?thesis
hoelzl@38656
   208
    using S.positive_integral_indicator(1)
hoelzl@35582
   209
    using assms unfolding distribution_def by auto
hoelzl@35582
   210
qed
hoelzl@35582
   211
hoelzl@35582
   212
lemma finite_expectation1:
hoelzl@38656
   213
  assumes "finite (X`space M)" and rv: "random_variable borel_space X"
hoelzl@35582
   214
  shows "expectation X = (\<Sum> r \<in> X ` space M. r * prob (X -` {r} \<inter> space M))"
hoelzl@38656
   215
proof (rule integral_on_finite(2)[OF assms(2,1)])
hoelzl@38656
   216
  fix x have "X -` {x} \<inter> space M \<in> sets M"
hoelzl@38656
   217
    using rv unfolding measurable_def by auto
hoelzl@38656
   218
  thus "\<mu> (X -` {x} \<inter> space M) \<noteq> \<omega>" using finite_measure by simp
hoelzl@38656
   219
qed
hoelzl@35582
   220
hoelzl@35582
   221
lemma finite_expectation:
hoelzl@38656
   222
  assumes "finite (space M)" "random_variable borel_space X"
hoelzl@38656
   223
  shows "expectation X = (\<Sum> r \<in> X ` (space M). r * real (distribution X {r}))"
hoelzl@38656
   224
  using assms unfolding distribution_def using finite_expectation1 by auto
hoelzl@38656
   225
hoelzl@35582
   226
lemma prob_x_eq_1_imp_prob_y_eq_0:
hoelzl@35582
   227
  assumes "{x} \<in> events"
hoelzl@38656
   228
  assumes "prob {x} = 1"
hoelzl@35582
   229
  assumes "{y} \<in> events"
hoelzl@35582
   230
  assumes "y \<noteq> x"
hoelzl@35582
   231
  shows "prob {y} = 0"
hoelzl@35582
   232
  using prob_one_inter[of "{y}" "{x}"] assms by auto
hoelzl@35582
   233
hoelzl@38656
   234
lemma distribution_empty[simp]: "distribution X {} = 0"
hoelzl@38656
   235
  unfolding distribution_def by simp
hoelzl@38656
   236
hoelzl@38656
   237
lemma distribution_space[simp]: "distribution X (X ` space M) = 1"
hoelzl@38656
   238
proof -
hoelzl@38656
   239
  have "X -` X ` space M \<inter> space M = space M" by auto
hoelzl@38656
   240
  thus ?thesis unfolding distribution_def by (simp add: measure_space_1)
hoelzl@38656
   241
qed
hoelzl@38656
   242
hoelzl@38656
   243
lemma distribution_one:
hoelzl@38656
   244
  assumes "random_variable M X" and "A \<in> events"
hoelzl@38656
   245
  shows "distribution X A \<le> 1"
hoelzl@38656
   246
proof -
hoelzl@38656
   247
  have "distribution X A \<le> \<mu> (space M)" unfolding distribution_def
hoelzl@38656
   248
    using assms[unfolded measurable_def] by (auto intro!: measure_mono)
hoelzl@38656
   249
  thus ?thesis by (simp add: measure_space_1)
hoelzl@38656
   250
qed
hoelzl@38656
   251
hoelzl@38656
   252
lemma distribution_finite:
hoelzl@38656
   253
  assumes "random_variable M X" and "A \<in> events"
hoelzl@38656
   254
  shows "distribution X A \<noteq> \<omega>"
hoelzl@38656
   255
  using distribution_one[OF assms] by auto
hoelzl@38656
   256
hoelzl@35582
   257
lemma distribution_x_eq_1_imp_distribution_y_eq_0:
hoelzl@35582
   258
  assumes X: "random_variable \<lparr>space = X ` (space M), sets = Pow (X ` (space M))\<rparr> X"
hoelzl@38656
   259
    (is "random_variable ?S X")
hoelzl@38656
   260
  assumes "distribution X {x} = 1"
hoelzl@35582
   261
  assumes "y \<noteq> x"
hoelzl@35582
   262
  shows "distribution X {y} = 0"
hoelzl@35582
   263
proof -
hoelzl@38656
   264
  have "sigma_algebra ?S" by (rule sigma_algebra_Pow)
hoelzl@38656
   265
  from distribution_prob_space[OF this X]
hoelzl@38656
   266
  interpret S: prob_space ?S "distribution X" by simp
hoelzl@38656
   267
hoelzl@38656
   268
  have x: "{x} \<in> sets ?S"
hoelzl@38656
   269
  proof (rule ccontr)
hoelzl@38656
   270
    assume "{x} \<notin> sets ?S"
hoelzl@35582
   271
    hence "X -` {x} \<inter> space M = {}" by auto
hoelzl@38656
   272
    thus "False" using assms unfolding distribution_def by auto
hoelzl@38656
   273
  qed
hoelzl@38656
   274
hoelzl@38656
   275
  have [simp]: "{y} \<inter> {x} = {}" "{x} - {y} = {x}" using `y \<noteq> x` by auto
hoelzl@38656
   276
hoelzl@38656
   277
  show ?thesis
hoelzl@38656
   278
  proof cases
hoelzl@38656
   279
    assume "{y} \<in> sets ?S"
hoelzl@38656
   280
    with `{x} \<in> sets ?S` assms show "distribution X {y} = 0"
hoelzl@38656
   281
      using S.measure_inter_full_set[of "{y}" "{x}"]
hoelzl@38656
   282
      by simp
hoelzl@38656
   283
  next
hoelzl@38656
   284
    assume "{y} \<notin> sets ?S"
hoelzl@35582
   285
    hence "X -` {y} \<inter> space M = {}" by auto
hoelzl@38656
   286
    thus "distribution X {y} = 0" unfolding distribution_def by auto
hoelzl@38656
   287
  qed
hoelzl@35582
   288
qed
hoelzl@35582
   289
hoelzl@35582
   290
end
hoelzl@35582
   291
hoelzl@35977
   292
locale finite_prob_space = prob_space + finite_measure_space
hoelzl@35977
   293
hoelzl@36624
   294
lemma finite_prob_space_eq:
hoelzl@38656
   295
  "finite_prob_space M \<mu> \<longleftrightarrow> finite_measure_space M \<mu> \<and> \<mu> (space M) = 1"
hoelzl@36624
   296
  unfolding finite_prob_space_def finite_measure_space_def prob_space_def prob_space_axioms_def
hoelzl@36624
   297
  by auto
hoelzl@36624
   298
hoelzl@36624
   299
lemma (in prob_space) not_empty: "space M \<noteq> {}"
hoelzl@36624
   300
  using prob_space empty_measure by auto
hoelzl@36624
   301
hoelzl@38656
   302
lemma (in finite_prob_space) sum_over_space_eq_1: "(\<Sum>x\<in>space M. \<mu> {x}) = 1"
hoelzl@38656
   303
  using measure_space_1 sum_over_space by simp
hoelzl@36624
   304
hoelzl@36624
   305
lemma (in finite_prob_space) positive_distribution: "0 \<le> distribution X x"
hoelzl@38656
   306
  unfolding distribution_def by simp
hoelzl@36624
   307
hoelzl@36624
   308
lemma (in finite_prob_space) joint_distribution_restriction_fst:
hoelzl@36624
   309
  "joint_distribution X Y A \<le> distribution X (fst ` A)"
hoelzl@36624
   310
  unfolding distribution_def
hoelzl@36624
   311
proof (safe intro!: measure_mono)
hoelzl@36624
   312
  fix x assume "x \<in> space M" and *: "(X x, Y x) \<in> A"
hoelzl@36624
   313
  show "x \<in> X -` fst ` A"
hoelzl@36624
   314
    by (auto intro!: image_eqI[OF _ *])
hoelzl@36624
   315
qed (simp_all add: sets_eq_Pow)
hoelzl@36624
   316
hoelzl@36624
   317
lemma (in finite_prob_space) joint_distribution_restriction_snd:
hoelzl@36624
   318
  "joint_distribution X Y A \<le> distribution Y (snd ` A)"
hoelzl@36624
   319
  unfolding distribution_def
hoelzl@36624
   320
proof (safe intro!: measure_mono)
hoelzl@36624
   321
  fix x assume "x \<in> space M" and *: "(X x, Y x) \<in> A"
hoelzl@36624
   322
  show "x \<in> Y -` snd ` A"
hoelzl@36624
   323
    by (auto intro!: image_eqI[OF _ *])
hoelzl@36624
   324
qed (simp_all add: sets_eq_Pow)
hoelzl@36624
   325
hoelzl@36624
   326
lemma (in finite_prob_space) distribution_order:
hoelzl@36624
   327
  shows "0 \<le> distribution X x'"
hoelzl@36624
   328
  and "(distribution X x' \<noteq> 0) \<longleftrightarrow> (0 < distribution X x')"
hoelzl@36624
   329
  and "r \<le> joint_distribution X Y {(x, y)} \<Longrightarrow> r \<le> distribution X {x}"
hoelzl@36624
   330
  and "r \<le> joint_distribution X Y {(x, y)} \<Longrightarrow> r \<le> distribution Y {y}"
hoelzl@36624
   331
  and "r < joint_distribution X Y {(x, y)} \<Longrightarrow> r < distribution X {x}"
hoelzl@36624
   332
  and "r < joint_distribution X Y {(x, y)} \<Longrightarrow> r < distribution Y {y}"
hoelzl@36624
   333
  and "distribution X {x} = 0 \<Longrightarrow> joint_distribution X Y {(x, y)} = 0"
hoelzl@36624
   334
  and "distribution Y {y} = 0 \<Longrightarrow> joint_distribution X Y {(x, y)} = 0"
hoelzl@36624
   335
  using positive_distribution[of X x']
hoelzl@36624
   336
    positive_distribution[of "\<lambda>x. (X x, Y x)" "{(x, y)}"]
hoelzl@36624
   337
    joint_distribution_restriction_fst[of X Y "{(x, y)}"]
hoelzl@36624
   338
    joint_distribution_restriction_snd[of X Y "{(x, y)}"]
hoelzl@36624
   339
  by auto
hoelzl@36624
   340
hoelzl@39097
   341
lemma (in finite_prob_space) distribution_mono:
hoelzl@39097
   342
  assumes "\<And>t. \<lbrakk> t \<in> space M ; X t \<in> x \<rbrakk> \<Longrightarrow> Y t \<in> y"
hoelzl@39097
   343
  shows "distribution X x \<le> distribution Y y"
hoelzl@39097
   344
  unfolding distribution_def
hoelzl@39097
   345
  using assms by (auto simp: sets_eq_Pow intro!: measure_mono)
hoelzl@39097
   346
hoelzl@39097
   347
lemma (in finite_prob_space) distribution_mono_gt_0:
hoelzl@39097
   348
  assumes gt_0: "0 < distribution X x"
hoelzl@39097
   349
  assumes *: "\<And>t. \<lbrakk> t \<in> space M ; X t \<in> x \<rbrakk> \<Longrightarrow> Y t \<in> y"
hoelzl@39097
   350
  shows "0 < distribution Y y"
hoelzl@39097
   351
  by (rule less_le_trans[OF gt_0 distribution_mono]) (rule *)
hoelzl@39097
   352
hoelzl@39097
   353
lemma (in finite_prob_space) sum_over_space_distrib:
hoelzl@39097
   354
  "(\<Sum>x\<in>X`space M. distribution X {x}) = 1"
hoelzl@39097
   355
  unfolding distribution_def measure_space_1[symmetric] using finite_space
hoelzl@39097
   356
  by (subst measure_finitely_additive'')
hoelzl@39097
   357
     (auto simp add: disjoint_family_on_def sets_eq_Pow intro!: arg_cong[where f=\<mu>])
hoelzl@39097
   358
hoelzl@39097
   359
lemma (in finite_prob_space) sum_over_space_real_distribution:
hoelzl@39097
   360
  "(\<Sum>x\<in>X`space M. real (distribution X {x})) = 1"
hoelzl@39097
   361
  unfolding distribution_def prob_space[symmetric] using finite_space
hoelzl@39097
   362
  by (subst real_finite_measure_finite_Union[symmetric])
hoelzl@39097
   363
     (auto simp add: disjoint_family_on_def sets_eq_Pow intro!: arg_cong[where f=prob])
hoelzl@39097
   364
hoelzl@39097
   365
lemma (in finite_prob_space) finite_sum_over_space_eq_1:
hoelzl@39097
   366
  "(\<Sum>x\<in>space M. real (\<mu> {x})) = 1"
hoelzl@39097
   367
  using sum_over_space_eq_1 finite_measure by (simp add: real_of_pinfreal_setsum sets_eq_Pow)
hoelzl@39097
   368
hoelzl@39097
   369
lemma (in finite_prob_space) distribution_finite:
hoelzl@39097
   370
  "distribution X A \<noteq> \<omega>"
hoelzl@39097
   371
  using finite_measure[of "X -` A \<inter> space M"]
hoelzl@39097
   372
  unfolding distribution_def sets_eq_Pow by auto
hoelzl@39097
   373
hoelzl@39097
   374
lemma (in finite_prob_space) real_distribution_gt_0[simp]:
hoelzl@39097
   375
  "0 < real (distribution Y y) \<longleftrightarrow>  0 < distribution Y y"
hoelzl@39097
   376
  using assms by (auto intro!: real_pinfreal_pos distribution_finite)
hoelzl@39097
   377
hoelzl@39097
   378
lemma (in finite_prob_space) real_distribution_mult_pos_pos:
hoelzl@39097
   379
  assumes "0 < distribution Y y"
hoelzl@39097
   380
  and "0 < distribution X x"
hoelzl@39097
   381
  shows "0 < real (distribution Y y * distribution X x)"
hoelzl@39097
   382
  unfolding real_of_pinfreal_mult[symmetric]
hoelzl@39097
   383
  using assms by (auto intro!: mult_pos_pos)
hoelzl@39097
   384
hoelzl@39097
   385
lemma (in finite_prob_space) real_distribution_divide_pos_pos:
hoelzl@39097
   386
  assumes "0 < distribution Y y"
hoelzl@39097
   387
  and "0 < distribution X x"
hoelzl@39097
   388
  shows "0 < real (distribution Y y / distribution X x)"
hoelzl@39097
   389
  unfolding divide_pinfreal_def real_of_pinfreal_mult[symmetric]
hoelzl@39097
   390
  using assms distribution_finite[of X x] by (cases "distribution X x") (auto intro!: mult_pos_pos)
hoelzl@39097
   391
hoelzl@39097
   392
lemma (in finite_prob_space) real_distribution_mult_inverse_pos_pos:
hoelzl@39097
   393
  assumes "0 < distribution Y y"
hoelzl@39097
   394
  and "0 < distribution X x"
hoelzl@39097
   395
  shows "0 < real (distribution Y y * inverse (distribution X x))"
hoelzl@39097
   396
  unfolding divide_pinfreal_def real_of_pinfreal_mult[symmetric]
hoelzl@39097
   397
  using assms distribution_finite[of X x] by (cases "distribution X x") (auto intro!: mult_pos_pos)
hoelzl@39097
   398
hoelzl@39097
   399
lemma (in prob_space) distribution_remove_const:
hoelzl@39097
   400
  shows "joint_distribution X (\<lambda>x. ()) {(x, ())} = distribution X {x}"
hoelzl@39097
   401
  and "joint_distribution (\<lambda>x. ()) X {((), x)} = distribution X {x}"
hoelzl@39097
   402
  and "joint_distribution X (\<lambda>x. (Y x, ())) {(x, y, ())} = joint_distribution X Y {(x, y)}"
hoelzl@39097
   403
  and "joint_distribution X (\<lambda>x. ((), Y x)) {(x, (), y)} = joint_distribution X Y {(x, y)}"
hoelzl@39097
   404
  and "distribution (\<lambda>x. ()) {()} = 1"
hoelzl@39097
   405
  unfolding measure_space_1[symmetric]
hoelzl@39097
   406
  by (auto intro!: arg_cong[where f="\<mu>"] simp: distribution_def)
hoelzl@35977
   407
hoelzl@39097
   408
lemma (in finite_prob_space) setsum_distribution_gen:
hoelzl@39097
   409
  assumes "Z -` {c} \<inter> space M = (\<Union>x \<in> X`space M. Y -` {f x}) \<inter> space M"
hoelzl@39097
   410
  and "inj_on f (X`space M)"
hoelzl@39097
   411
  shows "(\<Sum>x \<in> X`space M. distribution Y {f x}) = distribution Z {c}"
hoelzl@39097
   412
  unfolding distribution_def assms
hoelzl@39097
   413
  using finite_space assms
hoelzl@39097
   414
  by (subst measure_finitely_additive'')
hoelzl@39097
   415
     (auto simp add: disjoint_family_on_def sets_eq_Pow inj_on_def
hoelzl@39097
   416
      intro!: arg_cong[where f=prob])
hoelzl@39097
   417
hoelzl@39097
   418
lemma (in finite_prob_space) setsum_distribution:
hoelzl@39097
   419
  "(\<Sum>x \<in> X`space M. joint_distribution X Y {(x, y)}) = distribution Y {y}"
hoelzl@39097
   420
  "(\<Sum>y \<in> Y`space M. joint_distribution X Y {(x, y)}) = distribution X {x}"
hoelzl@39097
   421
  "(\<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
   422
  "(\<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
   423
  "(\<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
   424
  by (auto intro!: inj_onI setsum_distribution_gen)
hoelzl@39097
   425
hoelzl@39097
   426
lemma (in finite_prob_space) setsum_real_distribution_gen:
hoelzl@39097
   427
  assumes "Z -` {c} \<inter> space M = (\<Union>x \<in> X`space M. Y -` {f x}) \<inter> space M"
hoelzl@39097
   428
  and "inj_on f (X`space M)"
hoelzl@39097
   429
  shows "(\<Sum>x \<in> X`space M. real (distribution Y {f x})) = real (distribution Z {c})"
hoelzl@39097
   430
  unfolding distribution_def assms
hoelzl@39097
   431
  using finite_space assms
hoelzl@39097
   432
  by (subst real_finite_measure_finite_Union[symmetric])
hoelzl@39097
   433
     (auto simp add: disjoint_family_on_def sets_eq_Pow inj_on_def
hoelzl@39097
   434
        intro!: arg_cong[where f=prob])
hoelzl@39097
   435
hoelzl@39097
   436
lemma (in finite_prob_space) setsum_real_distribution:
hoelzl@39097
   437
  "(\<Sum>x \<in> X`space M. real (joint_distribution X Y {(x, y)})) = real (distribution Y {y})"
hoelzl@39097
   438
  "(\<Sum>y \<in> Y`space M. real (joint_distribution X Y {(x, y)})) = real (distribution X {x})"
hoelzl@39097
   439
  "(\<Sum>x \<in> X`space M. real (joint_distribution X (\<lambda>x. (Y x, Z x)) {(x, y, z)})) = real (joint_distribution Y Z {(y, z)})"
hoelzl@39097
   440
  "(\<Sum>y \<in> Y`space M. real (joint_distribution X (\<lambda>x. (Y x, Z x)) {(x, y, z)})) = real (joint_distribution X Z {(x, z)})"
hoelzl@39097
   441
  "(\<Sum>z \<in> Z`space M. real (joint_distribution X (\<lambda>x. (Y x, Z x)) {(x, y, z)})) = real (joint_distribution X Y {(x, y)})"
hoelzl@39097
   442
  by (auto intro!: inj_onI setsum_real_distribution_gen)
hoelzl@39097
   443
hoelzl@39097
   444
lemma (in finite_prob_space) real_distribution_order:
hoelzl@39097
   445
  shows "r \<le> real (joint_distribution X Y {(x, y)}) \<Longrightarrow> r \<le> real (distribution X {x})"
hoelzl@39097
   446
  and "r \<le> real (joint_distribution X Y {(x, y)}) \<Longrightarrow> r \<le> real (distribution Y {y})"
hoelzl@39097
   447
  and "r < real (joint_distribution X Y {(x, y)}) \<Longrightarrow> r < real (distribution X {x})"
hoelzl@39097
   448
  and "r < real (joint_distribution X Y {(x, y)}) \<Longrightarrow> r < real (distribution Y {y})"
hoelzl@39097
   449
  and "distribution X {x} = 0 \<Longrightarrow> real (joint_distribution X Y {(x, y)}) = 0"
hoelzl@39097
   450
  and "distribution Y {y} = 0 \<Longrightarrow> real (joint_distribution X Y {(x, y)}) = 0"
hoelzl@39097
   451
  using real_of_pinfreal_mono[OF distribution_finite joint_distribution_restriction_fst, of X Y "{(x, y)}"]
hoelzl@39097
   452
  using real_of_pinfreal_mono[OF distribution_finite joint_distribution_restriction_snd, of X Y "{(x, y)}"]
hoelzl@39097
   453
  using real_pinfreal_nonneg[of "joint_distribution X Y {(x, y)}"]
hoelzl@39097
   454
  by auto
hoelzl@39097
   455
hoelzl@39097
   456
lemma (in prob_space) joint_distribution_remove[simp]:
hoelzl@39097
   457
    "joint_distribution X X {(x, x)} = distribution X {x}"
hoelzl@39097
   458
  unfolding distribution_def by (auto intro!: arg_cong[where f="\<mu>"])
hoelzl@39097
   459
hoelzl@39097
   460
lemma (in finite_prob_space) distribution_1:
hoelzl@39097
   461
  "distribution X A \<le> 1"
hoelzl@39097
   462
  unfolding distribution_def measure_space_1[symmetric]
hoelzl@39097
   463
  by (auto intro!: measure_mono simp: sets_eq_Pow)
hoelzl@39097
   464
hoelzl@39097
   465
lemma (in finite_prob_space) real_distribution_1:
hoelzl@39097
   466
  "real (distribution X A) \<le> 1"
hoelzl@39097
   467
  unfolding real_pinfreal_1[symmetric]
hoelzl@39097
   468
  by (rule real_of_pinfreal_mono[OF _ distribution_1]) simp
hoelzl@39097
   469
hoelzl@39097
   470
lemma (in finite_prob_space) uniform_prob:
hoelzl@39097
   471
  assumes "x \<in> space M"
hoelzl@39097
   472
  assumes "\<And> x y. \<lbrakk>x \<in> space M ; y \<in> space M\<rbrakk> \<Longrightarrow> prob {x} = prob {y}"
hoelzl@39097
   473
  shows "prob {x} = 1 / real (card (space M))"
hoelzl@39097
   474
proof -
hoelzl@39097
   475
  have prob_x: "\<And> y. y \<in> space M \<Longrightarrow> prob {y} = prob {x}"
hoelzl@39097
   476
    using assms(2)[OF _ `x \<in> space M`] by blast
hoelzl@39097
   477
  have "1 = prob (space M)"
hoelzl@39097
   478
    using prob_space by auto
hoelzl@39097
   479
  also have "\<dots> = (\<Sum> x \<in> space M. prob {x})"
hoelzl@39097
   480
    using real_finite_measure_finite_Union[of "space M" "\<lambda> x. {x}", simplified]
hoelzl@39097
   481
      sets_eq_Pow inj_singleton[unfolded inj_on_def, rule_format]
hoelzl@39097
   482
      finite_space unfolding disjoint_family_on_def  prob_space[symmetric]
hoelzl@39097
   483
    by (auto simp add:setsum_restrict_set)
hoelzl@39097
   484
  also have "\<dots> = (\<Sum> y \<in> space M. prob {x})"
hoelzl@39097
   485
    using prob_x by auto
hoelzl@39097
   486
  also have "\<dots> = real_of_nat (card (space M)) * prob {x}" by simp
hoelzl@39097
   487
  finally have one: "1 = real (card (space M)) * prob {x}"
hoelzl@39097
   488
    using real_eq_of_nat by auto
hoelzl@39097
   489
  hence two: "real (card (space M)) \<noteq> 0" by fastsimp 
hoelzl@39097
   490
  from one have three: "prob {x} \<noteq> 0" by fastsimp
hoelzl@39097
   491
  thus ?thesis using one two three divide_cancel_right
hoelzl@39097
   492
    by (auto simp:field_simps)
hoelzl@39092
   493
qed
hoelzl@35977
   494
hoelzl@39092
   495
lemma (in prob_space) prob_space_subalgebra:
hoelzl@39092
   496
  assumes "N \<subseteq> sets M" "sigma_algebra (M\<lparr> sets := N \<rparr>)"
hoelzl@39092
   497
  shows "prob_space (M\<lparr> sets := N \<rparr>) \<mu>"
hoelzl@39092
   498
proof -
hoelzl@39092
   499
  interpret N: measure_space "M\<lparr> sets := N \<rparr>" \<mu>
hoelzl@39092
   500
    using measure_space_subalgebra[OF assms] .
hoelzl@39092
   501
  show ?thesis
hoelzl@39092
   502
    proof qed (simp add: measure_space_1)
hoelzl@35977
   503
qed
hoelzl@35977
   504
hoelzl@39092
   505
lemma (in prob_space) prob_space_of_restricted_space:
hoelzl@39092
   506
  assumes "\<mu> A \<noteq> 0" "\<mu> A \<noteq> \<omega>" "A \<in> sets M"
hoelzl@39092
   507
  shows "prob_space (restricted_space A) (\<lambda>S. \<mu> S / \<mu> A)"
hoelzl@39092
   508
  unfolding prob_space_def prob_space_axioms_def
hoelzl@39092
   509
proof
hoelzl@39092
   510
  show "\<mu> (space (restricted_space A)) / \<mu> A = 1"
hoelzl@39092
   511
    using `\<mu> A \<noteq> 0` `\<mu> A \<noteq> \<omega>` by (auto simp: pinfreal_noteq_omega_Ex)
hoelzl@39092
   512
  have *: "\<And>S. \<mu> S / \<mu> A = inverse (\<mu> A) * \<mu> S" by (simp add: mult_commute)
hoelzl@39092
   513
  interpret A: measure_space "restricted_space A" \<mu>
hoelzl@39092
   514
    using `A \<in> sets M` by (rule restricted_measure_space)
hoelzl@39092
   515
  show "measure_space (restricted_space A) (\<lambda>S. \<mu> S / \<mu> A)"
hoelzl@39092
   516
  proof
hoelzl@39092
   517
    show "\<mu> {} / \<mu> A = 0" by auto
hoelzl@39092
   518
    show "countably_additive (restricted_space A) (\<lambda>S. \<mu> S / \<mu> A)"
hoelzl@39092
   519
        unfolding countably_additive_def psuminf_cmult_right *
hoelzl@39092
   520
        using A.measure_countably_additive by auto
hoelzl@39092
   521
  qed
hoelzl@39092
   522
qed
hoelzl@39092
   523
hoelzl@39092
   524
lemma finite_prob_spaceI:
hoelzl@39092
   525
  assumes "finite (space M)" "sets M = Pow(space M)" "\<mu> (space M) = 1" "\<mu> {} = 0"
hoelzl@39092
   526
    and "\<And>A B. A\<subseteq>space M \<Longrightarrow> B\<subseteq>space M \<Longrightarrow> A \<inter> B = {} \<Longrightarrow> \<mu> (A \<union> B) = \<mu> A + \<mu> B"
hoelzl@39092
   527
  shows "finite_prob_space M \<mu>"
hoelzl@39092
   528
  unfolding finite_prob_space_eq
hoelzl@39092
   529
proof
hoelzl@39092
   530
  show "finite_measure_space M \<mu>" using assms
hoelzl@39092
   531
     by (auto intro!: finite_measure_spaceI)
hoelzl@39092
   532
  show "\<mu> (space M) = 1" by fact
hoelzl@39092
   533
qed
hoelzl@36624
   534
hoelzl@36624
   535
lemma (in finite_prob_space) finite_measure_space:
hoelzl@39097
   536
  fixes X :: "'a \<Rightarrow> 'x"
hoelzl@38656
   537
  shows "finite_measure_space \<lparr>space = X ` space M, sets = Pow (X ` space M)\<rparr> (distribution X)"
hoelzl@38656
   538
    (is "finite_measure_space ?S _")
hoelzl@39092
   539
proof (rule finite_measure_spaceI, simp_all)
hoelzl@36624
   540
  show "finite (X ` space M)" using finite_space by simp
hoelzl@39097
   541
next
hoelzl@39097
   542
  fix A B :: "'x set" assume "A \<inter> B = {}"
hoelzl@39097
   543
  then show "distribution X (A \<union> B) = distribution X A + distribution X B"
hoelzl@39097
   544
    unfolding distribution_def
hoelzl@39097
   545
    by (subst measure_additive)
hoelzl@39097
   546
       (auto intro!: arg_cong[where f=\<mu>] simp: sets_eq_Pow)
hoelzl@36624
   547
qed
hoelzl@36624
   548
hoelzl@39097
   549
lemma (in finite_prob_space) finite_prob_space_of_images:
hoelzl@39097
   550
  "finite_prob_space \<lparr> space = X ` space M, sets = Pow (X ` space M)\<rparr> (distribution X)"
hoelzl@39097
   551
  by (simp add: finite_prob_space_eq finite_measure_space)
hoelzl@39097
   552
hoelzl@39097
   553
lemma (in prob_space) joint_distribution_commute:
hoelzl@39097
   554
  "joint_distribution X Y x = joint_distribution Y X ((\<lambda>(x,y). (y,x))`x)"
hoelzl@39097
   555
  unfolding distribution_def by (auto intro!: arg_cong[where f=\<mu>])
hoelzl@39097
   556
hoelzl@39097
   557
lemma (in finite_prob_space) real_distribution_order':
hoelzl@39097
   558
  shows "real (distribution X {x}) = 0 \<Longrightarrow> real (joint_distribution X Y {(x, y)}) = 0"
hoelzl@39097
   559
  and "real (distribution Y {y}) = 0 \<Longrightarrow> real (joint_distribution X Y {(x, y)}) = 0"
hoelzl@39097
   560
  using real_of_pinfreal_mono[OF distribution_finite joint_distribution_restriction_fst, of X Y "{(x, y)}"]
hoelzl@39097
   561
  using real_of_pinfreal_mono[OF distribution_finite joint_distribution_restriction_snd, of X Y "{(x, y)}"]
hoelzl@39097
   562
  using real_pinfreal_nonneg[of "joint_distribution X Y {(x, y)}"]
hoelzl@39097
   563
  by auto
hoelzl@39097
   564
hoelzl@39096
   565
lemma (in finite_prob_space) finite_product_measure_space:
hoelzl@39097
   566
  fixes X :: "'a \<Rightarrow> 'x" and Y :: "'a \<Rightarrow> 'y"
hoelzl@39096
   567
  assumes "finite s1" "finite s2"
hoelzl@39096
   568
  shows "finite_measure_space \<lparr> space = s1 \<times> s2, sets = Pow (s1 \<times> s2)\<rparr> (joint_distribution X Y)"
hoelzl@39096
   569
    (is "finite_measure_space ?M ?D")
hoelzl@39097
   570
proof (rule finite_measure_spaceI, simp_all)
hoelzl@39097
   571
  show "finite (s1 \<times> s2)"
hoelzl@39096
   572
    using assms by auto
hoelzl@39097
   573
  show "joint_distribution X Y (s1\<times>s2) \<noteq> \<omega>"
hoelzl@39097
   574
    using distribution_finite .
hoelzl@39097
   575
next
hoelzl@39097
   576
  fix A B :: "('x*'y) set" assume "A \<inter> B = {}"
hoelzl@39097
   577
  then show "joint_distribution X Y (A \<union> B) = joint_distribution X Y A + joint_distribution X Y B"
hoelzl@39097
   578
    unfolding distribution_def
hoelzl@39097
   579
    by (subst measure_additive)
hoelzl@39097
   580
       (auto intro!: arg_cong[where f=\<mu>] simp: sets_eq_Pow)
hoelzl@39096
   581
qed
hoelzl@39096
   582
hoelzl@39097
   583
lemma (in finite_prob_space) finite_product_measure_space_of_images:
hoelzl@39096
   584
  shows "finite_measure_space \<lparr> space = X ` space M \<times> Y ` space M,
hoelzl@39096
   585
                                sets = Pow (X ` space M \<times> Y ` space M) \<rparr>
hoelzl@39096
   586
                              (joint_distribution X Y)"
hoelzl@39096
   587
  using finite_space by (auto intro!: finite_product_measure_space)
hoelzl@39096
   588
hoelzl@39085
   589
section "Conditional Expectation and Probability"
hoelzl@39085
   590
hoelzl@39085
   591
lemma (in prob_space) conditional_expectation_exists:
hoelzl@39083
   592
  fixes X :: "'a \<Rightarrow> pinfreal"
hoelzl@39083
   593
  assumes borel: "X \<in> borel_measurable M"
hoelzl@39083
   594
  and N_subalgebra: "N \<subseteq> sets M" "sigma_algebra (M\<lparr> sets := N \<rparr>)"
hoelzl@39083
   595
  shows "\<exists>Y\<in>borel_measurable (M\<lparr> sets := N \<rparr>). \<forall>C\<in>N.
hoelzl@39083
   596
      positive_integral (\<lambda>x. Y x * indicator C x) = positive_integral (\<lambda>x. X x * indicator C x)"
hoelzl@39083
   597
proof -
hoelzl@39083
   598
  interpret P: prob_space "M\<lparr> sets := N \<rparr>" \<mu>
hoelzl@39083
   599
    using prob_space_subalgebra[OF N_subalgebra] .
hoelzl@39083
   600
hoelzl@39083
   601
  let "?f A" = "\<lambda>x. X x * indicator A x"
hoelzl@39083
   602
  let "?Q A" = "positive_integral (?f A)"
hoelzl@39083
   603
hoelzl@39083
   604
  from measure_space_density[OF borel]
hoelzl@39083
   605
  have Q: "measure_space (M\<lparr> sets := N \<rparr>) ?Q"
hoelzl@39083
   606
    by (rule measure_space.measure_space_subalgebra[OF _ N_subalgebra])
hoelzl@39083
   607
  then interpret Q: measure_space "M\<lparr> sets := N \<rparr>" ?Q .
hoelzl@39083
   608
hoelzl@39083
   609
  have "P.absolutely_continuous ?Q"
hoelzl@39083
   610
    unfolding P.absolutely_continuous_def
hoelzl@39083
   611
  proof (safe, simp)
hoelzl@39083
   612
    fix A assume "A \<in> N" "\<mu> A = 0"
hoelzl@39083
   613
    moreover then have f_borel: "?f A \<in> borel_measurable M"
hoelzl@39083
   614
      using borel N_subalgebra by (auto intro: borel_measurable_indicator)
hoelzl@39083
   615
    moreover have "{x\<in>space M. ?f A x \<noteq> 0} = (?f A -` {0<..} \<inter> space M) \<inter> A"
hoelzl@39083
   616
      by (auto simp: indicator_def)
hoelzl@39083
   617
    moreover have "\<mu> \<dots> \<le> \<mu> A"
hoelzl@39083
   618
      using `A \<in> N` N_subalgebra f_borel
hoelzl@39083
   619
      by (auto intro!: measure_mono Int[of _ A] measurable_sets)
hoelzl@39083
   620
    ultimately show "?Q A = 0"
hoelzl@39083
   621
      by (simp add: positive_integral_0_iff)
hoelzl@39083
   622
  qed
hoelzl@39083
   623
  from P.Radon_Nikodym[OF Q this]
hoelzl@39083
   624
  obtain Y where Y: "Y \<in> borel_measurable (M\<lparr>sets := N\<rparr>)"
hoelzl@39083
   625
    "\<And>A. A \<in> sets (M\<lparr>sets:=N\<rparr>) \<Longrightarrow> ?Q A = P.positive_integral (\<lambda>x. Y x * indicator A x)"
hoelzl@39083
   626
    by blast
hoelzl@39084
   627
  with N_subalgebra show ?thesis
hoelzl@39084
   628
    by (auto intro!: bexI[OF _ Y(1)])
hoelzl@39083
   629
qed
hoelzl@39083
   630
hoelzl@39085
   631
definition (in prob_space)
hoelzl@39085
   632
  "conditional_expectation N X = (SOME Y. Y\<in>borel_measurable (M\<lparr>sets:=N\<rparr>)
hoelzl@39085
   633
    \<and> (\<forall>C\<in>N. positive_integral (\<lambda>x. Y x * indicator C x) = positive_integral (\<lambda>x. X x * indicator C x)))"
hoelzl@39085
   634
hoelzl@39085
   635
abbreviation (in prob_space)
hoelzl@39092
   636
  "conditional_prob N A \<equiv> conditional_expectation N (indicator A)"
hoelzl@39085
   637
hoelzl@39085
   638
lemma (in prob_space)
hoelzl@39085
   639
  fixes X :: "'a \<Rightarrow> pinfreal"
hoelzl@39085
   640
  assumes borel: "X \<in> borel_measurable M"
hoelzl@39085
   641
  and N_subalgebra: "N \<subseteq> sets M" "sigma_algebra (M\<lparr> sets := N \<rparr>)"
hoelzl@39085
   642
  shows borel_measurable_conditional_expectation:
hoelzl@39085
   643
    "conditional_expectation N X \<in> borel_measurable (M\<lparr> sets := N \<rparr>)"
hoelzl@39085
   644
  and conditional_expectation: "\<And>C. C \<in> N \<Longrightarrow>
hoelzl@39085
   645
      positive_integral (\<lambda>x. conditional_expectation N X x * indicator C x) =
hoelzl@39085
   646
      positive_integral (\<lambda>x. X x * indicator C x)"
hoelzl@39085
   647
   (is "\<And>C. C \<in> N \<Longrightarrow> ?eq C")
hoelzl@39085
   648
proof -
hoelzl@39085
   649
  note CE = conditional_expectation_exists[OF assms, unfolded Bex_def]
hoelzl@39085
   650
  then show "conditional_expectation N X \<in> borel_measurable (M\<lparr> sets := N \<rparr>)"
hoelzl@39085
   651
    unfolding conditional_expectation_def by (rule someI2_ex) blast
hoelzl@39085
   652
hoelzl@39085
   653
  from CE show "\<And>C. C\<in>N \<Longrightarrow> ?eq C"
hoelzl@39085
   654
    unfolding conditional_expectation_def by (rule someI2_ex) blast
hoelzl@39085
   655
qed
hoelzl@39085
   656
hoelzl@39091
   657
lemma (in sigma_algebra) factorize_measurable_function:
hoelzl@39091
   658
  fixes Z :: "'a \<Rightarrow> pinfreal" and Y :: "'a \<Rightarrow> 'c"
hoelzl@39091
   659
  assumes "sigma_algebra M'" and "Y \<in> measurable M M'" "Z \<in> borel_measurable M"
hoelzl@39091
   660
  shows "Z \<in> borel_measurable (sigma_algebra.vimage_algebra M' (space M) Y)
hoelzl@39091
   661
    \<longleftrightarrow> (\<exists>g\<in>borel_measurable M'. \<forall>x\<in>space M. Z x = g (Y x))"
hoelzl@39091
   662
proof safe
hoelzl@39091
   663
  interpret M': sigma_algebra M' by fact
hoelzl@39091
   664
  have Y: "Y \<in> space M \<rightarrow> space M'" using assms unfolding measurable_def by auto
hoelzl@39091
   665
  from M'.sigma_algebra_vimage[OF this]
hoelzl@39091
   666
  interpret va: sigma_algebra "M'.vimage_algebra (space M) Y" .
hoelzl@39091
   667
hoelzl@39091
   668
  { fix g :: "'c \<Rightarrow> pinfreal" assume "g \<in> borel_measurable M'"
hoelzl@39091
   669
    with M'.measurable_vimage_algebra[OF Y]
hoelzl@39091
   670
    have "g \<circ> Y \<in> borel_measurable (M'.vimage_algebra (space M) Y)"
hoelzl@39091
   671
      by (rule measurable_comp)
hoelzl@39091
   672
    moreover assume "\<forall>x\<in>space M. Z x = g (Y x)"
hoelzl@39091
   673
    then have "Z \<in> borel_measurable (M'.vimage_algebra (space M) Y) \<longleftrightarrow>
hoelzl@39091
   674
       g \<circ> Y \<in> borel_measurable (M'.vimage_algebra (space M) Y)"
hoelzl@39091
   675
       by (auto intro!: measurable_cong)
hoelzl@39091
   676
    ultimately show "Z \<in> borel_measurable (M'.vimage_algebra (space M) Y)"
hoelzl@39091
   677
      by simp }
hoelzl@39091
   678
hoelzl@39091
   679
  assume "Z \<in> borel_measurable (M'.vimage_algebra (space M) Y)"
hoelzl@39091
   680
  from va.borel_measurable_implies_simple_function_sequence[OF this]
hoelzl@39091
   681
  obtain f where f: "\<And>i. va.simple_function (f i)" and "f \<up> Z" by blast
hoelzl@39091
   682
hoelzl@39091
   683
  have "\<forall>i. \<exists>g. M'.simple_function g \<and> (\<forall>x\<in>space M. f i x = g (Y x))"
hoelzl@39091
   684
  proof
hoelzl@39091
   685
    fix i
hoelzl@39091
   686
    from f[of i] have "finite (f i`space M)" and B_ex:
hoelzl@39091
   687
      "\<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@39091
   688
      unfolding va.simple_function_def by auto
hoelzl@39091
   689
    from B_ex[THEN bchoice] guess B .. note B = this
hoelzl@39091
   690
hoelzl@39091
   691
    let ?g = "\<lambda>x. \<Sum>z\<in>f i`space M. z * indicator (B z) x"
hoelzl@39091
   692
hoelzl@39091
   693
    show "\<exists>g. M'.simple_function g \<and> (\<forall>x\<in>space M. f i x = g (Y x))"
hoelzl@39091
   694
    proof (intro exI[of _ ?g] conjI ballI)
hoelzl@39091
   695
      show "M'.simple_function ?g" using B by auto
hoelzl@39091
   696
hoelzl@39091
   697
      fix x assume "x \<in> space M"
hoelzl@39091
   698
      then have "\<And>z. z \<in> f i`space M \<Longrightarrow> indicator (B z) (Y x) = (indicator (f i -` {z} \<inter> space M) x::pinfreal)"
hoelzl@39091
   699
        unfolding indicator_def using B by auto
hoelzl@39091
   700
      then show "f i x = ?g (Y x)" using `x \<in> space M` f[of i]
hoelzl@39091
   701
        by (subst va.simple_function_indicator_representation) auto
hoelzl@39091
   702
    qed
hoelzl@39091
   703
  qed
hoelzl@39091
   704
  from choice[OF this] guess g .. note g = this
hoelzl@39091
   705
hoelzl@39091
   706
  show "\<exists>g\<in>borel_measurable M'. \<forall>x\<in>space M. Z x = g (Y x)"
hoelzl@39091
   707
  proof (intro ballI bexI)
hoelzl@39091
   708
    show "(SUP i. g i) \<in> borel_measurable M'"
hoelzl@39091
   709
      using g by (auto intro: M'.borel_measurable_simple_function)
hoelzl@39091
   710
    fix x assume "x \<in> space M"
hoelzl@39091
   711
    have "Z x = (SUP i. f i) x" using `f \<up> Z` unfolding isoton_def by simp
hoelzl@39091
   712
    also have "\<dots> = (SUP i. g i) (Y x)" unfolding SUPR_fun_expand
hoelzl@39091
   713
      using g `x \<in> space M` by simp
hoelzl@39091
   714
    finally show "Z x = (SUP i. g i) (Y x)" .
hoelzl@39091
   715
  qed
hoelzl@39091
   716
qed
hoelzl@39090
   717
hoelzl@35582
   718
end