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