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