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