# HG changeset patch # User hoelzl # Date 1402661300 -7200 # Node ID 19b7ace1c5da23081c70e1786c66d1b892de384f # Parent f51985ebd15262e48b58fed4cee8d12e676301a1 properties of normal distributed random variables (by Sudeep Kanav) diff -r f51985ebd152 -r 19b7ace1c5da CONTRIBUTORS --- a/CONTRIBUTORS Fri Jun 13 07:05:01 2014 +0200 +++ b/CONTRIBUTORS Fri Jun 13 14:08:20 2014 +0200 @@ -12,7 +12,7 @@ Waldmeister, etc.). * June 2014: Sudeep Kanav, TUM, and Johannes Hölzl, TUM - Various properties of Erlang and exponentially distributed random variables. + Various properties of exponentially, Erlang, and normal distributed random variables. * May 2014: Cezary Kaliszyk, University of Innsbruck, and Jasmin Blanchette, TUM SML-based engines for MaSh. diff -r f51985ebd152 -r 19b7ace1c5da NEWS --- a/NEWS Fri Jun 13 07:05:01 2014 +0200 +++ b/NEWS Fri Jun 13 14:08:20 2014 +0200 @@ -757,6 +757,9 @@ * Renamed abbreviation integral\<^sup>P to integral\<^sup>N. + - Formalized properties about exponentially, Erlang, and normal distributed + random variables. + * Library/Kleene-Algebra was removed because AFP/Kleene_Algebra subsumes it. *** Scala *** diff -r f51985ebd152 -r 19b7ace1c5da src/HOL/Probability/Distributions.thy --- a/src/HOL/Probability/Distributions.thy Fri Jun 13 07:05:01 2014 +0200 +++ b/src/HOL/Probability/Distributions.thy Fri Jun 13 14:08:20 2014 +0200 @@ -5,9 +5,120 @@ header {* Properties of Various Distributions *} theory Distributions - imports Probability_Measure Convolution + imports Convolution Information begin +lemma nn_integral_even_function: + fixes f :: "real \ ereal" + assumes [measurable]: "f \ borel_measurable borel" + assumes even: "\x. f x = f (- x)" + shows "(\\<^sup>+x. f x \lborel) = 2 * (\\<^sup>+x. f x * indicator {0 ..} x \lborel)" +proof - + def f' \ "\x. max 0 (f x)" + have [measurable]: "f' \ borel_measurable borel" + by (simp add: f'_def) + + { fix x :: ereal have "2 * x = x + x" + by (cases x) auto } + note two_mult = this + + have "(\\<^sup>+x. f x \lborel) = (\\<^sup>+x. f' x \lborel)" + unfolding f'_def nn_integral_max_0 .. + also have "\ = (\\<^sup>+x. f' x * indicator {0 ..} x + f' x * indicator {.. 0} x \lborel)" + by (intro nn_integral_cong_AE AE_I[where N="{0}"]) (auto split: split_indicator_asm) + also have "\ = (\\<^sup>+x. f' x * indicator {0 ..} x \lborel) + (\\<^sup>+x. f' x * indicator {.. 0} x \lborel)" + by (intro nn_integral_add) (auto simp: f'_def) + also have "(\\<^sup>+x. f' x * indicator {.. 0} x \lborel) = (\\<^sup>+x. f' x * indicator {0 ..} x \lborel)" + using even + by (subst nn_integral_real_affine[where c="-1" and t=0]) + (auto simp: f'_def one_ereal_def[symmetric] split: split_indicator intro!: nn_integral_cong) + also have "(\\<^sup>+x. f' x * indicator {0 ..} x \lborel) = (\\<^sup>+x. f x * indicator {0 ..} x \lborel)" + unfolding f'_def by (subst (2) nn_integral_max_0[symmetric]) (auto intro!: nn_integral_cong split: split_indicator split_max) + finally show ?thesis + unfolding two_mult by simp +qed + +lemma filterlim_power2_at_top[intro]: "filterlim (\(x::real). x\<^sup>2) at_top at_top" + by (auto intro!: filterlim_at_top_mult_at_top filterlim_ident simp: power2_eq_square) + +lemma distributed_integrable_var: + fixes X :: "'a \ real" + shows "distributed M lborel X (\x. ereal (f x)) \ integrable lborel (\x. f x * x) \ integrable M X" + using distributed_integrable[of M lborel X f "\x. x"] by simp + +lemma (in ordered_comm_monoid_add) setsum_pos: + "finite I \ I \ {} \ (\i. i \ I \ 0 < f i) \ 0 < setsum f I" + by (induct I rule: finite_ne_induct) (auto intro: add_pos_pos) + +lemma ln_sqrt: "0 < x \ ln (sqrt x) = ln x / 2" + by (simp add: ln_powr powr_numeral ln_powr[symmetric] mult_commute) + +lemma distr_cong: "M = K \ sets N = sets L \ (\x. x \ space M \ f x = g x) \ distr M N f = distr K L g" + using sets_eq_imp_space_eq[of N L] by (simp add: distr_def Int_def cong: rev_conj_cong) + +lemma AE_borel_affine: + fixes P :: "real \ bool" + shows "c \ 0 \ Measurable.pred borel P \ AE x in lborel. P x \ AE x in lborel. P (t + c * x)" + by (subst lborel_real_affine[where t="- t / c" and c="1 / c"]) + (simp_all add: AE_density AE_distr_iff field_simps) + +lemma density_distr: + assumes [measurable]: "f \ borel_measurable N" "X \ measurable M N" + shows "density (distr M N X) f = distr (density M (\x. f (X x))) N X" + by (intro measure_eqI) + (auto simp add: emeasure_density nn_integral_distr emeasure_distr + split: split_indicator intro!: nn_integral_cong) + +lemma ereal_mult_indicator: "ereal (x * indicator A y) = ereal x * indicator A y" + by (simp split: split_indicator) + +lemma (in prob_space) distributed_affine: + fixes f :: "real \ ereal" + assumes f: "distributed M lborel X f" + assumes c: "c \ 0" + shows "distributed M lborel (\x. t + c * X x) (\x. f ((x - t) / c) / \c\)" + unfolding distributed_def +proof safe + have [measurable]: "f \ borel_measurable borel" + using f by (simp add: distributed_def) + have [measurable]: "X \ borel_measurable M" + using f by (simp add: distributed_def) + + show "(\x. f ((x - t) / c) / \c\) \ borel_measurable lborel" + by simp + show "random_variable lborel (\x. t + c * X x)" + by simp + + have "AE x in lborel. 0 \ f x" + using f by (simp add: distributed_def) + from AE_borel_affine[OF _ _ this, where c="1/c" and t="- t / c"] c + show "AE x in lborel. 0 \ f ((x - t) / c) / ereal \c\" + by (auto simp add: field_simps) + + have eq: "\x. ereal \c\ * (f x / ereal \c\) = f x" + using c by (simp add: divide_ereal_def mult_ac one_ereal_def[symmetric]) + + have "density lborel f = distr M lborel X" + using f by (simp add: distributed_def) + with c show "distr M lborel (\x. t + c * X x) = density lborel (\x. f ((x - t) / c) / ereal \c\)" + by (subst (2) lborel_real_affine[where c="c" and t="t"]) + (simp_all add: density_density_eq density_distr distr_distr field_simps eq cong: distr_cong) +qed + +lemma (in prob_space) distributed_affineI: + fixes f :: "real \ ereal" + assumes f: "distributed M lborel (\x. (X x - t) / c) (\x. \c\ * f (x * c + t))" + assumes c: "c \ 0" + shows "distributed M lborel X f" +proof - + have eq: "\x. f x * ereal \c\ / ereal \c\ = f x" + using c by (simp add: divide_ereal_def mult_ac one_ereal_def[symmetric]) + + show ?thesis + using distributed_affine[OF f c, where t=t] c + by (simp add: field_simps eq) +qed + lemma measure_lebesgue_Icc: "measure lebesgue {a .. b} = (if a \ b then b - a else 0)" by (auto simp: measure_def) @@ -571,6 +682,35 @@ from erlang_distributed_setsum[OF assms(1,2) this assms(3,4)] show ?thesis by simp qed +lemma (in information_space) entropy_exponential: + assumes D: "distributed M lborel X (exponential_density l)" + shows "entropy b lborel X = log b (exp 1 / l)" +proof - + have l[simp, arith]: "0 < l" by (rule exponential_distributed_params[OF D]) + + have [simp]: "integrable lborel (exponential_density l)" + using distributed_integrable[OF D, of "\_. 1"] by simp + + have [simp]: "integral\<^sup>L lborel (exponential_density l) = 1" + using distributed_integral[OF D, of "\_. 1"] by (simp add: prob_space) + + have [simp]: "integrable lborel (\x. exponential_density l x * x)" + using erlang_ith_moment_integrable[OF l D, of 1] distributed_integrable[OF D, of "\x. x"] by simp + + have [simp]: "integral\<^sup>L lborel (\x. exponential_density l x * x) = 1 / l" + using erlang_ith_moment[OF l D, of 1] distributed_integral[OF D, of "\x. x"] by simp + + have "entropy b lborel X = - (\ x. exponential_density l x * log b (exponential_density l x) \lborel)" + using D by (rule entropy_distr) + also have "(\ x. exponential_density l x * log b (exponential_density l x) \lborel) = + (\ x. (ln l * exponential_density l x - l * (exponential_density l x * x)) / ln b \lborel)" + by (intro integral_cong) (auto simp: log_def ln_mult exponential_density_def field_simps) + also have "\ = (ln l - 1) / ln b" + by simp + finally show ?thesis + by (simp add: log_def divide_simps ln_div) +qed + subsection {* Uniform distribution *} lemma uniform_distrI: @@ -755,4 +895,561 @@ finally show "(\x. x\<^sup>2 * ?D x \lborel) = (b - a)\<^sup>2 / 12" . qed fact +subsection {* Normal distribution *} + +definition normal_density :: "real \ real \ real \ real" where + "normal_density \ \ x = 1 / sqrt (2 * pi * \\<^sup>2) * exp (-(x - \)\<^sup>2/ (2 * \\<^sup>2))" + +abbreviation std_normal_density :: "real \ real" where + "std_normal_density \ normal_density 0 1" + +lemma std_normal_density_def: "std_normal_density x = (1 / sqrt (2 * pi)) * exp (- x\<^sup>2 / 2)" + unfolding normal_density_def by simp + +lemma borel_measurable_normal_density[measurable]: "normal_density \ \ \ borel_measurable borel" + by (auto simp: normal_density_def[abs_def]) + +lemma nn_integral_gaussian: "(\\<^sup>+x. (exp (- x\<^sup>2)) \lborel) = sqrt pi" +proof - + let ?pI = "\f. (\\<^sup>+s. f (s::real) * indicator {0..} s \lborel)" + let ?gauss = "\x. exp (- x\<^sup>2)" + + have "?pI ?gauss * ?pI ?gauss = ?pI (\s. ?pI (\x. ereal (x * exp (-x\<^sup>2 * (1 + s\<^sup>2)))))" + proof- + let ?ff= "\(x, s). ((x * exp (- x\<^sup>2 * (1 + s\<^sup>2)) * indicator {0<..} s * indicator {0<..} x)) :: real" + + have *: "?pI ?gauss = (\\<^sup>+x. ?gauss x * indicator {0<..} x \lborel)" + by (intro nn_integral_cong_AE AE_I[where N="{0}"]) (auto split: split_indicator) + + have "?pI ?gauss * ?pI ?gauss = (\\<^sup>+x. \\<^sup>+s. ?ff (x, s) \lborel \lborel)" + unfolding * + apply (auto simp: nn_integral_nonneg nn_integral_cmult[symmetric]) + apply (auto intro!: nn_integral_cong split:split_indicator) + apply (auto simp: nn_integral_multc[symmetric]) + apply (subst nn_integral_real_affine[where t="0" and c="x"]) + by (auto simp: mult_exp_exp nn_integral_cmult[symmetric] field_simps zero_less_mult_iff + intro!: nn_integral_cong split:split_indicator) + also have "... = \\<^sup>+ (s::real). \\<^sup>+ (x::real). ?ff (x, s) \lborel \lborel" + by (rule lborel_pair.Fubini[symmetric]) auto + also have "... = ?pI (\s. ?pI (\x. ereal (x * exp (-x\<^sup>2 * (1 + s\<^sup>2)))))" + by (rule nn_integral_cong_AE) (auto intro!: nn_integral_cong_AE AE_I[where N="{0}"] split: split_indicator_asm) + finally show ?thesis + by simp + qed + also have "\ = ?pI (\s. ereal (1 / (2 * (1 + s\<^sup>2))))" + proof (intro nn_integral_cong ereal_right_mult_cong) + fix s :: real show "?pI (\x. ereal (x * exp (-x\<^sup>2 * (1 + s\<^sup>2)))) = ereal (1 / (2 * (1 + s\<^sup>2)))" + proof (subst nn_integral_FTC_atLeast) + have "((\a. - (exp (- (a\<^sup>2 * (1 + s\<^sup>2))) / (2 + 2 * s\<^sup>2))) ---> (- (0 / (2 + 2 * s\<^sup>2)))) at_top" + apply (intro tendsto_intros filterlim_compose[OF exp_at_bot] filterlim_compose[OF filterlim_uminus_at_bot_at_top]) + apply (subst mult_commute) + by (auto intro!: filterlim_tendsto_pos_mult_at_top filterlim_at_top_mult_at_top[OF filterlim_ident filterlim_ident] + simp: add_pos_nonneg power2_eq_square add_nonneg_eq_0_iff) + then show "((\a. - (exp (- a\<^sup>2 - s\<^sup>2 * a\<^sup>2) / (2 + 2 * s\<^sup>2))) ---> 0) at_top" + by (simp add: field_simps) + qed (auto intro!: derivative_eq_intros simp: field_simps add_nonneg_eq_0_iff) + qed + also have "... = ereal (pi / 4)" + proof (subst nn_integral_FTC_atLeast) + show "((\a. arctan a / 2) ---> (pi / 2) / 2 ) at_top" + by (intro tendsto_intros) (simp_all add: tendsto_arctan_at_top) + qed (auto intro!: derivative_eq_intros simp: add_nonneg_eq_0_iff field_simps power2_eq_square) + finally have "?pI ?gauss^2 = pi / 4" + by (simp add: power2_eq_square) + then have "?pI ?gauss = sqrt (pi / 4)" + using power_eq_iff_eq_base[of 2 "real (?pI ?gauss)" "sqrt (pi / 4)"] + nn_integral_nonneg[of lborel "\x. ?gauss x * indicator {0..} x"] + by (cases "?pI ?gauss") auto + then show ?thesis + by (subst nn_integral_even_function) (auto simp add: real_sqrt_divide) +qed + +lemma has_bochner_integral_gaussian: "has_bochner_integral lborel (\x. exp (- x\<^sup>2)) (sqrt pi)" + by (auto intro!: nn_integral_gaussian has_bochner_integral_nn_integral) + +lemma integral_gaussian: "(\x. (exp (- x\<^sup>2)) \lborel) = sqrt pi" + using has_bochner_integral_gaussian by (rule has_bochner_integral_integral_eq) + +lemma integrable_gaussian[intro]: "integrable lborel (\x. exp (- x\<^sup>2)::real)" + using has_bochner_integral_gaussian by rule + +context + fixes \ :: real + assumes \_pos[arith]: "0 < \" +begin + +lemma nn_integral_normal_density: "(\\<^sup>+x. normal_density \ \ x \lborel) = 1" + unfolding normal_density_def + apply (subst times_ereal.simps(1)[symmetric],subst nn_integral_cmult) + apply auto + apply (subst nn_integral_real_affine[where t=\ and c="(sqrt 2) * \"]) + by (auto simp: power_mult_distrib nn_integral_gaussian real_sqrt_mult one_ereal_def) + +lemma + shows normal_density_pos: "\x. 0 < normal_density \ \ x" + and normal_density_nonneg: "\x. 0 \ normal_density \ \ x" + by (auto simp: normal_density_def) + +lemma integrable_normal[intro]: "integrable lborel (normal_density \ \)" + by (auto intro!: integrableI_nn_integral_finite simp: nn_integral_normal_density normal_density_nonneg) + +lemma integral_normal_density[simp]: "(\x. normal_density \ \ x \lborel) = 1" + by (simp add: integral_eq_nn_integral normal_density_nonneg nn_integral_normal_density) + +lemma prob_space_normal_density: + "prob_space (density lborel (normal_density \ \))" (is "prob_space ?D") + proof qed (simp add: emeasure_density nn_integral_normal_density) + end + +lemma (in prob_space) normal_density_affine: + assumes X: "distributed M lborel X (normal_density \ \)" + assumes [simp, arith]: "0 < \" "\ \ 0" + shows "distributed M lborel (\x. \ + \ * X x) (normal_density (\ + \ * \) (\\\ * \))" +proof - + have eq: "\x. \\\ * normal_density (\ + \ * \) (\\\ * \) (x * \ + \) = + normal_density \ \ x" + by (simp add: normal_density_def real_sqrt_mult field_simps) + (simp add: power2_eq_square field_simps) + show ?thesis + by (rule distributed_affineI[OF _ `\ \ 0`, where t=\]) (simp_all add: eq X) +qed + +lemma (in prob_space) normal_standard_normal_convert: + assumes pos_var[simp, arith]: "0 < \" + shows "distributed M lborel X (normal_density \ \) = distributed M lborel (\x. (X x - \) / \) std_normal_density" +proof auto + assume "distributed M lborel X (\x. ereal (normal_density \ \ x))" + then have "distributed M lborel (\x. -\ / \ + (1/\) * X x) (\x. ereal (normal_density (-\ / \ + (1/\)* \) (\1/\\ * \) x))" + by(rule normal_density_affine) auto + + then show "distributed M lborel (\x. (X x - \) / \) (\x. ereal (std_normal_density x))" + by (simp add: diff_divide_distrib[symmetric] field_simps) +next + assume *: "distributed M lborel (\x. (X x - \) / \) (\x. ereal (std_normal_density x))" + have "distributed M lborel (\x. \ + \ * ((X x - \) / \)) (\x. ereal (normal_density \ \\\ x))" + using normal_density_affine[OF *, of \ \] by simp + then show "distributed M lborel X (\x. ereal (normal_density \ \ x))" by simp +qed + +lemma conv_normal_density_zero_mean: + assumes [simp, arith]: "0 < \" "0 < \" + shows "(\x. \\<^sup>+y. ereal (normal_density 0 \ (x - y) * normal_density 0 \ y) \lborel) = + normal_density 0 (sqrt (\\<^sup>2 + \\<^sup>2))" (is "?LHS = ?RHS") +proof - + def \' \ "\\<^sup>2" and \' \ "\\<^sup>2" + then have [simp, arith]: "0 < \'" "0 < \'" + by simp_all + let ?\ = "sqrt ((\' * \') / (\' + \'))" + have sqrt: "(sqrt (2 * pi * (\' + \')) * sqrt (2 * pi * (\' * \') / (\' + \'))) = + (sqrt (2 * pi * \') * sqrt (2 * pi * \'))" + by (subst power_eq_iff_eq_base[symmetric, where n=2]) + (simp_all add: real_sqrt_mult[symmetric] power2_eq_square) + have "?LHS = + (\x. \\<^sup>+y. ereal((normal_density 0 (sqrt (\' + \')) x) * normal_density (\' * x / (\' + \')) ?\ y) \lborel)" + apply (intro ext nn_integral_cong) + apply (simp add: normal_density_def \'_def[symmetric] \'_def[symmetric] sqrt mult_exp_exp) + apply (simp add: divide_simps power2_eq_square) + apply (simp add: field_simps) + done + + also have "... = + (\x. (normal_density 0 (sqrt (\\<^sup>2 + \\<^sup>2)) x) * \\<^sup>+y. ereal( normal_density (\\<^sup>2* x / (\\<^sup>2 + \\<^sup>2)) ?\ y) \lborel)" + by (subst nn_integral_cmult[symmetric]) (auto simp: \'_def \'_def normal_density_def) + + also have "... = (\x. (normal_density 0 (sqrt (\\<^sup>2 + \\<^sup>2)) x))" + by (subst nn_integral_normal_density) (auto simp: sum_power2_gt_zero_iff) + + finally show ?thesis by fast +qed + +lemma conv_std_normal_density: + "(\x. \\<^sup>+y. ereal (std_normal_density (x - y) * std_normal_density y) \lborel) = + (normal_density 0 (sqrt 2))" + by (subst conv_normal_density_zero_mean) simp_all + +lemma (in prob_space) sum_indep_normal: + assumes indep: "indep_var borel X borel Y" + assumes pos_var[arith]: "0 < \" "0 < \" + assumes normalX[simp]: "distributed M lborel X (normal_density \ \)" + assumes normalY[simp]: "distributed M lborel Y (normal_density \ \)" + shows "distributed M lborel (\x. X x + Y x) (normal_density (\ + \) (sqrt (\\<^sup>2 + \\<^sup>2)))" +proof - + have ind[simp]: "indep_var borel (\x. - \ + X x) borel (\x. - \ + Y x)" + proof - + have "indep_var borel ( (\x. -\ + x) o X) borel ((\x. - \ + x) o Y)" + by (auto intro!: indep_var_compose assms) + then show ?thesis by (simp add: o_def) + qed + + have "distributed M lborel (\x. -\ + 1 * X x) (normal_density (- \ + 1 * \) (\1\ * \))" + by(rule normal_density_affine[OF normalX pos_var(1), of 1 "-\"]) simp + then have 1[simp]: "distributed M lborel (\x. - \ + X x) (normal_density 0 \)" by simp + + have "distributed M lborel (\x. -\ + 1 * Y x) (normal_density (- \ + 1 * \) (\1\ * \))" + by(rule normal_density_affine[OF normalY pos_var(2), of 1 "-\"]) simp + then have 2[simp]: "distributed M lborel (\x. - \ + Y x) (normal_density 0 \)" by simp + + have *: "distributed M lborel (\x. (- \ + X x) + (- \ + Y x)) (\x. ereal (normal_density 0 (sqrt (\\<^sup>2 + \\<^sup>2)) x))" + using distributed_convolution[OF ind 1 2] conv_normal_density_zero_mean[OF pos_var] by simp + + have "distributed M lborel (\x. \ + \ + 1 * (- \ + X x + (- \ + Y x))) + (\x. ereal (normal_density (\ + \ + 1 * 0) (\1\ * sqrt (\\<^sup>2 + \\<^sup>2)) x))" + by (rule normal_density_affine[OF *, of 1 "\ + \"]) (auto simp: add_pos_pos) + + then show ?thesis by auto +qed + +lemma (in prob_space) diff_indep_normal: + assumes indep[simp]: "indep_var borel X borel Y" + assumes [simp, arith]: "0 < \" "0 < \" + assumes normalX[simp]: "distributed M lborel X (normal_density \ \)" + assumes normalY[simp]: "distributed M lborel Y (normal_density \ \)" + shows "distributed M lborel (\x. X x - Y x) (normal_density (\ - \) (sqrt (\\<^sup>2 + \\<^sup>2)))" +proof - + have "distributed M lborel (\x. 0 + - 1 * Y x) (\x. ereal (normal_density (0 + - 1 * \) (\- 1\ * \) x))" + by(rule normal_density_affine, auto) + then have [simp]:"distributed M lborel (\x. - Y x) (\x. ereal (normal_density (- \) \ x))" by simp + + have "distributed M lborel (\x. X x + (- Y x)) (normal_density (\ + - \) (sqrt (\\<^sup>2 + \\<^sup>2)))" + apply (rule sum_indep_normal) + apply (rule indep_var_compose[unfolded comp_def, of borel _ borel _ "\x. x" _ "\x. - x"]) + apply simp_all + done + then show ?thesis by simp +qed + +lemma (in prob_space) setsum_indep_normal: + assumes "finite I" "I \ {}" "indep_vars (\i. borel) X I" + assumes "\i. i \ I \ 0 < \ i" + assumes normal: "\i. i \ I \ distributed M lborel (X i) (normal_density (\ i) (\ i))" + shows "distributed M lborel (\x. \i\I. X i x) (normal_density (\i\I. \ i) (sqrt (\i\I. (\ i)\<^sup>2)))" + using assms +proof (induct I rule: finite_ne_induct) + case (singleton i) then show ?case by (simp add : abs_of_pos) +next + case (insert i I) + then have 1: "distributed M lborel (\x. (X i x) + (\i\I. X i x)) + (normal_density (\ i + setsum \ I) (sqrt ((\ i)\<^sup>2 + (sqrt (\i\I. (\ i)\<^sup>2))\<^sup>2)))" + apply (intro sum_indep_normal indep_vars_setsum insert real_sqrt_gt_zero) + apply (auto intro: indep_vars_subset intro!: setsum_pos) + apply fastforce + done + have 2: "(\x. (X i x)+ (\i\I. X i x)) = (\x. (\j\insert i I. X j x))" + "\ i + setsum \ I = setsum \ (insert i I)" + using insert by auto + + have 3: "(sqrt ((\ i)\<^sup>2 + (sqrt (\i\I. (\ i)\<^sup>2))\<^sup>2)) = (sqrt (\i\insert i I. (\ i)\<^sup>2))" + using insert by (simp add: setsum_nonneg) + + show ?case using 1 2 3 by simp +qed + +lemma nn_integral_x_exp_x_square: "(\\<^sup>+x. ereal (x * exp (- x\<^sup>2 )) \lborel) = ereal 1 / 2" + and nn_integral_x_exp_x_square_indicator: "(\\<^sup>+x. ereal( x * exp (-x\<^sup>2 )) * indicator {0..} x \lborel) = ereal 1 / 2" +proof - + let ?F = "\x. - exp (-x\<^sup>2 ) / 2" + + have 1: "(\\<^sup>+x. ereal (x * exp (- x\<^sup>2)) * indicator {0..} x \lborel) =ereal( 0 - ?F 0)" + apply (rule nn_integral_FTC_atLeast) + apply (auto intro!: derivative_eq_intros) + apply (rule tendsto_minus_cancel) + apply (simp add: field_simps) + proof - + have "((\(x::real). exp (- x\<^sup>2) / 2) ---> 0 / 2) at_top" + apply (intro tendsto_divide filterlim_compose[OF exp_at_bot] filterlim_compose[OF filterlim_uminus_at_bot_at_top]) + apply auto + done + then show "((\(x::real). exp (- x\<^sup>2) / 2) ---> 0) at_top" by simp + qed + + also have 2: "(\\<^sup>+x. ereal (x * exp (- x\<^sup>2)) * indicator {0..} x \lborel) = integral\<^sup>N lborel (\x. ereal (x * exp (- x\<^sup>2)))" + by (subst(2) nn_integral_max_0[symmetric]) + (auto intro!: nn_integral_cong split: split_indicator simp: max_def zero_le_mult_iff) + finally show "(\\<^sup>+x. ereal (x * exp (- x\<^sup>2)) \lborel) = ereal 1 / 2" by auto + + show "(\\<^sup>+x. ereal (x * exp (- x\<^sup>2)) * indicator {0..} x \lborel) = ereal 1 / 2" using 1 by auto +qed + +lemma borel_integral_x_times_standard_normal[intro]: "(\x. std_normal_density x * x \lborel) = 0" + and borel_integrable_x_times_standard_normal[intro]: "integrable lborel (\x. std_normal_density x * x)" + and borel_integral_x_times_standard_normal'[intro]: "(\x. x * std_normal_density x \lborel) = 0" + and borel_integrable_x_times_standard_normal'[intro]: "integrable lborel (\x. x * std_normal_density x)" +proof - + have 0: "(\\<^sup>+x. ereal (x * std_normal_density x) \lborel) = \\<^sup>+x. ereal (x * std_normal_density x) * indicator {0..} x \lborel" + apply (subst nn_integral_max_0[symmetric]) + unfolding max_def std_normal_density_def + apply (auto intro!: nn_integral_cong split:split_indicator simp: zero_le_divide_iff zero_le_mult_iff) + apply (metis not_le pi_gt_zero) + done + + have 1: "(\\<^sup>+x. ereal (- (x * std_normal_density x)) \lborel) = \\<^sup>+x. ereal (x * std_normal_density x) * indicator {0..} x \lborel" + apply (subst(2) nn_integral_real_affine[where c = "-1" and t = 0]) + apply(auto simp: std_normal_density_def split: split_indicator) + apply(subst nn_integral_max_0[symmetric]) + unfolding max_def std_normal_density_def + apply (auto intro!: nn_integral_cong split: split_indicator + simp: divide_le_0_iff mult_le_0_iff one_ereal_def[symmetric]) + apply (metis not_le pi_gt_zero) + done + + have 2: "sqrt pi / sqrt 2 * (\\<^sup>+x. ereal (x * std_normal_density x) * indicator {0..} x \lborel) = integral\<^sup>N lborel (\x. ereal (x * exp (- x\<^sup>2)))" + unfolding std_normal_density_def + apply (subst nn_integral_real_affine[where c = "sqrt 2" and t = 0]) + apply (auto simp: power_mult_distrib split: split_indicator) + apply (subst mult_assoc[symmetric]) + apply (subst nn_integral_cmult[symmetric]) + apply auto + apply (subst(2) nn_integral_max_0[symmetric]) + apply (auto intro!: nn_integral_cong split: split_indicator simp: max_def zero_le_mult_iff real_sqrt_mult) + done + + have *: "(\\<^sup>+x. ereal (x * std_normal_density x) * indicator {0..} x \lborel) = sqrt 2 / sqrt pi *(integral\<^sup>N lborel (\x. ereal (x * exp (- x\<^sup>2))))" + apply (subst 2[symmetric]) + apply (subst mult_assoc[symmetric]) + apply (auto simp: field_simps one_ereal_def[symmetric]) + done + + show "(\ x. x * std_normal_density x \lborel) = 0" "integrable lborel (\x. x * std_normal_density x)" + by (subst real_lebesgue_integral_def) + (auto simp: 0 1 * nn_integral_x_exp_x_square real_integrable_def) + + then show "(\ x. std_normal_density x * x \lborel) = 0" "integrable lborel (\x. std_normal_density x * x)" + by (simp_all add:mult_commute) +qed + +lemma (in prob_space) standard_normal_distributed_expectation: + assumes D: "distributed M lborel X std_normal_density " + shows "expectation X = 0" + by (auto simp: distributed_integral[OF D, of "\x. x", symmetric]) + +lemma (in prob_space) normal_distributed_expectation: + assumes pos_var[arith]: "0 < \" + assumes D: "distributed M lborel X (normal_density \ \)" + shows "expectation X = \" +proof - + def X' \ "\x. (X x - \) / \" + then have D1: "distributed M lborel X' std_normal_density" + by (simp add: normal_standard_normal_convert[OF pos_var, of X \, symmetric] D) + then have [simp]: "integrable M X'" + by (rule distributed_integrable_var) auto + + have "expectation X = expectation (\x. \ + \ * X' x)" + by (simp add: X'_def) + then show ?thesis + by (simp add: prob_space standard_normal_distributed_expectation[OF D1]) +qed + +lemma integral_xsquare_exp_xsquare: "(\ x. (x\<^sup>2 * exp (-x\<^sup>2 )) \lborel) = sqrt pi / 2" + and integrable_xsquare_exp_xsquare: "integrable lborel (\x. x\<^sup>2 * exp (- x\<^sup>2)::real)" +proof- + note filterlim_compose[OF exp_at_top, intro] filterlim_ident[intro] + + let ?f = "(\x. x * - exp (- x\<^sup>2) / 2 - 0 * - exp (- 0\<^sup>2) / 2 - + \ xa. 1 * (- exp (- xa\<^sup>2) / 2) * indicator {0..x} xa \lborel)::real\real" + let ?IFunc = "(\z. \x. (x\<^sup>2 * exp (- x\<^sup>2)) * indicator {0 .. z} x \lborel)::real\real" + + + from nn_integral_gaussian + have 1: "(\\<^sup>+xa. ereal (exp (- xa\<^sup>2)) * indicator {0..} xa \lborel) = ereal (sqrt pi) / ereal 2" + apply (subst (asm) nn_integral_even_function) + apply simp + apply simp + apply (cases "\\<^sup>+ xa. ereal (exp (- xa\<^sup>2)) * indicator {0..} xa \lborel") + apply auto + done + + then have I: "(\xa. exp (- xa\<^sup>2) * indicator {0..} xa \lborel) = sqrt pi / 2" + by (subst integral_eq_nn_integral) (auto simp: ereal_mult_indicator) + + have byparts: "?IFunc = (\z. (if z < 0 then 0 else ?f z))" + proof (intro HOL.ext, subst split_ifs, safe) + fix z::real assume [arith]:" \ z < 0 " + + have "?IFunc z = \x. (x * (x * exp (- x\<^sup>2))) * indicator {0 .. z} x \lborel" + by(auto intro!: integral_cong split: split_indicator simp: power2_eq_square) + + also have "... = (\x. x) z * (\x. - exp (- x\<^sup>2 ) / 2) z - (\x. x) 0 * (\x. - exp (- x\<^sup>2) / 2) 0 + - \x. 1 * ( - exp (- x\<^sup>2) / 2) * indicator {0 .. z} x \lborel" + by(rule integral_by_parts, auto intro!: derivative_eq_intros) + finally have "?IFunc z = ..." . + + then show "?IFunc z = ?f z" by simp + qed simp + + have [simp]: "(\y. \ x. x\<^sup>2 * exp (- x\<^sup>2) * indicator {0..} x * indicator {..y} x \lborel) = ?IFunc" + by(auto intro!: integral_cong split:split_indicator) + + have [intro]: "((\(x::real). x * exp (- x\<^sup>2) / 2) ---> 0) at_top" + proof - + have "((\(x::real). x * exp (- x\<^sup>2) / 2) ---> 0 / 2) at_top" + apply (intro tendsto_divide filterlim_compose[OF exp_at_bot] filterlim_compose[OF filterlim_uminus_at_bot_at_top]) + apply (auto simp: exp_minus inverse_eq_divide) + apply (rule lhospital_at_top_at_top[where f' = "\x. 1" and g' = "\x. 2 * x * exp (x\<^sup>2)"]) + apply (auto intro!: derivative_eq_intros eventually_elim1[OF eventually_gt_at_top, of 1]) + apply (subst inverse_eq_divide[symmetric]) + apply (rule tendsto_inverse_0_at_top) + apply (subst mult_assoc) + by (auto intro!: filterlim_tendsto_pos_mult_at_top[of "\x. 2" 2] filterlim_at_top_mult_at_top[OF filterlim_ident]) + + then show ?thesis by simp + qed + + have "((\x. (\y. (exp (- y\<^sup>2) * indicator {0..} y) * indicator {.. x} y \lborel) :: real) ---> \y. exp (- y\<^sup>2) * indicator {0..} y \lborel) at_top" + by (intro tendsto_integral_at_top integrable_mult_indicator) auto + also have "(\x. (\y. (exp (- y\<^sup>2) * indicator {0..} y) * indicator {.. x} y \lborel) :: real) = + (\x. (\y. exp (- y\<^sup>2) * indicator {0..x} y \lborel) :: real)" + by (auto simp: fun_eq_iff split: split_indicator intro!: integral_cong) + finally have *: "((\x. (\y. exp (- y\<^sup>2) * indicator {0..x} y \lborel) :: real) ---> \y. exp (- y\<^sup>2) * indicator {0..} y \lborel) at_top" + . + + have tends: "((\x. (\ xa. exp (- xa\<^sup>2) * indicator {0..x} xa \lborel) / 2) ---> (sqrt pi / 2) / 2) at_top" + apply (rule tendsto_divide) + apply (subst I[symmetric]) + apply (auto intro: *) + done + + have [intro]: "(?IFunc ---> sqrt pi / 4) at_top" + apply (simp add: byparts) + apply (subst filterlim_cong[where g = ?f]) + apply (auto simp: eventually_ge_at_top linorder_not_less) + proof - + have "((\x. (\ xa. exp (- xa\<^sup>2) * indicator {0..x} xa / 2 \lborel) - x * exp (- x\<^sup>2) / 2::real) ---> + (0 + sqrt pi / 4 - 0)) at_top" + apply (intro tendsto_diff) + apply auto + apply (subst divide_real_def) + using tends + by (auto intro!: integrable_mult_indicator) + then show "((\x. (\ xa. exp (- xa\<^sup>2) * indicator {0..x} xa \lborel) / 2 - x * exp (- x\<^sup>2) / 2) ---> sqrt pi / 4) at_top" by simp + qed + + have [intro]:"\y. integrable lborel (\x. x\<^sup>2 * exp (- x\<^sup>2) * indicator {0..} x * indicator {..y} x::real)" + apply (subst integrable_cong[where g = "\x. x\<^sup>2 * exp (- x\<^sup>2) * indicator {0..y} x" for y]) + by (auto intro!: borel_integrable_atLeastAtMost split:split_indicator) + + have **[intro]: "integrable lborel (\x. x\<^sup>2 * exp (- x\<^sup>2) * indicator {0..} x::real)" + by (rule integrable_monotone_convergence_at_top) auto + + have "(\x. x\<^sup>2 * exp (- x\<^sup>2) * indicator {0..} x \lborel) = sqrt pi / 4" + by (rule integral_monotone_convergence_at_top) auto + + then have "(\\<^sup>+x. ereal (x\<^sup>2 * exp (- x\<^sup>2)* indicator {0..} x) \lborel) = sqrt pi / 4" + by (subst nn_integral_eq_integral) auto + + then have ***: "(\\<^sup>+ x. ereal (x\<^sup>2 * exp (- x\<^sup>2)) \lborel) = sqrt pi / 2" + by (subst nn_integral_even_function) + (auto simp: real_sqrt_mult real_sqrt_divide ereal_mult_indicator) + + then show "(\ x. x\<^sup>2 * exp (- x\<^sup>2) \lborel) = sqrt pi / 2" + by (subst integral_eq_nn_integral) auto + + show "integrable lborel (\x. x\<^sup>2 * exp (- x\<^sup>2)::real)" + by (intro integrableI_nn_integral_finite[OF _ _ ***]) auto +qed + +lemma integral_xsquare_times_standard_normal[intro]: "(\ x. std_normal_density x * x\<^sup>2 \lborel) = 1" + and integrable_xsquare_times_standard_normal[intro]: "integrable lborel (\x. std_normal_density x * x\<^sup>2)" +proof - + have [intro]:"integrable lborel (\x. exp (- x\<^sup>2) * (2 * x\<^sup>2) / (sqrt 2 * sqrt pi))" + apply (subst integrable_cong[where g ="(\x. (2 * inverse (sqrt 2 * sqrt pi)) * (exp (- x\<^sup>2) * x\<^sup>2))"]) + by (auto intro!: integrable_xsquare_exp_xsquare simp: field_simps) + + have "(\ x. std_normal_density x * x\<^sup>2 \lborel) = (2 / sqrt pi) * \ x. x\<^sup>2 * exp (- x\<^sup>2) \lborel" + apply (subst integral_mult_right[symmetric]) + apply (rule integrable_xsquare_exp_xsquare) + unfolding std_normal_density_def + apply (subst lborel_integral_real_affine[where c = "sqrt 2" and t=0], simp_all) + unfolding integral_mult_right_zero[symmetric] integral_divide_zero[symmetric] + apply (intro integral_cong) + by (auto simp: power_mult_distrib real_sqrt_mult) + also have "... = 1" + by (subst integral_xsquare_exp_xsquare, auto) + finally show "(\ x. std_normal_density x * x\<^sup>2 \lborel) = 1" . + + show "integrable lborel (\x. std_normal_density x * x\<^sup>2)" + unfolding std_normal_density_def + apply (subst lborel_integrable_real_affine_iff[where c = "sqrt 2" and t=0, symmetric]) + by (auto simp: power_mult_distrib real_sqrt_mult) +qed + +lemma + assumes [arith]: "0 < \" + shows integral_xsquare_times_normal: "(\ x. normal_density \ \ x * (x - \)\<^sup>2 \lborel) = \\<^sup>2" + and integrable_xsquare_times_normal: "integrable lborel (\x. normal_density \ \ x * (x - \)\<^sup>2)" +proof - + have "(\ x. normal_density \ \ x * (x - \)\<^sup>2 \lborel) = \ * \ * \ x. std_normal_density x * x\<^sup>2 \lborel" + unfolding normal_density_def + apply (subst lborel_integral_real_affine[ where c = \ and t = \]) + apply (auto simp: power_mult_distrib) + unfolding integral_mult_right_zero[symmetric] integral_divide_zero[symmetric] + apply (intro integral_cong) + apply auto + unfolding normal_density_def + by (auto simp: real_sqrt_mult field_simps power2_eq_square[symmetric]) + + also have "... = \\<^sup>2" + by(auto simp: power2_eq_square[symmetric]) + + finally show "(\ x. normal_density \ \ x * (x - \)\<^sup>2 \lborel) = \\<^sup>2" . + + show "integrable lborel (\x. normal_density \ \ x * (x - \)\<^sup>2)" + unfolding normal_density_def + apply (subst lborel_integrable_real_affine_iff[ where c = \ and t = \, symmetric]) + apply auto + apply (auto simp: power_mult_distrib) + apply (subst integrable_cong[where g ="(\x. \ * (std_normal_density x * x\<^sup>2))"], auto) + unfolding std_normal_density_def + by (auto simp: field_simps real_sqrt_mult power2_eq_square[symmetric]) +qed + +lemma (in prob_space) standard_normal_distributed_variance: + fixes a b :: real + assumes D: "distributed M lborel X std_normal_density" + shows "variance X = 1" + apply (subst distributed_integral[OF D, of "(\x. (x - expectation X)\<^sup>2)", symmetric]) + by (auto simp: standard_normal_distributed_expectation[OF D]) + +lemma (in prob_space) normal_distributed_variance: + fixes a b :: real + assumes [simp, arith]: " 0 < \" + assumes D: "distributed M lborel X (normal_density \ \)" + shows "variance X = \\<^sup>2" +proof- + have *[intro]: "distributed M lborel (\x. (X x - \) / \) (\x. ereal (std_normal_density x))" + by (subst normal_standard_normal_convert[OF assms(1) , symmetric]) fact + + have "variance X = variance (\x. \ + \ * ((X x - \) / \) )" by simp + also have "... = \\<^sup>2 * 1" + apply (subst variance_affine) + apply (auto intro!: standard_normal_distributed_variance prob_space_normal_density + simp: distributed_integrable[OF *,of "\x. x", symmetric] + distributed_integrable[OF *,of "\x. x\<^sup>2", symmetric] variance_affine + simp del: integral_divide_zero) + done + + finally show ?thesis by simp +qed + +lemma (in information_space) entropy_normal_density: + assumes [arith]: "0 < \" + assumes D: "distributed M lborel X (normal_density \ \)" + shows "entropy b lborel X = log b (2 * pi * exp 1 * \\<^sup>2) / 2" +proof - + have "entropy b lborel X = - (\ x. normal_density \ \ x * log b (normal_density \ \ x) \lborel)" + using D by (rule entropy_distr) + also have "\ = - (\ x. normal_density \ \ x * (- ln (2 * pi * \\<^sup>2) - (x - \)\<^sup>2 / \\<^sup>2) / (2 * ln b) \lborel)" + by (intro arg_cong[where f="uminus"] integral_cong) + (auto simp: normal_density_def field_simps ln_mult log_def ln_div ln_sqrt) + also have "\ = - (\x. - (normal_density \ \ x * (ln (2 * pi * \\<^sup>2)) + (normal_density \ \ x * (x - \)\<^sup>2) / \\<^sup>2) / (2 * ln b) \lborel)" + by (intro arg_cong[where f="uminus"] integral_cong) (auto simp: divide_simps field_simps) + also have "\ = (\x. normal_density \ \ x * (ln (2 * pi * \\<^sup>2)) + (normal_density \ \ x * (x - \)\<^sup>2) / \\<^sup>2 \lborel) / (2 * ln b)" + by (simp del: minus_add_distrib) + also have "\ = (ln (2 * pi * \\<^sup>2) + 1) / (2 * ln b)" + by (simp add: integrable_xsquare_times_normal integrable_normal integral_xsquare_times_normal) + also have "\ = log b (2 * pi * exp 1 * \\<^sup>2) / 2" + by (simp add: log_def field_simps ln_mult) + finally show ?thesis . +qed + +end diff -r f51985ebd152 -r 19b7ace1c5da src/HOL/Probability/Information.thy --- a/src/HOL/Probability/Information.thy Fri Jun 13 07:05:01 2014 +0200 +++ b/src/HOL/Probability/Information.thy Fri Jun 13 14:08:20 2014 +0200 @@ -8,7 +8,6 @@ theory Information imports Independent_Family - Distributions "~~/src/HOL/Library/Convex" begin @@ -916,35 +915,6 @@ by (subst integral_mult_right) (auto simp: measure_def) qed -lemma (in information_space) entropy_exponential: - assumes D: "distributed M lborel X (exponential_density l)" - shows "entropy b lborel X = log b (exp 1 / l)" -proof - - have l[simp, arith]: "0 < l" by (rule exponential_distributed_params[OF D]) - - have [simp]: "integrable lborel (exponential_density l)" - using distributed_integrable[OF D, of "\_. 1"] by simp - - have [simp]: "integral\<^sup>L lborel (exponential_density l) = 1" - using distributed_integral[OF D, of "\_. 1"] by (simp add: prob_space) - - have [simp]: "integrable lborel (\x. exponential_density l x * x)" - using erlang_ith_moment_integrable[OF l D, of 1] distributed_integrable[OF D, of "\x. x"] by simp - - have [simp]: "integral\<^sup>L lborel (\x. exponential_density l x * x) = 1 / l" - using erlang_ith_moment[OF l D, of 1] distributed_integral[OF D, of "\x. x"] by simp - - have "entropy b lborel X = - (\ x. exponential_density l x * log b (exponential_density l x) \lborel)" - using D by (rule entropy_distr) - also have "(\ x. exponential_density l x * log b (exponential_density l x) \lborel) = - (\ x. (ln l * exponential_density l x - l * (exponential_density l x * x)) / ln b \lborel)" - by (intro integral_cong) (auto simp: log_def ln_mult exponential_density_def field_simps) - also have "\ = (ln l - 1) / ln b" - by simp - finally show ?thesis - by (simp add: log_def divide_simps ln_div) -qed - lemma (in information_space) entropy_simple_distributed: "simple_distributed M X f \ \(X) = - (\x\X`space M. f x * log b (f x))" by (subst entropy_distr[OF simple_distributed]) diff -r f51985ebd152 -r 19b7ace1c5da src/HOL/Probability/Probability.thy --- a/src/HOL/Probability/Probability.thy Fri Jun 13 07:05:01 2014 +0200 +++ b/src/HOL/Probability/Probability.thy Fri Jun 13 14:08:20 2014 +0200 @@ -6,7 +6,6 @@ Infinite_Product_Measure Projective_Limit Independent_Family - Information Distributions begin