src/HOL/Probability/Radon_Nikodym.thy
changeset 56996 891e992e510f
parent 56994 8d5e5ec1cac3
child 57447 87429bdecad5
equal deleted inserted replaced
56995:61855ade6c7e 56996:891e992e510f
    42       by (simp add: suminf)
    42       by (simp add: suminf)
    43   qed
    43   qed
    44 qed fact
    44 qed fact
    45 
    45 
    46 lemma (in sigma_finite_measure) Ex_finite_integrable_function:
    46 lemma (in sigma_finite_measure) Ex_finite_integrable_function:
    47   shows "\<exists>h\<in>borel_measurable M. integral\<^sup>P M h \<noteq> \<infinity> \<and> (\<forall>x\<in>space M. 0 < h x \<and> h x < \<infinity>) \<and> (\<forall>x. 0 \<le> h x)"
    47   shows "\<exists>h\<in>borel_measurable M. integral\<^sup>N M h \<noteq> \<infinity> \<and> (\<forall>x\<in>space M. 0 < h x \<and> h x < \<infinity>) \<and> (\<forall>x. 0 \<le> h x)"
    48 proof -
    48 proof -
    49   obtain A :: "nat \<Rightarrow> 'a set" where
    49   obtain A :: "nat \<Rightarrow> 'a set" where
    50     range[measurable]: "range A \<subseteq> sets M" and
    50     range[measurable]: "range A \<subseteq> sets M" and
    51     space: "(\<Union>i. A i) = space M" and
    51     space: "(\<Union>i. A i) = space M" and
    52     measure: "\<And>i. emeasure M (A i) \<noteq> \<infinity>" and
    52     measure: "\<And>i. emeasure M (A i) \<noteq> \<infinity>" and
    65   let ?h = "\<lambda>x. \<Sum>i. n i * indicator (A i) x"
    65   let ?h = "\<lambda>x. \<Sum>i. n i * indicator (A i) x"
    66   show ?thesis
    66   show ?thesis
    67   proof (safe intro!: bexI[of _ ?h] del: notI)
    67   proof (safe intro!: bexI[of _ ?h] del: notI)
    68     have "\<And>i. A i \<in> sets M"
    68     have "\<And>i. A i \<in> sets M"
    69       using range by fastforce+
    69       using range by fastforce+
    70     then have "integral\<^sup>P M ?h = (\<Sum>i. n i * emeasure M (A i))" using pos
    70     then have "integral\<^sup>N M ?h = (\<Sum>i. n i * emeasure M (A i))" using pos
    71       by (simp add: positive_integral_suminf positive_integral_cmult_indicator)
    71       by (simp add: nn_integral_suminf nn_integral_cmult_indicator)
    72     also have "\<dots> \<le> (\<Sum>i. (1 / 2)^Suc i)"
    72     also have "\<dots> \<le> (\<Sum>i. (1 / 2)^Suc i)"
    73     proof (rule suminf_le_pos)
    73     proof (rule suminf_le_pos)
    74       fix N
    74       fix N
    75       have "n N * emeasure M (A N) \<le> inverse (2^Suc N * emeasure M (A N)) * emeasure M (A N)"
    75       have "n N * emeasure M (A N) \<le> inverse (2^Suc N * emeasure M (A N)) * emeasure M (A N)"
    76         using n[of N]
    76         using n[of N]
    80         by (cases rule: ereal2_cases[of "n N" "emeasure M (A N)"])
    80         by (cases rule: ereal2_cases[of "n N" "emeasure M (A N)"])
    81            (simp_all add: inverse_eq_divide power_divide one_ereal_def ereal_power_divide)
    81            (simp_all add: inverse_eq_divide power_divide one_ereal_def ereal_power_divide)
    82       finally show "n N * emeasure M (A N) \<le> (1 / 2) ^ Suc N" .
    82       finally show "n N * emeasure M (A N) \<le> (1 / 2) ^ Suc N" .
    83       show "0 \<le> n N * emeasure M (A N)" using n[of N] `A N \<in> sets M` by (simp add: emeasure_nonneg)
    83       show "0 \<le> n N * emeasure M (A N)" using n[of N] `A N \<in> sets M` by (simp add: emeasure_nonneg)
    84     qed
    84     qed
    85     finally show "integral\<^sup>P M ?h \<noteq> \<infinity>" unfolding suminf_half_series_ereal by auto
    85     finally show "integral\<^sup>N M ?h \<noteq> \<infinity>" unfolding suminf_half_series_ereal by auto
    86   next
    86   next
    87     { fix x assume "x \<in> space M"
    87     { fix x assume "x \<in> space M"
    88       then obtain i where "x \<in> A i" using space[symmetric] by auto
    88       then obtain i where "x \<in> A i" using space[symmetric] by auto
    89       with disjoint n have "?h x = n i"
    89       with disjoint n have "?h x = n i"
    90         by (auto intro!: suminf_cmult_indicator intro: less_imp_le)
    90         by (auto intro!: suminf_cmult_indicator intro: less_imp_le)
   315         by (auto simp: indicator_def max_def)
   315         by (auto simp: indicator_def max_def)
   316       hence "(\<integral>\<^sup>+x. max (g x) (f x) * indicator A x \<partial>M) =
   316       hence "(\<integral>\<^sup>+x. max (g x) (f x) * indicator A x \<partial>M) =
   317         (\<integral>\<^sup>+x. g x * indicator (?A \<inter> A) x \<partial>M) +
   317         (\<integral>\<^sup>+x. g x * indicator (?A \<inter> A) x \<partial>M) +
   318         (\<integral>\<^sup>+x. f x * indicator ((space M - ?A) \<inter> A) x \<partial>M)"
   318         (\<integral>\<^sup>+x. f x * indicator ((space M - ?A) \<inter> A) x \<partial>M)"
   319         using f g sets unfolding G_def
   319         using f g sets unfolding G_def
   320         by (auto cong: positive_integral_cong intro!: positive_integral_add)
   320         by (auto cong: nn_integral_cong intro!: nn_integral_add)
   321       also have "\<dots> \<le> N (?A \<inter> A) + N ((space M - ?A) \<inter> A)"
   321       also have "\<dots> \<le> N (?A \<inter> A) + N ((space M - ?A) \<inter> A)"
   322         using f g sets unfolding G_def by (auto intro!: add_mono)
   322         using f g sets unfolding G_def by (auto intro!: add_mono)
   323       also have "\<dots> = N A"
   323       also have "\<dots> = N A"
   324         using plus_emeasure[OF sets'] union by auto
   324         using plus_emeasure[OF sets'] union by auto
   325       finally show "(\<integral>\<^sup>+x. max (g x) (f x) * indicator A x \<partial>M) \<le> N A" .
   325       finally show "(\<integral>\<^sup>+x. max (g x) (f x) * indicator A x \<partial>M) \<le> N A" .
   336           using f by (auto simp: G_def intro: SUP_upper2) }
   336           using f by (auto simp: G_def intro: SUP_upper2) }
   337     next
   337     next
   338       fix A assume "A \<in> sets M"
   338       fix A assume "A \<in> sets M"
   339       have "(\<integral>\<^sup>+x. (SUP i. f i x) * indicator A x \<partial>M) =
   339       have "(\<integral>\<^sup>+x. (SUP i. f i x) * indicator A x \<partial>M) =
   340         (\<integral>\<^sup>+x. (SUP i. f i x * indicator A x) \<partial>M)"
   340         (\<integral>\<^sup>+x. (SUP i. f i x * indicator A x) \<partial>M)"
   341         by (intro positive_integral_cong) (simp split: split_indicator)
   341         by (intro nn_integral_cong) (simp split: split_indicator)
   342       also have "\<dots> = (SUP i. (\<integral>\<^sup>+x. f i x * indicator A x \<partial>M))"
   342       also have "\<dots> = (SUP i. (\<integral>\<^sup>+x. f i x * indicator A x \<partial>M))"
   343         using `incseq f` f `A \<in> sets M`
   343         using `incseq f` f `A \<in> sets M`
   344         by (intro positive_integral_monotone_convergence_SUP)
   344         by (intro nn_integral_monotone_convergence_SUP)
   345            (auto simp: G_def incseq_Suc_iff le_fun_def split: split_indicator)
   345            (auto simp: G_def incseq_Suc_iff le_fun_def split: split_indicator)
   346       finally show "(\<integral>\<^sup>+x. (SUP i. f i x) * indicator A x \<partial>M) \<le> N A"
   346       finally show "(\<integral>\<^sup>+x. (SUP i. f i x) * indicator A x \<partial>M) \<le> N A"
   347         using f `A \<in> sets M` by (auto intro!: SUP_least simp: G_def)
   347         using f `A \<in> sets M` by (auto intro!: SUP_least simp: G_def)
   348     qed }
   348     qed }
   349   note SUP_in_G = this
   349   note SUP_in_G = this
   350   let ?y = "SUP g : G. integral\<^sup>P M g"
   350   let ?y = "SUP g : G. integral\<^sup>N M g"
   351   have y_le: "?y \<le> N (space M)" unfolding G_def
   351   have y_le: "?y \<le> N (space M)" unfolding G_def
   352   proof (safe intro!: SUP_least)
   352   proof (safe intro!: SUP_least)
   353     fix g assume "\<forall>A\<in>sets M. (\<integral>\<^sup>+x. g x * indicator A x \<partial>M) \<le> N A"
   353     fix g assume "\<forall>A\<in>sets M. (\<integral>\<^sup>+x. g x * indicator A x \<partial>M) \<le> N A"
   354     from this[THEN bspec, OF sets.top] show "integral\<^sup>P M g \<le> N (space M)"
   354     from this[THEN bspec, OF sets.top] show "integral\<^sup>N M g \<le> N (space M)"
   355       by (simp cong: positive_integral_cong)
   355       by (simp cong: nn_integral_cong)
   356   qed
   356   qed
   357   from SUP_countable_SUP [OF `G \<noteq> {}`, of "integral\<^sup>P M"] guess ys .. note ys = this
   357   from SUP_countable_SUP [OF `G \<noteq> {}`, of "integral\<^sup>N M"] guess ys .. note ys = this
   358   then have "\<forall>n. \<exists>g. g\<in>G \<and> integral\<^sup>P M g = ys n"
   358   then have "\<forall>n. \<exists>g. g\<in>G \<and> integral\<^sup>N M g = ys n"
   359   proof safe
   359   proof safe
   360     fix n assume "range ys \<subseteq> integral\<^sup>P M ` G"
   360     fix n assume "range ys \<subseteq> integral\<^sup>N M ` G"
   361     hence "ys n \<in> integral\<^sup>P M ` G" by auto
   361     hence "ys n \<in> integral\<^sup>N M ` G" by auto
   362     thus "\<exists>g. g\<in>G \<and> integral\<^sup>P M g = ys n" by auto
   362     thus "\<exists>g. g\<in>G \<and> integral\<^sup>N M g = ys n" by auto
   363   qed
   363   qed
   364   from choice[OF this] obtain gs where "\<And>i. gs i \<in> G" "\<And>n. integral\<^sup>P M (gs n) = ys n" by auto
   364   from choice[OF this] obtain gs where "\<And>i. gs i \<in> G" "\<And>n. integral\<^sup>N M (gs n) = ys n" by auto
   365   hence y_eq: "?y = (SUP i. integral\<^sup>P M (gs i))" using ys by auto
   365   hence y_eq: "?y = (SUP i. integral\<^sup>N M (gs i))" using ys by auto
   366   let ?g = "\<lambda>i x. Max ((\<lambda>n. gs n x) ` {..i})"
   366   let ?g = "\<lambda>i x. Max ((\<lambda>n. gs n x) ` {..i})"
   367   def f \<equiv> "\<lambda>x. SUP i. ?g i x"
   367   def f \<equiv> "\<lambda>x. SUP i. ?g i x"
   368   let ?F = "\<lambda>A x. f x * indicator A x"
   368   let ?F = "\<lambda>A x. f x * indicator A x"
   369   have gs_not_empty: "\<And>i x. (\<lambda>n. gs n x) ` {..i} \<noteq> {}" by auto
   369   have gs_not_empty: "\<And>i x. (\<lambda>n. gs n x) ` {..i} \<noteq> {}" by auto
   370   { fix i have "?g i \<in> G"
   370   { fix i have "?g i \<in> G"
   378   note g_in_G = this
   378   note g_in_G = this
   379   have "incseq ?g" using gs_not_empty
   379   have "incseq ?g" using gs_not_empty
   380     by (auto intro!: incseq_SucI le_funI simp add: atMost_Suc)
   380     by (auto intro!: incseq_SucI le_funI simp add: atMost_Suc)
   381   from SUP_in_G[OF this g_in_G] have [measurable]: "f \<in> G" unfolding f_def .
   381   from SUP_in_G[OF this g_in_G] have [measurable]: "f \<in> G" unfolding f_def .
   382   then have [simp, intro]: "f \<in> borel_measurable M" unfolding G_def by auto
   382   then have [simp, intro]: "f \<in> borel_measurable M" unfolding G_def by auto
   383   have "integral\<^sup>P M f = (SUP i. integral\<^sup>P M (?g i))" unfolding f_def
   383   have "integral\<^sup>N M f = (SUP i. integral\<^sup>N M (?g i))" unfolding f_def
   384     using g_in_G `incseq ?g`
   384     using g_in_G `incseq ?g`
   385     by (auto intro!: positive_integral_monotone_convergence_SUP simp: G_def)
   385     by (auto intro!: nn_integral_monotone_convergence_SUP simp: G_def)
   386   also have "\<dots> = ?y"
   386   also have "\<dots> = ?y"
   387   proof (rule antisym)
   387   proof (rule antisym)
   388     show "(SUP i. integral\<^sup>P M (?g i)) \<le> ?y"
   388     show "(SUP i. integral\<^sup>N M (?g i)) \<le> ?y"
   389       using g_in_G by (auto intro: SUP_mono)
   389       using g_in_G by (auto intro: SUP_mono)
   390     show "?y \<le> (SUP i. integral\<^sup>P M (?g i))" unfolding y_eq
   390     show "?y \<le> (SUP i. integral\<^sup>N M (?g i))" unfolding y_eq
   391       by (auto intro!: SUP_mono positive_integral_mono Max_ge)
   391       by (auto intro!: SUP_mono nn_integral_mono Max_ge)
   392   qed
   392   qed
   393   finally have int_f_eq_y: "integral\<^sup>P M f = ?y" .
   393   finally have int_f_eq_y: "integral\<^sup>N M f = ?y" .
   394   have "\<And>x. 0 \<le> f x"
   394   have "\<And>x. 0 \<le> f x"
   395     unfolding f_def using `\<And>i. gs i \<in> G`
   395     unfolding f_def using `\<And>i. gs i \<in> G`
   396     by (auto intro!: SUP_upper2 Max_ge_iff[THEN iffD2] simp: G_def)
   396     by (auto intro!: SUP_upper2 Max_ge_iff[THEN iffD2] simp: G_def)
   397   let ?t = "\<lambda>A. N A - (\<integral>\<^sup>+x. ?F A x \<partial>M)"
   397   let ?t = "\<lambda>A. N A - (\<integral>\<^sup>+x. ?F A x \<partial>M)"
   398   let ?M = "diff_measure N (density M f)"
   398   let ?M = "diff_measure N (density M f)"
   399   have f_le_N: "\<And>A. A \<in> sets M \<Longrightarrow> (\<integral>\<^sup>+x. ?F A x \<partial>M) \<le> N A"
   399   have f_le_N: "\<And>A. A \<in> sets M \<Longrightarrow> (\<integral>\<^sup>+x. ?F A x \<partial>M) \<le> N A"
   400     using `f \<in> G` unfolding G_def by auto
   400     using `f \<in> G` unfolding G_def by auto
   401   have emeasure_M: "\<And>A. A \<in> sets M \<Longrightarrow> emeasure ?M A = ?t A"
   401   have emeasure_M: "\<And>A. A \<in> sets M \<Longrightarrow> emeasure ?M A = ?t A"
   402   proof (subst emeasure_diff_measure)
   402   proof (subst emeasure_diff_measure)
   403     from f_le_N[of "space M"] show "finite_measure N" "finite_measure (density M f)"
   403     from f_le_N[of "space M"] show "finite_measure N" "finite_measure (density M f)"
   404       by (auto intro!: finite_measureI simp: emeasure_density cong: positive_integral_cong)
   404       by (auto intro!: finite_measureI simp: emeasure_density cong: nn_integral_cong)
   405   next
   405   next
   406     fix B assume "B \<in> sets N" with f_le_N[of B] show "emeasure (density M f) B \<le> emeasure N B"
   406     fix B assume "B \<in> sets N" with f_le_N[of B] show "emeasure (density M f) B \<le> emeasure N B"
   407       by (auto simp: sets_eq emeasure_density cong: positive_integral_cong)
   407       by (auto simp: sets_eq emeasure_density cong: nn_integral_cong)
   408   qed (auto simp: sets_eq emeasure_density)
   408   qed (auto simp: sets_eq emeasure_density)
   409   from emeasure_M[of "space M"] N.finite_emeasure_space positive_integral_positive[of M "?F (space M)"]
   409   from emeasure_M[of "space M"] N.finite_emeasure_space nn_integral_nonneg[of M "?F (space M)"]
   410   interpret M': finite_measure ?M
   410   interpret M': finite_measure ?M
   411     by (auto intro!: finite_measureI simp: sets_eq_imp_space_eq[OF sets_eq] N.emeasure_eq_measure ereal_minus_eq_PInfty_iff)
   411     by (auto intro!: finite_measureI simp: sets_eq_imp_space_eq[OF sets_eq] N.emeasure_eq_measure ereal_minus_eq_PInfty_iff)
   412 
   412 
   413   have ac: "absolutely_continuous M ?M" unfolding absolutely_continuous_def
   413   have ac: "absolutely_continuous M ?M" unfolding absolutely_continuous_def
   414   proof
   414   proof
   415     fix A assume A_M: "A \<in> null_sets M"
   415     fix A assume A_M: "A \<in> null_sets M"
   416     with `absolutely_continuous M N` have A_N: "A \<in> null_sets N"
   416     with `absolutely_continuous M N` have A_N: "A \<in> null_sets N"
   417       unfolding absolutely_continuous_def by auto
   417       unfolding absolutely_continuous_def by auto
   418     moreover from A_M A_N have "(\<integral>\<^sup>+ x. ?F A x \<partial>M) \<le> N A" using `f \<in> G` by (auto simp: G_def)
   418     moreover from A_M A_N have "(\<integral>\<^sup>+ x. ?F A x \<partial>M) \<le> N A" using `f \<in> G` by (auto simp: G_def)
   419     ultimately have "N A - (\<integral>\<^sup>+ x. ?F A x \<partial>M) = 0"
   419     ultimately have "N A - (\<integral>\<^sup>+ x. ?F A x \<partial>M) = 0"
   420       using positive_integral_positive[of M] by (auto intro!: antisym)
   420       using nn_integral_nonneg[of M] by (auto intro!: antisym)
   421     then show "A \<in> null_sets ?M"
   421     then show "A \<in> null_sets ?M"
   422       using A_M by (simp add: emeasure_M null_sets_def sets_eq)
   422       using A_M by (simp add: emeasure_M null_sets_def sets_eq)
   423   qed
   423   qed
   424   have upper_bound: "\<forall>A\<in>sets M. ?M A \<le> 0"
   424   have upper_bound: "\<forall>A\<in>sets M. ?M A \<le> 0"
   425   proof (rule ccontr)
   425   proof (rule ccontr)
   460     let ?f0 = "\<lambda>x. f x + b * indicator A0 x"
   460     let ?f0 = "\<lambda>x. f x + b * indicator A0 x"
   461     { fix A assume A: "A \<in> sets M"
   461     { fix A assume A: "A \<in> sets M"
   462       hence "A \<inter> A0 \<in> sets M" using `A0 \<in> sets M` by auto
   462       hence "A \<inter> A0 \<in> sets M" using `A0 \<in> sets M` by auto
   463       have "(\<integral>\<^sup>+x. ?f0 x  * indicator A x \<partial>M) =
   463       have "(\<integral>\<^sup>+x. ?f0 x  * indicator A x \<partial>M) =
   464         (\<integral>\<^sup>+x. f x * indicator A x + b * indicator (A \<inter> A0) x \<partial>M)"
   464         (\<integral>\<^sup>+x. f x * indicator A x + b * indicator (A \<inter> A0) x \<partial>M)"
   465         by (auto intro!: positive_integral_cong split: split_indicator)
   465         by (auto intro!: nn_integral_cong split: split_indicator)
   466       hence "(\<integral>\<^sup>+x. ?f0 x * indicator A x \<partial>M) =
   466       hence "(\<integral>\<^sup>+x. ?f0 x * indicator A x \<partial>M) =
   467           (\<integral>\<^sup>+x. f x * indicator A x \<partial>M) + b * emeasure M (A \<inter> A0)"
   467           (\<integral>\<^sup>+x. f x * indicator A x \<partial>M) + b * emeasure M (A \<inter> A0)"
   468         using `A0 \<in> sets M` `A \<inter> A0 \<in> sets M` A b `f \<in> G`
   468         using `A0 \<in> sets M` `A \<inter> A0 \<in> sets M` A b `f \<in> G`
   469         by (simp add: positive_integral_add positive_integral_cmult_indicator G_def) }
   469         by (simp add: nn_integral_add nn_integral_cmult_indicator G_def) }
   470     note f0_eq = this
   470     note f0_eq = this
   471     { fix A assume A: "A \<in> sets M"
   471     { fix A assume A: "A \<in> sets M"
   472       hence "A \<inter> A0 \<in> sets M" using `A0 \<in> sets M` by auto
   472       hence "A \<inter> A0 \<in> sets M" using `A0 \<in> sets M` by auto
   473       have f_le_v: "(\<integral>\<^sup>+x. ?F A x \<partial>M) \<le> N A" using `f \<in> G` A unfolding G_def by auto
   473       have f_le_v: "(\<integral>\<^sup>+x. ?F A x \<partial>M) \<le> N A" using `f \<in> G` A unfolding G_def by auto
   474       note f0_eq[OF A]
   474       note f0_eq[OF A]
   478       also have "\<dots> \<le> (\<integral>\<^sup>+x. f x * indicator A x \<partial>M) + ?M A"
   478       also have "\<dots> \<le> (\<integral>\<^sup>+x. f x * indicator A x \<partial>M) + ?M A"
   479         using emeasure_mono[of "A \<inter> A0" A ?M] `A \<in> sets M` `A0 \<in> sets M`
   479         using emeasure_mono[of "A \<inter> A0" A ?M] `A \<in> sets M` `A0 \<in> sets M`
   480         by (auto intro!: add_left_mono simp: sets_eq)
   480         by (auto intro!: add_left_mono simp: sets_eq)
   481       also have "\<dots> \<le> N A"
   481       also have "\<dots> \<le> N A"
   482         unfolding emeasure_M[OF `A \<in> sets M`]
   482         unfolding emeasure_M[OF `A \<in> sets M`]
   483         using f_le_v N.emeasure_eq_measure[of A] positive_integral_positive[of M "?F A"]
   483         using f_le_v N.emeasure_eq_measure[of A] nn_integral_nonneg[of M "?F A"]
   484         by (cases "\<integral>\<^sup>+x. ?F A x \<partial>M", cases "N A") auto
   484         by (cases "\<integral>\<^sup>+x. ?F A x \<partial>M", cases "N A") auto
   485       finally have "(\<integral>\<^sup>+x. ?f0 x * indicator A x \<partial>M) \<le> N A" . }
   485       finally have "(\<integral>\<^sup>+x. ?f0 x * indicator A x \<partial>M) \<le> N A" . }
   486     hence "?f0 \<in> G" using `A0 \<in> sets M` b `f \<in> G` by (auto simp: G_def)
   486     hence "?f0 \<in> G" using `A0 \<in> sets M` b `f \<in> G` by (auto simp: G_def)
   487     have int_f_finite: "integral\<^sup>P M f \<noteq> \<infinity>"
   487     have int_f_finite: "integral\<^sup>N M f \<noteq> \<infinity>"
   488       by (metis N.emeasure_finite ereal_infty_less_eq2(1) int_f_eq_y y_le)
   488       by (metis N.emeasure_finite ereal_infty_less_eq2(1) int_f_eq_y y_le)
   489     have  "0 < ?M (space M) - emeasure ?Mb (space M)"
   489     have  "0 < ?M (space M) - emeasure ?Mb (space M)"
   490       using pos_t
   490       using pos_t
   491       by (simp add: b emeasure_density_const)
   491       by (simp add: b emeasure_density_const)
   492          (simp add: M'.emeasure_eq_measure emeasure_eq_measure pos_M b_def)
   492          (simp add: M'.emeasure_eq_measure emeasure_eq_measure pos_M b_def)
   501                ereal_mult_eq_MInfty ereal_mult_eq_PInfty ereal_zero_less_0_iff less_eq_ereal_def)
   501                ereal_mult_eq_MInfty ereal_mult_eq_PInfty ereal_zero_less_0_iff less_eq_ereal_def)
   502     then have "emeasure M A0 \<noteq> 0" using ac `A0 \<in> sets M`
   502     then have "emeasure M A0 \<noteq> 0" using ac `A0 \<in> sets M`
   503       by (auto simp: absolutely_continuous_def null_sets_def)
   503       by (auto simp: absolutely_continuous_def null_sets_def)
   504     then have "0 < emeasure M A0" using emeasure_nonneg[of M A0] by auto
   504     then have "0 < emeasure M A0" using emeasure_nonneg[of M A0] by auto
   505     hence "0 < b * emeasure M A0" using b by (auto simp: ereal_zero_less_0_iff)
   505     hence "0 < b * emeasure M A0" using b by (auto simp: ereal_zero_less_0_iff)
   506     with int_f_finite have "?y + 0 < integral\<^sup>P M f + b * emeasure M A0" unfolding int_f_eq_y
   506     with int_f_finite have "?y + 0 < integral\<^sup>N M f + b * emeasure M A0" unfolding int_f_eq_y
   507       using `f \<in> G`
   507       using `f \<in> G`
   508       by (intro ereal_add_strict_mono) (auto intro!: SUP_upper2 positive_integral_positive)
   508       by (intro ereal_add_strict_mono) (auto intro!: SUP_upper2 nn_integral_nonneg)
   509     also have "\<dots> = integral\<^sup>P M ?f0" using f0_eq[OF sets.top] `A0 \<in> sets M` sets.sets_into_space
   509     also have "\<dots> = integral\<^sup>N M ?f0" using f0_eq[OF sets.top] `A0 \<in> sets M` sets.sets_into_space
   510       by (simp cong: positive_integral_cong)
   510       by (simp cong: nn_integral_cong)
   511     finally have "?y < integral\<^sup>P M ?f0" by simp
   511     finally have "?y < integral\<^sup>N M ?f0" by simp
   512     moreover from `?f0 \<in> G` have "integral\<^sup>P M ?f0 \<le> ?y" by (auto intro!: SUP_upper)
   512     moreover from `?f0 \<in> G` have "integral\<^sup>N M ?f0 \<le> ?y" by (auto intro!: SUP_upper)
   513     ultimately show False by auto
   513     ultimately show False by auto
   514   qed
   514   qed
   515   let ?f = "\<lambda>x. max 0 (f x)"
   515   let ?f = "\<lambda>x. max 0 (f x)"
   516   show ?thesis
   516   show ?thesis
   517   proof (intro bexI[of _ ?f] measure_eqI conjI)
   517   proof (intro bexI[of _ ?f] measure_eqI conjI)
   518     show "sets (density M ?f) = sets N"
   518     show "sets (density M ?f) = sets N"
   519       by (simp add: sets_eq)
   519       by (simp add: sets_eq)
   520     fix A assume A: "A\<in>sets (density M ?f)"
   520     fix A assume A: "A\<in>sets (density M ?f)"
   521     then show "emeasure (density M ?f) A = emeasure N A"
   521     then show "emeasure (density M ?f) A = emeasure N A"
   522       using `f \<in> G` A upper_bound[THEN bspec, of A] N.emeasure_eq_measure[of A]
   522       using `f \<in> G` A upper_bound[THEN bspec, of A] N.emeasure_eq_measure[of A]
   523       by (cases "integral\<^sup>P M (?F A)")
   523       by (cases "integral\<^sup>N M (?F A)")
   524          (auto intro!: antisym simp add: emeasure_density G_def emeasure_M density_max_0[symmetric])
   524          (auto intro!: antisym simp add: emeasure_density G_def emeasure_M density_max_0[symmetric])
   525   qed auto
   525   qed auto
   526 qed
   526 qed
   527 
   527 
   528 lemma (in finite_measure) split_space_into_finite_sets_and_rest:
   528 lemma (in finite_measure) split_space_into_finite_sets_and_rest:
   667   let ?N = "\<lambda>i. density N (indicator (Q i))" and ?M = "\<lambda>i. density M (indicator (Q i))"
   667   let ?N = "\<lambda>i. density N (indicator (Q i))" and ?M = "\<lambda>i. density M (indicator (Q i))"
   668   have "\<forall>i. \<exists>f\<in>borel_measurable (?M i). (\<forall>x. 0 \<le> f x) \<and> density (?M i) f = ?N i"
   668   have "\<forall>i. \<exists>f\<in>borel_measurable (?M i). (\<forall>x. 0 \<le> f x) \<and> density (?M i) f = ?N i"
   669   proof (intro allI finite_measure.Radon_Nikodym_finite_measure)
   669   proof (intro allI finite_measure.Radon_Nikodym_finite_measure)
   670     fix i
   670     fix i
   671     from Q show "finite_measure (?M i)"
   671     from Q show "finite_measure (?M i)"
   672       by (auto intro!: finite_measureI cong: positive_integral_cong
   672       by (auto intro!: finite_measureI cong: nn_integral_cong
   673                simp add: emeasure_density subset_eq sets_eq)
   673                simp add: emeasure_density subset_eq sets_eq)
   674     from Q have "emeasure (?N i) (space N) = emeasure N (Q i)"
   674     from Q have "emeasure (?N i) (space N) = emeasure N (Q i)"
   675       by (simp add: sets_eq[symmetric] emeasure_density subset_eq cong: positive_integral_cong)
   675       by (simp add: sets_eq[symmetric] emeasure_density subset_eq cong: nn_integral_cong)
   676     with Q_fin show "finite_measure (?N i)"
   676     with Q_fin show "finite_measure (?N i)"
   677       by (auto intro!: finite_measureI)
   677       by (auto intro!: finite_measureI)
   678     show "sets (?N i) = sets (?M i)" by (simp add: sets_eq)
   678     show "sets (?N i) = sets (?M i)" by (simp add: sets_eq)
   679     have [measurable]: "\<And>A. A \<in> sets M \<Longrightarrow> A \<in> sets N" by (simp add: sets_eq)
   679     have [measurable]: "\<And>A. A \<in> sets M \<Longrightarrow> A \<in> sets N" by (simp add: sets_eq)
   680     show "absolutely_continuous (?M i) (?N i)"
   680     show "absolutely_continuous (?M i) (?N i)"
   686   obtain f where borel: "\<And>i. f i \<in> borel_measurable M" "\<And>i x. 0 \<le> f i x"
   686   obtain f where borel: "\<And>i. f i \<in> borel_measurable M" "\<And>i x. 0 \<le> f i x"
   687     and f_density: "\<And>i. density (?M i) (f i) = ?N i"
   687     and f_density: "\<And>i. density (?M i) (f i) = ?N i"
   688     by force
   688     by force
   689   { fix A i assume A: "A \<in> sets M"
   689   { fix A i assume A: "A \<in> sets M"
   690     with Q borel have "(\<integral>\<^sup>+x. f i x * indicator (Q i \<inter> A) x \<partial>M) = emeasure (density (?M i) (f i)) A"
   690     with Q borel have "(\<integral>\<^sup>+x. f i x * indicator (Q i \<inter> A) x \<partial>M) = emeasure (density (?M i) (f i)) A"
   691       by (auto simp add: emeasure_density positive_integral_density subset_eq
   691       by (auto simp add: emeasure_density nn_integral_density subset_eq
   692                intro!: positive_integral_cong split: split_indicator)
   692                intro!: nn_integral_cong split: split_indicator)
   693     also have "\<dots> = emeasure N (Q i \<inter> A)"
   693     also have "\<dots> = emeasure N (Q i \<inter> A)"
   694       using A Q by (simp add: f_density emeasure_restricted subset_eq sets_eq)
   694       using A Q by (simp add: f_density emeasure_restricted subset_eq sets_eq)
   695     finally have "emeasure N (Q i \<inter> A) = (\<integral>\<^sup>+x. f i x * indicator (Q i \<inter> A) x \<partial>M)" .. }
   695     finally have "emeasure N (Q i \<inter> A) = (\<integral>\<^sup>+x. f i x * indicator (Q i \<inter> A) x \<partial>M)" .. }
   696   note integral_eq = this
   696   note integral_eq = this
   697   let ?f = "\<lambda>x. (\<Sum>i. f i x * indicator (Q i) x) + \<infinity> * indicator Q0 x"
   697   let ?f = "\<lambda>x. (\<Sum>i. f i x * indicator (Q i) x) + \<infinity> * indicator Q0 x"
   707       have Qi: "\<And>i. Q i \<in> sets M" using Q by auto
   707       have Qi: "\<And>i. Q i \<in> sets M" using Q by auto
   708       have [intro,simp]: "\<And>i. (\<lambda>x. f i x * indicator (Q i \<inter> A) x) \<in> borel_measurable M"
   708       have [intro,simp]: "\<And>i. (\<lambda>x. f i x * indicator (Q i \<inter> A) x) \<in> borel_measurable M"
   709         "\<And>i. AE x in M. 0 \<le> f i x * indicator (Q i \<inter> A) x"
   709         "\<And>i. AE x in M. 0 \<le> f i x * indicator (Q i \<inter> A) x"
   710         using borel Qi Q0(1) `A \<in> sets M` by (auto intro!: borel_measurable_ereal_times)
   710         using borel Qi Q0(1) `A \<in> sets M` by (auto intro!: borel_measurable_ereal_times)
   711       have "(\<integral>\<^sup>+x. ?f x * indicator A x \<partial>M) = (\<integral>\<^sup>+x. (\<Sum>i. f i x * indicator (Q i \<inter> A) x) + \<infinity> * indicator (Q0 \<inter> A) x \<partial>M)"
   711       have "(\<integral>\<^sup>+x. ?f x * indicator A x \<partial>M) = (\<integral>\<^sup>+x. (\<Sum>i. f i x * indicator (Q i \<inter> A) x) + \<infinity> * indicator (Q0 \<inter> A) x \<partial>M)"
   712         using borel by (intro positive_integral_cong) (auto simp: indicator_def)
   712         using borel by (intro nn_integral_cong) (auto simp: indicator_def)
   713       also have "\<dots> = (\<integral>\<^sup>+x. (\<Sum>i. f i x * indicator (Q i \<inter> A) x) \<partial>M) + \<infinity> * emeasure M (Q0 \<inter> A)"
   713       also have "\<dots> = (\<integral>\<^sup>+x. (\<Sum>i. f i x * indicator (Q i \<inter> A) x) \<partial>M) + \<infinity> * emeasure M (Q0 \<inter> A)"
   714         using borel Qi Q0(1) `A \<in> sets M`
   714         using borel Qi Q0(1) `A \<in> sets M`
   715         by (subst positive_integral_add) (auto simp del: ereal_infty_mult
   715         by (subst nn_integral_add) (auto simp del: ereal_infty_mult
   716             simp add: positive_integral_cmult_indicator sets.Int intro!: suminf_0_le)
   716             simp add: nn_integral_cmult_indicator sets.Int intro!: suminf_0_le)
   717       also have "\<dots> = (\<Sum>i. N (Q i \<inter> A)) + \<infinity> * emeasure M (Q0 \<inter> A)"
   717       also have "\<dots> = (\<Sum>i. N (Q i \<inter> A)) + \<infinity> * emeasure M (Q0 \<inter> A)"
   718         by (subst integral_eq[OF `A \<in> sets M`], subst positive_integral_suminf) auto
   718         by (subst integral_eq[OF `A \<in> sets M`], subst nn_integral_suminf) auto
   719       finally have "(\<integral>\<^sup>+x. ?f x * indicator A x \<partial>M) = (\<Sum>i. N (Q i \<inter> A)) + \<infinity> * emeasure M (Q0 \<inter> A)" .
   719       finally have "(\<integral>\<^sup>+x. ?f x * indicator A x \<partial>M) = (\<Sum>i. N (Q i \<inter> A)) + \<infinity> * emeasure M (Q0 \<inter> A)" .
   720       moreover have "(\<Sum>i. N (Q i \<inter> A)) = N ((\<Union>i. Q i) \<inter> A)"
   720       moreover have "(\<Sum>i. N (Q i \<inter> A)) = N ((\<Union>i. Q i) \<inter> A)"
   721         using Q Q_sets `A \<in> sets M`
   721         using Q Q_sets `A \<in> sets M`
   722         by (subst suminf_emeasure) (auto simp: disjoint_family_on_def sets_eq)
   722         by (subst suminf_emeasure) (auto simp: disjoint_family_on_def sets_eq)
   723       moreover have "\<infinity> * emeasure M (Q0 \<inter> A) = N (Q0 \<inter> A)"
   723       moreover have "\<infinity> * emeasure M (Q0 \<inter> A) = N (Q0 \<inter> A)"
   740 lemma (in sigma_finite_measure) Radon_Nikodym:
   740 lemma (in sigma_finite_measure) Radon_Nikodym:
   741   assumes ac: "absolutely_continuous M N" assumes sets_eq: "sets N = sets M"
   741   assumes ac: "absolutely_continuous M N" assumes sets_eq: "sets N = sets M"
   742   shows "\<exists>f \<in> borel_measurable M. (\<forall>x. 0 \<le> f x) \<and> density M f = N"
   742   shows "\<exists>f \<in> borel_measurable M. (\<forall>x. 0 \<le> f x) \<and> density M f = N"
   743 proof -
   743 proof -
   744   from Ex_finite_integrable_function
   744   from Ex_finite_integrable_function
   745   obtain h where finite: "integral\<^sup>P M h \<noteq> \<infinity>" and
   745   obtain h where finite: "integral\<^sup>N M h \<noteq> \<infinity>" and
   746     borel: "h \<in> borel_measurable M" and
   746     borel: "h \<in> borel_measurable M" and
   747     nn: "\<And>x. 0 \<le> h x" and
   747     nn: "\<And>x. 0 \<le> h x" and
   748     pos: "\<And>x. x \<in> space M \<Longrightarrow> 0 < h x" and
   748     pos: "\<And>x. x \<in> space M \<Longrightarrow> 0 < h x" and
   749     "\<And>x. x \<in> space M \<Longrightarrow> h x < \<infinity>" by auto
   749     "\<And>x. x \<in> space M \<Longrightarrow> h x < \<infinity>" by auto
   750   let ?T = "\<lambda>A. (\<integral>\<^sup>+x. h x * indicator A x \<partial>M)"
   750   let ?T = "\<lambda>A. (\<integral>\<^sup>+x. h x * indicator A x \<partial>M)"
   751   let ?MT = "density M h"
   751   let ?MT = "density M h"
   752   from borel finite nn interpret T: finite_measure ?MT
   752   from borel finite nn interpret T: finite_measure ?MT
   753     by (auto intro!: finite_measureI cong: positive_integral_cong simp: emeasure_density)
   753     by (auto intro!: finite_measureI cong: nn_integral_cong simp: emeasure_density)
   754   have "absolutely_continuous ?MT N" "sets N = sets ?MT"
   754   have "absolutely_continuous ?MT N" "sets N = sets ?MT"
   755   proof (unfold absolutely_continuous_def, safe)
   755   proof (unfold absolutely_continuous_def, safe)
   756     fix A assume "A \<in> null_sets ?MT"
   756     fix A assume "A \<in> null_sets ?MT"
   757     with borel have "A \<in> sets M" "AE x in M. x \<in> A \<longrightarrow> h x \<le> 0"
   757     with borel have "A \<in> sets M" "AE x in M. x \<in> A \<longrightarrow> h x \<le> 0"
   758       by (auto simp add: null_sets_density_iff)
   758       by (auto simp add: null_sets_density_iff)
   772 subsection {* Uniqueness of densities *}
   772 subsection {* Uniqueness of densities *}
   773 
   773 
   774 lemma finite_density_unique:
   774 lemma finite_density_unique:
   775   assumes borel: "f \<in> borel_measurable M" "g \<in> borel_measurable M"
   775   assumes borel: "f \<in> borel_measurable M" "g \<in> borel_measurable M"
   776   assumes pos: "AE x in M. 0 \<le> f x" "AE x in M. 0 \<le> g x"
   776   assumes pos: "AE x in M. 0 \<le> f x" "AE x in M. 0 \<le> g x"
   777   and fin: "integral\<^sup>P M f \<noteq> \<infinity>"
   777   and fin: "integral\<^sup>N M f \<noteq> \<infinity>"
   778   shows "density M f = density M g \<longleftrightarrow> (AE x in M. f x = g x)"
   778   shows "density M f = density M g \<longleftrightarrow> (AE x in M. f x = g x)"
   779 proof (intro iffI ballI)
   779 proof (intro iffI ballI)
   780   fix A assume eq: "AE x in M. f x = g x"
   780   fix A assume eq: "AE x in M. f x = g x"
   781   with borel show "density M f = density M g"
   781   with borel show "density M f = density M g"
   782     by (auto intro: density_cong)
   782     by (auto intro: density_cong)
   784   let ?P = "\<lambda>f A. \<integral>\<^sup>+ x. f x * indicator A x \<partial>M"
   784   let ?P = "\<lambda>f A. \<integral>\<^sup>+ x. f x * indicator A x \<partial>M"
   785   assume "density M f = density M g"
   785   assume "density M f = density M g"
   786   with borel have eq: "\<forall>A\<in>sets M. ?P f A = ?P g A"
   786   with borel have eq: "\<forall>A\<in>sets M. ?P f A = ?P g A"
   787     by (simp add: emeasure_density[symmetric])
   787     by (simp add: emeasure_density[symmetric])
   788   from this[THEN bspec, OF sets.top] fin
   788   from this[THEN bspec, OF sets.top] fin
   789   have g_fin: "integral\<^sup>P M g \<noteq> \<infinity>" by (simp cong: positive_integral_cong)
   789   have g_fin: "integral\<^sup>N M g \<noteq> \<infinity>" by (simp cong: nn_integral_cong)
   790   { fix f g assume borel: "f \<in> borel_measurable M" "g \<in> borel_measurable M"
   790   { fix f g assume borel: "f \<in> borel_measurable M" "g \<in> borel_measurable M"
   791       and pos: "AE x in M. 0 \<le> f x" "AE x in M. 0 \<le> g x"
   791       and pos: "AE x in M. 0 \<le> f x" "AE x in M. 0 \<le> g x"
   792       and g_fin: "integral\<^sup>P M g \<noteq> \<infinity>" and eq: "\<forall>A\<in>sets M. ?P f A = ?P g A"
   792       and g_fin: "integral\<^sup>N M g \<noteq> \<infinity>" and eq: "\<forall>A\<in>sets M. ?P f A = ?P g A"
   793     let ?N = "{x\<in>space M. g x < f x}"
   793     let ?N = "{x\<in>space M. g x < f x}"
   794     have N: "?N \<in> sets M" using borel by simp
   794     have N: "?N \<in> sets M" using borel by simp
   795     have "?P g ?N \<le> integral\<^sup>P M g" using pos
   795     have "?P g ?N \<le> integral\<^sup>N M g" using pos
   796       by (intro positive_integral_mono_AE) (auto split: split_indicator)
   796       by (intro nn_integral_mono_AE) (auto split: split_indicator)
   797     then have Pg_fin: "?P g ?N \<noteq> \<infinity>" using g_fin by auto
   797     then have Pg_fin: "?P g ?N \<noteq> \<infinity>" using g_fin by auto
   798     have "?P (\<lambda>x. (f x - g x)) ?N = (\<integral>\<^sup>+x. f x * indicator ?N x - g x * indicator ?N x \<partial>M)"
   798     have "?P (\<lambda>x. (f x - g x)) ?N = (\<integral>\<^sup>+x. f x * indicator ?N x - g x * indicator ?N x \<partial>M)"
   799       by (auto intro!: positive_integral_cong simp: indicator_def)
   799       by (auto intro!: nn_integral_cong simp: indicator_def)
   800     also have "\<dots> = ?P f ?N - ?P g ?N"
   800     also have "\<dots> = ?P f ?N - ?P g ?N"
   801     proof (rule positive_integral_diff)
   801     proof (rule nn_integral_diff)
   802       show "(\<lambda>x. f x * indicator ?N x) \<in> borel_measurable M" "(\<lambda>x. g x * indicator ?N x) \<in> borel_measurable M"
   802       show "(\<lambda>x. f x * indicator ?N x) \<in> borel_measurable M" "(\<lambda>x. g x * indicator ?N x) \<in> borel_measurable M"
   803         using borel N by auto
   803         using borel N by auto
   804       show "AE x in M. g x * indicator ?N x \<le> f x * indicator ?N x"
   804       show "AE x in M. g x * indicator ?N x \<le> f x * indicator ?N x"
   805            "AE x in M. 0 \<le> g x * indicator ?N x"
   805            "AE x in M. 0 \<le> g x * indicator ?N x"
   806         using pos by (auto split: split_indicator)
   806         using pos by (auto split: split_indicator)
   807     qed fact
   807     qed fact
   808     also have "\<dots> = 0"
   808     also have "\<dots> = 0"
   809       unfolding eq[THEN bspec, OF N] using positive_integral_positive[of M] Pg_fin by auto
   809       unfolding eq[THEN bspec, OF N] using nn_integral_nonneg[of M] Pg_fin by auto
   810     finally have "AE x in M. f x \<le> g x"
   810     finally have "AE x in M. f x \<le> g x"
   811       using pos borel positive_integral_PInf_AE[OF borel(2) g_fin]
   811       using pos borel nn_integral_PInf_AE[OF borel(2) g_fin]
   812       by (subst (asm) positive_integral_0_iff_AE)
   812       by (subst (asm) nn_integral_0_iff_AE)
   813          (auto split: split_indicator simp: not_less ereal_minus_le_iff) }
   813          (auto split: split_indicator simp: not_less ereal_minus_le_iff) }
   814   from this[OF borel pos g_fin eq] this[OF borel(2,1) pos(2,1) fin] eq
   814   from this[OF borel pos g_fin eq] this[OF borel(2,1) pos(2,1) fin] eq
   815   show "AE x in M. f x = g x" by auto
   815   show "AE x in M. f x = g x" by auto
   816 qed
   816 qed
   817 
   817 
   854       proof (rule null_sets_UN)
   854       proof (rule null_sets_UN)
   855         fix i ::nat have "?A i \<in> sets M"
   855         fix i ::nat have "?A i \<in> sets M"
   856           using borel Q0(1) by auto
   856           using borel Q0(1) by auto
   857         have "?N (?A i) \<le> (\<integral>\<^sup>+x. (i::ereal) * indicator (?A i) x \<partial>M)"
   857         have "?N (?A i) \<le> (\<integral>\<^sup>+x. (i::ereal) * indicator (?A i) x \<partial>M)"
   858           unfolding eq[OF `?A i \<in> sets M`]
   858           unfolding eq[OF `?A i \<in> sets M`]
   859           by (auto intro!: positive_integral_mono simp: indicator_def)
   859           by (auto intro!: nn_integral_mono simp: indicator_def)
   860         also have "\<dots> = i * emeasure M (?A i)"
   860         also have "\<dots> = i * emeasure M (?A i)"
   861           using `?A i \<in> sets M` by (auto intro!: positive_integral_cmult_indicator)
   861           using `?A i \<in> sets M` by (auto intro!: nn_integral_cmult_indicator)
   862         also have "\<dots> < \<infinity>" using emeasure_real[of "?A i"] by simp
   862         also have "\<dots> < \<infinity>" using emeasure_real[of "?A i"] by simp
   863         finally have "?N (?A i) \<noteq> \<infinity>" by simp
   863         finally have "?N (?A i) \<noteq> \<infinity>" by simp
   864         then show "?A i \<in> null_sets M" using in_Q0[OF `?A i \<in> sets M`] `?A i \<in> sets M` by auto
   864         then show "?A i \<in> null_sets M" using in_Q0[OF `?A i \<in> sets M`] `?A i \<in> sets M` by auto
   865       qed
   865       qed
   866       also have "(\<Union>i. ?A i) = Q0 \<inter> {x\<in>space M. f x \<noteq> \<infinity>}"
   866       also have "(\<Union>i. ?A i) = Q0 \<inter> {x\<in>space M. f x \<noteq> \<infinity>}"
   886   assumes f': "f' \<in> borel_measurable M" "AE x in M. 0 \<le> f' x"
   886   assumes f': "f' \<in> borel_measurable M" "AE x in M. 0 \<le> f' x"
   887   assumes density_eq: "density M f = density M f'"
   887   assumes density_eq: "density M f = density M f'"
   888   shows "AE x in M. f x = f' x"
   888   shows "AE x in M. f x = f' x"
   889 proof -
   889 proof -
   890   obtain h where h_borel: "h \<in> borel_measurable M"
   890   obtain h where h_borel: "h \<in> borel_measurable M"
   891     and fin: "integral\<^sup>P M h \<noteq> \<infinity>" and pos: "\<And>x. x \<in> space M \<Longrightarrow> 0 < h x \<and> h x < \<infinity>" "\<And>x. 0 \<le> h x"
   891     and fin: "integral\<^sup>N M h \<noteq> \<infinity>" and pos: "\<And>x. x \<in> space M \<Longrightarrow> 0 < h x \<and> h x < \<infinity>" "\<And>x. 0 \<le> h x"
   892     using Ex_finite_integrable_function by auto
   892     using Ex_finite_integrable_function by auto
   893   then have h_nn: "AE x in M. 0 \<le> h x" by auto
   893   then have h_nn: "AE x in M. 0 \<le> h x" by auto
   894   let ?H = "density M h"
   894   let ?H = "density M h"
   895   interpret h: finite_measure ?H
   895   interpret h: finite_measure ?H
   896     using fin h_borel pos
   896     using fin h_borel pos
   897     by (intro finite_measureI) (simp cong: positive_integral_cong emeasure_density add: fin)
   897     by (intro finite_measureI) (simp cong: nn_integral_cong emeasure_density add: fin)
   898   let ?fM = "density M f"
   898   let ?fM = "density M f"
   899   let ?f'M = "density M f'"
   899   let ?f'M = "density M f'"
   900   { fix A assume "A \<in> sets M"
   900   { fix A assume "A \<in> sets M"
   901     then have "{x \<in> space M. h x * indicator A x \<noteq> 0} = A"
   901     then have "{x \<in> space M. h x * indicator A x \<noteq> 0} = A"
   902       using pos(1) sets.sets_into_space by (force simp: indicator_def)
   902       using pos(1) sets.sets_into_space by (force simp: indicator_def)
   903     then have "(\<integral>\<^sup>+x. h x * indicator A x \<partial>M) = 0 \<longleftrightarrow> A \<in> null_sets M"
   903     then have "(\<integral>\<^sup>+x. h x * indicator A x \<partial>M) = 0 \<longleftrightarrow> A \<in> null_sets M"
   904       using h_borel `A \<in> sets M` h_nn by (subst positive_integral_0_iff) auto }
   904       using h_borel `A \<in> sets M` h_nn by (subst nn_integral_0_iff) auto }
   905   note h_null_sets = this
   905   note h_null_sets = this
   906   { fix A assume "A \<in> sets M"
   906   { fix A assume "A \<in> sets M"
   907     have "(\<integral>\<^sup>+x. f x * (h x * indicator A x) \<partial>M) = (\<integral>\<^sup>+x. h x * indicator A x \<partial>?fM)"
   907     have "(\<integral>\<^sup>+x. f x * (h x * indicator A x) \<partial>M) = (\<integral>\<^sup>+x. h x * indicator A x \<partial>?fM)"
   908       using `A \<in> sets M` h_borel h_nn f f'
   908       using `A \<in> sets M` h_borel h_nn f f'
   909       by (intro positive_integral_density[symmetric]) auto
   909       by (intro nn_integral_density[symmetric]) auto
   910     also have "\<dots> = (\<integral>\<^sup>+x. h x * indicator A x \<partial>?f'M)"
   910     also have "\<dots> = (\<integral>\<^sup>+x. h x * indicator A x \<partial>?f'M)"
   911       by (simp_all add: density_eq)
   911       by (simp_all add: density_eq)
   912     also have "\<dots> = (\<integral>\<^sup>+x. f' x * (h x * indicator A x) \<partial>M)"
   912     also have "\<dots> = (\<integral>\<^sup>+x. f' x * (h x * indicator A x) \<partial>M)"
   913       using `A \<in> sets M` h_borel h_nn f f'
   913       using `A \<in> sets M` h_borel h_nn f f'
   914       by (intro positive_integral_density) auto
   914       by (intro nn_integral_density) auto
   915     finally have "(\<integral>\<^sup>+x. h x * (f x * indicator A x) \<partial>M) = (\<integral>\<^sup>+x. h x * (f' x * indicator A x) \<partial>M)"
   915     finally have "(\<integral>\<^sup>+x. h x * (f x * indicator A x) \<partial>M) = (\<integral>\<^sup>+x. h x * (f' x * indicator A x) \<partial>M)"
   916       by (simp add: ac_simps)
   916       by (simp add: ac_simps)
   917     then have "(\<integral>\<^sup>+x. (f x * indicator A x) \<partial>?H) = (\<integral>\<^sup>+x. (f' x * indicator A x) \<partial>?H)"
   917     then have "(\<integral>\<^sup>+x. (f x * indicator A x) \<partial>?H) = (\<integral>\<^sup>+x. (f' x * indicator A x) \<partial>?H)"
   918       using `A \<in> sets M` h_borel h_nn f f'
   918       using `A \<in> sets M` h_borel h_nn f f'
   919       by (subst (asm) (1 2) positive_integral_density[symmetric]) auto }
   919       by (subst (asm) (1 2) nn_integral_density[symmetric]) auto }
   920   then have "AE x in ?H. f x = f' x" using h_borel h_nn f f'
   920   then have "AE x in ?H. f x = f' x" using h_borel h_nn f f'
   921     by (intro h.density_unique_finite_measure absolutely_continuous_AE[of M])
   921     by (intro h.density_unique_finite_measure absolutely_continuous_AE[of M])
   922        (auto simp add: AE_density)
   922        (auto simp add: AE_density)
   923   then show "AE x in M. f x = f' x"
   923   then show "AE x in M. f x = f' x"
   924     unfolding eventually_ae_filter using h_borel pos
   924     unfolding eventually_ae_filter using h_borel pos
   973     (is "sigma_finite_measure ?N \<longleftrightarrow> _")
   973     (is "sigma_finite_measure ?N \<longleftrightarrow> _")
   974 proof
   974 proof
   975   assume "sigma_finite_measure ?N"
   975   assume "sigma_finite_measure ?N"
   976   then interpret N: sigma_finite_measure ?N .
   976   then interpret N: sigma_finite_measure ?N .
   977   from N.Ex_finite_integrable_function obtain h where
   977   from N.Ex_finite_integrable_function obtain h where
   978     h: "h \<in> borel_measurable M" "integral\<^sup>P ?N h \<noteq> \<infinity>" and
   978     h: "h \<in> borel_measurable M" "integral\<^sup>N ?N h \<noteq> \<infinity>" and
   979     h_nn: "\<And>x. 0 \<le> h x" and
   979     h_nn: "\<And>x. 0 \<le> h x" and
   980     fin: "\<forall>x\<in>space M. 0 < h x \<and> h x < \<infinity>" by auto
   980     fin: "\<forall>x\<in>space M. 0 < h x \<and> h x < \<infinity>" by auto
   981   have "AE x in M. f x * h x \<noteq> \<infinity>"
   981   have "AE x in M. f x * h x \<noteq> \<infinity>"
   982   proof (rule AE_I')
   982   proof (rule AE_I')
   983     have "integral\<^sup>P ?N h = (\<integral>\<^sup>+x. f x * h x \<partial>M)" using f h h_nn
   983     have "integral\<^sup>N ?N h = (\<integral>\<^sup>+x. f x * h x \<partial>M)" using f h h_nn
   984       by (auto intro!: positive_integral_density)
   984       by (auto intro!: nn_integral_density)
   985     then have "(\<integral>\<^sup>+x. f x * h x \<partial>M) \<noteq> \<infinity>"
   985     then have "(\<integral>\<^sup>+x. f x * h x \<partial>M) \<noteq> \<infinity>"
   986       using h(2) by simp
   986       using h(2) by simp
   987     then show "(\<lambda>x. f x * h x) -` {\<infinity>} \<inter> space M \<in> null_sets M"
   987     then show "(\<lambda>x. f x * h x) -` {\<infinity>} \<inter> space M \<in> null_sets M"
   988       using f h(1) by (auto intro!: positive_integral_PInf borel_measurable_vimage)
   988       using f h(1) by (auto intro!: nn_integral_PInf borel_measurable_vimage)
   989   qed auto
   989   qed auto
   990   then show "AE x in M. f x \<noteq> \<infinity>"
   990   then show "AE x in M. f x \<noteq> \<infinity>"
   991     using fin by (auto elim!: AE_Ball_mp)
   991     using fin by (auto elim!: AE_Ball_mp)
   992 next
   992 next
   993   assume AE: "AE x in M. f x \<noteq> \<infinity>"
   993   assume AE: "AE x in M. f x \<noteq> \<infinity>"
  1034     have "(\<integral>\<^sup>+x. f x * indicator (A i \<inter> Q j) x \<partial>M) \<noteq> \<infinity>"
  1034     have "(\<integral>\<^sup>+x. f x * indicator (A i \<inter> Q j) x \<partial>M) \<noteq> \<infinity>"
  1035     proof (cases i)
  1035     proof (cases i)
  1036       case 0
  1036       case 0
  1037       have "AE x in M. f x * indicator (A i \<inter> Q j) x = 0"
  1037       have "AE x in M. f x * indicator (A i \<inter> Q j) x = 0"
  1038         using AE by (auto simp: A_def `i = 0`)
  1038         using AE by (auto simp: A_def `i = 0`)
  1039       from positive_integral_cong_AE[OF this] show ?thesis by simp
  1039       from nn_integral_cong_AE[OF this] show ?thesis by simp
  1040     next
  1040     next
  1041       case (Suc n)
  1041       case (Suc n)
  1042       then have "(\<integral>\<^sup>+x. f x * indicator (A i \<inter> Q j) x \<partial>M) \<le>
  1042       then have "(\<integral>\<^sup>+x. f x * indicator (A i \<inter> Q j) x \<partial>M) \<le>
  1043         (\<integral>\<^sup>+x. (Suc n :: ereal) * indicator (Q j) x \<partial>M)"
  1043         (\<integral>\<^sup>+x. (Suc n :: ereal) * indicator (Q j) x \<partial>M)"
  1044         by (auto intro!: positive_integral_mono simp: indicator_def A_def real_eq_of_nat)
  1044         by (auto intro!: nn_integral_mono simp: indicator_def A_def real_eq_of_nat)
  1045       also have "\<dots> = Suc n * emeasure M (Q j)"
  1045       also have "\<dots> = Suc n * emeasure M (Q j)"
  1046         using Q by (auto intro!: positive_integral_cmult_indicator)
  1046         using Q by (auto intro!: nn_integral_cmult_indicator)
  1047       also have "\<dots> < \<infinity>"
  1047       also have "\<dots> < \<infinity>"
  1048         using Q by (auto simp: real_eq_of_nat[symmetric])
  1048         using Q by (auto simp: real_eq_of_nat[symmetric])
  1049       finally show ?thesis by simp
  1049       finally show ?thesis by simp
  1050     qed
  1050     qed
  1051     then show "emeasure ?N (?A n) \<noteq> \<infinity>"
  1051     then show "emeasure ?N (?A n) \<noteq> \<infinity>"
  1107 
  1107 
  1108 lemma (in sigma_finite_measure) density_RN_deriv:
  1108 lemma (in sigma_finite_measure) density_RN_deriv:
  1109   "absolutely_continuous M N \<Longrightarrow> sets N = sets M \<Longrightarrow> density M (RN_deriv M N) = N"
  1109   "absolutely_continuous M N \<Longrightarrow> sets N = sets M \<Longrightarrow> density M (RN_deriv M N) = N"
  1110   by (metis RN_derivI Radon_Nikodym)
  1110   by (metis RN_derivI Radon_Nikodym)
  1111 
  1111 
  1112 lemma (in sigma_finite_measure) RN_deriv_positive_integral:
  1112 lemma (in sigma_finite_measure) RN_deriv_nn_integral:
  1113   assumes N: "absolutely_continuous M N" "sets N = sets M"
  1113   assumes N: "absolutely_continuous M N" "sets N = sets M"
  1114     and f: "f \<in> borel_measurable M"
  1114     and f: "f \<in> borel_measurable M"
  1115   shows "integral\<^sup>P N f = (\<integral>\<^sup>+x. RN_deriv M N x * f x \<partial>M)"
  1115   shows "integral\<^sup>N N f = (\<integral>\<^sup>+x. RN_deriv M N x * f x \<partial>M)"
  1116 proof -
  1116 proof -
  1117   have "integral\<^sup>P N f = integral\<^sup>P (density M (RN_deriv M N)) f"
  1117   have "integral\<^sup>N N f = integral\<^sup>N (density M (RN_deriv M N)) f"
  1118     using N by (simp add: density_RN_deriv)
  1118     using N by (simp add: density_RN_deriv)
  1119   also have "\<dots> = (\<integral>\<^sup>+x. RN_deriv M N x * f x \<partial>M)"
  1119   also have "\<dots> = (\<integral>\<^sup>+x. RN_deriv M N x * f x \<partial>M)"
  1120     using f by (simp add: positive_integral_density RN_deriv_nonneg)
  1120     using f by (simp add: nn_integral_density RN_deriv_nonneg)
  1121   finally show ?thesis by simp
  1121   finally show ?thesis by simp
  1122 qed
  1122 qed
  1123 
  1123 
  1124 lemma null_setsD_AE: "N \<in> null_sets M \<Longrightarrow> AE x in M. x \<notin> N"
  1124 lemma null_setsD_AE: "N \<in> null_sets M \<Longrightarrow> AE x in M. x \<notin> N"
  1125   using AE_iff_null_sets[of N M] by auto
  1125   using AE_iff_null_sets[of N M] by auto
  1259     using RN by auto
  1259     using RN by auto
  1260 
  1260 
  1261   have "N (?RN \<infinity>) = (\<integral>\<^sup>+ x. RN_deriv M N x * indicator (?RN \<infinity>) x \<partial>M)"
  1261   have "N (?RN \<infinity>) = (\<integral>\<^sup>+ x. RN_deriv M N x * indicator (?RN \<infinity>) x \<partial>M)"
  1262     using RN(1,3) by (subst RN(2)[symmetric]) (auto simp: emeasure_density)
  1262     using RN(1,3) by (subst RN(2)[symmetric]) (auto simp: emeasure_density)
  1263   also have "\<dots> = (\<integral>\<^sup>+ x. \<infinity> * indicator (?RN \<infinity>) x \<partial>M)"
  1263   also have "\<dots> = (\<integral>\<^sup>+ x. \<infinity> * indicator (?RN \<infinity>) x \<partial>M)"
  1264     by (intro positive_integral_cong) (auto simp: indicator_def)
  1264     by (intro nn_integral_cong) (auto simp: indicator_def)
  1265   also have "\<dots> = \<infinity> * emeasure M (?RN \<infinity>)"
  1265   also have "\<dots> = \<infinity> * emeasure M (?RN \<infinity>)"
  1266     using RN by (intro positive_integral_cmult_indicator) auto
  1266     using RN by (intro nn_integral_cmult_indicator) auto
  1267   finally have eq: "N (?RN \<infinity>) = \<infinity> * emeasure M (?RN \<infinity>)" .
  1267   finally have eq: "N (?RN \<infinity>) = \<infinity> * emeasure M (?RN \<infinity>)" .
  1268   moreover
  1268   moreover
  1269   have "emeasure M (?RN \<infinity>) = 0"
  1269   have "emeasure M (?RN \<infinity>) = 0"
  1270   proof (rule ccontr)
  1270   proof (rule ccontr)
  1271     assume "emeasure M {x \<in> space M. RN_deriv M N x = \<infinity>} \<noteq> 0"
  1271     assume "emeasure M {x \<in> space M. RN_deriv M N x = \<infinity>} \<noteq> 0"
  1285     using RN by (auto intro: real_of_ereal_pos)
  1285     using RN by (auto intro: real_of_ereal_pos)
  1286 
  1286 
  1287   have "N (?RN 0) = (\<integral>\<^sup>+ x. RN_deriv M N x * indicator (?RN 0) x \<partial>M)"
  1287   have "N (?RN 0) = (\<integral>\<^sup>+ x. RN_deriv M N x * indicator (?RN 0) x \<partial>M)"
  1288     using RN(1,3) by (subst RN(2)[symmetric]) (auto simp: emeasure_density)
  1288     using RN(1,3) by (subst RN(2)[symmetric]) (auto simp: emeasure_density)
  1289   also have "\<dots> = (\<integral>\<^sup>+ x. 0 \<partial>M)"
  1289   also have "\<dots> = (\<integral>\<^sup>+ x. 0 \<partial>M)"
  1290     by (intro positive_integral_cong) (auto simp: indicator_def)
  1290     by (intro nn_integral_cong) (auto simp: indicator_def)
  1291   finally have "AE x in N. RN_deriv M N x \<noteq> 0"
  1291   finally have "AE x in N. RN_deriv M N x \<noteq> 0"
  1292     using RN by (subst AE_iff_measurable[OF _ refl]) (auto simp: ac cong: sets_eq_imp_space_eq)
  1292     using RN by (subst AE_iff_measurable[OF _ refl]) (auto simp: ac cong: sets_eq_imp_space_eq)
  1293   with RN(3) eq show "AE x in N. 0 < real (RN_deriv M N x)"
  1293   with RN(3) eq show "AE x in N. 0 < real (RN_deriv M N x)"
  1294     by (auto simp: zero_less_real_of_ereal le_less)
  1294     by (auto simp: zero_less_real_of_ereal le_less)
  1295 qed
  1295 qed
  1299   and x: "{x} \<in> sets M"
  1299   and x: "{x} \<in> sets M"
  1300   shows "N {x} = RN_deriv M N x * emeasure M {x}"
  1300   shows "N {x} = RN_deriv M N x * emeasure M {x}"
  1301 proof -
  1301 proof -
  1302   from `{x} \<in> sets M`
  1302   from `{x} \<in> sets M`
  1303   have "density M (RN_deriv M N) {x} = (\<integral>\<^sup>+w. RN_deriv M N x * indicator {x} w \<partial>M)"
  1303   have "density M (RN_deriv M N) {x} = (\<integral>\<^sup>+w. RN_deriv M N x * indicator {x} w \<partial>M)"
  1304     by (auto simp: indicator_def emeasure_density intro!: positive_integral_cong)
  1304     by (auto simp: indicator_def emeasure_density intro!: nn_integral_cong)
  1305   with x density_RN_deriv[OF ac] RN_deriv_nonneg[of M N] show ?thesis
  1305   with x density_RN_deriv[OF ac] RN_deriv_nonneg[of M N] show ?thesis
  1306     by (auto simp: positive_integral_cmult_indicator)
  1306     by (auto simp: nn_integral_cmult_indicator)
  1307 qed
  1307 qed
  1308 
  1308 
  1309 end
  1309 end