src/HOL/Probability/Probability_Measure.thy
author hoelzl
Thu Jun 09 13:55:11 2011 +0200 (2011-06-09)
changeset 43339 9ba256ad6781
parent 42991 3fa22920bf86
child 43340 60e181c4eae4
permissions -rw-r--r--
jensens inequality
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@41981
    26
  "distribution X A = \<mu>' (X -` A \<inter> space M)"
hoelzl@35582
    27
hoelzl@40859
    28
abbreviation (in prob_space)
hoelzl@36624
    29
  "joint_distribution X Y \<equiv> distribution (\<lambda>x. (X x, Y x))"
hoelzl@35582
    30
hoelzl@43339
    31
lemma (in prob_space) prob_space_cong:
hoelzl@43339
    32
  assumes "\<And>A. A \<in> sets M \<Longrightarrow> measure N A = \<mu> A" "space N = space M" "sets N = sets M"
hoelzl@43339
    33
  shows "prob_space N"
hoelzl@43339
    34
proof -
hoelzl@43339
    35
  interpret N: measure_space N by (intro measure_space_cong assms)
hoelzl@43339
    36
  show ?thesis by default (insert assms measure_space_1, simp)
hoelzl@43339
    37
qed
hoelzl@43339
    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@43339
    65
lemma (in prob_space) not_zero_less_distribution[simp]:
hoelzl@43339
    66
  "(\<not> 0 < distribution X A) \<longleftrightarrow> distribution X A = 0"
hoelzl@43339
    67
  using distribution_positive[of X A] by arith
hoelzl@43339
    68
hoelzl@41981
    69
lemma (in prob_space) joint_distribution_remove[simp]:
hoelzl@41981
    70
    "joint_distribution X X {(x, x)} = distribution X {x}"
hoelzl@41981
    71
  unfolding distribution_def by (auto intro!: arg_cong[where f=\<mu>'])
hoelzl@41981
    72
hoelzl@43339
    73
lemma (in prob_space) not_empty: "space M \<noteq> {}"
hoelzl@43339
    74
  using prob_space empty_measure' by auto
hoelzl@43339
    75
hoelzl@42950
    76
lemma (in prob_space) measure_le_1: "X \<in> sets M \<Longrightarrow> \<mu> X \<le> 1"
hoelzl@42950
    77
  unfolding measure_space_1[symmetric]
hoelzl@42950
    78
  using sets_into_space
hoelzl@42950
    79
  by (intro measure_mono) auto
hoelzl@42950
    80
hoelzl@43339
    81
lemma (in prob_space) AE_I_eq_1:
hoelzl@43339
    82
  assumes "\<mu> {x\<in>space M. P x} = 1" "{x\<in>space M. P x} \<in> sets M"
hoelzl@43339
    83
  shows "AE x. P x"
hoelzl@43339
    84
proof (rule AE_I)
hoelzl@43339
    85
  show "\<mu> (space M - {x \<in> space M. P x}) = 0"
hoelzl@43339
    86
    using assms measure_space_1 by (simp add: measure_compl)
hoelzl@43339
    87
qed (insert assms, auto)
hoelzl@43339
    88
hoelzl@41981
    89
lemma (in prob_space) distribution_1:
hoelzl@41981
    90
  "distribution X A \<le> 1"
hoelzl@41981
    91
  unfolding distribution_def by simp
hoelzl@35582
    92
hoelzl@40859
    93
lemma (in prob_space) prob_compl:
hoelzl@41981
    94
  assumes A: "A \<in> events"
hoelzl@38656
    95
  shows "prob (space M - A) = 1 - prob A"
hoelzl@41981
    96
  using finite_measure_compl[OF A] by (simp add: prob_space)
hoelzl@35582
    97
hoelzl@40859
    98
lemma (in prob_space) prob_space_increasing: "increasing M prob"
hoelzl@41981
    99
  by (auto intro!: finite_measure_mono simp: increasing_def)
hoelzl@35582
   100
hoelzl@40859
   101
lemma (in prob_space) prob_zero_union:
hoelzl@35582
   102
  assumes "s \<in> events" "t \<in> events" "prob t = 0"
hoelzl@35582
   103
  shows "prob (s \<union> t) = prob s"
hoelzl@38656
   104
using assms
hoelzl@35582
   105
proof -
hoelzl@35582
   106
  have "prob (s \<union> t) \<le> prob s"
hoelzl@41981
   107
    using finite_measure_subadditive[of s t] assms by auto
hoelzl@35582
   108
  moreover have "prob (s \<union> t) \<ge> prob s"
hoelzl@41981
   109
    using assms by (blast intro: finite_measure_mono)
hoelzl@35582
   110
  ultimately show ?thesis by simp
hoelzl@35582
   111
qed
hoelzl@35582
   112
hoelzl@40859
   113
lemma (in prob_space) prob_eq_compl:
hoelzl@35582
   114
  assumes "s \<in> events" "t \<in> events"
hoelzl@35582
   115
  assumes "prob (space M - s) = prob (space M - t)"
hoelzl@35582
   116
  shows "prob s = prob t"
hoelzl@38656
   117
  using assms prob_compl by auto
hoelzl@35582
   118
hoelzl@40859
   119
lemma (in prob_space) prob_one_inter:
hoelzl@35582
   120
  assumes events:"s \<in> events" "t \<in> events"
hoelzl@35582
   121
  assumes "prob t = 1"
hoelzl@35582
   122
  shows "prob (s \<inter> t) = prob s"
hoelzl@35582
   123
proof -
hoelzl@38656
   124
  have "prob ((space M - s) \<union> (space M - t)) = prob (space M - s)"
hoelzl@38656
   125
    using events assms  prob_compl[of "t"] by (auto intro!: prob_zero_union)
hoelzl@38656
   126
  also have "(space M - s) \<union> (space M - t) = space M - (s \<inter> t)"
hoelzl@38656
   127
    by blast
hoelzl@38656
   128
  finally show "prob (s \<inter> t) = prob s"
hoelzl@38656
   129
    using events by (auto intro!: prob_eq_compl[of "s \<inter> t" s])
hoelzl@35582
   130
qed
hoelzl@35582
   131
hoelzl@40859
   132
lemma (in prob_space) prob_eq_bigunion_image:
hoelzl@35582
   133
  assumes "range f \<subseteq> events" "range g \<subseteq> events"
hoelzl@35582
   134
  assumes "disjoint_family f" "disjoint_family g"
hoelzl@35582
   135
  assumes "\<And> n :: nat. prob (f n) = prob (g n)"
hoelzl@35582
   136
  shows "(prob (\<Union> i. f i) = prob (\<Union> i. g i))"
hoelzl@35582
   137
using assms
hoelzl@35582
   138
proof -
hoelzl@38656
   139
  have a: "(\<lambda> i. prob (f i)) sums (prob (\<Union> i. f i))"
hoelzl@41981
   140
    by (rule finite_measure_UNION[OF assms(1,3)])
hoelzl@38656
   141
  have b: "(\<lambda> i. prob (g i)) sums (prob (\<Union> i. g i))"
hoelzl@41981
   142
    by (rule finite_measure_UNION[OF assms(2,4)])
hoelzl@38656
   143
  show ?thesis using sums_unique[OF b] sums_unique[OF a] assms by simp
hoelzl@35582
   144
qed
hoelzl@35582
   145
hoelzl@40859
   146
lemma (in prob_space) prob_countably_zero:
hoelzl@35582
   147
  assumes "range c \<subseteq> events"
hoelzl@35582
   148
  assumes "\<And> i. prob (c i) = 0"
hoelzl@38656
   149
  shows "prob (\<Union> i :: nat. c i) = 0"
hoelzl@38656
   150
proof (rule antisym)
hoelzl@38656
   151
  show "prob (\<Union> i :: nat. c i) \<le> 0"
hoelzl@41981
   152
    using finite_measure_countably_subadditive[OF assms(1)]
hoelzl@38656
   153
    by (simp add: assms(2) suminf_zero summable_zero)
hoelzl@41981
   154
qed simp
hoelzl@35582
   155
hoelzl@40859
   156
lemma (in prob_space) prob_equiprobable_finite_unions:
hoelzl@38656
   157
  assumes "s \<in> events"
hoelzl@38656
   158
  assumes s_finite: "finite s" "\<And>x. x \<in> s \<Longrightarrow> {x} \<in> events"
hoelzl@35582
   159
  assumes "\<And> x y. \<lbrakk>x \<in> s; y \<in> s\<rbrakk> \<Longrightarrow> (prob {x} = prob {y})"
hoelzl@38656
   160
  shows "prob s = real (card s) * prob {SOME x. x \<in> s}"
hoelzl@35582
   161
proof (cases "s = {}")
hoelzl@38656
   162
  case False hence "\<exists> x. x \<in> s" by blast
hoelzl@35582
   163
  from someI_ex[OF this] assms
hoelzl@35582
   164
  have prob_some: "\<And> x. x \<in> s \<Longrightarrow> prob {x} = prob {SOME y. y \<in> s}" by blast
hoelzl@35582
   165
  have "prob s = (\<Sum> x \<in> s. prob {x})"
hoelzl@41981
   166
    using finite_measure_finite_singleton[OF s_finite] by simp
hoelzl@35582
   167
  also have "\<dots> = (\<Sum> x \<in> s. prob {SOME y. y \<in> s})" using prob_some by auto
hoelzl@38656
   168
  also have "\<dots> = real (card s) * prob {(SOME x. x \<in> s)}"
hoelzl@38656
   169
    using setsum_constant assms by (simp add: real_eq_of_nat)
hoelzl@35582
   170
  finally show ?thesis by simp
hoelzl@38656
   171
qed simp
hoelzl@35582
   172
hoelzl@40859
   173
lemma (in prob_space) prob_real_sum_image_fn:
hoelzl@35582
   174
  assumes "e \<in> events"
hoelzl@35582
   175
  assumes "\<And> x. x \<in> s \<Longrightarrow> e \<inter> f x \<in> events"
hoelzl@35582
   176
  assumes "finite s"
hoelzl@38656
   177
  assumes disjoint: "\<And> x y. \<lbrakk>x \<in> s ; y \<in> s ; x \<noteq> y\<rbrakk> \<Longrightarrow> f x \<inter> f y = {}"
hoelzl@38656
   178
  assumes upper: "space M \<subseteq> (\<Union> i \<in> s. f i)"
hoelzl@35582
   179
  shows "prob e = (\<Sum> x \<in> s. prob (e \<inter> f x))"
hoelzl@35582
   180
proof -
hoelzl@38656
   181
  have e: "e = (\<Union> i \<in> s. e \<inter> f i)"
hoelzl@38656
   182
    using `e \<in> events` sets_into_space upper by blast
hoelzl@38656
   183
  hence "prob e = prob (\<Union> i \<in> s. e \<inter> f i)" by simp
hoelzl@38656
   184
  also have "\<dots> = (\<Sum> x \<in> s. prob (e \<inter> f x))"
hoelzl@41981
   185
  proof (rule finite_measure_finite_Union)
hoelzl@38656
   186
    show "finite s" by fact
hoelzl@38656
   187
    show "\<And>i. i \<in> s \<Longrightarrow> e \<inter> f i \<in> events" by fact
hoelzl@38656
   188
    show "disjoint_family_on (\<lambda>i. e \<inter> f i) s"
hoelzl@38656
   189
      using disjoint by (auto simp: disjoint_family_on_def)
hoelzl@38656
   190
  qed
hoelzl@38656
   191
  finally show ?thesis .
hoelzl@35582
   192
qed
hoelzl@35582
   193
hoelzl@42199
   194
lemma (in prob_space) prob_space_vimage:
hoelzl@42199
   195
  assumes S: "sigma_algebra S"
hoelzl@42199
   196
  assumes T: "T \<in> measure_preserving M S"
hoelzl@42199
   197
  shows "prob_space S"
hoelzl@35582
   198
proof -
hoelzl@42199
   199
  interpret S: measure_space S
hoelzl@42199
   200
    using S and T by (rule measure_space_vimage)
hoelzl@38656
   201
  show ?thesis
hoelzl@42199
   202
  proof
hoelzl@42199
   203
    from T[THEN measure_preservingD2]
hoelzl@42199
   204
    have "T -` space S \<inter> space M = space M"
hoelzl@42199
   205
      by (auto simp: measurable_def)
hoelzl@42199
   206
    with T[THEN measure_preservingD, of "space S", symmetric]
hoelzl@42199
   207
    show  "measure S (space S) = 1"
hoelzl@42199
   208
      using measure_space_1 by simp
hoelzl@35582
   209
  qed
hoelzl@35582
   210
qed
hoelzl@35582
   211
hoelzl@42988
   212
lemma prob_space_unique_Int_stable:
hoelzl@42988
   213
  fixes E :: "('a, 'b) algebra_scheme" and A :: "nat \<Rightarrow> 'a set"
hoelzl@42988
   214
  assumes E: "Int_stable E" "space E \<in> sets E"
hoelzl@42988
   215
  and M: "prob_space M" "space M = space E" "sets M = sets (sigma E)"
hoelzl@42988
   216
  and N: "prob_space N" "space N = space E" "sets N = sets (sigma E)"
hoelzl@42988
   217
  and eq: "\<And>X. X \<in> sets E \<Longrightarrow> finite_measure.\<mu>' M X = finite_measure.\<mu>' N X"
hoelzl@42988
   218
  assumes "X \<in> sets (sigma E)"
hoelzl@42988
   219
  shows "finite_measure.\<mu>' M X = finite_measure.\<mu>' N X"
hoelzl@42988
   220
proof -
hoelzl@42988
   221
  interpret M!: prob_space M by fact
hoelzl@42988
   222
  interpret N!: prob_space N by fact
hoelzl@42988
   223
  have "measure M X = measure N X"
hoelzl@42988
   224
  proof (rule measure_unique_Int_stable[OF `Int_stable E`])
hoelzl@42988
   225
    show "range (\<lambda>i. space M) \<subseteq> sets E" "incseq (\<lambda>i. space M)" "(\<Union>i. space M) = space E"
hoelzl@42988
   226
      using E M N by auto
hoelzl@42988
   227
    show "\<And>i. M.\<mu> (space M) \<noteq> \<infinity>"
hoelzl@42988
   228
      using M.measure_space_1 by simp
hoelzl@42988
   229
    show "measure_space \<lparr>space = space E, sets = sets (sigma E), measure_space.measure = M.\<mu>\<rparr>"
hoelzl@42988
   230
      using E M N by (auto intro!: M.measure_space_cong)
hoelzl@42988
   231
    show "measure_space \<lparr>space = space E, sets = sets (sigma E), measure_space.measure = N.\<mu>\<rparr>"
hoelzl@42988
   232
      using E M N by (auto intro!: N.measure_space_cong)
hoelzl@42988
   233
    { fix X assume "X \<in> sets E"
hoelzl@42988
   234
      then have "X \<in> sets (sigma E)"
hoelzl@42988
   235
        by (auto simp: sets_sigma sigma_sets.Basic)
hoelzl@42988
   236
      with eq[OF `X \<in> sets E`] M N show "M.\<mu> X = N.\<mu> X"
hoelzl@42988
   237
        by (simp add: M.finite_measure_eq N.finite_measure_eq) }
hoelzl@42988
   238
  qed fact
hoelzl@42988
   239
  with `X \<in> sets (sigma E)` M N show ?thesis
hoelzl@42988
   240
    by (simp add: M.finite_measure_eq N.finite_measure_eq)
hoelzl@42988
   241
qed
hoelzl@42988
   242
hoelzl@42199
   243
lemma (in prob_space) distribution_prob_space:
hoelzl@42199
   244
  assumes X: "random_variable S X"
hoelzl@42199
   245
  shows "prob_space (S\<lparr>measure := extreal \<circ> distribution X\<rparr>)" (is "prob_space ?S")
hoelzl@42199
   246
proof (rule prob_space_vimage)
hoelzl@42199
   247
  show "X \<in> measure_preserving M ?S"
hoelzl@42199
   248
    using X
hoelzl@42199
   249
    unfolding measure_preserving_def distribution_def_raw
hoelzl@42199
   250
    by (auto simp: finite_measure_eq measurable_sets)
hoelzl@42199
   251
  show "sigma_algebra ?S" using X by simp
hoelzl@42199
   252
qed
hoelzl@42199
   253
hoelzl@40859
   254
lemma (in prob_space) AE_distribution:
hoelzl@41981
   255
  assumes X: "random_variable MX X" and "AE x in MX\<lparr>measure := extreal \<circ> distribution X\<rparr>. Q x"
hoelzl@40859
   256
  shows "AE x. Q (X x)"
hoelzl@40859
   257
proof -
hoelzl@41981
   258
  interpret X: prob_space "MX\<lparr>measure := extreal \<circ> distribution X\<rparr>" using X by (rule distribution_prob_space)
hoelzl@40859
   259
  obtain N where N: "N \<in> sets MX" "distribution X N = 0" "{x\<in>space MX. \<not> Q x} \<subseteq> N"
hoelzl@40859
   260
    using assms unfolding X.almost_everywhere_def by auto
hoelzl@41981
   261
  from X[unfolded measurable_def] N show "AE x. Q (X x)"
hoelzl@41981
   262
    by (intro AE_I'[where N="X -` N \<inter> space M"])
hoelzl@41981
   263
       (auto simp: finite_measure_eq distribution_def measurable_sets)
hoelzl@40859
   264
qed
hoelzl@40859
   265
hoelzl@41981
   266
lemma (in prob_space) distribution_eq_integral:
hoelzl@41981
   267
  "random_variable S X \<Longrightarrow> A \<in> sets S \<Longrightarrow> distribution X A = expectation (indicator (X -` A \<inter> space M))"
hoelzl@41981
   268
  using finite_measure_eq[of "X -` A \<inter> space M"]
hoelzl@41981
   269
  by (auto simp: measurable_sets distribution_def)
hoelzl@35582
   270
hoelzl@43339
   271
lemma (in prob_space) expectation_less:
hoelzl@43339
   272
  assumes [simp]: "integrable M X"
hoelzl@43339
   273
  assumes gt: "\<forall>x\<in>space M. X x < b"
hoelzl@43339
   274
  shows "expectation X < b"
hoelzl@43339
   275
proof -
hoelzl@43339
   276
  have "expectation X < expectation (\<lambda>x. b)"
hoelzl@43339
   277
    using gt measure_space_1
hoelzl@43339
   278
    by (intro integral_less_AE) auto
hoelzl@43339
   279
  then show ?thesis using prob_space by simp
hoelzl@43339
   280
qed
hoelzl@43339
   281
hoelzl@43339
   282
lemma (in prob_space) expectation_greater:
hoelzl@43339
   283
  assumes [simp]: "integrable M X"
hoelzl@43339
   284
  assumes gt: "\<forall>x\<in>space M. a < X x"
hoelzl@43339
   285
  shows "a < expectation X"
hoelzl@43339
   286
proof -
hoelzl@43339
   287
  have "expectation (\<lambda>x. a) < expectation X"
hoelzl@43339
   288
    using gt measure_space_1
hoelzl@43339
   289
    by (intro integral_less_AE) auto
hoelzl@43339
   290
  then show ?thesis using prob_space by simp
hoelzl@43339
   291
qed
hoelzl@43339
   292
hoelzl@43339
   293
lemma convex_le_Inf_differential:
hoelzl@43339
   294
  fixes f :: "real \<Rightarrow> real"
hoelzl@43339
   295
  assumes "convex_on I f"
hoelzl@43339
   296
  assumes "x \<in> interior I" "y \<in> I"
hoelzl@43339
   297
  shows "f y \<ge> f x + Inf ((\<lambda>t. (f x - f t) / (x - t)) ` ({x<..} \<inter> I)) * (y - x)"
hoelzl@43339
   298
    (is "_ \<ge> _ + Inf (?F x) * (y - x)")
hoelzl@43339
   299
proof -
hoelzl@43339
   300
  show ?thesis
hoelzl@43339
   301
  proof (cases rule: linorder_cases)
hoelzl@43339
   302
    assume "x < y"
hoelzl@43339
   303
    moreover
hoelzl@43339
   304
    have "open (interior I)" by auto
hoelzl@43339
   305
    from openE[OF this `x \<in> interior I`] guess e . note e = this
hoelzl@43339
   306
    moreover def t \<equiv> "min (x + e / 2) ((x + y) / 2)"
hoelzl@43339
   307
    ultimately have "x < t" "t < y" "t \<in> ball x e"
hoelzl@43339
   308
      by (auto simp: mem_ball dist_real_def field_simps split: split_min)
hoelzl@43339
   309
    with `x \<in> interior I` e interior_subset[of I] have "t \<in> I" "x \<in> I" by auto
hoelzl@43339
   310
hoelzl@43339
   311
    have "open (interior I)" by auto
hoelzl@43339
   312
    from openE[OF this `x \<in> interior I`] guess e .
hoelzl@43339
   313
    moreover def K \<equiv> "x - e / 2"
hoelzl@43339
   314
    with `0 < e` have "K \<in> ball x e" "K < x" by (auto simp: mem_ball dist_real_def)
hoelzl@43339
   315
    ultimately have "K \<in> I" "K < x" "x \<in> I"
hoelzl@43339
   316
      using interior_subset[of I] `x \<in> interior I` by auto
hoelzl@43339
   317
hoelzl@43339
   318
    have "Inf (?F x) \<le> (f x - f y) / (x - y)"
hoelzl@43339
   319
    proof (rule Inf_lower2)
hoelzl@43339
   320
      show "(f x - f t) / (x - t) \<in> ?F x"
hoelzl@43339
   321
        using `t \<in> I` `x < t` by auto
hoelzl@43339
   322
      show "(f x - f t) / (x - t) \<le> (f x - f y) / (x - y)"
hoelzl@43339
   323
        using `convex_on I f` `x \<in> I` `y \<in> I` `x < t` `t < y` by (rule convex_on_diff)
hoelzl@43339
   324
    next
hoelzl@43339
   325
      fix y assume "y \<in> ?F x"
hoelzl@43339
   326
      with order_trans[OF convex_on_diff[OF `convex_on I f` `K \<in> I` _ `K < x` _]]
hoelzl@43339
   327
      show "(f K - f x) / (K - x) \<le> y" by auto
hoelzl@43339
   328
    qed
hoelzl@43339
   329
    then show ?thesis
hoelzl@43339
   330
      using `x < y` by (simp add: field_simps)
hoelzl@43339
   331
  next
hoelzl@43339
   332
    assume "y < x"
hoelzl@43339
   333
    moreover
hoelzl@43339
   334
    have "open (interior I)" by auto
hoelzl@43339
   335
    from openE[OF this `x \<in> interior I`] guess e . note e = this
hoelzl@43339
   336
    moreover def t \<equiv> "x + e / 2"
hoelzl@43339
   337
    ultimately have "x < t" "t \<in> ball x e"
hoelzl@43339
   338
      by (auto simp: mem_ball dist_real_def field_simps)
hoelzl@43339
   339
    with `x \<in> interior I` e interior_subset[of I] have "t \<in> I" "x \<in> I" by auto
hoelzl@43339
   340
hoelzl@43339
   341
    have "(f x - f y) / (x - y) \<le> Inf (?F x)"
hoelzl@43339
   342
    proof (rule Inf_greatest)
hoelzl@43339
   343
      have "(f x - f y) / (x - y) = (f y - f x) / (y - x)"
hoelzl@43339
   344
        using `y < x` by (auto simp: field_simps)
hoelzl@43339
   345
      also
hoelzl@43339
   346
      fix z  assume "z \<in> ?F x"
hoelzl@43339
   347
      with order_trans[OF convex_on_diff[OF `convex_on I f` `y \<in> I` _ `y < x`]]
hoelzl@43339
   348
      have "(f y - f x) / (y - x) \<le> z" by auto
hoelzl@43339
   349
      finally show "(f x - f y) / (x - y) \<le> z" .
hoelzl@43339
   350
    next
hoelzl@43339
   351
      have "open (interior I)" by auto
hoelzl@43339
   352
      from openE[OF this `x \<in> interior I`] guess e . note e = this
hoelzl@43339
   353
      then have "x + e / 2 \<in> ball x e" by (auto simp: mem_ball dist_real_def)
hoelzl@43339
   354
      with e interior_subset[of I] have "x + e / 2 \<in> {x<..} \<inter> I" by auto
hoelzl@43339
   355
      then show "?F x \<noteq> {}" by blast
hoelzl@43339
   356
    qed
hoelzl@43339
   357
    then show ?thesis
hoelzl@43339
   358
      using `y < x` by (simp add: field_simps)
hoelzl@43339
   359
  qed simp
hoelzl@43339
   360
qed
hoelzl@43339
   361
hoelzl@43339
   362
lemma (in prob_space) jensens_inequality:
hoelzl@43339
   363
  fixes a b :: real
hoelzl@43339
   364
  assumes X: "integrable M X" "X ` space M \<subseteq> I"
hoelzl@43339
   365
  assumes I: "I = {a <..< b} \<or> I = {a <..} \<or> I = {..< b} \<or> I = UNIV"
hoelzl@43339
   366
  assumes q: "integrable M (\<lambda>x. q (X x))" "convex_on I q"
hoelzl@43339
   367
  shows "q (expectation X) \<le> expectation (\<lambda>x. q (X x))"
hoelzl@43339
   368
proof -
hoelzl@43339
   369
  let "?F x" = "Inf ((\<lambda>t. (q x - q t) / (x - t)) ` ({x<..} \<inter> I))"
hoelzl@43339
   370
  from not_empty X(2) have "I \<noteq> {}" by auto
hoelzl@43339
   371
hoelzl@43339
   372
  from I have "open I" by auto
hoelzl@43339
   373
hoelzl@43339
   374
  note I
hoelzl@43339
   375
  moreover
hoelzl@43339
   376
  { assume "I \<subseteq> {a <..}"
hoelzl@43339
   377
    with X have "a < expectation X"
hoelzl@43339
   378
      by (intro expectation_greater) auto }
hoelzl@43339
   379
  moreover
hoelzl@43339
   380
  { assume "I \<subseteq> {..< b}"
hoelzl@43339
   381
    with X have "expectation X < b"
hoelzl@43339
   382
      by (intro expectation_less) auto }
hoelzl@43339
   383
  ultimately have "expectation X \<in> I"
hoelzl@43339
   384
    by (elim disjE)  (auto simp: subset_eq)
hoelzl@43339
   385
  moreover
hoelzl@43339
   386
  { fix y assume y: "y \<in> I"
hoelzl@43339
   387
    with q(2) `open I` have "Sup ((\<lambda>x. q x + ?F x * (y - x)) ` I) = q y"
hoelzl@43339
   388
      by (auto intro!: Sup_eq_maximum convex_le_Inf_differential image_eqI[OF _ y] simp: interior_open) }
hoelzl@43339
   389
  ultimately have "q (expectation X) = Sup ((\<lambda>x. q x + ?F x * (expectation X - x)) ` I)"
hoelzl@43339
   390
    by simp
hoelzl@43339
   391
  also have "\<dots> \<le> expectation (\<lambda>w. q (X w))"
hoelzl@43339
   392
  proof (rule Sup_least)
hoelzl@43339
   393
    show "(\<lambda>x. q x + ?F x * (expectation X - x)) ` I \<noteq> {}"
hoelzl@43339
   394
      using `I \<noteq> {}` by auto
hoelzl@43339
   395
  next
hoelzl@43339
   396
    fix k assume "k \<in> (\<lambda>x. q x + ?F x * (expectation X - x)) ` I"
hoelzl@43339
   397
    then guess x .. note x = this
hoelzl@43339
   398
    have "q x + ?F x * (expectation X  - x) = expectation (\<lambda>w. q x + ?F x * (X w - x))"
hoelzl@43339
   399
      using prob_space
hoelzl@43339
   400
      by (simp add: integral_add integral_cmult integral_diff lebesgue_integral_const X)
hoelzl@43339
   401
    also have "\<dots> \<le> expectation (\<lambda>w. q (X w))"
hoelzl@43339
   402
      using `x \<in> I` `open I` X(2)
hoelzl@43339
   403
      by (intro integral_mono integral_add integral_cmult integral_diff
hoelzl@43339
   404
                lebesgue_integral_const X q convex_le_Inf_differential)
hoelzl@43339
   405
         (auto simp: interior_open)
hoelzl@43339
   406
    finally show "k \<le> expectation (\<lambda>w. q (X w))" using x by auto
hoelzl@43339
   407
  qed
hoelzl@43339
   408
  finally show "q (expectation X) \<le> expectation (\<lambda>x. q (X x))" .
hoelzl@43339
   409
qed
hoelzl@43339
   410
hoelzl@41981
   411
lemma (in prob_space) distribution_eq_translated_integral:
hoelzl@41981
   412
  assumes "random_variable S X" "A \<in> sets S"
hoelzl@41981
   413
  shows "distribution X A = integral\<^isup>P (S\<lparr>measure := extreal \<circ> distribution X\<rparr>) (indicator A)"
hoelzl@35582
   414
proof -
hoelzl@41981
   415
  interpret S: prob_space "S\<lparr>measure := extreal \<circ> distribution X\<rparr>"
hoelzl@41689
   416
    using assms(1) by (rule distribution_prob_space)
hoelzl@35582
   417
  show ?thesis
hoelzl@41981
   418
    using S.positive_integral_indicator(1)[of A] assms by simp
hoelzl@35582
   419
qed
hoelzl@35582
   420
hoelzl@40859
   421
lemma (in prob_space) finite_expectation1:
hoelzl@40859
   422
  assumes f: "finite (X`space M)" and rv: "random_variable borel X"
hoelzl@41981
   423
  shows "expectation X = (\<Sum>r \<in> X ` space M. r * prob (X -` {r} \<inter> space M))" (is "_ = ?r")
hoelzl@41981
   424
proof (subst integral_on_finite)
hoelzl@41981
   425
  show "X \<in> borel_measurable M" "finite (X`space M)" using assms by auto
hoelzl@41981
   426
  show "(\<Sum> r \<in> X ` space M. r * real (\<mu> (X -` {r} \<inter> space M))) = ?r"
hoelzl@41981
   427
    "\<And>x. \<mu> (X -` {x} \<inter> space M) \<noteq> \<infinity>"
hoelzl@41981
   428
    using finite_measure_eq[OF borel_measurable_vimage, of X] rv by auto
hoelzl@38656
   429
qed
hoelzl@35582
   430
hoelzl@40859
   431
lemma (in prob_space) finite_expectation:
hoelzl@41689
   432
  assumes "finite (X`space M)" "random_variable borel X"
hoelzl@41981
   433
  shows "expectation X = (\<Sum> r \<in> X ` (space M). r * distribution X {r})"
hoelzl@38656
   434
  using assms unfolding distribution_def using finite_expectation1 by auto
hoelzl@38656
   435
hoelzl@40859
   436
lemma (in prob_space) prob_x_eq_1_imp_prob_y_eq_0:
hoelzl@35582
   437
  assumes "{x} \<in> events"
hoelzl@38656
   438
  assumes "prob {x} = 1"
hoelzl@35582
   439
  assumes "{y} \<in> events"
hoelzl@35582
   440
  assumes "y \<noteq> x"
hoelzl@35582
   441
  shows "prob {y} = 0"
hoelzl@35582
   442
  using prob_one_inter[of "{y}" "{x}"] assms by auto
hoelzl@35582
   443
hoelzl@40859
   444
lemma (in prob_space) distribution_empty[simp]: "distribution X {} = 0"
hoelzl@38656
   445
  unfolding distribution_def by simp
hoelzl@38656
   446
hoelzl@40859
   447
lemma (in prob_space) distribution_space[simp]: "distribution X (X ` space M) = 1"
hoelzl@38656
   448
proof -
hoelzl@38656
   449
  have "X -` X ` space M \<inter> space M = space M" by auto
hoelzl@41981
   450
  thus ?thesis unfolding distribution_def by (simp add: prob_space)
hoelzl@38656
   451
qed
hoelzl@38656
   452
hoelzl@40859
   453
lemma (in prob_space) distribution_one:
hoelzl@40859
   454
  assumes "random_variable M' X" and "A \<in> sets M'"
hoelzl@38656
   455
  shows "distribution X A \<le> 1"
hoelzl@38656
   456
proof -
hoelzl@41981
   457
  have "distribution X A \<le> \<mu>' (space M)" unfolding distribution_def
hoelzl@41981
   458
    using assms[unfolded measurable_def] by (auto intro!: finite_measure_mono)
hoelzl@41981
   459
  thus ?thesis by (simp add: prob_space)
hoelzl@38656
   460
qed
hoelzl@38656
   461
hoelzl@40859
   462
lemma (in prob_space) distribution_x_eq_1_imp_distribution_y_eq_0:
hoelzl@35582
   463
  assumes X: "random_variable \<lparr>space = X ` (space M), sets = Pow (X ` (space M))\<rparr> X"
hoelzl@38656
   464
    (is "random_variable ?S X")
hoelzl@38656
   465
  assumes "distribution X {x} = 1"
hoelzl@35582
   466
  assumes "y \<noteq> x"
hoelzl@35582
   467
  shows "distribution X {y} = 0"
hoelzl@41689
   468
proof cases
hoelzl@41689
   469
  { fix x have "X -` {x} \<inter> space M \<in> sets M"
hoelzl@41689
   470
    proof cases
hoelzl@41689
   471
      assume "x \<in> X`space M" with X show ?thesis
hoelzl@41689
   472
        by (auto simp: measurable_def image_iff)
hoelzl@41689
   473
    next
hoelzl@41689
   474
      assume "x \<notin> X`space M" then have "X -` {x} \<inter> space M = {}" by auto
hoelzl@41689
   475
      then show ?thesis by auto
hoelzl@41689
   476
    qed } note single = this
hoelzl@41689
   477
  have "X -` {x} \<inter> space M - X -` {y} \<inter> space M = X -` {x} \<inter> space M"
hoelzl@41689
   478
    "X -` {y} \<inter> space M \<inter> (X -` {x} \<inter> space M) = {}"
hoelzl@41689
   479
    using `y \<noteq> x` by auto
hoelzl@41981
   480
  with finite_measure_inter_full_set[OF single single, of x y] assms(2)
hoelzl@41981
   481
  show ?thesis by (auto simp: distribution_def prob_space)
hoelzl@41689
   482
next
hoelzl@41689
   483
  assume "{y} \<notin> sets ?S"
hoelzl@41689
   484
  then have "X -` {y} \<inter> space M = {}" by auto
hoelzl@41689
   485
  thus "distribution X {y} = 0" unfolding distribution_def by auto
hoelzl@35582
   486
qed
hoelzl@35582
   487
hoelzl@40859
   488
lemma (in prob_space) joint_distribution_Times_le_fst:
hoelzl@40859
   489
  assumes X: "random_variable MX X" and Y: "random_variable MY Y"
hoelzl@40859
   490
    and A: "A \<in> sets MX" and B: "B \<in> sets MY"
hoelzl@40859
   491
  shows "joint_distribution X Y (A \<times> B) \<le> distribution X A"
hoelzl@40859
   492
  unfolding distribution_def
hoelzl@41981
   493
proof (intro finite_measure_mono)
hoelzl@40859
   494
  show "(\<lambda>x. (X x, Y x)) -` (A \<times> B) \<inter> space M \<subseteq> X -` A \<inter> space M" by force
hoelzl@40859
   495
  show "X -` A \<inter> space M \<in> events"
hoelzl@40859
   496
    using X A unfolding measurable_def by simp
hoelzl@40859
   497
  have *: "(\<lambda>x. (X x, Y x)) -` (A \<times> B) \<inter> space M =
hoelzl@40859
   498
    (X -` A \<inter> space M) \<inter> (Y -` B \<inter> space M)" by auto
hoelzl@40859
   499
qed
hoelzl@40859
   500
hoelzl@40859
   501
lemma (in prob_space) joint_distribution_commute:
hoelzl@40859
   502
  "joint_distribution X Y x = joint_distribution Y X ((\<lambda>(x,y). (y,x))`x)"
hoelzl@41981
   503
  unfolding distribution_def by (auto intro!: arg_cong[where f=\<mu>'])
hoelzl@40859
   504
hoelzl@40859
   505
lemma (in prob_space) joint_distribution_Times_le_snd:
hoelzl@40859
   506
  assumes X: "random_variable MX X" and Y: "random_variable MY Y"
hoelzl@40859
   507
    and A: "A \<in> sets MX" and B: "B \<in> sets MY"
hoelzl@40859
   508
  shows "joint_distribution X Y (A \<times> B) \<le> distribution Y B"
hoelzl@40859
   509
  using assms
hoelzl@40859
   510
  by (subst joint_distribution_commute)
hoelzl@40859
   511
     (simp add: swap_product joint_distribution_Times_le_fst)
hoelzl@40859
   512
hoelzl@40859
   513
lemma (in prob_space) random_variable_pairI:
hoelzl@40859
   514
  assumes "random_variable MX X"
hoelzl@40859
   515
  assumes "random_variable MY Y"
hoelzl@41689
   516
  shows "random_variable (MX \<Otimes>\<^isub>M MY) (\<lambda>x. (X x, Y x))"
hoelzl@40859
   517
proof
hoelzl@40859
   518
  interpret MX: sigma_algebra MX using assms by simp
hoelzl@40859
   519
  interpret MY: sigma_algebra MY using assms by simp
hoelzl@40859
   520
  interpret P: pair_sigma_algebra MX MY by default
hoelzl@41689
   521
  show "sigma_algebra (MX \<Otimes>\<^isub>M MY)" by default
hoelzl@40859
   522
  have sa: "sigma_algebra M" by default
hoelzl@41689
   523
  show "(\<lambda>x. (X x, Y x)) \<in> measurable M (MX \<Otimes>\<^isub>M MY)"
hoelzl@41095
   524
    unfolding P.measurable_pair_iff[OF sa] using assms by (simp add: comp_def)
hoelzl@40859
   525
qed
hoelzl@40859
   526
hoelzl@40859
   527
lemma (in prob_space) joint_distribution_commute_singleton:
hoelzl@40859
   528
  "joint_distribution X Y {(x, y)} = joint_distribution Y X {(y, x)}"
hoelzl@41981
   529
  unfolding distribution_def by (auto intro!: arg_cong[where f=\<mu>'])
hoelzl@40859
   530
hoelzl@40859
   531
lemma (in prob_space) joint_distribution_assoc_singleton:
hoelzl@40859
   532
  "joint_distribution X (\<lambda>x. (Y x, Z x)) {(x, y, z)} =
hoelzl@40859
   533
   joint_distribution (\<lambda>x. (X x, Y x)) Z {((x, y), z)}"
hoelzl@41981
   534
  unfolding distribution_def by (auto intro!: arg_cong[where f=\<mu>'])
hoelzl@40859
   535
hoelzl@41689
   536
locale pair_prob_space = M1: prob_space M1 + M2: prob_space M2 for M1 M2
hoelzl@40859
   537
hoelzl@41689
   538
sublocale pair_prob_space \<subseteq> pair_sigma_finite M1 M2 by default
hoelzl@41689
   539
hoelzl@41689
   540
sublocale pair_prob_space \<subseteq> P: prob_space P
hoelzl@41689
   541
by default (simp add: pair_measure_times M1.measure_space_1 M2.measure_space_1 space_pair_measure)
hoelzl@40859
   542
hoelzl@40859
   543
lemma countably_additiveI[case_names countably]:
hoelzl@40859
   544
  assumes "\<And>A. \<lbrakk> range A \<subseteq> sets M ; disjoint_family A ; (\<Union>i. A i) \<in> sets M\<rbrakk> \<Longrightarrow>
hoelzl@41981
   545
    (\<Sum>n. \<mu> (A n)) = \<mu> (\<Union>i. A i)"
hoelzl@40859
   546
  shows "countably_additive M \<mu>"
hoelzl@40859
   547
  using assms unfolding countably_additive_def by auto
hoelzl@40859
   548
hoelzl@40859
   549
lemma (in prob_space) joint_distribution_prob_space:
hoelzl@40859
   550
  assumes "random_variable MX X" "random_variable MY Y"
hoelzl@41981
   551
  shows "prob_space ((MX \<Otimes>\<^isub>M MY) \<lparr> measure := extreal \<circ> joint_distribution X Y\<rparr>)"
hoelzl@41689
   552
  using random_variable_pairI[OF assms] by (rule distribution_prob_space)
hoelzl@40859
   553
hoelzl@42988
   554
hoelzl@42988
   555
locale finite_product_prob_space =
hoelzl@42988
   556
  fixes M :: "'i \<Rightarrow> ('a,'b) measure_space_scheme"
hoelzl@42988
   557
    and I :: "'i set"
hoelzl@42988
   558
  assumes prob_space: "\<And>i. prob_space (M i)" and finite_index: "finite I"
hoelzl@42988
   559
hoelzl@42988
   560
sublocale finite_product_prob_space \<subseteq> M: prob_space "M i" for i
hoelzl@42988
   561
  by (rule prob_space)
hoelzl@42988
   562
hoelzl@42988
   563
sublocale finite_product_prob_space \<subseteq> finite_product_sigma_finite M I
hoelzl@42988
   564
  by default (rule finite_index)
hoelzl@42988
   565
hoelzl@42988
   566
sublocale finite_product_prob_space \<subseteq> prob_space "\<Pi>\<^isub>M i\<in>I. M i"
hoelzl@42988
   567
  proof qed (simp add: measure_times M.measure_space_1 setprod_1)
hoelzl@42988
   568
hoelzl@42988
   569
lemma (in finite_product_prob_space) prob_times:
hoelzl@42988
   570
  assumes X: "\<And>i. i \<in> I \<Longrightarrow> X i \<in> sets (M i)"
hoelzl@42988
   571
  shows "prob (\<Pi>\<^isub>E i\<in>I. X i) = (\<Prod>i\<in>I. M.prob i (X i))"
hoelzl@42988
   572
proof -
hoelzl@42988
   573
  have "extreal (\<mu>' (\<Pi>\<^isub>E i\<in>I. X i)) = \<mu> (\<Pi>\<^isub>E i\<in>I. X i)"
hoelzl@42988
   574
    using X by (intro finite_measure_eq[symmetric] in_P) auto
hoelzl@42988
   575
  also have "\<dots> = (\<Prod>i\<in>I. M.\<mu> i (X i))"
hoelzl@42988
   576
    using measure_times X by simp
hoelzl@42988
   577
  also have "\<dots> = extreal (\<Prod>i\<in>I. M.\<mu>' i (X i))"
hoelzl@42988
   578
    using X by (simp add: M.finite_measure_eq setprod_extreal)
hoelzl@42988
   579
  finally show ?thesis by simp
hoelzl@42988
   580
qed
hoelzl@42988
   581
hoelzl@42988
   582
lemma (in prob_space) random_variable_restrict:
hoelzl@42988
   583
  assumes I: "finite I"
hoelzl@42988
   584
  assumes X: "\<And>i. i \<in> I \<Longrightarrow> random_variable (N i) (X i)"
hoelzl@42988
   585
  shows "random_variable (Pi\<^isub>M I N) (\<lambda>x. \<lambda>i\<in>I. X i x)"
hoelzl@42988
   586
proof
hoelzl@42988
   587
  { fix i assume "i \<in> I"
hoelzl@42988
   588
    with X interpret N: sigma_algebra "N i" by simp
hoelzl@42988
   589
    have "sets (N i) \<subseteq> Pow (space (N i))" by (rule N.space_closed) }
hoelzl@42988
   590
  note N_closed = this
hoelzl@42988
   591
  then show "sigma_algebra (Pi\<^isub>M I N)"
hoelzl@42988
   592
    by (simp add: product_algebra_def)
hoelzl@42988
   593
       (intro sigma_algebra_sigma product_algebra_generator_sets_into_space)
hoelzl@42988
   594
  show "(\<lambda>x. \<lambda>i\<in>I. X i x) \<in> measurable M (Pi\<^isub>M I N)"
hoelzl@42988
   595
    using X by (intro measurable_restrict[OF I N_closed]) auto
hoelzl@42988
   596
qed
hoelzl@42988
   597
hoelzl@40859
   598
section "Probability spaces on finite sets"
hoelzl@35582
   599
hoelzl@35977
   600
locale finite_prob_space = prob_space + finite_measure_space
hoelzl@35977
   601
hoelzl@40859
   602
abbreviation (in prob_space) "finite_random_variable M' X \<equiv> finite_sigma_algebra M' \<and> X \<in> measurable M M'"
hoelzl@40859
   603
hoelzl@40859
   604
lemma (in prob_space) finite_random_variableD:
hoelzl@40859
   605
  assumes "finite_random_variable M' X" shows "random_variable M' X"
hoelzl@40859
   606
proof -
hoelzl@40859
   607
  interpret M': finite_sigma_algebra M' using assms by simp
hoelzl@40859
   608
  then show "random_variable M' X" using assms by simp default
hoelzl@40859
   609
qed
hoelzl@40859
   610
hoelzl@40859
   611
lemma (in prob_space) distribution_finite_prob_space:
hoelzl@40859
   612
  assumes "finite_random_variable MX X"
hoelzl@41981
   613
  shows "finite_prob_space (MX\<lparr>measure := extreal \<circ> distribution X\<rparr>)"
hoelzl@40859
   614
proof -
hoelzl@41981
   615
  interpret X: prob_space "MX\<lparr>measure := extreal \<circ> distribution X\<rparr>"
hoelzl@40859
   616
    using assms[THEN finite_random_variableD] by (rule distribution_prob_space)
hoelzl@40859
   617
  interpret MX: finite_sigma_algebra MX
hoelzl@41689
   618
    using assms by auto
hoelzl@41981
   619
  show ?thesis by default (simp_all add: MX.finite_space)
hoelzl@40859
   620
qed
hoelzl@40859
   621
hoelzl@40859
   622
lemma (in prob_space) simple_function_imp_finite_random_variable[simp, intro]:
hoelzl@41689
   623
  assumes "simple_function M X"
hoelzl@41689
   624
  shows "finite_random_variable \<lparr> space = X`space M, sets = Pow (X`space M), \<dots> = x \<rparr> X"
hoelzl@41689
   625
    (is "finite_random_variable ?X _")
hoelzl@40859
   626
proof (intro conjI)
hoelzl@40859
   627
  have [simp]: "finite (X ` space M)" using assms unfolding simple_function_def by simp
hoelzl@41689
   628
  interpret X: sigma_algebra ?X by (rule sigma_algebra_Pow)
hoelzl@41689
   629
  show "finite_sigma_algebra ?X"
hoelzl@40859
   630
    by default auto
hoelzl@41689
   631
  show "X \<in> measurable M ?X"
hoelzl@40859
   632
  proof (unfold measurable_def, clarsimp)
hoelzl@40859
   633
    fix A assume A: "A \<subseteq> X`space M"
hoelzl@40859
   634
    then have "finite A" by (rule finite_subset) simp
hoelzl@40859
   635
    then have "X -` (\<Union>a\<in>A. {a}) \<inter> space M \<in> events"
hoelzl@40859
   636
      unfolding vimage_UN UN_extend_simps
hoelzl@40859
   637
      apply (rule finite_UN)
hoelzl@40859
   638
      using A assms unfolding simple_function_def by auto
hoelzl@40859
   639
    then show "X -` A \<inter> space M \<in> events" by simp
hoelzl@40859
   640
  qed
hoelzl@40859
   641
qed
hoelzl@40859
   642
hoelzl@40859
   643
lemma (in prob_space) simple_function_imp_random_variable[simp, intro]:
hoelzl@41689
   644
  assumes "simple_function M X"
hoelzl@41689
   645
  shows "random_variable \<lparr> space = X`space M, sets = Pow (X`space M), \<dots> = ext \<rparr> X"
hoelzl@41689
   646
  using simple_function_imp_finite_random_variable[OF assms, of ext]
hoelzl@40859
   647
  by (auto dest!: finite_random_variableD)
hoelzl@40859
   648
hoelzl@40859
   649
lemma (in prob_space) sum_over_space_real_distribution:
hoelzl@41981
   650
  "simple_function M X \<Longrightarrow> (\<Sum>x\<in>X`space M. distribution X {x}) = 1"
hoelzl@40859
   651
  unfolding distribution_def prob_space[symmetric]
hoelzl@41981
   652
  by (subst finite_measure_finite_Union[symmetric])
hoelzl@40859
   653
     (auto simp add: disjoint_family_on_def simple_function_def
hoelzl@40859
   654
           intro!: arg_cong[where f=prob])
hoelzl@40859
   655
hoelzl@40859
   656
lemma (in prob_space) finite_random_variable_pairI:
hoelzl@40859
   657
  assumes "finite_random_variable MX X"
hoelzl@40859
   658
  assumes "finite_random_variable MY Y"
hoelzl@41689
   659
  shows "finite_random_variable (MX \<Otimes>\<^isub>M MY) (\<lambda>x. (X x, Y x))"
hoelzl@40859
   660
proof
hoelzl@40859
   661
  interpret MX: finite_sigma_algebra MX using assms by simp
hoelzl@40859
   662
  interpret MY: finite_sigma_algebra MY using assms by simp
hoelzl@40859
   663
  interpret P: pair_finite_sigma_algebra MX MY by default
hoelzl@41689
   664
  show "finite_sigma_algebra (MX \<Otimes>\<^isub>M MY)" by default
hoelzl@40859
   665
  have sa: "sigma_algebra M" by default
hoelzl@41689
   666
  show "(\<lambda>x. (X x, Y x)) \<in> measurable M (MX \<Otimes>\<^isub>M MY)"
hoelzl@41095
   667
    unfolding P.measurable_pair_iff[OF sa] using assms by (simp add: comp_def)
hoelzl@40859
   668
qed
hoelzl@40859
   669
hoelzl@40859
   670
lemma (in prob_space) finite_random_variable_imp_sets:
hoelzl@40859
   671
  "finite_random_variable MX X \<Longrightarrow> x \<in> space MX \<Longrightarrow> {x} \<in> sets MX"
hoelzl@40859
   672
  unfolding finite_sigma_algebra_def finite_sigma_algebra_axioms_def by simp
hoelzl@40859
   673
hoelzl@41981
   674
lemma (in prob_space) finite_random_variable_measurable:
hoelzl@40859
   675
  assumes X: "finite_random_variable MX X" shows "X -` A \<inter> space M \<in> events"
hoelzl@40859
   676
proof -
hoelzl@40859
   677
  interpret X: finite_sigma_algebra MX using X by simp
hoelzl@40859
   678
  from X have vimage: "\<And>A. A \<subseteq> space MX \<Longrightarrow> X -` A \<inter> space M \<in> events" and
hoelzl@40859
   679
    "X \<in> space M \<rightarrow> space MX"
hoelzl@40859
   680
    by (auto simp: measurable_def)
hoelzl@40859
   681
  then have *: "X -` A \<inter> space M = X -` (A \<inter> space MX) \<inter> space M"
hoelzl@40859
   682
    by auto
hoelzl@40859
   683
  show "X -` A \<inter> space M \<in> events"
hoelzl@40859
   684
    unfolding * by (intro vimage) auto
hoelzl@40859
   685
qed
hoelzl@40859
   686
hoelzl@40859
   687
lemma (in prob_space) joint_distribution_finite_Times_le_fst:
hoelzl@40859
   688
  assumes X: "finite_random_variable MX X" and Y: "finite_random_variable MY Y"
hoelzl@40859
   689
  shows "joint_distribution X Y (A \<times> B) \<le> distribution X A"
hoelzl@40859
   690
  unfolding distribution_def
hoelzl@41981
   691
proof (intro finite_measure_mono)
hoelzl@40859
   692
  show "(\<lambda>x. (X x, Y x)) -` (A \<times> B) \<inter> space M \<subseteq> X -` A \<inter> space M" by force
hoelzl@40859
   693
  show "X -` A \<inter> space M \<in> events"
hoelzl@41981
   694
    using finite_random_variable_measurable[OF X] .
hoelzl@40859
   695
  have *: "(\<lambda>x. (X x, Y x)) -` (A \<times> B) \<inter> space M =
hoelzl@40859
   696
    (X -` A \<inter> space M) \<inter> (Y -` B \<inter> space M)" by auto
hoelzl@40859
   697
qed
hoelzl@40859
   698
hoelzl@40859
   699
lemma (in prob_space) joint_distribution_finite_Times_le_snd:
hoelzl@40859
   700
  assumes X: "finite_random_variable MX X" and Y: "finite_random_variable MY Y"
hoelzl@40859
   701
  shows "joint_distribution X Y (A \<times> B) \<le> distribution Y B"
hoelzl@40859
   702
  using assms
hoelzl@40859
   703
  by (subst joint_distribution_commute)
hoelzl@40859
   704
     (simp add: swap_product joint_distribution_finite_Times_le_fst)
hoelzl@40859
   705
hoelzl@40859
   706
lemma (in prob_space) finite_distribution_order:
hoelzl@41981
   707
  fixes MX :: "('c, 'd) measure_space_scheme" and MY :: "('e, 'f) measure_space_scheme"
hoelzl@40859
   708
  assumes "finite_random_variable MX X" "finite_random_variable MY Y"
hoelzl@40859
   709
  shows "r \<le> joint_distribution X Y {(x, y)} \<Longrightarrow> r \<le> distribution X {x}"
hoelzl@40859
   710
    and "r \<le> joint_distribution X Y {(x, y)} \<Longrightarrow> r \<le> distribution Y {y}"
hoelzl@40859
   711
    and "r < joint_distribution X Y {(x, y)} \<Longrightarrow> r < distribution X {x}"
hoelzl@40859
   712
    and "r < joint_distribution X Y {(x, y)} \<Longrightarrow> r < distribution Y {y}"
hoelzl@40859
   713
    and "distribution X {x} = 0 \<Longrightarrow> joint_distribution X Y {(x, y)} = 0"
hoelzl@40859
   714
    and "distribution Y {y} = 0 \<Longrightarrow> joint_distribution X Y {(x, y)} = 0"
hoelzl@40859
   715
  using joint_distribution_finite_Times_le_snd[OF assms, of "{x}" "{y}"]
hoelzl@40859
   716
  using joint_distribution_finite_Times_le_fst[OF assms, of "{x}" "{y}"]
hoelzl@41981
   717
  by (auto intro: antisym)
hoelzl@40859
   718
hoelzl@40859
   719
lemma (in prob_space) setsum_joint_distribution:
hoelzl@40859
   720
  assumes X: "finite_random_variable MX X"
hoelzl@40859
   721
  assumes Y: "random_variable MY Y" "B \<in> sets MY"
hoelzl@40859
   722
  shows "(\<Sum>a\<in>space MX. joint_distribution X Y ({a} \<times> B)) = distribution Y B"
hoelzl@40859
   723
  unfolding distribution_def
hoelzl@41981
   724
proof (subst finite_measure_finite_Union[symmetric])
hoelzl@40859
   725
  interpret MX: finite_sigma_algebra MX using X by auto
hoelzl@40859
   726
  show "finite (space MX)" using MX.finite_space .
hoelzl@40859
   727
  let "?d i" = "(\<lambda>x. (X x, Y x)) -` ({i} \<times> B) \<inter> space M"
hoelzl@40859
   728
  { fix i assume "i \<in> space MX"
hoelzl@40859
   729
    moreover have "?d i = (X -` {i} \<inter> space M) \<inter> (Y -` B \<inter> space M)" by auto
hoelzl@40859
   730
    ultimately show "?d i \<in> events"
hoelzl@40859
   731
      using measurable_sets[of X M MX] measurable_sets[of Y M MY B] X Y
hoelzl@40859
   732
      using MX.sets_eq_Pow by auto }
hoelzl@40859
   733
  show "disjoint_family_on ?d (space MX)" by (auto simp: disjoint_family_on_def)
hoelzl@41981
   734
  show "\<mu>' (\<Union>i\<in>space MX. ?d i) = \<mu>' (Y -` B \<inter> space M)"
hoelzl@41981
   735
    using X[unfolded measurable_def] by (auto intro!: arg_cong[where f=\<mu>'])
hoelzl@40859
   736
qed
hoelzl@40859
   737
hoelzl@40859
   738
lemma (in prob_space) setsum_joint_distribution_singleton:
hoelzl@40859
   739
  assumes X: "finite_random_variable MX X"
hoelzl@40859
   740
  assumes Y: "finite_random_variable MY Y" "b \<in> space MY"
hoelzl@40859
   741
  shows "(\<Sum>a\<in>space MX. joint_distribution X Y {(a, b)}) = distribution Y {b}"
hoelzl@40859
   742
  using setsum_joint_distribution[OF X
hoelzl@40859
   743
    finite_random_variableD[OF Y(1)]
hoelzl@40859
   744
    finite_random_variable_imp_sets[OF Y]] by simp
hoelzl@40859
   745
hoelzl@41689
   746
locale pair_finite_prob_space = M1: finite_prob_space M1 + M2: finite_prob_space M2 for M1 M2
hoelzl@40859
   747
hoelzl@41689
   748
sublocale pair_finite_prob_space \<subseteq> pair_prob_space M1 M2 by default
hoelzl@41689
   749
sublocale pair_finite_prob_space \<subseteq> pair_finite_space M1 M2  by default
hoelzl@41689
   750
sublocale pair_finite_prob_space \<subseteq> finite_prob_space P by default
hoelzl@40859
   751
hoelzl@42859
   752
locale product_finite_prob_space =
hoelzl@42859
   753
  fixes M :: "'i \<Rightarrow> ('a,'b) measure_space_scheme"
hoelzl@42859
   754
    and I :: "'i set"
hoelzl@42859
   755
  assumes finite_space: "\<And>i. finite_prob_space (M i)" and finite_index: "finite I"
hoelzl@42859
   756
hoelzl@42859
   757
sublocale product_finite_prob_space \<subseteq> M!: finite_prob_space "M i" using finite_space .
hoelzl@42859
   758
sublocale product_finite_prob_space \<subseteq> finite_product_sigma_finite M I by default (rule finite_index)
hoelzl@42859
   759
sublocale product_finite_prob_space \<subseteq> prob_space "Pi\<^isub>M I M"
hoelzl@42859
   760
proof
hoelzl@42859
   761
  show "\<mu> (space P) = 1"
hoelzl@42859
   762
    using measure_times[OF M.top] M.measure_space_1
hoelzl@42859
   763
    by (simp add: setprod_1 space_product_algebra)
hoelzl@42859
   764
qed
hoelzl@42859
   765
hoelzl@42859
   766
lemma funset_eq_UN_fun_upd_I:
hoelzl@42859
   767
  assumes *: "\<And>f. f \<in> F (insert a A) \<Longrightarrow> f(a := d) \<in> F A"
hoelzl@42859
   768
  and **: "\<And>f. f \<in> F (insert a A) \<Longrightarrow> f a \<in> G (f(a:=d))"
hoelzl@42859
   769
  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
   770
  shows "F (insert a A) = (\<Union>f\<in>F A. fun_upd f a ` (G f))"
hoelzl@42859
   771
proof safe
hoelzl@42859
   772
  fix f assume f: "f \<in> F (insert a A)"
hoelzl@42859
   773
  show "f \<in> (\<Union>f\<in>F A. fun_upd f a ` G f)"
hoelzl@42859
   774
  proof (rule UN_I[of "f(a := d)"])
hoelzl@42859
   775
    show "f(a := d) \<in> F A" using *[OF f] .
hoelzl@42859
   776
    show "f \<in> fun_upd (f(a:=d)) a ` G (f(a:=d))"
hoelzl@42859
   777
    proof (rule image_eqI[of _ _ "f a"])
hoelzl@42859
   778
      show "f a \<in> G (f(a := d))" using **[OF f] .
hoelzl@42859
   779
    qed simp
hoelzl@42859
   780
  qed
hoelzl@42859
   781
next
hoelzl@42859
   782
  fix f x assume "f \<in> F A" "x \<in> G f"
hoelzl@42859
   783
  from ***[OF this] show "f(a := x) \<in> F (insert a A)" .
hoelzl@42859
   784
qed
hoelzl@42859
   785
hoelzl@42859
   786
lemma extensional_funcset_insert_eq[simp]:
hoelzl@42859
   787
  assumes "a \<notin> A"
hoelzl@42859
   788
  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
   789
  apply (rule funset_eq_UN_fun_upd_I)
hoelzl@42859
   790
  using assms
hoelzl@42859
   791
  by (auto intro!: inj_onI dest: inj_onD split: split_if_asm simp: extensional_def)
hoelzl@42859
   792
hoelzl@42859
   793
lemma finite_extensional_funcset[simp, intro]:
hoelzl@42859
   794
  assumes "finite A" "finite B"
hoelzl@42859
   795
  shows "finite (extensional A \<inter> (A \<rightarrow> B))"
hoelzl@42859
   796
  using assms by induct (auto simp: extensional_funcset_insert_eq)
hoelzl@42859
   797
hoelzl@42859
   798
lemma finite_PiE[simp, intro]:
hoelzl@42859
   799
  assumes fin: "finite A" "\<And>i. i \<in> A \<Longrightarrow> finite (B i)"
hoelzl@42859
   800
  shows "finite (Pi\<^isub>E A B)"
hoelzl@42859
   801
proof -
hoelzl@42859
   802
  have *: "(Pi\<^isub>E A B) \<subseteq> extensional A \<inter> (A \<rightarrow> (\<Union>i\<in>A. B i))" by auto
hoelzl@42859
   803
  show ?thesis
hoelzl@42859
   804
    using fin by (intro finite_subset[OF *] finite_extensional_funcset) auto
hoelzl@42859
   805
qed
hoelzl@42859
   806
hoelzl@42892
   807
lemma (in product_finite_prob_space) singleton_eq_product:
hoelzl@42892
   808
  assumes x: "x \<in> space P" shows "{x} = (\<Pi>\<^isub>E i\<in>I. {x i})"
hoelzl@42892
   809
proof (safe intro!: ext[of _ x])
hoelzl@42892
   810
  fix y i assume *: "y \<in> (\<Pi> i\<in>I. {x i})" "y \<in> extensional I"
hoelzl@42892
   811
  with x show "y i = x i"
hoelzl@42892
   812
    by (cases "i \<in> I") (auto simp: extensional_def)
hoelzl@42892
   813
qed (insert x, auto)
hoelzl@42892
   814
hoelzl@42859
   815
sublocale product_finite_prob_space \<subseteq> finite_prob_space "Pi\<^isub>M I M"
hoelzl@42859
   816
proof
hoelzl@42859
   817
  show "finite (space P)"
hoelzl@42859
   818
    using finite_index M.finite_space by auto
hoelzl@42859
   819
hoelzl@42859
   820
  { fix x assume "x \<in> space P"
hoelzl@42892
   821
    with this[THEN singleton_eq_product]
hoelzl@42892
   822
    have "{x} \<in> sets P"
hoelzl@42859
   823
      by (auto intro!: in_P) }
hoelzl@42859
   824
  note x_in_P = this
hoelzl@42859
   825
hoelzl@42859
   826
  have "Pow (space P) \<subseteq> sets P"
hoelzl@42859
   827
  proof
hoelzl@42859
   828
    fix X assume "X \<in> Pow (space P)"
hoelzl@42859
   829
    moreover then have "finite X"
hoelzl@42859
   830
      using `finite (space P)` by (blast intro: finite_subset)
hoelzl@42859
   831
    ultimately have "(\<Union>x\<in>X. {x}) \<in> sets P"
hoelzl@42859
   832
      by (intro finite_UN x_in_P) auto
hoelzl@42859
   833
    then show "X \<in> sets P" by simp
hoelzl@42859
   834
  qed
hoelzl@42859
   835
  with space_closed show [simp]: "sets P = Pow (space P)" ..
hoelzl@42859
   836
hoelzl@42859
   837
  { fix x assume "x \<in> space P"
hoelzl@42859
   838
    from this top have "\<mu> {x} \<le> \<mu> (space P)" by (intro measure_mono) auto
hoelzl@42859
   839
    then show "\<mu> {x} \<noteq> \<infinity>"
hoelzl@42859
   840
      using measure_space_1 by auto }
hoelzl@42859
   841
qed
hoelzl@42859
   842
hoelzl@42859
   843
lemma (in product_finite_prob_space) measure_finite_times:
hoelzl@42859
   844
  "(\<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
   845
  by (rule measure_times) simp
hoelzl@42859
   846
hoelzl@42892
   847
lemma (in product_finite_prob_space) measure_singleton_times:
hoelzl@42892
   848
  assumes x: "x \<in> space P" shows "\<mu> {x} = (\<Prod>i\<in>I. M.\<mu> i {x i})"
hoelzl@42892
   849
  unfolding singleton_eq_product[OF x] using x
hoelzl@42892
   850
  by (intro measure_finite_times) auto
hoelzl@42892
   851
hoelzl@42892
   852
lemma (in product_finite_prob_space) prob_finite_times:
hoelzl@42859
   853
  assumes X: "\<And>i. i \<in> I \<Longrightarrow> X i \<subseteq> space (M i)"
hoelzl@42859
   854
  shows "prob (\<Pi>\<^isub>E i\<in>I. X i) = (\<Prod>i\<in>I. M.prob i (X i))"
hoelzl@42859
   855
proof -
hoelzl@42859
   856
  have "extreal (\<mu>' (\<Pi>\<^isub>E i\<in>I. X i)) = \<mu> (\<Pi>\<^isub>E i\<in>I. X i)"
hoelzl@42859
   857
    using X by (intro finite_measure_eq[symmetric] in_P) auto
hoelzl@42859
   858
  also have "\<dots> = (\<Prod>i\<in>I. M.\<mu> i (X i))"
hoelzl@42859
   859
    using measure_finite_times X by simp
hoelzl@42859
   860
  also have "\<dots> = extreal (\<Prod>i\<in>I. M.\<mu>' i (X i))"
hoelzl@42859
   861
    using X by (simp add: M.finite_measure_eq setprod_extreal)
hoelzl@42859
   862
  finally show ?thesis by simp
hoelzl@42859
   863
qed
hoelzl@42859
   864
hoelzl@42892
   865
lemma (in product_finite_prob_space) prob_singleton_times:
hoelzl@42892
   866
  assumes x: "x \<in> space P"
hoelzl@42892
   867
  shows "prob {x} = (\<Prod>i\<in>I. M.prob i {x i})"
hoelzl@42892
   868
  unfolding singleton_eq_product[OF x] using x
hoelzl@42892
   869
  by (intro prob_finite_times) auto
hoelzl@42892
   870
hoelzl@42892
   871
lemma (in product_finite_prob_space) prob_finite_product:
hoelzl@42892
   872
  "A \<subseteq> space P \<Longrightarrow> prob A = (\<Sum>x\<in>A. \<Prod>i\<in>I. M.prob i {x i})"
hoelzl@42892
   873
  by (auto simp add: finite_measure_singleton prob_singleton_times
hoelzl@42892
   874
           simp del: space_product_algebra
hoelzl@42892
   875
           intro!: setsum_cong prob_singleton_times)
hoelzl@42892
   876
hoelzl@40859
   877
lemma (in prob_space) joint_distribution_finite_prob_space:
hoelzl@40859
   878
  assumes X: "finite_random_variable MX X"
hoelzl@40859
   879
  assumes Y: "finite_random_variable MY Y"
hoelzl@41981
   880
  shows "finite_prob_space ((MX \<Otimes>\<^isub>M MY)\<lparr> measure := extreal \<circ> joint_distribution X Y\<rparr>)"
hoelzl@41689
   881
  by (intro distribution_finite_prob_space finite_random_variable_pairI X Y)
hoelzl@40859
   882
hoelzl@36624
   883
lemma finite_prob_space_eq:
hoelzl@41689
   884
  "finite_prob_space M \<longleftrightarrow> finite_measure_space M \<and> measure M (space M) = 1"
hoelzl@36624
   885
  unfolding finite_prob_space_def finite_measure_space_def prob_space_def prob_space_axioms_def
hoelzl@36624
   886
  by auto
hoelzl@36624
   887
hoelzl@38656
   888
lemma (in finite_prob_space) sum_over_space_eq_1: "(\<Sum>x\<in>space M. \<mu> {x}) = 1"
hoelzl@38656
   889
  using measure_space_1 sum_over_space by simp
hoelzl@36624
   890
hoelzl@36624
   891
lemma (in finite_prob_space) joint_distribution_restriction_fst:
hoelzl@36624
   892
  "joint_distribution X Y A \<le> distribution X (fst ` A)"
hoelzl@36624
   893
  unfolding distribution_def
hoelzl@41981
   894
proof (safe intro!: finite_measure_mono)
hoelzl@36624
   895
  fix x assume "x \<in> space M" and *: "(X x, Y x) \<in> A"
hoelzl@36624
   896
  show "x \<in> X -` fst ` A"
hoelzl@36624
   897
    by (auto intro!: image_eqI[OF _ *])
hoelzl@36624
   898
qed (simp_all add: sets_eq_Pow)
hoelzl@36624
   899
hoelzl@36624
   900
lemma (in finite_prob_space) joint_distribution_restriction_snd:
hoelzl@36624
   901
  "joint_distribution X Y A \<le> distribution Y (snd ` A)"
hoelzl@36624
   902
  unfolding distribution_def
hoelzl@41981
   903
proof (safe intro!: finite_measure_mono)
hoelzl@36624
   904
  fix x assume "x \<in> space M" and *: "(X x, Y x) \<in> A"
hoelzl@36624
   905
  show "x \<in> Y -` snd ` A"
hoelzl@36624
   906
    by (auto intro!: image_eqI[OF _ *])
hoelzl@36624
   907
qed (simp_all add: sets_eq_Pow)
hoelzl@36624
   908
hoelzl@36624
   909
lemma (in finite_prob_space) distribution_order:
hoelzl@36624
   910
  shows "0 \<le> distribution X x'"
hoelzl@36624
   911
  and "(distribution X x' \<noteq> 0) \<longleftrightarrow> (0 < distribution X x')"
hoelzl@36624
   912
  and "r \<le> joint_distribution X Y {(x, y)} \<Longrightarrow> r \<le> distribution X {x}"
hoelzl@36624
   913
  and "r \<le> joint_distribution X Y {(x, y)} \<Longrightarrow> r \<le> distribution Y {y}"
hoelzl@36624
   914
  and "r < joint_distribution X Y {(x, y)} \<Longrightarrow> r < distribution X {x}"
hoelzl@36624
   915
  and "r < joint_distribution X Y {(x, y)} \<Longrightarrow> r < distribution Y {y}"
hoelzl@36624
   916
  and "distribution X {x} = 0 \<Longrightarrow> joint_distribution X Y {(x, y)} = 0"
hoelzl@36624
   917
  and "distribution Y {y} = 0 \<Longrightarrow> joint_distribution X Y {(x, y)} = 0"
hoelzl@41981
   918
  using
hoelzl@36624
   919
    joint_distribution_restriction_fst[of X Y "{(x, y)}"]
hoelzl@36624
   920
    joint_distribution_restriction_snd[of X Y "{(x, y)}"]
hoelzl@41981
   921
  by (auto intro: antisym)
hoelzl@36624
   922
hoelzl@39097
   923
lemma (in finite_prob_space) distribution_mono:
hoelzl@39097
   924
  assumes "\<And>t. \<lbrakk> t \<in> space M ; X t \<in> x \<rbrakk> \<Longrightarrow> Y t \<in> y"
hoelzl@39097
   925
  shows "distribution X x \<le> distribution Y y"
hoelzl@39097
   926
  unfolding distribution_def
hoelzl@41981
   927
  using assms by (auto simp: sets_eq_Pow intro!: finite_measure_mono)
hoelzl@39097
   928
hoelzl@39097
   929
lemma (in finite_prob_space) distribution_mono_gt_0:
hoelzl@39097
   930
  assumes gt_0: "0 < distribution X x"
hoelzl@39097
   931
  assumes *: "\<And>t. \<lbrakk> t \<in> space M ; X t \<in> x \<rbrakk> \<Longrightarrow> Y t \<in> y"
hoelzl@39097
   932
  shows "0 < distribution Y y"
hoelzl@39097
   933
  by (rule less_le_trans[OF gt_0 distribution_mono]) (rule *)
hoelzl@39097
   934
hoelzl@39097
   935
lemma (in finite_prob_space) sum_over_space_distrib:
hoelzl@39097
   936
  "(\<Sum>x\<in>X`space M. distribution X {x}) = 1"
hoelzl@41981
   937
  unfolding distribution_def prob_space[symmetric] using finite_space
hoelzl@41981
   938
  by (subst finite_measure_finite_Union[symmetric])
hoelzl@41981
   939
     (auto simp add: disjoint_family_on_def sets_eq_Pow
hoelzl@41981
   940
           intro!: arg_cong[where f=\<mu>'])
hoelzl@39097
   941
hoelzl@39097
   942
lemma (in finite_prob_space) finite_sum_over_space_eq_1:
hoelzl@41981
   943
  "(\<Sum>x\<in>space M. prob {x}) = 1"
hoelzl@41981
   944
  using prob_space finite_space
hoelzl@41981
   945
  by (subst (asm) finite_measure_finite_singleton) auto
hoelzl@39097
   946
hoelzl@39097
   947
lemma (in prob_space) distribution_remove_const:
hoelzl@39097
   948
  shows "joint_distribution X (\<lambda>x. ()) {(x, ())} = distribution X {x}"
hoelzl@39097
   949
  and "joint_distribution (\<lambda>x. ()) X {((), x)} = distribution X {x}"
hoelzl@39097
   950
  and "joint_distribution X (\<lambda>x. (Y x, ())) {(x, y, ())} = joint_distribution X Y {(x, y)}"
hoelzl@39097
   951
  and "joint_distribution X (\<lambda>x. ((), Y x)) {(x, (), y)} = joint_distribution X Y {(x, y)}"
hoelzl@39097
   952
  and "distribution (\<lambda>x. ()) {()} = 1"
hoelzl@41981
   953
  by (auto intro!: arg_cong[where f=\<mu>'] simp: distribution_def prob_space[symmetric])
hoelzl@35977
   954
hoelzl@39097
   955
lemma (in finite_prob_space) setsum_distribution_gen:
hoelzl@39097
   956
  assumes "Z -` {c} \<inter> space M = (\<Union>x \<in> X`space M. Y -` {f x}) \<inter> space M"
hoelzl@39097
   957
  and "inj_on f (X`space M)"
hoelzl@39097
   958
  shows "(\<Sum>x \<in> X`space M. distribution Y {f x}) = distribution Z {c}"
hoelzl@39097
   959
  unfolding distribution_def assms
hoelzl@39097
   960
  using finite_space assms
hoelzl@41981
   961
  by (subst finite_measure_finite_Union[symmetric])
hoelzl@39097
   962
     (auto simp add: disjoint_family_on_def sets_eq_Pow inj_on_def
hoelzl@39097
   963
      intro!: arg_cong[where f=prob])
hoelzl@39097
   964
hoelzl@39097
   965
lemma (in finite_prob_space) setsum_distribution:
hoelzl@39097
   966
  "(\<Sum>x \<in> X`space M. joint_distribution X Y {(x, y)}) = distribution Y {y}"
hoelzl@39097
   967
  "(\<Sum>y \<in> Y`space M. joint_distribution X Y {(x, y)}) = distribution X {x}"
hoelzl@39097
   968
  "(\<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
   969
  "(\<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
   970
  "(\<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
   971
  by (auto intro!: inj_onI setsum_distribution_gen)
hoelzl@39097
   972
hoelzl@39097
   973
lemma (in finite_prob_space) uniform_prob:
hoelzl@39097
   974
  assumes "x \<in> space M"
hoelzl@39097
   975
  assumes "\<And> x y. \<lbrakk>x \<in> space M ; y \<in> space M\<rbrakk> \<Longrightarrow> prob {x} = prob {y}"
hoelzl@41981
   976
  shows "prob {x} = 1 / card (space M)"
hoelzl@39097
   977
proof -
hoelzl@39097
   978
  have prob_x: "\<And> y. y \<in> space M \<Longrightarrow> prob {y} = prob {x}"
hoelzl@39097
   979
    using assms(2)[OF _ `x \<in> space M`] by blast
hoelzl@39097
   980
  have "1 = prob (space M)"
hoelzl@39097
   981
    using prob_space by auto
hoelzl@39097
   982
  also have "\<dots> = (\<Sum> x \<in> space M. prob {x})"
hoelzl@41981
   983
    using finite_measure_finite_Union[of "space M" "\<lambda> x. {x}", simplified]
hoelzl@39097
   984
      sets_eq_Pow inj_singleton[unfolded inj_on_def, rule_format]
hoelzl@39097
   985
      finite_space unfolding disjoint_family_on_def  prob_space[symmetric]
hoelzl@39097
   986
    by (auto simp add:setsum_restrict_set)
hoelzl@39097
   987
  also have "\<dots> = (\<Sum> y \<in> space M. prob {x})"
hoelzl@39097
   988
    using prob_x by auto
hoelzl@39097
   989
  also have "\<dots> = real_of_nat (card (space M)) * prob {x}" by simp
hoelzl@39097
   990
  finally have one: "1 = real (card (space M)) * prob {x}"
hoelzl@39097
   991
    using real_eq_of_nat by auto
hoelzl@43339
   992
  hence two: "real (card (space M)) \<noteq> 0" by fastsimp
hoelzl@39097
   993
  from one have three: "prob {x} \<noteq> 0" by fastsimp
hoelzl@39097
   994
  thus ?thesis using one two three divide_cancel_right
hoelzl@39097
   995
    by (auto simp:field_simps)
hoelzl@39092
   996
qed
hoelzl@35977
   997
hoelzl@39092
   998
lemma (in prob_space) prob_space_subalgebra:
hoelzl@41545
   999
  assumes "sigma_algebra N" "sets N \<subseteq> sets M" "space N = space M"
hoelzl@41689
  1000
    and "\<And>A. A \<in> sets N \<Longrightarrow> measure N A = \<mu> A"
hoelzl@41689
  1001
  shows "prob_space N"
hoelzl@39092
  1002
proof -
hoelzl@41689
  1003
  interpret N: measure_space N
hoelzl@41689
  1004
    by (rule measure_space_subalgebra[OF assms])
hoelzl@39092
  1005
  show ?thesis
hoelzl@41689
  1006
  proof qed (insert assms(4)[OF N.top], simp add: assms measure_space_1)
hoelzl@35977
  1007
qed
hoelzl@35977
  1008
hoelzl@39092
  1009
lemma (in prob_space) prob_space_of_restricted_space:
hoelzl@41981
  1010
  assumes "\<mu> A \<noteq> 0" "A \<in> sets M"
hoelzl@41689
  1011
  shows "prob_space (restricted_space A \<lparr>measure := \<lambda>S. \<mu> S / \<mu> A\<rparr>)"
hoelzl@41689
  1012
    (is "prob_space ?P")
hoelzl@41689
  1013
proof -
hoelzl@41689
  1014
  interpret A: measure_space "restricted_space A"
hoelzl@39092
  1015
    using `A \<in> sets M` by (rule restricted_measure_space)
hoelzl@41689
  1016
  interpret A': sigma_algebra ?P
hoelzl@41689
  1017
    by (rule A.sigma_algebra_cong) auto
hoelzl@41689
  1018
  show "prob_space ?P"
hoelzl@39092
  1019
  proof
hoelzl@41689
  1020
    show "measure ?P (space ?P) = 1"
hoelzl@41981
  1021
      using real_measure[OF `A \<in> events`] `\<mu> A \<noteq> 0` by auto
hoelzl@41981
  1022
    show "positive ?P (measure ?P)"
hoelzl@41981
  1023
    proof (simp add: positive_def, safe)
hoelzl@41981
  1024
      show "0 / \<mu> A = 0" using `\<mu> A \<noteq> 0` by (cases "\<mu> A") (auto simp: zero_extreal_def)
hoelzl@41981
  1025
      fix B assume "B \<in> events"
hoelzl@41981
  1026
      with real_measure[of "A \<inter> B"] real_measure[OF `A \<in> events`] `A \<in> sets M`
hoelzl@41981
  1027
      show "0 \<le> \<mu> (A \<inter> B) / \<mu> A" by (auto simp: Int)
hoelzl@41981
  1028
    qed
hoelzl@41981
  1029
    show "countably_additive ?P (measure ?P)"
hoelzl@41981
  1030
    proof (simp add: countably_additive_def, safe)
hoelzl@41981
  1031
      fix B and F :: "nat \<Rightarrow> 'a set"
hoelzl@41981
  1032
      assume F: "range F \<subseteq> op \<inter> A ` events" "disjoint_family F"
hoelzl@41981
  1033
      { fix i
hoelzl@41981
  1034
        from F have "F i \<in> op \<inter> A ` events" by auto
hoelzl@41981
  1035
        with `A \<in> events` have "F i \<in> events" by auto }
hoelzl@41981
  1036
      moreover then have "range F \<subseteq> events" by auto
hoelzl@41981
  1037
      moreover have "\<And>S. \<mu> S / \<mu> A = inverse (\<mu> A) * \<mu> S"
hoelzl@41981
  1038
        by (simp add: mult_commute divide_extreal_def)
hoelzl@41981
  1039
      moreover have "0 \<le> inverse (\<mu> A)"
hoelzl@41981
  1040
        using real_measure[OF `A \<in> events`] by auto
hoelzl@41981
  1041
      ultimately show "(\<Sum>i. \<mu> (F i) / \<mu> A) = \<mu> (\<Union>i. F i) / \<mu> A"
hoelzl@41981
  1042
        using measure_countably_additive[of F] F
hoelzl@41981
  1043
        by (auto simp: suminf_cmult_extreal)
hoelzl@41981
  1044
    qed
hoelzl@39092
  1045
  qed
hoelzl@39092
  1046
qed
hoelzl@39092
  1047
hoelzl@39092
  1048
lemma finite_prob_spaceI:
hoelzl@41981
  1049
  assumes "finite (space M)" "sets M = Pow(space M)"
hoelzl@41981
  1050
    and "measure M (space M) = 1" "measure M {} = 0" "\<And>A. A \<subseteq> space M \<Longrightarrow> 0 \<le> measure M A"
hoelzl@41689
  1051
    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
  1052
  shows "finite_prob_space M"
hoelzl@39092
  1053
  unfolding finite_prob_space_eq
hoelzl@39092
  1054
proof
hoelzl@41689
  1055
  show "finite_measure_space M" using assms
hoelzl@41981
  1056
    by (auto intro!: finite_measure_spaceI)
hoelzl@41689
  1057
  show "measure M (space M) = 1" by fact
hoelzl@39092
  1058
qed
hoelzl@36624
  1059
hoelzl@36624
  1060
lemma (in finite_prob_space) finite_measure_space:
hoelzl@39097
  1061
  fixes X :: "'a \<Rightarrow> 'x"
hoelzl@41981
  1062
  shows "finite_measure_space \<lparr>space = X ` space M, sets = Pow (X ` space M), measure = extreal \<circ> distribution X\<rparr>"
hoelzl@41689
  1063
    (is "finite_measure_space ?S")
hoelzl@39092
  1064
proof (rule finite_measure_spaceI, simp_all)
hoelzl@36624
  1065
  show "finite (X ` space M)" using finite_space by simp
hoelzl@39097
  1066
next
hoelzl@39097
  1067
  fix A B :: "'x set" assume "A \<inter> B = {}"
hoelzl@39097
  1068
  then show "distribution X (A \<union> B) = distribution X A + distribution X B"
hoelzl@39097
  1069
    unfolding distribution_def
hoelzl@41981
  1070
    by (subst finite_measure_Union[symmetric])
hoelzl@41981
  1071
       (auto intro!: arg_cong[where f=\<mu>'] simp: sets_eq_Pow)
hoelzl@36624
  1072
qed
hoelzl@36624
  1073
hoelzl@39097
  1074
lemma (in finite_prob_space) finite_prob_space_of_images:
hoelzl@41981
  1075
  "finite_prob_space \<lparr> space = X ` space M, sets = Pow (X ` space M), measure = extreal \<circ> distribution X \<rparr>"
hoelzl@41981
  1076
  by (simp add: finite_prob_space_eq finite_measure_space measure_space_1 one_extreal_def)
hoelzl@39097
  1077
hoelzl@39096
  1078
lemma (in finite_prob_space) finite_product_measure_space:
hoelzl@39097
  1079
  fixes X :: "'a \<Rightarrow> 'x" and Y :: "'a \<Rightarrow> 'y"
hoelzl@39096
  1080
  assumes "finite s1" "finite s2"
hoelzl@41981
  1081
  shows "finite_measure_space \<lparr> space = s1 \<times> s2, sets = Pow (s1 \<times> s2), measure = extreal \<circ> joint_distribution X Y\<rparr>"
hoelzl@41689
  1082
    (is "finite_measure_space ?M")
hoelzl@39097
  1083
proof (rule finite_measure_spaceI, simp_all)
hoelzl@39097
  1084
  show "finite (s1 \<times> s2)"
hoelzl@39096
  1085
    using assms by auto
hoelzl@39097
  1086
next
hoelzl@39097
  1087
  fix A B :: "('x*'y) set" assume "A \<inter> B = {}"
hoelzl@39097
  1088
  then show "joint_distribution X Y (A \<union> B) = joint_distribution X Y A + joint_distribution X Y B"
hoelzl@39097
  1089
    unfolding distribution_def
hoelzl@41981
  1090
    by (subst finite_measure_Union[symmetric])
hoelzl@41981
  1091
       (auto intro!: arg_cong[where f=\<mu>'] simp: sets_eq_Pow)
hoelzl@39096
  1092
qed
hoelzl@39096
  1093
hoelzl@39097
  1094
lemma (in finite_prob_space) finite_product_measure_space_of_images:
hoelzl@39096
  1095
  shows "finite_measure_space \<lparr> space = X ` space M \<times> Y ` space M,
hoelzl@41689
  1096
                                sets = Pow (X ` space M \<times> Y ` space M),
hoelzl@41981
  1097
                                measure = extreal \<circ> joint_distribution X Y \<rparr>"
hoelzl@39096
  1098
  using finite_space by (auto intro!: finite_product_measure_space)
hoelzl@39096
  1099
hoelzl@40859
  1100
lemma (in finite_prob_space) finite_product_prob_space_of_images:
hoelzl@41689
  1101
  "finite_prob_space \<lparr> space = X ` space M \<times> Y ` space M, sets = Pow (X ` space M \<times> Y ` space M),
hoelzl@41981
  1102
                       measure = extreal \<circ> joint_distribution X Y \<rparr>"
hoelzl@41689
  1103
  (is "finite_prob_space ?S")
hoelzl@41981
  1104
proof (simp add: finite_prob_space_eq finite_product_measure_space_of_images one_extreal_def)
hoelzl@40859
  1105
  have "X -` X ` space M \<inter> Y -` Y ` space M \<inter> space M = space M" by auto
hoelzl@40859
  1106
  thus "joint_distribution X Y (X ` space M \<times> Y ` space M) = 1"
hoelzl@40859
  1107
    by (simp add: distribution_def prob_space vimage_Times comp_def measure_space_1)
hoelzl@40859
  1108
qed
hoelzl@40859
  1109
hoelzl@39085
  1110
section "Conditional Expectation and Probability"
hoelzl@39085
  1111
hoelzl@39085
  1112
lemma (in prob_space) conditional_expectation_exists:
hoelzl@41981
  1113
  fixes X :: "'a \<Rightarrow> extreal" and N :: "('a, 'b) measure_space_scheme"
hoelzl@41981
  1114
  assumes borel: "X \<in> borel_measurable M" "AE x. 0 \<le> X x"
hoelzl@41689
  1115
  and N: "sigma_algebra N" "sets N \<subseteq> sets M" "space N = space M" "\<And>A. measure N A = \<mu> A"
hoelzl@41981
  1116
  shows "\<exists>Y\<in>borel_measurable N. (\<forall>x. 0 \<le> Y x) \<and> (\<forall>C\<in>sets N.
hoelzl@41981
  1117
      (\<integral>\<^isup>+x. Y x * indicator C x \<partial>M) = (\<integral>\<^isup>+x. X x * indicator C x \<partial>M))"
hoelzl@39083
  1118
proof -
hoelzl@41689
  1119
  note N(4)[simp]
hoelzl@41689
  1120
  interpret P: prob_space N
hoelzl@41545
  1121
    using prob_space_subalgebra[OF N] .
hoelzl@39083
  1122
hoelzl@39083
  1123
  let "?f A" = "\<lambda>x. X x * indicator A x"
hoelzl@41689
  1124
  let "?Q A" = "integral\<^isup>P M (?f A)"
hoelzl@39083
  1125
hoelzl@39083
  1126
  from measure_space_density[OF borel]
hoelzl@41689
  1127
  have Q: "measure_space (N\<lparr> measure := ?Q \<rparr>)"
hoelzl@41689
  1128
    apply (rule measure_space.measure_space_subalgebra[of "M\<lparr> measure := ?Q \<rparr>"])
hoelzl@41689
  1129
    using N by (auto intro!: P.sigma_algebra_cong)
hoelzl@41689
  1130
  then interpret Q: measure_space "N\<lparr> measure := ?Q \<rparr>" .
hoelzl@39083
  1131
hoelzl@39083
  1132
  have "P.absolutely_continuous ?Q"
hoelzl@39083
  1133
    unfolding P.absolutely_continuous_def
hoelzl@41545
  1134
  proof safe
hoelzl@41689
  1135
    fix A assume "A \<in> sets N" "P.\<mu> A = 0"
hoelzl@41981
  1136
    then have f_borel: "?f A \<in> borel_measurable M" "AE x. x \<notin> A"
hoelzl@41981
  1137
      using borel N by (auto intro!: borel_measurable_indicator AE_not_in)
hoelzl@41981
  1138
    then show "?Q A = 0"
hoelzl@41981
  1139
      by (auto simp add: positive_integral_0_iff_AE)
hoelzl@39083
  1140
  qed
hoelzl@39083
  1141
  from P.Radon_Nikodym[OF Q this]
hoelzl@41981
  1142
  obtain Y where Y: "Y \<in> borel_measurable N" "\<And>x. 0 \<le> Y x"
hoelzl@41689
  1143
    "\<And>A. A \<in> sets N \<Longrightarrow> ?Q A =(\<integral>\<^isup>+x. Y x * indicator A x \<partial>N)"
hoelzl@39083
  1144
    by blast
hoelzl@41545
  1145
  with N(2) show ?thesis
hoelzl@41981
  1146
    by (auto intro!: bexI[OF _ Y(1)] simp: positive_integral_subalgebra[OF _ _ N(2,3,4,1)])
hoelzl@39083
  1147
qed
hoelzl@39083
  1148
hoelzl@39085
  1149
definition (in prob_space)
hoelzl@41981
  1150
  "conditional_expectation N X = (SOME Y. Y\<in>borel_measurable N \<and> (\<forall>x. 0 \<le> Y x)
hoelzl@41689
  1151
    \<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
  1152
hoelzl@39085
  1153
abbreviation (in prob_space)
hoelzl@39092
  1154
  "conditional_prob N A \<equiv> conditional_expectation N (indicator A)"
hoelzl@39085
  1155
hoelzl@39085
  1156
lemma (in prob_space)
hoelzl@41981
  1157
  fixes X :: "'a \<Rightarrow> extreal" and N :: "('a, 'b) measure_space_scheme"
hoelzl@41981
  1158
  assumes borel: "X \<in> borel_measurable M" "AE x. 0 \<le> X x"
hoelzl@41689
  1159
  and N: "sigma_algebra N" "sets N \<subseteq> sets M" "space N = space M" "\<And>A. measure N A = \<mu> A"
hoelzl@39085
  1160
  shows borel_measurable_conditional_expectation:
hoelzl@41545
  1161
    "conditional_expectation N X \<in> borel_measurable N"
hoelzl@41545
  1162
  and conditional_expectation: "\<And>C. C \<in> sets N \<Longrightarrow>
hoelzl@41689
  1163
      (\<integral>\<^isup>+x. conditional_expectation N X x * indicator C x \<partial>M) =
hoelzl@41689
  1164
      (\<integral>\<^isup>+x. X x * indicator C x \<partial>M)"
hoelzl@41545
  1165
   (is "\<And>C. C \<in> sets N \<Longrightarrow> ?eq C")
hoelzl@39085
  1166
proof -
hoelzl@39085
  1167
  note CE = conditional_expectation_exists[OF assms, unfolded Bex_def]
hoelzl@41545
  1168
  then show "conditional_expectation N X \<in> borel_measurable N"
hoelzl@39085
  1169
    unfolding conditional_expectation_def by (rule someI2_ex) blast
hoelzl@39085
  1170
hoelzl@41545
  1171
  from CE show "\<And>C. C \<in> sets N \<Longrightarrow> ?eq C"
hoelzl@39085
  1172
    unfolding conditional_expectation_def by (rule someI2_ex) blast
hoelzl@39085
  1173
qed
hoelzl@39085
  1174
hoelzl@41981
  1175
lemma (in sigma_algebra) factorize_measurable_function_pos:
hoelzl@41981
  1176
  fixes Z :: "'a \<Rightarrow> extreal" and Y :: "'a \<Rightarrow> 'c"
hoelzl@39091
  1177
  assumes "sigma_algebra M'" and "Y \<in> measurable M M'" "Z \<in> borel_measurable M"
hoelzl@41981
  1178
  assumes Z: "Z \<in> borel_measurable (sigma_algebra.vimage_algebra M' (space M) Y)"
hoelzl@41981
  1179
  shows "\<exists>g\<in>borel_measurable M'. \<forall>x\<in>space M. max 0 (Z x) = g (Y x)"
hoelzl@41981
  1180
proof -
hoelzl@39091
  1181
  interpret M': sigma_algebra M' by fact
hoelzl@39091
  1182
  have Y: "Y \<in> space M \<rightarrow> space M'" using assms unfolding measurable_def by auto
hoelzl@39091
  1183
  from M'.sigma_algebra_vimage[OF this]
hoelzl@39091
  1184
  interpret va: sigma_algebra "M'.vimage_algebra (space M) Y" .
hoelzl@39091
  1185
hoelzl@41981
  1186
  from va.borel_measurable_implies_simple_function_sequence'[OF Z] guess f . note f = this
hoelzl@39091
  1187
hoelzl@41689
  1188
  have "\<forall>i. \<exists>g. simple_function M' g \<and> (\<forall>x\<in>space M. f i x = g (Y x))"
hoelzl@39091
  1189
  proof
hoelzl@39091
  1190
    fix i
hoelzl@41981
  1191
    from f(1)[of i] have "finite (f i`space M)" and B_ex:
hoelzl@39091
  1192
      "\<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
  1193
      unfolding simple_function_def by auto
hoelzl@39091
  1194
    from B_ex[THEN bchoice] guess B .. note B = this
hoelzl@39091
  1195
hoelzl@39091
  1196
    let ?g = "\<lambda>x. \<Sum>z\<in>f i`space M. z * indicator (B z) x"
hoelzl@39091
  1197
hoelzl@41689
  1198
    show "\<exists>g. simple_function M' g \<and> (\<forall>x\<in>space M. f i x = g (Y x))"
hoelzl@39091
  1199
    proof (intro exI[of _ ?g] conjI ballI)
hoelzl@41689
  1200
      show "simple_function M' ?g" using B by auto
hoelzl@39091
  1201
hoelzl@39091
  1202
      fix x assume "x \<in> space M"
hoelzl@41981
  1203
      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
  1204
        unfolding indicator_def using B by auto
hoelzl@41981
  1205
      then show "f i x = ?g (Y x)" using `x \<in> space M` f(1)[of i]
hoelzl@39091
  1206
        by (subst va.simple_function_indicator_representation) auto
hoelzl@39091
  1207
    qed
hoelzl@39091
  1208
  qed
hoelzl@39091
  1209
  from choice[OF this] guess g .. note g = this
hoelzl@39091
  1210
hoelzl@41981
  1211
  show ?thesis
hoelzl@39091
  1212
  proof (intro ballI bexI)
hoelzl@41097
  1213
    show "(\<lambda>x. SUP i. g i x) \<in> borel_measurable M'"
hoelzl@39091
  1214
      using g by (auto intro: M'.borel_measurable_simple_function)
hoelzl@39091
  1215
    fix x assume "x \<in> space M"
hoelzl@41981
  1216
    have "max 0 (Z x) = (SUP i. f i x)" using f by simp
hoelzl@41981
  1217
    also have "\<dots> = (SUP i. g i (Y x))"
hoelzl@39091
  1218
      using g `x \<in> space M` by simp
hoelzl@41981
  1219
    finally show "max 0 (Z x) = (SUP i. g i (Y x))" .
hoelzl@41981
  1220
  qed
hoelzl@41981
  1221
qed
hoelzl@41981
  1222
hoelzl@41981
  1223
lemma (in sigma_algebra) factorize_measurable_function:
hoelzl@41981
  1224
  fixes Z :: "'a \<Rightarrow> extreal" and Y :: "'a \<Rightarrow> 'c"
hoelzl@41981
  1225
  assumes "sigma_algebra M'" and "Y \<in> measurable M M'" "Z \<in> borel_measurable M"
hoelzl@41981
  1226
  shows "Z \<in> borel_measurable (sigma_algebra.vimage_algebra M' (space M) Y)
hoelzl@41981
  1227
    \<longleftrightarrow> (\<exists>g\<in>borel_measurable M'. \<forall>x\<in>space M. Z x = g (Y x))"
hoelzl@41981
  1228
proof safe
hoelzl@41981
  1229
  interpret M': sigma_algebra M' by fact
hoelzl@41981
  1230
  have Y: "Y \<in> space M \<rightarrow> space M'" using assms unfolding measurable_def by auto
hoelzl@41981
  1231
  from M'.sigma_algebra_vimage[OF this]
hoelzl@41981
  1232
  interpret va: sigma_algebra "M'.vimage_algebra (space M) Y" .
hoelzl@41981
  1233
hoelzl@41981
  1234
  { fix g :: "'c \<Rightarrow> extreal" assume "g \<in> borel_measurable M'"
hoelzl@41981
  1235
    with M'.measurable_vimage_algebra[OF Y]
hoelzl@41981
  1236
    have "g \<circ> Y \<in> borel_measurable (M'.vimage_algebra (space M) Y)"
hoelzl@41981
  1237
      by (rule measurable_comp)
hoelzl@41981
  1238
    moreover assume "\<forall>x\<in>space M. Z x = g (Y x)"
hoelzl@41981
  1239
    then have "Z \<in> borel_measurable (M'.vimage_algebra (space M) Y) \<longleftrightarrow>
hoelzl@41981
  1240
       g \<circ> Y \<in> borel_measurable (M'.vimage_algebra (space M) Y)"
hoelzl@41981
  1241
       by (auto intro!: measurable_cong)
hoelzl@41981
  1242
    ultimately show "Z \<in> borel_measurable (M'.vimage_algebra (space M) Y)"
hoelzl@41981
  1243
      by simp }
hoelzl@41981
  1244
hoelzl@41981
  1245
  assume Z: "Z \<in> borel_measurable (M'.vimage_algebra (space M) Y)"
hoelzl@41981
  1246
  with assms have "(\<lambda>x. - Z x) \<in> borel_measurable M"
hoelzl@41981
  1247
    "(\<lambda>x. - Z x) \<in> borel_measurable (M'.vimage_algebra (space M) Y)"
hoelzl@41981
  1248
    by auto
hoelzl@41981
  1249
  from factorize_measurable_function_pos[OF assms(1,2) this] guess n .. note n = this
hoelzl@41981
  1250
  from factorize_measurable_function_pos[OF assms Z] guess p .. note p = this
hoelzl@41981
  1251
  let "?g x" = "p x - n x"
hoelzl@41981
  1252
  show "\<exists>g\<in>borel_measurable M'. \<forall>x\<in>space M. Z x = g (Y x)"
hoelzl@41981
  1253
  proof (intro bexI ballI)
hoelzl@41981
  1254
    show "?g \<in> borel_measurable M'" using p n by auto
hoelzl@41981
  1255
    fix x assume "x \<in> space M"
hoelzl@41981
  1256
    then have "p (Y x) = max 0 (Z x)" "n (Y x) = max 0 (- Z x)"
hoelzl@41981
  1257
      using p n by auto
hoelzl@41981
  1258
    then show "Z x = ?g (Y x)"
hoelzl@41981
  1259
      by (auto split: split_max)
hoelzl@39091
  1260
  qed
hoelzl@39091
  1261
qed
hoelzl@39090
  1262
hoelzl@42902
  1263
subsection "Borel Measure on {0 .. 1}"
hoelzl@42902
  1264
hoelzl@42902
  1265
definition pborel :: "real measure_space" where
hoelzl@42902
  1266
  "pborel = lborel.restricted_space {0 .. 1}"
hoelzl@42902
  1267
hoelzl@42902
  1268
lemma space_pborel[simp]:
hoelzl@42902
  1269
  "space pborel = {0 .. 1}"
hoelzl@42902
  1270
  unfolding pborel_def by auto
hoelzl@42902
  1271
hoelzl@42902
  1272
lemma sets_pborel:
hoelzl@42902
  1273
  "A \<in> sets pborel \<longleftrightarrow> A \<in> sets borel \<and> A \<subseteq> { 0 .. 1}"
hoelzl@42902
  1274
  unfolding pborel_def by auto
hoelzl@42902
  1275
hoelzl@42902
  1276
lemma in_pborel[intro, simp]:
hoelzl@42902
  1277
  "A \<subseteq> {0 .. 1} \<Longrightarrow> A \<in> sets borel \<Longrightarrow> A \<in> sets pborel"
hoelzl@42902
  1278
  unfolding pborel_def by auto
hoelzl@42902
  1279
hoelzl@42902
  1280
interpretation pborel: measure_space pborel
hoelzl@42902
  1281
  using lborel.restricted_measure_space[of "{0 .. 1}"]
hoelzl@42902
  1282
  by (simp add: pborel_def)
hoelzl@42902
  1283
hoelzl@42902
  1284
interpretation pborel: prob_space pborel
hoelzl@42902
  1285
  by default (simp add: one_extreal_def pborel_def)
hoelzl@42902
  1286
hoelzl@42902
  1287
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
  1288
  unfolding pborel.\<mu>'_def by (auto simp: pborel_def)
hoelzl@42902
  1289
hoelzl@42902
  1290
lemma pborel_singelton[simp]: "pborel.prob {a} = 0"
hoelzl@42902
  1291
  by (auto simp: pborel_prob)
hoelzl@42902
  1292
hoelzl@42902
  1293
lemma
hoelzl@42902
  1294
  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
  1295
    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
  1296
    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
  1297
    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
  1298
  unfolding pborel_prob by (auto simp: atLeastLessThan_subseteq_atLeastAtMost_iff
hoelzl@42902
  1299
    greaterThanAtMost_subseteq_atLeastAtMost_iff greaterThanLessThan_subseteq_atLeastAtMost_iff)
hoelzl@42902
  1300
hoelzl@42902
  1301
lemma pborel_lebesgue_measure:
hoelzl@42902
  1302
  "A \<in> sets pborel \<Longrightarrow> pborel.prob A = real (measure lebesgue A)"
hoelzl@42902
  1303
  by (simp add: sets_pborel pborel_prob)
hoelzl@42902
  1304
hoelzl@42902
  1305
lemma pborel_alt:
hoelzl@42902
  1306
  "pborel = sigma \<lparr>
hoelzl@42902
  1307
    space = {0..1},
hoelzl@42902
  1308
    sets = range (\<lambda>(x,y). {x..y} \<inter> {0..1}),
hoelzl@42902
  1309
    measure = measure lborel \<rparr>" (is "_ = ?R")
hoelzl@42902
  1310
proof -
hoelzl@42902
  1311
  have *: "{0..1::real} \<in> sets borel" by auto
hoelzl@42902
  1312
  have **: "op \<inter> {0..1::real} ` range (\<lambda>(x, y). {x..y}) = range (\<lambda>(x,y). {x..y} \<inter> {0..1})"
hoelzl@42902
  1313
    unfolding image_image by (intro arg_cong[where f=range]) auto
hoelzl@42902
  1314
  have "pborel = algebra.restricted_space (sigma \<lparr>space=UNIV, sets=range (\<lambda>(a, b). {a .. b :: real}),
hoelzl@42902
  1315
    measure = measure pborel\<rparr>) {0 .. 1}"
hoelzl@42902
  1316
    by (simp add: sigma_def lebesgue_def pborel_def borel_eq_atLeastAtMost lborel_def)
hoelzl@42902
  1317
  also have "\<dots> = ?R"
hoelzl@42902
  1318
    by (subst restricted_sigma)
hoelzl@42902
  1319
       (simp_all add: sets_sigma sigma_sets.Basic ** pborel_def image_eqI[of _ _ "(0,1)"])
hoelzl@42902
  1320
  finally show ?thesis .
hoelzl@42902
  1321
qed
hoelzl@42902
  1322
hoelzl@42860
  1323
subsection "Bernoulli space"
hoelzl@42860
  1324
hoelzl@42860
  1325
definition "bernoulli_space p = \<lparr> space = UNIV, sets = UNIV,
hoelzl@42860
  1326
  measure = extreal \<circ> setsum (\<lambda>b. if b then min 1 (max 0 p) else 1 - min 1 (max 0 p)) \<rparr>"
hoelzl@42860
  1327
hoelzl@42860
  1328
interpretation bernoulli: finite_prob_space "bernoulli_space p" for p
hoelzl@42860
  1329
  by (rule finite_prob_spaceI)
hoelzl@42860
  1330
     (auto simp: bernoulli_space_def UNIV_bool one_extreal_def setsum_Un_disjoint intro!: setsum_nonneg)
hoelzl@42860
  1331
hoelzl@42860
  1332
lemma bernoulli_measure:
hoelzl@42860
  1333
  "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
  1334
  unfolding bernoulli.\<mu>'_def unfolding bernoulli_space_def by (auto intro!: setsum_cong)
hoelzl@42860
  1335
hoelzl@42860
  1336
lemma bernoulli_measure_True: "0 \<le> p \<Longrightarrow> p \<le> 1 \<Longrightarrow> bernoulli.prob p {True} = p"
hoelzl@42860
  1337
  and bernoulli_measure_False: "0 \<le> p \<Longrightarrow> p \<le> 1 \<Longrightarrow> bernoulli.prob p {False} = 1 - p"
hoelzl@42860
  1338
  unfolding bernoulli_measure by simp_all
hoelzl@42860
  1339
hoelzl@35582
  1340
end