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